Command format

There is a mechanism for describing Tcl commands in Prolog as Prolog terms. This is used in two ways: firstly, to be able to represent Tcl commands in Prolog so that they can be subsequently passed to Tcl for evaluation; and secondly for passing terms back from Tcl to Prolog by doing the reverse transformation.

Why not represent a Tcl command as a simple atom or string? This can indeed be done, but commands are often not static and each time they are called require slightly different parameters. This means constructing different atoms or strings for each command in Prolog, which are expensive operations. A better solution is to represent a Tcl command as a Prolog term, something that can be quickly and efficiently constructed and stored by a Prolog system. Variable parts to a Tcl command (for example command arguments) can be passed in through Prolog variables.

In the special command format, a Tcl command is specified as follows.

Command ::= Name
| chars(code-list)
| write(term)
| writeq(term)
| write_canonical(term)
| format(Fmt,Args)
| dq(Command)
| br(Command)
| sqb(Command)
| min(Command)
| dot(ListOfNames)
| list(ListOfCommands)
| ListOfCommands

Fmt ::= atom

Name ::= atom { other than [] }
| number

ListOfCommands ::= []
| [ Command | ListOfCommands ]

ListOfNames ::= []
| [ Name | ListOfNames ]

Args ::= []
| [ term | Args ]


denote their printed representations
denotes the string represented by PrologString (a code-list)
denotes the string that is printed by the corresponding built-in predicate.
Please note: In general it is not possible to reconstruct Term from the string printed by write/1. If Term will be passed back into Prolog it therefore safest to use write_canonical(Term) (see Term I/O).

format(Fmt, Args)
denotes the string that is printed by the corresponding built-in predicate
denotes the string specified by Command, enclosed in double quotes
denotes the string specified by Command, enclosed in curly brackets
denotes the string specified by Command, enclosed in square brackets
denotes the string specified by Command, immediately preceded by a hyphen
denotes the widget path specified by ListOfName, preceded by and separated by dots
denotes the TCL list with one element for each element in ListOfCommands. This differs from just using ListOfCommands or br(ListOfCommands) when any of the elements contains spaces, braces or other characters treated specially by TCL.
denotes the string denoted by each element, separated by spaces. In many cases list(ListOfCommands) is a better choice.

Examples of command specifications and the resulting Tcl code:

     [set, x, 32]
         => set x 32
     [set, x, br([a, b, c])]
         => set x {a b c}
     [dot([panel,value_info,name]), configure, min(text), br(write('$display'/1))]
          => .panel.value_info.name configure -text {$display/1
     ['foo bar',baz]
          =>foo bar baz
     list(['foo bar',bar])
          => {foo bar} baz
     list(['foo { bar'',bar])
          => foo\ \{ \bar baz