the empty command

This page resulted from a discussion on comp.lang.tcl, starting 2015-02-12 titled "Definition of 'Tcl Script'" . NOTE: if anyone has a more resilient link for usenet discussion than Google, please substitute it here.

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

% eval {}

So this is "at least one command", yet no commands are executed. What's going on?

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.

Short of reaching into the parser, the empty command seems to be unobservable: