开发者

Embeddable formula interpreter

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 Sc开发者_如何学Pythonheme, 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.


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).


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/


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:

  • QtScript
  • V8

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜