refactor: make interpreter portable with static pools and I/O hooks

Co-authored-by: aider (openrouter/moonshotai/kimi-k2.6) <aider@aider.chat>
This commit is contained in:
2026-05-03 22:35:21 +03:00
parent b115744991
commit f033187c6c
6 changed files with 145 additions and 153 deletions
+58 -53
View File
@@ -1,34 +1,38 @@
#include "forth.h"
// Dynamic storage
int64_t *data_stack = NULL;
// Static storage for bare-metal portability
static int64_t data_stack_storage[FORTH_DATA_STACK_SIZE];
int64_t *data_stack = data_stack_storage;
int32_t data_sp = -1;
int32_t data_cap = 0;
int32_t data_cap = FORTH_DATA_STACK_SIZE;
Cell *ret_stack = NULL;
static Cell ret_stack_storage[FORTH_RET_STACK_SIZE];
Cell *ret_stack = ret_stack_storage;
int32_t rp = -1;
int32_t ret_cap = 0;
int32_t ret_cap = FORTH_RET_STACK_SIZE;
Cell* ip = NULL;
Word* dict_head = NULL;
int state = 0;
Cell *compile_buf = NULL;
static Cell compile_buf_storage[FORTH_COMPILE_BUF_SIZE];
Cell *compile_buf = compile_buf_storage;
int32_t compile_idx = 0;
int32_t compile_cap = 0;
int32_t compile_cap = FORTH_COMPILE_BUF_SIZE;
char compiling_name[MAX_NAME_LEN + 1] = {0};
char* input_buf = NULL;
size_t input_buf_cap = 0;
char* input_ptr = NULL;
int64_t *compile_stack = NULL;
static int64_t compile_stack_storage[FORTH_COMPILE_STACK_SIZE];
int64_t *compile_stack = compile_stack_storage;
int32_t compile_sp = -1;
int32_t compile_stack_cap = 0;
int32_t compile_stack_cap = FORTH_COMPILE_STACK_SIZE;
Cell *user_mem = NULL;
int64_t user_mem_size = 0;
Cell* here = NULL;
static Cell user_mem_storage[FORTH_USER_MEM_CELLS];
Cell *user_mem = user_mem_storage;
int64_t user_mem_size = FORTH_USER_MEM_CELLS;
Cell* here = user_mem_storage;
Word* w_exit = NULL;
Word* w_docolon = NULL;
@@ -37,17 +41,35 @@ Word* w_branch = NULL;
Word* w_zbranch = NULL;
Word* w_dot_quote_inner = NULL;
static Word word_pool[FORTH_MAX_WORDS];
static int32_t word_pool_used = 0;
static Cell word_body_storage[FORTH_MAX_WORD_BODY_CELLS];
static int32_t word_body_used = 0;
Word* forth_alloc_word(void) {
if (word_pool_used >= FORTH_MAX_WORDS) {
forth_printf("Dictionary full\n");
forth_panic();
}
return &word_pool[word_pool_used++];
}
Cell* forth_alloc_body(int32_t cells) {
if (word_body_used + cells > FORTH_MAX_WORD_BODY_CELLS) {
forth_printf("Word body pool exhausted\n");
forth_panic();
}
Cell* p = &word_body_storage[word_body_used];
word_body_used += cells;
return p;
}
// ---------- Data stack ----------
void data_push(int64_t val) {
if (data_sp + 1 >= data_cap) {
int32_t new_cap = data_cap ? data_cap * 2 : 128;
int64_t *tmp = realloc(data_stack, new_cap * sizeof(int64_t));
if (!tmp) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
data_stack = tmp;
data_cap = new_cap;
forth_printf("Data stack overflow\n");
return;
}
data_stack[++data_sp] = val;
}
@@ -61,69 +83,52 @@ int64_t data_pop(void) {
}
// ---------- Return stack (holds Cell values) ----------
static void ensure_ret_stack(void) {
if (rp + 1 >= ret_cap) {
int32_t new_cap = ret_cap ? ret_cap * 2 : 128;
Cell *tmp = realloc(ret_stack, new_cap * sizeof(Cell));
if (!tmp) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
ret_stack = tmp;
ret_cap = new_cap;
}
}
void ret_push_ip(Cell* val) {
ensure_ret_stack();
if (rp + 1 >= ret_cap) {
forth_printf("Return stack overflow\n");
return;
}
rp++;
ret_stack[rp].ptr = val; // store pointer in the 'ptr' member
}
Cell* ret_pop_ip(void) {
if (rp < 0) {
fprintf(stderr, "Return stack underflow\n");
forth_printf("Return stack underflow\n");
return NULL;
}
return ret_stack[rp--].ptr;
}
void ret_push_num(int64_t val) {
ensure_ret_stack();
if (rp + 1 >= ret_cap) {
forth_printf("Return stack overflow\n");
return;
}
rp++;
ret_stack[rp].num = val;
}
int64_t ret_pop_num(void) {
if (rp < 0) {
fprintf(stderr, "Return stack underflow (num)\n");
forth_printf("Return stack underflow (num)\n");
return 0;
}
return ret_stack[rp--].num;
}
// ---------- Compile stack (indices) ----------
static void ensure_compile_stack(void) {
if (compile_sp + 1 >= compile_stack_cap) {
int32_t new_cap = compile_stack_cap ? compile_stack_cap * 2 : 64;
int64_t *tmp = realloc(compile_stack, new_cap * sizeof(int64_t));
if (!tmp) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
compile_stack = tmp;
compile_stack_cap = new_cap;
}
}
void compile_push(int64_t idx) {
ensure_compile_stack();
if (compile_sp + 1 >= compile_stack_cap) {
forth_printf("Compile stack overflow\n");
return;
}
compile_stack[++compile_sp] = idx;
}
int64_t compile_pop(void) {
if (compile_sp < 0) {
fprintf(stderr, "Compile stack underflow\n");
forth_printf("Compile stack underflow\n");
return -1;
}
return compile_stack[compile_sp--];