AST - Expression node + dumper trait

This commit is contained in:
Emmanuel BENOîT 2022-12-30 23:50:02 +01:00
parent 912c7a265e
commit 8856d733ad
2 changed files with 56 additions and 0 deletions

55
src/ast.rs Normal file
View file

@ -0,0 +1,55 @@
use crate::tokens::{Token, TokenType};
/// This trait should be implemented by nodes to allow AST dumps.
pub trait AstDumper {
/// Dump the node as a string.
fn dump(&self) -> String;
}
/// An AST node that represents an expression.
#[derive(Debug, Clone)]
pub enum ExprNode {
/// Binary expression.
Binary {
left: Box<ExprNode>,
operator: Token,
right: Box<ExprNode>,
},
/// Unary expression.
Unary {
operator: Token,
right: Box<ExprNode>,
},
/// Grouping expression, containing a sub-expression.
Grouping { expression: Box<ExprNode> },
/// A litteral value, represented by the corresponding token.
Litteral { value: Token },
}
impl AstDumper for ExprNode {
fn dump(&self) -> String {
match self {
Self::Binary {
left,
operator,
right,
} => {
format!("( {} {} {} )", operator.lexeme, left.dump(), right.dump())
}
Self::Unary { operator, right } => {
format!("( {} {} )", operator.lexeme, right.dump())
}
Self::Grouping { expression } => {
format!("( {} )", expression.dump())
}
Self::Litteral { value } => match &value.token_type {
TokenType::String(s) => s.clone(),
TokenType::Number(n) => format!("{n}"),
_ => panic!("Unexpected token type for token {:#?}", value),
},
}
}
}

View file

@ -1,3 +1,4 @@
mod ast;
mod errors;
mod scanner;
mod tokens;