A brief introduction to the TCL Scripting Language

Matthew Scarpino

October 02, 2009

Matthew ScarpinoOctober 02, 2009

There's no getting around it. If you intend to use IBM's Full System Simulator (the topic of a five part series on Embedded.com) for nontrivial simulation, you need to know Tcl, the Tool Command Language.

In the past,Tcl made me very uncomfortable: It's wordy like a regular high-level language, but the words have no structure—no commas, parentheses, semicolons, or equal signs.

But as I learned Tcl, I realized that its bothersome aspects were really strengths. I've since developed a great deal of respect for the language.

This article isn't going to present Tcl in full. The goal is to explain enough of the topic so that you can read and write scripts that take full advantage of SystemSim's capabilities.

Throughout this treatment, I'll do my best to compare Tcl to the C language that we're familiar with. One application is based on the Sieve of Eratosthenes code in the five part series on Embedded.com. . But first, let's start with a simple Hello World!-type script.

Introducing Tcl
The AppendixE folder available on line with the code discussed here has no subfolders, just two files: square.tcl and sieve.tcl. Change to this directory and execute the following command:

tclsh square.tcl

This script computes five squared and returns Result is 25!. Listing E.1 below presents the content of square.tcl

Listing E.1 Basic Tcl: square.tcl

This script might not look impressive, but it provides a wealth of information about Tcl:

1) set assigns values to variables, expr receives expressions and returns a value, and puts prints strings to the console.

2) Simple syntax: commands don't have to be followed by semicolons, arguments aren't enclosed in parentheses or separated by commas, no equals sign in assignment statements.

3) Variables are untyped, variable names can start with a number, and string constants are enclosed in double quotes.

4) A variable's value is referenced by preceding the variable name with $, and variables can be referenced directly inside a string.

5) Tcl comments start with #.

6) The result of a command (for example, expr 5*5) can be accessed like a variable if surrounded by square brackets.

7) No main function or any specifically marked starting point; Tcl commands are interpreted one command after the next.

This last point is important. Every line in a Tcl script is a command followed by zero or more space-separated arguments. Nearly every command returns a value, and all values are processed as strings.

For example, expr doesn't know or care that $num holds a numeric value; all it knows is that 5 * 5 is a string that can be evaluated. It performs the evaluation and returns the result as a new string: 25.

If the code in Listing E.1 makes sense, the rest of this discussion won't provide any difficulty. The next sections present higher-level constructs in Tcl: conditional statements, lists, loops, and procedures.

Tcl control structures and aggregates operate like those in C/C++, but the syntax differs in many respects. This section presents Tcl conditional statements, lists, arrays, and loops. Throughout the discussion, keep in mind that every Tcl statement is a command and that Tcl syntax relies on curly braces rather than parentheses.

< Previous
Page 1 of 4
Next >

Loading comments...

Most Commented