/
softcore.c
2551 lines (2540 loc) · 102 KB
/
softcore.c
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
/*
** Ficl softcore
** both uncompressed and Lempel-Ziv compressed versions.
**
** Generated 2003/05/05 12:42:30
**/
#include "ficl.h"
static size_t ficlSoftcoreUncompressedSize = 25687; /* not including trailing null */
#if !FICL_WANT_LZ_SOFTCORE
static char ficlSoftcoreUncompressed[] =
": empty ( xn..x1 -- ) depth 0 ?do drop loop ;\n"
": cell- ( addr -- addr ) [ 1 cells ] literal - ;\n"
": -rot ( a b c -- c a b ) 2 -roll ;\n"
": abs ( x -- x )\n"
"dup 0< if negate endif ;\n"
"decimal 32 constant bl\n"
": space ( -- ) bl emit ;\n"
": spaces ( n -- ) 0 ?do space loop ;\n"
": abort\"\n"
"state @ if\n"
"postpone if\n"
"postpone .\"\n"
"postpone cr\n"
"-2\n"
"postpone literal\n"
"postpone throw\n"
"postpone endif\n"
"else\n"
"[char] \" parse\n"
"rot if\n"
"type\n"
"cr\n"
"-2 throw\n"
"else\n"
"2drop\n"
"endif\n"
"endif\n"
"; immediate\n"
".( loading CORE EXT words ) cr\n"
"0 constant false\n"
"false invert constant true\n"
": <> = 0= ;\n"
": 0<> 0= 0= ;\n"
": compile, , ;\n"
": convert char+ 65535 >number drop ; \\ cribbed from DPANS A.6.2.0970\n"
": erase ( addr u -- ) 0 fill ;\n"
"variable span\n"
": expect ( c-addr u1 -- ) accept span ! ;\n"
": nip ( y x -- x ) swap drop ;\n"
": tuck ( y x -- x y x) swap over ;\n"
": within ( test low high -- flag ) over - >r - r> u< ;\n"
": ? ( addr -- ) @ . ;\n"
": dump ( addr u -- )\n"
"0 ?do\n"
"dup c@ . 1+\n"
"i 7 and 7 = if cr endif\n"
"loop drop\n"
";\n"
".( loading SEARCH & SEARCH-EXT words ) cr\n"
": brand-wordlist ( wid -- ) last-word >name drop wid-set-name ;\n"
": ficl-named-wordlist \\ ( hash-size name -- ) run: ( -- wid )\n"
"ficl-wordlist dup create , brand-wordlist does> @ ;\n"
": wordlist ( -- )\n"
"1 ficl-wordlist ;\n"
": ficl-set-current ( wid -- old-wid )\n"
"get-current swap set-current ;\n"
": do-vocabulary ( -- )\n"
"does> @ search> drop >search ;\n"
": ficl-vocabulary ( nBuckets name -- )\n"
"ficl-named-wordlist do-vocabulary ;\n"
": vocabulary ( name -- )\n"
"1 ficl-vocabulary ;\n"
": previous ( -- ) search> drop ;\n"
"1 ficl-named-wordlist hidden\n"
": hide hidden dup >search ficl-set-current ;\n"
": also ( -- )\n"
"search> dup >search >search ;\n"
": forth ( -- )\n"
"search> drop\n"
"forth-wordlist >search ;\n"
": only ( -- )\n"
"-1 set-order ;\n"
"hide\n"
": list-wid ( wid -- )\n"
"dup wid-get-name ( wid c-addr u )\n"
"?dup if\n"
"type drop\n"
"else\n"
"drop .\" (unnamed wid) \" x.\n"
"endif cr\n"
";\n"
"set-current \\ stop hiding words\n"
": order ( -- )\n"
".\" Search:\" cr\n"
"get-order 0 ?do 3 spaces list-wid loop cr\n"
".\" Compile: \" get-current list-wid cr\n"
";\n"
": debug ' debug-xt ; immediate\n"
": on-step .\" S: \" .s-simple cr ;\n"
"previous \\ lose hidden words from search order\n"
"hide\n"
": ?[if] ( c-addr u -- c-addr u flag )\n"
"2dup s\" [if]\" compare-insensitive 0=\n"
";\n"
": ?[else] ( c-addr u -- c-addr u flag )\n"
"2dup s\" [else]\" compare-insensitive 0=\n"
";\n"
": ?[then] ( c-addr u -- c-addr u flag )\n"
"2dup s\" [then]\" compare-insensitive 0= >r\n"
"2dup s\" [endif]\" compare-insensitive 0= r>\n"
"or\n"
";\n"
"set-current\n"
": [else] ( -- )\n"
"1 \\ ( level )\n"
"begin\n"
"begin\n"
"parse-word dup while \\ ( level addr len )\n"
"?[if] if \\ ( level addr len )\n"
"2drop 1+ \\ ( level )\n"
"else \\ ( level addr len )\n"
"?[else] if \\ ( level addr len )\n"
"2drop 1- dup if 1+ endif\n"
"else\n"
"?[then] if 2drop 1- else 2drop endif\n"
"endif\n"
"endif ?dup 0= if exit endif \\ level\n"
"repeat 2drop \\ level\n"
"refill 0= until \\ level\n"
"drop\n"
"; immediate\n"
": [if] ( flag -- )\n"
"0= if postpone [else] then ; immediate\n"
": [then] ( -- ) ; immediate\n"
": [endif] ( -- ) ; immediate\n"
"previous\n"
"variable save-current\n"
": start-prefixes get-current save-current ! <prefixes> set-current ;\n"
": end-prefixes save-current @ set-current ;\n"
": show-prefixes <prefixes> >search words search> drop ;\n"
"start-prefixes\n"
"S\" FICL_WANT_EXTENDED_PREFIX\" ENVIRONMENT? drop [if]\n"
": \" postpone s\" ; immediate\n"
": .( postpone .( ; immediate\n"
": \\ postpone \\ ; immediate\n"
": // postpone \\ ; immediate\n"
": 0b 2 __tempbase ; immediate\n"
": 0o 8 __tempbase ; immediate\n"
"[endif]\n"
": 0d 10 __tempbase ; immediate\n"
": 0x 16 __tempbase ; immediate\n"
"end-prefixes\n"
"S\" FICL_WANT_USER\" ENVIRONMENT? drop [if]\n"
"variable nUser 0 nUser !\n"
": user \\ name ( -- )\n"
"nUser dup @ user 1 swap +! ;\n"
"[endif]\n"
"S\" FICL_WANT_LOCALS\" ENVIRONMENT? drop [if]\n"
": locals| ( name...name | -- )\n"
"begin\n"
"bl word count\n"
"dup 0= abort\" where's the delimiter??\"\n"
"over c@\n"
"[char] | - over 1- or\n"
"while\n"
"(local)\n"
"repeat 2drop 0 0 (local)\n"
"; immediate\n"
": local ( name -- ) bl word count (local) ; immediate\n"
": 2local ( name -- ) bl word count (2local) ; immediate\n"
": end-locals ( -- ) 0 0 (local) ; immediate\n"
": strdup ( c-addr length -- c-addr2 length2 ior )\n"
"0 locals| addr2 length c-addr | end-locals\n"
"length 1 + allocate\n"
"0= if\n"
"to addr2\n"
"c-addr addr2 length move\n"
"addr2 length 0\n"
"else\n"
"0 -1\n"
"endif\n"
";\n"
": strcat ( 2:a 2:b -- 2:new-a )\n"
"0 locals| b-length b-u b-addr a-u a-addr | end-locals\n"
"b-u to b-length\n"
"b-addr a-addr a-u + b-length move\n"
"a-addr a-u b-length +\n"
";\n"
": strcpy ( 2:a 2:b -- 2:new-a )\n"
"locals| b-u b-addr a-u a-addr | end-locals\n"
"a-addr 0 b-addr b-u strcat\n"
";\n"
"[endif]\n"
"S\" FICL_WANT_LOCALS\" ENVIRONMENT? drop [if]\n"
".( loading Johns-Hopkins locals ) cr\n"
"hide\n"
": compiled-zero ficlInstruction0 , ;\n"
": compiled-float-zero ficlInstructionF0 , ;\n"
": ?-- ( c-addr u -- c-addr u flag )\n"
"2dup s\" --\" compare 0= ;\n"
": ?} ( c-addr u -- c-addr u flag )\n"
"2dup s\" }\" compare 0= ;\n"
": ?| ( c-addr u -- c-addr u flag )\n"
"2dup s\" |\" compare 0= ;\n"
"1 constant local-is-double\n"
"2 constant local-is-float\n"
": parse-local-prefix-flags ( c-addr u -- c-addr u flags )\n"
"0 0 0 locals| stop-loop colon-offset flags u c-addr |\n"
"c-addr c@ [char] : =\n"
"if\n"
"over over 0 exit\n"
"endif\n"
"u 0 do\n"
"c-addr i + c@\n"
"case\n"
"[char] 1 of flags local-is-double invert and to flags endof\n"
"[char] 2 of flags local-is-double or to flags endof\n"
"[char] d of flags local-is-double or to flags endof\n"
"[char] f of flags local-is-float or to flags endof\n"
"[char] i of flags local-is-float invert and to flags endof\n"
"[char] s of flags local-is-double invert and to flags endof\n"
"[char] : of i 1+ to colon-offset 1 to stop-loop endof\n"
"1 to stop-loop\n"
"endcase\n"
"stop-loop if leave endif\n"
"loop\n"
"colon-offset 0=\n"
"colon-offset u =\n"
"or\n"
"if\n"
"c-addr u 0 exit\n"
"endif\n"
"c-addr colon-offset +\n"
"u colon-offset -\n"
"flags\n"
";\n"
": ?delim ( c-addr u -- state | c-addr u 0 )\n"
"?| if 2drop 1 exit endif\n"
"?-- if 2drop 2 exit endif\n"
"?} if 2drop 3 exit endif\n"
"dup 0=\n"
"if 2drop 4 exit endif\n"
"0\n"
";\n"
"set-current\n"
": {\n"
"0 0 0 locals| flags local-state nLocals |\n"
"begin\n"
"parse-word ?delim dup to local-state\n"
"0= while\n"
"nLocals 1+ to nLocals\n"
"repeat\n"
"nLocals 0 ?do\n"
"parse-local-prefix-flags to flags\n"
"flags local-is-double and if\n"
"flags local-is-float and if (f2local) else (2local) endif\n"
"else\n"
"flags local-is-float and if (flocal) else (local) endif\n"
"endif\n"
"loop \\ ( )\n"
"local-state 1 = if\n"
"begin\n"
"parse-word\n"
"?delim dup to local-state\n"
"0= while\n"
"parse-local-prefix-flags to flags\n"
"flags local-is-double and if\n"
"flags local-is-float and if\n"
"compiled-float-zero compiled-float-zero (f2local)\n"
"else\n"
"compiled-zero compiled-zero (2local)\n"
"endif\n"
"else\n"
"flags local-is-float and if\n"
"compiled-float-zero (flocal)\n"
"else\n"
"compiled-zero (local)\n"
"endif\n"
"endif\n"
"repeat\n"
"endif\n"
"0 0 (local)\n"
"local-state 2 = if\n"
"begin\n"
"parse-word\n"
"?delim dup to local-state\n"
"3 < while\n"
"local-state 0= if 2drop endif\n"
"repeat\n"
"endif\n"
"local-state 3 <> abort\" syntax error in { } local line\"\n"
"; immediate compile-only\n"
"previous\n"
"[endif]\n"
".( loading MARKER ) cr\n"
": marker ( \"name\" -- )\n"
"create\n"
"get-current ,\n"
"get-order dup ,\n"
"0 ?do , loop\n"
"does>\n"
"0 set-order \\ clear search order\n"
"dup body> >name drop\n"
"here - allot \\ reset HERE to my xt-addr\n"
"dup @ ( pfa current-wid )\n"
"dup set-current forget-wid ( pfa )\n"
"cell+ dup @ swap ( count count-addr )\n"
"over cells + swap ( last-wid-addr count )\n"
"0 ?do\n"
"dup @ dup ( wid-addr wid wid )\n"
">search forget-wid ( wid-addr )\n"
"cell-\n"
"loop\n"
"drop\n"
";\n"
"S\" FICL_WANT_OOP\" ENVIRONMENT? drop [if]\n"
".( loading ficl O-O extensions ) cr\n"
"17 ficl-vocabulary oop\n"
"also oop definitions\n"
"user current-class\n"
"0 current-class !\n"
": parse-method \\ name run: ( -- c-addr u )\n"
"parse-word\n"
"postpone sliteral\n"
"; compile-only\n"
": (lookup-method) { class 2:name -- class 0 | class xt 1 | class xt -1 }\n"
"class name class cell+ @ ( class c-addr u wid )\n"
"search-wordlist\n"
";\n"
": lookup-method { class 2:name -- class xt }\n"
"class name (lookup-method) ( 0 | xt 1 | xt -1 )\n"
"0= if\n"
"name type .\" not found in \"\n"
"class body> >name type\n"
"cr abort\n"
"endif\n"
";\n"
": find-method-xt \\ name ( class -- class xt )\n"
"parse-word lookup-method\n"
";\n"
": catch-method ( instance class c-addr u -- <method-signature> exc-flag )\n"
"lookup-method catch\n"
";\n"
": exec-method ( instance class c-addr u -- <method-signature> )\n"
"lookup-method execute\n"
";\n"
": --> ( instance class -- ??? )\n"
"state @ 0= if\n"
"find-method-xt execute\n"
"else\n"
"parse-method postpone exec-method\n"
"endif\n"
"; immediate\n"
": c-> ( instance class -- ?? exc-flag )\n"
"state @ 0= if\n"
"find-method-xt catch\n"
"else\n"
"parse-method postpone catch-method\n"
"endif\n"
"; immediate\n"
": method create does> body> >name lookup-method execute ;\n"
"1 ficl-named-wordlist instance-vars\n"
"instance-vars dup >search ficl-set-current\n"
": => \\ c:( class meta -- ) run: ( -- ??? ) invokes compiled method\n"
"drop find-method-xt compile, drop\n"
"; immediate compile-only\n"
": my=> \\ c:( -- ) run: ( -- ??? ) late bind compiled method of current-class\n"
"current-class @ dup postpone =>\n"
"; immediate compile-only\n"
": my=[ \\ same as my=> , but binds a chain of methods\n"
"current-class @\n"
"begin\n"
"parse-word 2dup ( class c-addr u c-addr u )\n"
"s\" ]\" compare while ( class c-addr u )\n"
"lookup-method ( class xt )\n"
"dup compile, ( class xt )\n"
"dup ?object if \\ If object member, get new class. Otherwise assume same class\n"
"nip >body cell+ @ ( new-class )\n"
"else\n"
"drop ( class )\n"
"endif\n"
"repeat 2drop drop\n"
"; immediate compile-only\n"
": do-instance-var\n"
"does> ( instance class addr[offset] -- addr[field] )\n"
"nip @ +\n"
";\n"
": addr-units: ( offset size \"name\" -- offset' )\n"
"create over , +\n"
"do-instance-var\n"
";\n"
": chars: \\ ( offset nCells \"name\" -- offset' ) Create n char member.\n"
"chars addr-units: ;\n"
": char: \\ ( offset nCells \"name\" -- offset' ) Create 1 char member.\n"
"1 chars: ;\n"
": cells: ( offset nCells \"name\" -- offset' )\n"
"cells >r aligned r> addr-units:\n"
";\n"
": cell: ( offset nCells \"name\" -- offset' )\n"
"1 cells: ;\n"
": do-aggregate\n"
"objectify\n"
"does> ( instance class pfa -- a-instance a-class )\n"
"2@ ( inst class a-class a-offset )\n"
"2swap drop ( a-class a-offset inst )\n"
"+ swap ( a-inst a-class )\n"
";\n"
": obj: { offset class meta -- offset' } \\ \"name\"\n"
"create offset , class ,\n"
"class meta --> get-size offset +\n"
"do-aggregate\n"
";\n"
": array: ( offset n class meta \"name\" -- offset' )\n"
"locals| meta class nobjs offset |\n"
"create offset , class ,\n"
"class meta --> get-size nobjs * offset +\n"
"do-aggregate\n"
";\n"
": ref: ( offset class meta \"name\" -- offset' )\n"
"locals| meta class offset |\n"
"create offset , class ,\n"
"offset cell+\n"
"does> ( inst class pfa -- ptr-inst ptr-class )\n"
"2@ ( inst class ptr-class ptr-offset )\n"
"2swap drop + @ swap\n"
";\n"
"S\" FICL_WANT_VCALL\" ENVIRONMENT? drop [if]\n"
": vcall: ( paramcnt \"name\" -- )\n"
"current-class @ 8 + dup @ dup 1+ rot ! \\ Kludge fix to get to .vtCount before it's defined.\n"
"create , , \\ ( paramcnt index -- )\n"
"does> \\ ( inst class pfa -- ptr-inst ptr-class )\n"
"nip 2@ vcall \\ ( params offset inst class offset -- )\n"
";\n"
": vcallr: 0x80000000 or vcall: ; \\ Call with return address desired.\n"
"S\" FICL_WANT_FLOAT\" ENVIRONMENT? drop [if]\n"
": vcallf: \\ ( paramcnt -<name>- f: r )\n"
"0x80000000 or\n"
"current-class @ 8 + dup @ dup 1+ rot ! \\ Kludge fix to get to .vtCount before it's defined.\n"
"create , , \\ ( paramcnt index -- )\n"
"does> \\ ( inst class pfa -- ptr-inst ptr-class )\n"
"nip 2@ vcall f> \\ ( params offset inst class offset -- f: r )\n"
";\n"
"[endif] \\ FICL_WANT_FLOAT\n"
"[endif] \\ FICL_WANT_VCALL\n"
": end-class ( old-wid addr[size] size -- )\n"
"swap ! set-current\n"
"search> drop \\ pop struct builder wordlist\n"
";\n"
": suspend-class ( old-wid addr[size] size -- ) end-class ;\n"
"set-current previous\n"
": do-do-instance ( -- )\n"
"s\" : .do-instance does> [ current-class @ ] literal ;\"\n"
"evaluate\n"
";\n"
":noname\n"
"wordlist\n"
"create\n"
"immediate\n"
"0 , \\ NULL parent class\n"
"dup , \\ wid\n"
"[ S\" FICL_WANT_VCALL\" ENVIRONMENT? drop ] [if]\n"
"4 cells , \\ instance size\n"
"[else]\n"
"3 cells , \\ instance size\n"
"[endif]\n"
"ficl-set-current\n"
"does> dup\n"
"; execute metaclass\n"
"metaclass drop cell+ @ brand-wordlist\n"
"metaclass drop current-class !\n"
"do-do-instance\n"
"instance-vars >search\n"
"create .super ( class metaclass -- parent-class )\n"
"0 cells , do-instance-var\n"
"create .wid ( class metaclass -- wid ) \\ return wid of class\n"
"1 cells , do-instance-var\n"
"S\" FICL_WANT_VCALL\" ENVIRONMENT? drop [if]\n"
"create .vtCount \\ Number of VTABLE methods, if any\n"
"2 cells , do-instance-var\n"
"create .size ( class metaclass -- size ) \\ return class's payload size\n"
"3 cells , do-instance-var\n"
"[else]\n"
"create .size ( class metaclass -- size ) \\ return class's payload size\n"
"2 cells , do-instance-var\n"
"[endif]\n"
": get-size metaclass => .size @ ;\n"
": get-wid metaclass => .wid @ ;\n"
": get-super metaclass => .super @ ;\n"
"S\" FICL_WANT_VCALL\" ENVIRONMENT? drop [if]\n"
": get-vtCount metaclass => .vtCount @ ;\n"
": get-vtAdd metaclass => .vtCount ;\n"
"[endif]\n"
": instance ( class metaclass \"name\" -- instance class )\n"
"locals| meta parent |\n"
"create\n"
"here parent --> .do-instance \\ ( inst class )\n"
"parent meta metaclass => get-size\n"
"allot \\ allocate payload space\n"
";\n"
": array ( n class metaclass \"name\" -- n instance class )\n"
"locals| meta parent nobj |\n"
"create nobj\n"
"here parent --> .do-instance \\ ( nobj inst class )\n"
"parent meta metaclass => get-size\n"
"nobj * allot \\ allocate payload space\n"
";\n"
": new \\ ( class metaclass \"name\" -- )\n"
"metaclass => instance --> init\n"
";\n"
": new-array ( n class metaclass \"name\" -- )\n"
"metaclass => array\n"
"--> array-init\n"
";\n"
": alloc \\ ( class metaclass -- instance class )\n"
"locals| meta class |\n"
"class meta metaclass => get-size allocate ( -- addr fail-flag )\n"
"abort\" allocate failed \" ( -- addr )\n"
"class 2dup --> init\n"
";\n"
": alloc-array \\ ( n class metaclass -- instance class )\n"
"locals| meta class nobj |\n"
"class meta metaclass => get-size\n"
"nobj * allocate ( -- addr fail-flag )\n"
"abort\" allocate failed \" ( -- addr )\n"
"nobj over class --> array-init\n"
"class\n"
";\n"
": allot { 2:this -- 2:instance }\n"
"here ( instance-address )\n"
"this my=> get-size allot\n"
"this drop 2dup --> init\n"
";\n"
": allot-array { nobj 2:this -- 2:instance }\n"
"here ( instance-address )\n"
"this my=> get-size nobj * allot\n"
"this drop 2dup ( 2instance 2instance )\n"
"nobj -rot --> array-init\n"
";\n"
": ref ( instance-addr class metaclass \"name\" -- )\n"
"drop create , ,\n"
"does> 2@\n"
";\n"
": resume-class { 2:this -- old-wid addr[size] size }\n"
"this --> .wid @ ficl-set-current ( old-wid )\n"
"this --> .size dup @ ( old-wid addr[size] size )\n"
"instance-vars >search\n"
";\n"
": sub ( class metaclass \"name\" -- old-wid addr[size] size )\n"
"wordlist\n"
"locals| wid meta parent |\n"
"parent meta metaclass => get-wid\n"
"wid wid-set-super \\ set superclass\n"
"create immediate \\ get the subclass name\n"
"wid brand-wordlist \\ label the subclass wordlist\n"
"here current-class ! \\ prep for do-do-instance\n"
"parent , \\ save parent class\n"
"wid , \\ save wid\n"
"[ S\" FICL_WANT_VCALL\" ENVIRONMENT? drop ] [if]\n"
"parent meta --> get-vtCount ,\n"
"[endif]\n"
"here parent meta --> get-size dup , ( addr[size] size )\n"
"metaclass => .do-instance\n"
"wid ficl-set-current -rot\n"
"do-do-instance\n"
"instance-vars >search \\ push struct builder wordlist\n"
";\n"
": offset-of ( class metaclass \"name\" -- offset )\n"
"drop find-method-xt nip >body @ ;\n"
": id ( class metaclass -- c-addr u )\n"
"drop body> >name ;\n"
": methods \\ ( class meta -- )\n"
"locals| meta class |\n"
"begin\n"
"class body> >name type .\" methods:\" cr\n"
"class meta --> get-wid >search words cr previous\n"
"class meta metaclass => get-super\n"
"dup to class\n"
"0= until cr\n"
";\n"
": pedigree ( class meta -- )\n"
"locals| meta class |\n"
"begin\n"
"class body> >name type space\n"
"class meta metaclass => get-super\n"
"dup to class\n"
"0= until cr\n"
";\n"
": see ( class meta -- )\n"
"metaclass => get-wid >search see previous ;\n"
": debug ( class meta -- )\n"
"find-method-xt debug-xt ;\n"
"previous set-current\n"
"metaclass drop\n"
"constant meta\n"
": subclass --> sub ;\n"
"S\" FICL_WANT_VCALL\" ENVIRONMENT? drop [if]\n"
": hasvtable 4 + ; immediate\n"
"[endif]\n"
":noname\n"
"wordlist\n"
"create immediate\n"
"0 , \\ NULL parent class\n"
"dup , \\ wid\n"
"0 , \\ instance size\n"
"[ S\" FICL_WANT_VCALL\" ENVIRONMENT? drop ] [if]\n"
"0 , \\ .vtCount\n"
"[endif]\n"
"ficl-set-current\n"
"does> meta\n"
"; execute object\n"
"object drop cell+ @ brand-wordlist\n"
"object drop current-class !\n"
"do-do-instance\n"
"instance-vars >search\n"
": class ( instance class -- class metaclass )\n"
"nip meta ;\n"
": init ( instance class -- )\n"
"meta\n"
"metaclass => get-size ( inst size )\n"
"erase ;\n"
": array-init ( nobj inst class -- )\n"
"0 dup locals| &init &next class inst |\n"
"class s\" init\" lookup-method to &init\n"
"s\" next\" lookup-method to &next\n"
"drop\n"
"0 ?do\n"
"inst class 2dup\n"
"&init execute\n"
"&next execute drop to inst\n"
"loop\n"
";\n"
": free \\ ( instance class -- )\n"
"drop free\n"
"abort\" free failed \"\n"
";\n"
": super ( instance class -- instance parent-class )\n"
"meta metaclass => get-super ;\n"
": pedigree ( instance class -- )\n"
"object => class\n"
"metaclass => pedigree ;\n"
": size ( instance class -- sizeof-instance )\n"
"object => class\n"
"metaclass => get-size ;\n"
": methods ( instance class -- )\n"
"object => class\n"
"metaclass => methods ;\n"
": index ( n instance class -- instance[n] class )\n"
"locals| class inst |\n"
"inst class\n"
"object => class\n"
"metaclass => get-size * ( n*size )\n"
"inst + class ;\n"
": next ( instance[n] class -- instance[n+1] class )\n"
"locals| class inst |\n"
"inst class\n"
"object => class\n"
"metaclass => get-size\n"
"inst +\n"
"class ;\n"
": prev ( instance[n] class -- instance[n-1] class )\n"
"locals| class inst |\n"
"inst class\n"
"object => class\n"
"metaclass => get-size\n"
"inst swap -\n"
"class ;\n"
": debug ( 2this -- ?? )\n"
"find-method-xt debug-xt ;\n"
"previous set-current\n"
"only definitions\n"
": oo only also oop definitions ;\n"
"[endif]\n"
"S\" FICL_WANT_OOP\" ENVIRONMENT? drop [if]\n"
".( loading ficl utility classes ) cr\n"
"also oop definitions\n"
"object subclass c-ref\n"
"cell: .class\n"
"cell: .instance\n"
": get ( inst class -- refinst refclass )\n"
"drop 2@ ;\n"
": set ( refinst refclass inst class -- )\n"
"drop 2! ;\n"
"end-class\n"
"object subclass c-byte\n"
"char: .payload\n"
": get drop c@ ;\n"
": set drop c! ;\n"
"end-class\n"
"object subclass c-2byte\n"
"2 chars: .payload\n"
": get drop w@ ;\n"
": set drop w! ;\n"
"end-class\n"
"object subclass c-4byte\n"
"4 chars: .payload\n"
": get drop q@ ;\n"
": set drop q! ;\n"
"end-class\n"
"object subclass c-cell\n"
"cell: .payload\n"
": get drop @ ;\n"
": set drop ! ;\n"
"end-class\n"
"object subclass c-ptr\n"
"c-cell obj: .addr\n"
": get-ptr ( inst class -- addr )\n"
"c-ptr => .addr\n"
"c-cell => get\n"
";\n"
": set-ptr ( addr inst class -- )\n"
"c-ptr => .addr\n"
"c-cell => set\n"
";\n"
": clr-ptr\n"
"0 -rot c-ptr => .addr c-cell => set\n"
";\n"
": ?null ( inst class -- flag )\n"
"c-ptr => get-ptr 0=\n"
";\n"
": inc-ptr ( inst class -- )\n"
"2dup 2dup ( i c i c i c )\n"
"c-ptr => get-ptr -rot ( i c addr i c )\n"
"--> @size + -rot ( addr' i c )\n"
"c-ptr => set-ptr\n"
";\n"
": dec-ptr ( inst class -- )\n"
"2dup 2dup ( i c i c i c )\n"
"c-ptr => get-ptr -rot ( i c addr i c )\n"
"--> @size - -rot ( addr' i c )\n"
"c-ptr => set-ptr\n"
";\n"
": index-ptr { index 2:this -- }\n"
"this --> get-ptr ( addr )\n"
"this --> @size index * + ( addr' )\n"
"this --> set-ptr\n"
";\n"
"end-class\n"
"c-ptr subclass c-cellPtr\n"
": @size 2drop 1 cells ;\n"
": get ( inst class -- cell )\n"
"c-ptr => get-ptr @\n"
";\n"
": set ( value inst class -- )\n"
"c-ptr => get-ptr !\n"
";\n"
"end-class\n"
"c-ptr subclass c-4bytePtr\n"
": @size 2drop 4 ;\n"
": get ( inst class -- value )\n"
"c-ptr => get-ptr q@\n"
";\n"
": set ( value inst class -- )\n"
"c-ptr => get-ptr q!\n"
";\n"
"end-class\n"
"c-ptr subclass c-2bytePtr\n"
": @size 2drop 2 ;\n"
": get ( inst class -- value )\n"
"c-ptr => get-ptr w@\n"
";\n"
": set ( value inst class -- )\n"
"c-ptr => get-ptr w!\n"
";\n"
"end-class\n"
"c-ptr subclass c-bytePtr\n"
": @size 2drop 1 ;\n"
": get ( inst class -- value )\n"
"c-ptr => get-ptr c@\n"
";\n"
": set ( value inst class -- )\n"
"c-ptr => get-ptr c!\n"
";\n"
"end-class\n"
"previous definitions\n"
"[endif]\n"
"S\" FICL_WANT_OOP\" ENVIRONMENT? drop [if]\n"
".( loading ficl string class ) cr\n"
"also oop definitions\n"
"object subclass c-string\n"
"c-cell obj: .count\n"
"c-cell obj: .buflen\n"
"c-ptr obj: .buf\n"
"32 constant min-buf\n"
": get-count ( 2:this -- count ) my=[ .count get ] ;\n"
": set-count ( count 2:this -- ) my=[ .count set ] ;\n"
": ?empty ( 2:this -- flag ) --> get-count 0= ;\n"
": get-buflen ( 2:this -- len ) my=[ .buflen get ] ;\n"
": set-buflen ( len 2:this -- ) my=[ .buflen set ] ;\n"
": get-buf ( 2:this -- ptr ) my=[ .buf get-ptr ] ;\n"
": set-buf { ptr len 2:this -- }\n"
"ptr this my=[ .buf set-ptr ]\n"
"len this my=> set-buflen\n"
";\n"
": clr-buf ( 2:this -- )\n"
"0 0 2over my=> set-buf\n"
"0 -rot my=> set-count\n"
";\n"
": free-buf { 2:this -- }\n"
"this my=> get-buf\n"
"?dup if\n"
"free\n"
"abort\" c-string free failed\"\n"
"this my=> clr-buf\n"
"endif\n"
";\n"
": size-buf { size 2:this -- }\n"
"size 0< abort\" need positive size for size-buf\"\n"
"size 0= if\n"
"this --> free-buf exit\n"
"endif\n"
"my=> min-buf size over / 1+ * chars to size\n"
"this --> get-buflen 0=\n"
"if\n"
"size allocate\n"
"abort\" out of memory\"\n"
"size this --> set-buf\n"
"size this --> set-buflen\n"
"exit\n"
"endif\n"
"size this --> get-buflen > if\n"
"this --> get-buf size resize\n"
"abort\" out of memory\"\n"
"size this --> set-buf\n"
"endif\n"
";\n"
": set { c-addr u 2:this -- }\n"
"u this --> size-buf\n"
"u this --> set-count\n"
"c-addr this --> get-buf u move\n"
";\n"
": get { 2:this -- c-addr u }\n"
"this --> get-buf\n"
"this --> get-count\n"
";\n"
": cat { c-addr u 2:this -- }\n"
"this --> get-count u + dup >r\n"
"this --> size-buf\n"
"c-addr this --> get-buf this --> get-count + u move\n"
"r> this --> set-count\n"
";\n"
": type { 2:this -- }\n"
"this --> ?empty if .\" (empty) \" exit endif\n"
"this --> .buf --> get-ptr\n"
"this --> .count --> get\n"
"type\n"
";\n"
": compare ( 2string 2:this -- n )\n"
"--> get\n"
"2swap\n"
"--> get\n"
"2swap compare\n"
";\n"
": hashcode ( 2:this -- hashcode )\n"
"--> get hash\n"
";\n"
": free ( 2:this -- ) 2dup --> free-buf object => free ;\n"
"end-class\n"
"c-string subclass c-hashstring\n"
"c-2byte obj: .hashcode\n"
": set-hashcode { 2:this -- }\n"
"this --> super --> hashcode\n"
"this --> .hashcode --> set\n"
";\n"
": get-hashcode ( 2:this -- hashcode )\n"
"--> .hashcode --> get\n"
";\n"
": set ( c-addr u 2:this -- )\n"
"2swap 2over --> super --> set\n"
"--> set-hashcode\n"
";\n"
": cat ( c-addr u 2:this -- )\n"
"2swap 2over --> super --> cat\n"
"--> set-hashcode\n"
";\n"
"end-class\n"
"previous definitions\n"
"[endif]\n"
"S\" FICL_PLATFORM_OS\" ENVIRONMENT? drop S\" WIN32\" compare-insensitive 0= [if]\n"
": GetProcAddress ( name-addr name-u hmodule -- address )\n"
"3 \\ argumentCount\n"
"0 \\ floatArgumentBitfield\n"
"2 \\ cstringArgumentBitfield\n"
"(get-proc-address) \\ functionAddress\n"
"[\n"
"multicall-calltype-function multicall-returntype-integer or literal \\ flags\n"
"]\n"
"multicall ;\n"
": LoadLibrary ( name-addr name-u -- hmodule )\n"
"2 \\ argumentCount\n"
"0 \\ floatArgumentBitfield\n"
"1 \\ cstringArgumentBitfield\n"
"[\n"
"S\" LoadLibraryA\" kernel32.dll GetProcAddress literal \\ functionAddress\n"
"multicall-calltype-function multicall-returntype-integer or literal \\ flags\n"
"]\n"
"multicall ;\n"
": FreeLibrary ( hmodule -- success )\n"
"1 \\ argumentCount\n"
"0 \\ floatArgumentBitfield\n"
"0 \\ cstringArgumentBitfield\n"
"[\n"
"S\" FreeLibrary\" kernel32.dll GetProcAddress literal \\ functionAddress\n"
"multicall-calltype-function multicall-returntype-integer or literal \\ flags\n"
"]\n"
"multicall ;\n"
": DebugBreak ( -- )\n"
"0 \\ argumentCount\n"
"0 \\ floatArgumentBitfield\n"
"0 \\ cstringArgumentBitfield\n"
"[\n"
"S\" DebugBreak\" kernel32.dll GetProcAddress literal \\ functionAddress\n"
"multicall-calltype-function multicall-returntype-void or literal \\ flags\n"
"]\n"
"multicall ;\n"
": OutputDebugString ( addr u -- )\n"
"2 \\ argumentCount\n"
"0 \\ floatArgumentBitfield\n"
"1 \\ cstringArgumentBitfield\n"
"[\n"
"S\" OutputDebugStringA\" kernel32.dll GetProcAddress literal \\ functionAddress\n"
"multicall-calltype-function multicall-returntype-void or literal \\ flags\n"
"]\n"
"multicall ;\n"
": GetTickCount ( -- ticks )\n"
"0 \\ argumentCount\n"
"0 \\ floatArgumentBitfield\n"
"0 \\ cstringArgumentBitfield\n"
"[\n"
"S\" GetTickCount\" kernel32.dll GetProcAddress literal \\ functionAddress\n"
"multicall-calltype-function multicall-returntype-integer or literal \\ flags\n"
"]\n"
"multicall ;\n"
"S\" user32.dll\" LoadLibrary constant user32.dll\n"
": MessageBox ( flags title-addr title-u body-addr body-u hwnd -- button )\n"
"6 \\ argumentCount\n"
"0 \\ floatArgumentBitfield\n"
"[\n"
"2 8 or literal \\ cstringArgumentBitfield\n"
"S\" MessageBoxA\" user32.dll GetProcAddress literal \\ functionAddress\n"
"multicall-calltype-function multicall-returntype-integer or literal \\ flags\n"
"]\n"
"multicall ;\n"
"0x00000000 constant MB_OK\n"
"0x00000001 constant MB_OKCANCEL\n"
"0x00000002 constant MB_ABORTRETRYIGNORE\n"
"0x00000003 constant MB_YESNOCANCEL\n"
"0x00000004 constant MB_YESNO\n"
"0x00000005 constant MB_RETRYCANCEL\n"
"0x00000010 constant MB_ICONHAND\n"
"0x00000020 constant MB_ICONQUESTION\n"
"0x00000030 constant MB_ICONEXCLAMATION\n"
"0x00000040 constant MB_ICONASTERISK\n"
"0x00000080 constant MB_USERICON\n"
"0x00000000 constant MB_DEFBUTTON1\n"
"0x00000100 constant MB_DEFBUTTON2\n"
"0x00000200 constant MB_DEFBUTTON3\n"
"0x00000300 constant MB_DEFBUTTON4\n"
"0x00000000 constant MB_APPLMODAL\n"
"0x00001000 constant MB_SYSTEMMODAL\n"
"0x00002000 constant MB_TASKMODAL\n"
"0x00004000 constant MB_HELP\n"
"0x00008000 constant MB_NOFOCUS\n"
"0x00010000 constant MB_SETFOREGROUND\n"
"0x00020000 constant MB_DEFAULT_DESKTOP_ONLY\n"
"0x00040000 constant MB_TOPMOST\n"
"0x00080000 constant MB_RIGHT\n"
"0x00100000 constant MB_RTLREADING\n"
"MB_ICONEXCLAMATION constant MB_ICONWARNING\n"
"MB_ICONHAND constant MB_ICONERROR\n"
"MB_ICONASTERISK constant MB_ICONINFORMATION\n"
"MB_ICONHAND constant MB_ICONSTOP\n"
"0x00200000 constant MB_SERVICE_NOTIFICATION\n"
"0x00040000 constant MB_SERVICE_NOTIFICATION\n"
"0x00040000 constant MB_SERVICE_NOTIFICATION_NT3X\n"
"0x0000000F constant MB_TYPEMASK\n"
"0x000000F0 constant MB_ICONMASK\n"
"0x00000F00 constant MB_DEFMASK\n"
"0x00003000 constant MB_MODEMASK\n"
"0x0000C000 constant MB_MISCMASK\n"
"1 constant IDOK\n"
"2 constant IDCANCEL\n"
"3 constant IDABORT\n"
"4 constant IDRETRY\n"
"5 constant IDIGNORE\n"
"6 constant IDYES\n"
"7 constant IDNO\n"
"8 constant IDCLOSE\n"
"9 constant IDHELP\n"
": output-debug-string OutputDebugString ;\n"
": debug-break DebugBreak ;\n"
": uaddr->cstring { addr u | cstring -- cstring }\n"
"u 1+ allocate\n"
"0= if\n"
"to cstring\n"
"addr cstring u move\n"