From 872195f6a548f2897341952594206dd8957e35fe Mon Sep 17 00:00:00 2001 From: Roman Godmaire Date: Mon, 6 May 2024 22:35:39 -0400 Subject: [PATCH] feat: support floats in ordering operators --- mute-interpreter/src/env/core.rs | 36 ++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/mute-interpreter/src/env/core.rs b/mute-interpreter/src/env/core.rs index f17f1ca..f0a0d66 100644 --- a/mute-interpreter/src/env/core.rs +++ b/mute-interpreter/src/env/core.rs @@ -279,8 +279,15 @@ pub(super) fn core() -> HashMap { Node::NativeFunc(|_env, args| { 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::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)?, }; @@ -292,8 +299,15 @@ pub(super) fn core() -> HashMap { Node::NativeFunc(|_env, args| { 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::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)?, }; @@ -305,8 +319,15 @@ pub(super) fn core() -> HashMap { Node::NativeFunc(|_env, args| { 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::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)?, }; @@ -318,8 +339,15 @@ pub(super) fn core() -> HashMap { Node::NativeFunc(|_env, args| { 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::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)?, };