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:
@@ -152,4 +152,50 @@ mod tests {
|
||||
assert!(has_const, "Logs should contain CONST(10)");
|
||||
assert!(has_result, "Logs should contain result 30");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rtl_operators() {
|
||||
let env = Environment::new();
|
||||
|
||||
// --- Arithmetic ---
|
||||
assert_eq!(format!("{}", env.run_script("(+ 10 20)").unwrap()), "30");
|
||||
assert_eq!(format!("{}", env.run_script("(- 20 10)").unwrap()), "10");
|
||||
assert_eq!(format!("{}", env.run_script("(* 10 20)").unwrap()), "200");
|
||||
assert_eq!(format!("{}", env.run_script("(/ 20 10)").unwrap()), "2");
|
||||
assert_eq!(format!("{}", env.run_script("(// 20 3)").unwrap()), "6"); // 20 // 3 = 6
|
||||
assert_eq!(format!("{}", env.run_script("(% 20 3)").unwrap()), "2"); // 20 % 3 = 2
|
||||
|
||||
// --- Logic / Bitwise ---
|
||||
assert_eq!(format!("{}", env.run_script("(and true false)").unwrap()), "false");
|
||||
assert_eq!(format!("{}", env.run_script("(or true false)").unwrap()), "true");
|
||||
assert_eq!(format!("{}", env.run_script("(xor true false)").unwrap()), "true");
|
||||
assert_eq!(format!("{}", env.run_script("(not true)").unwrap()), "false");
|
||||
|
||||
assert_eq!(format!("{}", env.run_script("(<< 1 2)").unwrap()), "4"); // 1 << 2 = 4
|
||||
assert_eq!(format!("{}", env.run_script("(>> 4 1)").unwrap()), "2"); // 4 >> 1 = 2
|
||||
assert_eq!(format!("{}", env.run_script("(and 3 1)").unwrap()), "1"); // 3 & 1 = 1
|
||||
|
||||
// --- Comparison ---
|
||||
assert_eq!(format!("{}", env.run_script("(= 10 10)").unwrap()), "true");
|
||||
assert_eq!(format!("{}", env.run_script("(= 10 20)").unwrap()), "false");
|
||||
assert_eq!(format!("{}", env.run_script("(<> 10 20)").unwrap()), "true");
|
||||
assert_eq!(format!("{}", env.run_script("(< 10 20)").unwrap()), "true");
|
||||
assert_eq!(format!("{}", env.run_script("(> 10 20)").unwrap()), "false");
|
||||
assert_eq!(format!("{}", env.run_script("(<= 10 10)").unwrap()), "true");
|
||||
assert_eq!(format!("{}", env.run_script("(>= 10 10)").unwrap()), "true");
|
||||
|
||||
// --- NaN ---
|
||||
assert_eq!(format!("{}", env.run_script("NaN").unwrap()), "NaN");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_date_parsing() {
|
||||
let env = Environment::new();
|
||||
let res = env.run_script("(date \"2023-01-01\")").unwrap();
|
||||
if let Value::DateTime(_) = res {
|
||||
// OK
|
||||
} else {
|
||||
panic!("Expected DateTime, got {:?}", res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user