Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Allison (3003)

Allison
  (email not shown publicly)

Human (I think).

Journal of Allison (3003)

Wednesday November 23, 2005
11:57 PM

Punie to AST

[ #27719 ]

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 {
    <PunieGrammar::expr> => PMC 'PunieGrammar' => "print 1" @ 0 {
        <PunieGrammar::gprint> => PMC 'PunieGrammar' => "print 1" @ 0 {
            <PunieGrammar::expr> => PMC 'PunieGrammar' => "1" @ 6 {
                <PunieGrammar::term> => PMC 'PunieGrammar' => "1" @ 6
            }
            [0] => PMC 'PunieGrammar' => "print" @ 0
        }
    }
}

And an AST something like:

<PAST::Stmts> => {
    'source' => 'print 1;',
    'pos' => '0',
    'children' => [
        <PAST::Stmt> => {
            'source' => 'print 1;',
            'pos' => '0',
            'children' => [
                <PAST::Exp> => {
                    'source' => 'print 1',
                    'pos' => '0',
                    'children' => [
                        <PAST::Op> => {
                            'source' => 'print 1',
                            'pos' => '0',
                            'op' => 'print',
                            'children' => [
                                <PAST::Exp> => {
                                    'source' => '1',
                                    'pos' => '6',
                                    'children' => [
                                        <PAST::Val> => {
                                            '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).

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.