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:
Michael Schimmel
2026-02-22 08:42:22 +01:00
parent 329b885c4b
commit f35606616b
4 changed files with 86 additions and 60 deletions
+2 -1
View File
@@ -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") {