AST - Added variable declarations

This commit is contained in:
Emmanuel BENOîT 2022-12-31 15:30:53 +01:00
parent c31a97eb76
commit 8e5a26bcc3

View file

@ -11,6 +11,8 @@ pub struct ProgramNode(pub Vec<StmtNode>);
/// An AST node that represents a statement. /// An AST node that represents a statement.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum StmtNode { pub enum StmtNode {
/// A variable declaration
VarDecl(String, Option<ExprNode>),
/// An single expression /// An single expression
Expression(ExprNode), Expression(ExprNode),
/// The print statement /// The print statement
@ -63,6 +65,8 @@ impl AstDumper for ProgramNode {
impl AstDumper for StmtNode { impl AstDumper for StmtNode {
fn dump(&self) -> String { fn dump(&self) -> String {
match self { match self {
Self::VarDecl(name, Some(expr)) => format!("( var {} {} )", name, expr.dump()),
Self::VarDecl(name, None) => format!("( var {} nil )", name),
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()),
} }