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/SymTableVis.java | 66 ++++++++++++- minijava/TypeCheckSimp.java | 231 ++++++++++++++++++++++---------------------- minijava/TypeEnum.java | 2 +- minijava/TypeInstance.java | 16 ++- 4 files changed, 196 insertions(+), 119 deletions(-) (limited to 'minijava') 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 extends GJDepthFirst { - public HashMap symt = new HashMap<>(); + public HashMap symt = new HashMap<>(); private void print_filter(String message) { boolean debug = true; @@ -18,6 +18,45 @@ public class SymTableVis extends GJDepthFirst { System.out.println(message); } + /** + * 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() @@ -27,23 +66,42 @@ public class SymTableVis extends GJDepthFirst { 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> { +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); 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; + } + } -- cgit v1.2.3