Add parser support for while statements

This commit is contained in:
greg 2017-01-03 03:19:52 -08:00
parent 905431b33c
commit 8ebf1b3056
2 changed files with 32 additions and 0 deletions

View File

@ -203,6 +203,9 @@ impl<'a> Evaluator<'a> {
(Call(name, args), None)
}
}
While(box test, body) => {
unimplemented!()
}
Conditional(box test, then_block, else_block) => {
if test.is_reducible() {
let (new_test, new_effect) = self.reduce_expr(test);

View File

@ -49,6 +49,7 @@ pub enum Expression {
Conditional(Box<Expression>, Box<Expression>, Option<Box<Expression>>),
Lambda(Function),
Block(VecDeque<Expression>),
While(Box<Expression>, Vec<Expression>),
}
impl fmt::Display for Statement {
@ -326,6 +327,7 @@ impl Parser {
Expression::StringLiteral(s)
}
Some(Keyword(Kw::If)) => try!(self.conditional_expr()),
Some(Keyword(Kw::While)) => try!(self.while_expr()),
Some(Identifier(_)) => try!(self.identifier_expr()),
Some(Token::LParen) => try!(self.paren_expr()),
Some(e) => {
@ -337,6 +339,33 @@ impl Parser {
})
}
fn while_expr(&mut self) -> ParseResult<Expression> {
use tokenizer::Token::*;
use self::Expression::*;
expect!(self, Keyword(Kw::While));
let test = try!(self.expression());
let mut body = Vec::new();
loop {
match self.peek() {
None |
Some(Keyword(Kw::End)) => break,
Some(Semicolon) | Some(Newline) => {
self.next();
continue;
}
_ => {
let exp = try!(self.expression());
body.push(exp);
}
}
}
expect!(self, Keyword(Kw::End));
Ok(While(Box::new(test), body))
}
fn conditional_expr(&mut self) -> ParseResult<Expression> {
use tokenizer::Token::*;
use self::Expression::*;