Add float division and integer/float coercion
This commit introduces support for floating-point division, including cases where an integer is divided by a float or vice versa. This allows for more flexible arithmetic operations within the DSL. Additionally, the `Value::Float` display implementation is adjusted to show `.0` for whole numbers, improving readability and consistency in output.
This commit is contained in:
@@ -35,6 +35,14 @@ fn register_arithmetic(env: &Environment) {
|
|||||||
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
||||||
ret: StaticType::Float,
|
ret: StaticType::Float,
|
||||||
},
|
},
|
||||||
|
Signature {
|
||||||
|
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Float]),
|
||||||
|
ret: StaticType::Float,
|
||||||
|
},
|
||||||
|
Signature {
|
||||||
|
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Int]),
|
||||||
|
ret: StaticType::Float,
|
||||||
|
},
|
||||||
Signature {
|
Signature {
|
||||||
params: StaticType::Tuple(vec![StaticType::Text, StaticType::Text]),
|
params: StaticType::Tuple(vec![StaticType::Text, StaticType::Text]),
|
||||||
ret: StaticType::Text,
|
ret: StaticType::Text,
|
||||||
@@ -94,6 +102,14 @@ fn register_arithmetic(env: &Environment) {
|
|||||||
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
||||||
ret: StaticType::Float,
|
ret: StaticType::Float,
|
||||||
},
|
},
|
||||||
|
Signature {
|
||||||
|
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Float]),
|
||||||
|
ret: StaticType::Float,
|
||||||
|
},
|
||||||
|
Signature {
|
||||||
|
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Int]),
|
||||||
|
ret: StaticType::Float,
|
||||||
|
},
|
||||||
Signature {
|
Signature {
|
||||||
params: StaticType::Tuple(vec![StaticType::Int]),
|
params: StaticType::Tuple(vec![StaticType::Int]),
|
||||||
ret: StaticType::Int,
|
ret: StaticType::Int,
|
||||||
@@ -169,6 +185,14 @@ fn register_arithmetic(env: &Environment) {
|
|||||||
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
||||||
ret: StaticType::Float,
|
ret: StaticType::Float,
|
||||||
},
|
},
|
||||||
|
Signature {
|
||||||
|
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Float]),
|
||||||
|
ret: StaticType::Float,
|
||||||
|
},
|
||||||
|
Signature {
|
||||||
|
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Int]),
|
||||||
|
ret: StaticType::Float,
|
||||||
|
},
|
||||||
// Variadic: any number of numeric args
|
// Variadic: any number of numeric args
|
||||||
Signature {
|
Signature {
|
||||||
params: StaticType::Variadic(Box::new(StaticType::Float)),
|
params: StaticType::Variadic(Box::new(StaticType::Float)),
|
||||||
@@ -212,6 +236,14 @@ fn register_arithmetic(env: &Environment) {
|
|||||||
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
||||||
ret: StaticType::Float,
|
ret: StaticType::Float,
|
||||||
},
|
},
|
||||||
|
Signature {
|
||||||
|
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Float]),
|
||||||
|
ret: StaticType::Float,
|
||||||
|
},
|
||||||
|
Signature {
|
||||||
|
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Int]),
|
||||||
|
ret: StaticType::Float,
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
env.register_native_fn("/", div_ty, Purity::Pure, |args| {
|
env.register_native_fn("/", div_ty, Purity::Pure, |args| {
|
||||||
if args.len() != 2 {
|
if args.len() != 2 {
|
||||||
|
|||||||
+7
-1
@@ -784,7 +784,13 @@ impl fmt::Display for Value {
|
|||||||
Value::Void => write!(f, "void"),
|
Value::Void => write!(f, "void"),
|
||||||
Value::Bool(b) => write!(f, "{}", b),
|
Value::Bool(b) => write!(f, "{}", b),
|
||||||
Value::Int(i) => write!(f, "{}", i),
|
Value::Int(i) => write!(f, "{}", i),
|
||||||
Value::Float(fl) => write!(f, "{}", fl),
|
Value::Float(fl) => {
|
||||||
|
if fl.fract() == 0.0 {
|
||||||
|
write!(f, "{:.1}", fl)
|
||||||
|
} else {
|
||||||
|
write!(f, "{}", fl)
|
||||||
|
}
|
||||||
|
}
|
||||||
Value::DateTime(ts) => match Utc.timestamp_millis_opt(*ts) {
|
Value::DateTime(ts) => match Utc.timestamp_millis_opt(*ts) {
|
||||||
chrono::LocalResult::Single(dt) => {
|
chrono::LocalResult::Single(dt) => {
|
||||||
write!(f, "#{}#", dt.format("%Y-%m-%d %H:%M:%S"))
|
write!(f, "#{}#", dt.format("%Y-%m-%d %H:%M:%S"))
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@ fn test_rtl_operators() {
|
|||||||
assert_eq!(format!("{}", env.run_script("(+ 10 20)").unwrap()), "30");
|
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("(- 20 10)").unwrap()), "10");
|
||||||
assert_eq!(format!("{}", env.run_script("(* 10 20)").unwrap()), "200");
|
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 10)").unwrap()), "2.0");
|
||||||
assert_eq!(format!("{}", env.run_script("(// 20 3)").unwrap()), "6");
|
assert_eq!(format!("{}", env.run_script("(// 20 3)").unwrap()), "6");
|
||||||
assert_eq!(format!("{}", env.run_script("(% 20 3)").unwrap()), "2");
|
assert_eq!(format!("{}", env.run_script("(% 20 3)").unwrap()), "2");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user