Author: bernhard
Date: Sun Dec 28 07:37:20 2008
New Revision: 34494
Modified:
trunk/languages/pipp/src/pct/actions.pm
trunk/languages/pipp/src/pct/grammar.pg
trunk/languages/pipp/t/php/closures.t
Log:
[Pipp] try to support closures
Add one working and one failing test case.
Modified: trunk/languages/pipp/src/pct/actions.pm
==============================================================================
--- trunk/languages/pipp/src/pct/actions.pm (original)
+++ trunk/languages/pipp/src/pct/actions.pm Sun Dec 28 07:37:20 2008
@@ -517,7 +517,15 @@
if $key eq 'open' {
# note that $<param_list> creates a new PAST::Block.
- @?BLOCK.unshift( $( $<param_list> ) );
+ my $block := $( $<param_list> );
+
+ # declare the bound vars a lexical
+ if +$<bind_list> == 1 {
+ for $<bind_list>[0]<VAR_NAME> {
+ $block.symbol( ~$_, :scope('lexical') );
+ }
+ }
+ @?BLOCK.unshift( $block );
}
else {
my $block := @?BLOCK.shift();
Modified: trunk/languages/pipp/src/pct/grammar.pg
==============================================================================
--- trunk/languages/pipp/src/pct/grammar.pg (original)
+++ trunk/languages/pipp/src/pct/grammar.pg Sun Dec 28 07:37:20 2008
@@ -394,12 +394,16 @@
# declarations
rule closure {
- 'function' <param_list>
+ 'function' <bind_list>? <param_list>
{*} #= open
'{' <statement_list> '}'
{*} #= close
}
+rule bind_list {
+ 'use' '(' [ '&'? <VAR_NAME> [',' '&'? <VAR_NAME>]* ]? ')'
+}
+
rule function_definition {
'function' <FUNCTION_NAME> <param_list>
{*} #= open
Modified: trunk/languages/pipp/t/php/closures.t
==============================================================================
--- trunk/languages/pipp/t/php/closures.t (original)
+++ trunk/languages/pipp/t/php/closures.t Sun Dec 28 07:37:20 2008
@@ -20,7 +20,7 @@
use FindBin;
use lib "$FindBin::Bin/../../../../lib", "$FindBin::Bin/../../lib";
-use Parrot::Test tests => 5;
+use Parrot::Test tests => 7;
=for perl6
@@ -111,9 +111,11 @@
function generator () {
- $anon_no_arg = function ($arg_1, $arg_2) {
+ $anon_two_args = function ($arg_1, $arg_2) {
echo "'$arg_1', '$arg_2' was passed to anon_two_args().\n";
};
+
+ return $anon_two_args;
}
$sub = generator();
@@ -124,3 +126,69 @@
CODE
'one', 'two' was passed to anon_two_args().
OUT
+
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'closure with one bound var' );
+<?php
+
+function gen_indentor ( ) {
+ $indention = ' ';
+ $indentor = function use ($indention) ($line) {
+ echo $indention . $line . "\n";
+ };
+
+ return $indentor;
+}
+
+$sub_1 = gen_indentor(' ');
+
+$sub_1('1a');
+$sub_1('1b');
+$sub_1('1c');
+
+?>
+CODE
+ 1a
+ 1b
+ 1c
+OUT
+
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'closure with a passed bound
var', todo => 'broken' );
+<?php
+
+function gen_indentor ( $indention ) {
+
+ $indentor = function use ($indention) ($line) {
+ echo $indention . $line . "\n";
+ };
+
+ return $indentor;
+}
+
+$sub_1 = gen_indentor(' ');
+$sub_2 = gen_indentor(' ');
+$sub_3 = gen_indentor(' ');
+
+$sub_1('1a');
+$sub_1('1b');
+$sub_1('1c');
+$sub_2('2a');
+$sub_2('2b');
+$sub_2('2c');
+$sub_3('3a');
+$sub_3('3b');
+$sub_3('3c');
+
+?>
+CODE
+ 1a
+ 1b
+ 1c
+ 2a
+ 2b
+ 2c
+ 3a
+ 3b
+ 3c
+OUT