mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 12:30:19 +00:00
...
This commit is contained in:
parent
976f809f80
commit
92090aeaa1
@ -9,17 +9,17 @@
|
|||||||
|
|
||||||
typedef struct c11_array {
|
typedef struct c11_array {
|
||||||
void* data;
|
void* data;
|
||||||
int count;
|
int length;
|
||||||
int elem_size;
|
int elem_size;
|
||||||
} c11_array;
|
} c11_array;
|
||||||
|
|
||||||
void c11_array__ctor(c11_array* self, int elem_size, int count);
|
void c11_array__ctor(c11_array* self, int elem_size, int length);
|
||||||
void c11_array__dtor(c11_array* self);
|
void c11_array__dtor(c11_array* self);
|
||||||
c11_array c11_array__copy(const c11_array* self);
|
c11_array c11_array__copy(const c11_array* self);
|
||||||
|
|
||||||
typedef struct c11_vector {
|
typedef struct c11_vector {
|
||||||
void* data;
|
void* data;
|
||||||
int count;
|
int length;
|
||||||
int capacity;
|
int capacity;
|
||||||
int elem_size;
|
int elem_size;
|
||||||
} c11_vector;
|
} c11_vector;
|
||||||
@ -39,43 +39,43 @@ c11_array c11_vector__submit(c11_vector* self);
|
|||||||
|
|
||||||
#define c11_vector__push(T, self, elem) \
|
#define c11_vector__push(T, self, elem) \
|
||||||
do { \
|
do { \
|
||||||
if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \
|
if((self)->length == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \
|
||||||
((T*)(self)->data)[(self)->count] = (elem); \
|
((T*)(self)->data)[(self)->length] = (elem); \
|
||||||
(self)->count++; \
|
(self)->length++; \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
#define c11_vector__pop(self) (--(self)->count)
|
#define c11_vector__pop(self) (--(self)->length)
|
||||||
|
|
||||||
#define c11_vector__back(T, self) (((T*)(self)->data)[(self)->count - 1])
|
#define c11_vector__back(T, self) (((T*)(self)->data)[(self)->length - 1])
|
||||||
|
|
||||||
#define c11_vector__extend(T, self, p, size) \
|
#define c11_vector__extend(T, self, p, size) \
|
||||||
do { \
|
do { \
|
||||||
c11_vector__reserve((self), (self)->count + (size)); \
|
c11_vector__reserve((self), (self)->length + (size)); \
|
||||||
memcpy((T*)(self)->data + (self)->count, (p), (size) * sizeof(T)); \
|
memcpy((T*)(self)->data + (self)->length, (p), (size) * sizeof(T)); \
|
||||||
(self)->count += (size); \
|
(self)->length += (size); \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
#define c11_vector__insert(T, self, index, elem) \
|
#define c11_vector__insert(T, self, index, elem) \
|
||||||
do { \
|
do { \
|
||||||
if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \
|
if((self)->length == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \
|
||||||
T* p = (T*)(self)->data + (index); \
|
T* p = (T*)(self)->data + (index); \
|
||||||
memmove(p + 1, p, ((self)->count - (index)) * sizeof(T)); \
|
memmove(p + 1, p, ((self)->length - (index)) * sizeof(T)); \
|
||||||
*p = (elem); \
|
*p = (elem); \
|
||||||
(self)->count++; \
|
(self)->length++; \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
#define c11_vector__erase(T, self, index) \
|
#define c11_vector__erase(T, self, index) \
|
||||||
do { \
|
do { \
|
||||||
T* p = (T*)(self)->data + (index); \
|
T* p = (T*)(self)->data + (index); \
|
||||||
memmove(p, p + 1, ((self)->count - (index)-1) * sizeof(T)); \
|
memmove(p, p + 1, ((self)->length - (index)-1) * sizeof(T)); \
|
||||||
(self)->count--; \
|
(self)->length--; \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
#define c11__reverse(T, self) \
|
#define c11__reverse(T, self) \
|
||||||
do { \
|
do { \
|
||||||
if(!self->data) break; \
|
if(!self->data) break; \
|
||||||
T* p = (T*)(self)->data; \
|
T* p = (T*)(self)->data; \
|
||||||
T* q = (T*)(self)->data + (self)->count - 1; \
|
T* q = (T*)(self)->data + (self)->length - 1; \
|
||||||
while(p < q) { \
|
while(p < q) { \
|
||||||
T tmp = *p; \
|
T tmp = *p; \
|
||||||
*p = *q; \
|
*p = *q; \
|
||||||
@ -87,4 +87,4 @@ c11_array c11_vector__submit(c11_vector* self);
|
|||||||
|
|
||||||
// NOTE: here we do an extra NULL check for it to avoid UB
|
// NOTE: here we do an extra NULL check for it to avoid UB
|
||||||
#define c11__foreach(T, self, it) \
|
#define c11__foreach(T, self, it) \
|
||||||
for(T* it = (self)->data; it && it != (T*)(self)->data + (self)->count; it++)
|
for(T* it = (self)->data; it && it != (T*)(self)->data + (self)->length; it++)
|
||||||
|
@ -1,27 +1,27 @@
|
|||||||
#if !defined(SMALLMAP_T__HEADER) && !defined(SMALLMAP_T__SOURCE)
|
#if !defined(SMALLMAP_T__HEADER) && !defined(SMALLMAP_T__SOURCE)
|
||||||
#include "pocketpy/common/vector.h"
|
#include "pocketpy/common/vector.h"
|
||||||
|
|
||||||
#define SMALLMAP_T__HEADER
|
#define SMALLMAP_T__HEADER
|
||||||
#define SMALLMAP_T__SOURCE
|
#define SMALLMAP_T__SOURCE
|
||||||
/* Input */
|
/* Input */
|
||||||
#define K int
|
#define K int
|
||||||
#define V float
|
#define V float
|
||||||
#define NAME c11_smallmap_i2f
|
#define NAME c11_smallmap_i2f
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Optional Input */
|
/* Optional Input */
|
||||||
#ifndef less
|
#ifndef less
|
||||||
#define less(a, b) ((a) < (b))
|
#define less(a, b) ((a) < (b))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef equal
|
#ifndef equal
|
||||||
#define equal(a, b) ((a) == (b))
|
#define equal(a, b) ((a) == (b))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Temprary macros */
|
/* Temprary macros */
|
||||||
#define partial_less(a, b) less((a).key, (b))
|
#define partial_less(a, b) less((a).key, (b))
|
||||||
#define CONCAT(A, B) CONCAT_(A, B)
|
#define CONCAT(A, B) CONCAT_(A, B)
|
||||||
#define CONCAT_(A, B) A##B
|
#define CONCAT_(A, B) A##B
|
||||||
|
|
||||||
#define KV CONCAT(NAME, _KV)
|
#define KV CONCAT(NAME, _KV)
|
||||||
#define METHOD(name) CONCAT(NAME, CONCAT(__, name))
|
#define METHOD(name) CONCAT(NAME, CONCAT(__, name))
|
||||||
@ -56,9 +56,7 @@ void METHOD(ctor)(NAME* self) {
|
|||||||
c11_vector__reserve(self, 4);
|
c11_vector__reserve(self, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
void METHOD(dtor)(NAME* self) {
|
void METHOD(dtor)(NAME* self) { c11_vector__dtor(self); }
|
||||||
c11_vector__dtor(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
NAME* METHOD(new)() {
|
NAME* METHOD(new)() {
|
||||||
NAME* self = malloc(sizeof(NAME));
|
NAME* self = malloc(sizeof(NAME));
|
||||||
@ -73,10 +71,10 @@ 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->length, key, partial_less, &index);
|
||||||
if(index != self->count){
|
if(index != self->length) {
|
||||||
KV* it = c11__at(KV, self, index);
|
KV* it = c11__at(KV, self, index);
|
||||||
if(equal(it->key, key)){
|
if(equal(it->key, key)) {
|
||||||
it->value = value;
|
it->value = value;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -87,8 +85,8 @@ void METHOD(set)(NAME* self, K key, V value) {
|
|||||||
|
|
||||||
V* METHOD(try_get)(const NAME* self, K key) {
|
V* METHOD(try_get)(const 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->length, key, partial_less, &index);
|
||||||
if(index != self->count){
|
if(index != self->length) {
|
||||||
KV* it = c11__at(KV, self, index);
|
KV* it = c11__at(KV, self, index);
|
||||||
if(equal(it->key, key)) return &it->value;
|
if(equal(it->key, key)) return &it->value;
|
||||||
}
|
}
|
||||||
@ -100,16 +98,14 @@ V METHOD(get)(const NAME* self, K key, V default_value) {
|
|||||||
return p ? *p : default_value;
|
return p ? *p : default_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool METHOD(contains)(const NAME* self, K key) {
|
bool METHOD(contains)(const NAME* self, K key) { return METHOD(try_get)(self, key) != NULL; }
|
||||||
return METHOD(try_get)(self, key) != NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
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->length, key, partial_less, &index);
|
||||||
if(index != self->count){
|
if(index != self->length) {
|
||||||
KV* it = c11__at(KV, self, index);
|
KV* it = c11__at(KV, self, index);
|
||||||
if(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;
|
||||||
}
|
}
|
||||||
@ -117,9 +113,7 @@ bool METHOD(del)(NAME* self, K key) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void METHOD(clear)(NAME* self) {
|
void METHOD(clear)(NAME* self) { c11_vector__clear(self); }
|
||||||
c11_vector__clear(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -13,13 +13,13 @@ typedef struct LinkedListNode {
|
|||||||
} LinkedListNode;
|
} LinkedListNode;
|
||||||
|
|
||||||
typedef struct LinkedList {
|
typedef struct LinkedList {
|
||||||
int count;
|
int length;
|
||||||
LinkedListNode head;
|
LinkedListNode head;
|
||||||
LinkedListNode tail;
|
LinkedListNode tail;
|
||||||
} LinkedList;
|
} LinkedList;
|
||||||
|
|
||||||
static void LinkedList__ctor(LinkedList* self) {
|
static void LinkedList__ctor(LinkedList* self) {
|
||||||
self->count = 0;
|
self->length = 0;
|
||||||
self->head.prev = NULL;
|
self->head.prev = NULL;
|
||||||
self->head.next = &self->tail;
|
self->head.next = &self->tail;
|
||||||
self->tail.prev = &self->head;
|
self->tail.prev = &self->head;
|
||||||
@ -31,7 +31,7 @@ static void LinkedList__push_back(LinkedList* self, LinkedListNode* node) {
|
|||||||
node->next = &self->tail;
|
node->next = &self->tail;
|
||||||
self->tail.prev->next = node;
|
self->tail.prev->next = node;
|
||||||
self->tail.prev = node;
|
self->tail.prev = node;
|
||||||
self->count++;
|
self->length++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LinkedList__push_front(LinkedList* self, LinkedListNode* node) {
|
static void LinkedList__push_front(LinkedList* self, LinkedListNode* node) {
|
||||||
@ -39,25 +39,25 @@ static void LinkedList__push_front(LinkedList* self, LinkedListNode* node) {
|
|||||||
node->next = self->head.next;
|
node->next = self->head.next;
|
||||||
self->head.next->prev = node;
|
self->head.next->prev = node;
|
||||||
self->head.next = node;
|
self->head.next = node;
|
||||||
self->count++;
|
self->length++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LinkedList__pop_back(LinkedList* self) {
|
static void LinkedList__pop_back(LinkedList* self) {
|
||||||
assert(self->count > 0);
|
assert(self->length > 0);
|
||||||
self->tail.prev->prev->next = &self->tail;
|
self->tail.prev->prev->next = &self->tail;
|
||||||
self->tail.prev = self->tail.prev->prev;
|
self->tail.prev = self->tail.prev->prev;
|
||||||
self->count--;
|
self->length--;
|
||||||
}
|
}
|
||||||
|
|
||||||
static LinkedListNode* LinkedList__back(LinkedList* self) {
|
static LinkedListNode* LinkedList__back(LinkedList* self) {
|
||||||
assert(self->count > 0);
|
assert(self->length > 0);
|
||||||
return self->tail.prev;
|
return self->tail.prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LinkedList__erase(LinkedList* self, LinkedListNode* node) {
|
static void LinkedList__erase(LinkedList* self, LinkedListNode* node) {
|
||||||
node->prev->next = node->next;
|
node->prev->next = node->next;
|
||||||
node->next->prev = node->prev;
|
node->next->prev = node->prev;
|
||||||
self->count--;
|
self->length--;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define LinkedList__apply(self, __STATEMENTS__) \
|
#define LinkedList__apply(self, __STATEMENTS__) \
|
||||||
@ -135,7 +135,7 @@ static void MemoryPool__ctor(MemoryPool* self) {
|
|||||||
|
|
||||||
static void* MemoryPool__alloc(MemoryPool* self) {
|
static void* MemoryPool__alloc(MemoryPool* self) {
|
||||||
MemoryPoolArena* arena;
|
MemoryPoolArena* arena;
|
||||||
if(self->_arenas.count == 0){
|
if(self->_arenas.length == 0){
|
||||||
arena = malloc(sizeof(MemoryPoolArena));
|
arena = malloc(sizeof(MemoryPoolArena));
|
||||||
MemoryPoolArena__ctor(arena);
|
MemoryPoolArena__ctor(arena);
|
||||||
LinkedList__push_back(&self->_arenas, (LinkedListNode*)arena);
|
LinkedList__push_back(&self->_arenas, (LinkedListNode*)arena);
|
||||||
@ -164,7 +164,7 @@ static void MemoryPool__dealloc(MemoryPool* self, void* p) {
|
|||||||
|
|
||||||
static void MemoryPool__shrink_to_fit(MemoryPool* self) {
|
static void MemoryPool__shrink_to_fit(MemoryPool* self) {
|
||||||
const int MIN_ARENA_COUNT = PK_GC_MIN_THRESHOLD * 100 / (kPoolObjectArenaSize);
|
const int MIN_ARENA_COUNT = PK_GC_MIN_THRESHOLD * 100 / (kPoolObjectArenaSize);
|
||||||
if(self->_arenas.count < MIN_ARENA_COUNT) return;
|
if(self->_arenas.length < MIN_ARENA_COUNT) return;
|
||||||
LinkedList__apply(&self->_arenas,
|
LinkedList__apply(&self->_arenas,
|
||||||
MemoryPoolArena* arena = (MemoryPoolArena*)node;
|
MemoryPoolArena* arena = (MemoryPoolArena*)node;
|
||||||
if(MemoryPoolArena__full(arena)) {
|
if(MemoryPoolArena__full(arena)) {
|
||||||
@ -301,8 +301,8 @@ void Pools_debug_info(char* buffer, int size) {
|
|||||||
);
|
);
|
||||||
buffer += n; size -= n;
|
buffer += n; size -= n;
|
||||||
// PoolObject
|
// PoolObject
|
||||||
int empty_arenas = PoolObject._empty_arenas.count;
|
int empty_arenas = PoolObject._empty_arenas.length;
|
||||||
int arenas = PoolObject._arenas.count;
|
int arenas = PoolObject._arenas.length;
|
||||||
// print empty arenas count
|
// print empty arenas count
|
||||||
n = snprintf(
|
n = snprintf(
|
||||||
buffer, size, "PoolObject: %d empty arenas, %d arenas\n",
|
buffer, size, "PoolObject: %d empty arenas, %d arenas\n",
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
void c11_sbuf__ctor(c11_sbuf* self) {
|
void c11_sbuf__ctor(c11_sbuf* self) {
|
||||||
c11_vector__ctor(&self->data, sizeof(char));
|
c11_vector__ctor(&self->data, sizeof(char));
|
||||||
c11_vector__reserve(&self->data, sizeof(c11_string) + 100);
|
c11_vector__reserve(&self->data, sizeof(c11_string) + 100);
|
||||||
self->data.count = sizeof(c11_string);
|
self->data.length = sizeof(c11_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11_sbuf__dtor(c11_sbuf* self) { c11_vector__dtor(&self->data); }
|
void c11_sbuf__dtor(c11_sbuf* self) { c11_vector__dtor(&self->data); }
|
||||||
@ -28,18 +28,18 @@ void c11_sbuf__write_pad(c11_sbuf* self, int count, char pad) {
|
|||||||
|
|
||||||
void c11_sbuf__write_int(c11_sbuf* self, int i) {
|
void c11_sbuf__write_int(c11_sbuf* self, int i) {
|
||||||
// len('-2147483648') == 11
|
// len('-2147483648') == 11
|
||||||
c11_vector__reserve(&self->data, self->data.count + 11 + 1);
|
c11_vector__reserve(&self->data, self->data.length + 11 + 1);
|
||||||
char* p = (char*)self->data.data + self->data.count;
|
char* p = (char*)self->data.data + self->data.length;
|
||||||
int n = snprintf(p, 11 + 1, "%d", i);
|
int n = snprintf(p, 11 + 1, "%d", i);
|
||||||
self->data.count += n;
|
self->data.length += n;
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11_sbuf__write_i64(c11_sbuf* self, int64_t val) {
|
void c11_sbuf__write_i64(c11_sbuf* self, int64_t val) {
|
||||||
// len('-9223372036854775808') == 20
|
// len('-9223372036854775808') == 20
|
||||||
c11_vector__reserve(&self->data, self->data.count + 20 + 1);
|
c11_vector__reserve(&self->data, self->data.length + 20 + 1);
|
||||||
char* p = (char*)self->data.data + self->data.count;
|
char* p = (char*)self->data.data + self->data.length;
|
||||||
int n = snprintf(p, 20 + 1, "%lld", (long long)val);
|
int n = snprintf(p, 20 + 1, "%lld", (long long)val);
|
||||||
self->data.count += n;
|
self->data.length += n;
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11_sbuf__write_f64(c11_sbuf* self, double val, int precision) {
|
void c11_sbuf__write_f64(c11_sbuf* self, double val, int precision) {
|
||||||
@ -143,7 +143,7 @@ c11_string* c11_sbuf__submit(c11_sbuf* self) {
|
|||||||
c11_vector__push(char, &self->data, '\0');
|
c11_vector__push(char, &self->data, '\0');
|
||||||
c11_array arr = c11_vector__submit(&self->data);
|
c11_array arr = c11_vector__submit(&self->data);
|
||||||
c11_string* retval = arr.data;
|
c11_string* retval = arr.data;
|
||||||
retval->size = arr.count - sizeof(c11_string) - 1;
|
retval->size = arr.length - sizeof(c11_string) - 1;
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,19 +12,20 @@ static c11_vector /*T=char* */ _r_interned;
|
|||||||
|
|
||||||
void py_Name__initialize() {
|
void py_Name__initialize() {
|
||||||
c11_smallmap_s2n__ctor(&_interned);
|
c11_smallmap_s2n__ctor(&_interned);
|
||||||
for(int i = 0; i < _r_interned.count; i++) {
|
for(int i = 0; i < _r_interned.length; i++) {
|
||||||
free(c11__at(char*, &_r_interned, i));
|
free(c11__at(char*, &_r_interned, i));
|
||||||
}
|
}
|
||||||
c11_vector__ctor(&_r_interned, sizeof(c11_sv));
|
c11_vector__ctor(&_r_interned, sizeof(c11_sv));
|
||||||
|
|
||||||
#define MAGIC_METHOD(x) if(x != py_name(#x)) abort();
|
#define MAGIC_METHOD(x) \
|
||||||
|
if(x != py_name(#x)) abort();
|
||||||
#include "pocketpy/xmacros/magics.h"
|
#include "pocketpy/xmacros/magics.h"
|
||||||
#undef MAGIC_METHOD
|
#undef MAGIC_METHOD
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_Name__finalize() {
|
void py_Name__finalize() {
|
||||||
// free all char*
|
// free all char*
|
||||||
for(int i = 0; i < _r_interned.count; i++) {
|
for(int i = 0; i < _r_interned.length; i++) {
|
||||||
free(c11__getitem(char*, &_r_interned, i));
|
free(c11__getitem(char*, &_r_interned, i));
|
||||||
}
|
}
|
||||||
c11_smallmap_s2n__dtor(&_interned);
|
c11_smallmap_s2n__dtor(&_interned);
|
||||||
@ -38,27 +39,26 @@ py_Name py_namev(c11_sv name) {
|
|||||||
uint16_t index = c11_smallmap_s2n__get(&_interned, name, 0);
|
uint16_t index = c11_smallmap_s2n__get(&_interned, name, 0);
|
||||||
if(index != 0) return index;
|
if(index != 0) return index;
|
||||||
// generate new index
|
// generate new index
|
||||||
if(_interned.count > 65530) c11__abort("py_Name index overflow");
|
if(_interned.length > 65530) c11__abort("py_Name index overflow");
|
||||||
// NOTE: we must allocate the string in the heap so iterators are not invalidated
|
// NOTE: we must allocate the string in the heap so iterators are not invalidated
|
||||||
char* p = malloc(name.size + 1);
|
char* p = malloc(name.size + 1);
|
||||||
memcpy(p, name.data, name.size);
|
memcpy(p, name.data, name.size);
|
||||||
p[name.size] = '\0';
|
p[name.size] = '\0';
|
||||||
c11_vector__push(char*, &_r_interned, p);
|
c11_vector__push(char*, &_r_interned, p);
|
||||||
index = _r_interned.count; // 1-based
|
index = _r_interned.length; // 1-based
|
||||||
// save to _interned
|
// save to _interned
|
||||||
c11_smallmap_s2n__set(&_interned, (c11_sv){p, name.size}, index);
|
c11_smallmap_s2n__set(&_interned, (c11_sv){p, name.size}, index);
|
||||||
assert(_interned.count == _r_interned.count);
|
assert(_interned.length == _r_interned.length);
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* py_name2str(py_Name index) {
|
const char* py_name2str(py_Name index) {
|
||||||
assert(index > 0 && index <= _interned.count);
|
assert(index > 0 && index <= _interned.length);
|
||||||
return c11__getitem(char*, &_r_interned, index - 1);
|
return c11__getitem(char*, &_r_interned, index - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
c11_sv py_name2sv(py_Name index) {
|
c11_sv py_name2sv(py_Name index) {
|
||||||
assert(index > 0 && index <= _interned.count);
|
assert(index > 0 && index <= _interned.length);
|
||||||
const char* p = py_name2str(index);
|
const char* p = py_name2str(index);
|
||||||
return (c11_sv){p, strlen(p)};
|
return (c11_sv){p, strlen(p)};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,28 +3,28 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void c11_array__ctor(c11_array* self, int elem_size, int count){
|
void c11_array__ctor(c11_array* self, int elem_size, int length){
|
||||||
self->data = malloc(elem_size * count);
|
self->data = malloc(elem_size * length);
|
||||||
self->count = count;
|
self->length = length;
|
||||||
self->elem_size = elem_size;
|
self->elem_size = elem_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11_array__dtor(c11_array* self){
|
void c11_array__dtor(c11_array* self){
|
||||||
free(self->data);
|
free(self->data);
|
||||||
self->data = NULL;
|
self->data = NULL;
|
||||||
self->count = 0;
|
self->length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
c11_array c11_array__copy(const c11_array* self){
|
c11_array c11_array__copy(const c11_array* self){
|
||||||
c11_array retval;
|
c11_array retval;
|
||||||
c11_array__ctor(&retval, self->elem_size, self->count);
|
c11_array__ctor(&retval, self->elem_size, self->length);
|
||||||
memcpy(retval.data, self->data, self->elem_size * self->count);
|
memcpy(retval.data, self->data, self->elem_size * self->length);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11_vector__ctor(c11_vector* self, int elem_size){
|
void c11_vector__ctor(c11_vector* self, int elem_size){
|
||||||
self->data = NULL;
|
self->data = NULL;
|
||||||
self->count = 0;
|
self->length = 0;
|
||||||
self->capacity = 0;
|
self->capacity = 0;
|
||||||
self->elem_size = elem_size;
|
self->elem_size = elem_size;
|
||||||
}
|
}
|
||||||
@ -32,7 +32,7 @@ void c11_vector__ctor(c11_vector* self, int elem_size){
|
|||||||
void c11_vector__dtor(c11_vector* self){
|
void c11_vector__dtor(c11_vector* self){
|
||||||
if(self->data) free(self->data);
|
if(self->data) free(self->data);
|
||||||
self->data = NULL;
|
self->data = NULL;
|
||||||
self->count = 0;
|
self->length = 0;
|
||||||
self->capacity = 0;
|
self->capacity = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,8 +40,8 @@ c11_vector c11_vector__copy(const c11_vector* self){
|
|||||||
c11_vector retval;
|
c11_vector retval;
|
||||||
c11_vector__ctor(&retval, self->elem_size);
|
c11_vector__ctor(&retval, self->elem_size);
|
||||||
c11_vector__reserve(&retval, self->capacity);
|
c11_vector__reserve(&retval, self->capacity);
|
||||||
memcpy(retval.data, self->data, self->elem_size * self->count);
|
memcpy(retval.data, self->data, self->elem_size * self->length);
|
||||||
retval.count = self->count;
|
retval.length = self->length;
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,18 +53,18 @@ void c11_vector__reserve(c11_vector* self, int capacity){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void c11_vector__clear(c11_vector* self){
|
void c11_vector__clear(c11_vector* self){
|
||||||
self->count = 0;
|
self->length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* c11_vector__emplace(c11_vector* self){
|
void* c11_vector__emplace(c11_vector* self){
|
||||||
if(self->count == self->capacity) c11_vector__reserve(self, self->capacity*2);
|
if(self->length == self->capacity) c11_vector__reserve(self, self->capacity*2);
|
||||||
void* p = (char*)self->data + self->elem_size * self->count;
|
void* p = (char*)self->data + self->elem_size * self->length;
|
||||||
self->count++;
|
self->length++;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool c11_vector__contains(const c11_vector *self, void *elem){
|
bool c11_vector__contains(const c11_vector *self, void *elem){
|
||||||
for(int i = 0; i < self->count; i++){
|
for(int i = 0; i < self->length; i++){
|
||||||
void* p = (char*)self->data + self->elem_size * i;
|
void* p = (char*)self->data + self->elem_size * i;
|
||||||
if(memcmp(p, elem, self->elem_size) == 0) return true;
|
if(memcmp(p, elem, self->elem_size) == 0) return true;
|
||||||
}
|
}
|
||||||
@ -74,11 +74,11 @@ bool c11_vector__contains(const c11_vector *self, void *elem){
|
|||||||
c11_array c11_vector__submit(c11_vector* self){
|
c11_array c11_vector__submit(c11_vector* self){
|
||||||
c11_array retval = {
|
c11_array retval = {
|
||||||
.data = self->data,
|
.data = self->data,
|
||||||
.count = self->count,
|
.length = self->length,
|
||||||
.elem_size = self->elem_size
|
.elem_size = self->elem_size
|
||||||
};
|
};
|
||||||
self->data = NULL;
|
self->data = NULL;
|
||||||
self->count = 0;
|
self->length = 0;
|
||||||
self->capacity = 0;
|
self->capacity = 0;
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
@ -454,11 +454,11 @@ typedef struct SequenceExpr {
|
|||||||
|
|
||||||
static void SequenceExpr__emit_(Expr* self_, Ctx* ctx) {
|
static void SequenceExpr__emit_(Expr* self_, Ctx* ctx) {
|
||||||
SequenceExpr* self = (SequenceExpr*)self_;
|
SequenceExpr* self = (SequenceExpr*)self_;
|
||||||
for(int i = 0; i < self->items.count; i++) {
|
for(int i = 0; i < self->items.length; i++) {
|
||||||
Expr* item = c11__getitem(Expr*, &self->items, i);
|
Expr* item = c11__getitem(Expr*, &self->items, i);
|
||||||
vtemit_(item, ctx);
|
vtemit_(item, ctx);
|
||||||
}
|
}
|
||||||
Ctx__emit_(ctx, self->opcode, self->items.count, self->line);
|
Ctx__emit_(ctx, self->opcode, self->items.length, self->line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SequenceExpr__dtor(Expr* self_) {
|
void SequenceExpr__dtor(Expr* self_) {
|
||||||
@ -472,7 +472,7 @@ bool TupleExpr__emit_store(Expr* self_, Ctx* ctx) {
|
|||||||
// TOS is an iterable
|
// TOS is an iterable
|
||||||
// items may contain StarredExpr, we should check it
|
// items may contain StarredExpr, we should check it
|
||||||
int starred_i = -1;
|
int starred_i = -1;
|
||||||
for(int i = 0; i < self->items.count; i++) {
|
for(int i = 0; i < self->items.length; i++) {
|
||||||
Expr* e = c11__getitem(Expr*, &self->items, i);
|
Expr* e = c11__getitem(Expr*, &self->items, i);
|
||||||
if(e->vt->is_starred) {
|
if(e->vt->is_starred) {
|
||||||
if(((StarredExpr*)e)->level > 0) {
|
if(((StarredExpr*)e)->level > 0) {
|
||||||
@ -485,24 +485,24 @@ bool TupleExpr__emit_store(Expr* self_, Ctx* ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(starred_i == -1) {
|
if(starred_i == -1) {
|
||||||
Bytecode* prev = c11__at(Bytecode, &ctx->co->codes, ctx->co->codes.count - 1);
|
Bytecode* prev = c11__at(Bytecode, &ctx->co->codes, ctx->co->codes.length - 1);
|
||||||
if(prev->op == OP_BUILD_TUPLE && prev->arg == self->items.count) {
|
if(prev->op == OP_BUILD_TUPLE && prev->arg == self->items.length) {
|
||||||
// build tuple and unpack it is meaningless
|
// build tuple and unpack it is meaningless
|
||||||
Ctx__revert_last_emit_(ctx);
|
Ctx__revert_last_emit_(ctx);
|
||||||
} else {
|
} else {
|
||||||
Ctx__emit_(ctx, OP_UNPACK_SEQUENCE, self->items.count, self->line);
|
Ctx__emit_(ctx, OP_UNPACK_SEQUENCE, self->items.length, self->line);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// starred assignment target must be in a tuple
|
// starred assignment target must be in a tuple
|
||||||
if(self->items.count == 1) return false;
|
if(self->items.length == 1) return false;
|
||||||
// starred assignment target must be the last one (differ from cpython)
|
// starred assignment target must be the last one (differ from cpython)
|
||||||
if(starred_i != self->items.count - 1) return false;
|
if(starred_i != self->items.length - 1) return false;
|
||||||
// a,*b = [1,2,3]
|
// a,*b = [1,2,3]
|
||||||
// stack is [1,2,3] -> [1,[2,3]]
|
// stack is [1,2,3] -> [1,[2,3]]
|
||||||
Ctx__emit_(ctx, OP_UNPACK_EX, self->items.count - 1, self->line);
|
Ctx__emit_(ctx, OP_UNPACK_EX, self->items.length - 1, self->line);
|
||||||
}
|
}
|
||||||
// do reverse emit
|
// do reverse emit
|
||||||
for(int i = self->items.count - 1; i >= 0; i--) {
|
for(int i = self->items.length - 1; i >= 0; i--) {
|
||||||
Expr* e = c11__getitem(Expr*, &self->items, i);
|
Expr* e = c11__getitem(Expr*, &self->items, i);
|
||||||
bool ok = vtemit_store(e, ctx);
|
bool ok = vtemit_store(e, ctx);
|
||||||
if(!ok) return false;
|
if(!ok) return false;
|
||||||
@ -822,7 +822,7 @@ static void BinaryExpr__emit_(Expr* self_, Ctx* ctx) {
|
|||||||
|
|
||||||
Ctx__emit_(ctx, opcode, arg, self->line);
|
Ctx__emit_(ctx, opcode, arg, self->line);
|
||||||
|
|
||||||
for(int i = 0; i < jmps.count; i++) {
|
for(int i = 0; i < jmps.length; i++) {
|
||||||
Ctx__patch_jump(ctx, c11__getitem(int, &jmps, i));
|
Ctx__patch_jump(ctx, c11__getitem(int, &jmps, i));
|
||||||
}
|
}
|
||||||
c11_vector__dtor(&jmps);
|
c11_vector__dtor(&jmps);
|
||||||
@ -1069,8 +1069,8 @@ void CallExpr__emit_(Expr* self_, Ctx* ctx) {
|
|||||||
Ctx__emit_int(ctx, e->key, self->line);
|
Ctx__emit_int(ctx, e->key, self->line);
|
||||||
vtemit_(e->val, ctx);
|
vtemit_(e->val, ctx);
|
||||||
}
|
}
|
||||||
int KWARGC = self->kwargs.count;
|
int KWARGC = self->kwargs.length;
|
||||||
int ARGC = self->args.count;
|
int ARGC = self->args.length;
|
||||||
assert(KWARGC < 256 && ARGC < 256);
|
assert(KWARGC < 256 && ARGC < 256);
|
||||||
Ctx__emit_(ctx, opcode, (KWARGC << 8) | ARGC, self->line);
|
Ctx__emit_(ctx, opcode, (KWARGC << 8) | ARGC, self->line);
|
||||||
}
|
}
|
||||||
@ -1101,7 +1101,7 @@ static void Ctx__ctor(Ctx* self, CodeObject* co, FuncDecl* func, int level) {
|
|||||||
|
|
||||||
static void Ctx__dtor(Ctx* self) {
|
static void Ctx__dtor(Ctx* self) {
|
||||||
// clean the expr stack
|
// clean the expr stack
|
||||||
for(int i = 0; i < self->s_expr.count; i++) {
|
for(int i = 0; i < self->s_expr.length; i++) {
|
||||||
vtdelete(c11__getitem(Expr*, &self->s_expr, i));
|
vtdelete(c11__getitem(Expr*, &self->s_expr, i));
|
||||||
}
|
}
|
||||||
c11_vector__dtor(&self->s_expr);
|
c11_vector__dtor(&self->s_expr);
|
||||||
@ -1123,16 +1123,16 @@ static int Ctx__get_loop(Ctx* self) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static CodeBlock* Ctx__enter_block(Ctx* self, CodeBlockType type) {
|
static CodeBlock* Ctx__enter_block(Ctx* self, CodeBlockType type) {
|
||||||
CodeBlock block = {type, self->curr_iblock, self->co->codes.count, -1, -1};
|
CodeBlock block = {type, self->curr_iblock, self->co->codes.length, -1, -1};
|
||||||
c11_vector__push(CodeBlock, &self->co->blocks, block);
|
c11_vector__push(CodeBlock, &self->co->blocks, block);
|
||||||
self->curr_iblock = self->co->blocks.count - 1;
|
self->curr_iblock = self->co->blocks.length - 1;
|
||||||
return c11__at(CodeBlock, &self->co->blocks, self->curr_iblock);
|
return c11__at(CodeBlock, &self->co->blocks, self->curr_iblock);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Ctx__exit_block(Ctx* self) {
|
static void Ctx__exit_block(Ctx* self) {
|
||||||
CodeBlock* block = c11__at(CodeBlock, &self->co->blocks, self->curr_iblock);
|
CodeBlock* block = c11__at(CodeBlock, &self->co->blocks, self->curr_iblock);
|
||||||
CodeBlockType curr_type = block->type;
|
CodeBlockType curr_type = block->type;
|
||||||
block->end = self->co->codes.count;
|
block->end = self->co->codes.length;
|
||||||
self->curr_iblock = block->parent;
|
self->curr_iblock = block->parent;
|
||||||
assert(self->curr_iblock >= 0);
|
assert(self->curr_iblock >= 0);
|
||||||
if(curr_type == CodeBlockType_FOR_LOOP) {
|
if(curr_type == CodeBlockType_FOR_LOOP) {
|
||||||
@ -1161,7 +1161,7 @@ static int Ctx__emit_virtual(Ctx* self, Opcode opcode, uint16_t arg, int line, b
|
|||||||
BytecodeEx bcx = {line, is_virtual, self->curr_iblock};
|
BytecodeEx bcx = {line, is_virtual, self->curr_iblock};
|
||||||
c11_vector__push(Bytecode, &self->co->codes, bc);
|
c11_vector__push(Bytecode, &self->co->codes, bc);
|
||||||
c11_vector__push(BytecodeEx, &self->co->codes_ex, bcx);
|
c11_vector__push(BytecodeEx, &self->co->codes_ex, bcx);
|
||||||
int i = self->co->codes.count - 1;
|
int i = self->co->codes.length - 1;
|
||||||
BytecodeEx* codes_ex = (BytecodeEx*)self->co->codes_ex.data;
|
BytecodeEx* codes_ex = (BytecodeEx*)self->co->codes_ex.data;
|
||||||
if(line == BC_KEEPLINE) { codes_ex[i].lineno = i >= 1 ? codes_ex[i - 1].lineno : 1; }
|
if(line == BC_KEEPLINE) { codes_ex[i].lineno = i >= 1 ? codes_ex[i - 1].lineno : 1; }
|
||||||
return i;
|
return i;
|
||||||
@ -1188,14 +1188,14 @@ static int Ctx__emit_int(Ctx* self, int64_t value, int line) {
|
|||||||
|
|
||||||
static void Ctx__patch_jump(Ctx* self, int index) {
|
static void Ctx__patch_jump(Ctx* self, int index) {
|
||||||
Bytecode* co_codes = (Bytecode*)self->co->codes.data;
|
Bytecode* co_codes = (Bytecode*)self->co->codes.data;
|
||||||
int target = self->co->codes.count;
|
int target = self->co->codes.length;
|
||||||
Bytecode__set_signed_arg(&co_codes[index], target - index);
|
Bytecode__set_signed_arg(&co_codes[index], target - index);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool Ctx__add_label(Ctx* self, py_Name name) {
|
static bool Ctx__add_label(Ctx* self, py_Name name) {
|
||||||
bool ok = c11_smallmap_n2i__contains(&self->co->labels, name);
|
bool ok = c11_smallmap_n2i__contains(&self->co->labels, name);
|
||||||
if(ok) return false;
|
if(ok) return false;
|
||||||
c11_smallmap_n2i__set(&self->co->labels, name, self->co->codes.count);
|
c11_smallmap_n2i__set(&self->co->labels, name, self->co->codes.length);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1212,7 +1212,7 @@ static int Ctx__add_const_string(Ctx* self, c11_sv key) {
|
|||||||
py_TValue tmp;
|
py_TValue tmp;
|
||||||
py_newstrn(&tmp, key.data, key.size);
|
py_newstrn(&tmp, key.data, key.size);
|
||||||
c11_vector__push(py_TValue, &self->co->consts, tmp);
|
c11_vector__push(py_TValue, &self->co->consts, tmp);
|
||||||
int index = self->co->consts.count - 1;
|
int index = self->co->consts.length - 1;
|
||||||
c11_smallmap_s2n__set(&self->co_consts_string_dedup_map,
|
c11_smallmap_s2n__set(&self->co_consts_string_dedup_map,
|
||||||
c11_string__sv(PyObject__userdata(tmp._obj)),
|
c11_string__sv(PyObject__userdata(tmp._obj)),
|
||||||
index);
|
index);
|
||||||
@ -1223,7 +1223,7 @@ static int Ctx__add_const_string(Ctx* self, c11_sv key) {
|
|||||||
static int Ctx__add_const(Ctx* self, py_Ref v) {
|
static int Ctx__add_const(Ctx* self, py_Ref v) {
|
||||||
assert(v->type != tp_str);
|
assert(v->type != tp_str);
|
||||||
c11_vector__push(py_TValue, &self->co->consts, *v);
|
c11_vector__push(py_TValue, &self->co->consts, *v);
|
||||||
return self->co->consts.count - 1;
|
return self->co->consts.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line) {
|
static void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line) {
|
||||||
@ -1237,7 +1237,7 @@ static void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int l
|
|||||||
|
|
||||||
// emit top -> pop -> delete
|
// emit top -> pop -> delete
|
||||||
static void Ctx__s_emit_top(Ctx* self) {
|
static void Ctx__s_emit_top(Ctx* self) {
|
||||||
assert(self->s_expr.count);
|
assert(self->s_expr.length);
|
||||||
Expr* top = c11_vector__back(Expr*, &self->s_expr);
|
Expr* top = c11_vector__back(Expr*, &self->s_expr);
|
||||||
vtemit_(top, self);
|
vtemit_(top, self);
|
||||||
vtdelete(top);
|
vtdelete(top);
|
||||||
@ -1249,16 +1249,16 @@ static void Ctx__s_push(Ctx* self, Expr* expr) { c11_vector__push(Expr*, &self->
|
|||||||
|
|
||||||
// top
|
// top
|
||||||
static Expr* Ctx__s_top(Ctx* self) {
|
static Expr* Ctx__s_top(Ctx* self) {
|
||||||
assert(self->s_expr.count);
|
assert(self->s_expr.length);
|
||||||
return c11_vector__back(Expr*, &self->s_expr);
|
return c11_vector__back(Expr*, &self->s_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// size
|
// size
|
||||||
static int Ctx__s_size(Ctx* self) { return self->s_expr.count; }
|
static int Ctx__s_size(Ctx* self) { return self->s_expr.length; }
|
||||||
|
|
||||||
// pop -> delete
|
// pop -> delete
|
||||||
static void Ctx__s_pop(Ctx* self) {
|
static void Ctx__s_pop(Ctx* self) {
|
||||||
assert(self->s_expr.count);
|
assert(self->s_expr.length);
|
||||||
Expr* top = c11_vector__back(Expr*, &self->s_expr);
|
Expr* top = c11_vector__back(Expr*, &self->s_expr);
|
||||||
vtdelete(top);
|
vtdelete(top);
|
||||||
c11_vector__pop(&self->s_expr);
|
c11_vector__pop(&self->s_expr);
|
||||||
@ -1266,7 +1266,7 @@ static void Ctx__s_pop(Ctx* self) {
|
|||||||
|
|
||||||
// pop move
|
// pop move
|
||||||
static Expr* Ctx__s_popx(Ctx* self) {
|
static Expr* Ctx__s_popx(Ctx* self) {
|
||||||
assert(self->s_expr.count);
|
assert(self->s_expr.length);
|
||||||
Expr* top = c11_vector__back(Expr*, &self->s_expr);
|
Expr* top = c11_vector__back(Expr*, &self->s_expr);
|
||||||
c11_vector__pop(&self->s_expr);
|
c11_vector__pop(&self->s_expr);
|
||||||
return top;
|
return top;
|
||||||
@ -1329,7 +1329,7 @@ static void Compiler__dtor(Compiler* self) {
|
|||||||
if((err = B)) return err
|
if((err = B)) return err
|
||||||
|
|
||||||
static NameScope name_scope(Compiler* self) {
|
static NameScope name_scope(Compiler* self) {
|
||||||
NameScope s = self->contexts.count > 1 ? NAME_LOCAL : NAME_GLOBAL;
|
NameScope s = self->contexts.length > 1 ? NAME_LOCAL : NAME_GLOBAL;
|
||||||
if(self->src->is_dynamic && s == NAME_GLOBAL) s = NAME_GLOBAL_UNKNOWN;
|
if(self->src->is_dynamic && s == NAME_GLOBAL) s = NAME_GLOBAL_UNKNOWN;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
@ -1338,7 +1338,7 @@ Error* SyntaxError(Compiler* self, const char* fmt, ...) {
|
|||||||
Error* err = malloc(sizeof(Error));
|
Error* err = malloc(sizeof(Error));
|
||||||
err->src = self->src;
|
err->src = self->src;
|
||||||
PK_INCREF(self->src);
|
PK_INCREF(self->src);
|
||||||
Token* t = self->i == self->tokens.count ? prev() : curr();
|
Token* t = self->i == self->tokens.length ? prev() : curr();
|
||||||
err->lineno = t->line;
|
err->lineno = t->line;
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
@ -1451,7 +1451,7 @@ static Error* EXPR_VARS(Compiler* self) {
|
|||||||
static void push_global_context(Compiler* self, CodeObject* co) {
|
static void push_global_context(Compiler* self, CodeObject* co) {
|
||||||
co->start_line = self->i == 0 ? 1 : prev()->line;
|
co->start_line = self->i == 0 ? 1 : prev()->line;
|
||||||
Ctx* ctx = c11_vector__emplace(&self->contexts);
|
Ctx* ctx = c11_vector__emplace(&self->contexts);
|
||||||
Ctx__ctor(ctx, co, NULL, self->contexts.count);
|
Ctx__ctor(ctx, co, NULL, self->contexts.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Error* pop_context(Compiler* self) {
|
static Error* pop_context(Compiler* self) {
|
||||||
@ -1473,11 +1473,11 @@ static Error* pop_context(Compiler* self) {
|
|||||||
if(co->nlocals > PK_MAX_CO_VARNAMES) {
|
if(co->nlocals > PK_MAX_CO_VARNAMES) {
|
||||||
return SyntaxError(self, "maximum number of local variables exceeded");
|
return SyntaxError(self, "maximum number of local variables exceeded");
|
||||||
}
|
}
|
||||||
if(co->consts.count > 65530) {
|
if(co->consts.length > 65530) {
|
||||||
return SyntaxError(self, "maximum number of constants exceeded");
|
return SyntaxError(self, "maximum number of constants exceeded");
|
||||||
}
|
}
|
||||||
// pre-compute LOOP_BREAK and LOOP_CONTINUE
|
// pre-compute LOOP_BREAK and LOOP_CONTINUE
|
||||||
for(int i = 0; i < codes->count; i++) {
|
for(int i = 0; i < codes->length; i++) {
|
||||||
Bytecode* bc = c11__at(Bytecode, codes, i);
|
Bytecode* bc = c11__at(Bytecode, codes, i);
|
||||||
if(bc->op == OP_LOOP_CONTINUE) {
|
if(bc->op == OP_LOOP_CONTINUE) {
|
||||||
CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
|
CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
|
||||||
@ -1492,7 +1492,7 @@ static Error* pop_context(Compiler* self) {
|
|||||||
if(func) {
|
if(func) {
|
||||||
// check generator
|
// check generator
|
||||||
Bytecode* codes = func->code.codes.data;
|
Bytecode* codes = func->code.codes.data;
|
||||||
int codes_length = func->code.codes.count;
|
int codes_length = func->code.codes.length;
|
||||||
|
|
||||||
for(int i = 0; i < codes_length; i++) {
|
for(int i = 0; i < codes_length; i++) {
|
||||||
if(codes[i].op == OP_YIELD_VALUE) {
|
if(codes[i].op == OP_YIELD_VALUE) {
|
||||||
@ -1510,7 +1510,7 @@ static Error* pop_context(Compiler* self) {
|
|||||||
}
|
}
|
||||||
if(func->type == FuncType_UNSET) {
|
if(func->type == FuncType_UNSET) {
|
||||||
bool is_simple = true;
|
bool is_simple = true;
|
||||||
if(func->kwargs.count > 0) is_simple = false;
|
if(func->kwargs.length > 0) is_simple = false;
|
||||||
if(func->starred_arg >= 0) is_simple = false;
|
if(func->starred_arg >= 0) is_simple = false;
|
||||||
if(func->starred_kwarg >= 0) is_simple = false;
|
if(func->starred_kwarg >= 0) is_simple = false;
|
||||||
|
|
||||||
@ -1855,7 +1855,7 @@ static Error* exprCall(Compiler* self) {
|
|||||||
c11_vector__push(CallExprKwArg, &e->kwargs, kw);
|
c11_vector__push(CallExprKwArg, &e->kwargs, kw);
|
||||||
} else {
|
} else {
|
||||||
// positional argument
|
// positional argument
|
||||||
if(e->kwargs.count > 0) {
|
if(e->kwargs.length > 0) {
|
||||||
return SyntaxError(self, "positional argument follows keyword argument");
|
return SyntaxError(self, "positional argument follows keyword argument");
|
||||||
}
|
}
|
||||||
c11_vector__push(Expr*, &e->args, Ctx__s_popx(ctx()));
|
c11_vector__push(Expr*, &e->args, Ctx__s_popx(ctx()));
|
||||||
@ -1994,7 +1994,7 @@ static Error* compile_while_loop(Compiler* self) {
|
|||||||
// optional else clause
|
// optional else clause
|
||||||
if(match(TK_ELSE)) {
|
if(match(TK_ELSE)) {
|
||||||
check(compile_block_body(self, compile_stmt));
|
check(compile_block_body(self, compile_stmt));
|
||||||
block->end2 = ctx()->co->codes.count;
|
block->end2 = ctx()->co->codes.length;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -2021,7 +2021,7 @@ static Error* compile_for_loop(Compiler* self) {
|
|||||||
// optional else clause
|
// optional else clause
|
||||||
if(match(TK_ELSE)) {
|
if(match(TK_ELSE)) {
|
||||||
check(compile_block_body(self, compile_stmt));
|
check(compile_block_body(self, compile_stmt));
|
||||||
block->end2 = ctx()->co->codes.count;
|
block->end2 = ctx()->co->codes.length;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -2097,10 +2097,10 @@ static FuncDecl_ push_f_context(Compiler* self, c11_sv name, int* out_index) {
|
|||||||
// add_func_decl
|
// add_func_decl
|
||||||
Ctx* top_ctx = ctx();
|
Ctx* top_ctx = ctx();
|
||||||
c11_vector__push(FuncDecl_, &top_ctx->co->func_decls, decl);
|
c11_vector__push(FuncDecl_, &top_ctx->co->func_decls, decl);
|
||||||
*out_index = top_ctx->co->func_decls.count - 1;
|
*out_index = top_ctx->co->func_decls.length - 1;
|
||||||
// push new context
|
// push new context
|
||||||
top_ctx = c11_vector__emplace(&self->contexts);
|
top_ctx = c11_vector__emplace(&self->contexts);
|
||||||
Ctx__ctor(top_ctx, &decl->code, decl, self->contexts.count);
|
Ctx__ctor(top_ctx, &decl->code, decl, self->contexts.length);
|
||||||
return decl;
|
return decl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2214,7 +2214,7 @@ static Error* compile_function(Compiler* self, int decorators) {
|
|||||||
check(compile_block_body(self, compile_stmt));
|
check(compile_block_body(self, compile_stmt));
|
||||||
check(pop_context(self));
|
check(pop_context(self));
|
||||||
|
|
||||||
if(decl->code.codes.count >= 2) {
|
if(decl->code.codes.length >= 2) {
|
||||||
Bytecode* codes = (Bytecode*)decl->code.codes.data;
|
Bytecode* codes = (Bytecode*)decl->code.codes.data;
|
||||||
|
|
||||||
if(codes[0].op == OP_LOAD_CONST && codes[1].op == OP_POP_TOP) {
|
if(codes[0].op == OP_LOAD_CONST && codes[1].op == OP_POP_TOP) {
|
||||||
@ -2463,14 +2463,15 @@ static Error* compile_stmt(Compiler* self) {
|
|||||||
consume_end_stmt();
|
consume_end_stmt();
|
||||||
break;
|
break;
|
||||||
case TK_YIELD:
|
case TK_YIELD:
|
||||||
if(self->contexts.count <= 1) return SyntaxError(self, "'yield' outside function");
|
if(self->contexts.length <= 1) return SyntaxError(self, "'yield' outside function");
|
||||||
check(EXPR_TUPLE(self));
|
check(EXPR_TUPLE(self));
|
||||||
Ctx__s_emit_top(ctx());
|
Ctx__s_emit_top(ctx());
|
||||||
Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line);
|
Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line);
|
||||||
consume_end_stmt();
|
consume_end_stmt();
|
||||||
break;
|
break;
|
||||||
case TK_YIELD_FROM:
|
case TK_YIELD_FROM:
|
||||||
if(self->contexts.count <= 1) return SyntaxError(self, "'yield from' outside function");
|
if(self->contexts.length <= 1)
|
||||||
|
return SyntaxError(self, "'yield from' outside function");
|
||||||
check(EXPR_TUPLE(self));
|
check(EXPR_TUPLE(self));
|
||||||
Ctx__s_emit_top(ctx());
|
Ctx__s_emit_top(ctx());
|
||||||
Ctx__emit_(ctx(), OP_GET_ITER, BC_NOARG, kw_line);
|
Ctx__emit_(ctx(), OP_GET_ITER, BC_NOARG, kw_line);
|
||||||
@ -2482,7 +2483,7 @@ static Error* compile_stmt(Compiler* self) {
|
|||||||
consume_end_stmt();
|
consume_end_stmt();
|
||||||
break;
|
break;
|
||||||
case TK_RETURN:
|
case TK_RETURN:
|
||||||
if(self->contexts.count <= 1) return SyntaxError(self, "'return' outside function");
|
if(self->contexts.length <= 1) return SyntaxError(self, "'return' outside function");
|
||||||
if(match_end_stmt(self)) {
|
if(match_end_stmt(self)) {
|
||||||
Ctx__emit_(ctx(), OP_RETURN_VALUE, 1, kw_line);
|
Ctx__emit_(ctx(), OP_RETURN_VALUE, 1, kw_line);
|
||||||
} else {
|
} else {
|
||||||
@ -2672,7 +2673,7 @@ Error* pk_compile(SourceData_ src, CodeObject* out) {
|
|||||||
#if 0
|
#if 0
|
||||||
Token* data = (Token*)tokens.data;
|
Token* data = (Token*)tokens.data;
|
||||||
printf("%s\n", src->filename->data);
|
printf("%s\n", src->filename->data);
|
||||||
for(int i = 0; i < tokens.count; i++) {
|
for(int i = 0; i < tokens.length; i++) {
|
||||||
Token* t = data + i;
|
Token* t = data + i;
|
||||||
c11_string* tmp = c11_string__new2(t->start, t->length);
|
c11_string* tmp = c11_string__new2(t->start, t->length);
|
||||||
if(t->value.index == TokenValue_STR) {
|
if(t->value.index == TokenValue_STR) {
|
||||||
|
@ -125,7 +125,7 @@ static void add_token_with_value(Lexer* self, TokenIndex type, TokenValue value)
|
|||||||
self->brackets_level,
|
self->brackets_level,
|
||||||
value};
|
value};
|
||||||
// handle "not in", "is not", "yield from"
|
// handle "not in", "is not", "yield from"
|
||||||
if(self->nexts.count > 0) {
|
if(self->nexts.length > 0) {
|
||||||
Token* back = &c11_vector__back(Token, &self->nexts);
|
Token* back = &c11_vector__back(Token, &self->nexts);
|
||||||
if(back->type == TK_NOT_KW && type == TK_IN) {
|
if(back->type == TK_NOT_KW && type == TK_IN) {
|
||||||
back->type = TK_NOT_IN;
|
back->type = TK_NOT_IN;
|
||||||
@ -336,12 +336,12 @@ static Error* _eat_string(Lexer* self, c11_sbuf* buff, char quote, enum StringTy
|
|||||||
|
|
||||||
// submit {expr} tokens
|
// submit {expr} tokens
|
||||||
bool eof = false;
|
bool eof = false;
|
||||||
int token_count = self->nexts.count;
|
int token_count = self->nexts.length;
|
||||||
while(!eof) {
|
while(!eof) {
|
||||||
Error* err = lex_one_token(self, &eof, true);
|
Error* err = lex_one_token(self, &eof, true);
|
||||||
if(err) return err;
|
if(err) return err;
|
||||||
}
|
}
|
||||||
if(self->nexts.count == token_count) {
|
if(self->nexts.length == token_count) {
|
||||||
// f'{}' is not allowed
|
// f'{}' is not allowed
|
||||||
return SyntaxError(self, "f-string: empty expression not allowed");
|
return SyntaxError(self, "f-string: empty expression not allowed");
|
||||||
}
|
}
|
||||||
@ -353,7 +353,7 @@ static Error* _eat_string(Lexer* self, c11_sbuf* buff, char quote, enum StringTy
|
|||||||
} else {
|
} else {
|
||||||
return SyntaxError(self, "f-string: single '}' is not allowed");
|
return SyntaxError(self, "f-string: single '}' is not allowed");
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
c11_sbuf__write_char(buff, c);
|
c11_sbuf__write_char(buff, c);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -600,7 +600,7 @@ static Error* lex_one_token(Lexer* self, bool* eof, bool is_fstring) {
|
|||||||
if(is_fstring) return SyntaxError(self, "unterminated f-string expression");
|
if(is_fstring) return SyntaxError(self, "unterminated f-string expression");
|
||||||
|
|
||||||
self->token_start = self->curr_char;
|
self->token_start = self->curr_char;
|
||||||
while(self->indents.count > 1) {
|
while(self->indents.length > 1) {
|
||||||
c11_vector__pop(&self->indents);
|
c11_vector__pop(&self->indents);
|
||||||
add_token(self, TK_DEDENT);
|
add_token(self, TK_DEDENT);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -637,7 +637,7 @@ Error* Lexer__process(SourceData_ src, TokenArray* out_tokens) {
|
|||||||
|
|
||||||
void TokenArray__dtor(TokenArray* self) {
|
void TokenArray__dtor(TokenArray* self) {
|
||||||
Token* data = self->data;
|
Token* data = self->data;
|
||||||
for(int i = 0; i < self->count; i++) {
|
for(int i = 0; i < self->length; i++) {
|
||||||
if(data[i].value.index == TokenValue_STR) { c11_string__delete(data[i].value._str); }
|
if(data[i].value.index == TokenValue_STR) { c11_string__delete(data[i].value._str); }
|
||||||
}
|
}
|
||||||
c11_array__dtor(self);
|
c11_array__dtor(self);
|
||||||
|
@ -844,7 +844,7 @@ FrameResult VM__run_top_frame(VM* self) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for(int i = 0; i < dict->count; i++) {
|
for(int i = 0; i < dict->length; i++) {
|
||||||
NameDict_KV* kv = c11__at(NameDict_KV, dict, i);
|
NameDict_KV* kv = c11__at(NameDict_KV, dict, i);
|
||||||
if(!kv->key) continue;
|
if(!kv->key) continue;
|
||||||
c11_sv name = py_name2sv(kv->key);
|
c11_sv name = py_name2sv(kv->key);
|
||||||
@ -1124,21 +1124,21 @@ static bool stack_format_object(VM* self, c11_sv spec) {
|
|||||||
py_StackRef val = TOP();
|
py_StackRef val = TOP();
|
||||||
if(spec.size == 0) return py_str(val);
|
if(spec.size == 0) return py_str(val);
|
||||||
|
|
||||||
if(spec.data[0] == '!'){
|
if(spec.data[0] == '!') {
|
||||||
if(c11_sv__startswith(spec, (c11_sv){"!r", 2})){
|
if(c11_sv__startswith(spec, (c11_sv){"!r", 2})) {
|
||||||
spec.data += 2;
|
spec.data += 2;
|
||||||
spec.size -= 2;
|
spec.size -= 2;
|
||||||
if(!py_repr(val)) return false;
|
if(!py_repr(val)) return false;
|
||||||
py_assign(val, py_retval());
|
py_assign(val, py_retval());
|
||||||
if(spec.size == 0) return true;
|
if(spec.size == 0) return true;
|
||||||
}else{
|
} else {
|
||||||
return ValueError("invalid conversion specifier (only !r is supported)");
|
return ValueError("invalid conversion specifier (only !r is supported)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(spec.size > 0);
|
assert(spec.size > 0);
|
||||||
|
|
||||||
if(spec.data[0] == ':'){
|
if(spec.data[0] == ':') {
|
||||||
spec.data++;
|
spec.data++;
|
||||||
spec.size--;
|
spec.size--;
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ int Frame__prepare_jump_exception_handler(Frame* self, ValueStack* _s) {
|
|||||||
|
|
||||||
void Frame__prepare_jump_break(Frame* self, ValueStack* _s, int target) {
|
void Frame__prepare_jump_break(Frame* self, ValueStack* _s, int target) {
|
||||||
int iblock = Frame__iblock(self);
|
int iblock = Frame__iblock(self);
|
||||||
if(target >= self->co->codes.count) {
|
if(target >= self->co->codes.length) {
|
||||||
while(iblock >= 0)
|
while(iblock >= 0)
|
||||||
iblock = Frame__exit_block(self, _s, iblock);
|
iblock = Frame__exit_block(self, _s, iblock);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include "pocketpy/common/memorypool.h"
|
#include "pocketpy/common/memorypool.h"
|
||||||
#include "pocketpy/objects/base.h"
|
#include "pocketpy/objects/base.h"
|
||||||
|
|
||||||
void ManagedHeap__ctor(ManagedHeap *self, VM *vm){
|
void ManagedHeap__ctor(ManagedHeap* self, VM* vm) {
|
||||||
c11_vector__ctor(&self->no_gc, sizeof(PyObject*));
|
c11_vector__ctor(&self->no_gc, sizeof(PyObject*));
|
||||||
c11_vector__ctor(&self->gen, sizeof(PyObject*));
|
c11_vector__ctor(&self->gen, sizeof(PyObject*));
|
||||||
|
|
||||||
@ -13,12 +13,12 @@ void ManagedHeap__ctor(ManagedHeap *self, VM *vm){
|
|||||||
self->gc_on_delete = NULL;
|
self->gc_on_delete = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ManagedHeap__dtor(ManagedHeap *self){
|
void ManagedHeap__dtor(ManagedHeap* self) {
|
||||||
for(int i = 0; i < self->gen.count; i++){
|
for(int i = 0; i < self->gen.length; i++) {
|
||||||
PyObject* obj = c11__getitem(PyObject*, &self->gen, i);
|
PyObject* obj = c11__getitem(PyObject*, &self->gen, i);
|
||||||
PyObject__delete(obj);
|
PyObject__delete(obj);
|
||||||
}
|
}
|
||||||
for(int i = 0; i < self->no_gc.count; i++){
|
for(int i = 0; i < self->no_gc.length; i++) {
|
||||||
PyObject* obj = c11__getitem(PyObject*, &self->no_gc, i);
|
PyObject* obj = c11__getitem(PyObject*, &self->no_gc, i);
|
||||||
PyObject__delete(obj);
|
PyObject__delete(obj);
|
||||||
}
|
}
|
||||||
@ -26,47 +26,43 @@ void ManagedHeap__dtor(ManagedHeap *self){
|
|||||||
c11_vector__dtor(&self->gen);
|
c11_vector__dtor(&self->gen);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ManagedHeap__collect_if_needed(ManagedHeap *self){
|
void ManagedHeap__collect_if_needed(ManagedHeap* self) {
|
||||||
if(self->gc_counter < self->gc_threshold) return;
|
if(self->gc_counter < self->gc_threshold) return;
|
||||||
self->gc_counter = 0;
|
self->gc_counter = 0;
|
||||||
ManagedHeap__collect(self);
|
ManagedHeap__collect(self);
|
||||||
self->gc_threshold = self->gen.count * 2;
|
self->gc_threshold = self->gen.length * 2;
|
||||||
if(self->gc_threshold < PK_GC_MIN_THRESHOLD){
|
if(self->gc_threshold < PK_GC_MIN_THRESHOLD) { self->gc_threshold = PK_GC_MIN_THRESHOLD; }
|
||||||
self->gc_threshold = PK_GC_MIN_THRESHOLD;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ManagedHeap__collect(ManagedHeap *self){
|
int ManagedHeap__collect(ManagedHeap* self) {
|
||||||
ManagedHeap__mark(self);
|
ManagedHeap__mark(self);
|
||||||
int freed = ManagedHeap__sweep(self);
|
int freed = ManagedHeap__sweep(self);
|
||||||
return freed;
|
return freed;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ManagedHeap__sweep(ManagedHeap *self){
|
int ManagedHeap__sweep(ManagedHeap* self) {
|
||||||
c11_vector alive;
|
c11_vector alive;
|
||||||
c11_vector__ctor(&alive, sizeof(PyObject*));
|
c11_vector__ctor(&alive, sizeof(PyObject*));
|
||||||
c11_vector__reserve(&alive, self->gen.count / 2);
|
c11_vector__reserve(&alive, self->gen.length / 2);
|
||||||
|
|
||||||
for(int i = 0; i < self->gen.count; i++){
|
for(int i = 0; i < self->gen.length; i++) {
|
||||||
PyObject* obj = c11__getitem(PyObject*, &self->gen, i);
|
PyObject* obj = c11__getitem(PyObject*, &self->gen, i);
|
||||||
if(obj->gc_marked) {
|
if(obj->gc_marked) {
|
||||||
obj->gc_marked = false;
|
obj->gc_marked = false;
|
||||||
c11_vector__push(PyObject*, &alive, obj);
|
c11_vector__push(PyObject*, &alive, obj);
|
||||||
} else {
|
} else {
|
||||||
if(self->gc_on_delete){
|
if(self->gc_on_delete) { self->gc_on_delete(self->vm, obj); }
|
||||||
self->gc_on_delete(self->vm, obj);
|
|
||||||
}
|
|
||||||
PyObject__delete(obj);
|
PyObject__delete(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear _no_gc marked flag
|
// clear _no_gc marked flag
|
||||||
for(int i=0; i<self->no_gc.count; i++){
|
for(int i = 0; i < self->no_gc.length; i++) {
|
||||||
PyObject* obj = c11__getitem(PyObject*, &self->no_gc, i);
|
PyObject* obj = c11__getitem(PyObject*, &self->no_gc, i);
|
||||||
obj->gc_marked = false;
|
obj->gc_marked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int freed = self->gen.count - alive.count;
|
int freed = self->gen.length - alive.length;
|
||||||
|
|
||||||
// destroy old gen
|
// destroy old gen
|
||||||
c11_vector__dtor(&self->gen);
|
c11_vector__dtor(&self->gen);
|
||||||
@ -77,28 +73,28 @@ int ManagedHeap__sweep(ManagedHeap *self){
|
|||||||
return freed;
|
return freed;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* ManagedHeap__new(ManagedHeap *self, py_Type type, int slots, int udsize){
|
PyObject* ManagedHeap__new(ManagedHeap* self, py_Type type, int slots, int udsize) {
|
||||||
PyObject* obj = PyObject__new(type, slots, udsize);
|
PyObject* obj = PyObject__new(type, slots, udsize);
|
||||||
c11_vector__push(PyObject*, &self->no_gc, obj);
|
c11_vector__push(PyObject*, &self->no_gc, obj);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* ManagedHeap__gcnew(ManagedHeap *self, py_Type type, int slots, int udsize){
|
PyObject* ManagedHeap__gcnew(ManagedHeap* self, py_Type type, int slots, int udsize) {
|
||||||
PyObject* obj = PyObject__new(type, slots, udsize);
|
PyObject* obj = PyObject__new(type, slots, udsize);
|
||||||
c11_vector__push(PyObject*, &self->gen, obj);
|
c11_vector__push(PyObject*, &self->gen, obj);
|
||||||
self->gc_counter++;
|
self->gc_counter++;
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* PyObject__new(py_Type type, int slots, int size){
|
PyObject* PyObject__new(py_Type type, int slots, int size) {
|
||||||
assert(slots >= 0 || slots == -1);
|
assert(slots >= 0 || slots == -1);
|
||||||
PyObject* self;
|
PyObject* self;
|
||||||
// header + slots + udsize
|
// header + slots + udsize
|
||||||
size = sizeof(PyObject) + PK_OBJ_SLOTS_SIZE(slots) + size;
|
size = sizeof(PyObject) + PK_OBJ_SLOTS_SIZE(slots) + size;
|
||||||
if(size <= kPoolObjectBlockSize){
|
if(size <= kPoolObjectBlockSize) {
|
||||||
self = PoolObject_alloc();
|
self = PoolObject_alloc();
|
||||||
self->gc_is_large = false;
|
self->gc_is_large = false;
|
||||||
}else{
|
} else {
|
||||||
self = malloc(size);
|
self = malloc(size);
|
||||||
self->gc_is_large = true;
|
self->gc_is_large = true;
|
||||||
}
|
}
|
||||||
@ -108,9 +104,9 @@ PyObject* PyObject__new(py_Type type, int slots, int size){
|
|||||||
|
|
||||||
// initialize slots or dict
|
// initialize slots or dict
|
||||||
void* p = (char*)self + 8;
|
void* p = (char*)self + 8;
|
||||||
if(slots >= 0){
|
if(slots >= 0) {
|
||||||
memset(p, 0, slots*sizeof(py_TValue));
|
memset(p, 0, slots * sizeof(py_TValue));
|
||||||
}else{
|
} else {
|
||||||
NameDict__ctor(p);
|
NameDict__ctor(p);
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
|
@ -317,7 +317,7 @@ py_Type pk_newtype(const char* name,
|
|||||||
bool is_python,
|
bool is_python,
|
||||||
bool is_sealed) {
|
bool is_sealed) {
|
||||||
c11_vector* types = &pk_current_vm->types;
|
c11_vector* types = &pk_current_vm->types;
|
||||||
py_Type index = types->count;
|
py_Type index = types->length;
|
||||||
py_TypeInfo* ti = c11_vector__emplace(types);
|
py_TypeInfo* ti = c11_vector__emplace(types);
|
||||||
py_TypeInfo* base_ti = base ? c11__at(py_TypeInfo, types, base) : NULL;
|
py_TypeInfo* base_ti = base ? c11__at(py_TypeInfo, types, base) : NULL;
|
||||||
if(base_ti && base_ti->is_sealed) {
|
if(base_ti && base_ti->is_sealed) {
|
||||||
@ -340,7 +340,7 @@ py_Type py_newtype(const char* name, py_Type base, const py_GlobalRef module, vo
|
|||||||
static bool
|
static bool
|
||||||
prepare_py_call(py_TValue* buffer, py_Ref argv, py_Ref p1, int kwargc, const FuncDecl* decl) {
|
prepare_py_call(py_TValue* buffer, py_Ref argv, py_Ref p1, int kwargc, const FuncDecl* decl) {
|
||||||
const CodeObject* co = &decl->code;
|
const CodeObject* co = &decl->code;
|
||||||
int decl_argc = decl->args.count;
|
int decl_argc = decl->args.length;
|
||||||
|
|
||||||
if(p1 - argv < decl_argc) {
|
if(p1 - argv < decl_argc) {
|
||||||
return TypeError("%s() takes %d positional arguments but %d were given",
|
return TypeError("%s() takes %d positional arguments but %d were given",
|
||||||
@ -452,9 +452,9 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case FuncType_SIMPLE:
|
case FuncType_SIMPLE:
|
||||||
if(p1 - argv != fn->decl->args.count) {
|
if(p1 - argv != fn->decl->args.length) {
|
||||||
const char* fmt = "%s() takes %d positional arguments but %d were given";
|
const char* fmt = "%s() takes %d positional arguments but %d were given";
|
||||||
TypeError(fmt, co->name->data, fn->decl->args.count, p1 - argv);
|
TypeError(fmt, co->name->data, fn->decl->args.length, p1 - argv);
|
||||||
return RES_ERROR;
|
return RES_ERROR;
|
||||||
}
|
}
|
||||||
if(kwargc) {
|
if(kwargc) {
|
||||||
@ -552,7 +552,7 @@ void pk__mark_value(py_TValue* val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void pk__mark_namedict(NameDict* dict) {
|
void pk__mark_namedict(NameDict* dict) {
|
||||||
for(int i = 0; i < dict->count; i++) {
|
for(int i = 0; i < dict->length; i++) {
|
||||||
NameDict_KV* kv = c11__at(NameDict_KV, dict, i);
|
NameDict_KV* kv = c11__at(NameDict_KV, dict, i);
|
||||||
pk__mark_value(&kv->value);
|
pk__mark_value(&kv->value);
|
||||||
}
|
}
|
||||||
@ -589,7 +589,7 @@ void CodeObject__gc_mark(const CodeObject* self) {
|
|||||||
void ManagedHeap__mark(ManagedHeap* self) {
|
void ManagedHeap__mark(ManagedHeap* self) {
|
||||||
VM* vm = self->vm;
|
VM* vm = self->vm;
|
||||||
// mark heap objects
|
// mark heap objects
|
||||||
for(int i = 0; i < self->no_gc.count; i++) {
|
for(int i = 0; i < self->no_gc.length; i++) {
|
||||||
PyObject* obj = c11__getitem(PyObject*, &self->no_gc, i);
|
PyObject* obj = c11__getitem(PyObject*, &self->no_gc, i);
|
||||||
mark_object(obj);
|
mark_object(obj);
|
||||||
}
|
}
|
||||||
@ -599,7 +599,7 @@ void ManagedHeap__mark(ManagedHeap* self) {
|
|||||||
}
|
}
|
||||||
// mark magic slots
|
// mark magic slots
|
||||||
py_TypeInfo* types = vm->types.data;
|
py_TypeInfo* types = vm->types.data;
|
||||||
int types_length = vm->types.count;
|
int types_length = vm->types.length;
|
||||||
// 0-th type is placeholder
|
// 0-th type is placeholder
|
||||||
for(int i = 1; i < types_length; i++) {
|
for(int i = 1; i < types_length; i++) {
|
||||||
for(int j = 0; j <= __missing__; j++) {
|
for(int j = 0; j <= __missing__; j++) {
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
static void disassemble(CodeObject* co) {
|
static void disassemble(CodeObject* co) {
|
||||||
c11_vector /*T=int*/ jumpTargets;
|
c11_vector /*T=int*/ jumpTargets;
|
||||||
c11_vector__ctor(&jumpTargets, sizeof(int));
|
c11_vector__ctor(&jumpTargets, sizeof(int));
|
||||||
for(int i = 0; i < co->codes.count; i++) {
|
for(int i = 0; i < co->codes.length; i++) {
|
||||||
Bytecode* bc = c11__at(Bytecode, &co->codes, i);
|
Bytecode* bc = c11__at(Bytecode, &co->codes, i);
|
||||||
if(Bytecode__is_forward_jump(bc)) {
|
if(Bytecode__is_forward_jump(bc)) {
|
||||||
int target = (int16_t)bc->arg + i;
|
int target = (int16_t)bc->arg + i;
|
||||||
@ -20,7 +20,7 @@ static void disassemble(CodeObject* co) {
|
|||||||
c11_sbuf__ctor(&ss);
|
c11_sbuf__ctor(&ss);
|
||||||
|
|
||||||
int prev_line = -1;
|
int prev_line = -1;
|
||||||
for(int i = 0; i < co->codes.count; i++) {
|
for(int i = 0; i < co->codes.length; i++) {
|
||||||
Bytecode byte = c11__getitem(Bytecode, &co->codes, i);
|
Bytecode byte = c11__getitem(Bytecode, &co->codes, i);
|
||||||
BytecodeEx ex = c11__getitem(BytecodeEx, &co->codes_ex, i);
|
BytecodeEx ex = c11__getitem(BytecodeEx, &co->codes_ex, i);
|
||||||
|
|
||||||
@ -101,7 +101,7 @@ static void disassemble(CodeObject* co) {
|
|||||||
}
|
}
|
||||||
} while(0);
|
} while(0);
|
||||||
|
|
||||||
if(i != co->codes.count - 1) c11_sbuf__write_char(&ss, '\n');
|
if(i != co->codes.length - 1) c11_sbuf__write_char(&ss, '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
c11_string* output = c11_sbuf__submit(&ss);
|
c11_string* output = c11_sbuf__submit(&ss);
|
||||||
@ -115,12 +115,12 @@ static bool dis_dis(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
|
|
||||||
CodeObject* code = NULL;
|
CodeObject* code = NULL;
|
||||||
if(py_istype(argv, tp_function)){
|
if(py_istype(argv, tp_function)) {
|
||||||
Function* ud = py_touserdata(argv);
|
Function* ud = py_touserdata(argv);
|
||||||
code = &ud->decl->code;
|
code = &ud->decl->code;
|
||||||
}else if(py_istype(argv, tp_code)){
|
} else if(py_istype(argv, tp_code)) {
|
||||||
code = py_touserdata(argv);
|
code = py_touserdata(argv);
|
||||||
}else{
|
} else {
|
||||||
return TypeError("dis() expected a code object");
|
return TypeError("dis() expected a code object");
|
||||||
}
|
}
|
||||||
disassemble(code);
|
disassemble(code);
|
||||||
|
@ -152,7 +152,7 @@ void CodeObject__dtor(CodeObject* self) {
|
|||||||
|
|
||||||
c11_vector__dtor(&self->blocks);
|
c11_vector__dtor(&self->blocks);
|
||||||
|
|
||||||
for(int i = 0; i < self->func_decls.count; i++) {
|
for(int i = 0; i < self->func_decls.length; i++) {
|
||||||
FuncDecl_ decl = c11__getitem(FuncDecl_, &self->func_decls, i);
|
FuncDecl_ decl = c11__getitem(FuncDecl_, &self->func_decls, i);
|
||||||
PK_DECREF(decl);
|
PK_DECREF(decl);
|
||||||
}
|
}
|
||||||
@ -173,7 +173,7 @@ int CodeObject__add_varname(CodeObject* self, py_Name name) {
|
|||||||
if(index >= 0) return index;
|
if(index >= 0) return index;
|
||||||
c11_vector__push(uint16_t, &self->varnames, name);
|
c11_vector__push(uint16_t, &self->varnames, name);
|
||||||
self->nlocals++;
|
self->nlocals++;
|
||||||
index = self->varnames.count - 1;
|
index = self->varnames.length - 1;
|
||||||
c11_smallmap_n2i__set(&self->varnames_inv, name, index);
|
c11_smallmap_n2i__set(&self->varnames_inv, name, index);
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ int py_import(const char* path_cstr) {
|
|||||||
|
|
||||||
c11_vector /* T=c11_sv */ cpnts = c11_sv__split(package_sv, '.');
|
c11_vector /* T=c11_sv */ cpnts = c11_sv__split(package_sv, '.');
|
||||||
for(int i = is_init; i < dot_count; i++) {
|
for(int i = is_init; i < dot_count; i++) {
|
||||||
if(cpnts.count == 0)
|
if(cpnts.length == 0)
|
||||||
return ImportError("attempted relative import beyond top-level package");
|
return ImportError("attempted relative import beyond top-level package");
|
||||||
c11_vector__pop(&cpnts);
|
c11_vector__pop(&cpnts);
|
||||||
}
|
}
|
||||||
@ -98,7 +98,7 @@ int py_import(const char* path_cstr) {
|
|||||||
// join cpnts
|
// join cpnts
|
||||||
c11_sbuf buf;
|
c11_sbuf buf;
|
||||||
c11_sbuf__ctor(&buf);
|
c11_sbuf__ctor(&buf);
|
||||||
for(int i = 0; i < cpnts.count; i++) {
|
for(int i = 0; i < cpnts.length; i++) {
|
||||||
if(i > 0) c11_sbuf__write_char(&buf, '.');
|
if(i > 0) c11_sbuf__write_char(&buf, '.');
|
||||||
c11_sbuf__write_sv(&buf, c11__getitem(c11_sv, &cpnts, i));
|
c11_sbuf__write_sv(&buf, c11__getitem(c11_sv, &cpnts, i));
|
||||||
}
|
}
|
||||||
@ -616,9 +616,9 @@ static void function__gc_mark(void* ud) {
|
|||||||
static bool function__doc__(int argc, py_Ref argv) {
|
static bool function__doc__(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
Function* func = py_touserdata(py_arg(0));
|
Function* func = py_touserdata(py_arg(0));
|
||||||
if(func->decl->docstring){
|
if(func->decl->docstring) {
|
||||||
py_newstr(py_retval(), func->decl->docstring);
|
py_newstr(py_retval(), func->decl->docstring);
|
||||||
}else{
|
} else {
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -79,7 +79,7 @@ static void Dict__rehash_2x(Dict* self) {
|
|||||||
do {
|
do {
|
||||||
Dict__ctor(self, new_capacity);
|
Dict__ctor(self, new_capacity);
|
||||||
|
|
||||||
for(int i = 0; i < old_dict.entries.count; i++) {
|
for(int i = 0; i < old_dict.entries.length; i++) {
|
||||||
DictEntry* entry = c11__at(DictEntry, &old_dict.entries, i);
|
DictEntry* entry = c11__at(DictEntry, &old_dict.entries, i);
|
||||||
if(py_isnil(&entry->key)) continue;
|
if(py_isnil(&entry->key)) continue;
|
||||||
int idx = entry->hash & (new_capacity - 1);
|
int idx = entry->hash & (new_capacity - 1);
|
||||||
@ -89,7 +89,7 @@ static void Dict__rehash_2x(Dict* self) {
|
|||||||
if(idx2 == -1) {
|
if(idx2 == -1) {
|
||||||
// insert new entry (empty slot)
|
// insert new entry (empty slot)
|
||||||
c11_vector__push(DictEntry, &self->entries, *entry);
|
c11_vector__push(DictEntry, &self->entries, *entry);
|
||||||
self->indices[idx]._[i] = self->entries.count - 1;
|
self->indices[idx]._[i] = self->entries.length - 1;
|
||||||
self->length++;
|
self->length++;
|
||||||
success = true;
|
success = true;
|
||||||
break;
|
break;
|
||||||
@ -108,10 +108,10 @@ static void Dict__rehash_2x(Dict* self) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void Dict__compact_entries(Dict* self) {
|
static void Dict__compact_entries(Dict* self) {
|
||||||
int* mappings = malloc(self->entries.count * sizeof(int));
|
int* mappings = malloc(self->entries.length * sizeof(int));
|
||||||
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for(int i = 0; i < self->entries.count; i++) {
|
for(int i = 0; i < self->entries.length; i++) {
|
||||||
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
|
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
|
||||||
if(py_isnil(&entry->key)) continue;
|
if(py_isnil(&entry->key)) continue;
|
||||||
mappings[i] = n;
|
mappings[i] = n;
|
||||||
@ -121,7 +121,7 @@ static void Dict__compact_entries(Dict* self) {
|
|||||||
}
|
}
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
self->entries.count = n;
|
self->entries.length = n;
|
||||||
// update indices
|
// update indices
|
||||||
for(int i = 0; i < self->capacity; i++) {
|
for(int i = 0; i < self->capacity; i++) {
|
||||||
for(int j = 0; j < PK_DICT_MAX_COLLISION; j++) {
|
for(int j = 0; j < PK_DICT_MAX_COLLISION; j++) {
|
||||||
@ -145,7 +145,7 @@ static bool Dict__set(Dict* self, py_TValue* key, py_TValue* val) {
|
|||||||
new_entry->hash = hash;
|
new_entry->hash = hash;
|
||||||
new_entry->key = *key;
|
new_entry->key = *key;
|
||||||
new_entry->val = *val;
|
new_entry->val = *val;
|
||||||
self->indices[idx]._[i] = self->entries.count - 1;
|
self->indices[idx]._[i] = self->entries.length - 1;
|
||||||
self->length++;
|
self->length++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -179,7 +179,7 @@ static int Dict__pop(Dict* self, py_Ref key) {
|
|||||||
py_newnil(&entry->key);
|
py_newnil(&entry->key);
|
||||||
self->indices[idx]._[i] = -1;
|
self->indices[idx]._[i] = -1;
|
||||||
self->length--;
|
self->length--;
|
||||||
if(self->length < self->entries.count / 2) Dict__compact_entries(self);
|
if(self->length < self->entries.length / 2) Dict__compact_entries(self);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if(res == -1) return -1; // error
|
if(res == -1) return -1; // error
|
||||||
@ -189,7 +189,7 @@ static int Dict__pop(Dict* self, py_Ref key) {
|
|||||||
|
|
||||||
static void DictIterator__ctor(DictIterator* self, Dict* dict) {
|
static void DictIterator__ctor(DictIterator* self, Dict* dict) {
|
||||||
self->curr = dict->entries.data;
|
self->curr = dict->entries.data;
|
||||||
self->end = self->curr + dict->entries.count;
|
self->end = self->curr + dict->entries.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
static DictEntry* DictIterator__next(DictIterator* self) {
|
static DictEntry* DictIterator__next(DictIterator* self) {
|
||||||
@ -291,7 +291,7 @@ static bool dict__repr__(int argc, py_Ref argv) {
|
|||||||
c11_sbuf__ctor(&buf);
|
c11_sbuf__ctor(&buf);
|
||||||
c11_sbuf__write_char(&buf, '{');
|
c11_sbuf__write_char(&buf, '{');
|
||||||
bool is_first = true;
|
bool is_first = true;
|
||||||
for(int i = 0; i < self->entries.count; i++) {
|
for(int i = 0; i < self->entries.length; i++) {
|
||||||
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
|
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
|
||||||
if(py_isnil(&entry->key)) continue;
|
if(py_isnil(&entry->key)) continue;
|
||||||
if(!is_first) c11_sbuf__write_cstr(&buf, ", ");
|
if(!is_first) c11_sbuf__write_cstr(&buf, ", ");
|
||||||
@ -377,7 +377,7 @@ static bool dict_update(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(1, tp_dict);
|
PY_CHECK_ARG_TYPE(1, tp_dict);
|
||||||
Dict* self = py_touserdata(argv);
|
Dict* self = py_touserdata(argv);
|
||||||
Dict* other = py_touserdata(py_arg(1));
|
Dict* other = py_touserdata(py_arg(1));
|
||||||
for(int i = 0; i < other->entries.count; i++) {
|
for(int i = 0; i < other->entries.length; i++) {
|
||||||
DictEntry* entry = c11__at(DictEntry, &other->entries, i);
|
DictEntry* entry = c11__at(DictEntry, &other->entries, i);
|
||||||
if(py_isnil(&entry->key)) continue;
|
if(py_isnil(&entry->key)) continue;
|
||||||
if(!Dict__set(self, &entry->key, &entry->val)) return false;
|
if(!Dict__set(self, &entry->key, &entry->val)) return false;
|
||||||
@ -449,7 +449,7 @@ static bool dict_values(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
static void dict__gc_mark(void* ud) {
|
static void dict__gc_mark(void* ud) {
|
||||||
Dict* self = ud;
|
Dict* self = ud;
|
||||||
for(int i = 0; i < self->entries.count; i++) {
|
for(int i = 0; i < self->entries.length; i++) {
|
||||||
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
|
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
|
||||||
if(py_isnil(&entry->key)) continue;
|
if(py_isnil(&entry->key)) continue;
|
||||||
pk__mark_value(&entry->key);
|
pk__mark_value(&entry->key);
|
||||||
@ -550,7 +550,7 @@ int py_dict_len(py_Ref self) {
|
|||||||
|
|
||||||
bool py_dict_apply(py_Ref self, bool (*f)(py_Ref, py_Ref, void*), void* ctx) {
|
bool py_dict_apply(py_Ref self, bool (*f)(py_Ref, py_Ref, void*), void* ctx) {
|
||||||
Dict* ud = py_touserdata(self);
|
Dict* ud = py_touserdata(self);
|
||||||
for(int i = 0; i < ud->entries.count; i++) {
|
for(int i = 0; i < ud->entries.length; i++) {
|
||||||
DictEntry* entry = c11__at(DictEntry, &ud->entries, i);
|
DictEntry* entry = c11__at(DictEntry, &ud->entries, i);
|
||||||
if(py_isnil(&entry->key)) continue;
|
if(py_isnil(&entry->key)) continue;
|
||||||
if(!f(&entry->key, &entry->val, ctx)) return false;
|
if(!f(&entry->key, &entry->val, ctx)) return false;
|
||||||
|
@ -33,7 +33,7 @@ int py_BaseException__get_lineno(py_Ref self, const CodeObject* code) {
|
|||||||
|
|
||||||
void py_BaseException__stpush(py_Ref self, SourceData_ src, int lineno, const char* func_name) {
|
void py_BaseException__stpush(py_Ref self, SourceData_ src, int lineno, const char* func_name) {
|
||||||
BaseException* ud = py_touserdata(self);
|
BaseException* ud = py_touserdata(self);
|
||||||
if(ud->stacktrace.count >= 7) return;
|
if(ud->stacktrace.length >= 7) return;
|
||||||
BaseExceptionFrame* frame = c11_vector__emplace(&ud->stacktrace);
|
BaseExceptionFrame* frame = c11_vector__emplace(&ud->stacktrace);
|
||||||
PK_INCREF(src);
|
PK_INCREF(src);
|
||||||
frame->src = src;
|
frame->src = src;
|
||||||
@ -156,7 +156,7 @@ static void c11_sbuf__write_exc(c11_sbuf* self, py_Ref exc) {
|
|||||||
|
|
||||||
BaseException* ud = py_touserdata(exc);
|
BaseException* ud = py_touserdata(exc);
|
||||||
|
|
||||||
for(int i = ud->stacktrace.count - 1; i >= 0; i--) {
|
for(int i = ud->stacktrace.length - 1; i >= 0; i--) {
|
||||||
BaseExceptionFrame* frame = c11__at(BaseExceptionFrame, &ud->stacktrace, i);
|
BaseExceptionFrame* frame = c11__at(BaseExceptionFrame, &ud->stacktrace, i);
|
||||||
SourceData__snapshot(frame->src,
|
SourceData__snapshot(frame->src,
|
||||||
self,
|
self,
|
||||||
|
@ -16,7 +16,7 @@ void py_newlistn(py_Ref 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);
|
||||||
ud->count = n;
|
ud->length = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_list_data(py_Ref self) {
|
py_Ref py_list_data(py_Ref self) {
|
||||||
@ -41,7 +41,7 @@ void py_list_delitem(py_Ref self, int i) {
|
|||||||
|
|
||||||
int py_list_len(py_Ref self) {
|
int py_list_len(py_Ref self) {
|
||||||
List* ud = py_touserdata(self);
|
List* ud = py_touserdata(self);
|
||||||
return ud->count;
|
return ud->length;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list_swap(py_Ref self, int i, int j) {
|
void py_list_swap(py_Ref self, int i, int j) {
|
||||||
@ -137,12 +137,12 @@ static bool list__getitem__(int argc, py_Ref argv) {
|
|||||||
py_Ref _1 = py_arg(1);
|
py_Ref _1 = py_arg(1);
|
||||||
if(_1->type == tp_int) {
|
if(_1->type == tp_int) {
|
||||||
int index = py_toint(py_arg(1));
|
int index = py_toint(py_arg(1));
|
||||||
if(!pk__normalize_index(&index, self->count)) return false;
|
if(!pk__normalize_index(&index, self->length)) return false;
|
||||||
*py_retval() = c11__getitem(py_TValue, self, index);
|
*py_retval() = c11__getitem(py_TValue, self, index);
|
||||||
return true;
|
return true;
|
||||||
} else if(_1->type == tp_slice) {
|
} else if(_1->type == tp_slice) {
|
||||||
int start, stop, step;
|
int start, stop, step;
|
||||||
bool ok = pk__parse_int_slice(_1, self->count, &start, &stop, &step);
|
bool ok = pk__parse_int_slice(_1, self->length, &start, &stop, &step);
|
||||||
if(!ok) return false;
|
if(!ok) return false;
|
||||||
py_newlist(py_retval());
|
py_newlist(py_retval());
|
||||||
List* list = py_touserdata(py_retval());
|
List* list = py_touserdata(py_retval());
|
||||||
@ -160,7 +160,7 @@ static bool list__setitem__(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||||
List* self = py_touserdata(py_arg(0));
|
List* self = py_touserdata(py_arg(0));
|
||||||
int index = py_toint(py_arg(1));
|
int index = py_toint(py_arg(1));
|
||||||
if(!pk__normalize_index(&index, self->count)) return false;
|
if(!pk__normalize_index(&index, self->length)) return false;
|
||||||
c11__setitem(py_TValue, self, index, *py_arg(2));
|
c11__setitem(py_TValue, self, index, *py_arg(2));
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
return true;
|
return true;
|
||||||
@ -171,7 +171,7 @@ static bool list__delitem__(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||||
List* self = py_touserdata(py_arg(0));
|
List* self = py_touserdata(py_arg(0));
|
||||||
int index = py_toint(py_arg(1));
|
int index = py_toint(py_arg(1));
|
||||||
if(!pk__normalize_index(&index, self->count)) return false;
|
if(!pk__normalize_index(&index, self->length)) return false;
|
||||||
c11_vector__erase(py_TValue, self, index);
|
c11_vector__erase(py_TValue, self, index);
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
return true;
|
return true;
|
||||||
@ -186,8 +186,8 @@ static bool list__add__(int argc, py_Ref argv) {
|
|||||||
List* list_1 = py_touserdata(_1);
|
List* list_1 = py_touserdata(_1);
|
||||||
py_newlist(py_retval());
|
py_newlist(py_retval());
|
||||||
List* list = py_touserdata(py_retval());
|
List* list = py_touserdata(py_retval());
|
||||||
c11_vector__extend(py_TValue, list, list_0->data, list_0->count);
|
c11_vector__extend(py_TValue, list, list_0->data, list_0->length);
|
||||||
c11_vector__extend(py_TValue, list, list_1->data, list_1->count);
|
c11_vector__extend(py_TValue, list, list_1->data, list_1->length);
|
||||||
} else {
|
} else {
|
||||||
py_newnotimplemented(py_retval());
|
py_newnotimplemented(py_retval());
|
||||||
}
|
}
|
||||||
@ -204,7 +204,7 @@ static bool list__mul__(int argc, py_Ref argv) {
|
|||||||
List* list = py_touserdata(py_retval());
|
List* list = py_touserdata(py_retval());
|
||||||
List* list_0 = py_touserdata(_0);
|
List* list_0 = py_touserdata(_0);
|
||||||
for(int i = 0; i < n; i++) {
|
for(int i = 0; i < n; i++) {
|
||||||
c11_vector__extend(py_TValue, list, list_0->data, list_0->count);
|
c11_vector__extend(py_TValue, list, list_0->data, list_0->length);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
py_newnotimplemented(py_retval());
|
py_newnotimplemented(py_retval());
|
||||||
@ -226,7 +226,7 @@ static bool list__repr__(int argc, py_Ref argv) {
|
|||||||
c11_sbuf buf;
|
c11_sbuf buf;
|
||||||
c11_sbuf__ctor(&buf);
|
c11_sbuf__ctor(&buf);
|
||||||
c11_sbuf__write_char(&buf, '[');
|
c11_sbuf__write_char(&buf, '[');
|
||||||
for(int i = 0; i < self->count; i++) {
|
for(int i = 0; i < self->length; i++) {
|
||||||
py_TValue* val = c11__at(py_TValue, self, i);
|
py_TValue* val = c11__at(py_TValue, self, i);
|
||||||
bool ok = py_repr(val);
|
bool ok = py_repr(val);
|
||||||
if(!ok) {
|
if(!ok) {
|
||||||
@ -234,7 +234,7 @@ static bool list__repr__(int argc, py_Ref argv) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
c11_sbuf__write_sv(&buf, py_tosv(py_retval()));
|
c11_sbuf__write_sv(&buf, py_tosv(py_retval()));
|
||||||
if(i != self->count - 1) c11_sbuf__write_cstr(&buf, ", ");
|
if(i != self->length - 1) c11_sbuf__write_cstr(&buf, ", ");
|
||||||
}
|
}
|
||||||
c11_sbuf__write_char(&buf, ']');
|
c11_sbuf__write_char(&buf, ']');
|
||||||
c11_sbuf__py_submit(&buf, py_retval());
|
c11_sbuf__py_submit(&buf, py_retval());
|
||||||
@ -246,7 +246,7 @@ static bool list_extend(int argc, py_Ref argv) {
|
|||||||
List* self = py_touserdata(py_arg(0));
|
List* self = py_touserdata(py_arg(0));
|
||||||
PY_CHECK_ARG_TYPE(1, tp_list);
|
PY_CHECK_ARG_TYPE(1, tp_list);
|
||||||
List* other = py_touserdata(py_arg(1));
|
List* other = py_touserdata(py_arg(1));
|
||||||
c11_vector__extend(py_TValue, self, other->data, other->count);
|
c11_vector__extend(py_TValue, self, other->data, other->length);
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -275,7 +275,7 @@ static bool list_copy(int argc, py_Ref argv) {
|
|||||||
py_newlist(py_retval());
|
py_newlist(py_retval());
|
||||||
List* self = py_touserdata(py_arg(0));
|
List* self = py_touserdata(py_arg(0));
|
||||||
List* list = py_touserdata(py_retval());
|
List* list = py_touserdata(py_retval());
|
||||||
c11_vector__extend(py_TValue, list, self->data, self->count);
|
c11_vector__extend(py_TValue, list, self->data, self->length);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,8 +330,8 @@ static bool list_pop(int argc, py_Ref argv) {
|
|||||||
return TypeError("pop() takes at most 2 arguments");
|
return TypeError("pop() takes at most 2 arguments");
|
||||||
}
|
}
|
||||||
List* self = py_touserdata(py_arg(0));
|
List* self = py_touserdata(py_arg(0));
|
||||||
if(self->count == 0) return IndexError("pop from empty list");
|
if(self->length == 0) return IndexError("pop from empty list");
|
||||||
if(!pk__normalize_index(&index, self->count)) return false;
|
if(!pk__normalize_index(&index, self->length)) return false;
|
||||||
*py_retval() = c11__getitem(py_TValue, self, index);
|
*py_retval() = c11__getitem(py_TValue, self, index);
|
||||||
c11_vector__erase(py_TValue, self, index);
|
c11_vector__erase(py_TValue, self, index);
|
||||||
return true;
|
return true;
|
||||||
@ -342,9 +342,9 @@ static bool list_insert(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||||
List* self = py_touserdata(py_arg(0));
|
List* self = py_touserdata(py_arg(0));
|
||||||
int index = py_toint(py_arg(1));
|
int index = py_toint(py_arg(1));
|
||||||
if(index < 0) index += self->count;
|
if(index < 0) index += self->length;
|
||||||
if(index < 0) index = 0;
|
if(index < 0) index = 0;
|
||||||
if(index > self->count) index = self->count;
|
if(index > self->length) index = self->length;
|
||||||
c11_vector__insert(py_TValue, self, index, *py_arg(2));
|
c11_vector__insert(py_TValue, self, index, *py_arg(2));
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
return true;
|
return true;
|
||||||
@ -380,7 +380,7 @@ static bool list_sort(int argc, py_Ref argv) {
|
|||||||
if(py_isnone(key)) key = NULL;
|
if(py_isnone(key)) key = NULL;
|
||||||
|
|
||||||
bool ok = c11__stable_sort(self->data,
|
bool ok = c11__stable_sort(self->data,
|
||||||
self->count,
|
self->length,
|
||||||
sizeof(py_TValue),
|
sizeof(py_TValue),
|
||||||
(int (*)(const void*, const void*, void*))lt_with_key,
|
(int (*)(const void*, const void*, void*))lt_with_key,
|
||||||
key);
|
key);
|
||||||
@ -405,7 +405,7 @@ static bool list__contains__(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
static void list__gc_mark(void* ud) {
|
static void list__gc_mark(void* ud) {
|
||||||
List* self = ud;
|
List* self = ud;
|
||||||
for(int i = 0; i < self->count; i++) {
|
for(int i = 0; i < self->length; i++) {
|
||||||
pk__mark_value(c11__at(py_TValue, self, i));
|
pk__mark_value(c11__at(py_TValue, self, i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,8 +52,8 @@ static bool namedict_items(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
py_Ref object = py_getslot(argv, 0);
|
py_Ref object = py_getslot(argv, 0);
|
||||||
NameDict* dict = PyObject__dict(object->_obj);
|
NameDict* dict = PyObject__dict(object->_obj);
|
||||||
py_newtuple(py_retval(), dict->count);
|
py_newtuple(py_retval(), dict->length);
|
||||||
for(int i = 0; i < dict->count; i++) {
|
for(int i = 0; i < dict->length; i++) {
|
||||||
py_Ref slot = py_tuple_getitem(py_retval(), i);
|
py_Ref slot = py_tuple_getitem(py_retval(), i);
|
||||||
py_newtuple(slot, 2);
|
py_newtuple(slot, 2);
|
||||||
NameDict_KV* kv = c11__at(NameDict_KV, dict, i);
|
NameDict_KV* kv = c11__at(NameDict_KV, dict, i);
|
||||||
|
@ -318,8 +318,8 @@ static bool str_split(int argc, py_Ref argv) {
|
|||||||
c11_sv sep = c11_string__sv(py_touserdata(&argv[1]));
|
c11_sv sep = c11_string__sv(py_touserdata(&argv[1]));
|
||||||
res = c11_sv__split2(self, sep);
|
res = c11_sv__split2(self, sep);
|
||||||
}
|
}
|
||||||
py_newlistn(py_retval(), res.count);
|
py_newlistn(py_retval(), res.length);
|
||||||
for(int i = 0; i < res.count; i++) {
|
for(int i = 0; i < res.length; i++) {
|
||||||
c11_sv item = c11__getitem(c11_sv, &res, i);
|
c11_sv item = c11__getitem(c11_sv, &res, i);
|
||||||
py_newstrn(py_list_getitem(py_retval(), i), item.data, item.size);
|
py_newstrn(py_list_getitem(py_retval(), i), item.data, item.size);
|
||||||
}
|
}
|
||||||
@ -563,8 +563,8 @@ static bool bytes__getitem__(int argc, py_Ref argv) {
|
|||||||
for(int i = start; step > 0 ? i < stop : i > stop; i += step) {
|
for(int i = start; step > 0 ? i < stop : i > stop; i += step) {
|
||||||
c11_vector__push(unsigned char, &res, data[i]);
|
c11_vector__push(unsigned char, &res, data[i]);
|
||||||
}
|
}
|
||||||
unsigned char* p = py_newbytes(py_retval(), res.count);
|
unsigned char* p = py_newbytes(py_retval(), res.length);
|
||||||
memcpy(p, res.data, res.count);
|
memcpy(p, res.data, res.length);
|
||||||
c11_vector__dtor(&res);
|
c11_vector__dtor(&res);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -86,7 +86,7 @@ py_Name
|
|||||||
CodeObject code;
|
CodeObject code;
|
||||||
SourceData_ source = SourceData__rcnew(buffer, "<bind>", EXEC_MODE, false);
|
SourceData_ source = SourceData__rcnew(buffer, "<bind>", EXEC_MODE, false);
|
||||||
Error* err = pk_compile(source, &code);
|
Error* err = pk_compile(source, &code);
|
||||||
if(err || code.func_decls.count != 1) {
|
if(err || code.func_decls.length != 1) {
|
||||||
c11__abort("py_newfunction(): invalid signature '%s'", sig);
|
c11__abort("py_newfunction(): invalid signature '%s'", sig);
|
||||||
}
|
}
|
||||||
FuncDecl_ decl = c11__getitem(FuncDecl_, &code.func_decls, 0);
|
FuncDecl_ decl = c11__getitem(FuncDecl_, &code.func_decls, 0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user