From 2eda13de38d4d06b4ad840563a6fd9c6a75bb134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 1 Jan 2023 10:58:45 +0100 Subject: [PATCH] AST - Conditional statement added --- src/ast.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/ast.rs b/src/ast.rs index fbf7561..febe145 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -19,6 +19,12 @@ pub enum StmtNode { Print(ExprNode), /// A block containing multiple statements. Block(Vec>), + /// A conditional statement. + IfStmt { + condition: ExprNode, + then_branch: Box, + else_branch: Option>, + }, } /// 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::Expression(expr) => format!("( {} )", expr.dump()), Self::Print(expr) => format!("(print {})", expr.dump()), + Self::Block(stmts) => stmts .iter() .map(|s| s.dump()) .collect::>() .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() + ), + }, } } }