diff options
Diffstat (limited to 'minijava')
-rw-r--r-- | minijava/SymTableVis.java | 66 | ||||
-rw-r--r-- | minijava/TypeCheckSimp.java | 231 | ||||
-rw-r--r-- | minijava/TypeEnum.java | 2 | ||||
-rw-r--r-- | minijava/TypeInstance.java | 16 |
4 files changed, 196 insertions, 119 deletions
diff --git a/minijava/SymTableVis.java b/minijava/SymTableVis.java index 65e511f..6f03c45 100644 --- a/minijava/SymTableVis.java +++ b/minijava/SymTableVis.java @@ -10,7 +10,7 @@ import java.util.*; */ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { - public HashMap<String,TypeEnum> symt = new HashMap<>(); + public HashMap<String,TypeInstance> symt = new HashMap<>(); private void print_filter(String message) { boolean debug = true; @@ -19,6 +19,45 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { } /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> "public" + * f4 -> "static" + * f5 -> "void" + * f6 -> "main" + * f7 -> "(" + * f8 -> "String" + * f9 -> "[" + * f10 -> "]" + * f11 -> Identifier() + * f12 -> ")" + * f13 -> "{" + * f14 -> ( VarDeclaration() )* + * f15 -> ( Statement() )* + * f16 -> "}" + * f17 -> "}" + */ + public R visit(MainClass n, A argu) { + + n.f1.accept(this, argu); + n.f11.accept(this, argu); + n.f14.accept(this, argu); + n.f15.accept(this, argu); + + this.print_filter("Processing main"); + + String id = n.f1.f0.tokenImage; + + TypeInstance type = new TypeInstance(id, TypeEnum.classname); + this.print_filter("Inserting " + id + " => " + type); + symt.put(id, type); + + return null; + + } + + /** * f0 -> Type() * f1 -> Identifier() * f2 -> ";" @@ -27,23 +66,42 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { this.print_filter("Processing declaration"); - TypeEnum type = TypeEnum.ERROR; + String id = n.f1.f0.tokenImage; + TypeInstance type = new TypeInstance("ERROR", TypeEnum.ERROR); switch (n.f0.f0.which) { case 0: - type = TypeEnum.int_array; break; + type = new TypeInstance("int_array", TypeEnum.int_array); break; + case 1: + type = new TypeInstance("bool", TypeEnum.bool); break; case 2: - type = TypeEnum.integer; break; + type = new TypeInstance("int", TypeEnum.integer); break; + case 3: + type = new TypeInstance(id, TypeEnum.classname); break; default: this.print_filter("Unsupported case"); } + this.print_filter("Inserting " + id + " => " + type); + // Safe? + symt.put(id, type); + + return null; + } + + public R visit(ClassDeclaration n, A argu) { + + this.print_filter("Processing class"); + String id = n.f1.f0.tokenImage; + TypeInstance type = new TypeInstance(id, TypeEnum.classname); this.print_filter("Inserting " + id + " => " + type); // Safe? symt.put(id, type); return null; + } + } diff --git a/minijava/TypeCheckSimp.java b/minijava/TypeCheckSimp.java index f9e71bd..4ad0639 100644 --- a/minijava/TypeCheckSimp.java +++ b/minijava/TypeCheckSimp.java @@ -8,11 +8,11 @@ import java.util.*; * Provides default methods which visit each node in the tree in depth-first * order. Your visitors may extend this class. */ -public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,TypeEnum>> { +public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,TypeInstance>> { private int offset; - private void printNode(Node n, HashMap<String,TypeEnum> argu, boolean enter, TypeEnum consensus) { + private void printNode(Node n, HashMap<String,TypeInstance> argu, boolean enter, TypeEnum consensus) { for (int i=0; i < this.offset; ++i) System.out.print("."); if (enter) @@ -32,11 +32,11 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type // // Auto class visitors--probably don't need to be overridden. // - public TypeInstance visit(NodeList n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(NodeList n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); - TypeInstance ret = new TypeInstance(TypeEnum.CHECK); + TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK); int _count=0; for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { TypeInstance node = e.nextElement().accept(this,argu); @@ -51,13 +51,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type return ret; } - public TypeInstance visit(NodeListOptional n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(NodeListOptional n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); TypeInstance ret; - if ( n.present() ) { // FIXME "present" seems to check if any nodes exist at all! - ret = new TypeInstance(TypeEnum.CHECK); + if ( n.present() ) { + ret = new TypeInstance(null, TypeEnum.CHECK); int _count=0; for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { TypeInstance node = e.nextElement().accept(this,argu); @@ -67,7 +67,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type } } else - ret = new TypeInstance(TypeEnum.CHECK); // FIXME used to read 'null' + ret = new TypeInstance(null, TypeEnum.CHECK); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -75,7 +75,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type } // FIXME - public TypeInstance visit(NodeOptional n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(NodeOptional n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -84,18 +84,18 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type if ( n.present() ) ret = n.node.accept(this,argu); else - ret = new TypeInstance(TypeEnum.CHECK); + ret = new TypeInstance(null, TypeEnum.CHECK); this.printNode(n, argu, false, ret.get_type()); --this.offset; return ret; } - public TypeInstance visit(NodeSequence n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(NodeSequence n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); - TypeInstance ret = new TypeInstance(TypeEnum.CHECK); + TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK); int _count=0; for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { TypeInstance node = e.nextElement().accept(this,argu); @@ -109,7 +109,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type return ret; } - public TypeInstance visit(NodeToken n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(NodeToken n, HashMap<String,TypeInstance> argu) { // A fixed string token. '⌣' ++this.offset; // this.printNode(n, argu, false, null); @@ -126,7 +126,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> ( TypeDeclaration() )* * f2 -> <EOF> */ - public TypeInstance visit(Goal n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(Goal n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -159,7 +159,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f16 -> "}" * f17 -> "}" */ - public TypeInstance visit(MainClass n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(MainClass n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -174,7 +174,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f8.accept(this, argu); n.f9.accept(this, argu); n.f10.accept(this, argu); - TypeInstance args = n.f11.accept(this, argu); + // TypeInstance args = n.f11.accept(this, argu); n.f12.accept(this, argu); n.f13.accept(this, argu); TypeInstance var_dec = n.f14.accept(this, argu); @@ -191,7 +191,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f0 -> ClassDeclaration() * | ClassExtendsDeclaration() */ - public TypeInstance visit(TypeDeclaration n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(TypeDeclaration n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -210,19 +210,25 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ - public TypeInstance visit(ClassDeclaration n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ClassDeclaration n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); - TypeInstance _ret=null; n.f0.accept(this, argu); - n.f1.accept(this, argu); + TypeInstance id = n.f1.accept(this, argu); n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); + TypeInstance vars = n.f3.accept(this, argu); + TypeInstance mehs = n.f4.accept(this, argu); n.f5.accept(this, argu); + TypeInstance ret = (id.get_type() == TypeEnum.classname && + vars.has_checked() && + mehs.has_checked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, argu, false, ret.get_type()); --this.offset; - return _ret; + return ret; } /** @@ -235,7 +241,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ - public TypeInstance visit(ClassExtendsDeclaration n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ClassExtendsDeclaration n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -258,7 +264,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> Identifier() * f2 -> ";" */ - public TypeInstance visit(VarDeclaration n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(VarDeclaration n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -287,7 +293,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f11 -> ";" * f12 -> "}" */ - public TypeInstance visit(MethodDeclaration n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(MethodDeclaration n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -305,9 +311,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f11.accept(this, argu); n.f12.accept(this, argu); TypeInstance ret = (stmt.equal_type(ret_type) && - stmt.get_type() == TypeEnum.CHECK) ? - new TypeInstance(TypeEnum.CHECK) : - new TypeInstance(TypeEnum.ERROR); + stmt.has_checked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -318,17 +324,17 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f0 -> FormalParameter() * f1 -> ( FormalParameterRest() )* */ - public TypeInstance visit(FormalParameterList n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(FormalParameterList n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); TypeInstance _ret=null; TypeInstance para1 = n.f0.accept(this, argu); TypeInstance parar = n.f1.accept(this, argu); - TypeInstance ret = (para1.get_type() == TypeEnum.CHECK && - parar.get_type() == TypeEnum.CHECK) ? - new TypeInstance(TypeEnum.CHECK) : - new TypeInstance(TypeEnum.ERROR); + TypeInstance ret = (para1.has_checked() && + parar.has_checked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -339,7 +345,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f0 -> Type() * f1 -> Identifier() */ - public TypeInstance visit(FormalParameter n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(FormalParameter n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -355,7 +361,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f0 -> "," * f1 -> FormalParameter() */ - public TypeInstance visit(FormalParameterRest n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(FormalParameterRest n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -373,7 +379,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * | IntegerType() * | Identifier() */ - public TypeInstance visit(Type n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(Type n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -389,11 +395,11 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> "[" * f2 -> "]" */ - public TypeInstance visit(ArrayType n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ArrayType n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); - TypeInstance ret = new TypeInstance(TypeEnum.int_array); + TypeInstance ret = new TypeInstance(null, TypeEnum.int_array); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -403,11 +409,11 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type /** * f0 -> "boolean" */ - public TypeInstance visit(BooleanType n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(BooleanType n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); - TypeInstance ret = new TypeInstance(TypeEnum.bool); + TypeInstance ret = new TypeInstance(null, TypeEnum.bool); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -417,11 +423,11 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type /** * f0 -> "int" */ - public TypeInstance visit(IntegerType n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(IntegerType n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); - TypeInstance ret = new TypeInstance(TypeEnum.integer); + TypeInstance ret = new TypeInstance(null, TypeEnum.integer); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -436,7 +442,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * | WhileStatement() * | PrintStatement() */ - public TypeInstance visit(Statement n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(Statement n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -452,7 +458,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> ( Statement() )* * f2 -> "}" */ - public TypeInstance visit(Block n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(Block n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -475,7 +481,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f2 -> Expression() * f3 -> ";" */ - public TypeInstance visit(AssignmentStatement n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(AssignmentStatement n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -484,8 +490,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance rhs = n.f2.accept(this, argu); n.f3.accept(this, argu); TypeInstance ret = (lhs.equal_type(rhs)) ? - new TypeInstance(TypeEnum.CHECK) : - new TypeInstance(TypeEnum.ERROR); + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -504,12 +510,11 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f5 -> Expression() * f6 -> ";" */ - public TypeInstance visit(ArrayAssignmentStatement n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ArrayAssignmentStatement n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); - TypeInstance id = new TypeInstance(argu.get(n.f0.f0.tokenImage)); - n.f0.accept(this, argu); + TypeInstance id = n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance index = n.f2.accept(this, argu); n.f3.accept(this, argu); @@ -519,8 +524,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance ret = (id.get_type() == TypeEnum.int_array && index.get_type() == TypeEnum.integer && value.get_type() == TypeEnum.integer) ? - new TypeInstance(TypeEnum.CHECK) : - new TypeInstance(TypeEnum.ERROR); + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -538,7 +543,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f5 -> "else" * f6 -> Statement() */ - public TypeInstance visit(IfStatement n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(IfStatement n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -551,9 +556,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance stmt2 = n.f6.accept(this, argu); TypeInstance ret = (expr.get_type() == TypeEnum.bool && stmt1.get_type() == stmt2.get_type() && - stmt1.get_type() == TypeEnum.CHECK) ? - new TypeInstance(TypeEnum.CHECK) : - new TypeInstance(TypeEnum.ERROR); + stmt1.has_checked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -570,7 +575,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f3 -> ")" * f4 -> Statement() */ - public TypeInstance visit(WhileStatement n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(WhileStatement n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -580,9 +585,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f3.accept(this, argu); TypeInstance stmt = n.f4.accept(this, argu); TypeInstance ret = (expr.get_type() == TypeEnum.bool && - stmt.get_type() == TypeEnum.CHECK) ? - new TypeInstance(TypeEnum.CHECK) : - new TypeInstance(TypeEnum.ERROR); + stmt.has_checked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -598,7 +603,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f3 -> ")" * f4 -> ";" */ - public TypeInstance visit(PrintStatement n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(PrintStatement n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -608,8 +613,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f3.accept(this, argu); n.f4.accept(this, argu); ret = (ret.get_type() == TypeEnum.integer) ? - new TypeInstance(TypeEnum.CHECK) : - new TypeInstance(TypeEnum.ERROR); + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -627,7 +632,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * | MessageSend() * | PrimaryExpression() */ - public TypeInstance visit(Expression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(Expression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -645,7 +650,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> "&&" * f2 -> PrimaryExpression() */ - public TypeInstance visit(AndExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(AndExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -654,8 +659,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance oper2 = n.f2.accept(this, argu); TypeInstance ret = (oper1.get_type() == TypeEnum.bool && oper2.get_type() == TypeEnum.bool) ? - new TypeInstance(TypeEnum.bool) : - new TypeInstance(TypeEnum.ERROR); + new TypeInstance(null, TypeEnum.bool) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -669,7 +674,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> "<" * f2 -> PrimaryExpression() */ - public TypeInstance visit(CompareExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(CompareExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -678,8 +683,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance oper2 = n.f2.accept(this, argu); TypeInstance ret = (oper1.get_type() == TypeEnum.integer && oper2.get_type() == TypeEnum.integer) ? - new TypeInstance(TypeEnum.bool) : - new TypeInstance(TypeEnum.ERROR); + new TypeInstance(null, TypeEnum.bool) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -693,7 +698,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> "+" * f2 -> PrimaryExpression() */ - public TypeInstance visit(PlusExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(PlusExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -702,8 +707,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance oper2 = n.f2.accept(this, argu); TypeInstance ret = (oper1.get_type() == TypeEnum.integer && oper2.get_type() == TypeEnum.integer) ? - new TypeInstance(TypeEnum.integer) : - new TypeInstance(TypeEnum.ERROR); + new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -717,7 +722,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> "-" * f2 -> PrimaryExpression() */ - public TypeInstance visit(MinusExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(MinusExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -726,8 +731,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance oper2 = n.f2.accept(this, argu); TypeInstance ret = (oper1.get_type() == TypeEnum.integer && oper2.get_type() == TypeEnum.integer) ? - new TypeInstance(TypeEnum.integer) : - new TypeInstance(TypeEnum.ERROR); + new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -741,7 +746,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> "*" * f2 -> PrimaryExpression() */ - public TypeInstance visit(TimesExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(TimesExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -750,8 +755,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance oper2 = n.f2.accept(this, argu); TypeInstance ret = (oper1.get_type() == TypeEnum.integer && oper2.get_type() == TypeEnum.integer) ? - new TypeInstance(TypeEnum.integer) : - new TypeInstance(TypeEnum.ERROR); + new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -767,7 +772,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f2 -> PrimaryExpression() * f3 -> "]" */ - public TypeInstance visit(ArrayLookup n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ArrayLookup n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -777,8 +782,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f3.accept(this, argu); TypeInstance ret = (array.get_type() == TypeEnum.int_array && index.get_type() == TypeEnum.integer) ? - new TypeInstance(TypeEnum.integer) : - new TypeInstance(TypeEnum.ERROR); + new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -792,15 +797,15 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> "." * f2 -> "length" */ - public TypeInstance visit(ArrayLength n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ArrayLength n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); TypeInstance ret = n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); - ret = (ret.get_type() == TypeEnum.int_array) ? new TypeInstance(TypeEnum.integer) : - new TypeInstance(TypeEnum.ERROR); + ret = (ret.get_type() == TypeEnum.int_array) ? new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -818,7 +823,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f4 -> ( ExpressionList() )? * f5 -> ")" */ - public TypeInstance visit(MessageSend n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(MessageSend n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -838,16 +843,16 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f0 -> Expression() * f1 -> ( ExpressionRest() )* */ - public TypeInstance visit(ExpressionList n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ExpressionList n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); TypeInstance expr1 = n.f0.accept(this, argu); TypeInstance exprr = n.f1.accept(this, argu); - TypeInstance ret = (expr1.get_type() == TypeEnum.CHECK && - exprr.get_type() == TypeEnum.CHECK) ? - new TypeInstance(TypeEnum.CHECK) : - new TypeInstance(TypeEnum.ERROR); + TypeInstance ret = (expr1.has_checked() && + exprr.has_checked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -858,7 +863,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f0 -> "," * f1 -> Expression() */ - public TypeInstance visit(ExpressionRest n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ExpressionRest n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -881,7 +886,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * | NotExpression() * | BracketExpression() */ - public TypeInstance visit(PrimaryExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(PrimaryExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -898,13 +903,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * * f0 -> <INTEGER_LITERAL> */ - public TypeInstance visit(IntegerLiteral n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(IntegerLiteral n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); this.printNode(n, argu, false, TypeEnum.integer); --this.offset; - return new TypeInstance(TypeEnum.integer); + return new TypeInstance(null, TypeEnum.integer); } /** @@ -912,13 +917,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * * f0 -> "true" */ - public TypeInstance visit(TrueLiteral n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(TrueLiteral n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); this.printNode(n, argu, false, TypeEnum.bool); --this.offset; - return new TypeInstance(TypeEnum.bool); + return new TypeInstance(null, TypeEnum.bool); } /** @@ -926,13 +931,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * * f0 -> "false" */ - public TypeInstance visit(FalseLiteral n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(FalseLiteral n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); this.printNode(n, argu, false, TypeEnum.bool); --this.offset; - return new TypeInstance(TypeEnum.bool); + return new TypeInstance(null, TypeEnum.bool); } // FIXME @@ -941,29 +946,29 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * * f0 -> <IDENTIFIER> */ - public TypeInstance visit(Identifier n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(Identifier n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); - TypeInstance ret = new TypeInstance(argu.get(n.f0.tokenImage)); + TypeInstance ret = argu.get(n.f0.tokenImage); this.printNode(n, argu, false, ret.get_type()); --this.offset; return ret; } - //FIXME FIXME FIXME + // FIXME FIXME FIXME /** * [40]: method exists? but where is the token? * * f0 -> "this" */ - public TypeInstance visit(ThisExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ThisExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); n.f0.accept(this, argu); - TypeInstance ret = new TypeInstance(TypeEnum.CHECK); + TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -979,7 +984,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f3 -> Expression() * f4 -> "]" */ - public TypeInstance visit(ArrayAllocationExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(ArrayAllocationExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -988,15 +993,15 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f2.accept(this, argu); n.f3.accept(this, argu); n.f4.accept(this, argu); - ret = (ret.get_type() == TypeEnum.integer) ? new TypeInstance(TypeEnum.int_array) : - new TypeInstance(TypeEnum.ERROR); + ret = (ret.get_type() == TypeEnum.integer) ? new TypeInstance(null, TypeEnum.int_array) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; return ret; } - // FIXME FIXME FIXME + // FIXME /** * [42]: * @@ -1005,7 +1010,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f2 -> "(" * f3 -> ")" */ - public TypeInstance visit(AllocationExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(AllocationExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -1025,7 +1030,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f0 -> "!" * f1 -> Expression() */ - public TypeInstance visit(NotExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(NotExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -1034,7 +1039,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type this.printNode(n, argu, false, ret.get_type()); --this.offset; - return (ret.get_type() == TypeEnum.bool) ? ret : new TypeInstance(TypeEnum.ERROR); + return (ret.get_type() == TypeEnum.bool) ? ret : new TypeInstance(null, TypeEnum.ERROR); } /** @@ -1044,7 +1049,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> Expression() * f2 -> ")" */ - public TypeInstance visit(BracketExpression n, HashMap<String,TypeEnum> argu) { + public TypeInstance visit(BracketExpression n, HashMap<String,TypeInstance> argu) { ++this.offset; this.printNode(n, argu, true, null); diff --git a/minijava/TypeEnum.java b/minijava/TypeEnum.java index 4003eba..1f3cac3 100644 --- a/minijava/TypeEnum.java +++ b/minijava/TypeEnum.java @@ -1,5 +1,5 @@ package minijava; public enum TypeEnum { - int_array, bool, integer, CHECK, ERROR + classname, int_array, bool, integer, CHECK, ERROR } diff --git a/minijava/TypeInstance.java b/minijava/TypeInstance.java index 483a093..f7d6329 100644 --- a/minijava/TypeInstance.java +++ b/minijava/TypeInstance.java @@ -2,9 +2,15 @@ package minijava; public class TypeInstance { TypeEnum type; + String type_name; - public TypeInstance(TypeEnum type) { + public String toString() { + return "name:" + type_name + "|type:" + type; + } + + public TypeInstance(String type_name, TypeEnum type) { this.type = type; + this.type_name = type_name; } public boolean equal_type(TypeInstance other) { @@ -22,8 +28,16 @@ public class TypeInstance { this.type == other.type; } + public boolean has_checked() { + return type != TypeEnum.ERROR; + } + public TypeEnum get_type() { return this.type; } + public String get_type_name() { + return this.type_name; + } + } |