AST - Store variable names as tokens in declarations

This commit is contained in:
Emmanuel BENOîT 2022-12-31 16:06:20 +01:00
parent 90557e6600
commit 157c938395
2 changed files with 4 additions and 4 deletions

View file

@ -12,7 +12,7 @@ pub struct ProgramNode(pub Vec<StmtNode>);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum StmtNode { pub enum StmtNode {
/// A variable declaration /// A variable declaration
VarDecl(String, Option<ExprNode>), VarDecl(Token, Option<ExprNode>),
/// An single expression /// An single expression
Expression(ExprNode), Expression(ExprNode),
/// The print statement /// The print statement
@ -68,8 +68,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, Some(expr)) => format!("( var {} {} )", name.lexeme, expr.dump()),
Self::VarDecl(name, None) => format!("( var {} nil )", name), Self::VarDecl(name, None) => format!("( var {} nil )", name.lexeme),
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()),
} }

View file

@ -105,7 +105,7 @@ impl Parser {
&TokenType::Semicolon, &TokenType::Semicolon,
"expected ';' after variable declaration", "expected ';' after variable declaration",
)?; )?;
Ok(ast::StmtNode::VarDecl(name.lexeme, initializer)) Ok(ast::StmtNode::VarDecl(name, initializer))
} }
/// Parse the following rule: /// Parse the following rule: