From 58d399dace9775def260540c6cf52e2f0f922644 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 8 Aug 2015 13:48:38 -0700 Subject: [PATCH] More function parsing --- src/parser.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 4e002a8..392184c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -15,7 +15,8 @@ pub enum AST { Statements(Vec), IfStatement(Box, Box, Option>), WhileStatement(Box, Box), - Function(Vec, Box), + Function(Box, Box), + ArgList(Vec), DoNothing } @@ -113,9 +114,45 @@ fn statement(tokens: &mut Tokens) -> ParseResult { fn function_block(tokens: &mut Tokens) -> ParseResult { expect!(Keyword(Kw::Fn), tokens); + + let name = match tokens.next() { + Some(&Identifier(ref s)) => AST::Name(s.clone()), + _ => return Err("bad parse in function_block()".to_string()) + }; + + expect!(LParen, tokens); + + let arguments = try!(argument_list(tokens)); + + expect!(RParen, tokens); + + let body = try!(statements(tokens)); + expect!(Keyword(Kw::End), tokens); - Ok(AST::Function(Vec::new(), Box::new(AST::DoNothing))) + Ok(AST::Function( + Box::new(arguments), + Box::new(body) + )) +} + +fn argument_list(tokens: &mut Tokens) -> ParseResult { + + let mut args: Vec = Vec::new(); + + loop { + let lookahead = tokens.peek().map(|i| i.clone()); + match lookahead { + Some(&Identifier(ref s)) => { + args.push(s.clone()); + tokens.next(); + expect!(Comma, tokens); + }, + _ => break + } + } + + Ok(AST::ArgList(args)) } fn let_expression(tokens: &mut Tokens) -> ParseResult {