Author: bernhard
Date: Mon Feb 12 14:00:34 2007
New Revision: 16958

Modified:
   trunk/languages/plumhead/src/partridge/Plumhead.pg
   trunk/languages/plumhead/src/partridge/PlumheadPAST.tg

Log:
[Plumhead partridge]
Start with support for if-statements.


Modified: trunk/languages/plumhead/src/partridge/Plumhead.pg
==============================================================================
--- trunk/languages/plumhead/src/partridge/Plumhead.pg  (original)
+++ trunk/languages/plumhead/src/partridge/Plumhead.pg  Mon Feb 12 14:00:34 2007
@@ -5,7 +5,7 @@
 # a PHP grammar
 # derived from on languages/abc/abc.pg
 
-token TOP         { <program> }
+token TOP                     { <program> }
 
 regex SEA                     { .* <?before: \<> }
 token CODE_START              { \< \? p h p }
@@ -23,25 +23,29 @@
 token ADD_OP                  { \+ | \- }
 token MUL_OP                  { \* | / | % }
 
-token program     { ^ <SEA> <code> }
+token IF                      { if }
+token ELSE                    { else }
+token program                 { ^ <SEA> <code> }
 
-rule  code        { <?CODE_START> <statement>* <?CODE_END> }
+rule  code                    { <?CODE_START> <statement>* <?CODE_END> }
 
-rule statement    { <ECHO> <expression> ; }
+rule statement                { <ECHO> <expression> ;
+                              | <IF> <?PAREN_OPEN> <expression> <?PAREN_CLOSE> 
\{ <statement>* \}
+                              }
 
-token expression  { <DOUBLEQUOTE_STRING> | <SINGLEQUOTE_STRING> | 
<adding_expression> }
+token expression              { <DOUBLEQUOTE_STRING> | <SINGLEQUOTE_STRING> | 
<adding_expression> }
 
-# TODO: this gets precedence wrong
-rule adding_expression { <multiplying_expression> <summand>? }
+# TODO: this gets precedence  wrong
+rule adding_expression        { <multiplying_expression> <summand>? }
 
-rule summand           { <ADD_OP> <adding_expression> }
+rule summand                  { <ADD_OP> <adding_expression> }
 
-# TODO: this gets precedence wrong
-rule multiplying_expression  { <unary_expression> <multiplicand>? }
+# TODO: this gets precedence  wrong
+rule multiplying_expression   { <unary_expression> <multiplicand>? }
 
-rule multiplicand      { <MUL_OP> <multiplying_expression> }
+rule multiplicand             { <MUL_OP> <multiplying_expression> }
 
-rule unary_expression { <UNARY_MINUS>? <postfix_expression> }
+rule unary_expression         { <UNARY_MINUS>? <postfix_expression> }
 
-rule postfix_expression { <NUMBER> | <?PAREN_OPEN> <expression> <?PAREN_CLOSE> 
}
+rule postfix_expression       { <NUMBER> | <?PAREN_OPEN> <expression> 
<?PAREN_CLOSE> }
 

Modified: trunk/languages/plumhead/src/partridge/PlumheadPAST.tg
==============================================================================
--- trunk/languages/plumhead/src/partridge/PlumheadPAST.tg      (original)
+++ trunk/languages/plumhead/src/partridge/PlumheadPAST.tg      Mon Feb 12 
14:00:34 2007
@@ -18,6 +18,7 @@
     .return (past)
 }
 
+# TODO: handle multiple code and sea blocks
 transform past (Plumhead::Grammar::program) :language('PIR') {
 
     .local pmc past
@@ -26,17 +27,17 @@
 
     $P0 = node['SEA']
     if null $P0 goto handled_sea
-    .local pmc past_sea
-    past_sea = tree.'get'('past', $P0, 'Plumhead::Grammar::SEA')
-    past.'push'(past_sea)
-handled_sea:
+        .local pmc past_sea
+        past_sea = tree.'get'('past', $P0, 'Plumhead::Grammar::SEA')
+        past.'push'(past_sea)
+    handled_sea:
 
     $P0 = node['code']
     if null $P0 goto handled_code
-    .local pmc past_code
-    past_code = tree.'get'('past', $P0, 'Plumhead::Grammar::code')
-    past.'push'(past_code)
-handled_code:
+        .local pmc past_code
+        past_code = tree.'get'('past', $P0, 'Plumhead::Grammar::code')
+        past.'push'(past_code)
+    handled_code:
 
     .return (past)
 }
@@ -72,11 +73,11 @@
         iter = new .Iterator, $P0
         iter_loop:
             unless iter goto iter_end
-            .local pmc cnode, cpast, cpast2
+            .local pmc cnode, cpast, cpast2, cpast3
             cnode = shift iter
             if null cnode goto iter_loop
                  $P1 = cnode['ECHO']
-                 if null $P1 goto iter_loop
+                 if null $P1 goto no_echo
                      $P2 = cnode['expression']
                      if null $P2 goto iter_loop
                          cpast = tree.'get'('past', $P2, 
'Plumhead::Grammar::expression')
@@ -85,6 +86,20 @@
                          cpast2.'init'( cpast, 'node'=> node, 'name' => 
'print' )  
                          past.'push'(cpast2)
                          goto iter_loop
+                 no_echo:
+                 $P1 = cnode['IF']
+                 if null $P1 goto no_if
+                     $P2 = cnode['expression']
+                     if null $P2 goto iter_loop
+                         cpast = tree.'get'('past', $P2, 
'Plumhead::Grammar::expression')
+                         if null cpast goto iter_loop
+                         cpast3 = tree.'get'('past', cnode, 
'Plumhead::Grammar::code')
+                         cpast2 = new 'PAST::Op'
+                         cpast2.'init'( cpast, cpast3, 'node'=> node, 
'pasttype' => 'if' )  
+                         past.'push'(cpast2)
+                         goto iter_loop
+                 no_if:
+                 goto iter_loop
          iter_end:
 
     handled_statement:

Reply via email to