Charj: add automatic forward declarations for class variables
authorAaron Becker <akbecker@gmail.com>
Wed, 26 May 2010 07:15:51 +0000 (02:15 -0500)
committerAaron Becker <akbecker@gmail.com>
Wed, 26 May 2010 07:15:51 +0000 (02:15 -0500)
src/langs/charj/src/charj/translator/Charj.stg
src/langs/charj/src/charj/translator/CharjSemantics.g
src/langs/charj/src/charj/translator/ClassSymbol.java
src/langs/charj/src/charj/translator/SymbolTable.java
src/langs/charj/src/charj/translator/SymbolWithScope.java
src/langs/charj/src/charj/translator/Translator.java

index e30c561e9b0848b17ee1f0ab19887acf54964ef3..a810e3db533413966230ba970d40c6d0c1f6ad58 100644 (file)
@@ -58,7 +58,8 @@ interfaceExtends(ts) ::=
 classDeclaration_h(sym, ident, ext, csds) ::=
 <<
 <sym.IncludeString>
-<sym.MemberTypeNames:{name| class <name>; separator = "\n"}>
+<sym.MemberTypeNames:{name| class <name>;
+}>
 <classDeclaration_h_cont(pds = sym.packageNames, sym = sym, ident = ident, ext = ext, csds = csds)>
 >>
 
index 2ec7de47f4b2c9ddf1977a99ca7da9ae1e3cfc73..de5798eedd01162a658a55d8e642eb6300959c1c 100644 (file)
@@ -183,12 +183,12 @@ scope ScopeStack;
 field [ClassSymbol type]
     :   ^(VAR_DECLARATOR ^(IDENT arrayDeclaratorList?) variableInitializer?)
     {
-            //System.out.println("Found variable: " + $type + " " + $IDENT.text);
-            //VariableSymbol sym = new VariableSymbol(symtab, $IDENT.text, $type);
-            //sym.definition = $field.start;
-            //sym.definitionTokenStream = input.getTokenStream();
-            //$VAR_DECLARATOR.symbol = sym;
-            //currentClass.define($IDENT.text, sym);
+            System.out.println("Found variable: " + $type + " " + $IDENT.text);
+            VariableSymbol sym = new VariableSymbol(symtab, $IDENT.text, $type);
+            sym.definition = $field.start;
+            sym.definitionTokenStream = input.getTokenStream();
+            $VAR_DECLARATOR.symbol = sym;
+            currentClass.define($IDENT.text, sym);
     }
     ;
     
@@ -282,11 +282,8 @@ objectType returns [ClassSymbol type]
     |   ^(REFERENCE_TYPE qualifiedTypeIdent arrayDeclaratorList?)
     |   ^(POINTER_TYPE qualifiedTypeIdent arrayDeclaratorList?)
         {
-            /*
-            System.out.println("Resolving type: " + $qualifiedTypeIdent.name);
             $type = currentClass.resolveType($qualifiedTypeIdent.name);
             if ($type == null) $type = symtab.resolveBuiltinType($qualifiedTypeIdent.name);
-            */
         }
     ;
 
index a1e17906dbc483076f4c5f919fdb982cf18e9cb6..3e173312367557bdf8eec7f88b7c5f5fa3f1cd7d 100644 (file)
@@ -18,6 +18,7 @@ public class ClassSymbol extends SymbolWithScope implements Scope {
     public Map<String, MethodSymbol> methods = new LinkedHashMap<String, MethodSymbol>();
 
     public boolean hasCopyCtor = false;
+    public boolean isPrimitive = false;
 
     public ClassSymbol(
             SymbolTable symtab, 
@@ -241,7 +242,8 @@ public class ClassSymbol extends SymbolWithScope implements Scope {
         if (debug()) System.out.println("Found " + fields.size() + " fields...");
         List<String> names = new ArrayList<String>();
         for (ClassSymbol c : getMemberTypes()) {
-            names.add(c.getFullyQualifiedName());
+            if (c.isPrimitive) continue;
+            names.add(c.getName());
             if (debug()) System.out.println("Found type " + c.getFullyQualifiedName());
         }
         return names;
index c5f7726f904f9c47e31426ea6c2eb8835468a6cb..3812e3e6fbd5aeeb3e8febf11b3f611b04774016 100644 (file)
@@ -67,14 +67,11 @@ public class SymbolTable {
         primitiveTypes.put("char",   new ClassSymbol(this, "char",   null, lang)); 
         primitiveTypes.put("short",  new ClassSymbol(this, "short",  null, lang)); 
         primitiveTypes.put("bool",   new ClassSymbol(this, "bool",   null, lang)); 
-        lang.define("void", primitiveTypes.get("void"));
-        lang.define("int", primitiveTypes.get("int"));
-        lang.define("long", primitiveTypes.get("long"));
-        lang.define("float", primitiveTypes.get("float"));
-        lang.define("double", primitiveTypes.get("double"));
-        lang.define("char", primitiveTypes.get("char"));
-        lang.define("short", primitiveTypes.get("short"));
-        lang.define("bool", primitiveTypes.get("bool"));
+        for (Map.Entry<String, ClassSymbol> entry : primitiveTypes.entrySet()) {
+            ClassSymbol c = entry.getValue();
+            lang.define(entry.getKey(), c);
+            c.isPrimitive = true;
+        }
     }
 
     public ClassSymbol resolveBuiltinType(String type) {
index bf4a28aaa8ad47c99aa397f1e6803d63390799f2..1b11cba55d2dc3b1b2781b3c0b8b307f30e6e642 100644 (file)
@@ -158,7 +158,7 @@ public abstract class SymbolWithScope
             parent = getEnclosingScope().getFullyQualifiedName();
         }
         if ( parent!=null ) {
-            return parent + "." + name;
+            return parent + "::" + name;
         }
         return name;
     }
index 2542a26a6d8c4f3f4156b308aec835fad290ae3b..8c4398072a6aa81f6ac6f04bbc9ad1b708dfc3f4 100644 (file)
@@ -220,7 +220,7 @@ public class Translator {
             String fullName = packageDir + "/" + typeName + ".cj";
                
             ClassLoader cl = Thread.currentThread().getContextClassLoader();
-            boolean fileExists = (cl.getResource(fullName) == null);
+            boolean fileExists = (cl.getResource(fullName) != null);
             if (!fileExists) {
                 if (debug()) System.out.println(
                         " \tloadType(" + typeName + "): not found");