Compare commits

...

4 Commits

Author SHA1 Message Date
blueloveTH
7ed09f927d try list 2024-06-30 15:32:40 +08:00
blueloveTH
391d26cdc5 some fix 2024-06-30 15:26:39 +08:00
blueloveTH
68e8536c89 some fix 2024-06-30 14:34:23 +08:00
blueloveTH
a6b3163635 some fix 2024-06-30 14:27:13 +08:00
11 changed files with 287 additions and 160 deletions

View File

@ -45,13 +45,25 @@ 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);
void py_newtuple(py_Ref, int count); /// Create a tuple with n UNINITIALIZED elements.
/// 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);
@ -78,16 +90,6 @@ 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);
@ -110,8 +112,7 @@ 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_getreg(int i); py_Ref py_reg(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);
@ -131,6 +132,10 @@ 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);
@ -164,7 +169,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_getlasterror(); py_Error* py_lasterror();
void py_Error__print(py_Error*); void py_Error__print(py_Error*);
/************* Operators *************/ /************* Operators *************/
@ -179,9 +184,16 @@ 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.
bool py_call(py_Ref f, ...); /// It prepares the stack and then performs a `vectorcall(argc, 0, false)`.
bool py_callmethod(py_Ref self, py_Name name, ...); /// The result will be set to `vm->last_retval`.
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)
@ -198,18 +210,9 @@ 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,16 +251,13 @@ 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 {
if(!py_callmethod(SECOND(), __getitem__, TOP())) goto __ERROR; // [a, b] -> [?, a, b]
// // [a, b] -> [?, a, b] PUSH(TOP()); // [a, b, b]
// PUSH(TOP()); // [a, b, b] *SECOND() = *THIRD(); // [a, a, b]
// *SECOND() = *THIRD(); // [a, a, b] bool ok = py_getunboundmethod(SECOND(), __getitem__, false, THIRD(), SECOND());
// bool ok = py_getunboundmethod(SECOND(), __getitem__, false, THIRD(), // [__getitem__, self, b]
// SECOND()); if(!ok) { if(!ok) goto __ERROR;
// // __getitem__ not found vectorcall_opcall(2);
// goto __ERROR;
// }
// py_vectorcall(2, 0, );
} }
DISPATCH(); DISPATCH();
} }
@ -387,18 +384,21 @@ 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, TOP())) goto __ERROR; if(!py_call(f, 1, TOP())) goto __ERROR;
*TOP() = self->last_retval; *TOP() = self->last_retval;
DISPATCH(); DISPATCH();
} }
case OP_BUILD_IMAG: { case OP_BUILD_IMAG: {
py_Ref _0 = py_getdict(&self->builtins, pk_id_complex); // [x]
assert(_0 != NULL); py_Ref f = py_getdict(&self->builtins, pk_id_complex);
py_TValue zero; assert(f != NULL);
py_newint(&zero, 0); py_TValue tmp = *TOP();
if(!py_call(_0, &zero, TOP())) goto __ERROR; *TOP() = *f; // [complex]
*TOP() = self->last_retval; py_newnull(SP()++); // [complex, NULL]
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,46 +419,62 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
PUSH(&tmp); PUSH(&tmp);
DISPATCH(); DISPATCH();
} }
// case OP_BUILD_LIST: { case OP_BUILD_LIST: {
// PyVar _0 = VAR(STACK_VIEW(byte.arg).to_list()); py_TValue tmp;
// STACK_SHRINK(byte.arg); py_newlistn(&tmp, byte.arg);
// PUSH(_0); py_TValue* begin = SP() - byte.arg;
// DISPATCH(); for(int i = 0; i < byte.arg; i++) {
// } py_list__setitem(&tmp, i, begin + i);
// case OP_BUILD_DICT: { }
// if(byte.arg == 0) { SP() = begin;
// PUSH(VAR(Dict())); PUSH(&tmp);
// DISPATCH() DISPATCH();
// } }
// PyVar _0 = VAR(STACK_VIEW(byte.arg).to_list()); case OP_BUILD_DICT: {
// _0 = call(_t(tp_dict), _0); py_TValue* begin = SP() - byte.arg;
// STACK_SHRINK(byte.arg); py_Ref tmp = py_pushtmp();
// PUSH(_0); py_newdict(tmp);
// DISPATCH(); for(int i = 0; i < byte.arg; i += 2) {
// } if(!py_setitem(tmp, begin + i, begin + i + 1)) goto __ERROR;
// case OP_BUILD_SET: { }
// PyVar _0 = VAR(STACK_VIEW(byte.arg).to_list()); SP() = begin;
// _0 = call(builtins->attr()[pk_id_set], _0); PUSH(tmp);
// STACK_SHRINK(byte.arg); DISPATCH();
// PUSH(_0); }
// DISPATCH(); case OP_BUILD_SET: {
// } py_TValue* begin = SP() - byte.arg;
// case OP_BUILD_SLICE: { py_Ref tmp = py_pushtmp();
// PyVar _2 = POPX(); // step py_newset(tmp);
// PyVar _1 = POPX(); // stop for(int i = 0; i < byte.arg; i++) {
// PyVar _0 = POPX(); // start if(!py_callmethod(tmp, pk_id_add, 1, begin + i)) goto __ERROR;
// PUSH(VAR(Slice(_0, _1, _2))); }
// DISPATCH(); SP() = begin;
// } PUSH(tmp);
// case OP_BUILD_STRING: { DISPATCH();
// SStream ss; }
// ArgsView view = STACK_VIEW(byte.arg); case OP_BUILD_SLICE: {
// for(PyVar obj: view) // [start, stop, step]
// ss << py_str(obj); py_TValue tmp;
// STACK_SHRINK(byte.arg); py_newslice(&tmp, THIRD(), SECOND(), TOP());
// PUSH(VAR(ss.str())); STACK_SHRINK(3);
// DISPATCH(); PUSH(&tmp);
// } 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,11 +3,9 @@
#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){ static unsigned char* pk_default_import_file(const char* path) { return NULL; }
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);
@ -15,7 +13,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);
@ -23,9 +21,14 @@ static void pk_default_stderr(const char* fmt, ...){
fflush(stderr); fflush(stderr);
} }
void pk_TypeInfo__ctor(pk_TypeInfo *self, py_Name name, py_Type base, PyObject* obj, const py_TValue* module, bool subclass_enabled){ void pk_TypeInfo__ctor(pk_TypeInfo* self,
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;
@ -36,11 +39,9 @@ void pk_TypeInfo__ctor(pk_TypeInfo *self, py_Name name, py_Type base, PyObject*
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){ void pk_TypeInfo__dtor(pk_TypeInfo* self) { c11_vector__dtor(&self->annotated_fields); }
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);
@ -65,26 +66,39 @@ 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){.type=tp_bool, .is_ptr=true, .extra=1, self->True = (py_TValue){
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0), .type = tp_bool,
.is_ptr = true,
.extra = 1,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0),
}; };
self->False = (py_TValue){.type=tp_bool, .is_ptr=true, .extra=0, self->False = (py_TValue){
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0), .type = tp_bool,
.is_ptr = true,
.extra = 0,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0),
}; };
self->None = (py_TValue){.type=tp_none_type, .is_ptr=true, self->None = (py_TValue){
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 0, 0), .type = tp_none_type,
.is_ptr = true,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 0, 0),
}; };
self->NotImplemented = (py_TValue){.type=tp_not_implemented_type, .is_ptr=true, self->NotImplemented = (py_TValue){
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 0, 0), .type = tp_not_implemented_type,
.is_ptr = true,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 0, 0),
}; };
self->Ellipsis = (py_TValue){.type=tp_ellipsis, .is_ptr=true, self->Ellipsis = (py_TValue){
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 0, 0), .type = tp_ellipsis,
.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) if(t != (expr)) abort() #define validate(t, expr) \
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));
@ -95,6 +109,9 @@ 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));
@ -118,7 +135,8 @@ 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, pk_VM__new_type(self, "NotImplementedType", tp_object, NULL, false)); validate(tp_not_implemented_type,
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));
@ -126,22 +144,30 @@ 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 */
py_Type public_types[] = {
tp_object, tp_type,
tp_int, tp_float, tp_bool, tp_str,
tp_list, 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++){ /* Setup Public Builtin Types */
py_Type public_types[] = {tp_object,
tp_type,
tp_int,
tp_float,
tp_bool,
tp_str,
tp_list,
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++) {
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);
@ -153,10 +179,8 @@ 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){ if(self->__dynamic_func_decl) { PK_DECREF(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
@ -166,12 +190,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
@ -181,7 +205,11 @@ void pk_VM__pop_frame(pk_VM* self){
Frame__delete(frame); Frame__delete(frame);
} }
py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const py_TValue* module, bool subclass_enabled){ py_Type pk_VM__new_type(pk_VM* self,
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));
@ -192,18 +220,18 @@ py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const py_TV
} }
/****************************************/ /****************************************/
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_getlasterror(){ py_Error* py_lasterror(){
return pk_current_vm->last_error; return pk_current_vm->last_error;
} }

13
src/public/py_dict.c Normal file
View File

@ -0,0 +1,13 @@
#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){
}

59
src/public/py_list.c Normal file
View File

@ -0,0 +1,59 @@
#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__); bool ok = py_callmethod(val, __repr__, 0, NULL);
if(ok) { if(ok) {
*out = pk_current_vm->last_retval; *out = pk_current_vm->last_retval;
return true; return true;
@ -25,3 +25,9 @@ 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,14 +4,10 @@
#include "pocketpy/objects/object.h" #include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
py_Ref py_getreg(int i){ py_Ref py_reg(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,6 +39,16 @@ 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
@ -85,6 +95,13 @@ 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);
@ -92,19 +109,3 @@ 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 callable, ...){ bool py_call(py_Ref f, int argc, py_Ref argv){
return -1; return -1;
} }
bool py_callmethod(py_Ref self, py_Name name, ...){ bool py_callmethod(py_Ref self, py_Name name, int argc, py_Ref argv){
return -1; return -1;
} }
@ -59,6 +59,10 @@ 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;
} }
@ -66,4 +70,5 @@ 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_getreg(0); py_Ref r0 = py_reg(0);
if(py_eval(source, r0)){ if(py_eval(source, r0)){
py_Error* err = py_getlasterror(); py_Error* err = py_lasterror();
py_Error__print(err); py_Error__print(err);
}else{ }else{
// handle the result // handle the result
py_Ref _0 = py_tuple__getitem(r0, 0); py_Ref _0 = py_list__getitem(r0, 0);
py_Ref _1 = py_tuple__getitem(r0, 1); py_Ref _1 = py_list__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);