This commit is contained in:
blueloveTH 2024-08-15 01:01:28 +08:00
parent 976f809f80
commit 92090aeaa1
21 changed files with 226 additions and 235 deletions

View File

@ -9,17 +9,17 @@
typedef struct c11_array {
void* data;
int count;
int length;
int elem_size;
} 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);
c11_array c11_array__copy(const c11_array* self);
typedef struct c11_vector {
void* data;
int count;
int length;
int capacity;
int elem_size;
} c11_vector;
@ -39,43 +39,43 @@ c11_array c11_vector__submit(c11_vector* self);
#define c11_vector__push(T, self, elem) \
do { \
if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \
((T*)(self)->data)[(self)->count] = (elem); \
(self)->count++; \
if((self)->length == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \
((T*)(self)->data)[(self)->length] = (elem); \
(self)->length++; \
} 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) \
do { \
c11_vector__reserve((self), (self)->count + (size)); \
memcpy((T*)(self)->data + (self)->count, (p), (size) * sizeof(T)); \
(self)->count += (size); \
c11_vector__reserve((self), (self)->length + (size)); \
memcpy((T*)(self)->data + (self)->length, (p), (size) * sizeof(T)); \
(self)->length += (size); \
} while(0)
#define c11_vector__insert(T, self, index, elem) \
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); \
memmove(p + 1, p, ((self)->count - (index)) * sizeof(T)); \
memmove(p + 1, p, ((self)->length - (index)) * sizeof(T)); \
*p = (elem); \
(self)->count++; \
(self)->length++; \
} while(0)
#define c11_vector__erase(T, self, index) \
do { \
T* p = (T*)(self)->data + (index); \
memmove(p, p + 1, ((self)->count - (index)-1) * sizeof(T)); \
(self)->count--; \
memmove(p, p + 1, ((self)->length - (index)-1) * sizeof(T)); \
(self)->length--; \
} while(0)
#define c11__reverse(T, self) \
do { \
if(!self->data) break; \
T* p = (T*)(self)->data; \
T* q = (T*)(self)->data + (self)->count - 1; \
T* q = (T*)(self)->data + (self)->length - 1; \
while(p < q) { \
T tmp = *p; \
*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
#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++)

View File

@ -1,27 +1,27 @@
#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__SOURCE
/* Input */
#define K int
#define V float
#define NAME c11_smallmap_i2f
#define SMALLMAP_T__HEADER
#define SMALLMAP_T__SOURCE
/* Input */
#define K int
#define V float
#define NAME c11_smallmap_i2f
#endif
/* Optional Input */
#ifndef less
#define less(a, b) ((a) < (b))
#define less(a, b) ((a) < (b))
#endif
#ifndef equal
#define equal(a, b) ((a) == (b))
#define equal(a, b) ((a) == (b))
#endif
/* Temprary macros */
#define partial_less(a, b) less((a).key, (b))
#define CONCAT(A, B) CONCAT_(A, B)
#define CONCAT_(A, B) A##B
#define partial_less(a, b) less((a).key, (b))
#define CONCAT(A, B) CONCAT_(A, B)
#define CONCAT_(A, B) A##B
#define KV CONCAT(NAME, _KV)
#define METHOD(name) CONCAT(NAME, CONCAT(__, name))
@ -56,9 +56,7 @@ void METHOD(ctor)(NAME* self) {
c11_vector__reserve(self, 4);
}
void METHOD(dtor)(NAME* self) {
c11_vector__dtor(self);
}
void METHOD(dtor)(NAME* self) { c11_vector__dtor(self); }
NAME* METHOD(new)() {
NAME* self = malloc(sizeof(NAME));
@ -73,10 +71,10 @@ void METHOD(delete)(NAME* self) {
void METHOD(set)(NAME* self, K key, V value) {
int index;
c11__lower_bound(KV, self->data, self->count, key, partial_less, &index);
if(index != self->count){
c11__lower_bound(KV, self->data, self->length, key, partial_less, &index);
if(index != self->length) {
KV* it = c11__at(KV, self, index);
if(equal(it->key, key)){
if(equal(it->key, key)) {
it->value = value;
return;
}
@ -87,8 +85,8 @@ void METHOD(set)(NAME* self, K key, V value) {
V* METHOD(try_get)(const NAME* self, K key) {
int index;
c11__lower_bound(KV, self->data, self->count, key, partial_less, &index);
if(index != self->count){
c11__lower_bound(KV, self->data, self->length, key, partial_less, &index);
if(index != self->length) {
KV* it = c11__at(KV, self, index);
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;
}
bool METHOD(contains)(const NAME* self, K key) {
return METHOD(try_get)(self, key) != NULL;
}
bool METHOD(contains)(const NAME* self, K key) { return METHOD(try_get)(self, key) != NULL; }
bool METHOD(del)(NAME* self, K key) {
int index;
c11__lower_bound(KV, self->data, self->count, key, partial_less, &index);
if(index != self->count){
c11__lower_bound(KV, self->data, self->length, key, partial_less, &index);
if(index != self->length) {
KV* it = c11__at(KV, self, index);
if(equal(it->key, key)){
if(equal(it->key, key)) {
c11_vector__erase(KV, self, index);
return true;
}
@ -117,9 +113,7 @@ bool METHOD(del)(NAME* self, K key) {
return false;
}
void METHOD(clear)(NAME* self) {
c11_vector__clear(self);
}
void METHOD(clear)(NAME* self) { c11_vector__clear(self); }
#endif

View File

@ -13,13 +13,13 @@ typedef struct LinkedListNode {
} LinkedListNode;
typedef struct LinkedList {
int count;
int length;
LinkedListNode head;
LinkedListNode tail;
} LinkedList;
static void LinkedList__ctor(LinkedList* self) {
self->count = 0;
self->length = 0;
self->head.prev = NULL;
self->head.next = &self->tail;
self->tail.prev = &self->head;
@ -31,7 +31,7 @@ static void LinkedList__push_back(LinkedList* self, LinkedListNode* node) {
node->next = &self->tail;
self->tail.prev->next = node;
self->tail.prev = node;
self->count++;
self->length++;
}
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;
self->head.next->prev = node;
self->head.next = node;
self->count++;
self->length++;
}
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 = self->tail.prev->prev;
self->count--;
self->length--;
}
static LinkedListNode* LinkedList__back(LinkedList* self) {
assert(self->count > 0);
assert(self->length > 0);
return self->tail.prev;
}
static void LinkedList__erase(LinkedList* self, LinkedListNode* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
self->count--;
self->length--;
}
#define LinkedList__apply(self, __STATEMENTS__) \
@ -135,7 +135,7 @@ static void MemoryPool__ctor(MemoryPool* self) {
static void* MemoryPool__alloc(MemoryPool* self) {
MemoryPoolArena* arena;
if(self->_arenas.count == 0){
if(self->_arenas.length == 0){
arena = malloc(sizeof(MemoryPoolArena));
MemoryPoolArena__ctor(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) {
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,
MemoryPoolArena* arena = (MemoryPoolArena*)node;
if(MemoryPoolArena__full(arena)) {
@ -301,8 +301,8 @@ void Pools_debug_info(char* buffer, int size) {
);
buffer += n; size -= n;
// PoolObject
int empty_arenas = PoolObject._empty_arenas.count;
int arenas = PoolObject._arenas.count;
int empty_arenas = PoolObject._empty_arenas.length;
int arenas = PoolObject._arenas.length;
// print empty arenas count
n = snprintf(
buffer, size, "PoolObject: %d empty arenas, %d arenas\n",

View File

@ -13,7 +13,7 @@
void c11_sbuf__ctor(c11_sbuf* self) {
c11_vector__ctor(&self->data, sizeof(char));
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); }
@ -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) {
// len('-2147483648') == 11
c11_vector__reserve(&self->data, self->data.count + 11 + 1);
char* p = (char*)self->data.data + self->data.count;
c11_vector__reserve(&self->data, self->data.length + 11 + 1);
char* p = (char*)self->data.data + self->data.length;
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) {
// len('-9223372036854775808') == 20
c11_vector__reserve(&self->data, self->data.count + 20 + 1);
char* p = (char*)self->data.data + self->data.count;
c11_vector__reserve(&self->data, self->data.length + 20 + 1);
char* p = (char*)self->data.data + self->data.length;
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) {
@ -143,7 +143,7 @@ c11_string* c11_sbuf__submit(c11_sbuf* self) {
c11_vector__push(char, &self->data, '\0');
c11_array arr = c11_vector__submit(&self->data);
c11_string* retval = arr.data;
retval->size = arr.count - sizeof(c11_string) - 1;
retval->size = arr.length - sizeof(c11_string) - 1;
return retval;
}

View File

@ -12,19 +12,20 @@ static c11_vector /*T=char* */ _r_interned;
void py_Name__initialize() {
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));
}
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"
#undef MAGIC_METHOD
}
void py_Name__finalize() {
// 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));
}
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);
if(index != 0) return 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
char* p = malloc(name.size + 1);
memcpy(p, name.data, name.size);
p[name.size] = '\0';
c11_vector__push(char*, &_r_interned, p);
index = _r_interned.count; // 1-based
index = _r_interned.length; // 1-based
// save to _interned
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;
}
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);
}
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);
return (c11_sv){p, strlen(p)};
}

View File

@ -3,28 +3,28 @@
#include <stdlib.h>
#include <string.h>
void c11_array__ctor(c11_array* self, int elem_size, int count){
self->data = malloc(elem_size * count);
self->count = count;
void c11_array__ctor(c11_array* self, int elem_size, int length){
self->data = malloc(elem_size * length);
self->length = length;
self->elem_size = elem_size;
}
void c11_array__dtor(c11_array* self){
free(self->data);
self->data = NULL;
self->count = 0;
self->length = 0;
}
c11_array c11_array__copy(const c11_array* self){
c11_array retval;
c11_array__ctor(&retval, self->elem_size, self->count);
memcpy(retval.data, self->data, self->elem_size * self->count);
c11_array__ctor(&retval, self->elem_size, self->length);
memcpy(retval.data, self->data, self->elem_size * self->length);
return retval;
}
void c11_vector__ctor(c11_vector* self, int elem_size){
self->data = NULL;
self->count = 0;
self->length = 0;
self->capacity = 0;
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){
if(self->data) free(self->data);
self->data = NULL;
self->count = 0;
self->length = 0;
self->capacity = 0;
}
@ -40,8 +40,8 @@ c11_vector c11_vector__copy(const c11_vector* self){
c11_vector retval;
c11_vector__ctor(&retval, self->elem_size);
c11_vector__reserve(&retval, self->capacity);
memcpy(retval.data, self->data, self->elem_size * self->count);
retval.count = self->count;
memcpy(retval.data, self->data, self->elem_size * self->length);
retval.length = self->length;
return retval;
}
@ -53,18 +53,18 @@ void c11_vector__reserve(c11_vector* self, int capacity){
}
void c11_vector__clear(c11_vector* self){
self->count = 0;
self->length = 0;
}
void* c11_vector__emplace(c11_vector* self){
if(self->count == self->capacity) c11_vector__reserve(self, self->capacity*2);
void* p = (char*)self->data + self->elem_size * self->count;
self->count++;
if(self->length == self->capacity) c11_vector__reserve(self, self->capacity*2);
void* p = (char*)self->data + self->elem_size * self->length;
self->length++;
return p;
}
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;
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 retval = {
.data = self->data,
.count = self->count,
.length = self->length,
.elem_size = self->elem_size
};
self->data = NULL;
self->count = 0;
self->length = 0;
self->capacity = 0;
return retval;
}

View File

@ -454,11 +454,11 @@ typedef struct SequenceExpr {
static void SequenceExpr__emit_(Expr* self_, Ctx* ctx) {
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);
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_) {
@ -472,7 +472,7 @@ bool TupleExpr__emit_store(Expr* self_, Ctx* ctx) {
// TOS is an iterable
// items may contain StarredExpr, we should check it
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);
if(e->vt->is_starred) {
if(((StarredExpr*)e)->level > 0) {
@ -485,24 +485,24 @@ bool TupleExpr__emit_store(Expr* self_, Ctx* ctx) {
}
if(starred_i == -1) {
Bytecode* prev = c11__at(Bytecode, &ctx->co->codes, ctx->co->codes.count - 1);
if(prev->op == OP_BUILD_TUPLE && prev->arg == self->items.count) {
Bytecode* prev = c11__at(Bytecode, &ctx->co->codes, ctx->co->codes.length - 1);
if(prev->op == OP_BUILD_TUPLE && prev->arg == self->items.length) {
// build tuple and unpack it is meaningless
Ctx__revert_last_emit_(ctx);
} else {
Ctx__emit_(ctx, OP_UNPACK_SEQUENCE, self->items.count, self->line);
Ctx__emit_(ctx, OP_UNPACK_SEQUENCE, self->items.length, self->line);
}
} else {
// 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)
if(starred_i != self->items.count - 1) return false;
if(starred_i != self->items.length - 1) return false;
// a,*b = [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
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);
bool ok = vtemit_store(e, ctx);
if(!ok) return false;
@ -822,7 +822,7 @@ static void BinaryExpr__emit_(Expr* self_, Ctx* ctx) {
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));
}
c11_vector__dtor(&jmps);
@ -1069,8 +1069,8 @@ void CallExpr__emit_(Expr* self_, Ctx* ctx) {
Ctx__emit_int(ctx, e->key, self->line);
vtemit_(e->val, ctx);
}
int KWARGC = self->kwargs.count;
int ARGC = self->args.count;
int KWARGC = self->kwargs.length;
int ARGC = self->args.length;
assert(KWARGC < 256 && ARGC < 256);
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) {
// 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));
}
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) {
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);
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);
}
static void Ctx__exit_block(Ctx* self) {
CodeBlock* block = c11__at(CodeBlock, &self->co->blocks, self->curr_iblock);
CodeBlockType curr_type = block->type;
block->end = self->co->codes.count;
block->end = self->co->codes.length;
self->curr_iblock = block->parent;
assert(self->curr_iblock >= 0);
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};
c11_vector__push(Bytecode, &self->co->codes, bc);
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;
if(line == BC_KEEPLINE) { codes_ex[i].lineno = i >= 1 ? codes_ex[i - 1].lineno : 1; }
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) {
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);
}
static bool Ctx__add_label(Ctx* self, py_Name name) {
bool ok = c11_smallmap_n2i__contains(&self->co->labels, name);
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;
}
@ -1212,7 +1212,7 @@ static int Ctx__add_const_string(Ctx* self, c11_sv key) {
py_TValue tmp;
py_newstrn(&tmp, key.data, key.size);
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_string__sv(PyObject__userdata(tmp._obj)),
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) {
assert(v->type != tp_str);
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) {
@ -1237,7 +1237,7 @@ static void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int l
// emit top -> pop -> delete
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);
vtemit_(top, self);
vtdelete(top);
@ -1249,16 +1249,16 @@ static void Ctx__s_push(Ctx* self, Expr* expr) { c11_vector__push(Expr*, &self->
// top
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);
}
// 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
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);
vtdelete(top);
c11_vector__pop(&self->s_expr);
@ -1266,7 +1266,7 @@ static void Ctx__s_pop(Ctx* self) {
// pop move
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);
c11_vector__pop(&self->s_expr);
return top;
@ -1329,7 +1329,7 @@ static void Compiler__dtor(Compiler* self) {
if((err = B)) return err
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;
return s;
}
@ -1338,7 +1338,7 @@ Error* SyntaxError(Compiler* self, const char* fmt, ...) {
Error* err = malloc(sizeof(Error));
err->src = 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;
va_list args;
va_start(args, fmt);
@ -1451,7 +1451,7 @@ static Error* EXPR_VARS(Compiler* self) {
static void push_global_context(Compiler* self, CodeObject* co) {
co->start_line = self->i == 0 ? 1 : prev()->line;
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) {
@ -1473,11 +1473,11 @@ static Error* pop_context(Compiler* self) {
if(co->nlocals > PK_MAX_CO_VARNAMES) {
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");
}
// 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);
if(bc->op == OP_LOOP_CONTINUE) {
CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
@ -1492,7 +1492,7 @@ static Error* pop_context(Compiler* self) {
if(func) {
// check generator
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++) {
if(codes[i].op == OP_YIELD_VALUE) {
@ -1510,7 +1510,7 @@ static Error* pop_context(Compiler* self) {
}
if(func->type == FuncType_UNSET) {
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_kwarg >= 0) is_simple = false;
@ -1855,7 +1855,7 @@ static Error* exprCall(Compiler* self) {
c11_vector__push(CallExprKwArg, &e->kwargs, kw);
} else {
// positional argument
if(e->kwargs.count > 0) {
if(e->kwargs.length > 0) {
return SyntaxError(self, "positional argument follows keyword argument");
}
c11_vector__push(Expr*, &e->args, Ctx__s_popx(ctx()));
@ -1994,7 +1994,7 @@ static Error* compile_while_loop(Compiler* self) {
// optional else clause
if(match(TK_ELSE)) {
check(compile_block_body(self, compile_stmt));
block->end2 = ctx()->co->codes.count;
block->end2 = ctx()->co->codes.length;
}
return NULL;
}
@ -2021,7 +2021,7 @@ static Error* compile_for_loop(Compiler* self) {
// optional else clause
if(match(TK_ELSE)) {
check(compile_block_body(self, compile_stmt));
block->end2 = ctx()->co->codes.count;
block->end2 = ctx()->co->codes.length;
}
return NULL;
}
@ -2097,10 +2097,10 @@ static FuncDecl_ push_f_context(Compiler* self, c11_sv name, int* out_index) {
// add_func_decl
Ctx* top_ctx = ctx();
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
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;
}
@ -2214,7 +2214,7 @@ static Error* compile_function(Compiler* self, int decorators) {
check(compile_block_body(self, compile_stmt));
check(pop_context(self));
if(decl->code.codes.count >= 2) {
if(decl->code.codes.length >= 2) {
Bytecode* codes = (Bytecode*)decl->code.codes.data;
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();
break;
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));
Ctx__s_emit_top(ctx());
Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line);
consume_end_stmt();
break;
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));
Ctx__s_emit_top(ctx());
Ctx__emit_(ctx(), OP_GET_ITER, BC_NOARG, kw_line);
@ -2482,7 +2483,7 @@ static Error* compile_stmt(Compiler* self) {
consume_end_stmt();
break;
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)) {
Ctx__emit_(ctx(), OP_RETURN_VALUE, 1, kw_line);
} else {
@ -2672,7 +2673,7 @@ Error* pk_compile(SourceData_ src, CodeObject* out) {
#if 0
Token* data = (Token*)tokens.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;
c11_string* tmp = c11_string__new2(t->start, t->length);
if(t->value.index == TokenValue_STR) {

View File

@ -125,7 +125,7 @@ static void add_token_with_value(Lexer* self, TokenIndex type, TokenValue value)
self->brackets_level,
value};
// 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);
if(back->type == TK_NOT_KW && type == TK_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
bool eof = false;
int token_count = self->nexts.count;
int token_count = self->nexts.length;
while(!eof) {
Error* err = lex_one_token(self, &eof, true);
if(err) return err;
}
if(self->nexts.count == token_count) {
if(self->nexts.length == token_count) {
// f'{}' is 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 {
return SyntaxError(self, "f-string: single '}' is not allowed");
}
}else{
} else {
c11_sbuf__write_char(buff, c);
}
} 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");
self->token_start = self->curr_char;
while(self->indents.count > 1) {
while(self->indents.length > 1) {
c11_vector__pop(&self->indents);
add_token(self, TK_DEDENT);
return NULL;
@ -637,7 +637,7 @@ Error* Lexer__process(SourceData_ src, TokenArray* out_tokens) {
void TokenArray__dtor(TokenArray* self) {
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); }
}
c11_array__dtor(self);

View File

@ -844,7 +844,7 @@ FrameResult VM__run_top_frame(VM* self) {
}
}
} 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);
if(!kv->key) continue;
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();
if(spec.size == 0) return py_str(val);
if(spec.data[0] == '!'){
if(c11_sv__startswith(spec, (c11_sv){"!r", 2})){
if(spec.data[0] == '!') {
if(c11_sv__startswith(spec, (c11_sv){"!r", 2})) {
spec.data += 2;
spec.size -= 2;
if(!py_repr(val)) return false;
py_assign(val, py_retval());
if(spec.size == 0) return true;
}else{
} else {
return ValueError("invalid conversion specifier (only !r is supported)");
}
}
assert(spec.size > 0);
if(spec.data[0] == ':'){
if(spec.data[0] == ':') {
spec.data++;
spec.size--;
}

View File

@ -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) {
int iblock = Frame__iblock(self);
if(target >= self->co->codes.count) {
if(target >= self->co->codes.length) {
while(iblock >= 0)
iblock = Frame__exit_block(self, _s, iblock);
} else {

View File

@ -2,7 +2,7 @@
#include "pocketpy/common/memorypool.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->gen, sizeof(PyObject*));
@ -13,12 +13,12 @@ void ManagedHeap__ctor(ManagedHeap *self, VM *vm){
self->gc_on_delete = NULL;
}
void ManagedHeap__dtor(ManagedHeap *self){
for(int i = 0; i < self->gen.count; i++){
void ManagedHeap__dtor(ManagedHeap* self) {
for(int i = 0; i < self->gen.length; i++) {
PyObject* obj = c11__getitem(PyObject*, &self->gen, i);
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__delete(obj);
}
@ -26,47 +26,43 @@ void ManagedHeap__dtor(ManagedHeap *self){
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;
self->gc_counter = 0;
ManagedHeap__collect(self);
self->gc_threshold = self->gen.count * 2;
if(self->gc_threshold < PK_GC_MIN_THRESHOLD){
self->gc_threshold = PK_GC_MIN_THRESHOLD;
}
self->gc_threshold = self->gen.length * 2;
if(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);
int freed = ManagedHeap__sweep(self);
return freed;
}
int ManagedHeap__sweep(ManagedHeap *self){
int ManagedHeap__sweep(ManagedHeap* self) {
c11_vector alive;
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);
if(obj->gc_marked) {
obj->gc_marked = false;
c11_vector__push(PyObject*, &alive, obj);
} else {
if(self->gc_on_delete){
self->gc_on_delete(self->vm, obj);
}
if(self->gc_on_delete) { self->gc_on_delete(self->vm, obj); }
PyObject__delete(obj);
}
}
// 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);
obj->gc_marked = false;
}
int freed = self->gen.count - alive.count;
int freed = self->gen.length - alive.length;
// destroy old gen
c11_vector__dtor(&self->gen);
@ -77,28 +73,28 @@ int ManagedHeap__sweep(ManagedHeap *self){
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);
c11_vector__push(PyObject*, &self->no_gc, 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);
c11_vector__push(PyObject*, &self->gen, obj);
self->gc_counter++;
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);
PyObject* self;
// header + slots + udsize
size = sizeof(PyObject) + PK_OBJ_SLOTS_SIZE(slots) + size;
if(size <= kPoolObjectBlockSize){
if(size <= kPoolObjectBlockSize) {
self = PoolObject_alloc();
self->gc_is_large = false;
}else{
} else {
self = malloc(size);
self->gc_is_large = true;
}
@ -108,9 +104,9 @@ PyObject* PyObject__new(py_Type type, int slots, int size){
// initialize slots or dict
void* p = (char*)self + 8;
if(slots >= 0){
memset(p, 0, slots*sizeof(py_TValue));
}else{
if(slots >= 0) {
memset(p, 0, slots * sizeof(py_TValue));
} else {
NameDict__ctor(p);
}
return self;

View File

@ -317,7 +317,7 @@ py_Type pk_newtype(const char* name,
bool is_python,
bool is_sealed) {
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* base_ti = base ? c11__at(py_TypeInfo, types, base) : NULL;
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
prepare_py_call(py_TValue* buffer, py_Ref argv, py_Ref p1, int kwargc, const FuncDecl* decl) {
const CodeObject* co = &decl->code;
int decl_argc = decl->args.count;
int decl_argc = decl->args.length;
if(p1 - argv < decl_argc) {
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:
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";
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;
}
if(kwargc) {
@ -552,7 +552,7 @@ void pk__mark_value(py_TValue* val) {
}
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);
pk__mark_value(&kv->value);
}
@ -589,7 +589,7 @@ void CodeObject__gc_mark(const CodeObject* self) {
void ManagedHeap__mark(ManagedHeap* self) {
VM* vm = self->vm;
// 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);
mark_object(obj);
}
@ -599,7 +599,7 @@ void ManagedHeap__mark(ManagedHeap* self) {
}
// mark magic slots
py_TypeInfo* types = vm->types.data;
int types_length = vm->types.count;
int types_length = vm->types.length;
// 0-th type is placeholder
for(int i = 1; i < types_length; i++) {
for(int j = 0; j <= __missing__; j++) {

View File

@ -8,7 +8,7 @@
static void disassemble(CodeObject* co) {
c11_vector /*T=int*/ jumpTargets;
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);
if(Bytecode__is_forward_jump(bc)) {
int target = (int16_t)bc->arg + i;
@ -20,7 +20,7 @@ static void disassemble(CodeObject* co) {
c11_sbuf__ctor(&ss);
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);
BytecodeEx ex = c11__getitem(BytecodeEx, &co->codes_ex, i);
@ -101,7 +101,7 @@ static void disassemble(CodeObject* co) {
}
} 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);
@ -115,12 +115,12 @@ static bool dis_dis(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
CodeObject* code = NULL;
if(py_istype(argv, tp_function)){
if(py_istype(argv, tp_function)) {
Function* ud = py_touserdata(argv);
code = &ud->decl->code;
}else if(py_istype(argv, tp_code)){
} else if(py_istype(argv, tp_code)) {
code = py_touserdata(argv);
}else{
} else {
return TypeError("dis() expected a code object");
}
disassemble(code);

View File

@ -152,7 +152,7 @@ void CodeObject__dtor(CodeObject* self) {
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);
PK_DECREF(decl);
}
@ -173,7 +173,7 @@ int CodeObject__add_varname(CodeObject* self, py_Name name) {
if(index >= 0) return index;
c11_vector__push(uint16_t, &self->varnames, name);
self->nlocals++;
index = self->varnames.count - 1;
index = self->varnames.length - 1;
c11_smallmap_n2i__set(&self->varnames_inv, name, index);
return index;
}

View File

@ -85,7 +85,7 @@ int py_import(const char* path_cstr) {
c11_vector /* T=c11_sv */ cpnts = c11_sv__split(package_sv, '.');
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");
c11_vector__pop(&cpnts);
}
@ -98,7 +98,7 @@ int py_import(const char* path_cstr) {
// join cpnts
c11_sbuf 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, '.');
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) {
PY_CHECK_ARGC(1);
Function* func = py_touserdata(py_arg(0));
if(func->decl->docstring){
if(func->decl->docstring) {
py_newstr(py_retval(), func->decl->docstring);
}else{
} else {
py_newnone(py_retval());
}
return true;

View File

@ -79,7 +79,7 @@ static void Dict__rehash_2x(Dict* self) {
do {
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);
if(py_isnil(&entry->key)) continue;
int idx = entry->hash & (new_capacity - 1);
@ -89,7 +89,7 @@ static void Dict__rehash_2x(Dict* self) {
if(idx2 == -1) {
// insert new entry (empty slot)
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++;
success = true;
break;
@ -108,10 +108,10 @@ static void Dict__rehash_2x(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;
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);
if(py_isnil(&entry->key)) continue;
mappings[i] = n;
@ -121,7 +121,7 @@ static void Dict__compact_entries(Dict* self) {
}
n++;
}
self->entries.count = n;
self->entries.length = n;
// update indices
for(int i = 0; i < self->capacity; i++) {
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->key = *key;
new_entry->val = *val;
self->indices[idx]._[i] = self->entries.count - 1;
self->indices[idx]._[i] = self->entries.length - 1;
self->length++;
return true;
}
@ -179,7 +179,7 @@ static int Dict__pop(Dict* self, py_Ref key) {
py_newnil(&entry->key);
self->indices[idx]._[i] = -1;
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;
}
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) {
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) {
@ -291,7 +291,7 @@ static bool dict__repr__(int argc, py_Ref argv) {
c11_sbuf__ctor(&buf);
c11_sbuf__write_char(&buf, '{');
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);
if(py_isnil(&entry->key)) continue;
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);
Dict* self = py_touserdata(argv);
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);
if(py_isnil(&entry->key)) continue;
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) {
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);
if(py_isnil(&entry->key)) continue;
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) {
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);
if(py_isnil(&entry->key)) continue;
if(!f(&entry->key, &entry->val, ctx)) return false;

View File

@ -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) {
BaseException* ud = py_touserdata(self);
if(ud->stacktrace.count >= 7) return;
if(ud->stacktrace.length >= 7) return;
BaseExceptionFrame* frame = c11_vector__emplace(&ud->stacktrace);
PK_INCREF(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);
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);
SourceData__snapshot(frame->src,
self,

View File

@ -16,7 +16,7 @@ void py_newlistn(py_Ref out, int n) {
py_newlist(out);
List* ud = py_touserdata(out);
c11_vector__reserve(ud, n);
ud->count = n;
ud->length = n;
}
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) {
List* ud = py_touserdata(self);
return ud->count;
return ud->length;
}
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);
if(_1->type == tp_int) {
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);
return true;
} else if(_1->type == tp_slice) {
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;
py_newlist(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);
List* self = py_touserdata(py_arg(0));
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));
py_newnone(py_retval());
return true;
@ -171,7 +171,7 @@ static bool list__delitem__(int argc, py_Ref argv) {
PY_CHECK_ARG_TYPE(1, tp_int);
List* self = py_touserdata(py_arg(0));
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);
py_newnone(py_retval());
return true;
@ -186,8 +186,8 @@ static bool list__add__(int argc, py_Ref argv) {
List* list_1 = py_touserdata(_1);
py_newlist(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_1->data, list_1->count);
c11_vector__extend(py_TValue, list, list_0->data, list_0->length);
c11_vector__extend(py_TValue, list, list_1->data, list_1->length);
} else {
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_0 = py_touserdata(_0);
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 {
py_newnotimplemented(py_retval());
@ -226,7 +226,7 @@ static bool list__repr__(int argc, py_Ref argv) {
c11_sbuf buf;
c11_sbuf__ctor(&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);
bool ok = py_repr(val);
if(!ok) {
@ -234,7 +234,7 @@ static bool list__repr__(int argc, py_Ref argv) {
return false;
}
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__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));
PY_CHECK_ARG_TYPE(1, tp_list);
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());
return true;
}
@ -275,7 +275,7 @@ static bool list_copy(int argc, py_Ref argv) {
py_newlist(py_retval());
List* self = py_touserdata(py_arg(0));
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;
}
@ -330,8 +330,8 @@ static bool list_pop(int argc, py_Ref argv) {
return TypeError("pop() takes at most 2 arguments");
}
List* self = py_touserdata(py_arg(0));
if(self->count == 0) return IndexError("pop from empty list");
if(!pk__normalize_index(&index, self->count)) return false;
if(self->length == 0) return IndexError("pop from empty list");
if(!pk__normalize_index(&index, self->length)) return false;
*py_retval() = c11__getitem(py_TValue, self, index);
c11_vector__erase(py_TValue, self, index);
return true;
@ -342,9 +342,9 @@ static bool list_insert(int argc, py_Ref argv) {
PY_CHECK_ARG_TYPE(1, tp_int);
List* self = py_touserdata(py_arg(0));
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 > self->count) index = self->count;
if(index > self->length) index = self->length;
c11_vector__insert(py_TValue, self, index, *py_arg(2));
py_newnone(py_retval());
return true;
@ -380,7 +380,7 @@ static bool list_sort(int argc, py_Ref argv) {
if(py_isnone(key)) key = NULL;
bool ok = c11__stable_sort(self->data,
self->count,
self->length,
sizeof(py_TValue),
(int (*)(const void*, const void*, void*))lt_with_key,
key);
@ -405,7 +405,7 @@ static bool list__contains__(int argc, py_Ref argv) {
static void list__gc_mark(void* 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));
}
}

View File

@ -52,8 +52,8 @@ static bool namedict_items(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
py_Ref object = py_getslot(argv, 0);
NameDict* dict = PyObject__dict(object->_obj);
py_newtuple(py_retval(), dict->count);
for(int i = 0; i < dict->count; i++) {
py_newtuple(py_retval(), dict->length);
for(int i = 0; i < dict->length; i++) {
py_Ref slot = py_tuple_getitem(py_retval(), i);
py_newtuple(slot, 2);
NameDict_KV* kv = c11__at(NameDict_KV, dict, i);

View File

@ -318,8 +318,8 @@ static bool str_split(int argc, py_Ref argv) {
c11_sv sep = c11_string__sv(py_touserdata(&argv[1]));
res = c11_sv__split2(self, sep);
}
py_newlistn(py_retval(), res.count);
for(int i = 0; i < res.count; i++) {
py_newlistn(py_retval(), res.length);
for(int i = 0; i < res.length; i++) {
c11_sv item = c11__getitem(c11_sv, &res, i);
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) {
c11_vector__push(unsigned char, &res, data[i]);
}
unsigned char* p = py_newbytes(py_retval(), res.count);
memcpy(p, res.data, res.count);
unsigned char* p = py_newbytes(py_retval(), res.length);
memcpy(p, res.data, res.length);
c11_vector__dtor(&res);
return true;
} else {

View File

@ -86,7 +86,7 @@ py_Name
CodeObject code;
SourceData_ source = SourceData__rcnew(buffer, "<bind>", EXEC_MODE, false);
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);
}
FuncDecl_ decl = c11__getitem(FuncDecl_, &code.func_decls, 0);