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:
=")
()")
[]$")
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.
Similarly one can use a Tcl variable as a generic function, as in:
% set f sqrt sqrt % [nap "f 9"] 3
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 an operand (now an 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
Multiple arguments are separated by commas in the usual way. For example:
% [nap "hypot(3,4)"] 5
A comma is treated as a low-precedence operator which produces a boxed array. The parentheses are needed to force the comma to be executed before the function. The fact that comma is just another operator is shown by:
% nap "both = 3,4" ::NAP::33-33 % $both all ::NAP::33-33 boxed MissingValue: 0 References: 1 Unit: (NULL) Dimension 0 Size: 2 Name: (NULL) Coordinate-variable: (NULL) Value: 31 32 % [nap "hypot both"] 5
Tcl array indices are enclosed by parentheses
("()"),
while C uses brackets
("[]").
NAP requires neither, since indexing is simply implied by adjacent operands (now 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.
Multiple function arguments and cross-product-indexing are both defined using a boxed argument. A boxed argument is normally produced using the comma operator. The comma operator has low precedence, so parentheses are normally needed for multiple function arguments and cross-product-indexing. The following example shows how an element of a matrix can be extracted using either full or cross-product indexing:
% nap "matrix = {
{2 3 4}
{5 6 7}
}"
::NAP::30-30
% [nap "matrix{0 2}"]; # Full index
4
% [nap "matrix(0,2)"]; # Cross-product index
4