Author: bernhard
Date: Wed Feb  1 10:58:39 2006
New Revision: 11398

Modified:
   trunk/languages/bc/Bc.java
   trunk/languages/bc/grammar/antlr_3/antlr_past2pir_past.g
   trunk/languages/bc/lib/Parrot/Test/Bc/Antlr2.pm
   trunk/languages/bc/lib/Parrot/Test/Bc/Antlr3.pm
Log:
Parrot bc: Let antlr_past2pir_past.g print out dummy PIR,
that P::T::Bc::Antlr executes.


Modified: trunk/languages/bc/Bc.java
==============================================================================
--- trunk/languages/bc/Bc.java  (original)
+++ trunk/languages/bc/Bc.java  Wed Feb  1 10:58:39 2006
@@ -1,5 +1,9 @@
 // $Id$
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
+
 import org.antlr.runtime.*;
 import org.antlr.runtime.tree.*;
 
@@ -7,13 +11,25 @@ public class Bc 
 {
   public static void main(String[] args) throws Exception 
   {
-    CharStream input = new ANTLRFileStream(args[0]);
-    System.out.println( "1" );
+    // XXX proper command line parsing
+    String bcFn  = args[0];
+    String pirFn = args[1];
+
+    // lexing
+    CharStream input = new ANTLRFileStream(bcFn);
     BcParserLexer lex = new BcParserLexer(input);
     CommonTokenStream tokens = new CommonTokenStream(lex);
     // System.out.println("tokens="+tokens);
+
+    // Parsing, generating PAST in ANTLT
     BcParser parser = new BcParser(tokens);
-    BcParser.program_return r = parser.program();
-    // System.out.println("tree: "+((Tree)r.tree).toStringTree());
+    BcParser.program_return antlrPast = parser.program();
+    // System.out.println("tree: "+((Tree)antlrPast.tree).toStringTree());
+    
+    // Printing out PIR, that sets up PAST in PIR
+    System.setOut( new PrintStream( new FileOutputStream( pirFn ) ) );
+    CommonTreeNodeStream nodes = new 
CommonTreeNodeStream((Tree)antlrPast.tree);
+    AntlrPast2PirPast treeParser = new AntlrPast2PirPast(nodes);
+    treeParser.gen_pir_past();
   }
 }

Modified: trunk/languages/bc/grammar/antlr_3/antlr_past2pir_past.g
==============================================================================
--- trunk/languages/bc/grammar/antlr_3/antlr_past2pir_past.g    (original)
+++ trunk/languages/bc/grammar/antlr_3/antlr_past2pir_past.g    Wed Feb  1 
10:58:39 2006
@@ -1,20 +1,6 @@
 // Copyright: 2006 The Perl Foundation.  All Rights Reserved.
 // $Id$
 
-// header "AntlrPast2PirPastTreeWalker.__init__" 
-// {
-  // self.reg       = 10;  // counter for unlimited number of PMC registers
-// }
-
-//-----------------------------------------------------------------------------
-// Options for ANTLR
-//-----------------------------------------------------------------------------
-// options 
-// {
-  // language = "Python";      // generate Lexer, Parser and TreeParser in 
Python
-// }
-
-
 //----------------------------------------------------------------------------
 // Transform ANTLR PAST to PIR that sets up PAST
 //----------------------------------------------------------------------------
@@ -29,21 +15,134 @@ options
 gen_pir_past
   : ^(PROGRAM INT)
     {
-      System.out.println(" OK in antlr_past2pir_past");
+      String pirBefore 
+        =   "#!/usr/bin/env parrot\n"
+          + "\n"
+          + "# Do not edit this file.\n"
+          + "# This file has been generated by Bc.java.\n"
+          + "\n"
+          + ".sub bc :main\n"
+          + "  load_bytecode 'languages/punie/lib/PAST.pir'\n"
+          + "  load_bytecode 'TGE.pbc'\n"
+          + "  load_bytecode 'languages/punie/lib/POST.pir'\n"
+          + "\n"
+          + "  .local pmc stmts_children\n"
+          + "  stmts_children = new PerlArray\n";
+      String pirAfter
+        =   "  .local pmc stmts\n"
+          + "  stmts = new 'PAST::Stmts'\n"
+          + "  stmts.set_node('1', 1, stmts_children)\n"
+          + "\n"
+          + "  # Compile the abstract syntax tree down to an opcode syntax 
tree\n"
+          + "  .local string ost_tg_source\n"
+          + "  ost_tg_source = 
_slurp_file('languages/punie/lib/past2post.g')\n"
+          + "  .local pmc ostgrammar\n"
+          + "  ostgrammar = new 'TGE'\n"
+          + "  ostgrammar.agcompile(ost_tg_source)\n"
+          + "  .local pmc ostbuilder\n"
+          + "  ostbuilder = ostgrammar.apply(stmts)\n"
+          + "  .local pmc ost\n"
+          + "  ost = ostbuilder.get('result')\n"
+          + "  \u0024I0 = defined ost\n"
+          + "  unless \u0024I0 goto err_no_ost # if OST fails stop\n"
+          + "\n"
+          + "  # Compile the OST down to PIR\n"
+          + "  .local string pir_tg_source\n"
+          + "  pir_tg_source = _slurp_file('languages/punie/lib/post2pir.g')\n"
+          + "  .local pmc pirgrammar\n"
+          + "  pirgrammar = new 'TGE'\n"
+          + "  pirgrammar.agcompile(pir_tg_source)\n"
+          + "  .local pmc pirbuilder\n"
+          + "  pirbuilder = pirgrammar.apply(ost)\n"
+          + "  .local pmc pir\n"
+          + "  pir = pirbuilder.get('result')\n"
+          + "  unless pir goto err_no_pir # if PIR not generated, stop\n"
+          + "\n"
+          + "  # Execute\n"
+          + "  .local pmc pir_compiler\n"
+          + "  .local pmc pir_compiled\n"
+          + "  pir_compiler = compreg \"PIR\"\n"
+          + "  pir_compiled = pir_compiler( pir )\n"
+          + "\n"
+          + "  pir_compiled()\n"
+          + "\n"
+          + "  print \"\\n\"\n"
+          + "\n"
+          + "  end\n"
+          + "\n"
+          + "  err_match_fail:\n"
+          + "    print \"parse failed\\n\"\n"
+          + "    goto cleanup\n"
+          + "\n"
+          + "  err_no_ast:\n"
+          + "    print 'Unable to construct AST.'\n"
+          + "    goto cleanup\n"
+          + "\n"
+          + "  err_no_ost:\n"
+          + "    print 'Unable to construct OST.'\n"
+          + "    goto cleanup\n"
+          + "\n"
+          + "  err_no_pir:\n"
+          + "    print 'Unable to construct PIR.'\n"
+          + "    goto cleanup\n"
+          + "\n"
+          + "  cleanup:\n"
+          + ".end\n"
+          + "\n"
+          + ".sub _slurp_file\n"
+          + "  .param string filename\n"
+          + "  .local pmc filehandle\n"
+          + "  filehandle = open filename, '<'\n"
+          + "  unless filehandle goto err_no_file\n"
+          + "  \u0024S1 = read filehandle, 65535\n"
+          + "  close filehandle\n"
+          + "  .return (\u0024S1)\n"
+          + "  err_no_file:\n"
+          + "    print 'Unable to open file: '\n"
+          + "    print filename\n"
+          + "    end\n"
+          + ".end\n";
+      String pirDummy
+        =   "               \u0024P10 = new 'PAST::Val' \n"
+          + "               \u0024P10.set_node( '1', 0, '1' ) \n"
+          + "               \u0024P10.valtype( 'num' ) \n"
+          + "               \u0024P20 = new 'PAST::Exp' \n"
+          + "               \u0024P21 = new PerlArray \n"
+          + "               push \u0024P21, \u0024P10 \n"
+          + "               \u0024P20.set_node('1', 1, \u0024P21) \n"
+          + "       \u0024P30 = new 'PAST::Op' \n"
+          + "       \u0024P31 = new PerlArray \n"
+          + "       push \u0024P31, \u0024P20 \n"
+          + "       \u0024P30.set_node('1', 1, 'print' ,\u0024P31) \n"
+          + "               \u0024P40 = new 'PAST::Exp' \n"
+          + "               \u0024P41 = new PerlArray \n"
+          + "               push \u0024P41, \u0024P30 \n"
+          + "               \u0024P40.set_node('1', 1, \u0024P41) \n"
+          + "  \u0024P50 = new 'PAST::Stmt' \n"
+          + "  \u0024P51 = new PerlArray \n"
+          + "  push \u0024P51, \u0024P40 \n"
+          + "  \u0024P50.set_node('1', 1 ,\u0024P51) \n"
+          + "  push stmts_children, \u0024P50 \n"
+          + "               \u0024P60 = new 'PAST::Val' \n"
+          + "               \u0024P60.set_node( '1', 0, '\\n' ) \n"
+          + "               \u0024P60.valtype( 'strqq' ) \n"
+          + "               \u0024P70 = new 'PAST::Exp' \n"
+          + "               \u0024P71 = new PerlArray \n"
+          + "               push \u0024P71, \u0024P60 \n"
+          + "               \u0024P70.set_node('1', 1, \u0024P71) \n"
+          + "       \u0024P80 = new 'PAST::Op' \n"
+          + "       \u0024P81 = new PerlArray \n"
+          + "       push \u0024P81, \u0024P70 \n"
+          + "       \u0024P80.set_node('1', 1, 'print' ,\u0024P81) \n"
+          + "               \u0024P90 = new 'PAST::Exp' \n"
+          + "               \u0024P91 = new PerlArray \n"
+          + "               push \u0024P91, \u0024P80 \n"
+          + "               \u0024P90.set_node('1', 1, \u0024P91) \n"
+          + "  \u0024P100 = new 'PAST::Stmt' \n"
+          + "  \u0024P101 = new PerlArray \n"
+          + "  push \u0024P101, \u0024P90 \n"
+          + "  \u0024P100.set_node('1', 1 ,\u0024P101) \n"
+          + "  push stmts_children, \u0024P100 \n";
+       System.out.print( pirBefore + pirDummy + pirAfter );    
     }
   ;
-/*      
-pir_before = """
-.local pmc stmts_children
-stmts_children = new PerlArray
-#"""
-      pir_after = """
-.local pmc stmts
-stmts = new 'PAST::Stmts'
-
-stmts.set_node('1', 1, stmts_children)
-            
-#"""
-
-      #gen_pir_past = #( [ PIR_OP, pir_before ], #PEPN, [PIR_OP, pir_after] ); 
-*/

Modified: trunk/languages/bc/lib/Parrot/Test/Bc/Antlr2.pm
==============================================================================
--- trunk/languages/bc/lib/Parrot/Test/Bc/Antlr2.pm     (original)
+++ trunk/languages/bc/lib/Parrot/Test/Bc/Antlr2.pm     Wed Feb  1 10:58:39 2006
@@ -20,11 +20,11 @@ sub get_test_prog {
     my $self = shift;
     my ( $count, $options ) = @_;
 
-    my $lang_fn        = Parrot::Test::per_test( '.bc', $count );
-    my $pir_fn         = $options->{with_past} ?
-                             Parrot::Test::per_test( '_antlr2.pir', $count )
-                             :
-                             Parrot::Test::per_test( '_antlr2_no_past.pir', 
$count );
+    my $lang_fn = Parrot::Test::per_test( '.bc', $count );
+    my $pir_fn  = $options->{with_past} ?
+                    Parrot::Test::per_test( '_antlr2.pir', $count )
+                    :
+                    Parrot::Test::per_test( '_antlr2_no_past.pir', $count );
 
     return ( "python languages/bc/bc.py languages/${lang_fn}", 
              "$self->{parrot} languages/${pir_fn}" );

Modified: trunk/languages/bc/lib/Parrot/Test/Bc/Antlr3.pm
==============================================================================
--- trunk/languages/bc/lib/Parrot/Test/Bc/Antlr3.pm     (original)
+++ trunk/languages/bc/lib/Parrot/Test/Bc/Antlr3.pm     Wed Feb  1 10:58:39 2006
@@ -17,9 +17,11 @@ sub get_test_prog {
     my $self = shift;
     my ( $count, $options ) = @_;
 
-    my $lang_fn        = Parrot::Test::per_test( '.bc', $count );
+    my $lang_fn = Parrot::Test::per_test( '.bc', $count );
+    my $pir_fn  = Parrot::Test::per_test( '_antlr3.pir', $count );
 
-    return ( "java Bc languages/${lang_fn}", );
+    return ( "java Bc languages/${lang_fn} languages/${pir_fn}",
+             "$self->{parrot} languages/${pir_fn}" );
 }
  
 
@@ -28,6 +30,7 @@ sub set_todo {
     my ( $options ) = @_;
 
     if ( ! $options->{with_antlr3} ) {
+        # XXX make this work
         # $self->{builder}->todo_skip( 'Not implemented with ANTLR3' );
     }
 

Reply via email to