From 7eb4fa4ef69e00e577963bd99b5752ac18bae811 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Fri, 4 Feb 2022 19:03:04 -0800 Subject: [PATCH] Update error message, add Int.__eq__ Signed-off-by: Alek Ratzloff --- src/obj/int.rs | 16 ++++++++++++++++ src/vm/error.rs | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/obj/int.rs b/src/obj/int.rs index 600a052..029dc5f 100644 --- a/src/obj/int.rs +++ b/src/obj/int.rs @@ -13,6 +13,22 @@ pub struct IntObj { thread_local! { static VTABLE: VTable = VTableBuilder::default() + .with_builtin("__eq__", |machine, _| { + let rhs_ptr: ObjPtr = machine.stack_pop()?; + let lhs_ptr: ObjPtr = machine.stack_pop()?; + let lhs = lhs_ptr.as_any().downcast_ref::(); + let rhs = rhs_ptr.as_any().downcast_ref::(); + let result = if let (Some(lhs), Some(rhs)) = (lhs, rhs) { + lhs.value() == rhs.value() + } else if let (Some(lhs), Some(rhs)) = (lhs, rhs_ptr.as_any().downcast_ref::()) { + // implicit upcast for this comparison allowed + lhs.value() as Float == rhs.value() + } else { + false + }; + machine.stack_push(BoolObj::new(result))?; + Ok(BuiltinExit::Return) + }) .with_builtin("__splat__", |machine, _| { let rhs_ptr: ObjPtr = machine.stack_pop()?; let lhs_ptr: ObjPtr = machine.stack_pop()?; diff --git a/src/vm/error.rs b/src/vm/error.rs index 6e3ef8a..264c8ab 100644 --- a/src/vm/error.rs +++ b/src/vm/error.rs @@ -15,7 +15,7 @@ pub enum RuntimeError { #[error("cannot call value '{0}'")] CannotCall(String), - #[error("expected {0} value")] + #[error("expected {0}")] WrongValue(String), #[error("at {0}")]