AST - Lambda functions

This commit is contained in:
Emmanuel BENOîT 2023-01-02 21:01:45 +01:00
parent 783c10067b
commit 4769a2a7f3

View file

@ -96,6 +96,14 @@ pub enum ExprNode {
/// A reference to a variable. /// A reference to a variable.
Variable { name: Token }, Variable { name: Token },
/// A lambda function.
Lambda {
/// The `fun` token which creates the lambda.
token: Token,
params: Vec<Token>,
body: Vec<StmtNode>,
},
/// A function call. /// A function call.
Call { Call {
/// Expression that corresponds to the callable. /// Expression that corresponds to the callable.
@ -242,6 +250,26 @@ impl AstDumper for ExprNode {
panic!("Unexpected token type for token {:#?}", value) panic!("Unexpected token type for token {:#?}", value)
} }
} }
ExprNode::Lambda {
token: _,
params,
body,
} => {
format!(
"( fun ({}) {} )",
params
.iter()
.map(|token| &token.lexeme as &str)
.collect::<Vec<&str>>()
.join(" "),
body.iter()
.map(|stmt| stmt.dump())
.collect::<Vec<String>>()
.join(" ")
)
}
ExprNode::Call { ExprNode::Call {
callee, callee,
right_paren: _, right_paren: _,