diff --git a/src/ceval.h b/src/ceval.h index 78841579..d7b5cf4f 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -20,7 +20,7 @@ PyVar VM::run_frame(Frame* frame){ const PyVar obj = frame->co->consts[byte.arg]; Function f = py_cast_v(this, obj); // copy f._module = frame->_module; - frame->push(py_object(this, f)); + frame->push(py_var(this, f)); } continue; case OP_SETUP_CLOSURE: { Function& f = py_cast(this, frame->top()); // reference @@ -70,7 +70,7 @@ PyVar VM::run_frame(Frame* frame){ continue; case OP_BUILD_TUPLE: { Args items = frame->pop_n_values_reversed(this, byte.arg); - frame->push(py_object(this, std::move(items))); + frame->push(py_var(this, std::move(items))); } continue; case OP_BUILD_TUPLE_REF: { Args items = frame->pop_n_reversed(byte.arg); @@ -80,7 +80,7 @@ PyVar VM::run_frame(Frame* frame){ Args items = frame->pop_n_values_reversed(this, byte.arg); StrStream ss; for(int i=0; i(this, asStr(items[i])); - frame->push(py_object(this, ss.str())); + frame->push(py_var(this, ss.str())); } continue; case OP_LOAD_EVAL_FN: frame->push(builtins->attr(m_eval)); continue; case OP_LIST_APPEND: { @@ -150,13 +150,13 @@ PyVar VM::run_frame(Frame* frame){ PyVar rhs = frame->pop_value(this); bool ret_c = rhs == frame->top_value(this); if(byte.arg == 1) ret_c = !ret_c; - frame->top() = py_object(this, ret_c); + frame->top() = py_var(this, ret_c); } continue; case OP_CONTAINS_OP: { PyVar rhs = frame->pop_value(this); bool ret_c = py_cast_v(this, call(rhs, __contains__, one_arg(frame->pop_value(this)))); if(byte.arg == 1) ret_c = !ret_c; - frame->push(py_object(this, ret_c)); + frame->push(py_var(this, ret_c)); } continue; case OP_UNARY_NEGATIVE: frame->top() = num_negated(frame->top_value(this)); @@ -164,7 +164,7 @@ PyVar VM::run_frame(Frame* frame){ case OP_UNARY_NOT: { PyVar obj = frame->pop_value(this); const PyVar& obj_bool = asBool(obj); - frame->push(py_object(this, !_py_cast_v(this, obj_bool))); + frame->push(py_var(this, !_py_cast_v(this, obj_bool))); } continue; case OP_POP_JUMP_IF_FALSE: if(!_py_cast_v(this, asBool(frame->pop_value(this)))) frame->jump_abs(byte.arg); @@ -182,7 +182,7 @@ PyVar VM::run_frame(Frame* frame){ case OP_EXCEPTION_MATCH: { const auto& e = py_cast(this, frame->top()); StrName name = frame->co->names[byte.arg].first; - frame->push(py_object(this, e.match_type(name))); + frame->push(py_var(this, e.match_type(name))); } continue; case OP_RAISE: { PyVar obj = frame->pop_value(this); @@ -192,7 +192,7 @@ PyVar VM::run_frame(Frame* frame){ } continue; case OP_RE_RAISE: _raise(); continue; case OP_BUILD_LIST: - frame->push(py_object(this, frame->pop_n_values_reversed(this, byte.arg).move_to_list())); + frame->push(py_var(this, frame->pop_n_values_reversed(this, byte.arg).move_to_list())); continue; case OP_BUILD_MAP: { Args items = frame->pop_n_values_reversed(this, byte.arg*2); @@ -203,7 +203,7 @@ PyVar VM::run_frame(Frame* frame){ frame->push(obj); } continue; case OP_BUILD_SET: { - PyVar list = py_object(this, + PyVar list = py_var(this, frame->pop_n_values_reversed(this, byte.arg).move_to_list() ); PyVar obj = call(builtins->attr("set"), one_arg(list)); @@ -212,10 +212,10 @@ PyVar VM::run_frame(Frame* frame){ case OP_DUP_TOP_VALUE: frame->push(frame->top_value(this)); continue; case OP_UNARY_STAR: { if(byte.arg > 0){ // rvalue - frame->top() = py_object(this, StarWrapper(frame->top_value(this), true)); + frame->top() = py_var(this, StarWrapper(frame->top_value(this), true)); }else{ PyRef_AS_C(frame->top()); // check ref - frame->top() = py_object(this, StarWrapper(frame->top(), false)); + frame->top() = py_var(this, StarWrapper(frame->top(), false)); } } continue; case OP_CALL_KWARGS_UNPACK: case OP_CALL_KWARGS: { @@ -286,7 +286,7 @@ PyVar VM::run_frame(Frame* frame){ Slice s; if(start != None) { s.start = (int)py_cast_v(this, start);} if(stop != None) { s.stop = (int)py_cast_v(this, stop);} - frame->push(py_object(this, s)); + frame->push(py_var(this, s)); } continue; case OP_IMPORT_NAME: { StrName name = frame->co->names[byte.arg].first; diff --git a/src/cffi.h b/src/cffi.h index 7ba80a20..7aa84280 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -19,7 +19,7 @@ struct CType{ CType& self = vm->_cast(args[0]); StrStream ss; ss << ""; - return py_object(vm, ss.str()); + return py_var(vm, ss.str()); }); } }; @@ -73,7 +73,7 @@ struct Pointer{ Pointer& self = vm->_cast(args[0]); StrStream ss; ss << "<" << self.ctype.name << "* at " << (i64)self.ptr << ">"; - return py_object(vm, ss.str()); + return py_var(vm, ss.str()); }); vm->bind_method<1>(type, "__add__", [](VM* vm, Args& args) { @@ -89,13 +89,13 @@ struct Pointer{ vm->bind_method<1>(type, "__eq__", [](VM* vm, Args& args) { Pointer& self = vm->_cast(args[0]); Pointer& other = vm->_cast(args[1]); - return py_object(vm, self.ptr == other.ptr); + return py_var(vm, self.ptr == other.ptr); }); vm->bind_method<1>(type, "__ne__", [](VM* vm, Args& args) { Pointer& self = vm->_cast(args[0]); Pointer& other = vm->_cast(args[1]); - return py_object(vm, self.ptr != other.ptr); + return py_var(vm, self.ptr != other.ptr); }); // https://docs.python.org/zh-cn/3/library/ctypes.html @@ -141,20 +141,20 @@ struct Pointer{ PyVar get(VM* vm){ switch(ctype.index){ - case C_TYPE("char_"): return py_object(vm, ref()); - case C_TYPE("int_"): return py_object(vm, ref()); - case C_TYPE("float_"): return py_object(vm, ref()); - case C_TYPE("double_"): return py_object(vm, ref()); - case C_TYPE("bool_"): return py_object(vm, ref()); + case C_TYPE("char_"): return py_var(vm, ref()); + case C_TYPE("int_"): return py_var(vm, ref()); + case C_TYPE("float_"): return py_var(vm, ref()); + case C_TYPE("double_"): return py_var(vm, ref()); + case C_TYPE("bool_"): return py_var(vm, ref()); case C_TYPE("void_"): vm->ValueError("cannot get void*"); break; - case C_TYPE("int8_"): return py_object(vm, ref()); - case C_TYPE("int16_"): return py_object(vm, ref()); - case C_TYPE("int32_"): return py_object(vm, ref()); - case C_TYPE("int64_"): return py_object(vm, ref()); - case C_TYPE("uint8_"): return py_object(vm, ref()); - case C_TYPE("uint16_"): return py_object(vm, ref()); - case C_TYPE("uint32_"): return py_object(vm, ref()); - case C_TYPE("uint64_"): return py_object(vm, ref()); + case C_TYPE("int8_"): return py_var(vm, ref()); + case C_TYPE("int16_"): return py_var(vm, ref()); + case C_TYPE("int32_"): return py_var(vm, ref()); + case C_TYPE("int64_"): return py_var(vm, ref()); + case C_TYPE("uint8_"): return py_var(vm, ref()); + case C_TYPE("uint16_"): return py_var(vm, ref()); + case C_TYPE("uint32_"): return py_var(vm, ref()); + case C_TYPE("uint64_"): return py_var(vm, ref()); case C_TYPE("void_p_"): return vm->new_object(ref(), C_TYPE_T("void_")); // use macro here to do extension default: UNREACHABLE(); @@ -245,7 +245,7 @@ struct Struct { Struct& self = vm->_cast(args[0]); StrStream ss; ss << self.info->name << "(" << ")"; - return py_object(vm, ss.str()); + return py_var(vm, ss.str()); }); } }; @@ -274,7 +274,7 @@ void add_module_c(VM* vm){ vm->bind_func<1>(mod, "sizeof", [](VM* vm, Args& args) { CType& ctype = vm->_cast(args[0]); - return py_object(vm, ctype.size); + return py_var(vm, ctype.size); }); vm->bind_func<3>(mod, "memcpy", [](VM* vm, Args& args) { @@ -309,12 +309,12 @@ void add_module_c(VM* vm){ vm->bind_func<2>(mod, "strcmp", [](VM* vm, Args& args) { Pointer& p1 = vm->_cast(args[0]); Pointer& p2 = vm->_cast(args[1]); - return py_object(vm, strcmp(p1.cast(), p2.cast())); + return py_var(vm, strcmp(p1.cast(), p2.cast())); }); vm->bind_func<1>(mod, "strlen", [](VM* vm, Args& args) { Pointer& p = vm->_cast(args[0]); - return py_object(vm, strlen(p.cast())); + return py_var(vm, strlen(p.cast())); }); } diff --git a/src/compiler.h b/src/compiler.h index b8a3de7e..f8576f4b 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -147,9 +147,9 @@ private: void eat_string(char quote, StringType type) { Str s = eat_string_until(quote, type == RAW_STRING); if(type == F_STRING){ - parser->set_next_token(TK("@fstr"), py_object(vm, s)); + parser->set_next_token(TK("@fstr"), py_var(vm, s)); }else{ - parser->set_next_token(TK("@str"), py_object(vm, s)); + parser->set_next_token(TK("@str"), py_var(vm, s)); } } @@ -171,9 +171,9 @@ private: if (m[1].matched) base = 16; if (m[2].matched) { if(base == 16) SyntaxError("hex literal should not contain a dot"); - parser->set_next_token(TK("@num"), py_object(vm, S_TO_FLOAT(m[0], &size))); + parser->set_next_token(TK("@num"), py_var(vm, S_TO_FLOAT(m[0], &size))); } else { - parser->set_next_token(TK("@num"), py_object(vm, S_TO_INT(m[0], &size, base))); + parser->set_next_token(TK("@num"), py_var(vm, S_TO_INT(m[0], &size, base))); } if (size != m.length()) UNREACHABLE(); } @@ -369,18 +369,18 @@ private: std::smatch m = *it; if (i < m.position()) { std::string literal = s.substr(i, m.position() - i); - emit(OP_LOAD_CONST, co()->add_const(py_object(vm, literal))); + emit(OP_LOAD_CONST, co()->add_const(py_var(vm, literal))); size++; } emit(OP_LOAD_EVAL_FN); - emit(OP_LOAD_CONST, co()->add_const(py_object(vm, m[1].str()))); + emit(OP_LOAD_CONST, co()->add_const(py_var(vm, m[1].str()))); emit(OP_CALL, 1); size++; i = (int)(m.position() + m.length()); } if (i < s.size()) { std::string literal = s.substr(i, s.size() - i); - emit(OP_LOAD_CONST, co()->add_const(py_object(vm, literal))); + emit(OP_LOAD_CONST, co()->add_const(py_var(vm, literal))); size++; } emit(OP_BUILD_STRING, size); @@ -399,7 +399,7 @@ private: emit(OP_RETURN_VALUE); func.code->optimize(vm); this->codes.pop(); - emit(OP_LOAD_FUNCTION, co()->add_const(py_object(vm, func))); + emit(OP_LOAD_FUNCTION, co()->add_const(py_var(vm, func))); if(name_scope() == NAME_LOCAL) emit(OP_SETUP_CLOSURE); } @@ -613,7 +613,7 @@ __LISTCOMP: if(peek() == TK("@id") && peek_next() == TK("=")) { consume(TK("@id")); const Str& key = parser->prev.str(); - emit(OP_LOAD_CONST, co()->add_const(py_object(vm, key))); + emit(OP_LOAD_CONST, co()->add_const(py_var(vm, key))); consume(TK("=")); co()->_rvalue += 1; EXPR(); co()->_rvalue -= 1; KWARGC++; @@ -929,7 +929,7 @@ __LISTCOMP: co()->_rvalue += 1; EXPR(); if (match(TK(","))) EXPR(); - else emit(OP_LOAD_CONST, co()->add_const(py_object(vm, ""))); + else emit(OP_LOAD_CONST, co()->add_const(py_var(vm, ""))); co()->_rvalue -= 1; emit(OP_ASSERT); consume_end_stmt(); @@ -1073,7 +1073,7 @@ __LISTCOMP: compile_block_body(); func.code->optimize(vm); this->codes.pop(); - emit(OP_LOAD_FUNCTION, co()->add_const(py_object(vm, func))); + emit(OP_LOAD_FUNCTION, co()->add_const(py_var(vm, func))); if(name_scope() == NAME_LOCAL) emit(OP_SETUP_CLOSURE); if(!co()->_is_compiling_class){ if(obj_name.empty()){ @@ -1101,8 +1101,8 @@ __LISTCOMP: } if(match(TK("@num"))) return parser->prev.value; if(match(TK("@str"))) return parser->prev.value; - if(match(TK("True"))) return py_object(vm, true); - if(match(TK("False"))) return py_object(vm, false); + if(match(TK("True"))) return py_var(vm, true); + if(match(TK("False"))) return py_var(vm, false); if(match(TK("None"))) return vm->None; if(match(TK("..."))) return vm->Ellipsis; return nullptr; diff --git a/src/iter.h b/src/iter.h index 547c522f..23ed2caa 100644 --- a/src/iter.h +++ b/src/iter.h @@ -20,7 +20,7 @@ public: PyVar next(){ if(!_has_next()) return nullptr; current += r.step; - return py_object(vm, current-r.step); + return py_var(vm, current-r.step); } }; @@ -46,7 +46,7 @@ public: PyVar next() { if(index == str->u8_length()) return nullptr; - return py_object(vm, str->u8_getitem(index++)); + return py_var(vm, str->u8_getitem(index++)); } }; diff --git a/src/main.cpp b/src/main.cpp index 4d9dfb56..28847f33 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,7 @@ int main(int argc, char** argv){ vm->bind_builtin_func<0>("input", [](pkpy::VM* vm, pkpy::Args& args){ static std::string line; std::getline(std::cin, line); - return py_object(vm, line); + return py_var(vm, line); }); if(argc == 1){ pkpy::REPL* repl = pkpy_new_repl(vm); diff --git a/src/pocketpy.h b/src/pocketpy.h index c3d4282a..1a24ebc4 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -22,21 +22,21 @@ CodeObject_ VM::compile(Str source, Str filename, CompileMode mode) { #define BIND_NUM_ARITH_OPT(name, op) \ _vm->_bind_methods<1>({"int","float"}, #name, [](VM* vm, Args& args){ \ if(is_both_int(args[0], args[1])){ \ - return py_object(vm, _py_cast_v(vm, args[0]) op _py_cast_v(vm, args[1])); \ + return py_var(vm, _py_cast_v(vm, args[0]) op _py_cast_v(vm, args[1])); \ }else{ \ - return py_object(vm, vm->num_to_float(args[0]) op vm->num_to_float(args[1])); \ + return py_var(vm, vm->num_to_float(args[0]) op vm->num_to_float(args[1])); \ } \ }); #define BIND_NUM_LOGICAL_OPT(name, op, is_eq) \ _vm->_bind_methods<1>({"int","float"}, #name, [](VM* vm, Args& args){ \ if(!is_both_int_or_float(args[0], args[1])){ \ - if constexpr(is_eq) return py_object(vm, args[0] op args[1]); \ + if constexpr(is_eq) return py_var(vm, args[0] op args[1]); \ vm->TypeError("unsupported operand type(s) for " #op ); \ } \ if(is_both_int(args[0], args[1])) \ - return py_object(vm, _py_cast_v(vm, args[0]) op _py_cast_v(vm, args[1])); \ - return py_object(vm, vm->num_to_float(args[0]) op vm->num_to_float(args[1])); \ + return py_var(vm, _py_cast_v(vm, args[0]) op _py_cast_v(vm, args[1])); \ + return py_var(vm, vm->num_to_float(args[0]) op vm->num_to_float(args[1])); \ }); @@ -73,8 +73,8 @@ void init_builtins(VM* _vm) { _vm->bind_builtin_func<1>("id", [](VM* vm, Args& args) { const PyVar& obj = args[0]; - if(obj.is_tagged()) return py_object(vm, (i64)0); - return py_object(vm, obj.bits); + if(obj.is_tagged()) return py_var(vm, (i64)0); + return py_var(vm, obj.bits); }); _vm->bind_builtin_func<1>("eval", [](VM* vm, Args& args) { @@ -101,23 +101,23 @@ void init_builtins(VM* _vm) { _vm->bind_builtin_func<1>("hash", [](VM* vm, Args& args){ i64 value = vm->hash(args[0]); if(((value << 2) >> 2) != value) value >>= 2; - return py_object(vm, value); + return py_var(vm, value); }); _vm->bind_builtin_func<1>("chr", [](VM* vm, Args& args) { i64 i = py_cast_v(vm, args[0]); if (i < 0 || i > 128) vm->ValueError("chr() arg not in range(128)"); - return py_object(vm, std::string(1, (char)i)); + return py_var(vm, std::string(1, (char)i)); }); _vm->bind_builtin_func<1>("ord", [](VM* vm, Args& args) { Str s = py_cast(vm, args[0]); if (s.size() != 1) vm->TypeError("ord() expected an ASCII character"); - return py_object(vm, (i64)(s.c_str()[0])); + return py_var(vm, (i64)(s.c_str()[0])); }); _vm->bind_builtin_func<2>("hasattr", [](VM* vm, Args& args) { - return py_object(vm, vm->getattr(args[0], py_cast(vm, args[1]), false) != nullptr); + return py_var(vm, vm->getattr(args[0], py_cast(vm, args[1]), false) != nullptr); }); _vm->bind_builtin_func<3>("setattr", [](VM* vm, Args& args) { @@ -133,7 +133,7 @@ void init_builtins(VM* _vm) { _vm->bind_builtin_func<1>("hex", [](VM* vm, Args& args) { std::stringstream ss; ss << std::hex << py_cast_v(vm, args[0]); - return py_object(vm, "0x" + ss.str()); + return py_var(vm, "0x" + ss.str()); }); _vm->bind_builtin_func<1>("dir", [](VM* vm, Args& args) { @@ -146,8 +146,8 @@ void init_builtins(VM* _vm) { std::vector keys = t_attr.keys(); names.insert(keys.begin(), keys.end()); List ret; - for (StrName name : names) ret.push_back(py_object(vm, name.str())); - return py_object(vm, std::move(ret)); + for (StrName name : names) ret.push_back(py_var(vm, name.str())); + return py_var(vm, std::move(ret)); }); _vm->bind_method<0>("object", "__repr__", [](VM* vm, Args& args) { @@ -156,11 +156,11 @@ void init_builtins(VM* _vm) { StrStream ss; ss << std::hex << addr; Str s = "<" + OBJ_NAME(vm->_t(self)) + " object at 0x" + ss.str() + ">"; - return py_object(vm, s); + return py_var(vm, s); }); - _vm->bind_method<1>("object", "__eq__", CPP_LAMBDA(py_object(vm, args[0] == args[1]))); - _vm->bind_method<1>("object", "__ne__", CPP_LAMBDA(py_object(vm, args[0] != args[1]))); + _vm->bind_method<1>("object", "__eq__", CPP_LAMBDA(py_var(vm, args[0] == args[1]))); + _vm->bind_method<1>("object", "__ne__", CPP_LAMBDA(py_var(vm, args[0] != args[1]))); _vm->bind_static_method<1>("type", "__new__", CPP_LAMBDA(vm->_t(args[0]))); @@ -172,20 +172,20 @@ void init_builtins(VM* _vm) { case 3: r.start = py_cast_v(vm, args[0]); r.stop = py_cast_v(vm, args[1]); r.step = py_cast_v(vm, args[2]); break; default: vm->TypeError("expected 1-3 arguments, but got " + std::to_string(args.size())); } - return py_object(vm, r); + return py_var(vm, r); }); _vm->bind_method<0>("range", "__iter__", CPP_LAMBDA( vm->PyIter(RangeIter(vm, args[0])) )); - _vm->bind_method<0>("NoneType", "__repr__", CPP_LAMBDA(py_object(vm, "None"))); - _vm->bind_method<0>("NoneType", "__json__", CPP_LAMBDA(py_object(vm, "null"))); + _vm->bind_method<0>("NoneType", "__repr__", CPP_LAMBDA(py_var(vm, "None"))); + _vm->bind_method<0>("NoneType", "__json__", CPP_LAMBDA(py_var(vm, "null"))); _vm->_bind_methods<1>({"int", "float"}, "__truediv__", [](VM* vm, Args& args) { f64 rhs = vm->num_to_float(args[1]); if (rhs == 0) vm->ZeroDivisionError(); - return py_object(vm, vm->num_to_float(args[0]) / rhs); + return py_var(vm, vm->num_to_float(args[0]) / rhs); }); _vm->_bind_methods<1>({"int", "float"}, "__pow__", [](VM* vm, Args& args) { @@ -200,25 +200,25 @@ void init_builtins(VM* _vm) { lhs *= lhs; rhs >>= 1; } - if(flag) return py_object(vm, (f64)(1.0 / ret)); - return py_object(vm, ret); + if(flag) return py_var(vm, (f64)(1.0 / ret)); + return py_var(vm, ret); }else{ - return py_object(vm, (f64)std::pow(vm->num_to_float(args[0]), vm->num_to_float(args[1]))); + return py_var(vm, (f64)std::pow(vm->num_to_float(args[0]), vm->num_to_float(args[1]))); } }); /************ PyInt ************/ _vm->bind_static_method<1>("int", "__new__", [](VM* vm, Args& args) { if (is_type(args[0], vm->tp_int)) return args[0]; - if (is_type(args[0], vm->tp_float)) return py_object(vm, (i64)py_cast_v(vm, args[0])); - if (is_type(args[0], vm->tp_bool)) return py_object(vm, _py_cast_v(vm, args[0]) ? 1 : 0); + if (is_type(args[0], vm->tp_float)) return py_var(vm, (i64)py_cast_v(vm, args[0])); + if (is_type(args[0], vm->tp_bool)) return py_var(vm, _py_cast_v(vm, args[0]) ? 1 : 0); if (is_type(args[0], vm->tp_str)) { const Str& s = py_cast(vm, args[0]); try{ size_t parsed = 0; i64 val = S_TO_INT(s, &parsed, 10); if(parsed != s.size()) throw std::invalid_argument(""); - return py_object(vm, val); + return py_var(vm, val); }catch(std::invalid_argument&){ vm->ValueError("invalid literal for int(): " + s.escape(true)); } @@ -230,20 +230,20 @@ void init_builtins(VM* _vm) { _vm->bind_method<1>("int", "__floordiv__", [](VM* vm, Args& args) { i64 rhs = py_cast_v(vm, args[1]); if(rhs == 0) vm->ZeroDivisionError(); - return py_object(vm, py_cast_v(vm, args[0]) / rhs); + return py_var(vm, py_cast_v(vm, args[0]) / rhs); }); _vm->bind_method<1>("int", "__mod__", [](VM* vm, Args& args) { i64 rhs = py_cast_v(vm, args[1]); if(rhs == 0) vm->ZeroDivisionError(); - return py_object(vm, py_cast_v(vm, args[0]) % rhs); + return py_var(vm, py_cast_v(vm, args[0]) % rhs); }); - _vm->bind_method<0>("int", "__repr__", CPP_LAMBDA(py_object(vm, std::to_string(py_cast_v(vm, args[0]))))); - _vm->bind_method<0>("int", "__json__", CPP_LAMBDA(py_object(vm, std::to_string(py_cast_v(vm, args[0]))))); + _vm->bind_method<0>("int", "__repr__", CPP_LAMBDA(py_var(vm, std::to_string(py_cast_v(vm, args[0]))))); + _vm->bind_method<0>("int", "__json__", CPP_LAMBDA(py_var(vm, std::to_string(py_cast_v(vm, args[0]))))); #define INT_BITWISE_OP(name,op) \ - _vm->bind_method<1>("int", #name, CPP_LAMBDA(py_object(vm, py_cast_v(vm, args[0]) op py_cast_v(vm, args[1])))); + _vm->bind_method<1>("int", #name, CPP_LAMBDA(py_var(vm, py_cast_v(vm, args[0]) op py_cast_v(vm, args[1])))); INT_BITWISE_OP(__lshift__, <<) INT_BITWISE_OP(__rshift__, >>) @@ -255,16 +255,16 @@ void init_builtins(VM* _vm) { /************ PyFloat ************/ _vm->bind_static_method<1>("float", "__new__", [](VM* vm, Args& args) { - if (is_type(args[0], vm->tp_int)) return py_object(vm, (f64)py_cast_v(vm, args[0])); + if (is_type(args[0], vm->tp_int)) return py_var(vm, (f64)py_cast_v(vm, args[0])); if (is_type(args[0], vm->tp_float)) return args[0]; - if (is_type(args[0], vm->tp_bool)) return py_object(vm, _py_cast_v(vm, args[0]) ? 1.0 : 0.0); + if (is_type(args[0], vm->tp_bool)) return py_var(vm, _py_cast_v(vm, args[0]) ? 1.0 : 0.0); if (is_type(args[0], vm->tp_str)) { const Str& s = py_cast(vm, args[0]); - if(s == "inf") return py_object(vm, INFINITY); - if(s == "-inf") return py_object(vm, -INFINITY); + if(s == "inf") return py_var(vm, INFINITY); + if(s == "-inf") return py_var(vm, -INFINITY); try{ f64 val = S_TO_FLOAT(s); - return py_object(vm, val); + return py_var(vm, val); }catch(std::invalid_argument&){ vm->ValueError("invalid literal for float(): '" + s + "'"); } @@ -275,18 +275,18 @@ void init_builtins(VM* _vm) { _vm->bind_method<0>("float", "__repr__", [](VM* vm, Args& args) { f64 val = py_cast_v(vm, args[0]); - if(std::isinf(val) || std::isnan(val)) return py_object(vm, std::to_string(val)); + if(std::isinf(val) || std::isnan(val)) return py_var(vm, std::to_string(val)); StrStream ss; ss << std::setprecision(std::numeric_limits::max_digits10-1-2) << val; std::string s = ss.str(); if(std::all_of(s.begin()+1, s.end(), isdigit)) s += ".0"; - return py_object(vm, s); + return py_var(vm, s); }); _vm->bind_method<0>("float", "__json__", [](VM* vm, Args& args) { f64 val = py_cast_v(vm, args[0]); if(std::isinf(val) || std::isnan(val)) vm->ValueError("cannot jsonify 'nan' or 'inf'"); - return py_object(vm, std::to_string(val)); + return py_var(vm, std::to_string(val)); }); /************ PyString ************/ @@ -295,18 +295,18 @@ void init_builtins(VM* _vm) { _vm->bind_method<1>("str", "__add__", [](VM* vm, Args& args) { const Str& lhs = py_cast(vm, args[0]); const Str& rhs = py_cast(vm, args[1]); - return py_object(vm, lhs + rhs); + return py_var(vm, lhs + rhs); }); _vm->bind_method<0>("str", "__len__", [](VM* vm, Args& args) { const Str& self = py_cast(vm, args[0]); - return py_object(vm, self.u8_length()); + return py_var(vm, self.u8_length()); }); _vm->bind_method<1>("str", "__contains__", [](VM* vm, Args& args) { const Str& self = py_cast(vm, args[0]); const Str& other = py_cast(vm, args[1]); - return py_object(vm, self.find(other) != Str::npos); + return py_var(vm, self.find(other) != Str::npos); }); _vm->bind_method<0>("str", "__str__", CPP_LAMBDA(args[0])); @@ -314,24 +314,24 @@ void init_builtins(VM* _vm) { _vm->bind_method<0>("str", "__repr__", [](VM* vm, Args& args) { const Str& _self = py_cast(vm, args[0]); - return py_object(vm, _self.escape(true)); + return py_var(vm, _self.escape(true)); }); _vm->bind_method<0>("str", "__json__", [](VM* vm, Args& args) { const Str& _self = py_cast(vm, args[0]); - return py_object(vm, _self.escape(false)); + return py_var(vm, _self.escape(false)); }); _vm->bind_method<1>("str", "__eq__", [](VM* vm, Args& args) { if(is_type(args[0], vm->tp_str) && is_type(args[1], vm->tp_str)) - return py_object(vm, py_cast(vm, args[0]) == py_cast(vm, args[1])); - return py_object(vm, args[0] == args[1]); + return py_var(vm, py_cast(vm, args[0]) == py_cast(vm, args[1])); + return py_var(vm, args[0] == args[1]); }); _vm->bind_method<1>("str", "__ne__", [](VM* vm, Args& args) { if(is_type(args[0], vm->tp_str) && is_type(args[1], vm->tp_str)) - return py_object(vm, py_cast(vm, args[0]) != py_cast(vm, args[1])); - return py_object(vm, args[0] != args[1]); + return py_var(vm, py_cast(vm, args[0]) != py_cast(vm, args[1])); + return py_var(vm, args[0] != args[1]); }); _vm->bind_method<1>("str", "__getitem__", [](VM* vm, Args& args) { @@ -340,24 +340,24 @@ void init_builtins(VM* _vm) { if(is_type(args[1], vm->tp_slice)){ Slice s = _py_cast_v(vm, args[1]); s.normalize(self.u8_length()); - return py_object(vm, self.u8_substr(s.start, s.stop)); + return py_var(vm, self.u8_substr(s.start, s.stop)); } int index = (int)py_cast_v(vm, args[1]); index = vm->normalized_index(index, self.u8_length()); - return py_object(vm, self.u8_getitem(index)); + return py_var(vm, self.u8_getitem(index)); }); _vm->bind_method<1>("str", "__gt__", [](VM* vm, Args& args) { const Str& self (py_cast(vm, args[0])); const Str& obj (py_cast(vm, args[1])); - return py_object(vm, self > obj); + return py_var(vm, self > obj); }); _vm->bind_method<1>("str", "__lt__", [](VM* vm, Args& args) { const Str& self (py_cast(vm, args[0])); const Str& obj (py_cast(vm, args[1])); - return py_object(vm, self < obj); + return py_var(vm, self < obj); }); _vm->bind_method<2>("str", "replace", [](VM* vm, Args& args) { @@ -371,19 +371,19 @@ void init_builtins(VM* _vm) { _copy.replace(pos, _old.length(), _new); pos += _new.length(); } - return py_object(vm, _copy); + return py_var(vm, _copy); }); _vm->bind_method<1>("str", "startswith", [](VM* vm, Args& args) { const Str& _self = py_cast(vm, args[0]); const Str& _prefix = py_cast(vm, args[1]); - return py_object(vm, _self.find(_prefix) == 0); + return py_var(vm, _self.find(_prefix) == 0); }); _vm->bind_method<1>("str", "endswith", [](VM* vm, Args& args) { const Str& _self = py_cast(vm, args[0]); const Str& _suffix = py_cast(vm, args[1]); - return py_object(vm, _self.rfind(_suffix) == _self.length() - _suffix.length()); + return py_var(vm, _self.rfind(_suffix) == _self.length() - _suffix.length()); }); _vm->bind_method<1>("str", "join", [](VM* vm, Args& args) { @@ -395,7 +395,7 @@ void init_builtins(VM* _vm) { if (i > 0) ss << self; ss << py_cast(vm, list[i]); } - return py_object(vm, ss.str()); + return py_var(vm, ss.str()); }); /************ PyList ************/ @@ -417,7 +417,7 @@ void init_builtins(VM* _vm) { List result; result.reserve(self.size() * n); for(int i = 0; i < n; i++) result.insert(result.end(), self.begin(), self.end()); - return py_object(vm, std::move(result)); + return py_var(vm, std::move(result)); }); _vm->bind_method<2>("list", "insert", [](VM* vm, Args& args) { @@ -435,19 +435,19 @@ void init_builtins(VM* _vm) { return vm->None; }); - _vm->bind_method<0>("list", "copy", CPP_LAMBDA(py_object(vm, py_cast(vm, args[0])))); + _vm->bind_method<0>("list", "copy", CPP_LAMBDA(py_var(vm, py_cast(vm, args[0])))); _vm->bind_method<1>("list", "__add__", [](VM* vm, Args& args) { const List& self = py_cast(vm, args[0]); const List& obj = py_cast(vm, args[1]); List new_list = self; new_list.insert(new_list.end(), obj.begin(), obj.end()); - return py_object(vm, new_list); + return py_var(vm, new_list); }); _vm->bind_method<0>("list", "__len__", [](VM* vm, Args& args) { const List& self = py_cast(vm, args[0]); - return py_object(vm, self.size()); + return py_var(vm, self.size()); }); _vm->bind_method<0>("list", "__iter__", [](VM* vm, Args& args) { @@ -462,7 +462,7 @@ void init_builtins(VM* _vm) { s.normalize(self.size()); List new_list; for(size_t i = s.start; i < s.stop; i++) new_list.push_back(self[i]); - return py_object(vm, std::move(new_list)); + return py_var(vm, std::move(new_list)); } int index = (int)py_cast_v(vm, args[1]); @@ -489,7 +489,7 @@ void init_builtins(VM* _vm) { /************ PyTuple ************/ _vm->bind_static_method<1>("tuple", "__new__", [](VM* vm, Args& args) { List list = py_cast(vm, vm->asList(args[0])); - return py_object(vm, std::move(list)); + return py_var(vm, std::move(list)); }); _vm->bind_method<0>("tuple", "__iter__", [](VM* vm, Args& args) { @@ -504,7 +504,7 @@ void init_builtins(VM* _vm) { s.normalize(self.size()); List new_list; for(size_t i = s.start; i < s.stop; i++) new_list.push_back(self[i]); - return py_object(vm, std::move(new_list)); + return py_var(vm, std::move(new_list)); } int index = (int)py_cast_v(vm, args[1]); @@ -514,7 +514,7 @@ void init_builtins(VM* _vm) { _vm->bind_method<0>("tuple", "__len__", [](VM* vm, Args& args) { const Tuple& self = py_cast(vm, args[0]); - return py_object(vm, self.size()); + return py_var(vm, self.size()); }); /************ PyBool ************/ @@ -522,21 +522,21 @@ void init_builtins(VM* _vm) { _vm->bind_method<0>("bool", "__repr__", [](VM* vm, Args& args) { bool val = py_cast_v(vm, args[0]); - return py_object(vm, val ? "True" : "False"); + return py_var(vm, val ? "True" : "False"); }); _vm->bind_method<0>("bool", "__json__", [](VM* vm, Args& args) { bool val = py_cast_v(vm, args[0]); - return py_object(vm, val ? "true" : "false"); + return py_var(vm, val ? "true" : "false"); }); _vm->bind_method<1>("bool", "__xor__", [](VM* vm, Args& args) { bool self = py_cast_v(vm, args[0]); bool other = py_cast_v(vm, args[1]); - return py_object(vm, self ^ other); + return py_var(vm, self ^ other); }); - _vm->bind_method<0>("ellipsis", "__repr__", CPP_LAMBDA(py_object(vm, "Ellipsis"))); + _vm->bind_method<0>("ellipsis", "__repr__", CPP_LAMBDA(py_var(vm, "Ellipsis"))); } #include "builtins.h" @@ -557,16 +557,16 @@ void add_module_time(VM* vm){ PyVar mod = vm->new_module("time"); vm->bind_func<0>(mod, "time", [](VM* vm, Args& args) { auto now = std::chrono::high_resolution_clock::now(); - return py_object(vm, std::chrono::duration_cast(now.time_since_epoch()).count() / 1000000.0); + return py_var(vm, std::chrono::duration_cast(now.time_since_epoch()).count() / 1000000.0); }); } void add_module_sys(VM* vm){ PyVar mod = vm->new_module("sys"); - vm->setattr(mod, "version", py_object(vm, PK_VERSION)); + vm->setattr(mod, "version", py_var(vm, PK_VERSION)); - vm->bind_func<1>(mod, "getrefcount", CPP_LAMBDA(py_object(vm, args[0].use_count()))); - vm->bind_func<0>(mod, "getrecursionlimit", CPP_LAMBDA(py_object(vm, vm->recursionlimit))); + vm->bind_func<1>(mod, "getrefcount", CPP_LAMBDA(py_var(vm, args[0].use_count()))); + vm->bind_func<0>(mod, "getrecursionlimit", CPP_LAMBDA(py_var(vm, vm->recursionlimit))); vm->bind_func<1>(mod, "setrecursionlimit", [](VM* vm, Args& args) { vm->recursionlimit = (int)py_cast_v(vm, args[0]); @@ -587,21 +587,21 @@ void add_module_json(VM* vm){ void add_module_math(VM* vm){ PyVar mod = vm->new_module("math"); - vm->setattr(mod, "pi", py_object(vm, 3.1415926535897932384)); - vm->setattr(mod, "e" , py_object(vm, 2.7182818284590452354)); + vm->setattr(mod, "pi", py_var(vm, 3.1415926535897932384)); + vm->setattr(mod, "e" , py_var(vm, 2.7182818284590452354)); - vm->bind_func<1>(mod, "log", CPP_LAMBDA(py_object(vm, std::log(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "log10", CPP_LAMBDA(py_object(vm, std::log10(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "log2", CPP_LAMBDA(py_object(vm, std::log2(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "sin", CPP_LAMBDA(py_object(vm, std::sin(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "cos", CPP_LAMBDA(py_object(vm, std::cos(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "tan", CPP_LAMBDA(py_object(vm, std::tan(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(py_object(vm, std::isnan(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(py_object(vm, std::isinf(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(py_object(vm, std::fabs(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "floor", CPP_LAMBDA(py_object(vm, (i64)std::floor(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(py_object(vm, (i64)std::ceil(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(py_object(vm, std::sqrt(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "log", CPP_LAMBDA(py_var(vm, std::log(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "log10", CPP_LAMBDA(py_var(vm, std::log10(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "log2", CPP_LAMBDA(py_var(vm, std::log2(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "sin", CPP_LAMBDA(py_var(vm, std::sin(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "cos", CPP_LAMBDA(py_var(vm, std::cos(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "tan", CPP_LAMBDA(py_var(vm, std::tan(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(py_var(vm, std::isnan(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(py_var(vm, std::isinf(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(py_var(vm, std::fabs(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "floor", CPP_LAMBDA(py_var(vm, (i64)std::floor(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(py_var(vm, (i64)std::ceil(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(py_var(vm, std::sqrt(vm->num_to_float(args[0]))))); } void add_module_dis(VM* vm){ @@ -644,7 +644,7 @@ struct FileIO { FileIO& io = vm->_cast(args[0]); std::string buffer; io._fs >> buffer; - return py_object(vm, buffer); + return py_var(vm, buffer); }); vm->bind_method<1>(type, "write", [](VM* vm, Args& args){ @@ -688,19 +688,19 @@ struct ReMatch { static void _register(VM* vm, PyVar mod, PyVar type){ vm->bind_method<-1>(type, "__init__", CPP_NOT_IMPLEMENTED()); - vm->bind_method<0>(type, "start", CPP_LAMBDA(py_object(vm, vm->_cast(args[0]).start))); - vm->bind_method<0>(type, "end", CPP_LAMBDA(py_object(vm, vm->_cast(args[0]).end))); + vm->bind_method<0>(type, "start", CPP_LAMBDA(py_var(vm, vm->_cast(args[0]).start))); + vm->bind_method<0>(type, "end", CPP_LAMBDA(py_var(vm, vm->_cast(args[0]).end))); vm->bind_method<0>(type, "span", [](VM* vm, Args& args) { auto& self = vm->_cast(args[0]); - return py_object(vm, two_args(py_object(vm, self.start), py_object(vm, self.end))); + return py_var(vm, two_args(py_var(vm, self.start), py_var(vm, self.end))); }); vm->bind_method<1>(type, "group", [](VM* vm, Args& args) { auto& self = vm->_cast(args[0]); int index = (int)py_cast_v(vm, args[1]); index = vm->normalized_index(index, self.m.size()); - return py_object(vm, self.m[index].str()); + return py_var(vm, self.m[index].str()); }); } }; @@ -738,7 +738,7 @@ void add_module_re(VM* vm){ const Str& repl = py_cast(vm, args[1]); const Str& string = py_cast(vm, args[2]); std::regex re(pattern); - return py_object(vm, std::regex_replace(string, re, repl)); + return py_var(vm, std::regex_replace(string, re, repl)); }); vm->bind_func<2>(mod, "split", [](VM* vm, Args& args) { @@ -749,9 +749,9 @@ void add_module_re(VM* vm){ std::sregex_token_iterator end; List vec; for(; it != end; ++it){ - vec.push_back(py_object(vm, it->str())); + vec.push_back(py_var(vm, it->str())); } - return py_object(vm, vec); + return py_var(vm, vec); }); } @@ -763,19 +763,19 @@ void add_module_random(VM* vm){ return vm->None; }); - vm->bind_func<0>(mod, "random", CPP_LAMBDA(py_object(vm, std::rand() / (f64)RAND_MAX))); + vm->bind_func<0>(mod, "random", CPP_LAMBDA(py_var(vm, std::rand() / (f64)RAND_MAX))); vm->bind_func<2>(mod, "randint", [](VM* vm, Args& args) { i64 a = py_cast_v(vm, args[0]); i64 b = py_cast_v(vm, args[1]); if(a > b) std::swap(a, b); - return py_object(vm, a + std::rand() % (b - a + 1)); + return py_var(vm, a + std::rand() % (b - a + 1)); }); vm->bind_func<2>(mod, "uniform", [](VM* vm, Args& args) { f64 a = py_cast_v(vm, args[0]); f64 b = py_cast_v(vm, args[1]); if(a > b) std::swap(a, b); - return py_object(vm, a + (b - a) * std::rand() / (f64)RAND_MAX); + return py_var(vm, a + (b - a) * std::rand() / (f64)RAND_MAX); }); CodeObject_ code = vm->compile(kRandomCode, "random.py", EXEC_MODE); @@ -975,13 +975,13 @@ extern "C" { } char* packet = strdup(ss.str().c_str()); switch(ret_code){ - case 'i': return py_object(vm, f_int(packet)); - case 'f': return py_object(vm, f_float(packet)); - case 'b': return py_object(vm, f_bool(packet)); + case 'i': return py_var(vm, f_int(packet)); + case 'f': return py_var(vm, f_float(packet)); + case 'b': return py_var(vm, f_bool(packet)); case 's': { char* p = f_str(packet); if(p == nullptr) return vm->None; - return py_object(vm, p); // no need to free(p) + return py_var(vm, p); // no need to free(p) } case 'N': f_None(packet); return vm->None; } diff --git a/src/ref.h b/src/ref.h index ac01b4b8..3ab0b9e7 100644 --- a/src/ref.h +++ b/src/ref.h @@ -118,7 +118,7 @@ struct TupleRef : BaseRef { for (int i = 0; i < objs.size(); i++) { args[i] = vm->PyRef_AS_C(objs[i])->get(vm, frame); } - return py_object(vm, std::move(args)); + return py_var(vm, std::move(args)); } void set(VM* vm, Frame* frame, PyVar val) const{ @@ -133,7 +133,7 @@ struct TupleRef : BaseRef { auto ref = vm->PyRef_AS_C(star.obj); List list; while((x = iter->next()) != nullptr) list.push_back(x); - ref->set(vm, frame, py_object(vm, std::move(list))); + ref->set(vm, frame, py_var(vm, std::move(list))); return; }else{ x = iter->next(); diff --git a/src/vm.h b/src/vm.h index 2c68b84f..7f43c4f5 100644 --- a/src/vm.h +++ b/src/vm.h @@ -20,8 +20,8 @@ namespace pkpy{ template<> ctype _py_cast_v(VM* vm, const PyVar& obj) { \ return OBJ_GET(ctype, obj); \ } \ - PyVar py_object(VM* vm, const ctype& value) { return vm->new_object(vm->ptype, value);} \ - PyVar py_object(VM* vm, ctype&& value) { return vm->new_object(vm->ptype, std::move(value));} + PyVar py_var(VM* vm, const ctype& value) { return vm->new_object(vm->ptype, value);} \ + PyVar py_var(VM* vm, ctype&& value) { return vm->new_object(vm->ptype, std::move(value));} class Generator: public BaseIter { std::unique_ptr frame; @@ -391,7 +391,7 @@ DEF_NATIVE_2(Exception, tp_exception) DEF_NATIVE_2(StarWrapper, tp_star_wrapper) template -std::enable_if_t && !std::is_same_v, PyVar> py_object(VM* vm, T _val){ +std::enable_if_t && !std::is_same_v, PyVar> py_var(VM* vm, T _val){ i64 val = static_cast(_val); if(((val << 2) >> 2) != val){ vm->_error("OverflowError", std::to_string(val) + " is out of range"); @@ -420,7 +420,7 @@ template<> f64 _py_cast_v(VM* vm, const PyVar& obj){ } template -std::enable_if_t, PyVar> py_object(VM* vm, T _val){ +std::enable_if_t, PyVar> py_var(VM* vm, T _val){ f64 val = static_cast(_val); i64 bits = __8B(val)._int; bits = (bits >> 2) << 2; @@ -428,7 +428,7 @@ std::enable_if_t, PyVar> py_object(VM* vm, T _val){ return PyVar(reinterpret_cast(bits)); } -const PyVar& py_object(VM* vm, bool val){ +const PyVar& py_var(VM* vm, bool val){ return val ? vm->True : vm->False; } @@ -440,15 +440,15 @@ template<> bool _py_cast_v(VM* vm, const PyVar& obj){ return obj == vm->True; } -PyVar py_object(VM* vm, const char* val){ - return py_object(vm, Str(val)); +PyVar py_var(VM* vm, const char* val){ + return py_var(vm, Str(val)); } PyVar VM::num_negated(const PyVar& obj){ if (is_int(obj)){ - return py_object(this, -py_cast_v(this, obj)); + return py_var(this, -py_cast_v(this, obj)); }else if(is_float(obj)){ - return py_object(this, -py_cast_v(this, obj)); + return py_var(this, -py_cast_v(this, obj)); } TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape(true)); return nullptr; @@ -467,12 +467,12 @@ f64 VM::num_to_float(const PyVar& obj){ const PyVar& VM::asBool(const PyVar& obj){ if(is_type(obj, tp_bool)) return obj; if(obj == None) return False; - if(is_type(obj, tp_int)) return py_object(this, py_cast_v(this, obj) != 0); - if(is_type(obj, tp_float)) return py_object(this, py_cast_v(this, obj) != 0.0); + if(is_type(obj, tp_int)) return py_var(this, py_cast_v(this, obj) != 0); + if(is_type(obj, tp_float)) return py_var(this, py_cast_v(this, obj) != 0.0); PyVarOrNull len_fn = getattr(obj, __len__, false); if(len_fn != nullptr){ PyVar ret = call(len_fn); - return py_object(this, py_cast_v(this, ret) > 0); + return py_var(this, py_cast_v(this, ret) > 0); } return True; } @@ -500,7 +500,7 @@ i64 VM::hash(const PyVar& obj){ } PyVar VM::asRepr(const PyVar& obj){ - if(is_type(obj, tp_type)) return py_object(this, "attr(__name__)) + "'>"); + if(is_type(obj, tp_type)) return py_var(this, "attr(__name__)) + "'>"); return call(obj, __repr__); } @@ -510,7 +510,7 @@ PyVar VM::new_type_object(PyVar mod, StrName name, PyVar base){ setattr(obj, __base__, base); Str fullName = name.str(); if(mod != builtins) fullName = OBJ_NAME(mod) + "." + name.str(); - setattr(obj, __name__, py_object(this, fullName)); + setattr(obj, __name__, py_var(this, fullName)); setattr(mod, name, obj); _all_types.push_back(obj); return obj; @@ -518,7 +518,7 @@ PyVar VM::new_type_object(PyVar mod, StrName name, PyVar base){ PyVar VM::new_module(StrName name) { PyVar obj = new_object(tp_module, DummyModule()); - setattr(obj, __name__, py_object(this, name.str())); + setattr(obj, __name__, py_var(this, name.str())); _modules.set(name, obj); return obj; } @@ -571,15 +571,15 @@ Str VM::disassemble(CodeObject_ co){ } StrStream consts; consts << "co_consts: "; - consts << py_cast(this, asRepr(py_object(this, co->consts))); + consts << py_cast(this, asRepr(py_var(this, co->consts))); StrStream names; names << "co_names: "; List list; for(int i=0; inames.size(); i++){ - list.push_back(py_object(this, co->names[i].first.str())); + list.push_back(py_var(this, co->names[i].first.str())); } - names << py_cast(this, asRepr(py_object(this, list))); + names << py_cast(this, asRepr(py_var(this, list))); ss << '\n' << consts.str() << '\n' << names.str() << '\n'; for(int i=0; iconsts.size(); i++){ @@ -636,7 +636,7 @@ void VM::init_builtin_types(){ setattr(_t(tp_object), __base__, None); for(auto [k, v]: _types.items()){ - setattr(v, __name__, py_object(this, k.str())); + setattr(v, __name__, py_var(this, k.str())); } std::vector pb_types = {"type", "object", "bool", "int", "float", "str", "list", "tuple", "range"}; @@ -696,7 +696,7 @@ PyVar VM::call(const PyVar& _callable, Args args, const Args& kwargs, bool opCal if(!fn.starred_arg.empty()){ List vargs; // handle *args while(i < args.size()) vargs.push_back(std::move(args[i++])); - locals->set(fn.starred_arg, py_object(this, Tuple::from_list(std::move(vargs)))); + locals->set(fn.starred_arg, py_var(this, Tuple::from_list(std::move(vargs)))); }else{ for(StrName key : fn.kwargs_order){ if(i < args.size()){ @@ -775,7 +775,7 @@ PyVarOrNull VM::getattr(const PyVar& obj, StrName name, bool throw_err) { return call(descriptor, one_arg(obj)); } if(is_type(*val, tp_function) || is_type(*val, tp_native_function)){ - return py_object(this, BoundMethod(obj, *val)); + return py_var(this, BoundMethod(obj, *val)); }else{ return *val; } @@ -798,12 +798,12 @@ void VM::setattr(PyVar& obj, StrName name, T&& value) { template void VM::bind_method(PyVar obj, Str funcName, NativeFuncRaw fn) { check_type(obj, tp_type); - setattr(obj, funcName, py_object(this, NativeFunc(fn, ARGC, true))); + setattr(obj, funcName, py_var(this, NativeFunc(fn, ARGC, true))); } template void VM::bind_func(PyVar obj, Str funcName, NativeFuncRaw fn) { - setattr(obj, funcName, py_object(this, NativeFunc(fn, ARGC, false))); + setattr(obj, funcName, py_var(this, NativeFunc(fn, ARGC, false))); } void VM::_error(Exception e){ @@ -811,7 +811,7 @@ void VM::_error(Exception e){ e.is_re = false; throw e; } - top_frame()->push(py_object(this, e)); + top_frame()->push(py_var(this, e)); _raise(); }