Question

I'm playing around a bit with ActionScript. What I want is that I can display a mathematical function from a string.

E.g. in my working python script I do something like that:

formula = 'x**2 + 3*x'
for x in range( 0, 100 ):
    y = eval( formula )
    graph.display( x, y )

I want to port this to ActionScript, but it seems like there is no more eval since version 3. How can I compute my function values anyway?

Was it helpful?

Solution

Something that might also work in your case, is using Javascript eval instead. You can use something like:

var result = ExternalInterface.call(myEvalFunctionInJS,formula)

to evaluate math functions.

This is an somewhat easy and useful workaround as javascript is quite close to actionscript.

If you put the ExternalInterface call inside an loop, it may become sluggish. To avoid that, you can write the loop in javascript. (You can even write the entire javascript inside as3, so that you do not need to touch the actual html page.)

edit: Here's an link for that.

http://www.actionscript.org/resources/articles/745/2/JavaScript-and-VBScript-Injection-in-ActionScript-3/Page2.html

OTHER TIPS

You will have to write an eval yourself. You will have to parse the string and invoke the right operators.

Here's a link to get you started.

The Tamarin project has a ECMAScript parser written in ES4. Try this as well.

"You can even write the entire javascript inside as3, so that you do not need to touch the actual html page." Do you have links / tutorials? – okoman

Both AS and JS are based on the same ECMAScript standard. So, if you pass a string of AS3 to a container, and use JS's eval on this string, it should work just fine.

Just noticed this question and realized I answered almost the exact same thing here: https://stackoverflow.com/a/11460839/1449525

To paraphrase myself, you can definitely use D.eval, AS3Eval, or ExternalInterface (as seen in the currently chosen answer) assuming you're running in a web page. However, all it seems like you really need is something like this simple MathParser (More info about the MathParser)

Here's how you'd use the MathParser:

package {
    import bkde.as3.parsers.*;
    import flash.display.Sprite;
    public class MathTest extends Sprite {
        public function MathTest() {
            var parser:MathParser = new MathParser([]);
            var compiledObj:CompiledObject = parser.doCompile("(10/3)*4+10");
            var answer:Number = parser.doEval(compiledObj.PolishArray, []);

            var xyParser:MathParser = new MathParser(["x", "y"]);
            var xyCompiledObj:CompiledObject = xyParser.doCompile("(x/3)*y+10");
            var xyAnswer:Number = xyParser.doEval(xyCompiledObj.PolishArray, [10, 4]);
        }
    }
}

I'm sure ExternalInterface stuff works just fine, but I have personal reservations about the cross language communication (especially in terms of efficiency and security) as well as just the awkward nature of it. I feel like a wholly-contained, same-language solution is typically preferable in most situations.

A bit late, but for reference, the D.eval library does what you are asking for:

http://www.riaone.com/products/deval/

It is free and works great for me, but doesn't come with source. I found this question looking for an alternative built-in or source-available solution.

There is also a seemingly abandoned project to port Tamarin to Flash itself:

http://eval.hurlant.com/

Would be awesome if more progress was made, but seems like a curiosity for now.

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