<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Sat May 25 22:21:38 GMT 2013 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='309'>
<header><title>Expose the Expression Parsing</title><author address="mailto:arjen.markus@wldelft.nl">Arjen Markus</author><status type='project' state='draft' tclversion="8.7" vote='prior'>$Revision: 1.2 $</status><history></history><created day='7' month='jan' year='2008' /><keyword>expr parse</keyword></header>
<abstract>This TIP proposes a new command to expose the parsing of expressions by the <emph style="bold">expr</emph> command. This will make it much easier to implement alternative number systems, such as complex numbers, or to implement symbolic algebra.</abstract>
<body><section title="Rationale">
<para>The <emph style="bold">expr</emph> 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:</para>
<para>A polynomial expression like <emph style="italic">1 + i*z**2</emph> could become:</para>
<verbatim><vline encoding='base64'>IFthZGQgW2NvbXBsZXggMSAwXSBbbXVsdCBbY29tcGxleCAwIDFdICR6ICR6XQ==</vline></verbatim>
<para>(where [complex] is used to make sure the constants are complex).</para>
<para>People used to the infix notation will find this a very clumsy, if not error-prone way of working.</para>
<para>Basic symbolic algebra, like the determination of a derivative (useful for certain numerical algorithms) is much easier when working with the prefix notation:</para>
<verbatim><vline encoding='base64'>IGRlcml2IFthZGQgJGV4cHIxICRleHByMl0gLS0+IGFkZCBbZGVyaXYgJGV4cHIxXSBbZGVyaXYgJGV4cHIyXQ==</vline></verbatim>
<para>This calls for an easy way to convert an infix notation to Tcl&apos;s prefix notation.</para>
</section>
<section title="Proposal">
<para>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.</para>
<para>The rules are simple:</para>
<itemize><item.i><para>Any valid <emph style="bold">expr</emph> expression can be converted. Invalid expressions result in the same error messages as if they were meant for <emph style="bold">expr</emph>.</para></item.i><item.i><para>The symbolic name for the command is simply the same as the string that represents the operation.</para></item.i><item.i><para>Operations have the same precedence as for <emph style="bold">expr</emph>. This TIP does not include a mechanism for introducing new operations.</para></item.i><item.i><para>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.</para></item.i><item.i><para>Errors that result from the evaluation of the expression are handled by the particular implementation of the operations and functions.</para></item.i><item.i><para>The resulting string can then be used by <emph style="bold">eval</emph> or <emph style="bold">uplevel</emph> to run the set of commands in the right order.</para></item.i></itemize>
</section>
<section title="Implementation Notes">
<para>There is no implementation of this command yet, but here is a sketch:</para>
<itemize><item.i><para>Let the <emph style="bold">expr</emph> parser construct a parse tree from the command.</para></item.i><item.i><para>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].</para></item.i></itemize>
<para>This limits the sort of expressions (in particular &quot;constants&quot; as understood by the specific arithmetic system) to expressions that can be parsed by the <emph style="bold">expr</emph> parser, but as this now handles lists, as a consequence of the <emph style="bold">in</emph> and <emph style="bold">ni</emph> operations, this does not seem a severe limitation.</para>
<para>The advantage of this approach is that much of the hard work is already done and that compatibility with the <emph style="bold">expr</emph> command is ensured.</para>
</section>
<section title="Copyright">
<para>This document is placed in the public domain</para>
</section>
</body></TIP>
