Remove BaseObj and fix Object::equals
* BaseObj felt a bit redundant. For everything that BaseObj did, we use
Obj instead.
* Object::equals was a little weird. It was used for giving back
equality, except when it wasn't. It's a little better defined now,
here's what I'm shooting for:
* *In general*, Object::equals will return true when two objects
refer to the same object.
* The exception to this rule is for "constant" objects, or "copy on
write" objects. These include, but are not limited to: Int, Float,
Bool, Nil, Str. Their base values are immutable and are the heart
of object equality.
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
126
src/obj/ty.rs
126
src/obj/ty.rs
@@ -4,14 +4,12 @@ use std::rc::Rc;
|
||||
|
||||
use gc::{Finalize, Trace};
|
||||
|
||||
use crate::obj::macros::*;
|
||||
use crate::obj::prelude::*;
|
||||
use crate::obj::{BaseObj, BUILTINS};
|
||||
use crate::obj::{macros::*, prelude::*, Obj, BUILTINS};
|
||||
use crate::vm::{Argc, Vm};
|
||||
|
||||
#[derive(Trace, Finalize)]
|
||||
pub struct Ty {
|
||||
base: BaseObj,
|
||||
base: Obj,
|
||||
#[unsafe_ignore_trace]
|
||||
name: Rc<String>,
|
||||
vtable: HashMap<String, ObjP>,
|
||||
@@ -55,20 +53,6 @@ impl Display for Ty {
|
||||
}
|
||||
|
||||
impl Object for Ty {
|
||||
fn equals(&self, other: &dyn Object) -> bool {
|
||||
if let Some(other) = other.as_any().downcast_ref::<Ty>() {
|
||||
// TODO Ty::equals : something more robust than this
|
||||
// Tys should hold equality if they have the same name
|
||||
// the problem is that Ty.get_attr("__ty__") is going to return itself, so we have
|
||||
// to go through attributes to specially exclude to the __ty__ attribute if it points
|
||||
// to ourself.
|
||||
// How do we detect that it's pointing to ourself? I suppose pointers are the way
|
||||
self.name == other.name
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&self, vm: &mut Vm, argc: Argc) {
|
||||
// TODO Object::call - need to handle "this object cannot be called" errors
|
||||
// BLOCKED-ON: exceptions
|
||||
@@ -167,54 +151,56 @@ pub fn init_types() {
|
||||
base_type: Ty,
|
||||
// type definitions
|
||||
Ty {
|
||||
// Conversion methods
|
||||
to_str => BuiltinFunction::create("to_str", BaseObj::to_str, 1),
|
||||
to_repr => BuiltinFunction::create("to_repr", BaseObj::to_repr, 1),
|
||||
to_bool => BuiltinFunction::create("to_bool", BaseObj::to_bool, 1),
|
||||
to_int => BuiltinFunction::create("to_int", BaseObj::not_implemented_un, 1),
|
||||
to_float => BuiltinFunction::create("to_float", BaseObj::not_implemented_un, 1),
|
||||
to_list => BuiltinFunction::create("to_list", BaseObj::not_implemented_un, 1),
|
||||
|
||||
// Constructor
|
||||
// TODO Ty::do_call, Ty::init - implement these methods
|
||||
__call__ => BuiltinFunction::create("__call__", BaseObj::not_implemented_un, 1),
|
||||
__init__ => BuiltinFunction::create("__init__", BaseObj::not_implemented_un, 1),
|
||||
__call__ => BuiltinFunction::create("__call__", Obj::not_implemented_un, 1),
|
||||
__init__ => BuiltinFunction::create("__init__", Obj::not_implemented_un, 1),
|
||||
|
||||
// Conversion methods
|
||||
to_str => BuiltinFunction::create("to_str", Obj::to_str, 1),
|
||||
to_repr => BuiltinFunction::create("to_repr", Obj::to_repr, 1),
|
||||
to_bool => BuiltinFunction::create("to_bool", Obj::to_bool, 1),
|
||||
to_int => BuiltinFunction::create("to_int", Obj::not_implemented_un, 1),
|
||||
to_float => BuiltinFunction::create("to_float", Obj::not_implemented_un, 1),
|
||||
to_list => BuiltinFunction::create("to_list", Obj::not_implemented_un, 1),
|
||||
|
||||
// Operators
|
||||
__add__ => BuiltinFunction::create("__add__", BaseObj::not_implemented_bin, 2),
|
||||
__sub__ => BuiltinFunction::create("__sub__", BaseObj::not_implemented_bin, 2),
|
||||
__mul__ => BuiltinFunction::create("__mul__", BaseObj::not_implemented_bin, 2),
|
||||
__div__ => BuiltinFunction::create("__div__", BaseObj::not_implemented_bin, 2),
|
||||
__and__ => BuiltinFunction::create("__and__", BaseObj::and, 2),
|
||||
__or__ => BuiltinFunction::create("__or__", BaseObj::or, 2),
|
||||
__ne__ => BuiltinFunction::create("__ne__", BaseObj::ne, 2),
|
||||
__eq__ => BuiltinFunction::create("__eq__", BaseObj::eq, 2),
|
||||
__gt__ => BuiltinFunction::create("__gt__", BaseObj::not_implemented_bin, 2),
|
||||
__ge__ => BuiltinFunction::create("__ge__", BaseObj::not_implemented_bin, 2),
|
||||
__lt__ => BuiltinFunction::create("__lt__", BaseObj::not_implemented_bin, 2),
|
||||
__le__ => BuiltinFunction::create("__le__", BaseObj::not_implemented_bin, 2),
|
||||
__pos__ => BuiltinFunction::create("__pos__", BaseObj::not_implemented_un, 1),
|
||||
__neg__ => BuiltinFunction::create("__neg__", BaseObj::not_implemented_un, 1),
|
||||
__not__ => BuiltinFunction::create("__not__", BaseObj::not, 1),
|
||||
__add__ => BuiltinFunction::create("__add__", Obj::not_implemented_bin, 2),
|
||||
__sub__ => BuiltinFunction::create("__sub__", Obj::not_implemented_bin, 2),
|
||||
__mul__ => BuiltinFunction::create("__mul__", Obj::not_implemented_bin, 2),
|
||||
__div__ => BuiltinFunction::create("__div__", Obj::not_implemented_bin, 2),
|
||||
__and__ => BuiltinFunction::create("__and__", Obj::and, 2),
|
||||
__or__ => BuiltinFunction::create("__or__", Obj::or, 2),
|
||||
__ne__ => BuiltinFunction::create("__ne__", Obj::ne, 2),
|
||||
__eq__ => BuiltinFunction::create("__eq__", Obj::eq, 2),
|
||||
__gt__ => BuiltinFunction::create("__gt__", Obj::not_implemented_bin, 2),
|
||||
__ge__ => BuiltinFunction::create("__ge__", Obj::not_implemented_bin, 2),
|
||||
__lt__ => BuiltinFunction::create("__lt__", Obj::not_implemented_bin, 2),
|
||||
__le__ => BuiltinFunction::create("__le__", Obj::not_implemented_bin, 2),
|
||||
__pos__ => BuiltinFunction::create("__pos__", Obj::not_implemented_un, 1),
|
||||
__neg__ => BuiltinFunction::create("__neg__", Obj::not_implemented_un, 1),
|
||||
__not__ => BuiltinFunction::create("__not__", Obj::not, 1),
|
||||
|
||||
// Methods
|
||||
len => BuiltinFunction::create("len", BaseObj::not_implemented_un, 1),
|
||||
len => BuiltinFunction::create("len", Obj::not_implemented_un, 1),
|
||||
hash => BuiltinFunction::create("hash", Obj::not_implemented_bin, 2),
|
||||
},
|
||||
Obj {
|
||||
// Constructor
|
||||
__call__ => BuiltinFunction::create("__call__", Obj::do_call, 1),
|
||||
__init__ => BuiltinFunction::create("__init__", Obj::init, 1),
|
||||
|
||||
// Methods
|
||||
},
|
||||
List {
|
||||
// Conversion methods
|
||||
to_repr => BuiltinFunction::create("to_repr", List::to_repr, 1),
|
||||
to_list => BuiltinFunction::create("to_list", List::to_list, 1),
|
||||
|
||||
// Constructor
|
||||
__call__ => BuiltinFunction::create("__call__", List::do_call, 2),
|
||||
__init__ => BuiltinFunction::create("__init__", List::init, 2),
|
||||
|
||||
// Conversion methods
|
||||
to_repr => BuiltinFunction::create("to_repr", List::to_repr, 1),
|
||||
to_list => BuiltinFunction::create("to_list", List::to_list, 1),
|
||||
|
||||
// Operators
|
||||
__index__ => BuiltinFunction::create("__index__", List::index, 2),
|
||||
|
||||
@@ -226,16 +212,16 @@ pub fn init_types() {
|
||||
extend => BuiltinFunction::create("extend", List::extend, 2),
|
||||
},
|
||||
Str {
|
||||
// Constructor
|
||||
__call__ => BuiltinFunction::create("__call__", Str::do_call, 2),
|
||||
__init__ => BuiltinFunction::create("__init__", Str::init, 2),
|
||||
|
||||
// Conversion methods
|
||||
to_str => BuiltinFunction::create("to_str", Str::to_str, 1),
|
||||
to_int => BuiltinFunction::create("to_int", Str::to_int, 1),
|
||||
to_float => BuiltinFunction::create("to_float", Str::to_float, 1),
|
||||
to_list => BuiltinFunction::create("to_list", Str::to_list, 1),
|
||||
|
||||
// Constructor
|
||||
__call__ => BuiltinFunction::create("__call__", Str::do_call, 2),
|
||||
__init__ => BuiltinFunction::create("__init__", Str::init, 2),
|
||||
|
||||
// Operators
|
||||
__add__ => BuiltinFunction::create("__add__", Str::add, 2),
|
||||
__mul__ => BuiltinFunction::create("__mul__", Str::mul, 2),
|
||||
@@ -247,14 +233,14 @@ pub fn init_types() {
|
||||
// TODO Str methods - .lower, .upper, .slice, etc
|
||||
},
|
||||
Int {
|
||||
// Conversion methods
|
||||
to_int => BuiltinFunction::create("to_int", Int::to_int, 1),
|
||||
to_float => BuiltinFunction::create("to_float", Int::to_float, 1),
|
||||
|
||||
// Constructor
|
||||
__call__ => BuiltinFunction::create("__call__", Int::do_call, 2),
|
||||
__init__ => BuiltinFunction::create("__init__", Int::init, 2),
|
||||
|
||||
// Conversion methods
|
||||
to_int => BuiltinFunction::create("to_int", Int::to_int, 1),
|
||||
to_float => BuiltinFunction::create("to_float", Int::to_float, 1),
|
||||
|
||||
// Operators
|
||||
__add__ => BuiltinFunction::create("__add__", Int::add, 2),
|
||||
__sub__ => BuiltinFunction::create("__sub__", Int::sub, 2),
|
||||
@@ -270,14 +256,14 @@ pub fn init_types() {
|
||||
// Methods
|
||||
},
|
||||
Float {
|
||||
// Conversion methods
|
||||
to_int => BuiltinFunction::create("to_int", Float::to_int, 1),
|
||||
to_float => BuiltinFunction::create("to_float", Float::to_float, 1),
|
||||
|
||||
// Constructor
|
||||
__call__ => BuiltinFunction::create("__call__", Float::do_call, 2),
|
||||
__init__ => BuiltinFunction::create("__init__", Float::init, 2),
|
||||
|
||||
// Conversion methods
|
||||
to_int => BuiltinFunction::create("to_int", Float::to_int, 1),
|
||||
to_float => BuiltinFunction::create("to_float", Float::to_float, 1),
|
||||
|
||||
// Operators
|
||||
__add__ => BuiltinFunction::create("__add__", Float::add, 2),
|
||||
__sub__ => BuiltinFunction::create("__sub__", Float::sub, 2),
|
||||
@@ -292,48 +278,48 @@ pub fn init_types() {
|
||||
// Methods
|
||||
},
|
||||
Bool {
|
||||
// Conversion methods
|
||||
to_int => BuiltinFunction::create("to_int", Bool::to_int, 1),
|
||||
to_float => BuiltinFunction::create("to_float", Bool::to_float, 1),
|
||||
|
||||
// Constructor
|
||||
__call__ => BuiltinFunction::create("__call__", Bool::do_call, 2),
|
||||
__init__ => BuiltinFunction::create("__init__", Bool::init, 2),
|
||||
|
||||
// Conversion methods
|
||||
to_int => BuiltinFunction::create("to_int", Bool::to_int, 1),
|
||||
to_float => BuiltinFunction::create("to_float", Bool::to_float, 1),
|
||||
|
||||
// Operators
|
||||
// Methods
|
||||
},
|
||||
Nil {
|
||||
// Conversion methods
|
||||
|
||||
// Constructor
|
||||
__call__ => BuiltinFunction::create("__call__", Nil::do_call, 1),
|
||||
__init__ => BuiltinFunction::create("__init__", Nil::init, 1),
|
||||
|
||||
// Conversion methods
|
||||
|
||||
// Operators
|
||||
// Methods
|
||||
},
|
||||
BuiltinFunction {
|
||||
// Conversion methods
|
||||
// Constructor
|
||||
// Conversion methods
|
||||
// Operators
|
||||
// Methods
|
||||
},
|
||||
UserFunction {
|
||||
// Conversion methods
|
||||
// Constructor
|
||||
// Conversion methods
|
||||
// Operators
|
||||
// Methods
|
||||
},
|
||||
Method {
|
||||
// Conversion methods
|
||||
// Constructor
|
||||
// Conversion methods
|
||||
// Operators
|
||||
// Methods
|
||||
},
|
||||
Module {
|
||||
// Conversion methods
|
||||
// Constructor
|
||||
// Conversion methods
|
||||
// Operators
|
||||
// Methods
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user