April 2004 Archives

What did you learn today?

| 5 Comments | 1 TrackBack

I can't believe I didn't know this one... Actually, I think I did know it, but I've been coding with a certain style for so long that it influenced my view of syntax requirements.. For example, did you know that you can write code that looks like this...

x = String.

            fromCharCode(123);

trace(x);  // {

... and it works just fine? The above behaves just like x = String.fromCharCode. The extra spaces, line feeds, tabs, etc, are all ignored around the dot operator.

Similarly,

var a:Array =  new Array("1", "2", "3");

trace(a.   join("."));

... traces "1.2.3" to the output window, just as expected.

I don't recall encountering this before in my years of programming. I might've, but haven't thought about it or just glazed over it. I've always omitted any sort of spaces around the . operator (and the -> operator in C++) as a habit (example: Class.method, not Class    .   method). It somewhat surprised me to find out that whitespace like this is ignored.. and not just by ActionScript. Java, C# and C++ all behave similarly. However, ColdFusion will generate a syntax error - 'The variable StructTest. ends with a "." character. You must supply an additional structure key or delete the "." character.'

I'm not recommending that you space your code like I have above, but it's interesting to note that you can if you want to. It's easy to envision situations where having that type of freedom is useful, but I think it would pollute code readability if used liberally.

Learn something new every day....

Alright, it's compiler naming time...

| 15 Comments

So last night was a big step for me in regards to the command line ActionScript compiler that I've mentioned in a few recent posts. I now have, for the most part, a working ActionScript syntax checker. I still have some things to finish up before releasing anything, but before a release happens I need help in naming this bad boy.

My development name for the project thus far has been "flasc" - Flash ActionScript Compiler. Pretty generic, straightforward, and not really creative - the "fla-stuff" has already been done (and it sounds too much like flasm).

Again, what I have done so far is not a fully functional compiler... yet. Right now there is no code generation. However, I wanted to release "flasc" as a syntax checker for now because it would potentially help those of us who use 3rd party editors like SE|PY. Additionally, now is a good time for me to create a sourceforge project as there's a pretty good head start on the project and it would be nice to have a few other sets of hands coding with me.

So, sometime soon I'm hoping to release the syntax checker. Like I said before I have some things to polish up first, and I need a new name. I don't want to give a target date because I've been coding this in my free time and I don't want to make a promise that I can't keep (we all know how free time fluctuates). The end of the month is probably reasonable, but again, no promises.

Anyway, I'm looking for some input. In development, "flasc" worked fine.. but I'm looking for a different project name to release it under (which will, in the future, be the same name that I'll apply to sourceforge with). Any ideas?

PHP Easy Templating System

| 11 Comments

Easy Template System is a PHP script that allows for the easy creation of web pages based on a user-defined template. Think of it as a "php-stylesheet." Everyone has heard "separate your content from presentation" before.. and this seems to be a pretty good way to go about it.

You define a template with placeholders for variables, set those variables in a php page (either hard-coded the content, or pull it from a database), and then run the page variables against the template. The template will then generate the HTML, replacing variables where necessary. This will separate the structure of the page from the content itself. Of course, you can then use CSS to generate the visuals and adjust the look of the page structure.. but by using a template system like this you can update an entire website by changing one file, instead of possibly thousands (similar to just including portions of HTML at various points in a page).

Some of the advanced examples are pretty killer. It looks like this is pretty flexible system. Has anyone been using this for any production websites, or have any experience with it? What do you think? Feel free to post your thoughts about ETS in the comments.

http://ets.sourceforge.net/.

Do you ";"?

| 17 Comments

In creating an ActionScript compiler, the ECMAScript Language Specific has been the single most valuable source of information for me.

Section 7.9 of the ECMA-262 Edition 3 document outlines how automatic semicolon insertion works in ECMAScript. Because ActionScript is based on this standard, not all of your statements require semicolons to terminate them. I'm sure you'll all aware of this fact by now... Check this out though:

b = 1;
c = 5;
x = {test:function() {
   trace("x.test() called");
 }};

a = b + c
(x).test();

trace("a = " + a); // a = NaN

Alright, so look at the code where we set a... b + c is 6, which looks like it should be the value of a. However, we trace out NaN. Why? The reason is because, according to the standard, a semi colon is not automatically inserted after c. In fact, the statement is actually parsed as..

a = b + c(x).test();

...which doesn't seem quite right based on the white space we specified. To the parser though, the parenthesis on the next line are interpreted as a function call. This explains why a was set to NaN - the number b plus undefined (there is no function c), returns NaN.

Why am I telling you this? Simple... use semicolons! Personally, I think it's a lazy habit to not use them. Using them will increase code clarity as purpose is made clear. Additionally, not using them can get you in trouble. There are times when they are not inserted automatically for you when you might think they should be (as in the example above). Rather than have the compiler think for you, best practice says you should explicitly tell it what you want it to do...

Food for thought... enjoy your weekend everyone!

Flash Reserved Word Listing

| 6 Comments

I've mentioned to a few people here and there that I'm working on a standalone actionscript compiler. This is, in fact, still true. The first phase is more or less complete, and I'm currently in the process of creating the ActionScript grammatical rules for the second phase.

A compiler, in a nutshell, is just a text parser with some transformations applied (how's that for an over-simplification?). A typical compiler consists of 4 or more phases, with each phase building off of the previous.

The first phase is the lexical analysis phase. The purpose of this phase is to determine what tokens are in the input stream (file, console, etc), and strip out any comments. For example, the statement "if (a > b)" might be tokenized as the following series of tokens: Token.IF, Token.L_PAREN, Token.IDENTIFIER "a", Token.GT, Token.IDENTIFIER "b", Token.R_PAREN.

Anyhow, in order to perform lexical analysis correctly all of the reserved words of the language must be known. Each reservered word gets its own special token to make it easier to parse the input file (variables are all lumped into the "identifier" token). Below a list of all of the reserved words (keywords), and what version of the Flash Player supports them.

Flash Reserved Word Listing
Flash Player 2on
Flash Player 3ifFrameLoaded
Flash Player 4add, and, break, case, continue, default, do, else, eq, ge, gt, if, le, lt, ne, not, or, switch, tellTarget, while
Flash Player 5delete, for, function, in, new, onClipEvent, return, this, typeof, var, void, with
Flash Player 6instanceof
Flash Player 7catch, finally, throw, try
ActionScript 2.0 - Flash Player 6class, dynamic, extends, get, implements, import, interface, intrinsic, private, public, set, static
"Special" Functions and Constantscall, duplicateMovieClip, eval, fscommand, getProperty, getTimer, getURL, getVersion, gotoAndPlay, gotoAndStop, int, length, loadMovie, loadMovieNum, loadVariables, loadVariablesNum, mbchr, mblength, mbord, mbsubstring, NaN, nextFrame, nextScene, Number, ord, play, prevFrame, prevScene, print, printNum, random, removeMovieClip, set, setProperty, startDrag, stop, stopAllSounds, stopDrag, substring, targetPath, toggleHighQuality, trace, unloadMovie, unloadMovieNum
Reserved for future useabstract, boolean, byte, char, const, debugger, double, enum, export, final, float, goto, int, long, native, package, protected, short, synchronized, throws, transient, volatile

Special functions are ones that are not reserved words, but have compile-time parameter checking. For an example, trace is not reserved yet "trace()" will give an error saying it requires exactly 1 parameter. Also, "NaN()" will report "A function call on a non-function was attempted."

Note that the keywords listed are grouped by which Flash Player supports them, and not by which Flash version they were introduced. Flash Player 4 supports the bytecodes generated by switch..case..default statements, yet those keywords were introduced in Flash MX. I separated the keywords like this because my compiler will support a targetted Flash Player version, and will generate errors if you attempt to use a feature not included in the target player. For instance, you can't use try..catch..finally if you're compiling for Flash Player 5.

An easy way to determine if a word is reserved is simply to try and use it as a function.

	function dynamic() { }; // reports an error

I don't believe that I've missed anything, but I wanted to post this and see if anyone can catch any mistakes I may have made. Also, I'm thinking that the "special" functions like trace should actually be reserved words. Sure you can write a function trace(), but what's the point of allowing that since you can't override it anyway?

However, a problem arises with words like duplicateMovieClip though. Used as a standalone function Flash will check parameters.. but used as a function on a MovieClip it won't. That may get a bit tricky, and I haven't given it much thought yet...

Anyway, expect to hear more about this in the future... I'm still in the somewhat early/planning stages, but once I complete the parser I'll release it open-source and hopefully gather some support. By the way, I'm coding the compiler in Java.

phpMyAdmin as a Flash RIA

| 11 Comments | 1 TrackBack

I stumbled across this link last night that I thought would be of interest to a lot of you. Instead of using phpMyAdmin to manage a MySQL database, now you can use a Flash RIA instead.

As I played with the demo a bit, I noticed that It seems to be pretty feature complete. You can check it out at Darren's $5 Script Archive. Sure it's not free, but since it's priced at $5 it shouldn't be out of reach, especially if it's something that you can really use.

Check out the live demo on the link above. I was wondering why someone hadn't made something like this sooner...

So who's gonna build something like this in Flex? :-)

Flex.org - The Directory for Flex

Archives