Skip to content

Commit

Permalink
wip literal per ANS forth
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Dec 16, 2024
1 parent e01d6c5 commit 8f787f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions forth/core.zf
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
Expand Down
12 changes: 11 additions & 1 deletion src/zforth/zforth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8f787f3

Please sign in to comment.