Question

I need something to embed in my C/C++ program to interpret formulas of the like x*log(x) or sin(x). I would like something small and simple, otherwise I can just embed Python, or Ch, or Scheme, or you name it. But all I need is simple formulas. I have searched the web without luck. Although I don't require it, performance (e.g., the use of a simple JIT) would be a bonus.

Was it helpful?

Solution

Lua - is by far the simplest to embed and use and there is a very small and very fast version with a JIT: http://luajit.org/

OTHER TIPS

Use Reverse Polish notation. There are few algorithms:
1. Parse equation and convert to infix notation (that is easy, you simply identify chain of symbols)
2. Using Dijkstra's shunting-yard algorithm convert infix to RPN.
3. Using DFS calculate the expression

I'm sure there is a lot implementations on the net. I even wrote my own few years ago (without functions feature).

An excellent portable language for this sort of thing is JavaScript. There are more than a few implementations to choose from, for C and C++ there are at least:

Another easily embeddable choice might be Lua. It's certainly easier to use something that already exists rather than reinventing an expression parser.

FFmpeg has a very nice one which you should check out.

What about writing your own simple stack-machine for calculating such formulas?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top