This commit is contained in:
blueloveTH 2024-06-26 03:23:46 +08:00
parent fb0ec57f38
commit 7e35fa2d56
8 changed files with 43 additions and 15 deletions

View File

@ -4,8 +4,9 @@ python prebuild.py
SRC=$(find src/ -name "*.c") SRC=$(find src/ -name "*.c")
FLAGS="-std=c11 -lm -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1" # -fsanitize=address,leak,undefined" FLAGS="-std=c11 -lm -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1"
SANITIZE_FLAGS="-fsanitize=address,leak,undefined"
echo "Compiling C files..." echo "Compiling C files..."
clang $FLAGS $SRC src2/main.c -o main clang $FLAGS $SANITIZE_FLAGS $SRC src2/main.c -o main

View File

@ -9,7 +9,7 @@ extern "C" {
typedef uint16_t StrName; typedef uint16_t StrName;
#define py_name(name) pk_StrName__map(#name) #define py_name(name) pk_StrName__map(name)
uint16_t pk_StrName__map(const char*); uint16_t pk_StrName__map(const char*);
uint16_t pk_StrName__map2(c11_string); uint16_t pk_StrName__map2(c11_string);

View File

@ -124,6 +124,7 @@ void py_import(const char* name, py_Ref out);
py_Error* py_getlasterror(); py_Error* py_getlasterror();
void py_Error__print(py_Error*); void py_Error__print(py_Error*);
/************* Operators *************/
int py_eq(const py_Ref, const py_Ref); int py_eq(const py_Ref, const py_Ref);
int py_le(const py_Ref, const py_Ref); int py_le(const py_Ref, const py_Ref);
int py_hash(const py_Ref, int64_t* out); int py_hash(const py_Ref, int64_t* out);

View File

@ -74,13 +74,15 @@ void METHOD(delete)(NAME* self) {
void METHOD(set)(NAME* self, K key, V value) { void METHOD(set)(NAME* self, K key, V value) {
int index; int index;
c11__lower_bound(KV, self->data, self->count, key, partial_less, &index); c11__lower_bound(KV, self->data, self->count, key, partial_less, &index);
if(index != self->count){
KV* it = c11__at(KV, self, index); KV* it = c11__at(KV, self, index);
if(index != self->count && equal(it->key, key)) { if(equal(it->key, key)){
it->value = value; it->value = value;
} else { return;
}
}
KV kv = {key, value}; KV kv = {key, value};
c11_vector__insert(KV, self, index, kv); c11_vector__insert(KV, self, index, kv);
}
} }
V* METHOD(try_get)(const NAME* self, K key) { V* METHOD(try_get)(const NAME* self, K key) {
@ -113,11 +115,13 @@ bool METHOD(contains)(const NAME* self, K key) {
bool METHOD(del)(NAME* self, K key) { bool METHOD(del)(NAME* self, K key) {
int index; int index;
c11__lower_bound(KV, self->data, self->count, key, partial_less, &index); c11__lower_bound(KV, self->data, self->count, key, partial_less, &index);
if(index != self->count){
KV* it = c11__at(KV, self, index); KV* it = c11__at(KV, self, index);
if(index != self->count && equal(it->key, key)) { if(equal(it->key, key)){
c11_vector__erase(KV, self, index); c11_vector__erase(KV, self, index);
return true; return true;
} }
}
return false; return false;
} }

View File

@ -115,5 +115,13 @@ PyObject* PyObject__new(py_Type type, int slots, int size){
self->type = type; self->type = type;
self->gc_marked = false; self->gc_marked = false;
self->slots = slots; self->slots = slots;
// initialize slots or dict
void* p = (char*)self + 8;
if(slots >= 0){
memset(p, 0, slots*sizeof(PyVar));
}else{
pk_NameDict__ctor(p);
}
return self; return self;
} }

14
src/interpreter/py_ops.c Normal file
View File

@ -0,0 +1,14 @@
#include "pocketpy/interpreter/vm.h"
#include "pocketpy/pocketpy.h"
int py_eq(const py_Ref lhs, const py_Ref rhs){
return 0;
}
int py_le(const py_Ref lhs, const py_Ref rhs){
return 0;
}
int py_hash(const py_Ref self, int64_t* out){
return 0;
}

View File

@ -176,7 +176,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self){
py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const PyVar* module, bool subclass_enabled){ py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const PyVar* 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, 0, sizeof(py_Type)); PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, -1, sizeof(py_Type));
py_Type* value = PyObject__value(typeobj); py_Type* value = PyObject__value(typeobj);
*value = type; *value = type;
pk_TypeInfo__ctor(ti, py_name(name), base, typeobj, module, subclass_enabled); pk_TypeInfo__ctor(ti, py_name(name), base, typeobj, module, subclass_enabled);

View File

@ -24,7 +24,7 @@ void py_newbool(py_Ref self, bool val) {
void py_newstr(py_Ref self, const char* data) { void py_newstr(py_Ref self, const char* data) {
pk_ManagedHeap* heap = &pk_current_vm->heap; pk_ManagedHeap* heap = &pk_current_vm->heap;
PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, sizeof(py_Str)); PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, sizeof(py_Str));
py_Str__ctor((py_Str*)PyObject__value(obj), data); py_Str__ctor(PyObject__value(obj), data);
self->type = tp_str; self->type = tp_str;
self->is_ptr = true; self->is_ptr = true;
self->_obj = obj; self->_obj = obj;