<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scripting languages &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<atom:link href="https://blog.sephiroth.it/category/blog/scripting-languages/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.sephiroth.it</link>
	<description>programmer by pure chance</description>
	<lastBuildDate>Wed, 28 Jul 2010 18:04:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.sephiroth.it/wp-content/uploads/2019/03/cropped-sepy-big-32x32.png</url>
	<title>scripting languages &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<link>https://blog.sephiroth.it</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Download flash camp presentation</title>
		<link>https://blog.sephiroth.it/2009/09/08/download-flash-camp-presentation/</link>
					<comments>https://blog.sephiroth.it/2009/09/08/download-flash-camp-presentation/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Tue, 08 Sep 2009 15:42:21 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex camp]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[presentation]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=278</guid>

					<description><![CDATA[I&#8217;m publishing here the pdf of the slides me and Gabriele used in the Flash Camp in Rimini last Saturday, if someone is interested. You can download the pdf here:&#160;flex camp.pdf P.S. Saturday night was really great, thank you everyone &#240;&#159;&#153;&#130;]]></description>
										<content:encoded><![CDATA[<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><br />
<a href="/wp-content/uploads/2009/09/flex_camp.pdf"><img fetchpriority="high" decoding="async" class="aligncenter size-full wp-image-424" title="Screen shot 2009-09-08 at 8.31.25 AM" src="http://ve.m5yn67dl.vesrv.com/wp-content/uploads/2009/09/Screen-shot-2009-09-08-at-8.31.25-AM.png" alt="" width="400" height="299" srcset="https://blog.sephiroth.it/wp-content/uploads/2009/09/Screen-shot-2009-09-08-at-8.31.25-AM.png 400w, https://blog.sephiroth.it/wp-content/uploads/2009/09/Screen-shot-2009-09-08-at-8.31.25-AM-300x224.png 300w, https://blog.sephiroth.it/wp-content/uploads/2009/09/Screen-shot-2009-09-08-at-8.31.25-AM-150x112.png 150w" sizes="(max-width: 400px) 100vw, 400px" /></a><br />
</span></p>
<div>
<div style="text-align: left;">I&#8217;m publishing here the pdf of the slides me and Gabriele used in the <a href="http://www.actionscript.it/showVetrina.cfm?id=58">Flash Camp</a> in Rimini last Saturday, if someone is interested.</div>
<div style="text-align: left;">You can download the pdf here: <a href="/wp-content/uploads/2009/09/flex_camp.pdf">flex camp.pdf</a></div>
</div>
<div>P.S. Saturday night was really great, thank you everyone 🙂</div>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2009/09/08/download-flash-camp-presentation/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Scripting Flash apps: scanning the input file</title>
		<link>https://blog.sephiroth.it/2008/12/16/scripting-flash-apps-scanning-the-input-file/</link>
					<comments>https://blog.sephiroth.it/2008/12/16/scripting-flash-apps-scanning-the-input-file/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Tue, 16 Dec 2008 17:06:54 +0000</pubDate>
				<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[aif]]></category>
		<category><![CDATA[lexer]]></category>
		<category><![CDATA[namespace]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[scanner]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[token]]></category>
		<category><![CDATA[yacc]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=269</guid>

					<description><![CDATA[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&#8217;ve preferred to talk about scanning first. We will go back to the grammar the next time, when &#8230; <a href="https://blog.sephiroth.it/2008/12/16/scripting-flash-apps-scanning-the-input-file/" class="more-link">Continue reading <span class="screen-reader-text">Scripting Flash apps: scanning the input file</span></a>]]></description>
										<content:encoded><![CDATA[<p>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&#8217;ve preferred to talk about <strong>scanning</strong> first.</p>
<p>We will go back to the grammar the next time, when talking about how to parse an input file.</p>
<p>I wrote about scanning (or <strong>lexing</strong> 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.</p>
<p>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 <strong>token</strong>. So a <strong>Scanner</strong> converts a textual input into a stream of tokens, each one rappresenting a possible valid word for our language. While scanning you don&#8217;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 <strong>Parser</strong> as we will see.</p>
<p>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 <em>regular expressions</em>: 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.<br />
Writing a general and reusable scanner is usually a good practice. A common approach is to use some <strong>scanner generators</strong>, 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.</p>
<p>You can download the source code for the regular expression based lexer (<em>RegexLexer</em>) <strong><a href="https://blog.sephiroth.it/wp-content/uploads/2008/12/scriptingflash.zip">here</a></strong>, as long as a simple usage example. Let&#8217;s see this example together so we can briefly discuss it (<em>PBLexer.as</em>):</p>
<p><span id="more-269"></span><br />
<code>package test<br />
{<br />
import it.sephiroth.scripting.utils.RegexLexer;<br />
import it.sephiroth.scripting.utils.RegexLexerContext;<br />
import it.sephiroth.scripting.utils.Token;<br />
public class PBLexer extends RegexLexer<br />
{<br />
private static const keyword_map: Object = {<br />
languageVersion:  TokensTable.LANGUAGE_VERSION,<br />
kernel:        TokensTable.KERNEL,<br />
parameter:      TokensTable.PARAMETER,<br />
input:        TokensTable.INPUT,<br />
image1:        TokensTable.IMAGE1,<br />
image2:        TokensTable.IMAGE2,<br />
image3:        TokensTable.IMAGE3,<br />
image4:        TokensTable.IMAGE4,<br />
"void":        TokensTable.VOID,<br />
output:        TokensTable.OUTPUT,<br />
evaluatePixel:    TokensTable.EVALUATE_PIXEL,<br />
"if":        TokensTable.IF,<br />
"else":        TokensTable.ELSE,<br />
"const":      TokensTable.CONST,<br />
"int":        TokensTable.INT,<br />
int2:        TokensTable.INT2,<br />
int3:        TokensTable.INT3,<br />
int4:        TokensTable.INT4,<br />
float:        TokensTable.FLOAT,<br />
float2:        TokensTable.FLOAT2,<br />
float3:        TokensTable.FLOAT3,<br />
float4:        TokensTable.FLOAT4,<br />
float2x2:      TokensTable.FLOAT2X2,<br />
float3x3:      TokensTable.FLOAT3X3,<br />
float4x4:      TokensTable.FLOAT4X4,<br />
pixel1:        TokensTable.PIXEL1,<br />
pixel2:        TokensTable.PIXEL2,<br />
pixel3:        TokensTable.PIXEL3,<br />
pixel4:        TokensTable.PIXEL4  };<br />
public function PBLexer( source_name: String, input: String )<br />
{<br />
super( source_name, input );<br />
addRule( "nl",      "\n",            -1,          RegexLexer.NL );<br />
addRule( "space",    "[ \t]+",           -1,          RegexLexer.SKIP );<br />
addRule( "comment",    "//.*",           -1,          RegexLexer.SKIP );<br />
addRule( "start_cmt",  "/\\*",           -1,          RegexLexer.START_EAT );<br />
addRule( "stop_cmt",  "\\*/",           -1,          RegexLexer.STOP_EAT );<br />
addRule( "ident",    "[a-zA-Z_][a-zA-Z_0-9]*",  TokensTable.IDENT,  onIdent );<br />
addRule( "number",    "0|([1-9][0-9]*(\\.[0-9]*)?)",  TokensTable.NUMBER, onNumber );<br />
addRule( "string",    "\"(\\\\\"|[^\"])+\"",    TokensTable.STRING, onString );<br />
addRule( "symbol",    "\\+\\+|\\-\\-|< =|>=|==|" +<br />
"!=|&amp;&amp;|\\|\\||\\*=|/=|" +<br />
"\\+=|\\-=|\\(|\\)|\\*|" +<br />
"\\+|,|\\-|\\.|/|:|;|#|" +<br />
"\\< |=|\\>|\\{|\\}|\\[|" +<br />
"\\]|\\?|!",          -1,          onSymbol );<br />
}<br />
private function onSymbol( context: RegexLexerContext ): void<br />
{<br />
var token: Token;<br />
token = new Token(<br />
context.token.value.charCodeAt( 0 ),<br />
context.token.value,<br />
context.token.sourceName,<br />
context.token.charno,<br />
context.token.lineno,<br />
context.token.columnno );<br />
context.token = token;<br />
}<br />
private function onIdent( context: RegexLexerContext ): void<br />
{<br />
var type: int;<br />
var token: Token;<br />
type = context.token.type;<br />
if( keyword_map[ context.token.value ] )<br />
{<br />
type = keyword_map[ context.token.value ];<br />
}<br />
token = new Token(<br />
type,<br />
context.token.value,<br />
context.token.sourceName,<br />
context.token.charno,<br />
context.token.lineno,<br />
context.token.columnno );<br />
context.token = token;<br />
}<br />
private function onNumber( context: RegexLexerContext ): void<br />
{<br />
var token: Token;<br />
var value: Number;<br />
value = parseFloat( context.token.value );<br />
token = new Token(<br />
context.token.type,<br />
value,<br />
context.token.sourceName,<br />
context.token.charno,<br />
context.token.lineno,<br />
context.token.columnno );<br />
context.token = token;<br />
}<br />
private function onString( context: RegexLexerContext ): void<br />
{<br />
var token: Token;<br />
var value: String;<br />
value = context.token.value.substr( 1, context.token.value.length - 2 );<br />
token = new Token(<br />
context.token.type,<br />
value,<br />
context.token.sourceName,<br />
context.token.charno,<br />
context.token.lineno,<br />
context.token.columnno );<br />
context.token = token;<br />
}<br />
}<br />
}</code><br />
As you may imagine, this example is a simple Lexer that is able to scan Pixel Bender input files. The Lexer is composed by a serie of rules registered using the function <em>addRule( name: String, regex: String, id: int, callback: Function )</em>. The callback function can be a user defined callback or a standard callback.<br />
There are some standard callbacks:</p>
<ul>
<li><strong>RegexLexer.NL</strong>: tells the Lexer that a new line token have been matched. This action increase an internal counter that keeps track of the line number;</li>
<li><strong>RegexLexer.SKIP</strong>: skip the current token;</li>
<li><strong>RegexLexer.START_EAT</strong>: start eating the tokens;</li>
<li><strong>RegexLexer.STOP_EAT</strong>: stop eating the tokens;</li>
</ul>
<p>Custom actions instead can be used to change the state of the Lexer or to generate a custom Token; take for instance the <em>onIdent</em> calback: each time an identifier is matched, we check if it is a keyword and if so, we generate the proper.</p>
<p>Here is an simple usage example:<br />
<code>package {<br />
import flash.display.Sprite;<br />
import it.sephiroth.scripting.utils.Token;<br />
import test.PBLexer;<br />
public class test extends Sprite<br />
{<br />
private static var INPUT: XML = <input />< ![CDATA[
/*****************************************************************************
*
* Copyright (C) 2008 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE:  All information contained  herein is,  and remains the property of
* Adobe Systems Incorporated and its suppliers, if any.  The intellectual and
* technical  concepts  contained  herein  are  proprietary  to  Adobe Systems
* Incorporated  and  its suppliers  and may  be covered  by U.S.  and Foreign
* Patents, patents in process, and are protected by trade secret or copyright
* law.  Dissemination of this information or reproduction of this material is
* strictly forbidden  unless prior  written permission is obtained from Adobe
* Systems Incorporated.
*
*****************************************************************************/
<languageVersion: 1.0;><br />
// basicBoxBlur: A simple example to demonstrate how to implement a 1<br />
//                iteration box blur.  The kernel is a good example to<br />
//                demonstrate how to implement a convolution using for loops.<br />
kernel basicBoxBlur<br />
< namespace : "AIF";
vendor : "Adobe Systems";
version : 2;
description : "variable Radius Box Blur"; ><br />
{<br />
// A parameter that represents the radius of the blur in both the X and Y<br />
// directions.<br />
parameter float blurRadius<br />
< minValue:float(0.0);
maxValue:float(25.0);
defaultValue:float(5.0);
>;<br />
// A dependent value.  These are calculated in the evaluateDependents()<br />
// function and save us from having to perform repetitive calculations<br />
// for each pixel.<br />
dependent int radiusAsInt;<br />
// evaluateDependents(): Built-in function to do any pre-processing of the<br />
//                       parameters.  Usage of this function helps avoid<br />
//                       repeating any necessary instructions per pixel.<br />
//                       In a filter as simple as this, the extra cycles<br />
//                       are probably not necessary, but it serves as a<br />
//                       good example.<br />
void<br />
evaluateDependents()<br />
{<br />
// Convert the dimension into a integer value for use in the iteration<br />
// over the kernel window.  All type conversions are required to be<br />
// explicit.<br />
radiusAsInt = int(ceil(blurRadius));<br />
}<br />
input image4 src;<br />
output float4 dst;<br />
// Region functions are not available in Flash targets, so we only define<br />
// the functions if we are executing on a different backend.<br />
#if !AIF_FLASH_TARGET<br />
// needed(): Indicates what area of the input is needed to fulfill the<br />
//           requested output region.  In this case, we consider a<br />
//           radius sized area of the input for each output pixel, so we<br />
//           need to outset the area by one.<br />
region needed(region outputRegion, imageRef inputRef)<br />
{<br />
float2 singlePixel = pixelSize(src);<br />
return outset(outputRegion, float2(singlePixel.x * ceil(blurRadius), singlePixel.y * ceil(blurRadius)));<br />
}<br />
// changed(): Indicates what area of the output is affected by the<br />
//            specified input.  In this case, we will blur out the image<br />
//            by the size of the radius, so the output is the input outset by<br />
//            one.<br />
region changed(region inputRegion, imageRef inputRef)<br />
{<br />
float2 singlePixel = pixelSize(src);<br />
return outset(inputRegion, float2(singlePixel.x * ceil(blurRadius), singlePixel.y * ceil(blurRadius)));<br />
}<br />
#endif<br />
// evaluatePixel(): The function of the filter that actually does the<br />
//                  processing of the image.  This function is called once<br />
//                  for each pixel of the output image.<br />
void<br />
evaluatePixel()<br />
{<br />
// Containers for both the accumulated color as well as the number of<br />
// pixels touched.<br />
float denominator = 0.0;<br />
float4 colorAccumulator = float4(0.0, 0.0, 0.0, 0.0);<br />
float2 singlePixel = pixelSize(src);<br />
// Iterate through the kernel window, adding up the samples in<br />
// that location.<br />
for(int i = -radiusAsInt; i < = radiusAsInt; i++)
{
for(int j = -radiusAsInt; j <= radiusAsInt; j++)
{
colorAccumulator += sampleNearest(src,
outCoord() + float2(float(i) * singlePixel.x, float(j) * singlePixel.y));
denominator++;
}
}
// calculate the resultant pixel value which is the accumulated color
// divided by the total number of pixels.
// In this case, the denominator is 9.
dst = colorAccumulator / denominator;
}
}
]]&gt;<br />
public function test()<br />
{<br />
super();<br />
var token: Token;<br />
var lexer: PBLexer;<br />
lexer = new PBLexer( "<file>", INPUT.text() );<br />
while( token = lexer.next() )<br />
{<br />
if( token.type == Token.EOF )<br />
{<br />
break;<br />
}<br />
trace( token );<br />
}<br />
}<br />
}<br />
}</file></code><br />
Next week we will move further and start discussing the grammar, implementing a simple Parser using a custom version of <a href="http://en.wikipedia.org/wiki/Yacc">Yacc</a> that generates ActionScript code.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2008/12/16/scripting-flash-apps-scanning-the-input-file/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Scripting Flash apps: design the language</title>
		<link>https://blog.sephiroth.it/2008/11/29/scripting-flash-apps-design-the-language/</link>
					<comments>https://blog.sephiroth.it/2008/11/29/scripting-flash-apps-design-the-language/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Sat, 29 Nov 2008 18:28:35 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[pixelbender]]></category>
		<category><![CDATA[scripting]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=268</guid>

					<description><![CDATA[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 &#8230; <a href="https://blog.sephiroth.it/2008/11/29/scripting-flash-apps-design-the-language/" class="more-link">Continue reading <span class="screen-reader-text">Scripting Flash apps: design the language</span></a>]]></description>
										<content:encoded><![CDATA[<p>Finally I had a bit of time to go back to the <a title="Scripting your Flash applications …" href="https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/">discussion</a> I started about a month ago: <strong>scripting your flash applications</strong>. 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&#8217;t benefit from.</p>
<p>So I decided to start with a simple example that is somehow related to <a href="http://aviary.com">what I&#8217;m doing</a> in these months; more examples will come and I&#8217;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.</p>
<p>Our first scripting language will be an extremely simple language that will be used to <strong>manipulate images</strong> (like <a href="http://labs.adobe.com/technologies/pixelbender/">Pixel Bender</a> 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 <strong>scripts as fast as native code</strong> (at least we hope so :P).</p>
<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:</p>
<ul>
<li>The language must be extremely <strong>simple to design and implement</strong> (it is a didactic stuff indeed!);</li>
<li>The language must be <strong>as simple as possible to learn and use</strong>: we won&#8217;t be an alternative to Pixel Bender (because we can&#8217;t &#8230; yet ;P)</li>
<li>The language must be <strong>quick to compile and execute</strong>.</li>
<li>A script must accept some parmeters to control its behaviour.</li>
<li>We must support comments!</li>
</ul>
<p>Now that we decided which are the goals, let&#8217;s start definign some limitation or features we must include to quickly reach a solution:</p>
<ul>
<li>The first implementation of the language will support a <strong>sigle data type: Numbers</strong>. We will move further adding more data types later &#8211; I don&#8217;t want to focus on type checking yet.</li>
<li>We will add support for basic expression operators, basic control flow. No support for functions yet.</li>
<li>The language will provide some useful operators or <strong>builtin functions</strong> to manage common operations like packing/unpacking color components and peform some mathematical operations.</li>
<li>Arguments to a script will be only Numbers at first.</li>
<li>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.</li>
<li>We will provide to implementations for the language: a simple (and slow) <strong>interpreter</strong> at first, <strong>then we will generate native SWF code</strong> to speedup the performances.</li>
</ul>
<p>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.</p>
<p>Now that we defined those basic pointers, let&#8217;s see how the language will look like:<br />
<code>// Simple crossfade filter<br />
// The crossfade intensity<br />
argument intensity;<br />
/**<br />
* A quick explanation of the used builtins:<br />
*  - 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);<br />
*  - out( id, x, y, color ): sets the ARGB color at x and y for the output image identified by id;<br />
*  - argb( a, r, b, b ): build an ARGB color from its components (clamping and rounding the values as required);<br />
*/<br />
evaluatePixel( x, y )<br />
{<br />
// this kind of assignment is similar to the python unpack assignment<br />
// but extracts the 4 components (ARGB) from a color.<br />
a1, r1, g1, b1 = in( 0, x, y );<br />
a2, r2, g2, b2 = in( 1, x, y );<br />
out( 0, x, y, argb(<br />
( 1 - intensity ) * a1 + intensity * a2,<br />
( 1 - intensity ) * r1 + intensity * r2,<br />
( 1 - intensity ) * g1 + intensity * g2,<br />
( 1 - intensity ) * b1 + intensity * b2 ));<br />
}<br />
</code><br />
As a side not, I must say that the language may change during implementation because of unexpected limitations we will find during development.</p>
<p>Next week we will move forward start implementing the language. Stay tuned!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2008/11/29/scripting-flash-apps-design-the-language/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Scripting your Flash applications &#8230;</title>
		<link>https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/</link>
					<comments>https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Mon, 27 Oct 2008 22:59:03 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[sandbox]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[virtual machine]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=266</guid>

					<description><![CDATA[As someone of you may know, I really like studying programming languages and sometimes implement for fun (or for work) simple scripting languages that help me out during development. The fight between scripting and programming languages will probably never end. Well, it is not really a fight but more an infinite race where the first &#8230; <a href="https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/" class="more-link">Continue reading <span class="screen-reader-text">Scripting your Flash applications &#8230;</span></a>]]></description>
										<content:encoded><![CDATA[<p>As someone of you may know, I really like studying programming languages and sometimes implement for fun (or for work) simple <strong>scripting languages</strong> that help me out during development.</p>
<p>The <em>fight</em> between scripting and programming languages will probably never end. Well, it is not really a fight but more an infinite race where the first position is exchanged continuosly between the two participants.<br />
We need <strong>flexibility</strong> first, and so we add to our complex applications a way to support scripting to be able to easilly perform changes without the need to run the long time taking compilation process a low level language might take; then, the more the scripts became complex, the more we need an <strong>increase in performances</strong>, and so we rapidly move to compiled plugin egines; plugins offer a bit of flexibility and usually a good speed. Then we grant other users the permission to write plugins and so we need a sandbox to run the plugins in and to be sure every single line of code that is executed doesn&#8217;t harm our application. And so we revert back to scripting &#8230; and so on.<br />
The race will never end but the technologies will continue to increase in speed and sometimes in complexity.</p>
<p>All the introduction is to say that its not the point to choose if scripting is useful or not for Flash applications, but to say that <strong>someone might need it</strong> for reasons we cannot think about right now, and someone else might not need it because they are fine with recompiling everything from scratch at each single small change.</p>
<p>I don&#8217;t think there are some many Flash  applications that support scripting. I built myself a few of them for <a href="http://alittleb.it">my company</a> but they are for a particular purpose and probably the same motivations that made me decide to implement a scripting language are hard to find for other kind of applications.</p>
<p>As far as I know, the most of the applications that need to be flexible use a plugin system (more or less complicated &#8211; as <a href="http://a.viary.com">Aviary</a> for instance and <a href="http://hobnox.com">Hobnox</a>) and are fine with it because &#8211; we must say that &#8211; <strong>ActionScript is itself a scripting language</strong> and a SWF file is nothing more than a bunch of instructions (more or less) generated by compiled ActionScript.</p>
<p>But sometimes <strong>security</strong> comes into account, and it is hard (even if possible) to limit the funcionalities used by a plugin if this plugin is created by a 3rd party developer.</p>
<p>And so it comes into account scripting. With scripting we have <strong>full control over what a developer can do</strong> an how he can do that. We can limit it, provide builtin functionalities &#8230; But everything costs something, and building a fast/flexible/powerful/useful scripting languages is not that easy as it might seem.</p>
<p>This is why I decided to spend a bit of time in explaining the basics to implement a scripting language in actionscript to be able to script our applications. I&#8217;ll walk through all the passes &#8211; from lexing to bytecode generation and maybe a quick look into Virtual Machines &#8211; with the aim to provide someone (maybe in the far future) a bunch of documents to use as reference to build their own scripting language.</p>
<p>I&#8217;ll try to give an in depth look, but as far as the argument is quite complicated and full of facets, you may not find here the right solution for you. Nor the fastest or the most flexible. But it is better then nothing so if you are interested in that keep reading the blog 🙂</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2008/10/27/scripting-your-flash-applications/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Expressions evaluation at (almost) native speed</title>
		<link>https://blog.sephiroth.it/2008/07/01/expressions-evaluation-at-almost-native-speed/</link>
					<comments>https://blog.sephiroth.it/2008/07/01/expressions-evaluation-at-almost-native-speed/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Wed, 02 Jul 2008 00:00:00 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[applicationDomain]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[haxe]]></category>
		<category><![CDATA[HxASM]]></category>
		<category><![CDATA[swfcompiler]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=257</guid>

					<description><![CDATA[Finally I found a bit of time this weekend to do some other tests with expressions evaluation. The results are pretty interesting, even if obvious from some point of view. I took the ExpressionEvaluator I wrote as example for the first post about this topic, and then I edited a bit the code adding just &#8230; <a href="https://blog.sephiroth.it/2008/07/01/expressions-evaluation-at-almost-native-speed/" class="more-link">Continue reading <span class="screen-reader-text">Expressions evaluation at (almost) native speed</span></a>]]></description>
										<content:encoded><![CDATA[<p>Finally I found a bit of time this weekend to do some other tests with <strong>expressions evaluation</strong>.</p>
<p>The results are pretty interesting, even if obvious from some point of view. I took the ExpressionEvaluator I wrote as example for the <a title="Runtime expression evaluation in ActionScript" href="https://blog.sephiroth.it/2008/04/19/runtime-expression-evaluation-in-actionscript/">first post</a> about this topic, and then I edited a bit the code adding just in time <strong>AS Bytecode compilation</strong>.<br />
Thanks to that, expressions evaluation is much more fast and always safe because it runs in its own <em>ApplicationDomain</em>.</p>
<p>You can <a href="https://blog.sephiroth.it/wp-content/uploads/2008/07/ExpressionEvaluator_compiler.zip">download the sources here</a>, that includes the edited code and a test file. <strong>The test file runs 1 milion of iterations and may hang your browser or at worst case your system. Reduce the value of the ITERATIONS constant if you are not sure about the power of you machine</strong>.</p>
<p>If you want to read a bit more details about that, click on the link below to continue reading.</p>
<p><span id="more-257"></span></p>
<p>I did some simple modification to the existing classes:</p>
<ul>
<li>Added a <em>compile</em> method to the IExpression interface;</li>
<li>Added the <em>symbolNames</em> getter to the SymbolTable class, to retrieve a list of all the symbols defined inside the symbol table.</li>
<li>Converted hxasm to Actionscript (finxing some small issues to avoid to include all the Haxe support classes);</li>
<li>Implemented the <em>compile</em> method on all the AST nodes using HxASM;</li>
<li>Added an <em>SWFCompile</em> class that takes an IExpression and a SymbolTable and produces an SWF;</li>
<li>Edited the <em>CompiledExpression</em> class adding a <em>compile</em> method that generates an SWF starting from an <em>IExpression</em>.</li>
</ul>
<p>Adding just in time compilation was easy a really quick thanks to <a href="http://haxe.org/com/libs/hxasm">HxASM</a>. <strong>HxASM</strong> is an <a href="http://haxe.org">Haxe</a> library written by Haxe&#8217;s author (<strong>Nicolas Cannasse</strong>) that makes you able to write low level bytecode instructions in ActionScript an generate a <em>ByteArray</em> that contains an (hopefully) valid SWF. The SWF can then be loaded by <em>Loader.loadBytes</em> and executed.</p>
<p>It have been easy to port HxASM to ActionScript too, thanks again to the <strong>Haxe compiler which is able to generate AS3 code starting from Haxe code</strong>.</p>
<p>Compilation was achieved easilly because the expressions we are compiling are really simple. The compiler generates a class called <em>CompiledExpression</em> that has a method <em>execute</em> that can be called to execute the expression. All the values inside the symbol table are exposed as public properties.</p>
<p>Just to make you understand better, givin this expression:</p>
<p><code>sin( x * 10 + y - 11 )</code></p>
<p>SWFCompiler will generate the bytecode for this class:<br />
<code lang="actionscript">class CompiledExpression<br />
{<br />
public var sin: *;<br />
public var x: *;<br />
public var y: *;<br />
public function execute(): Number<br />
{<br />
return sin( x * 10 + y - 11 );<br />
}<br />
}</code><br />
As you can see the type of the class variables is generic, but you can improve the generator to understand if a variable should contain a number or a function.</p>
<p>The generated SWF can be loaded and run easilly:<br />
<code lang="actionscript">var loader: Loader = new Loader();<br />
var parser: Parser = new Parser(<br />
new Scanner( "sin( x / ( 4 / 2 * 2 - 2 + 2 * x / x ) ) * 100" ) );<br />
var compiled: CompiledExpression = parser.parse();<br />
var data: ByteArray = compiled.compile();<br />
loader.contentLoaderInfo.addEventListener( Event.COMPLETE,<br />
function( event: Event ): void<br />
{<br />
var info: LoaderInfo = ( event.target as LoaderInfo );<br />
var klass: Class = (<br />
info.applicationDomain.getDefinition( "CompiledExpression" ) as Class );<br />
var cp: Object = new klass();<br />
cp.x = 10;<br />
cp.sin = Math.sin;<br />
trace( "Result", cp.execute() );<br />
} );<br />
loader.loadBytes( data, new LoaderContext( false,<br />
new ApplicationDomain( ApplicationDomain.currentDomain ) ) );<br />
</code><br />
I build a simple test (shipped with the sources) that runs 1 milion times an expression using 3 methods: Evaluation, Compiled and Native.</p>
<p>Results (on my Mac Pro 8 Core, 4GB ram) are obvious:</p>
<pre>eval 1000000 iterations: 12939
<strong>compiled 1000000 iterations: 848</strong>
native 1000000 iterations: 371</pre>
<p>As you can see native execution is faster, but compiled version runs really well (you might want to optimize the generated code to make it run faster) and it is many times faster then AST evaluation.</p>
<p>If you want to run a complex expression many times and dont want to stress your Flash Plugin, you can use the compiled version.</p>
<p>Gotta run, see u next time!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2008/07/01/expressions-evaluation-at-almost-native-speed/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
		<item>
		<title>Top Down or Bottom Up for Expression Evaluation ?</title>
		<link>https://blog.sephiroth.it/2008/05/03/top-down-or-bottom-up-for-expression-evaluation/</link>
					<comments>https://blog.sephiroth.it/2008/05/03/top-down-or-bottom-up-for-expression-evaluation/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Sun, 04 May 2008 01:00:35 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[abstract syntax tree]]></category>
		<category><![CDATA[ast]]></category>
		<category><![CDATA[byacc]]></category>
		<category><![CDATA[gnu flex]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[yacc]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=252</guid>

					<description><![CDATA[Last time I wrote about Top Down parsing, a technique for parsing that can be easilly implemented manually using function recursion. Today it is time to talk about one of the other ways of parsing, called Bottom Up parsing. This technique try to find the most important units first and then, based on a language &#8230; <a href="https://blog.sephiroth.it/2008/05/03/top-down-or-bottom-up-for-expression-evaluation/" class="more-link">Continue reading <span class="screen-reader-text">Top Down or Bottom Up for Expression Evaluation ?</span></a>]]></description>
										<content:encoded><![CDATA[<p><a title="Runtime expression evaluation in ActionScript" href="https://blog.sephiroth.it/2008/04/19/runtime-expression-evaluation-in-actionscript/">Last time</a> I wrote about <a href="http://en.wikipedia.org/wiki/Top-down_parsing">Top Down</a> parsing, a technique for parsing that can be easilly implemented manually using function recursion.</p>
<p>Today it is time to talk about one of the other ways of parsing, called <a href="http://en.wikipedia.org/wiki/Bottom-up_parsing">Bottom Up</a> parsing. This technique try to find the most important units first and then, based on a language grammar and a set of rules, try to infer higher order structures starting from them. Bottom Up parsers are the common output of <a href="http://en.wikipedia.org/wiki/Parser_generator">Parser Generators</a> (you can find a good comparison of parser generators <a href="http://en.wikipedia.org/wiki/Comparison_of_parser_generators">here</a>) as they are easier to generate automatically then recursive parser because they can be implemented using sets of tables and simple state based machines.</p>
<p>Writing down manually a bottom up parser is quite a tedious task and require quite a lot of time; moreover, this kind of parsers are difficult to maintain and are usually not really expandable or portable. This is why for my tests I decided to port <a href="http://invisible-island.net/byacc/byacc.html">bYacc</a> (a parser generator that usually generates C code) and edit it so it generates ActionScript code starting from Yacc-compatible input grammars. Having this kind of tool makes things a lot easier, because maintaining a grammar (that usually is composed by a few lines) is less time expensive than working with the generated code (that usually is many lines long).<br />
I will not release today the port because actually I had not time to make sure it is bugfree and I&#8217;ve only a working version for Mac, but I plan to release it shortly if you will ever need it for your own tasks. My goal for today was to compare the speed of the parser I wrote with an automatically generated bottom up parser, to see which is the faster approach.</p>
<p>I created a bottom up parser which is able to execute the same expressions accepted by the expression evaluator I wrote last time. There are anyways some differences that &#8211; as you will probably and hopefully understand in the future &#8211; that make those parsers really different. Some will be disussed shortly here.</p>
<p>To do that I created a Yacc grammar and some support classes.<br />
The parser grammar is really simple and really readable:<br />
<code>%{<br />
%}<br />
%token NUMBER SYMBOL<br />
%left '+' '-'<br />
%left '*' '/'<br />
%left NEG<br />
%%<br />
program<br />
: expr                { Vars.result = $1; }<br />
;<br />
expr<br />
: expr '+' expr       { $$ = $1 + $3; }<br />
| expr '-' expr       { $$ = $1 - $3; }<br />
| expr '*' expr       { $$ = $1 * $3; }<br />
| expr '/' expr       { $$ = $1 / $3; }<br />
| '-' expr %prec NEG  { $$ = -$2; }<br />
| '(' expr ')'        { $$ = $2; }<br />
| SYMBOL '(' expr ')' {<br />
if( Vars.SYMBOL_TABLE[ $1 ] )<br />
{<br />
$$ = Vars.SYMBOL_TABLE[ $1 ]( $3 );<br />
} else<br />
{<br />
trace( "can't find function" );<br />
}<br />
}<br />
| SYMBOL              {<br />
if( Vars.SYMBOL_TABLE[ $1 ] )<br />
{<br />
$$ = Vars.SYMBOL_TABLE[ $1 ];<br />
} else<br />
{<br />
trace( "can't find symbol" );<br />
}<br />
}<br />
| NUMBER              { $$ = yyval; }<br />
;<br />
%%</code><br />
<strong>Continue reading the extended entry to see the results.<span id="more-252"></span></strong></p>
<p>It defined a simple expression grammar which includes addition, subtraction, multiplication, negation, division, variables, function calls and sub expressions. The tokens accepted are numbers (integers or simple floating point numbers), symbols and all the punctuation and operands needed.</p>
<p>The grammar uses a simple support class (called <em>Vars</em>) which is used to store a global symbol table and the result of the evaluation.<br />
As you might have noted there is a really big difference about how the expression will be evaluated here and how do I evaluate it in my original expression parser: the expression is executed as soon as a rule is matched, and it is not compiled into an intermediate <a href="http://en.wikipedia.org/wiki/Abstract_syntax_tree">abstract syntax tree</a>.<br />
Usually those kind of tools are used in conjunction with a lexer generator (<a href="http://en.wikipedia.org/wiki/Flex_lexical_analyser">Flex</a> is a good example of lexer generator &#8211; don&#8217;t confuse it with Adobe Flex!! :D), that is able to automatically generate the code for a scanner based in a set of rules usually defined by regular expressions. I didn&#8217;t ported this kind of tool because I was already having a working scanner for my needs that is a porting of the scanner used in the expression evaluator library:</p>
<p><code lang="actionscript"><br />
package it.sephiroth.expr2<br />
{<br />
	public class Lexer<br />
	{<br />
		private var _pos: int;<br />
		private var _pool: Array;<br />
		private var _source: String;<br />
		public function Lexer( source: String )<br />
		{<br />
			_pos = 0;<br />
			_source = source;<br />
			_pool = new Array();<br />
		}<br />
		public function scan(): Object<br />
		{<br />
			if( _pool.length > 0 )<br />
			{<br />
				return _pool.shift();<br />
			}<br />
			skipWhites();<br />
			if( isEOF() )<br />
			{<br />
				return null;<br />
			}<br />
			var c: String = _source.charAt( _pos++ );<br />
			switch( c )<br />
			{<br />
				case '+':<br />
				case '-':<br />
				case '*':<br />
				case '/':<br />
				case '(':<br />
				case ')':<br />
				case ',':  return  { type: c.charCodeAt( 0 ), val: c };<br />
				default:<br />
					var buf: String = "";<br />
					var code: int = c.charCodeAt( 0 );<br />
					if( isNumber( code ) )<br />
					{<br />
						var num: Number;<br />
						while( isNumber( code ) )<br />
						{<br />
							buf += c;<br />
							if( isEOF() )<br />
							{<br />
								++_pos;<br />
								break;<br />
							}<br />
							c  = _source.charAt( _pos++ );<br />
							code = c.charCodeAt( 0 );<br />
						}<br />
						if( c == '.' )<br />
						{<br />
							buf += c;<br />
							c  = _source.charAt( _pos++ );<br />
							code = c.charCodeAt( 0 );<br />
							while( isNumber( code ) )<br />
							{<br />
								buf += c;<br />
								if( isEOF() )<br />
								{<br />
									++_pos;<br />
									break;<br />
								}<br />
								c  = _source.charAt( _pos++ );<br />
								code = c.charCodeAt( 0 );<br />
							}<br />
							num = parseFloat( buf );<br />
						}else<br />
						{<br />
							num = parseInt( buf );<br />
						}<br />
						--_pos;<br />
						return  { type: Token.NUMBER, val: num };<br />
					}<br />
					if( isAlpha( code ) || ( c == '_' ) )<br />
					{<br />
						while( isAlpha( code ) || ( c == '_' ) || isNumber( code ) )<br />
						{<br />
							buf += c;<br />
							if( isEOF() )<br />
							{<br />
								++_pos;<br />
								break;<br />
							}<br />
							c  = _source.charAt( _pos++ );<br />
							code = c.charCodeAt( 0 );<br />
						}<br />
						--_pos;<br />
						return  { type: Token.SYMBOL, val: buf };<br />
					}<br />
					break;<br />
			}<br />
			return null;<br />
		}<br />
		public function pushBack( token: Token ): void<br />
		{<br />
			_pool.push( token );<br />
		}<br />
		protected function isNumber( c: int ): Boolean<br />
		{<br />
			return ( c >= 48 ) && ( c < = 57 );
		}
		protected function isAlpha( c: int ): Boolean
		{
			return ( ( c >= 97 ) && ( c < = 122 ) ) || ( ( c >= 65 ) && ( c < = 90 ) );
		}
		protected function isEOF(): Boolean
		{
			return ( _pos >= _source.length );<br />
		}<br />
		protected function skipWhites(): void<br />
		{<br />
			while( !isEOF() )<br />
			{<br />
				var c: String = _source.charAt( _pos++ );<br />
				if( ( c != " " ) && ( c != "\t" ) )<br />
				{<br />
					--_pos;<br />
					break;<br />
				}<br />
			}<br />
		}<br />
	}<br />
}<br />
</code></p>
<p>After compiling the grammar with my parser generator and having written some support code we can use to do our tests, I finally had my small comparison test suite.  A quick note about the parser: It should be used only for testing and if you want to have a fast and safe expressions evaluator I suggest you to build your own or &#8211; if you can wait &#8211; wait a bit more 😉  The test can be easilly run from the command line without the need fo having Flex Builder (but you need the Flex SDK that you can freely download from the <a href="http://opensource.adobe.com/wiki/display/flexsdk/">Adobe Open Source Website</a>); I choose a quite long expression to make the execution and parsing time a bit high.  Here is the test I ran:</p>
<p><code><br />
package {<br />
	import flash.display.Sprite;<br />
	import flash.utils.getTimer;<br />
	import it.sephiroth.expr.CompiledExpression;<br />
	import it.sephiroth.expr.Parser;<br />
	import it.sephiroth.expr.Scanner;<br />
	import it.sephiroth.expr2.Vars;<br />
	public class TestExprEval extends Sprite<br />
	{<br />
		public static const ITERATIONS: int = 10000;<br />
		public static const EXPRESSION: String = "sin( x / ( 4 / 2.0 * 2 + (- 2.12345 + 2.12345) * x / x ) ) * 100 - ( sin( x / ( 4 / 2.0 * 2 + (- 2.12345 + 2.12345) * x / x ) ) * 100 )";<br />
		public function TestExprEval()<br />
		{<br />
			runTests();<br />
		}<br />
		private function runTests(): void<br />
		{<br />
			trace( "Running tests (this may take a while)" );<br />
			runTopDown();<br />
			runBottomUp();<br />
		}<br />
		private function runTopDown(): void<br />
		{<br />
			var sum: Number = 0;<br />
			for( var i: int = 0; i < ITERATIONS; ++i )
			{
				var t: Number = getTimer();
				var scanner: Scanner = new Scanner( EXPRESSION );
				var parser: Parser = new Parser( scanner );
				var compiled: CompiledExpression = parser.parse();
				compiled.execute( {
					'x'    : Math.random(),
					'sin'  : Math.sin,
					'cos'  : Math.cos
				} );
				sum += ( getTimer() - t );
			}
			trace( "Top Down parser:", sum / ITERATIONS );
		}
		private function runBottomUp(): void
		{
			var sum: Number = 0;
			Vars.symbolTable.sin = Math.sin;
			for( var i: int = 0; i < ITERATIONS; ++i )
			{
				Vars.symbolTable.x = Math.random();
				var t: Number = getTimer();
				var myparser: TestBottomUpParser = new TestBottomUpParser();
				myparser.yyparse()
				sum += ( getTimer() - t );
			}
			trace( "Bottom Up parser:", sum / ITERATIONS );
		}
	}
}
import it.sephiroth.expr2.BottomUpParser;
import it.sephiroth.expr2.Lexer;
class TestBottomUpParser extends BottomUpParser
{
	private var _lexer: Lexer;
	public function TestBottomUpParser()
	{
		super();
		_lexer = new Lexer( TestExprEval.EXPRESSION );
	}
	override public function yylex(): int
	{
		var value: Object = _lexer.scan();
		if( value == null )
		{
			return 0;
		}
		set_yylval( value.val );
		return value.type;
	}
}
</code></p>
<p>As you can see the test includes also the instantiation of all the objects needed. Running that from the command line, I obtained this result:<br />
</code><code>$ mxmlc -debug=true TestExprEval.as<br />
Loading configuration file /Applications/Adobe Flex Builder 3 Plug-in/sdks/3.0.0/frameworks/flex-config.xml<br />
/Users/gabriele/Desktop/TestExprParsers/src/TestExprEval.swf (11647 bytes)<br />
$ fdb TestExprEval.swf<br />
Adobe fdb (Flash Player Debugger) [build 3.0.0.477]<br />
Copyright (c) 2004-2007 Adobe, Inc. All rights reserved.<br />
Attempting to launch and connect to Player using URL<br />
TestExprEval.swf<br />
Player connected; session starting.<br />
Set breakpoints and then type 'continue' to resume the session.<br />
[SWF] Users:gabriele:Desktop:TestExprParsers:src:TestExprEval.swf - 25.942 bytes after decompression<br />
(fdb) continue<br />
[trace] Running tests (this may take a while)<br />
[trace] Top Down parser: 0.6979<br />
[trace] Bottom Up parser: 0.9063<br />
(fdb) quit</code><br />
This is not totally unexpected for such a simple grammar, but the execution time is much more better (around 20%) using a manually written top down parser than using an autogenerated bottom up parser; I say it is not totally unexpected because the performances should change as the grammar becames more complex, but in our situation this is what we get. And we can also achive a better result if we precompile the expression into an AST and the execute it. Add this code to your test and add the function call inside the <em>runTests</em> method:<br />
<code><br />
private function runTopDownCompiled(): void<br />
{<br />
	var sum: Number = 0;<br />
	var scanner: Scanner = new Scanner( EXPRESSION );<br />
	var parser: Parser = new Parser( scanner );<br />
	var compiled: CompiledExpression = parser.parse();<br />
	for( var i: int = 0; i < ITERATIONS; ++i )
	{
		var t: Number = getTimer();
		compiled.execute( {
			'x':     Math.random(),
			'sin'  : Math.sin,
			'cos'  : Math.cos
		} );
		sum += ( getTimer() - t );
	}
	trace( "Top Down parser (precompiled):", sum / ITERATIONS );
}
</code><br />
And then run again to see the result:<br />
</code><code>$ mxmlc -debug=true TestExprEval.as<br />
Loading configuration file /Applications/Adobe Flex Builder 3 Plug-in/sdks/3.0.0/frameworks/flex-config.xml<br />
/Users/gabriele/Desktop/TestExprParsers/src/TestExprEval.swf (11794 bytes)<br />
$ fdb TestExprEval.swf<br />
Adobe fdb (Flash Player Debugger) [build 3.0.0.477]<br />
Copyright (c) 2004-2007 Adobe, Inc. All rights reserved.<br />
Attempting to launch and connect to Player using URL<br />
TestExprEval.swf<br />
Player connected; session starting.<br />
Set breakpoints and then type 'continue' to resume the session.<br />
[SWF] Users:gabriele:Desktop:TestExprParsers:src:TestExprEval.swf - 26.272 bytes after decompression<br />
(fdb) continue<br />
[trace] Running tests (this may take a while)<br />
[trace] Top Down parser: 0.698<br />
[trace] Bottom Up parser: 0.9001<br />
[trace] Top Down parser (precompiled): 0.0315</code><br />
This is really a good thing: as we can see, the most of the time is taken by the parser and the scanner, and if we remove their execution from the loop and just evaluate the generated AST, we get a great speed improvment. As far as I know this is quite good if you have a set of expressions that does not vary and do you want to execute them many times.</p>
<p>And this is why quite all the languages precompile their source code into bytecode before execution.</p>
<p>Unfortunately it is time to go now, but I&#8217;ve really a lot of stuff to show you next week (the parser generator, a way to &#8211; try to &#8211; write an expression evaluator that evaluate expressions at near-native speed &#8230;) so stay tuned as usual!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2008/05/03/top-down-or-bottom-up-for-expression-evaluation/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Runtime expression evaluation in ActionScript</title>
		<link>https://blog.sephiroth.it/2008/04/19/runtime-expression-evaluation-in-actionscript/</link>
					<comments>https://blog.sephiroth.it/2008/04/19/runtime-expression-evaluation-in-actionscript/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Sat, 19 Apr 2008 15:00:00 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[scripting languages]]></category>
		<category><![CDATA[abstract syntax tree]]></category>
		<category><![CDATA[ast]]></category>
		<category><![CDATA[evaluation]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[lexer]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[postfix]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=250</guid>

					<description><![CDATA[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&#8217;ve not enought time &#8230; <a href="https://blog.sephiroth.it/2008/04/19/runtime-expression-evaluation-in-actionscript/" class="more-link">Continue reading <span class="screen-reader-text">Runtime expression evaluation in ActionScript</span></a>]]></description>
										<content:encoded><![CDATA[<p>Finally I have a bit of free time to write down this post.<br />
Some days ago a guy on the <a href="http://sephiroth.it/phpBB/">forum</a> asked about how to <strong>evaluate at runtime simple mathematical expressions</strong> with support for symbols and function calls.</p>
<p>Now that the <em>eval</em> function have been removed from the language for reasons I&#8217;ve not enought time to talk about, we need to parse and execute those expressions manually.</p>
<p>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 <a href="http://en.wikipedia.org/wiki/Lexical_analysis">Scanner</a> (or lexer &#8211; however you want to call it) to tokenize the expression, a <a href="http://en.wikipedia.org/wiki/Parser">Parser</a> that convert the expression into an <a href="http://en.wikipedia.org/wiki/Abstract_Syntax_Tree">Abstract Syntax Tree</a> and a simple routine that evaluates that AST using a <a href="http://en.wikipedia.org/wiki/Symbol_table">Symbol Table</a> as evaluation context.</p>
<p>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.</p>
<p>Here is a simple example that shows how does the code works:<br />
<code lang="actionscript"><br />
import it.sephiroth.expr.CompiledExpression;<br />
import it.sephiroth.expr.Parser;<br />
import it.sephiroth.expr.Scanner;<br />
public class Example<br />
{<br />
public static function run(): void<br />
{<br />
var expression: String = "sin( x / ( 8 / 2 + (-0.12 + 2.3) * x / x ) ) * 100";<br />
var scanner: Scanner = new Scanner( expression );<br />
var parser: Parser = new Parser( scanner );<br />
var compiled: CompiledExpression = parser.parse();<br />
var context: Object = {<br />
x:    100,<br />
sin:   Math.sin,<br />
cos:  Math.cos<br />
};<br />
trace( 'Postfix:', compiled.toString() );<br />
trace( 'Result:', compiled.execute( context ) );<br />
}<br />
}<br />
</code><br />
Before I forgot, you can download the source code here with a simple example included.</p>
<p><span id="more-250"></span></p>
<p>Building an expression parser and evaluator is quite a simple task if you don&#8217;t plan to write a superfast library or a superpowerful engine. The steps are something like <em>predefined</em> and can be subdivided into tree groups:</p>
<ul>
<li><strong>Lexical analysis</strong></li>
<li><strong>Parsing</strong></li>
<li><strong>Evaluation</strong></li>
</ul>
<p>My code simply follow those steps to make you able to evaluate an expression. Before starting with the code anyways it is really important to define a set of rules and functionalities our expression evaluator will include. This is foundamental because based on the features the operations performed during the tree steps above may vary.</p>
<p>In my code I simply want to accept integer and float numbers (float numbers defined using the simple <em>dot syntax</em>), negative numbers, variables, function calls, basic operations (subtract, add, multiply, divide and negate) and parethesis for grouping.</p>
<h3>Lexical Analysis</h3>
<p>During Lexical analysis the <strong>input string is analyzed</strong> to provide the parser a set of chunks, called <strong>tokens</strong>, that can be <strong>interpreted easilly by our code</strong>. The main goal of the lexical analysis is just to group togheter some characters and give them a meaning (for instance: 1 is a number, / the divide operation and so on), discarding the unuseful ones (like spaces). At this stage there is <strong>no validation of the input</strong>, because the lexical analyzer (<em>Scanner.as</em>) doesn&#8217;t know anything about how does the input will be interpreted.<br />
The lexical analyzer generates a stream of tokens (<em>Token.as</em>) of different type (<em>TokenType.as</em>) which are then feeded to the Parser.</p>
<h3>Parsing</h3>
<p>The Parsing phase is one of the most important and is when the program starts to take a streams of token and give a meaning to a set of them. During this step the Parser (<em>Parser.as</em>) <strong>reads from the Scanner the tokens</strong> as needed and, given a defined Grammar, is able to group them togheter and <strong>understand if the input is grammatically correct</strong> or not.<br />
In our sitatuation the grammar is implicit: the expression must be formatted correctly, every open parenthesis must match a closed one and function calls allows one or more arguments. When working on more complex parsers, the grammar may became much more complex (think about a whatever language parser for instance), and so the technique I used (called <a href="http://en.wikipedia.org/wiki/Recursive_descent_parser">Recursive Descent Parsing</a>) risk to become too difficult to use, too time and memory consuming. At this stage may be helpful to use <strong>parser generators</strong>, but I&#8217;ll talk about that the next time because I&#8217;ve something interestingto show you.<br />
During this phase the input is validated but also other two really important tasks are performed: the first one is that <strong>all the identifiers found inside the input are recorded inside a Symbol Table</strong> (<em>SymbolTable.as</em>), than later will be filled at runtime with valid values for the indetifiers to execute the expression correctly. The second one is that <strong>the input is translated into an intermediate form</strong> that is <strong>easier for a computer language to deal with</strong>: the Abstract Syntax Tree (<em>it.sephiroth.expr.ast.*</em> package). The <strong>AST</strong> is a tree structure where each node rappresents a portion of code that then can be analyzed easilly using a tree walker or other means. When evaluating expressions an AST is not always useful because <strong>expressions evaluation may be performed using a stack based direct execution</strong>.</p>
<h3>Evaluation</h3>
<p>When the AST is ready, we are sure that the expression is syntactically valid and can be executed providing the right <strong>context</strong>. A context is simply the variables and functions that are needed by the expression to return a value, and are assigned to the identified included inside the Symbol Table. When the expression is executing, it requests from the symbol table the values needed. In my code the evaluation is performed by each AST node itself, but we may use also a tree walker to walk each node and execute it based on its type.</p>
<p>This is a super general introduction about how does the code works, hope to have a bit of free time to go more in depth about this. Just to show you something working, here is a simple Flex examples that plots an expression. The potting is not accurate, but the example can be used to give you an idea about how does the code may be used. With the separation provided, we will be able to replace the single parts of the code with improved versions, obtaining anyway a correct result.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2008/04/19/runtime-expression-evaluation-in-actionscript/feed/</wfw:commentRss>
			<slash:comments>25</slash:comments>
		
		
			</item>
	</channel>
</rss>
