consolidated and improved templates
authorAaron Becker <abecker3@illinois.edu>
Mon, 9 Jun 2008 07:36:12 +0000 (07:36 +0000)
committerAaron Becker <abecker3@illinois.edu>
Mon, 9 Jun 2008 07:36:12 +0000 (07:36 +0000)
implemented .h code generation
fixed charmxi output directory problem
fixed method type code generation bug

src/langs/charj/Makefile
src/langs/charj/src/charj/translator/Charj.stg
src/langs/charj/src/charj/translator/CharjEmitter.g
src/langs/charj/src/charj/translator/Translator.java
src/langs/charj/tests/hello.cj

index 689f7e96cea624068ff4722b7fb45aa2f930cca8..182ada8a40289660e721da848ebfedcc50c8931e 100644 (file)
@@ -14,5 +14,5 @@ all:
        $(ANT) all
 
 test:
-       ./bin/charjc --debug --verbose --stdout tests/hello.cj
+       ./bin/charjc --debug --verbose --stdout tests/Hello.cj
 
index 4b9c92377a31d6fdffd3875f843bd2265b7a3d93..a58ef8eb61689201fe6225f1678a77637b3ab024 100644 (file)
@@ -22,13 +22,7 @@ classExtends_ci(type) ::= "<type>"
 classExtends_cc(type) ::= ": public CBase_<type>"
 
 
-interfaceExtends_ci(ts) ::=
-<<
-: public <ts; separator=", public ">
->>
-
-
-interfaceExtends_cc(ts) ::=
+interfaceExtends(ts) ::=
 <<
 : public <ts; separator=", public ">
 >>
@@ -37,7 +31,7 @@ interfaceExtends_cc(ts) ::=
 classDeclaration_ci(mod, ident, gen, ext, impl, ctls) ::=
 <<
 module <ident> {
-    <ident> <gen>: public <ext> {
+    chare <ident> <gen>: <ext> {
         <ctls>
     }
 }
@@ -45,12 +39,23 @@ module <ident> {
 >>
 
 
+classDeclaration_h(mod, ident, gen, ext, impl, ctls) ::=
+<<
+
+
+class <ident> <gen>: public CBase_<ident> <impl> {
+    <ctls>
+};
+
+
+>>
+
 classDeclaration_cc(mod, ident, gen, ext, impl, ctls) ::=
 <<
 
 
 #include "<ident>.decl.h"
-<mod> class <ident> <gen>: public CBase_<ident> <impl> {
+class <ident> <gen>: public CBase_<ident> <impl> {
     <ctls>
 };
 #include "<ident>.def.h"
@@ -59,7 +64,7 @@ classDeclaration_cc(mod, ident, gen, ext, impl, ctls) ::=
 >>
 
 
-classTopLevelScope_ci(classScopeDeclarations) ::= 
+classTopLevelScope(classScopeDeclarations) ::= 
 <<
 // _classTopLevelScope_
 <classScopeDeclarations; separator="\n">
@@ -67,11 +72,16 @@ classTopLevelScope_ci(classScopeDeclarations) ::=
 >>
 
 
-classTopLevelScope_cc(classScopeDeclarations) ::= 
+funcMethodDecl_h(modl, 
+            gtpl,
+            ty,
+            id,
+            fpl,
+            adl,
+            tc,
+            block) ::=
 <<
-// _classTopLevelScope_
-<classScopeDeclarations; separator="\n">
-// _!classTopLevelScope_
+<modl>: <ty> <gtpl> <id><fpl> <adl> <tc>;
 >>
 
 
@@ -94,6 +104,12 @@ entry void<gtpl> <id><fpl> <tc>;
 >>
 
 
+voidMethodDecl_h(modl, gtpl, id, fpl, tc, block) ::=
+<<
+<modl>: void<gtpl> <id><fpl> <tc>;
+>>
+
+
 voidMethodDecl_cc(modl, gtpl, id, fpl, tc, block) ::=
 <<
 <modl>: void<gtpl> <id><fpl> <tc> <block>
@@ -106,6 +122,12 @@ entry<gtpl> <id><fpl> <tc>;
 >>
 
 
+ctorDecl_h(modl, gtpl, id, fpl, tc, block) ::=
+<<
+<modl>:<gtpl> <id><fpl> <tc>;
+>>
+
+
 ctorDecl_cc(modl, gtpl, id, fpl, tc, block) ::=
 <<
 <modl>:<gtpl> <id><fpl> <tc> <block>
index 16a208481bc135661bb591b5a32076210e6ff913..ae1eb0b16412649155ada756b9b34193289f0952 100644 (file)
@@ -108,22 +108,32 @@ importDeclaration
     ;
     
 typeDeclaration
-    :   ^(CLASS m=modifierList IDENT g=genericTypeParameterList? e=classExtendsClause? i=implementsClause? c=classTopLevelScope) 
+    :   ^(CLASS m=modifierList IDENT g=genericTypeParameterList? 
+                e=classExtendsClause? i=implementsClause? c=classTopLevelScope) 
         -> {emitCC()}? classDeclaration_cc(
-            mod={$m.st}, 
-            ident={$IDENT.text}, 
-            gen={$g.st}, 
-            ext={$e.st}, 
-            impl={$i.st},
-            ctls={$c.st})
-        -> classDeclaration_ci(
-            mod={$m.st}, 
-            ident={$IDENT.text}, 
-            gen={$g.st}, 
-            ext={$e.st}, 
-            impl={$i.st},
-            ctls={$c.st})
-    |   ^(INTERFACE modifierList IDENT genericTypeParameterList? interfaceExtendsClause? interfaceTopLevelScope)
+                mod={$m.st}, 
+                ident={$IDENT.text}, 
+                gen={$g.st}, 
+                ext={$e.st}, 
+                impl={$i.st},
+                ctls={$c.st})
+        -> {emitCI()}? classDeclaration_ci(
+                mod={$m.st}, 
+                ident={$IDENT.text}, 
+                gen={$g.st}, 
+                ext={$e.st}, 
+                impl={$i.st},
+                ctls={$c.st})
+        -> {emitH()}? classDeclaration_h(
+                mod={$m.st}, 
+                ident={$IDENT.text}, 
+                gen={$g.st}, 
+                ext={$e.st}, 
+                impl={$i.st},
+                ctls={$c.st})
+        ->
+    |   ^(INTERFACE modifierList IDENT genericTypeParameterList? 
+                interfaceExtendsClause? interfaceTopLevelScope)
         -> template(t={$text}) "<t>"
     |   ^(ENUM modifierList IDENT implementsClause? enumTopLevelScope)
         -> template(t={$text}) "<t>"
@@ -132,14 +142,14 @@ typeDeclaration
 
 classExtendsClause
     :   ^(EXTENDS_CLAUSE t=type) 
-        -> {emitCC()}? classExtends_cc(type={$t.st})
-        -> classExtends_ci(type={$t.st})
+        -> {emitCC() || emitH()}? classExtends_cc(type={$t.st})
+        -> {emitCI()}? classExtends_ci(type={$t.st})
+        ->
     ;   
 
 interfaceExtendsClause 
     :   ^(EXTENDS_CLAUSE (typeList+=type)+) 
-        -> {emitCC()}? interfaceExtends_cc(ts={$typeList})
-        -> interfaceExtends_ci(ts={$typeList})
+        -> interfaceExtends(ts={$typeList})
     ;   
     
 implementsClause
@@ -175,8 +185,7 @@ enumConstant
     
 classTopLevelScope
     :   ^(CLASS_TOP_LEVEL_SCOPE (csd+=classScopeDeclarations)*) 
-        -> {emitCC()}? classTopLevelScope_cc(classScopeDeclarations={$csd})
-        -> classTopLevelScope_ci(classScopeDeclarations={$csd})
+        -> classTopLevelScope(classScopeDeclarations={$csd})
     ;
     
 classScopeDeclarations
@@ -186,32 +195,50 @@ classScopeDeclarations
         -> template(t={$text}) "/*csi*/ <t>"
     |   ^(FUNCTION_METHOD_DECL m=modifierList g=genericTypeParameterList? 
             ty=type IDENT f=formalParameterList a=arrayDeclaratorList? 
-            tt=throwsClause? b=block?)
-        -> funcMethodDecl_cc(
-            modl={$m.st}, 
-            gtpl={$g.st}, 
-            ty={$t.text},
-            id={$IDENT.text}, 
-            fpl={$f.st}, 
-            adl={$a.st},
-            tc={$tt.st}, 
-            block={$b.st})
+            tc=throwsClause? b=block?)
+        -> {emitCC()}? funcMethodDecl_cc(
+                modl={$m.st}, 
+                gtpl={$g.st}, 
+                ty={$ty.text},
+                id={$IDENT.text}, 
+                fpl={$f.st}, 
+                adl={$a.st},
+                tc={$tc.st}, 
+                block={$b.st})
+        -> {emitH()}? funcMethodDecl_h(
+                modl={$m.st}, 
+                gtpl={$g.st}, 
+                ty={$ty.text},
+                id={$IDENT.text}, 
+                fpl={$f.st}, 
+                adl={$a.st},
+                tc={$tc.st}, 
+                block={$b.st})
+        ->
     |   ^(VOID_METHOD_DECL m=modifierList g=genericTypeParameterList? IDENT 
             f=formalParameterList t=throwsClause? b=block?)
         -> {emitCC()}? voidMethodDecl_cc(
-            modl={$m.st}, 
-            gtpl={$g.st}, 
-            id={$IDENT.text}, 
-            fpl={$f.st}, 
-            tc={$t.st}, 
-            block={$b.st})
-        -> voidMethodDecl_ci(
-            modl={$m.st}, 
-            gtpl={$g.st}, 
-            id={$IDENT.text}, 
-            fpl={$f.st}, 
-            tc={$t.st}, 
-            block={$b.st})
+                modl={$m.st}, 
+                gtpl={$g.st}, 
+                id={$IDENT.text}, 
+                fpl={$f.st}, 
+                tc={$t.st}, 
+                block={$b.st})
+        -> {emitCI()}? voidMethodDecl_ci(
+                modl={$m.st}, 
+                gtpl={$g.st}, 
+                id={$IDENT.text}, 
+                fpl={$f.st}, 
+                tc={$t.st}, 
+                block={$b.st})
+        -> {emitH()}? voidMethodDecl_h(
+                modl={$m.st}, 
+                gtpl={$g.st}, 
+                id={$IDENT.text}, 
+                fpl={$f.st}, 
+                tc={$t.st}, 
+                block={$b.st})
+        ->
 
 // template(t={$text}) "/*voidmethod*/ <t>"
     |   ^(VAR_DECLARATION modifierList type variableDeclaratorList)
@@ -219,20 +246,27 @@ classScopeDeclarations
     |   ^(CONSTRUCTOR_DECL m=modifierList g=genericTypeParameterList? IDENT f=formalParameterList 
             t=throwsClause? b=block)
         -> {emitCC()}? ctorDecl_cc(
-            modl={$m.st}, 
-            gtpl={$g.st}, 
-            id={$IDENT.text}, 
-            fpl={$f.st}, 
-            tc={$t.st}, 
-            block={$b.st})
-        -> ctorDecl_ci(
-            modl={$m.st}, 
-            gtpl={$g.st}, 
-            id={$IDENT.text}, 
-            fpl={$f.st}, 
-            tc={$t.st}, 
-            block={$b.st})
-        //-> template(t={$text}) "ctor <t>"
+                modl={$m.st}, 
+                gtpl={$g.st}, 
+                id={$IDENT.text}, 
+                fpl={$f.st}, 
+                tc={$t.st}, 
+                block={$b.st})
+        -> {emitCI()}? ctorDecl_ci(
+                modl={$m.st}, 
+                gtpl={$g.st}, 
+                id={$IDENT.text}, 
+                fpl={$f.st}, 
+                tc={$t.st}, 
+                block={$b.st})
+        -> {emitH()}? ctorDecl_h(
+                modl={$m.st}, 
+                gtpl={$g.st}, 
+                id={$IDENT.text}, 
+                fpl={$f.st}, 
+                tc={$t.st}, 
+                block={$b.st})
+        ->
     |   d=typeDeclaration
         -> template(t={$d.st}) "type <t>"
     ;
index 9869149c8a27679163f1a66e73d95fe9eaf64110..895eb1b6c4674b316fa62c548aed406dfe364e18 100644 (file)
@@ -7,7 +7,6 @@ import java.io.*;
 import org.antlr.runtime.*;
 import org.antlr.runtime.tree.*;
 import org.antlr.stringtemplate.*;
-import charj.translator.StreamEmitter;
 
 
 /**
@@ -103,7 +102,7 @@ public class Translator {
         String tempFile = filename.substring(0, lastSlash + 1) + ".charj/";
         new File(tempFile).mkdir();
         tempFile += filename.substring(lastSlash + 1, lastDot) + m.extension();
-        if (m_verbose) System.out.println("\n [charjc] create: " + tempFile);
+        if (m_verbose) System.out.println(" [charjc] create: " + tempFile);
         FileWriter fw = new FileWriter(tempFile);
         fw.write(output);
         fw.close();
@@ -123,17 +122,34 @@ public class Translator {
     {
         int lastDot = filename.lastIndexOf(".");
         int lastSlash = filename.lastIndexOf("/");
-        String baseFilename = filename.substring(0, lastSlash + 1) + 
-            ".charj/" + filename.substring(lastSlash + 1, lastDot);
-        String cmd = charmc + " " + baseFilename + ".ci";
+        String baseDirectory = filename.substring(0, lastSlash + 1);
+        String tempDirectory = baseDirectory + ".charj/";
+        String moduleName = filename.substring(lastSlash + 1, lastDot);
+        String baseTempFilename = tempDirectory + moduleName;
+
+        // Compile interface file
+        String cmd = charmc + " " + baseTempFilename + ".ci";
         File currentDir = new File(".");
         int retVal = exec(cmd, currentDir);
         if (retVal != 0) {
             error("Could not compile generated interface file");
             return;
         }
-        
-        cmd = charmc + " -c " + baseFilename + ".cc";
+
+        // Move decl.h and def.h into temp directory.
+        // charmxi/charmc doesn't offer control over where to generate these
+        cmd = "mv " + moduleName + ".decl.h " + moduleName + ".def.h " +
+            tempDirectory;
+        retVal = exec(cmd, currentDir);
+         if (retVal != 0) {
+            error("Could not move .decl.h and .def.h files " +
+                    "into temp directory");
+            return;
+        }       
+
+        // Compile c++ output
+        cmd = charmc + " -c " + baseTempFilename + ".cc" + 
+            " -o " + baseTempFilename + ".o";
         retVal = exec(cmd, currentDir);
         if (retVal != 0) {
             error("Could not compile generated C++ file");
@@ -141,9 +157,9 @@ public class Translator {
         }
 
         // move generated .o and .h file into .cj directory
-        cmd = "mv -f " + baseFilename + ".o" + " .";
+        cmd = "mv -f " + baseTempFilename + ".o " + baseDirectory;
         exec(cmd, currentDir);
-        cmd = "cp -f " + baseFilename + ".h" + " .";
+        cmd = "cp -f " + baseTempFilename + ".h " + baseDirectory;
         exec(cmd, currentDir);
     }
 
@@ -153,7 +169,7 @@ public class Translator {
     private int exec(String cmd, File outputDir) throws
         IOException, InterruptedException
     {
-        if (m_verbose) System.out.println("\n [charjc] exec: " + cmd + "\n");
+        if (m_verbose) System.out.println(" [charjc] exec: " + cmd);
         Process p = Runtime.getRuntime().exec(cmd, null, outputDir);
         StreamEmitter stdout = new StreamEmitter(
                 p.getInputStream(), System.out);
index 573092bd75d86a926399d7c917dee923a04b1387..7769ed4ce1be6064c4f782bc12d8cb309444752a 100644 (file)
@@ -4,15 +4,15 @@ import charj.lang.someObject;
 public class Hello extends Chare {
     entry Hello() {}
     entry void sayHello(int payload) {
-        MyObject someObject = new MyObject(1, 2, "hello", new Array<int>(1));
-        int[] myArray = new int[25];
-        Array<int> myGenericArray = new Array<int>(25);
+        //MyObject someObject = new MyObject(1, 2, "hello", new Array<int>(1));
+        //int myArray[25] = new int[25];
+        //Array<int> myGenericArray = new Array<int>(25);
         for (int i=0; i<25; ++i) {
-            myArray[i] = payload + i;
-            myGenericArray[i] = payload * i;
+            //myArray[i] = payload + i;
+            //myGenericArray[i] = payload * i;
         }
     }
     public int anotherMethod(int x) { return x; }
-    private void aVoidMethod() {}
+//    private void aVoidMethod() {}
 }