>::value, "P should derive from BaseRef");
return new_object(tp_ref, std::forward(value));
}
inline const BaseRef* PyRef_AS_C(const PyVar& obj)
{
if(!obj->is_type(tp_ref)) TypeError("expected an l-value");
return (const BaseRef*)(obj->value());
}
inline const Str& PyStr_AS_C(const PyVar& obj) {
check_type(obj, tp_str);
return OBJ_GET(Str, obj);
}
inline PyVar PyStr(const Str& value) {
// some BUGs here
// if(value.size() == 1){
// char c = value.c_str()[0];
// if(c >= 0) return _ascii_str_pool[(int)c];
// }
return new_object(tp_str, value);
}
DEF_NATIVE(Int, i64, tp_int)
DEF_NATIVE(Float, f64, tp_float)
DEF_NATIVE(List, pkpy::List, tp_list)
DEF_NATIVE(Tuple, pkpy::Tuple, tp_tuple)
DEF_NATIVE(Function, pkpy::Function_, tp_function)
DEF_NATIVE(NativeFunc, pkpy::NativeFunc, tp_native_function)
DEF_NATIVE(Iter, pkpy::shared_ptr, tp_native_iterator)
DEF_NATIVE(BoundMethod, pkpy::BoundMethod, tp_bound_method)
DEF_NATIVE(Range, pkpy::Range, tp_range)
DEF_NATIVE(Slice, pkpy::Slice, tp_slice)
DEF_NATIVE(Exception, pkpy::Exception, tp_exception)
// there is only one True/False, so no need to copy them!
inline bool PyBool_AS_C(const PyVar& obj){return obj == True;}
inline const PyVar& PyBool(bool value){return value ? True : False;}
void init_builtin_types(){
PyVar _tp_object = pkpy::make_shared>(1, 0);
PyVar _tp_type = pkpy::make_shared>(1, 1);
_all_types.push_back(_tp_object);
_all_types.push_back(_tp_type);
tp_object = 0; tp_type = 1;
_types["object"] = _tp_object;
_types["type"] = _tp_type;
tp_bool = _new_type_object("bool");
tp_int = _new_type_object("int");
tp_float = _new_type_object("float");
tp_str = _new_type_object("str");
tp_list = _new_type_object("list");
tp_tuple = _new_type_object("tuple");
tp_slice = _new_type_object("slice");
tp_range = _new_type_object("range");
tp_module = _new_type_object("module");
tp_ref = _new_type_object("_ref");
tp_function = _new_type_object("function");
tp_native_function = _new_type_object("native_function");
tp_native_iterator = _new_type_object("native_iterator");
tp_bound_method = _new_type_object("bound_method");
tp_super = _new_type_object("super");
tp_exception = _new_type_object("Exception");
this->None = new_object(_new_type_object("NoneType"), DUMMY_VAL);
this->Ellipsis = new_object(_new_type_object("ellipsis"), DUMMY_VAL);
this->True = new_object(tp_bool, true);
this->False = new_object(tp_bool, false);
this->builtins = new_module("builtins");
this->_main = new_module("__main__");
this->_py_op_call = new_object(_new_type_object("_internal"), DUMMY_VAL);
this->_py_op_yield = new_object(_new_type_object("_internal"), DUMMY_VAL);
setattr(_t(tp_type), __base__, _t(tp_object));
setattr(_t(tp_object), __base__, None);
for (auto& [name, type] : _types) {
setattr(type, __name__, PyStr(name));
}
std::vector publicTypes = {"type", "object", "bool", "int", "float", "str", "list", "tuple", "range"};
for (auto& name : publicTypes) {
setattr(builtins, name, _types[name]);
}
}
i64 hash(const PyVar& obj){
if (obj->is_type(tp_int)) return PyInt_AS_C(obj);
if (obj->is_type(tp_bool)) return PyBool_AS_C(obj) ? 1 : 0;
if (obj->is_type(tp_float)){
f64 val = PyFloat_AS_C(obj);
return (i64)std::hash()(val);
}
if (obj->is_type(tp_str)) return PyStr_AS_C(obj).hash();
if (obj->is_type(tp_type)) return (i64)obj.get();
if (obj->is_type(tp_tuple)) {
i64 x = 1000003;
const pkpy::Tuple& items = PyTuple_AS_C(obj);
for (int i=0; i> 2)); // recommended by Github Copilot
}
return x;
}
TypeError("unhashable type: " + OBJ_NAME(_t(obj)).escape(true));
return 0;
}
/***** Error Reporter *****/
private:
void _error(const Str& name, const Str& msg){
_error(pkpy::Exception(name, msg));
}
void _error(pkpy::Exception e){
if(callstack.empty()){
e.is_re = false;
throw e;
}
top_frame()->push(PyException(e));
_raise();
}
void _raise(){
bool ok = top_frame()->jump_to_exception_handler();
if(ok) throw HandledException();
else throw UnhandledException();
}
public:
void IOError(const Str& msg) { _error("IOError", msg); }
void NotImplementedError(){ _error("NotImplementedError", ""); }
void TypeError(const Str& msg){ _error("TypeError", msg); }
void ZeroDivisionError(){ _error("ZeroDivisionError", "division by zero"); }
void IndexError(const Str& msg){ _error("IndexError", msg); }
void ValueError(const Str& msg){ _error("ValueError", msg); }
void NameError(const Str& name){ _error("NameError", "name " + name.escape(true) + " is not defined"); }
void AttributeError(PyVar obj, const Str& name){
_error("AttributeError", "type " + OBJ_NAME(_t(obj)).escape(true) + " has no attribute " + name.escape(true));
}
inline void check_type(const PyVar& obj, Type type){
if(obj->is_type(type)) return;
TypeError("expected " + OBJ_NAME(_t(type)).escape(true) + ", but got " + OBJ_NAME(_t(obj)).escape(true));
}
inline PyVar& _t(Type t){
return _all_types[t.index];
}
inline PyVar& _t(const PyVar& obj){
return _all_types[OBJ_GET(Type, _t(obj->type)).index];
}
template
PyVar register_class(PyVar mod){
PyVar type = new_type_object(mod, T::_name(), _t(tp_object));
if(OBJ_NAME(mod) != T::_mod()) UNREACHABLE();
T::_register(this, mod, type);
return type;
}
template
inline T& py_cast(const PyVar& obj){
check_type(obj, T::_type(this));
return OBJ_GET(T, obj);
}
~VM() {
if(!use_stdio){
delete _stdout;
delete _stderr;
}
}
CodeObject_ compile(Str source, Str filename, CompileMode mode);
};
/***** Pointers' Impl *****/
PyVar NameRef::get(VM* vm, Frame* frame) const{
PyVar* val;
val = frame->f_locals().try_get(name());
if(val) return *val;
val = frame->f_globals().try_get(name());
if(val) return *val;
val = vm->builtins->attr().try_get(name());
if(val) 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()[name()] = std::move(val); break;
case NAME_GLOBAL:
{
PyVar* existing = frame->f_locals().try_get(name());
if(existing != nullptr){
*existing = std::move(val);
}else{
frame->f_globals()[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(), 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->call(obj, __getitem__, pkpy::one_arg(index));
}
void IndexRef::set(VM* vm, Frame* frame, PyVar val) const{
vm->call(obj, __setitem__, pkpy::two_args(index, val));
}
void IndexRef::del(VM* vm, Frame* frame) const{
vm->call(obj, __delitem__, pkpy::one_arg(index));
}
PyVar TupleRef::get(VM* vm, Frame* frame) const{
pkpy::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{
#define TUPLE_REF_SET() \
if(args.size() > objs.size()) vm->ValueError("too many values to unpack"); \
if(args.size() < objs.size()) vm->ValueError("not enough values to unpack"); \
for (int i = 0; i < objs.size(); i++) vm->PyRef_AS_C(objs[i])->set(vm, frame, args[i]);
if(val->is_type(vm->tp_tuple)){
const pkpy::Tuple& args = OBJ_GET(pkpy::Tuple, val);
TUPLE_REF_SET()
}else if(val->is_type(vm->tp_list)){
const pkpy::List& args = OBJ_GET(pkpy::List, val);
TUPLE_REF_SET()
}else{
vm->TypeError("only tuple or list can be unpacked");
}
#undef TUPLE_REF_SET
}
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(v->is_type(vm->tp_ref)) v = vm->PyRef_AS_C(v)->get(vm, this);
}
PyVar pkpy::NativeFunc::operator()(VM* vm, pkpy::Args& args) const{
int args_size = args.size() - (int)method; // remove self
if(argc != -1 && args_size != argc) {
vm->TypeError("expected " + std::to_string(argc) + " arguments, but got " + std::to_string(args_size));
}
return f(vm, args);
}
void CodeObject::optimize(VM* vm){
for(int i=1; inum_negated(consts[pos]);
}
}
}