develooper Front page | perl.cvs.parrot | Postings from December 2008

[svn:parrot] r34383 - in trunk/languages/pipp: . src/common src/pct t/in_php

From:
bernhard
Date:
December 26, 2008 08:30
Subject:
[svn:parrot] r34383 - in trunk/languages/pipp: . src/common src/pct t/in_php
Message ID:
20081226163018.6685FCBA12@x12.develooper.com
Author: bernhard
Date: Fri Dec 26 08:30:17 2008
New Revision: 34383

Modified:
   trunk/languages/pipp/CREDITS
   trunk/languages/pipp/src/common/builtins.pir
   trunk/languages/pipp/src/common/php_array.pir
   trunk/languages/pipp/src/pct/actions.pm
   trunk/languages/pipp/src/pct/grammar.pg
   trunk/languages/pipp/t/in_php/array.t

Log:
RT #61696: [PATCH] implement array() and '=>' within pipp
Courtesy of Daniel Keane.


Modified: trunk/languages/pipp/CREDITS
==============================================================================
--- trunk/languages/pipp/CREDITS	(original)
+++ trunk/languages/pipp/CREDITS	Fri Dec 26 08:30:17 2008
@@ -29,3 +29,4 @@
 E: keane.dp@gmail.com
 D: Implementation of 'elsif'
 D: Implementation of 'do-while'
+D: Implementation of 'array()'

Modified: trunk/languages/pipp/src/common/builtins.pir
==============================================================================
--- trunk/languages/pipp/src/common/builtins.pir	(original)
+++ trunk/languages/pipp/src/common/builtins.pir	Fri Dec 26 08:30:17 2008
@@ -310,6 +310,14 @@
     .RETURN_BOOL($I0)
 .end
 
+.sub 'infix:=>'
+    .param pmc key
+    .param pmc value
+    $P0 = new 'ResizablePMCArray'
+    $P0[0] = key
+    $P0[1] = value
+    .return($P0)
+.end
 
 .include 'languages/pipp/src/common/php_standard.pir'
 

Modified: trunk/languages/pipp/src/common/php_array.pir
==============================================================================
--- trunk/languages/pipp/src/common/php_array.pir	(original)
+++ trunk/languages/pipp/src/common/php_array.pir	Fri Dec 26 08:30:17 2008
@@ -61,6 +61,32 @@
     .REGISTER_LONG_CONSTANT(cst, 'COUNT_RECURSIVE', COUNT_RECURSIVE)
 .end
 
+.sub 'array'
+    .param pmc args :slurpy
+    .local pmc array, iter
+    array = new 'PhpArray'
+    iter = new 'Iterator', args
+    $I1 = 0
+    args_loop:
+        unless iter goto args_end
+        $P0 = shift iter
+        $I0 = isa $P0, 'ResizablePMCArray'
+        unless $I0 goto add_var
+        $P1 = $P0[0]
+        $P2 = $P0[1]
+        array[$P1] = $P2
+        goto args_loop
+    add_var:
+        array[$I1] = $P0
+        $I1 = $I1 + 1
+        goto end
+    end:
+        goto args_loop
+    args_end:
+    .return(array)
+.end
+
+
 =item C<array array_change_key_case(array input [, int case=CASE_LOWER])>
 
 Retuns an array with all string keys lowercased [or uppercased]

Modified: trunk/languages/pipp/src/pct/actions.pm
==============================================================================
--- trunk/languages/pipp/src/pct/actions.pm	(original)
+++ trunk/languages/pipp/src/pct/actions.pm	Fri Dec 26 08:30:17 2008
@@ -182,6 +182,43 @@
     make $past;
 }
 
+method instantiate_array($/) {
+    my $past := PAST::Op.new(
+                    :pasttype( 'call' ),
+                    :name( 'array' ),
+                    :node( $/ )
+                );
+  
+                #for $<expression> {
+                #$past.push( $($_) );
+                #}
+
+                #for $<key_value_pair> {
+                #$past.push( $($_) );
+                #}
+    for $<array_arguments> {
+        $past.push( $($_) );
+    }   
+
+    make $past;
+}
+
+method array_arguments($/, $key) {
+    make $( $/{$key} );
+}
+
+method key_value_pair($/) { 
+   my $past := PAST::Op.new(
+                    :node( $/ ),
+                    :pasttype( 'call' ),
+                    :name( 'infix:=>' ),
+                    :returns( 'Array' ),
+                    $( $<key> ),
+                    $( $<value> )
+            );
+   make $past;
+}
+
 method method_call($/) {
     my $past := PAST::Op.new(
                     :name( ~$<METHOD_NAME> ),

Modified: trunk/languages/pipp/src/pct/grammar.pg
==============================================================================
--- trunk/languages/pipp/src/pct/grammar.pg	(original)
+++ trunk/languages/pipp/src/pct/grammar.pg	Fri Dec 26 08:30:17 2008
@@ -278,6 +278,21 @@
     {*}
 }
 
+rule instantiate_array {
+	'array' '(' [ <array_arguments> [  [',' <array_arguments> ]* [',' <array_arguments> ]?  ]? ','?  ]?  ')'
+	{*}
+}
+
+rule array_arguments {	
+	| <key_value_pair>	{*}	#= key_value_pair
+	| <expression>		{*}	#= expression
+}
+
+rule key_value_pair {
+	<key=expression> '=>' <value=expression>
+	{*}
+}
+
 rule constructor_call {
     'new' <CLASS_NAME>
     {*}
@@ -358,6 +373,7 @@
 rule term {
       <method_call>            {*}   #= method_call
     | <function_call>          {*}   #= function_call
+	| <instantiate_array>	   {*}	 #= instantiate_array
     | <constructor_call>       {*}   #= constructor_call
     | '(' <expression> {*} ')'       #= expression
     | <literal>                {*}   #= literal

Modified: trunk/languages/pipp/t/in_php/array.t
==============================================================================
--- trunk/languages/pipp/t/in_php/array.t	(original)
+++ trunk/languages/pipp/t/in_php/array.t	Fri Dec 26 08:30:17 2008
@@ -27,7 +27,7 @@
 
 require_once 'Test.php';
 
-plan(8);
+plan(11);
 $count = 1;
 
 $hello['world'] = 'hi';
@@ -61,5 +61,18 @@
 is( count($thrice), 2, 'count of $thrice', $count );
 $count++;
 
+#test array() function
+
+$arrayfunc = array(0, "key" => "key", 1);
+
+is( $arrayfunc[0], 0, 'arrayfunc[0]', $count );
+$count++;
+is( $arrayfunc[1], 1, 'arrayfunc[1]', $count );
+$count++;
+is( $arrayfunc["key"], 'key', 'arrayfunc[key]', $count );
+$count++;
+
+
+
 # vim: expandtab shiftwidth=4 ft=php:
 ?>



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About