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:
@@ -196,6 +196,7 @@ void do_do_var(Word* w);
|
|||||||
void do_do_const(Word* w);
|
void do_do_const(Word* w);
|
||||||
void do_here(Word* w);
|
void do_here(Word* w);
|
||||||
void do_allot(Word* w);
|
void do_allot(Word* w);
|
||||||
|
void do_comma(Word* w);
|
||||||
|
|
||||||
// Return stack
|
// Return stack
|
||||||
void do_to_r(Word* w);
|
void do_to_r(Word* w);
|
||||||
|
|||||||
+22
-1
@@ -1,13 +1,21 @@
|
|||||||
#include "forth.h"
|
#include "forth.h"
|
||||||
|
|
||||||
// Static storage for bare-metal portability
|
// Static storage for bare-metal portability
|
||||||
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
static int64_t data_stack_storage[FORTH_DATA_STACK_SIZE];
|
static int64_t data_stack_storage[FORTH_DATA_STACK_SIZE];
|
||||||
int64_t *data_stack = data_stack_storage;
|
int64_t *data_stack = data_stack_storage;
|
||||||
|
#else
|
||||||
|
int64_t *data_stack;
|
||||||
|
#endif
|
||||||
int32_t data_sp = -1;
|
int32_t data_sp = -1;
|
||||||
int32_t data_cap = FORTH_DATA_STACK_SIZE;
|
int32_t data_cap = FORTH_DATA_STACK_SIZE;
|
||||||
|
|
||||||
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
static Cell ret_stack_storage[FORTH_RET_STACK_SIZE];
|
static Cell ret_stack_storage[FORTH_RET_STACK_SIZE];
|
||||||
Cell *ret_stack = ret_stack_storage;
|
Cell *ret_stack = ret_stack_storage;
|
||||||
|
#else
|
||||||
|
Cell *ret_stack;
|
||||||
|
#endif
|
||||||
int32_t rp = -1;
|
int32_t rp = -1;
|
||||||
int32_t ret_cap = FORTH_RET_STACK_SIZE;
|
int32_t ret_cap = FORTH_RET_STACK_SIZE;
|
||||||
Cell* ip = NULL;
|
Cell* ip = NULL;
|
||||||
@@ -16,8 +24,12 @@ Word* dict_head = NULL;
|
|||||||
|
|
||||||
int state = 0;
|
int state = 0;
|
||||||
|
|
||||||
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
static Cell compile_buf_storage[FORTH_COMPILE_BUF_SIZE];
|
static Cell compile_buf_storage[FORTH_COMPILE_BUF_SIZE];
|
||||||
Cell *compile_buf = compile_buf_storage;
|
Cell *compile_buf = compile_buf_storage;
|
||||||
|
#else
|
||||||
|
Cell *compile_buf;
|
||||||
|
#endif
|
||||||
int32_t compile_idx = 0;
|
int32_t compile_idx = 0;
|
||||||
int32_t compile_cap = FORTH_COMPILE_BUF_SIZE;
|
int32_t compile_cap = FORTH_COMPILE_BUF_SIZE;
|
||||||
char compiling_name[MAX_NAME_LEN + 1] = {0};
|
char compiling_name[MAX_NAME_LEN + 1] = {0};
|
||||||
@@ -26,15 +38,24 @@ char* input_buf = NULL;
|
|||||||
size_t input_buf_cap = 0;
|
size_t input_buf_cap = 0;
|
||||||
char* input_ptr = NULL;
|
char* input_ptr = NULL;
|
||||||
|
|
||||||
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
static int64_t compile_stack_storage[FORTH_COMPILE_STACK_SIZE];
|
static int64_t compile_stack_storage[FORTH_COMPILE_STACK_SIZE];
|
||||||
int64_t *compile_stack = compile_stack_storage;
|
int64_t *compile_stack = compile_stack_storage;
|
||||||
|
#else
|
||||||
|
int64_t *compile_stack;
|
||||||
|
#endif
|
||||||
int32_t compile_sp = -1;
|
int32_t compile_sp = -1;
|
||||||
int32_t compile_stack_cap = FORTH_COMPILE_STACK_SIZE;
|
int32_t compile_stack_cap = FORTH_COMPILE_STACK_SIZE;
|
||||||
|
|
||||||
|
#ifndef FORTH_CUSTOM_MEMORY
|
||||||
static Cell user_mem_storage[FORTH_USER_MEM_CELLS];
|
static Cell user_mem_storage[FORTH_USER_MEM_CELLS];
|
||||||
Cell *user_mem = user_mem_storage;
|
Cell *user_mem = user_mem_storage;
|
||||||
int64_t user_mem_size = FORTH_USER_MEM_CELLS;
|
|
||||||
Cell* here = user_mem_storage;
|
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_exit = NULL;
|
||||||
Word* w_docolon = NULL;
|
Word* w_docolon = NULL;
|
||||||
|
|||||||
+14
-2
@@ -456,13 +456,25 @@ void do_here(Word* w) {
|
|||||||
void do_allot(Word* w) {
|
void do_allot(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
int64_t n = data_pop();
|
int64_t n = data_pop();
|
||||||
if (here + n > user_mem + user_mem_size) {
|
int64_t idx = here - user_mem;
|
||||||
forth_printf("User memory overflow\n");
|
if (idx + n < 0 || idx + n > user_mem_size) {
|
||||||
|
forth_printf("User memory out of bounds\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
here += n;
|
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
|
// Variable and constant
|
||||||
void do_variable(Word* w) {
|
void do_variable(Word* w) {
|
||||||
(void)w;
|
(void)w;
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ int main(void) {
|
|||||||
add_primitive("constant", do_constant, 0);
|
add_primitive("constant", do_constant, 0);
|
||||||
add_primitive("here", do_here, 0);
|
add_primitive("here", do_here, 0);
|
||||||
add_primitive("allot", do_allot, 0);
|
add_primitive("allot", do_allot, 0);
|
||||||
|
add_primitive(",", do_comma, 0);
|
||||||
|
|
||||||
// Return stack
|
// Return stack
|
||||||
add_primitive(">r", do_to_r, 0);
|
add_primitive(">r", do_to_r, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user