Skip to content
Tcl Source Code View Ticket
Login

View Ticket

patch to enhance expr expression syntax

Deleted218232ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:54:52
OriginalBugID: 2669 RFE
Version: 8.2
SubmitDate: '1999-08-31'
LastModified: '1999-09-15'
Severity: SER
Status: Closed
Submitter: techsupp
ChangedBy: hobbs
OS: Linux-Red Hat
Machine: Other
FixedDate: '2000-10-25'
ClosedDate: '1999-09-15'


Name:

Roger E Critchlow Jr



DesiredBehavior:

Make the expr command use conventional variable reference and function

    call syntax

    for references to Tcl scalar variables and calls to Tcl commands and

    procedures in

    addition to the Tcl macro/command substitution currently implemented.

    Hence,

    {expr a} would return the value of the Tcl variable named "a" or report

    "can't read "a":

    no such variable", rather than throwing a syntax error, and {expr

    f(1,2,3)} would

    return the result of calling [f 1 2 3] or report "invalid command name:

    "f"", rather than

    throwing an undefined math function error.  The expr parser is enhanced

    to accept

    identifiers which include the namespace delimiter.   The existing expr

    syntax is

    unaffected by this enhancement, and existing programs which do not

    depend on

    syntax errors or undefined math function errors will continue to work

    without 

    modification.

    

    For instance, the factorial function currently written as:

    

    proc factorial {n} { expr {$n>1 ? $n*[factorial [expr {$n-1}]] : 1} }

    

    can be written in the enhanced expr as simply:

    

    proc factorial {n} { expr n >1 ? n*factorial(n-1) : 1} }

    

    eliminating two quotations, three dollars, and a nested call to expr.

    It's

    not only easier to read and write, it evaluates more efficiently

    because

    nested calls to expr are no longer needed to pass computed arguments.

    

    Calls to Tcl commands which take a variable name as an argument need

    to quote the variable name if written in enhanced expr syntax.

    

    set a 2; expr set(a,3)

    

    creates a variable named "2" with the value "3",  but

    

    expr set({a},3)

    

    sets the value of "a" to "3". 

    

    Array references through the enhanced expr can be done by writing a

    function call to the array name with array index(es) as argument(s) and

    supplying a Tcl procedure of the same name which constructs a Tcl array

    reference.  This isn't the greatest solution, but it at least allows

    references

    to computed array indexes, eg a(i+1), without nested calls to expr.

    

    The patches to implement this enhancement come to 294 lines in diff -r

    -c format, confined to tclParseExpr.c and tclCompExpr.c



Patch:

http://www.elf.org/pub/tcl8.2.0-expr.patch



PatchFiles:

tclCompExpr.c

    tclParseExpr.c





The newsgroup pointed out several compatability with this idea. 
-- 09/15/1999 hobbs
welch Closed ticket [218232ffff]. [History diff]2000-10-26 08:17:54
dgp Open ticket [218232ffff]. [History diff]2001-04-19 00:47:37
dgp Deleted ticket [218232ffff]. [History diff]2001-04-19 00:48:47
Log in (anonymous is fine) to add a comment.