I can now parse one thing

This commit is contained in:
greg 2015-12-25 02:03:11 -08:00
parent 247638c4db
commit 509ab80b9c
3 changed files with 60 additions and 18 deletions

View File

@ -57,10 +57,11 @@ impl ReplState for InterpreterState {
fn repl_handler(input: &str, state: &mut InterpreterState) -> String { fn repl_handler(input: &str, state: &mut InterpreterState) -> String {
if state.show_tokens { if state.show_tokens {
format!("Tokens: {:?}", tokenize(input)) println!("Tokens: {:?}", tokenize(input))
} else if state.show_parse{
format!("Parse: {:?}", parse(tokenize(input)))
} else {
format!("{:?}", parse(tokenize(input)))
} }
if state.show_parse {
println!("Parse: {:?}", parse(tokenize(input)))
}
format!("{:?}", parse(tokenize(input)))
} }

View File

@ -1,13 +1,8 @@
use std::iter::Peekable;
use std::slice::Iter;
use tokenizer::Token; use tokenizer::Token;
/* grammar
expr : term ((PLUS|MIMUS) term)*
term : factor ((MUL | DIV) factor)*
factor : NUM | LPAREN expr RPAREN
*/
#[derive(Debug)] #[derive(Debug)]
enum AST { enum AST {
BinOp(Box<AST>, Box<AST>, Box<AST>), BinOp(Box<AST>, Box<AST>, Box<AST>),
@ -22,9 +17,56 @@ pub struct ParseError {
pub type ParseResult = Result<AST, ParseError>; pub type ParseResult = Result<AST, ParseError>;
pub fn parse(input: Vec<Token>) -> ParseResult { /* grammar
let mut current_token: Token;
return Err(ParseError { err: "error!".to_string() }); expr : term ((PLUS|MIMUS) term)*
term : factor ((MUL | DIV) factor)*
factor : NUM | LPAREN expr RPAREN
*/
struct Parser<'a> {
tokens: Peekable<Iter<'a, Token>>
}
impl<'a> Parser<'a> {
fn parse(&mut self) -> ParseResult {
let r = self.expr();
match self.expect(Token::Separator) {
None => (),
Some(err) => return Err(err)
}
match self.expect(Token::EOF) {
None => (),
Some(err) => return Err(err)
}
r
}
fn expect(&mut self, expected: Token) -> Option<ParseError> {
match self.tokens.next() {
Some(next) if *next == expected => None,
Some(next) => {
let err = format!("Expected {:?} but got {:?}", expected, next);
Some(ParseError { err: err })
}
None => {
let err = format!("Expected {:?} but got end of input", expected);
Some(ParseError { err: err })
}
}
}
fn expr(&mut self) -> ParseResult {
self.tokens.next();
return Ok(AST::Number(5.0));
}
}
pub fn parse(input: Vec<Token>) -> ParseResult {
let mut iter = input.iter().peekable();
let mut parser = Parser { tokens: iter };
return parser.parse();
} }

View File

@ -1,4 +1,4 @@
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub enum Token { pub enum Token {
EOF, EOF,
Separator, Separator,
@ -36,7 +36,6 @@ pub fn tokenize(input: &str) -> Vec<Token> {
';' => true, ';' => true,
'(' => true, '(' => true,
')' => true, ')' => true,
'.' => true,
_ => false _ => false
} }
} }