diff --git a/src/ast/expr.rs b/src/ast/expr.rs --- a/src/ast/expr.rs +++ b/src/ast/expr.rs @@ -32,6 +32,7 @@ }; Spanned::new(e, span) } + pub fn grouping( left_paren: &Token, inner: Spanned, @@ -52,6 +53,7 @@ let e = Self::Literal { lit }; Spanned::new(e, span) } + pub fn unary(op: Spanned, expr: Spanned) -> Spanned { let span = op.span().join(&expr.span()); let e = Self::Unary { @@ -102,24 +104,34 @@ } } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, strum::Display)] pub enum BinaryOp { // Equality + #[strum(to_string = "==")] Eq, + #[strum(to_string = "!=")] NotEq, // Comparison + #[strum(to_string = ">")] Greater, + #[strum(to_string = ">=")] GreaterEq, + #[strum(to_string = "<")] Less, + #[strum(to_string = "<=")] LessEq, // Term + #[strum(to_string = "+")] Add, + #[strum(to_string = "-")] Sub, // Factor + #[strum(to_string = "*")] Mult, + #[strum(to_string = "/")] Div, } @@ -208,19 +220,7 @@ Expr::Binary { left, op, right } => { let left = left.print_rpn(); let right = right.print_rpn(); - let op_str = match op.as_ref() { - BinaryOp::Eq => "==", - BinaryOp::NotEq => "!=", - BinaryOp::Greater => ">", - BinaryOp::GreaterEq => ">=", - BinaryOp::Less => "<", - BinaryOp::LessEq => "<=", - BinaryOp::Add => "+", - BinaryOp::Sub => "-", - BinaryOp::Mult => "*", - BinaryOp::Div => "/", - }; - format!("({op_str} {left} {right})") + format!("({} {left} {right})", op.as_ref()) } Expr::Grouping { inner } => format!("({})", inner.print_rpn()), Expr::Literal { lit } => match lit.as_ref() {