Add parser support for while statements
This commit is contained in:
parent
905431b33c
commit
8ebf1b3056
@ -203,6 +203,9 @@ impl<'a> Evaluator<'a> {
|
|||||||
(Call(name, args), None)
|
(Call(name, args), None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
While(box test, body) => {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
Conditional(box test, then_block, else_block) => {
|
Conditional(box test, then_block, else_block) => {
|
||||||
if test.is_reducible() {
|
if test.is_reducible() {
|
||||||
let (new_test, new_effect) = self.reduce_expr(test);
|
let (new_test, new_effect) = self.reduce_expr(test);
|
||||||
|
@ -49,6 +49,7 @@ pub enum Expression {
|
|||||||
Conditional(Box<Expression>, Box<Expression>, Option<Box<Expression>>),
|
Conditional(Box<Expression>, Box<Expression>, Option<Box<Expression>>),
|
||||||
Lambda(Function),
|
Lambda(Function),
|
||||||
Block(VecDeque<Expression>),
|
Block(VecDeque<Expression>),
|
||||||
|
While(Box<Expression>, Vec<Expression>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Statement {
|
impl fmt::Display for Statement {
|
||||||
@ -326,6 +327,7 @@ impl Parser {
|
|||||||
Expression::StringLiteral(s)
|
Expression::StringLiteral(s)
|
||||||
}
|
}
|
||||||
Some(Keyword(Kw::If)) => try!(self.conditional_expr()),
|
Some(Keyword(Kw::If)) => try!(self.conditional_expr()),
|
||||||
|
Some(Keyword(Kw::While)) => try!(self.while_expr()),
|
||||||
Some(Identifier(_)) => try!(self.identifier_expr()),
|
Some(Identifier(_)) => try!(self.identifier_expr()),
|
||||||
Some(Token::LParen) => try!(self.paren_expr()),
|
Some(Token::LParen) => try!(self.paren_expr()),
|
||||||
Some(e) => {
|
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> {
|
fn conditional_expr(&mut self) -> ParseResult<Expression> {
|
||||||
use tokenizer::Token::*;
|
use tokenizer::Token::*;
|
||||||
use self::Expression::*;
|
use self::Expression::*;
|
||||||
|
Loading…
Reference in New Issue
Block a user