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

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".

Sunday, June 5, 2011

ANTLR translator for embedded scripting language

Update 7/16/2011 I have posted code for this.

Last month, I wrote a post detailing the building of a scripting language interpreter inside of ActionScript. The interpreter worked from abstract syntax trees represented in XML, however the idea of programming in this environment is not too appealing. Therefore, I knew I had to build a translator to take a C-like syntax and translate it into my XML abstract syntax tree format. I chose to use ANTLR as my parser generator and used the related StringTemplate template engine to bridge the gap between parsed AST and XML representation. The Definitive ANTLR Reference by Terrence Parr (the creator of ANTLR) was a lot of help when it came to learning about how to use ANTLR and about non-trivial parsing in general.

So far my language consists of functions that may be called by various aspects of the game engine and that may call each other. Various types are supported, type casting is implementing, scoping is implemented, function returns are implemented and while/for/if statements are all supported. I am probably going to implement array support as well. Now that I can write complicated scripting procedures, a lot more opportunities will open for game design when I begin that phase but the code will remain penetrable since it is compiled from a readable C-subset syntax.

Monday, May 23, 2011

Media company One Line Creative's website launch

I recently had the pleasure of implementing a website for Thomas Byrd and One Line Creative which has recently launched. One Line Creative is a media production company and you can check out their videos on the website and also the One Line Creative blog.

Monday, May 16, 2011

Embedded scripting language in ActionScript

Update 7/16/2011 I have posted code for this.

I have recently been working on various projects both for myself and others but one thing which I have been focused on is creating a scripting language for the game I am working on. A lot of my entries so far have been about spin-off projects relating to the work I am doing for my ActionScript game and this is no different.

I knew that I wanted to create a scripting language that resembled C in it's structure. Ultimately, the idea was to have a function based language, however, where functions are written out in a style similar to C but there are special calls that can be made throughout the script and functions are either called by triggers on the map (handled by the game engine) or specially, such as scripts to run automatically at such points as level start, level end, et cetera.

The actual format of the scripting language is being handled separately. I have an ANTLR grammar that I am working on to parse the actual code and compile it and I will maybe post about that in the future, but this post documents handling and interpreting the intermediate representation which is what my compiler will eventually output from the C-like input.

Friday, April 22, 2011

Build ZDoom from source in Xcode 4

Update 5/8/2011 I have received word that this tutorial works with Xcode 3.2.4 as well as Xcode 4. Thanks to andreb. I'd like to thank him for pointing out I could use the \$SRCROOT variable instead of /absolute/path/to/workspace/ZDoom-Xcode.

When I was younger, I used to make maps and other mods for the popular old school game Doom. The community of mappers for the game was already quite established by the time I got into it, due to Doom being one of the original games to develop a large modding community. Even after it's heyday (and still to this day) the community thrives due to two things: the game is classically enjoyable and fun to mod for and the existence of source ports has kept the game relatively up to date. Almost no one runs vanilla Doom these days, due to the slow but steady deprecation of DOS support on many systems but plenty of programmers have kept the game alive by creating modified engines (or source ports) which run on modern operating systems without a hitch and add features to the game to make it more interesting while allowing it to keep it's charm and simplicity. This is thanks to John Carmack and id Software's open sourcing of the Doom engine back in 1997. ZDoom is one of the more popular updated engines and has been around for a long time.

A few years ago, I switched from PC to Mac and although I am happy with this change the one thing I left behind was gaming. Although I don't care too much these days if the latest game is being ported to OS X or not, I did miss some of the old games I used to play. There are so many free maps and add ons from the Doom community that just buying Doom, downloading a source port and getting some mods is enough to keep one entertained indefinitely. It will also only set you back about ten dollars to purchase Doom, making it extremely cheap gaming.

There are plenty of ports for Doom which release OS X binaries and these are all great ports, however ZDoom has such a large number of maps and features that you miss out on a lot of good mods if you aren't running it. I had never gotten around to running it on OS X before because they didn't have any binaries, but I finally decided to dive in and build it myself. Luckily, they support OS X by using CMake, but unluckily, nothing works perfectly and this is what had halted me from getting too deep before. As great as running CMake and then make would be, the build process doesn't work quite that easily. Before I continue however, I discovered that the DRD Team already hosted unofficial OS X builds of ZDoom so if you would like to spare yourself a headache and just download those, you can run ZDoom on OS X without building anything.

Wednesday, April 20, 2011

Space Opera, an x86 assembly arcade game

 Screenshot of Space Opera, in action

Back in 2008, as a freshman in my second semester of college, I wrote a game in x86 assembly language as my final project for a core computer science class I was taking. The class was Computer Systems Organization, taught by Professor Nathan Hull and I think it was one of the more important classes that I took. This class came fairly early in the track (once you were out of the introductory Java classes) and it was one of the classes that began to explore the field of programming and computer science on a lower level than contrived OOP simulations of unrealistic problems.

A lot of people do not like learning assembly and it certainly is directly inapplicable in most fields these days, however I am one of the people who believes it is very important to at least have some experience in it because you really start to understand what is happening behind the scenes in any language once you have worked in it. To have such a low level knowledge of what is happening is useful and interesting, and I credit this class with helping to draw a clear line between my high school self and post-high school self in terms of understanding paradigms beyond introduction-to-Java type programming. Not to say that all the work I did in high school was that simplistic or bad, but that the mentality I had was rooted in easy to digest examples of OOP paradigms from introductory books and this starts to become limiting, at a point.