/
core.tex
6240 lines (4966 loc) · 194 KB
/
core.tex
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
% !TeX root = forth.tex
% !TeX spellcheck = en_US
% !TeX program = pdflatex
\chapter{Glossary} % 6
\label{chap:core}
\section{Core words} % 6.1
\wordlist{core}
\begin{worddef}{0010}{!}[store]
\item \stack{x a-addr}{}
Store \param{x} at \param{a-addr}.
\see \xref[3.3.3.1 Address alignment]{usage:aaddr}.
\begin{testing} % T.6.1.0010 !
See \tref{core:,}{,}.
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}[num]{0030}{\num}[number-sign]
\item \stack{ud_1}{ud_2}
Divide \param{ud_1} by the number in \word{BASE} giving the
quotient \param{ud_2} and the remainder \param{n}. (\param{n} is
the least significant digit of \param{ud_1}.) Convert \param{n}
to external form and add the resulting character to the beginning
of the pictured numeric output string. An ambiguous condition
exists if \word{num} executes outside of a
\word{num-start} \word{num-end} delimited number conversion.
\see \wref{core:num-end}{\num>},
\wref{core:numS}{\num{}S},
\wref{core:num-start}{<\num}.
\begin{testing} % T.6.1.0030 #
\texttt{\word{:} GP3 \word{num-start} 1 0 \word{num} \word{num} \word{num-end} \word{Sq} 01" S= \word{;}} \\
\test{GP3}{<TRUE>}
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}[num-end]{0040}{\num>}[number-sign-greater]
\item \stack{xd}{c-addr u}
Drop \param{xd}. Make the pictured numeric output string
available as a character string. \param{c-addr} and \param{u}
specify the resulting character string. A program may replace
characters within the string.
\see \wref{core:num}{\num},
\wref{core:numS}{\num{}S},
\wref{core:num-start}{<\num}.
\begin{testing} % T.6.1.0040 #>
See \tref{core:num}{\num},
\tref{core:numS}{\num{}S},
\tref{core:HOLD}{HOLD} and
\tref{core:SIGN}{SIGN}.
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}[numS]{0050}{\num{}S}[number-sign-s]
\item \stack{ud_1}{ud_2}
Convert one digit of \param{ud_1} according to the rule for
\word{num}. Continue conversion until the quotient is zero.
\param{ud_2} is zero. An ambiguous condition exists if
\word{numS} executes outside of a \word{num-start} \word{num-end}
delimited number conversion.
\see \wref{core:num}{\num},
\wref{core:num-end}{\num>},
\wref{core:num-start}{<\num}.
\begin{testing} % T.6.1.0050 #S
\ttfamily
\texttt{\word{:} GP4 \word{num-start} 1 0 \word{numS} \word{num-end} \word{Sq} 1" S= \word{;}} \\
\test{GP4}{<TRUE>}
\word{:} GP5 \\
\tab \word{BASE} \word{@} <TRUE> \\
\tab MAX-BASE \word{1+} 2 \word{DO} \tab[2] \word{bs} FOR EACH POSSIBLE BASE \\
\tab[2] \word{I} \word{BASE} \word{!} \tab[5.8] \word{bs} TBD: ASSUMES BASE WORKS \\
\tab[3] \word{I} 0 \word{num-start} \word{numS} \word{num-end} \word{Sq} 10" S= \word{AND} \\
\tab \word{LOOP} \\
\tab \word{SWAP} \word{BASE} \word{!} \word{;} \\
\test{GP5}{<TRUE>}
\word{:} GP6 \\
\tab \word{BASE} \word{@} \word{toR} 2 \word{BASE} \word{!} \\
\tab MAX-UINT MAX-UINT \word{num-start} \word{numS} \word{num-end} \tab \word{bs} MAXIMUM UD TO BINARY \\
\tab \word{Rfrom} \word{BASE} \word{!} \tab[10.6] \word{bs} S: C-ADDR U \\
\tab \word{DUP} \#BITS-UD \word{=} \word{SWAP} \\
\tab 0 \word{DO} \tab[13.6] \word{bs} S: C-ADDR FLAG \\
\tab[2] \word{OVER} \word{C@} \word{[CHAR]} 1 \word{=} \word{AND} \tab[1.2] \word{bs} ALL ONES \\
\tab[2] \word{toR} \word{CHAR+} \word{Rfrom} \\
\tab \word{LOOP} \word{SWAP} \word{DROP} \word{;} \\
\test{GP6}{<TRUE>}
\word{:} GP7 \\
\tab \word{BASE} \word{@} \word{toR} MAX-BASE \word{BASE} \word{!} \\
\tab <TRUE> \\
\tab A 0 \word{DO} \\
\tab[2] \word{I} 0 \word{num-start} \word{numS} \word{num-end} \\
\tab[2] 1 \word{=} \word{SWAP} \word{C@} \word{I} 30 \word{+} \word{=} \word{AND} \word{AND} \\
\tab \word{LOOP} \\
\tab MAX-BASE A \word{DO} \\
\tab[2] \word{I} 0 \word{num-start} \word{numS} \word{num-end} \\
\tab[2] 1 \word{=} \word{SWAP} \word{C@} 41 \word{I} A \word{-} \word{+} \word{=} \word{AND} \word{AND} \\
\tab \word{LOOP} \\
\tab \word{Rfrom} \word{BASE} \word{!} \word{;} \\
\test{GP7}{<TRUE>}
\end{testing}
\end{worddef}
\vspace*{-2ex}
\enlargethispage{6ex}
\begin{worddef}{0070}{'}[tick]
\item \stack{"<spaces>name"}{xt}
Skip leading space delimiters. Parse \param{name} delimited by
a space. Find \param{name} and return \param{xt}, the execution
token for \param{name}. An ambiguous condition exists if
\param{name} is not found. When interpreting,
\texttt{' xyz EXECUTE} is equivalent to \texttt{xyz}.
\see \xref[3.4 The Forth text interpreter]{usage:interpret},
\xref[3.4.1 Parsing]{usage:parsing},
\rref{core:'}{},
\rref{core:POSTPONE}{POSTPONE},
\rref{core:[']}{[']}.
\begin{rationale} % A.6.1.0070 '
Typical use: {\ldots} \word{'} \param{name}.
Many Forth systems use a state-smart tick. Many do not.
Forth-\snapshot\ follows the usage of Forth 94.
\see \xref[A.3.4.3..2 Interpretation semantics]{rat:interpret},
\rref{core:FIND}{FIND}.
\end{rationale}
\begin{testing} % T.6.1.0070 '
\test{\word{:} GT1 123 \word{;} }{ } \\
\test{\word{'} GT1 \word{EXECUTE}}{123}
\end{testing}
\end{worddef}
\begin{worddef}[p]{0080}{(}[paren]
\compile
Perform the execution semantics given below.
\execute
\stack{"ccc<paren>"}{}
Parse \param{ccc} delimited by \texttt{)}
(right parenthesis).
\word{p} is an immediate word.
The number of characters in \param{ccc} may be zero to the
number of characters in the parse area.
\see \xref[3.4.1 Parsing]{usage:parsing},
\xref[11.6.1.0080 (]{file:p},
\rref{core:p}{}.
\begin{rationale} % A.6.1.0080 (
Typical use: {\ldots} \word{p} \param{ccc}\texttt{)} {\ldots}
\end{rationale}
\begin{testing} % T.6.1.0080 (
\word{bs} \textdf{There is no space either side of the ).}\\[\parskip]
\test{\word{p} A comment)1234}{} \\
\test{\word{:} pc1 \word{p} A comment)1234 \word{;} pc1}{1234}
\end{testing}
\end{worddef}
\vspace*{-1.5ex}
\begin{worddef}{0090}{*}[star]
\item \stack{n_1|u_1 n_2|u_2}{n_3|u_3}
Multiply \param{n_1|u_1} by \param{n_2|u_2} giving the product
\param{n_3|u_3}.
\begin{testing} % T.6.1.0090 *
\test{ 0 0 \word{*}}{ 0} \tab[4] \word{bs} TEST IDENTITIE{\bs}S \\
\test{ 0 1 \word{*}}{ 0} \\
\test{ 1 0 \word{*}}{ 0} \\
\test{ 1 2 \word{*}}{ 2} \\
\test{ 2 1 \word{*}}{ 2} \\
\test{ 3 3 \word{*}}{ 9} \\
\test{-3 3 \word{*}}{-9} \\
\test{ 3 -3 \word{*}}{-9} \\
\test{-3 -3 \word{*}}{ 9}
\test{MID-UINT+1 1 \word{RSHIFT} 2 \word{*} }{MID-UINT+1} \\
\test{MID-UINT+1 2 \word{RSHIFT} 4 \word{*} }{MID-UINT+1} \\
\test{MID-UINT+1 1 \word{RSHIFT} MID-UINT+1 \word{OR} 2 \word{*}}{MID-UINT+1} \\
\end{testing}
\end{worddef}
\vspace*{-1.5ex}
\begin{worddef}{0100}{*/}[star-slash]
\item \stack{n_1 n_2 n_3}{n_4}
Multiply \param{n_1} by \param{n_2} producing the intermediate
double-cell result $d$. Divide $d$ by \param{n_3} giving the
single-cell quotient \param{n_4}. An ambiguous condition exists
if \param{n_3} is zero or if the quotient \param{n_4} lies
outside the range of a signed number. If $d$ and \param{n_3}
differ in sign, the implementation-defined result returned will
be the same as that returned by either the phrase
\word{toR} \word{M*} \word{Rfrom} \word{FM/MOD} \word{SWAP} \word{DROP}
or the phrase
\word{toR} \word{M*} \word{Rfrom} \word{SM/REM} \word{SWAP} \word{DROP}.
\see \xref[3.2.2.1 Integer division]{usage:div}.
\begin{testing} % T.6.1.0100 */
\ttfamily
IFFLOORED \tab \word{:} T*/ T*/MOD \word{SWAP} \word{DROP} \word{;} \\
IFSYM \tab[2.8] \word{:} T*/ T*/MOD \word{SWAP} \word{DROP} \word{;}
\test{ 0 2 1 \word{*/}}{ 0 2 1 T*/} \\
\test{ 1 2 1 \word{*/}}{ 1 2 1 T*/} \\
\test{ 2 2 1 \word{*/}}{ 2 2 1 T*/} \\
\test{ -1 2 1 \word{*/}}{ -1 2 1 T*/} \\
\test{ -2 2 1 \word{*/}}{ -2 2 1 T*/} \\
\test{ 0 2 -1 \word{*/}}{ 0 2 -1 T*/} \\
\test{ 1 2 -1 \word{*/}}{ 1 2 -1 T*/} \\
\test{ 2 2 -1 \word{*/}}{ 2 2 -1 T*/} \\
\test{ -1 2 -1 \word{*/}}{ -1 2 -1 T*/} \\
\test{ -2 2 -1 \word{*/}}{ -2 2 -1 T*/} \\
\test{ 2 2 2 \word{*/}}{ 2 2 2 T*/} \\
\test{ -1 2 -1 \word{*/}}{ -1 2 -1 T*/} \\
\test{ -2 2 -2 \word{*/}}{ -2 2 -2 T*/} \\
\test{ 7 2 3 \word{*/}}{ 7 2 3 T*/} \\
\test{ 7 2 -3 \word{*/}}{ 7 2 -3 T*/} \\
\test{ -7 2 3 \word{*/}}{ -7 2 3 T*/} \\
\test{ -7 2 -3 \word{*/}}{ -7 2 -3 T*/} \\
\test{MAX-INT 2 MAX-INT \word{*/}}{MAX-INT 2 MAX-INT T*/} \\
\test{MIN-INT 2 MIN-INT \word{*/}}{MIN-INT 2 MIN-INT T*/}
\end{testing}
\end{worddef}
\vspace*{-1.5ex}
\begin{worddef}{0110}{*/MOD}[star-slash-mod]
\item \stack{n_1 n_2 n_3}{n_4 n_5}
Multiply \param{n_1} by \param{n_2} producing the intermediate
double-cell result $d$. Divide $d$ by \param{n_3} producing the
single-cell remainder \param{n_4} and the single-cell quotient
\param{n_5}. An ambiguous condition exists if \param{n_3} is
zero, or if the quotient \param{n_5} lies outside the range of a
single-cell signed integer. If $d$ and \param{n_3} differ in
sign, the implementation-defined result returned will be the
same as that returned by either the phrase
\word{toR} \word{M*} \word{Rfrom} \word{FM/MOD} or the phrase
\word{toR} \word{M*} \word{Rfrom} \word{SM/REM}.
\see \xref[3.2.2.1 Integer division]{usage:div}.
\begin{testing} % T.6.1.0110 */MOD
\ttfamily
IFFLOORED \tab \word{:} T*/MOD \word{toR} \word{M*} \word{Rfrom} \word{FM/MOD} \word{;} \\
IFSYM \tab[2.8] \word{:} T*/MOD \word{toR} \word{M*} \word{Rfrom} \word{SM/REM} \word{;}
\test{ 0 2 1 \word{*/MOD}}{ 0 2 1 T*/MOD} \\
\test{ 1 2 1 \word{*/MOD}}{ 1 2 1 T*/MOD} \\
\test{ 2 2 1 \word{*/MOD}}{ 2 2 1 T*/MOD} \\
\test{ -1 2 1 \word{*/MOD}}{ -1 2 1 T*/MOD} \\
\test{ -2 2 1 \word{*/MOD}}{ -2 2 1 T*/MOD} \\
\test{ 0 2 -1 \word{*/MOD}}{ 0 2 -1 T*/MOD} \\
\test{ 1 2 -1 \word{*/MOD}}{ 1 2 -1 T*/MOD} \\
\test{ 2 2 -1 \word{*/MOD}}{ 2 2 -1 T*/MOD} \\
\test{ -1 2 -1 \word{*/MOD}}{ -1 2 -1 T*/MOD} \\
\test{ -2 2 -1 \word{*/MOD}}{ -2 2 -1 T*/MOD} \\
\test{ 2 2 2 \word{*/MOD}}{ 2 2 2 T*/MOD} \\
\test{ -1 2 -1 \word{*/MOD}}{ -1 2 -1 T*/MOD} \\
\test{ -2 2 -2 \word{*/MOD}}{ -2 2 -2 T*/MOD} \\
\test{ 7 2 3 \word{*/MOD}}{ 7 2 3 T*/MOD} \\
\test{ 7 2 -3 \word{*/MOD}}{ 7 2 -3 T*/MOD} \\
\test{ -7 2 3 \word{*/MOD}}{ -7 2 3 T*/MOD} \\
\test{ -7 2 -3 \word{*/MOD}}{ -7 2 -3 T*/MOD} \\
\test{MAX-INT 2 MAX-INT \word{*/MOD}}{MAX-INT 2 MAX-INT T*/MOD} \\
\test{MIN-INT 2 MIN-INT \word{*/MOD}}{MIN-INT 2 MIN-INT T*/MOD}
\end{testing}
\end{worddef}
\vspace*{-1.5ex}
\begin{worddef}{0120}{+}[plus]
\item \stack{n_1|u_1 n_2|u_2}{n_3|u_3}
Add \param{n_2|u_2} to \param{n_1|u_1}, giving the sum
\param{n_3|u_3}.
\see \xref[3.3.3.1 Address alignment]{usage:aaddr}.
\begin{testing} % T.6.1.0120 +
\test{ 0 5 \word{+}}{ 5} \\
\test{ 5 0 \word{+}}{ 5} \\
\test{ 0 -5 \word{+}}{ -5} \\
\test{ -5 0 \word{+}}{ -5} \\
\test{ 1 2 \word{+}}{ 3} \\
\test{ 1 -2 \word{+}}{ -1} \\
\test{ -1 2 \word{+}}{ 1} \\
\test{ -1 -2 \word{+}}{ -3} \\
\test{ -1 1 \word{+}}{ 0} \\
\test{MID-UINT 1 \word{+}}{MID-UINT+1}
\pagebreak
\end{testing}
\end{worddef}
\begin{worddef}{0130}{+!}[plus-store]
\item \stack{n|u a-addr}{}
Add \param{n|u} to the single-cell number at \param{a-addr}.
\see \xref[3.3.3.1 Address alignment]{usage:aaddr}.
\begin{testing} % T.6.1.0130 +!
\test{ 0 1ST \word{!} }{ } \\
\test{ 1 1ST \word{+!} }{ } \\
\test{ 1ST \word{@} }{1} \\
\test{-1 1ST \word{+!} 1ST \word{@}}{0}
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}{0140}{+LOOP}[plus-loop]
\interpret
Interpretation semantics for this word are undefined.
\compile
\stack[C]{do-sys}{}
Append the run-time semantics given below to the current
definition. Resolve the destination of all unresolved
occurrences of \word{LEAVE} between the location given
by \param{do-sys} and the next location for a transfer of
control, to execute the words following \word{+LOOP}.
\runtime
\stack{n}{}
\stack[R]{loop-sys_1}{|loop-sys_2}
An ambiguous condition exists if the loop control parameters
are unavailable. Add \param{n} to the loop index. If the loop
index did not cross the boundary between the loop limit minus
one and the loop limit, continue execution at the beginning
of the loop. Otherwise, discard the current loop control
parameters and continue execution immediately following the
loop.
\see \wref{core:DO}{DO},
\wref{core:I}{I},
\wref{core:LEAVE}{LEAVE},
\rref{core:+LOOP}{}.
\begin{rationale} % A.6.1.0140 +LOOP
Typical use:
\word{:} \texttt{X} ~{\ldots} limit first \word{DO}
{\ldots} step \word{+LOOP}
\word{;}
\end{rationale}
\begin{testing} % T.6.1.0140 +LOOP
\ttfamily
\test{\word{:} GD2 \word{DO} \word{I} -1 \word{+LOOP} \word{;}}{} \\
\test{ 1 4 GD2}{4 3 2 1} \\
\test{ -1 2 GD2}{2 1 0 -1} \\
\test{MID-UINT MID-UINT+1 GD2}{MID-UINT+1 MID-UINT}
\word{VARIABLE} gditerations \\
\word{VARIABLE} gdincrement
\word{:} gd7 \word{p} limit start increment -{}- ) \\
\tab gdincrement \word{!} \\
\tab 0 gditerations \word{!} \\
\tab \word{DO} \\
\tab[2] 1 gditerations \word{+!} \\
\tab[2] \word{I} \\
\tab[2] gditerations \word{@} 6 \word{=} \word{IF} \word{LEAVE} \word{THEN} \\
\tab[2] gdincrement \word{@} \\
\tab \word{+LOOP} gditerations \word{@} \\
\word{;}
\test{ 4 4 -1 gd7}{ 4 1 } \\
\test{ 1 4 -1 gd7}{ 4 3 2 1 4 } \\
\test{ 4 1 -1 gd7}{ 1 0 -1 -2 -3 -4 6 } \\
\test{ 4 1 0 gd7}{ 1 1 1 1 1 1 6 } \\
\test{ 0 0 0 gd7}{ 0 0 0 0 0 0 6 } \\
\test{ 1 4 0 gd7}{ 4 4 4 4 4 4 6 } \\
\test{ 1 4 1 gd7}{ 4 5 6 7 8 9 6 } \\
\test{ 4 1 1 gd7}{ 1 2 3 3 } \\
\test{ 4 4 1 gd7}{ 4 5 6 7 8 9 6 } \\
\test{ 2 -1 -1 gd7}{-1 -2 -3 -4 -5 -6 6 } \\
\test{ -1 2 -1 gd7}{ 2 1 0 -1 4 } \\
\test{ 2 -1 0 gd7}{-1 -1 -1 -1 -1 -1 6 } \\
\test{ -1 2 0 gd7}{ 2 2 2 2 2 2 6 } \\
\test{ -1 2 1 gd7}{ 2 3 4 5 6 7 6 } \\
\test{ 2 -1 1 gd7}{-1 0 1 3 } \\
\test{ -20 30 -10 gd7}{30 20 10 0 -10 -20 6 } \\
\test{ -20 31 -10 gd7}{31 21 11 1 -9 -19 6 } \\
\test{ -20 29 -10 gd7}{29 19 9 -1 -11 5 }
\word{bs} \textdf{With large and small increments}
MAX-UINT 8 RSHIFT 1+ CONSTANT ustep \\
ustep NEGATE CONSTANT -ustep \\
MAX-INT 7 RSHIFT 1+ CONSTANT step \\
step NEGATE CONSTANT -step
\word{VARIABLE} bump
\test{ \word{:} gd8 bump \word{!} \word{DO} \word{1+} bump \word{@} \word{+LOOP} \word{;}}{}
\test{ 0 MAX-UINT 0 ustep gd8}{256} \\
\test{ 0 0 MAX-UINT -ustep gd8}{256} \\
\test{ 0 MAX-INT MIN-INT step gd8}{256} \\
\test{ 0 MIN-INT MAX-INT -step gd8}{256}
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}{0150}{,}[comma]
\item \stack{x}{}
Reserve one cell of data space and store \param{x} in the cell.
If the data-space pointer is aligned when \word{,} begins
execution, it will remain aligned when \word{,} finishes
execution. An ambiguous condition exists if the data-space
pointer is not aligned prior to execution of \word{,}.
\see \xref[3.3.3 Data space]{usage:dataspace},
\xref[3.3.3.1 Address alignment]{usage:aaddr},
\rref{core:,}{}.
\begin{rationale} % A.6.1.0150 ,
The use of \word{,} (comma) for compiling execution tokens is
not portable.
See: \wref{core:COMPILE,}{COMPILE,}.
\end{rationale}
\begin{testing} % T.6.1.0150 ,
\ttfamily
\word{HERE} 1 \word{,} \\
\word{HERE} 2 \word{,} \\
\word{CONSTANT} 2ND \\
\word{CONSTANT} 1ST
\test{ 1ST 2ND \word{Uless}}{<TRUE>} \word{bs} HERE MUST GROW WITH ALLOT \\
\test{ 1ST \word{CELL+} }{2ND} \word{bs} {\ldots} BY ONE CELL \\
\test{ 1ST 1 \word{CELLS} \word{+} }{2ND} \\
\test{ 1ST \word{@} 2ND \word{@} }{1 2} \\
\test{ 5 1ST \word{!} }{ } \\
\test{ 1ST \word{@} 2ND \word{@} }{5 2} \\
\test{ 6 2ND \word{!} }{ } \\
\test{ 1ST \word{@} 2ND \word{@} }{5 6} \\
\test{ 1ST \word{2@}}{6 5} \\
\test{ 2 1 1ST \word{2!}}{ } \\
\test{ 1ST \word{2@}}{2 1} \\
\test{1S 1ST \word{!} 1ST \word{@} }{1S } \tab \word{bs} CAN STORE CELL-WIDE VALUE
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}{0160}{-}[minus]
\item \stack{n_1|u_1 n_2|u_2}{n_3|u_3}
Subtract \param{n_2|u_2} from \param{n_1|u_1}, giving the
difference \param{n_3|u_3}.
\see \xref[3.3.3.1 Address alignment]{usage:aaddr}.
\begin{testing} % T.6.1.0160 -
\test{ 0 5 \word{-}}{ -5} \\
\test{ 5 0 \word{-}}{ 5} \\
\test{ 0 -5 \word{-}}{ 5} \\
\test{ -5 0 \word{-}}{ -5} \\
\test{ 1 2 \word{-}}{ -1} \\
\test{ 1 -2 \word{-}}{ 3} \\
\test{ -1 2 \word{-}}{ -3} \\
\test{ -1 -2 \word{-}}{ 1} \\
\test{ 0 1 \word{-}}{ -1} \\
\test{MID-UINT+1 1 \word{-}}{MID-UINT}
\end{testing}
\end{worddef}
\vspace*{-2ex}
\enlargethispage{4ex}
\begin{worddef}[d]{0180}{.{}}[dot]
\item \stack{n}{}
Display \param{n} in free field format.
\see \xref[3.2.1.2 Digit conversion]{usage:digits},
\xref[3.2.1.3 Free-field number display]{usage:dot}.
\begin{testing} % T.6.1.0180 .
See \tref{core:EMIT}{EMIT}.
\end{testing}
\end{worddef}
\begin{worddef}[.q]{0190}{."}[dot-quote]
\interpret
Interpretation semantics for this word are undefined.
\compile
\stack{"ccc<quote>"}{}
Parse \param{ccc} delimited by \texttt{"} (double-quote).
Append the run-time semantics given below to the current
definition.
\runtime
\stack{}{}
Display \param{ccc}.
\see \xref[3.4.1 Parsing]{usage:parsing},
\wref{core:.p}{.(},
\rref{core:.q}{}.
\begin{rationale} % A.6.1.0190 ."
Typical use:
\word{:} \texttt{X} {\ldots}
\word{.q} \emph{ccc}\texttt{"} {\ldots}
\word{;}
An implementation may define interpretation semantics for
\word{.q} if desired. In one plausible implementation,
interpreting \word{.q} would display the delimited message.
In another plausible implementation, interpreting \word{.q}
would compile code to display the message later. In still
another plausible implementation, interpreting \word{.q} would
be treated as an exception. Given this variation a Standard
Program may not use \word{.q} while interpreting. Similarly,
a Standard Program may not compile \word{POSTPONE} \word{.q}
inside a new word, and then use that word while interpreting.
\end{rationale}
\begin{testing} % T.6.1.0190 ."
\test{\word{:} pb1 \word{CR} \word{.q} You should see 2345: "\word{.q} 2345"\word{;} pb1}{}
See \tref{core:EMIT}{EMIT}.
\end{testing}
\end{worddef}
\enlargethispage{4ex}
\vspace*{-2ex}
\begin{worddef}{0230}{/}[slash]
\item \stack{n_1 n_2}{n_3}
Divide \param{n_1} by \param{n_2}, giving the single-cell quotient
\param{n_3}. An ambiguous condition exists if \param{n_2} is zero.
If \param{n_1} and \param{n_2} differ in sign, the
implementation-defined result returned will be the same as that
returned by either the phrase
\word{toR} \word{StoD} \word{Rfrom} \word{FM/MOD} \word{SWAP} \word{DROP}
or the phrase
\word{toR} \word{StoD} \word{Rfrom} \word{SM/REM} \word{SWAP} \word{DROP}.
\see \xref[3.2.2.1 Integer division]{usage:div}.
\begin{testing} % T.6.1.0230 /
\ttfamily
IFFLOORED \tab \word{:} T/ T/MOD \word{SWAP} \word{DROP} \word{;} \\
IFSYM \tab[2.8] \word{:} T/ T/MOD \word{SWAP} \word{DROP} \word{;}
\test{ 0 1 \word{/}}{ 0 1 T/} \\
\test{ 1 1 \word{/}}{ 1 1 T/} \\
\test{ 2 1 \word{/}}{ 2 1 T/} \\
\test{ -1 1 \word{/}}{ -1 1 T/} \\
\test{ -2 1 \word{/}}{ -2 1 T/} \\
\test{ 0 -1 \word{/}}{ 0 -1 T/} \\
\test{ 1 -1 \word{/}}{ 1 -1 T/} \\
\test{ 2 -1 \word{/}}{ 2 -1 T/} \\
\test{ -1 -1 \word{/}}{ -1 -1 T/} \\
\test{ -2 -1 \word{/}}{ -2 -1 T/} \\
\test{ 2 2 \word{/}}{ 2 2 T/} \\
\test{ -1 -1 \word{/}}{ -1 -1 T/} \\
\test{ -2 -2 \word{/}}{ -2 -2 T/} \\
\test{ 7 3 \word{/}}{ 7 3 T/} \\
\test{ 7 -3 \word{/}}{ 7 -3 T/} \\
\test{ -7 3 \word{/}}{ -7 3 T/} \\
\test{ -7 -3 \word{/}}{ -7 -3 T/} \\
\test{MAX-INT 1 \word{/}}{MAX-INT 1 T/} \\
\test{MIN-INT 1 \word{/}}{MIN-INT 1 T/} \\
\test{MAX-INT MAX-INT \word{/}}{MAX-INT MAX-INT T/} \\
\test{MIN-INT MIN-INT \word{/}}{MIN-INT MIN-INT T/}
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}{0240}{/MOD}[slash-mod]
\item \stack{n_1 n_2}{n_3 n_4}
Divide \param{n_1} by \param{n_2}, giving the single-cell remainder
\param{n_3} and the single-cell quotient \param{n_4}. An ambiguous
condition exists if \param{n_2} is zero. If \param{n_1} and
\param{n_2} differ in sign, the implementation-defined result
returned will be the same as that returned by either the phrase
\word{toR} \word{StoD} \word{Rfrom} \word{FM/MOD}
or the phrase
\word{toR} \word{StoD} \word{Rfrom} \word{SM/REM}.
\see \xref[3.2.2.1 Integer division]{usage:div}.
\begin{testing} % T.6.1.0240 /MOD
\ttfamily
IFFLOORED \tab \word{:} T/MOD \word{toR} \word{StoD} \word{Rfrom} \word{FM/MOD} \word{;} \\
IFSYM \tab[2.8] \word{:} T/MOD \word{toR} \word{StoD} \word{Rfrom} \word{SM/REM} \word{;}
\test{ 0 1 \word{/MOD}}{ 0 1 T/MOD} \\
\test{ 1 1 \word{/MOD}}{ 1 1 T/MOD} \\
\test{ 2 1 \word{/MOD}}{ 2 1 T/MOD} \\
\test{ -1 1 \word{/MOD}}{ -1 1 T/MOD} \\
\test{ -2 1 \word{/MOD}}{ -2 1 T/MOD} \\
\test{ 0 -1 \word{/MOD}}{ 0 -1 T/MOD} \\
\test{ 1 -1 \word{/MOD}}{ 1 -1 T/MOD} \\
\test{ 2 -1 \word{/MOD}}{ 2 -1 T/MOD} \\
\test{ -1 -1 \word{/MOD}}{ -1 -1 T/MOD} \\
\test{ -2 -1 \word{/MOD}}{ -2 -1 T/MOD} \\
\test{ 2 2 \word{/MOD}}{ 2 2 T/MOD} \\
\test{ -1 -1 \word{/MOD}}{ -1 -1 T/MOD} \\
\test{ -2 -2 \word{/MOD}}{ -2 -2 T/MOD} \\
\test{ 7 3 \word{/MOD}}{ 7 3 T/MOD} \\
\test{ 7 -3 \word{/MOD}}{ 7 -3 T/MOD} \\
\test{ -7 3 \word{/MOD}}{ -7 3 T/MOD} \\
\test{ -7 -3 \word{/MOD}}{ -7 -3 T/MOD} \\
\test{MAX-INT 1 \word{/MOD}}{MAX-INT 1 T/MOD} \\
\test{MIN-INT 1 \word{/MOD}}{MIN-INT 1 T/MOD} \\
\test{MAX-INT MAX-INT \word{/MOD}}{MAX-INT MAX-INT T/MOD} \\
\test{MIN-INT MIN-INT \word{/MOD}}{MIN-INT MIN-INT T/MOD}
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}[0less]{0250}{0<}[zero-less]
\item \stack{n}{flag}
\param{flag} is true if and only if \param{n} is less than zero.
\begin{testing} % T.6.1.0250 0<
\test{ 0 \word{0less}}{<FALSE>} \\
\test{ -1 \word{0less}}{<TRUE> } \\
\test{MIN-INT \word{0less}}{<TRUE> } \\
\test{ 1 \word{0less}}{<FALSE>} \\
\test{MAX-INT \word{0less}}{<FALSE>}
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}{0270}{0=}[zero-equals]
\item \stack{x}{flag}
\param{flag} is true if and only if \param{x} is equal to zero.
\begin{testing} % T.6.1.0270 0=
\test{ 0 \word{0=}}{<TRUE> } \\
\test{ 1 \word{0=}}{<FALSE>} \\
\test{ 2 \word{0=}}{<FALSE>} \\
\test{ -1 \word{0=}}{<FALSE>} \\
\test{MAX-UINT \word{0=}}{<FALSE>} \\
\test{MIN-INT \word{0=}}{<FALSE>} \\
\test{MAX-INT \word{0=}}{<FALSE>}
\end{testing}
\end{worddef}
\vspace*{-2ex}
\begin{worddef}{0290}{1+}[one-plus]
\item \stack{n_1|u_1}{n_2|u_2}
Add one (1) to \param{n_1|u_1} giving the sum
\param{n_2|u_2}.
\begin{testing} % T.6.1.0290 1+
\test{ 0 \word{1+}}{ 1} \\
\test{ -1 \word{1+}}{ 0} \\
\test{ 1 \word{1+}}{ 2} \\
\test{MID-UINT \word{1+}}{MID-UINT+1} \\
\test{MAX-INT \word{1+}}{ MIN-INT}
\end{testing}
\end{worddef}
\begin{worddef}{0300}{1-}[one-minus]
\item \stack{n_1|u_1}{n_2|u_2}
Subtract one (1) from \param{n_1|u_1} giving the difference
\param{n_2|u_2}.
\begin{testing} % T.6.1.0300 1-
\test{ 2 \word{1-}}{ 1} \\
\test{ 1 \word{1-}}{ 0} \\
\test{ 0 \word{1-}}{ -1} \\
\test{MID-UINT+1 \word{1-}}{MID-UINT}
\end{testing}
\end{worddef}
\begin{worddef}{0310}{2!}[two-store]
\item \stack{x_1 x_2 a-addr}{}
Store the cell pair \param{x_1 x_2} at \param{a-addr}, with
\param{x_2} at \param{a-addr} and \param{x_1} at the next
consecutive cell. It is equivalent to the sequence
\word{SWAP} \word{OVER} \word{!} \word{CELL+} \word{!}.
\see \xref[3.3.3.1 Address alignment]{usage:aaddr}.
\begin{testing} % T.6.1.0310 2!
See \tref{core:,}{,}.
\end{testing}
\end{worddef}
\begin{worddef}{0320}{2*}[two-star]
\item \stack{x_1}{x_2}
\param{x_2} is the result of shifting \param{x_1} one bit toward
the most-significant bit, filling the vacated least-significant
bit with zero.
\begin{testing} % T.6.1.0320 2*
\test{ 0S \word{2*} }{ 0S} \\
\test{ 1 \word{2*} }{ 2} \\
\test{4000 \word{2*} }{8000} \\
\test{ 1S \word{2*} 1 \word{XOR}}{ 1S} \\
\test{ MSB \word{2*} }{ 0S}
\end{testing}
\end{worddef}
\begin{worddef}{0330}{2/}[two-slash]
\item \stack{x_1}{x_2}
\param{x_2} is the result of shifting \param{x_1} one bit toward
the least-significant bit, leaving the most-significant bit
unchanged.
\begin{testing} % T.6.1.0330 2/
\test{ 0S \word{2/}}{ 0S} \\
\test{ 1 \word{2/}}{ 0} \\
\test{ 4000 \word{2/}}{2000} \\
\test{ 1S \word{2/}}{ 1S} \word{bs} MSB PROPOGATED \\
\test{ 1S 1 \word{XOR} \word{2/}}{ 1S} \\
\test{MSB \word{2/} MSB \word{AND}}{ MSB}
\end{testing}
\end{worddef}
\begin{worddef}{0350}{2@}[two-fetch]
\item \stack{a-addr}{x_1 x_2}
Fetch the cell pair \param{x_1 x_2} stored at \param{a-addr}.
\param{x_2} is stored at \param{a-addr} and \param{x_1} at the
next consecutive cell. It is equivalent to the sequence
\word{DUP} \word{CELL+} \word{@} \word{SWAP} \word{@}.
\see \xref[3.3.3.1 Address alignment]{usage:aaddr},
\wref{core:2!}{2!}.
\begin{testing} % T.6.1.0350 2@
See \tref{core:,}{,}.
\end{testing}
\end{worddef}
\begin{worddef}{0370}{2DROP}[two-drop]
\item \stack{x_1 x_2}{}
Drop cell pair \param{x_1 x_2} from the stack.
\begin{testing} % T.6.1.0370 2DROP
\test{1 2 \word{2DROP}}{}
\end{testing}
\end{worddef}
\begin{worddef}{0380}{2DUP}[two-dupe]
\item \stack{x_1 x_2}{x_1 x_2 x_1 x_2}
Duplicate cell pair \param{x_1 x_2}.
\begin{testing} % T.6.1.0380 2DUP
\test{1 2 \word{2DUP}}{1 2 1 2}
\end{testing}
\end{worddef}
\begin{worddef}{0400}{2OVER}[two-over]
\item \stack{x_1 x_2 x_3 x_4}{x_1 x_2 x_3 x_4 x_1 x_2}
Copy cell pair \param{x_1 x_2} to the top of the stack.
\begin{testing} % T.6.1.0400 2OVER
\test{1 2 3 4 \word{2OVER}}{1 2 3 4 1 2}
\end{testing}
\end{worddef}
\begin{worddef}{0430}{2SWAP}[two-swap]
\item \stack{x_1 x_2 x_3 x_4}{x_3 x_4 x_1 x_2}
Exchange the top two cell pairs.
\begin{testing} % T.6.1.0430 2SWAP
\test{1 2 3 4 \word{2SWAP}}{3 4 1 2}
\end{testing}
\end{worddef}
\begin{worddef}{0450}{:}[colon]
\item \stack[C]{"<spaces>name"}{colon-sys}
Skip leading space delimiters. Parse \param{name} delimited by a
space. Create a definition for \param{name}, called a ``colon
definition''. Enter compilation state and start the current
definition, producing \param{colon-sys}. Append the initiation
semantics given below to the current definition.
The execution semantics of \param{name} will be determined by the
words compiled into the body of the definition. The current
definition shall not be findable in the dictionary until it is
ended (or until the execution of \word{DOES} in some systems).
\init \stack{i*x}{i*x}
\stack[R]{}{nest-sys}
Save implementation-dependent information \param{nest-sys}
about the calling definition. The stack effects \param{i*x}
represent arguments to \param{name}.
\execute[name]
\stack{i*x}{j*x}
Execute the definition \param{name}. The stack effects
\param{i*x} and \param{j*x} represent arguments to and
results from \param{name}, respectively.
\see \xref[3.4 The Forth text interpreter]{usage:interpret},
\xref[3.4.1 Parsing]{usage:parsing},
\xref[3.4.5 Compilation]{usage:compilation},
\wref{core:DOES}{DOES>},
\wref{core:[}{[},
\wref{core:]}{]},
\wref{tools:;CODE}{;CODE},
\rref{core::}{}.
\begin{rationale} % A.6.1.0450 :
Typical use:
\word{:} \emph{name} {\ldots} \word{;}
In Forth 83, this word was specified to alter the search order.
This specification is explicitly removed in this standard. We
believe that in most cases this has no effect; however, systems
that allow many search orders found the Forth-83 behavior of
colon very undesirable.
Note that colon does not itself invoke the compiler. Colon sets
compilation state so that later words in the parse area are
compiled.
\end{rationale}
\begin{testing} % T.6.1.0450 :
\test{\word{:} NOP \word{:} \word{POSTPONE} \word{;} \word{;}}{} \\
\test{NOP NOP1 NOP NOP2}{} \\
\test{NOP1}{} \\
\test{NOP2}{}
The following tests the dictionary search order:
\test{\word{:} GDX 123 \word{;} \tab \word{:} GDX GDX 234 \word{;}}{} \\
\test{GDX}{123 234}
\end{testing}
\end{worddef}
\pagebreak
\begin{worddef}{0460}{;}[semicolon]
\interpret
Interpretation semantics for this word are undefined.
\compile
\stack[C]{colon-sys}{}
Append the run-time semantics below to the current definition. End
the current definition, allow it to be found in the dictionary and
enter interpretation state, consuming \param{colon-sys}. If the
data-space pointer is not aligned, reserve enough data space to
align it.
An ambiguous condition exists if the compilation semantics of
\word{;} are preformed inside a quotation (\word[tools]{[:} {\ldots}
\word[tools]{;]} block).
\runtime
\stack{}{}
\stack[R]{nest-sys}{}
Return to the calling definition specified by \param{nest-sys}.
\see \xref[3.4 The Forth text interpreter]{usage:command},
\xref[3.4.5 Compilation]{usage:compilation},
\rref{core:;}{}.
\begin{rationale} % A.6.1.0460 ;
Typical use:
\word{:} \emph{name} {\ldots} \word{;}
One function performed by both \word{;} and \word[tools]{;CODE}
is to allow the current definition to be found in the
dictionary. If the current definition was created by
\word{:NONAME} the current definition has no definition name
and thus cannot be found in the dictionary. If \word{:NONAME}
is implemented the Forth compiler must maintain enough
information about the current definition to allow \word{;} and
\word[tools]{;CODE} to determine whether or not any action must
be taken to allow it to be found.
\end{rationale}
\begin{testing} % T.6.1.0460 ;
See \tref{core::}{:}.
\end{testing}
\end{worddef}
\begin{worddef}[less]{0480}{<}[less-than]
\item \stack{n_1 n_2}{flag}
\param{flag} is true if and only if \param{n_1} is less than
\param{n_2}.
\see \wref{core:Uless}{U<}.
\begin{testing} % T.6.1.0480 <
\test{ 0 1 \word{less}}{<TRUE> } \\
\test{ 1 2 \word{less}}{<TRUE> } \\
\test{ -1 0 \word{less}}{<TRUE> } \\
\test{ -1 1 \word{less}}{<TRUE> } \\
\test{MIN-INT 0 \word{less}}{<TRUE> } \\
\test{MIN-INT MAX-INT \word{less}}{<TRUE> } \\
\test{ 0 MAX-INT \word{less}}{<TRUE> } \\
\test{ 0 0 \word{less}}{<FALSE>} \\
\test{ 1 1 \word{less}}{<FALSE>} \\
\test{ 1 0 \word{less}}{<FALSE>} \\
\test{ 2 1 \word{less}}{<FALSE>} \\
\test{ 0 -1 \word{less}}{<FALSE>} \\
\test{ 1 -1 \word{less}}{<FALSE>} \\
\test{ 0 MIN-INT \word{less}}{<FALSE>} \\
\test{MAX-INT MIN-INT \word{less}}{<FALSE>} \\
\test{MAX-INT 0 \word{less}}{<FALSE>}
\end{testing}
\end{worddef}
\begin{worddef}[num-start]{0490}{<\num}[less-number-sign]
\item \stack{}{}
Initialize the pictured numeric output conversion process.
\see \wref{core:num}{\num},
\wref{core:num-end}{\num>},
\wref{core:numS}{\num{}S}.
\begin{testing} % T.6.1.0490 <#
See \tref{core:num}{\num},
\tref{core:numS}{\num{}S},
\tref{core:HOLD}{HOLD},
\tref{core:SIGN}{SIGN}.
\end{testing}
\end{worddef}
\begin{worddef}{0530}{=}[equals]
\item \stack{x_1 x_2}{flag}
\param{flag} is true if and only if \param{x_1} is bit-for-bit
the same as \param{x_2}.
\begin{testing} % T.6.1.0530 =
\test{ 0 0 \word{=}}{<TRUE> } \\
\test{ 1 1 \word{=}}{<TRUE> } \\
\test{-1 -1 \word{=}}{<TRUE> } \\
\test{ 1 0 \word{=}}{<FALSE>} \\
\test{-1 0 \word{=}}{<FALSE>} \\
\test{ 0 1 \word{=}}{<FALSE>} \\
\test{ 0 -1 \word{=}}{<FALSE>} \\
\end{testing}