-
Notifications
You must be signed in to change notification settings - Fork 0
/
forth.l
executable file
·289 lines (227 loc) · 6.86 KB
/
forth.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
# The paths above assumes a "global installation" of PicoLisp.
(de wordMsgSym (Token)
(intern (pack "_" Token"_")) )
(setq *BlockMap '((: . +DefineWord) (do . +DoLoop) (if . +IfElseThen)))
(class +WordBlock)
# parent tempStack
(dm T (Parent)
(=: parent Parent)
(=: tempStack NIL) )
(dm handleToken> (Token)
(if (get *BlockMap (intern Token))
(new (list @) This) # creating and returning new child
(push (:: tempStack) (if (format Token) @ (wordMsgSym Token)))
NIL ) )
(dm getRevTempStack> ()
(when (reverse (: tempStack))
#(println @)
(=: tempStack NIL)
@ ) )
(dm pushOnTempStack> (Child)
(push (:: tempStack) Child) )
### end of class +WordBlock
(class +Forth +WordBlock)
# true false paused dStack rStack wordBlock
(dm T ()
(super NIL)
(=: true -1)
(=: false 0)
(=: paused NIL)
(=: dStack NIL) # data stack
(=: rStack NIL) # return stack, aka loop control stack
(=: wordBlock This) )
(dm bool> (Truth)
(if Truth (: true) (: false)) )
(dm popDataBool> ()
(n0 (popDStack> This)) )
(dm popDStack> ()
(or (pop (:: dStack)) (prog (prinl "Stack underflow!") NIL)) )
(dm pushRStack> (N)
(push (:: rStack) N) )
(dm popRStack> ()
(pop (:: rStack)) )
(dm indexLtLimit> ()
(< (car (: rStack)) (cadr (: rStack))) )
(dm incIndex> ()
(push (:: rStack) (inc (pop (:: rStack)))) )
(dm prompt> ()
(let (Level 0 WB (: wordBlock))
(until (= WB This)
(inc 'Level)
(setq WB (; WB parent)) )
(pack (or (gt0 Level) "-") "> ") ) )
(dm applyToken> (Token)
(case Token
("pause" (=: paused 'paused))
("bye" (bye))
(T (ifn (= (: wordBlock) This)
# wordBlock is some uncompleted child
(let WB (: wordBlock)
(when (handleToken> (: wordBlock) Token)
(=: wordBlock @)
# wordBlock WB has been completed
# Execute WB if it was the immediate child of the +Forth object
(when (= @ This)
(execute> WB This) ) ) )
# wordBlock is the +Forth object itself
#(prinl "wordBlock is This: " (= (: wordBlock) This))
(if (format Token)
(push (:: dStack) @)
(let MsgSym (wordMsgSym Token)
(if (method MsgSym This)
(send MsgSym This)
# Prepare new wordBlock child if Token is found in *BlockMap ...
(if (get *BlockMap (intern Token))
(=: wordBlock (new (list @) This))
(prinl "*** Undefined word: " Token) ) ) ) ) ) ) ) )
(dm executeBlock> (Block)
#(prin "* executeBlock> Block: ") (println Block)
(for Elem Block
(if (num? Elem)
(push (:: dStack) @)
(if (isa '+WordBlock Elem)
(execute> Elem This)
(if (method Elem This)
(send Elem This)
(prinl "*** Undefined word: " Elem) ) ) ) ) )
(dm _drop_ ()
(pop (:: dStack)) )
(dm _dup_ ()
(when (car (: dStack)) (push (:: dStack) @)) )
(dm _over_ ()
(when (cadr (: dStack)) (push (:: dStack) @)) )
(dm _swap_ ()
(when (cadr (: dStack)) (=: dStack (cons @ (car (: dStack)) (tail -2 (: dStack))))) )
(dm _1+_ ()
(when (pop (:: dStack)) (push (:: dStack) (inc @))) )
(dm _1-_ ()
(when (pop (:: dStack)) (push (:: dStack) (dec @))) )
(dm _+_ ()
(when (cadr (: dStack)) (=: dStack (cons (+ @ (car (: dStack))) (tail -2 (: dStack))))) )
(dm _*_ ()
(when (cadr (: dStack)) (=: dStack (cons (* @ (car (: dStack))) (tail -2 (: dStack))))) )
(dm _<_ ()
(when (cadr (: dStack)) (=: dStack (cons
(bool> This (< @ (car (: dStack)))) (tail -2 (: dStack)) ) ) ) )
(dm _>_ ()
(when (cadr (: dStack)) (=: dStack (cons
(bool> This (> @ (car (: dStack)))) (tail -2 (: dStack)) ) ) ) )
(dm _i_ ()
(if (car (: rStack))
(push (:: dStack) @)
(prinl "Loop control stack empty!") ) )
(dm _.s_ ()
(println (or (reverse (: dStack)) "empty")) )
(dm _t_ () # just for testing
(prin "tempStack: ") (println (: tempStack)) )
### end of class +Forth
(class +DefineWord +WordBlock)
# newWord body
(dm T (Parent)
(super Parent)
(prinl "+DefineWord")
(=: newWord NIL)
(=: body NIL) )
(dm handleToken> (Token)
(let Block NIL
(ifn (= ";" Token)
(ifn (: newWord)
(=: newWord Token)
# newWord already set, collect body tokens ...
(setq Block (super Token)) ) # Block may now become a new child
# Got the closing ";"
(=: body (getRevTempStack> This))
(setq Block (: parent)) # subsequent tokens will go to parent
(pushOnTempStack> Block This) ) # pushing This onto parent's token stack
Block ) )
(dm execute> (F)
(ifn (and (: newWord) (: body))
(prinl "Ignoring empty DefineWord object")
(push F (cons (wordMsgSym (: newWord))
(list () (list 'executeBlock> (lit F) (lit (: body)))) ) )
(prin "Done defining " (: newWord) " as ") (println (: body)) ) )
### end of class +DefineWord
(class +DoLoop +WordBlock)
# body
(dm T (Parent)
(super Parent)
(=: body NIL) )
(dm handleToken> (Token)
(let Block NIL
(ifn (= "loop" Token)
(setq Block (super Token)) # Block may now become a new child
# Got the closing "loop"
(=: body (getRevTempStack> This))
(setq Block (: parent)) # subsequent tokens will go to parent
(pushOnTempStack> Block This) ) # pushing This onto parent's token stack
Block ) )
(dm execute> (F)
(let Index (popDStack> F)
(pushRStack> F (popDStack> F)) # limit
(pushRStack> F Index) )
(while (indexLtLimit> F)
(executeBlock> F (: body))
(incIndex> F) )
(popRStack> F)
(popRStack> F) )
### end of class +DoLoop
(class +IfElseThen +WordBlock)
# trueBranch falseBranch gotElse
(dm T (Parent)
(super Parent)
(=: trueBranch NIL)
(=: falseBranch NIL)
(=: gotElse NIL) )
(dm handleToken> (Token)
(let Block NIL
(case Token
("else"
(=: trueBranch (getRevTempStack> This))
(=: gotElse T) )
("then"
(if (: gotElse)
(=: falseBranch (getRevTempStack> This))
(=: trueBranch (getRevTempStack> This)) )
(setq Block (: parent)) # subsequent tokens will go to parent
(pushOnTempStack> Block This) ) # pushing This onto parent's token stack
(T (setq Block (super Token))) ) # Block may now become a new child
Block ) )
(dm execute> (F)
(executeBlock> F (if (popDataBool> F) (: trueBranch) (: falseBranch))) )
### end of class +IfElseThen
(de doLine (Forth LineChars)
(let Tokens (mapcar pack (split LineChars " "))
(for Token Tokens
(unless (or (sp? Token) (get F 'paused))
#(println "doLine: " Forth Token)
(applyToken> Forth Token) ) ) ) )
(de forthRepl (F)
(setq *CurrentForth F)
(put F 'paused NIL)
(in NIL
(until (get F 'paused)
(prin (prompt> F))
(doLine F (line)) ) ) )
(de emuForthReplHandler (Line)
(doLine *CurrentForth (chop Line))
(ifn (get *CurrentForth 'paused)
"> "
(off *ReplHandler)
": " ) )
(de activateEmuForthRepl (F)
(setq *CurrentForth F)
(setq *ReplHandler emuForthReplHandler)
(emulog "Activated " F)
NIL )
(de resume ()
(if *EMUENV
(activateEmuForthRepl *CurrentForth)
(forthRepl *CurrentForth) ) )
(setq *F1 (new '(+Forth))
*F2 (new '(+Forth)) )
(if *EMUENV
(activateEmuForthRepl *F1)
# Normal PicoLisp ...
(prinl (pack "-> "
(forthRepl *F1) ) ) )