Get parsing REPL working

This commit is contained in:
greg 2015-12-24 22:01:59 -08:00
parent be98f8387e
commit 247638c4db
2 changed files with 36 additions and 1 deletions

View File

@ -11,6 +11,9 @@ use simplerepl::{REPL, ReplState};
use tokenizer::tokenize; use tokenizer::tokenize;
mod tokenizer; mod tokenizer;
use parser::{ParseResult, parse};
mod parser;
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
@ -55,7 +58,9 @@ 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)) format!("Tokens: {:?}", tokenize(input))
} else if state.show_parse{
format!("Parse: {:?}", parse(tokenize(input)))
} else { } else {
format!("{:?}", tokenize(input)) format!("{:?}", parse(tokenize(input)))
} }
} }

30
src/parser.rs Normal file
View File

@ -0,0 +1,30 @@
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>),
Number(f64),
Name(String),
}
#[derive(Debug)]
pub struct ParseError {
err: String
}
pub type ParseResult = Result<AST, ParseError>;
pub fn parse(input: Vec<Token>) -> ParseResult {
let mut current_token: Token;
return Err(ParseError { err: "error!".to_string() });
}