This commit is contained in:
blueloveTH 2024-07-03 12:53:54 +08:00
parent 841cc25a4e
commit 6ac3fdccdb
5 changed files with 69 additions and 50 deletions

View File

@ -11,7 +11,7 @@
extern "C" {
#endif
typedef struct c11_array{
typedef struct c11_array {
void* data;
int count;
int elem_size;
@ -21,7 +21,7 @@ void c11_array__ctor(c11_array* self, int elem_size, int count);
void c11_array__dtor(c11_array* self);
c11_array c11_array__copy(const c11_array* self);
typedef struct c11_vector{
typedef struct c11_vector {
void* data;
int count;
int capacity;
@ -41,53 +41,56 @@ c11_array c11_vector__submit(c11_vector* self);
#define c11__at(T, self, index) ((T*)(self)->data + index)
#define c11_vector__push(T, self, elem) \
do{ \
if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity*2); \
do { \
if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \
((T*)(self)->data)[(self)->count] = (elem); \
(self)->count++; \
}while(0)
} while(0)
#define c11_vector__pop(self) (--(self)->count)
#define c11_vector__back(T, self) \
(((T*)(self)->data)[(self)->count - 1])
#define c11_vector__back(T, self) (((T*)(self)->data)[(self)->count - 1])
#define c11_vector__extend(T, self, p, size) \
do{ \
do { \
c11_vector__reserve((self), (self)->count + (size)); \
memcpy((T*)(self)->data + (self)->count, (p), (size) * sizeof(T)); \
(self)->count += (size); \
}while(0)
} while(0)
#define c11_vector__insert(T, self, index, elem) \
do{ \
if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity*2); \
do { \
if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \
T* p = (T*)(self)->data + (index); \
memmove(p + 1, p, ((self)->count - (index)) * sizeof(T)); \
*p = (elem); \
(self)->count++; \
}while(0)
} while(0)
#define c11_vector__erase(T, self, index) \
do{ \
do { \
T* p = (T*)(self)->data + (index); \
memmove(p, p + 1, ((self)->count - (index) - 1) * sizeof(T)); \
memmove(p, p + 1, ((self)->count - (index)-1) * sizeof(T)); \
(self)->count--; \
}while(0)
} while(0)
#define c11_vector__reverse(T, self, start, end) \
do{ \
do { \
T* p = (T*)(self)->data + (start); \
T* q = (T*)(self)->data + (end); \
while(p < q){ \
T tmp = *p; *p = *q; *q = tmp; \
p++; q--; \
while(p < q) { \
T tmp = *p; \
*p = *q; \
*q = tmp; \
p++; \
q--; \
} \
}while(0)
} while(0)
// NOTE: here we do an extra NULL check for it to avoid UB
#define c11__foreach(T, self, it) \
for(T* it = (T*)(self)->data; it != (T*)(self)->data + (self)->count; it++)
for(T* it = (self)->data; it && it != (T*)(self)->data + (self)->count; it++)
#ifdef __cplusplus
}

View File

@ -48,8 +48,8 @@ c11_vector c11_vector__copy(const c11_vector* self){
void c11_vector__reserve(c11_vector* self, int capacity){
if(capacity < 4) capacity = 4;
if(capacity <= self->capacity) return;
self->data = realloc(self->data, self->elem_size * capacity);
self->capacity = capacity;
self->data = realloc(self->data, self->elem_size * self->capacity);
}
void c11_vector__clear(c11_vector* self){

View File

@ -3,6 +3,7 @@
#include "pocketpy/common/sstream.h"
#include "pocketpy/objects/codeobject.h"
#include "pocketpy/pocketpy.h"
#include <stdbool.h>
int UnboundLocalError(py_Name name) { return -1; }
@ -604,6 +605,18 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
// DISPATCH_JUMP_ABSOLUTE(target)
// }
/*****************************************/
case OP_FSTRING_EVAL: {
assert(false);
}
case OP_REPR: {
assert(false);
}
case OP_CALL: {
assert(false);
}
case OP_CALL_VARGS: {
assert(false);
}
case OP_RETURN_VALUE: {
if(byte.arg == BC_NOARG) {
self->last_retval = POPX();

View File

@ -98,11 +98,13 @@ static bool _py_number__pow__(int argc, py_Ref argv) {
py_newfloat(py_retval(), pow(lhs, rhs));
}
} else {
// rhs >= 0
int64_t ret = 1;
while(rhs) {
while(true){
if(rhs & 1) ret *= lhs;
lhs *= lhs;
rhs >>= 1;
if(!rhs) break;
lhs *= lhs; // place this here to avoid overflow
}
py_newint(py_retval(), ret);
}

View File

@ -67,12 +67,13 @@ static void disassemble(CodeObject* co) {
}
char buf[32];
snprintf(buf, sizeof(buf), "%-8s%-3s%-3d", line, pointer, i);
snprintf(buf, sizeof(buf), "%-8s%-3s%-3d ", line, pointer, i);
c11_sbuf__write_cstr(&ss, buf);
snprintf(buf, sizeof(buf), " %-24s", OP_NAMES[byte.op]);
c11_sbuf__write_cstr(&ss, buf);
c11_sbuf__write_cstr(&ss, OP_NAMES[byte.op]);
c11_sbuf__write_char(&ss, ex.is_virtual ? '*' : ' ');
int padding = 24 - strlen(OP_NAMES[byte.op]);
for(int j = 0; j < padding; j++) c11_sbuf__write_char(&ss, ' ');
// _opcode_argstr(this, i, byte, co);
do {