version 1.140, 2005/07/27 10:23:10
|
version 1.141, 2005/10/02 11:30:33
|
Line 1294 You can print the top of stack element w
|
Line 1294 You can print the top of stack element w
|
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 |
Line 1327 operands:
|
Line 1327 operands:
|
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}: |
|
|
Line 1339 To change the sign, use @code{negate}:
|
Line 1339 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}. |
|
|
Line 1380 Two more stack manipulation words are:
|
Line 1380 Two more stack manipulation words are:
|
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. |
|
|
Line 1398 Given: How do you get:
|
Line 1398 Given: How do you get:
|
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}. |
|
|
Line 1529 it in other definitions:
|
Line 1529 it in other definitions:
|
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}. |
|
|
Line 1582 more complicated words (I usually do thi
|
Line 1582 more complicated words (I usually do thi
|
you have to work through every definition before you can understand |
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 |
Line 1643 Float (on the FP stack)
|
Line 1643 Float (on the FP 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 |
Line 1823 into stack trouble. However, I recommen
|
Line 1823 into stack trouble. However, I recommen
|
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}. |
|
|
Line 1881 You can optionally use an @code{else}-pa
|
Line 1881 You can optionally use an @code{else}-pa
|
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}. |
|
|
Line 1958 operation of the Boolean operations to a
|
Line 1958 operation of the Boolean operations to a
|
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}. |
Line 2036 Here's a loop with an exit at the end:
|
Line 2036 Here's a loop with an exit at the end:
|
@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}. |
|
|
Line 2082 You can access the counter of a counted
|
Line 2082 You can access the counter of a counted
|
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: |
|
|
Line 2144 For recursive definitions you can use @c
|
Line 2144 For recursive definitions you can use @c
|
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}. |
|
|
Line 2244 You cannot mix using locals and using th
|
Line 2244 You cannot mix using locals and using th
|
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}. |
|
|
Line 2308 .
|
Line 2308 .
|
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: |
|
|
Line 2416 type
|
Line 2416 type
|
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}. |
|
|
Line 2575 Likewise, you can put that into definiti
|
Line 2575 Likewise, you can put that into definiti
|
: 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 |
Line 2789 exception, the stacks have the same dept
|
Line 2789 exception, the stacks have the same dept
|
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 |
Line 2955 foo .
|
Line 2955 foo .
|
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}. |
|
|
Line 3056 You can define @code{ENDIF} in this way:
|
Line 3056 You can define @code{ENDIF} in this way:
|
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 |
|
|
Line 3096 number computed at compile time into the
|
Line 3096 number computed at compile time into the
|
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 |
|
|
Line 3269 order
|
Line 3269 order
|
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 |
Line 5096 doc-search
|
Line 5096 doc-search
|
doc--trailing |
doc--trailing |
doc-/string |
doc-/string |
doc-bounds |
doc-bounds |
|
doc-pad |
|
|
@comment TODO examples |
@comment TODO examples |
|
|
Line 8872 For ways of storing character strings in
|
Line 8872 For ways of storing character strings in
|
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->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 |