The Tcl compiler uses a preparsing method, as described above, to output a tree-like structuring of expressions and commands. At the roots are the individual top level script statements, which include only those commands not nested in cmdlists within control structures. For example, all commands found within the arglists for commands like proc, if, etc. cannot be roots of the tree, although the proc, etc. commands can be, if they themselves are not nested.
The node types for this tree include notations for nested commands ([ ... ]), concatenated expressions (ie. $a$b), etc. An example follows:
Source Code
# this is a comment set a 5 # concatenate the returns of the two nested # commands and pass it as an arg to foo. foo [bar set][foo {5}]
Expression Dictionary (Parse Tree)
1. cmd ``set''
2. constant ``a''
3. constant ``5''
4. cmd ``foo''
5. cmd ``bar''
6. constant ``set''
7. nested statement [2 args][#5][#6]
8. nested statement [2 args][#4][#3]
9. concat [2 args][#7][#8]
Top Level Command List (Tree Roots)
[3 args][#1][#2][#3][2 args][#4][#9]
Notes: ``#n'' refers to a reference to item number n. The compiler detects multiple uses of the same constants and other primitive objects, and reuses these entries. For example, the entry for the foo command (#4) is reused. This system is also able to recognize the difference between commands and static strings, where the command name is the first argument. It also strips the curly braces from the static string in the third line of code. The concatenation and nested commands were discovered at compile time. Lastly, comments have been stripped.