[ RfDs/CfVs | Other proposals ]
Problem
SolutionBase versionThe following syntax for locals is proposed. The sequence:declares local arguments, local values, and dummy outputs. Local arguments and local values both behave as locals, they differ only in respect to their initialisation. The local arguments are automatically initialised from the data stack on entry, the rightmost being taken from the top of the data stack. Local arguments and local values can be referenced by name within the word during compilation. The output names are dummies to allow a local declaration to be read as a stack comment.
-- to :} section is optional, i.e., |
or {: direct to :} is permitted.
The outputs are provided in the notation so that complete stack
comments can be produced. However, all text between --
and :} is ignored. This facility is there to permit the
notation to form a complete stack comment, which eases documentation.
Local arguments and values return their values when referenced,
and must be preceded by TO to perform a store.
In the example below, a and b are local arguments,
and c and d are local values.
: foo {: a b | c d -- :} a b + to c a b * to d cr c . d . ; Local types and extensionsAlthough out of the scope of this proposal, it should be noted that some current Forth systems use indicators to define local values of sizes other than a cell. To avoid issues when porting code to such systems, names ending in a ': ' (colon) should be avoided.
At least one Forth implementation uses local value names ending in the ': foo {: a b | F: f1 F: f2 -- c :} ... ; [ ' character to indicate local buffers. This character should
be avoided to prevent disfranchising implementations that implement
the behaviour. For similar reasons the use of non-alphabetic single
character local names should also be avoided.
DiscussionThe phrase{ ... } rather than
{: ... :} has been used by systems for
over 25 years. However, it conflicts with existing practise in other
Forth systems. Choosing the new name allows both practises to coexist.
The '| ' (ASCII $7C) character is widely used as the separator between
local arguments and local values. Other characters accepted in current
Forth implementations are '\ ' (ASCII $5C) and '¦ ' ($A6). We propose
only to consider the '| ' character further. Only recognition of the
'| ' separator is mandatory.
Some current systems permit TO to be used with floats (children of
FVALUE ) and other data types. Such systems often provide additional
operators such as +TO (add from stack to item) for children of
VALUE and FVALUE .
ProposalIn order to facilitate the use of BNF in the{: definition it is
necessary to move the BNF definition added to 2.2.1 Numeric notation
by the X:number-prefix proposal to a more general description in
section 2.2 Notation.
The following notation is used to define the syntax of the various elements within the document:13.3.3.1 Compilation semantics Replace "at least eight locals" in the last paragraph with "at least sixteen locals". 13.3.3.2 Syntax restrictions Replace item f) A program that declares more than eight locals in a single definition has an environmental dependency;with A program that declares more than sixteen locals in a single definition has an environmental dependency;13.4.1.2 Ambiguous conditions Add the following ambiguous conditions:
13.6.2.xxxx {: brace-colon LOCAL EXT
|
0 [if]
BUILDLV c-addr u +n mode
When executed during compilation, BUILDLV passes a message to the
system identifying a new local argument whose definition name is
given by the string of characters identified by c-addr u. The size
of the data item is given by +n address units, and the mode
identifies the construction required as follows:
0 - finish construction of initialisation and data storage
allocation code. C-addr and u are ignored. +n is 0
(other values are reserved for future use).
1 - identify a local argument, +n = cell
2 - identify a local value, +n = cell
3+ - reserved for future use
-ve - implementation specific values
The result of executing BUILDLV during compilation of a definition
is to create a set of named local arguments and values, each of
which is a definition name, that only have execution semantics
within the scope of that definition's source.
Note that it is often useful to accumulate and store the size of
locals storage (0=none), so that compilers for EXIT can easily
determine if locals clean up code is required.
[then]
VARIABLE #LVS \ -- addr
\ Holds size of locals storage required.
: BUILDLV \ c-addr u +n mode --
\ Dummy for testing
OVER #LVS +!
CR 2SWAP TYPE SPACE SWAP . .
;
: TOKEN \ -- caddr u
\ Get the next space delimited token from the input stream.
\ Can be extended to permit multiple line declarations.
PARSE-NAME
;
: LTERM? \ caddr u -- flag
\ Return true if the string caddr/u is "--" or ":}"
2DUP S" --" COMPARE 0= >R
S" :}" COMPARE 0= R> OR
;
: LSEP? \ caddr u -- flag
\ Return true if the string caddr/u is the separator between
\ local arguments and local values or buffers.
2DUP S" |" COMPARE 0= >R
S" \" COMPARE 0= R> OR
;
: {: \ --
\ Parse the locals declaration up to the closing ":}".
0 #LVS ! \ indicate no locals yet
0 >R \ indicate arguments
BEGIN
TOKEN 2DUP LTERM? 0=
WHILE \ -- caddr len
2DUP LSEP? IF \ if '|'
R> DROP 1 >R \ change to vars and buffers
ELSE
R@ 0= IF \ argument?
CELL 1
ELSE \ value
CELL 2
THEN
BUILDLV
THEN
REPEAT
BEGIN
S" :}" COMPARE
WHILE
TOKEN
REPEAT
0 0 0 0 BUILDLV
R> DROP
; IMMEDIATE
: LT1 {: a b | c -- f :}
CR ." Hello1 " CR a ;
T{ 3 4 LT1 -> 3 }T \ Outputs Hello1
: LT2 {: a | b c e :}
CR ." Hello2 " CR ;
T{ 3 LT2 -> }T \ Outputs Hello2
: LT3 {: a b c -- :}
CR ." Hello3 " CR ;
T{ 3 4 5 LT3 -> }T \ Outputs Hello3
: LT4 {: a b c :}
CR ." Hello4 " CR ;
T{ 3 4 5 LT4 -> }T \ Outputs Hello4
: LT5 {: a | b c d -- e f g :}
CR ." Hello5 " CR
a 2* to b b 2* to c c 2* to d
b c d ;
T{ 3 LT5 -> 6 12 24 }T \ Outputs Hello5
\
", "|
",
":
" and "}
"
with the more general condition of using a single non-alphabetic
character as a local name.
:
" and "}
"
as ambiguous condition.{
and {:
.
Note that you can be both a system implementor and a programmer, so you can submit both kinds of ballots.