Implement Str methods
* to_str * to_repr * to_bool * len * __add__ * __mul__ Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
16
src/obj.rs
16
src/obj.rs
@@ -116,9 +116,10 @@ pub fn init_types(builtins: &mut HashMap<String, ObjP>) {
|
||||
// type definitions
|
||||
Type {
|
||||
// Method conversion
|
||||
to_string => builtins.create_builtin_function("to_string", BaseObjInst::to_repr, 1),
|
||||
to_str => builtins.create_builtin_function("to_str", BaseObjInst::to_repr, 1),
|
||||
to_repr => builtins.create_builtin_function("to_repr", BaseObjInst::to_repr, 1),
|
||||
to_bool => builtins.create_builtin_function("to_bool", BaseObjInst::to_bool, 1),
|
||||
len => builtins.create_builtin_function("len", BaseObjInst::not_implemented_un, 1),
|
||||
// Operators
|
||||
__add__ => builtins.create_builtin_function("__add__", BaseObjInst::not_implemented_bin, 2),
|
||||
__sub__ => builtins.create_builtin_function("__sub__", BaseObjInst::not_implemented_bin, 2),
|
||||
@@ -137,7 +138,14 @@ pub fn init_types(builtins: &mut HashMap<String, ObjP>) {
|
||||
__not__ => builtins.create_builtin_function("__not__", BaseObjInst::not, 1),
|
||||
},
|
||||
Obj { },
|
||||
Str { },
|
||||
Str {
|
||||
to_str => builtins.create_builtin_function("to_str", StrInst::to_str, 1),
|
||||
to_repr => builtins.create_builtin_function("to_repr", StrInst::to_repr, 1),
|
||||
len => builtins.create_builtin_function("len", StrInst::len, 1),
|
||||
// Operators
|
||||
__add__ => builtins.create_builtin_function("__add__", StrInst::add, 2),
|
||||
__mul__ => builtins.create_builtin_function("__mul__", StrInst::mul, 2),
|
||||
},
|
||||
Int { },
|
||||
Float { },
|
||||
Bool { },
|
||||
@@ -824,7 +832,7 @@ fn test_obj_vtable() {
|
||||
let to_string_ptr = str1.borrow_mut().get_attr_lazy(
|
||||
str1.clone(),
|
||||
builtins.get("Method").unwrap().clone(),
|
||||
"to_string",
|
||||
"to_str",
|
||||
);
|
||||
assert!(to_string_ptr.is_some());
|
||||
|
||||
@@ -838,7 +846,7 @@ fn test_obj_vtable() {
|
||||
let method_to_string_ptr = to_string_ptr.borrow_mut().get_attr_lazy(
|
||||
to_string_ptr.clone(),
|
||||
builtins.get("Method").unwrap().clone(),
|
||||
"to_string",
|
||||
"to_str",
|
||||
);
|
||||
assert!(method_to_string_ptr.is_some());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user