AST - Refactored binary expressions
This commit is contained in:
parent
58c1bbcf68
commit
75387c5ac4
2 changed files with 24 additions and 34 deletions
23
src/ast.rs
23
src/ast.rs
|
@ -94,6 +94,17 @@ pub struct SetExpr {
|
||||||
pub value: Box<ExprNode>,
|
pub value: Box<ExprNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A binary expression.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct BinaryExpr {
|
||||||
|
/// The left side expression
|
||||||
|
pub left: Box<ExprNode>,
|
||||||
|
/// The operator
|
||||||
|
pub operator: Token,
|
||||||
|
/// The right side expression
|
||||||
|
pub right: Box<ExprNode>,
|
||||||
|
}
|
||||||
|
|
||||||
/// An AST node that represents an expression.
|
/// An AST node that represents an expression.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ExprNode {
|
pub enum ExprNode {
|
||||||
|
@ -106,18 +117,10 @@ pub enum ExprNode {
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Logical binary expression.
|
/// Logical binary expression.
|
||||||
Logical {
|
Logical(BinaryExpr),
|
||||||
left: Box<ExprNode>,
|
|
||||||
operator: Token,
|
|
||||||
right: Box<ExprNode>,
|
|
||||||
},
|
|
||||||
|
|
||||||
/// Binary expression.
|
/// Binary expression.
|
||||||
Binary {
|
Binary(BinaryExpr),
|
||||||
left: Box<ExprNode>,
|
|
||||||
operator: Token,
|
|
||||||
right: Box<ExprNode>,
|
|
||||||
},
|
|
||||||
|
|
||||||
/// Unary expression.
|
/// Unary expression.
|
||||||
Unary {
|
Unary {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{ExprNode, ProgramNode, StmtNode},
|
ast::{BinaryExpr, ExprNode, ProgramNode, StmtNode},
|
||||||
tokens::Token,
|
tokens::Token,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -181,6 +181,14 @@ fn dump_expression(expr: &ExprNode) -> Dumper {
|
||||||
dumper
|
dumper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn dump_binary_expr(dumper: &mut Dumper, binary_expr: &BinaryExpr) {
|
||||||
|
dump_expr_node(dumper, &binary_expr.left);
|
||||||
|
dumper.current_line().push(' ');
|
||||||
|
dumper.current_line().push_str(&binary_expr.operator.lexeme);
|
||||||
|
dumper.current_line().push(' ');
|
||||||
|
dump_expr_node(dumper, &binary_expr.right);
|
||||||
|
}
|
||||||
|
|
||||||
fn dump_expr_node(dumper: &mut Dumper, expr: &ExprNode) {
|
fn dump_expr_node(dumper: &mut Dumper, expr: &ExprNode) {
|
||||||
match expr {
|
match expr {
|
||||||
ExprNode::Assignment { name, value, id: _ } => {
|
ExprNode::Assignment { name, value, id: _ } => {
|
||||||
|
@ -191,29 +199,8 @@ fn dump_expr_node(dumper: &mut Dumper, expr: &ExprNode) {
|
||||||
dump_expr_node(dumper, value);
|
dump_expr_node(dumper, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
ExprNode::Logical {
|
ExprNode::Logical(binary_expr) => dump_binary_expr(dumper, binary_expr),
|
||||||
left,
|
ExprNode::Binary(binary_expr) => dump_binary_expr(dumper, binary_expr),
|
||||||
operator,
|
|
||||||
right,
|
|
||||||
} => {
|
|
||||||
dump_expr_node(dumper, left);
|
|
||||||
dumper.current_line().push(' ');
|
|
||||||
dumper.current_line().push_str(&operator.lexeme);
|
|
||||||
dumper.current_line().push(' ');
|
|
||||||
dump_expr_node(dumper, right);
|
|
||||||
}
|
|
||||||
|
|
||||||
ExprNode::Binary {
|
|
||||||
left,
|
|
||||||
operator,
|
|
||||||
right,
|
|
||||||
} => {
|
|
||||||
dump_expr_node(dumper, left);
|
|
||||||
dumper.current_line().push(' ');
|
|
||||||
dumper.current_line().push_str(&operator.lexeme);
|
|
||||||
dumper.current_line().push(' ');
|
|
||||||
dump_expr_node(dumper, right);
|
|
||||||
}
|
|
||||||
|
|
||||||
ExprNode::Unary { operator, right } => {
|
ExprNode::Unary { operator, right } => {
|
||||||
dumper.current_line().push_str(&operator.lexeme);
|
dumper.current_line().push_str(&operator.lexeme);
|
||||||
|
|
Loading…
Reference in a new issue