.\" t
.\" Note that this file *must* be preprocessed with tbl before being
.\" passed to *roff. Fail to do this and things will break...
.de Hl
.br
\l'\\n(.lu-\\n(.iu'
.sp
..
.TL
TIP #309: Expose the Expression Parsing
.AU
Arjen Markus
.DA "07 Jan 2008"
.AI
.BX DRAFT
.AB
This TIP proposes a new command to expose the parsing of expressions by the \fBexpr\fR command. This will make it much easier to implement alternative number systems, such as complex numbers, or to implement symbolic algebra.
.AE
.TS
box;
lb | lb.
TIP #309: Expose the Expression Parsing
_
.T&
l | l.
Type: Project
State: Draft
Vote: Pending
Version: $Revision: 1.2 $
Tcl-Version: 8.7
Post-History:
Keywords: expr parse
.TE
.1C
.NH 1
\fBRationale\fR
.LP
The \fBexpr\fR 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:
.LP
A polynomial expression like \fI1 + i*z**2\fR could become:
.LD
\ [add\ [complex\ 1\ 0]\ [mult\ [complex\ 0\ 1]\ $z\ $z]
.DE
.LP
(where [complex] is used to make sure the constants are complex).
.LP
People used to the infix notation will find this a very clumsy, if not error-prone way of working.
.LP
Basic symbolic algebra, like the determination of a derivative (useful for certain numerical algorithms) is much easier when working with the prefix notation:
.LD
\ deriv\ [add\ $expr1\ $expr2]\ -->\ add\ [deriv\ $expr1]\ [deriv\ $expr2]
.DE
.LP
This calls for an easy way to convert an infix notation to Tcl's prefix notation.
.NH 1
\fBProposal\fR
.LP
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.
.LP
The rules are simple:
.IP \ \(bu
Any valid \fBexpr\fR expression can be converted. Invalid expressions result in the same error messages as if they were meant for \fBexpr\fR.
.IP \ \(bu
The symbolic name for the command is simply the same as the string that represents the operation.
.IP \ \(bu
Operations have the same precedence as for \fBexpr\fR. This TIP does not include a mechanism for introducing new operations.
.IP \ \(bu
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.
.IP \ \(bu
Errors that result from the evaluation of the expression are handled by the particular implementation of the operations and functions.
.IP \ \(bu
The resulting string can then be used by \fBeval\fR or \fBuplevel\fR to run the set of commands in the right order.
.NH 1
\fBImplementation Notes\fR
.LP
There is no implementation of this command yet, but here is a sketch:
.IP \ \(bu
Let the \fBexpr\fR parser construct a parse tree from the command.
.IP \ \(bu
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].
.LP
This limits the sort of expressions (in particular "constants" as understood by the specific arithmetic system) to expressions that can be parsed by the \fBexpr\fR parser, but as this now handles lists, as a consequence of the \fBin\fR and \fBni\fR operations, this does not seem a severe limitation.
.LP
The advantage of this approach is that much of the hard work is already done and that compatibility with the \fBexpr\fR command is ensured.
.NH 1
\fBCopyright\fR
.LP
This document is placed in the public domain
.SH
Colophon
.LP
TIP AutoGenerator - written by Donal K. Fellows