From 9f4330889adc7b551e756c6d82cf6b3e6cbe89c2 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 20 Jul 2015 01:46:02 -0700 Subject: [PATCH] Starting parsing work --- src/main.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 47e1efc..779853c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,9 +24,15 @@ enum Token { #[derive(Debug)] enum ASTNode { - GenericNode + Name(String), + Number(f64), + BinOp(Box, Box, Box) } +enum ParseResult<'a> { + Ok(ASTNode, &'a [Token]), + Err(String, &'a [Token]) +} fn repl() { let stdin = io::stdin(); @@ -45,11 +51,15 @@ fn repl() { buf.clear(); println!("Tokens: {:?}", tokens); - let ast = parse(tokens); - println!("AST: {:?}", ast); + match parse(tokens) { + ParseResult::Ok(ast, _) => println!("AST: {:?}", ast), + ParseResult::Err(err, _) => println!("Error: {}", err) + } + /* let eval = evaluate(&ast); println!("{}", eval); + */ }, Err(err) => { println!("Error: {}", err); @@ -123,17 +133,11 @@ fn tokenize(input: &str) -> Vec { tokens } -fn parse(_input: Vec) -> ASTNode { - ASTNode::GenericNode +fn parse<'a>(input: Vec) -> ParseResult<'a> { + + let mut tokens = input.iter().peekable(); + + + return ParseResult::Ok(ASTNode::Name("Hella".to_string()), &[]); } -fn evaluate(input: &ASTNode) -> String { - - return match eval_ast(input) { - ASTNode::GenericNode => "Not implemented".to_string() - }; - - fn eval_ast(_input: &ASTNode) -> ASTNode { - return ASTNode::GenericNode; - } -}