-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathblocks.frt
4752 lines (3538 loc) · 140 KB
/
blocks.frt
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
ciforth lab $Revision: 5.196 $ (c) Albert van der Horst
: EMPTY STACK
: DICTIONARY FULL
: FIRST ARGUMENT MUST BE OPTION
: ISN'T UNIQUE
: EMPTY NAME FOR NEW DEFINITION
: DISK RANGE ?
: FULL STACK/DICTIONARY FULL
: ERROR ACCESSING BLOCKS FROM MASS STORAGE
: UNRESOLVED FORWARD REFERENCE
: NOT A WORD, NOR A NUMBER OR OTHER DENOTATION
: WORD IS NOT FOUND
: NOT RECOGNIZED
: ERROR, NO FURTHER INFORMATION
: SAVE/RESTORE MUST RUN FROM FLOPPY
: CANNOT FIND WORD TO BE POSTPONED
: CANNOT FIND WORD TO BE COMPILED
: COMPILATION ONLY, USE IN DEFINITION
: EXECUTION ONLY
: CONDITIONALS NOT PAIRED
: STACK UNBALANCE, STRUCTURE UNFINISHED?
: IN PROTECTED DICTIONARY
: USE ONLY WHEN LOADING
: OFF CURRENT EDITING SCREEN
: (WARNING) NOT PRESENT, THOUGH WANTED
: LIST EXPECTS DECIMAL
: AS: PREVIOUS INSTRUCTION INCOMPLETE
: AS: INSTRUCTION PROHIBITED IRREGULARLY
: AS: UNEXPECTED FIXUP/COMMAER
: AS: DUPLICATE FIXUP/UNEXPECTED COMMAER
: AS: COMMAERS IN WRONG ORDER
: AS: DESIGN ERROR, INCOMPATIBLE MASK
: AS: PREVIOUS OPCODE PLUS FIXUPS INCONSISTENT
: INPUT EXHAUSTED
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: REGRESSION TEST FAILS, STACK DEPTH ERROR
: REGRESSION TEST FAILS, RETURN VALUE ERROR
: REGRESSION TEST MALL-FORMED, SECOND PART MISSING
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: NO BUFFER COULD BE FREED, ALL LOCKED
: EXECUTION OF EXTERNAL PROGRAM FAILED
: NOT ENOUGH MEMORY FOR ALLOCATE
: UNKNOWN FORMAT IDENTIFIER
: CANNOT HEAPIFY BUFFER
: NO FORWARD DEFINITION TO RESOLVE
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
: ( NO TEXT MESSAGE AVAILABLE FOR THIS ERROR )
( ************** configuration *******************************)
\
( CONFIG ?LEAVE-SRC ?LEAVE-BLOCK ?16 ?32 ?64 ) \ C4apr09
\ BASE @ 1- 9 <> 13 ?ERROR
DECIMAL
: ?LEAVE-SRC IF SRC CELL+ @ PP ! THEN ;
: ?LEAVE-BLOCK ?LEAVE-SRC ;
: CONFIG CREATE 0= , DOES> @ ?LEAVE-SRC ;
0 CELL+ DUP 2 = CONFIG ?16 DUP 2 > CONFIG ?32+
DUP 4 = CONFIG ?32 DUP 8 = CONFIG ?64 DROP
ENVIRONMENT CPU PREVIOUS 36 BASE ! DROP
DUP ARMV8 = CONFIG ?ARM
DUP AMDX86 = OVER 80386 = OR SWAP 8088. DROP = OR
CONFIG ?IL
WANT CONFIG]
( CONFIG] ?LI ?WI ?OSX ?PC ?MS ?FD ?HD )
"CALL[" PRESENT DUP CONFIG ?WI \ (MS-windows)
"BIOS31" PRESENT DUP CONFIG ?DP \ DPMI (legacy windows)
DUP 0= "BIOSN" PRESENT AND DUP CONFIG ?MS \ MS-DOS
OR OR DUP CONFIG ?WIMS \ One of 3 above
"XOSV" PRESENT DUP CONFIG ?OSX \ OSX.
DUP 0= "XOS" PRESENT AND DUP CONFIG ?LI \ Linux, BSD.,
OR OR CONFIG ?HS \ Any host
"BIOSN" PRESENT CONFIG ?PC \ MS-DOS OR dpmi
"LBAPAR" PRESENT DUP CONFIG ?HD \ Hard disk, modern
"SEC-RW" PRESENT DUP CONFIG ?FD \ Floppy or hard disk old
OR CONFIG ?SA
( CONFIG] ?RP1 ?OP1 ?RV ) \ C2jul07
DECIMAL
\ For the moment all rasp's are type 1 ( A/B)
\ and all oran's are type 1 ( Orange Pi 1+ )
ENVIRONMENT CPU PREVIOUS 36 BASE ! DROP 100 / ARM = DECIMAL
0 CELL+ 2DUP 4 = AND CONFIG ?RP1
2DUP 8 = AND CONFIG ?OP1
2DROP
ENVIRONMENT CPU PREVIOUS 36 BASE ! DROP 0RISCV = DECIMAL
CONFIG ?RV
CREATE CONFIG]
( HELP ) CF: ?HS \ A5dec07
: HELP-WANTED? ." Press space to skip " TYPE
." , other key to confirm" CR KEY BL <> ;
: HELP 1 20 INDEX CR
"I will try to start a help window" TYPE CR
"Press a key" TYPE CR KEY DROP
"XOS" PRESENT IF
"PDF" HELP-WANTED? IF
"acroread ci86.lina.pdf&" SYSTEM EXIT THEN
"PostScript" HELP-WANTED? IF
"gv ci86.lina.ps&" SYSTEM THEN
"info" HELP-WANTED? IF
"info -f ci86.lina.info" SYSTEM THEN
ELSE "BDOSN" PRESENT IF
"wina.pdf" SYSTEM THEN THEN ;
( -syscalls- ) CF: ?LI ?32 \ AvdH B5feb08
DECIMAL
13 CONSTANT __NR_time
43 CONSTANT __NR_times
1 CONSTANT __NR_exit
120 CONSTANT __NR_clone
37 CONSTANT __NR_kill
48 CONSTANT __NR_signal
12 CONSTANT __NR_chdir
HEX
36 CONSTANT __NR_ioctl 8E CONSTANT __NR_select
2A CONSTANT __NR_pipe
65 CONSTANT __NR_ioperm 6E CONSTANT __NR_iopl
CREATE -syscalls- DECIMAL
( -syscalls- ) CF: ?LI ?64 \ AvdH B5feb08
DECIMAL
201 CONSTANT __NR_time
100 CONSTANT __NR_times
60 CONSTANT __NR_exit
56 CONSTANT __NR_clone
62 CONSTANT __NR_kill
48 CONSTANT __NR_signal
80 CONSTANT __NR_chdir
HEX
10 CONSTANT __NR_ioctl 17 CONSTANT __NR_select
16 CONSTANT __NR_pipe
0AD CONSTANT __NR_ioperm 0AC CONSTANT __NR_iopl
CREATE -syscalls- DECIMAL
( -syscalls- ) CF: ?OSX \ AvdH B4feb10
\ 13 CONSTANT __NR_time
\ 43 CONSTANT __NR_times
1 CONSTANT __NR_exit
\ 120 CONSTANT __NR_clone
\ 37 CONSTANT __NR_kill
\ 48 CONSTANT __NR_signal
\ 12 CONSTANT __NR_chdir
CREATE -syscalls- DECIMAL
( -legacy- ) CF: ?LI \ AvdH A8jun24
: LINOS XOS ;
( -legacy- $S $I PRESENT? REQUIRE ) \ C3oct2 AvdH
\ In behalf of old ciforth programs.
"ALIAS" WANTED
'$/ ALIAS $S '$^ ALIAS $I
: REQUIRE NAME WANTED ; : REQUIRED WANTED ;
: PRESENT? PRESENT 0= 0= ; \ For WORD sc: it IS found as such
( -legacy- IN ) \ AvdH C3oct2
"ALIAS" WANTED
\ Fake a parse area that starts at address 0.
'PP ALIAS IN
\ UNCLEAR IF THIS EVER WORKED, KEEP FOR REFERENCE!
\ \ Use L_>IN instead of >IN , don't store into it!
\ : L_>IN PP @ SRC @ - (>IN) ! (>IN) ;
\ 'L_>IN ALIAS >IN
( -traditional- >IN 0>IN ) \ AvdH C3oct2
"ALIAS" WANTED
\ Fake a parse area that starts at address 0.
'PP ALIAS >IN
\ Set parse pointer at start of line.
: 0>IN BEGIN -1 PP +! PP @ 1- C@ ^J = PP @ SRC @ = OR UNTIL ;
\ Closest match to traditional line by line interpreting.
\ Set parse pointer at start of next line,
\ e.g. 'next-line ALIAS REFILL
: next-line BEGIN PP @ 1- C@ ^J <> PP @ SRC CELL+ @ <> AND
WHILE +1 PP +! REPEAT PP @ SRC CELL+ @ <> ;
( -traditional- VOCABULARY ) \ AvdH B5dec01
\ Use replacing vocabularies instead of pushing namespaces.
: FORTH CONTEXT @ 'ONLY >WID <> IF PREVIOUS THEN FORTH ;
: VOCABULARY NAMESPACE DOES> CELL+ CONTEXT ! ;
ALSO \ Start up with two FORTH namespaces.
\ -traditional- WORD FIND (WORD) (PARSE) $!-BD \ C3oct2 AvdH
"ALIAS" WANTED
\ Store a brain-dead string cs into s
: $!-BD 2DUP C! 1+ SWAP CMOVE ;
\ ISO
: FIND DUP COUNT PRESENT DUP IF NIP DUP SWAP
>FFA @ 4 AND -1 SWAP IF NEGATE THEN THEN ;
\ ISO
: WORD DUP BL = IF DROP NAME ELSE >R
BEGIN PP@@ R@ = WHILE DROP REPEAT DROP -1 PP +!
R> PARSE THEN HERE 34 BLANK HERE $!-BD HERE ;
'NAME ALIAS (WORD) 'PARSE ALIAS (PARSE)
( -traditional- ?LOADING ?EXEC TRIAD ABORT" ) \ AvdH C2feb22
\ Exceptions on compilation modes.
: ?EXEC STATE @ 12 ?ERROR ;
: ?LOADING BLK @ 0= 16 ?ERROR ;
\ Print 3 screens around n .
: TRIAD ^L EMIT
3 / 3 * 3 OVER + SWAP DO
CR I LIST 1
+LOOP CR 0 MESSAGE ;
: (ABORT") ROT IF ETYPE ABORT ELSE 2DROP THEN ;
: ABORT" ?COMP POSTPONE " '(ABORT") , ; IMMEDIATE
\
( ) \ AvdH B8feb11
\ This last block belonging to traditional restores
\ meta-behaviour that is traditionally expected.
WANT INCLUDE-FILE
CREATE -traditional- \ Last legacy block!
( **************ISO language extension ***********************)
EXIT
An ISO language extension is either an ISO word, implemented in
a possibly non-portable way, or a word that is not defined in
the standard but that is implemented by using only standard
words.
Sometimes it is an ``in''tension, a loadable extension required
for ISO, that in my opinion should never be loaded.
Note that ISO words are only documented by the comment ISO.
\
( -memory- _AH ) \ AvdH B8jan24
DECIMAL VARIABLE _ADP HERE DUP , DUP , _ADP !
2 CELLS CONSTANT overhead : _AH ( -- adr ) _ADP @ ;
: _bump ( -- ) _AH @ _ADP ! ;
: _alloc_f ( adr -- ) CELL+ DUP >R CELL+ + R> ! ;
: _free ( adr -- ) 0 SWAP CELL+ ! ;
: _split ( adr -- ) DUP $@ SWAP @ ALIGNED >R 0 R@ CELL+ !
R@ ! R> SWAP ! ;
: _merge ( adr -- ) DUP @ OVER U< IF DROP ELSE DUP >R
BEGIN @ DUP CELL+ @ UNTIL R> ! THEN ;
: _free? ( adr -- f ) CELL+ @ 0= ;
: _avail ( adr -- n ) DUP @ SWAP - overhead - ;
: _remain ( adr -- n ) DUP $@ SWAP @ ALIGNED DUP IF
- NIP ELSE DROP SWAP - THEN overhead - ;
: _search ( n -- adr ) _AH BEGIN DUP _merge 2DUP _remain >
WHILE @ DUP _AH = 50 ?ERROR REPEAT _ADP ! DROP ;
( -memory- _allocate _resize heapify ) \ AvdH B8jan24
DECIMAL WANT _AH
: _allocate ( n -- adu ) DUP 0< 50 ?ERROR _AH _merge
DUP _AH _remain > IF _bump DUP _search THEN
_AH _free? 0= IF _AH _split _bump THEN
_AH _alloc_f _AH overhead + ;
: _alloc&move ( n -- adu) DUP _allocate DUP >R SWAP CMOVE R> ;
: _resize ( adu n -- adu) DUP 0< 50 ?ERROR
OVER overhead - >R R@ _merge DUP R@
_avail > IF _alloc&move R> _free ELSE R> _alloc_f THEN ;
\ For address len make buffer current where it is in.
: migrate BEGIN _bump OVER _AH _AH @ 1- WITHIN UNTIL
_AH 2@ <> 52 ?ERROR + _AH @ 1- _AH WITHIN 52 ?ERROR ;
: heapify ( adr n -- ) 2DUP migrate SWAP _AH CELL+ !
_AH _split _bump overhead 2* - _AH _alloc_f
_AH _split _AH _free _bump _AH @ _AH CELL+ ! ;
( -memory- ALLOCATE RESIZE SIZE INIT-ALLOC ) \ AvdH B9aug21
WANT _AH _allocate
: INTEGRITY? ( -- f ) \ Returns true if ok or crashes.
_AH @ BEGIN DUP @ 2DUP < WHILE NIP REPEAT NIP
DUP BEGIN DUP @ 2DUP < WHILE NIP REPEAT NIP = ;
: ALLOCATE ['] _allocate CATCH ; ( ISO )
: FREE overhead - _free 0 ; ( ISO )
: RESIZE ['] _resize CATCH DUP IF NIP THEN ; ( ISO )
: SIZE DUP 1 CELLS - @ SWAP - ;
\ Add n space to allocation space.
: ADD-ALLOC ALIGN HERE SWAP ALIGN DUP ALLOT heapify ;
\ Assign a quarter of the memory to the heap.
: INIT-ALLOC DSP@ HERE - 4 / ADD-ALLOC ;
\ Make a freshly created object at `HERE permanent.
: >ALLOC DUP >R HERE OVER - DUP ALLOCATE THROW
DUP >R SWAP MOVE R> R> DP ! ;
( REALLOT REALLOC AT-HERE ) \ AvdHB5Mar30
"ALIAS" WANTED
\ For DEA of data fill the data pointer with here.
: AT-HERE HERE SWAP >DFA ! ;
\ For DEA of data reserve LENGTH bytes at here.
: REALLOT SWAP AT-HERE ALLOT ;
'REALLOT ALIAS REALLOC
( *s /s fix-scale -fixedpoint- ) \ AvdH C3nov06
WANT ALIAS
8 CELLS 4 - CONSTANT fix-scale \ n represents n.2^-scale.
1 fix-scale LSHIFT CONSTANT 1/1
1 fix-scale 1- LSHIFT CONSTANT 1/2
\ As */ * / but scaled.
: */s >R UM* R> UM/MOD NIP ;
: *s 1/1 */s ; : /s 1/1 SWAP */s ;
\ Most other operators remain the same:
'+ ALIAS +s '- ALIAS -s '*/ ALIAS */s
\ For a RATIO (n/d) return an equivalent scaled NUMBER.
'/s ALIAS rat>s \ By accident.
\ Print a scaled double number
: .mantissa 0 DO BASE @ 1/1 */MOD &0 + EMIT LOOP DROP SPACE ;
: _D.s 1/1 UM/MOD 0 <# #S #> TYPE &. EMIT 20 .mantissa ;
: U.s 0 _D.s ;
( DSQRT SQRTs HYPOs -fixedpoint- ) \ AvdH C3oct10
WANT *s
\ For DOUBLE return FLOOR of its square root
: DSQRT 2DUP OR 0= IF DROP EXIT THEN
DUP IF -1 1 RSHIFT ELSE 1000 THEN >R
BEGIN 2DUP R@ SM/REM R@ + 1 RSHIFT DUP R@ < WHILE
RDROP >R DROP REPEAT
2DROP 2DROP R> ;
: SQRTs 1/1 UM* DSQRT ;
: SQs DUP *s ;
\ For AS BS sides of rectangular triangle return hypothenusa CS
: HYPOs DUP UM* ROT DUP UM* D+ DSQRT ;
\
( ,, CELL ) \ AvdH C3nov05
\ Allot sc .
: ,, HERE SWAP DUP ALLOT MOVE ;
\ Complements CELL+ CELLS
0 CELL+ CONSTANT CELL
( SEARCH-WORDLIST GET-CURRENT SET-CURRENT ) \ AvdH B2aug12
\ ISO
: GET-CURRENT CURRENT @ ;
: SET-CURRENT CURRENT ! ;
: SEARCH-WORDLIST (FIND) NIP NIP
DUP IF DUP >FFA @ 4 AND IF 1 ELSE -1 THEN THEN ;
( NOT 0<> >= <= UMIN U> U.R ) \ AvdH C3Nov05
"ALIAS" WANTED
'0= ALIAS NOT \ Not ISO
: 0<> 0= NOT ; \ ISO
: U> SWAP U< ; \ ISO
: U.R SWAP 0 D.R ; \ ISO
\ The remainder of the words are not ISO
: UMIN 2DUP U< IF SWAP THEN NIP ;
: UMAX 2DUP U< IF SWAP THEN DROP ;
: >= < NOT ; : <= > NOT ;
( D0= D0<> D0< D= D< D- M+ DRSHIFT DLSHIFT DU< ) \ AvdH C4Apr12
"NOT" WANTED "0<>" WANTED
: D0= OR 0= ; : D0<> OR 0<> ;
: D- DNEGATE D+ ; : D0< NIP 0< ; : D= D- D0= ;
: D< ROT 2DUP <> IF > NIP NIP ELSE 2DROP U< THEN ;
: DU< ROT 2DUP <> IF U> NIP NIP ELSE 2DROP U< THEN ;
: D> 2SWAP D< ; : D>= D< NOT ; : D<= D> NOT ;
: DLSHIFT 0 ?DO 2DUP D+ LOOP ;
: DRSHIFT 0 ?DO 2 UDM/MOD ROT DROP LOOP ;
: M+ S>D D+ ;
( PARSE-NAME SAVE-INPUT ) \ AvdH C3oct2
"ALIAS" WANTED
\ Remainder are all ISO words.
: SAVE-INPUT SRC 2@ PP @ 3 ;
: RESTORE-INPUT DROP PP ! SRC 2! -1 ;
'NAME ALIAS PARSE-NAME
\
( TIME&DATE ) CF: ?LI \ AH B30423
"-syscalls-" WANTED : SSE 0 0 0 __NR_time XOS ; ( 1970/1/1)
: | OVER , + ; : 5m 31 | 30 | 31 | 30 | 31 | ;
DATA TABLE ( start of month within leap period) -1
31 | 28 | 5m 5m 31 | 28 | 5m 5m 31 | 29 | 5m 5m
31 | 28 | 5m 5m , : T[] CELLS TABLE + @ ;
\ For DAYS within leap return MONTHS
: MONTHS >R 0 BEGIN R@ OVER T[] > WHILE 1+ REPEAT 1- RDROP ;
\ For DAYS within leap period return DAY MONTH YEARS
: SPLIT-LEAP DUP MONTHS DUP >R T[] - R> 12 /MOD >R 1+ R> ;
\ For TIME return SEC MIN HOUR DAYS
: SPLIT-OFF-TIME 0 60 UM/MOD 60 /MOD 24 /MOD ;
\ For DAYS return DAY MONTH YEAR
: SPLIT-OFF-DATE 1461 /MOD >R SPLIT-LEAP R> 4 * + 1970 + ;
\ Return current SEC MIN HOUR DAY MONTH YEAR
: TIME&DATE SSE SPLIT-OFF-TIME SPLIT-OFF-DATE ;
( TIME&DATE ) CF: ?PC \ AH A30612
HEX
\ Return current DAY MONTH YEAR
: DATE 2A00 _ _ _ BDOSO DROP SWAP >R >R 2DROP
R> 100 /MOD R> ;
\ Return current SEC MIN HOUR
: TIME 2C00 _ _ _ BDOSO 2DROP SWAP >R >R DROP
R> 100 / R> 100 /MOD ;
\ Return current SEC MIN HOUR DAY MONTH YEAR
: TIME&DATE TIME DATE ;
\ In fact a check is in order whether the day has changed
: TIME&DATE DATE >R >R >R TIME DATE 2DROP DUP R> = IF
R> R> ELSE RDROP RDROP 2DROP 2DROP RECURSE THEN ;
DECIMAL
( DEADBEEF leading_hex_digit) CF: \ AvdH A5jun28
\ Are denotations starting with "Z" already known?
"A" 'ONLY >WID (FIND) NIP NIP ?LEAVE-BLOCK
\ Apparently not, so:
"ALIAS" WANTED
CURRENT @ 'ONLY >WID CURRENT ! '3
\ Make sure Forth understands DEADBEEF as a number
DUP ALIAS A DUP ALIAS B DUP ALIAS C DUP ALIAS D
DUP ALIAS E DUP ALIAS F
DROP CURRENT !
\
\ Use 'ONLY >WID CURRENT ! instead of DEFINITIONS
( DEADHORSE leading_hex_digit) CF: \ AvdH A5jun28
\ Are denotations starting with "Z" already known?
"Z" 'ONLY >WID (FIND) NIP NIP ?LEAVE-BLOCK
\ Apparently not, so:
"ALIAS" WANTED
CURRENT @ 'ONLY >WID CURRENT ! '3
\ Make sure Forth understands DEADHORSE as a number
( DUP ALIAS A DUP ALIAS B DUP ALIAS C DUP ALIAS D )
( DUP ALIAS E DUP ALIAS F ) DUP ALIAS G DUP ALIAS H
DUP ALIAS I DUP ALIAS J DUP ALIAS K DUP ALIAS L
DUP ALIAS M DUP ALIAS N DUP ALIAS O DUP ALIAS P
DUP ALIAS Q DUP ALIAS R DUP ALIAS S DUP ALIAS T
DUP ALIAS U DUP ALIAS V DUP ALIAS W DUP ALIAS X
DUP ALIAS Y DUP ALIAS Z
DROP CURRENT !
\ Use 'ONLY >WID CURRENT ! instead of DEFINITIONS
( TUCK -ROT PICK ROLL CS-ROLL AHEAD ) \ C2Dec28 AvdH
\ Obscure stack manipulations.
: PICK 1+ CELLS DSP@ + @ ; \ ISO
: TUCK SWAP OVER ; \ ISO
: -ROT ROT ROT ; \ ISO
: ROLL 1+ >R DSP@ DUP CELL+ R> 2 - CELLS \ ISO
2DUP + @ >R CELL+ MOVE DROP R> ;
: CS-ROLL 2* 1+ DUP >R ROLL R> ROLL ; \ ISO
: AHEAD 'BRANCH , (FORWARD 2 ; IMMEDIATE \ ISO
( -plain-control- ) \ C2Dec28 AvdH
( 'AHEAD HIDDEN ) : AHEAD 'BRANCH , (FORWARD ; IMMEDIATE
'IF HIDDEN : IF '0BRANCH , (FORWARD ; IMMEDIATE
'ELSE HIDDEN : ELSE 'BRANCH , (FORWARD SWAP FORWARD) ;
IMMEDIATE
'THEN HIDDEN : THEN FORWARD) ; IMMEDIATE
'BEGIN HIDDEN : BEGIN (BACK ; IMMEDIATE
'WHILE HIDDEN : WHILE '0BRANCH , (FORWARD SWAP ; IMMEDIATE
'REPEAT HIDDEN : REPEAT 'BRANCH , BACK) FORWARD) ; IMMEDIATE
'AGAIN HIDDEN : AGAIN 'BRANCH , BACK) ; IMMEDIATE
'UNTIL HIDDEN : UNTIL '0BRANCH , BACK) ; IMMEDIATE
'DO HIDDEN : DO '(DO) , (FORWARD (BACK ; IMMEDIATE
'?DO HIDDEN : ?DO '(?DO) , (FORWARD (BACK ; IMMEDIATE
'+LOOP HIDDEN : +LOOP '(+LOOP) , '0BRANCH , BACK) 'UNLOOP ,
FORWARD) ; IMMEDIATE : CS-ROLL ROLL ;
'LOOP HIDDEN : LOOP '1 , POSTPONE +LOOP ; IMMEDIATE
( 2>R 2R> 2R@ 2CONSTANT 2VARIABLE ) \ AvdH C1Apr21
\ Double return stack manipulations all ISO
: 2>R POSTPONE SWAP POSTPONE >R POSTPONE >R ; IMMEDIATE
: 2R> POSTPONE R> POSTPONE R> POSTPONE SWAP ; IMMEDIATE
: 2R@ POSTPONE 2R> POSTPONE 2DUP POSTPONE 2>R ; IMMEDIATE
: 2VARIABLE DATA 0. 2, ; \ ISO
: 2CONSTANT CREATE 2, DOES> 2@ ; \ ISO
\
( READ-LINE GETCHAR ) \ AvdH A5jun28
\ From HANDLE get : a CHAR. Errors are thrown , 6=eof.
: GETCHAR >R 0 DSP@ 1 R@ READ-FILE THROW 0= 6 AND THROW
DUP ^M = IF DROP R@ RECURSE THEN RDROP ;
: eol? ^J = ; \ For CHAR : "it IS a line end".
\ To BUFFER read COUNT chars from HANDLE. Leave free ADDRESS.
: (READ-LINE) SWAP BEGIN >R >R R@ GETCHAR 2DUP SWAP C!
eol? IF R> R> DROP 0 ELSE 1+ R> R> 1- THEN
DUP WHILE REPEAT 2DROP ;
\ To BUFFER read a line of at most COUNT char's from HANDLE.
\ Leave actual COUNT, "chars REMAIN", ERROR.
: READ-LINE ROT DUP >R ROT ROT '(READ-LINE) CATCH
DUP 6 = IF 2DROP 2DROP 0 0 0 ELSE DUP IF 0 0 ROT ELSE
DROP R@ - -1 0 THEN THEN RDROP ;
( WRITE-LINE PUTCHAR ) \ AvdH A5jun28
\ Linux file ending.
DATA CR$ 1 , ^J C,
\ Output CHAR to HANDLE. Errors are thrown.
: PUTCHAR >R DSP@ 1 R> WRITE-FILE THROW DROP ;
\ Write a line from BUFFER COUNT characters to HANDLE.
\ Errors are thrown.
: (WRITE-LINE) DUP >R WRITE-FILE THROW
CR$ $@ R> WRITE-FILE THROW ;
\ Write a line from BUFFER COUNT characters to HANDLE.
\ Leave actual ERROR.
: WRITE-LINE '(WRITE-LINE) CATCH DUP IF >R 2DROP DROP R> THEN ;
( -traditional- INCLUDE-FILE include ) \ C3Nov05 AvdH
WANT R/W READ-LINE
VARIABLE SOURCE-ID 0 SOURCE-ID !
: REFILL SRC @ DUP B/BUF SOURCE-ID @ READ-LINE THROW >R
+ SRC CELL+ ! SRC @ PP ! R> ;
: NAME BEGIN NAME DUP 0= WHILE 2DROP REFILL 0= IF 0.0 EXIT
THEN REPEAT ;
: (INCLUDE-FILE) BEGIN REFILL WHILE INTERPRET REPEAT ;
: INCLUDE-FILE SOURCE-ID @ >R SAVE SOURCE-ID !
SOURCE-ID @ NEGATE DUP LOCK (BUFFER) 2 CELLS + SRC !
'(INCLUDE-FILE) CATCH SOURCE-ID @ NEGATE UNLOCK
RESTORE R> SOURCE-ID ! THROW ;
: include NAME R/O OPEN-FILE THROW DUP >R
'INCLUDE-FILE CATCH R> CLOSE-FILE SWAP THROW THROW ;
( COMPARE $= BOUNDS ALIGN UNUSED ) \ AvdH A1oct04
\ ISO
: COMPARE ROT 2DUP SWAP - >R
MIN CORA DUP IF RDROP ELSE DROP R> THEN ;
\ For STRING1 and STRING2 : "they ARE equal".
: $= ROT OVER = IF CORA 0 = ELSE DROP DROP DROP 0 THEN ;
\ In general use
: BOUNDS OVER + SWAP ;
: UNUSED DSP@ HERE - ;
CF:
"ALIGNED" PRESENT ?LEAVE-BLOCK
\ ISO
: ALIGNED 1- 0 CELL+ 1- OR 1+ ;
: ALIGN DP @ ALIGNED DP ! ;
\
( -manifest MAX-N TRUE NULL NULL$ NONE R/O W/O R/W ) \ AvdH
\ Define some manifest constants.
-1 CONSTANT TRUE \ Flag
0 CONSTANT FALSE \ Flag
0 CONSTANT NULL \ Invalid address
: NULL$ 0 0 ; \ Invalid string
-1 CONSTANT NONE \ Invalid index, where valid is pos.
-1 DUP CONSTANT MAX-U 1 RSHIFT CONSTANT MAX-N
: MAX-UD MAX-U MAX-U ; : MAX-D MAX-U MAX-N ;
0 CONSTANT R/O
1 CONSTANT W/O
2 CONSTANT R/W
HEX : EXEC- 4900 OR ; DECIMAL
\
( 0> U.R ) \ AvdH A2oct23
: 0> 0 > ;
: U.R 0 SWAP D.R ;
( DEFER IS ) \ AvdH B4feb28
\ Default action for not filled in deferred word.
: DEFER-ERROR -1 9 ?ERROR ;
\ Define a word, with a changable behaviour. "deferred" word".
: DEFER CREATE 'DEFER-ERROR , DOES> @ EXECUTE ;
\ Fill XT in as the behaviour of the named deferred word.
\ This ugly word is state-smart!
: IS NAME FOUND >BODY STATE @ IF
POSTPONE LITERAL POSTPONE ! ELSE ! THEN ; IMMEDIATE
\
( [IF] ] [ELSE] [THEN] [DEFINED] [UNDEFINED] ) \ AvdH A7feb20
"COMPARE" WANTED
\ From ANSI manual.
: SKIPPING
1 BEGIN NAME DUP WHILE
2DUP "[IF]" COMPARE 0= IF 2DROP 1+ ELSE
2DUP "[ELSE]" COMPARE 0= IF 2DROP 1- DUP IF 1+ THEN ELSE
"[THEN]" COMPARE 0= IF 1- THEN THEN THEN
?DUP 0= IF EXIT THEN
REPEAT 2DROP DROP ;
: [IF] 0= IF SKIPPING THEN ; IMMEDIATE
: [ELSE] SKIPPING ; IMMEDIATE
: [THEN] ; IMMEDIATE
: [UNDEFINED] NAME 2DUP WANTED PRESENT 0= ; IMMEDIATE
: [DEFINED] POSTPONE [UNDEFINED] 0= ; IMMEDIATE
( VALUE TO FROM ) \ AvdH B2aug07
VARIABLE TO-MESSAGE \ 0 : FROM , 1 : TO .
DATA _value_jumps '@ , '! , '+! ,
: FROM 0 TO-MESSAGE ! ;
\ ISO
: TO 1 TO-MESSAGE ! ;
\ Signal that we want to add to value
: +TO 2 TO-MESSAGE ! ;
\ ISO
: VALUE CREATE , DOES> _value_jumps TO-MESSAGE @ CELLS +
@ EXECUTE FROM ;
( LOCAL ) \ AHCH C3dec20
\ These local's cannot be used recursively
WANT VALUE [{ BAG DO-BAG
16 BAG _locals _locals !BAG
: LOCAL POSTPONE [{ _ VALUE }]
POSTPONE TO LATEST >LFA @ DUP _locals BAG+!
POSTPONE LITERAL POSTPONE EXECUTE
; IMMEDIATE
: ; _locals DO-BAG I @ HIDDEN LOOP-BAG POSTPONE ;
_locals !BAG ;
IMMEDIATE
( ORDER .WID .VOCS ) \ AvdH B9jan22
\ Print all namespace (voc) names in existence.
: .VOCS 'ID. FOR-VOCS ;
\ Print a voc's name from the WID)
: .WID 0 CELL+ - BODY> ID. ;
\ Print the current search order by namespace names
: ORDER CONTEXT BEGIN $@ DUP 'ONLY >WID <> WHILE .WID REPEAT
2DROP &[ EMIT SPACE CURRENT @ .WID &] EMIT ;
\
( PAGE AT-XY INVIS REVERSE ) \ AvdH B5Feb25
HEX "HEX:" WANTED ".FORMAT" WANTED
FORMAT-WID DEFINITIONS
: e 1B CRS$ $C+ ; \ Print escape
: dd DEC: 0 <# #S #> CRS$ $+! ; \ Print N no space.
PREVIOUS DEFINITIONS
\ Define and execute simple escape commands
: esc-seq CREATE $, DROP DOES> 1B EMIT $@ TYPE ;
"[2J" esc-seq CLEAR \ Clear Page
"[H" esc-seq HOME \ Set cursor home
"[?25l" esc-seq INVIS \ Make cursor invisible
"[?25h" esc-seq CVVIS \ Make cursor visible
"[0m" esc-seq NORMAL
"[7m" esc-seq REVERSE
: PAGE HOME CLEAR ; \ ISO
: AT-XY 1+ SWAP 1+ SWAP "%e [%dd ;%dd H" .FORMAT ; \ ISO
( -color- ESCAPE-COLOR ) \ AvdH C3oct08
WANT ,, ESC
CREATE ESCAPE-COLOR ESC C, &[ C, HERE &4 C, &9 C, &; C,
HERE &3 C, &7 C, &; C, &1 C, &m C,
CONSTANT ESCAPE-FORE CONSTANT ESCAPE-BACK
: FORE-COLOR 0 <# # # #> CREATE ,, DOES>
2 ESCAPE-FORE SWAP CMOVE ESCAPE-COLOR 10 TYPE ;
: BACK-COLOR 0 <# # # #> CREATE ,, DOES>
2 ESCAPE-BACK SWAP CMOVE ESCAPE-COLOR 10 TYPE ;
( -color- default-bw red bred ) \ AvdH C3oct08
\ Set the foreground color of the screen in a loop.
: (f-c)s 0 DO DUP FORE-COLOR 1+ LOOP DROP ;
30 8 (f-c)s black red green yellow blue pink aqua grey
\ Set the background color of the screen in a loop.
: (b-c)s 0 DO DUP BACK-COLOR 1+ LOOP DROP ;
40 10 (b-c)s bblack bred bgreen byellow bblue bpink baqua
bgrey b48 bwhite
\ This is sufficient to reset colors to regular B&W
: default-bw ESC EMIT "[00m" TYPE ;
\
( EKEY ) \ AvdH C3oct08
\ Assume that the next key is immediately available.
: EKEY KEY BEGIN KEY? WHILE 8 LSHIFT KEY OR REPEAT ;
\
( BAG !BAG BAG? BAG+! BAG@- BAG-REMOVE BAG-HOLE BAG-INSERT )
\ Warning uses $@ as as @+
( Build a bag with X items. )
: BUILD-BAG HERE CELL+ , CELLS ALLOT ;
( Create a bag "x" with X items. )
: BAG CREATE HERE CELL+ , CELLS ALLOT DOES> ;
: !BAG DUP CELL+ SWAP ! ; ( Make the BAG empty )
: BAG? $@ = 0= ; ( For the BAG : it IS non-empty )
: BAG+! DUP >R @ ! 0 CELL+ R> +! ; ( Push ITEM to the BAG )
: BAG@- 0 CELL+ NEGATE OVER +! @ @ ; ( From BAG: pop ITEM )
: BAG-REMOVE ( Remove entry at ADDRESS from BAG. )
>R DUP CELL+ SWAP OVER R@ @ SWAP - MOVE -1 CELLS R> +! ;
: BAG-HOLE ( Make hole at ADDRESS in BAG. )
>R DUP CELL+ OVER R@ @ SWAP - MOVE 0 CELL+ R> +! ;
( Insert VALUE at ADDRESS in BAG. )
: BAG-INSERT OVER SWAP BAG-HOLE ! ;
( |BAG| DO-BAG .BAG BAG-WHERE IN-BAG? BAG- SET+ SET- ) \ AvdH
: |BAG| $@ SWAP - 0 CELL+ / ; ( For BAG : NUMBER of items )
\ Loop over a bag, see ``.BAG'' for an example.
: DO-BAG POSTPONE $@ POSTPONE SWAP POSTPONE ?DO ; IMMEDIATE
: LOOP-BAG 0 CELL+ POSTPONE LITERAL POSTPONE +LOOP ; IMMEDIATE
: .BAG DO-BAG I ? LOOP-BAG ; ( Print BAG )
( For VALUE and BAG : ADDRESS of value in bag/nill.)
: BAG-WHERE DO-BAG DUP I @ = IF DROP I UNLOOP EXIT THEN
LOOP-BAG DROP 0 ;
( For VALUE and BAG : value IS present in bag.)
: IN-BAG? BAG-WHERE 0= 0= ;
( Remove VALUE from BAG. )
: BAG- DUP >R BAG-WHERE R> BAG-REMOVE ;
( Add/remove VALUE to bag, used as a SET, i.e. no duplicates.)
: SET+ 2DUP IN-BAG? IF 2DROP ELSE BAG+! THEN ;
: SET- 2DUP IN-BAG? IF BAG- ELSE 2DROP THEN ;
( FORMAT FORMAT&EVAL .FORMAT ) \ AH&CH C2feb15
DATA CRS$ 4096 ALLOT \ ":2" WANTED
NAMESPACE FORMAT-WID FORMAT-WID DEFINITIONS
: c CRS$ $C+ ; : n ^J c ; : r ^M c ; \ Add single char's
: d S>D 0 (D.R) CRS$ $+! ; \ Add INT as a string.
: s CRS$ $+! ; \ Add a STRING as such.
PREVIOUS DEFINITIONS
\ Format the first part of STRING, up till %, leave REST.
: _plain &% $/ CRS$ $+! ;
\ Format X with first word of STRING, up till BL, leave REST.
: _format BL $/ 2SWAP >R >R 'FORMAT-WID >WID (FIND) NIP NIP
DUP 0= 51 ?ERROR EXECUTE R> R> ;
\ Format X1 .. Xn using the format STRING.
: FORMAT 0 CRS$ ! BEGIN DUP WHILE _plain DUP IF _format THEN
REPEAT 2DROP CRS$ $@ ;
: FORMAT&EVAL FORMAT EVALUATE ; : .FORMAT FORMAT TYPE ;
( class endclass M: M; ) \ AH B6jan23
"SWAP-DP" WANTED "FORMAT" WANTED VARIABLE LAST-IN
VARIABLE DP-MARKER DATA NAME$ 128 ALLOT DATA BLD$ 4096 ALLOT
: -WORD 1- BEGIN 1- DUP C@ ?BLANK UNTIL 1+ ; ( in -- firstch)
: {BLD PP @ LAST-IN ! ;
\ Retain input since {BLD excluding word name.
: BLD} LAST-IN @ PP @ -WORD OVER - BLD$ $+! ;
: class NAME NAME$ $! NAME$ $@ "VARIABLE ^%s" FORMAT&EVAL
"" BLD$ $! SWAP-DP HERE DP-MARKER ! {BLD ;
: M: BLD} HERE DP-MARKER @ - >R SWAP-DP :
R> NAME$ $@ "^%s @ %d +" FORMAT&EVAL ;
: M; POSTPONE ; SWAP-DP {BLD ; IMMEDIATE
: endclass BLD} DP-MARKER @ HERE - ALLOT SWAP-DP
BLD$ $@ NAME$ $@ ": BUILD-%s HERE >R %s R> ;" FORMAT&EVAL
NAME$ $@ 2DUP 2DUP 2DUP
": %s CREATE BUILD-%s ^%s ! DOES> ^%s ! ;" FORMAT&EVAL ;
( CONDS CASE )
"ALIAS" WANTED
: CONDS 0 ; IMMEDIATE
: THENS BEGIN DUP WHILE POSTPONE THEN REPEAT
DROP ; IMMEDIATE
\ ISO : CASE
'CONDS ALIAS CASE
'ELSE ALIAS ENDOF
: OF POSTPONE OVER POSTPONE = POSTPONE IF
POSTPONE DROP ; IMMEDIATE
: ENDCASE POSTPONE DROP POSTPONE THENS ; IMMEDIATE
'IF ALIAS ?OF \ NOT ISO : ?OF .. ENDOF
( :NONAME MARKER ANEW ) \ C0apr25
"POSTFIX" WANTED
: :NONAME "NONAME" POSTFIX : LATEST DUP HIDDEN !CSP ; \ ISO
\ ISO
: MARKER HERE CREATE , DOES> @ 'FORGET-VOC FOR-VOCS DP ! ;
\ Destructive MARKER
: ANEW PP @ >R NAME FOUND DUP IF EXECUTE _ THEN DROP
R> PP ! MARKER ;
( BIN-SEARCH binary_search_by stack ) \ AvdH
( nmin nmax xt -- nres )
\ See description in next screen.
\
VARIABLE COMP \ Execution token of comparison word.
: BIN-SEARCH >R
BEGIN \ Loop variant IMAX - IMIN
2DUP <> WHILE
2DUP + 2/ ( -- ihalf )
DUP R@ EXECUTE IF
1+ SWAP ROT DROP \ Replace IMIN
ELSE
NIP \ Replace IMAX
THEN
REPEAT
DROP RDROP ;
( binary_search_description )
EXIT
( BIN-SEARCH : n IMIN, n IMAX, xt COMP -- n IRES )
Uses a comparison routine with execution token `COMP'
`COMP' must have the stack diagram ( IT -- flag) , where flag
typically means that IT compares lower or equal to some fixed
value. It may be TRUE , FALSE or undefined for `IMAX' , but
it must be monotonic down in the range [IMIN,IMAX), i.e.
if IMIN<=IX<=IY<IMAX then if IX COMP gives false, IY COMP
cannot give true.
BIN-SEARCH finds the first index `IT' between `IMIN'
and `IMAX' (exclusive) for which `COMP' returns false.
or else ``IMAX''.
An empty range is possible, (`IMIN' and `IMAX' are equal.)
See also binary_search_test in the examples section.
( MEMOIZE ) \ AvdHB6jan23
WANT BAG |BAG| BIN-SEARCH
1000 CONSTANT CSIZE
CSIZE BAG keys CSIZE BAG values
\ Reset and reallocate to N items, for zero, just reset.
: INIT-MEMOIZE DROP keys !BAG values !BAG ;
: key[] 1+ CELLS keys + ; \ For INDEX return ADDRESS
VARIABLE _compand