Charj: add SDAG AST rewriting pass
authorAaron Becker <akbecker@gmail.com>
Sat, 18 Sep 2010 00:00:08 +0000 (19:00 -0500)
committerAaron Becker <akbecker@gmail.com>
Sat, 18 Sep 2010 00:00:08 +0000 (19:00 -0500)
src/langs/charj/build.xml
src/langs/charj/src/charj/translator/AtomicBlocks.g
src/langs/charj/src/charj/translator/Charj.g
src/langs/charj/src/charj/translator/CharjEmitter.g
src/langs/charj/src/charj/translator/SymbolDefiner.g
src/langs/charj/src/charj/translator/SymbolResolver.g
src/langs/charj/src/charj/translator/SymbolWithScope.java

index eedc42629c4b11f161f100d5e7154d0f2a18bdb2..5a29ed36d59d7507fc9415b0ca3c9e7ebf86bad8 100644 (file)
             grammar.path="${buildsrc}/charj/translator"/>
     </target>
 
-    <target name="compile" depends="CharjEmitter" description="compile">
+    <target name="compile"
+        depends="CharjASTModifier, SymbolDefiner, SymbolResolver, InitPUPCollector, CharjASTModifier2, CharjSemantics, AtomicBlocks, CharjEmitter"
+        description="compile">
         <javac debug="true" srcdir="${buildsrc}" destdir="${classes}"
                target="1.5" listfiles="Yes" deprecation="Yes">
                <classpath refid="${classpath}"/>
index b1d42c1e0d2838f1add31c07dd7620a4e6a0380b..102ba142ccd0e763fba8523c975c25ba6e5b22dc 100644 (file)
@@ -5,6 +5,7 @@ options {
     tokenVocab = Charj;
     ASTLabelType = CharjAST;
     filter = true;
+    output = AST;
 }
 
 @header {
@@ -14,82 +15,18 @@ import java.util.Iterator;
 
 @members {
     SymbolTable symtab;
-    Scope currentScope;
-    ClassSymbol currentClass = null;
 
-    public SymbolResolver(TreeNodeStream input, SymbolTable symtab) {
+    public AtomicBlocks(TreeNodeStream input, SymbolTable symtab) {
         this(input);
         this.symtab = symtab;
-        this.currentScope = symtab.getDefaultPkg();
     }
 }
 
 
 topdown
-    :   enterClass
-    |   enterMethod
-    |   varDeclaration
-    |   expression
-    |   assignment
-    ;
-
-bottomup
-    :   exitClass
-    ;
-
-enterMethod
-@init {
-boolean entry = false;
-}
-    :   ^((FUNCTION_METHOD_DECL | ENTRY_FUNCTION_DECL {entry = true;})
-            (^(MODIFIER_LIST .*))?
-            (^(GENERIC_TYPE_PARAM_LIST .*))? 
-            type IDENT .*)
-        {
-            $IDENT.def.type = $type.sym;
-            $IDENT.symbolType = $IDENT.def.type;
-        }
-    |   ^((CONSTRUCTOR_DECL | ENTRY_CONSTRUCTOR_DECL {entry = true;})
-            (^(MODIFIER_LIST .*))?
-            (^(GENERIC_TYPE_PARAM_LIST .*))? 
-            IDENT .*)
-        {
-            $IDENT.def.type = (ClassSymbol)$IDENT.def.scope;
-            $IDENT.symbolType = $IDENT.def.type;
-        }
+    :   block
     ;
 
-enterClass
-    :   ^(TYPE classType IDENT
-            (^('extends' parent=type))?
-            (^('implements' type+))?
-            (^((FUNCTION_METHOD_DECL | ENTRY_FUNCTION_DECL | PRIMITIVE_VAR_DECLARATION |
-                OBJECT_VAR_DECLARATION | CONSTRUCTOR_DECL | ENTRY_CONSTRUCTOR_DECL) .*))*)
-        {
-            currentClass = (ClassSymbol)$IDENT.def.type;
-        }
-    ;
-
-exitClass
-    :   ^(TYPE classType IDENT
-            (^('extends' parent=type))?
-            (^('implements' type+))?
-            (^((FUNCTION_METHOD_DECL | ENTRY_FUNCTION_DECL | PRIMITIVE_VAR_DECLARATION |
-                OBJECT_VAR_DECLARATION | CONSTRUCTOR_DECL | ENTRY_CONSTRUCTOR_DECL) .*))*)
-        { currentClass = null; }
-    ;
-
-varDeclaration
-    :   ^((PRIMITIVE_VAR_DECLARATION | OBJECT_VAR_DECLARATION)
-            (^(MODIFIER_LIST .*))? type
-            ^(VAR_DECLARATOR_LIST (^(VAR_DECLARATOR ^(IDENT .*) .*)
-            )+))
-    |   ^(FORMAL_PARAM_STD_DECL (^(MODIFIER_LIST .*))? type ^(IDENT .*))
-        {
-            $IDENT.def.type = $type.sym;
-            $IDENT.symbolType = $type.sym;
-        }
-    ;
 
 parenthesizedExpression
     :   ^(PAREN_EXPR exp=expression)
@@ -100,31 +37,17 @@ expression
     ;
 
 assignment
-    :   ^(ASSIGNMENT IDENT expr)
+    :   ^(ASSIGNMENT IDENT expression)
     ;
 
 
 type
-    :   VOID {
-            scope = $VOID.scope;
-            typeText.add(new TypeName("void"));
-        }
-    |   ^(SIMPLE_TYPE t=. {
-            scope = $SIMPLE_TYPE.scope;
-            typeText.add(new TypeName($t.getText()));
-        } .*)
-    |   ^(OBJECT_TYPE { scope = $OBJECT_TYPE.scope; }
-            ^(QUALIFIED_TYPE_IDENT (^(IDENT (^(TEMPLATE_INST
-                (t1=type {tparams.add($t1.sym);} | lit1=literalVal {tparams.add($lit1.type);} )*))?
-                {typeText.add(new TypeName($IDENT.text, tparams));}))+) .*)
-    |   ^(REFERENCE_TYPE { scope = $REFERENCE_TYPE.scope; }
-            ^(QUALIFIED_TYPE_IDENT (^(IDENT  {typeText.add(new TypeName($IDENT.text));} .*))+) .*)
-    |   ^(PROXY_TYPE { scope = $PROXY_TYPE.scope; proxy = true; }
-            ^(QUALIFIED_TYPE_IDENT (^(IDENT {typeText.add(new TypeName($IDENT.text));} .*))+) .*)
-    |   ^(POINTER_TYPE { scope = $POINTER_TYPE.scope; pointer = true; }
-            ^(QUALIFIED_TYPE_IDENT (^(IDENT (^(TEMPLATE_INST
-            (t1=type {tparams.add($t1.sym);} | lit1=literalVal {tparams.add($lit1.type);} )*))?
-            {typeText.add(new TypeName($IDENT.text, tparams));}))+) .*)
+    :   VOID 
+    |   ^(SIMPLE_TYPE .*)
+    |   ^(OBJECT_TYPE .*)
+    |   ^(REFERENCE_TYPE .*)
+    |   ^(PROXY_TYPE .*)
+    |   ^(POINTER_TYPE .*)
     ;
 
 classType
@@ -136,12 +59,14 @@ classType
     |   ^(CHARE_ARRAY ARRAY_DIMENSION)
     ;
 
+
 block returns [boolean containsSDAG]
 @init { boolean hasSDAGstatement = false; }
     :   ^(BLOCK (sdagNonLeader |
             sdagLeader {hasSDAGstatement = $sdagLeader.containsSDAG; } |
-            block {hasSDAGstatement |= $block.containsSDAG})*) {
+            b=block {hasSDAGstatement |= $b.containsSDAG;})*) {
             $containsSDAG = hasSDAGstatement;
+            ((LocalScope)$BLOCK.def).hasSDAG = $containsSDAG;
         }
     ;
     
@@ -152,29 +77,27 @@ statement
     ;
 
 sdagLeader returns [boolean containsSDAG]
+@init { $containsSDAG = false; }
     :   ^(OVERLAP block) {$containsSDAG = true;}
-    |   ^(WHEN (IDENT expression? formalParameterList)* block) {$containsSDAG = true;}
-    |   ^(IF parenthesizedExpression ifblock=block elseblock=block?) {
-            $containsSDAG = $ifblock.containsSDAG || $elseblock.containsSDAG;
-        }
-    |   ^(FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block) {
-            $containsSDAG = $block.containsSDAG;
+    |   ^(WHEN (IDENT expression? ^(FORMAL_PARAM_LIST .*))* block) {$containsSDAG = true;}
+    |   ^(IF parenthesizedExpression ifblock=block elseblock=block?
+            {$ifblock.containsSDAG || $elseblock.containsSDAG}?)
+        {
+            $containsSDAG = true;
         }
-        -> {$containsSDAG}? ^(SDAG_FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block)
-        -> ^(FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block)
-    |   ^(FOR_EACH localModifierList? type IDENT expression block) {
-            $containsSDAG = $block.containsSDAG;
+        -> ^(SDAG_IF parenthesizedExpression $ifblock $elseblock)
+    |   ^(FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block {$block.containsSDAG}?) {
+            $containsSDAG = true;
         }
-    |   ^(WHILE parenthesizedExpression block) {
-            $containsSDAG = $block.containsSDAG;
+        -> ^(SDAG_FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block)
+    |   ^(WHILE parenthesizedExpression block {$block.containsSDAG}?) {
+            $containsSDAG = true;
         }
-        -> {$containsSDAG}? ^(SDAG_WHILE parenthesizedExpression block)
-        -> ^(WHILE parenthesizedExpression block)
-    |   ^(DO block parenthesizedExpression) {
-            $containsSDAG = $block.containsSDAG;
+        -> ^(SDAG_WHILE parenthesizedExpression block)
+    |   ^(DO block parenthesizedExpression {$block.containsSDAG}?) {
+            $containsSDAG = true;
         }
-        -> {$containsSDAG}? ^(SDAG_DO block parenthesizedExpression)
-        -> ^(DO block parenthesizedExpression)
+        -> ^(SDAG_DO block parenthesizedExpression)
     ;
 
 sdagNonLeader
@@ -184,16 +107,8 @@ sdagNonLeader
     |   ^(SWITCH parenthesizedExpression switchCaseLabel*)
     |   ^(RETURN expression?)
     |   ^(THROW expression)
-    |   ^(BREAK IDENT?) {
-            if ($IDENT != null) {
-                translator.error(this, "Labeled break not supported yet, ignoring.", $IDENT);
-            }
-        }
-    |   ^(CONTINUE IDENT?) {
-            if ($IDENT != null) {
-                translator.error(this, "Labeled continue not supported yet, ignoring.", $IDENT);
-            }
-        }
+    |   ^(BREAK IDENT?)
+    |   ^(CONTINUE IDENT?)
     |   ^(LABELED_STATEMENT IDENT statement)
     |   expression
     |   ^('delete' expression)
@@ -206,12 +121,13 @@ sdagNonLeader
     ;
         
 switchCaseLabel
-    :   ^(CASE expression blockStatement*)
-    |   ^(DEFAULT blockStatement*)
+    :   ^(CASE expression statement*)
+    |   ^(DEFAULT statement*)
     ;
     
 forInit
-    :   localVariableDeclaration 
+    :   ^(PRIMITIVE_VAR_DECLARATION .*)
+    |   ^(OBJECT_VAR_DECLARATION .*)
     |   expression+
     ;
 
index 130f7e022ade6a1837bc8cda1f39324d4eca730f..217f73044e0c8f979f62654821b31ee5ade8d6fc 100644 (file)
@@ -208,7 +208,9 @@ tokens {
     PRE_INC;
     QUALIFIED_TYPE_IDENT;
     RANGE_EXPRESSION;
+    SDAG_IF;
     SDAG_DO;
+    SDAG_FOR;
     SDAG_WHILE;
     STATIC_ARRAY_CREATOR;
     SUPER_CONSTRUCTOR_CALL;
index f4a70bda92d980192eddc44743273bc0fe52a6d3..0f8562f059fff402e316f9ed8f9946040474a17c 100644 (file)
@@ -716,6 +716,10 @@ sdagStatement
         -> template(b={$block.st}) "} overlap <b> atomic {"
     |   ^(WHEN (wa+=whenArgument)* block)
         -> template(w={wa}, b={$block.st}) "} when <w> <b> atomic {"
+    |   ^(SDAG_IF parenthesizedExpression ifblock=block elseblock=block?)
+    |   ^(SDAG_FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block)
+    |   ^(SDAG_WHILE parenthesizedExpression block)
+    |   ^(SDAG_DO block parenthesizedExpression)
     ;
 
 whenArgument
index f81b72eb8e9f131895aa98166d5a511615df1985..996aa75b54019b831bf182bf7fe11177ebfa672a 100644 (file)
@@ -61,7 +61,9 @@ enterPackage
 
 enterBlock
     :   BLOCK {
-            currentScope = new LocalScope(symtab, currentScope);
+            $BLOCK.scope = new LocalScope(symtab, currentScope);
+            $BLOCK.def = (LocalScope)$BLOCK.scope;
+            currentScope = $BLOCK.scope;
         }
     ;
 
@@ -166,6 +168,7 @@ enterClass
             (^((FUNCTION_METHOD_DECL | ENTRY_FUNCTION_DECL | DIVCON_METHOD_DECL | PRIMITIVE_VAR_DECLARATION |
                 OBJECT_VAR_DECLARATION | CONSTRUCTOR_DECL | ENTRY_CONSTRUCTOR_DECL) .*))*)
         {
+            //System.out.println("Defined class " + $IDENT.text);
             ClassSymbol sym = new ClassSymbol(symtab, $IDENT.text,
                     (ClassSymbol)currentScope.
                       resolveType(TypeName.createTypeName($parent.text)),
@@ -216,8 +219,8 @@ varDeclaration
             ^(VAR_DECLARATOR_LIST (^(VAR_DECLARATOR ^(IDENT .*) .*)
             {
                 Type varType = currentScope.resolveType($type.typeName);
-                /*System.out.println("Defining var " + $IDENT.text + " with type " +
-                    varType + " typename " + $type.typeName);*/
+                //System.out.println("Defining var " + $IDENT.text + " with type " +
+                //    varType + " typename " + $type.typeName);
                 VariableSymbol sym = new VariableSymbol(symtab, $IDENT.text, varType);
                 sym.definition = $IDENT;
                 sym.definitionTokenStream = input.getTokenStream();
@@ -234,7 +237,7 @@ varDeclaration
     |   ^(FORMAL_PARAM_STD_DECL (^(MODIFIER_LIST .*))? type ^(IDENT .*))
         {
             Type varType = currentScope.resolveType($type.typeName);
-            /*System.out.println("Defining argument var " + $IDENT.text + " with type " + varType);*/
+            //System.out.println("Defining argument var " + $IDENT.text + " with type " + varType);
             VariableSymbol sym = new VariableSymbol(symtab, $IDENT.text,
                     currentScope.resolveType($type.typeName));
             sym.definition = $IDENT;
index 1ba4a8b9f33b16cd8c3643fb77bb426abc000816..5ec06a6038719c710bc585adfc5f54857a8f67cf 100644 (file)
@@ -231,7 +231,7 @@ primaryExpression returns [Type type]
                 s = null;
                 /*System.out.println("No expression context: " + $e.text);*/
             } else {
-                /*System.out.println("Expression context is: " + cxt + " for symbol named " + memberText);*/
+                //System.out.println("Expression context is: " + cxt + " for symbol named " + memberText);
                 if (memberText.equals("this")) s = cxt;
                 else if (memberText.equals("super")) s = cxt.superClass;
                 else s = cxt.resolve(memberText);
index c2bc4ec2238b7d44e7ae98622c13e4efcd1d4bce..3de2351afe4c46b71f020de03403fd1862d1cda0 100644 (file)
@@ -8,6 +8,8 @@ public abstract class SymbolWithScope
     extends Symbol 
     implements Scope {
 
+    public boolean hasSDAG = false;
+
     public SymbolWithScope(SymbolTable symtab) {
         super(symtab);
     }