diff --git a/src/builtins.rs b/src/builtins.rs index 5d5beb4..75ab905 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -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 { diff --git a/src/obj.rs b/src/obj.rs index 1f38867..a6422f3 100644 --- a/src/obj.rs +++ b/src/obj.rs @@ -20,6 +20,8 @@ pub type Ptr = Gc>; pub type ObjP = Ptr; pub type Attrs = HashMap; +// 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(ptr: ObjP, closure: impl FnOnce(&T) -> Out) -> Out where @@ -116,7 +118,7 @@ pub fn init_types(builtins: &mut HashMap) { // 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) { // 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) { __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 { },