TIP 282: Enhanced Expression Syntax

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:         Will Duquette <William.H.Duquette@jpl.nasa.gov>
Author:         Don Porter <dgp@users.sourceforge.net>
State:          Draft
Type:           Project
Vote:           Pending
Created:        13-Oct-2006
Post-History:   
Keywords:       expr,operator,assignment
Tcl-Version:    8.7

Abstract

This TIP extends the syntax of the expr command to allow a sequence of mathematical computations to be expressed clearly and concisely.

Rationale

The ugliness of mathematical code written in Tcl is a perennial source of complaint. Consider the following computation:

    set x [expr {5*$y + 6*$z}]
    set w [expr {$x**2 + $y**2}]
    set v [expr {$w**2 + $y**2}]

The [expr {...}] constructs make the code considerably harder to read. But suppose expr syntax included an assignment operator:

    expr {x = 5*$y  + 6*$z}
    expr {w = $x**2 + $y**2}
    expr {v = $w**2 + $y**2}

Next, suppose that an expr expression could include subexpressions, delimited by a ";" character:

    expr {
        x = 5*$y  + 6*$z;
        w = $x**2 + $y**2;
        v = $w**2 + $y**2
    }

The sequence of computations is now much clearer.

Specification

Assignment Operator

The expr syntax should be extended with an assignment operator which has C-like semantics, i.e., the result of an assignment is the assigned value. This operator shall be written as "=". The term on the left side of the operator shall be a variable name, and the term on the right side of the operator shall be any expression. The result of the overall assignment expression shall be the result of the subexpression on the right hand side of the operator.

Expression Separator Operator

An expr expression can be separated into subexpressions by the semicolon (";") character, which will have the same semantics as the "," operator in C. Thus, the return value of a call to expr is the value of the final subexpression, and the both sides of the expression shall be expressions.

Reference Implementation

See http://sourceforge.net/tracker/?func=detail&aid=1969722&group_id=10894&atid=310894

Copyright

This document has been placed in the public domain.

History