feat: support floats in ordering operators

This commit is contained in:
Roman Godmaire 2024-05-06 22:35:39 -04:00
parent e4484297a9
commit 872195f6a5

View file

@ -279,8 +279,15 @@ pub(super) fn core() -> HashMap<String, Node> {
Node::NativeFunc(|_env, args| { Node::NativeFunc(|_env, args| {
arg_count!(2, args.len()); arg_count!(2, args.len());
let less_than = match (args[0].borrow(), args[1].borrow()) { let mut args = args.into_iter();
let lhs = args.next().unwrap();
let rhs = args.next().unwrap();
let less_than = match (lhs, rhs) {
(Node::Int(lhs), Node::Int(rhs)) => lhs < rhs, (Node::Int(lhs), Node::Int(rhs)) => lhs < rhs,
(Node::Float(lhs), Node::Float(rhs)) => lhs < rhs,
(Node::Float(lhs), Node::Int(rhs)) => lhs < rhs as f64,
(Node::Int(lhs), Node::Float(rhs)) => (lhs as f64) < rhs,
_ => Err(Error::ExpectedNumber)?, _ => Err(Error::ExpectedNumber)?,
}; };
@ -292,8 +299,15 @@ pub(super) fn core() -> HashMap<String, Node> {
Node::NativeFunc(|_env, args| { Node::NativeFunc(|_env, args| {
arg_count!(2, args.len()); arg_count!(2, args.len());
let greater_than = match (args[0].borrow(), args[1].borrow()) { let mut args = args.into_iter();
let lhs = args.next().unwrap();
let rhs = args.next().unwrap();
let greater_than = match (lhs, rhs) {
(Node::Int(lhs), Node::Int(rhs)) => lhs > rhs, (Node::Int(lhs), Node::Int(rhs)) => lhs > rhs,
(Node::Float(lhs), Node::Float(rhs)) => lhs > rhs,
(Node::Float(lhs), Node::Int(rhs)) => lhs > rhs as f64,
(Node::Int(lhs), Node::Float(rhs)) => (lhs as f64) > rhs,
_ => Err(Error::ExpectedNumber)?, _ => Err(Error::ExpectedNumber)?,
}; };
@ -305,8 +319,15 @@ pub(super) fn core() -> HashMap<String, Node> {
Node::NativeFunc(|_env, args| { Node::NativeFunc(|_env, args| {
arg_count!(2, args.len()); arg_count!(2, args.len());
let less_than_equal = match (args[0].borrow(), args[1].borrow()) { let mut args = args.into_iter();
let lhs = args.next().unwrap();
let rhs = args.next().unwrap();
let less_than_equal = match (lhs, rhs) {
(Node::Int(lhs), Node::Int(rhs)) => lhs <= rhs, (Node::Int(lhs), Node::Int(rhs)) => lhs <= rhs,
(Node::Float(lhs), Node::Float(rhs)) => lhs <= rhs,
(Node::Float(lhs), Node::Int(rhs)) => lhs <= rhs as f64,
(Node::Int(lhs), Node::Float(rhs)) => (lhs as f64) <= rhs,
_ => Err(Error::ExpectedNumber)?, _ => Err(Error::ExpectedNumber)?,
}; };
@ -318,8 +339,15 @@ pub(super) fn core() -> HashMap<String, Node> {
Node::NativeFunc(|_env, args| { Node::NativeFunc(|_env, args| {
arg_count!(2, args.len()); arg_count!(2, args.len());
let greater_than_equal = match (args[0].borrow(), args[1].borrow()) { let mut args = args.into_iter();
let lhs = args.next().unwrap();
let rhs = args.next().unwrap();
let greater_than_equal = match (lhs, rhs) {
(Node::Int(lhs), Node::Int(rhs)) => lhs >= rhs, (Node::Int(lhs), Node::Int(rhs)) => lhs >= rhs,
(Node::Float(lhs), Node::Float(rhs)) => lhs >= rhs,
(Node::Float(lhs), Node::Int(rhs)) => lhs >= rhs as f64,
(Node::Int(lhs), Node::Float(rhs)) => (lhs as f64) >= rhs,
_ => Err(Error::ExpectedNumber)?, _ => Err(Error::ExpectedNumber)?,
}; };