From fa22cdb3b292bff81f760af2e7447078f5c71e4a Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 27 Feb 2023 21:01:53 +0800 Subject: [PATCH] up --- amalgamate.py | 4 +- src/ceval.h | 5 +- src/cffi.h | 36 +++---- src/codeobject.h | 8 +- src/obj.h | 9 +- src/pocketpy.h | 52 +++++----- src/ref.h | 147 ++++++++++++++++++++++++---- src/vm.h | 248 ++++++++++++----------------------------------- 8 files changed, 248 insertions(+), 261 deletions(-) diff --git a/amalgamate.py b/amalgamate.py index b12b931d..6bff82ae 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -3,8 +3,8 @@ with open("src/opcodes.h", "rt", encoding='utf-8') as f: pipeline = [ ["common.h", "memory.h", "str.h", "tuplelist.h", "namedict.h", "builtins.h", "error.h"], - ["obj.h", "parser.h", "ref.h", "codeobject.h", "frame.h"], - ["vm.h", "ceval.h", "compiler.h", "repl.h"], + ["obj.h", "parser.h", "codeobject.h", "frame.h"], + ["vm.h", "ref.h", "ceval.h", "compiler.h", "repl.h"], ["iter.h", "cffi.h", "pocketpy.h"] ] diff --git a/src/ceval.h b/src/ceval.h index 75bf82c0..3167306b 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -1,6 +1,7 @@ #pragma once #include "vm.h" +#include "ref.h" namespace pkpy{ @@ -283,8 +284,8 @@ PyVar VM::run_frame(Frame* frame){ PyVar stop = frame->pop_value(this); PyVar start = frame->pop_value(this); Slice s; - if(start != None) { s.start = (int)PyInt_AS_C(start);} - if(stop != None) { s.stop = (int)PyInt_AS_C(stop);} + if(start != None) { s.start = (int)py_cast_v(this, start);} + if(stop != None) { s.stop = (int)py_cast_v(this, stop);} frame->push(PySlice(s)); } continue; case OP_IMPORT_NAME: { diff --git a/src/cffi.h b/src/cffi.h index 746498be..c574135b 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -78,12 +78,12 @@ struct Pointer{ vm->bind_method<1>(type, "__add__", [](VM* vm, Args& args) { Pointer& self = vm->py_cast(args[0]); - return vm->new_object(self + vm->PyInt_AS_C(args[1])); + return vm->new_object(self + py_cast(vm, args[1])); }); vm->bind_method<1>(type, "__sub__", [](VM* vm, Args& args) { Pointer& self = vm->py_cast(args[0]); - return vm->new_object(self - vm->PyInt_AS_C(args[1])); + return vm->new_object(self - py_cast_v(vm, args[1])); }); vm->bind_method<1>(type, "__eq__", [](VM* vm, Args& args) { @@ -101,13 +101,13 @@ struct Pointer{ // https://docs.python.org/zh-cn/3/library/ctypes.html vm->bind_method<1>(type, "__getitem__", [](VM* vm, Args& args) { Pointer& self = vm->py_cast(args[0]); - i64 index = vm->PyInt_AS_C(args[1]); + i64 index = py_cast_v(vm, args[1]); return (self+index).get(vm); }); vm->bind_method<2>(type, "__setitem__", [](VM* vm, Args& args) { Pointer& self = vm->py_cast(args[0]); - i64 index = vm->PyInt_AS_C(args[1]); + i64 index = py_cast_v(vm, args[1]); (self+index).set(vm, args[2]); return vm->None; }); @@ -164,20 +164,20 @@ struct Pointer{ void set(VM* vm, const PyVar& val){ switch(ctype.index){ - case C_TYPE("char_"): ref() = vm->PyInt_AS_C(val); break; - case C_TYPE("int_"): ref() = vm->PyInt_AS_C(val); break; + case C_TYPE("char_"): ref() = py_cast_v(vm, val); break; + case C_TYPE("int_"): ref() = py_cast_v(vm, val); break; case C_TYPE("float_"): ref() = vm->PyFloat_AS_C(val); break; case C_TYPE("double_"): ref() = vm->PyFloat_AS_C(val); break; case C_TYPE("bool_"): ref() = vm->PyBool_AS_C(val); break; case C_TYPE("void_"): vm->ValueError("cannot set void*"); break; - case C_TYPE("int8_"): ref() = vm->PyInt_AS_C(val); break; - case C_TYPE("int16_"): ref() = vm->PyInt_AS_C(val); break; - case C_TYPE("int32_"): ref() = vm->PyInt_AS_C(val); break; - case C_TYPE("int64_"): ref() = vm->PyInt_AS_C(val); break; - case C_TYPE("uint8_"): ref() = vm->PyInt_AS_C(val); break; - case C_TYPE("uint16_"): ref() = vm->PyInt_AS_C(val); break; - case C_TYPE("uint32_"): ref() = vm->PyInt_AS_C(val); break; - case C_TYPE("uint64_"): ref() = vm->PyInt_AS_C(val); break; + case C_TYPE("int8_"): ref() = py_cast_v(vm, val); break; + case C_TYPE("int16_"): ref() = py_cast_v(vm, val); break; + case C_TYPE("int32_"): ref() = py_cast_v(vm, val); break; + case C_TYPE("int64_"): ref() = py_cast_v(vm, val); break; + case C_TYPE("uint8_"): ref() = py_cast_v(vm, val); break; + case C_TYPE("uint16_"): ref() = py_cast_v(vm, val); break; + case C_TYPE("uint32_"): ref() = py_cast_v(vm, val); break; + case C_TYPE("uint64_"): ref() = py_cast_v(vm, val); break; case C_TYPE("void_p_"): ref() = vm->py_cast(val).ptr; break; // use macro here to do extension default: UNREACHABLE(); @@ -262,7 +262,7 @@ void add_module_c(VM* vm){ vm->setattr(mod, "nullptr", vm->new_object(nullptr, C_TYPE_T("void_"))); vm->bind_func<1>(mod, "malloc", [](VM* vm, Args& args) { - i64 size = vm->PyInt_AS_C(args[0]); + i64 size = py_cast_v(vm, args[0]); return vm->new_object(malloc(size), C_TYPE_T("void_")); }); @@ -280,15 +280,15 @@ void add_module_c(VM* vm){ vm->bind_func<3>(mod, "memcpy", [](VM* vm, Args& args) { Pointer& dst = vm->py_cast(args[0]); Pointer& src = vm->py_cast(args[1]); - i64 size = vm->PyInt_AS_C(args[2]); + i64 size = py_cast_v(vm, args[2]); memcpy(dst.ptr, src.ptr, size); return vm->None; }); vm->bind_func<3>(mod, "memset", [](VM* vm, Args& args) { Pointer& dst = vm->py_cast(args[0]); - i64 val = vm->PyInt_AS_C(args[1]); - i64 size = vm->PyInt_AS_C(args[2]); + i64 val = py_cast_v(vm, args[1]); + i64 size = py_cast_v(vm, args[2]); memset(dst.ptr, (int)val, size); return vm->None; }); diff --git a/src/codeobject.h b/src/codeobject.h index 3a000ed5..97ad83b7 100644 --- a/src/codeobject.h +++ b/src/codeobject.h @@ -1,11 +1,17 @@ #pragma once #include "obj.h" -#include "ref.h" #include "error.h" namespace pkpy{ +enum NameScope { + NAME_LOCAL = 0, + NAME_GLOBAL, + NAME_ATTR, + NAME_SPECIAL, +}; + enum Opcode { #define OPCODE(name) OP_##name, #include "opcodes.h" diff --git a/src/obj.h b/src/obj.h index 8fea9eac..62b7284d 100644 --- a/src/obj.h +++ b/src/obj.h @@ -164,14 +164,13 @@ union __8B { __8B(f64 val) : _float(val) {} }; -// Cast a PyVar to a native type `T` by reference +template +T py_cast_v(VM* vm, const PyVar& var) { UNREACHABLE(); } +template +T _py_cast_v(VM* vm, const PyVar& var) { UNREACHABLE(); } template T& py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); } template -T py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); } -template T& _py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); } -template -T _py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); } } // namespace pkpy \ No newline at end of file diff --git a/src/pocketpy.h b/src/pocketpy.h index 54e328b2..ecc00117 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -90,7 +90,7 @@ void init_builtins(VM* _vm) { _vm->bind_builtin_func<-1>("exit", [](VM* vm, Args& args) { if(args.size() == 0) std::exit(0); - else if(args.size() == 1) std::exit((int)vm->PyInt_AS_C(args[0])); + else if(args.size() == 1) std::exit((int)py_cast_v(vm, args[0])); else vm->TypeError("exit() takes at most 1 argument"); return vm->None; }); @@ -105,7 +105,7 @@ void init_builtins(VM* _vm) { }); _vm->bind_builtin_func<1>("chr", [](VM* vm, Args& args) { - i64 i = vm->PyInt_AS_C(args[0]); + i64 i = py_cast_v(vm, args[0]); if (i < 0 || i > 128) vm->ValueError("chr() arg not in range(128)"); return vm->PyStr(std::string(1, (char)i)); }); @@ -132,7 +132,7 @@ void init_builtins(VM* _vm) { _vm->bind_builtin_func<1>("hex", [](VM* vm, Args& args) { std::stringstream ss; - ss << std::hex << vm->PyInt_AS_C(args[0]); + ss << std::hex << py_cast_v(vm, args[0]); return vm->PyStr("0x" + ss.str()); }); @@ -167,9 +167,9 @@ void init_builtins(VM* _vm) { _vm->bind_static_method<-1>("range", "__new__", [](VM* vm, Args& args) { Range r; switch (args.size()) { - case 1: r.stop = vm->PyInt_AS_C(args[0]); break; - case 2: r.start = vm->PyInt_AS_C(args[0]); r.stop = vm->PyInt_AS_C(args[1]); break; - case 3: r.start = vm->PyInt_AS_C(args[0]); r.stop = vm->PyInt_AS_C(args[1]); r.step = vm->PyInt_AS_C(args[2]); break; + case 1: r.stop = py_cast_v(vm, args[0]); break; + case 2: r.start = py_cast_v(vm, args[0]); r.stop = py_cast_v(vm, args[1]); break; + 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 vm->PyRange(r); @@ -228,22 +228,22 @@ void init_builtins(VM* _vm) { }); _vm->bind_method<1>("int", "__floordiv__", [](VM* vm, Args& args) { - i64 rhs = vm->PyInt_AS_C(args[1]); + i64 rhs = py_cast_v(vm, args[1]); if(rhs == 0) vm->ZeroDivisionError(); - return py_object(vm, vm->PyInt_AS_C(args[0]) / rhs); + return py_object(vm, py_cast_v(vm, args[0]) / rhs); }); _vm->bind_method<1>("int", "__mod__", [](VM* vm, Args& args) { - i64 rhs = vm->PyInt_AS_C(args[1]); + i64 rhs = py_cast_v(vm, args[1]); if(rhs == 0) vm->ZeroDivisionError(); - return py_object(vm, vm->PyInt_AS_C(args[0]) % rhs); + return py_object(vm, py_cast_v(vm, args[0]) % rhs); }); - _vm->bind_method<0>("int", "__repr__", CPP_LAMBDA(vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0]))))); - _vm->bind_method<0>("int", "__json__", CPP_LAMBDA(vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0]))))); + _vm->bind_method<0>("int", "__repr__", CPP_LAMBDA(vm->PyStr(std::to_string(py_cast_v(vm, args[0]))))); + _vm->bind_method<0>("int", "__json__", CPP_LAMBDA(vm->PyStr(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, vm->PyInt_AS_C(args[0]) op vm->PyInt_AS_C(args[1])))); + _vm->bind_method<1>("int", #name, CPP_LAMBDA(py_object(vm, py_cast_v(vm, args[0]) op py_cast_v(vm, args[1])))); INT_BITWISE_OP(__lshift__, <<) INT_BITWISE_OP(__rshift__, >>) @@ -255,7 +255,7 @@ 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 vm->PyFloat((f64)vm->PyInt_AS_C(args[0])); + if (is_type(args[0], vm->tp_int)) return vm->PyFloat((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 vm->PyFloat(vm->_PyBool_AS_C(args[0]) ? 1.0 : 0.0); if (is_type(args[0], vm->tp_str)) { @@ -343,7 +343,7 @@ void init_builtins(VM* _vm) { return vm->PyStr(self.u8_substr(s.start, s.stop)); } - int index = (int)vm->PyInt_AS_C(args[1]); + int index = (int)py_cast_v(vm, args[1]); index = vm->normalized_index(index, self.u8_length()); return vm->PyStr(self.u8_getitem(index)); }); @@ -413,7 +413,7 @@ void init_builtins(VM* _vm) { _vm->bind_method<1>("list", "__mul__", [](VM* vm, Args& args) { const List& self = vm->PyList_AS_C(args[0]); - int n = (int)vm->PyInt_AS_C(args[1]); + int n = (int)py_cast_v(vm, args[1]); List result; result.reserve(self.size() * n); for(int i = 0; i < n; i++) result.insert(result.end(), self.begin(), self.end()); @@ -422,7 +422,7 @@ void init_builtins(VM* _vm) { _vm->bind_method<2>("list", "insert", [](VM* vm, Args& args) { List& _self = vm->PyList_AS_C(args[0]); - int index = (int)vm->PyInt_AS_C(args[1]); + int index = (int)py_cast_v(vm, args[1]); if(index < 0) index += _self.size(); if(index < 0) index = 0; if(index > _self.size()) index = _self.size(); @@ -465,14 +465,14 @@ void init_builtins(VM* _vm) { return vm->PyList(std::move(new_list)); } - int index = (int)vm->PyInt_AS_C(args[1]); + int index = (int)py_cast_v(vm, args[1]); index = vm->normalized_index(index, self.size()); return self[index]; }); _vm->bind_method<2>("list", "__setitem__", [](VM* vm, Args& args) { List& self = vm->PyList_AS_C(args[0]); - int index = (int)vm->PyInt_AS_C(args[1]); + int index = (int)py_cast_v(vm, args[1]); index = vm->normalized_index(index, self.size()); self[index] = args[2]; return vm->None; @@ -480,7 +480,7 @@ void init_builtins(VM* _vm) { _vm->bind_method<1>("list", "__delitem__", [](VM* vm, Args& args) { List& self = vm->PyList_AS_C(args[0]); - int index = (int)vm->PyInt_AS_C(args[1]); + int index = (int)py_cast_v(vm, args[1]); index = vm->normalized_index(index, self.size()); self.erase(self.begin() + index); return vm->None; @@ -507,7 +507,7 @@ void init_builtins(VM* _vm) { return vm->PyTuple(std::move(new_list)); } - int index = (int)vm->PyInt_AS_C(args[1]); + int index = (int)py_cast_v(vm, args[1]); index = vm->normalized_index(index, self.size()); return self[index]; }); @@ -569,7 +569,7 @@ void add_module_sys(VM* vm){ vm->bind_func<0>(mod, "getrecursionlimit", CPP_LAMBDA(py_object(vm, vm->recursionlimit))); vm->bind_func<1>(mod, "setrecursionlimit", [](VM* vm, Args& args) { - vm->recursionlimit = (int)vm->PyInt_AS_C(args[0]); + vm->recursionlimit = (int)py_cast_v(vm, args[0]); return vm->None; }); } @@ -698,7 +698,7 @@ struct ReMatch { vm->bind_method<1>(type, "group", [](VM* vm, Args& args) { auto& self = vm->py_cast(args[0]); - int index = (int)vm->PyInt_AS_C(args[1]); + int index = (int)py_cast_v(vm, args[1]); index = vm->normalized_index(index, self.m.size()); return vm->PyStr(self.m[index].str()); }); @@ -759,14 +759,14 @@ void add_module_random(VM* vm){ PyVar mod = vm->new_module("random"); std::srand(std::time(0)); vm->bind_func<1>(mod, "seed", [](VM* vm, Args& args) { - std::srand((unsigned int)vm->PyInt_AS_C(args[0])); + std::srand((unsigned int)py_cast_v(vm, args[0])); return vm->None; }); vm->bind_func<0>(mod, "random", CPP_LAMBDA(vm->PyFloat(std::rand() / (f64)RAND_MAX))); vm->bind_func<2>(mod, "randint", [](VM* vm, Args& args) { - i64 a = vm->PyInt_AS_C(args[0]); - i64 b = vm->PyInt_AS_C(args[1]); + 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)); }); diff --git a/src/ref.h b/src/ref.h index 0529f964..5e7e8e5f 100644 --- a/src/ref.h +++ b/src/ref.h @@ -1,6 +1,7 @@ #pragma once #include "obj.h" +#include "vm.h" namespace pkpy { @@ -11,22 +12,61 @@ struct BaseRef { virtual ~BaseRef() = default; }; -enum NameScope { - NAME_LOCAL = 0, - NAME_GLOBAL, - NAME_ATTR, - NAME_SPECIAL, -}; - struct NameRef : BaseRef { const std::pair pair; inline StrName name() const { return pair.first; } inline NameScope scope() const { return pair.second; } NameRef(const std::pair& pair) : pair(pair) {} - PyVar get(VM* vm, Frame* frame) const; - void set(VM* vm, Frame* frame, PyVar val) const; - void del(VM* vm, Frame* frame) const; + PyVar get(VM* vm, Frame* frame) const{ + PyVar* val; + val = frame->f_locals().try_get(name()); + if(val != nullptr) return *val; + val = frame->f_closure_try_get(name()); + if(val != nullptr) return *val; + val = frame->f_globals().try_get(name()); + if(val != nullptr) return *val; + val = vm->builtins->attr().try_get(name()); + if(val != nullptr) return *val; + vm->NameError(name()); + return nullptr; + } + + void set(VM* vm, Frame* frame, PyVar val) const{ + switch(scope()) { + case NAME_LOCAL: frame->f_locals().set(name(), std::move(val)); break; + case NAME_GLOBAL: + if(frame->f_locals().try_set(name(), std::move(val))) return; + frame->f_globals().set(name(), std::move(val)); + break; + default: UNREACHABLE(); + } + } + + void del(VM* vm, Frame* frame) const{ + switch(scope()) { + case NAME_LOCAL: { + if(frame->f_locals().contains(name())){ + frame->f_locals().erase(name()); + }else{ + vm->NameError(name()); + } + } break; + case NAME_GLOBAL: + { + if(frame->f_locals().contains(name())){ + frame->f_locals().erase(name()); + }else{ + if(frame->f_globals().contains(name())){ + frame->f_globals().erase(name()); + }else{ + vm->NameError(name()); + } + } + } break; + default: UNREACHABLE(); + } + } }; struct AttrRef : BaseRef { @@ -34,9 +74,19 @@ struct AttrRef : BaseRef { NameRef attr; AttrRef(PyVar obj, NameRef attr) : obj(obj), attr(attr) {} - PyVar get(VM* vm, Frame* frame) const; - void set(VM* vm, Frame* frame, PyVar val) const; - void del(VM* vm, Frame* frame) const; + PyVar get(VM* vm, Frame* frame) const{ + return vm->getattr(obj, attr.name()); + } + + void set(VM* vm, Frame* frame, PyVar val) const{ + vm->setattr(obj, attr.name(), std::move(val)); + } + + void del(VM* vm, Frame* frame) const{ + if(!obj->is_attr_valid()) vm->TypeError("cannot delete attribute"); + if(!obj->attr().contains(attr.name())) vm->AttributeError(obj, attr.name()); + obj->attr().erase(attr.name()); + } }; struct IndexRef : BaseRef { @@ -44,19 +94,78 @@ struct IndexRef : BaseRef { PyVar index; IndexRef(PyVar obj, PyVar index) : obj(obj), index(index) {} - PyVar get(VM* vm, Frame* frame) const; - void set(VM* vm, Frame* frame, PyVar val) const; - void del(VM* vm, Frame* frame) const; + PyVar get(VM* vm, Frame* frame) const{ + return vm->fast_call(__getitem__, two_args(obj, index)); + } + + void set(VM* vm, Frame* frame, PyVar val) const{ + Args args(3); + args[0] = obj; args[1] = index; args[2] = std::move(val); + vm->fast_call(__setitem__, std::move(args)); + } + + void del(VM* vm, Frame* frame) const{ + vm->fast_call(__delitem__, two_args(obj, index)); + } }; struct TupleRef : BaseRef { Tuple objs; TupleRef(Tuple&& objs) : objs(std::move(objs)) {} - PyVar get(VM* vm, Frame* frame) const; - void set(VM* vm, Frame* frame, PyVar val) const; - void del(VM* vm, Frame* frame) const; + PyVar get(VM* vm, Frame* frame) const{ + Tuple args(objs.size()); + for (int i = 0; i < objs.size(); i++) { + args[i] = vm->PyRef_AS_C(objs[i])->get(vm, frame); + } + return vm->PyTuple(std::move(args)); + } + + void set(VM* vm, Frame* frame, PyVar val) const{ + val = vm->asIter(val); + BaseIter* iter = vm->PyIter_AS_C(val); + for(int i=0; itp_star_wrapper)){ + auto& star = vm->PyStarWrapper_AS_C(objs[i]); + if(star.rvalue) vm->ValueError("can't use starred expression here"); + if(i != objs.size()-1) vm->ValueError("* can only be used at the end"); + auto ref = vm->PyRef_AS_C(star.obj); + List list; + while((x = iter->next()) != nullptr) list.push_back(x); + ref->set(vm, frame, vm->PyList(std::move(list))); + return; + }else{ + x = iter->next(); + if(x == nullptr) vm->ValueError("not enough values to unpack"); + vm->PyRef_AS_C(objs[i])->set(vm, frame, x); + } + } + PyVarOrNull x = iter->next(); + if(x != nullptr) vm->ValueError("too many values to unpack"); + } + + void del(VM* vm, Frame* frame) const{ + for(int i=0; iPyRef_AS_C(objs[i])->del(vm, frame); + } }; +template +PyVarRef VM::PyRef(P&& value) { + static_assert(std::is_base_of_v); + return new_object(tp_ref, std::forward

(value)); +} + +const BaseRef* VM::PyRef_AS_C(const PyVar& obj) +{ + if(!is_type(obj, tp_ref)) TypeError("expected an l-value"); + return static_cast(obj->value()); +} + +/***** Frame's Impl *****/ +inline void Frame::try_deref(VM* vm, PyVar& v){ + if(is_type(v, vm->tp_ref)) v = vm->PyRef_AS_C(v)->get(vm, this); +} + } // namespace pkpy \ No newline at end of file diff --git a/src/vm.h b/src/vm.h index 6b91b1e1..38339707 100644 --- a/src/vm.h +++ b/src/vm.h @@ -90,19 +90,6 @@ public: return call(obj, __repr__); } - const PyVar& asBool(const PyVar& obj){ - if(is_type(obj, tp_bool)) return obj; - if(obj == None) return False; - if(is_type(obj, tp_int)) return PyBool(PyInt_AS_C(obj) != 0); - if(is_type(obj, tp_float)) return PyBool(PyFloat_AS_C(obj) != 0.0); - PyVarOrNull len_fn = getattr(obj, __len__, false); - if(len_fn != nullptr){ - PyVar ret = call(len_fn); - return PyBool(PyInt_AS_C(ret) > 0); - } - return True; - } - PyVar asIter(const PyVar& obj){ if(is_type(obj, tp_native_iterator)) return obj; PyVarOrNull iter_f = getattr(obj, __iter__, false); @@ -441,15 +428,7 @@ public: bind_func(builtins, funcName, fn); } - inline f64 num_to_float(const PyVar& obj){ - if(is_float(obj)){ - return PyFloat_AS_C(obj); - } else if (is_int(obj)){ - return (f64)PyInt_AS_C(obj); - } - TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape(true)); - return 0; - } + int normalized_index(int index, int size){ if(index < 0) index += size; @@ -535,18 +514,6 @@ public: Type tp_slice, tp_range, tp_module, tp_ref; Type tp_super, tp_exception, tp_star_wrapper; - template - inline PyVarRef PyRef(P&& value) { - static_assert(std::is_base_of_v); - return new_object(tp_ref, std::forward

(value)); - } - - inline const BaseRef* PyRef_AS_C(const PyVar& obj) - { - if(!is_type(obj, tp_ref)) TypeError("expected an l-value"); - return static_cast(obj->value()); - } - template inline PyVar PyIter(P&& value) { static_assert(std::is_base_of_v); @@ -572,11 +539,6 @@ public: return new_object(tp_str, value); } - inline i64 PyInt_AS_C(const PyVar& obj){ - check_type(obj, tp_int); - return obj.bits >> 2; - } - inline i64 _PyInt_AS_C(const PyVar& obj){ return obj.bits >> 2; } @@ -676,28 +638,6 @@ public: for(auto [k, v]: _modules.items()) v->attr()._try_perfect_rehash(); } - i64 hash(const PyVar& obj){ - if (is_type(obj, tp_str)) return PyStr_AS_C(obj).hash(); - if (is_int(obj)) return PyInt_AS_C(obj); - if (is_type(obj, tp_tuple)) { - i64 x = 1000003; - const Tuple& items = PyTuple_AS_C(obj); - for (int i=0; i> 2)); // recommended by Github Copilot - } - return x; - } - if (is_type(obj, tp_type)) return obj.bits; - if (is_type(obj, tp_bool)) return _PyBool_AS_C(obj) ? 1 : 0; - if (is_float(obj)){ - f64 val = PyFloat_AS_C(obj); - return (i64)std::hash()(val); - } - TypeError("unhashable type: " + OBJ_NAME(_t(obj)).escape(true)); - return 0; - } - /***** Error Reporter *****/ void _error(StrName name, const Str& msg){ _error(Exception(name, msg)); @@ -789,128 +729,15 @@ public: CodeObject_ compile(Str source, Str filename, CompileMode mode); void post_init(); PyVar num_negated(const PyVar& obj); + f64 num_to_float(const PyVar& obj); + const PyVar& asBool(const PyVar& obj); + i64 hash(const PyVar& obj); + + template + PyVarRef PyRef(P&& value); + const BaseRef* PyRef_AS_C(const PyVar& obj); }; -/***** Pointers' Impl *****/ -PyVar NameRef::get(VM* vm, Frame* frame) const{ - PyVar* val; - val = frame->f_locals().try_get(name()); - if(val != nullptr) return *val; - val = frame->f_closure_try_get(name()); - if(val != nullptr) return *val; - val = frame->f_globals().try_get(name()); - if(val != nullptr) return *val; - val = vm->builtins->attr().try_get(name()); - if(val != nullptr) return *val; - vm->NameError(name()); - return nullptr; -} - -void NameRef::set(VM* vm, Frame* frame, PyVar val) const{ - switch(scope()) { - case NAME_LOCAL: frame->f_locals().set(name(), std::move(val)); break; - case NAME_GLOBAL: - if(frame->f_locals().try_set(name(), std::move(val))) return; - frame->f_globals().set(name(), std::move(val)); - break; - default: UNREACHABLE(); - } -} - -void NameRef::del(VM* vm, Frame* frame) const{ - switch(scope()) { - case NAME_LOCAL: { - if(frame->f_locals().contains(name())){ - frame->f_locals().erase(name()); - }else{ - vm->NameError(name()); - } - } break; - case NAME_GLOBAL: - { - if(frame->f_locals().contains(name())){ - frame->f_locals().erase(name()); - }else{ - if(frame->f_globals().contains(name())){ - frame->f_globals().erase(name()); - }else{ - vm->NameError(name()); - } - } - } break; - default: UNREACHABLE(); - } -} - -PyVar AttrRef::get(VM* vm, Frame* frame) const{ - return vm->getattr(obj, attr.name()); -} - -void AttrRef::set(VM* vm, Frame* frame, PyVar val) const{ - vm->setattr(obj, attr.name(), std::move(val)); -} - -void AttrRef::del(VM* vm, Frame* frame) const{ - if(!obj->is_attr_valid()) vm->TypeError("cannot delete attribute"); - if(!obj->attr().contains(attr.name())) vm->AttributeError(obj, attr.name()); - obj->attr().erase(attr.name()); -} - -PyVar IndexRef::get(VM* vm, Frame* frame) const{ - return vm->fast_call(__getitem__, two_args(obj, index)); -} - -void IndexRef::set(VM* vm, Frame* frame, PyVar val) const{ - Args args(3); - args[0] = obj; args[1] = index; args[2] = std::move(val); - vm->fast_call(__setitem__, std::move(args)); -} - -void IndexRef::del(VM* vm, Frame* frame) const{ - vm->fast_call(__delitem__, two_args(obj, index)); -} - -PyVar TupleRef::get(VM* vm, Frame* frame) const{ - Tuple args(objs.size()); - for (int i = 0; i < objs.size(); i++) { - args[i] = vm->PyRef_AS_C(objs[i])->get(vm, frame); - } - return vm->PyTuple(std::move(args)); -} - -void TupleRef::set(VM* vm, Frame* frame, PyVar val) const{ - val = vm->asIter(val); - BaseIter* iter = vm->PyIter_AS_C(val); - for(int i=0; itp_star_wrapper)){ - auto& star = vm->PyStarWrapper_AS_C(objs[i]); - if(star.rvalue) vm->ValueError("can't use starred expression here"); - if(i != objs.size()-1) vm->ValueError("* can only be used at the end"); - auto ref = vm->PyRef_AS_C(star.obj); - List list; - while((x = iter->next()) != nullptr) list.push_back(x); - ref->set(vm, frame, vm->PyList(std::move(list))); - return; - }else{ - x = iter->next(); - if(x == nullptr) vm->ValueError("not enough values to unpack"); - vm->PyRef_AS_C(objs[i])->set(vm, frame, x); - } - } - PyVarOrNull x = iter->next(); - if(x != nullptr) vm->ValueError("too many values to unpack"); -} - -void TupleRef::del(VM* vm, Frame* frame) const{ - for(int i=0; iPyRef_AS_C(objs[i])->del(vm, frame); -} - -/***** Frame's Impl *****/ -inline void Frame::try_deref(VM* vm, PyVar& v){ - if(is_type(v, vm->tp_ref)) v = vm->PyRef_AS_C(v)->get(vm, this); -} - PyVar NativeFunc::operator()(VM* vm, Args& args) const{ int args_size = args.size() - (int)method; // remove self if(argc != -1 && args_size != argc) { @@ -961,11 +788,11 @@ std::enable_if_t, PyVar> py_object(VM* vm, T _val){ val = (val << 2) | 0b01; return PyVar(reinterpret_cast(val)); } -template<> i64 py_cast(VM* vm, const PyVar& obj){ +template<> i64 py_cast_v(VM* vm, const PyVar& obj){ vm->check_type(obj, vm->tp_int); return obj.bits >> 2; } -template<> i64 _py_cast(VM* vm, const PyVar& obj){ +template<> i64 _py_cast_v(VM* vm, const PyVar& obj){ return obj.bits >> 2; } @@ -975,13 +802,13 @@ PyVar py_object(VM* vm, f64 val){ bits |= 0b10; return PyVar(reinterpret_cast(bits)); } -template<> f64 py_cast(VM* vm, const PyVar& obj){ +template<> f64 py_cast_v(VM* vm, const PyVar& obj){ vm->check_type(obj, vm->tp_float); i64 bits = obj.bits; bits = (bits >> 2) << 2; return __8B(bits)._float; } -template<> f64 _py_cast(VM* vm, const PyVar& obj){ +template<> f64 _py_cast_v(VM* vm, const PyVar& obj){ i64 bits = obj.bits; bits = (bits >> 2) << 2; return __8B(bits)._float; @@ -990,11 +817,11 @@ template<> f64 _py_cast(VM* vm, const PyVar& obj){ PyVar py_object(VM* vm, bool val){ return val ? vm->True : vm->False; } -template<> bool py_cast(VM* vm, const PyVar& obj){ +template<> bool py_cast_v(VM* vm, const PyVar& obj){ vm->check_type(obj, vm->tp_bool); return obj == vm->True; } -template<> bool _py_cast(VM* vm, const PyVar& obj){ +template<> bool _py_cast_v(VM* vm, const PyVar& obj){ return obj == vm->True; } @@ -1011,7 +838,7 @@ DEF_NATIVE_2(StarWrapper, tp_star_wrapper) PyVar VM::num_negated(const PyVar& obj){ if (is_int(obj)){ - return py_object(this, -PyInt_AS_C(obj)); + return py_object(this, -py_cast_v(this, obj)); }else if(is_float(obj)){ return py_object(this, -PyFloat_AS_C(obj)); } @@ -1019,4 +846,49 @@ PyVar VM::num_negated(const PyVar& obj){ return nullptr; } +f64 VM::num_to_float(const PyVar& obj){ + if(is_float(obj)){ + return PyFloat_AS_C(obj); + } else if (is_int(obj)){ + return (f64)py_cast_v(this, obj); + } + TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape(true)); + return 0; +} + +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 PyBool(py_cast_v(this, obj) != 0); + if(is_type(obj, tp_float)) return PyBool(PyFloat_AS_C(obj) != 0.0); + PyVarOrNull len_fn = getattr(obj, __len__, false); + if(len_fn != nullptr){ + PyVar ret = call(len_fn); + return PyBool(py_cast_v(this, ret) > 0); + } + return True; +} + +i64 VM::hash(const PyVar& obj){ + if (is_type(obj, tp_str)) return PyStr_AS_C(obj).hash(); + if (is_int(obj)) return py_cast_v(this, obj); + if (is_type(obj, tp_tuple)) { + i64 x = 1000003; + const Tuple& items = PyTuple_AS_C(obj); + for (int i=0; i> 2)); // recommended by Github Copilot + } + return x; + } + if (is_type(obj, tp_type)) return obj.bits; + if (is_type(obj, tp_bool)) return _PyBool_AS_C(obj) ? 1 : 0; + if (is_float(obj)){ + f64 val = PyFloat_AS_C(obj); + return (i64)std::hash()(val); + } + TypeError("unhashable type: " + OBJ_NAME(_t(obj)).escape(true)); + return 0; +} + } // namespace pkpy \ No newline at end of file