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>,
|
||||
}
|
||||
|
||||
/// 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.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ExprNode {
|
||||
|
@ -106,18 +117,10 @@ pub enum ExprNode {
|
|||
},
|
||||
|
||||
/// Logical binary expression.
|
||||
Logical {
|
||||
left: Box<ExprNode>,
|
||||
operator: Token,
|
||||
right: Box<ExprNode>,
|
||||
},
|
||||
Logical(BinaryExpr),
|
||||
|
||||
/// Binary expression.
|
||||
Binary {
|
||||
left: Box<ExprNode>,
|
||||
operator: Token,
|
||||
right: Box<ExprNode>,
|
||||
},
|
||||
Binary(BinaryExpr),
|
||||
|
||||
/// Unary expression.
|
||||
Unary {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::fmt::Write;
|
||||
|
||||
use crate::{
|
||||
ast::{ExprNode, ProgramNode, StmtNode},
|
||||
ast::{BinaryExpr, ExprNode, ProgramNode, StmtNode},
|
||||
tokens::Token,
|
||||
};
|
||||
|
||||
|
@ -181,6 +181,14 @@ fn dump_expression(expr: &ExprNode) -> 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) {
|
||||
match expr {
|
||||
ExprNode::Assignment { name, value, id: _ } => {
|
||||
|
@ -191,29 +199,8 @@ fn dump_expr_node(dumper: &mut Dumper, expr: &ExprNode) {
|
|||
dump_expr_node(dumper, value);
|
||||
}
|
||||
|
||||
ExprNode::Logical {
|
||||
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::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::Logical(binary_expr) => dump_binary_expr(dumper, binary_expr),
|
||||
ExprNode::Binary(binary_expr) => dump_binary_expr(dumper, binary_expr),
|
||||
|
||||
ExprNode::Unary { operator, right } => {
|
||||
dumper.current_line().push_str(&operator.lexeme);
|
||||
|
|
Loading…
Reference in a new issue