AST - Expression node + dumper trait
This commit is contained in:
parent
912c7a265e
commit
8856d733ad
2 changed files with 56 additions and 0 deletions
55
src/ast.rs
Normal file
55
src/ast.rs
Normal 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),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod ast;
|
||||||
mod errors;
|
mod errors;
|
||||||
mod scanner;
|
mod scanner;
|
||||||
mod tokens;
|
mod tokens;
|
||||||
|
|
Loading…
Reference in a new issue