Version 0 of the empty command

Updated 2015-02-17 13:37:48 by aspect

Rule #1 of the Dodekalogue states that a script consists of one or more commands. This is a bit counterintuitive, since the empty script is obviously a command:

% eval {}

While it doesn't have much effect in practice, the way the Tcl interpreter is structured means that the empty script is made up of exactly one command, which has no words.

To justify this requires digging into the Tcl parser, which is kinda fun anyway, so let's have a look.

In short: Tcl_EvalEx repeatedly calls Tcl_ParseCommand, which fills in a Tcl_Parse structure with information about the next command in the script. The relevant parts of this structure here are commentStart, commentSize, commandStart, commandSize and numWords. Thus, we can infer that each call to Tcl_ParseCommand consumes a leading comment and a single command from the input. In the case of the empty script (or a script containing only comments and whitespace), commandSize and numWords are both 0, so the condition if (parsePtr->numWords > 0) is false and Tcl_EvalEx skips evaluation, advances to the end of the script and finishes the loop.

So, it seems the empty script contains one command, which is empty. Commands (like the empty command) which contain no words are parsed, but not evaluated. How much this knowledge is worth? I don't know.

You can observe some of this behaviour at the script level with tclparser.