-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathforthright.f
607 lines (561 loc) · 7.04 KB
/
forthright.f
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
: '\n' 10 ;
: bl 32 ;
: cr '\n' emit ;
: space bl emit ;
: negate 0 swap - ;
: true -1 ;
: false 0 ;
: not 0= ;
: literal immediate ['] lit , , ;
: here dp @ ;
: ':' [ char : ] literal ;
: ';' [ char ; ] literal ;
: '(' [ char ( ] literal ;
: ')' [ char ) ] literal ;
: '"' [ char " ] literal ;
: 'A' [ char A ] literal ;
: '0' [ char 0 ] literal ;
: '-' [ char - ] literal ;
: '.' [ char . ] literal ;
: [compile] immediate word (find) >cfa , ;
: recurse immediate latest @ >cfa , ;
: if immediate ['] 0branch , here 0 , ;
: then immediate dup here swap - swap ! ;
: else immediate ['] branch , here 0 , swap dup here swap - swap ! ;
: begin immediate here ;
: until immediate ['] 0branch , here - , ;
: again immediate ['] branch , here - , ;
: while immediate ['] 0branch , here swap 0 , ;
: repeat immediate ['] branch , here - , dup here swap - swap ! ;
: unless immediate ['] not , [compile] if ;
: ( immediate
1
begin
key
dup '(' = if
drop
1+
else
')' = if
1-
then
then
dup 0= until
drop
;
: nip swap drop ;
: tuck swap over ;
: pick 1+ 4 * dsp@ + @ ;
: spaces
begin
dup 0>
while
space
1-
repeat
drop
;
: decimal 10 base ! ;
: hex 16 base ! ;
: u.
base @ u/mod
?dup if
recurse
then
dup 10 < if
'0'
else
10 -
'A'
then
+
emit
;
: .s
dsp@
0 s0 @ + 4-
begin
over over <=
while
dup @ u.
space
4-
repeat
drop drop
;
: uwidth
base @ /
?dup if
recurse 1+
else
1
then
;
: u.r swap dup uwidth rot swap - spaces u. ;
: .r
swap
dup 0< if
negate
1
swap
rot
1-
else
0
swap
rot
then
swap dup uwidth rot swap - spaces swap
if
'-' emit
then
u.
;
: . 0 .r space ;
: u. u. space ;
: ? @ . ;
: within
-rot over <= if
> if
true
else
false
then
else
2drop
false
then
;
: depth s0 @ dsp@ - 4- 4 / ;
: aligned 3 + 3 invert and ;
: align here aligned dp ! ;
: c, here c! 1 dp +! ;
: s" immediate
state @ if
['] litstring ,
here
0 ,
begin
key
dup '"' <>
while
c,
repeat
drop
dup
here swap -
4-
swap !
align
else
here
begin
key
dup '"' <>
while
over c!
1+
repeat
drop
here -
here
swap
then
;
: ." immediate
state @ if
[compile] s"
['] tell ,
else
begin
key
dup '"' = if
drop
exit
then
emit
again
then
;
: constant word header, docol , ['] lit , , ['] exit , ;
: allot dp +! ;
: cells 4 * ;
: variable word header, dodoes , 0 , 1 cells allot ;
: create word header, dodoes , 0 , ;
: does> r> latest @ >dfa ! ;
: value word header, docol , ['] lit , , ['] exit , ;
: to immediate
word (find) >dfa 4+
state @ if
['] lit , , ['] ! ,
else
!
then
;
: +to immediate
word (find) >dfa 4+
state @ if
['] lit , , ['] +! ,
else
+!
then
;
: id.
4+ dup c@ f_lenmask and
begin
dup 0>
while
swap 1+
dup c@
emit
swap 1-
repeat
2drop
;
: ?hidden 4+ c@ f_hidden and ;
: ?immediate 4+ c@ f_immed and ;
: words
latest @
begin
?dup
while
dup ?hidden not if
dup id.
space
then
@
repeat
cr
;
: forget word (find) dup @ latest ! dp ! ;
: dump
base @ -rot
hex
begin
?dup
while
over 8 u.r
space
2dup
1- 15 and 1+
begin
?dup
while
swap
dup c@
2 .r space
1+ swap 1-
repeat
drop
2dup 1- 15 and 1+
begin
?dup
while
swap
dup c@
dup 32 128 within if
emit
else
drop '.' emit
then
1+ swap 1-
repeat
drop
cr
dup 1- 15 and 1+
tuck
-
>r + r>
repeat
drop
base !
;
: case immediate
0
;
: of immediate
['] over ,
['] = ,
[compile] if
['] drop ,
;
: endof immediate [compile] else ;
: endcase immediate
['] drop ,
begin
?dup
while
[compile] then
repeat
;
: exception-marker rdrop 0 ;
: catch dsp@ 4+ >r ['] exception-marker 4+ >r execute ;
: throw
?dup if
rsp@
begin
dup r0 4- <
while
dup @
['] exception-marker 4+ = if
4+
rsp!
dup dup dup
r>
4-
swap over
!
dsp! exit
then
4+
repeat
drop
case
0 1- of
." aborted" cr
endof
." uncaught throw "
dup . cr
endcase
quit
then
;
: abort 0 1- throw ;
: [char] char ['] lit , , ; immediate
: 2over 3 pick 3 pick ;
: cell+ 1 cells + ;
: chars ;
: char+ 1 chars + ;
: 2! swap over ! cell+ ! ;
: 2@ dup cell+ @ swap @ ;
: move cmove ;
: 2>r ['] swap , ['] >r , ['] >r , ; immediate
: 2r> ['] r> , ['] r> , ['] swap , ; immediate
: 2r@ 2r> 2dup 2>r ;
: abs dup 0< if negate then ;
create leave-sp 32 cells allot
leave-sp leave-sp !
: leave
['] unloop ,
['] branch ,
leave-sp @ leave-sp - 31 cells >
if abort then
1 cells leave-sp +!
here leave-sp @ !
0 ,
; immediate
: resolve-leaves
begin
leave-sp @ @ over >
leave-sp @ leave-sp > and
while
here leave-sp @ @ - leave-sp @ @ !
1 cells negate leave-sp +!
repeat
drop
;
: do ['] (do) , here 0 ; immediate
: ?do
['] 2dup ,
['] <> ,
['] 0branch ,
0 ,
['] (do) ,
here 1
; immediate
: resolve-do
if
dup here - ,
dup 2 cells - here over - swap !
else
dup here - ,
then ;
: loop
['] (loop) ,
resolve-do
resolve-leaves
; immediate
: +loop
['] (+loop) ,
resolve-do
resolve-leaves
; immediate
: cfa>
latest @
begin
?dup
while
2dup swap
< if
nip
exit
then
@
repeat
drop
0
;
: see
word (find)
here
latest @
begin
2 pick
over
<>
while
nip
dup @
repeat
drop
swap
':' emit space dup id. space
dup ?immediate if ." immediate " then
>dfa
begin
2dup >
while
dup @
case
['] lit of
4 + dup @
.
endof
['] litstring of
[ char s ] literal emit '"' emit space
4 + dup @
swap 4 + swap
2dup tell
'"' emit space
+ aligned
4 -
endof
['] 0branch of
." 0branch ( "
4 + dup @
.
." ) "
endof
['] branch of
." branch ( "
4 + dup @
.
." ) "
endof
['] (loop) of
." (loop) ( "
4 + dup @
.
." ) "
endof
['] (+loop) of
." (+loop) ( "
4 + dup @
.
." ) "
endof
['] ['] of
." ['] "
4 + dup @
cfa>
id. space
endof
['] exit of
2dup
4 +
<> if
." exit "
then
endof
dup
cfa>
id. space
endcase
4 +
repeat
';' emit cr
2drop
;
: :noname 0 0 header, here docol , ] ;
: ' word (find) >cfa ;
: print-stack-trace
rsp@
begin
dup r0 4- <
while
dup @
case
['] exception-marker 4+ of
." catch ( dsp="
4+ dup @ u.
." ) "
endof
dup
cfa>
?dup if
2dup
id.
[ char + ] literal emit
swap >dfa 4+ - .
then
endcase
4+
repeat
drop
cr
;
: z" immediate
state @ if
['] litstring ,
here
0 ,
begin
key
dup '"' <>
while
here c!
1 dp +!
repeat
0 here c!
1 dp +!
drop
dup
here swap -
4-
swap !
align
['] drop ,
else
here
begin
key
dup '"' <>
while
over c!
1+
repeat
drop
0 swap c!
here
then
;
: strlen
dup
begin
dup c@ 0<>
while
1+
repeat
swap -
;
: cstring swap over here swap cmove here + 0 swap c! here ;
: bye ;
: unused data-segment-size here data-segment-start - - 4 / ;
cr cr cr
6 spaces ." Forthright ver 1.0" cr
." Copyright 2016, Niclas Hedhman" cr
5 spaces ." All rights reserved." cr cr
unused . ." cells remaining" cr
." <ok>" cr
init-done
true echo ( Enable echo )