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



The bare expression before an O RLY? should both set IT and be used as
a test in the O RLY?, but it should only be evaluated once.
---
 languages/lolcode/src/parser/actions.pm |    9 +++++++--
 languages/lolcode/t/03-if.t             |   16 +++++++++++++++-
 2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/languages/lolcode/src/parser/actions.pm b/languages/lolcode/src/parser/actions.pm
index a9e2cc0..1ab265a 100644
--- a/languages/lolcode/src/parser/actions.pm
+++ b/languages/lolcode/src/parser/actions.pm
@@ -88,12 +88,15 @@ method function($/) {
     make $past;
 }
 
+# Because we must bind the first <expression> to IT, we can't immediately
+# add $expr to $past. The code would probably be more clear if PAST::Node
+# supported shift() in addition to unshift().
 method ifthen($/) {
     my $count := +$<expression> - 1;
     my $expr  := $( $<expression>[$count] );
     my $then  := $( $<block>[$count] );
     $then.blocktype('immediate');
-    my $past := PAST::Op.new( $expr, $then,
+    my $past := PAST::Op.new( $then,
                               :pasttype('if'),
                               :node( $/ )
                             );
@@ -103,16 +106,18 @@ method ifthen($/) {
         $past.push( $else );
     }
     while ($count != 0) {
+        $past.unshift( $expr );
         $count := $count - 1;
         $expr  := $( $<expression>[$count] );
         $then  := $( $<block>[$count] );
         $then.blocktype('immediate');
-        $past  := PAST::Op.new( $expr, $then, $past,
+        $past  := PAST::Op.new( $then, $past,
                                :pasttype('if'),
                                :node( $/ )
                              );
     }
     my $it := PAST::Var.new( :name( 'IT' ), :scope('package'), :viviself('Undef'));
+    $past.unshift( $it );
     my $bind := PAST::Op.new( :pasttype('bind'), :node( $/ ) );
     $bind.push( $it );
     $bind.push( $expr );
diff --git a/languages/lolcode/t/03-if.t b/languages/lolcode/t/03-if.t
index 3c178d2..02b2623 100644
--- a/languages/lolcode/t/03-if.t
+++ b/languages/lolcode/t/03-if.t
@@ -1,5 +1,5 @@
 HAI 1.2
-  VISIBLE "1..14"
+  VISIBLE "1..16"
 
   BTW SANITY CHECK
   WIN
@@ -151,5 +151,19 @@ HAI 1.2
       OIC
   OIC
 
+  BTW O RLY? SHOULD ONLY CALL A FUNCTION ONCE WHEN SETTING IT
+  HOW DUZ I ReturnWin
+    VISIBLE "ok 15"
+    "ok 16"
+  IF U SAY SO
+
+  ReturnWin
+  O RLY?
+    YA RLY
+      VISIBLE IT
+    NO WAI
+      VISIBLE "nok 16"
+  OIC
+
   BTW vim: set filetype=lolcode :
 KTHXBYE

Reply via email to