-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINTERP.F
More file actions
396 lines (311 loc) · 13.5 KB
/
INTERP.F
File metadata and controls
396 lines (311 loc) · 13.5 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
\ BenOS v1.0 Forth interpreter and compiler (c) Benjamin Hoyt 1997
: source ( -- c-addr u ) \ return the input source string
src 2@ ;
( x1 through xn describes the current input specification )
: save-input ( -- xn ... x1 n )
source >in @ source-id blk @ \ save variables
5 ; \ # of items saved on stack
( attempt to restore input spec xn through x1, flag true on error )
: restore-input ( xn ... x1 n -- flag )
dup 5 = \ only 5 parameters?
if drop dup
if dup block drop \ reload block
then blk ! \ restore variables
src-id ! >in ! src 2!
false \ and return false = success
else 0 ?do drop loop true \ n <> 5, kill params and return true
then ;
( saves input specification to return stack, x1 through xn are backwards )
: input>r ( -- ) ( r: -- x1 .. xn n -- )
r> save-input dup
begin ?dup
while 1- rot >r \ one parameter >r
repeat >r >r ; \ push n >r and remember return addr!
( restore input specification from return stack, flag is *false if success* )
: inputr> ( -- flag ) ( r: x1 .. xn n -- )
r> r> dup \ get return address and n from r:
begin ?dup
while 1- r> -rot \ one parameters from r:
repeat restore-input swap >r ; \ restore input, put back return addr!
: _compiler ( i*x c-addr u -- j*x ) \ compile token c-addr u
2dup search-wordlists ?dup \ try to find c-addr u in search order
if 2swap 2drop 0< \ we found it!
if compile, \ normal word, compile it
else execute \ immediate word, interpret it
then
else number? dup 0= \ try to convert to number
-13 and throw 0> \ not a number, token undefined
if postpone 2literal \ compile double cell literal
else postpone literal
then
then ;
( defer main token compiler - we may want to change it later )
defer compiler ( i*x c-addr u -- j*x ) ' _compiler is compiler
: _interpreter ( i*x c-addr u -- j*x ) \ interpret token c-addr u
2dup search-order \ search for token
if nip nip dup type@
type.restrict and 0<> \ is it compile-only?
-14 and throw \ don't interpret compile-only word
xt@ execute \ else execute it
else number? 0= -13 and throw \ try to convert to number
then ;
( defer main token interpreter - we may want to change it later )
defer interpreter ( i*x c-addr u -- j*x ) ' _interpreter is interpreter
( this points to compiler if state is true, or interpreter if state is false )
defer interp-token ( i*x c-addr u -- j*x )
: [ ( -- ) \ enter interpretation mode
['] interpreter is interp-token \ change token handler to interpreter
state off ; immediate \ and set state to false
: ] ( -- ) \ enter compiliation mode
['] compiler is interp-token \ change token handler to compiler
state on ; \ and set state to true
: ?stack ( -- ) \ throw error message if stack error
sp@ sp0 @ u> -4 and throw \ stack underflow
sp@ sp0 @ /dstack @ - u<
-3 and throw ; \ stack overflow
: interpret ( i*x -- j*x ) \ interpret the whole TIB
begin parse-word dup \ try to parse, loop while word parsed
while interp-token ?stack \ handle token, check for stack errors
repeat 2drop ?unused ; \ check for dictionary overflow
: !csp ( -- ) \ save the check stack pointer
sp@ csp ! ;
: ?csp ( -- ) \ check saved stack pointer
sp@ csp @ <> -22 and throw ; \ current sp is <> csp so throw
( save the current input source specification and interpret string c-addr u )
: evaluate ( c-addr u -- )
input>r \ save input source
src 2! >in off src-id on \ save input source and define new one
interpret \ interpret
inputr> drop ; \ restore old input source
: query ( -- ) \ refill the input source from keyboard
key-tib @ dup #key-tib accept \ ACCEPT chars into key-tib
space src 2! >in off ; \ set SOURCE and reset >IN
( refill the terminal input buffer, returning true if successful )
: refill ( -- flag )
blk @
if false exit \ return false if loading block
then source-id \ evaluating/file so return false
if false exit
then query true ; \ else keyboard
( prompt is deferred, we set it to status-prompt later )
defer prompt ( -- )
: init-quit ( -- ) \ set up things for QUIT
blk off src-id off \ no block loading, keyboard input
postpone [ ; \ set interpret mode
: .ok ( -- ) \ display "ok" if interpreting
state @
ifz ." ok"
then ;
: _quit ( -- ) ( r: i*x -- ) \ reset to text interpreter
rp0 @ rp! init-quit
begin prompt refill drop \ display prompt and refill tib
interpret .ok \ interpret and print "ok" message
again ;
' _quit is quit \ resolve forward reference
: load ( i*x u -- j*x ) \ interpret block u
input>r \ save input source
dup blk ! \ set BLK
block 1k src 2! >in off \ define new input source
interpret \ interpret
inputr> drop ; \ restore old input source
: thru ( i*x u1 u2 -- j*x ) \ interpret block u1...u2
1+ swap
do i load
loop ;
: : ( "name" -- ) \ create colon def and begin compiling
!csp \ store check stack pointer
header, ] ; \ compile header, begin interpreting
: exit ( -- ) \ exit current definition at runtime
$C3 c, ; immediate restrict \ ret
( end the current definition and allow it to be found in the dictionary )
: ; ( -- )
?csp reveal \ check stack and reveal name
postpone exit \ compile code to exit word
postpone [ ; immediate restrict \ and return to interpreting
: immediate ( -- ) \ make most recent definition immediate
type.immediate or-type ;
: restrict ( -- ) \ make last definition compile-only
type.restrict or-type ;
: \ ( "ccc<eol>" -- ) \ parse and discard till end-of-line
blk @
if >in @ 63 + -64 and
else source nip
then >in ! ; immediate
: ( ( "ccc<paren>" -- ) \ parse and discard ccc
[char] ) parse 2drop ; immediate \ used to comment till ending )
: .( ( "ccc<paren>" -- ) \ parse and discard ccc
[char] ) parse type ; immediate \ used to display till ending )
: cmp0, ( -- ) \ compile code to compare TOS with 0
$0B c, $DB c, \ or ebx, ebx
$8B c, $5D c, $00 c, \ mov ebx, [ebp]
$8D c, $6D c, $04 c, ; \ lea ebp, 4 [ebp]
: >mark ( -- orig ) \ mark a forward branch
here 0 , ;
: >resolve ( orig -- ) \ resolve a forward branch
here over cell+ - swap ! ; \ convert -> relative addr and !
: <mark ( -- dest ) \ mark a backward branch
here ;
: <resolve ( dest -- ) \ resolve a backward branch
rel, ; \ relative address for Intel jumps
: if ( -- orig ) \ begin an IF .. THEN clause
cmp0, $0F c, $84 c, \ branch to orig if TOS = 0
>mark ; immediate restrict \ mark origin
: ifz ( -- orig ) \ begin an IFZ .. THEN clause
cmp0, $0F c, $85 c, \ branch to orig if TOS <> 0
>mark ; immediate restrict \ mark origin
: then ( orig -- ) \ end an IF .. THEN clause
>resolve ; immediate restrict \ resolve origin
: else ( orig1 -- orig2 ) \ begins an if .. ELSE .. THEN clause
$E9 c, >mark \ unconditional branch to orig2
swap >resolve ; immediate restrict \ resolve orig1
: found? ( x | c-addr 0 -- ) \ abort with error msg if x = 0
0= -13 and throw ;
: postpone ( "name" -- ) \ postpone name's compilation semantics
name \ parse name
search-wordlists ?dup found? 0< \ try to find it, is it immediate?
if compile compile \ non-immediate word
then compile, ; immediate restrict
: ahead ( -- orig ) \ unconditional branch to orig
$E9 c, >mark ; immediate restrict \ mark origin
: begin ( -- dest ) \ begin BEGIN .. UNTIL etc loop
<mark ; immediate restrict \ mark destination
: again ( dest -- ) \ end BEGIN .. AGAIN loop
$E9 c, \ unconditional branch to dest
<resolve ; immediate restrict \ resolve destination
: until ( dest -- ) \ end BEGIN .. UNTIL loop
cmp0, $0F c, $84 c, \ branch to dest if TOS = 0
<resolve ; immediate restrict \ resolve destination
: while ( dest -- orig dest ) \ begin .. WHILE .. REPEAT clause
cmp0, $0F c, $84 c, \ branch to dest if TOS = 0
>mark swap ; immediate restrict \ mark orig and place orig under dest
: repeat ( orig dest -- ) \ end BEGIN .. WHILE .. REPEAT
postpone again \ unconditional branch to dest
postpone then ; immediate restrict \ resolve WHILE origin
: do ( -- do-sys ) \ begin a DO .. LOOP loop
postpone (do) \ call (do)
>mark \ mark end of loop address
<mark ; immediate restrict \ mark start of loop address
: ?do ( -- do-sys ) \ begin a ?DO .. LOOP loop
postpone (?do) \ call (?do)
>mark \ mark end of loop address
<mark ; immediate restrict \ mark start of loop address
: loop ( do-sys -- ) \ end a DO .. LOOP loop
$FF c, $04 c, $24 c, \ inc dword [esp]
$0F c, $81 c, rel, \ jno "start of loop"
$83 c, $C4 c, $0C c, \ add esp, # 12
here swap ! ; immediate restrict
: +loop ( do-sys -- ) \ end a DO .. +LOOP loop
$8B c, $C3 c, \ mov eax, ebx
$8B c, $5D c, $00 c, \ mov ebx, [ebp]
$83 c, $C5 c, $04 c, \ add ebp, # 4
$01 c, $04 c, $24 c, \ add [esp], eax
$0F c, $81 c, rel, \ jno "start of loop"
$83 c, $C4 c, $0C c, \ add esp, # 12
here swap ! ; immediate restrict
: leave ( do-sys -- ) \ exit one DO .. LOOP
$83 c, $C4 c, $08 c, \ add esp, # 8
$C3 c, ; immediate restrict \ ret
: unloop ( do-sys -- ) \ discard loop params but don't exit
$83 c, $C4 c, $0C c, \ add esp, # 12
; immediate restrict
: ' ( "name" -- xt ) \ return name's execution token
name search-wordlists found? ;
: ` ( "name" -- hp ) \ return name's header pointer
name search-order found? ;
: ['] ( "name" -- ) \ return xt of name at runtime
' postpone literal ; immediate restrict
: char ( "name" -- char ) \ return first char of parsed name
name drop c@ ;
: [char] ( "name" -- char ) \ return first char of name at runtime
char postpone literal ; immediate restrict
: recurse ( -- ) \ recurse to current def at runtime
last xt@ compile, ; immediate restrict
: value ( x "name" -- ) \ define a value which returns x
header
$83 c, $ED c, $04 c, \ sub ebp, # 4
$89 c, $5D c, $00 c, \ mov [ebp], ebx
$8D c, $1D c, , \ lea ebx, x (aligned mov ebx, # x)
$C3 c, ; \ ret
: constant ( x "name" -- ) \ define constant name which returns x
value ;
: create ( "name" -- ) \ create name which returns addr
header
$83 c, $ED c, $04 c, \ sub ebp, # 4
$89 c, $5D c, $00 c, \ mov [ebp], ebx
$BB c, here 5 + , \ mov ebx, # data-addr
$C3 c, ; \ ret
: variable ( "name" -- ) \ create name with one cell data space
create 0 , ;
: user ( "name" -- ) \ create user variable called name
header
$83 c, $ED c, $04 c, \ sub ebp, # 4
$89 c, $5D c, $00 c, \ mov [ebp], ebx
$8B c, $9E c, user-ofs @ , \ mov ebx, user-ofs [esi]
cell user-ofs +! $C3 c, ; \ ret
: defer ( "name" -- ) \ define name which executes is ISed xt
header
$B8 c, 0 , \ mov eax, # xt
$FF c, $E0 c, ; \ jmp eax
: >body ( xt -- a-addr ) \ return data address of xt
dup c@ $B8 =
if 1 \ defer
else dup 4 + @ $1D8D005D =
if 8 \ value
else 12 \ create
then
then + ;
: addr ( "name" -- a-addr ) \ return name's data address
' >body ;
: state@ ( -- state ) \ return contents of STATE
state @ ;
: ," ( "ccc<quote>" -- ) \ parse and compile string ccc
[char] " parse name, ;
: abort" ( "ccc<quote>" -- ) \ if TOS true, abort and display ccc
postpone (abort") ," ; immediate restrict
: ." ( "ccc<quote>" -- ) \ display ccc at runtime
postpone (.") ," ; immediate restrict
create string-buf ( -- c-addr ) \ one string buffer for immediate S"
256 chars allot
: s" ( "ccc<quote>" -- ) \ parse ccc and return string c-addr u
state@
if postpone (s") ," exit \ compile (s") and string ccc
then [char] " parse \ interpreting, place in S" buffer
string-buf place string-buf count ; immediate
: c" ( "ccc<quote>" -- ) \ return c-addr for ccc at runtime
postpone (c") ," ; immediate restrict
: to ( x "name" -- ) \ store x in value name at runtime
addr state@
if $8B c, $C3 c, \ mov eax, ebx
$8B c, $5D c, $00 c, \ mov ebx, [ebp]
$83 c, $C5 c, $04 c, \ add ebp, # 4
$A3 c, , \ mov data-addr , eax
else ! \ interpreting, store IMMEDIATEly
then ; immediate
: is ( xt "name" -- ) \ store xt in defer name at runtime
postpone to ; immediate
: defer@ ( "name" -- xt ) \ return defer's current xt
addr state@
if postpone literal \ postpone data addr as literal
postpone @ \ and fetch xt from it
else @ \ interpreting, just fetch xt now
then ; immediate
: wordlist ( -- wid ) \ create a wordlist returning its wid
align here
dup [ 3 cells ] literal + , \ hash table pointer
wid-link , dup to wid-link \ link to previous wordlist
0 , \ pointer to this vocab's header
#threads , \ #threads
here #threads cells dup allot \ hash table
erase ; \ initially zeroes
: :noname ( -- xt ) \ create a nameless colon definition
align here ] \ align, return xt and start compiling
0 to last !csp ; \ last = 0 doesn't allow revealing
: _does> ( -- ) \ runtime for does>
here r@ 1+ \ save here and get ptr to does code
last xt@ 6 + dp ! \ here -> offset into create code
$90 c, compile, \ nop; call does-code
dp ! ; \ restore dp
: does> ( -- ) \ define run time code for last CREATEd
postpone _does> \ call _does>
$C3 c, \ ret
$5B c, ; immediate restrict \ pop ebx