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


Implement WIN & FAIL. Finish ifthen implementation. Add MEBBE test to
t/03-if.t
diff --git a/languages/lolcode/src/parser/actions.pm b/languages/lolcode/src/parser/actions.pm
index ca442dc..1878939 100644
--- a/languages/lolcode/src/parser/actions.pm
+++ b/languages/lolcode/src/parser/actions.pm
@@ -88,6 +88,33 @@ method function($/) {
     make $past;
 }
 
+method ifthen($/) {
+    my $count := +$<expression> - 1;
+    my $expr  := $( $<expression>[$count] );
+    my $then  := $( $<block>[$count] );
+    $then.blocktype('immediate');
+    my $past := PAST::Op.new( $expr, $then,
+                              :pasttype('if'),
+                              :node( $/ )
+                            );
+    if ( $<else> ) {
+        my $else := $( $<else>[0] );
+        $else.blocktype('immediate');
+        $past.push( $else );
+    }
+    while ($count != 0) {
+        $count := $count - 1;
+        $expr  := $( $<expression>[$count] );
+        $then  := $( $<block>[$count] );
+        $then.blocktype('immediate');
+        $past  := PAST::Op.new( $expr, $then, $past,
+                               :pasttype('if'),
+                               :node( $/ )
+                             );
+    }
+    make $past;
+}
+
 method block($/) {
     my $past := PAST::Block.new( :blocktype('declaration'), :node( $/ ) );
     for $<statement> {
@@ -115,6 +142,14 @@ method integer($/) {
 }
 
 
+method boolean($/) {
+    if (~$/ eq 'FAIL' ) {
+        make PAST::Val.new( :value( 0 ), :returns('Boolean'), :node($/) );
+    } else {
+        make PAST::Val.new( :value( 1 ), :returns('Boolean'), :node($/) );
+    }
+}
+
 method quote($/) {
     make PAST::Val.new( :value( $($<string_literal>) ), :node($/) );
 }
diff --git a/languages/lolcode/src/parser/grammar.pg b/languages/lolcode/src/parser/grammar.pg
index 8729510..6e41716 100644
--- a/languages/lolcode/src/parser/grammar.pg
+++ b/languages/lolcode/src/parser/grammar.pg
@@ -21,6 +21,7 @@ rule statement {
     | <declare>    {*}   #= declare
     | <assign>     {*}   #= assign
     | <function>   {*}   #= function
+    | <ifthen>     {*}   #= ifthen
     | <expression> {*}   #= bare_expression
 }
 
@@ -58,7 +59,7 @@ rule ifthen {
     ]*
     [
       'NO' 'WAI' <statement_terminator>
-      <block>
+      $<else>=<block>
     ]?
     'OIC'
     {*}
@@ -93,6 +94,7 @@ rule expression {
 
 rule value {
     | <integer>  {*}                             #= integer
+    | <boolean>  {*}                             #= boolean
     | <quote>    {*}                             #= quote
 }
 
@@ -100,17 +102,22 @@ rule variable { <identifier> {*} }
 
 token identifier { <!keyword> $<name>=( <[a..zA..Z]> \w* ) {*} }
 
+# Because PGE doesn't yet know how to do longest token matching,
+# order all tokens in reverse alpha order to avoid a parsing bug.
 token keyword {
-    [ 'A' | 'AN' | 'FAIL' | 'FOUND' | 'GTFO' | 'HAS' | 'HOW' | 'I' | 'IF' | 'ITZ'
-    | 'KTHXBYE' | 'MEBBE' | 'NO' | 'O' | 'OIC' | 'OMG' | 'OMGWTF' | 'R'
-    | 'RLY' | 'RLY?' | 'SAY' | 'SO' | 'U' | 'VISIBLE' | 'WAI' | 'WIN' | 'WTF?' | 'YA'
-    | 'YR' ] >>
+    [ 'YR' | 'YA' | 'WTF?' | 'WIN' | 'WAI' | 'VISIBLE' | 'U' | 'SUM' | 'SO'
+    | 'SMALLR' | 'SAY' | 'RLY?' | 'RLY' | 'R' | 'QUOSHUNT' | 'PRODUKT'
+    | 'OMGWTF' | 'OMG' | 'OIC' | 'O' | 'NO' | 'MOD' | 'MEBBE' | 'KTHXBYE'
+    | 'ITZ' | 'IF' | 'I' | 'HOW' | 'HAS' | 'GTFO' | 'FOUND' | 'FAIL' | 'DIFF'
+    | 'BIGGR' | 'AN' | 'A' ] >>
 }
 
 rule integer { \d+ {*} }
 
 token float { \d+ [ '.' \d+ ]? }
 
+rule boolean { [ 'WIN' | 'FAIL' ] {*} }
+
 rule quote {
     [ \" <string_literal: "> \" ]
     {*}
diff --git a/languages/lolcode/t/03-if.t b/languages/lolcode/t/03-if.t
index 75019bd..0219054 100644
--- a/languages/lolcode/t/03-if.t
+++ b/languages/lolcode/t/03-if.t
@@ -1,5 +1,5 @@
 HAI 1.2
-  VISIBLE "1..2"
+  VISIBLE "1..3"
 
   WIN
   O RLY?
@@ -17,4 +17,16 @@ HAI 1.2
       VISIBLE "ok 2"
   OIC
 
+  FAIL
+  O RLY?
+    YA RLY
+      VISIBLE "nok 3"
+    MEBBE FAIL
+      VISIBLE "nok 3"
+    MEBBE WIN
+      VISIBLE "ok 3"
+    NO WAI
+      VISIBLE "nok 3"
+  OIC
+
 KTHXBYE

Reply via email to