2020-03-08 04:13:15 +11:00
|
|
|
; A dictionary entry has this structure:
|
2020-03-10 10:50:51 +11:00
|
|
|
; - 7b name (zero-padded)
|
|
|
|
; - 1b flags (bit 0: IMMEDIATE)
|
2020-03-08 04:13:15 +11:00
|
|
|
; - 2b prev pointer
|
|
|
|
; - 2b code pointer
|
2020-03-08 09:09:45 +11:00
|
|
|
; - Parameter field (PF)
|
2020-03-08 04:13:15 +11:00
|
|
|
;
|
|
|
|
; The code pointer point to "word routines". These routines expect to be called
|
2020-03-08 09:09:45 +11:00
|
|
|
; with IY pointing to the PF. They themselves are expected to end by jumping
|
2020-03-08 04:13:15 +11:00
|
|
|
; to the address at the top of the Return Stack. They will usually do so with
|
|
|
|
; "jp exit".
|
|
|
|
|
2020-03-08 09:09:45 +11:00
|
|
|
; Execute a word containing native code at its PF address (PFA)
|
2020-03-08 04:13:15 +11:00
|
|
|
nativeWord:
|
|
|
|
jp (iy)
|
|
|
|
|
2020-03-10 06:12:44 +11:00
|
|
|
; Execute a list of atoms, which usually ends with EXIT.
|
|
|
|
; IY points to that list.
|
2020-03-08 04:13:15 +11:00
|
|
|
compiledWord:
|
|
|
|
push iy \ pop hl
|
|
|
|
inc hl
|
|
|
|
inc hl
|
|
|
|
; HL points to next Interpreter pointer.
|
|
|
|
call pushRS
|
|
|
|
ld l, (iy)
|
|
|
|
ld h, (iy+1)
|
|
|
|
push hl \ pop iy
|
|
|
|
; IY points to code link
|
|
|
|
jp executeCodeLink
|
|
|
|
|
2020-03-08 09:09:45 +11:00
|
|
|
; Pushes the PFA directly
|
|
|
|
cellWord:
|
|
|
|
push iy
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
; Pushes the address in the first word of the PF
|
|
|
|
sysvarWord:
|
|
|
|
ld l, (iy)
|
|
|
|
ld h, (iy+1)
|
|
|
|
push hl
|
|
|
|
jp exit
|
|
|
|
|
2020-03-08 14:18:14 +11:00
|
|
|
; The word was spawned from a definition word that has a DOES>. PFA+2 (right
|
|
|
|
; after the actual cell) is a link to the slot right after that DOES>.
|
|
|
|
; Therefore, what we need to do push the cell addr like a regular cell, then
|
|
|
|
; follow the link from the PFA, and then continue as a regular compiledWord.
|
|
|
|
doesWord:
|
|
|
|
push iy ; like a regular cell
|
|
|
|
ld l, (iy+2)
|
|
|
|
ld h, (iy+3)
|
|
|
|
push hl \ pop iy
|
|
|
|
jr compiledWord
|
|
|
|
|
2020-03-10 05:19:51 +11:00
|
|
|
; The IF word checks the stack for zero. If it's non-zero, it does nothing and
|
|
|
|
; allow compiledWord to continue.
|
|
|
|
; If it's zero, it tracksback RS, advance it until it finds a ELSE, a THEN, or
|
|
|
|
; an EXIT (not supposed to happen unless the IF is misconstructed). Whether
|
|
|
|
; it's a ELSE or THEN, the same thing happens: we resume execution after the
|
|
|
|
; ELSE/THEN. If it's a EXIT, we simply execute it.
|
|
|
|
ifWord:
|
|
|
|
pop hl
|
|
|
|
ld a, h
|
|
|
|
or l
|
|
|
|
jp nz, exit ; non-zero, continue
|
|
|
|
; Zero, seek ELSE, THEN or EXIT. Continue to elseWord
|
|
|
|
|
|
|
|
; If a ELSE word is executed, it means that the preceding IF had a non-zero
|
|
|
|
; condition and continued execution. This means that upon encountering an ELSE,
|
|
|
|
; we must search for a THEN or an EXIT.
|
|
|
|
; To simplify implementation and share code with ifWord, we also match ELSE,
|
|
|
|
; which is only possible in malformed construct. Therefore "IF ELSE ELSE" is
|
|
|
|
; valid and interpreted as "IF ELSE THEN".
|
|
|
|
elseWord:
|
|
|
|
; to save processing, we test EXIT, ELSE and THEN in the order they
|
|
|
|
; appear, address-wise. This way, we don't need to push/pop HL: we can
|
|
|
|
; SUB the difference between the words and check for zeroes.
|
|
|
|
call popRS
|
|
|
|
; We need to save that IP somewhere. Let it be BC
|
|
|
|
ld b, h
|
|
|
|
ld c, l
|
|
|
|
.loop:
|
|
|
|
; Whether there's a match or not, we will resume the operation at IP+2,
|
|
|
|
; which means that we have to increase BC anyways. Let's do it now.
|
|
|
|
inc bc \ inc bc
|
|
|
|
call intoHL
|
|
|
|
or a ; clear carry
|
2020-03-10 06:12:44 +11:00
|
|
|
ld de, EXIT
|
2020-03-10 05:19:51 +11:00
|
|
|
sbc hl, de
|
|
|
|
jp z, exit
|
|
|
|
; Not EXIT, let's continue with ELSE. No carry possible because EXIT
|
|
|
|
; is first word. No need to clear.
|
|
|
|
ld de, ELSE-EXIT
|
|
|
|
sbc hl, de
|
|
|
|
jr c, .nomatch ; A word between EXIT and ELSE. No match.
|
|
|
|
jr z, .match ; We have a ELSE
|
|
|
|
; Let's try with THEN. Again, no carry possible, C cond was handled.
|
|
|
|
ld de, THEN-ELSE
|
|
|
|
sbc hl, de
|
|
|
|
jr z, .match ; We have a THEN
|
|
|
|
.nomatch:
|
|
|
|
; Nothing matched, which means that we need to continue looking.
|
|
|
|
; BC is already IP+2
|
|
|
|
ld h, b
|
|
|
|
ld l, c
|
|
|
|
jr .loop
|
|
|
|
.match:
|
|
|
|
; Matched a ELSE or a THEN, which means we need to continue executing
|
|
|
|
; word from IP+2, which is already in BC.
|
|
|
|
push bc \ pop iy
|
|
|
|
jp compiledWord
|
|
|
|
|
|
|
|
; This word does nothing. It's never going to be executed unless the wordlist
|
|
|
|
; is misconstructed.
|
|
|
|
thenWord:
|
|
|
|
jp exit
|
|
|
|
|
2020-03-08 11:25:55 +11:00
|
|
|
; This is not a word, but a number literal. This works a bit differently than
|
|
|
|
; others: PF means nothing and the actual number is placed next to the
|
|
|
|
; numberWord reference in the compiled word list. What we need to do to fetch
|
|
|
|
; that number is to play with the Return stack: We pop it, read the number, push
|
|
|
|
; it to the Parameter stack and then push an increase Interpreter Pointer back
|
|
|
|
; to RS.
|
|
|
|
numberWord:
|
|
|
|
call popRS
|
|
|
|
ld e, (hl)
|
|
|
|
inc hl
|
|
|
|
ld d, (hl)
|
|
|
|
inc hl
|
|
|
|
call pushRS
|
|
|
|
push de
|
|
|
|
jp exit
|
|
|
|
NUMBER:
|
|
|
|
.dw numberWord
|
|
|
|
|
2020-03-09 23:49:51 +11:00
|
|
|
; Similarly to numberWord, this is not a real word, but a string literal.
|
|
|
|
; Instead of being followed by a 2 bytes number, it's followed by a
|
|
|
|
; null-terminated string. This is not expected to be called in a regular
|
|
|
|
; context. Only words expecting those literals will look for them. This is why
|
|
|
|
; the litWord triggers abort.
|
|
|
|
litWord:
|
|
|
|
call popRS
|
|
|
|
call intoHL
|
|
|
|
call printstr ; let's print the word before abort.
|
|
|
|
ld hl, .msg
|
|
|
|
call printstr
|
|
|
|
jp abort
|
|
|
|
.msg:
|
|
|
|
.db "undefined word", 0
|
|
|
|
LIT:
|
|
|
|
.dw litWord
|
2020-03-08 11:25:55 +11:00
|
|
|
|
2020-03-08 04:13:15 +11:00
|
|
|
; ( R:I -- )
|
2020-03-09 23:49:51 +11:00
|
|
|
.db ";"
|
|
|
|
.fill 7
|
2020-03-08 04:13:15 +11:00
|
|
|
.dw 0
|
2020-03-10 06:12:44 +11:00
|
|
|
EXIT:
|
2020-03-08 04:13:15 +11:00
|
|
|
.dw nativeWord
|
|
|
|
; When we call the EXIT word, we have to do a "double exit" because our current
|
|
|
|
; Interpreter pointer is pointing to the word *next* to our EXIT reference when,
|
|
|
|
; in fact, we want to continue processing the one above it.
|
|
|
|
call popRS
|
|
|
|
exit:
|
2020-03-08 12:20:11 +11:00
|
|
|
; Before we continue: is SP within bounds?
|
|
|
|
call chkPS
|
|
|
|
; we're good
|
2020-03-08 04:13:15 +11:00
|
|
|
call popRS
|
|
|
|
; We have a pointer to a word
|
|
|
|
push hl \ pop iy
|
2020-03-09 23:49:51 +11:00
|
|
|
jp compiledWord
|
2020-03-08 04:13:15 +11:00
|
|
|
|
2020-03-08 09:09:45 +11:00
|
|
|
; ( R:I -- )
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "QUIT"
|
|
|
|
.fill 4
|
2020-03-08 09:09:45 +11:00
|
|
|
.dw EXIT
|
2020-03-10 06:12:44 +11:00
|
|
|
QUIT:
|
2020-03-08 09:09:45 +11:00
|
|
|
.dw nativeWord
|
|
|
|
quit:
|
2020-03-09 23:49:51 +11:00
|
|
|
jp forthRdLine
|
2020-03-08 12:20:11 +11:00
|
|
|
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "ABORT"
|
|
|
|
.fill 3
|
2020-03-08 12:20:11 +11:00
|
|
|
.dw QUIT
|
2020-03-10 06:12:44 +11:00
|
|
|
ABORT:
|
2020-03-08 12:20:11 +11:00
|
|
|
.dw nativeWord
|
|
|
|
abort:
|
2020-03-09 23:49:51 +11:00
|
|
|
; Reinitialize PS (RS is reinitialized in forthInterpret
|
2020-03-08 12:20:11 +11:00
|
|
|
ld sp, (INITIAL_SP)
|
2020-03-09 23:49:51 +11:00
|
|
|
jp forthRdLine
|
2020-03-08 09:09:45 +11:00
|
|
|
|
2020-03-08 05:15:19 +11:00
|
|
|
.db "BYE"
|
|
|
|
.fill 5
|
2020-03-08 12:20:11 +11:00
|
|
|
.dw ABORT
|
2020-03-10 06:12:44 +11:00
|
|
|
BYE:
|
2020-03-08 05:15:19 +11:00
|
|
|
.dw nativeWord
|
2020-03-09 23:49:51 +11:00
|
|
|
; Goodbye Forth! Before we go, let's restore the stack
|
|
|
|
ld sp, (INITIAL_SP)
|
|
|
|
; unwind stack underflow buffer
|
|
|
|
pop af \ pop af \ pop af
|
|
|
|
; success
|
|
|
|
xor a
|
|
|
|
ret
|
2020-03-08 05:15:19 +11:00
|
|
|
|
2020-03-08 04:13:15 +11:00
|
|
|
; ( c -- )
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "EMIT"
|
|
|
|
.fill 4
|
2020-03-08 05:15:19 +11:00
|
|
|
.dw BYE
|
2020-03-10 06:12:44 +11:00
|
|
|
EMIT:
|
2020-03-08 04:13:15 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop hl
|
|
|
|
ld a, l
|
|
|
|
call stdioPutC
|
|
|
|
jp exit
|
|
|
|
|
2020-03-11 04:00:57 +11:00
|
|
|
; ( c port -- )
|
|
|
|
.db "PC!"
|
|
|
|
.fill 5
|
|
|
|
.dw EMIT
|
|
|
|
PSTORE:
|
|
|
|
.dw nativeWord
|
|
|
|
pop bc
|
|
|
|
pop hl
|
|
|
|
out (c), l
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
; ( port -- c )
|
|
|
|
.db "PC@"
|
|
|
|
.fill 5
|
|
|
|
.dw PSTORE
|
|
|
|
PFETCH:
|
|
|
|
.dw nativeWord
|
|
|
|
pop bc
|
|
|
|
ld h, 0
|
|
|
|
in l, (c)
|
|
|
|
push hl
|
|
|
|
jp exit
|
|
|
|
|
2020-03-08 04:13:15 +11:00
|
|
|
; ( addr -- )
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "EXECUTE"
|
|
|
|
.db 0
|
2020-03-11 04:00:57 +11:00
|
|
|
.dw PFETCH
|
2020-03-10 06:12:44 +11:00
|
|
|
EXECUTE:
|
2020-03-08 04:13:15 +11:00
|
|
|
.dw nativeWord
|
2020-03-10 06:12:44 +11:00
|
|
|
pop iy ; is a wordref
|
2020-03-08 04:13:15 +11:00
|
|
|
executeCodeLink:
|
|
|
|
ld l, (iy)
|
|
|
|
ld h, (iy+1)
|
|
|
|
; HL points to code pointer
|
|
|
|
inc iy
|
|
|
|
inc iy
|
|
|
|
; IY points to PFA
|
|
|
|
jp (hl) ; go!
|
|
|
|
|
2020-03-08 10:53:20 +11:00
|
|
|
.db ":"
|
|
|
|
.fill 7
|
|
|
|
.dw EXECUTE
|
2020-03-10 06:12:44 +11:00
|
|
|
DEFINE:
|
2020-03-08 10:53:20 +11:00
|
|
|
.dw nativeWord
|
|
|
|
call entryhead
|
|
|
|
ld de, compiledWord
|
|
|
|
ld (hl), e
|
|
|
|
inc hl
|
|
|
|
ld (hl), d
|
|
|
|
inc hl
|
2020-03-09 23:49:51 +11:00
|
|
|
; At this point, we've processed the name literal following the ':'.
|
|
|
|
; What's next? We have, in IP, a pointer to words that *have already
|
|
|
|
; been compiled by INTERPRET*. All those bytes will be copied as-is.
|
|
|
|
; All we need to do is to know how many bytes to copy. To do so, we
|
|
|
|
; skip compwords until EXIT is reached.
|
2020-03-10 13:13:11 +11:00
|
|
|
ex de, hl ; DE is our dest
|
2020-03-10 10:50:51 +11:00
|
|
|
ld l, (ix)
|
|
|
|
ld h, (ix+1)
|
2020-03-08 10:53:20 +11:00
|
|
|
.loop:
|
2020-03-10 13:13:11 +11:00
|
|
|
call HLPointsNUMBER
|
|
|
|
jr nz, .notNUMBER
|
|
|
|
; is number
|
|
|
|
ld bc, 4
|
|
|
|
ldir
|
2020-03-08 10:53:20 +11:00
|
|
|
jr .loop
|
2020-03-10 13:13:11 +11:00
|
|
|
.notNUMBER:
|
|
|
|
call HLPointsLIT
|
|
|
|
jr nz, .notLIT
|
|
|
|
; is lit
|
|
|
|
ldi
|
|
|
|
ldi
|
|
|
|
inc hl \ inc hl
|
|
|
|
call strcpyM
|
|
|
|
inc hl ; byte after word termination
|
|
|
|
jr .loop
|
|
|
|
.notLIT:
|
|
|
|
; it's a word
|
|
|
|
call HLPointsIMMED
|
|
|
|
jr nz, .notIMMED
|
|
|
|
; Immediate word, we'll have to call it.
|
|
|
|
; Before we make our call, let's save our current HL/DE position
|
|
|
|
ld (HERE), de
|
|
|
|
ld e, (hl)
|
|
|
|
inc hl
|
|
|
|
ld d, (hl)
|
|
|
|
inc hl ; point to next word
|
|
|
|
push de \ pop iy ; prepare for executeCodeLink
|
|
|
|
ld (ix), l
|
|
|
|
ld (ix+1), h
|
|
|
|
; Push return address
|
|
|
|
ld hl, .retList
|
|
|
|
call pushRS
|
|
|
|
; Ready!
|
|
|
|
jp executeCodeLink
|
|
|
|
.notIMMED:
|
|
|
|
; a good old regular word. We have 2 bytes to copy. But before we do,
|
|
|
|
; let's check whether it's an EXIT. LDI doesn't affect Z, so we can
|
|
|
|
; make our jump later.
|
|
|
|
call HLPointsEXIT
|
|
|
|
ldi
|
|
|
|
ldi
|
|
|
|
jr nz, .loop
|
2020-03-09 23:49:51 +11:00
|
|
|
; HL has our new RS' TOS
|
|
|
|
ld (ix), l
|
|
|
|
ld (ix+1), h
|
|
|
|
ld (HERE), de ; update HERE
|
2020-03-08 10:53:20 +11:00
|
|
|
jp exit
|
|
|
|
|
2020-03-10 13:13:11 +11:00
|
|
|
; This label is pushed to RS when an IMMED word is called. When that word calls
|
|
|
|
; exit, this is where it returns. When we return, RS will need to be popped so
|
|
|
|
; that we stay on the proper RS level.
|
|
|
|
.retList:
|
|
|
|
.dw .retWord
|
|
|
|
.retWord:
|
|
|
|
.dw .retEntry
|
|
|
|
.retEntry:
|
|
|
|
call popRS ; unwind stack
|
|
|
|
; recall old HL / DE values
|
|
|
|
ld l, (ix)
|
|
|
|
ld h, (ix+1)
|
|
|
|
ld de, (HERE)
|
|
|
|
; continue!
|
|
|
|
jr .loop
|
|
|
|
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "DOES>"
|
|
|
|
.fill 3
|
2020-03-08 14:18:14 +11:00
|
|
|
.dw DEFINE
|
2020-03-10 06:12:44 +11:00
|
|
|
DOES:
|
2020-03-08 14:18:14 +11:00
|
|
|
.dw nativeWord
|
|
|
|
; We run this when we're in an entry creation context. Many things we
|
|
|
|
; need to do.
|
|
|
|
; 1. Change the code link to doesWord
|
|
|
|
; 2. Leave 2 bytes for regular cell variable.
|
|
|
|
; 3. Get the Interpreter pointer from the stack and write this down to
|
|
|
|
; entry PFA+2.
|
|
|
|
; 3. exit. Because we've already popped RS, a regular exit will abort
|
|
|
|
; colon definition, so we're good.
|
|
|
|
ld iy, (CURRENT)
|
|
|
|
ld hl, doesWord
|
|
|
|
call wrCompHL
|
|
|
|
inc iy \ inc iy ; cell variable space
|
|
|
|
call popRS
|
|
|
|
call wrCompHL
|
|
|
|
ld (HERE), iy
|
|
|
|
jp exit
|
|
|
|
|
2020-03-10 13:13:11 +11:00
|
|
|
|
|
|
|
.db "IMMEDIA"
|
|
|
|
.db 0
|
|
|
|
.dw DOES
|
|
|
|
IMMEDIATE:
|
|
|
|
.dw nativeWord
|
|
|
|
ld hl, (CURRENT)
|
|
|
|
dec hl
|
|
|
|
dec hl
|
|
|
|
dec hl
|
|
|
|
inc (hl)
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
; ( n -- )
|
|
|
|
.db "LITERAL"
|
|
|
|
.db 1 ; IMMEDIATE
|
|
|
|
.dw IMMEDIATE
|
|
|
|
LITERAL:
|
|
|
|
.dw nativeWord
|
|
|
|
ld hl, (HERE)
|
|
|
|
ld de, NUMBER
|
|
|
|
ld (hl), e
|
|
|
|
inc hl
|
|
|
|
ld (hl), d
|
|
|
|
inc hl
|
|
|
|
pop de ; number from stack
|
|
|
|
ld (hl), e
|
|
|
|
inc hl
|
|
|
|
ld (hl), d
|
|
|
|
inc hl
|
|
|
|
ld (HERE), hl
|
|
|
|
jp exit
|
|
|
|
|
2020-03-08 04:13:15 +11:00
|
|
|
; ( -- c )
|
2020-03-08 09:09:45 +11:00
|
|
|
.db "KEY"
|
|
|
|
.fill 5
|
2020-03-10 13:13:11 +11:00
|
|
|
.dw LITERAL
|
2020-03-10 06:12:44 +11:00
|
|
|
KEY:
|
2020-03-08 04:13:15 +11:00
|
|
|
.dw nativeWord
|
|
|
|
call stdioGetC
|
|
|
|
ld h, 0
|
|
|
|
ld l, a
|
|
|
|
push hl
|
|
|
|
jp exit
|
|
|
|
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "INTERPR"
|
|
|
|
.db 0
|
2020-03-08 04:13:15 +11:00
|
|
|
.dw KEY
|
2020-03-10 06:12:44 +11:00
|
|
|
INTERPRET:
|
2020-03-08 04:13:15 +11:00
|
|
|
.dw nativeWord
|
2020-03-08 05:15:19 +11:00
|
|
|
interpret:
|
2020-03-08 09:09:45 +11:00
|
|
|
ld iy, COMPBUF
|
2020-03-09 23:49:51 +11:00
|
|
|
.loop:
|
|
|
|
call readword
|
|
|
|
jr nz, .end
|
2020-03-08 04:13:15 +11:00
|
|
|
call compile
|
2020-03-09 23:49:51 +11:00
|
|
|
jr .loop
|
|
|
|
.end:
|
2020-03-10 06:12:44 +11:00
|
|
|
ld hl, QUIT
|
2020-03-09 23:49:51 +11:00
|
|
|
call wrCompHL
|
2020-03-08 09:09:45 +11:00
|
|
|
ld iy, COMPBUF
|
2020-03-08 04:13:15 +11:00
|
|
|
jp compiledWord
|
2020-03-08 04:50:54 +11:00
|
|
|
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "CREATE"
|
|
|
|
.fill 2
|
2020-03-08 09:09:45 +11:00
|
|
|
.dw INTERPRET
|
2020-03-10 06:12:44 +11:00
|
|
|
CREATE:
|
2020-03-08 09:09:45 +11:00
|
|
|
.dw nativeWord
|
2020-03-08 10:53:20 +11:00
|
|
|
call entryhead
|
|
|
|
jp nz, quit
|
2020-03-08 09:09:45 +11:00
|
|
|
ld de, cellWord
|
|
|
|
ld (hl), e
|
|
|
|
inc hl
|
|
|
|
ld (hl), d
|
|
|
|
inc hl
|
|
|
|
ld (HERE), hl
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
.db "HERE"
|
|
|
|
.fill 4
|
|
|
|
.dw CREATE
|
2020-03-10 06:12:44 +11:00
|
|
|
HERE_: ; Caution: conflicts with actual variable name
|
2020-03-08 09:09:45 +11:00
|
|
|
.dw sysvarWord
|
|
|
|
.dw HERE
|
|
|
|
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "CURRENT"
|
|
|
|
.db 0
|
2020-03-08 14:18:14 +11:00
|
|
|
.dw HERE_
|
2020-03-10 06:12:44 +11:00
|
|
|
CURRENT_:
|
2020-03-08 14:18:14 +11:00
|
|
|
.dw sysvarWord
|
|
|
|
.dw CURRENT
|
|
|
|
|
2020-03-08 04:50:54 +11:00
|
|
|
; ( n -- )
|
|
|
|
.db "."
|
|
|
|
.fill 7
|
2020-03-08 14:18:14 +11:00
|
|
|
.dw CURRENT_
|
2020-03-10 06:12:44 +11:00
|
|
|
DOT:
|
2020-03-08 04:50:54 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop de
|
2020-03-08 12:20:11 +11:00
|
|
|
; We check PS explicitly because it doesn't look nice to spew gibberish
|
|
|
|
; before aborting the stack underflow.
|
|
|
|
call chkPS
|
2020-03-08 04:50:54 +11:00
|
|
|
call pad
|
|
|
|
call fmtDecimalS
|
|
|
|
call printstr
|
|
|
|
jp exit
|
2020-03-08 09:09:45 +11:00
|
|
|
|
|
|
|
; ( n a -- )
|
|
|
|
.db "!"
|
|
|
|
.fill 7
|
|
|
|
.dw DOT
|
2020-03-10 06:12:44 +11:00
|
|
|
STORE:
|
2020-03-08 09:09:45 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop iy
|
|
|
|
pop hl
|
|
|
|
ld (iy), l
|
|
|
|
ld (iy+1), h
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
; ( a -- n )
|
|
|
|
.db "@"
|
|
|
|
.fill 7
|
|
|
|
.dw STORE
|
2020-03-10 06:12:44 +11:00
|
|
|
FETCH:
|
2020-03-08 09:09:45 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop hl
|
|
|
|
call intoHL
|
|
|
|
push hl
|
|
|
|
jp exit
|
2020-03-08 11:42:07 +11:00
|
|
|
|
2020-03-08 13:12:30 +11:00
|
|
|
; ( a b -- b a )
|
|
|
|
.db "SWAP"
|
|
|
|
.fill 4
|
|
|
|
.dw FETCH
|
2020-03-10 06:12:44 +11:00
|
|
|
SWAP:
|
2020-03-08 13:12:30 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop hl
|
|
|
|
ex (sp), hl
|
|
|
|
push hl
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
; ( a -- a a )
|
|
|
|
.db "DUP"
|
|
|
|
.fill 5
|
|
|
|
.dw SWAP
|
2020-03-10 06:12:44 +11:00
|
|
|
DUP:
|
2020-03-08 13:12:30 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop hl
|
|
|
|
push hl
|
|
|
|
push hl
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
; ( a b -- a b a )
|
|
|
|
.db "OVER"
|
|
|
|
.fill 4
|
|
|
|
.dw DUP
|
2020-03-10 06:12:44 +11:00
|
|
|
OVER:
|
2020-03-08 13:12:30 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop hl ; B
|
|
|
|
pop de ; A
|
|
|
|
push de
|
|
|
|
push hl
|
|
|
|
push de
|
|
|
|
jp exit
|
|
|
|
|
2020-03-08 11:42:07 +11:00
|
|
|
; ( a b -- c ) A + B
|
|
|
|
.db "+"
|
|
|
|
.fill 7
|
2020-03-08 13:12:30 +11:00
|
|
|
.dw OVER
|
2020-03-10 06:12:44 +11:00
|
|
|
PLUS:
|
2020-03-08 11:42:07 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop hl
|
|
|
|
pop de
|
|
|
|
add hl, de
|
|
|
|
push hl
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
; ( a b -- c ) A - B
|
|
|
|
.db "-"
|
|
|
|
.fill 7
|
|
|
|
.dw PLUS
|
2020-03-10 06:12:44 +11:00
|
|
|
MINUS:
|
2020-03-08 11:42:07 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop de ; B
|
|
|
|
pop hl ; A
|
|
|
|
or a ; reset carry
|
|
|
|
sbc hl, de
|
|
|
|
push hl
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
; ( a b -- c ) A * B
|
|
|
|
.db "*"
|
|
|
|
.fill 7
|
|
|
|
.dw MINUS
|
2020-03-10 06:12:44 +11:00
|
|
|
MULT:
|
2020-03-08 11:42:07 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop de
|
|
|
|
pop bc
|
|
|
|
call multDEBC
|
|
|
|
push hl
|
|
|
|
jp exit
|
|
|
|
|
|
|
|
; ( a b -- c ) A / B
|
|
|
|
.db "/"
|
|
|
|
.fill 7
|
|
|
|
.dw MULT
|
2020-03-10 06:12:44 +11:00
|
|
|
DIV:
|
2020-03-08 11:42:07 +11:00
|
|
|
.dw nativeWord
|
|
|
|
pop de
|
|
|
|
pop hl
|
|
|
|
call divide
|
|
|
|
push bc
|
|
|
|
jp exit
|
|
|
|
|
2020-03-10 05:19:51 +11:00
|
|
|
.db "IF"
|
|
|
|
.fill 6
|
|
|
|
.dw DIV
|
2020-03-10 06:12:44 +11:00
|
|
|
IF:
|
2020-03-10 05:19:51 +11:00
|
|
|
.dw ifWord
|
|
|
|
|
|
|
|
.db "ELSE"
|
|
|
|
.fill 4
|
|
|
|
.dw IF
|
2020-03-10 06:12:44 +11:00
|
|
|
ELSE:
|
2020-03-10 05:19:51 +11:00
|
|
|
.dw elseWord
|
|
|
|
|
|
|
|
.db "THEN"
|
|
|
|
.fill 4
|
|
|
|
.dw ELSE
|
2020-03-10 06:12:44 +11:00
|
|
|
THEN:
|
2020-03-10 05:19:51 +11:00
|
|
|
.dw thenWord
|
|
|
|
|
2020-03-08 13:12:30 +11:00
|
|
|
; End of native words
|
|
|
|
|
|
|
|
; ( a -- )
|
|
|
|
; @ .
|
|
|
|
.db "?"
|
|
|
|
.fill 7
|
2020-03-10 05:19:51 +11:00
|
|
|
.dw THEN
|
2020-03-10 06:12:44 +11:00
|
|
|
FETCHDOT:
|
2020-03-08 13:12:30 +11:00
|
|
|
.dw compiledWord
|
2020-03-10 06:12:44 +11:00
|
|
|
.dw FETCH
|
|
|
|
.dw DOT
|
|
|
|
.dw EXIT
|
2020-03-08 13:12:30 +11:00
|
|
|
|
|
|
|
; ( n a -- )
|
|
|
|
; SWAP OVER @ + SWAP !
|
|
|
|
.db "+!"
|
|
|
|
.fill 6
|
|
|
|
.dw FETCHDOT
|
2020-03-10 06:12:44 +11:00
|
|
|
STOREINC:
|
2020-03-08 13:12:30 +11:00
|
|
|
.dw compiledWord
|
2020-03-10 06:12:44 +11:00
|
|
|
.dw SWAP
|
|
|
|
.dw OVER
|
|
|
|
.dw FETCH
|
|
|
|
.dw PLUS
|
|
|
|
.dw SWAP
|
|
|
|
.dw STORE
|
|
|
|
.dw EXIT
|
2020-03-08 13:12:30 +11:00
|
|
|
|
|
|
|
; ( n -- )
|
|
|
|
; HERE +!
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "ALLOT"
|
|
|
|
.fill 3
|
2020-03-08 13:12:30 +11:00
|
|
|
.dw STOREINC
|
2020-03-10 06:12:44 +11:00
|
|
|
ALLOT:
|
2020-03-08 13:12:30 +11:00
|
|
|
.dw compiledWord
|
2020-03-10 06:12:44 +11:00
|
|
|
.dw HERE_
|
|
|
|
.dw STOREINC
|
|
|
|
.dw EXIT
|
2020-03-08 14:18:14 +11:00
|
|
|
|
2020-03-08 14:23:08 +11:00
|
|
|
; CREATE 2 ALLOT
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "VARIABL"
|
|
|
|
.db 0
|
2020-03-08 14:23:08 +11:00
|
|
|
.dw ALLOT
|
2020-03-10 06:12:44 +11:00
|
|
|
VARIABLE:
|
2020-03-08 14:23:08 +11:00
|
|
|
.dw compiledWord
|
2020-03-10 06:12:44 +11:00
|
|
|
.dw CREATE
|
2020-03-08 14:23:08 +11:00
|
|
|
.dw NUMBER
|
|
|
|
.dw 2
|
2020-03-10 06:12:44 +11:00
|
|
|
.dw ALLOT
|
|
|
|
.dw EXIT
|
2020-03-08 14:23:08 +11:00
|
|
|
|
2020-03-08 14:18:14 +11:00
|
|
|
; ( n -- )
|
|
|
|
; CREATE HERE @ ! DOES> @
|
2020-03-10 10:50:51 +11:00
|
|
|
.db "CONSTAN"
|
|
|
|
.db 0
|
2020-03-08 14:23:08 +11:00
|
|
|
.dw VARIABLE
|
2020-03-10 06:12:44 +11:00
|
|
|
CONSTANT:
|
2020-03-08 14:18:14 +11:00
|
|
|
.dw compiledWord
|
2020-03-10 06:12:44 +11:00
|
|
|
.dw CREATE
|
|
|
|
.dw HERE_
|
|
|
|
.dw FETCH
|
|
|
|
.dw STORE
|
|
|
|
.dw DOES
|
|
|
|
.dw FETCH
|
|
|
|
.dw EXIT
|