I can now parse one thing
This commit is contained in:
parent
247638c4db
commit
509ab80b9c
11
src/main.rs
11
src/main.rs
@ -57,10 +57,11 @@ impl ReplState for InterpreterState {
|
||||
|
||||
fn repl_handler(input: &str, state: &mut InterpreterState) -> String {
|
||||
if state.show_tokens {
|
||||
format!("Tokens: {:?}", tokenize(input))
|
||||
} else if state.show_parse{
|
||||
format!("Parse: {:?}", parse(tokenize(input)))
|
||||
} else {
|
||||
println!("Tokens: {:?}", tokenize(input))
|
||||
}
|
||||
|
||||
if state.show_parse {
|
||||
println!("Parse: {:?}", parse(tokenize(input)))
|
||||
}
|
||||
format!("{:?}", parse(tokenize(input)))
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,8 @@
|
||||
use std::iter::Peekable;
|
||||
use std::slice::Iter;
|
||||
|
||||
use tokenizer::Token;
|
||||
|
||||
/* grammar
|
||||
|
||||
expr : term ((PLUS|MIMUS) term)*
|
||||
term : factor ((MUL | DIV) factor)*
|
||||
factor : NUM | LPAREN expr RPAREN
|
||||
|
||||
*/
|
||||
|
||||
#[derive(Debug)]
|
||||
enum AST {
|
||||
BinOp(Box<AST>, Box<AST>, Box<AST>),
|
||||
@ -22,9 +17,56 @@ pub struct ParseError {
|
||||
|
||||
pub type ParseResult = Result<AST, ParseError>;
|
||||
|
||||
pub fn parse(input: Vec<Token>) -> ParseResult {
|
||||
let mut current_token: Token;
|
||||
/* grammar
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Token {
|
||||
EOF,
|
||||
Separator,
|
||||
@ -36,7 +36,6 @@ pub fn tokenize(input: &str) -> Vec<Token> {
|
||||
';' => true,
|
||||
'(' => true,
|
||||
')' => true,
|
||||
'.' => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user