f35606616b
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.
31 lines
1.1 KiB
Rust
31 lines
1.1 KiB
Rust
use crate::ast::compiler::optimizer::Purity;
|
|
use crate::ast::environment::Environment;
|
|
use crate::ast::types::{Signature, StaticType, Value};
|
|
use chrono::{NaiveDate, NaiveDateTime};
|
|
|
|
pub fn register(env: &Environment) {
|
|
let date_ty = StaticType::Function(Box::new(Signature {
|
|
params: StaticType::Tuple(vec![StaticType::Text]),
|
|
ret: StaticType::DateTime,
|
|
}));
|
|
|
|
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") {
|
|
let ts = dt
|
|
.and_hms_opt(0, 0, 0)
|
|
.unwrap()
|
|
.and_utc()
|
|
.timestamp_millis();
|
|
return Value::DateTime(ts);
|
|
}
|
|
// Try parse YYYY-MM-DD HH:MM:SS
|
|
if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
|
|
return Value::DateTime(dt.and_utc().timestamp_millis());
|
|
}
|
|
}
|
|
Value::Void
|
|
});
|
|
}
|