I would like to evaluate a math string in my corona app. Right now I’m focusing on the trig functions, so let’s let the example be the most difficult we’re likely to face:
local expr = "2sin(4pi+2)+7"
My goal is for this to somehow be (either) evaluated as is with maybe a pi --> math.pi switch, or to even break it up. The breaking up would be much more difficult, however, since it COULD be as complicated a above, but could also just be sin(1).
So I would prefer to stay as close to the python eval(expr) function as possible, but if that can’t happen, I am flexible.
The simplest way would be to replace
sinwithmath.sin(piwithmath.piand so on), add missing multiplications signs, and run it throughloadstring, butloadstringis not available in Corona environment.This means you will need to write your own parser for these expressions. I found a discussion on Corona forums that may help you as a starting point: here, with some details and a demo here