mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
up
This commit is contained in:
parent
3c972c9f46
commit
fa22cdb3b2
@ -3,8 +3,8 @@ with open("src/opcodes.h", "rt", encoding='utf-8') as f:
|
|||||||
|
|
||||||
pipeline = [
|
pipeline = [
|
||||||
["common.h", "memory.h", "str.h", "tuplelist.h", "namedict.h", "builtins.h", "error.h"],
|
["common.h", "memory.h", "str.h", "tuplelist.h", "namedict.h", "builtins.h", "error.h"],
|
||||||
["obj.h", "parser.h", "ref.h", "codeobject.h", "frame.h"],
|
["obj.h", "parser.h", "codeobject.h", "frame.h"],
|
||||||
["vm.h", "ceval.h", "compiler.h", "repl.h"],
|
["vm.h", "ref.h", "ceval.h", "compiler.h", "repl.h"],
|
||||||
["iter.h", "cffi.h", "pocketpy.h"]
|
["iter.h", "cffi.h", "pocketpy.h"]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "vm.h"
|
#include "vm.h"
|
||||||
|
#include "ref.h"
|
||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
@ -283,8 +284,8 @@ PyVar VM::run_frame(Frame* frame){
|
|||||||
PyVar stop = frame->pop_value(this);
|
PyVar stop = frame->pop_value(this);
|
||||||
PyVar start = frame->pop_value(this);
|
PyVar start = frame->pop_value(this);
|
||||||
Slice s;
|
Slice s;
|
||||||
if(start != None) { s.start = (int)PyInt_AS_C(start);}
|
if(start != None) { s.start = (int)py_cast_v<i64>(this, start);}
|
||||||
if(stop != None) { s.stop = (int)PyInt_AS_C(stop);}
|
if(stop != None) { s.stop = (int)py_cast_v<i64>(this, stop);}
|
||||||
frame->push(PySlice(s));
|
frame->push(PySlice(s));
|
||||||
} continue;
|
} continue;
|
||||||
case OP_IMPORT_NAME: {
|
case OP_IMPORT_NAME: {
|
||||||
|
36
src/cffi.h
36
src/cffi.h
@ -78,12 +78,12 @@ struct Pointer{
|
|||||||
|
|
||||||
vm->bind_method<1>(type, "__add__", [](VM* vm, Args& args) {
|
vm->bind_method<1>(type, "__add__", [](VM* vm, Args& args) {
|
||||||
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
||||||
return vm->new_object<Pointer>(self + vm->PyInt_AS_C(args[1]));
|
return vm->new_object<Pointer>(self + py_cast<i64>(vm, args[1]));
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_method<1>(type, "__sub__", [](VM* vm, Args& args) {
|
vm->bind_method<1>(type, "__sub__", [](VM* vm, Args& args) {
|
||||||
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
||||||
return vm->new_object<Pointer>(self - vm->PyInt_AS_C(args[1]));
|
return vm->new_object<Pointer>(self - py_cast_v<i64>(vm, args[1]));
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_method<1>(type, "__eq__", [](VM* vm, Args& args) {
|
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
|
// https://docs.python.org/zh-cn/3/library/ctypes.html
|
||||||
vm->bind_method<1>(type, "__getitem__", [](VM* vm, Args& args) {
|
vm->bind_method<1>(type, "__getitem__", [](VM* vm, Args& args) {
|
||||||
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
||||||
i64 index = vm->PyInt_AS_C(args[1]);
|
i64 index = py_cast_v<i64>(vm, args[1]);
|
||||||
return (self+index).get(vm);
|
return (self+index).get(vm);
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_method<2>(type, "__setitem__", [](VM* vm, Args& args) {
|
vm->bind_method<2>(type, "__setitem__", [](VM* vm, Args& args) {
|
||||||
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
||||||
i64 index = vm->PyInt_AS_C(args[1]);
|
i64 index = py_cast_v<i64>(vm, args[1]);
|
||||||
(self+index).set(vm, args[2]);
|
(self+index).set(vm, args[2]);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
@ -164,20 +164,20 @@ struct Pointer{
|
|||||||
|
|
||||||
void set(VM* vm, const PyVar& val){
|
void set(VM* vm, const PyVar& val){
|
||||||
switch(ctype.index){
|
switch(ctype.index){
|
||||||
case C_TYPE("char_"): ref<char>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("char_"): ref<char>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("int_"): ref<int>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("int_"): ref<int>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("float_"): ref<float>() = vm->PyFloat_AS_C(val); break;
|
case C_TYPE("float_"): ref<float>() = vm->PyFloat_AS_C(val); break;
|
||||||
case C_TYPE("double_"): ref<double>() = vm->PyFloat_AS_C(val); break;
|
case C_TYPE("double_"): ref<double>() = vm->PyFloat_AS_C(val); break;
|
||||||
case C_TYPE("bool_"): ref<bool>() = vm->PyBool_AS_C(val); break;
|
case C_TYPE("bool_"): ref<bool>() = vm->PyBool_AS_C(val); break;
|
||||||
case C_TYPE("void_"): vm->ValueError("cannot set void*"); break;
|
case C_TYPE("void_"): vm->ValueError("cannot set void*"); break;
|
||||||
case C_TYPE("int8_"): ref<int8_t>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("int8_"): ref<int8_t>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("int16_"): ref<int16_t>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("int16_"): ref<int16_t>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("int32_"): ref<int32_t>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("int32_"): ref<int32_t>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("int64_"): ref<int64_t>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("int64_"): ref<int64_t>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("uint8_"): ref<uint8_t>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("uint8_"): ref<uint8_t>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("uint16_"): ref<uint16_t>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("uint16_"): ref<uint16_t>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("uint32_"): ref<uint32_t>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("uint32_"): ref<uint32_t>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("uint64_"): ref<uint64_t>() = vm->PyInt_AS_C(val); break;
|
case C_TYPE("uint64_"): ref<uint64_t>() = py_cast_v<i64>(vm, val); break;
|
||||||
case C_TYPE("void_p_"): ref<void*>() = vm->py_cast<Pointer>(val).ptr; break;
|
case C_TYPE("void_p_"): ref<void*>() = vm->py_cast<Pointer>(val).ptr; break;
|
||||||
// use macro here to do extension
|
// use macro here to do extension
|
||||||
default: UNREACHABLE();
|
default: UNREACHABLE();
|
||||||
@ -262,7 +262,7 @@ void add_module_c(VM* vm){
|
|||||||
vm->setattr(mod, "nullptr", vm->new_object<Pointer>(nullptr, C_TYPE_T("void_")));
|
vm->setattr(mod, "nullptr", vm->new_object<Pointer>(nullptr, C_TYPE_T("void_")));
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "malloc", [](VM* vm, Args& args) {
|
vm->bind_func<1>(mod, "malloc", [](VM* vm, Args& args) {
|
||||||
i64 size = vm->PyInt_AS_C(args[0]);
|
i64 size = py_cast_v<i64>(vm, args[0]);
|
||||||
return vm->new_object<Pointer>(malloc(size), C_TYPE_T("void_"));
|
return vm->new_object<Pointer>(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) {
|
vm->bind_func<3>(mod, "memcpy", [](VM* vm, Args& args) {
|
||||||
Pointer& dst = vm->py_cast<Pointer>(args[0]);
|
Pointer& dst = vm->py_cast<Pointer>(args[0]);
|
||||||
Pointer& src = vm->py_cast<Pointer>(args[1]);
|
Pointer& src = vm->py_cast<Pointer>(args[1]);
|
||||||
i64 size = vm->PyInt_AS_C(args[2]);
|
i64 size = py_cast_v<i64>(vm, args[2]);
|
||||||
memcpy(dst.ptr, src.ptr, size);
|
memcpy(dst.ptr, src.ptr, size);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_func<3>(mod, "memset", [](VM* vm, Args& args) {
|
vm->bind_func<3>(mod, "memset", [](VM* vm, Args& args) {
|
||||||
Pointer& dst = vm->py_cast<Pointer>(args[0]);
|
Pointer& dst = vm->py_cast<Pointer>(args[0]);
|
||||||
i64 val = vm->PyInt_AS_C(args[1]);
|
i64 val = py_cast_v<i64>(vm, args[1]);
|
||||||
i64 size = vm->PyInt_AS_C(args[2]);
|
i64 size = py_cast_v<i64>(vm, args[2]);
|
||||||
memset(dst.ptr, (int)val, size);
|
memset(dst.ptr, (int)val, size);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "obj.h"
|
#include "obj.h"
|
||||||
#include "ref.h"
|
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
|
enum NameScope {
|
||||||
|
NAME_LOCAL = 0,
|
||||||
|
NAME_GLOBAL,
|
||||||
|
NAME_ATTR,
|
||||||
|
NAME_SPECIAL,
|
||||||
|
};
|
||||||
|
|
||||||
enum Opcode {
|
enum Opcode {
|
||||||
#define OPCODE(name) OP_##name,
|
#define OPCODE(name) OP_##name,
|
||||||
#include "opcodes.h"
|
#include "opcodes.h"
|
||||||
|
@ -164,14 +164,13 @@ union __8B {
|
|||||||
__8B(f64 val) : _float(val) {}
|
__8B(f64 val) : _float(val) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cast a PyVar to a native type `T` by reference
|
template<typename T>
|
||||||
|
T py_cast_v(VM* vm, const PyVar& var) { UNREACHABLE(); }
|
||||||
|
template<typename T>
|
||||||
|
T _py_cast_v(VM* vm, const PyVar& var) { UNREACHABLE(); }
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T& py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); }
|
T& py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); }
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); }
|
|
||||||
template<typename T>
|
|
||||||
T& _py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); }
|
T& _py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); }
|
||||||
template<typename T>
|
|
||||||
T _py_cast(VM* vm, const PyVar& var) { UNREACHABLE(); }
|
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
@ -90,7 +90,7 @@ void init_builtins(VM* _vm) {
|
|||||||
|
|
||||||
_vm->bind_builtin_func<-1>("exit", [](VM* vm, Args& args) {
|
_vm->bind_builtin_func<-1>("exit", [](VM* vm, Args& args) {
|
||||||
if(args.size() == 0) std::exit(0);
|
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<i64>(vm, args[0]));
|
||||||
else vm->TypeError("exit() takes at most 1 argument");
|
else vm->TypeError("exit() takes at most 1 argument");
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
@ -105,7 +105,7 @@ void init_builtins(VM* _vm) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind_builtin_func<1>("chr", [](VM* vm, Args& args) {
|
_vm->bind_builtin_func<1>("chr", [](VM* vm, Args& args) {
|
||||||
i64 i = vm->PyInt_AS_C(args[0]);
|
i64 i = py_cast_v<i64>(vm, args[0]);
|
||||||
if (i < 0 || i > 128) vm->ValueError("chr() arg not in range(128)");
|
if (i < 0 || i > 128) vm->ValueError("chr() arg not in range(128)");
|
||||||
return vm->PyStr(std::string(1, (char)i));
|
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) {
|
_vm->bind_builtin_func<1>("hex", [](VM* vm, Args& args) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << std::hex << vm->PyInt_AS_C(args[0]);
|
ss << std::hex << py_cast_v<i64>(vm, args[0]);
|
||||||
return vm->PyStr("0x" + ss.str());
|
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) {
|
_vm->bind_static_method<-1>("range", "__new__", [](VM* vm, Args& args) {
|
||||||
Range r;
|
Range r;
|
||||||
switch (args.size()) {
|
switch (args.size()) {
|
||||||
case 1: r.stop = vm->PyInt_AS_C(args[0]); break;
|
case 1: r.stop = py_cast_v<i64>(vm, args[0]); break;
|
||||||
case 2: r.start = vm->PyInt_AS_C(args[0]); r.stop = vm->PyInt_AS_C(args[1]); break;
|
case 2: r.start = py_cast_v<i64>(vm, args[0]); r.stop = py_cast_v<i64>(vm, 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 3: r.start = py_cast_v<i64>(vm, args[0]); r.stop = py_cast_v<i64>(vm, args[1]); r.step = py_cast_v<i64>(vm, args[2]); break;
|
||||||
default: vm->TypeError("expected 1-3 arguments, but got " + std::to_string(args.size()));
|
default: vm->TypeError("expected 1-3 arguments, but got " + std::to_string(args.size()));
|
||||||
}
|
}
|
||||||
return vm->PyRange(r);
|
return vm->PyRange(r);
|
||||||
@ -228,22 +228,22 @@ void init_builtins(VM* _vm) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind_method<1>("int", "__floordiv__", [](VM* vm, Args& args) {
|
_vm->bind_method<1>("int", "__floordiv__", [](VM* vm, Args& args) {
|
||||||
i64 rhs = vm->PyInt_AS_C(args[1]);
|
i64 rhs = py_cast_v<i64>(vm, args[1]);
|
||||||
if(rhs == 0) vm->ZeroDivisionError();
|
if(rhs == 0) vm->ZeroDivisionError();
|
||||||
return py_object(vm, vm->PyInt_AS_C(args[0]) / rhs);
|
return py_object(vm, py_cast_v<i64>(vm, args[0]) / rhs);
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind_method<1>("int", "__mod__", [](VM* vm, Args& args) {
|
_vm->bind_method<1>("int", "__mod__", [](VM* vm, Args& args) {
|
||||||
i64 rhs = vm->PyInt_AS_C(args[1]);
|
i64 rhs = py_cast_v<i64>(vm, args[1]);
|
||||||
if(rhs == 0) vm->ZeroDivisionError();
|
if(rhs == 0) vm->ZeroDivisionError();
|
||||||
return py_object(vm, vm->PyInt_AS_C(args[0]) % rhs);
|
return py_object(vm, py_cast_v<i64>(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", "__repr__", CPP_LAMBDA(vm->PyStr(std::to_string(py_cast_v<i64>(vm, 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", "__json__", CPP_LAMBDA(vm->PyStr(std::to_string(py_cast_v<i64>(vm, args[0])))));
|
||||||
|
|
||||||
#define INT_BITWISE_OP(name,op) \
|
#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<i64>(vm, args[0]) op py_cast_v<i64>(vm, args[1]))));
|
||||||
|
|
||||||
INT_BITWISE_OP(__lshift__, <<)
|
INT_BITWISE_OP(__lshift__, <<)
|
||||||
INT_BITWISE_OP(__rshift__, >>)
|
INT_BITWISE_OP(__rshift__, >>)
|
||||||
@ -255,7 +255,7 @@ void init_builtins(VM* _vm) {
|
|||||||
|
|
||||||
/************ PyFloat ************/
|
/************ PyFloat ************/
|
||||||
_vm->bind_static_method<1>("float", "__new__", [](VM* vm, Args& args) {
|
_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<i64>(vm, args[0]));
|
||||||
if (is_type(args[0], vm->tp_float)) return 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_bool)) return vm->PyFloat(vm->_PyBool_AS_C(args[0]) ? 1.0 : 0.0);
|
||||||
if (is_type(args[0], vm->tp_str)) {
|
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));
|
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<i64>(vm, args[1]);
|
||||||
index = vm->normalized_index(index, self.u8_length());
|
index = vm->normalized_index(index, self.u8_length());
|
||||||
return vm->PyStr(self.u8_getitem(index));
|
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) {
|
_vm->bind_method<1>("list", "__mul__", [](VM* vm, Args& args) {
|
||||||
const List& self = vm->PyList_AS_C(args[0]);
|
const List& self = vm->PyList_AS_C(args[0]);
|
||||||
int n = (int)vm->PyInt_AS_C(args[1]);
|
int n = (int)py_cast_v<i64>(vm, args[1]);
|
||||||
List result;
|
List result;
|
||||||
result.reserve(self.size() * n);
|
result.reserve(self.size() * n);
|
||||||
for(int i = 0; i < n; i++) result.insert(result.end(), self.begin(), self.end());
|
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) {
|
_vm->bind_method<2>("list", "insert", [](VM* vm, Args& args) {
|
||||||
List& _self = vm->PyList_AS_C(args[0]);
|
List& _self = vm->PyList_AS_C(args[0]);
|
||||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
int index = (int)py_cast_v<i64>(vm, args[1]);
|
||||||
if(index < 0) index += _self.size();
|
if(index < 0) index += _self.size();
|
||||||
if(index < 0) index = 0;
|
if(index < 0) index = 0;
|
||||||
if(index > _self.size()) index = _self.size();
|
if(index > _self.size()) index = _self.size();
|
||||||
@ -465,14 +465,14 @@ void init_builtins(VM* _vm) {
|
|||||||
return vm->PyList(std::move(new_list));
|
return vm->PyList(std::move(new_list));
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
int index = (int)py_cast_v<i64>(vm, args[1]);
|
||||||
index = vm->normalized_index(index, self.size());
|
index = vm->normalized_index(index, self.size());
|
||||||
return self[index];
|
return self[index];
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind_method<2>("list", "__setitem__", [](VM* vm, Args& args) {
|
_vm->bind_method<2>("list", "__setitem__", [](VM* vm, Args& args) {
|
||||||
List& self = vm->PyList_AS_C(args[0]);
|
List& self = vm->PyList_AS_C(args[0]);
|
||||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
int index = (int)py_cast_v<i64>(vm, args[1]);
|
||||||
index = vm->normalized_index(index, self.size());
|
index = vm->normalized_index(index, self.size());
|
||||||
self[index] = args[2];
|
self[index] = args[2];
|
||||||
return vm->None;
|
return vm->None;
|
||||||
@ -480,7 +480,7 @@ void init_builtins(VM* _vm) {
|
|||||||
|
|
||||||
_vm->bind_method<1>("list", "__delitem__", [](VM* vm, Args& args) {
|
_vm->bind_method<1>("list", "__delitem__", [](VM* vm, Args& args) {
|
||||||
List& self = vm->PyList_AS_C(args[0]);
|
List& self = vm->PyList_AS_C(args[0]);
|
||||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
int index = (int)py_cast_v<i64>(vm, args[1]);
|
||||||
index = vm->normalized_index(index, self.size());
|
index = vm->normalized_index(index, self.size());
|
||||||
self.erase(self.begin() + index);
|
self.erase(self.begin() + index);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
@ -507,7 +507,7 @@ void init_builtins(VM* _vm) {
|
|||||||
return vm->PyTuple(std::move(new_list));
|
return vm->PyTuple(std::move(new_list));
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
int index = (int)py_cast_v<i64>(vm, args[1]);
|
||||||
index = vm->normalized_index(index, self.size());
|
index = vm->normalized_index(index, self.size());
|
||||||
return self[index];
|
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<0>(mod, "getrecursionlimit", CPP_LAMBDA(py_object(vm, vm->recursionlimit)));
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "setrecursionlimit", [](VM* vm, Args& args) {
|
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<i64>(vm, args[0]);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -698,7 +698,7 @@ struct ReMatch {
|
|||||||
|
|
||||||
vm->bind_method<1>(type, "group", [](VM* vm, Args& args) {
|
vm->bind_method<1>(type, "group", [](VM* vm, Args& args) {
|
||||||
auto& self = vm->py_cast<ReMatch>(args[0]);
|
auto& self = vm->py_cast<ReMatch>(args[0]);
|
||||||
int index = (int)vm->PyInt_AS_C(args[1]);
|
int index = (int)py_cast_v<i64>(vm, args[1]);
|
||||||
index = vm->normalized_index(index, self.m.size());
|
index = vm->normalized_index(index, self.m.size());
|
||||||
return vm->PyStr(self.m[index].str());
|
return vm->PyStr(self.m[index].str());
|
||||||
});
|
});
|
||||||
@ -759,14 +759,14 @@ void add_module_random(VM* vm){
|
|||||||
PyVar mod = vm->new_module("random");
|
PyVar mod = vm->new_module("random");
|
||||||
std::srand(std::time(0));
|
std::srand(std::time(0));
|
||||||
vm->bind_func<1>(mod, "seed", [](VM* vm, Args& args) {
|
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<i64>(vm, args[0]));
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_func<0>(mod, "random", CPP_LAMBDA(vm->PyFloat(std::rand() / (f64)RAND_MAX)));
|
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) {
|
vm->bind_func<2>(mod, "randint", [](VM* vm, Args& args) {
|
||||||
i64 a = vm->PyInt_AS_C(args[0]);
|
i64 a = py_cast_v<i64>(vm, args[0]);
|
||||||
i64 b = vm->PyInt_AS_C(args[1]);
|
i64 b = py_cast_v<i64>(vm, args[1]);
|
||||||
if(a > b) std::swap(a, b);
|
if(a > b) std::swap(a, b);
|
||||||
return py_object(vm, a + std::rand() % (b - a + 1));
|
return py_object(vm, a + std::rand() % (b - a + 1));
|
||||||
});
|
});
|
||||||
|
147
src/ref.h
147
src/ref.h
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "obj.h"
|
#include "obj.h"
|
||||||
|
#include "vm.h"
|
||||||
|
|
||||||
namespace pkpy {
|
namespace pkpy {
|
||||||
|
|
||||||
@ -11,22 +12,61 @@ struct BaseRef {
|
|||||||
virtual ~BaseRef() = default;
|
virtual ~BaseRef() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum NameScope {
|
|
||||||
NAME_LOCAL = 0,
|
|
||||||
NAME_GLOBAL,
|
|
||||||
NAME_ATTR,
|
|
||||||
NAME_SPECIAL,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct NameRef : BaseRef {
|
struct NameRef : BaseRef {
|
||||||
const std::pair<StrName, NameScope> pair;
|
const std::pair<StrName, NameScope> pair;
|
||||||
inline StrName name() const { return pair.first; }
|
inline StrName name() const { return pair.first; }
|
||||||
inline NameScope scope() const { return pair.second; }
|
inline NameScope scope() const { return pair.second; }
|
||||||
NameRef(const std::pair<StrName, NameScope>& pair) : pair(pair) {}
|
NameRef(const std::pair<StrName, NameScope>& pair) : pair(pair) {}
|
||||||
|
|
||||||
PyVar get(VM* vm, Frame* frame) const;
|
PyVar get(VM* vm, Frame* frame) const{
|
||||||
void set(VM* vm, Frame* frame, PyVar val) const;
|
PyVar* val;
|
||||||
void del(VM* vm, Frame* frame) const;
|
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 {
|
struct AttrRef : BaseRef {
|
||||||
@ -34,9 +74,19 @@ struct AttrRef : BaseRef {
|
|||||||
NameRef attr;
|
NameRef attr;
|
||||||
AttrRef(PyVar obj, NameRef attr) : obj(obj), attr(attr) {}
|
AttrRef(PyVar obj, NameRef attr) : obj(obj), attr(attr) {}
|
||||||
|
|
||||||
PyVar get(VM* vm, Frame* frame) const;
|
PyVar get(VM* vm, Frame* frame) const{
|
||||||
void set(VM* vm, Frame* frame, PyVar val) const;
|
return vm->getattr(obj, attr.name());
|
||||||
void del(VM* vm, Frame* frame) const;
|
}
|
||||||
|
|
||||||
|
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 {
|
struct IndexRef : BaseRef {
|
||||||
@ -44,19 +94,78 @@ struct IndexRef : BaseRef {
|
|||||||
PyVar index;
|
PyVar index;
|
||||||
IndexRef(PyVar obj, PyVar index) : obj(obj), index(index) {}
|
IndexRef(PyVar obj, PyVar index) : obj(obj), index(index) {}
|
||||||
|
|
||||||
PyVar get(VM* vm, Frame* frame) const;
|
PyVar get(VM* vm, Frame* frame) const{
|
||||||
void set(VM* vm, Frame* frame, PyVar val) const;
|
return vm->fast_call(__getitem__, two_args(obj, index));
|
||||||
void del(VM* vm, Frame* frame) const;
|
}
|
||||||
|
|
||||||
|
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 {
|
struct TupleRef : BaseRef {
|
||||||
Tuple objs;
|
Tuple objs;
|
||||||
TupleRef(Tuple&& objs) : objs(std::move(objs)) {}
|
TupleRef(Tuple&& objs) : objs(std::move(objs)) {}
|
||||||
|
|
||||||
PyVar get(VM* vm, Frame* frame) const;
|
PyVar get(VM* vm, Frame* frame) const{
|
||||||
void set(VM* vm, Frame* frame, PyVar val) const;
|
Tuple args(objs.size());
|
||||||
void del(VM* vm, Frame* frame) const;
|
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; i<objs.size(); i++){
|
||||||
|
PyVarOrNull x;
|
||||||
|
if(is_type(objs[i], vm->tp_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; i<objs.size(); i++) vm->PyRef_AS_C(objs[i])->del(vm, frame);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename P>
|
||||||
|
PyVarRef VM::PyRef(P&& value) {
|
||||||
|
static_assert(std::is_base_of_v<BaseRef, RAW(P)>);
|
||||||
|
return new_object(tp_ref, std::forward<P>(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
const BaseRef* VM::PyRef_AS_C(const PyVar& obj)
|
||||||
|
{
|
||||||
|
if(!is_type(obj, tp_ref)) TypeError("expected an l-value");
|
||||||
|
return static_cast<const BaseRef*>(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
|
} // namespace pkpy
|
248
src/vm.h
248
src/vm.h
@ -90,19 +90,6 @@ public:
|
|||||||
return call(obj, __repr__);
|
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){
|
PyVar asIter(const PyVar& obj){
|
||||||
if(is_type(obj, tp_native_iterator)) return obj;
|
if(is_type(obj, tp_native_iterator)) return obj;
|
||||||
PyVarOrNull iter_f = getattr(obj, __iter__, false);
|
PyVarOrNull iter_f = getattr(obj, __iter__, false);
|
||||||
@ -441,15 +428,7 @@ public:
|
|||||||
bind_func<ARGC>(builtins, funcName, fn);
|
bind_func<ARGC>(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){
|
int normalized_index(int index, int size){
|
||||||
if(index < 0) index += size;
|
if(index < 0) index += size;
|
||||||
@ -535,18 +514,6 @@ public:
|
|||||||
Type tp_slice, tp_range, tp_module, tp_ref;
|
Type tp_slice, tp_range, tp_module, tp_ref;
|
||||||
Type tp_super, tp_exception, tp_star_wrapper;
|
Type tp_super, tp_exception, tp_star_wrapper;
|
||||||
|
|
||||||
template<typename P>
|
|
||||||
inline PyVarRef PyRef(P&& value) {
|
|
||||||
static_assert(std::is_base_of_v<BaseRef, RAW(P)>);
|
|
||||||
return new_object(tp_ref, std::forward<P>(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const BaseRef* PyRef_AS_C(const PyVar& obj)
|
|
||||||
{
|
|
||||||
if(!is_type(obj, tp_ref)) TypeError("expected an l-value");
|
|
||||||
return static_cast<const BaseRef*>(obj->value());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename P>
|
template<typename P>
|
||||||
inline PyVar PyIter(P&& value) {
|
inline PyVar PyIter(P&& value) {
|
||||||
static_assert(std::is_base_of_v<BaseIter, RAW(P)>);
|
static_assert(std::is_base_of_v<BaseIter, RAW(P)>);
|
||||||
@ -572,11 +539,6 @@ public:
|
|||||||
return new_object(tp_str, value);
|
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){
|
inline i64 _PyInt_AS_C(const PyVar& obj){
|
||||||
return obj.bits >> 2;
|
return obj.bits >> 2;
|
||||||
}
|
}
|
||||||
@ -676,28 +638,6 @@ public:
|
|||||||
for(auto [k, v]: _modules.items()) v->attr()._try_perfect_rehash();
|
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<items.size(); i++) {
|
|
||||||
i64 y = hash(items[i]);
|
|
||||||
x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 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<f64>()(val);
|
|
||||||
}
|
|
||||||
TypeError("unhashable type: " + OBJ_NAME(_t(obj)).escape(true));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/***** Error Reporter *****/
|
/***** Error Reporter *****/
|
||||||
void _error(StrName name, const Str& msg){
|
void _error(StrName name, const Str& msg){
|
||||||
_error(Exception(name, msg));
|
_error(Exception(name, msg));
|
||||||
@ -789,128 +729,15 @@ public:
|
|||||||
CodeObject_ compile(Str source, Str filename, CompileMode mode);
|
CodeObject_ compile(Str source, Str filename, CompileMode mode);
|
||||||
void post_init();
|
void post_init();
|
||||||
PyVar num_negated(const PyVar& obj);
|
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<typename P>
|
||||||
|
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; i<objs.size(); i++){
|
|
||||||
PyVarOrNull x;
|
|
||||||
if(is_type(objs[i], vm->tp_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; i<objs.size(); i++) vm->PyRef_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{
|
PyVar NativeFunc::operator()(VM* vm, Args& args) const{
|
||||||
int args_size = args.size() - (int)method; // remove self
|
int args_size = args.size() - (int)method; // remove self
|
||||||
if(argc != -1 && args_size != argc) {
|
if(argc != -1 && args_size != argc) {
|
||||||
@ -961,11 +788,11 @@ std::enable_if_t<std::is_integral_v<T>, PyVar> py_object(VM* vm, T _val){
|
|||||||
val = (val << 2) | 0b01;
|
val = (val << 2) | 0b01;
|
||||||
return PyVar(reinterpret_cast<int*>(val));
|
return PyVar(reinterpret_cast<int*>(val));
|
||||||
}
|
}
|
||||||
template<> i64 py_cast<i64>(VM* vm, const PyVar& obj){
|
template<> i64 py_cast_v<i64>(VM* vm, const PyVar& obj){
|
||||||
vm->check_type(obj, vm->tp_int);
|
vm->check_type(obj, vm->tp_int);
|
||||||
return obj.bits >> 2;
|
return obj.bits >> 2;
|
||||||
}
|
}
|
||||||
template<> i64 _py_cast<i64>(VM* vm, const PyVar& obj){
|
template<> i64 _py_cast_v<i64>(VM* vm, const PyVar& obj){
|
||||||
return obj.bits >> 2;
|
return obj.bits >> 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -975,13 +802,13 @@ PyVar py_object(VM* vm, f64 val){
|
|||||||
bits |= 0b10;
|
bits |= 0b10;
|
||||||
return PyVar(reinterpret_cast<int*>(bits));
|
return PyVar(reinterpret_cast<int*>(bits));
|
||||||
}
|
}
|
||||||
template<> f64 py_cast<f64>(VM* vm, const PyVar& obj){
|
template<> f64 py_cast_v<f64>(VM* vm, const PyVar& obj){
|
||||||
vm->check_type(obj, vm->tp_float);
|
vm->check_type(obj, vm->tp_float);
|
||||||
i64 bits = obj.bits;
|
i64 bits = obj.bits;
|
||||||
bits = (bits >> 2) << 2;
|
bits = (bits >> 2) << 2;
|
||||||
return __8B(bits)._float;
|
return __8B(bits)._float;
|
||||||
}
|
}
|
||||||
template<> f64 _py_cast<f64>(VM* vm, const PyVar& obj){
|
template<> f64 _py_cast_v<f64>(VM* vm, const PyVar& obj){
|
||||||
i64 bits = obj.bits;
|
i64 bits = obj.bits;
|
||||||
bits = (bits >> 2) << 2;
|
bits = (bits >> 2) << 2;
|
||||||
return __8B(bits)._float;
|
return __8B(bits)._float;
|
||||||
@ -990,11 +817,11 @@ template<> f64 _py_cast<f64>(VM* vm, const PyVar& obj){
|
|||||||
PyVar py_object(VM* vm, bool val){
|
PyVar py_object(VM* vm, bool val){
|
||||||
return val ? vm->True : vm->False;
|
return val ? vm->True : vm->False;
|
||||||
}
|
}
|
||||||
template<> bool py_cast<bool>(VM* vm, const PyVar& obj){
|
template<> bool py_cast_v<bool>(VM* vm, const PyVar& obj){
|
||||||
vm->check_type(obj, vm->tp_bool);
|
vm->check_type(obj, vm->tp_bool);
|
||||||
return obj == vm->True;
|
return obj == vm->True;
|
||||||
}
|
}
|
||||||
template<> bool _py_cast<bool>(VM* vm, const PyVar& obj){
|
template<> bool _py_cast_v<bool>(VM* vm, const PyVar& obj){
|
||||||
return obj == vm->True;
|
return obj == vm->True;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1011,7 +838,7 @@ DEF_NATIVE_2(StarWrapper, tp_star_wrapper)
|
|||||||
|
|
||||||
PyVar VM::num_negated(const PyVar& obj){
|
PyVar VM::num_negated(const PyVar& obj){
|
||||||
if (is_int(obj)){
|
if (is_int(obj)){
|
||||||
return py_object(this, -PyInt_AS_C(obj));
|
return py_object(this, -py_cast_v<i64>(this, obj));
|
||||||
}else if(is_float(obj)){
|
}else if(is_float(obj)){
|
||||||
return py_object(this, -PyFloat_AS_C(obj));
|
return py_object(this, -PyFloat_AS_C(obj));
|
||||||
}
|
}
|
||||||
@ -1019,4 +846,49 @@ PyVar VM::num_negated(const PyVar& obj){
|
|||||||
return nullptr;
|
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<i64>(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<i64>(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<i64>(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<i64>(this, obj);
|
||||||
|
if (is_type(obj, tp_tuple)) {
|
||||||
|
i64 x = 1000003;
|
||||||
|
const Tuple& items = PyTuple_AS_C(obj);
|
||||||
|
for (int i=0; i<items.size(); i++) {
|
||||||
|
i64 y = hash(items[i]);
|
||||||
|
x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 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<f64>()(val);
|
||||||
|
}
|
||||||
|
TypeError("unhashable type: " + OBJ_NAME(_t(obj)).escape(true));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
Loading…
x
Reference in New Issue
Block a user