Enhanced Expression SyntaxWill DuquetteDon Porter$Revision: 1.4 $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:
ICAgIHNldCB4IFtleHByIHs1KiR5ICsgNioken1dICAgIHNldCB3IFtleHByIHskeCoqMiArICR5KioyfV0=ICAgIHNldCB2IFtleHByIHskdyoqMiArICR5KioyfV0=
The [expr {...}] constructs make the code considerably harder to read. But suppose expr syntax included an assignment operator:
ICAgIGV4cHIge3ggPSA1KiR5ICArIDYqJHp9ICAgIGV4cHIge3cgPSAkeCoqMiArICR5KioyfQ==ICAgIGV4cHIge3YgPSAkdyoqMiArICR5KioyfQ==
Next, suppose that an expr expression could include subexpressions, delimited by a ";" character:
ICAgIGV4cHIgew==ICAgICAgICB4ID0gNSokeSAgKyA2KiR6Ow==ICAgICAgICB3ID0gJHgqKjIgKyAkeSoqMjs=ICAgICAgICB2ID0gJHcqKjIgKyAkeSoqMg==ICAgIH0=
The sequence of computations is now much clearer.
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.
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.
This document has been placed in the public domain.