[Parrot-users] A new for loop?

Jay Emerson jayemerson at gmail.com
Thu Jul 7 16:17:22 UTC 2011


I've declared the following grammar rule and associated method
function, below, adapted from squaak, obviously, but trying to use the
pasttype('for').  I'm having trouble with the parameter of the loop.
This simple result shows the current behavior (obviously I'd like the
result to be printing 1, 2, and 3).  Advice on how to get the loop
element, which is supposedly passed as a parameter, into $iter so the
function body has access to it... would be most appreciated.  I
figured 2-3 hours of struggling was a noble effort.

Thanks,

Jay

> i = 99
> myfor(i in c(1,2,3)) { print(i) }
99
99
99
>

rule statement:sym<myfor> {
    <sym> '(' <identifier> 'in' <EXPR> ')' '{'
        <statement>*
    '}'
}

method statement:sym<myfor>($/) {
    our $?BLOCK;
    our @?BLOCK;

    $?BLOCK := PAST::Block.new( :blocktype('immediate'),
                                :node($/) );
    @?BLOCK.unshift($?BLOCK);

    my $iter := $<identifier>.ast;
    $iter.isdecl(1);
    $iter.scope('parameter');
    $iter.viviself(0);

    $?BLOCK.symbol($iter.name(), :scope('parameter'));

    my $body := @?BLOCK.shift();
    $?BLOCK  := @?BLOCK[0];
    for $<statement> {
        $body.push($_.ast);
    }

    my $set := $<EXPR>.ast;

    # Trying to figure out the parameters?  This causes fatal problems:
    #my $param := PAST::Op.new( $iter.name(), :pirop('get_params'), :node($/) );
    #$body.push($param);

    my $loop := PAST::Op.new( $set, $body, :pasttype('for'), :node($/) );

    make PAST::Stmts.new( $loop, :node($/) );
}


-- 
John W. Emerson (Jay)
Associate Professor of Statistics
Department of Statistics
Yale University
http://www.stat.yale.edu/~jay


More information about the Parrot-users mailing list