Saturday, September 15, 2007

I Want it that WAE

Whew! I just now finished my very first programming language, WAE. That's right, I wrote a parser and interpreter from (sort of) scratch. Unfortunately it has nothing to do with the Backstreet Boys.

The part I'm not telling you is that it's for CS 173: Programming Languages and that everyone in that class did the same, but it is still big.

Out of all of this I have realized that it is good to get started on work early so you can start your Friday night earlier than 12:30 or so you can eat sometime between 4PM and then. Honestly though, as CompSci projects go, this one was not that bad. It was only about 6 hours in total and only 400 lines of code, which pasted into a typical word processor would be about 10 pages single-spaced or about 19 double-spaced. Obviously this is comparing apples to oranges a bit since a good number of my lines are blank. On the other hand, code (especially scheme) is much more dense than most prose.

Anyway, this wonderful language is pretty simplistic compared with something that people would actually use, but seeing as I banged it out in one 6-hour session, it's not bad.

Here is a very brief overview of how it works:

The syntax is similar to Scheme/Lisp and uses prefix notation with curly braces {}. This is not by mistake. Scheme has the wonderful feature that it can tokenize parenthesized strings merely by adding a single quote to the beginning. So this:


'(a b c)

would return a list with the elements "a" "b" and "c". This is amazingly useful because it allows me to skip the entire step of scanning, which is a bitch. Seriously, I spent a good part of my summer fighting with Java over whether I could actually use if for scanning.

Normally, I would show a "Hello World," but since this language doesn't support strings, that won't happen. Like I said, simple language.

Instead, I will have it add 1 and 1. Behold:


{+ 1 1}

To most people this looks weird, but it has some nice advantages. First, it is very simple to parse; secondly, it makes nesting very trivial like so:


{+ 1 {+ 2 3}}


That means "add 1 to the sum of 2 and 3."

It even has scoped variables!


{with {{x 1}
{z x}
{x 2}
{y 3}
{w x}}
{+ 3 {+ {- z w} {+ y x}}}}


The result of which is 7. See if you can figure out why!

If you are really interested in this and other languages, I highly recommend CSCI1730 by Shriram. You can take a look at his book (for free!) here.

Now off to Joe's!