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

@@ -45,8 +45,8 @@ pub(crate) fn println(vm: &mut Vm, state: FunctionState) -> FunctionResult {
let method_type = vm.builtins().get("Method").unwrap().clone();
let to_repr = obj
.borrow_mut()
.get_attr_lazy(obj.clone(), method_type, "to_repr")
.expect("no to_repr");
.get_attr_lazy(obj.clone(), method_type, "to_str")
.expect("no to_str");
to_repr.borrow().call(vm, 0);
FunctionResult::Yield(0)
}
@@ -65,8 +65,8 @@ pub(crate) fn print(vm: &mut Vm, state: FunctionState) -> FunctionResult {
let method_type = vm.builtins().get("Method").unwrap().clone();
let to_repr = obj
.borrow_mut()
.get_attr_lazy(obj.clone(), method_type, "to_repr")
.expect("no to_repr");
.get_attr_lazy(obj.clone(), method_type, "to_str")
.expect("no to_str");
to_repr.borrow().call(vm, 0);
FunctionResult::Yield(0)
}
@@ -87,6 +87,23 @@ impl BaseObjInst {
// Common functions
//
pub(crate) fn to_str(vm: &mut Vm, state: FunctionState) -> FunctionResult {
match state {
FunctionState::Begin => {
let this = vm.frame_stack()[0].clone();
let method_type = vm.builtins().get("Method").unwrap().clone();
let to_repr = this
.borrow_mut()
.get_attr_lazy(this.clone(), method_type, "to_repr")
.expect("no to_repr");
to_repr.borrow().call(vm, 0);
FunctionResult::Yield(0)
}
FunctionState::Resume(0) => FunctionResult::Return,
_ => unreachable!(),
}
}
pub(crate) fn to_repr(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
let str_value = format!("{}", vm.frame_stack()[0].borrow());
vm.create_str(str_value).into()
@@ -200,7 +217,7 @@ impl StrInst {
with_obj_downcast(vm.frame_stack()[0].clone(), |str_inst: &StrInst| {
str_inst.str_value().as_str().escape_default().collect()
});
vm.create_str(escaped).into()
vm.create_str(format!("'{}'", escaped)).into()
}
pub(crate) fn len(vm: &mut Vm, _state: FunctionState) -> FunctionResult {

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 { },