Compare commits

..

No commits in common. "7ed09f927de386361f999a69f7b8606a0fcd7b98" and "4860c08e03682914bd817d5a921de9b6a431a246" have entirely different histories.

11 changed files with 159 additions and 286 deletions

View File

@ -45,25 +45,13 @@ void py_newfloat(py_Ref, double);
void py_newbool(py_Ref, bool); void py_newbool(py_Ref, bool);
void py_newstr(py_Ref, const char*); void py_newstr(py_Ref, const char*);
void py_newstrn(py_Ref, const char*, int); void py_newstrn(py_Ref, const char*, int);
void py_newStr_(py_Ref, py_Str);
// void py_newfstr(py_Ref, const char*, ...); // void py_newfstr(py_Ref, const char*, ...);
void py_newbytes(py_Ref, const unsigned char*, int); void py_newbytes(py_Ref, const unsigned char*, int);
void py_newnone(py_Ref); void py_newnone(py_Ref);
void py_newnull(py_Ref); void py_newnull(py_Ref);
/// Create a tuple with n UNINITIALIZED elements. void py_newtuple(py_Ref, int count);
/// You should initialize all elements before using it.
void py_newtuple(py_Ref, int n);
/// Create a list.
void py_newlist(py_Ref); void py_newlist(py_Ref);
/// Create a list with n UNINITIALIZED elements.
/// You should initialize all elements before using it.
void py_newlistn(py_Ref, int n);
// opaque types
void py_newdict(py_Ref);
void py_newset(py_Ref);
void py_newslice(py_Ref, const py_Ref start, const py_Ref stop, const py_Ref step);
// new style decl-based function // new style decl-based function
void py_newfunction(py_Ref out, py_CFunction, const char* sig); void py_newfunction(py_Ref out, py_CFunction, const char* sig);
@ -90,6 +78,16 @@ void py_newnotimplemented(py_Ref out);
/// @param slots number of slots. Use -1 to create a `__dict__`. /// @param slots number of slots. Use -1 to create a `__dict__`.
/// @param udsize size of your userdata. You can use `py_touserdata()` to get the pointer to it. /// @param udsize size of your userdata. You can use `py_touserdata()` to get the pointer to it.
void py_newobject(py_Ref out, py_Type type, int slots, int udsize); void py_newobject(py_Ref out, py_Type type, int slots, int udsize);
/************* Stack Values Creation *************/
void py_pushint(int64_t);
void py_pushfloat(double);
void py_pushbool(bool);
void py_pushstr(const char*);
void py_pushstrn(const char*, int);
void py_pushnone();
void py_pushnull();
/************* Type Cast *************/ /************* Type Cast *************/
int64_t py_toint(const py_Ref); int64_t py_toint(const py_Ref);
double py_tofloat(const py_Ref); double py_tofloat(const py_Ref);
@ -112,7 +110,8 @@ bool py_istype(const py_Ref, py_Type);
/************* References *************/ /************* References *************/
#define py_arg(i) (py_Ref)((char*)argv+((i)<<4)) #define py_arg(i) (py_Ref)((char*)argv+((i)<<4))
py_Ref py_reg(int i); py_Ref py_getreg(int i);
void py_setreg(int i, const py_Ref val);
py_Ref py_getdict(const py_Ref self, py_Name name); py_Ref py_getdict(const py_Ref self, py_Name name);
void py_setdict(py_Ref self, py_Name name, const py_Ref val); void py_setdict(py_Ref self, py_Name name, const py_Ref val);
@ -132,10 +131,6 @@ bool py_setattr(py_Ref self, py_Name name, const py_Ref val);
/// Deletes the attribute of the object. /// Deletes the attribute of the object.
bool py_delattr(py_Ref self, py_Name name); bool py_delattr(py_Ref self, py_Name name);
bool py_getitem(const py_Ref self, const py_Ref key, py_Ref out);
bool py_setitem(py_Ref self, const py_Ref key, const py_Ref val);
bool py_delitem(py_Ref self, const py_Ref key);
/// Equivalent to `*dst = *src`. /// Equivalent to `*dst = *src`.
void py_assign(py_Ref dst, const py_Ref src); void py_assign(py_Ref dst, const py_Ref src);
@ -169,7 +164,7 @@ py_Ref py_getmodule(const char* name);
int py_import(const char* name, py_Ref out); int py_import(const char* name, py_Ref out);
/************* Errors *************/ /************* Errors *************/
py_Error* py_lasterror(); py_Error* py_getlasterror();
void py_Error__print(py_Error*); void py_Error__print(py_Error*);
/************* Operators *************/ /************* Operators *************/
@ -184,16 +179,9 @@ bool py_repr(const py_Ref, py_Ref out);
/// It consumes `argc + kwargc` arguments from the stack. /// It consumes `argc + kwargc` arguments from the stack.
/// The result will be set to `vm->last_retval`. /// The result will be set to `vm->last_retval`.
int pk_vectorcall(int argc, int kwargc, bool op_call); int pk_vectorcall(int argc, int kwargc, bool op_call);
/// Call a function.
/// It prepares the stack and then performs a `vectorcall(argc, 0, false)`. bool py_call(py_Ref f, ...);
/// The result will be set to `vm->last_retval`. bool py_callmethod(py_Ref self, py_Name name, ...);
bool py_call(py_Ref f, int argc, py_Ref argv);
/// Call a method.
/// It prepares the stack and then performs a `vectorcall(argc+1, 0, false)`.
/// The result will be set to `vm->last_retval`.
bool py_callmethod(py_Ref self, py_Name name, int argc, py_Ref argv);
/// The return value of the most recent vectorcall.
py_Ref py_lastretval();
#define py_isnull(self) ((self)->type == 0) #define py_isnull(self) ((self)->type == 0)
@ -210,9 +198,18 @@ void py_list__setitem(py_Ref self, int i, const py_Ref val);
void py_list__delitem(py_Ref self, int i); void py_list__delitem(py_Ref self, int i);
int py_list__len(const py_Ref self); int py_list__len(const py_Ref self);
void py_list__append(py_Ref self, const py_Ref val); void py_list__append(py_Ref self, const py_Ref val);
void py_list__extend(py_Ref self, const py_Ref begin, const py_Ref end);
void py_list__clear(py_Ref self); void py_list__clear(py_Ref self);
void py_list__insert(py_Ref self, int i, const py_Ref val); void py_list__insert(py_Ref self, int i, const py_Ref val);
// unchecked functions, if self is not a dict, the behavior is undefined
int py_dict__len(const py_Ref self);
bool py_dict__contains(const py_Ref self, const py_Ref key);
py_Ref py_dict__getitem(const py_Ref self, const py_Ref key);
void py_dict__setitem(py_Ref self, const py_Ref key, const py_Ref val);
void py_dict__delitem(py_Ref self, const py_Ref key);
void py_dict__clear(py_Ref self);
// internal functions // internal functions
typedef struct pk_TypeInfo pk_TypeInfo; typedef struct pk_TypeInfo pk_TypeInfo;
pk_TypeInfo* pk_tpinfo(const py_Ref self); pk_TypeInfo* pk_tpinfo(const py_Ref self);

View File

@ -251,13 +251,16 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
if(ti->m__getitem__) { if(ti->m__getitem__) {
if(!ti->m__getitem__(2, SECOND(), SECOND())) goto __ERROR; if(!ti->m__getitem__(2, SECOND(), SECOND())) goto __ERROR;
} else { } else {
// [a, b] -> [?, a, b] if(!py_callmethod(SECOND(), __getitem__, TOP())) goto __ERROR;
PUSH(TOP()); // [a, b, b] // // [a, b] -> [?, a, b]
*SECOND() = *THIRD(); // [a, a, b] // PUSH(TOP()); // [a, b, b]
bool ok = py_getunboundmethod(SECOND(), __getitem__, false, THIRD(), SECOND()); // *SECOND() = *THIRD(); // [a, a, b]
// [__getitem__, self, b] // bool ok = py_getunboundmethod(SECOND(), __getitem__, false, THIRD(),
if(!ok) goto __ERROR; // SECOND()); if(!ok) {
vectorcall_opcall(2); // // __getitem__ not found
// goto __ERROR;
// }
// py_vectorcall(2, 0, );
} }
DISPATCH(); DISPATCH();
} }
@ -384,21 +387,18 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
// [x] // [x]
py_Ref f = py_getdict(&self->builtins, pk_id_long); py_Ref f = py_getdict(&self->builtins, pk_id_long);
assert(f != NULL); assert(f != NULL);
if(!py_call(f, 1, TOP())) goto __ERROR; if(!py_call(f, TOP())) goto __ERROR;
*TOP() = self->last_retval; *TOP() = self->last_retval;
DISPATCH(); DISPATCH();
} }
case OP_BUILD_IMAG: { case OP_BUILD_IMAG: {
// [x] py_Ref _0 = py_getdict(&self->builtins, pk_id_complex);
py_Ref f = py_getdict(&self->builtins, pk_id_complex); assert(_0 != NULL);
assert(f != NULL); py_TValue zero;
py_TValue tmp = *TOP(); py_newint(&zero, 0);
*TOP() = *f; // [complex] if(!py_call(_0, &zero, TOP())) goto __ERROR;
py_newnull(SP()++); // [complex, NULL] *TOP() = self->last_retval;
py_newint(SP()++, 0); // [complex, NULL, 0]
*SP()++ = tmp; // [complex, NULL, 0, x]
vectorcall_opcall(2); // [complex(x, 0)]
DISPATCH(); DISPATCH();
} }
case OP_BUILD_BYTES: { case OP_BUILD_BYTES: {
@ -419,62 +419,46 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
PUSH(&tmp); PUSH(&tmp);
DISPATCH(); DISPATCH();
} }
case OP_BUILD_LIST: { // case OP_BUILD_LIST: {
py_TValue tmp; // PyVar _0 = VAR(STACK_VIEW(byte.arg).to_list());
py_newlistn(&tmp, byte.arg); // STACK_SHRINK(byte.arg);
py_TValue* begin = SP() - byte.arg; // PUSH(_0);
for(int i = 0; i < byte.arg; i++) { // DISPATCH();
py_list__setitem(&tmp, i, begin + i); // }
} // case OP_BUILD_DICT: {
SP() = begin; // if(byte.arg == 0) {
PUSH(&tmp); // PUSH(VAR(Dict()));
DISPATCH(); // DISPATCH()
} // }
case OP_BUILD_DICT: { // PyVar _0 = VAR(STACK_VIEW(byte.arg).to_list());
py_TValue* begin = SP() - byte.arg; // _0 = call(_t(tp_dict), _0);
py_Ref tmp = py_pushtmp(); // STACK_SHRINK(byte.arg);
py_newdict(tmp); // PUSH(_0);
for(int i = 0; i < byte.arg; i += 2) { // DISPATCH();
if(!py_setitem(tmp, begin + i, begin + i + 1)) goto __ERROR; // }
} // case OP_BUILD_SET: {
SP() = begin; // PyVar _0 = VAR(STACK_VIEW(byte.arg).to_list());
PUSH(tmp); // _0 = call(builtins->attr()[pk_id_set], _0);
DISPATCH(); // STACK_SHRINK(byte.arg);
} // PUSH(_0);
case OP_BUILD_SET: { // DISPATCH();
py_TValue* begin = SP() - byte.arg; // }
py_Ref tmp = py_pushtmp(); // case OP_BUILD_SLICE: {
py_newset(tmp); // PyVar _2 = POPX(); // step
for(int i = 0; i < byte.arg; i++) { // PyVar _1 = POPX(); // stop
if(!py_callmethod(tmp, pk_id_add, 1, begin + i)) goto __ERROR; // PyVar _0 = POPX(); // start
} // PUSH(VAR(Slice(_0, _1, _2)));
SP() = begin; // DISPATCH();
PUSH(tmp); // }
DISPATCH(); // case OP_BUILD_STRING: {
} // SStream ss;
case OP_BUILD_SLICE: { // ArgsView view = STACK_VIEW(byte.arg);
// [start, stop, step] // for(PyVar obj: view)
py_TValue tmp; // ss << py_str(obj);
py_newslice(&tmp, THIRD(), SECOND(), TOP()); // STACK_SHRINK(byte.arg);
STACK_SHRINK(3); // PUSH(VAR(ss.str()));
PUSH(&tmp); // DISPATCH();
DISPATCH(); // }
}
case OP_BUILD_STRING: {
py_TValue* begin = SP() - byte.arg;
py_Ref tmp = py_pushtmp();
pk_SStream ss;
pk_SStream__ctor(&ss);
for(int i = 0; i < byte.arg; i++) {
if(!py_str(begin + i, tmp)) goto __ERROR;
py_Str* item = py_touserdata(tmp);
pk_SStream__write_Str(&ss, item);
}
SP() = begin;
py_newStr_(tmp, pk_SStream__submit(&ss));
PUSH(tmp);
DISPATCH();
}
/**************************** */ /**************************** */
case OP_RETURN_VALUE: { case OP_RETURN_VALUE: {
self->last_retval = byte.arg == BC_NOARG ? POPX() : self->None; self->last_retval = byte.arg == BC_NOARG ? POPX() : self->None;

View File

@ -3,9 +3,11 @@
#include "pocketpy/common/sstream.h" #include "pocketpy/common/sstream.h"
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
static unsigned char* pk_default_import_file(const char* path) { return NULL; } static unsigned char* pk_default_import_file(const char* path){
return NULL;
}
static void pk_default_stdout(const char* fmt, ...) { static void pk_default_stdout(const char* fmt, ...){
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vfprintf(stdout, fmt, args); vfprintf(stdout, fmt, args);
@ -13,7 +15,7 @@ static void pk_default_stdout(const char* fmt, ...) {
fflush(stdout); fflush(stdout);
} }
static void pk_default_stderr(const char* fmt, ...) { static void pk_default_stderr(const char* fmt, ...){
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
@ -21,14 +23,9 @@ static void pk_default_stderr(const char* fmt, ...) {
fflush(stderr); fflush(stderr);
} }
void pk_TypeInfo__ctor(pk_TypeInfo* self, void pk_TypeInfo__ctor(pk_TypeInfo *self, py_Name name, py_Type base, PyObject* obj, const py_TValue* module, bool subclass_enabled){
py_Name name,
py_Type base,
PyObject* obj,
const py_TValue* module,
bool subclass_enabled) {
memset(self, 0, sizeof(pk_TypeInfo)); memset(self, 0, sizeof(pk_TypeInfo));
self->name = name; self->name = name;
self->base = base; self->base = base;
@ -39,9 +36,11 @@ void pk_TypeInfo__ctor(pk_TypeInfo* self,
c11_vector__ctor(&self->annotated_fields, sizeof(py_Name)); c11_vector__ctor(&self->annotated_fields, sizeof(py_Name));
} }
void pk_TypeInfo__dtor(pk_TypeInfo* self) { c11_vector__dtor(&self->annotated_fields); } void pk_TypeInfo__dtor(pk_TypeInfo *self){
c11_vector__dtor(&self->annotated_fields);
}
void pk_VM__ctor(pk_VM* self) { void pk_VM__ctor(pk_VM* self){
self->top_frame = NULL; self->top_frame = NULL;
pk_NameDict__ctor(&self->modules); pk_NameDict__ctor(&self->modules);
@ -66,39 +65,26 @@ void pk_VM__ctor(pk_VM* self) {
pk_ManagedHeap__ctor(&self->heap, self); pk_ManagedHeap__ctor(&self->heap, self);
ValueStack__ctor(&self->stack); ValueStack__ctor(&self->stack);
self->True = (py_TValue){ self->True = (py_TValue){.type=tp_bool, .is_ptr=true, .extra=1,
.type = tp_bool, ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0),
.is_ptr = true,
.extra = 1,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0),
}; };
self->False = (py_TValue){ self->False = (py_TValue){.type=tp_bool, .is_ptr=true, .extra=0,
.type = tp_bool, ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0),
.is_ptr = true,
.extra = 0,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0),
}; };
self->None = (py_TValue){ self->None = (py_TValue){.type=tp_none_type, .is_ptr=true,
.type = tp_none_type, ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 0, 0),
.is_ptr = true,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 0, 0),
}; };
self->NotImplemented = (py_TValue){ self->NotImplemented = (py_TValue){.type=tp_not_implemented_type, .is_ptr=true,
.type = tp_not_implemented_type, ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 0, 0),
.is_ptr = true,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 0, 0),
}; };
self->Ellipsis = (py_TValue){ self->Ellipsis = (py_TValue){.type=tp_ellipsis, .is_ptr=true,
.type = tp_ellipsis, ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 0, 0),
.is_ptr = true,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 0, 0),
}; };
/* Init Builtin Types */ /* Init Builtin Types */
// 0: unused // 0: unused
pk_TypeInfo__ctor(c11_vector__emplace(&self->types), 0, 0, NULL, NULL, false); pk_TypeInfo__ctor(c11_vector__emplace(&self->types), 0, 0, NULL, NULL, false);
#define validate(t, expr) \ #define validate(t, expr) if(t != (expr)) abort()
if(t != (expr)) abort()
validate(tp_object, pk_VM__new_type(self, "object", 0, NULL, true)); validate(tp_object, pk_VM__new_type(self, "object", 0, NULL, true));
validate(tp_type, pk_VM__new_type(self, "type", 1, NULL, false)); validate(tp_type, pk_VM__new_type(self, "type", 1, NULL, false));
@ -109,9 +95,6 @@ void pk_VM__ctor(pk_VM* self) {
validate(tp_str, pk_VM__new_type(self, "str", tp_object, NULL, false)); validate(tp_str, pk_VM__new_type(self, "str", tp_object, NULL, false));
validate(tp_list, pk_VM__new_type(self, "list", tp_object, NULL, false)); validate(tp_list, pk_VM__new_type(self, "list", tp_object, NULL, false));
pk_TypeInfo* ti = c11__at(pk_TypeInfo, &self->types, tp_list);
ti->dtor = (void (*)(void*))c11_vector__dtor;
validate(tp_tuple, pk_VM__new_type(self, "tuple", tp_object, NULL, false)); validate(tp_tuple, pk_VM__new_type(self, "tuple", tp_object, NULL, false));
validate(tp_slice, pk_VM__new_type(self, "slice", tp_object, NULL, false)); validate(tp_slice, pk_VM__new_type(self, "slice", tp_object, NULL, false));
@ -135,8 +118,7 @@ void pk_VM__ctor(pk_VM* self) {
validate(tp_classmethod, pk_VM__new_type(self, "classmethod", tp_object, NULL, false)); validate(tp_classmethod, pk_VM__new_type(self, "classmethod", tp_object, NULL, false));
validate(tp_none_type, pk_VM__new_type(self, "NoneType", tp_object, NULL, false)); validate(tp_none_type, pk_VM__new_type(self, "NoneType", tp_object, NULL, false));
validate(tp_not_implemented_type, validate(tp_not_implemented_type, pk_VM__new_type(self, "NotImplementedType", tp_object, NULL, false));
pk_VM__new_type(self, "NotImplementedType", tp_object, NULL, false));
validate(tp_ellipsis, pk_VM__new_type(self, "ellipsis", tp_object, NULL, false)); validate(tp_ellipsis, pk_VM__new_type(self, "ellipsis", tp_object, NULL, false));
validate(tp_op_call, pk_VM__new_type(self, "__op_call", tp_object, NULL, false)); validate(tp_op_call, pk_VM__new_type(self, "__op_call", tp_object, NULL, false));
@ -144,30 +126,22 @@ void pk_VM__ctor(pk_VM* self) {
validate(tp_syntax_error, pk_VM__new_type(self, "SyntaxError", tp_exception, NULL, false)); validate(tp_syntax_error, pk_VM__new_type(self, "SyntaxError", tp_exception, NULL, false));
validate(tp_stop_iteration, pk_VM__new_type(self, "StopIteration", tp_exception, NULL, false)); validate(tp_stop_iteration, pk_VM__new_type(self, "StopIteration", tp_exception, NULL, false));
#undef validate #undef validate
self->StopIteration = c11__at(pk_TypeInfo, &self->types, tp_stop_iteration)->self; self->StopIteration = c11__at(pk_TypeInfo, &self->types, tp_stop_iteration)->self;
self->builtins = *py_newmodule("builtins", NULL); self->builtins = *py_newmodule("builtins", NULL);
/* Setup Public Builtin Types */ /* Setup Public Builtin Types */
py_Type public_types[] = {tp_object, py_Type public_types[] = {
tp_type, tp_object, tp_type,
tp_int, tp_int, tp_float, tp_bool, tp_str,
tp_float, tp_list, tp_tuple,
tp_bool, tp_slice, tp_range,
tp_str, tp_bytes, tp_dict, tp_property,
tp_list, tp_exception, tp_stop_iteration, tp_syntax_error
tp_tuple, };
tp_slice,
tp_range,
tp_bytes,
tp_dict,
tp_property,
tp_exception,
tp_stop_iteration,
tp_syntax_error};
for(int i = 0; i < PK_ARRAY_COUNT(public_types); i++) { for(int i=0; i<PK_ARRAY_COUNT(public_types); i++){
py_Type t = public_types[i]; py_Type t = public_types[i];
pk_TypeInfo* ti = c11__at(pk_TypeInfo, &self->types, t); pk_TypeInfo* ti = c11__at(pk_TypeInfo, &self->types, t);
py_setdict(&self->builtins, ti->name, &ti->self); py_setdict(&self->builtins, ti->name, &ti->self);
@ -179,8 +153,10 @@ void pk_VM__ctor(pk_VM* self) {
self->main = *py_newmodule("__main__", NULL); self->main = *py_newmodule("__main__", NULL);
} }
void pk_VM__dtor(pk_VM* self) { void pk_VM__dtor(pk_VM* self){
if(self->__dynamic_func_decl) { PK_DECREF(self->__dynamic_func_decl); } if(self->__dynamic_func_decl){
PK_DECREF(self->__dynamic_func_decl);
}
// destroy all objects // destroy all objects
pk_ManagedHeap__dtor(&self->heap); pk_ManagedHeap__dtor(&self->heap);
// clear frames // clear frames
@ -190,12 +166,12 @@ void pk_VM__dtor(pk_VM* self) {
ValueStack__clear(&self->stack); ValueStack__clear(&self->stack);
} }
void pk_VM__push_frame(pk_VM* self, Frame* frame) { void pk_VM__push_frame(pk_VM* self, Frame* frame){
frame->f_back = self->top_frame; frame->f_back = self->top_frame;
self->top_frame = frame; self->top_frame = frame;
} }
void pk_VM__pop_frame(pk_VM* self) { void pk_VM__pop_frame(pk_VM* self){
assert(self->top_frame); assert(self->top_frame);
Frame* frame = self->top_frame; Frame* frame = self->top_frame;
// reset stack pointer // reset stack pointer
@ -205,11 +181,7 @@ void pk_VM__pop_frame(pk_VM* self) {
Frame__delete(frame); Frame__delete(frame);
} }
py_Type pk_VM__new_type(pk_VM* self, py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const py_TValue* module, bool subclass_enabled){
const char* name,
py_Type base,
const py_TValue* module,
bool subclass_enabled) {
py_Type type = self->types.count; py_Type type = self->types.count;
pk_TypeInfo* ti = c11_vector__emplace(&self->types); pk_TypeInfo* ti = c11_vector__emplace(&self->types);
PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, -1, sizeof(py_Type)); PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, -1, sizeof(py_Type));
@ -220,18 +192,18 @@ py_Type pk_VM__new_type(pk_VM* self,
} }
/****************************************/ /****************************************/
void PyObject__delete(PyObject* self) { void PyObject__delete(PyObject *self){
pk_TypeInfo* ti = c11__at(pk_TypeInfo, &pk_current_vm->types, self->type); pk_TypeInfo* ti = c11__at(pk_TypeInfo, &pk_current_vm->types, self->type);
if(ti->dtor) ti->dtor(PyObject__value(self)); if(ti->dtor) ti->dtor(PyObject__value(self));
if(self->slots == -1) pk_NameDict__dtor(PyObject__dict(self)); if(self->slots == -1) pk_NameDict__dtor(PyObject__dict(self));
if(self->gc_is_large) { if(self->gc_is_large){
free(self); free(self);
} else { }else{
PoolObject_dealloc(self); PoolObject_dealloc(self);
} }
} }
void pk_ManagedHeap__mark(pk_ManagedHeap* self) { void pk_ManagedHeap__mark(pk_ManagedHeap* self){
// for(int i=0; i<self->no_gc.count; i++){ // for(int i=0; i<self->no_gc.count; i++){
// PyObject* obj = c11__getitem(PyObject*, &self->no_gc, i); // PyObject* obj = c11__getitem(PyObject*, &self->no_gc, i);
// vm->__obj_gc_mark(obj); // vm->__obj_gc_mark(obj);

View File

@ -5,7 +5,7 @@
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
py_Error* py_lasterror(){ py_Error* py_getlasterror(){
return pk_current_vm->last_error; return pk_current_vm->last_error;
} }

View File

@ -1,13 +0,0 @@
#include "pocketpy/pocketpy.h"
#include "pocketpy/common/utils.h"
#include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h"
void py_newdict(py_Ref out){
}
void py_newset(py_Ref out){
}

View File

@ -1,59 +0,0 @@
#include "pocketpy/pocketpy.h"
#include "pocketpy/common/utils.h"
#include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h"
typedef c11_vector List;
void py_newlist(py_Ref out) {
pk_VM* vm = pk_current_vm;
PyObject* obj = pk_ManagedHeap__gcnew(&vm->heap, tp_list, 0, sizeof(List));
List* userdata = PyObject__value(obj);
c11_vector__ctor(userdata, sizeof(py_TValue));
out->type = tp_list;
out->is_ptr = true;
out->_obj = obj;
}
void py_newlistn(py_Ref out, int n) {
py_newlist(out);
List* userdata = py_touserdata(out);
c11_vector__reserve(userdata, n);
userdata->count = n;
}
py_Ref py_list__getitem(const py_Ref self, int i) {
List* userdata = py_touserdata(self);
return c11__at(py_TValue, userdata, i);
}
void py_list__setitem(py_Ref self, int i, const py_Ref val) {
List* userdata = py_touserdata(self);
c11__setitem(py_TValue, userdata, i, *val);
}
void py_list__delitem(py_Ref self, int i) {
List* userdata = py_touserdata(self);
c11_vector__erase(py_TValue, userdata, i);
}
int py_list__len(const py_Ref self) {
List* userdata = py_touserdata(self);
return userdata->count;
}
void py_list__append(py_Ref self, const py_Ref val) {
List* userdata = py_touserdata(self);
c11_vector__push(py_TValue, userdata, *val);
}
void py_list__clear(py_Ref self) {
List* userdata = py_touserdata(self);
c11_vector__clear(userdata);
}
void py_list__insert(py_Ref self, int i, const py_Ref val) {
List* userdata = py_touserdata(self);
c11_vector__insert(py_TValue, userdata, i, *val);
}

View File

@ -12,7 +12,7 @@ bool py_str(const py_Ref val, py_Ref out) { return 0; }
bool py_repr(const py_Ref val, py_Ref out) { bool py_repr(const py_Ref val, py_Ref out) {
const pk_TypeInfo* ti = pk_tpinfo(val); const pk_TypeInfo* ti = pk_tpinfo(val);
if(ti->m__repr__) return ti->m__repr__(1, val, out); if(ti->m__repr__) return ti->m__repr__(1, val, out);
bool ok = py_callmethod(val, __repr__, 0, NULL); bool ok = py_callmethod(val, __repr__);
if(ok) { if(ok) {
*out = pk_current_vm->last_retval; *out = pk_current_vm->last_retval;
return true; return true;
@ -25,9 +25,3 @@ bool py_getattr(const py_Ref self, py_Name name, py_Ref out) { return true; }
bool py_setattr(py_Ref self, py_Name name, const py_Ref val) { return -1; } bool py_setattr(py_Ref self, py_Name name, const py_Ref val) { return -1; }
bool py_delattr(py_Ref self, py_Name name) { return -1; } bool py_delattr(py_Ref self, py_Name name) { return -1; }
bool py_getitem(const py_Ref self, const py_Ref key, py_Ref out) { return -1; }
bool py_setitem(py_Ref self, const py_Ref key, const py_Ref val) { return -1; }
bool py_delitem(py_Ref self, const py_Ref key) { return -1; }

View File

@ -4,10 +4,14 @@
#include "pocketpy/objects/object.h" #include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
py_Ref py_reg(int i){ py_Ref py_getreg(int i){
return pk_current_vm->reg + i; return pk_current_vm->reg + i;
} }
void py_setreg(int i, const py_Ref val){
pk_current_vm->reg[i] = *val;
}
py_Ref py_getdict(const py_Ref self, py_Name name){ py_Ref py_getdict(const py_Ref self, py_Name name){
assert(self && self->is_ptr); assert(self && self->is_ptr);
return pk_NameDict__try_get(PyObject__dict(self->_obj), name); return pk_NameDict__try_get(PyObject__dict(self->_obj), name);

View File

@ -39,16 +39,6 @@ void py_newstrn(py_Ref out, const char* data, int size) {
out->_obj = obj; out->_obj = obj;
} }
void py_newStr_(py_Ref out, py_Str input){
pk_ManagedHeap* heap = &pk_current_vm->heap;
PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, sizeof(py_Str));
py_Str* userdata = PyObject__value(obj);
*userdata = input;
out->type = tp_str;
out->is_ptr = true;
out->_obj = obj;
}
void py_newbytes(py_Ref out, const unsigned char* data, int size) { void py_newbytes(py_Ref out, const unsigned char* data, int size) {
pk_ManagedHeap* heap = &pk_current_vm->heap; pk_ManagedHeap* heap = &pk_current_vm->heap;
// 4 bytes size + data // 4 bytes size + data
@ -95,13 +85,6 @@ void py_newnotimplemented(py_Ref out) {
*out = vm->NotImplemented; *out = vm->NotImplemented;
} }
void py_newslice(py_Ref out, const py_Ref start, const py_Ref stop, const py_Ref step){
py_newobject(out, tp_slice, 3, 0);
py_setslot(out, 0, start);
py_setslot(out, 1, stop);
py_setslot(out, 2, step);
}
void py_newobject(py_Ref out, py_Type type, int slots, int udsize){ void py_newobject(py_Ref out, py_Type type, int slots, int udsize){
pk_ManagedHeap* heap = &pk_current_vm->heap; pk_ManagedHeap* heap = &pk_current_vm->heap;
PyObject* obj = pk_ManagedHeap__gcnew(heap, type, slots, udsize); PyObject* obj = pk_ManagedHeap__gcnew(heap, type, slots, udsize);
@ -109,3 +92,19 @@ void py_newobject(py_Ref out, py_Type type, int slots, int udsize){
out->is_ptr = true; out->is_ptr = true;
out->_obj = obj; out->_obj = obj;
} }
void py_pushint(int64_t val) { py_newint(pk_current_vm->stack.sp++, val); }
void py_pushfloat(double val) { py_newfloat(pk_current_vm->stack.sp++, val); }
void py_pushbool(bool val) { py_newbool(pk_current_vm->stack.sp++, val); }
void py_pushstr(const char* val) { py_newstr(pk_current_vm->stack.sp++, val); }
void py_pushstrn(const char* val, int size) { py_newstrn(pk_current_vm->stack.sp++, val, size); }
void py_pushnone() { py_newnone(pk_current_vm->stack.sp++); }
void py_pushnull() { py_newnull(pk_current_vm->stack.sp++); }

View File

@ -47,11 +47,11 @@ int py_eval(const char* source, py_Ref out) {
PK_UNREACHABLE(); PK_UNREACHABLE();
} }
bool py_call(py_Ref f, int argc, py_Ref argv){ bool py_call(py_Ref callable, ...){
return -1; return -1;
} }
bool py_callmethod(py_Ref self, py_Name name, int argc, py_Ref argv){ bool py_callmethod(py_Ref self, py_Name name, ...){
return -1; return -1;
} }
@ -59,10 +59,6 @@ int pk_vectorcall(int argc, int kwargc, bool op_call){
return -1; return -1;
} }
py_Ref py_lastretval(){
return &pk_current_vm->last_retval;
}
bool py_getunboundmethod(const py_Ref self, py_Name name, bool fallback, py_Ref out, py_Ref out_self){ bool py_getunboundmethod(const py_Ref self, py_Name name, bool fallback, py_Ref out, py_Ref out_self){
return -1; return -1;
} }
@ -70,5 +66,4 @@ bool py_getunboundmethod(const py_Ref self, py_Name name, bool fallback, py_Ref
pk_TypeInfo* pk_tpinfo(const py_Ref self){ pk_TypeInfo* pk_tpinfo(const py_Ref self){
pk_VM* vm = pk_current_vm; pk_VM* vm = pk_current_vm;
return c11__at(pk_TypeInfo, &vm->types, self->type); return c11__at(pk_TypeInfo, &vm->types, self->type);
} }

View File

@ -25,16 +25,16 @@ int main(int argc, char** argv) {
#endif #endif
py_initialize(); py_initialize();
const char* source = "[1, 'a']"; const char* source = "1, 'a'";
py_Ref r0 = py_reg(0); py_Ref r0 = py_getreg(0);
if(py_eval(source, r0)){ if(py_eval(source, r0)){
py_Error* err = py_lasterror(); py_Error* err = py_getlasterror();
py_Error__print(err); py_Error__print(err);
}else{ }else{
// handle the result // handle the result
py_Ref _0 = py_list__getitem(r0, 0); py_Ref _0 = py_tuple__getitem(r0, 0);
py_Ref _1 = py_list__getitem(r0, 1); py_Ref _1 = py_tuple__getitem(r0, 1);
int _L0 = py_toint(_0); int _L0 = py_toint(_0);
const char* _L1 = py_tostr(_1); const char* _L1 = py_tostr(_1);
printf("%d, %s\n", _L0, _L1); printf("%d, %s\n", _L0, _L1);