From 30eddf7737d6b0a64192af4292ece1acb1f5e4b1 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 25 Jul 2015 16:12:42 -0700 Subject: [PATCH] while statements --- src/parser.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index 4a163ce..8eaeb8f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -12,7 +12,8 @@ pub enum AST { BinOp(Box, Box, Box), Binding(String, Box), Statements(Vec), - IfStatement(Box, Box, Option>) + IfStatement(Box, Box, Option>), + WhileStatement(Box, Box) } pub enum ParseResult { @@ -126,6 +127,9 @@ fn expression(tokens: &mut Tokens) -> ParseResult { Some(&Keyword(Kw::If)) => { if_expression(tokens) }, + Some(&Keyword(Kw::While)) => { + while_expression(tokens) + }, _ => rhs(tokens) } } @@ -165,6 +169,27 @@ fn if_expression(tokens: &mut Tokens) -> ParseResult { )) } +fn while_expression(tokens: &mut Tokens) -> ParseResult { + expect!(Keyword(Kw::While), tokens); + let while_expression = match expression(tokens) { + err@ParseResult::Err(_) => return err, + ParseResult::Ok(ast) => ast + }; + + expect!(Separator, tokens); + let statements = match statements(tokens) { + err@ParseResult::Err(_) => return err, + ParseResult::Ok(ast) => ast + }; + + expect!(Keyword(Kw::End), tokens); + + ParseResult::Ok(AST::WhileStatement( + Box::new(while_expression), + Box::new(statements), + )) +} + fn rhs(tokens: &mut Tokens) -> ParseResult { let next = tokens.next(); if let Some(&Identifier(ref value)) = next {