From 762acb336997d43e15d64ff591103614abe4806f Mon Sep 17 00:00:00 2001 From: bd-912 Date: Sat, 23 Mar 2024 21:09:05 -0600 Subject: Added minor classname functionality to ST --- minijava/TypeCheckSimp.java | 231 ++++++++++++++++++++++---------------------- 1 file changed, 118 insertions(+), 113 deletions(-) (limited to 'minijava/TypeCheckSimp.java') 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> { +public class TypeCheckSimp implements GJVisitor> { private int offset; - private void printNode(Node n, HashMap argu, boolean enter, TypeEnum consensus) { + private void printNode(Node n, HashMap 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 argu) { + public TypeInstance visit(NodeList n, HashMap 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 e = n.elements(); e.hasMoreElements(); ) { TypeInstance node = e.nextElement().accept(this,argu); @@ -51,13 +51,13 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(NodeListOptional n, HashMap 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 e = n.elements(); e.hasMoreElements(); ) { TypeInstance node = e.nextElement().accept(this,argu); @@ -67,7 +67,7 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(NodeOptional n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -84,18 +84,18 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(NodeSequence n, HashMap 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 e = n.elements(); e.hasMoreElements(); ) { TypeInstance node = e.nextElement().accept(this,argu); @@ -109,7 +109,7 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(NodeToken n, HashMap argu) { // A fixed string token. '⌣' ++this.offset; // this.printNode(n, argu, false, null); @@ -126,7 +126,7 @@ public class TypeCheckSimp implements GJVisitor ( TypeDeclaration() )* * f2 -> */ - public TypeInstance visit(Goal n, HashMap argu) { + public TypeInstance visit(Goal n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -159,7 +159,7 @@ public class TypeCheckSimp implements GJVisitor "}" * f17 -> "}" */ - public TypeInstance visit(MainClass n, HashMap argu) { + public TypeInstance visit(MainClass n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -174,7 +174,7 @@ public class TypeCheckSimp implements GJVisitor ClassDeclaration() * | ClassExtendsDeclaration() */ - public TypeInstance visit(TypeDeclaration n, HashMap argu) { + public TypeInstance visit(TypeDeclaration n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -210,19 +210,25 @@ public class TypeCheckSimp implements GJVisitor ( MethodDeclaration() )* * f5 -> "}" */ - public TypeInstance visit(ClassDeclaration n, HashMap argu) { + public TypeInstance visit(ClassDeclaration n, HashMap 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 ( MethodDeclaration() )* * f7 -> "}" */ - public TypeInstance visit(ClassExtendsDeclaration n, HashMap argu) { + public TypeInstance visit(ClassExtendsDeclaration n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -258,7 +264,7 @@ public class TypeCheckSimp implements GJVisitor Identifier() * f2 -> ";" */ - public TypeInstance visit(VarDeclaration n, HashMap argu) { + public TypeInstance visit(VarDeclaration n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -287,7 +293,7 @@ public class TypeCheckSimp implements GJVisitor ";" * f12 -> "}" */ - public TypeInstance visit(MethodDeclaration n, HashMap argu) { + public TypeInstance visit(MethodDeclaration n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -305,9 +311,9 @@ public class TypeCheckSimp implements GJVisitor FormalParameter() * f1 -> ( FormalParameterRest() )* */ - public TypeInstance visit(FormalParameterList n, HashMap argu) { + public TypeInstance visit(FormalParameterList n, HashMap 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 Type() * f1 -> Identifier() */ - public TypeInstance visit(FormalParameter n, HashMap argu) { + public TypeInstance visit(FormalParameter n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -355,7 +361,7 @@ public class TypeCheckSimp implements GJVisitor "," * f1 -> FormalParameter() */ - public TypeInstance visit(FormalParameterRest n, HashMap argu) { + public TypeInstance visit(FormalParameterRest n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -373,7 +379,7 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(Type n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -389,11 +395,11 @@ public class TypeCheckSimp implements GJVisitor "[" * f2 -> "]" */ - public TypeInstance visit(ArrayType n, HashMap argu) { + public TypeInstance visit(ArrayType n, HashMap 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 "boolean" */ - public TypeInstance visit(BooleanType n, HashMap argu) { + public TypeInstance visit(BooleanType n, HashMap 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 "int" */ - public TypeInstance visit(IntegerType n, HashMap argu) { + public TypeInstance visit(IntegerType n, HashMap 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 argu) { + public TypeInstance visit(Statement n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -452,7 +458,7 @@ public class TypeCheckSimp implements GJVisitor ( Statement() )* * f2 -> "}" */ - public TypeInstance visit(Block n, HashMap argu) { + public TypeInstance visit(Block n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -475,7 +481,7 @@ public class TypeCheckSimp implements GJVisitor Expression() * f3 -> ";" */ - public TypeInstance visit(AssignmentStatement n, HashMap argu) { + public TypeInstance visit(AssignmentStatement n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -484,8 +490,8 @@ public class TypeCheckSimp implements GJVisitor Expression() * f6 -> ";" */ - public TypeInstance visit(ArrayAssignmentStatement n, HashMap argu) { + public TypeInstance visit(ArrayAssignmentStatement n, HashMap 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 "else" * f6 -> Statement() */ - public TypeInstance visit(IfStatement n, HashMap argu) { + public TypeInstance visit(IfStatement n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -551,9 +556,9 @@ public class TypeCheckSimp implements GJVisitor ")" * f4 -> Statement() */ - public TypeInstance visit(WhileStatement n, HashMap argu) { + public TypeInstance visit(WhileStatement n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -580,9 +585,9 @@ public class TypeCheckSimp implements GJVisitor ")" * f4 -> ";" */ - public TypeInstance visit(PrintStatement n, HashMap argu) { + public TypeInstance visit(PrintStatement n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -608,8 +613,8 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(Expression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -645,7 +650,7 @@ public class TypeCheckSimp implements GJVisitor "&&" * f2 -> PrimaryExpression() */ - public TypeInstance visit(AndExpression n, HashMap argu) { + public TypeInstance visit(AndExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -654,8 +659,8 @@ public class TypeCheckSimp implements GJVisitor "<" * f2 -> PrimaryExpression() */ - public TypeInstance visit(CompareExpression n, HashMap argu) { + public TypeInstance visit(CompareExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -678,8 +683,8 @@ public class TypeCheckSimp implements GJVisitor "+" * f2 -> PrimaryExpression() */ - public TypeInstance visit(PlusExpression n, HashMap argu) { + public TypeInstance visit(PlusExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -702,8 +707,8 @@ public class TypeCheckSimp implements GJVisitor "-" * f2 -> PrimaryExpression() */ - public TypeInstance visit(MinusExpression n, HashMap argu) { + public TypeInstance visit(MinusExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -726,8 +731,8 @@ public class TypeCheckSimp implements GJVisitor "*" * f2 -> PrimaryExpression() */ - public TypeInstance visit(TimesExpression n, HashMap argu) { + public TypeInstance visit(TimesExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -750,8 +755,8 @@ public class TypeCheckSimp implements GJVisitor PrimaryExpression() * f3 -> "]" */ - public TypeInstance visit(ArrayLookup n, HashMap argu) { + public TypeInstance visit(ArrayLookup n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -777,8 +782,8 @@ public class TypeCheckSimp implements GJVisitor "." * f2 -> "length" */ - public TypeInstance visit(ArrayLength n, HashMap argu) { + public TypeInstance visit(ArrayLength n, HashMap 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 ( ExpressionList() )? * f5 -> ")" */ - public TypeInstance visit(MessageSend n, HashMap argu) { + public TypeInstance visit(MessageSend n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -838,16 +843,16 @@ public class TypeCheckSimp implements GJVisitor Expression() * f1 -> ( ExpressionRest() )* */ - public TypeInstance visit(ExpressionList n, HashMap argu) { + public TypeInstance visit(ExpressionList n, HashMap 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 "," * f1 -> Expression() */ - public TypeInstance visit(ExpressionRest n, HashMap argu) { + public TypeInstance visit(ExpressionRest n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -881,7 +886,7 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(PrimaryExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -898,13 +903,13 @@ public class TypeCheckSimp implements GJVisitor */ - public TypeInstance visit(IntegerLiteral n, HashMap argu) { + public TypeInstance visit(IntegerLiteral n, HashMap 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 "true" */ - public TypeInstance visit(TrueLiteral n, HashMap argu) { + public TypeInstance visit(TrueLiteral n, HashMap 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 "false" */ - public TypeInstance visit(FalseLiteral n, HashMap argu) { + public TypeInstance visit(FalseLiteral n, HashMap 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 */ - public TypeInstance visit(Identifier n, HashMap argu) { + public TypeInstance visit(Identifier n, HashMap 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 argu) { + public TypeInstance visit(ThisExpression n, HashMap 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 Expression() * f4 -> "]" */ - public TypeInstance visit(ArrayAllocationExpression n, HashMap argu) { + public TypeInstance visit(ArrayAllocationExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -988,15 +993,15 @@ public class TypeCheckSimp implements GJVisitor "(" * f3 -> ")" */ - public TypeInstance visit(AllocationExpression n, HashMap argu) { + public TypeInstance visit(AllocationExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -1025,7 +1030,7 @@ public class TypeCheckSimp implements GJVisitor "!" * f1 -> Expression() */ - public TypeInstance visit(NotExpression n, HashMap argu) { + public TypeInstance visit(NotExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -1034,7 +1039,7 @@ public class TypeCheckSimp implements GJVisitor Expression() * f2 -> ")" */ - public TypeInstance visit(BracketExpression n, HashMap argu) { + public TypeInstance visit(BracketExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); -- cgit v1.2.3