: GP4 <# 1 0 #S #> S" 1" S= ;
' xyz EXECUTE
is equivalent to xyz
.
)
(right parenthesis).
( is an immediate word.
The number of characters in ccc may be zero to the
number of characters in the parse area.
"
(double-quote).
Append the run-time semantics given below to the current
definition.
X
...
." ccc"
...
;
An implementation may define interpretation semantics for
." if desired. In one plausible implementation,
interpreting ." would display the delimited message.
In another plausible implementation, interpreting ."
would compile code to display the message later. In still
another plausible implementation, interpreting ." would
be treated as an exception. Given this variation a Standard
Program may not use ." while interpreting. Similarly,
a Standard Program may not compile POSTPONE ."
inside a new word, and then use that word while interpreting.
"
(double-quote).
Append the run-time semantics given below to the current
definition.
X
... DOES> ... ;
Following DOES>, a Standard Program may not make any
assumptions regarding the ability to find either the name of
the definition containing the DOES> or any previous
definition whose name may be concealed by it. DOES>
effectively ends one definition and begins another as far as
local variables and control-flow structures are concerned.
The compilation behavior makes it clear that the user is not
entitled to place DOES> inside any control-flow
structures.
JSR
reference.
NOT
was originally provided in Forth as a
flag operator to make control structures readable. Under its
intended usage the following two definitions would produce
identical results:
NOT
as a cell-wide one's-complement
operation, functionally equivalent to the phrase -1
XOR. At the same time, the data type manipulated by
this word was changed from a flag to a cell-wide collection of
bits and the standard value for true was changed from "1"
(rightmost bit only set) to "-1" (all bits set). As these
definitions of TRUE and NOT
were incompatible
with their previous definitions, many Forth users continue to
rely on the old definitions. Hence both versions are in common
use.
Therefore, usage of NOT
cannot be standardized at
this time. The two traditional meanings of NOT
—
that of negating the sense of a flag and that of doing a one's
complement operation — are made available by 0= and
INVERT, respectively.
ENDIF
POSTPONE THEN
; IMMEDIATE
: X
...
IF ... ENDIF
... ;
POSTPONE replaces most of the functionality of
COMPILE
and [COMPILE]. COMPILE
and
[COMPILE] are used for the same purpose: postpone the
compilation behavior of the next word in the parse area.
COMPILE
was designed to be applied to non-immediate
words and [COMPILE] to immediate words. This burdens
the programmer with needing to know which words in a system
are immediate. Consequently, Forth standards have had to
specify the immediacy or non-immediacy of all words covered by
the standard. This unnecessarily constrains implementors.
A second problem with COMPILE
is that some
programmers have come to expect and exploit a particular
implementation, namely:
: COMPILE
R>
DUP @ , CELL+ >R
;
This implementation will not work on native code Forth systems.
In a native code Forth using inline code expansion and peephole
optimization, the size of the object code produced varies; this
information is difficult to communicate to a "dumb"
COMPILE
. A "smart" (i.e., immediate) COMPILE
would not have this problem, but this was forbidden in previous
standards.
For these reasons, COMPILE
has not been included in
the standard and [COMPILE] has been moved in favor of
POSTPONE. Additional discussion can be found in Hayes,
J.R., "Postpone", Proceedings of the 1989 Rochester
Forth Conference.
INTERPRET
that embodies the text interpreter semantics
described in 3.4 The Forth text interpreter.
Further discussion of the interpret loop can be found in
A.6.2.0945 COMPILE,.
X
... RECURSE ... ;
This is Forth's recursion operator; in some implementations it
is called MYSELF
. The usual example is the coding of
the factorial function.
n2 = n1(n1-1)(n1-2)...(2)(1), the product of n1
with all positive integers less than itself (as a special case,
zero factorial equals one). While beloved of computer scientists,
recursion makes unusually heavy use of both stacks and should
therefore be used with caution. See alternate definition in
A.6.1.2140 REPEAT.
"
(double-quote).
Append the run-time semantics given below to the current
definition.
FOO
S" ]"
EVALUATE
;
FOO
will leave the system in compilation state. Similarly, after
LOADing a block containing ], the system
will be in compilation state.
Note that ] does not affect the parse area and that the
only effect that : has on the parse area is to parse a
word. This entitles a program to use these words to set the
state with known side-effects on the parse area. For example:
: NOP
: POSTPONE ; IMMEDIATE
;
NOP
ALIGN NOP
ALIGNED
Some non-compliant systems have ] invoke a
compiler loop in addition to setting STATE. Such a
system would inappropriately attempt to compile the second
use of NOP
.
)
(right
parenthesis). .( is an immediate word.
"
(double-quote) and
append the run-time semantics given below to the current
definition.
)
WORD COUNT TYPE
; IMMEDIATE
This works fine when used in a line like:
.( HELLO)
5
.
but consider what happens if the user enters an empty
string:
.( )
5
.
The definition of .( shown above would treat the
)
as a leading delimiter, skip it, and continue
consuming characters until it located another )
that followed a non-)
character, or until the
parse area was empty. In the example shown, the
5
.
would be treated as part of the string to be printed.
With PARSE, we could write a correct definition of
.(:
: .(
[CHAR] )
PARSE TYPE
; IMMEDIATE
This definition avoids the "empty string" anomaly.
"
(double-quote), using the
translation rules below. Append the run-time semantics given below to
the current definition.
\a | BEL | (alert, | ASCII 7) |
\b | BS | (backspace, | ASCII 8) |
\e | ESC | (escape, | ASCII 27) |
\f | FF | (form feed, | ASCII 12) |
\l | LF | (line feed, | ASCII 10) |
\m | CR/LF | pair | (ASCII 13, 10) |
\n | newline | (implementation dependent , e.g., CR/LF, CR, LF, LF/CR) | |
\q | double-quote | (ASCII 34) | |
\r | CR | (carriage return, | ASCII 13) |
\t | HT | (horizontal tab, | ASCII 9) |
\v | VT | (vertical tab, | ASCII 11) |
\z | NUL | (no character, | ASCII 0) |
\" | double-quote | (ASCII 34) | |
\x <hexdigit><hexdigit> | |||
The resulting character is the conversion of these two hexadecimal
digits. An ambiguous conditions exists if \x is not
followed by two hexadecimal characters.
| |||
\\ | backslash itself | (ASCII 92) |
SAVE-
INPUT
and RESTORE-INPUT are called with the same input source
in effect.
In the above examples, the EVALUATE phrase could have
been replaced by a phrase involving INCLUDE-FILE
and the same rules would apply.
The standard does not specify what happens if a program
violates the above rules. A Standard System might check for
the violation and return an exception indication from
RESTORE-INPUT, or it might fail in an unpredictable
way.
The return value from RESTORE-INPUT is primarily
intended to report the case where the program attempts to
restore the position of an input source whose position cannot
be restored. The keyboard might be such an input source.
Nesting of SAVE-INPUT and RESTORE-INPUT is
allowed. For example, the following situation works as
expected:
Input Source | possible stack values | |||
block | >IN @ | BLK @ | 2 | |
EVALUATE | >IN @ | 1 | ||
keyboard | >IN @ | 1 | ||
text file | >IN @ | lo-pos | hi-pos | 3 |
"
ABORT "
;
T{ s$ EVALUATE si_inc @ -> 0 2345 15 }T
Testing nesting "
33000 32000 34000 WITHIN
The above implementation returns false for that test,
even though the unsigned number 33000 is clearly within the
range {{32000 ... 34000}}.
The problem is that, in the incorrect implementation, the
signed comparison < gives the wrong answer when 32000
is compared to 33000, because when those numbers are treated
as signed numbers, 33000 is treated as negative 32536, while
32000 remains positive.
Replacing < with U< in the above implementation
makes it work with unsigned numbers, but causes problems with
certain signed number ranges; in particular, the test: