-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathforth.s
More file actions
2045 lines (1765 loc) · 40.2 KB
/
forth.s
File metadata and controls
2045 lines (1765 loc) · 40.2 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
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
.intel_syntax noprefix
stack_size = 1024
f_code = 0x80
f_immediate = 0x40
.macro item name, flags = 0
link = p_item - .
9:
.if link >= -256/2 && link < 256/2
.byte \flags
.byte link
.elseif link >= -256*256/2 && link < 256*256/2
.byte \flags | 1
.word link
.elseif link >= -256*256*256*256/2 && link < 256*256*256*256/2
.byte \flags | 2
.int link
.elseif link >= -256*256*256*256*256*256*256*256/2 && link < 256*256*256*256*256*256*256*256/2
.byte \flags | 3
.quad link
.endif
p_item = 9b
.byte 9f - . - 1
.ascii "\name"
9:
.endm
.macro callb adr
.if \adr > .
.error "callb do not for forward!"
.endif
.byte b_call8b0 + (. - \adr + 1) >> 8
.byte (. - \adr + 1) & 255
.endm
.section .data
data0:
init_stack: .quad 0
init_rstack: .quad 0
emit_buf: .byte 0
inbuf_size = 256
msg_bye:
.ascii "\nBye!\n"
msg_bye_len = . - msg_bye
bcmd:
.quad bcmd_bad, bcmd_bye, bcmd_num0, bcmd_num1, bcmd_num2, bcmd_num3, bcmd_num4, bcmd_num8 # 0x00
.quad bcmd_lit8, bcmd_lit16, bcmd_lit32, bcmd_lit64, bcmd_call8f, bcmd_call16, bcmd_call32, bcmd_call64
.quad bcmd_branch8, bcmd_branch16, bcmd_qbranch8, bcmd_qbranch16, bcmd_qnbranch8, bcmd_qnbranch16,bcmd_bad, bcmd_exit # 0x10
.quad bcmd_wp, bcmd_neg, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_wm, bcmd_add, bcmd_sub, bcmd_mul, bcmd_div, bcmd_mod, bcmd_divmod, bcmd_abs # 0x20
.quad bcmd_var0, bcmd_var8, bcmd_var16, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_dup, bcmd_drop, bcmd_swap, bcmd_rot, bcmd_mrot, bcmd_over, bcmd_pick, bcmd_roll # 0x30
.quad bcmd_depth, bcmd_nip, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_get, bcmd_set, bcmd_get8, bcmd_set8, bcmd_get16, bcmd_set16, bcmd_get32, bcmd_set32 # 0x40
.quad bcmd_setp, bcmd_setm, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_neq
.quad bcmd_zeq, bcmd_zlt, bcmd_zgt, bcmd_eq, bcmd_lt, bcmd_gt, bcmd_lteq, bcmd_gteq # 0x50
.quad bcmd_and, bcmd_or, bcmd_xor, bcmd_invert, bcmd_rshift, bcmd_lshift, bcmd_bad, bcmd_bad
.quad bcmd_2r, bcmd_r2, bcmd_rget, bcmd_rpick, bcmd_bfind, bcmd_compare, bcmd_move, bcmd_fill # 0x60
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_type, bcmd_emit, bcmd_str, bcmd_strp, bcmd_count, bcmd_bad, bcmd_bad, bcmd_bad # 0x80
.quad bcmd_expect, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad # 0x90
.quad bcmd_do, bcmd_i, bcmd_j, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_call8b0, bcmd_call8b1, bcmd_call8b2, bcmd_call8b3, bcmd_call8b4, bcmd_call8b5, bcmd_call8b6, bcmd_call8b7 # 0xA0
.quad bcmd_call8b8, bcmd_call8b9, bcmd_call8b10, bcmd_call8b11, bcmd_call8b12, bcmd_call8b13, bcmd_call8b14, bcmd_call8b15
.quad bcmd_loop8b0, bcmd_loop8b1, bcmd_loop8b2, bcmd_loop8b3, bcmd_loop8b4, bcmd_loop8b5, bcmd_loop8b6, bcmd_loop8b7 # 0xB0
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad
.quad bcmd_blword, bcmd_quit, bcmd_find, bcmd_cfa, bcmd_execute, bcmd_numberq, bcmd_word, bcmd_bad # 0xF0
.quad bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_bad, bcmd_syscall
# forth last_item context @ ! h dup 8 + swap ! quit
start: .byte b_call16
.word forth - . - 2
.byte b_call16
.word last_item - . - 2
.byte b_call16
.word context - . - 2
.byte b_get
.byte b_set
.byte b_call16
.word vhere - . - 2
.byte b_dup
.byte b_call16
.word h - . - 2
.byte b_set
.byte b_call16
.word definitions - . - 2
.byte b_call16
.word tib - . - 2
.byte b_set
.byte b_lit16
.word fcode_end - fcode
.byte b_call16
.word ntib - . - 2
.byte b_set
.byte b_call16
.word interpret - . - 2
.byte b_quit
inbuf: .byte b_var0
.space inbuf_size
# begin inbuf dup tib ! inbuf_size expect span @ #tib ! 0 >in ! interpret again
quit: .byte b_lit8, 10
.byte b_call16
.word base - . - 2
.byte b_set
.byte b_num0
.byte b_call16
.word state - . - 2
.byte b_set
quit0: .byte b_strp, 1
.ascii "\n"
.byte b_call16
.word prstack - . - 2
.byte b_strp
.byte 2
.ascii "> "
.byte b_call16
.word inbuf - . - 2
.byte b_dup
.byte b_call16
.word tib - . - 2
.byte b_set
.byte b_lit16
.word inbuf_size
.byte b_expect
.byte b_call16
.word span - . - 2
.byte b_get
.byte b_dup
.byte b_qnbranch8, quit_bye - .
.byte b_call16
.word ntib - . - 2
.byte b_set
.byte b_num0
.byte b_call16
.word bin - . - 2
.byte b_set
.byte b_call16
.word interpret - . - 2
.byte b_branch8, quit0 - .
quit_bye: .byte b_bye
bad_code: .byte b_lit8, 16
.byte b_call16
.word base - . - 2
.byte b_set
.byte b_call16
.word dot - . - 2
.byte b_call16
.word dot - . - 2
.byte b_strp
.byte 1f - 0f
0: .ascii " : bad byte code!"
1: .byte b_branch8
.byte quit - .
p_item = .
item forth
forth: .byte b_var8
.byte does_voc - .
.quad 0
does_voc:
.byte b_call8f, context - . - 1
.byte b_set
.byte b_exit
item current
current: .byte b_var0
.quad 0
item definitions
definitions: .byte b_call8f, context - . - 1
.byte b_get
callb current
.byte b_set, b_exit
item context
context: .byte b_var0
v_context: .quad 0
item 0, f_code
.byte b_num0
.byte b_exit
item 1, f_code
.byte b_num1
.byte b_exit
item 2, f_code
.byte b_num2
.byte b_exit
item 3, f_code
.byte b_num3
.byte b_exit
item 4, f_code
.byte b_num4
.byte b_exit
item 8, f_code
.byte b_num8
.byte b_exit
item lit8, f_code
.byte b_lit8
.byte 31
.byte b_exit
item lit16, f_code
.byte b_lit16
.word 31415
.byte b_exit
item lit32, f_code
.byte b_lit32
.int 31415926
.byte b_exit
item lit64, f_code
.byte b_lit64
.quad 31415926897
.byte b_exit
item call8f, f_code
.byte b_call8f
.byte 0f - . - 1
0: .byte b_exit
item call16, f_code
.byte b_call16
.word 0f - . - 2
0: .byte b_exit
item call32, f_code
.byte b_call32
.int 0f - . - 4
0: .byte b_exit
item call64, f_code
.byte b_call64
.quad 0f - . - 8
0: .byte b_exit
item call8b0, f_code
callb 0b
.byte b_exit
item branch8, f_code
.byte b_branch8
.byte 0f - .
0: .byte b_exit
item branch16, f_code
.byte b_branch16
.word 0f - .
0: .byte b_exit
item "?branch8", f_code
.byte b_qbranch8
.byte 0f - .
0: .byte b_exit
item "?branch16", f_code
.byte b_qbranch16
.word 0f - .
0: .byte b_exit
item "?nbranch8", f_code
.byte b_qnbranch8
.byte 0f - .
0: .byte b_exit
item "?nbranch16", f_code
.byte b_qnbranch16
.word 0f - .
0: .byte b_exit
item exit, f_code
.byte b_exit
item 1-, f_code
.byte b_wm
.byte b_exit
item 1+, f_code
.byte b_wp
.byte b_exit
item +, f_code
.byte b_add
.byte b_exit
item -, f_code
.byte b_sub
.byte b_exit
item *, f_code
.byte b_mul
.byte b_exit
item /, f_code
.byte b_div
.byte b_exit
item mod, f_code
.byte b_mod
.byte b_exit
item /mod, f_code
.byte b_divmod
.byte b_exit
item abs, f_code
.byte b_abs
.byte b_exit
item dup, f_code
.byte b_dup
.byte b_exit
item drop, f_code
.byte b_drop
.byte b_exit
item swap, f_code
.byte b_swap
.byte b_exit
item rot, f_code
.byte b_rot
.byte b_exit
item -rot, f_code
.byte b_mrot
.byte b_exit
item over, f_code
.byte b_over
.byte b_exit
item pick, f_code
.byte b_pick
.byte b_exit
item roll, f_code
.byte b_roll
.byte b_exit
item depth, f_code
.byte b_depth
.byte b_exit
item @, f_code
.byte b_get
.byte b_exit
item !, f_code
.byte b_set
.byte b_exit
item c@, f_code
.byte b_get8
.byte b_exit
item c!, f_code
.byte b_set8
.byte b_exit
item w@, f_code
.byte b_get16
.byte b_exit
item w!, f_code
.byte b_set16
.byte b_exit
item i@, f_code
.byte b_get32
.byte b_exit
item i!, f_code
.byte b_set32
.byte b_exit
item +!, f_code
.byte b_setp
.byte b_exit
item -!, f_code
.byte b_setm
.byte b_exit
item >r, f_code
.byte b_2r
.byte b_exit
item r>, f_code
.byte b_r2
.byte b_exit
item r@, f_code
.byte b_rget
.byte b_exit
item rpick, f_code
.byte b_rpick
.byte b_exit
item move, f_code
.byte b_move
.byte b_exit
item fill, f_code
.byte b_fill
.byte b_exit
item syscall, f_code
.byte b_syscall
.byte b_exit
item compare, f_code
.byte b_compare
.byte b_exit
item bfind, f_code
.byte b_bfind
.byte b_exit
item "0=", f_code
.byte b_zeq
.byte b_exit
item 0<, f_code
.byte b_zlt
.byte b_exit
item 0>, f_code
.byte b_zgt
.byte b_exit
item "=", f_code
.byte b_eq
.byte b_exit
item "<>", f_code
.byte b_neq
.byte b_exit
item <, f_code
.byte b_lt
.byte b_exit
item >, f_code
.byte b_gt
.byte b_exit
item "<=", f_code
.byte b_lteq
.byte b_exit
item ">=", f_code
.byte b_gteq
.byte b_exit
item and, f_code
.byte b_and
.byte b_exit
item or, f_code
.byte b_or
.byte b_exit
item xor, f_code
.byte b_xor
.byte b_exit
item invert, f_code
.byte b_invert
.byte b_exit
item rshift, f_code
.byte b_rshift
.byte b_exit
item lshift, f_code
.byte b_lshift
.byte b_exit
item type, f_code
.byte b_type
.byte b_exit
item expect, f_code
.byte b_expect
.byte b_exit
item emit, f_code
.byte b_emit
.byte b_exit
item count, f_code
.byte b_count
.byte b_exit
item "(\")", f_code
.byte b_str
.byte b_exit
item "(.\")", f_code
.byte b_strp
.byte b_exit
item var8, f_code
.byte b_var8
.byte 0f - .
0: .byte b_exit
item var16, f_code
.byte b_var16
.word 0f - .
0: .byte b_exit
item blword, f_code
.byte b_blword
.byte b_exit
item word, f_code
.byte b_word
.byte b_exit
item find, f_code
.byte b_find
.byte b_exit
item cfa, f_code
.byte b_cfa
.byte b_exit
item execute, f_code
.byte b_execute
.byte b_exit
item quit
.byte b_quit
item base
base: .byte b_var0
v_base: .quad 10
holdbuf_len = 70
item holdbuf
holdbuf: .byte b_var0
.space holdbuf_len
item holdpoint
holdpoint: .byte b_var0
.quad 0
item span
span: .byte b_var0
v_span: .quad 0
# : hold holdpoint @ 1- dup holdbuf > if drop drop else dup holdpoint ! c! then ;
item hold
hold: callb holdpoint # holdpoint
.byte b_get # @
.byte b_wm # 1-
.byte b_dup # dup
callb holdbuf # holdbuf
.byte b_gt # >
.byte b_qbranch8 # if
.byte 0f - .
.byte b_drop # drop
.byte b_drop # drop
.byte b_branch8 # êîìàíäà ïåðåõîäà íà âîçâðàò (ïîñëå then)
.byte 1f - .
0: .byte b_dup # dup
callb holdpoint # holdpoint
.byte b_set # !
.byte b_set8 # c!
1: .byte b_exit # ;
# : # base /mod swap dup 10 < if c" 0 + else 10 - c" A + then hold ;
item "#"
conv: .byte b_call16
.word base - . - 2 # base
.byte b_get # @
.byte b_divmod # /mod
.byte b_swap # swap
.byte b_dup # dup
.byte b_lit8
.byte 10 # 10
.byte b_lt # <
.byte b_qnbranch8 # if
.byte 0f - .
.byte b_lit8
.byte '0' # c" 0
.byte b_add # +
.byte b_branch8 # else
.byte 1f - .
0: .byte b_lit8
.byte 'A' - 10 # c" A
.byte b_add # +
1: .byte b_call16
.word hold - . - 2 # hold
.byte b_exit # ;
# : <# holdbuf 70 + holdpoint ! ;
item '<#'
conv_start: .byte b_call16
.word holdbuf - . - 2
.byte b_lit8
.byte holdbuf_len
.byte b_add
.byte b_call16
.word holdpoint - . - 2
.byte b_set
.byte b_exit
# : #s do # dup 0=until ;
item "#s"
conv_s: callb conv
.byte b_dup
.byte b_qbranch8
.byte conv_s - .
.byte b_exit
# : #> holdpoint @ holdbuf 70 + over - ;
item "#>"
conv_end: .byte b_call16
.word holdpoint - . - 2
.byte b_get
.byte b_call16
.word holdbuf - . - 2
.byte b_lit8
.byte holdbuf_len
.byte b_add
.byte b_over
.byte b_sub
.byte b_exit
item "."
dot: .byte b_dup
.byte b_abs
callb conv_start
.byte b_lit8
.byte ' '
.byte b_call16
.word hold - . - 2
callb conv_s
.byte b_drop
.byte b_zlt
.byte b_qnbranch8
.byte 1f - .
.byte b_lit8
.byte '-'
.byte b_call16
.word hold - . - 2
1: callb conv_end
.byte b_type
.byte b_exit
item tib
tib: .byte b_var0
v_tib: .quad 0
item "#tib"
ntib: .byte b_var0
v_ntib: .quad 0
item ">in"
bin: .byte b_var0
v_in: .quad 0
# : .s depth dup . c": emit do dup while dup pick . 1- again drop ;
item ".s" # 11 22 33
prstack: .byte b_depth # 11 22 33 3
.byte b_dup # 11 22 33 3 3
.byte b_strp
.byte 2
.ascii "( "
.byte b_call16 # 11 22 33 3
.word dot - . - 2
.byte b_strp # 11 22 33 3
.byte 3
.ascii "): "
.byte b_dup, b_zlt
.byte b_qnbranch8, 1f - .
.byte b_strp
.byte 14
.ascii "\nStack fault!\n"
.byte b_quit
1: .byte b_dup # 11 22 33 3 3
.byte b_qnbranch8 # 11 22 33 3
.byte 2f - .
.byte b_dup # 11 22 33 3 3
.byte b_pick # 11 22 33 3 11
.byte b_call16 # 11 22 33 3
.word dot - . - 2
.byte b_wm # 11 22 33 2
.byte b_branch8
.byte 1b - .
2: .byte b_drop # 11 22 33
.byte b_exit
.macro prs new_line = 1
.byte b_call16
.word prstack - . - 2
.if \new_line > 0
.byte b_lit8, '\n'
.byte b_emit
.endif
.endm
.macro pr string
.byte b_strp
.byte 9f - 8f
8: .ascii "\n\string"
9:
.endm
item state
state: .byte b_var0
.quad 0
item "]"
.byte b_num1
callb state
.byte b_set, b_exit
item "[", f_immediate
.byte b_num0
callb state
.byte b_set, b_exit
item test_bvc
test_bvc: .byte b_dup, b_neg
.byte b_num0
.byte b_gteq
.byte b_over
.byte b_lit16
.word 0x3ff
.byte b_lteq
.byte b_and
.byte b_qnbranch8, 1f - .
.byte b_num0
.byte b_exit
item test_bv
test_bv: .byte b_dup, b_lit8, 0x80, b_gteq, b_over, b_lit8, 0x7f, b_lteq, b_and, b_qnbranch8, 1f - ., b_num0
.byte b_exit
1: .byte b_dup
.byte b_lit16
.word 0x8001
.byte b_gteq
.byte b_over
.byte b_lit16
.word 0x7ffe
.byte b_lteq, b_and, b_qnbranch8, 2f - ., b_num1, b_exit
2: .byte b_dup
.byte b_lit32
.int 0x80000002
.byte b_gteq
.byte b_over
.byte b_lit32
.int 0x7ffffffd
.byte b_lteq, b_and, b_qnbranch8, 3f - ., b_num2, b_exit
3: .byte b_num3
.byte b_exit
.byte b_num0, b_exit
item h
h: .byte b_var0
.quad 0
item here
here: callb h
.byte b_get
.byte b_exit
# : allot h +! ;
item allot
allot: callb h
.byte b_setp, b_exit
# : , here ! 8 allot ;
item ","
c_64: callb here
.byte b_set, b_num8
callb allot
.byte b_exit
# : i, here w! 4 allot ;
item "i,"
c_32: callb here
.byte b_set32, b_num4
callb allot
.byte b_exit
# : w, here w! 2 allot ;
item "w,"
c_16: callb here
.byte b_set16, b_num2
callb allot
.byte b_exit
# : c, here c! 1 allot ;
item "c,"
c_8: callb here
.byte b_set8, b_num1
callb allot
.byte b_exit
# êîìïèëÿöèÿ áàéò-êîäà
# : compile_bf here 1+ ;
item compile_c
compile_c: .byte b_get8
callb c_8
.byte b_exit
# êîìïèëÿöèÿ çíà÷åíèÿ â îäèí, äâà, ÷åòûðå èëè âîñåìü áàéò
# íà ñòåêå çíà÷åíèå è áàéò, ïîëó÷åííûé test_dv
item compile_1248
compile_1248: .byte b_dup
.byte b_zeq
.byte b_qnbranch8, 1f - .
.byte b_drop
callb c_8
.byte b_exit
1: .byte b_dup, b_num1, b_eq, b_qnbranch8, 2f - .
.byte b_drop
callb c_16
.byte b_exit
2: .byte b_num2, b_eq, b_qnbranch8, 3f - .
callb c_32
.byte b_exit
3: callb c_64
.byte b_exit
# êîìïèëÿöèÿ ÷èñëà
item compile_n
compile_n: callb test_bv
.byte b_dup
.byte b_zeq
.byte b_qnbranch8, 1f - .
.byte b_drop, b_lit8, b_lit8
callb c_8
callb c_8
.byte b_exit
1: .byte b_dup, b_num1, b_eq, b_qnbranch8, 2f - ., b_drop, b_lit8, b_lit16
callb c_8
callb c_16
.byte b_exit
2: .byte b_num2, b_eq, b_qnbranch8, 3f - ., b_lit8, b_lit32
callb c_8
callb c_32
.byte b_exit
3: .byte b_lit8, b_lit64
callb c_8
callb c_64
.byte b_exit
# êîìïèëÿöèÿ âûçîâà áàéò-êîäà
item compile_b
compile_b: callb here
.byte b_num2, b_add
.byte b_sub
callb test_bvc
.byte b_dup
.byte b_zeq
.byte b_qnbranch8, 1f - .
.byte b_drop
.byte b_neg
.byte b_dup
.byte b_lit8, 8
.byte b_rshift
.byte b_lit8, b_call8b0
.byte b_or
callb c_8
callb c_8
.byte b_exit
1: .byte b_dup, b_num1, b_eq, b_qnbranch8, 2f - ., b_drop, b_lit8, b_call16
callb c_8
.byte b_wm
callb c_16
.byte b_exit
2: .byte b_num2, b_eq, b_qnbranch8, 3f - ., b_lit8, b_call32
callb c_8
.byte b_num2, b_sub
callb c_32
.byte b_exit
3: .byte b_lit8, b_call64
callb c_8
.byte b_num3, b_sub
callb c_64
.byte b_exit
#: $compile dup c@ 0x80 and if cfa compile_c else cfa compile_b then ;
item "$compile"
_compile: .byte b_dup, b_get8, b_lit8, 0x80, b_and, b_qnbranch8, 1f - ., b_cfa
callb compile_c
.byte b_exit
1: .byte b_cfa
callb compile_b
.byte b_exit
item interpret
interpret: .byte b_blword
.byte b_dup
.byte b_qnbranch8
.byte 0f - .
.byte b_over
.byte b_over
.byte b_find
.byte b_dup
.byte b_qnbranch8
.byte 1f - .
.byte b_mrot
.byte b_drop
.byte b_drop
callb state
.byte b_get
.byte b_qnbranch8, irpt_execute - . # åñëè 0, ïîåõàëè íà èñïîëíåíèå
.byte b_dup, b_get8, b_lit8, f_immediate, b_and # âûòàùèëè immediate èç ôëàãîâ ñëîâà
.byte b_qbranch8, irpt_execute - . # åñëè ôëàã óñòàíîâëåí - íà èñïîëíåíèå
# âñå ñëîæèëîñüá êîìïèëèðóåì!
callb _compile
.byte b_branch8, 2f - .
irpt_execute: .byte b_cfa # ñþäà ïîïàäàåì, êîãäà íàäî èñïîëíÿòü (state = 0 èëè immediate ñëîâà óñòàíîâëåí)
.byte b_execute
.byte b_branch8, 2f - .
1: .byte b_drop
.byte b_over, b_over
.byte b_numberq
# ïðîâåðêà, ÷òî ÷èñëî ïðåîáðàçîâàëîñü
.byte b_qbranch8, 3f - . # åñëè íà ñòåêå íå 0, çíà÷èò, ïðåîáðàçîâàëîñü è èäåì íà ìåòêó 3
.byte b_type # èíà÷å ïå÷àòàåì ñëîâî
.byte b_strp # ïîòîì äèàãíîñòèêó
.byte 19 # ýòî äëèííà ñîîáùåíèß íèæå
.ascii " : word not found!\n"
.byte b_quit # è çàâåðøàåì èíòåðïðåòàöèþ
3: .byte b_nip, b_nip # óäàëèì çíà÷åíèÿ, ñîõðàíåííûå äëÿ ïå÷àòè ñëîâà (êîìàíäû b_over, b_over)
# â ñòåêå - ïðåîáðàçîâàííîå ÷èñëî
callb state # ïðîâåðèì, êîìïèëÿöèÿ èëè èñïîëíåíèå
.byte b_get
.byte b_qnbranch8, 2f - . # åñëè èñïîëíåíèå - áîëüøå íè÷åãî äåëàòü íå íóæíî; íà ïðîâåðêó - è âûõîä
# êîìïèëÿöèÿ ÷èñëà
callb compile_n
2: # ïðîâåðêà ñòåêà íà âûõîä çà ãðàíèöó
.byte b_depth # ïîëó÷àåì ãëóáèíó ñòåêà
.byte b_zlt # ñðàâíèâàåì, ÷òî ìåíüøå 0 (êîìàíäà 0<)
.byte b_qnbranch8, interpret_ok - . # åñëè óñëîâèå ëîæíî, òî âñå â ïîðßäêå, äåëàåì ïåðåõîä
.byte b_strp # èíà÷å âûâîäèì äèàãíîñòèêó
.byte 14
.ascii "\nstack fault!\n"
.byte b_quit # è çàâåðøàåì èíòåðïðåòàöèþ
interpret_ok: .byte b_branch8
.byte interpret - .
0: .byte b_drop
.byte b_exit
# : str, dup -rot dup c, here swap move 1+ h +!;
item "str,"
c_str: .byte b_dup, b_mrot, b_dup
callb c_8
callb here
.byte b_swap, b_move
callb h
.byte b_setp
.byte b_exit
# : : here blword dup ifnot drop drop ." error - name" exit then here current @ @ - test_bv dup c, compile_1248 str, current @ ! ' exit c, 1 state ! 128 ; immediate
item ":", f_immediate