GEOS SDK TechDocs
|
|
4 Syntax and Structure
|
4.2 Expressions
The Tcl language has syntactic similarities to both Unix and Lisp. However, the interpretation of commands is different in Tcl than in either of those other two systems. A Tcl command string consists of one or more commands separated by newline characters. Each command consists of a collection of fields separated by white space (spaces or tabs). The first field must be the name of a command, and the additional fields, if any, are arguments that will be passed to that command. For example, the command:
var a 22
has three fields: the first,
var
, is the name of a Tcl command, and the last two,
a
and
22
, will be passed as arguments to the
var
command. The command name may refer to a built-in Tcl command, an application specific command, or a command procedure defined with the built-in
proc
command. Arguments are passed literally as text strings. Individual commands may interpret those strings in any fashion they wish. The
var
command, for example, will treat its first argument as the name of a variable and its second argument as a string value to assign to that variable. For other commands, arguments may be interpreted as integers, lists, file names, or Tcl commands.
If the first non-blank character in a command is
#
(a number sign), then everything from the # up through the next newline character is treated as comment and discarded by the parser.
Normally each argument field ends at the next white space (tabs or spaces), but curly braces ("{" and "}") may be used to group arguments in different ways. If an argument field begins with a left brace, then the argument is not terminated by white space; it ends at the matching right brace. Tcl will strip off the outermost layer of braces before passing the argument to the command. For example, in the command:
var a {b c}
the
var
command will receive two arguments:
a
and
b c
. The matching right brace need not be on the same line as the left brace; in this case the newline will be included in the argument field along with any other characters up to the matching right brace. In many cases an argument field to one command consists of a Tcl command string that will be executed later; braces allow complex command structures to be built up without confusion. For example, the
eval
command takes one argument, which is a command string;
eval
invokes the Tcl interpreter to execute the command string. The command:
eval {
var a 22
var b 33
}
will assign the value 22 to
a
and 33 to
b
.
Tcl braces act like quote characters in most other languages, in that they prevent any special interpretation of the characters between the left brace and the matching right brace.
When an argument is in braces, then command, variable, and backslash substitutions do not occur in the normal fashion; all Tcl does is to strip off the outer layer of braces and pass the contents to the command. Braces are only significant in a command field if the first character of the field is a left brace. Otherwise neither left nor right braces in the field will be treated specially (except as part of variable substitution).
Normally, each command occupies one line (the command is terminated by a newline character). Thus, the string:
var a 22 var b 33
will be interpreted as two separate commands. However, brackets may be used to group commands in ways other than one-command-per-line. If the first character of a command is an open bracket, then the command is not terminated by a newline character; instead, it consists of all the characters up to the matching close bracket. Newline characters inside a bracketed command are treated as white space (they will act as argument separators for arguments that are not enclosed in braces). For example, the string:
[var a 22] [var b 33]
will have the same effect as the previous example.
If an open bracket occurs in any of the fields of a command, then command substitution occurs. All of the text up to the matching close bracket is treated as a Tcl command and executed immediately. The result of that command is substituted for the bracketed text. For example, consider the command:
var a [var b]
When the
var
command has only a single argument, it is the name of a variable and
var
returns the contents of that variable. In this case, if variable
b
has the value
test
, then the command above is equivalent to the command:
var a test
Brackets can be used in more complex ways. for example, if the variable
b
has the value
tmp
and the variable
c
has the value
val
, then the command:
var a test[var b].[var c]
is equivalent to the command:
var a testtmp.val
If a field is enclosed in braces then the brackets and the characters between them are not interpreted specially; they are passed through to the argument verbatim.
The dollar sign ($) may be used as a special shorthand form for substituting variables. If $ appears in an argument that is not enclosed in braces then variable substitution will occur. The characters after the $, up to the first character that is not a number, letter, or underscore, are taken as a variable name and the string value of that variable is substituted for the name. Or, if the dollar sign is followed by an open curly brace, then the variable name consists of all the characters up to the next close curly brace. For example, if variable
outfile
has the value
test
, then the command:
var a $outfile.c
is equivalent to the command:
var a test.c
and the command:
var a abc${outfile}tmp
is equivalent to the command:
var a abctesttmp
Variable substitution does not occur in arguments that are enclosed in braces: the dollar sign and variable name are passed through to the argument verbatim.
The dollar sign abbreviation is simply a shorthand form.
$a
is completely equivalent to
[var a]
; it is provided as a convenience to reduce typing.
Backslashes may be used to insert non-printing characters into command fields and also to insert braces, brackets, and dollar signs into fields without them being interpreted specially as previously described. The backslash sequences understood by the Tcl interpreter are listed below. In each case, the backslash sequence is replaced by the given character.
For example, in the command:
var a \{x\[\ yz\141
the second argument to
var
is
{x[ yza
(note the <space> as part of the argument).
If a backslash is followed by something other than one of the options listed below, then the backslash is transmitted to the argument field without any special processing, and the Tcl scanner continues normal processing with the next character. For example, in the command:
var \*a \\\{test
the first argument will be
\*a
and the second
\{test
.
If an argument is enclosed in braces, then backslash sequences inside the argument are parsed but no substitution occurs. In particular, backslashed braces are not counted in locating the matching right brace that terminates the argument. for example, in the command:
var a {\{abc}
the second argument to var will be
\{abc
.
The backslash mechanism is not sufficient to generate any argument structure; it only covers the most common cases. To produce particularly complicated arguments it will probably be easiest to use the
format
command along with command substitution.
GEOS SDK TechDocs
|
|
4 Syntax and Structure
|
4.2 Expressions