3 ;; We need some basic words to be available before we can implement the actual
4 ;; interpreter. For this reason we need to write some words in assembly, even
5 ;; though they depend only on other Forth words. Such words are defined in this
8 ;; With these words, we can finally defined INTERPRET, and from there we'll load
9 ;; everything else from an external file.
11 ;; This file is included from main.asm; see that file for more information.
13 ;; Define a Forth word that is implemented in Forth. (The body will be a list of
15 macro forth label, name, immediate {
16 header label, name, immediate
21 dq HERE, GET, PUT ; Set the memory at the address pointed to by HERE
22 dq HERE, GET, LIT, 8, PLUS ; Calculate new address for HERE to point to
23 dq HERE, PUT ; Update HERE to point to the new address
26 ;; Mark the last added word as immediate.
27 forth IMMEDIATE, 'IMMEDIATE', 1
34 ;; Given the address of a word, return 0 if the given word is not immediate.
35 forth IS_IMMEDIATE, 'IMMEDIATE?'
40 ;; Enter immediate mode, immediately
41 forth INTO_IMMEDIATE, '[', 1
42 dq LIT, 0, STATE, PUT_BYTE
45 ;; Enter compilation mode
46 forth OUTOF_IMMEDIATE, ']'
47 dq LIT, 1, STATE, PUT_BYTE
50 ;; INTERPRET-WORD expects a word as a (buffer, length) pair on the stack. It
51 ;; interprets and executes the word. It's behavior depends on the current STATE.
52 ;; It provides special handling for integers.
53 forth INTERPRET_WORD, 'INTERPRET-WORD'
55 ;; Stack is (word length word length).
56 dq FIND ; Try to find word
58 dq ZBRANCH, 8 * 22 ; Check if word is found
62 dq STATE, GET, ZBRANCH, 8 * 11 ; Check whether we are in compilation or immediate mode
64 ;; (Word found, compilation mode)
65 dq DUP_, IS_IMMEDIATE, NOT_, ZBRANCH, 8 * 6 ; If the word is immediate, continue as we would in immediate mode
67 ;; Otherwise, we want to compile this word
73 ;; (Word found, immediate mode)
76 ;; Stack is (word length addr)
83 ;; - No word is found, assume it is an integer literal -
84 ;; Stack is (word length addr)
88 dq STATE, GET, ZBRANCH, 8 * 5 ; Check whether we are in compilation or immediate mode
90 ;; (Number, compilation mode)
95 ;; (Number, immediate mode)
98 ;; The INTERPRET word reads and interprets a single word from the user.
99 forth INTERPRET, 'INTERPRET'