channels working
[watForth.git] / forth.forth
1 \ This program is free software: you can redistribute it and/or modify
2 \ it under the terms of the GNU General Public License as published by
3 \ the Free Software Foundation, either version 3 of the License, or
4 \ (at your option) any later version.
5 \
6 \ This program is distributed in the hope that it will be useful,
7 \ but WITHOUT ANY WARRANTY; without even the implied warranty of
8 \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 \ GNU General Public License for more details.
10 \
11 \ You should have received a copy of the GNU General Public License
12 \ along with this program. If not, see <http://www.gnu.org/licenses/>.
13
14 \ word EXECUTE 12 define
15 16500 execute WORD 16500 29 execute
16 word DEFINE 29 29 execute
17 word RINIT 3 define
18 word NOOP 13 define
19 word MODE 14336 define
20 word EXECUTE-WORD 16680 define
21 word EXECUTE-NUM 16720 define
22 word INTERPRET 16400 define
23 word KEY 5 define
24 word WORD-START 20 define
25 word WORD-PUT 19 define
26 word WORD-END 23 define
27 word STRING-START 39 define
28 word STRING-PUT 40 define
29 word STRING-END 41 define
30 word NUMBER 22 define
31 word FIND 21 define
32 word FIND-DOES 44 define
33 word DEFINE-DOES 45 define
34 word QUIT 16384 define
35 word BYE 25 define
36 word WORDS 27 define
37 word CHANNEL-IN 35 define
38 word CHANNEL-OUT 48 define
39 word CHANNEL-OPEN 51 define
40 word CHANNEL-AWAIT 50 define
41 word !HERE 36 define
42 word HERE 28 define
43
44 word STACKTRACE 46 define
45 word FETCH 47 define
46
47 word ; 1 define
48 word JZ: 14 define
49 word JNZ: 15 define
50 word J-1: 24 define
51 word JMP: 18 define
52 word , 33 define
53 word + 7 define
54 word - 34 define
55 word =? 37 define
56 word WS? 17 define
57 word . 9 define
58 word .S 38 define
59 word @ 10 define
60 word @8u 53 define
61 word @+ 42 define
62 word ! 11 define
63 word !+ 43 define
64 word LIT 2 define
65 word DUP 6 define
66 word 2DUP 30 define
67 word DROP 16 define
68 word 2DROP 32 define
69 word SWAP 26 define
70 word ROT 31 define
71 word -ROT 54 define
72
73 \ ' "TICK" returns address of word's execution semantics
74 word ' here define
75 word word find , word find find , word ; find ,
76
77 \ : "COLON" sets execution semantics
78 word : here define
79 word : here define-does
80 ' word , ' here , ' define , ' ; ,
81
82 \ :> "DOES" sets compilation semantics
83 word :> here define-does
84 : :> ' word , ' here , ' define-does , ' ; ,
85
86 \ ::> "COLON DOES" sets execution and compilation semantics
87 word ::> here define-does
88 word ::> here define
89 ' word , ' 2dup , ' here , ' define-does , ' here , ' define , ' ; ,
90
91 \ write the execution semantics of a word to memory
92 : MEMORIZE-WORD ' dup , ' JNZ: , here 12 + , ' 2drop , ' ; ,
93 ' 2dup , ' find , ' dup , ' JZ: , here 16 + , ' , , ' 2drop , ' ; ,
94 ' drop , ' lit , ' lit , ' , , ' execute-num , ' , , ' ; ,
95
96 \ change interpreter semantics to "memorize-word"
97 ::> MEMORIZING ' lit , ' memorize-word , ' lit , ' mode , ' ! , ' ; ,
98
99 \ memorize the compiler
100 \memorizing
101 \: executing lit execute-word lit mode ! ;
102 \: FINISH-" drop string-end ;
103 \: BSLASH-" drop key string-put JMP: \here \8 \+ \,
104 \: " string-start \: KLOOP key 92 =? JNZ: bslash-" 34 =? JNZ: finish-" string-put JMP: KLOOP
105 \: DO" \:> " " swap lit lit , , lit lit , , ;
106 \: DOIF \:> IF lit JZ: , here dup , ;
107 \: DOELSE \:> ELSE lit JMP: , here dup , here -rot ! ;
108 \: DOTHEN \:> THEN here swap ! ;
109 \: COMPILE-WORD dup
110 \doif 2dup find-does dup
111 \doif rot 2drop execute ;
112 \dothen drop 2dup find dup
113 \doif , 2drop ;
114 \dothen drop lit lit , execute-num , ;
115 \dothen 2drop \do" Compilation Error: null word" .s bye
116 \: compiling lit compile-word lit mode ! ;
117
118 \ compile the rest of the compiler
119 \compiling
120
121 \ ; "RET" compilation semantics: ends a function and returns to executing mode
122 :> ; lit \' ; \, , lit \' ; \, , executing \' ; \,
123
124 \ ;; "SEMIRET" compilation semantics: simply writes a return instruction
125 :> ;; lit \' ; \, , ;
126
127 \ <: "OVERLOAD COLON" extend previous execution semantics of word
128 ::> <: \compiling word 2dup find dup if here swap , define else drop here define
129 then compiling ;
130
131 \ <:> "OVERLOAD COLON DOES" extend previous compilation semantics of word
132 ::> <:> \compiling word 2dup find-does dup if here swap , define-does else drop
133 here define-does then compiling ;
134
135 \ execution semantics of COLON, DOES, and COLON DOES now extended to
136 \ automatically switch to compilation mode
137 <: : compiling ;
138 <: :> compiling ;
139 <: ::> compiling ;
140
141 \ Multi-line comments
142 ::> ( key 41 =? swap -1 =? swap drop + if ;; then JMP: \' ( \, ;
143
144 (
145 End of bootstrap process
146 beyond this point, all hope is lost
147 )
148
149 \ Set the number conversion base
150 : BASE 14348 ! ;
151 : BASE10 10 base ;
152
153 \ TICK compilation semantics
154 :> ' lit lit , word find , ;
155 \ "TICK DOES" (get compilation semantics execution token)
156 : '> word find-does ;
157 :> '> word find-does , ;
158
159 \ Include a remote file
160 : REQUIRE" ' quit channel-open dup \' " \, fetch channel-await ;
161
162 \ Let's try it
163 REQUIRE" { \"url\": \"forth/test-watfor.forth\" }"
164
165 \ Print intro string
166 " watForth-32 Interactive CLI:
167 " .s