Preliminary addition of function blocks

This commit is contained in:
greg 2015-08-08 12:51:46 -07:00
parent e870d8172a
commit 57ea1bae30
2 changed files with 15 additions and 0 deletions

View File

@ -7,6 +7,12 @@
<statement> := let <id> = <expr>
| <expr>
| <fn_block>
<fn_block> := fn <id> ( <arg_list> ) <statements> end
<arg_list> := e
| <id> , <arg_list>
<expr> := if <expr> then <statements> end
| if <expr> then <statements> else <statements> end

View File

@ -15,6 +15,7 @@ pub enum AST {
Statements(Vec<AST>),
IfStatement(Box<AST>, Box<AST>, Option<Box<AST>>),
WhileStatement(Box<AST>, Box<AST>),
Function(Vec<String>, Box<AST>),
DoNothing
}
@ -105,10 +106,18 @@ fn statements(tokens: &mut Tokens) -> ParseResult {
fn statement(tokens: &mut Tokens) -> ParseResult {
match tokens.peek().map(|i| i.clone()) {
Some(&Keyword(Kw::Let)) => let_expression(tokens),
Some(&Keyword(Kw::Fn)) => function_block(tokens),
_ => expression(tokens)
}
}
fn function_block(tokens: &mut Tokens) -> ParseResult {
expect!(Keyword(Kw::Fn), tokens);
expect!(Keyword(Kw::End), tokens);
Ok(AST::Function(Vec::new(), Box::new(AST::DoNothing)))
}
fn let_expression(tokens: &mut Tokens) -> ParseResult {
expect!(Keyword(Kw::Let), tokens);
if let Some(&Identifier(ref name)) = tokens.next() {