forth: make branching cells store relative offsets

This should help with fixing IF/THEN/ELSE in colon defs.
This commit is contained in:
Virgil Dupras 2020-03-11 16:46:25 -04:00
parent 6757c097ea
commit 02b56c547a

View File

@ -51,13 +51,22 @@ doesWord:
push hl \ pop iy push hl \ pop iy
jr compiledWord jr compiledWord
; This word is followed by a wordref to branch to. Set RS to that address. ; This word is followed by *relative* offset (to the cell's addr) to where to
; branch to. For example, The branching cell of "IF THEN" would contain 4. Add
; this value to RS.
branchWord: branchWord:
push de
ld l, (ix) ld l, (ix)
ld h, (ix+1) ld h, (ix+1)
call intoHL ld e, (hl)
inc hl
ld d, (hl)
dec hl
or a ; clear carry
add hl, de
ld (ix), l ld (ix), l
ld (ix+1), h ld (ix+1), h
pop de
jp exit jp exit
BRANCH: BRANCH:
@ -574,6 +583,15 @@ IF:
.dw IF .dw IF
ELSE: ELSE:
.dw nativeWord .dw nativeWord
; First, let's set IF's branching cell.
pop de ; cell's address
ld hl, (CMPDST)
; also skip ELSE word.
inc hl \ inc hl \ inc hl \ inc hl
or a ; clear carry
sbc hl, de ; HL now has relative offset
ex de, hl ; HL has branching cell
call DEinHL
; Set IF's branching cell to current atom address and spit our own ; Set IF's branching cell to current atom address and spit our own
; uncondition branching cell, which will then be picked up by THEN. ; uncondition branching cell, which will then be picked up by THEN.
; First, let's spit our 4 bytes ; First, let's spit our 4 bytes
@ -584,13 +602,6 @@ ELSE:
ld de, ABORTREF ld de, ABORTREF
call DEinHL call DEinHL
ld (CMPDST), hl ld (CMPDST), hl
; We've spit our ELSE bytes, but we haven't updated our IF's forward
; branching cell. That cell's address is currently at (SP-2). Let's do
; some stack-fu to get it.
ex de, hl ; value to write now in DE
pop hl
ex (sp), hl ; IF's cell's address now in HL
call DEinHL
jp exit jp exit
.db "THEN" .db "THEN"
@ -600,8 +611,12 @@ ELSE:
THEN: THEN:
.dw nativeWord .dw nativeWord
; See comments in IF and ELSE ; See comments in IF and ELSE
pop hl ; where to put our own address pop de ; cell's address
ld de, (CMPDST) ; that's our branching address ld hl, (CMPDST)
; There is nothing to skip because THEN leaves nothing.
or a ; clear carry
sbc hl, de ; HL now has relative offset
ex de, hl ; HL has branching cell
call DEinHL call DEinHL
jp exit jp exit