From 764b2222c7bce6a109b6dc496f196bcd55b61225 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 14 Mar 2020 09:23:58 -0400 Subject: [PATCH] forth: replace (fbr?) by SKIP? This will allow us to support backward branching with just one new (bbr) word. Also, this allow us to have "(" word sooned in core.fth and thus allow for earlier commenting. --- apps/forth/core.fth | 22 +++++++++++++--------- apps/forth/dict.asm | 36 +++++++++++++++++------------------- apps/forth/dictionary.txt | 5 +++-- apps/forth/util.asm | 4 ---- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/apps/forth/core.fth b/apps/forth/core.fth index 6aab627..b01ddc7 100644 --- a/apps/forth/core.fth +++ b/apps/forth/core.fth @@ -4,20 +4,24 @@ : ALLOT HERE +! ; : , H ! 2 ALLOT ; : C, H C! 1 ALLOT ; -: IF ['] (fbr?) , H 1 ALLOT ; IMMEDIATE -: THEN DUP H -^ SWAP C! ; IMMEDIATE -: ELSE ['] (fbr) , 1 ALLOT DUP H -^ SWAP C! H 1 - ; IMMEDIATE +: NOT 1 SWAP SKIP? EXIT 0 * ; : RECURSE R> R> 2 - >R >R EXIT ; -: ( LIT@ ) WORD SCMP IF RECURSE THEN ; IMMEDIATE +: ( LIT@ ) WORD SCMP NOT SKIP? RECURSE ; IMMEDIATE ( Hello, hello, krkrkrkr... do you hear me? ) ( Ah, voice at last! Some lines above need comments ) -( IF: write (fbr?) addr, push HERE, create cell ) -( THEN: Subtract TOS from H to get offset to write to cell ) -( in that same TOS's addr ) -( ELSE: write (fbr) addr, allot, then same as THEN ) +( BTW: Forth lines limited to 64 cols because of default ) +( input buffer size in Collapse OS ) +( NOT: a bit convulted because we don't have IF yet ) ( RECURSE: RS TOS is for RECURSE itself, then we have to dig ) ( one more level to get to RECURSE's parent's caller. ) -: NOT IF 0 ELSE 1 THEN ; +( IF true, skip following (fbr). Also, push br cell ref H, ) +( to PS ) +: IF ['] SKIP? , ['] (fbr) , H 1 ALLOT ; IMMEDIATE +( Subtract TOS from H to get offset to write to IF or ELSE's ) +( br cell ) +: THEN DUP H -^ SWAP C! ; IMMEDIATE +( write (fbr) addr, allot, then same as THEN ) +: ELSE ['] (fbr) , 1 ALLOT DUP H -^ SWAP C! H 1 - ; IMMEDIATE : ? @ . ; : VARIABLE CREATE 2 ALLOT ; : CONSTANT CREATE H ! DOES> @ ; diff --git a/apps/forth/dict.asm b/apps/forth/dict.asm index 6667760..9d66136 100644 --- a/apps/forth/dict.asm +++ b/apps/forth/dict.asm @@ -711,12 +711,27 @@ CMP: push bc jp next + .db "SKIP?" + .fill 2 + .dw CMP + .db 0 +CSKIP: + .dw nativeWord + pop hl + ld a, h + or l + jp z, next ; False, do nothing. + ld hl, (IP) + call compSkip + ld (IP), hl + jp next + ; This word's atom is followed by 1b *relative* offset (to the cell's addr) to ; where to branch to. For example, The branching cell of "IF THEN" would ; contain 3. Add this value to RS. .db "(fbr)" .fill 2 - .dw CMP + .dw CSKIP .db 0 FBR: .dw nativeWord @@ -728,23 +743,6 @@ FBR: pop de jp next -; Conditional branch, only branch if TOS is zero - .db "(fbr?)" - .fill 1 - .dw FBR - .db 0 -FBRC: - .dw nativeWord - pop hl - ld a, h - or l - jr z, FBR+2 - ; skip next byte in RS - ld hl, (IP) - inc hl - ld (IP), hl - jp next - LATEST: - .dw FBRC + .dw FBR diff --git a/apps/forth/dictionary.txt b/apps/forth/dictionary.txt index 230e455..ecf236e 100644 --- a/apps/forth/dictionary.txt +++ b/apps/forth/dictionary.txt @@ -49,8 +49,6 @@ LITERAL n -- *I* Inserts number from TOS as a literal VARIABLE c -- Creates cell x with 2 bytes allocation. *** Flow *** -(fbr?) f -- Conditionally branches forward by the number - specified in its atom's cell. (fbr) -- Branches forward by the number specified in its atom's cell. ELSE I:a -- *I* Compiles a (fbr) and set branching cell at a. @@ -60,6 +58,9 @@ INTERPRET -- Get a line from stdin, compile it in tmp memory, then execute the compiled contents. QUIT R:drop -- Return to interpreter promp immediately RECURSE R:I -- R:I-2 Run the current word again. +SKIP? f -- If f is true, skip the execution of the next atom. + Use this right before ";" and you're gonna have a + bad time. THEN I:a -- *I* Set branching cell at a. *** Parameter Stack *** diff --git a/apps/forth/util.asm b/apps/forth/util.asm index 4dec153..b2b1a7d 100644 --- a/apps/forth/util.asm +++ b/apps/forth/util.asm @@ -73,10 +73,6 @@ HLPointsBR: push de ld de, FBR call HLPointsDE - jr z, .end - ld de, FBRC - call HLPointsDE -.end: pop de ret