| |
|
| @comment %**end of header (This is for running Texinfo on a region.) |
@comment %**end of header (This is for running Texinfo on a region.) |
| @copying |
@copying |
| This manual is for Gforth |
This manual is for Gforth (version @value{VERSION}, @value{UPDATED}), |
| (version @value{VERSION}, @value{UPDATED}), |
a fast and portable implementation of the ANS Forth language. It |
| a fast and portable implementation of the ANS Forth language |
serves as reference manual, but it also contains an introduction to |
| |
Forth and a Forth tutorial. |
| |
|
| Copyright @copyright{} 1995, 1996, 1997, 1998, 2000, 2003 Free Software Foundation, Inc. |
Copyright @copyright{} 1995, 1996, 1997, 1998, 2000, 2003, 2004,2005 Free Software Foundation, Inc. |
| |
|
| @quotation |
@quotation |
| Permission is granted to copy, distribute and/or modify this document |
Permission is granted to copy, distribute and/or modify this document |
| * Files:: |
* Files:: |
| * Blocks:: |
* Blocks:: |
| * Other I/O:: |
* Other I/O:: |
| |
* OS command line arguments:: |
| * Locals:: |
* Locals:: |
| * Structures:: |
* Structures:: |
| * Object-oriented Forth:: |
* Object-oriented Forth:: |
| Tools |
Tools |
| |
|
| * ANS Report:: Report the words used, sorted by wordset. |
* ANS Report:: Report the words used, sorted by wordset. |
| |
* Stack depth changes:: Where does this stack item come from? |
| |
|
| ANS conformance |
ANS conformance |
| |
|
| severely broken (such that it causes another signal before recovering |
severely broken (such that it causes another signal before recovering |
| from the first); this option avoids endless loops in such cases. |
from the first); this option avoids endless loops in such cases. |
| |
|
| |
@cindex --no-dynamic, command-line option |
| |
@cindex --dynamic, command-line option |
| @item --no-dynamic |
@item --no-dynamic |
| @item --dynamic |
@item --dynamic |
| Disable or enable dynamic superinstructions with replication |
Disable or enable dynamic superinstructions with replication |
| (@pxref{Dynamic Superinstructions}). |
(@pxref{Dynamic Superinstructions}). |
| |
|
| |
@cindex --no-super, command-line option |
| @item --no-super |
@item --no-super |
| Disable dynamic superinstructions, use just dynamic replication; this is |
Disable dynamic superinstructions, use just dynamic replication; this is |
| useful if you want to patch threaded code (@pxref{Dynamic |
useful if you want to patch threaded code (@pxref{Dynamic |
| Superinstructions}). |
Superinstructions}). |
| |
|
| |
@cindex --ss-number, command-line option |
| |
@item --ss-number=@var{N} |
| |
Use only the first @var{N} static superinstructions compiled into the |
| |
engine (default: use them all; note that only @code{gforth-fast} has |
| |
any). This option is useful for measuring the performance impact of |
| |
static superinstructions. |
| |
|
| |
@cindex --ss-min-..., command-line options |
| |
@item --ss-min-codesize |
| |
@item --ss-min-ls |
| |
@item --ss-min-lsu |
| |
@item --ss-min-nexts |
| |
Use specified metric for determining the cost of a primitive or static |
| |
superinstruction for static superinstruction selection. @code{Codesize} |
| |
is the native code size of the primive or static superinstruction, |
| |
@code{ls} is the number of loads and stores, @code{lsu} is the number of |
| |
loads, stores, and updates, and @code{nexts} is the number of dispatches |
| |
(not taking dynamic superinstructions into account), i.e. every |
| |
primitive or static superinstruction has cost 1. Default: |
| |
@code{codesize} if you use dynamic code generation, otherwise |
| |
@code{nexts}. |
| |
|
| |
@cindex --ss-greedy, command-line option |
| |
@item --ss-greedy |
| |
This option is useful for measuring the performance impact of static |
| |
superinstructions. By default, an optimal shortest-path algorithm is |
| |
used for selecting static superinstructions. With @option{--ss-greedy} |
| |
this algorithm is modified to assume that anything after the static |
| |
superinstruction currently under consideration is not combined into |
| |
static superinstructions. With @option{--ss-min-nexts} this produces |
| |
the same result as a greedy algorithm that always selects the longest |
| |
superinstruction available at the moment. E.g., if there are |
| |
superinstructions AB and BCD, then for the sequence A B C D the optimal |
| |
algorithm will select A BCD and the greedy algorithm will select AB C D. |
| |
|
| |
@cindex --print-metrics, command-line option |
| |
@item --print-metrics |
| |
Prints some metrics used during static superinstruction selection: |
| |
@code{code size} is the actual size of the dynamically generated code. |
| |
@code{Metric codesize} is the sum of the codesize metrics as seen by |
| |
static superinstruction selection; there is a difference from @code{code |
| |
size}, because not all primitives and static superinstructions are |
| |
compiled into dynamically generated code, and because of markers. The |
| |
other metrics correspond to the @option{ss-min-...} options. This |
| |
option is useful for evaluating the effects of the @option{--ss-...} |
| |
options. |
| |
|
| @end table |
@end table |
| |
|
| @cindex loading files at startup |
@cindex loading files at startup |
| default image @file{gforth.fi} consist of a sequence of filenames and |
default image @file{gforth.fi} consist of a sequence of filenames and |
| @code{-e @var{forth-code}} options that are interpreted in the sequence |
@code{-e @var{forth-code}} options that are interpreted in the sequence |
| in which they are given. The @code{-e @var{forth-code}} or |
in which they are given. The @code{-e @var{forth-code}} or |
| @code{--evaluate @var{forth-code}} option evaluates the Forth |
@code{--evaluate @var{forth-code}} option evaluates the Forth code. This |
| code. This option takes only one argument; if you want to evaluate more |
option takes only one argument; if you want to evaluate more Forth |
| Forth words, you have to quote them or use @code{-e} several times. To exit |
words, you have to quote them or use @code{-e} several times. To exit |
| after processing the command line (instead of entering interactive mode) |
after processing the command line (instead of entering interactive mode) |
| append @code{-e bye} to the command line. |
append @code{-e bye} to the command line. You can also process the |
| |
command-line arguments with a Forth program (@pxref{OS command line |
| |
arguments}). |
| |
|
| @cindex versions, invoking other versions of Gforth |
@cindex versions, invoking other versions of Gforth |
| If you have several versions of Gforth installed, @code{gforth} will |
If you have several versions of Gforth installed, @code{gforth} will |
| for Forth source-code files. |
for Forth source-code files. |
| |
|
| @item |
@item |
| |
@cindex @code{GFORTHSYSTEMPREFIX} -- environment variable |
| |
|
| |
@code{GFORTHSYSTEMPREFIX} -- specifies what to prepend to the argument |
| |
of @code{system} before passing it to C's @code{system()}. Default: |
| |
@code{"./$COMSPEC /c "} on Windows, @code{""} on other OSs. The prefix |
| |
and the command are directly concatenated, so if a space between them is |
| |
necessary, append it to the prefix. |
| |
|
| |
@item |
| @cindex @code{GFORTH} -- environment variable |
@cindex @code{GFORTH} -- environment variable |
| @code{GFORTH} -- used by @file{gforthmi}, @xref{gforthmi}. |
@code{GFORTH} -- used by @file{gforthmi}, @xref{gforthmi}. |
| |
|
| In general, words consume their stack arguments (@code{.s} is an |
In general, words consume their stack arguments (@code{.s} is an |
| exception). |
exception). |
| |
|
| @assignment |
@quotation Assignment |
| What does the stack contain after @code{5 6 7 .}? |
What does the stack contain after @code{5 6 7 .}? |
| @endassignment |
@end quotation |
| |
|
| |
|
| @node Arithmetics Tutorial, Stack Manipulation Tutorial, Stack Tutorial, Tutorial |
@node Arithmetics Tutorial, Stack Manipulation Tutorial, Stack Tutorial, Tutorial |
| 3 4 5 * + . |
3 4 5 * + . |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| What are the infix expressions corresponding to the Forth code above? |
What are the infix expressions corresponding to the Forth code above? |
| Write @code{6-7*8+9} in Forth notation@footnote{This notation is also |
Write @code{6-7*8+9} in Forth notation@footnote{This notation is also |
| known as Postfix or RPN (Reverse Polish Notation).}. |
known as Postfix or RPN (Reverse Polish Notation).}. |
| @endassignment |
@end quotation |
| |
|
| To change the sign, use @code{negate}: |
To change the sign, use @code{negate}: |
| |
|
| 2 negate . |
2 negate . |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Convert -(-3)*4-5 to Forth. |
Convert -(-3)*4-5 to Forth. |
| @endassignment |
@end quotation |
| |
|
| @code{/mod} performs both @code{/} and @code{mod}. |
@code{/mod} performs both @code{/} and @code{mod}. |
| |
|
| 1 2 .s tuck .s 2drop drop |
1 2 .s tuck .s 2drop drop |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Replace @code{nip} and @code{tuck} with combinations of other stack |
Replace @code{nip} and @code{tuck} with combinations of other stack |
| manipulation words. |
manipulation words. |
| |
|
| 1 2 3 1 2 3 4 |
1 2 3 1 2 3 4 |
| 1 2 3 1 3 |
1 2 3 1 3 |
| @end example |
@end example |
| @endassignment |
@end quotation |
| |
|
| @example |
@example |
| 5 dup * . |
5 dup * . |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Write 17^3 and 17^4 in Forth, without writing @code{17} more than once. |
Write 17^3 and 17^4 in Forth, without writing @code{17} more than once. |
| Write a piece of Forth code that expects two numbers on the stack |
Write a piece of Forth code that expects two numbers on the stack |
| (@var{a} and @var{b}, with @var{b} on top) and computes |
(@var{a} and @var{b}, with @var{b} on top) and computes |
| @code{(a-b)(a+1)}. |
@code{(a-b)(a+1)}. |
| @endassignment |
@end quotation |
| |
|
| Reference: @ref{Stack Manipulation}. |
Reference: @ref{Stack Manipulation}. |
| |
|
| 3 fourth-power . |
3 fourth-power . |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Write colon definitions for @code{nip}, @code{tuck}, @code{negate}, and |
Write colon definitions for @code{nip}, @code{tuck}, @code{negate}, and |
| @code{/mod} in terms of other Forth words, and check if they work (hint: |
@code{/mod} in terms of other Forth words, and check if they work (hint: |
| test your tests on the originals first). Don't let the |
test your tests on the originals first). Don't let the |
| @samp{redefined}-Messages spook you, they are just warnings. |
@samp{redefined}-Messages spook you, they are just warnings. |
| @endassignment |
@end quotation |
| |
|
| Reference: @ref{Colon Definitions}. |
Reference: @ref{Colon Definitions}. |
| |
|
| just @code{( -- )}. You should also add some descriptive comment to |
just @code{( -- )}. You should also add some descriptive comment to |
| more complicated words (I usually do this in the lines following |
more complicated words (I usually do this in the lines following |
| @code{:}). If you don't do this, your code becomes unreadable (because |
@code{:}). If you don't do this, your code becomes unreadable (because |
| you have to work through every definition before you can undertsand |
you have to work through every definition before you can understand |
| any). |
any). |
| |
|
| @assignment |
@quotation Assignment |
| The stack effect of @code{swap} can be written like this: @code{x1 x2 -- |
The stack effect of @code{swap} can be written like this: @code{x1 x2 -- |
| x2 x1}. Describe the stack effect of @code{-}, @code{drop}, @code{dup}, |
x2 x1}. Describe the stack effect of @code{-}, @code{drop}, @code{dup}, |
| @code{over}, @code{rot}, @code{nip}, and @code{tuck}. Hint: When you |
@code{over}, @code{rot}, @code{nip}, and @code{tuck}. Hint: When you |
| are done, you can compare your stack effects to those in this manual |
are done, you can compare your stack effects to those in this manual |
| (@pxref{Word Index}). |
(@pxref{Word Index}). |
| @endassignment |
@end quotation |
| |
|
| Sometimes programmers put comments at various places in colon |
Sometimes programmers put comments at various places in colon |
| definitions that describe the contents of the stack at that place (stack |
definitions that describe the contents of the stack at that place (stack |
| |
|
| You can find a more complete list in @ref{Notation}. |
You can find a more complete list in @ref{Notation}. |
| |
|
| @assignment |
@quotation Assignment |
| Write stack-effect comments for all definitions you have written up to |
Write stack-effect comments for all definitions you have written up to |
| now. |
now. |
| @endassignment |
@end quotation |
| |
|
| |
|
| @node Types Tutorial, Factoring Tutorial, Stack-Effect Comments Tutorial, Tutorial |
@node Types Tutorial, Factoring Tutorial, Stack-Effect Comments Tutorial, Tutorial |
| function; and since there is only one result, you don't have to deal with |
function; and since there is only one result, you don't have to deal with |
| the order of results, either. |
the order of results, either. |
| |
|
| In Forth (and other stack-based languages, e.g., Postscript) the |
In Forth (and other stack-based languages, e.g., PostScript) the |
| parameter and result order of a definition is important and should be |
parameter and result order of a definition is important and should be |
| designed well. The general guideline is to design the stack effect such |
designed well. The general guideline is to design the stack effect such |
| that the word is simple to use in most cases, even if that complicates |
that the word is simple to use in most cases, even if that complicates |
| definitions without locals for exercise purposes to help you gain the |
definitions without locals for exercise purposes to help you gain the |
| essential factoring skills. |
essential factoring skills. |
| |
|
| @assignment |
@quotation Assignment |
| Rewrite your definitions until now with locals |
Rewrite your definitions until now with locals |
| @endassignment |
@end quotation |
| |
|
| Reference: @ref{Locals}. |
Reference: @ref{Locals}. |
| |
|
| 3 2 min . |
3 2 min . |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Write @code{min} without @code{else}-part (hint: what's the definition |
Write @code{min} without @code{else}-part (hint: what's the definition |
| of @code{nip}?). |
of @code{nip}?). |
| @endassignment |
@end quotation |
| |
|
| Reference: @ref{Selection}. |
Reference: @ref{Selection}. |
| |
|
| 1 foo . |
1 foo . |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Write @code{min} without @code{if}. |
Write @code{min} without @code{if}. |
| @endassignment |
@end quotation |
| |
|
| For reference, see @ref{Boolean Flags}, @ref{Numeric comparison}, and |
For reference, see @ref{Boolean Flags}, @ref{Numeric comparison}, and |
| @ref{Bitwise operations}. |
@ref{Bitwise operations}. |
| @code{Until} consumes a flag; if it is non-zero, execution continues at |
@code{Until} consumes a flag; if it is non-zero, execution continues at |
| the @code{begin}, otherwise after the @code{until}. |
the @code{begin}, otherwise after the @code{until}. |
| |
|
| @assignment |
@quotation Assignment |
| Write a definition for computing the greatest common divisor. |
Write a definition for computing the greatest common divisor. |
| @endassignment |
@end quotation |
| |
|
| Reference: @ref{Simple Loops}. |
Reference: @ref{Simple Loops}. |
| |
|
| There is also @code{+do}, which expects signed numbers (important for |
There is also @code{+do}, which expects signed numbers (important for |
| deciding whether to enter the loop). |
deciding whether to enter the loop). |
| |
|
| @assignment |
@quotation Assignment |
| Write a definition for computing the nth Fibonacci number. |
Write a definition for computing the nth Fibonacci number. |
| @endassignment |
@end quotation |
| |
|
| You can also use increments other than 1: |
You can also use increments other than 1: |
| |
|
| 8 fac2 . |
8 fac2 . |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Write a recursive definition for computing the nth Fibonacci number. |
Write a recursive definition for computing the nth Fibonacci number. |
| @endassignment |
@end quotation |
| |
|
| Reference (including indirect recursion): @xref{Calls and returns}. |
Reference (including indirect recursion): @xref{Calls and returns}. |
| |
|
| standard; Gforth has no problem). However, they solve the same |
standard; Gforth has no problem). However, they solve the same |
| problems, so this shouldn't be an issue. |
problems, so this shouldn't be an issue. |
| |
|
| @assignment |
@quotation Assignment |
| Can you rewrite any of the definitions you wrote until now in a better |
Can you rewrite any of the definitions you wrote until now in a better |
| way using the return stack? |
way using the return stack? |
| @endassignment |
@end quotation |
| |
|
| Reference: @ref{Return stack}. |
Reference: @ref{Return stack}. |
| |
|
| v3 5 cells dump |
v3 5 cells dump |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Write a definition @code{vsum ( addr u -- n )} that computes the sum of |
Write a definition @code{vsum ( addr u -- n )} that computes the sum of |
| @code{u} cells, with the first of these cells at @code{addr}, the next |
@code{u} cells, with the first of these cells at @code{addr}, the next |
| one at @code{addr cell+} etc. |
one at @code{addr cell+} etc. |
| @endassignment |
@end quotation |
| |
|
| You can also reserve memory without creating a new word: |
You can also reserve memory without creating a new word: |
| |
|
| type |
type |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| @code{Emit ( c -- )} types @code{c} as character (not a number). |
@code{Emit ( c -- )} types @code{c} as character (not a number). |
| Implement @code{type ( addr u -- )}. |
Implement @code{type ( addr u -- )}. |
| @endassignment |
@end quotation |
| |
|
| Reference: @ref{Memory Blocks}. |
Reference: @ref{Memory Blocks}. |
| |
|
| : close-output ( -- ) fd-out close-file throw ; |
: close-output ( -- ) fd-out close-file throw ; |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| How could you modify @code{copy-file} so that it copies until a second line is |
How could you modify @code{copy-file} so that it copies until a second line is |
| matched? Can you write a program that extracts a section of a text file, |
matched? Can you write a program that extracts a section of a text file, |
| given the line that starts and the line that terminates that section? |
given the line that starts and the line that terminates that section? |
| @endassignment |
@end quotation |
| |
|
| @node Interpretation and Compilation Semantics and Immediacy Tutorial, Execution Tokens Tutorial, Files Tutorial, Tutorial |
@node Interpretation and Compilation Semantics and Immediacy Tutorial, Execution Tokens Tutorial, Files Tutorial, Tutorial |
| @section Interpretation and Compilation Semantics and Immediacy |
@section Interpretation and Compilation Semantics and Immediacy |
| 3 2 ' / catch .s |
3 2 ' / catch .s |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Try the same with @code{execute} instead of @code{catch}. |
Try the same with @code{execute} instead of @code{catch}. |
| @endassignment |
@end quotation |
| |
|
| @code{Throw} always jumps to the dynamically next enclosing |
@code{Throw} always jumps to the dynamically next enclosing |
| @code{catch}, even if it has to leave several call levels to achieve |
@code{catch}, even if it has to leave several call levels to achieve |
| foo . |
foo . |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Define @code{defer ( "name" -- )}, which creates a word that stores an |
Define @code{defer ( "name" -- )}, which creates a word that stores an |
| XT (at the start the XT of @code{abort}), and upon execution |
XT (at the start the XT of @code{abort}), and upon execution |
| @code{execute}s the XT. Define @code{is ( xt "name" -- )} that stores |
@code{execute}s the XT. Define @code{is ( xt "name" -- )} that stores |
| @code{xt} into @code{name}, a word defined with @code{defer}. Indirect |
@code{xt} into @code{name}, a word defined with @code{defer}. Indirect |
| recursion is one application of @code{defer}. |
recursion is one application of @code{defer}. |
| @endassignment |
@end quotation |
| |
|
| Reference: @ref{User-defined Defining Words}. |
Reference: @ref{User-defined Defining Words}. |
| |
|
| POSTPONE then ; immediate |
POSTPONE then ; immediate |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Write @code{MY-2DUP} that has compilation semantics equivalent to |
Write @code{MY-2DUP} that has compilation semantics equivalent to |
| @code{2dup}, but compiles @code{over over}. |
@code{2dup}, but compiles @code{over over}. |
| @endassignment |
@end quotation |
| |
|
| @c !! @xref{Macros} for reference |
@c !! @xref{Macros} for reference |
| |
|
| see bar |
see bar |
| @end example |
@end example |
| |
|
| @assignment |
@quotation Assignment |
| Write @code{]L} which allows writing the example above as @code{: bar ( |
Write @code{]L} which allows writing the example above as @code{: bar ( |
| -- n ) [ 2 2 + ]L ;} |
-- n ) [ 2 2 + ]L ;} |
| @endassignment |
@end quotation |
| |
|
| @c !! @xref{Macros} for reference |
@c !! @xref{Macros} for reference |
| |
|
| Yes, the order of wordlists in the output of @code{order} is reversed |
Yes, the order of wordlists in the output of @code{order} is reversed |
| from stack comments and the output of @code{.s} and thus unintuitive. |
from stack comments and the output of @code{.s} and thus unintuitive. |
| |
|
| @assignment |
@quotation Assignment |
| Define @code{>order ( wid -- )} with adds @code{wid} as first searched |
Define @code{>order ( wid -- )} with adds @code{wid} as first searched |
| wordlist to the search order. Define @code{previous ( -- )}, which |
wordlist to the search order. Define @code{previous ( -- )}, which |
| removes the first searched wordlist from the search order. Experiment |
removes the first searched wordlist from the search order. Experiment |
| with boundary conditions (you will see some crashes or situations that |
with boundary conditions (you will see some crashes or situations that |
| are hard or impossible to leave). |
are hard or impossible to leave). |
| @endassignment |
@end quotation |
| |
|
| The search order is a powerful foundation for providing features similar |
The search order is a powerful foundation for providing features similar |
| to Modula-2 modules and C++ namespaces. However, trying to modularize |
to Modula-2 modules and C++ namespaces. However, trying to modularize |
| |
|
| @example |
@example |
| @kbd{qwer341@key{RET}} |
@kbd{qwer341@key{RET}} |
| :1: Undefined word |
*the terminal*:2: Undefined word |
| qwer341 |
>>>qwer341<<< |
| ^^^^^^^ |
Backtrace: |
| $400D2BA8 Bounce |
$2A95B42A20 throw |
| $400DBDA8 no.extensions |
$2A95B57FB8 no.extensions |
| @end example |
@end example |
| |
|
| The exact text, other than the ``Undefined word'' may differ slightly on |
The exact text, other than the ``Undefined word'' may differ slightly |
| your system, but the effect is the same; when the text interpreter |
on your system, but the effect is the same; when the text interpreter |
| detects an error, it discards any remaining text on a line, resets |
detects an error, it discards any remaining text on a line, resets |
| certain internal state and prints an error message. For a detailed description of error messages see @ref{Error |
certain internal state and prints an error message. For a detailed |
| messages}. |
description of error messages see @ref{Error messages}. |
| |
|
| The text interpreter waits for you to press carriage-return, and then |
The text interpreter waits for you to press carriage-return, and then |
| processes your input line. Starting at the beginning of the line, it |
processes your input line. Starting at the beginning of the line, it |
| |
|
| @example |
@example |
| @kbd{12 dup fred dup@key{RET}} |
@kbd{12 dup fred dup@key{RET}} |
| :1: Undefined word |
*the terminal*:3: Undefined word |
| 12 dup fred dup |
12 dup >>>fred<<< dup |
| ^^^^ |
Backtrace: |
| $400D2BA8 Bounce |
$2A95B42A20 throw |
| $400DBDA8 no.extensions |
$2A95B57FB8 no.extensions |
| @end example |
@end example |
| |
|
| When you press the carriage-return key, the text interpreter starts to |
When you press the carriage-return key, the text interpreter starts to |
| without affecting the stack. Type: |
without affecting the stack. Type: |
| |
|
| @example |
@example |
| @kbd{clearstack 1 2 3@key{RET}} ok |
@kbd{clearstacks 1 2 3@key{RET}} ok |
| @kbd{.s@key{RET}} <3> 1 2 3 ok |
@kbd{.s@key{RET}} <3> 1 2 3 ok |
| @end example |
@end example |
| |
|
| The text interpreter looks up the word @code{clearstack} and executes |
The text interpreter looks up the word @code{clearstacks} and executes |
| it; it tidies up the stack and removes any entries that may have been |
it; it tidies up the stacks and removes any entries that may have been |
| left on it by earlier examples. The text interpreter pushes each of the |
left on it by earlier examples. The text interpreter pushes each of the |
| three numbers in turn onto the stack. Finally, the text interpreter |
three numbers in turn onto the stack. Finally, the text interpreter |
| looks up the word @code{.s} and executes it. The effect of executing |
looks up the word @code{.s} and executes it. The effect of executing |
| |
|
| @example |
@example |
| @kbd{: add-two 2 + . ;@key{RET}} |
@kbd{: add-two 2 + . ;@key{RET}} |
| ^^^^^^^ |
*the terminal*:4: Undefined word |
| Error: Undefined word |
: >>>add-two<<< 2 + . ; |
| @end example |
@end example |
| |
|
| The reason that this didn't happen is bound up in the way that @code{:} |
The reason that this didn't happen is bound up in the way that @code{:} |
| * Files:: |
* Files:: |
| * Blocks:: |
* Blocks:: |
| * Other I/O:: |
* Other I/O:: |
| |
* OS command line arguments:: |
| * Locals:: |
* Locals:: |
| * Structures:: |
* Structures:: |
| * Object-oriented Forth:: |
* Object-oriented Forth:: |
| |
|
| doc-+ |
doc-+ |
| doc-1+ |
doc-1+ |
| |
doc-under+ |
| doc-- |
doc-- |
| doc-1- |
doc-1- |
| doc-* |
doc-* |
| doc-sf! |
doc-sf! |
| doc-df@ |
doc-df@ |
| doc-df! |
doc-df! |
| |
doc-sw@ |
| |
doc-uw@ |
| |
doc-w! |
| |
doc-sl@ |
| |
doc-ul@ |
| |
doc-l! |
| |
|
| @node Address arithmetic, Memory Blocks, Memory Access, Memory |
@node Address arithmetic, Memory Blocks, Memory Access, Memory |
| @subsection Address arithmetic |
@subsection Address arithmetic |
| doc--trailing |
doc--trailing |
| doc-/string |
doc-/string |
| doc-bounds |
doc-bounds |
| |
doc-pad |
| |
|
| @comment TODO examples |
@comment TODO examples |
| |
|
| @i{n2} OF @i{code2} ENDOF |
@i{n2} OF @i{code2} ENDOF |
| @dots{} |
@dots{} |
| ( n ) @i{default-code} ( n ) |
( n ) @i{default-code} ( n ) |
| ENDCASE |
ENDCASE ( ) |
| @end example |
@end example |
| |
|
| Executes the first @i{codei}, where the @i{ni} is equal to @i{n}. If no |
Executes the first @i{codei}, where the @i{ni} is equal to @i{n}. If |
| @i{ni} matches, the optional @i{default-code} is executed. The optional |
no @i{ni} matches, the optional @i{default-code} is executed. The |
| default case can be added by simply writing the code after the last |
optional default case can be added by simply writing the code after |
| @code{ENDOF}. It may use @i{n}, which is on top of the stack, but must |
the last @code{ENDOF}. It may use @i{n}, which is on top of the stack, |
| not consume it. |
but must not consume it. The value @i{n} is consumed by this |
| |
construction (either by a OF that matches, or by the ENDCASE, if no OF |
| |
matches). |
| |
|
| @progstyle |
@progstyle |
| To keep the code understandable, you should ensure that on all paths |
To keep the code understandable, you should ensure that you change the |
| through a selection construct the stack is changed in the same way |
stack in the same way (wrt. number and types of stack items consumed |
| (wrt. number and types of stack items consumed and pushed). |
and pushed) on all paths through a selection construct. |
| |
|
| @node Simple Loops, Counted Loops, Selection, Control Structures |
@node Simple Loops, Counted Loops, Selection, Control Structures |
| @subsection Simple Loops |
@subsection Simple Loops |
| : foo |
: foo |
| 107645672 stats + - ; |
107645672 stats + - ; |
| |
|
| \ use ' stats . to show that 107645672 is the xt for stats |
\ use ' foo . to show that 107645672 is the xt for foo |
| @end example |
@end example |
| |
|
| You can use techniques like this to make new defining words in terms of |
You can use techniques like this to make new defining words in terms of |
| : bar ... greet ... ; |
: bar ... greet ... ; |
| : greet1 ( -- ) ." Good morning" ; |
: greet1 ( -- ) ." Good morning" ; |
| : greet2 ( -- ) ." Hello" ; |
: greet2 ( -- ) ." Hello" ; |
| ' greet2 <IS> greet \ make greet behave like greet2 |
' greet2 IS greet \ make greet behave like greet2 |
| @end example |
@end example |
| |
|
| @progstyle |
@progstyle |
| @example |
@example |
| : real: : ; \ retain access to the original |
: real: : ; \ retain access to the original |
| defer : \ redefine as a deferred word |
defer : \ redefine as a deferred word |
| ' my: <IS> : \ use special version of : |
' my: IS : \ use special version of : |
| \ |
\ |
| \ load application here |
\ load application here |
| \ |
\ |
| ' real: <IS> : \ go back to the original |
' real: IS : \ go back to the original |
| @end example |
@end example |
| |
|
| |
|
| One thing to note is that @code{<IS>} consumes its name when it is |
One thing to note is that @code{IS} has special compilation semantics, |
| executed. If you want to specify the name at compile time, use |
such that it parses the name at compile time (like @code{TO}): |
| @code{[IS]}: |
|
| |
|
| @example |
@example |
| : set-greet ( xt -- ) |
: set-greet ( xt -- ) |
| [IS] greet ; |
IS greet ; |
| |
|
| ' greet1 set-greet |
' greet1 set-greet |
| @end example |
@end example |
| |
|
| |
In situations where @code{IS} does not fit, use @code{defer!} instead. |
| |
|
| A deferred word can only inherit execution semantics from the xt |
A deferred word can only inherit execution semantics from the xt |
| (because that is all that an xt can represent -- for more discussion of |
(because that is all that an xt can represent -- for more discussion of |
| this @pxref{Tokens for Words}); by default it will have default |
this @pxref{Tokens for Words}); by default it will have default |
| semantics of the deferred word in the usual ways: |
semantics of the deferred word in the usual ways: |
| |
|
| @example |
@example |
| : bar .... ; compile-only |
: bar .... ; immediate |
| Defer fred immediate |
Defer fred immediate |
| Defer jim |
Defer jim |
| |
|
| ' bar <IS> jim \ jim has default semantics |
' bar IS jim \ jim has default semantics |
| ' bar <IS> fred \ fred is immediate |
' bar IS fred \ fred is immediate |
| @end example |
@end example |
| |
|
| doc-defer |
doc-defer |
| doc-<is> |
doc-defer! |
| doc-[is] |
|
| doc-is |
doc-is |
| |
doc-defer@ |
| |
doc-action-of |
| @comment TODO document these: what's defers [is] |
@comment TODO document these: what's defers [is] |
| doc-what's |
|
| doc-defers |
doc-defers |
| |
|
| @c Use @code{words-deferred} to see a list of deferred words. |
@c Use @code{words-deferred} to see a list of deferred words. |
| |
|
| Definitions in ANS Forth for @code{defer}, @code{<is>} and @code{[is]} |
Definitions of these words (except @code{defers}) in ANS Forth are |
| are provided in @file{compat/defer.fs}. |
provided in @file{compat/defer.fs}. |
| |
|
| |
|
| @node Aliases, , Deferred words, Defining Words |
@node Aliases, , Deferred words, Defining Words |
| doc-hex |
doc-hex |
| doc-decimal |
doc-decimal |
| |
|
| |
|
| @cindex '-prefix for character strings |
@cindex '-prefix for character strings |
| @cindex &-prefix for decimal numbers |
@cindex &-prefix for decimal numbers |
| |
@cindex #-prefix for decimal numbers |
| @cindex %-prefix for binary numbers |
@cindex %-prefix for binary numbers |
| @cindex $-prefix for hexadecimal numbers |
@cindex $-prefix for hexadecimal numbers |
| |
@cindex 0x-prefix for hexadecimal numbers |
| Gforth allows you to override the value of @code{base} by using a |
Gforth allows you to override the value of @code{base} by using a |
| prefix@footnote{Some Forth implementations provide a similar scheme by |
prefix@footnote{Some Forth implementations provide a similar scheme by |
| implementing @code{$} etc. as parsing words that process the subsequent |
implementing @code{$} etc. as parsing words that process the subsequent |
| @cite{Number Conversion and Literals}, by Wil Baden; Forth Dimensions |
@cite{Number Conversion and Literals}, by Wil Baden; Forth Dimensions |
| 20(3) pages 26--27. In such implementations, unlike in Gforth, a space |
20(3) pages 26--27. In such implementations, unlike in Gforth, a space |
| is required between the prefix and the number.} before the first digit |
is required between the prefix and the number.} before the first digit |
| of an (integer) number. Four prefixes are supported: |
of an (integer) number. The following prefixes are supported: |
| |
|
| @itemize @bullet |
@itemize @bullet |
| @item |
@item |
| @code{&} -- decimal |
@code{&} -- decimal |
| @item |
@item |
| |
@code{#} -- decimal |
| |
@item |
| @code{%} -- binary |
@code{%} -- binary |
| @item |
@item |
| @code{$} -- hexadecimal |
@code{$} -- hexadecimal |
| @item |
@item |
| @code{'} -- base @code{max-char+1} |
@code{0x} -- hexadecimal, if base<33. |
| |
@item |
| |
@code{'} -- numeric value (e.g., ASCII code) of next character; an |
| |
optional @code{'} may be present after the character. |
| @end itemize |
@end itemize |
| |
|
| Here are some examples, with the equivalent decimal number shown after |
Here are some examples, with the equivalent decimal number shown after |
| in braces: |
in braces: |
| |
|
| -$41 (-65), %1001101 (205), %1001.0001 (145 - a double-precision number), |
-$41 (-65), %1001101 (205), %1001.0001 (145 - a double-precision number), |
| 'AB (16706; ascii A is 65, ascii B is 66, number is 65*256 + 66), |
'A (65), |
| 'ab (24930; ascii a is 97, ascii B is 98, number is 97*256 + 98), |
-'a' (-97), |
| &905 (905), $abc (2478), $ABC (2478). |
&905 (905), $abc (2478), $ABC (2478). |
| |
|
| @cindex number conversion - traps for the unwary |
@cindex number conversion - traps for the unwary |
| |
|
| @c anton: these belong in the input stream section |
@c anton: these belong in the input stream section |
| doc-parse |
doc-parse |
| |
doc-parse-name |
| doc-parse-word |
doc-parse-word |
| doc-name |
doc-name |
| doc-word |
doc-word |
| |
|
| |
|
| @c ------------------------------------------------------------- |
@c ------------------------------------------------------------- |
| @node Other I/O, Locals, Blocks, Words |
@node Other I/O, OS command line arguments, Blocks, Words |
| @section Other I/O |
@section Other I/O |
| @cindex I/O - keyboard and display |
@cindex I/O - keyboard and display |
| |
|
| doc-key |
doc-key |
| doc-key? |
doc-key? |
| doc-ekey |
doc-ekey |
| doc-ekey? |
|
| doc-ekey>char |
doc-ekey>char |
| doc->number |
doc-ekey? |
| doc->float |
|
| |
Gforth recognizes various keys available on ANSI terminals (in MS-DOS |
| |
you need the ANSI.SYS driver to get that behaviour). These are the |
| |
keyboard events produced by various common keys: |
| |
|
| |
doc-k-left |
| |
doc-k-right |
| |
doc-k-up |
| |
doc-k-down |
| |
doc-k-home |
| |
doc-k-end |
| |
doc-k-prior |
| |
doc-k-next |
| |
doc-k-insert |
| |
doc-k-delete |
| |
|
| |
The function keys (aka keypad keys) are: |
| |
|
| |
doc-k1 |
| |
doc-k2 |
| |
doc-k3 |
| |
doc-k4 |
| |
doc-k5 |
| |
doc-k6 |
| |
doc-k7 |
| |
doc-k8 |
| |
doc-k9 |
| |
doc-k10 |
| |
doc-k11 |
| |
doc-k12 |
| |
|
| |
Note that K11 and K12 are not as widely available. The shifted |
| |
function keys are also not very widely available: |
| |
|
| |
doc-s-k8 |
| |
doc-s-k1 |
| |
doc-s-k2 |
| |
doc-s-k3 |
| |
doc-s-k4 |
| |
doc-s-k5 |
| |
doc-s-k6 |
| |
doc-s-k7 |
| |
doc-s-k8 |
| |
doc-s-k9 |
| |
doc-s-k10 |
| |
doc-s-k11 |
| |
doc-s-k12 |
| |
|
| |
Words for inputting one line from the keyboard: |
| |
|
| doc-accept |
doc-accept |
| doc-edit-line |
doc-edit-line |
| doc-pad |
|
| |
Conversion words: |
| |
|
| |
doc-s>number? |
| |
doc-s>unumber? |
| |
doc->number |
| |
doc->float |
| |
|
| |
|
| @comment obsolescent words.. |
@comment obsolescent words.. |
| |
Obsolescent input and conversion words: |
| |
|
| doc-convert |
doc-convert |
| doc-expect |
doc-expect |
| doc-span |
doc-span |
| doc-broken-pipe-error |
doc-broken-pipe-error |
| |
|
| |
|
| |
@node OS command line arguments, Locals, Other I/O, Words |
| |
@section OS command line arguments |
| |
@cindex OS command line arguments |
| |
@cindex command line arguments, OS |
| |
@cindex arguments, OS command line |
| |
|
| |
The usual way to pass arguments to Gforth programs on the command line |
| |
is via the @option{-e} option, e.g. |
| |
|
| |
@example |
| |
gforth -e "123 456" foo.fs -e bye |
| |
@end example |
| |
|
| |
However, you may want to interpret the command-line arguments directly. |
| |
In that case, you can access the (image-specific) command-line arguments |
| |
through @code{next-arg}: |
| |
|
| |
doc-next-arg |
| |
|
| |
Here's an example program @file{echo.fs} for @code{next-arg}: |
| |
|
| |
@example |
| |
: echo ( -- ) |
| |
begin |
| |
next-arg 2dup 0 0 d<> while |
| |
type space |
| |
repeat |
| |
2drop ; |
| |
|
| |
echo cr bye |
| |
@end example |
| |
|
| |
This can be invoked with |
| |
|
| |
@example |
| |
gforth echo.fs hello world |
| |
@end example |
| |
|
| |
and it will print |
| |
|
| |
@example |
| |
hello world |
| |
@end example |
| |
|
| |
The next lower level of dealing with the OS command line are the |
| |
following words: |
| |
|
| |
doc-arg |
| |
doc-shift-args |
| |
|
| |
Finally, at the lowest level Gforth provides the following words: |
| |
|
| |
doc-argc |
| |
doc-argv |
| |
|
| @c ------------------------------------------------------------- |
@c ------------------------------------------------------------- |
| @node Locals, Structures, Other I/O, Words |
@node Locals, Structures, OS command line arguments, Words |
| @section Locals |
@section Locals |
| @cindex locals |
@cindex locals |
| |
|
| @cindex @code{method} usage |
@cindex @code{method} usage |
| @example |
@example |
| object class graphical \ "object" is the parent class |
object class graphical \ "object" is the parent class |
| method draw ( x y graphical -- ) |
method draw ( x y -- ) |
| class; |
class; |
| @end example |
@end example |
| |
|
| : draw ( x y -- ) |
: draw ( x y -- ) |
| circle-radius @@ draw-circle ; |
circle-radius @@ draw-circle ; |
| |
|
| : init ( n-radius -- ( |
: init ( n-radius -- ) |
| circle-radius ! ; |
circle-radius ! ; |
| class; |
class; |
| @end example |
@end example |
| doc-depth |
doc-depth |
| doc-fdepth |
doc-fdepth |
| doc-clearstack |
doc-clearstack |
| |
doc-clearstacks |
| |
|
| The following words inspect memory. |
The following words inspect memory. |
| |
|
| |
|
| @node Common Disassembler, 386 Assembler, Common Assembler, Assembler and Code Words |
@node Common Disassembler, 386 Assembler, Common Assembler, Assembler and Code Words |
| @subsection Common Disassembler |
@subsection Common Disassembler |
| |
@cindex disassembler, general |
| |
@cindex gdb disassembler |
| |
|
| You can disassemble a @code{code} word with @code{see} |
You can disassemble a @code{code} word with @code{see} |
| (@pxref{Debugging}). You can disassemble a section of memory with |
(@pxref{Debugging}). You can disassemble a section of memory with |
| |
|
| doc-disasm |
doc-discode |
| |
|
| |
There are two kinds of disassembler for Gforth: The Forth disassembler |
| |
(available on some CPUs) and the gdb disassembler (available on |
| |
platforms with @command{gdb} and @command{mktemp}). If both are |
| |
available, the Forth disassembler is used by default. If you prefer |
| |
the gdb disassembler, say |
| |
|
| |
@example |
| |
' disasm-gdb is discode |
| |
@end example |
| |
|
| The disassembler generally produces output that can be fed into the |
If neither is available, @code{discode} performs @code{dump}. |
| |
|
| |
The Forth disassembler generally produces output that can be fed into the |
| assembler (i.e., same syntax, etc.). It also includes additional |
assembler (i.e., same syntax, etc.). It also includes additional |
| information in comments. In particular, the address of the instruction |
information in comments. In particular, the address of the instruction |
| is given in a comment before the instruction. |
is given in a comment before the instruction. |
| |
|
| |
The gdb disassembler produces output in the same format as the gdb |
| |
@code{disassemble} command (@pxref{Machine Code,,Source and machine |
| |
code,gdb,Debugging with GDB}), in the default flavour (AT&T syntax for |
| |
the 386 and AMD64 architectures). |
| |
|
| @code{See} may display more or less than the actual code of the word, |
@code{See} may display more or less than the actual code of the word, |
| because the recognition of the end of the code is unreliable. You can |
because the recognition of the end of the code is unreliable. You can |
| use @code{disasm} if it did not display enough. It may display more, if |
use @code{discode} if it did not display enough. It may display more, if |
| the code word is not immediately followed by a named word. If you have |
the code word is not immediately followed by a named word. If you have |
| something else there, you can follow the word with @code{align latest ,} |
something else there, you can follow the word with @code{align latest ,} |
| to ensure that the end is recognized. |
to ensure that the end is recognized. |
| @example |
@example |
| ax bx mov \ move ebx,eax |
ax bx mov \ move ebx,eax |
| 3 # ax mov \ mov eax,3 |
3 # ax mov \ mov eax,3 |
| 100 di ) ax mov \ mov eax,100[edi] |
100 di d) ax mov \ mov eax,100[edi] |
| 4 bx cx di) ax mov \ mov eax,4[ebx][ecx] |
4 bx cx di) ax mov \ mov eax,4[ebx][ecx] |
| .w ax bx mov \ mov bx,ax |
.w ax bx mov \ mov bx,ax |
| @end example |
@end example |
| <n> # <reg> <inst> |
<n> # <reg> <inst> |
| <mem> <reg> <inst> |
<mem> <reg> <inst> |
| <reg> <mem> <inst> |
<reg> <mem> <inst> |
| |
<n> # <mem> <inst> |
| @end example |
@end example |
| |
|
| Immediate to memory is not supported. The shift/rotate syntax is: |
The shift/rotate syntax is: |
| |
|
| @example |
@example |
| <reg/mem> 1 # shl \ shortens to shift without immediate |
<reg/mem> 1 # shl \ shortens to shift without immediate |
| Gforth allows you to pass an arbitrary string to the host operating |
Gforth allows you to pass an arbitrary string to the host operating |
| system shell (if such a thing exists) for execution. |
system shell (if such a thing exists) for execution. |
| |
|
| |
|
| doc-sh |
doc-sh |
| doc-system |
doc-system |
| doc-$? |
doc-$? |
| doc-getenv |
doc-getenv |
| |
|
| |
|
| @c ------------------------------------------------------------- |
@c ------------------------------------------------------------- |
| @node Keeping track of Time, Miscellaneous Words, Passing Commands to the OS, Words |
@node Keeping track of Time, Miscellaneous Words, Passing Commands to the OS, Words |
| @section Keeping track of Time |
@section Keeping track of Time |
| in file included from \evaluated string/:-1 |
in file included from \evaluated string/:-1 |
| in file included from ./yyy.fs:1 |
in file included from ./yyy.fs:1 |
| ./xxx.fs:4: Invalid memory address |
./xxx.fs:4: Invalid memory address |
| bar |
>>>bar<<< |
| ^^^ |
|
| Backtrace: |
Backtrace: |
| $400E664C @@ |
$400E664C @@ |
| $400E6664 foo |
$400E6664 foo |
| error happened when text-interpreting line 4 of the file |
error happened when text-interpreting line 4 of the file |
| @file{./xxx.fs}. This line is given (it contains @code{bar}), and the |
@file{./xxx.fs}. This line is given (it contains @code{bar}), and the |
| word on the line where the error happened, is pointed out (with |
word on the line where the error happened, is pointed out (with |
| @code{^^^}). |
@code{>>>} and @code{<<<}). |
| |
|
| The file containing the error was included in line 1 of @file{./yyy.fs}, |
The file containing the error was included in line 1 of @file{./yyy.fs}, |
| and @file{yyy.fs} was included from a non-file (in this case, by giving |
and @file{yyy.fs} was included from a non-file (in this case, by giving |
| |
|
| @menu |
@menu |
| * ANS Report:: Report the words used, sorted by wordset. |
* ANS Report:: Report the words used, sorted by wordset. |
| |
* Stack depth changes:: Where does this stack item come from? |
| @end menu |
@end menu |
| |
|
| See also @ref{Emacs and Gforth}. |
See also @ref{Emacs and Gforth}. |
| |
|
| @node ANS Report, , Tools, Tools |
@node ANS Report, Stack depth changes, Tools, Tools |
| @section @file{ans-report.fs}: Report the words used, sorted by wordset |
@section @file{ans-report.fs}: Report the words used, sorted by wordset |
| @cindex @file{ans-report.fs} |
@cindex @file{ans-report.fs} |
| @cindex report the words used in your program |
@cindex report the words used in your program |
| compilation semantics of @code{S"}, it is a Core word; if you also use |
compilation semantics of @code{S"}, it is a Core word; if you also use |
| its interpretation semantics, it is a File word. |
its interpretation semantics, it is a File word. |
| |
|
| |
|
| |
@node Stack depth changes, , ANS Report, Tools |
| |
@section Stack depth changes during interpretation |
| |
@cindex @file{depth-changes.fs} |
| |
@cindex depth changes during interpretation |
| |
@cindex stack depth changes during interpretation |
| |
@cindex items on the stack after interpretation |
| |
|
| |
Sometimes you notice that, after loading a file, there are items left |
| |
on the stack. The tool @file{depth-changes.fs} helps you find out |
| |
quickly where in the file these stack items are coming from. |
| |
|
| |
The simplest way of using @file{depth-changes.fs} is to include it |
| |
before the file(s) you want to check, e.g.: |
| |
|
| |
@example |
| |
gforth depth-changes.fs my-file.fs |
| |
@end example |
| |
|
| |
This will compare the stack depths of the data and FP stack at every |
| |
empty line (in interpretation state) against these depths at the last |
| |
empty line (in interpretation state). If the depths are not equal, |
| |
the position in the file and the stack contents are printed with |
| |
@code{~~} (@pxref{Debugging}). This indicates that a stack depth |
| |
change has occured in the paragraph of non-empty lines before the |
| |
indicated line. It is a good idea to leave an empty line at the end |
| |
of the file, so the last paragraph is checked, too. |
| |
|
| |
Checking only at empty lines usually works well, but sometimes you |
| |
have big blocks of non-empty lines (e.g., when building a big table), |
| |
and you want to know where in this block the stack depth changed. You |
| |
can check all interpreted lines with |
| |
|
| |
@example |
| |
gforth depth-changes.fs -e "' all-lines is depth-changes-filter" my-file.fs |
| |
@end example |
| |
|
| |
This checks the stack depth at every end-of-line. So the depth change |
| |
occured in the line reported by the @code{~~} (not in the line |
| |
before). |
| |
|
| |
Note that, while this offers better accuracy in indicating where the |
| |
stack depth changes, it will often report many intentional stack depth |
| |
changes (e.g., when an interpreted computation stretches across |
| |
several lines). You can suppress the checking of some lines by |
| |
putting backslashes at the end of these lines (not followed by white |
| |
space), and using |
| |
|
| |
@example |
| |
gforth depth-changes.fs -e "' most-lines is depth-changes-filter" my-file.fs |
| |
@end example |
| |
|
| @c ****************************************************************** |
@c ****************************************************************** |
| @node ANS conformance, Standard vs Extensions, Tools, Top |
@node ANS conformance, Standard vs Extensions, Tools, Top |
| @chapter ANS conformance |
@chapter ANS conformance |
| @item providing the String Extensions word set (another easy one) |
@item providing the String Extensions word set (another easy one) |
| @end itemize |
@end itemize |
| |
|
| |
Gforth has the following environmental restrictions: |
| |
|
| |
@cindex environmental restrictions |
| |
@itemize @bullet |
| |
@item |
| |
While processing the OS command line, if an exception is not caught, |
| |
Gforth exits with a non-zero exit code instyead of performing QUIT. |
| |
|
| |
@item |
| |
When an @code{throw} is performed after a @code{query}, Gforth does not |
| |
allways restore the input source specification in effect at the |
| |
corresponding catch. |
| |
|
| |
@end itemize |
| |
|
| |
|
| @cindex system documentation |
@cindex system documentation |
| In addition, ANS Forth systems are required to document certain |
In addition, ANS Forth systems are required to document certain |
| implementation choices. This chapter tries to meet these |
implementation choices. This chapter tries to meet these |
| @item conditions under which control characters match a space delimiter: |
@item conditions under which control characters match a space delimiter: |
| @cindex space delimiters |
@cindex space delimiters |
| @cindex control characters as delimiters |
@cindex control characters as delimiters |
| If @code{WORD} is called with the space character as a delimiter, all |
If @code{word} is called with the space character as a delimiter, all |
| white-space characters (as identified by the C macro @code{isspace()}) |
white-space characters (as identified by the C macro @code{isspace()}) |
| are delimiters. @code{PARSE}, on the other hand, treats space like other |
are delimiters. @code{Parse}, on the other hand, treats space like other |
| delimiters. @code{SWORD} treats space like @code{WORD}, but behaves |
delimiters. @code{Parse-name}, which is used by the outer |
| like @code{PARSE} otherwise. @code{Name}, which is used by the outer |
|
| interpreter (aka text interpreter) by default, treats all white-space |
interpreter (aka text interpreter) by default, treats all white-space |
| characters as delimiters. |
characters as delimiters. |
| |
|
| @cindex image file initialization sequence |
@cindex image file initialization sequence |
| @cindex initialization sequence of image file |
@cindex initialization sequence of image file |
| |
|
| You can add your own initialization to the startup sequence through the |
You can add your own initialization to the startup sequence of an image |
| deferred word @code{'cold}. @code{'cold} is invoked just before the |
through the deferred word @code{'cold}. @code{'cold} is invoked just |
| image-specific command line processing (i.e., loading files and |
before the image-specific command line processing (i.e., loading files |
| evaluating (@code{-e}) strings) starts. |
and evaluating (@code{-e}) strings) starts. |
| |
|
| A sequence for adding your initialization usually looks like this: |
A sequence for adding your initialization usually looks like this: |
| |
|
| (your turnkey application) that never returns; instead, it exits Gforth |
(your turnkey application) that never returns; instead, it exits Gforth |
| via @code{bye} or @code{throw}. |
via @code{bye} or @code{throw}. |
| |
|
| @cindex command-line arguments, access |
You can access the (image-specific) command-line arguments through |
| @cindex arguments on the command line, access |
@code{argc}, @code{argv} and @code{arg} (@pxref{OS command line |
| You can access the (image-specific) command-line arguments through the |
arguments}). |
| variables @code{argc} and @code{argv}. @code{arg} provides convenient |
|
| access to @code{argv}. |
|
| |
|
| If @code{'cold} exits normally, Gforth processes the command-line |
If @code{'cold} exits normally, Gforth processes the command-line |
| arguments as files to be loaded and strings to be evaluated. Therefore, |
arguments as files to be loaded and strings to be evaluated. Therefore, |
| @code{'cold} should remove the arguments it has used in this case. |
@code{'cold} should remove the arguments it has used in this case. |
| |
|
| |
|
| |
|
| doc-'cold |
doc-'cold |
| doc-argc |
|
| doc-argv |
|
| doc-arg |
|
| |
|
| |
|
| |
|
| @c ****************************************************************** |
@c ****************************************************************** |
| @node Engine, Cross Compiler, Image Files, Top |
@node Engine, Cross Compiler, Image Files, Top |