AST - Conditional statement added
This commit is contained in:
parent
81aef09c5b
commit
2eda13de38
1 changed files with 21 additions and 0 deletions
21
src/ast.rs
21
src/ast.rs
|
@ -19,6 +19,12 @@ pub enum StmtNode {
|
||||||
Print(ExprNode),
|
Print(ExprNode),
|
||||||
/// A block containing multiple statements.
|
/// A block containing multiple statements.
|
||||||
Block(Vec<Box<StmtNode>>),
|
Block(Vec<Box<StmtNode>>),
|
||||||
|
/// A conditional statement.
|
||||||
|
IfStmt {
|
||||||
|
condition: ExprNode,
|
||||||
|
then_branch: Box<StmtNode>,
|
||||||
|
else_branch: Option<Box<StmtNode>>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An AST node that represents an expression.
|
/// An AST node that represents an expression.
|
||||||
|
@ -77,11 +83,26 @@ impl AstDumper for StmtNode {
|
||||||
Self::VarDecl(name, None) => format!("( var {} nil )", name.lexeme),
|
Self::VarDecl(name, None) => format!("( var {} nil )", name.lexeme),
|
||||||
Self::Expression(expr) => format!("( {} )", expr.dump()),
|
Self::Expression(expr) => format!("( {} )", expr.dump()),
|
||||||
Self::Print(expr) => format!("(print {})", expr.dump()),
|
Self::Print(expr) => format!("(print {})", expr.dump()),
|
||||||
|
|
||||||
Self::Block(stmts) => stmts
|
Self::Block(stmts) => stmts
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| s.dump())
|
.map(|s| s.dump())
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join(" "),
|
.join(" "),
|
||||||
|
|
||||||
|
Self::IfStmt {
|
||||||
|
condition,
|
||||||
|
then_branch,
|
||||||
|
else_branch,
|
||||||
|
} => match else_branch {
|
||||||
|
None => format!("( if {} {} () )", condition.dump(), then_branch.dump()),
|
||||||
|
Some(stmt) => format!(
|
||||||
|
"( if {} {} {} )",
|
||||||
|
condition.dump(),
|
||||||
|
then_branch.dump(),
|
||||||
|
stmt.dump()
|
||||||
|
),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue