feat: switch to 64-bit integers and dynamic memory
Co-authored-by: aider (openrouter/deepseek/deepseek-v4-pro) <aider@aider.chat>
This commit is contained in:
+85
-66
@@ -1,103 +1,122 @@
|
||||
#include "forth.h"
|
||||
|
||||
// Global variables
|
||||
int32_t data_stack[DATA_STACK_SIZE];
|
||||
int sp = -1;
|
||||
Cell ret_stack[RET_STACK_SIZE];
|
||||
int rp = -1;
|
||||
// Dynamic storage
|
||||
int64_t *data_stack = NULL;
|
||||
int32_t data_sp = -1;
|
||||
int32_t data_cap = 0;
|
||||
|
||||
Cell *ret_stack = NULL;
|
||||
int32_t rp = -1;
|
||||
int32_t ret_cap = 0;
|
||||
Cell* ip = NULL;
|
||||
|
||||
Word dict[DICT_SIZE];
|
||||
int dict_idx = 0;
|
||||
Word* dict_head = NULL;
|
||||
Cell dict_bodies[BODY_SIZE];
|
||||
int body_idx = 0;
|
||||
|
||||
int state = 0;
|
||||
Cell compile_buf[COMPILE_BUF_SIZE];
|
||||
int compile_idx = 0;
|
||||
char compiling_name[MAX_NAME_LEN + 1];
|
||||
Cell *compile_buf = NULL;
|
||||
int32_t compile_idx = 0;
|
||||
int32_t compile_cap = 0;
|
||||
char compiling_name[MAX_NAME_LEN + 1] = {0};
|
||||
|
||||
char input_buf[INPUT_BUF_SIZE];
|
||||
char input_buf[1024] = {0};
|
||||
char* input_ptr = NULL;
|
||||
|
||||
Cell* compile_stack[COMPILE_STACK_SIZE];
|
||||
int compile_sp = -1;
|
||||
int64_t *compile_stack = NULL;
|
||||
int32_t compile_sp = -1;
|
||||
int32_t compile_stack_cap = 0;
|
||||
|
||||
int32_t user_mem[USER_MEMORY_SIZE];
|
||||
int32_t* here = &user_mem[0];
|
||||
Cell *user_mem = NULL;
|
||||
int64_t user_mem_size = 0;
|
||||
Cell* here = NULL;
|
||||
|
||||
// Stack helpers
|
||||
void data_push(int32_t val) {
|
||||
if (sp < DATA_STACK_SIZE - 1) {
|
||||
data_stack[++sp] = val;
|
||||
} else {
|
||||
printf("Data stack overflow\n");
|
||||
// ---------- 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;
|
||||
}
|
||||
data_stack[++data_sp] = val;
|
||||
}
|
||||
|
||||
int32_t data_pop(void) {
|
||||
if (sp >= 0) {
|
||||
return data_stack[sp--];
|
||||
} else {
|
||||
printf("Data stack underflow\n");
|
||||
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) ----------
|
||||
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) {
|
||||
if (rp < RET_STACK_SIZE - 1) {
|
||||
rp++;
|
||||
ret_stack[rp].cell_ptr = val;
|
||||
} else {
|
||||
printf("Return stack overflow\n");
|
||||
}
|
||||
ensure_ret_stack();
|
||||
rp++;
|
||||
ret_stack[rp].ptr = val; // store pointer in the 'ptr' member
|
||||
}
|
||||
|
||||
Cell* ret_pop_ip(void) {
|
||||
if (rp >= 0) {
|
||||
Cell* res = ret_stack[rp].cell_ptr;
|
||||
rp--;
|
||||
return res;
|
||||
} else {
|
||||
printf("Return stack underflow\n");
|
||||
if (rp < 0) {
|
||||
fprintf(stderr, "Return stack underflow\n");
|
||||
return NULL;
|
||||
}
|
||||
return ret_stack[rp--].ptr;
|
||||
}
|
||||
|
||||
void ret_push_num(int32_t val) {
|
||||
if (rp < RET_STACK_SIZE - 1) {
|
||||
rp++;
|
||||
ret_stack[rp].num = val;
|
||||
} else {
|
||||
printf("Return stack overflow\n");
|
||||
}
|
||||
void ret_push_num(int64_t val) {
|
||||
ensure_ret_stack();
|
||||
rp++;
|
||||
ret_stack[rp].num = val;
|
||||
}
|
||||
|
||||
int32_t ret_pop_num(void) {
|
||||
if (rp >= 0) {
|
||||
int32_t res = ret_stack[rp].num;
|
||||
rp--;
|
||||
return res;
|
||||
} else {
|
||||
printf("Return stack underflow\n");
|
||||
int64_t ret_pop_num(void) {
|
||||
if (rp < 0) {
|
||||
fprintf(stderr, "Return stack underflow (num)\n");
|
||||
return 0;
|
||||
}
|
||||
return ret_stack[rp--].num;
|
||||
}
|
||||
|
||||
void compile_push(Cell* addr) {
|
||||
if (compile_sp < COMPILE_STACK_SIZE - 1) {
|
||||
compile_stack[++compile_sp] = addr;
|
||||
} else {
|
||||
printf("Compile stack overflow\n");
|
||||
// ---------- 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;
|
||||
}
|
||||
}
|
||||
|
||||
Cell* compile_pop(void) {
|
||||
if (compile_sp >= 0) {
|
||||
return compile_stack[compile_sp--];
|
||||
} else {
|
||||
printf("Compile stack underflow\n");
|
||||
return NULL;
|
||||
}
|
||||
void compile_push(int64_t idx) {
|
||||
ensure_compile_stack();
|
||||
compile_stack[++compile_sp] = idx;
|
||||
}
|
||||
|
||||
int64_t compile_pop(void) {
|
||||
if (compile_sp < 0) {
|
||||
fprintf(stderr, "Compile stack underflow\n");
|
||||
return -1;
|
||||
}
|
||||
return compile_stack[compile_sp--];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user