AST - Function declarations
This commit is contained in:
parent
0a38903154
commit
3738fb4f00
1 changed files with 21 additions and 0 deletions
21
src/ast.rs
21
src/ast.rs
|
@ -13,6 +13,12 @@ pub struct ProgramNode(pub Vec<StmtNode>);
|
||||||
pub enum StmtNode {
|
pub enum StmtNode {
|
||||||
/// A variable declaration
|
/// A variable declaration
|
||||||
VarDecl(Token, Option<ExprNode>),
|
VarDecl(Token, Option<ExprNode>),
|
||||||
|
/// A function declaration
|
||||||
|
FunDecl {
|
||||||
|
name: Token,
|
||||||
|
params: Vec<Token>,
|
||||||
|
body: Vec<StmtNode>,
|
||||||
|
},
|
||||||
/// An single expression
|
/// An single expression
|
||||||
Expression(ExprNode),
|
Expression(ExprNode),
|
||||||
/// The print statement
|
/// The print statement
|
||||||
|
@ -111,6 +117,21 @@ impl AstDumper for StmtNode {
|
||||||
match self {
|
match self {
|
||||||
Self::VarDecl(name, Some(expr)) => format!("( var {} {} )", name.lexeme, expr.dump()),
|
Self::VarDecl(name, Some(expr)) => format!("( var {} {} )", name.lexeme, expr.dump()),
|
||||||
Self::VarDecl(name, None) => format!("( var {} nil )", name.lexeme),
|
Self::VarDecl(name, None) => format!("( var {} nil )", name.lexeme),
|
||||||
|
|
||||||
|
Self::FunDecl { name, params, body } => format!(
|
||||||
|
"( fun {} ({}) {} )",
|
||||||
|
name.lexeme,
|
||||||
|
params
|
||||||
|
.iter()
|
||||||
|
.map(|token| &token.lexeme as &str)
|
||||||
|
.collect::<Vec<&str>>()
|
||||||
|
.join(" "),
|
||||||
|
body.iter()
|
||||||
|
.map(|stmt| stmt.dump())
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" ")
|
||||||
|
),
|
||||||
|
|
||||||
Self::Expression(expr) => expr.dump(),
|
Self::Expression(expr) => expr.dump(),
|
||||||
Self::Print(expr) => format!("(print {})", expr.dump()),
|
Self::Print(expr) => format!("(print {})", expr.dump()),
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue