Add RTL functions for arithmetic and comparisons
This commit introduces a new module `ast::rtl` to house the standard runtime library functions for the language. It includes implementations for arithmetic operators (+, -, *, /, //, %), logical operators (and, or, xor, not), bitwise shifts (<<, >>), and comparison operators (=, <>, <, >, <=, >=). Additionally, a `date` function for parsing datetime strings has been added. This enhances the language's capability to handle basic mathematical and logical operations. Add RTL functions for arithmetic and comparisons This commit introduces the runtime library (RTL) functions for basic arithmetic operations, logical/bitwise operations, and comparisons. It also includes a `date` function for parsing datetime strings. Integration tests have been updated to cover these new functionalities.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
use crate::ast::types::{Value, StaticType, Signature};
|
||||
use crate::ast::environment::Environment;
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
let date_ty = StaticType::Function(Box::new(Signature {
|
||||
params: vec![StaticType::Text],
|
||||
ret: StaticType::DateTime
|
||||
}));
|
||||
|
||||
env.register_native("date", date_ty, |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
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user