Mise en place du visiteur et ébauche de pretty printer.
This commit is contained in:
62
src/expr.rs
62
src/expr.rs
@@ -1,13 +1,13 @@
|
||||
use crate::token::Token;
|
||||
|
||||
pub struct Binary {
|
||||
left: Expr,
|
||||
left: Box<Expr>,
|
||||
operator: Token,
|
||||
right: Expr,
|
||||
right: Box<Expr>,
|
||||
}
|
||||
|
||||
pub struct Grouping {
|
||||
expression: Expr,
|
||||
expression: Box<Expr>,
|
||||
}
|
||||
|
||||
pub enum Literal {
|
||||
@@ -19,12 +19,58 @@ pub enum Literal {
|
||||
|
||||
pub struct Unary {
|
||||
operator: Token,
|
||||
right: Expr,
|
||||
right: Box<Expr>,
|
||||
}
|
||||
|
||||
pub enum Expr {
|
||||
Binary ,
|
||||
Grouping,
|
||||
Literal,
|
||||
Unary,
|
||||
BinaryExpr(Binary),
|
||||
GroupingExpr(Grouping),
|
||||
LiteralExpr(Literal),
|
||||
UnaryExpr(Unary),
|
||||
}
|
||||
|
||||
pub trait Visitor {
|
||||
fn visit(&self);
|
||||
}
|
||||
|
||||
impl Visitor for Binary {
|
||||
fn visit(&self) {
|
||||
print!("( {}",&self.operator.literal);
|
||||
&self.left.visit();
|
||||
&self.right.visit();
|
||||
print!(")");
|
||||
}
|
||||
}
|
||||
|
||||
impl Visitor for Grouping {
|
||||
fn visit(&self) {
|
||||
print!("( group ");
|
||||
&self.expression.visit();
|
||||
print!(")");
|
||||
}
|
||||
}
|
||||
|
||||
impl Visitor for Literal {
|
||||
fn visit(&self) {
|
||||
match &self {
|
||||
Literal::LiteralNumber(i) => print!("{}", i),
|
||||
Literal::LiteralString(s) => print!("{}", s),
|
||||
Literal::LiteralBool(b) => print!("{}", b),
|
||||
Literal::LiteralNil => print!("nil"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Visitor for Unary {
|
||||
fn visit(&self) {
|
||||
print!("( {}",&self.operator.literal);
|
||||
&self.right.visit();
|
||||
print!(")");
|
||||
}
|
||||
}
|
||||
|
||||
impl Visitor for Expr {
|
||||
fn visit(&self) {
|
||||
&self.visit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::env;
|
||||
//use std::io::Write;
|
||||
use std::process;
|
||||
use crate::expr::Expr;
|
||||
|
||||
mod rlox_interpreter;
|
||||
mod scanner;
|
||||
@@ -31,6 +31,7 @@ fn main() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Implémentations de référence :
|
||||
// https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust
|
||||
|
||||
|
||||
Reference in New Issue
Block a user