Index: src/parser/Actions.pm =================================================================== --- src/parser/Actions.pm (revision 31290) +++ src/parser/Actions.pm (working copy) @@ -120,6 +120,44 @@ make $past; } +method for_stmt($/) { + # translates to: + # $P0 = new 'Iterator', + # while $P0: + # i = shift $P0 + # ... + + # XXX implement complex for syntax + + # XXX right now this uses a Block rather than Stmts so that $iter's scope + # XXX is confined to this 'for'. Better to use Stmts and make $iter an anonymous register. + #my $past := PAST::Stmts.new( :node($/) ); + my $past := PAST::Block.new( :blocktype('immediate'), :node($/) ); + + # create iterator + my $list := $( $ ); + my $iter := PAST::Var.new( :name('iter'), :scope('register'), :node($/) ); + my $iterdecl := PAST::Var.new( :name('iter'), :scope('register'), :node($/), :isdecl(1) ); + $past.push( $iterdecl ); + $past.push( PAST::Op.new( $iter, $list, + :inline(' %0 = new "Iterator", %1'), + :node($/) ) ); + + # make loop body + my $tgt := $( $ ); + my $loop := PAST::Stmts.new( :node($/) ); + my $shifted := PAST::Op.new( $iter, + :inline(' %r = shift %0'), + :node($/) ); + $loop.push( PAST::Op.new( $tgt, $shifted, :pasttype('bind'), :node($/) ) ); + $loop.push( $( $ ) ); + + $past.push( PAST::Op.new( $iter, $loop, + :pasttype('while'), + :node($/) ) ); + make $past; +} + method parameter_list($/) { ## the only place for parameters to live is in a function block; ## create that here already. Index: src/parser/Grammar.pg =================================================================== --- src/parser/Grammar.pg (revision 31290) +++ src/parser/Grammar.pg (working copy) @@ -58,7 +58,7 @@ token compound_stmt { | {*} #= if_stmt | {*} #= while_stmt - | + | {*} #= for_stmt | {*} #= try_stmt | | {*} #= funcdef @@ -79,8 +79,10 @@ } rule for_stmt { - 'for' 'in' ':' - [ 'else' ':' ]? + 'for' 'in' ':' +# 'for' 'in' ':' +# [ 'else' ':' ]? + {*} } rule try_stmt { Index: t/00-parrot/06-stmts.t =================================================================== --- t/00-parrot/06-stmts.t (revision 31290) +++ t/00-parrot/06-stmts.t (working copy) @@ -1,6 +1,6 @@ #!./parrot pynie.pbc -print '1..7' +print '1..12' # if @@ -24,4 +24,50 @@ else: print 'ok 7' +# for +lst = [ 1,2,3,4 ] +n = 0 +ok = 1 +for i in lst: + n += 1 + if i != n: ok = 0 +if ok: print 'ok 8' +else: print 'nok 8' + +n = 0 +ok = 1 +for i in [ 1,2,3,4 ]: + n += 1 + if i != n: ok = 0 +if ok: print 'ok 9' +else: print 'nok 9' + +n = 0 +ok = 1 +for i in 1,2,3,4: + n += 1 + if i != n: ok = 0 +if ok: print 'ok 10' +else: print 'nok 10' + +# nested fors +lst = [ ] +for i in range(3): + # XXX ResizablePMCArray has a 'push' method, use list.append + for j in range(3): + lst.push((i + 1) * (j + 1)) +ok = 1 +lst2 = [ 1, 2, 3, 2, 4, 6, 3, 6, 9 ] +for i in range(9): + if lst[i] != lst2[i]: ok = 0 +if ok: print 'ok 11' +else: print 'nok 11' + +# multiple iterators +#ok = 0 +#for i, j in ( (0, 0), (1, 2), (2, 4) ): +# if j == i * 2: ok += 1 +#if ok == 3: print 'ok 12' +#else: print 'nok 12' +print 'not ok 12 # TODO implement multiple iterators'