diff --git a/examples/test.lua b/examples/test.lua index 3cab439..c300cd3 100644 --- a/examples/test.lua +++ b/examples/test.lua @@ -1,7 +1 @@ -function max (a, b) - local m = a - if b > a then - m = b - end - return m -end \ No newline at end of file +local c = max(a, b) \ No newline at end of file diff --git a/src/ast.rs b/src/ast.rs index bf9186c..aa6caf5 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -272,6 +272,7 @@ pub enum Expression { ValueRef(String), BinOp(BinaryOperator, Box>, Box>), FunctionDefinition(Vec>, Block), + FunctionCall(Box>, Node), } impl Parse for Expression { @@ -281,6 +282,22 @@ impl Parse for Expression { } } +#[derive(Debug, Clone)] +pub struct ExpressionList(Vec>); + +impl Parse for ExpressionList { + fn parse(mut stream: TokenStream) -> Result { + let mut list = Vec::new(); + list.push(stream.parse()?); + while stream.peek() == Some(Token::Symbol(',')) { + stream.next(); + list.push(stream.parse()?); + } + + Ok(ExpressionList(list)) + } +} + #[derive(Debug, Clone)] pub struct PrimaryExpression(Node); @@ -302,8 +319,30 @@ impl BinaryOperator { impl Parse for PrimaryExpression { fn parse(mut stream: TokenStream) -> Result { let pre = Metadata::pre(&mut stream, "expression")?; + + let peeked = stream.peek(); + let mut expression = if peeked == Some(Token::Keyword(Keyword::Function)) { + let function = stream.parse::>()?; + Expression::FunctionDefinition(function.kind.params, function.kind.block) + } else { + Expression::ValueRef(stream.parse()?) + }; + + if let Some(Token::Symbol('(')) = stream.peek() { + stream.next(); + let expression_list = stream.parse::>()?; + stream.expect(Token::Symbol(')'))?; + expression = Expression::FunctionCall( + Box::new(Node { + kind: expression, + meta: Metadata::produce(&mut stream, pre.clone()), + }), + expression_list, + ); + } + Ok(PrimaryExpression(Node { - kind: Expression::ValueRef(stream.parse()?), + kind: expression, meta: Metadata::produce(&mut stream, pre), })) }