diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-03-18 16:49:21 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-03-18 16:49:21 -0600 |
commit | 7a26f1108fb412daa4495628fc5136da2bb0ee34 (patch) | |
tree | 1cba07dec7b6b75103403bbb0ba2794527d18223 /minijava | |
parent | acb209e4bef36186744ee09c67fcad77550e78d2 (diff) |
Comments containing information on is done/needs to be done
Diffstat (limited to 'minijava')
-rw-r--r-- | minijava/SymTableVis.java | 2 | ||||
-rw-r--r-- | minijava/TypeCheckSimp.java | 599 |
2 files changed, 330 insertions, 271 deletions
diff --git a/minijava/SymTableVis.java b/minijava/SymTableVis.java index e0854f0..65e511f 100644 --- a/minijava/SymTableVis.java +++ b/minijava/SymTableVis.java @@ -29,6 +29,8 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> { TypeEnum type = TypeEnum.ERROR; switch (n.f0.f0.which) { + case 0: + type = TypeEnum.int_array; break; case 2: type = TypeEnum.integer; break; default: diff --git a/minijava/TypeCheckSimp.java b/minijava/TypeCheckSimp.java index ab5d8c9..f9e71bd 100644 --- a/minijava/TypeCheckSimp.java +++ b/minijava/TypeCheckSimp.java @@ -13,29 +13,29 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type private int offset; private void printNode(Node n, HashMap<String,TypeEnum> argu, boolean enter, TypeEnum consensus) { - for (int i=0; i < this.offset; ++i) + for (int i=0; i < this.offset; ++i) System.out.print("."); - if (enter) + if (enter) System.out.print("Visiting "); else System.out.print("Leaving "); System.out.print(n.getClass().getSimpleName()); - if (!enter) { - if (consensus == TypeEnum.ERROR) - System.out.print(" did not type check."); - else - System.out.print(" found type " + consensus); - } - System.out.println(); + if (!enter) { + if (consensus == TypeEnum.ERROR) + System.out.print(" did not type check."); + else + System.out.print(" found type " + consensus); + } + System.out.println(); } // // Auto class visitors--probably don't need to be overridden. // public TypeInstance visit(NodeList n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = new TypeInstance(TypeEnum.CHECK); int _count=0; for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { @@ -45,17 +45,17 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type ret = node; _count++; } - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } public TypeInstance visit(NodeListOptional n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - - TypeInstance ret; + ++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); int _count=0; @@ -68,33 +68,33 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type } else ret = new TypeInstance(TypeEnum.CHECK); // FIXME used to read 'null' - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; - return ret; + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; + return ret; } // FIXME public TypeInstance visit(NodeOptional n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); + ++this.offset; + this.printNode(n, argu, true, null); + + TypeInstance ret; - TypeInstance ret; - if ( n.present() ) ret = n.node.accept(this,argu); else ret = new TypeInstance(TypeEnum.CHECK); - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; - return ret; + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; + return ret; } public TypeInstance visit(NodeSequence n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = new TypeInstance(TypeEnum.CHECK); int _count=0; for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { @@ -104,16 +104,16 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type _count++; } - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } public TypeInstance visit(NodeToken n, HashMap<String,TypeEnum> argu) { - // A fixed string token. '⌣' - ++this.offset; - // this.printNode(n, argu, false, null); - --this.offset; + // A fixed string token. '⌣' + ++this.offset; + // this.printNode(n, argu, false, null); + --this.offset; return null; } @@ -127,15 +127,15 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f2 -> <EOF> */ public TypeInstance visit(Goal n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++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); - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } @@ -160,9 +160,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f17 -> "}" */ public TypeInstance visit(MainClass n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); TypeInstance name = n.f1.accept(this, argu); n.f2.accept(this, argu); @@ -182,8 +182,8 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f16.accept(this, argu); n.f17.accept(this, argu); - this.printNode(n, argu, false, stmt.get_type()); - --this.offset; + this.printNode(n, argu, false, stmt.get_type()); + --this.offset; return stmt; } @@ -192,13 +192,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * | ClassExtendsDeclaration() */ public TypeInstance visit(TypeDeclaration n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = n.f0.accept(this, argu); - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } @@ -211,9 +211,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f5 -> "}" */ public TypeInstance visit(ClassDeclaration n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -221,7 +221,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f3.accept(this, argu); n.f4.accept(this, argu); n.f5.accept(this, argu); - --this.offset; + --this.offset; return _ret; } @@ -236,9 +236,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f7 -> "}" */ public TypeInstance visit(ClassExtendsDeclaration n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -248,7 +248,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f5.accept(this, argu); n.f6.accept(this, argu); n.f7.accept(this, argu); - --this.offset; + --this.offset; return _ret; } @@ -259,15 +259,15 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f2 -> ";" */ public TypeInstance visit(VarDeclaration n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++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); - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } @@ -288,9 +288,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f12 -> "}" */ public TypeInstance visit(MethodDeclaration n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); TypeInstance ret_type = n.f1.accept(this, argu); n.f2.accept(this, argu); @@ -304,13 +304,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance retur = n.f10.accept(this, argu); 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); - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + TypeInstance ret = (stmt.equal_type(ret_type) && + stmt.get_type() == TypeEnum.CHECK) ? + new TypeInstance(TypeEnum.CHECK) : + new TypeInstance(TypeEnum.ERROR); + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } @@ -319,19 +319,19 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> ( FormalParameterRest() )* */ public TypeInstance visit(FormalParameterList n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++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); - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + parar.get_type() == TypeEnum.CHECK) ? + new TypeInstance(TypeEnum.CHECK) : + new TypeInstance(TypeEnum.ERROR); + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } @@ -340,14 +340,14 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> Identifier() */ public TypeInstance visit(FormalParameter n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = n.f0.accept(this, argu); n.f1.accept(this, argu); - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } @@ -356,14 +356,14 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> FormalParameter() */ public TypeInstance visit(FormalParameterRest n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); TypeInstance ret = n.f1.accept(this, argu); - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } @@ -374,13 +374,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * | Identifier() */ public TypeInstance visit(Type n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - - TypeInstance ret = n.f0.accept(this, argu); - + ++this.offset; + this.printNode(n, argu, true, null); + + TypeInstance ret = n.f0.accept(this, argu); + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } @@ -390,16 +390,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f2 -> "]" */ public TypeInstance visit(ArrayType n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = new TypeInstance(TypeEnum.int_array); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } @@ -407,12 +404,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f0 -> "boolean" */ public TypeInstance visit(BooleanType n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = new TypeInstance(TypeEnum.bool); + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } @@ -420,13 +418,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f0 -> "int" */ public TypeInstance visit(IntegerType n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = new TypeInstance(TypeEnum.integer); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } @@ -439,13 +437,13 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * | PrintStatement() */ public TypeInstance visit(Statement n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = n.f0.accept(this, argu); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } @@ -455,28 +453,32 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f2 -> "}" */ public TypeInstance visit(Block n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); TypeInstance ret = n.f1.accept(this, argu); n.f2.accept(this, argu); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } + // FIXME FIXME FIXME + // Given we only have a few types, what is a subtype of what? /** + * [23]: Expression is a subtype of identifier, and e typechecks* + * * f0 -> Identifier() * f1 -> "=" * f2 -> Expression() * f3 -> ";" */ public TypeInstance visit(AssignmentStatement n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance lhs = n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance rhs = n.f2.accept(this, argu); @@ -484,25 +486,29 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type TypeInstance ret = (lhs.equal_type(rhs)) ? new TypeInstance(TypeEnum.CHECK) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } + // FIXME (this may be done) /** + * [24]: Identifier is an integer array, expressions are both integers + * * f0 -> Identifier() * f1 -> "[" - * f2 -> Expression() //int + * f2 -> Expression() * f3 -> "]" * f4 -> "=" - * f5 -> Expression() //int + * f5 -> Expression() * f6 -> ";" */ public TypeInstance visit(ArrayAssignmentStatement n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + + TypeInstance id = new TypeInstance(argu.get(n.f0.f0.tokenImage)); n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance index = n.f2.accept(this, argu); @@ -510,17 +516,20 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f4.accept(this, argu); TypeInstance value = n.f5.accept(this, argu); n.f6.accept(this, argu); - TypeInstance ret = (index.get_type() == TypeEnum.integer && + 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); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [25]: Expression is a bool, both statements type-check + * * f0 -> "if" * f1 -> "(" * f2 -> Expression() @@ -530,9 +539,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f6 -> Statement() */ public TypeInstance visit(IfStatement n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance expr = n.f2.accept(this, argu); @@ -545,14 +554,16 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type stmt1.get_type() == TypeEnum.CHECK) ? new TypeInstance(TypeEnum.CHECK) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [26]: Expression is a bool, statement type-checks + * * f0 -> "while" * f1 -> "(" * f2 -> Expression() @@ -560,9 +571,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f4 -> Statement() */ public TypeInstance visit(WhileStatement n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance expr = n.f2.accept(this, argu); @@ -572,13 +583,15 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type stmt.get_type() == TypeEnum.CHECK) ? new TypeInstance(TypeEnum.CHECK) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [27]: Expression is an integer + * * f0 -> "System.out.println" * f1 -> "(" * f2 -> Expression() @@ -586,9 +599,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f4 -> ";" */ public TypeInstance visit(PrintStatement n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance ret = n.f2.accept(this, argu); @@ -597,9 +610,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type ret = (ret.get_type() == TypeEnum.integer) ? new TypeInstance(TypeEnum.CHECK) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } @@ -615,25 +628,27 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * | PrimaryExpression() */ public TypeInstance visit(Expression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = n.f0.accept(this, argu); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** - * f0 -> PrimaryExpressionppp() + * DONE [28]: If expressions are both booleans, return is a boolean + * + * f0 -> PrimaryExpression() * f1 -> "&&" * f2 -> PrimaryExpression() */ public TypeInstance visit(AndExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance oper1 = n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance oper2 = n.f2.accept(this, argu); @@ -641,21 +656,23 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type oper2.get_type() == TypeEnum.bool) ? new TypeInstance(TypeEnum.bool) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [29]: If expressions are both integers, return is a boolean + * * f0 -> PrimaryExpression() * f1 -> "<" * f2 -> PrimaryExpression() */ public TypeInstance visit(CompareExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance oper1 = n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance oper2 = n.f2.accept(this, argu); @@ -663,21 +680,23 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type oper2.get_type() == TypeEnum.integer) ? new TypeInstance(TypeEnum.bool) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [30]: If expressions are both integers, return is an integer + * * f0 -> PrimaryExpression() * f1 -> "+" * f2 -> PrimaryExpression() */ public TypeInstance visit(PlusExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance oper1 = n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance oper2 = n.f2.accept(this, argu); @@ -685,21 +704,23 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type oper2.get_type() == TypeEnum.integer) ? new TypeInstance(TypeEnum.integer) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [31]: If expressions are both integers, return is an integer + * * f0 -> PrimaryExpression() * f1 -> "-" * f2 -> PrimaryExpression() */ public TypeInstance visit(MinusExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance oper1 = n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance oper2 = n.f2.accept(this, argu); @@ -707,21 +728,23 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type oper2.get_type() == TypeEnum.integer) ? new TypeInstance(TypeEnum.integer) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [32]: If expressions are both integers, return is an integer + * * f0 -> PrimaryExpression() * f1 -> "*" * f2 -> PrimaryExpression() */ public TypeInstance visit(TimesExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance oper1 = n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance oper2 = n.f2.accept(this, argu); @@ -729,22 +752,25 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type oper2.get_type() == TypeEnum.integer) ? new TypeInstance(TypeEnum.integer) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [33]: If expr1 is an integer array and expr2 is an int, + * return is an int + * * f0 -> PrimaryExpression() * f1 -> "[" * f2 -> PrimaryExpression() * f3 -> "]" */ public TypeInstance visit(ArrayLookup n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance array = n.f0.accept(this, argu); n.f1.accept(this, argu); TypeInstance index = n.f2.accept(this, argu); @@ -753,33 +779,38 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type index.get_type() == TypeEnum.integer) ? new TypeInstance(TypeEnum.integer) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [34]: If expr1 is an integer array, return is an int + * * f0 -> PrimaryExpression() * f1 -> "." * f2 -> "length" */ public TypeInstance visit(ArrayLength n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++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); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } + // FIXME FIXME FIXME /** + * [35]: + * * f0 -> PrimaryExpression() * f1 -> "." * f2 -> Identifier() @@ -788,9 +819,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f5 -> ")" */ public TypeInstance visit(MessageSend n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); @@ -798,7 +829,7 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f3.accept(this, argu); n.f4.accept(this, argu); n.f5.accept(this, argu); - --this.offset; + --this.offset; return _ret; } @@ -808,14 +839,19 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> ( ExpressionRest() )* */ public TypeInstance visit(ExpressionList n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - - TypeInstance _ret=null; + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance expr1 = n.f0.accept(this, argu); TypeInstance exprr = n.f1.accept(this, argu); - --this.offset; - return _ret; + TypeInstance ret = (expr1.get_type() == TypeEnum.CHECK && + exprr.get_type() == TypeEnum.CHECK) ? + new TypeInstance(TypeEnum.CHECK) : + new TypeInstance(TypeEnum.ERROR); + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; + return ret; } /** @@ -823,14 +859,14 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f1 -> Expression() */ public TypeInstance visit(ExpressionRest n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); TypeInstance ret = n.f1.accept(this, argu); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } @@ -846,83 +882,97 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * | BracketExpression() */ public TypeInstance visit(PrimaryExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + TypeInstance ret = n.f0.accept(this, argu); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * DONE [36]: return is an int + * + * * f0 -> <INTEGER_LITERAL> */ public TypeInstance visit(IntegerLiteral n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + this.printNode(n, argu, false, TypeEnum.integer); - --this.offset; + --this.offset; return new TypeInstance(TypeEnum.integer); } /** + * DONE [37]: return is a bool + * * f0 -> "true" */ public TypeInstance visit(TrueLiteral n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + this.printNode(n, argu, false, TypeEnum.bool); - --this.offset; + --this.offset; return new TypeInstance(TypeEnum.bool); } /** + * DONE [38]: return is a bool + * * f0 -> "false" */ public TypeInstance visit(FalseLiteral n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + this.printNode(n, argu, false, TypeEnum.bool); - --this.offset; + --this.offset; return new TypeInstance(TypeEnum.bool); } + // FIXME /** + * [39]: id is a symbol in the current domain, return is id's type + * * f0 -> <IDENTIFIER> */ public TypeInstance visit(Identifier n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); + ++this.offset; + this.printNode(n, argu, true, null); + + TypeInstance ret = new TypeInstance(argu.get(n.f0.tokenImage)); - TypeInstance ret = new TypeInstance(argu.get(n.f0.tokenImage)); - - this.printNode(n, argu, false, TypeEnum.CHECK); - --this.offset; - return new TypeInstance(TypeEnum.CHECK); + this.printNode(n, argu, false, ret.get_type()); + --this.offset; + return ret; } - //FIXME + //FIXME FIXME FIXME /** + * [40]: method exists? but where is the token? + * * f0 -> "this" */ public TypeInstance visit(ThisExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); - TypeInstance ret = new TypeInstance(TypeEnum.CHECK); - - this.printNode(n, argu, false, ret.get_type()); - --this.offset; + TypeInstance ret = new TypeInstance(TypeEnum.CHECK); + + this.printNode(n, argu, false, ret.get_type()); + --this.offset; return ret; } /** + * DONE [41]: if expression is an integer, return is an int array + * * f0 -> "new" * f1 -> "int" * f2 -> "[" @@ -930,9 +980,9 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type * f4 -> "]" */ public TypeInstance visit(ArrayAllocationExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); TypeInstance ret = n.f1.accept(this, argu); n.f2.accept(this, argu); @@ -940,63 +990,70 @@ public class TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,Type n.f4.accept(this, argu); ret = (ret.get_type() == TypeEnum.integer) ? new TypeInstance(TypeEnum.int_array) : new TypeInstance(TypeEnum.ERROR); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } + // FIXME FIXME FIXME /** + * [42]: + * * f0 -> "new" * f1 -> Identifier() * f2 -> "(" * f3 -> ")" */ public TypeInstance visit(AllocationExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); TypeInstance ret = n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } /** + * [43]: if expression is a boolean, return is a boolean + * * f0 -> "!" * f1 -> Expression() */ public TypeInstance visit(NotExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); TypeInstance ret = n.f1.accept(this, argu); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return (ret.get_type() == TypeEnum.bool) ? ret : new TypeInstance(TypeEnum.ERROR); } /** + * [44]: if e is a type, return is that same type + * * f0 -> "(" * f1 -> Expression() * f2 -> ")" */ public TypeInstance visit(BracketExpression n, HashMap<String,TypeEnum> argu) { - ++this.offset; - this.printNode(n, argu, true, null); - + ++this.offset; + this.printNode(n, argu, true, null); + n.f0.accept(this, argu); TypeInstance ret = n.f1.accept(this, argu); n.f2.accept(this, argu); - + this.printNode(n, argu, false, ret.get_type()); - --this.offset; + --this.offset; return ret; } |