# TIP #309: Expose the Expression Parsing

 TIP: 309 Title: Expose the Expression Parsing Version: \$Revision: 1.2 \$ Author: Arjen Markus State: Draft Type: Project Tcl-Version: 8.7 Vote: Pending Created: Monday, 07 January 2008 Keywords: expr, parse

Please enter your e-mail address (required), your name (optional), make your edits to this TIP and its Abstract, and Submit them. See TIP 3 for a description of the TIP format.

You are advised to copy the text of the TIP into an editor, make your edits there, and then paste back.

 E-mail: Name:

 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. Body: ~ 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 Log: This is a supplement to the main log message to allow you to add extra notes if you wish.

Note that when you submit this form, the server will attempt to store your personal details (i.e. name and email address) as cookies in your browser so that next time you edit a TIP, you do not need to retype this info. We do not retain the information on our server (other than as logged in the TIP itself and in the CVS log) and you can delete it without ill-effect if you wish. You are invited to study the implementation code if you wish to understand exactly how the information is stored in your browser.

TIP AutoGenerator - written by Donal K. Fellows