TIP #282: Enhanced Expression Syntax

Title:Enhanced Expression Syntax
Version:$Revision: 1.4 $
Authors: Will Duquette <William dot H dot Duquette at jpl dot nasa dot gov>
Don Porter <dgp at users dot sourceforge dot net>
Created:Friday, 13 October 2006
Keywords:expr, operator, assignment


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


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.


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 [1]


This document has been placed in the public domain.

Powered by Tcl[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows