Chapter 8. Scripting SpecTcl

Since the command language for SpecTcl is already built on top of a scripting language, it is natural to think of extending SpecTcl to some extent via scripting. This chapter will introduce SpecTcl scripting, and describe a few strategies for handlig specific scripting problems that are frequently encountered.

The map for the material in this chapter is:

8.1. An introduction to scripting with Tcl/Tk

If you understand scripting with Tcl/Tk you will understand 90% of scripting with SpecTcl. It is therefore only natural that the starting point of this chapter is some background in scripting with Tcl/Tk.

Tcl syntax rules. For a complete and powerful programming language, Tcl has surprisingly few syntax rules. Eleven to be precise with the Tcl Core team wrangling over whether or not it's time to add a 12'th .

Much of Tcl's power and ease of use comes from the fact that it does have very few rules, and that these rules are applied with no exceptions. The 11 rules of Tcl are:

  1. Commands A Tcl script is a string containing one or more commands. Semicolons and newlines are command separators unless quoted as described below. Close brackets are command terminators during command substitution (see below) unless quoted.

  2. Evaluation A command is evaluated in two steps. First, the Tcl interpreter breaks the command into words and performs substitutions as described below. These substitutions are performed in the same way for all commands. The first word is used to locate a comamand procedure to carry out the command, then all of the words of the command are passed to the command procedure. The command procedure is free to interpret each of its words in any way it likes, such as an integer, variable name, list, or Tcl script. Different commands interpret their words differently.

  3. Words Words of a command are separated by white space (except for newlines, which are command separators).

  4. Double quotes. If the first character of a word is double-quote () then the word is terminated by the next double-quote character. If semi-colons, close brackets, or white space characters (including newlines) appear between the quotes then they are treated as ordinary characters and included in the word. Command substitution, variable substitution, and backslash substitution are performed on the characters between the quotes as described below. The double-quotes are not retained as part of the word.

  5. Braces If the first character of a word is an open brace ({) then the word is terminated by the matching close brace (}). Braces nest within the word: for each additional open brace there must be an additional close brace (however, if an open brace or close brace within the word is quoted with a backslash then it is not counted in locating the matching close brace). No substitutions are performed on the characters between the braces except for backslash-newline substitutions described below, nor do semi-colons, newlines, close brackets, or white space receive any special interpretation. The word will consist of exactly the characters between the outer braces, not including the braces themselves.

  6. Command substitution If a word contains an open bracket ([) then Tcl performs command substitution. To do this it invokes the Tcl interpreter recursively to process the characters following the open bracket as a Tcl script. The script may contain any number of commands and must be terminated by a close bracket (]). The result of the script (i.e. the result of its last command) is substituted into the word in place of the brackets and all of the characters between them. There may be any number of command substitutions in a single word. Command substitution is not performed on words enclosed in braces.

  7. Variable substitution If a word contains a dollar-sign ($) then Tcl performs variâ[m able substitution: the dollar-sign and the following characters are replaced in the word by the value of a variable. Variable substitution may take any of the following forms:

    $name

    Name is the name of a scalar variable; the name is a sequence of one or more characters that are a letter, digit, underscore, or namespace separators (two or more colons).

    $name(index)

    Name gives the name of an array variable and index gives the name of an element within that array. Name must contain only letters, digits, underscores, and namespace separators, and may be an empty string. Command substitutions, variable substitutions, and backslash substitutions are performed on the characters of index.

    ${name}

    Name is the name of a scalar variable. It may contain any characters whatsoever except for close braces.

    There may be any number of variable substitutions in a single word. Variable substitution is not performed on words enclosed in braces.

  8. Backslash substitution If a backslash (\) appears within a word then backslash substitution occurs. In all cases but those described below the backslash is dropped and the following character is treated as an ordinary character and included in the word. This allows characters such as double quotes, close brackets, and dollar signs to be included in words without triggering special processing. The following table lists the backslash sequences that are handled specially, along with the value that replaces each sequence.

    \a

    Audible alert (bell) (0x7).

    \b

    Backspace (0x8).

    \f

    Form feed (0xc).

    \n

    Newline (0xa).

    \r

    Carriage-return (0xd).

    \t

    Tab (0x9).

    \v

    Vertical tab (0xb).

    \<newline>whiteSpace

    A single space character replaces the backslash, newline, and all spaces and tabs after the newline. This backslash sequence is unique in that it is replaced in a separate pre-pass before the command is actually parsed. This means that it will be replaced even when it occurs between braces, and the resulting space will be treated as a word separator if it isnât in braces or quotes.

    \\

    Backslash (\).

    \ooo

    The digits ooo (one, two, or three of them) give an eight-bit octal value for the Unicode character that will be inserted. The upper bits of the Unicode character will be 0.

    \xhh

    The hexadecimal digits hh give an eight-bit hexadecimal value for the Unicode character that will be inserted. Any number of hexadecimal digits may be present; however, all but the last two are ignored (the result is always a one-byte quantity). The upper bits of the Unicode char acter will be 0.

    \uhhhh

    The hexadecimal digits hhhh (one, two, three, or four of them) give a sixteen-bit hexadecimal value for the Unicode character that will be inserted.

    Backslash substitution is not performed on words enclosed in braces, except for backslash-newline as described above.

  9. Comments If a hash character (#) appears at a point where Tcl is expecting the first character of the first word of a command, then the hash character and the characters that follow it, up through the next newline, are treated as a comment and ignored. The comment character only has significance when it appears at the beginning of a command.

  10. Order of substitution Each character is processed exactly once by the Tcl interpreter as part of creating the words of a command. For example, if variable substitution occurs then no further substitutions are performed on the value of the variable; the value is inserted into the word verbatim. If command substitution occurs then the nested command is processed entirely by the recursive call to the Tcl interpreter; no substitutions are performed before making the recursive call and no additional substitutions are performed on the result of the nested script.

    Substitutions take place from left to right, and each substitution is evaluated completely before attempting to evaluate the next. Thus, a sequence like

    
                     set y [set x 0][incr x][incr x]
                  
    will always set the variable y to the value, 012.

  11. Substitution and word boundaries Substitutions do not affect the word boundaries of a command. For example, during variable substitution the entire value of the variable becomes part of a single word, even if the variable's value contains spaces.

Let's take a look at some examples. Here's a simple command that illustrates the first three rules:

Example 8-1. A simple Tcl Command


set a 1234
                
This command consist of the three words set a and 1234. The first word set selects the actual comman processor. In this case, a command that sets the value of a variable a (the second command word) To the value of its third command word. After this command, the variable a has the value 1234.

One important Tcl paradigm has the acronym: EIAS (Everything Is A String). This means that 1234 is a string unless and until it is operated on by a command that requires it be a number (e.g. expr).

Let's look at quoting with double quotes:

Example 8-2. Tcl command with double quote quoting


set a "Mary had a little lamb"
                
This is still a three word command. Due to the use of quoting, the third word (and the final value of the variable a) is the entire string Mary had a little labm.

Using braces we can write the previous example as:

Example 8-3. Quoting with braces:


set a {Mary had a little lamb}
            

In this pair of simple examples, there is no difference between using double quotes or braces. Now let's look at command substition. the expr command computes the value of an expression:

Example 8-4. Command substitution in Tcl:


set a [expr 1 + 2 + 3]
            

This is still a three word command. The last word is the return value of the command expr 1 + 2 + 3. After this substitution, the set command sees:


set a 6
            

Recall that variable substitution can be triggered by the $ in front of the name of a variable.

Example 8-5. Variable substitution


set a 2
set b 4
set c [expr $a + $b]
            

Here's how things look to Tcl on the last command. Tcl detects that it needs to evaluate a command (expr $a + $b) and that the result of that command wil be the last word on the command. In evaluating the expr command it detecst first that it must substitute 2 for a, and 4 for b making the expr command: expr 2 + 4. The expr command is invoked and it returns 6 which makes the set command set c 6.

Let's look at backslash substitution. Recall that \n substitutes a new line. Here is one place where the difference between {} and "" substitution becomes evident:

Example 8-6. Backslash substitution


set a "First line \n second line"
set b {all on the \n same line}
            

The first example sets a to a string with a newline embedded so that First line and second line are separated by a newline. Since, braces inhibit substitution, the second example Simply puts a backslash followed by an n into the text.

Comments in Tcl can be rather tricky. Look at the following:

Example 8-7. Comments and the # character.


# this is a comment
set a #legal
            

The first line is clearly a comment. The second line is a bit counter intuitive. You might think this is going to display the value of a (what set does with only one aditional parameter

In fact, the comment rule states that # only starts a comment if it appears where the command word of the command is expected (you can almost think of # as a command that means "ignore this command"). Therefore, the second line in the example, sets a to #legal.

Let's take one last look at substitution. Consider:

Example 8-8. Tcl only does one round of substitution:


set a 1234
set b {$a}
set c $b
            

First recall that braces inhibit substitution. Therefore the value of b is $a. You might think that this would make the value of c 1234 ($b -> $a -> 1234). Tcl only does one round of substitution, however. Therefore c will have the value $a.

Common Tcl commands. Next lets look at a few of the common tcl commands. Specifically, variable manipulation and arrays, arithmetic commands, flow control commands, and lists.

Tcl recognizes two types of variables; scalars and arrays. We have already seen scalar variables in our examples. Arrays in Tcl are much more powerful than those in laguages like C, C++, Fortran. In Tcl arrays are collections of variables that are referenced via an index. While in most languages, indices are integers, in Tcl, arrays indices are strings. For example:

Example 8-9. Setting an array element


set capitals(Michigan) Lansing
                

The variable capitals is an array. It's element indexed by Michigan has been assigned the value Lansing. The command puts outputs a text string after the statement above,


puts $capitals(Michigan)
            
Prints Lansing.

Substitution can also work within array indices for example:


set state Michigan
puts $capitals($state)
            
also prints Lansing.

Using strings to index arrays will often remove the need to search for things. Array indexing is done by Tcl using hash tables and is therefore on the average constant time. See http://en.wikipedia.org/wiki/Hash_tables for information about hash tables.

While EIAS in Tcl, often variables have strings that can be interpreted as numeric values, and you may want to do some computing on those values. The two most common commands for computing on numeric data are expr and incr.

The expr command takes its remaining command words and treats them as an arithmetic expression. The result of the command is the evaluation of the expression. The expr command allows a level of indirection by requesting an additional round of substitution on its command words. This can be expensive if the expression is evaluated frequently and does not require this extra substitution. It is therefore common for expr to be given a braced list to avoid the initial interpreter substitutution.

Example 8-10. Substituion in expr


set a 12
set b 21

set c [expr $a + $b]
set d [expr {$a + $b}]

set e {$b}
set f [expr $a + $e]
            

In the example above, c and d will have the same values. Because of the double substitution, the last command will assign 33 to f

The incr command requires one or two additional command words. The first word is a variable that must have a value that can be converted to an integer. If there is no third command word, the variable is incremented by 1 if there is a third word it must be an integer and is the amount the variable is incremented by. The incr command returns the result of the increment.

Example 8-11. The incr command


set a 0
set b [incr a]
set c [incr a 5]
            

b is assigned the value 1. c is assigned the value 6. At the end of the example, the value of a is also 6.

Let's examine how Tcl implements flow control. Remember that the language does not have any explicit flow control syntax. Flow control is just handled by commands that treat one or more of their command line words as scripts that may be evaulated.

Consider the following:

Example 8-12. The if command


set x 10
if {$x < 100} {
    set y 10
} else {
   set y 20
   set z 15
}
            

Looking at the previous example, it's hard to think of the \ if as a command. Let's apply the rules of Tcl to figure out the command words. The first word if is easy. This means the command will process a conditional. The second word is $x < 100 and is evaluated as an expression. Nonzero results make the condition for the if command tru, and zero results make the condition false.

The third word, using the rules for braced quotation is the script set y 0. The fourth word is else this is optional and if present, means there will be a fnial fifth word, a script:


set y 20
set z 15
            
The if command evaluates its second word. If true, The tcl interpreter is asked to execute the script that is the third word. If there is a fourth word else, the fifth word is executed as a script. The if command also supports the elseif keyword which allows you to provide an additional condition and script to evaluate if that statement is true. As many elseif's can be used as needed.

In the example above, y is given the value 10, and z is not set.

While the command looks like a statment construct very similar to C's if statement. There are enough differences that the following intuitive constructions are all errors:

Example 8-13. Bad if constructions


if {$x < 12 } set a $b

if {$x < 12 } {
  set a $b
}
else {
  set c $d
}
            

the first command in the block is an error because the script must be one word and it is not. Enclosing the script set a $b in braces corrects this example. The second example is an error because the if command is ended after the close brace due to the newline. The interpreter will complain that there is no command named else.

Tcl has several other flow control operations. I'm going to present two looping constructions, and call it quits here. You may want to look up the Tcl switch command for another flow control operation. It is also possible to create your own flow control operations.

Example 8-14. while


set a 0
while {$a < 10} {
    dosomething
    incr a
}
            

The while command executes its 3'd word as a script repeatedly as long as the expression that makes up its second word is true prior to executing the script.

The for command provides a simpler way to express most counted loops:

Example 8-15. for


for {set a 0} {$a < 10} {incr a} {
    dosomething
}
            

The for command requires four additional command words. The first word is a script that is executed to initialize the loop. The second is an expression that is evaluated to determine if the fourth word should be executed as a script or, if false, the comand terminates. The third additional word is a script that is executed after each execution of the fourth word.

This example is identical to the while example.

You can organize your script into procedures. The proc command gives a parameterized block of code a name and makes that name known as a command to the interpreter. If you define a proc that has the same name as an existing command or proc, the existing command or proc is overridden.

Example 8-16. Tcl proc commands


proc sum3 {a b c} {
    return [expr $a + $b +c]
}

set a [sum3 1 2 3]
set b [sum3 5 6 7]
set d [sum3 $a $b 8]
            

The proc command requires a word that names the proc, a word that provides formal parameters and a word that is a script that is executed when the proc is invoked. As we can see, a proc is invoked just like any other command. Any additional command words after the procname are assigned to the formal parameters before the proc is called.

In the example above, a is assigned the value 6. b the value 18. c is therefore 6 + 18 + 8 or 32.

I'm going to close this brief description of the common Tcl commands by introducing one possible type of value that a variable can hold. Let's go back to our quoting example:


set a {Mary had a little lamb}
            
The value of a is itself a list of words in the same sense that a Tcl command is a list of words. Tcl calls such strings lists. Tcl provides several commands that can manipulate lists.

Lists serve as Tcl's mechanism for structuring data. Lists can also be neste for example:


set a {{Mary had a little lamb} {whose fleece was white as snow}}
            
a is a two element list. The first element is itself a list Mary had a little lamb as is the second element of the list whose fleece was white as snow.

Here are a few of the commands that Tcl supplies to work on lists:

lappend variable element...

Appends each element to the variable as a list element. If necessary, elements are quoted to ensure each element is one list element in the target list, e.g "a b c" is appended as {a b c}. The lappend command will create variable if it does not already exist.

llength list

Returns the number of elements in list

lindex list index

Returns one element of list selected by index. The index is an integer. 0 selects the first list element, 1 the second and so forth. The special index end selects the last element.

lsearch ?options? list pattern

Returns the index (suitable for use in lindex) of the element of list that matches the search pattern. The optional ?options? clarify how the pattern is to be applied. Look at one of the Tcl references for more information.

eval list1 ...

Takes the elements of all of the lists passed to it and executes it as a Tcl command. e.g.:


set command {set a}
set value   1234
eval $command $value
                        
is the same as

set a 1234
                        

One final command; source: source takes a filename as a parameter and executes the Tcl script in that file.

Tcl "reading" list. Here's a short list of Tcl resources. Some online, some tutoring programs, some books you can get at Amazon or borrow from your office mate if you're lucky.

Tcl Reading list

Books

Brent Welch, Ken Jones, and Jeffrey Hobbs, 2003, 0-13-038560-3, Prentice Hall, Practical Programming in Tcl and Tck.

John Ousterhout, 1994, 0-13-978-0201633375, Addison-Wesley Professional Computing Series, Tcl and the Tk Toolkit.

Clif Flynt, 2003, 0-13-978-1558608023, Morgan Kaufmann, Tcl/Tk A Developers Guide.

Online resources

The Tclers Wiki.

TclTutor, Clif Flynt.

Tcl Programming, Richard Suchenwirth.

Online Tcl and Tk tutorials.