From 247638c4db78f212c2bb8e899f697d653bd59860 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 24 Dec 2015 22:01:59 -0800 Subject: [PATCH] Get parsing REPL working --- src/main.rs | 7 ++++++- src/parser.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/parser.rs diff --git a/src/main.rs b/src/main.rs index e98a438..deaba87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,9 @@ use simplerepl::{REPL, ReplState}; use tokenizer::tokenize; mod tokenizer; +use parser::{ParseResult, parse}; +mod parser; + fn main() { let args: Vec = std::env::args().collect(); @@ -55,7 +58,9 @@ 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 { - format!("{:?}", tokenize(input)) + format!("{:?}", parse(tokenize(input))) } } diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..f9c57fa --- /dev/null +++ b/src/parser.rs @@ -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, Box, Box), + Number(f64), + Name(String), +} + +#[derive(Debug)] +pub struct ParseError { + err: String +} + +pub type ParseResult = Result; + +pub fn parse(input: Vec) -> ParseResult { + let mut current_token: Token; + + return Err(ParseError { err: "error!".to_string() }); +} +