while statements

This commit is contained in:
greg 2015-07-25 16:12:42 -07:00
parent 42719dc2f2
commit 30eddf7737

View File

@ -12,7 +12,8 @@ pub enum AST {
BinOp(Box<AST>, Box<AST>, Box<AST>), BinOp(Box<AST>, Box<AST>, Box<AST>),
Binding(String, Box<AST>), Binding(String, Box<AST>),
Statements(Vec<AST>), Statements(Vec<AST>),
IfStatement(Box<AST>, Box<AST>, Option<Box<AST>>) IfStatement(Box<AST>, Box<AST>, Option<Box<AST>>),
WhileStatement(Box<AST>, Box<AST>)
} }
pub enum ParseResult { pub enum ParseResult {
@ -126,6 +127,9 @@ fn expression(tokens: &mut Tokens) -> ParseResult {
Some(&Keyword(Kw::If)) => { Some(&Keyword(Kw::If)) => {
if_expression(tokens) if_expression(tokens)
}, },
Some(&Keyword(Kw::While)) => {
while_expression(tokens)
},
_ => rhs(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 { fn rhs(tokens: &mut Tokens) -> ParseResult {
let next = tokens.next(); let next = tokens.next();
if let Some(&Identifier(ref value)) = next { if let Some(&Identifier(ref value)) = next {