ficlPageHeader("local variables in Ficl") ficlAddToNavBarAs("Locals") def entry(definition, description): print "
Ficl provides excellent support for local variables, and the purists be damned—we use 'em all the time.
Local variables can only be declared inside a definition, and are only visible in that definition. Please refer to the ANS standard for FORTH for more general information on local variables. ficlHeader1("John-Hopkins Forth Argument Syntax") ?> ANS Forth does not specify a complete local variable facility. Instead, it defines a foundation upon which to build one. Ficl comes with an adaptation of the Johns-Hopkins local variable syntax, as developed by John Hayes et al. However, Ficl extends this syntax with support for double-cell and floating-point numbers.
Here's the basic syntax of a JH-local variable declaration:
{ arguments
| locals
-- ignored
}
(For experienced FORTH programmers: the declaration is designed to look like a stack comment,
but it uses curly braces instead of parentheses.) Each section must list zero or more
legal Ficl word names; comments and preprocessing are not allowed here.
Here's what each section denotes:
-- and } are treated as a comment, and ignored.
| and -- sections are optional,
but they must appear in the order shown if they appear at all.)
ficlHeader2("Argument Types") ?> Every time you specify a local variable (in either the arguments or the locals section), you can also specify the type of the local variable. By default, a local variable is a single-cell integer; you can specify that the local be a double-cell integer, and/or a floating-point number.
To specify the type of a local, specify one or more of the following single-character specifiers,
followed by a colon (:).
f2:foo would specify a double-width floating-point
number.
The type specifiers are read right-to left, and when two specifiers conflict, the rightmost
one takes priority. So 2is1f2:foo would still specifiy a double-width floating-point
number.
Note that this syntax only works for Ficl's JH-locals. Locals
defined in some other way (say, with the FORTH standard word LOCALS|)
will ignore this syntax, and the entire string will be used as the name of
the local (type and all).
ficlHeader2("A Simple Example") ?>
: DEMONSTRATE-JH-LOCALS { c b a f:float -- a+b f:float*2 }
a b +
2.0e float f*
;
ficlPageFooter()
?>