From e111afb040ecdc9b4ad0707eaffe61869dc50202 Mon Sep 17 00:00:00 2001 From: tocariimaa Date: Wed, 5 Feb 2025 14:13:55 -0300 Subject: [PATCH] refactor word compilation to allow recursion --- pila.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/pila.c b/pila.c index 78d5d82..c3ddd22 100644 --- a/pila.c +++ b/pila.c @@ -71,9 +71,8 @@ struct RpnState { char *rsp; /* reader start pointer */ char *rep; /* " end " */ /* current word compilation state */ - UserWord cur_comp; - isize cur_comp_cap; - Str cur_comp_name; + Word *cur_compw; + isize cur_compw_cap; }; struct DictionaryEntry { @@ -347,11 +346,20 @@ emit_nat(RpnState *st) void compile_start(RpnState *st, Str wn) { + Word *w = malloc(sizeof(*w)); + w->name = wn; + w->kind = WORD_USER; + + UserWord uw; + uw.words = calloc(16, sizeof(Word)); + uw.len = 0; + + w->uword = uw; + st->cur_compw_cap = 16; + st->cur_compw = w; st->compiling = true; - st->cur_comp_name = wn; - st->cur_comp.words = calloc(16, sizeof(Word)); - st->cur_comp.len = 0; - st->cur_comp_cap = 16; + + add_word(st, w); } void @@ -378,13 +386,9 @@ compile_end_nat(RpnState *st) signal_error(st, "not in compiling mode"); return; } - Word *w = malloc(sizeof(*w)); - + Assert(st->cur_compw != nil); st->compiling = false; - w->name = st->cur_comp_name; - w->kind = WORD_USER; - w->uword = st->cur_comp; - add_word(st, w); + add_word(st, st->cur_compw); } Str @@ -643,7 +647,7 @@ eval(RpnState *st, Str src) if (w->kind == WORD_NATIVE && w->nat == compile_end_nat) { w->nat(st); } else { - st->cur_comp.words[st->cur_comp.len++] = w; + st->cur_compw->uword.words[st->cur_compw->uword.len++] = w; } } } else if (*st->rsp == '\\' || *st->rsp == '(') {