TIP 309: Expose the Expression Parsing

Author:		Arjen Markus <[email protected]>
State:		Draft
Type:		Project
Vote:		Pending
Created:	07-Jan-2008
Tcl-Version:	9.1
Keywords:	expr, parse


This TIP proposes a new command to expose the parsing of expressions by the expr command. This will make it much easier to implement alternative number systems, such as complex numbers, or to implement symbolic algebra.


The expr command uses the traditional infix notation for arithmetical expressions. Tcl itself uses a prefix notation. While it is quite easy to create a set of procedures to do complex number arithmetic, using them means the use of prefix notation, for example:

A polynomial expression like 1 + i*z**2 could become:

 [add [complex 1 0] [mult [complex 0 1] $z $z]

(where [complex] is used to make sure the constants are complex).

People used to the infix notation will find this a very clumsy, if not error-prone way of working.

Basic symbolic algebra, like the determination of a derivative (useful for certain numerical algorithms) is much easier when working with the prefix notation:

 deriv [add $expr1 $expr2] --> add [deriv $expr1] [deriv $expr2]

This calls for an easy way to convert an infix notation to Tcl's prefix notation.


Introduce a new command, tentatively called s-expr, as this is the traditional term used in LISP for expressions in the prefix notation, that converts a given infix expression into an equivalent prefix expression.

The rules are simple:

Implementation Notes

There is no implementation of this command yet, but here is a sketch:

This limits the sort of expressions (in particular "constants" as understood by the specific arithmetic system) to expressions that can be parsed by the expr parser, but as this now handles lists, as a consequence of the in and ni operations, this does not seem a severe limitation.

The advantage of this approach is that much of the hard work is already done and that compatibility with the expr command is ensured.


This document is placed in the public domain