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


 adds preamble section to tge grammar to allow for includes and global 
defines at the top of the PIR generated by TGE

For example, I use

preamble{
.include 'interpinfo.pasm'
}

The diff is relative to
compilers/tge

 Compiler.pir |   62 
++++++++++++++++++++++++++++++++++++++++++-----------------
 Parser.pg    |   24 +++++++++++++---------
 2 files changed, 59 insertions(+), 27 deletions(-)

Kevin
Index: TGE/Parser.pg
===================================================================
--- TGE/Parser.pg       (revision 13329)
+++ TGE/Parser.pg       (working copy)
@@ -3,25 +3,29 @@
 token start { ^ <TGE::Parser::statements>* $ }
 
 rule statements { 
-     <TGE::Parser::transrule>
-    | <TGE::Parser::grammardec>
+     <transrule>
+    | <grammardec>
+    | <preamble>
 }
 
+rule preamble {
+    preamble <action>
+}
+
 rule grammardec {
-    grammar <TGE::Parser::type>
-    <TGE::Parser::inherit>?;
+    grammar <type> <inherit>?;
 }
 
 rule inherit {
-    is <TGE::Parser::type>
+    is <type>
 }
 
 rule transrule {
-    transform <TGE::Parser::name> 
-    \( <TGE::Parser::type> \)
-    <TGE::Parser::parent>?
-    <TGE::Parser::language>?
-    <TGE::Parser::action>
+    transform <name> 
+    \( <type> \)
+    <parent>?
+    <language>?
+    <action>
 }
     
 rule parent {
Index: TGE/Compiler.pir
===================================================================
--- TGE/Compiler.pir    (revision 13329)
+++ TGE/Compiler.pir    (working copy)
@@ -31,7 +31,7 @@
     .local pmc match
     .local pmc start_rule
     start_rule = find_global "TGE::Parser", "start"
-    match = start_rule(source)
+    match = start_rule(source, 'grammar'=>'TGE::Parser')
     # Verify the parse
     $I0 = match.__get_bool()
     unless $I0 goto err_parse    # if parse fails, stop
@@ -61,6 +61,7 @@
     self.add_rule("statement",  "result", ".", "statement_result")
     self.add_rule("transrule",  "result", ".", "transrule_result")
     self.add_rule("grammardec", "result", ".", "grammardec_result")
+    self.add_rule("preamble",   "result", ".", "preamble_result")
     self.add_rule("type",       "value",  ".", "type_value")
     self.add_rule("inherit",    "value",  ".", "inherit_value")
     self.add_rule("name",       "value",  ".", "name_value")
@@ -120,9 +121,8 @@
     unless iter, iter_end         # while (entries) ...
       shift $S1, iter           # get the key of the iterator
       $P2 = iter[$S1]
-      $S2 = substr $S1, 13
 
-      result = tree.get('result', $P2, $S2)
+      result = tree.get('result', $P2, $S1)
 
       goto iter_loop
   iter_end:
@@ -144,11 +144,10 @@
       $P3 = new Undef
       shift $S1, iter           # get the key of the iterator
       $P2 = iter[$S1]
-      $S2 = substr $S1, 13
 
-      $P3 = tree.get('value', $P2, $S2)
+      $P3 = tree.get('value', $P2, $S1)
 
-      rule[$S2] = $P3
+      rule[$S1] = $P3
       goto iter_loop
   iter_end:
 
@@ -165,6 +164,31 @@
     .return ()
 .end
 
+# preamble contains global includes and macro definitions
+.sub preamble_result :method
+    .param pmc tree
+    .param pmc node
+    .local pmc preamble
+    preamble = new .Hash
+
+    .local pmc iter
+    iter = new Iterator, node    # setup iterator for node
+    iter = 0
+  iter_loop:
+    unless iter, iter_end         # while (entries) ...
+      $P3 = new Undef
+      shift $S1, iter           # get the key of the iterator
+      $P2 = iter[$S1]
+
+      $P3 = tree.get('value', $P2, $S1)
+
+      preamble[$S1] = $P3
+      goto iter_loop
+  iter_end:
+    preamble["build"] = "preamble"
+    .return (preamble)
+.end
+
 .sub grammardec_result :method
     .param pmc tree
     .param pmc node
@@ -179,11 +203,10 @@
       $P3 = new Undef
       shift $S1, iter           # get the key of the iterator
       $P2 = iter[$S1]
-      $S2 = substr $S1, 13
 
-      $P3 = tree.get('value', $P2, $S2)
+      $P3 = tree.get('value', $P2, $S1)
 
-      decl[$S2] = $P3
+      decl[$S1] = $P3
       goto iter_loop
   iter_end:
     decl["build"] = "grammar"
@@ -195,7 +218,7 @@
     .param pmc tree
     .param pmc node
     $P1 = node[0]
-    $P2 = $P1['TGE::Parser::type']
+    $P2 = $P1['type']
     .local pmc value
     value = tree.get('value', $P2, 'type')
     .return (value)
@@ -278,7 +301,7 @@
     # Unnamed grammars are class 'AnonGrammar'
     .local string grammarname
     grammarname = 'AnonGrammar'
-     rule_data = self.'parse_grammar'(source)
+    rule_data = self.'parse_grammar'(source)
 
     # Construct grammar rules from the data structure of rule info
     .local pmc statement
@@ -289,17 +312,22 @@
     unless iter goto loop_end
         statement = shift iter
         $S0 = statement['build']
-        if $S0 == 'grammar' goto grammar_build
+      unless $S0 == 'rule' goto grammar_build
           $S1 = self.'rule_string'(statement)
           outstring .= $S1
           $S2 = self.'rule_header'(statement)
           header_string .= $S2
-    goto loop_start
+          goto loop_start
       grammar_build:
-        $S1 = self.'grammar_string'(statement)
-        outstring .= $S1
-        grammarname = statement['type']
-    goto loop_start
+      unless $S0 == 'grammar' goto preamble_build
+          $S1 = self.'grammar_string'(statement)
+          outstring .= $S1
+          grammarname = statement['type']
+          goto loop_start
+      preamble_build:
+          $S1 = statement['action']
+          outstring .= $S1
+          goto loop_start
 loop_end:
 
     outstring .= "\n.sub __init :method\n"

Reply via email to