From 8f787f3ff9776298558877071e678613e7f07efa Mon Sep 17 00:00:00 2001 From: Smooth Operator Date: Mon, 16 Dec 2024 11:52:28 -0500 Subject: [PATCH] wip `literal` per ANS forth --- forth/core.zf | 2 ++ src/zforth/zforth.c | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/forth/core.zf b/forth/core.zf index f81bf03..96e1cd2 100644 --- a/forth/core.zf +++ b/forth/core.zf @@ -58,6 +58,8 @@ : allot h +! ; : var : ' lit , here 5 allot here swap ! 5 allot postpone ; ; : const : ' lit , , postpone ; ; +: constant >r : r> postpone literal postpone ; ; +: variable >r here r> postpone , constant ; ( 'begin' gets the current address, a jump or conditional jump back is generated by 'again', 'until' ) diff --git a/src/zforth/zforth.c b/src/zforth/zforth.c index 5fa99eb..4fbd5ad 100644 --- a/src/zforth/zforth.c +++ b/src/zforth/zforth.c @@ -49,6 +49,7 @@ typedef enum { PRIM_JMP, PRIM_JMP0, PRIM_TICK, PRIM_COMMENT, PRIM_PUSHR, PRIM_POPR, PRIM_EQUAL, PRIM_SYS, PRIM_PICK, PRIM_COMMA, PRIM_KEY, PRIM_LITS, PRIM_LEN, PRIM_AND, PRIM_OR, PRIM_XOR, PRIM_SHL, PRIM_SHR, + PRIM_LITERAL, PRIM_COUNT } zf_prim; @@ -58,7 +59,8 @@ static const char prim_names[] = _("pickr") _("_immediate") _("@@") _("!!") _("swap") _("rot") _("jmp") _("jmp0") _("'") _("_(") _(">r") _("r>") _("=") _("sys") _("pick") _(",,") _("key") _("lits") - _("##") _("&") _("|") _("^") _("<<") _(">>"); + _("##") _("&") _("|") _("^") _("<<") _(">>") + _("_literal"); /* User variables are variables which are shared between forth and C. From @@ -552,6 +554,14 @@ static void do_prim(zf_ctx *ctx, zf_prim op, const char *input) COMPILING(ctx) = 0; break; + case PRIM_LITERAL: + /* At compile time, compiles a value from the stack into the + * definition as a literal. At run time, the value will be pushed + * on the stack. */ + if(COMPILING(ctx)) dict_add_lit(ctx, zf_pop(ctx)); + /* FIXME: else abort "!compiling"? */ + break; + case PRIM_LIT: /* At run time, push next value from dictionary on stack */ ctx->ip += dict_get_cell(ctx, ctx->ip, &d1);