Fix some bugs uncovered by testing

* To_str on objects will call to_repr by default
* Print() and println() will call to_str by default instead of to_repr
* Fix Str.to_repr to include single quotes
* Fix Int.__pos__ and Int.__neg__ arg counts

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2024-09-25 08:48:11 -07:00
parent 11a5a1247e
commit f35053a6c1
2 changed files with 28 additions and 8 deletions

View File

@@ -20,6 +20,8 @@ pub type Ptr<T> = Gc<GcCell<T>>;
pub type ObjP = Ptr<dyn Obj>;
pub type Attrs = HashMap<String, ObjP>;
// TODO obj::with_obj_downcast - optimize downcasts of "known" types with an unchecked downcast
/// Downcast an object pointer to a concrete type, and do something with that object.
pub fn with_obj_downcast<T, Out>(ptr: ObjP, closure: impl FnOnce(&T) -> Out) -> Out
where
@@ -116,7 +118,7 @@ pub fn init_types(builtins: &mut HashMap<String, ObjP>) {
// type definitions
Type {
// Method conversion
to_str => builtins.create_builtin_function("to_str", BaseObjInst::to_repr, 1),
to_str => builtins.create_builtin_function("to_str", BaseObjInst::to_str, 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),
@@ -145,6 +147,7 @@ pub fn init_types(builtins: &mut HashMap<String, ObjP>) {
// Operators
__add__ => builtins.create_builtin_function("__add__", StrInst::add, 2),
__mul__ => builtins.create_builtin_function("__mul__", StrInst::mul, 2),
// .lower, .upper, .slice, etc
},
Int {
// Operators
@@ -156,8 +159,8 @@ pub fn init_types(builtins: &mut HashMap<String, ObjP>) {
__ge__ => builtins.create_builtin_function("__ge__", IntInst::ge, 2),
__lt__ => builtins.create_builtin_function("__lt__", IntInst::lt, 2),
__le__ => builtins.create_builtin_function("__le__", IntInst::le, 2),
__pos__ => builtins.create_builtin_function("__pos__", IntInst::pos, 2),
__neg__ => builtins.create_builtin_function("__neg__", IntInst::neg, 2),
__pos__ => builtins.create_builtin_function("__pos__", IntInst::pos, 1),
__neg__ => builtins.create_builtin_function("__neg__", IntInst::neg, 1),
},
Float { },
Bool { },