From b01fe1e8e5541d6c11f905d7fbb949d747f29230 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Wed, 27 Mar 2024 22:53:08 -0600 Subject: SymbolTable to separate library, Class/Method Instances --- Typecheck.java | 10 +- misc/PPrinter.java | 737 +++++++++++++++++++++++++++++++++++ misc/PrintFilter.java | 15 + st/AbstractInstance.java | 35 ++ st/ClassInstance.java | 37 ++ st/MethodInstance.java | 24 ++ st/SymTableVis.java | 219 +++++++++++ st/TypeEnum.java | 5 + st/TypeInstance.java | 32 ++ typecheck/library/PPrinter.java | 737 ----------------------------------- typecheck/library/SymTableVis.java | 101 ----- typecheck/library/TypeCheckSimp.java | 194 ++++----- typecheck/library/TypeEnum.java | 5 - typecheck/library/TypeInstance.java | 43 -- typecheck/library/Utilities.java | 14 - typecheck/tests/RetrieveInteger.java | 2 +- vaporize/library/VaporizeSimp.java | 4 - 17 files changed, 1209 insertions(+), 1005 deletions(-) create mode 100644 misc/PPrinter.java create mode 100644 misc/PrintFilter.java create mode 100644 st/AbstractInstance.java create mode 100644 st/ClassInstance.java create mode 100644 st/MethodInstance.java create mode 100644 st/SymTableVis.java create mode 100644 st/TypeEnum.java create mode 100644 st/TypeInstance.java delete mode 100644 typecheck/library/PPrinter.java delete mode 100644 typecheck/library/SymTableVis.java delete mode 100644 typecheck/library/TypeEnum.java delete mode 100644 typecheck/library/TypeInstance.java delete mode 100644 typecheck/library/Utilities.java diff --git a/Typecheck.java b/Typecheck.java index 6d45598..d2639f5 100644 --- a/Typecheck.java +++ b/Typecheck.java @@ -1,10 +1,10 @@ -// Helper for HW2/CS453. import java.io.*; import visitor.*; import syntaxtree.*; import java.util.*; -// Files are stored in the typecheck directory/package. +import st.*; import typecheck.library.*; +import misc.*; public class Typecheck { public static void main(String[] args) { @@ -16,15 +16,15 @@ public class Typecheck { // GJDepthFirst. R=Void, A=String. PPrinter pp = new PPrinter(); root.accept(pp, ""); - Utilities.print_filter("===================================================", true); + PrintFilter.print("===================================================", true); // // Build the symbol table. Top-down visitor, inherits from // // GJDepthFirst. R=Void, A=Integer. SymTableVis pv = new SymTableVis(); root.accept(pv, 0); - HashMap symt = pv.symt; - Utilities.print_filter("===================================================", true); + HashMap symt = pv.symt; + PrintFilter.print("===================================================", true); // Do type checking. Bottom-up visitor, also inherits from // GJDepthFirst. Visit functions return MyTpe (=R), and diff --git a/misc/PPrinter.java b/misc/PPrinter.java new file mode 100644 index 0000000..eff2357 --- /dev/null +++ b/misc/PPrinter.java @@ -0,0 +1,737 @@ +package misc; + +import syntaxtree.*; +import visitor.*; +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 PPrinter extends GJDepthFirst { + + private int offset; + + private void printNode(Node n, A argu) { + for (int i=0; i < this.offset; ++i) + PrintFilter.print(".", false); + PrintFilter.print(n.getClass().getSimpleName(), true); + ++this.offset; + } + + // + // User-generated visitor methods below + // + + /** + * f0 -> MainClass() + * f1 -> ( TypeDeclaration() )* + * f2 -> + */ + public R visit(Goal n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * 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) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + n.f6.accept(this, argu); + n.f7.accept(this, argu); + n.f8.accept(this, argu); + n.f9.accept(this, argu); + n.f10.accept(this, argu); + n.f11.accept(this, argu); + n.f12.accept(this, argu); + n.f13.accept(this, argu); + n.f14.accept(this, argu); + n.f15.accept(this, argu); + n.f16.accept(this, argu); + n.f17.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> ClassDeclaration() + * | ClassExtendsDeclaration() + */ + public R visit(TypeDeclaration n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> ( VarDeclaration() )* + * f4 -> ( MethodDeclaration() )* + * f5 -> "}" + */ + public R visit(ClassDeclaration n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "extends" + * f3 -> Identifier() + * f4 -> "{" + * f5 -> ( VarDeclaration() )* + * f6 -> ( MethodDeclaration() )* + * f7 -> "}" + */ + public R visit(ClassExtendsDeclaration n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + n.f6.accept(this, argu); + n.f7.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + * f2 -> ";" + */ + public R visit(VarDeclaration n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "public" + * f1 -> Type() + * f2 -> Identifier() + * f3 -> "(" + * f4 -> ( FormalParameterList() )? + * f5 -> ")" + * f6 -> "{" + * f7 -> ( VarDeclaration() )* + * f8 -> ( Statement() )* + * f9 -> "return" + * f10 -> Expression() + * f11 -> ";" + * f12 -> "}" + */ + public R visit(MethodDeclaration n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + n.f6.accept(this, argu); + n.f7.accept(this, argu); + n.f8.accept(this, argu); + n.f9.accept(this, argu); + n.f10.accept(this, argu); + n.f11.accept(this, argu); + n.f12.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> FormalParameter() + * f1 -> ( FormalParameterRest() )* + */ + public R visit(FormalParameterList n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + */ + public R visit(FormalParameter n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "," + * f1 -> FormalParameter() + */ + public R visit(FormalParameterRest n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> ArrayType() + * | BooleanType() + * | IntegerType() + * | Identifier() + */ + public R visit(Type n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "int" + * f1 -> "[" + * f2 -> "]" + */ + public R visit(ArrayType n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "boolean" + */ + public R visit(BooleanType n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "int" + */ + public R visit(IntegerType n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> Block() + * | AssignmentStatement() + * | ArrayAssignmentStatement() + * | IfStatement() + * | WhileStatement() + * | PrintStatement() + */ + public R visit(Statement n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "{" + * f1 -> ( Statement() )* + * f2 -> "}" + */ + public R visit(Block n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> Identifier() + * f1 -> "=" + * f2 -> Expression() + * f3 -> ";" + */ + public R visit(AssignmentStatement n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> Identifier() + * f1 -> "[" + * f2 -> Expression() + * f3 -> "]" + * f4 -> "=" + * f5 -> Expression() + * f6 -> ";" + */ + public R visit(ArrayAssignmentStatement n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + n.f6.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "if" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> Statement() + * f5 -> "else" + * f6 -> Statement() + */ + public R visit(IfStatement n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + n.f6.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "while" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> Statement() + */ + public R visit(WhileStatement n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "System.out.println" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> ";" + */ + public R visit(PrintStatement n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> AndExpression() + * | CompareExpression() + * | PlusExpression() + * | MinusExpression() + * | TimesExpression() + * | ArrayLookup() + * | ArrayLength() + * | MessageSend() + * | PrimaryExpression() + */ + public R visit(Expression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "&&" + * f2 -> PrimaryExpression() + */ + public R visit(AndExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "<" + * f2 -> PrimaryExpression() + */ + public R visit(CompareExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "+" + * f2 -> PrimaryExpression() + */ + public R visit(PlusExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "-" + * f2 -> PrimaryExpression() + */ + public R visit(MinusExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "*" + * f2 -> PrimaryExpression() + */ + public R visit(TimesExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "[" + * f2 -> PrimaryExpression() + * f3 -> "]" + */ + public R visit(ArrayLookup n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "." + * f2 -> "length" + */ + public R visit(ArrayLength n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "." + * f2 -> Identifier() + * f3 -> "(" + * f4 -> ( ExpressionList() )? + * f5 -> ")" + */ + public R visit(MessageSend n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> Expression() + * f1 -> ( ExpressionRest() )* + */ + public R visit(ExpressionList n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "," + * f1 -> Expression() + */ + public R visit(ExpressionRest n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> IntegerLiteral() + * | TrueLiteral() + * | FalseLiteral() + * | Identifier() + * | ThisExpression() + * | ArrayAllocationExpression() + * | AllocationExpression() + * | NotExpression() + * | BracketExpression() + */ + public R visit(PrimaryExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> + */ + public R visit(IntegerLiteral n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "true" + */ + public R visit(TrueLiteral n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "false" + */ + public R visit(FalseLiteral n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> + */ + public R visit(Identifier n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "this" + */ + public R visit(ThisExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "new" + * f1 -> "int" + * f2 -> "[" + * f3 -> Expression() + * f4 -> "]" + */ + public R visit(ArrayAllocationExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "new" + * f1 -> Identifier() + * f2 -> "(" + * f3 -> ")" + */ + public R visit(AllocationExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "!" + * f1 -> Expression() + */ + public R visit(NotExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + --this.offset; + return _ret; + } + + /** + * f0 -> "(" + * f1 -> Expression() + * f2 -> ")" + */ + public R visit(BracketExpression n, A argu) { + this.printNode(n, argu); + R _ret=null; + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + --this.offset; + return _ret; + } + + public R visit(NodeToken n, A argu) { + for (int i=0; i < this.offset; ++i) + PrintFilter.print(".", false); + PrintFilter.print(n.getClass().getSimpleName() + + " => " + + n.toString(), true); + R _ret=null; + return _ret; + } + +} diff --git a/misc/PrintFilter.java b/misc/PrintFilter.java new file mode 100644 index 0000000..971ef04 --- /dev/null +++ b/misc/PrintFilter.java @@ -0,0 +1,15 @@ +package misc; + + +public class PrintFilter { + + public static void print(String message, boolean newline) { + boolean debug = true; + if (debug) { + System.out.print(message); + if (newline) + System.out.println(); + } + } + +} diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java new file mode 100644 index 0000000..a7dc8eb --- /dev/null +++ b/st/AbstractInstance.java @@ -0,0 +1,35 @@ +package st; + +public abstract class AbstractInstance { + protected String name; // the literal name of the declaration + protected TypeEnum type; // the type of the declaration + protected int size; // the size in memory + + public AbstractInstance(String name, TypeEnum type) { + this.type = type; + this.name = name; + } + + public abstract String toString(); + + public boolean equals(AbstractInstance other) { + return this.name == other.get_name(); + } + + public int hashCode() { + return this.name.hashCode(); + } + + public String get_name() { + return this.name; + } + + public TypeEnum get_type() { + return this.type; + } + + public int get_size() { + return this.size; + } + +} diff --git a/st/ClassInstance.java b/st/ClassInstance.java new file mode 100644 index 0000000..c1b07af --- /dev/null +++ b/st/ClassInstance.java @@ -0,0 +1,37 @@ +package st; + +import java.util.ArrayList; + +public class ClassInstance extends AbstractInstance { + private ArrayList attrs; // the list of class-fields + private ArrayList mtds; // the list of methods + private String ext; // the name of the extended class (null if none) + + public ClassInstance(String name) { + super(name, TypeEnum.classname); + this.ext = null; + } + + public ClassInstance(String name, String ext) { + super(name, TypeEnum.classname); + this.ext = ext; + } + + public String toString() { + return this.name + ":" + this.type + "(" + + this.ext + ")"; + } + + public void add_attribute(TypeInstance attr) { + this.attrs.add(attr); + } + + public void add_attribute(MethodInstance mtd) { + this.mtds.add(mtd); + } + + public String get_extend() { + return this.ext; + } + +} diff --git a/st/MethodInstance.java b/st/MethodInstance.java new file mode 100644 index 0000000..c0082af --- /dev/null +++ b/st/MethodInstance.java @@ -0,0 +1,24 @@ +package st; + +import java.util.ArrayList; + +public class MethodInstance extends AbstractInstance { + private ArrayList args; // the list of arguments + private TypeEnum rtrn; // the returned type + + public MethodInstance(String name, TypeEnum rtrn) { + super(name, TypeEnum.method); + this.args = new ArrayList<>(); + this.rtrn = rtrn; + } + + public String toString() { + return name + ":" + type + "[" + + this.rtrn + "]"; + } + + public void add_argument(TypeEnum arg) { + this.args.add(arg); + } + +} diff --git a/st/SymTableVis.java b/st/SymTableVis.java new file mode 100644 index 0000000..280a89c --- /dev/null +++ b/st/SymTableVis.java @@ -0,0 +1,219 @@ +package st; + +import syntaxtree.*; +import visitor.*; +import java.util.*; +import misc.*; + +/** + * Provides default methods which visit each node in the tree in depth-first + * order. Your visitors may extend this class. + */ +public class SymTableVis extends GJDepthFirst { + + public HashMap symt = new HashMap<>(); + + /** + * 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) { + + PrintFilter.print("Processing main", true); + + String id = n.f1.f0.tokenImage; + + ClassInstance type = new ClassInstance(id); + PrintFilter.print("Inserting " + id + " => " + type, true); + symt.put(id, type); + + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + n.f6.accept(this, argu); + n.f7.accept(this, argu); + n.f8.accept(this, argu); + n.f9.accept(this, argu); + n.f10.accept(this, argu); + n.f11.accept(this, argu); + n.f12.accept(this, argu); + n.f13.accept(this, argu); + n.f14.accept(this, argu); + n.f15.accept(this, argu); + n.f16.accept(this, argu); + n.f17.accept(this, argu); + return null; + + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> ( VarDeclaration() )* + * f4 -> ( MethodDeclaration() )* + * f5 -> "}" + */ + public R visit(ClassDeclaration n, A argu) { + + PrintFilter.print("Processing class", true); + + String id = n.f1.f0.tokenImage; + + ClassInstance type = new ClassInstance(id); + PrintFilter.print("Inserting " + id + " => " + type, true); + // Safe? + + symt.put(id, type); + + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + return null; + + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "extends" + * f3 -> Identifier() + * f4 -> "{" + * f5 -> ( VarDeclaration() )* + * f6 -> ( MethodDeclaration() )* + * f7 -> "}" + */ + public R visit(ClassExtendsDeclaration n, A argu) { + PrintFilter.print("Processing class", true); + + String id = n.f1.f0.tokenImage; + String ext = n.f3.f0.tokenImage; + + ClassInstance type = new ClassInstance(id, ext); + PrintFilter.print("Inserting " + id + " => " + type + + "(" + ext + ")", true); + + symt.put(id, type); + + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + n.f6.accept(this, argu); + n.f7.accept(this, argu); + return null; + } + + + /** + * f0 -> Type() + * f1 -> Identifier() + * f2 -> ";" + */ + public R visit(VarDeclaration n, A argu) { + + PrintFilter.print("Processing declaration", true); + + String id = n.f1.f0.tokenImage; + TypeEnum rtrn = TypeEnum.ERROR; + switch (n.f0.f0.which) { + case 0: + rtrn = TypeEnum.intarray; break; + case 1: + rtrn = TypeEnum.bool; break; + case 2: + rtrn = TypeEnum.integer; break; + case 3: + rtrn = TypeEnum.classname; break; + default: + PrintFilter.print("Unsupported case", true); + } + + TypeInstance type = new TypeInstance(id, rtrn); + PrintFilter.print("Inserting " + type, true); + symt.put(id, type); + + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + return null; + } + + /** + * f0 -> "public" + * f1 -> Type() + * f2 -> Identifier() + * f3 -> "(" + * f4 -> ( FormalParameterList() )? + * f5 -> ")" + * f6 -> "{" + * f7 -> ( VarDeclaration() )* + * f8 -> ( Statement() )* + * f9 -> "return" + * f10 -> Expression() + * f11 -> ";" + * f12 -> "}" + */ + public R visit(MethodDeclaration n, A argu) { + + PrintFilter.print("Processing method", true); + + String id = n.f2.f0.tokenImage; + + TypeEnum rtrn = TypeEnum.ERROR; + switch (n.f1.f0.which) { + case 0: + rtrn = TypeEnum.intarray; break; + case 1: + rtrn = TypeEnum.bool; break; + case 2: + rtrn = TypeEnum.integer; break; + default: + PrintFilter.print("Unsupported case", true); + } + + MethodInstance type = new MethodInstance(id, rtrn); + PrintFilter.print("Inserting " + type, true); + symt.put(id, type); + + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + n.f4.accept(this, argu); + n.f5.accept(this, argu); + n.f6.accept(this, argu); + n.f7.accept(this, argu); + n.f8.accept(this, argu); + n.f9.accept(this, argu); + n.f10.accept(this, argu); + n.f11.accept(this, argu); + n.f12.accept(this, argu); + return null; + } + +} diff --git a/st/TypeEnum.java b/st/TypeEnum.java new file mode 100644 index 0000000..f6fc6ae --- /dev/null +++ b/st/TypeEnum.java @@ -0,0 +1,5 @@ +package st; + +public enum TypeEnum { + classname, method, intarray, bool, integer, CHECK, ERROR +} diff --git a/st/TypeInstance.java b/st/TypeInstance.java new file mode 100644 index 0000000..13947ae --- /dev/null +++ b/st/TypeInstance.java @@ -0,0 +1,32 @@ +package st; + +public class TypeInstance extends AbstractInstance { + + public TypeInstance(String name, TypeEnum type) { + super(name, type); + } + + public String toString() { + return this.name + ":" + this.type; + } + + public boolean same_type(TypeInstance other) { + /** + * Given a TypeInstance object other, + * returns true if other object + * is the same type as this one. + * + * We can say two types are equal, as + * long as they are not equal on a + * type error! + */ + + return this.type != TypeEnum.ERROR && + this.type == other.get_type(); + } + + public boolean has_checked() { + return type != TypeEnum.ERROR; + } + +} diff --git a/typecheck/library/PPrinter.java b/typecheck/library/PPrinter.java deleted file mode 100644 index e8e1b15..0000000 --- a/typecheck/library/PPrinter.java +++ /dev/null @@ -1,737 +0,0 @@ -package typecheck.library; - -import syntaxtree.*; -import visitor.*; -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 PPrinter extends GJDepthFirst { - - private int offset; - - private void printNode(Node n, A argu) { - for (int i=0; i < this.offset; ++i) - Utilities.print_filter(".", false); - Utilities.print_filter(n.getClass().getSimpleName(), true); - ++this.offset; - } - - // - // User-generated visitor methods below - // - - /** - * f0 -> MainClass() - * f1 -> ( TypeDeclaration() )* - * f2 -> - */ - public R visit(Goal n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * 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) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - n.f7.accept(this, argu); - n.f8.accept(this, argu); - n.f9.accept(this, argu); - n.f10.accept(this, argu); - n.f11.accept(this, argu); - n.f12.accept(this, argu); - n.f13.accept(this, argu); - n.f14.accept(this, argu); - n.f15.accept(this, argu); - n.f16.accept(this, argu); - n.f17.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> ClassDeclaration() - * | ClassExtendsDeclaration() - */ - public R visit(TypeDeclaration n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "{" - * f3 -> ( VarDeclaration() )* - * f4 -> ( MethodDeclaration() )* - * f5 -> "}" - */ - public R visit(ClassDeclaration n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "extends" - * f3 -> Identifier() - * f4 -> "{" - * f5 -> ( VarDeclaration() )* - * f6 -> ( MethodDeclaration() )* - * f7 -> "}" - */ - public R visit(ClassExtendsDeclaration n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - n.f7.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> Type() - * f1 -> Identifier() - * f2 -> ";" - */ - public R visit(VarDeclaration n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "public" - * f1 -> Type() - * f2 -> Identifier() - * f3 -> "(" - * f4 -> ( FormalParameterList() )? - * f5 -> ")" - * f6 -> "{" - * f7 -> ( VarDeclaration() )* - * f8 -> ( Statement() )* - * f9 -> "return" - * f10 -> Expression() - * f11 -> ";" - * f12 -> "}" - */ - public R visit(MethodDeclaration n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - n.f7.accept(this, argu); - n.f8.accept(this, argu); - n.f9.accept(this, argu); - n.f10.accept(this, argu); - n.f11.accept(this, argu); - n.f12.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> FormalParameter() - * f1 -> ( FormalParameterRest() )* - */ - public R visit(FormalParameterList n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> Type() - * f1 -> Identifier() - */ - public R visit(FormalParameter n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "," - * f1 -> FormalParameter() - */ - public R visit(FormalParameterRest n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> ArrayType() - * | BooleanType() - * | IntegerType() - * | Identifier() - */ - public R visit(Type n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "int" - * f1 -> "[" - * f2 -> "]" - */ - public R visit(ArrayType n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "boolean" - */ - public R visit(BooleanType n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "int" - */ - public R visit(IntegerType n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> Block() - * | AssignmentStatement() - * | ArrayAssignmentStatement() - * | IfStatement() - * | WhileStatement() - * | PrintStatement() - */ - public R visit(Statement n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "{" - * f1 -> ( Statement() )* - * f2 -> "}" - */ - public R visit(Block n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> Identifier() - * f1 -> "=" - * f2 -> Expression() - * f3 -> ";" - */ - public R visit(AssignmentStatement n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> Identifier() - * f1 -> "[" - * f2 -> Expression() - * f3 -> "]" - * f4 -> "=" - * f5 -> Expression() - * f6 -> ";" - */ - public R visit(ArrayAssignmentStatement n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "if" - * f1 -> "(" - * f2 -> Expression() - * f3 -> ")" - * f4 -> Statement() - * f5 -> "else" - * f6 -> Statement() - */ - public R visit(IfStatement n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "while" - * f1 -> "(" - * f2 -> Expression() - * f3 -> ")" - * f4 -> Statement() - */ - public R visit(WhileStatement n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "System.out.println" - * f1 -> "(" - * f2 -> Expression() - * f3 -> ")" - * f4 -> ";" - */ - public R visit(PrintStatement n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> AndExpression() - * | CompareExpression() - * | PlusExpression() - * | MinusExpression() - * | TimesExpression() - * | ArrayLookup() - * | ArrayLength() - * | MessageSend() - * | PrimaryExpression() - */ - public R visit(Expression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "&&" - * f2 -> PrimaryExpression() - */ - public R visit(AndExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "<" - * f2 -> PrimaryExpression() - */ - public R visit(CompareExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "+" - * f2 -> PrimaryExpression() - */ - public R visit(PlusExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "-" - * f2 -> PrimaryExpression() - */ - public R visit(MinusExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "*" - * f2 -> PrimaryExpression() - */ - public R visit(TimesExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "[" - * f2 -> PrimaryExpression() - * f3 -> "]" - */ - public R visit(ArrayLookup n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "." - * f2 -> "length" - */ - public R visit(ArrayLength n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "." - * f2 -> Identifier() - * f3 -> "(" - * f4 -> ( ExpressionList() )? - * f5 -> ")" - */ - public R visit(MessageSend n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> Expression() - * f1 -> ( ExpressionRest() )* - */ - public R visit(ExpressionList n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "," - * f1 -> Expression() - */ - public R visit(ExpressionRest n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> IntegerLiteral() - * | TrueLiteral() - * | FalseLiteral() - * | Identifier() - * | ThisExpression() - * | ArrayAllocationExpression() - * | AllocationExpression() - * | NotExpression() - * | BracketExpression() - */ - public R visit(PrimaryExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> - */ - public R visit(IntegerLiteral n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "true" - */ - public R visit(TrueLiteral n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "false" - */ - public R visit(FalseLiteral n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> - */ - public R visit(Identifier n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "this" - */ - public R visit(ThisExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "new" - * f1 -> "int" - * f2 -> "[" - * f3 -> Expression() - * f4 -> "]" - */ - public R visit(ArrayAllocationExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "new" - * f1 -> Identifier() - * f2 -> "(" - * f3 -> ")" - */ - public R visit(AllocationExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "!" - * f1 -> Expression() - */ - public R visit(NotExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - --this.offset; - return _ret; - } - - /** - * f0 -> "(" - * f1 -> Expression() - * f2 -> ")" - */ - public R visit(BracketExpression n, A argu) { - this.printNode(n, argu); - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - --this.offset; - return _ret; - } - - public R visit(NodeToken n, A argu) { - for (int i=0; i < this.offset; ++i) - Utilities.print_filter(".", false); - Utilities.print_filter(n.getClass().getSimpleName() + - " => " + - n.toString(), true); - R _ret=null; - return _ret; - } - -} diff --git a/typecheck/library/SymTableVis.java b/typecheck/library/SymTableVis.java deleted file mode 100644 index 615b1c9..0000000 --- a/typecheck/library/SymTableVis.java +++ /dev/null @@ -1,101 +0,0 @@ -package typecheck.library; - -import syntaxtree.*; -import visitor.*; -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 SymTableVis extends GJDepthFirst { - - public HashMap symt = new HashMap<>(); - - /** - * 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); - - Utilities.print_filter("Processing main", true); - - String id = n.f1.f0.tokenImage; - - TypeInstance type = new TypeInstance(id, TypeEnum.classname); - Utilities.print_filter("Inserting " + id + " => " + type, true); - symt.put(id, type); - - return null; - - } - - /** - * f0 -> Type() - * f1 -> Identifier() - * f2 -> ";" - */ - public R visit(VarDeclaration n, A argu) { - - Utilities.print_filter("Processing declaration", true); - - String id = n.f1.f0.tokenImage; - TypeInstance type = new TypeInstance("ERROR", TypeEnum.ERROR); - switch (n.f0.f0.which) { - case 0: - type = new TypeInstance("int_array", TypeEnum.int_array); break; - case 1: - type = new TypeInstance("bool", TypeEnum.bool); break; - case 2: - type = new TypeInstance("int", TypeEnum.integer); break; - case 3: - type = new TypeInstance(id, TypeEnum.classname); break; - default: - Utilities.print_filter("Unsupported case", true); - } - - Utilities.print_filter("Inserting " + id + " => " + type, true); - // Safe? - symt.put(id, type); - - return null; - } - - public R visit(ClassDeclaration n, A argu) { - - Utilities.print_filter("Processing class", true); - - String id = n.f1.f0.tokenImage; - - TypeInstance type = new TypeInstance(id, TypeEnum.classname); - Utilities.print_filter("Inserting " + id + " => " + type, true); - // Safe? - symt.put(id, type); - - return null; - - } - - -} diff --git a/typecheck/library/TypeCheckSimp.java b/typecheck/library/TypeCheckSimp.java index b4844a1..3d262fd 100644 --- a/typecheck/library/TypeCheckSimp.java +++ b/typecheck/library/TypeCheckSimp.java @@ -2,34 +2,36 @@ package typecheck.library; import syntaxtree.*; import visitor.*; +import st.*; +import misc.*; 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) - Utilities.print_filter(".", false); + PrintFilter.print(".", false); if (enter) - Utilities.print_filter("Visiting ", false); + PrintFilter.print("Visiting ", false); else - Utilities.print_filter("Leaving ", false); - Utilities.print_filter(n.getClass().getSimpleName(), false); + PrintFilter.print("Leaving ", false); + PrintFilter.print(n.getClass().getSimpleName(), false); if (!enter) { if (consensus == TypeEnum.ERROR) - Utilities.print_filter(" did not type check.", false); + PrintFilter.print(" did not type check.", false); else - Utilities.print_filter(" found type " + consensus, false); + PrintFilter.print(" found type " + consensus, false); } - Utilities.print_filter("", true); + PrintFilter.print("", true); } - public TypeInstance visit(NodeList n, HashMap argu) { + public TypeInstance visit(NodeList n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -48,7 +50,7 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(NodeListOptional n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -72,7 +74,7 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(NodeOptional n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -88,7 +90,7 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(NodeSequence n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -106,13 +108,13 @@ public class TypeCheckSimp implements GJVisitor argu) { + public TypeInstance visit(NodeToken n, HashMap argu) { // A fixed string token. '⌣' for (int i=0; i < this.offset; ++i) - Utilities.print_filter(".", false); - Utilities.print_filter("Leaving " + n.getClass().getSimpleName() + - " => " + - n.toString(), true); + PrintFilter.print(".", false); + PrintFilter.print("Leaving " + n.getClass().getSimpleName() + + " => " + + n.toString(), true); return null; } @@ -125,7 +127,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); @@ -158,7 +160,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); @@ -188,9 +190,9 @@ public class TypeCheckSimp implements GJVisitor ClassDeclaration() - * | ClassExtendsDeclaration() + * | ClassExtendsDeclaration() */ - public TypeInstance visit(TypeDeclaration n, HashMap argu) { + public TypeInstance visit(TypeDeclaration n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -209,7 +211,7 @@ 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); @@ -240,7 +242,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); @@ -269,7 +271,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); @@ -298,7 +300,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); @@ -315,7 +317,7 @@ 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); @@ -350,7 +352,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); @@ -366,7 +368,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); @@ -380,15 +382,15 @@ public class TypeCheckSimp implements GJVisitor ArrayType() - * | BooleanType() - * | IntegerType() - * | Identifier() + * | BooleanType() + * | IntegerType() + * | Identifier() */ - public TypeInstance visit(Type n, HashMap argu) { + public TypeInstance visit(Type n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); - TypeInstance ret = n.f0.accept(this, argu); + TypeInstance ret = n.f0.accept(this, argu); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -400,11 +402,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(null, TypeEnum.int_array); + TypeInstance ret = new TypeInstance(null, TypeEnum.intarray); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -414,11 +416,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(null, TypeEnum.bool); + TypeInstance ret = new TypeInstance(null, TypeEnum.bool); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -428,7 +430,7 @@ 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); @@ -441,13 +443,13 @@ public class TypeCheckSimp implements GJVisitor Block() - * | AssignmentStatement() - * | ArrayAssignmentStatement() - * | IfStatement() - * | WhileStatement() - * | PrintStatement() + * | AssignmentStatement() + * | ArrayAssignmentStatement() + * | IfStatement() + * | WhileStatement() + * | PrintStatement() */ - public TypeInstance visit(Statement n, HashMap argu) { + public TypeInstance visit(Statement n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -463,7 +465,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); @@ -486,7 +488,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); @@ -494,7 +496,7 @@ 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); @@ -526,7 +528,7 @@ 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); @@ -580,7 +582,7 @@ 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); @@ -608,7 +610,7 @@ 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); @@ -628,16 +630,16 @@ public class TypeCheckSimp implements GJVisitor AndExpression() - * | CompareExpression() - * | PlusExpression() - * | MinusExpression() - * | TimesExpression() - * | ArrayLookup() - * | ArrayLength() - * | MessageSend() - * | PrimaryExpression() + * | CompareExpression() + * | PlusExpression() + * | MinusExpression() + * | TimesExpression() + * | ArrayLookup() + * | ArrayLength() + * | MessageSend() + * | PrimaryExpression() */ - public TypeInstance visit(Expression n, HashMap argu) { + public TypeInstance visit(Expression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -655,7 +657,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); @@ -679,7 +681,7 @@ 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); @@ -703,7 +705,7 @@ 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); @@ -727,7 +729,7 @@ 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); @@ -751,7 +753,7 @@ 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); @@ -777,7 +779,7 @@ 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); @@ -785,7 +787,7 @@ 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(null, TypeEnum.integer) : + ret = (ret.get_type() == TypeEnum.intarray) ? new TypeInstance(null, TypeEnum.integer) : new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); @@ -828,7 +830,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); @@ -847,7 +849,7 @@ 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); @@ -867,7 +869,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,16 +883,16 @@ public class TypeCheckSimp implements GJVisitor IntegerLiteral() - * | TrueLiteral() - * | FalseLiteral() - * | Identifier() - * | ThisExpression() - * | ArrayAllocationExpression() - * | AllocationExpression() - * | NotExpression() - * | BracketExpression() + * | TrueLiteral() + * | FalseLiteral() + * | Identifier() + * | ThisExpression() + * | ArrayAllocationExpression() + * | AllocationExpression() + * | NotExpression() + * | BracketExpression() */ - public TypeInstance visit(PrimaryExpression n, HashMap argu) { + public TypeInstance visit(PrimaryExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -907,7 +909,7 @@ 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); @@ -921,7 +923,7 @@ 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); @@ -935,7 +937,7 @@ 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); @@ -950,12 +952,14 @@ 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 = (argu.get(n.f0.tokenImage) != null) ? - argu.get(n.f0.tokenImage) : new TypeInstance(null, TypeEnum.ERROR); + new TypeInstance(argu.get(n.f0.tokenImage).get_name(), + argu.get(n.f0.tokenImage).get_type()) : + new TypeInstance(null, TypeEnum.ERROR); this.printNode(n, argu, false, ret.get_type()); --this.offset; @@ -968,7 +972,7 @@ public class TypeCheckSimp implements GJVisitor "this" */ - public TypeInstance visit(ThisExpression n, HashMap argu) { + public TypeInstance visit(ThisExpression n, HashMap argu) { ++this.offset; this.printNode(n, argu, true, null); @@ -989,7 +993,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); @@ -998,7 +1002,7 @@ 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); @@ -1035,7 +1039,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); @@ -1054,7 +1058,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/typecheck/library/TypeEnum.java b/typecheck/library/TypeEnum.java deleted file mode 100644 index be6cab2..0000000 --- a/typecheck/library/TypeEnum.java +++ /dev/null @@ -1,5 +0,0 @@ -package typecheck.library; - -public enum TypeEnum { - classname, int_array, bool, integer, CHECK, ERROR -} diff --git a/typecheck/library/TypeInstance.java b/typecheck/library/TypeInstance.java deleted file mode 100644 index 756d39b..0000000 --- a/typecheck/library/TypeInstance.java +++ /dev/null @@ -1,43 +0,0 @@ -package typecheck.library; - -public class TypeInstance { - TypeEnum type; - String type_name; - - 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) { - /** - * Given a TypeInstance object other, - * returns true if other object - * is the same type as this one. - * - * We can say two types are equal, as - * long as they are not equal on a - * type error! - */ - - return this.type != TypeEnum.ERROR && - 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; - } - -} diff --git a/typecheck/library/Utilities.java b/typecheck/library/Utilities.java deleted file mode 100644 index fad1e7e..0000000 --- a/typecheck/library/Utilities.java +++ /dev/null @@ -1,14 +0,0 @@ -package typecheck.library; - -public class Utilities { - - public static void print_filter(String message, boolean newline) { - boolean debug = false; - if (debug) { - System.out.print(message); - if (newline) - System.out.println(); - } - } - -} diff --git a/typecheck/tests/RetrieveInteger.java b/typecheck/tests/RetrieveInteger.java index 036ad8b..1997e38 100644 --- a/typecheck/tests/RetrieveInteger.java +++ b/typecheck/tests/RetrieveInteger.java @@ -4,7 +4,7 @@ class RetrieveInteger{ int j ; i = new GetInteger() ; - j = i.GetInteger() ; + j = i.Get() ; System.out.println(j) ; } diff --git a/vaporize/library/VaporizeSimp.java b/vaporize/library/VaporizeSimp.java index 289ca25..80db36b 100644 --- a/vaporize/library/VaporizeSimp.java +++ b/vaporize/library/VaporizeSimp.java @@ -4,10 +4,6 @@ import syntaxtree.*; import visitor.*; 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 VaporizeSimp extends GJDepthFirst { private int offset; -- cgit v1.2.3