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

[svn:parrot] r34188 - in trunk: . languages/pipp languages/pipp/src/pct languages/pipp/t/php

From:
bernhard
Date:
December 21, 2008 02:07
Subject:
[svn:parrot] r34188 - in trunk: . languages/pipp languages/pipp/src/pct languages/pipp/t/php
Message ID:
20081221100735.A5FFFCBA12@x12.develooper.com
Author: bernhard
Date: Sun Dec 21 02:07:33 2008
New Revision: 34188

Modified:
   trunk/NEWS
   trunk/languages/pipp/CREDITS
   trunk/languages/pipp/src/pct/actions.pm
   trunk/languages/pipp/src/pct/grammar.pg
   trunk/languages/pipp/t/php/control_flow.t

Log:
[Pipp] Add support for 'elsif'
Courtesy of Daniel Keane.


Modified: trunk/NEWS
==============================================================================
--- trunk/NEWS	(original)
+++ trunk/NEWS	Sun Dec 21 02:07:33 2008
@@ -9,6 +9,9 @@
   + PIRC
     - refactoring of data structures
     - various bug fixes and updates
+- Languages
+  + Pipp
+    - added support for 'elsif'
 
 New in 0.8.2
 - Implementation

Modified: trunk/languages/pipp/CREDITS
==============================================================================
--- trunk/languages/pipp/CREDITS	(original)
+++ trunk/languages/pipp/CREDITS	Sun Dec 21 02:07:33 2008
@@ -24,3 +24,6 @@
 D: Bugfixes
 D: Implementation of functions
 D: Code beautification
+
+N: Daniel Keane
+D: Implementation of 'elsif'

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 21 02:07:33 2008
@@ -232,20 +232,59 @@
     make $past;
 }
 
-method if_statement($/) {
+method conditional_expression($/) {
     my $past := PAST::Op.new(
                     $( $<expression> ),
                     $( $<block> ),
-                    :pasttype('if'),
                     :node($/)
                 );
-    for $<else_clause> {
-        $past.push( $( $_ ) );
-    }
-
     make $past;
 }
 
+method if_statement($/) {
+    my $past := $($<conditional_expression>);
+    $past.pasttype('if');
+        
+    my $else := undef;
+    if +$<else_clause> != 0 {
+            $else := $($<else_clause>[0]);
+    }
+    my $firsteif := undef;
+    if(+$<elseif_clause> != 0) {
+        my $count := +$<elseif_clause> - 1;
+        $firsteif := $($<elseif_clause>[$count]);
+        while $count != 0 {
+                my $eif := $($<elseif_clause>[$count]);
+                $count--;
+                my $eifchild := $($<elseif_clause>[$count]);
+                if($else) {
+                        $eif.push($else);
+                }
+                $eif.push($eifchild);
+        }
+        if($else && +$<elseif_clause> == 1) {
+                $firsteif.push($else);
+        }
+     }
+
+     if($firsteif) {
+             $past.push($firsteif);
+     }
+     elsif($else) {
+             $past.push($else);
+     }
+     make $past;
+}
+
+method else_clause($/) {
+        make $($<block>);
+}
+
+method elseif_clause($/) {
+        my $past := $($<conditional_expression>);
+        $past.pasttype('if');
+        make $past;
+}
 method var_assign($/) {
     make PAST::Op.new(
              $( $<var> ),
@@ -295,18 +334,11 @@
          );
 }
 
-method else_clause($/) {
-    make $( $<block> );
-}
 
 method while_statement($/) {
-    my $cond  := $( $<expression> );
-    my $block := $( $<block> );
-
-    make PAST::Op.new( $cond,
-                       $block,
-                       :pasttype('while'),
-                       :node($/) );
+    my $past := $($<conditional_expression>);
+    $past.pasttype('while');
+    make $past;
 }
 
 method for_statement($/) {

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 21 02:07:33 2008
@@ -171,8 +171,15 @@
     {*}
 }
 
+rule conditional_expression {
+    '(' <expression> ')' <block>
+    {*}
+}
+
 rule if_statement {
-    'if' '(' <expression> ')' <block> <else_clause>?
+    'if' <conditional_expression>
+    [<elseif_clause>]*
+    [<else_clause>]?
     {*}
 }
 
@@ -181,13 +188,13 @@
     {*}
 }
 
-rule elsif_clause {
-    'elsif' '(' <expression> ')' <block>
+rule elseif_clause {
+    'elseif' <conditional_expression>
     {*}
 }
 
 rule while_statement {
-    'while' '(' <expression> ')' <block>
+    'while' <conditional_expression>
     {*}
 }
 

Modified: trunk/languages/pipp/t/php/control_flow.t
==============================================================================
--- trunk/languages/pipp/t/php/control_flow.t	(original)
+++ trunk/languages/pipp/t/php/control_flow.t	Sun Dec 21 02:07:33 2008
@@ -24,7 +24,7 @@
 
 use Parrot::Config ();
 use Parrot::Test;
-use Test::More     tests => 15;
+use Test::More     tests => 16;
 
 language_output_is( 'Pipp', <<'CODE', <<'OUT', 'if, one statement in block' );
 <?php
@@ -75,7 +75,7 @@
 if block
 OUT
 
-language_output_is( 'Pipp', <<'CODE', <<'OUT', 'i/else taking else-branchf' );
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'if/else taking else-branch' );
 <?php
 if (0)
 {
@@ -90,6 +90,25 @@
 else block
 OUT
 
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'if/elseif/else taking elseif-branch' );
+<?php
+$x = 1;
+if($x ==0)
+{
+  echo "if block\n";
+}
+elseif($x == 1)
+{
+  echo "elseif block\n";
+}
+else
+{
+  echo "else block\n";
+}
+CODE
+elseif block
+OUT
+
 language_output_is( 'Pipp', <<'CODE', <<'OUT' . q{  }, 'positive int' );
 <?php
 if (1) {



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