From 329c521964cce947f665b589c7ff5d66d3a6475b Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 24 Jul 2015 02:05:15 -0700 Subject: [PATCH] Parsing statement blocks works --- src/parser.rs | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 1849974..8d985c1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -10,7 +10,8 @@ pub enum AST { LangString(String), Number(f64), BinOp(Box, Box, Box), - Binding(String, Box) + Binding(String, Box), + Statements(Vec) } pub enum ParseResult { @@ -51,14 +52,48 @@ pub fn parse(input: Vec) -> ParseResult { let mut tokens: Tokens = input.iter().peekable(); - if let ParseResult::Ok(ast) = let_expression(&mut tokens) { - expect!(EOF, &mut tokens); - return ParseResult::Ok(ast); + match statements(&mut tokens) { + ok@ParseResult::Ok(_) => { + expect!(EOF, &mut tokens); + ok + }, + err@ParseResult::Err(_) => err } - - return ParseResult::Err("Bad parse".to_string()); } +fn statements(input: &mut Tokens) -> ParseResult { + + let mut statements = Vec::new(); + + let initial_statement = statement(input); + match initial_statement { + ParseResult::Ok(ast) => { + statements.push(ast); + loop { + let lookahead = input.peek().map(|i| i.clone()); + if let Some(&Separator) = lookahead { + input.next(); + if let ParseResult::Ok(ast_next) = statement(input) { + statements.push(ast_next); + } else { + return ParseResult::Err("bad thing happened".to_string()); + } + } else { + break; + } + } + }, + err@ParseResult::Err(_) => { + return err; + } + } + + return ParseResult::Ok(AST::Statements(statements)); +} + +fn statement(input: &mut Tokens) -> ParseResult { + let_expression(input) +} fn let_expression(input: &mut Tokens) -> ParseResult { expect!(Keyword(Kw::Let), input); @@ -84,5 +119,5 @@ fn let_expression(input: &mut Tokens) -> ParseResult { } } - return ParseResult::Err("Bad parse".to_string()); + return ParseResult::Err("Bad parse in let_expression()".to_string()); }