Introduce Purity enum for optimizer
Refactor the optimizer to use a Purity enum instead of a boolean for tracking function purity. This allows for a more granular representation of purity: - `Impure`: Functions with side effects. - `SideEffectFree`: Functions without side effects but may not be deterministic (e.g., `now()`, `random()`). - `Pure`: Functions without side effects and are deterministic. This change enhances the optimizer's ability to perform more aggressive optimizations by accurately determining function purity.
This commit is contained in:
+19
-18
@@ -1,3 +1,4 @@
|
||||
use crate::ast::compiler::optimizer::Purity;
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::types::{Signature, StaticType, Value};
|
||||
use std::rc::Rc;
|
||||
@@ -39,7 +40,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
ret: StaticType::DateTime,
|
||||
},
|
||||
]);
|
||||
env.register_native("+", add_ty, true, |args| {
|
||||
env.register_native("+", add_ty, Purity::Pure, |args| {
|
||||
if args.len() == 2 {
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a + b),
|
||||
@@ -99,7 +100,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
ret: StaticType::DateTime,
|
||||
},
|
||||
]);
|
||||
env.register_native("-", sub_ty, true, |args| {
|
||||
env.register_native("-", sub_ty, Purity::Pure, |args| {
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -152,7 +153,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
ret: StaticType::Float,
|
||||
},
|
||||
]);
|
||||
env.register_native("*", mul_ty, true, |args| {
|
||||
env.register_native("*", mul_ty, Purity::Pure, |args| {
|
||||
if args.len() == 2 {
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a * b),
|
||||
@@ -189,7 +190,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
ret: StaticType::Float,
|
||||
},
|
||||
]);
|
||||
env.register_native("/", div_ty, true, |args| {
|
||||
env.register_native("/", div_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -215,7 +216,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
env.register_native("//", int_div_ty, true, |args| {
|
||||
env.register_native("//", int_div_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -238,7 +239,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
env.register_native("%", mod_ty, true, |args| {
|
||||
env.register_native("%", mod_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -272,7 +273,7 @@ fn register_comparison(env: &Environment) {
|
||||
]);
|
||||
|
||||
// --- Greater Than (>) ---
|
||||
env.register_native(">", cmp_ty.clone(), true, |args| {
|
||||
env.register_native(">", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -287,7 +288,7 @@ fn register_comparison(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Less Than (<) ---
|
||||
env.register_native("<", cmp_ty.clone(), true, |args| {
|
||||
env.register_native("<", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -302,7 +303,7 @@ fn register_comparison(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Greater Or Equal (>=) ---
|
||||
env.register_native(">=", cmp_ty.clone(), true, |args| {
|
||||
env.register_native(">=", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -317,7 +318,7 @@ fn register_comparison(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Less Or Equal (<=) ---
|
||||
env.register_native("<=", cmp_ty.clone(), true, |args| {
|
||||
env.register_native("<=", cmp_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -336,7 +337,7 @@ fn register_comparison(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Any, StaticType::Any]),
|
||||
ret: StaticType::Bool,
|
||||
}));
|
||||
env.register_native("=", eq_ty.clone(), true, |args| {
|
||||
env.register_native("=", eq_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -357,7 +358,7 @@ fn register_comparison(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Not Equal (<>) ---
|
||||
env.register_native("<>", eq_ty, true, |args| {
|
||||
env.register_native("<>", eq_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -388,7 +389,7 @@ fn register_logic(env: &Environment) {
|
||||
ret: StaticType::Int,
|
||||
},
|
||||
]);
|
||||
env.register_native("not", not_ty, true, |args| {
|
||||
env.register_native("not", not_ty, Purity::Pure, |args| {
|
||||
if args.len() != 1 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -410,7 +411,7 @@ fn register_logic(env: &Environment) {
|
||||
ret: StaticType::Int,
|
||||
},
|
||||
]);
|
||||
env.register_native("and", logic_op_ty.clone(), true, |args| {
|
||||
env.register_native("and", logic_op_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -422,7 +423,7 @@ fn register_logic(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Or (or) ---
|
||||
env.register_native("or", logic_op_ty.clone(), true, |args| {
|
||||
env.register_native("or", logic_op_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -434,7 +435,7 @@ fn register_logic(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Xor (xor) ---
|
||||
env.register_native("xor", logic_op_ty.clone(), true, |args| {
|
||||
env.register_native("xor", logic_op_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -450,7 +451,7 @@ fn register_logic(env: &Environment) {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
env.register_native("<<", shift_ty.clone(), true, |args| {
|
||||
env.register_native("<<", shift_ty.clone(), Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
@@ -461,7 +462,7 @@ fn register_logic(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Shift Right (>>) ---
|
||||
env.register_native(">>", shift_ty, true, |args| {
|
||||
env.register_native(">>", shift_ty, Purity::Pure, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::ast::compiler::optimizer::Purity;
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::types::{Signature, StaticType, Value};
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
@@ -8,7 +9,7 @@ pub fn register(env: &Environment) {
|
||||
ret: StaticType::DateTime,
|
||||
}));
|
||||
|
||||
env.register_native("date", date_ty, true, |args| {
|
||||
env.register_native("date", date_ty, Purity::Pure, |args| {
|
||||
if let Value::Text(s) = &args[0] {
|
||||
// Try parse YYYY-MM-DD
|
||||
if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
|
||||
|
||||
Reference in New Issue
Block a user