TIP: 309
Title: Expose the Expression Parsing
Version: $Revision: 1.2 $
Author: Arjen Markus <[email protected]>
State: Draft
Type: Project
Vote: Pending
Created: 07-Jan-2008
Post-History:
Tcl-Version: 8.7
Keywords: expr, parse
~ Abstract
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.
~ Rationale
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.
~ Proposal
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:
* Any valid '''expr''' expression can be converted. Invalid expressions
result in the same error messages as if they were meant for '''expr'''.
* The symbolic name for the command is simply the same as the string that
represents the operation.
* Operations have the same precedence as for '''expr'''. This TIP does not
include a mechanism for introducing new operations.
* Functions are translated into a command of the same name (no particular
namespace) and the list of comma-separated arguments is converted into an
ordinary sequence of arguments.
* Errors that result from the evaluation of the expression are handled by the
particular implementation of the operations and functions.
* The resulting string can then be used by '''eval''' or '''uplevel''' to run
the set of commands in the right order.
~ Implementation Notes
There is no implementation of this command yet, but here is a sketch:
* Let the '''expr''' parser construct a parse tree from the command.
* Let a new function convert the parse tree into a string holding the prefix
expression and pass that to the interpreter as the result of [[s-expr]].
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.
~ Copyright
This document is placed in the public domain