Charj: fix primitive type lookup for method symbol table insertion
authorAaron Becker <akbecker@gmail.com>
Mon, 24 May 2010 18:58:48 +0000 (13:58 -0500)
committerAaron Becker <akbecker@gmail.com>
Mon, 24 May 2010 18:58:48 +0000 (13:58 -0500)
src/langs/charj/src/charj/translator/CharjSemantics.g
src/langs/charj/src/charj/translator/ClassSymbol.java
src/langs/charj/src/charj/translator/SymbolTable.java

index a8f04a1f2ad69735769671aa0f6858a90259e870..af63402596a1a9f134fd235d602f1e77d95e56cf 100644 (file)
@@ -113,7 +113,7 @@ packageDeclaration
     
 importDeclarations returns [List<CharjAST> packageNames]
 @init {
-       packageNames = new ArrayList<CharjAST>();
+    packageNames = new ArrayList<CharjAST>();
 }
     :   (^('import' qualifiedIdentifier './*'?)
                { packageNames.add($qualifiedIdentifier.start); })*
@@ -123,18 +123,19 @@ importDeclarations returns [List<CharjAST> packageNames]
 typeDeclaration[List<CharjAST> imports] returns [ClassSymbol sym]
 scope ScopeStack; // top-level type scope
     :   ^(TYPE ('class' | chareType) IDENT
-            (^('extends' parent=type))? (^('implements' type+))? classScopeDeclaration*)
-        {
-            Scope outerScope = $ScopeStack[-1]::current;
-            $sym = new ClassSymbol(symtab, $IDENT.text, outerScope.resolveType($parent.text), outerScope);
-            outerScope.define($sym.name, $sym);
-            currentClass = $sym;
-            $sym.definition = $typeDeclaration.start;
-            $sym.definitionTokenStream = input.getTokenStream();
-            $IDENT.symbol = $sym;
-            $ScopeStack::current = $sym;
-            importPackages($sym, $imports);
-        }
+            (^('extends' parent=type))? (^('implements' type+))?
+            {
+                Scope outerScope = $ScopeStack[-1]::current;
+                $sym = new ClassSymbol(symtab, $IDENT.text, outerScope.resolveType($parent.text), outerScope);
+                outerScope.define($sym.name, $sym);
+                currentClass = $sym;
+                $sym.definition = $typeDeclaration.start;
+                $sym.definitionTokenStream = input.getTokenStream();
+                $IDENT.symbol = $sym;
+                $ScopeStack::current = $sym;
+                importPackages($sym, $imports);
+            }
+            classScopeDeclaration*)
     |   ^('interface' IDENT (^('extends' type+))?  interfaceScopeDeclaration*)
     |   ^('enum' IDENT (^('implements' type+))? enumConstant+ classScopeDeclaration*)
     ;
@@ -156,7 +157,6 @@ scope ScopeStack;
             ty=type IDENT f=formalParameterList a=arrayDeclaratorList? 
             b=block?)
         {
-            /*
             ClassSymbol returnType = currentClass.resolveType($ty.text);
             MethodSymbol sym = new MethodSymbol(symtab, $IDENT.text, currentClass, returnType);
             currentMethod = sym;
@@ -164,7 +164,6 @@ scope ScopeStack;
             sym.definitionTokenStream = input.getTokenStream();
             currentClass.members.put($IDENT.text, sym);
             $FUNCTION_METHOD_DECL.symbol = sym;
-            */
         }
     |   ^(PRIMITIVE_VAR_DECLARATION modifierList? simpleType variableDeclaratorList)
     |   ^(OBJECT_VAR_DECLARATION modifierList? objectType variableDeclaratorList)
index 227d3677349eb59298962d4aaaedbd5f11241a69..2930ee8c5906640fff87ee695f4c0becbe706280 100644 (file)
@@ -157,6 +157,8 @@ public class ClassSymbol extends SymbolWithScope implements Scope {
 
         // look for type in classes already defined in imported packages
         for (String packageName : imports.keySet()) {
+            if ( debug() ) System.out.println( "Looking for type " +
+                    type + " in package " + packageName);
             PackageScope pkg = resolvePackage(packageName);
             ClassSymbol cs = pkg.resolveType(type);
             if ( cs != null) { // stop looking, found it
index e557f8dcc64caffe4cc4ed0c48ed84ee925d87e8..c5f7726f904f9c47e31426ea6c2eb8835468a6cb 100644 (file)
@@ -67,6 +67,14 @@ 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"));
     }
 
     public ClassSymbol resolveBuiltinType(String type) {