This commit is contained in:
blueloveTH 2024-06-30 15:32:40 +08:00
parent 391d26cdc5
commit 7ed09f927d
3 changed files with 85 additions and 57 deletions

View File

@ -3,9 +3,7 @@
#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;
@ -23,7 +21,12 @@ 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;
@ -36,9 +39,7 @@ 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;
@ -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){
.type = tp_bool,
.is_ptr = true,
.extra = 1,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0), ._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){
.type = tp_bool,
.is_ptr = true,
.extra = 0,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 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){
.type = tp_none_type,
.is_ptr = true,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 0, 0), ._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){
.type = tp_not_implemented_type,
.is_ptr = true,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 0, 0), ._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){
.type = tp_ellipsis,
.is_ptr = true,
._obj = pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 0, 0), ._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));
@ -132,14 +150,22 @@ void pk_VM__ctor(pk_VM* 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[] = { py_Type public_types[] = {tp_object,
tp_object, tp_type, tp_type,
tp_int, tp_float, tp_bool, tp_str, tp_int,
tp_list, tp_tuple, tp_float,
tp_slice, tp_range, tp_bool,
tp_bytes, tp_dict, tp_property, tp_str,
tp_exception, tp_stop_iteration, tp_syntax_error 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++) { for(int i = 0; i < PK_ARRAY_COUNT(public_types); i++) {
py_Type t = public_types[i]; py_Type t = public_types[i];
@ -154,9 +180,7 @@ void pk_VM__ctor(pk_VM* self){
} }
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
@ -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));

View File

@ -25,7 +25,7 @@ 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_reg(0);
if(py_eval(source, r0)){ if(py_eval(source, r0)){
@ -33,8 +33,8 @@ int main(int argc, char** argv) {
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);