Parse function calls

This commit is contained in:
Sofia 2026-03-14 17:25:03 +02:00
parent c051e8ee6b
commit 6340f67152
2 changed files with 41 additions and 8 deletions

View File

@ -1,7 +1 @@
function max (a, b)
local m = a
if b > a then
m = b
end
return m
end
local c = max(a, b)

View File

@ -272,6 +272,7 @@ pub enum Expression {
ValueRef(String),
BinOp(BinaryOperator, Box<Node<Expression>>, Box<Node<Expression>>),
FunctionDefinition(Vec<Node<String>>, Block),
FunctionCall(Box<Node<Expression>>, Node<ExpressionList>),
}
impl Parse for Expression {
@ -281,6 +282,22 @@ impl Parse for Expression {
}
}
#[derive(Debug, Clone)]
pub struct ExpressionList(Vec<Node<Expression>>);
impl Parse for ExpressionList {
fn parse(mut stream: TokenStream) -> Result<Self, TokenStreamError> {
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<Expression>);
@ -302,8 +319,30 @@ impl BinaryOperator {
impl Parse for PrimaryExpression {
fn parse(mut stream: TokenStream) -> Result<Self, TokenStreamError> {
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::<Node<Function>>()?;
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::<Node<ExpressionList>>()?;
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),
}))
}