The Parrot Compiler Toolkit Tutorial

Ovid on 2008-03-27T15:57:17

The tutorial for writing compilers in Parrot is rather interesting. After failing to build parrot on Solaris, destroying the master boot record on my Windows box (don't ask) and killing all network connectivity in VMWare Ubuntu (never, never hire me for any system administration work), I finally managed to get parrot compiled (the magic invocation on Ubuntu appears to be sudo apt-get install build-essential). What amazes me about all of this is that the BBC pays me for this sort of fun.

If you're working through the tutorial, you might find the following bash aliases useful (adjust paths to taste):

alias squaak="$HOME/code/parrot-0.6.0/parrot \
  $HOME/code/parrot-0.6.0/languages/squaak/squaak.pir"
alias example="find $HOME/code/parrot-0.6.0/languages/ -name '*.pm'| \
  xargs grep "

The first one is obvious and the second one lets me quickly and easily search for example code that suits my needs.

Also, don't forget to rerun 'make' every time you change the grammar or actions. D'oh!

It's also worth noting that just because your code parses doesn't mean that it can be converted to PIR. Right now I'm trying to work out the cryptic Null PMC access in find_method() error (doesn't occur during the parse as my syntax is valid). The Parrot Compiler Toolkit is new and lots of fun, but plenty of rough edges, too.

Update: found the bug. The following two methods are not the same. Not a useful error message, though. It appears accurate and I'm sure it will get better with time.

# Mine:
method if_statement($/) {
    my $cond := $( $ );
    my $then := $( $ );
    my $past := PAST::OP.new(
        $cond, $then,
        :pasttype('if'),
        :node($/)
    );

    if $ {
        $past.push( $( $[0] ) );
    }
    make $past;
}

# Tutorial:
method if_statement($/) {
    my $cond := $( $ );
    my $then := $( $ );
    my $past := PAST::Op.new( $cond, $then,
                              :pasttype('if'),
                              :node($/) );
    if $ {
        $past.push( $( $[0] ) );
    }
    make $past;
}

See the bug?


That was'nt easy

htoug on 2008-03-27T18:25:40

/me hates case-errors

Re:That was'nt easy

Ovid on 2008-03-27T18:47:55

Same here. I eventually gave up and cut-n-pasted the code from the tutorial and things worked. Then it was a binary search through the code before I saw my problem.

Then there's that little bit in the try/catch grammar rules which requires embedded pir, but the tutorial doesn't mention that when it suggests you implement it.