Scripting Flash apps: scanning the input file

Here we go. I know that probably I should have started this new topic talking about the grammar of the language we are going to implement, but as long as I see grammar strictly related to parsing, I’ve preferred to talk about scanning first.

We will go back to the grammar the next time, when talking about how to parse an input file.

I wrote about scanning (or lexing if you prefer) a while ago, when blogging about expression evalution in ActionScript. Scanning an input file is quite always the same (although some languages might require unusual features), and what I wrote about expressions works also for a general language.

The goal of the scanning process is to group some characters together, skipping the ones that have no meaning for the language like spaces. Each group of characters is usually called token. So a Scanner converts a textual input into a stream of tokens, each one rappresenting a possible valid word for our language. While scanning you don’t take care about the meaning of what you are grouping or about the fact that a sequence of tokens is meaningful. This is a task for the Parser as we will see.

When talking about expressions, I showed a manual implementation for a Lexer. Now I want to take a different approach and show you a possible implementation for a simple dynamic scanner. This scanner will be based on regular expressions: each regular expression will rappresent a given token, and we will be able to assign callbacks to the scanner that will be executed each time a given token is extracted from the input.
Writing a general and reusable scanner is usually a good practice. A common approach is to use some scanner generators, that are usually regular expression based too but are able to generate the code for a scanner at compile time. Our approach is different (and generates slower scanners) because regular expressions are evaluated at runtime; but it is fine for a test project.

You can download the source code for the regular expression based lexer (RegexLexer) here, as long as a simple usage example. Let’s see this example together so we can briefly discuss it (PBLexer.as):

Continue reading

Scripting Flash apps: design the language

Finally I had a bit of time to go back to the discussion I started about a month ago: scripting your flash applications. I spent a bit of time thinking which is the best approach to start discussing this kind of topic; it is not as easy as it seems because the risk is to fall into a too complex discussion that a lot of people won’t benefit from.

So I decided to start with a simple example that is somehow related to what I’m doing in these months; more examples will come and I’ll analyze different aspects of scripting trying always to keep the discussion as simple as possible even if this may bring me to do avoid some hard to discuss approaches or optimizations.

Our first scripting language will be an extremely simple language that will be used to manipulate images (like Pixel Bender does). Obviously the goal is not to write something that can be compared with Pixel Bender, but at least we would like to provide an implementation that will lead to scripts as fast as native code (at least we hope so :P).

The first step is to design the language, and to do that we need to do the step zero that is to define the goals we want to achive with the first implementation:

  • The language must be extremely simple to design and implement (it is a didactic stuff indeed!);
  • The language must be as simple as possible to learn and use: we won’t be an alternative to Pixel Bender (because we can’t … yet ;P)
  • The language must be quick to compile and execute.
  • A script must accept some parmeters to control its behaviour.
  • We must support comments!

Now that we decided which are the goals, let’s start definign some limitation or features we must include to quickly reach a solution:

  • The first implementation of the language will support a sigle data type: Numbers. We will move further adding more data types later – I don’t want to focus on type checking yet.
  • We will add support for basic expression operators, basic control flow. No support for functions yet.
  • The language will provide some useful operators or builtin functions to manage common operations like packing/unpacking color components and peform some mathematical operations.
  • Arguments to a script will be only Numbers at first.
  • A script will be defined as a simple sequence of arguments followed by a function definition. This function will be applied sequentially to every pixel of one or more source images.
  • We will provide to implementations for the language: a simple (and slow) interpreter at first, then we will generate native SWF code to speedup the performances.

Even if briefly, it is quite important to define the goals of our language and then the features, because a lack of design (even if extremely simple and poor as the current one :P) may lead to unexpected issues during implementation.

Now that we defined those basic pointers, let’s see how the language will look like:
// Simple crossfade filter
// The crossfade intensity
argument intensity;
/**
* A quick explanation of the used builtins:
* - in( id, x, y ): gets the ARGB color at x and y of the image identified by id (there can be more than one image as input);
* - out( id, x, y, color ): sets the ARGB color at x and y for the output image identified by id;
* - argb( a, r, b, b ): build an ARGB color from its components (clamping and rounding the values as required);
*/
evaluatePixel( x, y )
{
// this kind of assignment is similar to the python unpack assignment
// but extracts the 4 components (ARGB) from a color.
a1, r1, g1, b1 = in( 0, x, y );
a2, r2, g2, b2 = in( 1, x, y );
out( 0, x, y, argb(
( 1 - intensity ) * a1 + intensity * a2,
( 1 - intensity ) * r1 + intensity * r2,
( 1 - intensity ) * g1 + intensity * g2,
( 1 - intensity ) * b1 + intensity * b2 ));
}

As a side not, I must say that the language may change during implementation because of unexpected limitations we will find during development.

Next week we will move forward start implementing the language. Stay tuned!