2020-03-07 12:13:15 -05:00
|
|
|
; *** Const ***
|
|
|
|
; Base of the Return Stack
|
|
|
|
.equ RS_ADDR 0xf000
|
|
|
|
; Number of bytes we keep as a padding between HERE and the scratchpad
|
|
|
|
.equ PADDING 0x20
|
2020-03-07 17:09:45 -05:00
|
|
|
; Max length of dict entry names
|
2020-03-09 19:50:51 -04:00
|
|
|
.equ NAMELEN 7
|
2020-03-07 12:13:15 -05:00
|
|
|
; Offset of the code link relative to the beginning of the word
|
2020-03-09 19:50:51 -04:00
|
|
|
.equ CODELINK_OFFSET NAMELEN+3
|
2020-03-07 12:13:15 -05:00
|
|
|
|
2020-03-12 11:39:27 -04:00
|
|
|
; Flags for the "flag field" of the word structure
|
|
|
|
; IMMEDIATE word
|
|
|
|
.equ FLAG_IMMED 0
|
|
|
|
; This wordref is not a regular word (it's not preceeded by a name). It's one
|
|
|
|
; of the NUMBER, LIT, BRANCH etc. entities.
|
|
|
|
.equ FLAG_UNWORD 1
|
|
|
|
|
2020-03-07 12:13:15 -05:00
|
|
|
; *** Variables ***
|
|
|
|
.equ INITIAL_SP FORTH_RAMSTART
|
2020-03-13 16:40:55 -04:00
|
|
|
; wordref of the last entry of the dict.
|
2020-03-07 12:13:15 -05:00
|
|
|
.equ CURRENT @+2
|
2020-03-15 22:46:17 -04:00
|
|
|
; Pointer to the next free byte in dict.
|
2020-03-07 12:13:15 -05:00
|
|
|
.equ HERE @+2
|
2020-03-13 16:01:09 -04:00
|
|
|
; Interpreter pointer. See Execution model comment below.
|
|
|
|
.equ IP @+2
|
2020-03-10 21:37:06 -04:00
|
|
|
; Pointer to where we currently are in the interpretation of the current line.
|
2020-03-07 12:13:15 -05:00
|
|
|
.equ INPUTPOS @+2
|
2020-03-17 21:19:56 -04:00
|
|
|
; Pointer to the system's number parsing function. It points to then entry that
|
|
|
|
; had the "(parse)" name at startup. During stage0, it's out builtin PARSE,
|
|
|
|
; but at stage1, it becomes "(parse)" from core.fs. It can also be changed at
|
|
|
|
; runtime.
|
|
|
|
.equ PARSEPTR @+2
|
2020-03-15 22:46:17 -04:00
|
|
|
.equ FORTH_RAMEND @+2
|
2020-03-07 12:13:15 -05:00
|
|
|
|
2020-03-12 00:14:44 -04:00
|
|
|
; (HERE) usually starts at RAMEND, but in certain situations, such as in stage0,
|
|
|
|
; (HERE) will begin at a strategic place.
|
|
|
|
.equ HERE_INITIAL FORTH_RAMEND
|
|
|
|
|
2020-03-10 21:37:06 -04:00
|
|
|
; EXECUTION MODEL
|
|
|
|
; After having read a line through stdioReadLine, we want to interpret it. As
|
|
|
|
; a general rule, we go like this:
|
|
|
|
;
|
|
|
|
; 1. read single word from line
|
2020-03-15 22:46:17 -04:00
|
|
|
; 2. Can we find the word in dict?
|
|
|
|
; 3. If yes, execute that word, goto 1
|
|
|
|
; 4. Is it a number?
|
|
|
|
; 5. If yes, push that number to PS, goto 1
|
|
|
|
; 6. Error: undefined word.
|
2020-03-13 16:01:09 -04:00
|
|
|
;
|
|
|
|
; EXECUTING A WORD
|
|
|
|
;
|
|
|
|
; At it's core, executing a word is having the wordref in IY and call
|
2020-03-14 17:48:24 -04:00
|
|
|
; EXECUTE. Then, we let the word do its things. Some words are special,
|
2020-03-13 16:01:09 -04:00
|
|
|
; but most of them are of the compiledWord type, and that's their execution that
|
|
|
|
; we describe here.
|
|
|
|
;
|
|
|
|
; First of all, at all time during execution, the Interpreter Pointer (IP)
|
|
|
|
; points to the wordref we're executing next.
|
|
|
|
;
|
|
|
|
; When we execute a compiledWord, the first thing we do is push IP to the Return
|
|
|
|
; Stack (RS). Therefore, RS' top of stack will contain a wordref to execute
|
|
|
|
; next, after we EXIT.
|
|
|
|
;
|
|
|
|
; At the end of every compiledWord is an EXIT. This pops RS, sets IP to it, and
|
|
|
|
; continues.
|
2020-03-10 21:37:06 -04:00
|
|
|
|
2020-03-07 12:13:15 -05:00
|
|
|
; *** Code ***
|
|
|
|
forthMain:
|
2020-03-07 20:20:11 -05:00
|
|
|
; STACK OVERFLOW PROTECTION:
|
|
|
|
; To avoid having to check for stack underflow after each pop operation
|
|
|
|
; (which can end up being prohibitive in terms of costs), we give
|
|
|
|
; ourselves a nice 6 bytes buffer. 6 bytes because we seldom have words
|
|
|
|
; requiring more than 3 items from the stack. Then, at each "exit" call
|
|
|
|
; we check for stack underflow.
|
|
|
|
push af \ push af \ push af
|
2020-03-07 12:13:15 -05:00
|
|
|
ld (INITIAL_SP), sp
|
2020-03-12 00:14:44 -04:00
|
|
|
; LATEST is a *indirect* label to the latest entry of the dict. See
|
|
|
|
; default at the bottom of dict.asm. This indirection allows us to
|
|
|
|
; override latest to a value set in a binary dict compiled separately,
|
|
|
|
; for example by the stage0 bin.
|
2020-03-10 16:02:40 -04:00
|
|
|
ld hl, LATEST
|
2020-03-12 00:14:44 -04:00
|
|
|
call intoHL
|
2020-03-07 12:13:15 -05:00
|
|
|
ld (CURRENT), hl
|
2020-03-12 00:14:44 -04:00
|
|
|
ld hl, HERE_INITIAL
|
2020-03-07 12:13:15 -05:00
|
|
|
ld (HERE), hl
|
2020-03-14 19:10:39 -04:00
|
|
|
; Set (INPUTPOS) to somewhere where there's a NULL so we consider
|
|
|
|
; ourselves EOL.
|
|
|
|
ld (INPUTPOS), hl
|
|
|
|
xor a
|
|
|
|
ld (hl), a
|
2020-03-17 21:19:56 -04:00
|
|
|
; Set up PARSEPTR
|
|
|
|
ld hl, PARSE-CODELINK_OFFSET
|
|
|
|
call find
|
|
|
|
ld (PARSEPTR), de
|
2020-03-07 17:09:45 -05:00
|
|
|
forthRdLine:
|
|
|
|
ld hl, msgOk
|
|
|
|
call printstr
|
2020-03-13 16:01:09 -04:00
|
|
|
forthRdLineNoOk:
|
2020-03-14 17:48:24 -04:00
|
|
|
; Setup return stack. After INTERPRET, we run forthExecLine
|
2020-03-13 16:01:09 -04:00
|
|
|
ld ix, RS_ADDR
|
2020-03-15 22:46:17 -04:00
|
|
|
ld hl, MAINLOOP
|
2020-03-14 17:48:24 -04:00
|
|
|
push hl
|
|
|
|
jp EXECUTE+2
|
|
|
|
|
|
|
|
.db 0b10 ; UNWORD
|
|
|
|
INTERPRET:
|
2020-03-17 17:46:58 -04:00
|
|
|
.dw compiledWord
|
|
|
|
.dw FIND_
|
|
|
|
.dw CSKIP
|
|
|
|
.dw .maybeNum
|
|
|
|
; It's a word, execute it
|
|
|
|
.dw EXECUTE
|
|
|
|
.dw EXIT
|
|
|
|
|
2020-03-15 22:46:17 -04:00
|
|
|
.maybeNum:
|
2020-03-17 17:46:58 -04:00
|
|
|
.dw compiledWord
|
2020-03-17 21:19:56 -04:00
|
|
|
.dw PARSEI
|
2020-03-17 17:46:58 -04:00
|
|
|
.dw R2P ; exit INTERPRET
|
|
|
|
.dw DROP
|
|
|
|
.dw EXIT
|
2020-03-15 22:46:17 -04:00
|
|
|
|
|
|
|
.db 0b10 ; UNWORD
|
|
|
|
MAINLOOP:
|
2020-03-14 17:48:24 -04:00
|
|
|
.dw compiledWord
|
2020-03-15 22:46:17 -04:00
|
|
|
.dw INTERPRET
|
2020-03-14 19:10:39 -04:00
|
|
|
.dw INP
|
|
|
|
.dw FETCH
|
2020-03-14 17:48:24 -04:00
|
|
|
.dw CFETCH
|
|
|
|
.dw CSKIP
|
2020-03-15 22:46:17 -04:00
|
|
|
.dw QUIT
|
|
|
|
.dw MAINLOOP
|
2020-03-10 21:37:06 -04:00
|
|
|
|
2020-03-07 17:09:45 -05:00
|
|
|
msgOk:
|
|
|
|
.db " ok", 0
|