# Expression Evaluator

**Posted:**September 14, 2011

**Filed under:**Sketches |

**Tags:**math, musings, parser combinators Leave a comment

I’ve always wanted to have the capability to type in expressions into a sketch and have it react accordingly; to me, it’s kind of always been the “next level” of interactivity with a sketch. I remember a while back there was a sketch, possibly on OpenProcessing (but I can’t find it now) that had a series of about 20 circles rotating about like some sort of solar system, and there was a text field letting you type in an expression as a function of n (presumably the “number” of the circle) which determined the size of the n-th circle. Or something like that. I LOVED that sketch since you could get a massive variety of beautiful interactions by just changing what the expression was.

To be honest I’m very surprised that there isn’t a math/symbolic expression library within the Java standard library. Imagine if there was a standard mathematical input widget within AWT, or Swing, or even controlP5 – great ideas would spring out from all corners of the sketch community. I might even consider creating and maintaining a simple math library to help others along. I personally have always felt a longing for a simple math library for several years. I remember trying to write a symbolic evaluator back in 10th grade of high school – and I got SWAMPED. Of course, I lacked the necessary understanding of both math and programming back then, but I didn’t even get close. I wanted to write programs involving mathematical functions, but the libraries just didn’t exist.

Anyways, enough of the rambles. An arithmetic calculator is the flagship example of Parser Combinators, and I found it enjoyable to extend the “usual” grammar to include variables and functions. I have a feeling that this grammar is going to be heavily used in my future sketches. Sep12 isn’t fancy; it just demonstrates the evaluation capabilities.