From bbf5840691772fc698cb3bedd5809c053f78c420 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sat, 6 Feb 2021 22:52:20 -0800 Subject: [PATCH] Allow for a vocabulary stack. --- ueforth/common/boot.fs | 2 +- ueforth/common/core.h | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/ueforth/common/boot.fs b/ueforth/common/boot.fs index edf512e..bfbe7ef 100644 --- a/ueforth/common/boot.fs +++ b/ueforth/common/boot.fs @@ -38,7 +38,7 @@ : base ( -- a ) 'sys 4 cells + ; : 'heap ( -- a ) 'sys 5 cells + ; : current ( -- a ) 'sys 6 cells + ; -: context ( -- a ) 'sys 7 cells + ; +: 'context ( -- a ) 'sys 7 cells + ; : context 'context @ ; : 'notfound ( -- a ) 'sys 8 cells + ; ( Dictionary ) diff --git a/ueforth/common/core.h b/ueforth/common/core.h index 8db86a4..f381eab 100644 --- a/ueforth/common/core.h +++ b/ueforth/common/core.h @@ -5,6 +5,7 @@ #define LOWER(ch) ((ch) & 0x5F) #define IMMEDIATE 1 #define SMUDGE 2 +#define VOCABULARY_DEPTH 16 #if PRINT_ERRORS #include @@ -13,7 +14,7 @@ static struct { const char *tib; cell_t ntib, tin, state, base; - cell_t *heap, **current, **context, notfound; + cell_t *heap, **current, ***context, notfound; int argc; char **argv; cell_t *rp; // spot to park main thread @@ -47,14 +48,16 @@ static cell_t same(const char *a, const char *b, cell_t len) { } static cell_t find(const char *name, cell_t len) { - cell_t *pos = *g_sys.context; - cell_t clen = CELL_LEN(len); - while (pos) { - if (!(pos[-1] & SMUDGE) && len == pos[-3] && - same(name, (const char *) &pos[-3 - clen], len) == 0) { - return (cell_t) pos; + for (cell_t ***voc = g_sys.context; *voc; ++voc) { + cell_t *pos = **voc; + cell_t clen = CELL_LEN(len); + while (pos) { + if (!(pos[-1] & SMUDGE) && len == pos[-3] && + same(name, (const char *) &pos[-3 - clen], len) == 0) { + return (cell_t) pos; + } + pos = (cell_t *) pos[-2]; // Follow link } - pos = (cell_t *) pos[-2]; // Follow link } return 0; } @@ -131,10 +134,13 @@ static void ueforth_init(int argc, char *argv[], void *heap, cell_t *rp = g_sys.heap + 1; g_sys.heap += STACK_SIZE; // FORTH vocabulary - *g_sys.heap++ = 0; - g_sys.current = (cell_t **) g_sys.heap; - g_sys.context = (cell_t **) g_sys.heap; *g_sys.heap++ = 0; + *g_sys.heap++ = 0; cell_t *forth = g_sys.heap; *g_sys.heap++ = 0; *g_sys.heap++ = 0; *g_sys.heap++ = 0; + // Vocabulary stack + g_sys.current = (cell_t **) forth; + g_sys.context = (cell_t ***) g_sys.heap; + *g_sys.heap++ = (cell_t) forth; + for (int i = 0; i < VOCABULARY_DEPTH; ++i) { *g_sys.heap++ = 0; } ueforth_run(0); (*g_sys.current)[-1] = IMMEDIATE; // Make last word ; IMMEDIATE