Ajout des instructions.

This commit is contained in:
2024-05-21 08:44:11 +02:00
parent 3e27109d2a
commit de653f9bc5
6 changed files with 118 additions and 30 deletions

View File

@@ -1,4 +1,5 @@
use crate::expr::{Binary, Expr, ExprVisitor, Grouping, Literal, Unary};
use crate::stmt::{ExpressionStatement, PrintStatement, StatementVisitor};
pub struct ASTPrinter {
pub(crate) depth: u32
@@ -59,4 +60,22 @@ impl ExprVisitor<()> for ASTPrinter {
Expr::UnaryExpr(u) => { self.visit_unary(u); }
}
}
}
impl StatementVisitor<()> for ASTPrinter {
fn visit_expr_stmt(&mut self, e: &ExpressionStatement) -> () {
println!("{} EXPR_STMT(", self.ast_tab());
self.depth+=1;
self.visit_expr(&e.expr);
self.depth-=1;
print!(") ");
}
fn visit_print(&mut self, p: &PrintStatement) -> () {
println!("{} EXPR_STMT(", self.ast_tab());
self.depth+=1;
self.visit_expr(&p.expr);
self.depth-=1;
print!(") ");
}
}