Browse Source

C-ified some less antiquated constructs to the antiquated form

main
Ludovic 'Archivist' Lagouardette 4 months ago
parent
commit
5411462218
1 changed files with 214 additions and 137 deletions
  1. +214
    -137
      lib.c

+ 214
- 137
lib.c View File

@ -33,7 +33,8 @@ struct label {
#ifdef NOSTDLIB #ifdef NOSTDLIB
static size_t strlen(const char* c) { static size_t strlen(const char* c) {
size_t j = 0;
size_t j;
j = 0;
while(*(c++)) { while(*(c++)) {
j++; j++;
} }
@ -41,8 +42,10 @@ static size_t strlen(const char* c) {
} }
static void* memcpy(void* _dest, const void* _src, size_t sz) { static void* memcpy(void* _dest, const void* _src, size_t sz) {
char* dest = _dest;
const char* src = _src;
char* dest;
const char* src;
dest = _dest;
src = _src;
while(sz--) { while(sz--) {
*(dest++) = *(src++); *(dest++) = *(src++);
} }
@ -59,8 +62,10 @@ static int strcmp(const char* dest, const char* src) {
} }
static void* memmove(void* _dest, const void* _src, size_t sz) { static void* memmove(void* _dest, const void* _src, size_t sz) {
char* dest = _dest;
const char* src = _src;
char* dest;
const char* src;
dest = _dest;
src = _src;
if (src < dest) { if (src < dest) {
src += sz; src += sz;
dest += sz; dest += sz;
@ -76,7 +81,8 @@ static void* memmove(void* _dest, const void* _src, size_t sz) {
} }
static void* memset(void* _dest, int src, size_t sz) { static void* memset(void* _dest, int src, size_t sz) {
char* dest = _dest;
char* dest;
dest = _dest;
while(sz--) { while(sz--) {
*(dest++) = src++; *(dest++) = src++;
} }
@ -92,7 +98,8 @@ static int isdigit(int d) {
} }
static int atoi(const char* c) { static int atoi(const char* c) {
int ret = 0;
int ret;
ret = 0;
while(*c) { while(*c) {
ret *= 10; ret *= 10;
ret += *c - '0'; ret += *c - '0';
@ -104,13 +111,17 @@ static int atoi(const char* c) {
#endif #endif
int ink_add_native(struct context* ctx, const char* name, void(*value)(struct context*)) { int ink_add_native(struct context* ctx, const char* name, void(*value)(struct context*)) {
int len;
char* copy;
if(ctx->native_words == NULL) { if(ctx->native_words == NULL) {
ctx->native_words = ctx->inner_malloc(sizeof(struct native_fn) * 8); ctx->native_words = ctx->inner_malloc(sizeof(struct native_fn) * 8);
ctx->native_words_top = 0; ctx->native_words_top = 0;
ctx->native_words_capacity = 8; ctx->native_words_capacity = 8;
} else if(ctx->native_words_top == ctx->native_words_capacity) { } else if(ctx->native_words_top == ctx->native_words_capacity) {
int new_count = (ctx->native_words_capacity + ctx->native_words_capacity/2);
void* renewed = ctx->inner_realloc(ctx->native_words, sizeof(struct native_fn) * new_count);
int new_count;
void* renewed;
new_count = (ctx->native_words_capacity + ctx->native_words_capacity/2);
renewed = ctx->inner_realloc(ctx->native_words, sizeof(struct native_fn) * new_count);
if(renewed == NULL) { if(renewed == NULL) {
return -3; return -3;
} else { } else {
@ -118,8 +129,8 @@ int ink_add_native(struct context* ctx, const char* name, void(*value)(struct co
ctx->native_words_capacity = new_count; ctx->native_words_capacity = new_count;
} }
} }
kt">int len = strlen(name);
kt">char* copy = ctx->inner_malloc(len+1);
len = strlen(name);
copy = ctx->inner_malloc(len+1);
if(copy == NULL) { if(copy == NULL) {
return -4; return -4;
} }
@ -132,13 +143,18 @@ int ink_add_native(struct context* ctx, const char* name, void(*value)(struct co
} }
static int ink_add_indigenous(struct context* ctx, const char* name, struct elem* m, size_t count) { static int ink_add_indigenous(struct context* ctx, const char* name, struct elem* m, size_t count) {
int len, i;
char* copy;
if(ctx->words == NULL) { if(ctx->words == NULL) {
ctx->words = ctx->malloc(sizeof(struct fn) * 8); ctx->words = ctx->malloc(sizeof(struct fn) * 8);
ctx->words_top = 0; ctx->words_top = 0;
ctx->words_capacity = 8; ctx->words_capacity = 8;
} else if(ctx->words_top == ctx->words_capacity) { } else if(ctx->words_top == ctx->words_capacity) {
int new_count = (ctx->words_capacity + ctx->words_capacity/2);
void* renewed = ctx->realloc(ctx->words, sizeof(struct native_fn) * new_count);
int new_count;
void* renewed;
new_count = (ctx->words_capacity + ctx->words_capacity/2);
renewed = ctx->realloc(ctx->words, sizeof(struct native_fn) * new_count);
if(renewed == NULL) { if(renewed == NULL) {
return -1; return -1;
} else { } else {
@ -146,7 +162,6 @@ static int ink_add_indigenous(struct context* ctx, const char* name, struct elem
ctx->words_capacity = new_count; ctx->words_capacity = new_count;
} }
} }
int i;
for(i = 0; i < ctx->words_top; ++i) { for(i = 0; i < ctx->words_top; ++i) {
if(strcmp(name, ctx->words[i].name) == 0) { if(strcmp(name, ctx->words[i].name) == 0) {
ctx->free(ctx->words[i].things); ctx->free(ctx->words[i].things);
@ -156,8 +171,8 @@ static int ink_add_indigenous(struct context* ctx, const char* name, struct elem
return i; return i;
} }
} }
kt">int len = strlen(name);
kt">char* copy = ctx->malloc(len+1);
len = strlen(name);
copy = ctx->malloc(len+1);
if(copy == NULL) { if(copy == NULL) {
return -2; return -2;
} }
@ -184,8 +199,10 @@ static int ink_add_lex_string(struct context* ctx, const char* name) {
ctx->lex_reserved_words_top = 0; ctx->lex_reserved_words_top = 0;
ctx->lex_reserved_words_capacity = 8; ctx->lex_reserved_words_capacity = 8;
} else if(ctx->lex_reserved_words_top == ctx->lex_reserved_words_capacity) { } else if(ctx->lex_reserved_words_top == ctx->lex_reserved_words_capacity) {
int new_count = (ctx->lex_reserved_words_capacity + ctx->lex_reserved_words_capacity/2);
void* renewed = ctx->inner_realloc(ctx->lex_reserved_words, sizeof(struct native_fn) * new_count);
int new_count;
new_count = (ctx->lex_reserved_words_capacity + ctx->lex_reserved_words_capacity/2);
void* renewed;
renewed = ctx->inner_realloc(ctx->lex_reserved_words, sizeof(struct native_fn) * new_count);
if(renewed == NULL) { if(renewed == NULL) {
return -5; return -5;
} else { } else {
@ -198,7 +215,8 @@ static int ink_add_lex_string(struct context* ctx, const char* name) {
return i; return i;
} }
} }
int len = strlen(name);
int len;
len = strlen(name);
i = ctx->lex_reserved_words_top; i = ctx->lex_reserved_words_top;
ctx->lex_reserved_words[i] = ctx->malloc(len+1); ctx->lex_reserved_words[i] = ctx->malloc(len+1);
memcpy(ctx->lex_reserved_words[i], name, len); memcpy(ctx->lex_reserved_words[i], name, len);
@ -208,15 +226,18 @@ static int ink_add_lex_string(struct context* ctx, const char* name) {
} }
int ink_push(struct context* ctx, struct elem value) { int ink_push(struct context* ctx, struct elem value) {
struct ink_routine* current;
if(ctx->routine_current >= ctx->routines_top) return -65; if(ctx->routine_current >= ctx->routines_top) return -65;
k">struct ink_routine* current = ctx->routines + ctx->routine_current;
current = ctx->routines + ctx->routine_current;
if(current->stack == NULL) { if(current->stack == NULL) {
current->stack = ctx->malloc(sizeof(struct elem) * 8); current->stack = ctx->malloc(sizeof(struct elem) * 8);
current->top = 0; current->top = 0;
current->capacity = 8; current->capacity = 8;
} else if(current->top == current->capacity) { } else if(current->top == current->capacity) {
int new_count = (current->capacity + current->capacity/2);
void* renewed = ctx->realloc(current->stack, sizeof(struct elem) * new_count);
int new_count;
void* renewed;
new_count = (current->capacity + current->capacity/2);
renewed = ctx->realloc(current->stack, sizeof(struct elem) * new_count);
if(renewed == NULL) { if(renewed == NULL) {
return -18; return -18;
} else { } else {
@ -230,16 +251,20 @@ int ink_push(struct context* ctx, struct elem value) {
} }
int ink_push_fn(struct context* ctx, struct stack_frame value) { int ink_push_fn(struct context* ctx, struct stack_frame value) {
struct ink_routine* current;
if(ctx->routine_current >= ctx->routines_top) return -55; if(ctx->routine_current >= ctx->routines_top) return -55;
struct ink_routine* current = ctx->routines + ctx->routine_current;
current = ctx->routines + ctx->routine_current;
if(current->panic) return -56; if(current->panic) return -56;
if(current->function_stack == NULL) { if(current->function_stack == NULL) {
current->function_stack = ctx->malloc(sizeof(struct stack_frame) * 8); current->function_stack = ctx->malloc(sizeof(struct stack_frame) * 8);
current->function_stack_top = 0; current->function_stack_top = 0;
current->function_stack_capacity = 8; current->function_stack_capacity = 8;
} else if(current->function_stack_top == current->function_stack_capacity) { } else if(current->function_stack_top == current->function_stack_capacity) {
int new_count = (current->function_stack_capacity + current->function_stack_capacity/2);
void* renewed = ctx->realloc(current->function_stack, sizeof(struct stack_frame) * new_count);
int new_count;
void* renewed;
new_count = (current->function_stack_capacity + current->function_stack_capacity/2);
renewed = ctx->realloc(current->function_stack, sizeof(struct stack_frame) * new_count);
if(renewed == NULL) { if(renewed == NULL) {
return -9; return -9;
} else { } else {
@ -269,7 +294,8 @@ void ink_pop(struct context* ctx) {
} }
struct context* ink_make_context(void*(*malloc)(size_t), void*(*realloc)(void*, size_t), void(*free)(void*), int(*putchar)(int)) { struct context* ink_make_context(void*(*malloc)(size_t), void*(*realloc)(void*, size_t), void(*free)(void*), int(*putchar)(int)) {
struct context* ctx = (struct context*)malloc(sizeof(struct context));
struct context* ctx;
ctx = (struct context*)malloc(sizeof(struct context));
ctx->malloc = malloc; ctx->malloc = malloc;
ctx->realloc = realloc; ctx->realloc = realloc;
ctx->free = free; ctx->free = free;
@ -320,7 +346,8 @@ static char* ink_itoa(struct context* _, int cpy) {
#ifndef NOSTDLIB #ifndef NOSTDLIB
struct context* ink_make_default_context() { struct context* ink_make_default_context() {
struct context* ctx = ink_make_context(malloc, realloc, free, putchar);
struct context* ctx;
ctx = ink_make_context(malloc, realloc, free, putchar);
ink_std_library(ctx); ink_std_library(ctx);
return ctx; return ctx;
} }
@ -328,12 +355,13 @@ struct context* ink_make_default_context() {
static int ink_consume_one(int* end, struct context* pContext, char** buffer, char* r) { static int ink_consume_one(int* end, struct context* pContext, char** buffer, char* r) {
int i; int i;
int done;
struct elem value;
if(*end == 0) { if(*end == 0) {
return 0; return 0;
} }
r[*end] = 0; r[*end] = 0;
int done = 0;
struct elem value;
done = 0;
if (strcmp(r, _KEYWORD_INK_FUNCTION) == 0) { if (strcmp(r, _KEYWORD_INK_FUNCTION) == 0) {
value.value = 0; value.value = 0;
value.type = INK_FUNCTION_KW; value.type = INK_FUNCTION_KW;
@ -433,8 +461,9 @@ static int ink_lex(struct context *pContext, char* buffer) {
int i; int i;
// Limits the token size to 127 chars // Limits the token size to 127 chars
char r[128]; char r[128];
int end = 0;
int end;
int err; int err;
end = 0;
while(*buffer != 0) { while(*buffer != 0) {
if(isspace(*buffer)) { if(isspace(*buffer)) {
@ -469,27 +498,32 @@ static int lblcmp(const char* label, const char* other, size_t label_sz) {
} }
int ink_make_routine(struct context* ctx) { int ink_make_routine(struct context* ctx) {
struct ink_routine* it;
struct ink_routine* end;
// Allocate space if needed // Allocate space if needed
if(ctx->routines == NULL) { if(ctx->routines == NULL) {
ctx->routines = ctx->inner_malloc(sizeof(struct ink_routine) * 8); ctx->routines = ctx->inner_malloc(sizeof(struct ink_routine) * 8);
ctx->routines_top = 0; ctx->routines_top = 0;
ctx->routines_capacity = 8; ctx->routines_capacity = 8;
k">struct ink_routine* it = ctx->routines;
k">struct ink_routine* end = ctx->routines + 8;
it = ctx->routines;
end = ctx->routines + 8;
for(;it != end;++it) { for(;it != end;++it) {
it->stack = NULL; it->stack = NULL;
it->function_stack = NULL; it->function_stack = NULL;
it->panic = INK_ROUTINE_CAN_REUSE; it->panic = INK_ROUTINE_CAN_REUSE;
} }
} else if(ctx->routines_top == ctx->routines_capacity) { } else if(ctx->routines_top == ctx->routines_capacity) {
int new_count = (ctx->routines_capacity + ctx->routines_capacity/2);
void* renewed = ctx->inner_realloc(ctx->routines, sizeof(struct stack_frame) * new_count);
int new_count;
void* renewed;
new_count = (ctx->routines_capacity + ctx->routines_capacity/2);
renewed = ctx->inner_realloc(ctx->routines, sizeof(struct stack_frame) * new_count);
if(renewed == NULL) { if(renewed == NULL) {
return -99; return -99;
} else { } else {
ctx->routines = renewed; ctx->routines = renewed;
k">struct ink_routine* it = ctx->routines + ctx->routines_capacity;
k">struct ink_routine* end = ctx->routines + new_count;
it = ctx->routines + ctx->routines_capacity;
end = ctx->routines + new_count;
for(;it != end;++it) { for(;it != end;++it) {
it->panic = INK_ROUTINE_CAN_REUSE; it->panic = INK_ROUTINE_CAN_REUSE;
} }
@ -497,11 +531,12 @@ int ink_make_routine(struct context* ctx) {
} }
} }
k">struct ink_routine* it = ctx->routines;
k">struct ink_routine* end = ctx->routines + ctx->routines_capacity;
it = ctx->routines;
end = ctx->routines + ctx->routines_capacity;
// Looks for a reusable routine space then uses it // Looks for a reusable routine space then uses it
for(;it != end;++it) { for(;it != end;++it) {
if(it->panic == INK_ROUTINE_CAN_REUSE) { if(it->panic == INK_ROUTINE_CAN_REUSE) {
int idx;
it->panic = 0; it->panic = 0;
it->stack = NULL; it->stack = NULL;
it->top = 0; it->top = 0;
@ -509,7 +544,7 @@ int ink_make_routine(struct context* ctx) {
it->function_stack = NULL; it->function_stack = NULL;
it->function_stack_top = 0; it->function_stack_top = 0;
it->function_stack_capacity = 0; it->function_stack_capacity = 0;
kt">int idx = it - ctx->routines;
idx = it - ctx->routines;
if(idx >= ctx->routines_top) { if(idx >= ctx->routines_top) {
ctx->routines_top = idx + 1; ctx->routines_top = idx + 1;
} }
@ -519,10 +554,11 @@ int ink_make_routine(struct context* ctx) {
} }
int ink_kill_routine(struct context* ctx, int routine){ int ink_kill_routine(struct context* ctx, int routine){
struct ink_routine* curr;
if(routine < 0 || routine >= ctx->routines_top) { if(routine < 0 || routine >= ctx->routines_top) {
return 0; return 0;
} }
k">struct ink_routine* curr = ctx->routines + routine;
curr = ctx->routines + routine;
if(curr->panic == INK_ROUTINE_CAN_REUSE) { if(curr->panic == INK_ROUTINE_CAN_REUSE) {
return 0; return 0;
} }
@ -546,18 +582,19 @@ int ink_kill_routine(struct context* ctx, int routine){
* @internal Loop from hell * @internal Loop from hell
*/ */
static int ink_parse(struct context* pContext, struct elem* executable_buffer, int* executable_buffer_top) { static int ink_parse(struct context* pContext, struct elem* executable_buffer, int* executable_buffer_top) {
struct ink_routine* currentRoutine = pContext->routines + pContext->routine_current;
int i;
struct ink_routine* currentRoutine;
int i, function_buffer_top, function_name, mode;
#define LABEL_BUFFER 128 #define LABEL_BUFFER 128
#define FUNCTION_BUFFER 256 #define FUNCTION_BUFFER 256
struct label labels[LABEL_BUFFER]; struct label labels[LABEL_BUFFER];
struct elem function_buffer[FUNCTION_BUFFER]; struct elem function_buffer[FUNCTION_BUFFER];
int function_buffer_top = 0;
int function_name = -1;
currentRoutine = pContext->routines + pContext->routine_current;
function_buffer_top = 0;
function_name = -1;
#define MODE_EXECUTABLE 0 #define MODE_EXECUTABLE 0
#define MODE_FUNCTION 1 #define MODE_FUNCTION 1
#define MODE_DO 2 #define MODE_DO 2
kt">int mode = mi">0;
mode = n">MODE_EXECUTABLE;
memset(labels, 0, sizeof(struct label)*LABEL_BUFFER); memset(labels, 0, sizeof(struct label)*LABEL_BUFFER);
// Loop from hell, good luck, pro-tip: leave the parser alone // Loop from hell, good luck, pro-tip: leave the parser alone
@ -632,8 +669,10 @@ static int ink_parse(struct context* pContext, struct elem* executable_buffer, i
int k; int k;
for(k = 0; k < LABEL_BUFFER; k++) { for(k = 0; k < LABEL_BUFFER; k++) {
if(labels[k].active) { if(labels[k].active) {
const char* lbl = labels[k].name;
int label_sz = strlen(lbl);
int label_sz;
const char* lbl;
lbl = labels[k].name;
label_sz = strlen(lbl);
if(lblcmp(labels[k].name, pContext->lex_reserved_words[pt.value], label_sz) == 0) { if(lblcmp(labels[k].name, pContext->lex_reserved_words[pt.value], label_sz) == 0) {
function_buffer[j].type = INK_INTEGER; function_buffer[j].type = INK_INTEGER;
function_buffer[j].value = labels[k].dest - j; function_buffer[j].value = labels[k].dest - j;
@ -672,16 +711,18 @@ static int ink_parse(struct context* pContext, struct elem* executable_buffer, i
} }
int ink_step(struct context *pContext) { int ink_step(struct context *pContext) {
struct ink_routine* currentRoutine = pContext->routines + pContext->routine_current;
struct ink_routine* currentRoutine;
struct stack_frame frame;
struct stack_frame* top;
struct elem next;
int t;
currentRoutine = pContext->routines + pContext->routine_current;
pContext->steps++; pContext->steps++;
if(currentRoutine->function_stack_top == 0) return 0; if(currentRoutine->function_stack_top == 0) return 0;
if(pContext->panic) { if(pContext->panic) {
return -1; return -1;
} }
struct stack_frame frame;
struct stack_frame* top;
struct elem next;
int t;
top = &currentRoutine->function_stack[currentRoutine->function_stack_top-1]; top = &currentRoutine->function_stack[currentRoutine->function_stack_top-1];
t = top->executing.type; t = top->executing.type;
switch(t) { switch(t) {
@ -733,8 +774,11 @@ int ink_step(struct context *pContext) {
} }
void ink_compile(struct context *pContext, char* buffer) { void ink_compile(struct context *pContext, char* buffer) {
int routine = ink_make_routine(pContext);
int saved = pContext->routine_current;
int routine, saved, i, executable_buffer_top;
// Main function has a size limit of 256 (need to know that for REPL
struct elem executable_buffer[256];
routine = ink_make_routine(pContext);
saved = pContext->routine_current;
pContext->routine_current = routine; pContext->routine_current = routine;
struct ink_routine* currentRoutine = pContext->routines + routine; struct ink_routine* currentRoutine = pContext->routines + routine;
currentRoutine->stack = NULL; currentRoutine->stack = NULL;
@ -746,10 +790,8 @@ void ink_compile(struct context *pContext, char* buffer) {
pContext->panic = 1; pContext->panic = 1;
return; return;
} }
int i = 0;
// Main function has a size limit of 256 (need to know that for REPL
struct elem executable_buffer[256];
int executable_buffer_top = 0;
i = 0;
executable_buffer_top = 0;
err = ink_parse(pContext, executable_buffer, &executable_buffer_top); err = ink_parse(pContext, executable_buffer, &executable_buffer_top);
if(err < 0) { if(err < 0) {
pContext->panic = 1; pContext->panic = 1;
@ -780,8 +822,8 @@ void ink_compile(struct context *pContext, char* buffer) {
} }
int ink_can_run(struct context* pContext) { int ink_can_run(struct context* pContext) {
int it = 0;
for(;it < pContext->routines_top; ++it) {
int it;
for(n">it = 0; it < pContext->routines_top; ++it) {
if(pContext->routines[it].panic == 0) { if(pContext->routines[it].panic == 0) {
return 1; return 1;
} }
@ -830,8 +872,10 @@ int ink_new_type(
ctx->types_top = 0; ctx->types_top = 0;
ctx->types_capacity = 8; ctx->types_capacity = 8;
} else if(ctx->types_top == ctx->types_capacity) { } else if(ctx->types_top == ctx->types_capacity) {
int new_count = (ctx->types_capacity + ctx->types_capacity/2);
void* renewed = ctx->inner_realloc(ctx->types, sizeof(struct ink_type) * new_count);
int new_count;
void* renewed;
new_count = (ctx->types_capacity + ctx->types_capacity/2);
renewed = ctx->inner_realloc(ctx->types, sizeof(struct ink_type) * new_count);
if(renewed == NULL) { if(renewed == NULL) {
return -129; return -129;
} else { } else {
@ -855,8 +899,9 @@ int ink_new_type(
} }
static struct element_slab* ink_get_value_link(struct context* ctx, struct elem ref) { static struct element_slab* ink_get_value_link(struct context* ctx, struct elem ref) {
int type_id;
if(ref.type < 16) return NULL; if(ref.type < 16) return NULL;
int type_id = ref.type - 16;
type_id = ref.type - 16;
if(type_id >= ctx->types_top) return NULL; if(type_id >= ctx->types_top) return NULL;
if(ctx->types[type_id].element_size == 0) return NULL; if(ctx->types[type_id].element_size == 0) return NULL;
if(ref.value < 0) return NULL; if(ref.value < 0) return NULL;
@ -873,24 +918,23 @@ void* ink_get_value(struct context* ctx, struct elem ref) {
} }
struct elem ink_make_native(struct context* ctx, int type, void* ptr) { struct elem ink_make_native(struct context* ctx, int type, void* ptr) {
int type_id;
struct elem ret;
if(type < 16) { if(type < 16) {
struct elem ret;
ret.type = 0; ret.type = 0;
ret.value = -130; ret.value = -130;
return ret; return ret;
} }
// Apply invariant of the user defined types // Apply invariant of the user defined types
kt">int type_id = type - 16;
type_id = type - 16;
if(type_id >= ctx->types_top) { if(type_id >= ctx->types_top) {
struct elem ret;
ret.type = 0; ret.type = 0;
ret.value = -129; ret.value = -129;
return ret; return ret;
} }
if(ctx->panic) { if(ctx->panic) {
struct elem ret;
ret.type = 0; ret.type = 0;
ret.value = -135; ret.value = -135;
return ret; return ret;
@ -903,10 +947,11 @@ struct elem ink_make_native(struct context* ctx, int type, void* ptr) {
ctx->types[type_id].elements_capacity = 8; ctx->types[type_id].elements_capacity = 8;
memset(ctx->types[type_id].elements + ctx->types[type_id].elements_top, 0, sizeof(struct element_slab)*(ctx->types[type_id].elements_capacity - ctx->types[type_id].elements_top)); memset(ctx->types[type_id].elements + ctx->types[type_id].elements_top, 0, sizeof(struct element_slab)*(ctx->types[type_id].elements_capacity - ctx->types[type_id].elements_top));
} else if(ctx->types[type_id].elements_top == ctx->types[type_id].elements_capacity) { } else if(ctx->types[type_id].elements_top == ctx->types[type_id].elements_capacity) {
int new_count = (ctx->types[type_id].elements_capacity + ctx->types[type_id].elements_capacity/2);
void* renewed = ctx->inner_realloc(ctx->types[type_id].elements, sizeof(struct element_slab) * new_count);
int new_count;
void* renewed;
new_count = (ctx->types[type_id].elements_capacity + ctx->types[type_id].elements_capacity/2);
renewed = ctx->inner_realloc(ctx->types[type_id].elements, sizeof(struct element_slab) * new_count);
if(renewed == NULL) { if(renewed == NULL) {
struct elem ret;
ret.type = 0; ret.type = 0;
ret.value = -129; ret.value = -129;
return ret; return ret;
@ -918,8 +963,8 @@ struct elem ink_make_native(struct context* ctx, int type, void* ptr) {
} }
// Push value in store // Push value in store
int g = ctx->types[type_id].elements_capacity;
int i;
int g, i;
g = ctx->types[type_id].elements_capacity;
for(i = 0; i < g; ++i) { for(i = 0; i < g; ++i) {
if(! ctx->types[type_id].elements[i].in_use) { if(! ctx->types[type_id].elements[i].in_use) {
ctx->types[type_id].elements[i].in_use = 1; ctx->types[type_id].elements[i].in_use = 1;
@ -929,7 +974,6 @@ struct elem ink_make_native(struct context* ctx, int type, void* ptr) {
} else { } else {
void* new_ptr = ctx->malloc(ctx->types[type_id].element_size); void* new_ptr = ctx->malloc(ctx->types[type_id].element_size);
if(new_ptr == NULL) { if(new_ptr == NULL) {
struct elem ret;
ret.type = 0; ret.type = 0;
ret.value = -139; ret.value = -139;
return ret; return ret;
@ -938,13 +982,11 @@ struct elem ink_make_native(struct context* ctx, int type, void* ptr) {
ctx->types[type_id].elements[i].data = new_ptr; ctx->types[type_id].elements[i].data = new_ptr;
} }
ctx->types[type_id].elements_top = max(ctx->types[type_id].elements_top, i+1); ctx->types[type_id].elements_top = max(ctx->types[type_id].elements_top, i+1);
struct elem ret;
ret.type = type; ret.type = type;
ret.value = i; ret.value = i;
return ret; return ret;
} }
} }
struct elem ret;
ret.type = 0; ret.type = 0;
ret.value = -140; ret.value = -140;
return ret; return ret;
@ -952,6 +994,9 @@ struct elem ink_make_native(struct context* ctx, int type, void* ptr) {
void ink_gc(struct context* ctx) { void ink_gc(struct context* ctx) {
int i, j, k; int i, j, k;
int marked;
struct element_slab* v;
for(i = 0; i < ctx->types_top; ++i) { for(i = 0; i < ctx->types_top; ++i) {
for(j = 0; j < ctx->types[i].elements_top; ++j) { for(j = 0; j < ctx->types[i].elements_top; ++j) {
ctx->types[i].elements[j].uses = 0; ctx->types[i].elements[j].uses = 0;
@ -961,22 +1006,23 @@ void ink_gc(struct context* ctx) {
// Start by marking the roots of the routines // Start by marking the roots of the routines
for(i = 0; i < ctx->routines_top; ++i) { for(i = 0; i < ctx->routines_top; ++i) {
for(j = 0; j < ctx->routines[i].top; ++j) { for(j = 0; j < ctx->routines[i].top; ++j) {
k">struct element_slab* v = ink_get_value_link(ctx, ctx->routines[i].stack[j]);
v = ink_get_value_link(ctx, ctx->routines[i].stack[j]);
if(v != NULL) ++v->uses; if(v != NULL) ++v->uses;
} }
} }
// Mark the rest of the data // Mark the rest of the data
int marked;
do { do {
marked = 0; marked = 0;
for (i = 0; i < ctx->types_top; ++i) { for (i = 0; i < ctx->types_top; ++i) {
for (j = 0; j < ctx->types[i].elements_top; ++j) { for (j = 0; j < ctx->types[i].elements_top; ++j) {
// Only mark from things that are active and detected as in use // Only mark from things that are active and detected as in use
if (ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].uses) { if (ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].uses) {
struct ink_collection_list c = ctx->types[i].gc(ctx, ctx->types[i].elements[j].data);
struct ink_collection_list c;
c = ctx->types[i].gc(ctx, ctx->types[i].elements[j].data);
for (k = 0; k < c.count; ++k) { for (k = 0; k < c.count; ++k) {
struct element_slab *v = ink_get_value_link(ctx, c.elements[k]);
struct element_slab *v;
v = ink_get_value_link(ctx, c.elements[k]);
// Never mark twice to avoid infinite loops with e.g. arrays that contain themselves // Never mark twice to avoid infinite loops with e.g. arrays that contain themselves
if (v != NULL && !v->uses) { if (v != NULL && !v->uses) {
++v->uses; ++v->uses;
@ -1009,14 +1055,17 @@ void ink_gc(struct context* ctx) {
/**********************************************************************************************************************/ /**********************************************************************************************************************/
static void print_stacktrace(struct context* _) { static void print_stacktrace(struct context* _) {
int i = 0;
struct ink_routine* currentRoutine = _->routines + _->routine_current;
for(; i < currentRoutine->function_stack_top; ++i) {
int i;
struct ink_routine* currentRoutine;
currentRoutine = _->routines + _->routine_current;
for(i = 0; i < currentRoutine->function_stack_top; ++i) {
struct elem thing; struct elem thing;
char *n;
thing = currentRoutine->function_stack[i].executing; thing = currentRoutine->function_stack[i].executing;
switch(thing.type) { switch(thing.type) {
case INK_NATIVE_FUNCTION: { case INK_NATIVE_FUNCTION: {
kt">char *n = _->native_words[thing.value].name;
n = _->native_words[thing.value].name;
while (*n) { while (*n) {
_->putchar(*n); _->putchar(*n);
++n; ++n;
@ -1025,7 +1074,7 @@ static void print_stacktrace(struct context* _) {
break; break;
} }
case INK_FUNCTION:{ case INK_FUNCTION:{
kt">char *n = _->native_words[thing.value].name;
n = _->native_words[thing.value].name;
while (*n) { while (*n) {
_->putchar(*n); _->putchar(*n);
++n; ++n;
@ -1047,13 +1096,14 @@ static void print_stacktrace(struct context* _) {
} }
static void add_int(struct context* ctx) { static void add_int(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
struct elem b;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 2) { if(currentRoutine->top < 2) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
struct elem b;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
b = currentRoutine->stack[currentRoutine->top-2]; b = currentRoutine->stack[currentRoutine->top-2];
if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
@ -1065,13 +1115,14 @@ static void add_int(struct context* ctx) {
} }
static void sub_int(struct context* ctx) { static void sub_int(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
struct elem b;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 2) { if(currentRoutine->top < 2) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
struct elem b;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
b = currentRoutine->stack[currentRoutine->top-2]; b = currentRoutine->stack[currentRoutine->top-2];
if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
@ -1083,13 +1134,14 @@ static void sub_int(struct context* ctx) {
} }
static void mult_int(struct context* ctx) { static void mult_int(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
struct elem b;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 2) { if(currentRoutine->top < 2) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
struct elem b;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
b = currentRoutine->stack[currentRoutine->top-2]; b = currentRoutine->stack[currentRoutine->top-2];
if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
@ -1101,13 +1153,14 @@ static void mult_int(struct context* ctx) {
} }
static void div_int(struct context* ctx) { static void div_int(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
struct elem b;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 2) { if(currentRoutine->top < 2) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
struct elem b;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
b = currentRoutine->stack[currentRoutine->top-2]; b = currentRoutine->stack[currentRoutine->top-2];
if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
@ -1119,13 +1172,14 @@ static void div_int(struct context* ctx) {
} }
static void rem_int(struct context* ctx) { static void rem_int(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
struct elem b;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 2) { if(currentRoutine->top < 2) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
struct elem b;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
b = currentRoutine->stack[currentRoutine->top-2]; b = currentRoutine->stack[currentRoutine->top-2];
if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
@ -1137,20 +1191,22 @@ static void rem_int(struct context* ctx) {
} }
static void dupe_elem(struct context* ctx) { static void dupe_elem(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
int err;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 1) { if(currentRoutine->top < 1) {
ctx->panic = 1; ctx->panic = 1;
return; return;
} }
struct elem a;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
int err;
err = ink_push(ctx, a); err = ink_push(ctx, a);
if(err < 0) ctx->panic; if(err < 0) ctx->panic;
} }
static void drop_elem(struct context* ctx) { static void drop_elem(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 1) { if(currentRoutine->top < 1) {
ctx->panic = 1; ctx->panic = 1;
return; return;
@ -1159,36 +1215,38 @@ static void drop_elem(struct context* ctx) {
} }
static void pluck_elem(struct context* ctx) { static void pluck_elem(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
int position, err;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 1) { if(currentRoutine->top < 1) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
if(a.type != INK_INTEGER) { if(a.type != INK_INTEGER) {
ctx->panic = 1; ctx->panic = 1;
return; return;
} }
kt">int position = currentRoutine->top - (a.value + 1);
position = currentRoutine->top - (a.value + 1);
if(position >= currentRoutine->top || position < 0) { if(position >= currentRoutine->top || position < 0) {
ctx->panic = 1; ctx->panic = 1;
return; return;
} }
ink_pop(ctx); ink_pop(ctx);
int err;
err = ink_push(ctx, currentRoutine->stack[position]); err = ink_push(ctx, currentRoutine->stack[position]);
if(err < 0) ctx->panic; if(err < 0) ctx->panic;
} }
static void swap_elem(struct context* ctx) { static void swap_elem(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
struct elem b;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 2) { if(currentRoutine->top < 2) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
struct elem b;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
b = currentRoutine->stack[currentRoutine->top-2]; b = currentRoutine->stack[currentRoutine->top-2];
currentRoutine->stack[currentRoutine->top-2] = a; currentRoutine->stack[currentRoutine->top-2] = a;
@ -1196,12 +1254,13 @@ static void swap_elem(struct context* ctx) {
} }
static void return_if(struct context* ctx) { static void return_if(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 1) { if(currentRoutine->top < 1) {
ctx->panic = 1; ctx->panic = 1;
return; return;
} }
struct elem a;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
if(a.type != INK_INTEGER) { if(a.type != INK_INTEGER) {
ctx->panic = 1; ctx->panic = 1;
@ -1216,12 +1275,13 @@ static void return_if(struct context* ctx) {
} }
static void jump_if(struct context* ctx) { static void jump_if(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 1) { if(currentRoutine->top < 1) {
ctx->panic = 1; ctx->panic = 1;
return; return;
} }
struct elem a;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
if(a.type != INK_INTEGER) { if(a.type != INK_INTEGER) {
ctx->panic = 1; ctx->panic = 1;
@ -1238,16 +1298,19 @@ static void jump_if(struct context* ctx) {
} }
static void print_int(struct context* ctx) { static void print_int(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
char* n;
char* str;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) { if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
ink_pop(ctx); ink_pop(ctx);
kt">char* n = ink_itoa(ctx, a.value);
kt">char* str = n;
n = ink_itoa(ctx, a.value);
str = n;
while (*str) { while (*str) {
ctx->putchar(*str); ctx->putchar(*str);
++str; ++str;
@ -1256,12 +1319,13 @@ static void print_int(struct context* ctx) {
} }
static void print_as_utf8(struct context* ctx) { static void print_as_utf8(struct context* ctx) {
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
struct ink_routine* currentRoutine;
struct elem a;
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) { if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
ctx->panic = 1; ctx->panic = 1;
return; return;
} }
struct elem a;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
if(a.value <= 0x7F) { if(a.value <= 0x7F) {
ctx->putchar(a.value); ctx->putchar(a.value);
@ -1301,13 +1365,15 @@ static int get_type_by_name(struct context* ctx, const char* name) {
} }
static void collect_array(struct context* ctx, void* array) { static void collect_array(struct context* ctx, void* array) {
struct ink_array* ary = array;
struct ink_array* ary;
ary = array;
if(ary->elements != NULL) ctx->free(ary->elements); if(ary->elements != NULL) ctx->free(ary->elements);
} }
static struct ink_collection_list gc_array(struct context* ctx, void* array) { static struct ink_collection_list gc_array(struct context* ctx, void* array) {
struct ink_array* ary = array;
struct ink_array* ary;
struct ink_collection_list c; struct ink_collection_list c;
ary = array;
c.elements = ctx->inner_malloc(sizeof(struct elem)*ary->top); c.elements = ctx->inner_malloc(sizeof(struct elem)*ary->top);
c.count = ary->top; c.count = ary->top;
memcpy(c.elements, ary->elements, sizeof(struct elem)*ary->top); memcpy(c.elements, ary->elements, sizeof(struct elem)*ary->top);
@ -1315,25 +1381,30 @@ static struct ink_collection_list gc_array(struct context* ctx, void* array) {
} }
static void new_array(struct context* ctx) { static void new_array(struct context* ctx) {
int tid = get_type_by_name(ctx, "array");
int tid;
struct elem e;
struct ink_array ary; struct ink_array ary;
tid = get_type_by_name(ctx, "array");
ary.elements = NULL; ary.elements = NULL;
ary.top = 0; ary.top = 0;
ary.capacity = 0; ary.capacity = 0;
k">struct elem e = ink_make_native(ctx, tid, &ary);
e = ink_make_native(ctx, tid, &ary);
ink_push(ctx, e); ink_push(ctx, e);
} }
static void push_array(struct context* ctx) { static void push_array(struct context* ctx) {
int tid = get_type_by_name(ctx, "array");
struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
int tid;
struct elem a;
struct ink_routine* currentRoutine;
struct ink_array* ary;
tid = get_type_by_name(ctx, "array");
currentRoutine = ctx->routines + ctx->routine_current;
if(currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top-1].type != tid) { if(currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top-1].type != tid) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
a = currentRoutine->stack[currentRoutine->top-1]; a = currentRoutine->stack[currentRoutine->top-1];
k">struct ink_array* ary= ink_get_value(ctx, a);
ary= ink_get_value(ctx, a);
if(ary == NULL) { if(ary == NULL) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
@ -1345,8 +1416,10 @@ static void push_array(struct context* ctx) {
ary->top = 0; ary->top = 0;
ary->capacity = 8; ary->capacity = 8;
} else if(ary->top == ary->capacity) { } else if(ary->top == ary->capacity) {
int new_count = (ary->capacity + ary->capacity/2);
void* renewed = ctx->realloc(ary->elements, sizeof(struct elem) * new_count);
int new_count;
void* renewed;
new_count = (ary->capacity + ary->capacity/2);
renewed = ctx->realloc(ary->elements, sizeof(struct elem) * new_count);
if(renewed == NULL) { if(renewed == NULL) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
@ -1363,22 +1436,26 @@ static void push_array(struct context* ctx) {
static void index_array(struct context* ctx) { static void index_array(struct context* ctx) {
int tid = get_type_by_name(ctx, "array");
struct ink_routine *currentRoutine = ctx->routines + ctx->routine_current;
int tid;
struct ink_routine *currentRoutine;
struct elem a;
struct ink_array *ary;
struct elem idx;
tid = get_type_by_name(ctx, "array");
currentRoutine = ctx->routines + ctx->routine_current;
if (currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) { if (currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
struct elem a;
a = currentRoutine->stack[currentRoutine->top - 1]; a = currentRoutine->stack[currentRoutine->top - 1];
k">struct ink_array *ary = ink_get_value(ctx, a);
ary = ink_get_value(ctx, a);
if (ary == NULL) { if (ary == NULL) {
currentRoutine->panic = 1; currentRoutine->panic = 1;
return; return;
} }
ink_pop(ctx); ink_pop(ctx);
struct elem idx;
idx = currentRoutine->stack[currentRoutine->top - 1]; idx = currentRoutine->stack[currentRoutine->top - 1];
ink_pop(ctx); ink_pop(ctx);
@ -1396,9 +1473,9 @@ static void run_gc(struct context* ctx) {
int ink_std_library(struct context* ctx) { int ink_std_library(struct context* ctx) {
int v;
int v, array_t;
v = 0; v = 0;
kt">int array_t = ink_new_type(ctx, "array", sizeof(struct ink_array), collect_array, gc_array);
array_t = ink_new_type(ctx, "array", sizeof(struct ink_array), collect_array, gc_array);
v += ink_add_native(ctx, "array.new", new_array); v += ink_add_native(ctx, "array.new", new_array);
v += ink_add_native(ctx, "array.push", push_array); v += ink_add_native(ctx, "array.push", push_array);
v += ink_add_native(ctx, "array.index", index_array); v += ink_add_native(ctx, "array.index", index_array);

Loading…
Cancel
Save