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 ]
|
where
chars(
PrologString)
write(
Term)
writeq(
Term)
write_canonical(
Term)
Please note: In general it is not possible to reconstruct Term from the string printed bywrite/1
. If Term will be passed back into Prolog it therefore safest to usewrite_canonical(
Term)
(see Term I/O).
format(
Fmt,
Args)
dq(
Command)
br(
Command)
sqb(
Command)
min(
Command)
dot(
ListOfName)
list(
ListOfCommands)
br(
ListOfCommands)
when any of the elements contains spaces, braces or other characters treated
specially by TCL.
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