WIP : Eval routine for CoronaSDK

The sandboxed environment of CoronaSDK disables a few routines that make lua flexible, however for those that have worked with javascript, would recollect that there is a wonderful function called eval. This can be passed an expression that is evaluated. I even recollect using it in Shell Scripting where you can assign the results of an eval to a variable and then continue working with that. How can I do that in Corona?

I was working on another project where I needed some kind of parsing when I thought why not show what I have. There are plenty of half-baked projects lying around on my hdd, I had hoped to put them together in a cookbook for a publisher, that's another story...

back to this, here's a video to demonstrate the eval routine, it is a bit buggy but it works fine for smaller expressions. If I get the chance I will extend it to respect the BODMAS (Brackets, Ordinals Division Multiplication Addition Substraction) the order in which the expressions are evaluated.

so for now, enjoy the video...

local text = native.newTextField(20,40,280,30,nil)

local button = display.newRect(100,120,120,30)
 button:setFillColor(255,255,255)

button:addEventListener("tap",function() 
 local res = eval(text.text)
 native.showAlert("Result",res,{"OK"},nil)
 end)

That is the UI bit that is showing you the results, etc.

Comments

Popular Posts