Saturday, July 16, 2011

AS3 embedded language virtual machine and compiler beta release

Download my simple, domain specific still-in-beta embedded-in-ActionScript scripting language I am writing for my game! Includes compiler and virtual machine.

I have found that recently some people have been coming to my blog in search of a scripting language that one can embed in ActionScript. I have written a few blog entries about my look into this and my accomplishments but they have been lacking in code and have been nothing more than updates to no one about my own progress. I haven't been updating as often as I'd like and I would really like to show off the actual virtual machine I've created along with the compiler so I have decided since people have been looking for it, I'll bite the bullet and put it up. The scripting language I have written is called CandyScript. It is extremely simple so far and not super powerful, but it does what I need it to do for now. It is also still in "beta" because I am actively working on it; this release isn't really a library, it's just a simple code base people interested can learn from.

The reason why I haven't yet posted anything is because I am also a newbie at creating domain specific languages. I originally thought that I would hold off until I had an actual library but to be honest, library bloat is always annoying and a lot of people don't want some blogger's "library" but just some code they can look at/use/play around with. My scripting language is a secondary project to my game; I am not going to generalize it for everyone's needs and maintain it as such so that it is a true library so why pretend that I will? Instead, I think it is a fairly straight-forward virtual machine and compiler grammar set that I wish I could have looked at when I was figuring out the stuff and that I hope is useful for people who want to see a simple machine in action. It isn't file after file after linked library; if you have an initial grounding in the topic you should be able to step through my implementation in a debugger and read through the code and get an idea of what is happening. Even before then, you can play around and see what I did just by writing scripts and sending them through my VM. There are only two "meaty" classes: an interpreter which loads a compiled script into memory and a virtual machine which executes the script. Seven tiny extra classes provide support to these two main classes (a few are just dummy classes written to represent types such as null). Likewise, the ANTLR grammar is split into two files; one which parses the C-subset scripting language I wrote into an abstract syntax tree and one which turns the tree into a compiled XML file.

I would just like to make one thing clear; I hope I can help people understand better about virtual machines by using and looking at my code. But I am also pretty much a newbie to this field as well. There may be naive mistakes and implementations littering my code; I do not know. All I know is it seems to work for me. It is in beta, however, and I am designing it to my needs, not generalized needs. I can't vouch for it as though I am an expert level DSL writer who knows all the tricks of writing interpreters and compilers; to an expert this may (or may not) look laughably amateurish. All I know, once again, is that it works and seems to work pretty fast for a scripting language literally built on top of another scripting language :) I hope people can learn from this and that someone doesn't come by and telling me I am leading everyone terribly astray; even if I am, my blog certainly doesn't generate enough traffic for me to find that out :)

Friday, July 1, 2011

Adult Swim scheduling bug

As I have mentioned before, I run the single serving site, IsFamilyGuyCurrentlyPlayingOnAdultSwim.com. Tonight, the first of July, as I went to check if Family Guy was indeed playing, I noticed the site was displaying my catchall bug informing the user something went wrong. Nervous that Adult Swim had retooled their scheduling or caught on and blocked me from leeching their scheduling data, I began to investigate.

As it turns out, it was a bug I had never caught in which the first day of the month (which begins at 12 midnight) was looking backwards for the previous day's schedule (which is what late night programming is technically classified under). So, being the first of July, the late night Adult Swim programming is still technically part June 30th's programming block. However, my naive code was simply subtracting 1 from the date so that it was looking for day 0 and not day 30 (the last day of June).

Adult Swim stores schedules in XML files located at URLs such as http://schedule.adultswim.com/asXml/1.EST.xml. This link may appear to lead to a blank page but if you look at the source, it is an XML file containing show data. The naming format is fairly simple: the "1" is the date of the current month so that if it is July, then 1 represents July 1st. The "EST" means this is the schedule for Eastern Standard Time (they have all the American time zones). So basically, on the first of the month, my website knew it was first but technically the scheduling for all late night programming on the first is really part of yesterday. Unfortunately, my error resulted in looking for a file named "http://schedule.adultswim.com/asXml/0.EST.xml" instead of "http://schedule.adultswim.com/asXml/30.EST.xml".

Before, I said the format "1.EST.xml" meant the first of the current month for Eastern time zones in the US? Well one catch, apparently if it is the first, then "30.EST.xml" or "31.EST.xml" or any other "last day of month" schedule points to last month's 30th or 31st or what have you. Luckily, in PHP, it is easy to work with dates and times and calling date("t", mktime(0, 0, 0, date("n") - 1, 1, date("y"))); returns the last day of the last month. So, in August this will return 31 (31 days in July) but right now (in July) it returns 30 (30 days in June).

What is interesting is that I went to confirm that the official Adult Swim schedule was working and that the "30.EST.xml" and the like all actually pointed to June 30th's programming and not July's. When loading the page I was greeted with this:

It looks like Adult Swim has a similar issue; the schedule does not load and inspecting the page reveals it is trying to load "undefined.EST.xml". I haven't looked at this page in a while, but I assume it's a similar error of judgement when programming in terms of "it's 2 AM, we need to actually pull the programming from yesterday so let's do some subtraction and forget about how the day before the first isn't the 0th".