Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2004-11-23
08:37 • Ticket [1062849fff] EXPR with single substitution status still Closed with 4... artifact: b814f420ef user: msofer
2004-11-09
17:35 • Ticket [1062849fff]: 4 changes artifact: 66b12e1fbd user: dkf
17:31 • Closed ticket [1062849fff]. artifact: ae874f6409 user: dkf
04:01 • New ticket [1062849fff]. artifact: 6150e8d61c user: alove

EXPR with single substitution

Closed / Rejected1062849fffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 22 years ago
Descriptionplain2004-11-09 04:01:07
SUMMARY: The EXPR command in its regular form 
performs two substitutions: one by the Tcl parser and 
one by the EXPR command itself.  But in practice, the 
TCL parser is rarely needed; almost all EXPR commands 
are coded with double-braces to improve performance.  
This is a main contributor to what is known as "quoting 
hell" when using TCL:

set var [expr {$a*25}]
set speed [expr {($a+$b)/75}]

create line [expr {$a+2}] [expr {$a2+7}] \
[expr {$a+$bh-2}] [expr {$b2+$a+3}] 

Rather than using more quotes to give a single 
substitution and more speed, I propose a new command 
called XP, which is identical to EXPR with double braces.  
Using this command, the above code would look like this:

set var [xp $a*25]
set speed [xp ($a+$b)/75]

create line [xp $a+2] [xp $a2+7] \
[xp $a+$bh-2] [xp $b2+$a+3] 

The third line saves 16 characters of typing, including 
eight double-braces that require the shift key.

IMPLEMENTATION: This command would be identical to 
EXPR except that it would skip the TCL parser.  For 
demonstration, you can simulate the command (but not 
its speed) using

proc xp {a} { eval expr { $a } } 

(This proc works only if you exclude spaces from the 
expression, which is why the above examples have no 
spaces.)
dkfplain2004-11-09 17:31:38
Logged In: YES 
user_id=79902

Absolutely categorically no.

This was how [expr] behaved in Tcl 8.0a1, and it was
specifically changed to the current behaviour on the grounds
that the community *hated* having commands that had magical
syntactic relationships with the Tcl parser.  It was about
the only time the team at Sunlabs (as they were then) got a
full-blown flaming on comp.lang.tcl if memory serves me
right. I might not be a complete stranger to controversy,
but I know when to stay clear of something... :^)
dkfplain2004-11-09 17:35:30
Logged In: YES 
user_id=79902

Workaround in your case above: create a little [sum] command :)

create line [sum $a 2] [sum $a2 7] \
    [sum $a $bh -2] [sum $b2 $a 3]
msoferplain2004-11-23 08:37:23
Logged In: YES 
user_id=148712

The basic functioning of Tcl is described in Tcl.n as:
   1. the parser substitutes the words in the command line
   2. the first word in the substituted line is called as a
command, with the remaining words as arguments. The command
may interpret its arguments in any way it chooses.

This is true for every Tcl command - whether built-in, or a
proc, or a C-coded extension command. In a sense, this *is*
what Tcl is.

Thus, the substitution by the Tcl eval-loop cannot be
avoided without destroying Tcl's simplicity - ie, without
making xp the sole Tcl command that fails to satisfy the
Endekalogue. That is a no-no, and is what Donal was alluding
to. "Skipping the Tcl parser" is beyond the means of a
command - the parser does its work before noticing what the
command is. Think of what should happen for eg
   set cmd xp
   set a [$cmd $a*2]
Start parsing/substituting, and stop when you notice it is
actually xp?

Your examples were all typed without spaces - ie, it seems
that you propose an xp that takes a single argument. The
choice is either that
   set x [xp $a + $a * $b ].0
errors out (after being parsed to determine the number of
arguments to xp), or else the Tcl parser has to get a new
rule that says "if the command name is xp, the rest of the
command will be passed as a single string argument to xp";
but you still have to parse to look for the closing ']' -
so, no real savings.

In summary: in order to work as proposed, xp would have to
destroy Tcl's simplicity. And it would still fail to be as
fast as 
expr {...}.
Log in (anonymous is fine) to add a comment.