Punie to AST

Allison on 2005-11-24T04:57:40

I've checked in the code to transform Punie match objects into AST trees. The core pieces are:

  • A tree grammar for the transformation in languages/punie/lib/pge2past.g
  • A series of AST nodes in languages/punie/lib/PAST/
  • A script that parses a Punie source file, transforms the match result into an AST tree, and dumps the tree in a text format for verification. This is named punie2.pir, because it's still not far enough along to replace punie.pir.

So, if you run:

parrot punie2.pir demo.p1
You'll get a match object something like:
"$/" => PMC 'PGE::Rule' => "print 1;" @ 0 {
     => PMC 'PunieGrammar' => "print 1" @ 0 {
         => PMC 'PunieGrammar' => "print 1" @ 0 {
             => PMC 'PunieGrammar' => "1" @ 6 {
                 => PMC 'PunieGrammar' => "1" @ 6
            }
            [0] => PMC 'PunieGrammar' => "print" @ 0
        }
    }
}
And an AST something like:
 => { 
    'source' => 'print 1;',
    'pos' => '0',
    'children' => [
         => { 
            'source' => 'print 1;',
            'pos' => '0',
            'children' => [
                 => { 
                    'source' => 'print 1',
                    'pos' => '0',
                    'children' => [
                         => { 
                            'source' => 'print 1',
                            'pos' => '0',
                            'op' => 'print',
                            'children' => [
                                 => { 
                                    'source' => '1',
                                    'pos' => '6',
                                    'children' => [
                                         => { 
                                            'source' => '1',
                                            'pos' => '6',
                                            'value' => '1',
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

In these source is the chunk of source code from the original file corresponding to the current node (good for error reporting), pos is the offset from the start of the source file to the point where the corresponding match node started to match, and children is a list of child nodes for the current node. Some node types, like PAST::Op and PAST::Val have custom attributes.

I've still got some time left to work today, so I'll start on the OST now (though, I probably won't bother to post about it unless I run across something exciting).