feat: add comma, fix allot, and support bare-metal custom memory

Co-authored-by: aider (openrouter/moonshotai/kimi-k2.6) <aider@aider.chat>
This commit is contained in:
2026-05-03 23:07:26 +03:00
parent 1c0c6daf4f
commit 19c8608c76
4 changed files with 38 additions and 3 deletions
+1
View File
@@ -196,6 +196,7 @@ void do_do_var(Word* w);
void do_do_const(Word* w);
void do_here(Word* w);
void do_allot(Word* w);
void do_comma(Word* w);
// Return stack
void do_to_r(Word* w);
+22 -1
View File
@@ -1,13 +1,21 @@
#include "forth.h"
// Static storage for bare-metal portability
#ifndef FORTH_CUSTOM_MEMORY
static int64_t data_stack_storage[FORTH_DATA_STACK_SIZE];
int64_t *data_stack = data_stack_storage;
#else
int64_t *data_stack;
#endif
int32_t data_sp = -1;
int32_t data_cap = FORTH_DATA_STACK_SIZE;
#ifndef FORTH_CUSTOM_MEMORY
static Cell ret_stack_storage[FORTH_RET_STACK_SIZE];
Cell *ret_stack = ret_stack_storage;
#else
Cell *ret_stack;
#endif
int32_t rp = -1;
int32_t ret_cap = FORTH_RET_STACK_SIZE;
Cell* ip = NULL;
@@ -16,8 +24,12 @@ Word* dict_head = NULL;
int state = 0;
#ifndef FORTH_CUSTOM_MEMORY
static Cell compile_buf_storage[FORTH_COMPILE_BUF_SIZE];
Cell *compile_buf = compile_buf_storage;
#else
Cell *compile_buf;
#endif
int32_t compile_idx = 0;
int32_t compile_cap = FORTH_COMPILE_BUF_SIZE;
char compiling_name[MAX_NAME_LEN + 1] = {0};
@@ -26,15 +38,24 @@ char* input_buf = NULL;
size_t input_buf_cap = 0;
char* input_ptr = NULL;
#ifndef FORTH_CUSTOM_MEMORY
static int64_t compile_stack_storage[FORTH_COMPILE_STACK_SIZE];
int64_t *compile_stack = compile_stack_storage;
#else
int64_t *compile_stack;
#endif
int32_t compile_sp = -1;
int32_t compile_stack_cap = FORTH_COMPILE_STACK_SIZE;
#ifndef FORTH_CUSTOM_MEMORY
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;
#else
Cell *user_mem;
Cell* here;
#endif
int64_t user_mem_size = FORTH_USER_MEM_CELLS;
Word* w_exit = NULL;
Word* w_docolon = NULL;
+14 -2
View File
@@ -456,13 +456,25 @@ void do_here(Word* w) {
void do_allot(Word* w) {
(void)w;
int64_t n = data_pop();
if (here + n > user_mem + user_mem_size) {
forth_printf("User memory overflow\n");
int64_t idx = here - user_mem;
if (idx + n < 0 || idx + n > user_mem_size) {
forth_printf("User memory out of bounds\n");
return;
}
here += n;
}
void do_comma(Word* w) {
(void)w;
int64_t val = data_pop();
if (here + 1 > user_mem + user_mem_size) {
forth_printf("User memory overflow\n");
return;
}
here->num = val;
here++;
}
// Variable and constant
void do_variable(Word* w) {
(void)w;
+1
View File
@@ -75,6 +75,7 @@ int main(void) {
add_primitive("constant", do_constant, 0);
add_primitive("here", do_here, 0);
add_primitive("allot", do_allot, 0);
add_primitive(",", do_comma, 0);
// Return stack
add_primitive(">r", do_to_r, 0);