Overview of the Tcl Language

Tcl[#!tcl!#] is designed to address the need for a ``scripting'' language, providing high-level control over a program. The interface between Tcl and the running program consists of the Tcl runtime library, which is embedded into the C application code.

Tcl appears to the programmer as a syntactically simple combination of Lisp [#!lispcraft!#], Perl [#!perl!#] and the Unix Shell language [#!sh!#]. Like the Unix Shell, it supports nested commands, automatic concatenation, and newline-command-termination. From Lisp, it borrows `defun'-like syntax, default procedure arguments, and eval(). Like Perl, Tcl's only data type is the string.

In Tcl, every statement can be thought of as a function call, in the form of ``cmd arg arg arg'', where the `cmd' pseudo-function is the first whitespace-separated argument and the args are the various whitespace-separated items that follow it. Square-brackets denote nested commands; dollar-signs indicate variable substitution; curly-braces serve to group text into a single argument, without performing substitution.

An example follows:

# comments start with a '#' character.
# sets variable a to value "5"
#   (the string, not the number!)
set a 5
  
# sets b to the string "10".
# The 'expr' command converts the string
#   "5" to the number 5, (it is not
#   performed by the interpeter)
set b [expr $a+5]
  
# sets c to "5.510"
set c $a.$a$b
  
# outputs the string "5".
puts stdout $a
  
# define a new function
#  note default argument of '1'.
# 'proc' is a command taking "fact", 
#  "{n 1}" and "if ... " as arguments
#  (curlies are stripped by the interpreter).
proc fact { {n 1} } {
  if {$n <= 1} {
    return 1;
  } else {
    return [expr $n*[fact [expr $n-1]]];
  }
}
  
# "foo" is called with args "bar", "7"
foo bar 7

One of the most interesting features of Tcl is its ability to be embedded and extended through its C language library. From the C programmer's perspective, the Tcl interpreter is treated as an instantiable object (embedded), which is passed the contents of the script to be run. This object exists as a data structure in C working in tandem with a C function library. The Tcl runtime library exposes all of the core commands to the C programmer directly, so Tcl statements may be executed as C function calls to routines provided by the library. This library is identical to the one that Tcl uses to evaluate statements in a script. Thus, programs like wish are nothing more than main loops which collect user input and pass it one statement at a time to a routine called Tcl_Eval().

Tcl can also be extended from the C program by registering commands with the Tcl interpreter. The interpreter will call back to your C code when and if that Tcl command is executed. In fact, this is how all of the core language commands are implemented. In the above example, the set command is nothing more than a registered callback whose function pointer is the library function which handles set - the very same one which is available to the C language programmer directly! The last statement in the example, ``foo'' doesn't exist in the default Tcl language - it is either a call to a procedure defined in Tcl itself using the proc mechanism, or a callback to a C function defined by the user, where the arguments ``bar'' and ``7'' are passed to it, in their string forms.