From f17cafd9107d5db84e5ef575fe9e60742eb78422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sat, 31 Dec 2022 14:36:01 +0100 Subject: [PATCH] AST - Program and basic statements --- src/ast.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/ast.rs b/src/ast.rs index 51e8b18..899546e 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,5 +1,22 @@ use crate::tokens::Token; +/* --------- * + * AST nodes * + * --------- */ + +/// The AST node for the program +#[derive(Default, Debug, Clone)] +pub struct ProgramNode(Vec); + +/// An AST node that represents a statement. +#[derive(Debug, Clone)] +pub enum StmtNode { + /// An single expression + Expression(ExprNode), + /// The print statement + Print(ExprNode), +} + /// An AST node that represents an expression. #[derive(Debug, Clone)] pub enum ExprNode { @@ -33,6 +50,25 @@ pub trait AstDumper { fn dump(&self) -> String; } +impl AstDumper for ProgramNode { + fn dump(&self) -> String { + self.0 + .iter() + .map(|node| node.dump()) + .collect::>() + .join(" ") + } +} + +impl AstDumper for StmtNode { + fn dump(&self) -> String { + match self { + Self::Expression(expr) => format!("( {} )", expr.dump()), + Self::Print(expr) => format!("(print {})", expr.dump()), + } + } +} + impl AstDumper for ExprNode { fn dump(&self) -> String { match self {