nap command
The standard Tcl command "expr" is based on C
conventions for operators and functions.
NAP expressions use similar conventions and can include any
of the following tokens separated by white-space characters:
=")
()")
[]$")
If the nap command has multiple arguments then
these are concatenated.
Thus it is not always necessary to enclose the expression by
quote (")
characters, but this practice is recommended because it
{})
$[]) substitution within braces
Like
"expr",
nap does the Tcl substitution defined by any brackets and dollars
("[]$")
remaining after normal command parsing.
However, unlike
"expr",
nap also substitutes for Tcl variable names that are not preceded by a
"$"
(except where the name is the left operand of the assignment operator
"=").
The value of the Tcl name is treated as a NAP expression, which is evaluated
and the OOC-name of the result replaces the name.
This substitution is repeated (up to eight times) until a single OOC-name is
generated.
The expressions in the following example include:
length containing the string "3.5"
breadth defined by NAP to contain
"::NAP::13-13"
2 and 10
area containing the string
"length * breadth"
% set length 3.5 3.5 % nap "breadth = 2" ::NAP::13-13 % [nap "2 * (length + breadth)"] 11 % set area "length * breadth" length * breadth % [nap "10 * area"] 70
Each constant is replaced by the OOC-name of a NAO representing its value. After substitution, the expression consists of OOC-names, operators, function names and parentheses.
NAP allows names which include namespaces, as in:
% namespace eval ::mySpace {}; # create namespace "mySpace"
% nap "::mySpace::x = 8"
::NAP::13-13
% [nap "3 + ::mySpace::x"]
11
Function arguments can be enclosed by parentheses (as required by many other languages), but these parentheses are not required by the syntax. A name (which cannot be a Tcl variable name or it would have been substituted) followed by a OOC-name is treated as a function name. Thus the following two commands are equivalent:
% [nap "sin(3.14)"] 0.00159265 % [nap "sin 3.14"] 0.00159265
Tcl array indices are enclosed by parentheses
("()"),
while C uses brackets
("[]").
NAP requires neither, since indexing is simply implied by adjacent OOC-names.
Thus the following two commands
(which give elements 1, 0, 2 and 0 of the vector
{5 7 6})
are equivalent:
% [nap "{5 7 6}({1 0 2 0})"]
7 5 6 5
% [nap "{5 7 6}{1 0 2 0}"]
7 5 6 5
As explained above, there is no syntactic need for parentheses around single function arguments and array indices. However, since most other computer languages do require such parentheses, it may aid human readability to include them.