Files
ai-forth-experiment/forth_core.c
T
2026-05-03 22:35:21 +03:00

136 lines
3.3 KiB
C

#include "forth.h"
// 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 = FORTH_DATA_STACK_SIZE;
static Cell ret_stack_storage[FORTH_RET_STACK_SIZE];
Cell *ret_stack = ret_stack_storage;
int32_t rp = -1;
int32_t ret_cap = FORTH_RET_STACK_SIZE;
Cell* ip = NULL;
Word* dict_head = 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 = 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;
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 = FORTH_COMPILE_STACK_SIZE;
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;
Word* w_lit = NULL;
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) {
forth_printf("Data stack overflow\n");
return;
}
data_stack[++data_sp] = val;
}
int64_t data_pop(void) {
if (data_sp < 0) {
fprintf(stderr, "Data stack underflow\n");
return 0;
}
return data_stack[data_sp--];
}
// ---------- Return stack (holds Cell values) ----------
void ret_push_ip(Cell* val) {
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) {
forth_printf("Return stack underflow\n");
return NULL;
}
return ret_stack[rp--].ptr;
}
void ret_push_num(int64_t val) {
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) {
forth_printf("Return stack underflow (num)\n");
return 0;
}
return ret_stack[rp--].num;
}
// ---------- Compile stack (indices) ----------
void compile_push(int64_t idx) {
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) {
forth_printf("Compile stack underflow\n");
return -1;
}
return compile_stack[compile_sp--];
}