1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-27 17:48:05 +11:00

forth: allow DEFINE's ";" to be at the start of a newline

Previous to this commit, DEFINE's algo would miss a ";" right after a newline.

This allows for a much nicer formatting+comment of core.fs.
This commit is contained in:
Virgil Dupras 2020-03-16 22:09:23 -04:00
parent aec98a7a3a
commit 5d5517ac44
3 changed files with 45 additions and 24 deletions

View File

@ -20,13 +20,27 @@
a reference to "," so that this word is written to HERE. a reference to "," so that this word is written to HERE.
NOT: a bit convulted because we don't have IF yet ) NOT: a bit convulted because we don't have IF yet )
: IF COMPILE SKIP? COMPILE (fbr) H 1 ALLOT ; IMMEDIATE
( Subtract TOS from H to get offset to write to IF or ELSE's : IF ( -- a | a: br cell addr )
br cell ) COMPILE SKIP? ( if true, don't branch )
: THEN DUP H -^ SWAP C! ; IMMEDIATE COMPILE (fbr)
( write (fbr) addr, allot, then same as THEN ) H ( push a )
: ELSE 1 ALLOT ( br cell allot )
COMPILE (fbr) 1 ALLOT DUP H -^ SWAP C! H 1 - ; IMMEDIATE ; IMMEDIATE
: THEN ( a -- | a: br cell addr )
DUP H -^ SWAP ( a-H a )
C!
; IMMEDIATE
: ELSE ( a1 -- a2 | a1: IF cell a2: ELSE cell )
COMPILE (fbr)
1 ALLOT
DUP H -^ SWAP ( a-H a )
C!
H 1 - ( push a. -1 for allot offset )
; IMMEDIATE
: ? @ . ; : ? @ . ;
: VARIABLE CREATE 2 ALLOT ; : VARIABLE CREATE 2 ALLOT ;
: CONSTANT CREATE H ! DOES> @ ; : CONSTANT CREATE H ! DOES> @ ;

View File

@ -278,7 +278,7 @@ DEFINE:
ld (HERE), hl ld (HERE), hl
.loop: .loop:
; did we reach ";"? ; did we reach ";"?
ld hl, (INPUTPOS) call toword
ld a, (hl) ld a, (hl)
cp ';' cp ';'
jr nz, .compile jr nz, .compile

View File

@ -4,25 +4,35 @@ pad:
ld a, PADDING ld a, PADDING
jp addHL jp addHL
; Advance (INPUTPOS) until a non-whitespace is met. If needed,
; call fetchline.
; Set HL to newly set (INPUTPOS)
toword:
ld hl, (INPUTPOS)
; skip leading whitespace
dec hl ; offset leading "inc hl"
.loop:
inc hl
ld a, (hl)
or a
; When at EOL, fetch a new line directly
jr z, .empty
cp ' '+1
jr c, .loop
ret
.empty:
call fetchline
jr toword
; Read word from (INPUTPOS) and return, in HL, a null-terminated word. ; Read word from (INPUTPOS) and return, in HL, a null-terminated word.
; Advance (INPUTPOS) to the character following the whitespace ending the ; Advance (INPUTPOS) to the character following the whitespace ending the
; word. ; word.
; When we're at EOL, we call fetchline directly, so this call always returns ; When we're at EOL, we call fetchline directly, so this call always returns
; a word. ; a word.
readword: readword:
ld hl, (INPUTPOS) call toword
; skip leading whitespace
dec hl ; offset leading "inc hl"
.loop1:
inc hl
ld a, (hl)
or a
; When at EOL, fetch a new line directly
jr z, .empty
cp ' '+1
jr c, .loop1
push hl ; --> lvl 1. that's our result push hl ; --> lvl 1. that's our result
.loop2: .loop:
inc hl inc hl
ld a, (hl) ld a, (hl)
; special case: is A null? If yes, we will *not* inc A so that we don't ; special case: is A null? If yes, we will *not* inc A so that we don't
@ -30,7 +40,7 @@ readword:
or a or a
jr z, .noinc jr z, .noinc
cp ' '+1 cp ' '+1
jr nc, .loop2 jr nc, .loop
; we've just read a whitespace, HL is pointing to it. Let's transform ; we've just read a whitespace, HL is pointing to it. Let's transform
; it into a null-termination, inc HL, then set (INPUTPOS). ; it into a null-termination, inc HL, then set (INPUTPOS).
xor a xor a
@ -40,9 +50,6 @@ readword:
ld (INPUTPOS), hl ld (INPUTPOS), hl
pop hl ; <-- lvl 1. our result pop hl ; <-- lvl 1. our result
ret ; Z set from XOR A ret ; Z set from XOR A
.empty:
call fetchline
jr readword
; Sets Z if (HL) == E and (HL+1) == D ; Sets Z if (HL) == E and (HL+1) == D
HLPointsDE: HLPointsDE: