-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.tex
1541 lines (1212 loc) · 54.3 KB
/
tools.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{The optional Programming-Tools word set} % 15
\wordlist{tools}
\section{Introduction} % 15.1
This optional word set contains words most often used during the
development of applications.
\section{Additional terms and notation} % 15.2
\label{tools:terms}
\remove{x:name-token}{None.}
\begin{description}
\item[\uline{name token:}]
\place{x:name-token}{An abstract data type identifying a named word.
Name tokens can be passed to words (such as \word{NAMEtoSTRING})
to obtain information about the named word.}
\end{description}
\section{Additional usage requirements} % 15.3
\subsection{Data types} % 15.3.1
\label{tools:datatype}
A name token is a single-cell value that identifies a named word.
Append table \ref{tools:types} to table \ref{table:datatypes}.
\begin{table}[h]
\begin{center}
\caption{Data types}
\label{tools:types}
\begin{tabular}{llr}
\hline\hline
\emph{Symbol} & \emph{Data type} & \emph{Size on stack} \\
\hline
\emph{nt} & name token & 1 cell \\
\emph{quotation-sys} & colon definition status & implementation dependent \\
\hline\hline
\end{tabular}
\end{center}
\end{table}
\remove{x:name-token}{See: \xref{rat:tools:nt}.}
\subsection{Colon definition status}
\label{tools:quotation-sys}
The implementation-dependent \emph{quotation-sys} type
contains the data that needs to be saved for the enclosing
colon definition and restored after the end of the quotation.
It is used in combination with \emph{colon-sys}.
\subsection{The Forth dictionary} % 15.3.2
\label{tools:dict}
A program using the words \word{CODE} or \word{;CODE} associated
with assembler code has an environmental dependency on that
particular instruction set and assembler notation.
Programs using the words \word{EDITOR} or \word{ASSEMBLER} require
the Search Order word set or an equivalent implementation-defined
capability.
See: \xref[3.3 The Forth dictionary]{usage:dict}.
\section{Additional documentation requirements} % 15.4
\subsection{System documentation} % 15.4.1
\subsubsection{Implementation-defined options} % 15.4.1.1
\label{tools:impopt}
\begin{itemize}
\item ending sequence for input following
\wref{tools:;CODE}{;CODE} and
\wref{tools:CODE}{CODE};
\item manner of processing input following
\wref{tools:;CODE}{;CODE} and
\wref{tools:CODE}{CODE};
\item search-order capability for
\wref{tools:EDITOR}{EDITOR} and
\wref{tools:ASSEMBLER}{ASSEMBLER}
(\xref[15.3.3 The Forth dictionary]{tools:dict});
\item source and format of display by \wref{tools:SEE}{SEE}.
\end{itemize}
\subsubsection{Ambiguous conditions} % 15.4.1.2
\label{tools:ambiguous}
\begin{itemize}
\item deleting the compilation word-list (\wref{tools:FORGET}{});
\item fewer than $u+1$ items on control-flow stack
(\wref{tools:CS-PICK}{CS-PICK},
\wref{tools:CS-ROLL}{CS-ROLL});
\item \emph{name} can't be found (\wref{tools:FORGET}{}, \wref{tools:SYNONYM}{});
\item \emph{name} not defined via \wref{core:CREATE}{CREATE}
(\wref{tools:;CODE}{;CODE});
\item \wref{core:POSTPONE}{POSTPONE} applied to \wref{tools:[IF]}{[IF]};
\item reaching the end of the input source before matching
\wref{tools:[ELSE]}{[ELSE]} or \wref{tools:[THEN]}{[THEN]}
(\wref{tools:[IF]}{[IF]});
\item removing a needed definition (\wref{tools:FORGET}{});
\item \wref{core:IMMEDIATE}{} is applied to a word defined by \wref{tools:SYNONYM}{};
\item \wref{tools:NRfrom}{} is used with data not stored by \wref{tools:NtoR}{};
\item adding to or deleting from the wordlist during the execution of
\wref{tools:TRAVERSE-WORDLIST};
\item The compilation semantics of \wref{tools:;CODE}{} or \wref{core:;}{} used within
a quotation \\(\word{[:} {\ldots} \word{;]}).
\end{itemize}
\subsubsection{Other system documentation} % 15.4.1.3
\begin{itemize}
\item no additional requirements.
\end{itemize}
\subsection{Program documentation} % 15.4.2
\subsubsection{Environmental dependencies} % 15.4.2.1
\begin{itemize}
\item using the words \wref{tools:;CODE}{;CODE} or
\wref{tools:CODE}{CODE}.
\end{itemize}
\subsubsection{Other program documentation} % 15.4.2.2
\begin{itemize}
\item no additional requirements.
\end{itemize}
\section{Compliance and labeling} % 15.5
\enlargethispage{2ex}
\subsection{Forth-\snapshot{} systems} % 15.5.1
The phrase ``Providing the Programming-Tools word set'' shall be
appended to the label of any Standard System that provides all of
the Programming-Tools word set.
The phrase ``Providing \emph{name(s)} from the Programming-Tools
Extensions word set'' shall be appended to the label of any Standard
System that provides portions of the Programming-Tools Extensions
word set.
The phrase ``Providing the Programming-Tools Extensions word set''
shall be appended to the label of any Standard System that provides
all of the Programming-Tools and Programming-Tools Extensions word
sets.
\subsection{Forth-\snapshot{} programs} % 15.5.2
The phrase ``Requiring the Programming-Tools word set'' shall be
appended to the label of Standard Programs that require the system
to provide the Programming-Tools word set.
The phrase ``Requiring \emph{name(s)} from the Programming-Tools
Extensions word set'' shall be appended to the label of Standard
Programs that require the system to provide portions of the
Programming-Tools Extensions word set.
The phrase ``Requiring the Programming-Tools Extensions word set''
shall be appended to the label of Standard Programs that require the
system to provide all of the Programming-Tools and Programming-Tools
Extensions word sets.
\section{Glossary} % 15.6
\subsection{Programming-Tools words} % 15.6.1
\begin{worddef}{0220}{.S}[dot-s]
\item \stack{}{}
Copy and display the values currently on the data stack. The
format of the display is implementation-dependent.
\word{.S} may be implemented using pictured numeric output words.
Consequently, its use may corrupt the transient region identified
by \word[core]{num-end}.
\see \xref[3.3.3.6 Other transient regions]{usage:transient},
\rref{tools:.S}{}.
\begin{rationale} % A.15.6.1.0220 .S
\word[tools]{.S} is a debugging convenience found on almost
all Forth systems. It is universally mentioned in Forth texts.
\end{rationale}
\end{worddef}
\begin{worddef}[q]{0600}{?}[question]
\item \stack{a-addr}{}
Display the value stored at \param{a-addr}.
\word{q} may be implemented using pictured numeric output words.
Consequently, its use may corrupt the transient region identified
by \word[core]{num-end}.
\see \xref[3.3.3.6 Other transient regions]{usage:transient}.
\end{worddef}
\begin{worddef}{1280}{DUMP}
\item \stack{addr u}{}
Display the contents of \param{u} consecutive addresses starting
at \param{addr}. The format of the display is implementation
dependent.
\word{DUMP} may be implemented using pictured numeric output
words. Consequently, its use may corrupt the transient region
identified by \word[core]{num-end}.
\see \xref[3.3.3.6 Other transient regions]{usage:transient}.
\end{worddef}
\begin{worddef}{2194}{SEE}
\item \stack{"<spaces>name"}{}
Display a human-readable representation of the named word's
definition. The source of the representation (object-code
decompilation, source block, etc.) and the particular form of
the display is implementation defined.
\word{SEE} may be implemented using pictured numeric output
words. Consequently, its use may corrupt the transient region
identified by \word[core]{num-end}.
\see \xref[3.3.3.6 Other transient regions]{usage:transient},
\rref{tools:SEE}{}.
\begin{rationale} % A.15.6.1.2194 SEE
\word[tools]{SEE} acts as an on-line form of documentation of
words, allowing modification of words by decompiling and
regenerating with appropriate changes.
\end{rationale}
\end{worddef}
\begin{worddef}{2465}{WORDS}
\item \stack{}{}
List the definition names in the first word list of the search
order. The format of the display is implementation-dependent.
\word{WORDS} may be implemented using pictured numeric output
words. Consequently, its use may corrupt the transient region
identified by \word[core]{num-end}.
\see \xref[3.3.3.6 Other transient regions]{usage:transient},
\rref{tools:WORDS}{}.
\begin{rationale} % A.15.6.1.2465 WORDS
\word[tools]{WORDS} is a debugging convenience found on almost
all Forth systems. It is universally referred to in Forth texts.
\end{rationale}
\end{worddef}
\subsection{Programming-Tools extension words} % 15.6.2
\extended
\begin{worddef}{0470}{;CODE}[semicolon-code]
\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}.
Subsequent characters in the parse area typically represent
source code in a programming language, usually some form of
assembly language. Those characters are processed in an
implementation-defined manner, generating the corresponding
machine code. The process continues, refilling the input buffer
as needed, until an implementation-defined ending sequence is
processed.
An ambiguous condition exists if the compilation semantics of
\word{;CODE} is preformed inside a quotation.
\runtime
\stack{}{}
\stack[R]{nest-sys}{}
Replace the execution semantics of the most recent definition
with the \emph{name} execution semantics given below. Return
control to the calling definition specified by \param{nest-sys}.
An ambiguous condition exists if the most recent definition was
not defined with \word[core]{CREATE} or a user-defined word that
calls \word[core]{CREATE}.
\execute[name]
\stack{i*x}{j*x}
Perform the machine code sequence that was generated following
\word{;CODE}.
\see \wref{core:DOES}{DOES>},
\rref{tools:;CODE}{}.
\begin{rationale} % A.15.6.2.0470 ;CODE
Typical use:
\word[core]{:} \texttt{namex}
{\ldots} \arg{create} {\ldots}
\word{;CODE} {\ldots}
where \texttt{namex} is a defining word, and \arg{create} is
\word[core]{CREATE} or any user defined word that calls
\word[core]{CREATE}.
\end{rationale}
\end{worddef}
\pagebreak
\begin{worddef}{}{;]}[semi-bracket][x:quotations]
\interpret
Interpretation semantics for this word are undefined.
\compile
\stack[C]{quotation-sys colon-sys}{}
Ends the current nested definition, and resumes compilation to the
previous (containing) current definition. It appends the following
run-time action to the (containing) current definition.
\runtime
\stack{}{xt}
\param{xt} is the execution token of the nested definition.
\see \wref{tools:[:}{}, \rref{tools:[:}{}.
\begin{implement}
\textdf{See \iref{tools:[:}{}.}
\end{implement}
\end{worddef}
\begin{worddef}{0702}{AHEAD}
\interpret
Interpretation semantics for this word are undefined.
\compile
\stack[C]{}{orig}
Put the location of a new unresolved forward reference
\param{orig} onto the control flow stack. Append the run-time
semantics given below to the current definition. The semantics
are incomplete until \param{orig} is resolved (e.g., by
\word[core]{THEN}).
\runtime
\stack{}{}
Continue execution at the location specified by the resolution
of \param{orig}.
\begin{testing} % T.15.6.2.0702 AHEAD
\test{\word{:} pt1 \word{AHEAD} 1111 2222 \word{THEN} 3333 \word{;}}{} \\
\test{pt1}{3333}
\end{testing}
\end{worddef}
\begin{worddef}{0740}{ASSEMBLER}
\item \stack{}{}
Replace the first word list in the search order with the
\word{ASSEMBLER} word list.
\see \xref[16. The optional Search-Order word set]{wordlist:search}.
\end{worddef}
\begin{worddef}{0830}{BYE}
\item \stack{}{}
Return control to the host operating system, if any.
\end{worddef}
\pagebreak
\begin{worddef}{0930}{CODE}
\item \stack{"<spaces>name"}{}
Skip leading space delimiters. Parse \param{name} delimited by a
space. Create a definition for \param{name}, called a ``code
definition'', with the execution semantics defined below.
Subsequent characters in the parse area typically represent
source code in a programming language, usually some form of
assembly language. Those characters are processed in an
implementation-defined manner, generating the corresponding
machine code. The process continues, refilling the input buffer
as needed, until an implementation-defined ending sequence is
processed.
\execute[name]
\stack{i*x}{j*x}
Execute the machine code sequence that was generated following
\word{CODE}.
\see \xref[3.4.1 Parsing]{usage:parsing},
\rref{tools:CODE}{}.
\begin{rationale} % A.15.6.2.0930 CODE
Some Forth systems implement the assembly function by adding
an \word[tools]{ASSEMBLER} word list to the search order,
using the text interpreter to parse a postfix assembly
language with lexical characteristics similar to Forth source
code. Typically, in such systems, assembly ends when a word
\texttt{END-CODE} is interpreted.
\end{rationale}
\end{worddef}
\begin{worddef}{1015}{CS-PICK}[c-s-pick]
\interpret
Interpretation semantics for this word are undefined.
\execute
\stack[C]{dest_u {\ldots} orig_0|dest_0}{dest_u {\ldots} orig_0|dest_0 dest_u}
\stack[S]{u}{}
Remove \param{u}. Copy \param{dest_u} to the top of the
control-flow stack. An ambiguous condition exists if there
are less than \param{u}+1 items, each of which shall be an
\param{orig} or \param{dest}, on the control-flow stack
before \word{CS-PICK} is executed.
If the control-flow stack is implemented using the data stack,
\param{u} shall be the topmost item on the data stack.
\see \rref{tools:CS-PICK}{}.
\begin{rationale} % A.15.6.2.1015 CS-PICK
The intent is to copy a \param{dest} on the control-flow
stack so that it can be resolved more than once. For example:
\setwordlist{core}
\begin{quote}\ttfamily
\word{bs} Conditionally transfer control to beginning of \\
\word{bs} loop. This is similar in spirit to C's "continue" \\
\word{bs} statement.
\word{:} ?REPEAT ~~\word{p} dest -{}- dest ) \word{bs} Compilation \\
\tab\tab\tab~\word{p} flag -{}- ) \tab~\word{bs} Execution \\
\tab 0 \word[tools]{CS-PICK} ~ \word{POSTPONE} \word{UNTIL} \\
\word{;} \word{IMMEDIATE}
\word{:} XX \word{p} -{}- ) \word{bs} Example use of ?REPEAT \\
\tab \word{BEGIN} \\
\tab\tab {\ldots} \\
\tab flag ?REPEAT \word{p} Go back to BEGIN if flag is false ) \\
\tab\tab {\ldots} \\
\tab flag ?REPEAT \word{p} Go back to BEGIN if flag is false ) \\
\tab\tab {\ldots} \\
\tab flag \word{UNTIL}~~ \word{p} Go back to BEGIN if flag is false ) \\
\word{;}
\end{quote}
\setwordlist{tools}
\end{rationale}
\begin{testing} % T.15.6.2.1015 CS-PICK
\ttfamily
\word{:} ?repeat \\
\tab 0 \word{CS-PICK} \word{POSTPONE} \word{UNTIL} \\
\word{;} \word{IMMEDIATE}
\word{VARIABLE} pt4
\word{:} <= \word{more} \word{0=} \word{;}
\test{\word{:} pt5 \word{p} n1 -{}- ) \\
\tab[2.4] pt4 \word{!} \\
\tab[2.4] \word{BEGIN} \\
\tab[3.4] -1 pt4 \word{+!} \\
\tab[3.4] pt4 \word{@} 4 <= ?repeat \tab \word{bs} \textdf{Back to \word{BEGIN} if false} \\
\tab[3.4] 111 \\
\tab[3.4] pt4 \word{@} 3 <= ?repeat \\
\tab[3.4] 222 \\
\tab[3.4] pt4 \word{@} 2 <= ?repeat \\
\tab[3.4] 333 \\
\tab[3.4] pt4 \word{@} 1 \word{=} \\
\tab[2.4] \word{UNTIL} \\
\tab[1.2] \word{;}}{}
\test{6 pt5}{111 111 222 111 222 333 111 222 333}
\end{testing}
\end{worddef}
\begin{worddef}{1020}{CS-ROLL}[c-s-roll]
\interpret
Interpretation semantics for this word are undefined.
\execute
\stack[C]{orig_u|dest_u orig_{u-1}|dest_{u-1} {\ldots} orig_0|dest_0}
{orig_{u-1}|dest_{u-1} {\ldots} orig_0|dest_0 orig_u|dest_u}
\stack[S]{u}{}
Remove \param{u}. Rotate \param{u}+1 elements on top of the
control-flow stack so that \param{orig_u|dest_u} is on top of
the control-flow stack. An ambiguous condition exists if there
are less than \param{u}+1 items, each of which shall be an
\param{orig} or \param{dest}, on the control-flow stack before
\word{CS-ROLL} is executed.
If the control-flow stack is implemented using the data stack,
\param{u} shall be the topmost item on the data stack.
\see \rref{tools:CS-ROLL}{}.
\begin{rationale} % A.15.6.2.1020 CS-ROLL
The intent is to modify the order in which the \param{orig}s
and \param{dest}s on the control-flow stack are to be resolved
by subsequent control-flow words. For example, \word[core]{WHILE}
could be implemented in terms of \word[core]{IF} and
\word{CS-ROLL}, as follows:
\setwordlist{core}
\begin{quote}\ttfamily
\word{:} \word{WHILE} ~ \word{p} dest -{}- orig dest ) \\
\tab \word{POSTPONE} \word{IF} ~ 1 \word[tools]{CS-ROLL} \\
\word{;} \word{IMMEDIATE}
\end{quote}
\setwordlist{tools}
\end{rationale}
\begin{testing} % T.15.6.2.1020 CS-ROLL
\ttfamily
\test{\word{:} ?DONE \word{p} dest -{}- orig dest ) \tab \word{bs} \textdf{Same as WHILE} \\
\tab[2.4] \word{POSTPONE} \word{IF} 1 \word{CS-ROLL} \\
\tab[1.2] \word{;} \word{IMMEDIATE}}{} \\
\test{\word{:} pt6 \\
\tab[2.4] \word{toR} \\
\tab[2.4] \word{BEGIN} \\
\tab[3.4] \word{R@} \\
\tab[2.4] ?DONE \\
\tab[3.4] \word{R@} \\
\tab[3.4] \word{Rfrom} \word{1-} \word{toR} \\
\tab[2.4] \word{REPEAT} \\
\tab[2.4] \word{Rfrom} \word{DROP} \\
\tab[1.2] \word{;}}{}
\test{5 pt6}{5 4 3 2 1}
\word{:} mix\_up 2 \word{CS-ROLL} \word{;} \word{IMMEDIATE} \tab \word{bs} \textdf{cs-rot}
\word{:} pt7 \word{p} f3 f2 f1 -{}- ? ) \\
\tab \word{IF} 1111 \word{ROT} \word{ROT} \tab ( -{}- 1111 f3 f2 ) \tab[1.8] ( cs:\ -{}- o1 ) \\
\tab[2] \word{IF} 2222 \word{SWAP} \tab[0.6] ( -{}- 1111 2222 f3 ) \tab[0.6] ( cs:\ -{}- o1 o2 ) \\
\tab[3] \word{IF} \tab[18.8] ( cs:\ -{}- o1 o2 o3 ) \\
\tab[4] 3333 mix\_up ( -{}- 1111 2222 3333 ) \tab[-0.8] ( cs:\ -{}- o2 o3 o1 ) \\
\tab[3] \word{THEN} \tab[17.6] ( cs:\ -{}- o2 o3 ) \\
\tab[3] 4444 \tab[1] \word{bs} \textdf{Hence failure of first IF comes here and falls through} \\
\tab[2] \word{THEN} \tab[18.6] ( cs:\ -{}- o2 ) \\
\tab[2] 5555 \tab[2] \word{bs} \textdf{Failure of 3rd IF comes here} \\
\tab[1] \word{THEN} \tab[19.6] ( cs:\ -{}- ) \\
\tab[1] 6666 \tab[3] \word{bs} \textdf{Failure of 2nd IF comes here} \\
\tab \word{;}
\test{-1 -1 -1 pt7}{1111 2222 3333 4444 5555 6666} \\
\test{ 0 -1 -1 pt7}{1111 2222 5555 6666 } \\
\test{ 0 0 -1 pt7}{1111 0 6666 } \\
\test{ 0 0 0 pt7}{0 0 4444 5555 6666 }
\word{:} [1cs-roll] 1 \word{CS-ROLL} \word{;} \word{IMMEDIATE}
\test{\word{:} pt8 \\
\tab[2.2] \word{toR} \\
\tab[2.2] \word{AHEAD} 111 \\
\tab[2.2] \word{BEGIN} 222 \\
\tab[3.2] [1cs-roll] \\
\tab[3.2] \word{THEN} \\
\tab[3.2] 333 \\
\tab[3.2] \word{Rfrom} \word{1-} \word{toR} \\
\tab[3.2] \word{R@} \word{0less} \\
\tab[2.2] \word{UNTIL} \\
\tab[2.2] \word{Rfrom} \word{DROP} \\
\tab[1.2] \word{;}}{}
\test{1 pt8}{333 222 333}
\end{testing}
\end{worddef}
\begin{worddef}{1300}{EDITOR}
\item \stack{}{}
Replace the first word list in the search order with the
\word{EDITOR} word list.
\see \xref[16. The Optional Search-Order Word Set]{wordlist:search}.
\end{worddef}
% -------------------------------------------------------------------
\begin{worddef*}{}{FIND-NAME}[][x:find-name]
\item \stack{c-addr u}{nt|0}
\place{x:find-name}{%
Find the definition identified by the string \param{c-addr u} in the current
search order. Return its name token \param{nt}, if found, otherwise 0.
}
\see \wref{tools:FIND-NAME-IN}{},
\rref{tools:FIND-NAME}{}.
\begin{rationale}
\place{x:find-name}{%
\word{FIND-NAME} and \word{FIND-NAME-IN} are natural factors of all words that
look up words in the dictionary, such as \word{FIND}, \word{'}, \word{POSTPONE},
the text interpreter, and \word[search]{SEARCH-WORDLIST}. So implementing them
does not cost additional code, only some refactoring effort.}
\place{x:find-name}{%
This approach is not compatible with system that use separate word headers for the
interpretation and compilation semantics of a word. This problem already exists for
the other words that deal with name tokens. However, such systems have been known
for a lest two decades, and have seen little to no uptake in standard systems.
}
\place{x:find-name}{%
Typically used to build a custom text interpreter:
}
\setwordlist{tools}\ttfamily
\uline{\word{:} get-name-token \word{p} "name<space>" -{}- nt )} \\
\tab[1] \uline{\word{PARSE-NAME} \word{FIND-NAME} \word{DUP} \word{0=} -13 \word{AND} \word[exception]{THROW}}\\
\uline{\word{;}}
\uline{\word{:} \word{'} get-name-token \word{NAMEtoINTERPRET} \word{;}}
\uline{\word{:} \word{POSTPONE}} \\
\tab[1] \uline{get-name-token \word{NAMEtoCOMPILE} \word{SWAP} \word{POSTPONE} \word{LITERAL} \word{COMPILE,}}\\
\uline{\word{;} \word{IMMEDIATE}}
\uline{\word{bs} \textdf{User-defined text interpreter}} \\
\uline{\word{:} interpret-word} \\
\tab[1] \uline{\word{PARSE-NAME} \word{2DUP} \word{FIND-NAME} \word{IF}} \\
\tab[2] \uline{\word{NIP} \word{NIP} \word{STATE} \word{@} \word{IF}} \\
\tab[3] \uline{\word{NAMEtoCOMPILE}} \\
\tab[2] \uline{\word{ELSE}} \\
\tab[3] \uline{\word{NAMEtoINTERPRET}} \\
\tab[2] \uline{\word{THEN} \word{EXECUTE}} \\
\tab[1] \uline{\word{ELSE}} \\
\tab[2] \uline{\word{bs} \textdf{Process numbers}} \\
\tab[2] \uline{0 0 \word{2SWAP} \word{toNUMBER} \word{2DROP}} \\
\tab[2] \uline{\word{STATE} \word{@} \word{IF} \word[double]{2LITERAL} \word{THEN}} \\
\tab[1] \uline{\word{THEN}} \\
\uline{\word{;}}
\end{rationale}
\begin{implement}
\uline{\word{bs} \word{FIND-NAME} \textdf{and} \word{FIND-NAME-IN}}\\
\uline{\word{bs} \textdf{mostly by Bernd Paysan}} \\
\uline{\word{bs} \textdf{\url{http://forth-standard.org/standard/core/FIND\#reply-146}}}
\uline{\word{[DEFINED]} gforth \word{[IF]}} \\
\tab \uline{\word{bs} \textdf{Gforth's implementation of \word[local]{b:} {\ldots} uses a system-specific}}\\
\tab \uline{\word{bs} \textdf{wordlist that does not work with the \word{FIND-NAME-IN} below.}}\\
\tab \uline{\word{bs} \textdf{Therefore we use the reference implementation of \word[local]{b:} instead.}}\\
\tab \uline{\word[file]{REQUIRE} locals.fs}\\
\uline{\word{[THEN]}}
\uline{\word{[UNDEFINED]} bounds \word{[IF]}} \\
\tab[1] \uline{\word{:} bounds \word{p} \textdf{addr len -{}- addr+len addr} )} \\
\tab[2] \uline{\word{OVER} \word{+} \word{SWAP}} \\
\tab[1] \uline{\word{;}} \\
\uline{\word{[THEN]}}
\uline{\word{[UNDEFINED]} -rot \word{[IF]}} \\
\tab[1] \uline{\word{:} -rot \word{p} \textdf{a b c -{}- c a b} )} \\
\tab[2] \uline{\word{ROT} \word{ROT}} \\
\tab[1] \uline{\word{;}} \\
\uline{\word{[THEN]}}
\uline{\word{:} >lower \word{p} \textdf{c1 -{}- c2} )} \\
\tab \uline{\word{DUP} 'A' 'Z' \word{1+} \word{WITHIN} \word{BL} \word{AND} \word{OR}} \\
\uline{\word{;}}
\uline{\word{:} istr= \word{p} \textdf{addr1 u1 addr2 u2 -{}- flag} )} \\
\tab[1] \uline{\word{ROT} \word{OVER} \word{ne} \word{IF} \word{2DROP} \word{FALSE} \word{EXIT} \word{THEN}} \\
\tab[1] \uline{bounds \word{qDO}} \\
\tab[2] \uline{\word{DUP} \word{C@} >lower \word{I} \word{C@} >lower \word{ne} \word{IF} \word{DROP} \word{FALSE} \word{UNLOOP} \word{EXIT} \word{THEN}} \\
\tab[2] \uline{\word{1+}} \\
\tab[1] \uline{\word{LOOP}} \\
\tab[1] \uline{\word{DROP} \word{TRUE}} \\
\uline{\word{;}}
\uline{\word{:} find-name-in-helper \word{p} \textdf{addr u wid -{}- nt / 0} )} \\
\tab[1] \uline{\word{DUP} \word{toR} \word{NAMEtoSTRING} \word{2OVER} istr= \word{IF}} \\
\tab[2] \uline{\word{ROT} \word{DROP} \word{Rfrom} -rot \word{FALSE}} \\
\tab[1] \uline{\word{ELSE}} \\
\tab[2] \uline{\word{Rfrom} \word{DROP} \word{TRUE}} \\
\tab[1] \uline{\word{THEN}} \\
\uline{\word{;}}
\uline{\word{:} \word{FIND-NAME-IN} \word{p} \textdf{addr u wid -{}- nt / 0} )}\\
\tab[1] \uline{\word{toR} 0 -rot \word{Rfrom}} \\
\tab[1] \uline{\word{[']} find-name-in-helper} \\
\tab[1] \uline{\word{SWAP} \word{TRAVERSE-WORDLIST} \word{2DROP}} \\
\uline{\word{;}}
\uline{\word{:} \word{FIND-NAME} \word[local]{b:} c-addr u -{}- nt | 0 :\}} \\
\tab[1] \uline{\word[search]{GET-ORDER}} \\
\tab[1] \uline{\word{[DEFINED]} gforth \word{[IF]}} \\
\tab[2] \uline{\word{[} \word{'} locals \word{toBODY} \word{]} \word{LITERAL} \word{SWAP} \word{1+}} \\
\tab[1] \uline{\word{[ELSE]} \word{[DEFINED]} vfxforth \word{[IF]}} \\
\tab[3] \uline{\word{[} \word{'} localvars \word{toBODY} 3 \word{CELLS} \word{+} \word{]} \word{LITERAL} \word{SWAP} \word{1+}} \\
\tab[2] \uline{\word{[ELSE]}} \\
\tab[3] \uline{\word{CR} \word{.p} \textdf{warning:\ find-name does not find locals} )}\\
\tab[2] \uline{\word{[THEN]}} \\
\tab[1] \uline{\word{[THEN]}} \\
\tab[1] \uline{0 \word{SWAP} 0 \word{qDO} \word{p} \textdf{widn {\ldots} widi nt | 0})} \\
\tab[2] \uline{\word{DUP} \word{0=} \word{IF}} \\
\tab[3] \uline{\word{DROP} c-addr u \word{ROT} \word{FIND-NAME-IN}} \\
\tab[2] \uline{\word{ELSE}} \\
\tab[3] \uline{\word{NIP}} \\
\tab[2] \uline{\word{THEN}} \\
\tab[1] \uline{\word{LOOP}} \\
\uline{\word{;}}
\end{implement}
\begin{testing}\ttfamily\setwordlist{tools}
\uline{\word{:} >lower \word{p} c1 -{}- c2 )} \\
\tab[1] \uline{\word{DUP} 'A' 'Z' \word{1+} \word{WITHIN} \word{BL} \word{AND} \word{OR}} \\
\uline{\word{;}}
\uline{\word{:} istr= \word{p} addr1 u1 addr2 u2 -{}- flag )} \\
\tab[1] \uline{\word{ROT} \word{OVER} \word{ne} \word{IF}
\word{2DROP} \word{DROP} \word{FALSE} \word{EXIT}
\word{THEN}} \\
\tab[1] \uline{bounds \word{qDO}} \\
\tab[2] \uline{\word{DUP} \word{C@} >lower \word{I} \word{C@} >lower \word{ne} \word{IF} \word{DROP} \word{FALSE} \word{UNLOOP} \word{EXIT} \word{THEN}} \\
\tab[2] \uline{\word{1+}} \\
\tab[1] \uline{\word{LOOP}} \\
\tab[1] \uline{\word{DROP} \word{TRUE}} \\
\uline{\word{;}}
\uline{\word[search]{WORDLIST} \word{CONSTANT} fntwl} \\
\uline{\word[search]{GET-CURRENT} fntwl \word[search]{SET-CURRENT}} \\
\uline{\word{:} fnt1 25 \word{;}} \\
\uline{\word{:} fnt2 34 \word{;} \word{IMMEDIATE}} \\
\uline{\word[search]{SET-CURRENT}} \\
\uline{\test{\word{Sq} fnt1" fntwl \word{FIND-NAME-IN} \word{NAMEtoINTERPRET} \word{EXECUTE}}{25}}
\test{\uline{\word{:} fnt3}\\
\tab[1] \uline{\word{[} \word{Sq} fnt1" fntwl \word{FIND-NAME-IN} \word{NAMEtoCOMPILE} \word{EXECUTE} \word{]}}\\
\uline{\word{;} fnt3}}{\uline{25}} \\
\uline{\test{\word{Sq} fnt1" fntwl \word{FIND-NAME-IN} \word{NAMEtoSTRING} \word{Sq} fnt1" istr=}{\word{TRUE}}}
\uline{\test{\word{Sq} fnt2" fntwl \word{FIND-NAME-IN} \word{NAMEtoINTERPRET} \word{EXECUTE}}{34}} \\
\uline{\test{\word{Sq} fnt2" fntwl \word{FIND-NAME-IN} \word{NAMEtoCOMPILE} \word{EXECUTE}}{34}}
\uline{\word{:} fnt4 fntwl \word{FIND-NAME-IN} \word{NAMEtoCOMPILE} \word{EXECUTE} \word{;} \word{IMMEDIATE}}
\uline{\test{\word{Sq} fnt2" \word{]} fnt4 \word{[}}{34}}
\uline{\test{\word{Sq} fnt0" fntwl \word{FIND-NAME-IN}}{0}}
\uline{\word{:} fnt5 42 \word{;}} \\
\uline{\word{:} fnt6 51 \word{;} \word{IMMEDIATE}}
\uline{\test{\word{Sq} fnt5" \word{FIND-NAME} \word{NAMEtoINTERPRET} \word{EXECUTE}}{42}}
\test{\uline{\word{:} fnt7} \\
\tab[1] \uline{\word{[} \word{Sq} fnt5" \word{FIND-NAME} \word{NAMEtoCOMPILE} \word{EXECUTE} \word{]}} \\
\uline{\word{;} fnt7}}{\uline{42}}
\uline{\test{\word{Sq} fnt5" \word{FIND-NAME} \word{NAMEtoSTRING} \word{Sq} fnt5" istr=}{\word{TRUE}}}
\uline{\test{\word{Sq} fnt6" \word{FIND-NAME} \word{NAMEtoINTERPRET} \word{EXECUTE}}{51}} \\
\uline{\test{\word{Sq} fnt6" \word{FIND-NAME} \word{NAMEtoCOMPILE} \word{EXECUTE}}{51}}
\uline{\word{:} fnt8 \word{FIND-NAME} \word{NAMEtoCOMPILE} \word{EXECUTE} \word{;} \word{IMMEDIATE}}
\uline{\test{\word{Sq} fnt6" \word{]} fnt8 \word{[}}{51}} \\
\uline{\test{\word{Sq} fnt0hfshkshdfskl" \word{FIND-NAME}}{0}} \\
\uline{\test{\word{Seq} s\bs{"}" \word{FIND-NAME} \word{NAMEtoINTERPRET} \word{EXECUTE} bla" \word{Sq} bla" \word[string]{COMPARE}}{0}}
\uline{\test{\word{:} fnt9 \word{[} \word{Seq} s\bs{"}" \word{FIND-NAME} \word{NAMEtoCOMPILE} \word{EXECUTE} ble" \word{]} \word{;}}{}} \\
\uline{\test{fnt9 \word{Sq} ble" \word[string]{COMPARE}}{0}}
\uline{\word{:} fnta \word{FIND-NAME} \word{NAMEtoINTERPRET} \word{EXECUTE} \word{;} \word{IMMEDIATE}} \\
\uline{\test{\word{:} fntb \word{[} \word{Seq} s\bs{"}" \word{]} fnta bli" \word[double]{2LITERAL} \word{;}}{}} \\
\uline{\test{fntb \word{Sq} bli" \word[string]{COMPARE}}{0}}
\uline{\word{:} fnt-interpret-words \word{p} {\ldots} \textdf{"rest-of-line"} -{}- {\ldots} )} \\
\tab[1] \uline{\word{BEGIN}} \\
\tab[2] \uline{\word{PARSE-NAME} \word{DUP}} \\
\tab[1] \uline{\word{WHILE}} \\
\tab[2] \uline{\word{2DUP} \word{FIND-NAME} \word{DUP} \word{0=} -13 \word{AND} \word[exception]{THROW}} \\
\tab[2] \uline{\word{NIP} \word{NIP} \word{STATE} \word{@} \word{IF}} \\
\tab[3] \uline{\word{NAMEtoCOMPILE}} \\
\tab[2] \uline{\word{ELSE}} \\
\tab[3] \uline{\word{NAMEtoINTERPRET}} \\
\tab[2] \uline{\word{THEN} \word{EXECUTE}} \\
\tab[1] \uline{\word{REPEAT} \word{2DROP}} \\
\uline{\word{;}}
\test{\uline{fnt-interpret-words fnt5 \word{VALUE} fntd fnt6 \word{TO} fntd}\\
\uline{fntd}}{\uline{fnt6}} \\
\uline{\test{fnt-interpret-words \word{Sq} yyy" \word{:} fnte \word{Sq} yyy" \word{;} fnte \word[string]{COMPARE}}{0}} \\
\test{\uline{fnt-interpret-words \word{:} fntc \word[local]{b:} xa xb :\}} \\
\tab \uline{xa xb \word{TO} xa \word{TO} xb xa xb \word{Sq} xxx"}\\
\uline{\word{;} \word{Sq} xxx" \word{SWAP} fntc \word[string]{COMPARE}}}{\uline{0}}
\end{testing}
\end{worddef*}
\begin{worddef*}{}{FIND-NAME-IN}[][x:find-name]
\item \stack{c-addr u wid}{nt|0}
\place{x:find-name}{%
Find the definition identified by the string \param{c-addr u} in the wordlist
\param{wid}. Return its name token \param{nt}, if found, otherwise 0.
}
\see \xref{tools:FIND-NAME}{},
\rref{tools:FIND-NAME}{}.
\begin{implement}
\uline{\textdf{See \iref{tools:FIND-NAME}{}.}}
\end{implement}
\begin{testing}
\uline{\textdf{See \iref{tools:FIND-NAME}{}.}}
\end{testing}
\end{worddef*}
% -------------------------------------------------------------------
\begin{worddef}{1580}{FORGET}
\item \stack{"<spaces>name"}{}
Skip leading space delimiters. Parse \param{name} delimited by a
space. Find \param{name}, then delete \param{name} from the
dictionary along with all words added to the dictionary after
\param{name}. An ambiguous condition exists if \param{name} cannot
be found.
If the Search-Order word set is present, \word{FORGET} searches
the compilation word list. An ambiguous condition exists if the
compilation word list is deleted.
An ambiguous condition exists if \word{FORGET} removes a word
required for correct execution.
\note This word is obsolescent and is included as a concession to
existing implementations.
\see \xref[3.4.1 Parsing]{usage:parsing},
\rref{tools:FORGET}{}.
\begin{rationale} % A.15.6.2.1580 FORGET
Typical use:
{\ldots} \word{FORGET} \emph{name} {\ldots}
\word{FORGET} \emph{name} tries to infer the previous dictionary
state from \emph{name}; this is not always possible. As a
consequence, \word{FORGET} \emph{name} removes \emph{name} and
all following words in the name space.
See \rref{core:MARKER}{}.
\end{rationale}
\end{worddef}
% -------------------------------------------------------------------
\pagebreak
\begin{worddef}[NtoR]{1908}{N>R}[n-to-r]%[X:n-to-r]
\interpret
Interpretation semantics for this word are undefined.
\execute \stack{i*n +n}{} \stack[R]{}{j*x +n}
Remove \param{n}+1 items from the data stack and store them for later
retrieval by \word{NRfrom}. The return stack may be used to store the
data. Until this data has been retrieved by \word{NRfrom}:
\begin{itemize}
\item this data will not be overwritten by a subsequent invocation of
\word{NtoR} and
\item a program may not access data placed on the return stack before
the invocation of \word{NtoR}.
\end{itemize}
\see \wref{tools:NRfrom}{},
\rref{tools:NtoR}{}.
\begin{rationale}
An implementation may store the stack items in any manner. It may
store them on the return stack, in any order. A stack-constrained
system may prefer to use a buffer to store the items and place a
reference to the buffer on the return stack.
\see \wref{core:SAVE-INPUT}{},
\wref{core:RESTORE-INPUT}{},
\wref{search:GET-ORDER}{}, \linebreak
\wref{search:SET-ORDER}{}.
\end{rationale}
\begin{implement} % I.15.6.2.1908 N>R
\textdf{This implementation depends on the return address being on
the return stack.}
\begin{tabbing}
\tab \= \tab \= \tab[11] \= \kill
\word{:} \word{NtoR} \word{bs} xn .. x1 N -{}- ; R: -{}- x1 .. xn n \\
\word{bs} \textdf{Transfer N items and count to the return stack.} \+ \\
\word{DUP} \>\> \word{bs} xn .. x1 N N -{}- \\
\word{BEGIN} \+ \\
\word{DUP} \- \\
\word{WHILE} \+ \\
\word{ROT} \word{Rfrom} \word{SWAP} \word{toR} \word{toR} \>\word{bs} xn .. N N -{}- ; R: .. x1 -{}- \\
\word{1-} \> \word{bs} xn .. N 'N -{}- ; R: .. x1 -{}- \- \\
\word{REPEAT} \\
\word{DROP} \>\> \word{bs} N -{}- ; R: x1 .. xn -{}- \\
\word{Rfrom} \word{SWAP} \word{toR} \word{toR} \- \\
\word{;}
\end{tabbing}
\end{implement}
\begin{testing} % T.15.6.2.1908 N>R
\word{:} TNR1 \word{NtoR} \word{SWAP} \word{NRfrom} \word{;} \\
\test{1 2 10 20 30 3 TNR1}{2 1 10 20 30 3}
\word{:} TNR2 \word{NtoR} \word{NtoR} \word{SWAP} \word{NRfrom} \word{NRfrom} \word{;} \\
\test{1 2 10 20 30 3 40 50 2 TNR2}{2 1 10 20 30 3 40 50 2}
\end{testing}
\end{worddef}
\begin{worddef}[NAMEtoCOMPILE]{1909}[10]{NAME>COMPILE}[name-to-compile]%[X:traverse-wordlist]
\item \stack{nt}{x xt}
\param{x xt} represents the compilation semantics of the word
\param{nt}. The returned \param{xt} has the stack effect
\stack{i*x x}{j*x}. Executing \param{xt} consumes
\param{x} and performs the compilation semantics of the word
represented by \param{nt}.
\see
\rref{tools:NAMEtoCOMPILE}{},
\wref{tools:TRAVERSE-WORDLIST}{}.
\begin{rationale}
In a traditional \param{xt}+immediate-flag system, the
\param{x xt} returned by \word{NAMEtoCOMPILE} is
typically \param{xt1 xt2}, where \param{xt1} is the
\param{xt} of the word under consideration, and
\param{xt2} is the \param{xt} of \word{EXECUTE}
(for immediate words) or \word{COMPILE,} (for words
with default compilation semantics).
If you want to \word{POSTPONE} \param{nt}, you can
do so with
\begin{quote}\ttfamily
\word{NAMEtoCOMPILE} \word{SWAP} \word{POSTPONE}
\word{LITERAL} \word{COMPILE,}
\end{quote}
\end{rationale}
\end{worddef}
\begin{worddef}[NAMEtoINTERPRET]{1909}[20]{NAME>INTERPRET}[name-to-interpret]%[X:traverse-wordlist]
\item \stack{nt}{xt|0}
\param{xt} represents the interpretation semantics of the word
\param{nt}. If \param{nt} has no interpretation semantics,
\word{NAMEtoINTERPRET} returns 0.
\note
This standard does not define the interpretation semantics of
some words, but systems are allowed to do so.
\see \xref{tools:TRAVERSE-WORDLIST}{}.
\end{worddef}
\begin{worddef}[NAMEtoSTRING]{1909}[40]{NAME>STRING}[name-to-string]%[X:traverse-wordlist]
\item \stack{nt}{c-addr u}
\word{NAMEtoSTRING} returns the name of the word \param{nt}
in the character string \param{c-addr u}. The case of the characters
in the string is implementation-dependent. The buffer containing