Runtime expression evaluation in ActionScript

Finally I have a bit of free time to write down this post.
Some days ago a guy on the forum asked about how to evaluate at runtime simple mathematical expressions with support for symbols and function calls.

Now that the eval function have been removed from the language for reasons I’ve not enought time to talk about, we need to parse and execute those expressions manually.

I built a simple Actionscript library that can be used to parse mathematical expressions: it has support for function calls, for variables and it is able to convert the expression into a postfix rappresentation if you may need it. The expression parser is quite simple and have been built manually following some concepts related to programming language compilers; it includes a Scanner (or lexer – however you want to call it) to tokenize the expression, a Parser that convert the expression into an Abstract Syntax Tree and a simple routine that evaluates that AST using a Symbol Table as evaluation context.

There are no comments inside the code right now; I hope to find a little bit of time to write an in depth discussion about this topic. In the mean time you can continue reading the entry for a general explanation about how does the code works and for some examples that may be useful.

Here is a simple example that shows how does the code works:

import it.sephiroth.expr.CompiledExpression;
import it.sephiroth.expr.Parser;
import it.sephiroth.expr.Scanner;
public class Example
{
public static function run(): void
{
var expression: String = "sin( x / ( 8 / 2 + (-0.12 + 2.3) * x / x ) ) * 100";
var scanner: Scanner = new Scanner( expression );
var parser: Parser = new Parser( scanner );
var compiled: CompiledExpression = parser.parse();
var context: Object = {
x: 100,
sin: Math.sin,
cos: Math.cos
};
trace( 'Postfix:', compiled.toString() );
trace( 'Result:', compiled.execute( context ) );
}
}

Before I forgot, you can download the source code here with a simple example included.

Continue reading

Finally a code coverage tool for AS3!

Today is really a good day.

I was waiting for this kind of tool since a lot of time, and thanks to Joe we can finally do Code Coverage of our Actionscript code.

The project is hosted on Google Code, and can be obviously downloaded for free; the project is born after some tests of the author, that edited the Flex compiler to include additional informations that are used to generate a code coverage log that then can be read by softwares that display the output in a more user friendly and fashion way.

For anyone who doesn’t know, Code Coverage is a good practice used most in software testing, that is applied to understand how much source code is touched by our tests. This way a developer knows if a test covers all the source code (or the required portions) and can tune his tests if they don’t.

This project is probably one of the first born after the public release of the Flex SDK source code, and we are pretty sure that there are much more projects coming in the near future.

Finally we can do a little bit of Code Coverage in Aviary 🙂

ActionScript 3 import alias! Where is it?

working with Flex2/ActionScript 3 sometimes you may find that in different libraries there as classes (always in different packages) with the same name.
Ok, no problem, in fact I remember I’ve read something in the as3 documentation about the import alias directive..
A quick google search and in fact I come to this page: http://livedocs.adobe.com/…?href=as3_specification106.html

Cool, I thought. Then I applied the directive in my as3 project (because two ‘Location’ classes were giving me problems in flex) but when clicked on compile it throws me a syntax error  🙁
Strange, it’s described in the documentation, but dont work!.. It’s also described in the es4 specs.
Fortunately Colin gives me an answer: “… single name aliases were indeed dropped for AS3. even if the plan is that they will go into the ES4 standard, and if that occurs, they will be added to ActionScript eventually.”
I hope it will happen soon!

IMHO, I would prefer a syntax like the python one about the import alias: http://docs.python.org/ref/import.html

Something like: import os as somethingelse, but it’s just a stylistic issue for me…

Programming logic

During my tests with Flex2 I encountered a strange behavior with the “for” statement and I first thought about a bug of the player.
This simple script was crashing the player (it enters in an infinite loop):
<?xml version="1.0" encoding="utf-8"?>
private function test():void
{
for(var a:uint = 10; a >= 0; a--)
{
trace(a);
}
}

Once I changed the type of the “a” variable into a “Number” and an “int” I understand what I was missing…
The script worked well after the type change. But to be sure I did also a little test in C which gave me the same results as the flex2 one (infinite loop):

int main(int argc, char *argv[])
{
unsigned int a;
for(a = 10; a >= 0; a--)
{
printf("a = %d\n", a);
}
system("PAUSE");
return 0;
}

Yes, it’s absolutely logical!
After the last step, when a is 0, the program loses next value because it cannot be a negative value and then the loop continues to cicle.

Ok, it’s not a player bug… it’s me 🙁

Flash Player 9, Flex 2, ActionScript 3.0: A Survey of the New Landscape

Colin has posted an interesting article on flash9, flex2, actionscript3.
Why ActionScript 2 coders should start learning ActionScript3 as soon as possible and why timeline coders and flash designers shouldn’t…
Colin underlined a significant aspect of this new flash release: the free flex2 command line compiler. (I asked for it since so much time!)

Read the full entry here:
http://www.moock.org/blog/archives/000189.html