This is the set of 13 rules that define the Cloverfield project language. For comparison, see Tcl's Dodekalogue.
The following rules define the syntax and semantics of the <Cloverfield> language:
A <Cloverfield> script is a string containing one or more commands. Semi-colons and newlines are command separators unless quoted as described below. Close brackets are command terminators during command substitution (see below) unless quoted.
A command is evaluated in two steps. First, the <Cloverfield> interpreter breaks the command into words and performs substitutions as described below. These substitutions are performed in the same way for all commands. The first word is used to locate a command procedure to carry out the command, then all of the words of the command are passed to the command procedure. The first word is implicitly prefixed by the argument expansion word modifier described in rule [11], and is recursively flattened until it forms an atom (ie does not form a valid list with more than one element). For instance, “{{{cmd a b} c d} e f} g h” is equivalent to “cmd a b c d e f g h”. The command procedure is free to interpret each of its words in any way it likes, such as an integer, variable name, list, or <Cloverfield> script. Different commands interpret their words differently.
Words of a command are separated by white space (except for newlines, which are command separators).
If the first character of a word is double-quote (“"”) then the word is terminated by the next double-quote character. If semi-colons, close brackets, or white space characters (including newlines) appear between the quotes then they are treated as ordinary characters and included in the word. Command substitution, variable substitution, and backslash substitution are performed on the characters between the quotes as described below. However variable references are substituted with their current value at the time of substitution. The double-quotes are not retained as part of the word.
If the first character of a word is an open brace (“{”) and rule [11] does not apply, then the word is terminated by the matching close brace (“}”). Braces nest within the word: for each additional open brace there must be an additional close brace. However, there are instances where characters lose their significance:
No substitutions are performed on the characters between the braces except for backslash-newline substitutions described below, nor do semi-colons, newlines, close brackets, or white space receive any special interpretation. The word will consist of exactly the characters between the outer braces, not including the braces themselves.
If the first character of a word is an open parenthesis (“(”), then the rest of the word is itself broken into subwords until a matching close parenthesis (“)”) is found, and the subwords are in turn substituted in such a way that their boundaries are preserved. The resulting word will consist of exactly the characters between the outer parentheses after substitutions, not including the parentheses themselves or the commented characters or words.
If a word contains an open bracket (“[”) then <Cloverfield> performs command substitution. To do this it invokes the <Cloverfield> interpreter recursively to process the characters following the open bracket as a <Cloverfield> script. The script may contain any number of commands and must be terminated by a close bracket (“]”). The result of the script (i.e. the result of its last command) is substituted into the word in place of the brackets and all of the characters between them. There may be any number of command substitutions in a single word. Command substitution is not performed on words enclosed in braces.
If a word contains a dollar-sign (“$”), not followed by an ampersand (“&”), followed by one of the forms described below, then <Cloverfield> performs variable substitution: the dollar-sign and the following characters are replaced in the word by the value of a variable. If an ampersand immediately follows the dollar-sign, then <Cloverfield> performs variable reference: the dollar-sign and the following characters are replaced in the word by a reference to a variable, which will be dereferenced each time its value is needed. Note that the referenced variable is the one that existed at the time of substitution. The reference remains valid even if the variable is deleted for some reason (like a local variable when a procedure returns).
Variables are specified by two parts: the name part and the index part. The name part of the variable that immediately follows the dollar-sign or dollar-sign plus ampersand may take any of the following forms:
The index part immediately follows the name part and may take any of the following forms:
In the absence of a recognized index part, the name designates a scalar variable. There can be several index parts that designate subparts of the variable recursively. There may be any number of variable substitutions or references in a single word.
If a backslash (“\”) appears within a word then backslash substitution occurs. In all cases but those described below the backslash is dropped and the following character is treated as an ordinary character and included in the word. This allows characters such as double quotes, close brackets, and dollar signs to be included in words without triggering special processing. The following table lists the backslash sequences that are handled specially, along with the value that replaces each sequence.
If a hash character (“#”) appears at a point where <Cloverfield> is expecting the first character of a word, then the hash character and the characters that follow it, up through the next (unescaped) newline, are treated as a comment and ignored.
If a word starts with a string that obeys rule [5] immediately followed by a non-whitespace character, then the leading part is a word modifier. The interpretation of the rest of the word depends on the form taken by this word modifier. Word modifiers serve varying purposes, such as modifying the behavior of the parser, or the interpretation of the word data. Recognized word modifiers are:
cmd {data}ABCDEF this is ignored foo bar baz #{\"[[$ this is also ignored ABCDEF a b c d
{meta foo}bar
{meta}{meta foo}bar
{meta}{meta baz}{meta foo}bar
Each character is processed exactly once by the <Cloverfield> interpreter as part of creating the words of a command. For example, if variable substitution occurs then no further substitutions are performed on the value of the variable; the value is inserted into the word verbatim. If command substitution occurs then the nested command is processed entirely by the recursive call to the <Cloverfield> interpreter; no substitutions are performed before making the recursive call and no additional substitutions are performed on the result of the nested script.
Substitutions take place from left to right, and each substitution is evaluated completely before attempting to evaluate the next. Thus, a sequence like
set y [[set x 0]][[incr x]][[incr x]]
will always set the variable y to the value, 012.
Substitutions do not affect the word boundaries of a command, except for word modifiers as specified in rule [11]. For example, during variable substitution the entire value of the variable becomes part of a single word, even if the variable's value contains spaces.