| www.delorie.com/gnu/docs/gforth/gforth_23.html | search |
![]() Buy GNU books! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You should put a correct stack effect on every definition, even if it is
just ( -- ). You should also add some descriptive comment to
more complicated words (I usually do this in the lines following
:). If you don't do this, your code becomes unreadable (because
you have to work through every definition before you can undertsand
any).
swap can be written like this: x1 x2 --
x2 x1. Describe the stack effect of -, drop, dup,
over, rot, nip, and tuck. Hint: When you
are done, you can compare your stack effects to those in this manual
(see section Word Index).
Sometimes programmers put comments at various places in colon definitions that describe the contents of the stack at that place (stack comments); i.e., they are like the first part of a stack-effect comment. E.g.,
: cubed ( n -- n^3 ) dup squared ( n n^2 ) * ; |
In this case the stack comment is pretty superfluous, because the word is simple enough. If you think it would be a good idea to add such a comment to increase readability, you should also consider factoring the word into several simpler words (see section Factoring), which typically eliminates the need for the stack comment; however, if you decide not to refactor it, then having such a comment is better than not having it.
The names of the stack items in stack-effect and stack comments in the standard, in this manual, and in many programs specify the type through a type prefix, similar to Fortran and Hungarian notation. The most frequent prefixes are:
n
u
c
f
false or true.
a-addr,a-
c-addr,c-
xt
w,x
d
ud
r
You can find a more complete list in 5.1 Notation.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |