1
0
This commit is contained in:
2024-11-22 11:04:41 +03:00
parent 8f1f9022ba
commit 1d1ed76969
2 changed files with 13 additions and 2 deletions

View File

@@ -21,6 +21,10 @@ pub fn optimize_expr(expr: TypedExpr) -> TypedExpr {
} }
pub fn bubble_binop_vars(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 = reorder_commutative_expr(expr);
let expr = match expr { let expr = match expr {
@@ -138,6 +142,10 @@ fn optimize_special_cases(lhs: TypedExpr, op: BinOp, rhs: TypedExpr) -> TypedExp
lhs 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 // Multiplication/Division by one
(lhs, TypedExpr::Int { value: 1, .. }) if matches!(op, BinOp::Mul | BinOp::Div) => lhs, (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, (lhs, TypedExpr::Float { value: 1.0, .. }) if matches!(op, BinOp::Mul | BinOp::Div) => lhs,

View File

@@ -78,8 +78,11 @@ impl fmt::Display for Type {
impl TypedExpr { impl TypedExpr {
pub fn cast_to_float(self) -> TypedExpr { pub fn cast_to_float(self) -> TypedExpr {
Self::IntToFloat { match self {
Self::IntToFloat { .. } | Self::Float { .. } => self,
_ => Self::IntToFloat {
value: Box::new(self), value: Box::new(self),
},
} }
} }