AST - Added variable declarations
This commit is contained in:
parent
c31a97eb76
commit
8e5a26bcc3
1 changed files with 4 additions and 0 deletions
|
@ -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()),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue