mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
improve code
This commit is contained in:
parent
f9320f8a3e
commit
29cd6fe59b
@ -55,3 +55,9 @@
|
|||||||
#else
|
#else
|
||||||
#define PK_PLATFORM_SEP '/'
|
#define PK_PLATFORM_SEP '/'
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#ifndef restrict
|
||||||
|
#define restrict
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
@ -47,7 +47,7 @@ typedef py_TValue* py_GlobalRef;
|
|||||||
typedef py_TValue* py_StackRef;
|
typedef py_TValue* py_StackRef;
|
||||||
/// An item reference to a container object. It invalidates when the container is modified.
|
/// An item reference to a container object. It invalidates when the container is modified.
|
||||||
typedef py_TValue* py_ItemRef;
|
typedef py_TValue* py_ItemRef;
|
||||||
/// An output reference for returning a value.
|
/// An output reference for returning a value. Only use this for function arguments.
|
||||||
typedef py_TValue* py_OutRef;
|
typedef py_TValue* py_OutRef;
|
||||||
|
|
||||||
typedef struct py_Frame py_Frame;
|
typedef struct py_Frame py_Frame;
|
||||||
|
@ -192,7 +192,7 @@ const char* py_Frame_sourceloc(py_Frame* self, int* lineno) {
|
|||||||
return loc.src->filename->data;
|
return loc.src->filename->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_Frame_newglobals(py_Frame* frame, py_Ref out) {
|
void py_Frame_newglobals(py_Frame* frame, py_OutRef out) {
|
||||||
if(!frame) {
|
if(!frame) {
|
||||||
pk_mappingproxy__namedict(out, &pk_current_vm->main);
|
pk_mappingproxy__namedict(out, &pk_current_vm->main);
|
||||||
return;
|
return;
|
||||||
@ -204,7 +204,7 @@ void py_Frame_newglobals(py_Frame* frame, py_Ref out) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_Frame_newlocals(py_Frame* frame, py_Ref out) {
|
void py_Frame_newlocals(py_Frame* frame, py_OutRef out) {
|
||||||
if(!frame) {
|
if(!frame) {
|
||||||
py_newdict(out);
|
py_newdict(out);
|
||||||
return;
|
return;
|
||||||
|
@ -580,7 +580,7 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
|
|||||||
}
|
}
|
||||||
case PKL_BUILD_LIST: {
|
case PKL_BUILD_LIST: {
|
||||||
int length = pkl__read_int(&p);
|
int length = pkl__read_int(&p);
|
||||||
py_OutRef val = py_retval();
|
py_Ref val = py_retval();
|
||||||
py_newlistn(val, length);
|
py_newlistn(val, length);
|
||||||
for(int i = length - 1; i >= 0; i--) {
|
for(int i = length - 1; i >= 0; i--) {
|
||||||
py_StackRef item = py_peek(-1);
|
py_StackRef item = py_peek(-1);
|
||||||
@ -592,7 +592,7 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
|
|||||||
}
|
}
|
||||||
case PKL_BUILD_TUPLE: {
|
case PKL_BUILD_TUPLE: {
|
||||||
int length = pkl__read_int(&p);
|
int length = pkl__read_int(&p);
|
||||||
py_OutRef val = py_retval();
|
py_Ref val = py_retval();
|
||||||
py_Ref p = py_newtuple(val, length);
|
py_Ref p = py_newtuple(val, length);
|
||||||
for(int i = length - 1; i >= 0; i--) {
|
for(int i = length - 1; i >= 0; i--) {
|
||||||
p[i] = *py_peek(-1);
|
p[i] = *py_peek(-1);
|
||||||
@ -603,7 +603,7 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
|
|||||||
}
|
}
|
||||||
case PKL_BUILD_DICT: {
|
case PKL_BUILD_DICT: {
|
||||||
int length = pkl__read_int(&p);
|
int length = pkl__read_int(&p);
|
||||||
py_OutRef val = py_pushtmp();
|
py_Ref val = py_pushtmp();
|
||||||
py_newdict(val);
|
py_newdict(val);
|
||||||
py_StackRef begin = py_peek(-1) - 2 * length;
|
py_StackRef begin = py_peek(-1) - 2 * length;
|
||||||
py_StackRef end = py_peek(-1);
|
py_StackRef end = py_peek(-1);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include "pocketpy/common/sstream.h"
|
#include "pocketpy/common/sstream.h"
|
||||||
#include "pocketpy/common/utils.h"
|
#include "pocketpy/common/utils.h"
|
||||||
#include "pocketpy/interpreter/vm.h"
|
#include "pocketpy/interpreter/vm.h"
|
||||||
#include "pocketpy/objects/object.h"
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
static bool isclose(float a, float b) { return fabs(a - b) < 1e-4; }
|
static bool isclose(float a, float b) { return fabs(a - b) < 1e-4; }
|
||||||
@ -544,7 +543,7 @@ static bool mat3x3__eq__(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
DEFINE_BOOL_NE(mat3x3, mat3x3__eq__)
|
DEFINE_BOOL_NE(mat3x3, mat3x3__eq__)
|
||||||
|
|
||||||
static void matmul(const c11_mat3x3* lhs, const c11_mat3x3* rhs, c11_mat3x3* out) {
|
static void matmul(const c11_mat3x3* lhs, const c11_mat3x3* rhs, c11_mat3x3* restrict out) {
|
||||||
out->_11 = lhs->_11 * rhs->_11 + lhs->_12 * rhs->_21 + lhs->_13 * rhs->_31;
|
out->_11 = lhs->_11 * rhs->_11 + lhs->_12 * rhs->_21 + lhs->_13 * rhs->_31;
|
||||||
out->_12 = lhs->_11 * rhs->_12 + lhs->_12 * rhs->_22 + lhs->_13 * rhs->_32;
|
out->_12 = lhs->_11 * rhs->_12 + lhs->_12 * rhs->_22 + lhs->_13 * rhs->_32;
|
||||||
out->_13 = lhs->_11 * rhs->_13 + lhs->_12 * rhs->_23 + lhs->_13 * rhs->_33;
|
out->_13 = lhs->_11 * rhs->_13 + lhs->_12 * rhs->_23 + lhs->_13 * rhs->_33;
|
||||||
@ -562,7 +561,7 @@ static float determinant(const c11_mat3x3* m) {
|
|||||||
m->_13 * (m->_21 * m->_32 - m->_22 * m->_31);
|
m->_13 * (m->_21 * m->_32 - m->_22 * m->_31);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool inverse(const c11_mat3x3* m, c11_mat3x3* out) {
|
static bool inverse(const c11_mat3x3* m, c11_mat3x3* restrict out) {
|
||||||
float det = determinant(m);
|
float det = determinant(m);
|
||||||
if(isclose(det, 0)) return false;
|
if(isclose(det, 0)) return false;
|
||||||
float invdet = 1.0f / det;
|
float invdet = 1.0f / det;
|
||||||
@ -578,7 +577,7 @@ static bool inverse(const c11_mat3x3* m, c11_mat3x3* out) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void trs(c11_vec2 t, float r, c11_vec2 s, c11_mat3x3* out) {
|
static void trs(c11_vec2 t, float r, c11_vec2 s, c11_mat3x3* restrict out) {
|
||||||
float cr = cosf(r);
|
float cr = cosf(r);
|
||||||
float sr = sinf(r);
|
float sr = sinf(r);
|
||||||
// clang-format off
|
// clang-format off
|
||||||
|
@ -500,12 +500,12 @@ static bool builtins_locals(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newglobals(py_Ref out) {
|
void py_newglobals(py_OutRef out) {
|
||||||
py_Frame* frame = pk_current_vm->top_frame;
|
py_Frame* frame = pk_current_vm->top_frame;
|
||||||
py_Frame_newglobals(frame, out);
|
py_Frame_newglobals(frame, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newlocals(py_Ref out) {
|
void py_newlocals(py_OutRef out) {
|
||||||
py_Frame* frame = pk_current_vm->top_frame;
|
py_Frame* frame = pk_current_vm->top_frame;
|
||||||
py_Frame_newlocals(frame, out);
|
py_Frame_newlocals(frame, out);
|
||||||
}
|
}
|
||||||
|
@ -266,7 +266,7 @@ static bool dict__new__(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newdict(py_Ref out) {
|
void py_newdict(py_OutRef out) {
|
||||||
Dict* ud = py_newobject(out, tp_dict, 0, sizeof(Dict));
|
Dict* ud = py_newobject(out, tp_dict, 0, sizeof(Dict));
|
||||||
Dict__ctor(ud, 7, 8);
|
Dict__ctor(ud, 7, 8);
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,15 @@
|
|||||||
#include "pocketpy/pocketpy.h"
|
#include "pocketpy/pocketpy.h"
|
||||||
|
|
||||||
#include "pocketpy/common/utils.h"
|
#include "pocketpy/common/utils.h"
|
||||||
#include "pocketpy/objects/object.h"
|
|
||||||
#include "pocketpy/interpreter/vm.h"
|
#include "pocketpy/interpreter/vm.h"
|
||||||
#include "pocketpy/common/sstream.h"
|
#include "pocketpy/common/sstream.h"
|
||||||
|
|
||||||
void py_newlist(py_Ref out) {
|
void py_newlist(py_OutRef out) {
|
||||||
List* ud = py_newobject(out, tp_list, 0, sizeof(List));
|
List* ud = py_newobject(out, tp_list, 0, sizeof(List));
|
||||||
c11_vector__ctor(ud, sizeof(py_TValue));
|
c11_vector__ctor(ud, sizeof(py_TValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newlistn(py_Ref out, int n) {
|
void py_newlistn(py_OutRef out, int n) {
|
||||||
py_newlist(out);
|
py_newlist(out);
|
||||||
List* ud = py_touserdata(out);
|
List* ud = py_touserdata(out);
|
||||||
c11_vector__reserve(ud, n);
|
c11_vector__reserve(ud, n);
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
#include "pocketpy/pocketpy.h"
|
#include "pocketpy/pocketpy.h"
|
||||||
|
|
||||||
#include "pocketpy/common/utils.h"
|
|
||||||
#include "pocketpy/common/sstream.h"
|
#include "pocketpy/common/sstream.h"
|
||||||
#include "pocketpy/objects/object.h"
|
#include "pocketpy/objects/object.h"
|
||||||
#include "pocketpy/interpreter/vm.h"
|
#include "pocketpy/interpreter/vm.h"
|
||||||
|
|
||||||
void py_newslice(py_Ref out) {
|
void py_newslice(py_OutRef out) {
|
||||||
VM* vm = pk_current_vm;
|
VM* vm = pk_current_vm;
|
||||||
PyObject* obj = ManagedHeap__gcnew(&vm->heap, tp_slice, 3, 0);
|
PyObject* obj = ManagedHeap__gcnew(&vm->heap, tp_slice, 3, 0);
|
||||||
out->type = tp_slice;
|
out->type = tp_slice;
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
#include "pocketpy/interpreter/vm.h"
|
#include "pocketpy/interpreter/vm.h"
|
||||||
#include "pocketpy/common/sstream.h"
|
#include "pocketpy/common/sstream.h"
|
||||||
|
|
||||||
void py_newstr(py_Ref out, const char* data) { py_newstrv(out, (c11_sv){data, strlen(data)}); }
|
void py_newstr(py_OutRef out, const char* data) { py_newstrv(out, (c11_sv){data, strlen(data)}); }
|
||||||
|
|
||||||
char* py_newstrn(py_Ref out, int size) {
|
char* py_newstrn(py_OutRef out, int size) {
|
||||||
if(size < 8) {
|
if(size < 8) {
|
||||||
out->type = tp_str;
|
out->type = tp_str;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
@ -42,7 +42,7 @@ void py_newfstr(py_OutRef out, const char* fmt, ...) {
|
|||||||
c11_sbuf__py_submit(&buf, out);
|
c11_sbuf__py_submit(&buf, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* py_newbytes(py_Ref out, int size) {
|
unsigned char* py_newbytes(py_OutRef out, int size) {
|
||||||
ManagedHeap* heap = &pk_current_vm->heap;
|
ManagedHeap* heap = &pk_current_vm->heap;
|
||||||
// 4 bytes size + data
|
// 4 bytes size + data
|
||||||
PyObject* obj = ManagedHeap__gcnew(heap, tp_bytes, 0, sizeof(c11_bytes) + size);
|
PyObject* obj = ManagedHeap__gcnew(heap, tp_bytes, 0, sizeof(c11_bytes) + size);
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
#include "pocketpy/objects/object.h"
|
#include "pocketpy/objects/object.h"
|
||||||
#include "pocketpy/interpreter/vm.h"
|
#include "pocketpy/interpreter/vm.h"
|
||||||
|
|
||||||
py_ObjectRef py_newtuple(py_Ref out, int n) {
|
py_ObjectRef py_newtuple(py_OutRef out, int n) {
|
||||||
VM* vm = pk_current_vm;
|
VM* vm = pk_current_vm;
|
||||||
PyObject* obj = ManagedHeap__gcnew(&vm->heap, tp_tuple, n, 0);
|
PyObject* obj = ManagedHeap__gcnew(&vm->heap, tp_tuple, n, 0);
|
||||||
out->type = tp_tuple;
|
out->type = tp_tuple;
|
||||||
|
@ -7,42 +7,42 @@
|
|||||||
#include "pocketpy/interpreter/vm.h"
|
#include "pocketpy/interpreter/vm.h"
|
||||||
#include "pocketpy/compiler/compiler.h"
|
#include "pocketpy/compiler/compiler.h"
|
||||||
|
|
||||||
void py_newint(py_Ref out, int64_t val) {
|
void py_newint(py_OutRef out, py_i64 val) {
|
||||||
out->type = tp_int;
|
out->type = tp_int;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
out->_i64 = val;
|
out->_i64 = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newfloat(py_Ref out, double val) {
|
void py_newfloat(py_OutRef out, py_f64 val) {
|
||||||
out->type = tp_float;
|
out->type = tp_float;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
out->_f64 = val;
|
out->_f64 = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newbool(py_Ref out, bool val) {
|
void py_newbool(py_OutRef out, bool val) {
|
||||||
out->type = tp_bool;
|
out->type = tp_bool;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
out->_bool = val;
|
out->_bool = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newnone(py_Ref out) {
|
void py_newnone(py_OutRef out) {
|
||||||
out->type = tp_NoneType;
|
out->type = tp_NoneType;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newnotimplemented(py_Ref out) {
|
void py_newnotimplemented(py_OutRef out) {
|
||||||
out->type = tp_NotImplementedType;
|
out->type = tp_NotImplementedType;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newellipsis(py_Ref out) {
|
void py_newellipsis(py_OutRef out) {
|
||||||
out->type = tp_ellipsis;
|
out->type = tp_ellipsis;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newnil(py_Ref out) { out->type = 0; }
|
void py_newnil(py_OutRef out) { out->type = 0; }
|
||||||
|
|
||||||
void py_newnativefunc(py_Ref out, py_CFunction f) {
|
void py_newnativefunc(py_OutRef out, py_CFunction f) {
|
||||||
out->type = tp_nativefunc;
|
out->type = tp_nativefunc;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
out->_cfunc = f;
|
out->_cfunc = f;
|
||||||
@ -90,7 +90,7 @@ void py_bind(py_Ref obj, const char* sig, py_CFunction f) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
py_Name
|
py_Name
|
||||||
py_newfunction(py_Ref out, const char* sig, py_CFunction f, const char* docstring, int slots) {
|
py_newfunction(py_OutRef out, const char* sig, py_CFunction f, const char* docstring, int slots) {
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
snprintf(buffer, sizeof(buffer), "def %s: pass", sig);
|
snprintf(buffer, sizeof(buffer), "def %s: pass", sig);
|
||||||
// fn(a, b, *c, d=1) -> None
|
// fn(a, b, *c, d=1) -> None
|
||||||
@ -118,13 +118,13 @@ py_Name
|
|||||||
return decl_name;
|
return decl_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newboundmethod(py_Ref out, py_Ref self, py_Ref func) {
|
void py_newboundmethod(py_OutRef out, py_Ref self, py_Ref func) {
|
||||||
py_newobject(out, tp_boundmethod, 2, 0);
|
py_newobject(out, tp_boundmethod, 2, 0);
|
||||||
py_setslot(out, 0, self);
|
py_setslot(out, 0, self);
|
||||||
py_setslot(out, 1, func);
|
py_setslot(out, 1, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* py_newobject(py_Ref out, py_Type type, int slots, int udsize) {
|
void* py_newobject(py_OutRef out, py_Type type, int slots, int udsize) {
|
||||||
ManagedHeap* heap = &pk_current_vm->heap;
|
ManagedHeap* heap = &pk_current_vm->heap;
|
||||||
PyObject* obj = ManagedHeap__gcnew(heap, type, slots, udsize);
|
PyObject* obj = ManagedHeap__gcnew(heap, type, slots, udsize);
|
||||||
out->type = type;
|
out->type = type;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user