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 ;; The INTERPRET word reads and interprets user input. It's behavior depends on
51 ;; the current STATE. It provides special handling for integers.
52 forth INTERPRET, 'INTERPRET'
56 ;; Stack is (word length word length).
57 dq FIND ; Try to find word
59 dq ZBRANCH, 8 * 22 ; Check if word is found
63 dq STATE, GET, ZBRANCH, 8 * 11 ; Check whether we are in compilation or immediate mode
65 ;; (Word found, compilation mode)
66 dq DUP_, IS_IMMEDIATE, NOT_, ZBRANCH, 8 * 6 ; If the word is immediate, continue as we would in immediate mode
68 ;; Otherwise, we want to compile this word
74 ;; (Word found, immediate mode)
77 ;; Stack is (word length addr)
84 ;; - No word is found, assume it is an integer literal -
85 ;; Stack is (word length addr)
89 dq STATE, GET, ZBRANCH, 8 * 5 ; Check whether we are in compilation or immediate mode
91 ;; (Number, compilation mode)
96 ;; (Number, immediate mode)