2022-12-31 10:13:51 +01:00
|
|
|
use crate::tokens::Token;
|
2022-12-30 23:50:02 +01:00
|
|
|
|
2022-12-31 14:36:01 +01:00
|
|
|
/* --------- *
|
|
|
|
* AST nodes *
|
|
|
|
* --------- */
|
|
|
|
|
|
|
|
/// The AST node for the program
|
|
|
|
#[derive(Default, Debug, Clone)]
|
2022-12-31 14:45:35 +01:00
|
|
|
pub struct ProgramNode(pub Vec<StmtNode>);
|
2022-12-31 14:36:01 +01:00
|
|
|
|
|
|
|
/// An AST node that represents a statement.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum StmtNode {
|
|
|
|
/// An single expression
|
|
|
|
Expression(ExprNode),
|
|
|
|
/// The print statement
|
|
|
|
Print(ExprNode),
|
|
|
|
}
|
|
|
|
|
2022-12-30 23:50:02 +01:00
|
|
|
/// 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 },
|
|
|
|
}
|
|
|
|
|
2022-12-31 14:27:20 +01:00
|
|
|
/* -------------------------------- *
|
|
|
|
* Dumper trait and implementations *
|
|
|
|
* -------------------------------- */
|
|
|
|
|
|
|
|
/// This trait should be implemented by nodes to allow AST dumps.
|
|
|
|
pub trait AstDumper {
|
|
|
|
/// Dump the node as a string.
|
|
|
|
fn dump(&self) -> String;
|
|
|
|
}
|
|
|
|
|
2022-12-31 14:36:01 +01:00
|
|
|
impl AstDumper for ProgramNode {
|
|
|
|
fn dump(&self) -> String {
|
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.map(|node| node.dump())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(" ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AstDumper for StmtNode {
|
|
|
|
fn dump(&self) -> String {
|
|
|
|
match self {
|
|
|
|
Self::Expression(expr) => format!("( {} )", expr.dump()),
|
|
|
|
Self::Print(expr) => format!("(print {})", expr.dump()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 23:50:02 +01:00
|
|
|
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())
|
|
|
|
}
|
2022-12-31 10:13:51 +01:00
|
|
|
Self::Litteral { value } => {
|
|
|
|
if value.is_litteral() {
|
|
|
|
value.lexeme.clone()
|
|
|
|
} else {
|
|
|
|
panic!("Unexpected token type for token {:#?}", value)
|
|
|
|
}
|
|
|
|
}
|
2022-12-30 23:50:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|