GSoC Idea: Parsing mathematical expressions

Parsing mathematical expressions

AM Mathematical expressions like those parsed by the [expr] command are not easily manipulated via Tcl commands. What I mean is, if I want to use complex numbers or add support for array expressions (see TIP 363 ([L1 ]), I can do that via Tcl's prefix notation:

   set sum [+ $op1 $op2]

or

   set dotp [inprod $vector1 $vector2]

That is okay for simple expressions, but when things get more complicated, like:

   set diffusion [expr {$diff*(2.0*$conck - $conckm1 - $conckp1)/$deltx**2}]

the prefix notation is cumbersome:

   set diffusion [vecmult $diff [vecdiv [vecsub [vecmult 2.0 $conck] $conckm1 $conckp1] [expr {$deltx**2}]]]

My idea is to make a small extension to convert expression from the mathematical infix notation to Tcl's prefix automatically. I currently have something that almost does the job, but it is still a fair amount of work to make it practical. You could even add extra functionality here, like the introduction of custom operations.

Benefits for the student: learn about parsing.

Benefits for Tcl: more flexibility in mathematical/physical applications.

Lars H: This is not so far from what the infix package does already, but I suppose you might want the extra speed of a C-coded parser (and a slightly nicer calling convention). One thing I've thought about is relation to these things is that one could make it possible to stash parsed/translated/compiled forms of expressions within the intRep of the Tcl_Obj of the expression, like Tcl does with its expressions. Or rather, I've though about whether it would be possible to expose such functionality at the script level. Currently infix stashes its "compiled code" in an array instead, which is a major reason why the calling convention is unintuitive…

AM Ah! I had forgotten about infix. Now, this is beginning to take the form of a not entirely trivial project, I'd say.

Lars H: How so? Having an existing codebase to make fully practical (perhaps even for a delimited field) seems less ambitious than getting something to work in the first place. Oh, you mean my idea about stashing things in Tcl_Objs? Yes, that's probably a separate project, and perhaps not one suited for GSOC. (Actually, I don't think it would be that much coding, but it would require a thorough understanding of the concepts.)

Sarnold: Did you check xsource? It is worth checking out, even if it has bugs remaining. AM Interesting, but at a first glance not quite what I am after. Lars's infix package comes closer, as it allows you to define what an operation actually means. But I will have a closer look ;).