From 1d1ed76969174e948dd6e8bccc4cb91678937d5f Mon Sep 17 00:00:00 2001 From: lionarius Date: Fri, 22 Nov 2024 11:04:41 +0300 Subject: [PATCH] .. --- src/ast/optimization.rs | 8 ++++++++ src/ast/typed.rs | 7 +++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/ast/optimization.rs b/src/ast/optimization.rs index b269d5d..f1a269a 100644 --- a/src/ast/optimization.rs +++ b/src/ast/optimization.rs @@ -21,6 +21,10 @@ pub fn optimize_expr(expr: TypedExpr) -> TypedExpr { } pub fn bubble_binop_vars(expr: TypedExpr) -> TypedExpr { + if expr.is_const() { + return expr; + } + let expr = reorder_commutative_expr(expr); let expr = match expr { @@ -138,6 +142,10 @@ fn optimize_special_cases(lhs: TypedExpr, op: BinOp, rhs: TypedExpr) -> TypedExp lhs }, + // Subtraction by zero + (lhs, TypedExpr::Int { value: 0, .. }) if matches!(op, BinOp::Sub) => lhs, + (lhs, TypedExpr::Float { value: 0.0, .. }) if matches!(op, BinOp::Sub) => lhs, + // Multiplication/Division by one (lhs, TypedExpr::Int { value: 1, .. }) if matches!(op, BinOp::Mul | BinOp::Div) => lhs, (lhs, TypedExpr::Float { value: 1.0, .. }) if matches!(op, BinOp::Mul | BinOp::Div) => lhs, diff --git a/src/ast/typed.rs b/src/ast/typed.rs index ae6d4a7..a9bcfa4 100644 --- a/src/ast/typed.rs +++ b/src/ast/typed.rs @@ -78,8 +78,11 @@ impl fmt::Display for Type { impl TypedExpr { pub fn cast_to_float(self) -> TypedExpr { - Self::IntToFloat { - value: Box::new(self), + match self { + Self::IntToFloat { .. } | Self::Float { .. } => self, + _ => Self::IntToFloat { + value: Box::new(self), + }, } }