# New Ticket Created by  "Daniel Keane" 
# Please include the string:  [perl #61696]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=61696 >


First stab at implementing array() function and '=>' within pipp.

Changes made:

* src/pct/actions.pm: added following methods: instantiate_array(),
array_arguments(), key_value_pair()

* src/pct/grammer.pg: added following rules: instantiate_array,
array_arguments, key_value_pair.  Amend term rule by adding
instantiate_array rule.

* src/common/builtins:  added sub 'infix:=>'

* src/common/php_array.pir: added sub 'array'

* t/in_php/array.t: added 3 tests for array()

TODO:  Add type checking of key values

Thanks
Index: t/in_php/array.t
===================================================================
--- t/in_php/array.t	(revision 34350)
+++ t/in_php/array.t	(working copy)
@@ -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:
 ?>
Index: src/pct/actions.pm
===================================================================
--- src/pct/actions.pm	(revision 34350)
+++ src/pct/actions.pm	(working copy)
@@ -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> ),
Index: src/pct/grammar.pg
===================================================================
--- src/pct/grammar.pg	(revision 34350)
+++ src/pct/grammar.pg	(working copy)
@@ -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
Index: src/common/php_array.pir
===================================================================
--- src/common/php_array.pir	(revision 34350)
+++ src/common/php_array.pir	(working copy)
@@ -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]
Index: src/common/builtins.pir
===================================================================
--- src/common/builtins.pir	(revision 34350)
+++ src/common/builtins.pir	(working copy)
@@ -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'
 

Reply via email to