Type recs
to find out with which recognizers are
currently being used by Gforth. When invoked in a colon definition
after defining a local, the output of recs
is (at the
time of this writing):
rec-name ( rec-local search-order ( Forth Forth Root ) )
rec-scope rec-number rec-float rec-complex rec-string rec-to rec-dtick
rec-tick rec-body rec-env rec-meta
Here the notation name ( name1 ... namen ) indicates that name is a recognizer sequence that contains the recognizers name1 ... namen.
The recognizers in this sequence are:
rec-name
Recognizes locals and words in the search order.
rec-local
Recognizes locals.
search-order
Recognizes words in the search order. This is shown as recognizer
sequence, because the wordlists (see Word Lists) themselves are
also recognizers: They implement the recognizer interface
(see Defining recognizers) in
addition to working with find-name-in
.
rec-scope
Recognizes voc1:voc2:..vocn:word
, where
voc1 is a vocabulary in the search order, voc2 is a vocabulary
found in voc1, and so on, until word is found in vocn.
The result behaves as if word had been found directly in the
search order. Example: environment:max-n
.
rec-number
Single-cell integers (#-15
, $-f
), characters
('A'
), and double-cell integers #-15.
, with or without
number prefixes (see Integer and character literals).
rec-float
Floating-point numbers (1e
, see Floating-point number and complex literals)
rec-complex
Complex numbers (1e+2ei
, see Floating-point number and complex literals)
rec-string
Strings ("abc"
, see String and environment variable literals).
rec-to
Recognizes ->v
(equivalent to to v
),
+>v
(equivalent to +to v
), and '>v
(equivalent to addr v
), where v is a value-flavoured
word (see Values). Also recognizes @>d
(equivalent to
action-of d
), and =>d
(equivalent to is
d
), where d is a defer-flavoured word (see Deferred Words).
rec-dtick
Recognizes ``word
and produces the name token of word
(see Literals for tokens and addresses).
rec-tick
Recognizes `word
and produces the execution token of word
(see Literals for tokens and addresses).
rec-body
Recognizes <word>
for the body address of word and
<word+num>
for an offset num from the body address
of word (see Literals for tokens and addresses).
rec-env
Recognizes ${env}
for the string contained at run-time in
the environment variable env see String and environment variable literals).
rec-meta
Recognizes rec?string
, e.g., float?1.
.
Rec-rec
is a recognizer found in the search order (e.g.,
rec-float
, and this recognizer then tries to recognize
string (e.g, 1.
), and the result becomes the result of
rec-meta
. This may be useful in cases where you want to use a
specific recognizer, e.g., to deal with conflicts.
The order of the recognizers is significant, because they are tried
from left to right, and the first recognizer that recognizes a word is
actually used. E.g., if you define a local b
, it will
supersede Gforth’s predefined word b
.
In most cases, however, recognizers are designed to avoid matching the
same strings as other recognizers. E.g., rec-env
(the
environment variable recognizer) requires braces to avoid a conflict
with the number recognizer when recognizing environment variables like
ADD
; i.e., rec-env
recognizes ${ADD}
, while
rec-number
recognizes $ADD
.
There are a few cases where Gforth’s recognizers can recognize the same string, however:
However, there are no conflicts of Gforth-defined words with decimal
numbers prefixed with #
or hex numbers prefixed with $
,
so it is a good practice to use these prefixes (it’s also a good idea
to make sure that the right base
is used). An older practice
(before number prefixes were introduced) was to prefix hex numbers
with 0
.
In the code bases we have looked at, starting words with '
(quote aka tick) is much more common than starting them with `
(backquote aka backtick), so the recognizers for the xt and the nt use
`
to reduce the number of conflicts.
rec-number
and the floating-point
recognizer rec-float
recognize, e.g., 1.
. Because
rec-number
is (by default) earlier, 1.
is recognized as a
double-cell integer. If you change the recognizer order to use
rec-float
first, 1.
is recognized as a floating-point
number, but loading code written in Standard Forth may behave in a
non-standard way.
In any case, it’s a good practice to avoid that conflict in your own
code as follows: Always write double-cell integers with a number
prefix, e.g., #1.
; and always write floating-point numbers with
an e
, e.g., 1e
.
->
. You can
avoid a conflict by using to myvalue
or to?->myvalue
(the latter works with postpone
).
Note that most Forth systems do not support all the recognizers we
describe above, but rec-name rec-number rec-float
are relatively common (even if a system uses a hard-coded text
interpreter instead of the flexible recognizer system).
You can use locate
(see Locating source code definitions) to
determine which recognizer recognizes a piece of source code. E.g.:
locate float?1.
will show that rec-meta
recognizes float?1.
. However,
if the recognizer recognizes a dictionary word (e.g., the scope
recognizer), locate
will show that word.
Wordlists are also recognizers, as can be seen by the search order
being shown as recognizer sequence containing the wordlists, . A
wordlist recognizes the words that it contains. Just execute
the wordlist-id, and it will behave as a recognizer:
"dup" forth-wordlist execute
produces the same result as
"dup" rec-name
Actually, rec-name
searches all wordlists in the search order;
with the default search order, that finds the dup
in
forth-wordlist
.
Print the system recognizer order, with the first-searched recognizer leftmost. For recognizer sequences, first the name is printed, then ‘(’, then the content of the sequence, then ‘)’. For a deferred word, the name of the deferred word is shown, not that of the recognizer inside; if it contains a recognizer sequence, the name of the deferred word and the contents of the sequence are shown.
Recognizes (see Defining recognizers)
a visible local or a visible named word. If
successful, translation represents the text-interpretation
semantics (interpreting, compiling, postponing) of that word
(see translate-name
).
Recognizes (see Defining recognizers) a visible local. If successful, translation represents pushing the value of the local at run-time (for details see Gforth locals and see Macros).
Recognizes (see Defining recognizers)
vocabulary:word
, where vocabulary is found
in the search order. Otherwise the behaviour is like that of
rec-name
. The general form can have several
vocabularies preceding word, separated by :
; the
first (leftmost) vocabulary is found in the search order, the
second in the first, etc. word is looked up in the
rightmost vocabulary.
Recognizes (see Defining recognizers)
a single or double number (without or with prefix), or
a character. If successful, translation represents pushing
that number at run-time (see translate-cell
and
translate-dcell
).
Recognizes (see Defining recognizers)
a floating-point number, translation represents
pushing that number at run-time (see translate-float
).
In Gforth, numbers containing decimal digits and a dot are also
recognized as FP numbers (but by default shadowed by
double-cell numbers).
A complex number has the format a+bi
, where
a and b are floating point numbers including their
signs. If c-addr u is a complex number, translation
represents pushing that number at run-time (see
translate-complex
). If c-addr u is not recognized
as a complex number, translation is translate-none
.
A string starts and ends with "
and may contain escaped
characters, including \"
(see String and character literals). If c-addr u is the start of a string, the
translation represents parsing the rest of the string, if
necessary, converting the escaped characters, and pushing the
string at run-time (translate-string
scan-translate-string
). If c-addr u is not
recognized as the start of a string, translation is
translate-none
.
Recognizes (see Defining recognizers)
->v
(TO v
), +>v
(+TO v
), '>v
(ADDR v
),
@>d
(ACTION-OF d
) and =>d
(IS d
), where v is a value-flavoured word and
d is a defer-flavoured word. If successful,
translation represents performing the operation on
v/d at run-time.
Recognizes (see Defining recognizers) ``word
.
If successful, translation represents pushing the name
token of word at run-time (see translate-cell
).
Example: ``S"
gives the nt of S"
.
Recognizes (see Defining recognizers) `word
. If
successful, translation represents pushing the execution
token of word at run-time (see translate-cell
).
Example: `dup
gives the xt of dup.
Recognizes (see Defining recognizers)
<word>
and <word+number>
.
If successful, translation represents pushing the sum of
the body address word and number (0 if absent) at
run-time (see translate-cell
).
Recognizes (see Defining recognizers)
${envvar}
. If successful,
translation represents passing envvar to getenv
at run-time (see translate-env
).
Example: ${HOME}
gives the home directory.
Recognizes (see Defining recognizers)
myrec?mystring
. Produces the result of passing
mystring to rec-myrec
.
Example: hex num?cafe
will be recognized as number even
if a word cafe
is in the search order.
Example: float?123.
will be recognized as float.