From 157c9383955794d75efcc71be9c0cbed70cf0511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sat, 31 Dec 2022 16:06:20 +0100 Subject: [PATCH] AST - Store variable names as tokens in declarations --- src/ast.rs | 6 +++--- src/parser.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 0cf5576..1d47a59 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -12,7 +12,7 @@ pub struct ProgramNode(pub Vec); #[derive(Debug, Clone)] pub enum StmtNode { /// A variable declaration - VarDecl(String, Option), + VarDecl(Token, Option), /// An single expression Expression(ExprNode), /// The print statement @@ -68,8 +68,8 @@ impl AstDumper for ProgramNode { impl AstDumper for StmtNode { fn dump(&self) -> String { match self { - Self::VarDecl(name, Some(expr)) => format!("( var {} {} )", name, expr.dump()), - Self::VarDecl(name, None) => format!("( var {} nil )", name), + Self::VarDecl(name, Some(expr)) => format!("( var {} {} )", name.lexeme, expr.dump()), + Self::VarDecl(name, None) => format!("( var {} nil )", name.lexeme), Self::Expression(expr) => format!("( {} )", expr.dump()), Self::Print(expr) => format!("(print {})", expr.dump()), } diff --git a/src/parser.rs b/src/parser.rs index 5c71870..243269f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -105,7 +105,7 @@ impl Parser { &TokenType::Semicolon, "expected ';' after variable declaration", )?; - Ok(ast::StmtNode::VarDecl(name.lexeme, initializer)) + Ok(ast::StmtNode::VarDecl(name, initializer)) } /// Parse the following rule: