2020-03-08 04:13:15 +11:00
|
|
|
; Return address of scratchpad in HL
|
|
|
|
pad:
|
|
|
|
ld hl, (HERE)
|
2020-03-08 04:50:54 +11:00
|
|
|
ld a, PADDING
|
2020-03-08 09:09:45 +11:00
|
|
|
jp addHL
|
2020-03-08 04:13:15 +11:00
|
|
|
|
|
|
|
; Read word from (INPUTPOS) and return, in HL, a null-terminated word.
|
|
|
|
; Advance (INPUTPOS) to the character following the whitespace ending the
|
|
|
|
; word.
|
|
|
|
; Z set of word was read, unset if end of line.
|
|
|
|
readword:
|
|
|
|
ld hl, (INPUTPOS)
|
|
|
|
; skip leading whitespace
|
|
|
|
dec hl ; offset leading "inc hl"
|
|
|
|
.loop1:
|
|
|
|
inc hl
|
|
|
|
ld a, (hl)
|
|
|
|
or a
|
|
|
|
jr z, .empty
|
|
|
|
cp ' '+1
|
|
|
|
jr c, .loop1
|
|
|
|
push hl ; --> lvl 1. that's our result
|
|
|
|
.loop2:
|
|
|
|
inc hl
|
|
|
|
ld a, (hl)
|
|
|
|
; special case: is A null? If yes, we will *not* inc A so that we don't
|
|
|
|
; go over the bounds of our input string.
|
|
|
|
or a
|
|
|
|
jr z, .noinc
|
|
|
|
cp ' '+1
|
|
|
|
jr nc, .loop2
|
|
|
|
; we've just read a whitespace, HL is pointing to it. Let's transform
|
|
|
|
; it into a null-termination, inc HL, then set (INPUTPOS).
|
|
|
|
xor a
|
|
|
|
ld (hl), a
|
|
|
|
inc hl
|
|
|
|
.noinc:
|
|
|
|
ld (INPUTPOS), hl
|
|
|
|
pop hl ; <-- lvl 1. our result
|
|
|
|
ret ; Z set from XOR A
|
|
|
|
.empty:
|
|
|
|
ld (hl), a
|
|
|
|
inc a ; unset Z
|
|
|
|
ret
|
|
|
|
|
2020-03-10 10:50:51 +11:00
|
|
|
; Sets Z if (HL) == E and (HL+1) == D
|
|
|
|
HLPointsDE:
|
2020-03-09 23:49:51 +11:00
|
|
|
ld a, (hl)
|
|
|
|
cp e
|
2020-03-10 10:50:51 +11:00
|
|
|
ret nz ; no
|
2020-03-09 23:49:51 +11:00
|
|
|
inc hl
|
|
|
|
ld a, (hl)
|
2020-03-10 10:50:51 +11:00
|
|
|
dec hl
|
2020-03-09 23:49:51 +11:00
|
|
|
cp d ; Z has our answer
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2020-03-10 10:50:51 +11:00
|
|
|
HLPointsNUMBER:
|
2020-03-09 23:49:51 +11:00
|
|
|
push de
|
|
|
|
ld de, NUMBER
|
2020-03-10 10:50:51 +11:00
|
|
|
call HLPointsDE
|
2020-03-09 23:49:51 +11:00
|
|
|
pop de
|
|
|
|
ret
|
|
|
|
|
2020-03-10 10:50:51 +11:00
|
|
|
HLPointsLIT:
|
2020-03-09 23:49:51 +11:00
|
|
|
push de
|
|
|
|
ld de, LIT
|
2020-03-10 10:50:51 +11:00
|
|
|
call HLPointsDE
|
2020-03-09 23:49:51 +11:00
|
|
|
pop de
|
|
|
|
ret
|
|
|
|
|
2020-03-13 12:16:20 +11:00
|
|
|
HLPointsBR:
|
2020-03-09 23:49:51 +11:00
|
|
|
push de
|
2020-03-13 12:16:20 +11:00
|
|
|
ld de, FBR
|
2020-03-10 10:50:51 +11:00
|
|
|
call HLPointsDE
|
2020-03-09 23:49:51 +11:00
|
|
|
pop de
|
|
|
|
ret
|
|
|
|
|
2020-03-10 10:50:51 +11:00
|
|
|
; Skip the compword where HL is currently pointing. If it's a regular word,
|
2020-03-09 23:49:51 +11:00
|
|
|
; it's easy: we inc by 2. If it's a NUMBER, we inc by 4. If it's a LIT, we skip
|
|
|
|
; to after null-termination.
|
|
|
|
compSkip:
|
2020-03-10 10:50:51 +11:00
|
|
|
call HLPointsNUMBER
|
2020-03-12 10:52:49 +11:00
|
|
|
jr z, .isNum
|
2020-03-13 12:16:20 +11:00
|
|
|
call HLPointsBR
|
2020-03-12 10:52:49 +11:00
|
|
|
jr z, .isBranch
|
2020-03-10 10:50:51 +11:00
|
|
|
call HLPointsLIT
|
|
|
|
jr nz, .isWord
|
2020-03-09 23:49:51 +11:00
|
|
|
; We have a literal
|
2020-03-10 10:50:51 +11:00
|
|
|
inc hl \ inc hl
|
2020-03-09 23:49:51 +11:00
|
|
|
call strskip
|
|
|
|
inc hl ; byte after word termination
|
2020-03-10 10:50:51 +11:00
|
|
|
ret
|
2020-03-12 10:52:49 +11:00
|
|
|
.isNum:
|
2020-03-09 23:49:51 +11:00
|
|
|
; skip by 4
|
2020-03-12 10:52:49 +11:00
|
|
|
inc hl
|
|
|
|
; continue to isBranch
|
|
|
|
.isBranch:
|
|
|
|
; skip by 3
|
|
|
|
inc hl
|
2020-03-10 10:50:51 +11:00
|
|
|
; continue to isWord
|
|
|
|
.isWord:
|
|
|
|
; skip by 2
|
|
|
|
inc hl \ inc hl
|
2020-03-09 23:49:51 +11:00
|
|
|
ret
|
|
|
|
|
2020-03-11 07:02:40 +11:00
|
|
|
; ***readLIT***
|
2020-03-10 10:50:51 +11:00
|
|
|
; The goal of this routine is to read a string literal following the currently
|
|
|
|
; executed words. For example, CREATE and DEFINE need this. Things are a little
|
|
|
|
; twisted, so bear with me while I explain how it works.
|
|
|
|
;
|
|
|
|
; When we call this routine, everything has been compiled. We're on an atom and
|
|
|
|
; we're executing it. Now, we're looking for a string literal or a word-with-a
|
2020-03-11 07:02:40 +11:00
|
|
|
; name that follows our readLIT caller. We could think that this word is
|
|
|
|
; right there on RS' TOS, but not always! You have to account for words wrapping
|
|
|
|
; the caller. For example, "VARIABLE" calls "CREATE". If you call
|
|
|
|
; "VARIABLE foo", if CREATE looks at what follows in RS' TOS, it will only find
|
|
|
|
; the "2" in "CREATE 2 ALLOT".
|
2020-03-10 10:50:51 +11:00
|
|
|
;
|
2020-03-11 07:02:40 +11:00
|
|
|
; In this case, we actually need to check in RS' *bottom of stack* for our
|
|
|
|
; answer. If that atom is a LIT, we're good. We make HL point to it and advance
|
|
|
|
; IP to byte following null-termination.
|
2020-03-10 10:50:51 +11:00
|
|
|
;
|
|
|
|
; If it isn't, things get interesting: If it's a word reference, then it's
|
2020-03-09 23:49:51 +11:00
|
|
|
; not an invalid literal. For example, one could want to redefine an existing
|
|
|
|
; word. So in that case, we'll copy the word's name on the pad (it might not be
|
|
|
|
; null-terminated) and set HL to point to it.
|
|
|
|
; How do we know that our reference is a word reference (it could be, for
|
|
|
|
; example, a NUMBER reference)? We check that its address is more than QUIT, the
|
|
|
|
; second word in our dict. We don't accept EXIT because it's the termination
|
|
|
|
; word. Yeah, it means that ";" can't be overridden...
|
|
|
|
; If name can't be read, we abort
|
2020-03-11 07:02:40 +11:00
|
|
|
;
|
|
|
|
; BOS vs TOS: What we cover so far is the "CREATE" and friends cases, where we
|
|
|
|
; want to read BOS. There are, however, cases where we want to read TOS, that is
|
|
|
|
; that we want to read the LIT right next to our atom. Example: "(". When
|
|
|
|
; processing comments, we are at compile time and want to read words from BOS,
|
|
|
|
; yes), however, in "("'s definition, there's "LIT@ )", which means "fetch LIT
|
|
|
|
; next to me and push this to stack". This LIT we want to fetch is *not* from
|
|
|
|
; BOS, it's from TOS.
|
|
|
|
;
|
|
|
|
; This is why we have readLITBOS and readLITTOS. readLIT uses HL and DE and is
|
|
|
|
; not used directly.
|
|
|
|
|
|
|
|
; Given a RS stack pointer HL, read LIT next to it (or abort) and set HL to
|
|
|
|
; point to its associated string. Set DE to there the RS stack pointer should
|
|
|
|
; point next.
|
|
|
|
readLIT:
|
2020-03-10 10:50:51 +11:00
|
|
|
call HLPointsLIT
|
2020-03-09 23:49:51 +11:00
|
|
|
jr nz, .notLIT
|
2020-03-10 10:50:51 +11:00
|
|
|
; RS BOS is a LIT, make HL point to string, then skip this RS compword.
|
2020-03-09 23:49:51 +11:00
|
|
|
inc hl \ inc hl ; HL now points to string itself
|
2020-03-11 07:02:40 +11:00
|
|
|
; HL has our its final value
|
|
|
|
ld d, h
|
|
|
|
ld e, l
|
2020-03-10 10:50:51 +11:00
|
|
|
call strskip
|
|
|
|
inc hl ; byte after word termination
|
2020-03-11 07:02:40 +11:00
|
|
|
ex de, hl
|
2020-03-10 10:50:51 +11:00
|
|
|
ret
|
2020-03-09 23:49:51 +11:00
|
|
|
.notLIT:
|
2020-03-13 02:39:27 +11:00
|
|
|
; Alright, not a literal, but is it a word?
|
|
|
|
call HLPointsUNWORD
|
2020-03-13 12:16:20 +11:00
|
|
|
jr z, .notWord
|
2020-03-09 23:49:51 +11:00
|
|
|
; Not a number, then it's a word. Copy word to pad and point to it.
|
2020-03-11 07:02:40 +11:00
|
|
|
push hl ; --> lvl 1. we need it to set DE later
|
2020-03-09 23:49:51 +11:00
|
|
|
call intoHL
|
|
|
|
or a ; clear carry
|
|
|
|
ld de, CODELINK_OFFSET
|
|
|
|
sbc hl, de
|
|
|
|
; That's our return value
|
2020-03-11 07:02:40 +11:00
|
|
|
push hl ; --> lvl 2
|
2020-03-09 23:49:51 +11:00
|
|
|
; HL now points to word offset, let'd copy it to pad
|
|
|
|
ex de, hl
|
|
|
|
call pad
|
|
|
|
ex de, hl
|
|
|
|
ld bc, NAMELEN
|
|
|
|
ldir
|
|
|
|
; null-terminate
|
|
|
|
xor a
|
|
|
|
ld (de), a
|
2020-03-11 07:02:40 +11:00
|
|
|
pop hl ; <-- lvl 2
|
|
|
|
pop de ; <-- lvl 1
|
|
|
|
; Advance IP by 2
|
|
|
|
inc de \ inc de
|
2020-03-09 23:49:51 +11:00
|
|
|
ret
|
|
|
|
.notWord:
|
|
|
|
ld hl, .msg
|
|
|
|
call printstr
|
|
|
|
jp abort
|
|
|
|
.msg:
|
|
|
|
.db "word expected", 0
|
|
|
|
|
2020-03-11 07:02:40 +11:00
|
|
|
readLITBOS:
|
2020-03-14 07:01:09 +11:00
|
|
|
; Before we start: is our RS empty? If IX == RS_ADDR, it is (it only has
|
|
|
|
; its safety net). When that happens, we actually want to run readLITTOS
|
|
|
|
push hl
|
|
|
|
push de
|
|
|
|
push ix \ pop hl
|
|
|
|
ld de, RS_ADDR
|
|
|
|
or a ; clear carry
|
|
|
|
sbc hl, de
|
|
|
|
pop de
|
|
|
|
pop hl
|
|
|
|
jr z, readLITTOS
|
2020-03-11 07:02:40 +11:00
|
|
|
push de
|
2020-03-14 07:01:09 +11:00
|
|
|
; Our bottom-of-stack is RS_ADDR+2 because RS_ADDR is occupied by our
|
|
|
|
; ABORTREF safety net.
|
|
|
|
ld hl, (RS_ADDR+2)
|
2020-03-11 07:02:40 +11:00
|
|
|
call readLIT
|
2020-03-14 07:01:09 +11:00
|
|
|
ld (RS_ADDR+2), de
|
2020-03-11 07:02:40 +11:00
|
|
|
pop de
|
|
|
|
ret
|
|
|
|
|
|
|
|
readLITTOS:
|
|
|
|
push de
|
2020-03-14 07:01:09 +11:00
|
|
|
ld hl, (IP)
|
2020-03-11 07:02:40 +11:00
|
|
|
call readLIT
|
2020-03-14 07:01:09 +11:00
|
|
|
ld (IP), de
|
2020-03-11 07:02:40 +11:00
|
|
|
pop de
|
|
|
|
ret
|
|
|
|
|
2020-03-08 04:13:15 +11:00
|
|
|
; Find the entry corresponding to word where (HL) points to and sets DE to
|
|
|
|
; point to that entry.
|
|
|
|
; Z if found, NZ if not.
|
|
|
|
find:
|
2020-03-10 06:12:44 +11:00
|
|
|
push hl
|
|
|
|
push bc
|
2020-03-08 04:13:15 +11:00
|
|
|
ld de, (CURRENT)
|
2020-03-10 06:12:44 +11:00
|
|
|
ld bc, CODELINK_OFFSET
|
2020-03-08 04:13:15 +11:00
|
|
|
.inner:
|
2020-03-10 06:12:44 +11:00
|
|
|
; DE is a wordref, let's go to beginning of struct
|
|
|
|
push de ; --> lvl 1
|
|
|
|
or a ; clear carry
|
|
|
|
ex de, hl
|
|
|
|
sbc hl, bc
|
|
|
|
ex de, hl ; We're good, DE points to word name
|
2020-03-08 09:09:45 +11:00
|
|
|
ld a, NAMELEN
|
2020-03-08 04:13:15 +11:00
|
|
|
call strncmp
|
2020-03-10 06:12:44 +11:00
|
|
|
pop de ; <-- lvl 1, return to wordref
|
|
|
|
jr z, .end ; found
|
2020-03-13 02:39:27 +11:00
|
|
|
call .prev
|
2020-03-08 04:13:15 +11:00
|
|
|
jr nz, .inner
|
|
|
|
; Z set? end of dict unset Z
|
|
|
|
inc a
|
2020-03-10 06:12:44 +11:00
|
|
|
.end:
|
|
|
|
pop bc
|
|
|
|
pop hl
|
2020-03-08 04:13:15 +11:00
|
|
|
ret
|
|
|
|
|
2020-03-13 02:39:27 +11:00
|
|
|
; For DE being a wordref, move DE to the previous wordref.
|
|
|
|
; Z is set if DE point to 0 (no entry). NZ if not.
|
|
|
|
.prev:
|
|
|
|
dec de \ dec de \ dec de ; prev field
|
|
|
|
call intoDE
|
|
|
|
; DE points to prev. Is it zero?
|
|
|
|
xor a
|
|
|
|
or d
|
|
|
|
or e
|
|
|
|
; Z will be set if DE is zero
|
|
|
|
ret
|
|
|
|
|
2020-03-08 11:25:55 +11:00
|
|
|
; Write compiled data from HL into IY, advancing IY at the same time.
|
|
|
|
wrCompHL:
|
|
|
|
ld (iy), l
|
|
|
|
inc iy
|
|
|
|
ld (iy), h
|
|
|
|
inc iy
|
|
|
|
ret
|
|
|
|
|
2020-03-08 10:53:20 +11:00
|
|
|
; Spit name + prev in (HERE) and adjust (HERE) and (CURRENT)
|
|
|
|
; HL points to new (HERE)
|
|
|
|
entryhead:
|
2020-03-11 07:02:40 +11:00
|
|
|
call readLITBOS
|
2020-03-08 10:53:20 +11:00
|
|
|
ld de, (HERE)
|
|
|
|
call strcpy
|
|
|
|
ex de, hl ; (HERE) now in HL
|
|
|
|
ld de, (CURRENT)
|
|
|
|
ld a, NAMELEN
|
|
|
|
call addHL
|
2020-03-13 02:39:27 +11:00
|
|
|
call DEinHL
|
|
|
|
; Set word flags: not IMMED, not UNWORD, so it's 0
|
|
|
|
xor a
|
2020-03-10 10:50:51 +11:00
|
|
|
ld (hl), a
|
|
|
|
inc hl
|
2020-03-10 06:12:44 +11:00
|
|
|
ld (CURRENT), hl
|
2020-03-08 10:53:20 +11:00
|
|
|
ld (HERE), hl
|
|
|
|
ret
|
2020-03-10 13:13:11 +11:00
|
|
|
|
2020-03-11 12:37:06 +11:00
|
|
|
; Sets Z if wordref at HL is of the IMMEDIATE type
|
|
|
|
HLisIMMED:
|
2020-03-10 13:13:11 +11:00
|
|
|
dec hl
|
2020-03-13 02:39:27 +11:00
|
|
|
bit FLAG_IMMED, (hl)
|
2020-03-10 13:13:11 +11:00
|
|
|
inc hl
|
2020-03-13 02:39:27 +11:00
|
|
|
; We need an invert flag. We want to Z to be set when flag is non-zero.
|
|
|
|
jp toggleZ
|
2020-03-11 12:37:06 +11:00
|
|
|
|
|
|
|
; Sets Z if wordref at (HL) is of the IMMEDIATE type
|
|
|
|
HLPointsIMMED:
|
|
|
|
push hl
|
|
|
|
call intoHL
|
|
|
|
call HLisIMMED
|
2020-03-10 13:13:11 +11:00
|
|
|
pop hl
|
|
|
|
ret
|
2020-03-11 07:02:40 +11:00
|
|
|
|
2020-03-13 02:39:27 +11:00
|
|
|
; Sets Z if wordref at HL is of the UNWORD type
|
|
|
|
HLisUNWORD:
|
|
|
|
dec hl
|
|
|
|
bit FLAG_UNWORD, (hl)
|
|
|
|
inc hl
|
|
|
|
; We need an invert flag. We want to Z to be set when flag is non-zero.
|
|
|
|
jp toggleZ
|
|
|
|
|
|
|
|
; Sets Z if wordref at (HL) is of the IMMEDIATE type
|
|
|
|
HLPointsUNWORD:
|
|
|
|
push hl
|
|
|
|
call intoHL
|
|
|
|
call HLisUNWORD
|
|
|
|
pop hl
|
|
|
|
ret
|
|
|
|
|
2020-03-11 07:02:40 +11:00
|
|
|
; Checks flags Z and C and sets BC to 0 if Z, 1 if C and -1 otherwise
|
|
|
|
flagsToBC:
|
|
|
|
ld bc, 0
|
|
|
|
ret z ; equal
|
|
|
|
inc bc
|
|
|
|
ret c ; >
|
|
|
|
; <
|
|
|
|
dec bc
|
|
|
|
dec bc
|
|
|
|
ret
|
|
|
|
|
2020-03-11 12:37:06 +11:00
|
|
|
; Write DE in (HL), advancing HL by 2.
|
|
|
|
DEinHL:
|
|
|
|
ld (hl), e
|
|
|
|
inc hl
|
|
|
|
ld (hl), d
|
|
|
|
inc hl
|
|
|
|
ret
|