-
Notifications
You must be signed in to change notification settings - Fork 12
/
bootstrap.fs
2774 lines (2377 loc) · 63.6 KB
/
bootstrap.fs
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
h@l@h@!h@C+h!k1k0-h@$k:k0-h@k1k0-+$h@C+h!ih@!h@C+h!kefh@!h@C+h!l!
h@l@h@!h@C+h!k1k0-h@$k h@k1k0-+$h@C+h!ih@!h@C+h!kefh@!h@C+h!l!
h@l@ h@!h@C+h! k1k0-h@$ k\h@k1k0-+$ h@C+h!
i h@!h@C+h!
kkf h@!h@C+h!
kLf h@!h@C+h!
k:k0- h@!h@C+h!
k=f h@!h@C+h!
kJf h@!h@C+h!
k0k5-C* h@!h@C+h!
kef h@!h@C+h!
l!
\ **Now we can use single-line comments!**
\ planckforth -
\ Copyright (C) 2021 nineties
\ This project aims to bootstrap a Forth interpreter
\ from hand-written tiny ELF binary.
\ In the 1st stage, only single character words are registered
\ in the dictionary.
\ List of builtin words:
\ 'Q' ( n -- ) Exit the process
\ 'C' ( -- n ) The size of Cells
\ 'h' ( -- a-addr ) The address of 'here' cell
\ 'l' ( -- a-addr ) The address of 'latest' cell
\ 'k' ( -- c ) Read character
\ 't' ( c -- ) Print character
\ 'j' ( -- ) Unconditional branch
\ 'J' ( n -- ) Jump if a == 0
\ 'f' ( c -- xt ) Get execution token of c
\ 'x' ( xt -- ... ) Run the execution token
\ '@' ( a-addr -- w ) Load value from addr
\ '!' ( w a-addr -- ) Store value to addr
\ '?' ( c-addr -- c ) Load byte from addr
\ '$' ( c c-addr -- ) Store byte to addr
\ 'd' ( -- a-addr ) Get data stack pointer
\ 'D' ( a-addr -- ) Set data stack pointer
\ 'r' ( -- a-addr ) Get return stack pointer
\ 'R' ( a-addr -- ) Set return stack pointer
\ 'i' ( -- a-addr ) Get the interpreter function
\ 'e' ( -- ) Exit current function
\ 'L' ( -- u ) Load immediate
\ 'S' ( -- c-addr ) Load string literal
\ '+' ( a b -- c ) c = (a + b)
\ '-' ( a b -- c ) c = (a - b)
\ '*' ( a b -- c ) c = (a * b)
\ '/' ( a b -- c ) c = (a / b)
\ '%' ( a b -- c ) c = (a % b)
\ '&' ( a b -- c ) c = (a & b)
\ '|' ( a b -- c ) c = (a | b)
\ '^' ( a b -- c ) c = (a ^ b)
\ '<' ( a b -- c ) c = (a < b)
\ 'u' ( a b -- c ) c = (a unsigned< b)
\ '=' ( a b -- c ) c = (a == b)
\ '(' ( a b -- c ) c = a << b (logical)
\ ')' ( a b -- c ) c = a >> b (logical)
\ '%' ( a b -- c ) c = a >> b (arithmetic)
\ 'v' ( -- a-addr u ) argv and argc
\ 'V' ( -- c-addr ) Runtime information string
\ The 1st stage interpreter repeats execution of k, f and x.
\ The following line is an example program of planckforth
\ which prints "Hello World!\n"
\ --
\ kHtketkltkltkotk tkWtkotkrtkltkdtk!tk:k0-tk0k0-Q
\ --
\ This code repeats that 'k' reads a character and 't' prints it.
\ Note that ':' (58) minus '0' (48) is '\n' (10).
\ The structure of the dictionary.
\ +------+----------+---------+------------+---------------+
\ | link | len+flag | name... | padding... | code field ...|
\ +------+----------+---------+------------+---------------+
\ - link pointer to the previous entry (CELL byte)
\ - length of the name (6 bits)
\ - smudge bit (1 bit)
\ - immediate bit (1 bit)
\ - characters of the name (N bytes)
\ - padding to align CELL boundary if necessary.
\ - codewords and datawords (CELL-bye aligned)
\ The code group at the beginning of this file
\ defines ' ' and '\n' as no-op operation and
\ '\' to read following characters until '\n'.
\ Since I couldn't write a comment at the beginning,
\ I repost the definition of '\' for explanation.
\ --
\ h@ ( save addr of new entry )
\ l@ h@!h@C+h! ( set link pointer. *here++ = latest )
\ k1k0-h@$ k\h@k1k0-+$ h@C+h! ( write the name '\' and its length )
\ i h@!h@C+h! ( docol )
\ kkf h@!h@C+h! ( key )
\ kLf h@!h@C+h! ( lit )
\ k:k0- h@!h@C+h! ( '\n' )
\ k=f h@!h@C+h! ( = )
\ kJf h@!h@C+h! ( branch )
\ k0k5-C* h@!h@C+h! ( -5*CELL )
\ kef h@!h@C+h! ( exit )
\ l! ( set latest to this new entry. )
\ --
\ That's all for the brief explanation. Let's restart bootstrap!
\ The COMMA operator
\ ',' ( a -- ) Store a to 'here' and increment 'here' CELL bytes.
h@l@ h@!h@C+h! k1k0-h@$ k,h@k1k0-+$ h@C+h!
i h@!h@C+h! \ docol
\ store 'a' to here
khf h@!h@C+h!
k@f h@!h@C+h!
k!f h@!h@C+h!
\ here <- here + CELL
khf h@!h@C+h!
k@f h@!h@C+h!
kCf h@!h@C+h!
k+f h@!h@C+h!
khf h@!h@C+h!
k!f h@!h@C+h!
\ exit
kef h@!h@C+h!
l!
\ TICK-like operator
\ '\'' ( "c" -- xt ) Get execution token of following character
\ NB: This definition is different from the usual definition of tick
\ because it does not skip leading spaces and can read only a single
\ character. It will be redefined in later stage.
h@l@, k1k0-h@$ k'h@k1k0-+$ h@C+h!
i, kkf, kff, kef,
l!
\ Utility for defining a word
\ 'c' ( "c" -- w )
\ Read character, create new word then push its address.
\ 'latest' will not be updated.
h@l@, k1k0-h@$ kch@k1k0-+$ h@C+h!
i, 'h, '@, 'l, '@, ',,
'L, k1k0-, 'h, '@, '$, \ fill 1
'k, 'h, '@, 'L, k1k0-, '+, '$, \ fill "c"
'L, k0k0-, 'h, '@, 'L, k2k0-, '+, '$, \ fill "\0"
'h, '@, 'C, '+, 'h, '!,
'e, l!
\ '_' ( a -- ) DROP
c_ i, 'd, 'C, '+, 'D, 'e, l!
\ '#' ( a -- a a ) DUP
c# i, 'd, '@, 'e, l!
\ Implementations of TOR and FROMR are a bit tricky.
\ Since return-address will be placed at the top of return stack,
\ the code in the body of these function have to manipulate
\ 2nd element of the stack.
\ '{' ( a -- R:a ) TOR
\ Move value from data stack to return stack.
c{ i,
'r, 'r, '@, \ ( a rsp ret )
'r, 'C, '-, '#, \ ( a rsp ret rsp-1 rsp-1 )
'R, \ ( a rsp+1 ret rsp ) extend return stack
'!, \ ( a rsp+1 ) store return address to the top
'!, \ store a to the 2nd
'e, l!
\ '}' ( R:a -- a ) FROMR
\ Move value from return stack to data stack.
c} i,
'r, 'C, '+, '@, \ ( a ) load 2nd value
'r, '@, \ ( a ret ) load return addr
'r, 'C, '+, '#, \ ( a ret rsp+1 rsp+1 )
'R, \ ( a ret rsp ) reduce return stack
'!, \ ( a , R:ret ) store return addr to top of return stack
'e, l!
\ 'o' ( a b -- a b a ) OVER
co i, 'd, 'C, '+, '@, 'e, l!
\ '~' ( a b -- b a ) SWAP
c~ i,
'o, \ ( a b a )
'{, \ ( a b , R:a )
'd, 'C, '+, \ ( a b sp+1 , R:a )
'!, \ ( b , R:a )
'}, \ ( b a )
'e, l!
\ 'B' ( c -- ) C-COMMA
\ Store byte 'c' to here and increment it
cB i, 'h, '@, '$, 'h, '@, 'L, k1k0-, '+, 'h, '!, 'e, l!
\ 'm' ( c-addr u -- ) CMOVE,
\ Copy u bytes from c-addr to here,
\ increment here u bytes.
cm i,
\ <loop>
'#, 'J, k>k0-C*, \ goto <exit> if u=0
'{, \ preserve u
'#, '?, 'B, \ copy byte
'L, k1k0-, '+, \ increment c-addr
'}, 'L, k1k0-, '-, \ decrement u
'j, k0k?-C*, \ goto <loop>
\ <exit>
'_, '_,
'e, l!
\ 'a' ( c-addr -- a-addr ) ALIGNED
\ Round up to a nearest multiple of CELL
ca i,
'L, Ck1k0--, '+, \ ( a+CELL-1 )
'L, k0k0-C-, \ ( a+CELL-1 ~(CELL-1) )
'&,
'e, l!
\ 'A' ( -- ) ALIGN
\ Round up 'here' to a nearest multiple of CELL
cA i, 'h, '@, 'a, 'h, '!, 'e, l!
\ 'E' ( c-addr1 c-addr2 -- flag ) STR=
\ Compare null-terminated strings.
\ Return 1 if they are same 0 otherwise.
cE i,
\ <loop>
'o, '?, 'o, '?, \ ( c-addr1 c-addr2 c1 c2 )
'o, '=, 'J, k=k0-C*, \ goto <not_equal> if c1<>c2
'J, kAk0-C*, \ goto <equal> if c1==0
'L, k1k0-, '+, '~, \ increment c-addr2
'L, k1k0-, '+, '~, \ increment c-addr1
'j, k0kC-C*, \ goto <loop>
\ <not_equal>
'_, '_, '_, 'L, k0k0-, 'e,
\ <equal>
'_, '_, 'L, k1k0-, 'e,
l!
\ 'z' ( c-addr -- u ) STRLEN
\ Calculate length of string
cz i,
'L, k0k0-, \ 0
\ <loop>
'o, '?, 'J, k;k0-C*, \ goto <exit> if '\0'
'L, k1k0-, '+, '~, \ increment u
'L, k1k0-, '+, '~, \ increment c-addr
'j, k0k=-C*, \ goto <loop>
\ <exit>
'~, '_, 'e,
l!
\ 's' ( c -- n)
\ Return 1 if c==' ' or c=='\n', 0 otherwise.
cs i, '#, 'L, k , '=, '~, 'L, k:k0-, '=, '|, 'e, l!
\ 'W' ( "name" -- c-addr )
\ Skip leading spaces (' ' and '\n'),
\ Read name, then return its address.
\ The maximum length of the name is 63. The behavior is undefined
\ when the name exceeds 63 characters.
\ The buffer will be terminated with '\0'.
\ Note that it returns the address of statically allocated buffer,
\ so the content will be overwritten each time 'W' executed.
\ Allocate buffer of 63+1 bytes or more,
\ push the address for compilation of 'W'
h@ # kpk0-+ h! A
cW~
i,
\ skip leading spaces
'k, '#, 's, 'J, k4k0-C*, '_, 'j, k0k7-C*,
\ p=address of buffer
'L, #, '~,
\ <loop>
\ ( p c )
'o, '$, \ store c to p
'L, k1k0-, '+, \ increment p
'k, '#, 's, 'J, k0k9-C*, \ goto <loop> if c is not space
'_,
'L, k0k0-, 'o, '$, \ fill \0
'_, 'L, , \ return buf
'e, l!
\ 'F' ( c-addr -- w )
\ Lookup multi-character word from dictionary.
\ Return 0 if the word is not found.
\ Entries with smudge-bit=1 are ignored.
cF i,
'l, '@,
\ <loop> ( addr it )
'#, 'J, kEk0-C*, \ goto <exit> if it=NULL
'#, 'C, '+, '?, \ ( addr it len+flag )
'L, k@, '&, \ test smudge-bit of it
'J, k4k0-C*,
\ <1>
\ smudge-bit=1
'@, \ load link
'j, k0k>-C*, \ goto <loop>
\ <2>
\ smudge-bit=0
'o, 'o, \ ( addr it addr it )
'L, Ck1k0-+, '+, \ address of name
\ ( addr1 it addr1 addr2 )
'E, 'J, k0k:-C*, \ goto <1> if different name
\ <exit>
'{, '_, '}, \ Drop addr, return it
'e, l!
\ 'G' ( w -- xt )
\ Get CFA of the word
cG i,
'C, '+, '#, '?, \ ( addr len+flag )
'L, kok0-, '&, \ take length
'+, \ add length to the addr
'L, k2k0-, '+, \ add 2 to the addr (len+field and \0)
'a, \ align
'e, l!
\ 'M' ( -- a-addr)
\ The state variable
\ 0: immediate mode
\ 1: compile mode
h@ k0k0-, \ allocate 1 cell and fill 0
cM~ i, 'L, , 'e, l!
\ 'I'
\ The 2nd Stage Interpreter
cI i,
\ <loop>
'W, \ read name from input
'F, \ find word
'M, '@, \ read state
'J, kAk0-C*, \ goto <immediate> if state=0
\ <compile>
'#, 'C, '+, '?, \ ( w len+flag )
'L, k@k@+, '&, \ test immediate bit
'L, k0k0-, '=,
'J, k5k0-C*, \ goto <immediate> if immediate-bit=1
'G, ',, \ compile
'j, k0kE-C*, \ goto <loop>
\ <immediate>
'G, 'x, \ execute
'j, k0kI-C*, \ goto <loop>
l!
I \ Enter 2nd Stage
\ === 2nd Stage Interpreter ===
} _ \ Drop 1st stage interpreter from call stack
\ '\'' ( "name" -- xt )
\ Redefine existing '\'' which uses 'k' and 'f'
\ to use 'W' and 'F'.
c ' i , ' W , ' F , ' G , ' e , l !
\ [ immediate ( -- )
\ Switch to immediate mode
c [ i , ' L , k 0 k 0 - , ' M , ' ! , ' e , l !
\ Set immediate-bit of [
l @ C + # { ? k @ k @ + | } $
\ ] ( -- )
\ Switch to compile mode
c ] i , ' L , k 1 k 0 - , ' M , ' ! , ' e , l !
\ : ( "name" -- ) COLON
\ Read name, create word with smudge=1,
\ compile 'docol' and enter compile mode.
c : i ,
' A , \ align here
' h , ' @ ,
' l , ' @ , ' , , \ fill link
' l , ' ! , \ update latest
' W , \ read name ( addr )
' # , ' z , ' # , \ ( addr len len )
' L , k @ , ' | , \ set smudge-bit
' B , \ fill length + smudge-bit
' m , \ fill name
' L , k 0 k 0 - , ' B , \ fill \0
' A , \ align here
' i , ' , , \ compile docol
' ] , \ enter compile mode
' e , l !
\ ; ( -- ) SEMICOLON
\ Compile 'exit', unsmudge latest, and enter immediate mode.
c ; i ,
' A , \ align here
' L , ' e , ' , , \ compile exit
' l , ' @ ,
' C , ' + , ' # , ' ? ,
' L , k [ k d + , \ 0xbf
' & , ' ~ , ' $ , \ unsmudge
' [ , \ enter immediate mode
' e , l !
\ Set immediate-bit of ';'
l @ C + # { ? k @ k @ + | } $
: immediate-bit [ ' L , k @ k @ + , ] ; \ 0x80
: smudge-bit [ ' L , k @ , ] ; \ 0x40
: length-mask [ ' L , k o k 0 - , ] ; \ 0x3f
\ ( "name" -- )
: set-immediate
W F C + # { ? immediate-bit | } $
;
\ Set immediate-bit of single-line comment word \
\ so that we can write comments in compile-mode.
set-immediate \
\ Set immediate-bit of 'latest'
: immediate
l @ C + # { ? immediate-bit | } $
;
: alias-builtin \ ( "name-new" "name-old" -- )
\ Create new word "name-new".
\ Copy code pointer of builtin word "name-old" to
\ the new word "name-new".
\ "name-old" must not be a FORTH word.
A h @ l @ , l ! \ fill link, update latest
W # z # B m \ fill length and chars of "name-new"
[ ' L , k 0 k 0 - , ] B \ fill \0
A
W F G @ , \ fill code-pointer of "name-old"
;
\ Add new names to builtin primities.
\ Instead of defining as a new FORTH word like shown below,
\ the aliases are created by copying their code-pointer.
\ : new-name old-name ;
\ Primitive operators which manipulate program counter and return stack
\ can not be defined as a FORTH word.
alias-builtin quit Q
alias-builtin cell C
alias-builtin &here h
alias-builtin &latest l
alias-builtin emit t
alias-builtin branch j
alias-builtin 0branch J
alias-builtin execute x
alias-builtin c@ ?
alias-builtin c! $
alias-builtin sp@ d
alias-builtin sp! D
alias-builtin rp@ r
alias-builtin rp! R
alias-builtin docol i
alias-builtin exit e
alias-builtin lit L
alias-builtin litstring S
alias-builtin /mod /
alias-builtin and &
alias-builtin or |
alias-builtin xor ^
alias-builtin u< u
alias-builtin lshift (
alias-builtin rshift )
alias-builtin arshift %
alias-builtin runtime-info_ V
: bye [ ' L , k 0 k 0 - , ] quit ;
\ Rename existing FORTH words
: >cfa G ;
: c, B ;
: memcpy, m ;
: strlen z ;
: streq E ;
: state M ;
: aligned a ;
: align A ;
: here &here @ ;
: latest &latest @ ;
: >dfa >cfa cell + ;
\ === Stub Functions ===
\ Use 1-step indirect reference so that we can replace
\ the runtime later.
: allot-cell &here @ # cell + &here ! ;
alias-builtin key-old k
allot-cell : &key [ ' L , , ] ;
allot-cell : &key! [ ' L , , ] ;
: key &key @ execute ; \ ( -- c ) Push -1 at EOF
' key-old &key !
: key! &key! @ execute ; \ ( -- c ) Throw exception at EOF
' key-old &key! !
allot-cell : &word [ ' L , , ] ;
: word &word @ execute ; \ ( "name" -- c-addr e )
: stub-word W [ ' L , k 0 k 0 - , ] ;
' stub-word &word !
allot-cell : &word! [ ' L , , ] ;
: word! &word! @ execute ; \ ( "name" -- c-addr ) Throw exception at error
' W &word! !
allot-cell : &find [ ' L , , ] ; \ ( c-addr -- nt|0 )
allot-cell : &find! [ ' L , , ] ; \ ( c-addr -- nt ) Throw exception at error
: find &find @ execute ;
: find! &find! @ execute ;
' F &find !
' F &find! !
: ' word! find! >cfa ;
\ === Compilers ===
\ compile: ( n -- )
\ runtime: ( -- n )
: literal
lit lit , \ compile lit
, \ compile n
; immediate
\ compile: ( "name" -- )
\ '[compile] word' compiles word *now* even if it is immediate
: [compile]
' ,
; immediate
\ ( xt -- )
\ postpone compilation of xt
: (compile)
[compile] literal \ compile 'literal'
[ ' , ] literal , \ compile ,
;
\ compile: ( "name" -- )
\ 'compile word' compiles word *later* even if it is immediate
: compile
' (compile)
; immediate
\ runtime: ( w -- )
: compile, , ;
\ ( -- xt )
: :noname
align
here latest , &latest !
smudge-bit c, \ length 0
align
here
[ docol ] literal , \ compile docol
] \ enter compile mode
;
\ ( "name" -- xt )
\ compile time tick
: [']
' \ read name and get xt
[compile] literal \ call literal
; immediate
\ === Constants ===
\ Since we don't have integer literals yet,
\ define small integer words for convenience
\ and readability.
: 0 [ key 0 key 0 - ] literal ;
: 1 [ key 1 key 0 - ] literal ;
: 2 [ key 2 key 0 - ] literal ;
: 3 [ key 3 key 0 - ] literal ;
: 4 [ key 4 key 0 - ] literal ;
: 5 [ key 5 key 0 - ] literal ;
: 6 [ key 6 key 0 - ] literal ;
: 7 [ key 7 key 0 - ] literal ;
: 8 [ key 8 key 0 - ] literal ;
: 9 [ key 9 key 0 - ] literal ;
: 10 [ key : key 0 - ] literal ;
: 11 [ key ; key 0 - ] literal ;
: 12 [ key < key 0 - ] literal ;
: 13 [ key = key 0 - ] literal ;
: 14 [ key > key 0 - ] literal ;
: 15 [ key ? key 0 - ] literal ;
: 16 [ key @ key 0 - ] literal ;
: -1 [ key 0 key 1 - ] literal ;
: true 1 ;
: false 0 ;
\ === Address Arithmetic ===
: cell+ cell + ;
: cell- cell - ;
: cells cell * ;
: char+ 1 + ;
: char- 1 - ;
: chars ;
\ === Stack Manipulation ===
: drop sp@ cell+ sp! ; \ ( w -- )
: dup sp@ @ ; \ ( w -- w w )
: >r rp@ rp@ @ rp@ cell - dup rp! ! ! ; \ ( w -- R:w )
: r> rp@ cell + @ rp@ @ rp@ cell + dup rp! ! ; \ ( R:w -- w)
: r@ rp@ cell + @ ; \ ( -- w, R: w -- w )
: swap sp@ cell + dup @ >r ! r> ; \ ( a b -- b a )
: rot >r swap r> swap ; \ ( a b c -- b c a )
: -rot swap >r swap r> ; \ ( a b c -- c a b )
: nip swap drop ; \ ( a b -- b )
: over >r dup r> swap ; \ ( a b -- a b a )
: tuck dup -rot ; \ ( a b -- b a b )
: pick cells sp@ + cell + @ ; \ ( xu ... x0 u -- xu ... x0 xu )
: 2drop drop drop ; \ ( a b -- )
: 3drop 2drop drop ; \ ( a b c -- )
: 2dup over over ; \ ( a b -- a b a b )
: 3dup 2 pick 2 pick 2 pick ; \ ( a b c -- a b c a b c )
: 2swap >r -rot r> -rot ; \ ( a b c d -- c d a b )
: 2nip 2swap 2drop ; \ ( a b c d -- c d )
: 2over 3 pick 3 pick ; \ ( a b c d -- a b c d a b )
: 2tuck 2swap 2over ; \ ( a b c d -- c d a b c d )
: 2rot >r >r 2swap r> r> 2swap ; \ ( a b c d e f -- c d e f a b )
: -2rot 2swap >r >r 2swap r> r> ; \ ( a b c d e f -- e f a b c d )
: rdrop r> rp@ ! ; \ ( R:w -- )
\ ( R xu ... x0 u -- xu ... x0 xu )
: rpick
cells rp@ + cell + @
;
\ ( -- a-addr )
\ The bottom address of stacks.
\ sp@ and rp@ points bottom if runtime so far is correct.
: sp0 [ sp@ ] literal ;
: rp0 [ rp@ ] literal ;
\ === Integer Arithmetic ===
: 1+ 1 + ;
: 1- 1 - ;
: / /mod swap drop ;
: mod /mod drop ;
\ ( n -- -n )
: negate 0 swap - ;
\ ( n1 -- n2 )
: not false = ;
\ ( n1 -- n2 )
\ bitwise invert
: invert -1 xor ;
: > swap < ;
: <= > not ;
: >= < not ;
: u> swap u< ;
: u<= u> not ;
: u>= u< not ;
: <> = not ;
: 0= 0 = ;
: 0<> 0 <> ;
: 0< 0 < ;
: 0> 0 > ;
: 0<= 0 <= ;
: 0>= 0 >= ;
\ ( x a b -- f )
\ Returns a <= x & x < b if a <= b.
\ It is equivalent to x-a u< b-a. See chapter 4 of
\ Hacker's delight.
: within over - >r - r> u< ;
\ arithmetic shift
: 2* 1 lshift ;
: 2/ 1 arshift ;
\ === Conditional Branch ===
\ <condition> if <if-true> then
\ <condition> if <if-true> else <if-false> then
\ <condition> unless <if-false> then
\ <condition> unless <if-false> else <if-true> then
\ compile: ( -- orig )
\ runtime: ( n -- )
: if
compile 0branch
here 0 , \ save location of offset, fill dummy
; immediate
\ compile: ( orig -- )
\ runtime: ( -- )
: then
here \ ( orig dest )
over - \ ( orig offset )
swap ! \ fill offset to orig
; immediate
\ compile: ( orig1 -- orig2 )
\ runtime: ( -- )
: else
compile branch
here 0 , \ save location of offset, fill dummy
swap
\ fill offset, here-orig1, to orig1
here
over -
swap !
; immediate
\ compile: ( -- orig )
\ runtime: ( n -- )
: unless
compile not
[compile] if
; immediate
\ ( n -- n n | n )
\ duplicate if n<>0
: ?dup dup if dup then ;
\ === Loops ===
\ begin <body> <condition> until
\ begin <body> again
\ begin <condition> while <body> repeat
\ compile: ( -- dest )
\ runtime: ( -- )
: begin
here \ save location
; immediate
\ compile: ( dest -- )
\ runtime: ( n -- )
: until
compile 0branch
here - , \ fill offset
; immediate
\ compile: ( dest -- )
\ runtime: ( -- )
: again
compile branch
here - , \ fill offset
; immediate
\ compile: ( dest -- orig dest )
\ runtime: ( n -- )
\ dest=location of begin
\ orig=location of while
: while
compile 0branch
here swap
0 , \ save location, fill dummy
; immediate
\ compile: ( orig dest -- )
\ runtime: ( -- )
\ dest=location of begin
\ orig=location of while
: repeat
compile branch
here - , \ fill offset from here to begin
here over - swap ! \ backfill offset from while to here
; immediate
\ === Recursive Call ===
\ recursive call.
\ compiles xt of current definition
: recurse
latest >cfa ,
; immediate
\ === Case ===
\ ---
\ <value> case
\ <value1> of <case1> endof
\ <value2> of <case2> endof
\ ...
\ <default case>
\ endcase
\ ---
\ This is equivalent to
\ ---
\ <value>
\ <value1> over = if drop <case1> else
\ <value2> over = if drop <case2> else
\ ...
\ <default case>
\ then ... then then
\ ---
\ compile: ( -- 0 )
\ runtime: ( n -- )
: case
0 \ push 0 to indicate there is no more case
; immediate
\ compile: ( -- orig )
: of
compile over
compile =
[compile] if
compile drop
; immediate
\ <value> a b rangeof <body> endof
\ Execute <body> when
\ a <= <value> and <value> <= b
: rangeof
compile 2
compile pick
compile >=
compile swap
compile 2
compile pick
compile <=
compile and
[compile] if
compile drop
; immediate
\ compile: ( orig1 -- orig2 )
: endof
[compile] else
; immediate
: endcase
compile drop
begin ?dup while
[compile] then
repeat
; immediate
\ === Integer Arithmetic (that require control flow words) ===
\ ( a b -- c )
: max 2dup > if drop else nip then ;
: min 2dup < if drop else nip then ;
: abs dup 0< if negate then ;
\ === Multiline Comment ===
: '(' [ key ( ] literal ;
: ')' [ key ) ] literal ;
: (
1 \ depth counter
begin ?dup while
key! case
'(' of 1+ endof \ increment depth
')' of 1- endof \ decrement depth
endcase
repeat
; immediate
(
Now we can use multiline comment with ( nests. )
)
( === Memory Operation === )
: +! ( n a-addr -- ) tuck @ + swap ! ;
: -! ( n a-addr -- ) tuck @ swap - swap ! ;
\ allocate n bytes
: allot ( n -- ) &here +! ;
( === create and does> === )
\ no-operation
: nop ;
\ ( "name" -- )
\ Read name and create new dictionary entry.
\ When the word is executed, it pushs value of here
\ at the end of the entry.
: create
align
latest , \ fill link
here cell- &latest ! \ update latest
word! dup strlen
dup c, memcpy, 0 c, align \ fill length, name and \0
docol , \ compile docol
['] lit ,
here 3 cells + , \ compile the address
['] nop , \ does>, if any, will fill this cell
['] exit , \ compile exit
;
: >body ( xt -- a-addr )
5 cells +
;
: (does>)
latest >cfa
3 cells + ! \ replace nop
;
: does>
align
0 [compile] literal \ literal for xt
here cell- \ save addr of xt
\ replace nop with xt at runtime
compile (does>)
[compile] ; \ finish compilation of initialization part
:noname \ start compilation of does> part
swap ! \ backfill xt to the operand of literal
; immediate
( === Variable and Constant === )
\ ( "name" -- )
: variable create 0 , ;
\ ( n "name" -- )
: constant create , does> @ ;
( === Value === )
\ ( n "name" -- )
: value create , does> @ ;
\ ( n "name" -- )
: to
word! find! >cfa >body
state @ if
[compile] literal
compile !
else
!
then
; immediate
( === Throw and Catch === )
\ 'xt catch' saves data stack pointer and a marker
\ to indicate where to return on return stack
\ then execute 'xt'.
\ When 'n throw' is executed, the catch statement returns
\ 'n'. If no throw is executed, returns 0.
\ At the beginning of execution of 'xt', return stack
\ contains following information.
\ +-------------------------+
\ | original return address |
\ | saved stack pointer |
\ | exception marker | <- top of return stack
\ +-------------------------+
\ If no 'throw' is called, after execution of 'xt'
\ program goes to the exception-marker because it is
\ on the top of return stack.
\ The exception-marker drops 'saved stack pointer',
\ push 0 to indicate no error and return to the
\ 'original return address'.
\ When 'n throw' is called, it scans return stack
\ to find the exception-marker, restore return stack pointer
\ and data stack pointer, push error code, and returns to
\ the 'original return address'
create exception-marker
' rdrop , \ drop saved stack pointer
0 literal \ push 0 to indicate no-error
' exit ,
: catch ( xt -- n )
sp@ cell+ >r \ save stack pointer
exception-marker >r \ push exception marker
execute
;
: success 0 ;
: throw ( w -- )
?dup unless exit then \ do nothing if no error
rp@
begin
dup rp0 cell- < \ rp < rp0
while
dup @ \ load return stack entry
exception-marker = if
rp! \ restore return stack pointer
rdrop \ drop exception marker