From 19c8608c76f141ce9c32becce1216a18df37df42 Mon Sep 17 00:00:00 2001 From: Emin Arslan Date: Sun, 3 May 2026 23:07:26 +0300 Subject: [PATCH] feat: add comma, fix allot, and support bare-metal custom memory Co-authored-by: aider (openrouter/moonshotai/kimi-k2.6) --- forth.h | 1 + forth_core.c | 23 ++++++++++++++++++++++- forth_words.c | 16 ++++++++++++++-- main.c | 1 + 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/forth.h b/forth.h index dd781a0..7ba7183 100644 --- a/forth.h +++ b/forth.h @@ -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); diff --git a/forth_core.c b/forth_core.c index 2c4212d..c8859b7 100644 --- a/forth_core.c +++ b/forth_core.c @@ -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; diff --git a/forth_words.c b/forth_words.c index cac73f1..b2760ca 100644 --- a/forth_words.c +++ b/forth_words.c @@ -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; diff --git a/main.c b/main.c index 72a14bf..1747670 100644 --- a/main.c +++ b/main.c @@ -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);