Move builtin object methods from src/obj.rs to src/builtins.rs

Builtin functions are now living in the builtins file. They're still
part of BaseObjInst but they just are in a different file. Also,
implement the base "not" function.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2024-09-24 11:50:51 -07:00
parent 3a7c04686a
commit 4b5d2af117
3 changed files with 111 additions and 91 deletions

View File

@@ -324,87 +324,11 @@ pub trait Obj: Debug + Display + Any + Trace {
////////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Default, Trace)]
struct BaseObjInst {
pub(crate) struct BaseObjInst {
attrs: Attrs,
is_instantiated: bool,
}
//
// Base function implementations
//
impl BaseObjInst {
fn add(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __add__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn sub(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __sub__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn mul(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __mul__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn div(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __div__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn and(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __and__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn or(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __or__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn ne(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __ne__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn eq(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __eq__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn gt(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __gt__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn ge(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __ge__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn lt(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __lt__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn le(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __le__ function (self: {:?}, rhs: {:?})", vm.frame_stack()[0].borrow(), vm.frame_stack()[1].borrow())
}
fn pos(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __pos__ function (self: {:?})", vm.frame_stack()[0].borrow())
}
fn neg(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __neg__ function (self: {:?})", vm.frame_stack()[0].borrow())
}
fn not(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
todo!("Raise some kind of not implemented/not callable error for __not__ function (self: {:?})", vm.frame_stack()[0].borrow())
}
fn to_bool(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
vm.create_bool(vm.frame_stack()[0].borrow().is_truthy())
.into()
}
fn to_repr(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
let str_value = format!("{}", vm.frame_stack()[0].borrow());
vm.create_str(str_value).into()
}
}
impl Finalize for BaseObjInst {
fn finalize(&self) {}
}