Added embed keyword and functionality
authorAaron Becker <abecker3@illinois.edu>
Tue, 10 Jun 2008 00:41:13 +0000 (00:41 +0000)
committerAaron Becker <abecker3@illinois.edu>
Tue, 10 Jun 2008 00:41:13 +0000 (00:41 +0000)
Added all c++ keywords as charj keywords to prevent incompatibilities
Improved import mechanism for inheritance
Nicer looking block output
Added special debugging cases to makefile
Added embed block to test case
fixed block templates to allow child output

src/langs/charj/Makefile
src/langs/charj/src/charj/translator/Charj.g
src/langs/charj/src/charj/translator/Charj.stg
src/langs/charj/src/charj/translator/CharjEmitter.g
src/langs/charj/tests/Hello.cj

index 182ada8a40289660e721da848ebfedcc50c8931e..27318042bc0ea861417639eabe6fdbd40b87f50a 100644 (file)
@@ -16,3 +16,10 @@ all:
 test:
        ./bin/charjc --debug --verbose --stdout tests/Hello.cj
 
+
+# The lousy antlrv3 ant task won't give proper output from antlr errors, so
+# use these horrible direct invocations instead
+debug-charj:
+       java -classpath /Users/abecker/Documents/workspace/charm/src/langs/charj/lib/antlr-2.7.7.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/antlr-3.1b1.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/antlr-runtime-3.1b1.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/stringtemplate-3.1.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/gunit-1.0.2.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/JSAP-2.1.jar org.antlr.Tool -o /Users/abecker/Documents/workspace/charm/src/langs/charj/build/src/charj/translator -lib /Users/abecker/Documents/workspace/charm/src/langs/charj/build/src/charj/translator /Users/abecker/Documents/workspace/charm/src/langs/charj/build/src/charj/translator/Charj.g
+debug-emitter:
+       java -classpath /Users/abecker/Documents/workspace/charm/src/langs/charj/lib/antlr-2.7.7.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/antlr-3.1b1.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/antlr-runtime-3.1b1.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/stringtemplate-3.1.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/gunit-1.0.2.jar:/Users/abecker/Documents/workspace/charm/src/langs/charj/lib/JSAP-2.1.jar org.antlr.Tool -o /Users/abecker/Documents/workspace/charm/src/langs/charj/build/src/charj/translator -lib /Users/abecker/Documents/workspace/charm/src/langs/charj/build/src/charj/translator /Users/abecker/Documents/workspace/charm/src/langs/charj/build/src/charj/translator/CharjEmitter.g
index aec74fc50e30e9d62499d7c3b45b6f79ea55652a..6e39a4582fb5c911ea12ee92c7833c47226eebbd 100644 (file)
@@ -69,7 +69,11 @@ tokens {
     XOR                     = '^'               ;
     XOR_ASSIGN              = '^='              ;
 
-    // keywords
+    // Charj-specific operators
+    DOUBLE_LCURLY           = '{{'              ;
+    DOUBLE_RCURLY           = '}}'              ;
+
+    // Keywords common to Java and Charj
     
     ABSTRACT                = 'abstract'        ;
     ASSERT                  = 'assert'          ;
@@ -82,6 +86,7 @@ tokens {
     CLASS                   = 'class'           ;
     CONTINUE                = 'continue'        ;
     DEFAULT                 = 'default'         ;
+    DELETE                  = 'delete'          ;
     DO                      = 'do'              ;
     DOUBLE                  = 'double'          ;
     ELSE                    = 'else'            ;
@@ -106,7 +111,6 @@ tokens {
     PRIVATE                 = 'private'         ;
     PROTECTED               = 'protected'       ;
     PUBLIC                  = 'public'          ;
-    ENTRY                   = 'entry'           ;
     RETURN                  = 'return'          ;
     SHORT                   = 'short'           ;
     STATIC                  = 'static'          ;
@@ -122,6 +126,45 @@ tokens {
     VOID                    = 'void'            ;
     VOLATILE                = 'volatile'        ;
     WHILE                   = 'while'           ;
+
+    // Charj-specific keywords
+    EMBED                   = 'embed'           ;
+    ENTRY                   = 'entry'           ;
+
+
+    // C++ keywords that aren't used in charj. 
+    // We don't use these ourselves, but they're still reserved
+    ASM                     = 'asm'             ;
+    AUTO                    = 'auto'            ;
+    BOOL                    = 'bool'            ;
+    CONST_CAST              = 'const_cast'      ;
+    DYNAMIC_CAST            = 'dynamic_cast'    ;
+    EXPLICIT                = 'explicit'        ;
+    EXPORT                  = 'export'          ;
+    EXTERN                  = 'extern'          ;
+    FRIEND                  = 'friend'          ;
+    GOTO                    = 'goto'            ;
+    INLINE                  = 'inline'          ;
+    MUTABLE                 = 'mutable'         ;
+    NAMESPACE               = 'namespace'       ;
+    OPERATOR                = 'operator'        ;
+    REGISTER                = 'register'        ;
+    REINTERPRET_CAST        = 'reinterpret_cast';
+    SIGNED                  = 'signed'          ;
+    SIZEOF                  = 'sizeof'          ;
+    STATIC_CAST             = 'static_cast'     ;
+    STRUCT                  = 'struct'          ;
+    TEMPLATE                = 'template'        ;
+    TYPEDEF                 = 'typedef'         ;
+    TYPEID                  = 'typeid'          ;
+    TYPENAME                = 'typename'        ;
+    UNION                   = 'union'           ;
+    UNSIGNED                = 'unsigned'        ;
+    USING                   = 'using'           ;
+    VIRTUAL                 = 'virtual'         ;
+    WCHAR_T                 = 'wchar_t'         ;
+    
+    
     
     // tokens for imaginary nodes
     
@@ -650,6 +693,7 @@ statement
     |   BREAK IDENT? SEMI                                                   ->  ^(BREAK IDENT?)
     |   CONTINUE IDENT? SEMI                                                ->  ^(CONTINUE IDENT?)
     |   IDENT COLON statement                                               ->  ^(LABELED_STATEMENT IDENT statement)
+    |   EMBED STRING_LITERAL EMBED_BLOCK                                    ->  ^(EMBED STRING_LITERAL EMBED_BLOCK)
     |   expression SEMI!
     |   SEMI // Preserve empty statements.
     ;           
@@ -1044,6 +1088,12 @@ WS  :  (' '|'\r'|'\t'|'\u000C'|'\n')
     }
     ;
 
+EMBED_BLOCK
+    :   DOUBLE_LCURLY '\n'?
+        ( options {greedy=false;} : . )* 
+        DOUBLE_RCURLY 
+    ;
+
 COMMENT
     :   '/*' ( options {greedy=false;} : . )* '*/'
     {   
index d94e85f6483bcd2e88cbd10e720af6e7de65d67e..edc97eefcf1155be25eac4616aa0d95ce8187bb4 100644 (file)
@@ -35,7 +35,7 @@ charjSource_cc(pd, ids, tds) ::=
 classExtends_ci(type) ::= "<type>"
 
 
-classExtends_cc_h(type) ::= ": public CBase_<type>"
+classExtends_cc_h(type) ::= "#include \<<type>.h\>"
 
 
 interfaceExtends(ts) ::=
@@ -47,6 +47,7 @@ interfaceExtends(ts) ::=
 classDeclaration_ci(mod, ident, gen, ext, impl, ctls) ::=
 <<
 module <ident> {
+    extern module <ext>;
     chare <ident> <gen>: <ext> {
         <ctls>
     }
@@ -58,7 +59,8 @@ module <ident> {
 classDeclaration_h(mod, ident, gen, ext, impl, ctls) ::=
 <<
 
-
+#include "<ident>.decl.h"
+<ext>
 class <ident> <gen>: public CBase_<ident> <impl> {
     <ctls>
 };
@@ -71,6 +73,7 @@ classDeclaration_cc(mod, ident, gen, ext, impl, ctls) ::=
 
 
 #include "<ident>.decl.h"
+<ext>
 class <ident> <gen>: public CBase_<ident> <impl> {
     <ctls>
 };
@@ -82,46 +85,23 @@ class <ident> <gen>: public CBase_<ident> <impl> {
 
 classTopLevelScope(classScopeDeclarations) ::= 
 <<
-// _classTopLevelScope_
 <classScopeDeclarations; separator="\n">
-// _!classTopLevelScope_
 >>
 
 
-funcMethodDecl_h(modl, 
-            gtpl,
-            ty,
-            id,
-            fpl,
-            adl,
-            tc,
-            block) ::=
+funcMethodDecl_h(modl, gtpl, ty, id, fpl, adl, tc, block) ::=
 <<
 <modl>: <ty> <gtpl> <id><fpl> <adl> <tc>;
 >>
 
 
-funcMethodDecl_ci(modl, 
-            gtpl,
-            ty,
-            id,
-            fpl,
-            adl,
-            tc,
-            block) ::=
+funcMethodDecl_ci(modl, gtpl, ty, id, fpl, adl, tc, block) ::=
 <<
 entry <ty> <gtpl> <id><fpl> <adl> <tc>;
 >>
 
 
-funcMethodDecl_cc(modl, 
-            gtpl,
-            ty,
-            id,
-            fpl,
-            adl,
-            tc,
-            block) ::=
+funcMethodDecl_cc(modl, gtpl, ty, id, fpl, adl, tc, block) ::=
 <<
 <modl>: <ty> <gtpl> <id><fpl> <adl> <tc> <block>
 >>
@@ -163,3 +143,17 @@ ctorDecl_cc(modl, gtpl, id, fpl, tc, block) ::=
 >>
 
 
+block_cc(bsl) ::=
+<<
+{
+    <bsl; separator="\n">
+}
+>>
+
+
+embed_cc(str, blk) ::=
+<<
+// embedded block: <str>
+<blk>
+// end embedded block: <str>
+>>
index 6021ea8754c81564b62c4a4c0ac71968a5f20fb1..e2dd50c762ed7ac31ffa2f35449ed8abf9167279 100644 (file)
@@ -191,7 +191,7 @@ classTopLevelScope
     ;
     
 classScopeDeclarations
-//@init { boolean entry = false; }
+@init { boolean entry = false; }
     :   ^(CLASS_INSTANCE_INITIALIZER block)
         -> template(t={$text}) "/*cii*/ <t>"
     |   ^(CLASS_STATIC_INITIALIZER block)
@@ -199,15 +199,15 @@ classScopeDeclarations
     |   ^(FUNCTION_METHOD_DECL m=modifierList g=genericTypeParameterList? 
             ty=type IDENT f=formalParameterList a=arrayDeclaratorList? 
             tc=throwsClause? b=block?)
-//        { 
-//            // determine whether this is an entry method
-//            CharjAST modList = (CharjAST)$m.start;
-//            for (CharjAST mod : (List<CharjAST>)modList.getChildren()) {
-//                if (mod.token.getType() == ENTRY) {
-//                    entry = true;
-//                }
-//            }
-//        }
+        { 
+            // determine whether this is an entry method
+            CharjAST modList = (CharjAST)$m.start;
+            for (CharjAST mod : (List<CharjAST>)modList.getChildren()) {
+                if (mod.token.getType() == ENTRY) {
+                    entry = true;
+                }
+            }
+        }
         -> {emitCC()}? funcMethodDecl_cc(
                 modl={$m.st}, 
                 gtpl={$g.st}, 
@@ -226,18 +226,27 @@ classScopeDeclarations
                 adl={$a.st},
                 tc={$tc.st}, 
                 block={$b.st})
-//        -> {(emitCI() && entry)}? funcMethodDecl_ci(
-//                modl={$m.st}, 
-//                gtpl={$g.st}, 
-//                ty={$ty.text},
-//                id={$IDENT.text}, 
-//                fpl={$f.st}, 
-//                adl={$a.st},
-//                tc={$tc.st}, 
-//                block={$b.st})
+        -> {(emitCI() && entry)}? funcMethodDecl_ci(
+                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?)
+        { 
+            // determine whether this is an entry method
+            CharjAST modList = (CharjAST)$m.start;
+            for (CharjAST mod : (List<CharjAST>)modList.getChildren()) {
+                if (mod.token.getType() == ENTRY) {
+                    entry = true;
+                }
+            }
+        }
         -> {emitCC()}? voidMethodDecl_cc(
                 modl={$m.st}, 
                 gtpl={$g.st}, 
@@ -245,7 +254,7 @@ classScopeDeclarations
                 fpl={$f.st}, 
                 tc={$t.st}, 
                 block={$b.st})
-        -> {emitCI()}? voidMethodDecl_ci(
+        -> {emitCI() && entry}? voidMethodDecl_ci(
                 modl={$m.st}, 
                 gtpl={$g.st}, 
                 id={$IDENT.text}, 
@@ -266,6 +275,15 @@ classScopeDeclarations
         -> template(t={$text}) "vardecl <t>"
     |   ^(CONSTRUCTOR_DECL m=modifierList g=genericTypeParameterList? IDENT f=formalParameterList 
             t=throwsClause? b=block)
+        { 
+            // determine whether this is an entry method
+            CharjAST modList = (CharjAST)$m.start;
+            for (CharjAST mod : (List<CharjAST>)modList.getChildren()) {
+                if (mod.token.getType() == ENTRY) {
+                    entry = true;
+                }
+            }
+        }
         -> {emitCC()}? ctorDecl_cc(
                 modl={$m.st}, 
                 gtpl={$g.st}, 
@@ -273,7 +291,7 @@ classScopeDeclarations
                 fpl={$f.st}, 
                 tc={$t.st}, 
                 block={$b.st})
-        -> {emitCI()}? ctorDecl_ci(
+        -> {emitCI() && entry}? ctorDecl_ci(
                 modl={$m.st}, 
                 gtpl={$g.st}, 
                 id={$IDENT.text}, 
@@ -469,8 +487,12 @@ qualifiedIdentifier
     ;
     
 block
-    :   ^(BLOCK_SCOPE blockStatement*)
-        -> template(t={$text}) "<t>"
+@init { boolean emptyBlock = true; }
+    :   ^(BLOCK_SCOPE (b+=blockStatement)*)
+        { emptyBlock = ($b == null || $b.size() == 0); }
+        -> {emitCC() && emptyBlock}? template(bsl={$b}) "{ }"
+        -> {emitCC()}? block_cc(bsl={$b})
+        ->
     ;
     
 blockStatement
@@ -479,7 +501,7 @@ blockStatement
     |   typeDeclaration
         -> template(t={$text}) "<t>"
     |   statement
-        -> template(t={$text}) "<t>"
+        -> {$statement.st}
     ;
     
 localVariableDeclaration
@@ -521,6 +543,9 @@ statement
         -> template(t={$text}) "<t>"
     |   expression
         -> template(t={$text}) "<t>"
+    |   ^(EMBED STRING_LITERAL EMBED_BLOCK)
+        ->  embed_cc(str={$STRING_LITERAL.text}, blk={$EMBED_BLOCK.text})
+//template(t={$EMBED_BLOCK.text}) "<t>"
     |   SEMI // Empty statement.
         -> template(t={$text}) "<t>"
     ;
index 7769ed4ce1be6064c4f782bc12d8cb309444752a..3511dfe66fe01e4cbaf75812cb1014a20a46e8eb 100644 (file)
@@ -3,16 +3,25 @@ import charj.lang.someObject;
 
 public class Hello extends Chare {
     entry Hello() {}
+    public Hello(int x) {}
+    private Hello(int x, int y) {}
     entry void sayHello(int payload) {
+        int y = 1;
+        embed "c++" {{
+            int* x = &y;
+        }}
         //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;
-        }
+        //for (int i=0; i<25; ++i) {
+        //    myArray[i] = payload + i;
+        //    myGenericArray[i] = payload * i;
+        //}
     }
+    entry int SayHello() { }
+    public void anotherMethod() { }
     public int anotherMethod(int x) { return x; }
-//    private void aVoidMethod() {}
+    private void aVoidMethod() {}
+    private int aNonVoidMethod(int x) { return x; }
 }