Update error message, add Int.__eq__

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-02-04 19:03:04 -08:00
parent 3b034052e5
commit 7eb4fa4ef6
2 changed files with 17 additions and 1 deletions

View File

@@ -13,6 +13,22 @@ pub struct IntObj {
thread_local! { thread_local! {
static VTABLE: VTable = VTableBuilder::default() 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::<IntObj>();
let rhs = rhs_ptr.as_any().downcast_ref::<IntObj>();
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::<FloatObj>()) {
// 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, _| { .with_builtin("__splat__", |machine, _| {
let rhs_ptr: ObjPtr = machine.stack_pop()?; let rhs_ptr: ObjPtr = machine.stack_pop()?;
let lhs_ptr: ObjPtr = machine.stack_pop()?; let lhs_ptr: ObjPtr = machine.stack_pop()?;

View File

@@ -15,7 +15,7 @@ pub enum RuntimeError {
#[error("cannot call value '{0}'")] #[error("cannot call value '{0}'")]
CannotCall(String), CannotCall(String),
#[error("expected {0} value")] #[error("expected {0}")]
WrongValue(String), WrongValue(String),
#[error("at {0}")] #[error("at {0}")]