summaryrefslogtreecommitdiff
path: root/typecheck
diff options
context:
space:
mode:
Diffstat (limited to 'typecheck')
-rw-r--r--typecheck/library/PPrinter.java737
-rw-r--r--typecheck/library/SymTableVis.java101
-rw-r--r--typecheck/library/TypeCheckSimp.java1070
-rw-r--r--typecheck/library/TypeEnum.java5
-rw-r--r--typecheck/library/TypeInstance.java43
-rw-r--r--typecheck/library/Utilities.java14
-rw-r--r--typecheck/tests/BinaryTree-error.java334
-rw-r--r--typecheck/tests/BinaryTree.java334
-rw-r--r--typecheck/tests/Branch-error.java11
-rw-r--r--typecheck/tests/Branch.java11
-rw-r--r--typecheck/tests/BubbleSort-error.java93
-rw-r--r--typecheck/tests/BubbleSort.java93
-rw-r--r--typecheck/tests/DeclareNone.java6
-rw-r--r--typecheck/tests/Empty.java5
-rw-r--r--typecheck/tests/Factorial-error.java16
-rw-r--r--typecheck/tests/Factorial.java16
-rw-r--r--typecheck/tests/IsPositive.java17
-rw-r--r--typecheck/tests/LinearSearch-error.java99
-rw-r--r--typecheck/tests/LinearSearch.java99
-rw-r--r--typecheck/tests/LinkedList-error.java278
-rw-r--r--typecheck/tests/LinkedList.java278
-rw-r--r--typecheck/tests/MoreThan4-error.java29
-rw-r--r--typecheck/tests/MoreThan4.java29
-rw-r--r--typecheck/tests/NewNone.java9
-rw-r--r--typecheck/tests/Printer-error.java5
-rw-r--r--typecheck/tests/Printer.java5
-rw-r--r--typecheck/tests/QuickSort-error.java112
-rw-r--r--typecheck/tests/QuickSort.java112
-rw-r--r--typecheck/tests/RetrieveInteger.java17
-rw-r--r--typecheck/tests/SimpleArithmetic.java6
-rw-r--r--typecheck/tests/SimpleArray-error.java6
-rw-r--r--typecheck/tests/SimpleArray.java6
-rw-r--r--typecheck/tests/TreeVisitor-error.java376
-rw-r--r--typecheck/tests/TreeVisitor.java374
34 files changed, 4746 insertions, 0 deletions
diff --git a/typecheck/library/PPrinter.java b/typecheck/library/PPrinter.java
new file mode 100644
index 0000000..e8e1b15
--- /dev/null
+++ b/typecheck/library/PPrinter.java
@@ -0,0 +1,737 @@
+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<R,A> extends GJDepthFirst<R,A> {
+
+ 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 -> <EOF>
+ */
+ 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 -> <INTEGER_LITERAL>
+ */
+ 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 -> <IDENTIFIER>
+ */
+ 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
new file mode 100644
index 0000000..615b1c9
--- /dev/null
+++ b/typecheck/library/SymTableVis.java
@@ -0,0 +1,101 @@
+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<R,A> extends GJDepthFirst<R,A> {
+
+ public HashMap<String,TypeInstance> 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
new file mode 100644
index 0000000..b4844a1
--- /dev/null
+++ b/typecheck/library/TypeCheckSimp.java
@@ -0,0 +1,1070 @@
+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 TypeCheckSimp implements GJVisitor<TypeInstance,HashMap<String,TypeInstance>> {
+
+ private int offset;
+
+ private void printNode(Node n, HashMap<String,TypeInstance> argu, boolean enter, TypeEnum consensus) {
+ for (int i=0; i < this.offset; ++i)
+ Utilities.print_filter(".", false);
+ if (enter)
+ Utilities.print_filter("Visiting ", false);
+ else
+ Utilities.print_filter("Leaving ", false);
+ Utilities.print_filter(n.getClass().getSimpleName(), false);
+ if (!enter) {
+ if (consensus == TypeEnum.ERROR)
+ Utilities.print_filter(" did not type check.", false);
+ else
+ Utilities.print_filter(" found type " + consensus, false);
+ }
+ Utilities.print_filter("", true);
+ }
+
+ public TypeInstance visit(NodeList n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK);
+ int _count=0;
+ for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) {
+ TypeInstance node = e.nextElement().accept(this,argu);
+ e.nextElement().accept(this,argu);
+ if (node.get_type() == TypeEnum.ERROR)
+ ret = node;
+ _count++;
+ }
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ public TypeInstance visit(NodeListOptional n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret;
+ if ( n.present() ) {
+ ret = new TypeInstance(null, TypeEnum.CHECK);
+ int _count=0;
+ for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) {
+ TypeInstance node = e.nextElement().accept(this,argu);
+ if (node.get_type() == TypeEnum.ERROR)
+ ret = node;
+ _count++;
+ }
+ }
+ else
+ ret = new TypeInstance(null, TypeEnum.CHECK);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ // FIXME
+ public TypeInstance visit(NodeOptional n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret;
+
+ if ( n.present() )
+ ret = n.node.accept(this,argu);
+ else
+ ret = new TypeInstance(null, TypeEnum.CHECK);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ public TypeInstance visit(NodeSequence n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK);
+ int _count=0;
+ for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) {
+ TypeInstance node = e.nextElement().accept(this,argu);
+ if (node.get_type() == TypeEnum.ERROR)
+ ret = node;
+ _count++;
+ }
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ public TypeInstance visit(NodeToken n, HashMap<String,TypeInstance> 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);
+ return null;
+ }
+
+ //
+ // User-generated visitor methods below
+ //
+
+ /**
+ * f0 -> MainClass()
+ * f1 -> ( TypeDeclaration() )*
+ * f2 -> <EOF>
+ */
+ public TypeInstance visit(Goal n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --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 TypeInstance visit(MainClass n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance name = n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ 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);
+ // TypeInstance args = n.f11.accept(this, argu); // FIXME Type in the main class declaration uses an illegal minijava type?
+ n.f12.accept(this, argu);
+ n.f13.accept(this, argu);
+ TypeInstance var_dec = n.f14.accept(this, argu);
+ TypeInstance stmt = n.f15.accept(this, argu);
+ n.f16.accept(this, argu);
+ n.f17.accept(this, argu);
+
+ this.printNode(n, argu, false, stmt.get_type());
+ --this.offset;
+ return stmt;
+ }
+
+ /**
+ * f0 -> ClassDeclaration()
+ * | ClassExtendsDeclaration()
+ */
+ public TypeInstance visit(TypeDeclaration n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = n.f0.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "{"
+ * f3 -> ( VarDeclaration() )*
+ * f4 -> ( MethodDeclaration() )*
+ * f5 -> "}"
+ */
+ public TypeInstance visit(ClassDeclaration n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance id = n.f1.accept(this, argu);
+ n.f2.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;
+ }
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "extends"
+ * f3 -> Identifier()
+ * f4 -> "{"
+ * f5 -> ( VarDeclaration() )*
+ * f6 -> ( MethodDeclaration() )*
+ * f7 -> "}"
+ */
+ public TypeInstance visit(ClassExtendsDeclaration n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance id = n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ TypeInstance ext = n.f3.accept(this, argu);
+ n.f4.accept(this, argu);
+ TypeInstance vars = n.f5.accept(this, argu);
+ TypeInstance mehs = n.f6.accept(this, argu);
+ n.f7.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;
+ }
+
+ // FIXME (this may be ST-only)
+ /**
+ * f0 -> Type()
+ * f1 -> Identifier()
+ * f2 -> ";"
+ */
+ public TypeInstance visit(VarDeclaration n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ // FIXME
+ /**
+ * f0 -> "public"
+ * f1 -> Type()
+ * f2 -> Identifier()
+ * f3 -> "("
+ * f4 -> ( FormalParameterList() )?
+ * f5 -> ")"
+ * f6 -> "{"
+ * f7 -> ( VarDeclaration() )*
+ * f8 -> ( Statement() )*
+ * f9 -> "return"
+ * f10 -> Expression()
+ * f11 -> ";"
+ * f12 -> "}"
+ */
+ public TypeInstance visit(MethodDeclaration n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance ret_type = n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ 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);
+ TypeInstance stmt = n.f8.accept(this, argu);
+ n.f9.accept(this, argu);
+ TypeInstance retur = n.f10.accept(this, argu);
+ n.f11.accept(this, argu);
+ n.f12.accept(this, argu);
+ TypeInstance ret = (stmt.equal_type(ret_type) &&
+ stmt.has_checked()) ?
+ new TypeInstance(null, TypeEnum.CHECK) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> FormalParameter()
+ * f1 -> ( FormalParameterRest() )*
+ */
+ public TypeInstance visit(FormalParameterList n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance _ret=null;
+ TypeInstance para1 = n.f0.accept(this, argu);
+ TypeInstance parar = n.f1.accept(this, argu);
+ TypeInstance ret = (para1.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;
+ return ret;
+ }
+
+ /**
+ * f0 -> Type()
+ * f1 -> Identifier()
+ */
+ public TypeInstance visit(FormalParameter n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> ","
+ * f1 -> FormalParameter()
+ */
+ public TypeInstance visit(FormalParameterRest n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance ret = n.f1.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> ArrayType()
+ * | BooleanType()
+ * | IntegerType()
+ * | Identifier()
+ */
+ public TypeInstance visit(Type n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = n.f0.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> "int"
+ * f1 -> "["
+ * f2 -> "]"
+ */
+ public TypeInstance visit(ArrayType n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = new TypeInstance(null, TypeEnum.int_array);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> "boolean"
+ */
+ public TypeInstance visit(BooleanType n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = new TypeInstance(null, TypeEnum.bool);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> "int"
+ */
+ public TypeInstance visit(IntegerType n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = new TypeInstance(null, TypeEnum.integer);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> Block()
+ * | AssignmentStatement()
+ * | ArrayAssignmentStatement()
+ * | IfStatement()
+ * | WhileStatement()
+ * | PrintStatement()
+ */
+ public TypeInstance visit(Statement n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = n.f0.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> "{"
+ * f1 -> ( Statement() )*
+ * f2 -> "}"
+ */
+ public TypeInstance visit(Block n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance ret = n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ // FIXME FIXME FIXME
+ // Given we only have a few types, what is a subtype of what?
+ /**
+ * [23]: Expression is a subtype of identifier, and e typechecks*
+ *
+ * f0 -> Identifier()
+ * f1 -> "="
+ * f2 -> Expression()
+ * f3 -> ";"
+ */
+ public TypeInstance visit(AssignmentStatement n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance lhs = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance rhs = n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ TypeInstance ret = (lhs.equal_type(rhs)) ?
+ new TypeInstance(null, TypeEnum.CHECK) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ // FIXME (this may be done)
+ /**
+ * [24]: Identifier is an integer array, expressions are both integers
+ *
+ * f0 -> Identifier()
+ * f1 -> "["
+ * f2 -> Expression()
+ * f3 -> "]"
+ * f4 -> "="
+ * f5 -> Expression()
+ * f6 -> ";"
+ */
+ public TypeInstance visit(ArrayAssignmentStatement n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance id = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance index = n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ n.f4.accept(this, argu);
+ TypeInstance value = n.f5.accept(this, argu);
+ n.f6.accept(this, argu);
+ TypeInstance ret = (id.get_type() == TypeEnum.int_array &&
+ index.get_type() == TypeEnum.integer &&
+ value.get_type() == TypeEnum.integer) ?
+ new TypeInstance(null, TypeEnum.CHECK) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [25]: Expression is a bool, both statements type-check
+ *
+ * f0 -> "if"
+ * f1 -> "("
+ * f2 -> Expression()
+ * f3 -> ")"
+ * f4 -> Statement()
+ * f5 -> "else"
+ * f6 -> Statement()
+ */
+ public TypeInstance visit(IfStatement n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance expr = n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ TypeInstance stmt1 = n.f4.accept(this, argu);
+ n.f5.accept(this, argu);
+ TypeInstance stmt2 = n.f6.accept(this, argu);
+ TypeInstance ret = (expr.get_type() == TypeEnum.bool &&
+ stmt1.get_type() == stmt2.get_type() &&
+ stmt1.has_checked()) ?
+ new TypeInstance(null, TypeEnum.CHECK) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+
+ }
+
+ /**
+ * DONE [26]: Expression is a bool, statement type-checks
+ *
+ * f0 -> "while"
+ * f1 -> "("
+ * f2 -> Expression()
+ * f3 -> ")"
+ * f4 -> Statement()
+ */
+ public TypeInstance visit(WhileStatement n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance expr = n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ TypeInstance stmt = n.f4.accept(this, argu);
+ TypeInstance ret = (expr.get_type() == TypeEnum.bool &&
+ stmt.has_checked()) ?
+ new TypeInstance(null, TypeEnum.CHECK) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [27]: Expression is an integer
+ *
+ * f0 -> "System.out.println"
+ * f1 -> "("
+ * f2 -> Expression()
+ * f3 -> ")"
+ * f4 -> ";"
+ */
+ public TypeInstance visit(PrintStatement n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance ret = n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ n.f4.accept(this, argu);
+ ret = (ret.get_type() == TypeEnum.integer) ?
+ new TypeInstance(null, TypeEnum.CHECK) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> AndExpression()
+ * | CompareExpression()
+ * | PlusExpression()
+ * | MinusExpression()
+ * | TimesExpression()
+ * | ArrayLookup()
+ * | ArrayLength()
+ * | MessageSend()
+ * | PrimaryExpression()
+ */
+ public TypeInstance visit(Expression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = n.f0.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [28]: If expressions are both booleans, return is a boolean
+ *
+ * f0 -> PrimaryExpression()
+ * f1 -> "&&"
+ * f2 -> PrimaryExpression()
+ */
+ public TypeInstance visit(AndExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance oper1 = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance oper2 = n.f2.accept(this, argu);
+ TypeInstance ret = (oper1.get_type() == TypeEnum.bool &&
+ oper2.get_type() == TypeEnum.bool) ?
+ new TypeInstance(null, TypeEnum.bool) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [29]: If expressions are both integers, return is a boolean
+ *
+ * f0 -> PrimaryExpression()
+ * f1 -> "<"
+ * f2 -> PrimaryExpression()
+ */
+ public TypeInstance visit(CompareExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance oper1 = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance oper2 = n.f2.accept(this, argu);
+ TypeInstance ret = (oper1.get_type() == TypeEnum.integer &&
+ oper2.get_type() == TypeEnum.integer) ?
+ new TypeInstance(null, TypeEnum.bool) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [30]: If expressions are both integers, return is an integer
+ *
+ * f0 -> PrimaryExpression()
+ * f1 -> "+"
+ * f2 -> PrimaryExpression()
+ */
+ public TypeInstance visit(PlusExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance oper1 = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance oper2 = n.f2.accept(this, argu);
+ TypeInstance ret = (oper1.get_type() == TypeEnum.integer &&
+ oper2.get_type() == TypeEnum.integer) ?
+ new TypeInstance(null, TypeEnum.integer) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [31]: If expressions are both integers, return is an integer
+ *
+ * f0 -> PrimaryExpression()
+ * f1 -> "-"
+ * f2 -> PrimaryExpression()
+ */
+ public TypeInstance visit(MinusExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance oper1 = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance oper2 = n.f2.accept(this, argu);
+ TypeInstance ret = (oper1.get_type() == TypeEnum.integer &&
+ oper2.get_type() == TypeEnum.integer) ?
+ new TypeInstance(null, TypeEnum.integer) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [32]: If expressions are both integers, return is an integer
+ *
+ * f0 -> PrimaryExpression()
+ * f1 -> "*"
+ * f2 -> PrimaryExpression()
+ */
+ public TypeInstance visit(TimesExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance oper1 = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance oper2 = n.f2.accept(this, argu);
+ TypeInstance ret = (oper1.get_type() == TypeEnum.integer &&
+ oper2.get_type() == TypeEnum.integer) ?
+ new TypeInstance(null, TypeEnum.integer) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [33]: If expr1 is an integer array and expr2 is an int,
+ * return is an int
+ *
+ * f0 -> PrimaryExpression()
+ * f1 -> "["
+ * f2 -> PrimaryExpression()
+ * f3 -> "]"
+ */
+ public TypeInstance visit(ArrayLookup n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance array = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ TypeInstance index = n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ TypeInstance ret = (array.get_type() == TypeEnum.int_array &&
+ index.get_type() == TypeEnum.integer) ?
+ new TypeInstance(null, TypeEnum.integer) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [34]: If expr1 is an integer array, return is an int
+ *
+ * f0 -> PrimaryExpression()
+ * f1 -> "."
+ * f2 -> "length"
+ */
+ public TypeInstance visit(ArrayLength n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ ret = (ret.get_type() == TypeEnum.int_array) ? new TypeInstance(null, TypeEnum.integer) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ // FIXME FIXME FIXME
+ /**
+ * [35]: PrimaryExpr must be a classname, id must be a method name, expressionlist must be correct?
+ *
+ * f0 -> PrimaryExpression()
+ * f1 -> "."
+ * f2 -> Identifier()
+ * f3 -> "("
+ * f4 -> ( ExpressionList() )?
+ * f5 -> ")"
+ */
+ public TypeInstance visit(MessageSend n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, 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 new TypeInstance(null, TypeEnum.ERROR);
+ }
+
+ // FIXME
+ /**
+ * f0 -> Expression()
+ * f1 -> ( ExpressionRest() )*
+ */
+ public TypeInstance visit(ExpressionList n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance expr1 = n.f0.accept(this, argu);
+ TypeInstance exprr = n.f1.accept(this, argu);
+ TypeInstance ret = (expr1.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;
+ return ret;
+ }
+
+ /**
+ * f0 -> ","
+ * f1 -> Expression()
+ */
+ public TypeInstance visit(ExpressionRest n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance ret = n.f1.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * f0 -> IntegerLiteral()
+ * | TrueLiteral()
+ * | FalseLiteral()
+ * | Identifier()
+ * | ThisExpression()
+ * | ArrayAllocationExpression()
+ * | AllocationExpression()
+ * | NotExpression()
+ * | BracketExpression()
+ */
+ public TypeInstance visit(PrimaryExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ TypeInstance ret = n.f0.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [36]: return is an int
+ *
+ *
+ * f0 -> <INTEGER_LITERAL>
+ */
+ public TypeInstance visit(IntegerLiteral n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ this.printNode(n, argu, false, TypeEnum.integer);
+ --this.offset;
+ return new TypeInstance(null, TypeEnum.integer);
+ }
+
+ /**
+ * DONE [37]: return is a bool
+ *
+ * f0 -> "true"
+ */
+ public TypeInstance visit(TrueLiteral n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ this.printNode(n, argu, false, TypeEnum.bool);
+ --this.offset;
+ return new TypeInstance(null, TypeEnum.bool);
+ }
+
+ /**
+ * DONE [38]: return is a bool
+ *
+ * f0 -> "false"
+ */
+ public TypeInstance visit(FalseLiteral n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ this.printNode(n, argu, false, TypeEnum.bool);
+ --this.offset;
+ return new TypeInstance(null, TypeEnum.bool);
+ }
+
+ // FIXME
+ /**
+ * [39]: id is a symbol in the current domain, return is id's type
+ *
+ * f0 -> <IDENTIFIER>
+ */
+ public TypeInstance visit(Identifier n, HashMap<String,TypeInstance> 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);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ // FIXME FIXME FIXME
+ /**
+ * [40]: method exists? but where is the token?
+ *
+ * f0 -> "this"
+ */
+ public TypeInstance visit(ThisExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * DONE [41]: if expression is an integer, return is an int array
+ *
+ * f0 -> "new"
+ * f1 -> "int"
+ * f2 -> "["
+ * f3 -> Expression()
+ * f4 -> "]"
+ */
+ public TypeInstance visit(ArrayAllocationExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ TypeInstance ret = n.f3.accept(this, argu);
+ n.f4.accept(this, argu);
+ ret = (ret.get_type() == TypeEnum.integer) ? new TypeInstance(null, TypeEnum.int_array) :
+ new TypeInstance(null, TypeEnum.ERROR);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ // FIXME
+ /**
+ * [42]:
+ *
+ * f0 -> "new"
+ * f1 -> Identifier()
+ * f2 -> "("
+ * f3 -> ")"
+ */
+ public TypeInstance visit(AllocationExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance ret = n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+ /**
+ * [43]: if expression is a boolean, return is a boolean
+ *
+ * f0 -> "!"
+ * f1 -> Expression()
+ */
+ public TypeInstance visit(NotExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance ret = n.f1.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return (ret.get_type() == TypeEnum.bool) ? ret : new TypeInstance(null, TypeEnum.ERROR);
+ }
+
+ /**
+ * [44]: if e is a type, return is that same type
+ *
+ * f0 -> "("
+ * f1 -> Expression()
+ * f2 -> ")"
+ */
+ public TypeInstance visit(BracketExpression n, HashMap<String,TypeInstance> argu) {
+ ++this.offset;
+ this.printNode(n, argu, true, null);
+
+ n.f0.accept(this, argu);
+ TypeInstance ret = n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+
+ this.printNode(n, argu, false, ret.get_type());
+ --this.offset;
+ return ret;
+ }
+
+}
diff --git a/typecheck/library/TypeEnum.java b/typecheck/library/TypeEnum.java
new file mode 100644
index 0000000..be6cab2
--- /dev/null
+++ b/typecheck/library/TypeEnum.java
@@ -0,0 +1,5 @@
+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
new file mode 100644
index 0000000..756d39b
--- /dev/null
+++ b/typecheck/library/TypeInstance.java
@@ -0,0 +1,43 @@
+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
new file mode 100644
index 0000000..fad1e7e
--- /dev/null
+++ b/typecheck/library/Utilities.java
@@ -0,0 +1,14 @@
+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/BinaryTree-error.java b/typecheck/tests/BinaryTree-error.java
new file mode 100644
index 0000000..d9be857
--- /dev/null
+++ b/typecheck/tests/BinaryTree-error.java
@@ -0,0 +1,334 @@
+class BinaryTree{
+ public static void main(String[] a){
+ System.out.println(new BT().Start());
+ }
+}
+
+
+// This class invokes the methods to create a tree,
+// insert, delete and serach for elements on it
+class BT {
+
+ public int Start(){
+ Tree root ;
+ boolean ntb ;
+ int nti ;
+
+ root = new Tree();
+ ntb = root.Init(16);
+ ntb = root.Print();
+ System.out.println(100000000);
+ ntb = root.Insert(8) ;
+ ntb = root.Print();
+ ntb = root.Insert(24) ;
+ ntb = root.Insert(4) ;
+ ntb = root.Insert(12) ;
+ ntb = root.Insert(20) ;
+ ntb = root.Insert(28) ;
+ ntb = root.Insert(14) ;
+ ntb = root.Print();
+ System.out.println(root.Search(24));
+ System.out.println(root.Search(12));
+ System.out.println(root.Search(16));
+ System.out.println(root.Search(50));
+ System.out.println(root.Search(12));
+ ntb = root.Delete(); // TE, should be Delete(12)
+ ntb = root.Print();
+ System.out.println(root.Search(12));
+
+ return 0 ;
+ }
+
+}
+
+class Tree{
+ Tree left ;
+ Tree right;
+ int key ;
+ boolean has_left ;
+ boolean has_right ;
+ Tree my_null ;
+
+ // Initialize a node with a key value and no children
+ public boolean Init(int v_key){
+ key = v_key ;
+ has_left = false ;
+ has_right = false ;
+ return true ;
+ }
+
+ // Update the right child with rn
+ public boolean SetRight(Tree rn){
+ right = rn ;
+ return true ;
+ }
+
+ // Update the left child with ln
+ public boolean SetLeft(Tree ln){
+ left = ln ;
+ return true ;
+ }
+
+ public Tree GetRight(){
+ return right ;
+ }
+
+ public Tree GetLeft(){
+ return left;
+ }
+
+ public int GetKey(){
+ return key ;
+ }
+
+ public boolean SetKey(int v_key){
+ key = v_key ;
+ return true ;
+ }
+
+ public boolean GetHas_Right(){
+ return has_right ;
+ }
+
+ public boolean GetHas_Left(){
+ return has_left ;
+ }
+
+ public boolean SetHas_Left(boolean val){
+ has_left = val ;
+ return true ;
+ }
+
+ public boolean SetHas_Right(boolean val){
+ has_right = val ;
+ return true ;
+ }
+
+ // This method compares two integers and
+ // returns true if they are equal and false
+ // otherwise
+ public boolean Compare(int num1 , int num2){
+ boolean ntb ;
+ int nti ;
+
+ ntb = false ;
+ nti = num2 + 1 ;
+ if (num1 < num2) ntb = false ;
+ else if (!(num1 < nti)) ntb = false ;
+ else ntb = true ;
+ return ntb ;
+ }
+
+
+ // Insert a new element in the tree
+ public boolean Insert(int v_key){
+ Tree new_node ;
+ boolean ntb ;
+ boolean cont ;
+ int key_aux ;
+ Tree current_node ;
+
+ new_node = new Tree();
+ ntb = new_node.Init(v_key) ;
+ current_node = this ;
+ cont = true ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux){
+ if (current_node.GetHas_Left())
+ current_node = current_node.GetLeft() ;
+ else {
+ cont = false ;
+ ntb = current_node.SetHas_Left(true);
+ ntb = current_node.SetLeft(new_node);
+ }
+ }
+ else{
+ if (current_node.GetHas_Right())
+ current_node = current_node.GetRight() ;
+ else {
+ cont = false ;
+ ntb = current_node.SetHas_Right(true);
+ ntb = current_node.SetRight(new_node);
+ }
+ }
+ }
+ return true ;
+ }
+
+
+ // Delete an element from the tree
+ public boolean Delete(int v_key){
+ Tree current_node ;
+ Tree parent_node ;
+ boolean cont ;
+ boolean found ;
+ boolean is_root ;
+ int key_aux ;
+ boolean ntb ;
+
+ current_node = this ;
+ parent_node = this ;
+ cont = true ;
+ found = false ;
+ is_root = true ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux)
+ if (current_node.GetHas_Left()){
+ parent_node = current_node ;
+ current_node = current_node.GetLeft() ;
+ }
+ else cont = false ;
+ else
+ if (key_aux < v_key)
+ if (current_node.GetHas_Right()){
+ parent_node = current_node ;
+ current_node = current_node.GetRight() ;
+ }
+ else cont = false ;
+ else {
+ if (is_root)
+ if ((!current_node.GetHas_Right()) &&
+ (!current_node.GetHas_Left()) )
+ ntb = true ;
+ else
+ ntb = this.Remove(parent_node,current_node);
+ else ntb = this.Remove(parent_node,current_node);
+ found = true ;
+ cont = false ;
+ }
+ is_root = false ;
+ }
+ return found ;
+ }
+
+
+ // Check if the element to be removed will use the
+ // righ or left subtree if one exists
+ public boolean Remove(Tree p_node, Tree c_node){
+ boolean ntb ;
+ int auxkey1 ;
+ int auxkey2 ;
+
+ if (c_node.GetHas_Left())
+ ntb = this.RemoveLeft(p_node,c_node) ;
+ else
+ if (c_node.GetHas_Right())
+ ntb = this.RemoveRight(p_node,c_node) ;
+ else {
+ auxkey1 = c_node.GetKey();
+ //auxtree01 = p_node.GetLeft() ;
+ //auxkey2 = auxtree01.GetKey() ;
+ auxkey2 = (p_node.GetLeft()).GetKey() ;
+ if (this.Compare(auxkey1,auxkey2)) {
+ ntb = p_node.SetLeft(my_null);
+ ntb = p_node.SetHas_Left(false);
+ }
+ else {
+ ntb = p_node.SetRight(my_null);
+ ntb = p_node.SetHas_Right(false);
+ }
+ }
+ return true ;
+ }
+
+
+ // Copy the child key to the parent until a leaf is
+ // found and remove the leaf. This is done with the
+ // right subtree
+ public boolean RemoveRight(Tree p_node, Tree c_node){
+ boolean ntb ;
+
+ while (c_node.GetHas_Right()){
+ //auxtree01 = c_node.GetRight() ;
+ //auxint02 = auxtree01.GetKey();
+ //ntb = c_node.SetKey(auxint02);
+ ntb = c_node.SetKey((c_node.GetRight()).GetKey());
+ p_node = c_node ;
+ c_node = c_node.GetRight() ;
+ }
+ ntb = p_node.SetRight(my_null);
+ ntb = p_node.SetHas_Right(false);
+ return true ;
+ }
+
+
+ // Copy the child key to the parent until a leaf is
+ // found and remove the leaf. This is done with the
+ // left subtree
+ public boolean RemoveLeft(Tree p_node, Tree c_node){
+ boolean ntb ;
+
+ while (c_node.GetHas_Left()){
+ //auxtree01 = c_node.GetLeft() ;
+ //auxint02 = auxtree01.GetKey();
+ //ntb = c_node.SetKey(auxint02);
+ ntb = c_node.SetKey((c_node.GetLeft()).GetKey());
+ p_node = c_node ;
+ c_node = c_node.GetLeft() ;
+ }
+ ntb = p_node.SetLeft(my_null);
+ ntb = p_node.SetHas_Left(false);
+ return true ;
+ }
+
+ // Search for an elemnt in the tree
+ public int Search(int v_key){
+ boolean cont ;
+ int ifound ;
+ Tree current_node;
+ int key_aux ;
+
+ current_node = this ;
+ cont = true ;
+ ifound = 0 ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux)
+ if (current_node.GetHas_Left())
+ current_node = current_node.GetLeft() ;
+ else cont = false ;
+ else
+ if (key_aux < v_key)
+ if (current_node.GetHas_Right())
+ current_node = current_node.GetRight() ;
+ else cont = false ;
+ else {
+ ifound = 1 ;
+ cont = false ;
+ }
+ }
+ return ifound ;
+ }
+
+ // Invoke the method to really print the tree elements
+ public boolean Print(){
+ Tree current_node;
+ boolean ntb ;
+
+ current_node = this ;
+ ntb = this.RecPrint(current_node);
+ return true ;
+ }
+
+ // Print the elements of the tree
+ public boolean RecPrint(Tree node){
+ boolean ntb ;
+
+ if (node.GetHas_Left()){
+ //auxtree01 = node.GetLeft() ;
+ //ntb = this.RecPrint(auxtree01);
+ ntb = this.RecPrint(node.GetLeft());
+ } else ntb = true ;
+ System.out.println(node.GetKey());
+ if (node.GetHas_Right()){
+ //auxtree01 = node.GetRight() ;
+ //ntb = this.RecPrint(auxtree01);
+ ntb = this.RecPrint(node.GetRight());
+ } else ntb = true ;
+ return true ;
+ }
+
+}
+
diff --git a/typecheck/tests/BinaryTree.java b/typecheck/tests/BinaryTree.java
new file mode 100644
index 0000000..18d1464
--- /dev/null
+++ b/typecheck/tests/BinaryTree.java
@@ -0,0 +1,334 @@
+class BinaryTree{
+ public static void main(String[] a){
+ System.out.println(new BT().Start());
+ }
+}
+
+
+// This class invokes the methods to create a tree,
+// insert, delete and serach for elements on it
+class BT {
+
+ public int Start(){
+ Tree root ;
+ boolean ntb ;
+ int nti ;
+
+ root = new Tree();
+ ntb = root.Init(16);
+ ntb = root.Print();
+ System.out.println(100000000);
+ ntb = root.Insert(8) ;
+ ntb = root.Print();
+ ntb = root.Insert(24) ;
+ ntb = root.Insert(4) ;
+ ntb = root.Insert(12) ;
+ ntb = root.Insert(20) ;
+ ntb = root.Insert(28) ;
+ ntb = root.Insert(14) ;
+ ntb = root.Print();
+ System.out.println(root.Search(24));
+ System.out.println(root.Search(12));
+ System.out.println(root.Search(16));
+ System.out.println(root.Search(50));
+ System.out.println(root.Search(12));
+ ntb = root.Delete(12);
+ ntb = root.Print();
+ System.out.println(root.Search(12));
+
+ return 0 ;
+ }
+
+}
+
+class Tree{
+ Tree left ;
+ Tree right;
+ int key ;
+ boolean has_left ;
+ boolean has_right ;
+ Tree my_null ;
+
+ // Initialize a node with a key value and no children
+ public boolean Init(int v_key){
+ key = v_key ;
+ has_left = false ;
+ has_right = false ;
+ return true ;
+ }
+
+ // Update the right child with rn
+ public boolean SetRight(Tree rn){
+ right = rn ;
+ return true ;
+ }
+
+ // Update the left child with ln
+ public boolean SetLeft(Tree ln){
+ left = ln ;
+ return true ;
+ }
+
+ public Tree GetRight(){
+ return right ;
+ }
+
+ public Tree GetLeft(){
+ return left;
+ }
+
+ public int GetKey(){
+ return key ;
+ }
+
+ public boolean SetKey(int v_key){
+ key = v_key ;
+ return true ;
+ }
+
+ public boolean GetHas_Right(){
+ return has_right ;
+ }
+
+ public boolean GetHas_Left(){
+ return has_left ;
+ }
+
+ public boolean SetHas_Left(boolean val){
+ has_left = val ;
+ return true ;
+ }
+
+ public boolean SetHas_Right(boolean val){
+ has_right = val ;
+ return true ;
+ }
+
+ // This method compares two integers and
+ // returns true if they are equal and false
+ // otherwise
+ public boolean Compare(int num1 , int num2){
+ boolean ntb ;
+ int nti ;
+
+ ntb = false ;
+ nti = num2 + 1 ;
+ if (num1 < num2) ntb = false ;
+ else if (!(num1 < nti)) ntb = false ;
+ else ntb = true ;
+ return ntb ;
+ }
+
+
+ // Insert a new element in the tree
+ public boolean Insert(int v_key){
+ Tree new_node ;
+ boolean ntb ;
+ boolean cont ;
+ int key_aux ;
+ Tree current_node ;
+
+ new_node = new Tree();
+ ntb = new_node.Init(v_key) ;
+ current_node = this ;
+ cont = true ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux){
+ if (current_node.GetHas_Left())
+ current_node = current_node.GetLeft() ;
+ else {
+ cont = false ;
+ ntb = current_node.SetHas_Left(true);
+ ntb = current_node.SetLeft(new_node);
+ }
+ }
+ else{
+ if (current_node.GetHas_Right())
+ current_node = current_node.GetRight() ;
+ else {
+ cont = false ;
+ ntb = current_node.SetHas_Right(true);
+ ntb = current_node.SetRight(new_node);
+ }
+ }
+ }
+ return true ;
+ }
+
+
+ // Delete an element from the tree
+ public boolean Delete(int v_key){
+ Tree current_node ;
+ Tree parent_node ;
+ boolean cont ;
+ boolean found ;
+ boolean is_root ;
+ int key_aux ;
+ boolean ntb ;
+
+ current_node = this ;
+ parent_node = this ;
+ cont = true ;
+ found = false ;
+ is_root = true ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux)
+ if (current_node.GetHas_Left()){
+ parent_node = current_node ;
+ current_node = current_node.GetLeft() ;
+ }
+ else cont = false ;
+ else
+ if (key_aux < v_key)
+ if (current_node.GetHas_Right()){
+ parent_node = current_node ;
+ current_node = current_node.GetRight() ;
+ }
+ else cont = false ;
+ else {
+ if (is_root)
+ if ((!current_node.GetHas_Right()) &&
+ (!current_node.GetHas_Left()) )
+ ntb = true ;
+ else
+ ntb = this.Remove(parent_node,current_node);
+ else ntb = this.Remove(parent_node,current_node);
+ found = true ;
+ cont = false ;
+ }
+ is_root = false ;
+ }
+ return found ;
+ }
+
+
+ // Check if the element to be removed will use the
+ // righ or left subtree if one exists
+ public boolean Remove(Tree p_node, Tree c_node){
+ boolean ntb ;
+ int auxkey1 ;
+ int auxkey2 ;
+
+ if (c_node.GetHas_Left())
+ ntb = this.RemoveLeft(p_node,c_node) ;
+ else
+ if (c_node.GetHas_Right())
+ ntb = this.RemoveRight(p_node,c_node) ;
+ else {
+ auxkey1 = c_node.GetKey();
+ //auxtree01 = p_node.GetLeft() ;
+ //auxkey2 = auxtree01.GetKey() ;
+ auxkey2 = (p_node.GetLeft()).GetKey() ;
+ if (this.Compare(auxkey1,auxkey2)) {
+ ntb = p_node.SetLeft(my_null);
+ ntb = p_node.SetHas_Left(false);
+ }
+ else {
+ ntb = p_node.SetRight(my_null);
+ ntb = p_node.SetHas_Right(false);
+ }
+ }
+ return true ;
+ }
+
+
+ // Copy the child key to the parent until a leaf is
+ // found and remove the leaf. This is done with the
+ // right subtree
+ public boolean RemoveRight(Tree p_node, Tree c_node){
+ boolean ntb ;
+
+ while (c_node.GetHas_Right()){
+ //auxtree01 = c_node.GetRight() ;
+ //auxint02 = auxtree01.GetKey();
+ //ntb = c_node.SetKey(auxint02);
+ ntb = c_node.SetKey((c_node.GetRight()).GetKey());
+ p_node = c_node ;
+ c_node = c_node.GetRight() ;
+ }
+ ntb = p_node.SetRight(my_null);
+ ntb = p_node.SetHas_Right(false);
+ return true ;
+ }
+
+
+ // Copy the child key to the parent until a leaf is
+ // found and remove the leaf. This is done with the
+ // left subtree
+ public boolean RemoveLeft(Tree p_node, Tree c_node){
+ boolean ntb ;
+
+ while (c_node.GetHas_Left()){
+ //auxtree01 = c_node.GetLeft() ;
+ //auxint02 = auxtree01.GetKey();
+ //ntb = c_node.SetKey(auxint02);
+ ntb = c_node.SetKey((c_node.GetLeft()).GetKey());
+ p_node = c_node ;
+ c_node = c_node.GetLeft() ;
+ }
+ ntb = p_node.SetLeft(my_null);
+ ntb = p_node.SetHas_Left(false);
+ return true ;
+ }
+
+ // Search for an elemnt in the tree
+ public int Search(int v_key){
+ boolean cont ;
+ int ifound ;
+ Tree current_node;
+ int key_aux ;
+
+ current_node = this ;
+ cont = true ;
+ ifound = 0 ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux)
+ if (current_node.GetHas_Left())
+ current_node = current_node.GetLeft() ;
+ else cont = false ;
+ else
+ if (key_aux < v_key)
+ if (current_node.GetHas_Right())
+ current_node = current_node.GetRight() ;
+ else cont = false ;
+ else {
+ ifound = 1 ;
+ cont = false ;
+ }
+ }
+ return ifound ;
+ }
+
+ // Invoke the method to really print the tree elements
+ public boolean Print(){
+ Tree current_node;
+ boolean ntb ;
+
+ current_node = this ;
+ ntb = this.RecPrint(current_node);
+ return true ;
+ }
+
+ // Print the elements of the tree
+ public boolean RecPrint(Tree node){
+ boolean ntb ;
+
+ if (node.GetHas_Left()){
+ //auxtree01 = node.GetLeft() ;
+ //ntb = this.RecPrint(auxtree01);
+ ntb = this.RecPrint(node.GetLeft());
+ } else ntb = true ;
+ System.out.println(node.GetKey());
+ if (node.GetHas_Right()){
+ //auxtree01 = node.GetRight() ;
+ //ntb = this.RecPrint(auxtree01);
+ ntb = this.RecPrint(node.GetRight());
+ } else ntb = true ;
+ return true ;
+ }
+
+}
+
diff --git a/typecheck/tests/Branch-error.java b/typecheck/tests/Branch-error.java
new file mode 100644
index 0000000..bc58ff4
--- /dev/null
+++ b/typecheck/tests/Branch-error.java
@@ -0,0 +1,11 @@
+class Branch {
+ public static void main(String[] a) {
+ int a ;
+ a = 3 ;
+
+ if (a)
+ System.out.println(1) ;
+ else
+ System.out.println(2) ;
+ }
+}
diff --git a/typecheck/tests/Branch.java b/typecheck/tests/Branch.java
new file mode 100644
index 0000000..43df01f
--- /dev/null
+++ b/typecheck/tests/Branch.java
@@ -0,0 +1,11 @@
+class Branch {
+ public static void main(String[] a) {
+ boolean b ;
+ b = false ;
+
+ if (b)
+ System.out.println(1) ;
+ else
+ System.out.println(2) ;
+ }
+}
diff --git a/typecheck/tests/BubbleSort-error.java b/typecheck/tests/BubbleSort-error.java
new file mode 100644
index 0000000..97a1c1d
--- /dev/null
+++ b/typecheck/tests/BubbleSort-error.java
@@ -0,0 +1,93 @@
+class BubbleSort{
+ public static void main(String[] a){
+ System.out.println(new BBS().Start(10));
+ }
+}
+
+
+// This class contains the array of integers and
+// methods to initialize, print and sort the array
+// using Bublesort
+class BBS{
+
+ int[] number ;
+ int size ;
+
+ // Invoke the Initialization, Sort and Printing
+ // Methods
+ public int Start(int sz){
+ int aux01 ;
+ aux01 = this.Init(sz);
+ aux01 = this.Print();
+ System.out.println(99999);
+ aux01 = this.Sort();
+ aux01 = this.Print();
+ return 0 ;
+ }
+
+
+ // Sort array of integers using Bublesort method
+ public int Sort(){
+ int nt ;
+ int i ;
+ int aux02 ;
+ int aux04 ;
+ int aux05 ;
+ int aux06 ;
+ int aux07 ;
+ int j ;
+ int t ;
+ i = size - 1 ;
+ aux02 = 0 - 1 ;
+ while (aux02 < i) {
+ j = 1 ;
+ //aux03 = i+1 ;
+ while (j < (i+1)){
+ aux07 = j - 1 ;
+ aux04 = number[aux07] ;
+ aux05 = number[j] ;
+ if (aux05 < aux04) {
+ aux06 = j - 1 ;
+ t = number[aux06] ;
+ number[aux06] = number[j] ;
+ number[j] = t;
+ }
+ else nt = 0 ;
+ j = j + 1 ;
+ }
+ i = i - 1 ;
+ }
+ return 0 ;
+ }
+
+ // Printing method
+ public int Print(){
+ int j ;
+ j = 0 ;
+ while (j < (size)) {
+ System.out.println(number[j]);
+ j = j + 1 ;
+ }
+ return 0 ;
+ }
+
+ // Initialize array of integers
+ public int Init(int sz){
+ size = sz1 ; //TE
+ number = new int[sz] ;
+
+ number[0] = 20 ;
+ number[1] = 7 ;
+ number[2] = 12 ;
+ number[3] = 18 ;
+ number[4] = 2 ;
+ number[5] = 11 ;
+ number[6] = 6 ;
+ number[7] = 9 ;
+ number[8] = 19 ;
+ number[9] = 5 ;
+
+ return 0 ;
+ }
+
+}
diff --git a/typecheck/tests/BubbleSort.java b/typecheck/tests/BubbleSort.java
new file mode 100644
index 0000000..e5645a9
--- /dev/null
+++ b/typecheck/tests/BubbleSort.java
@@ -0,0 +1,93 @@
+class BubbleSort{
+ public static void main(String[] a){
+ System.out.println(new BBS().Start(10));
+ }
+}
+
+
+// This class contains the array of integers and
+// methods to initialize, print and sort the array
+// using Bublesort
+class BBS{
+
+ int[] number ;
+ int size ;
+
+ // Invoke the Initialization, Sort and Printing
+ // Methods
+ public int Start(int sz){
+ int aux01 ;
+ aux01 = this.Init(sz);
+ aux01 = this.Print();
+ System.out.println(99999);
+ aux01 = this.Sort();
+ aux01 = this.Print();
+ return 0 ;
+ }
+
+
+ // Sort array of integers using Bublesort method
+ public int Sort(){
+ int nt ;
+ int i ;
+ int aux02 ;
+ int aux04 ;
+ int aux05 ;
+ int aux06 ;
+ int aux07 ;
+ int j ;
+ int t ;
+ i = size - 1 ;
+ aux02 = 0 - 1 ;
+ while (aux02 < i) {
+ j = 1 ;
+ //aux03 = i+1 ;
+ while (j < (i+1)){
+ aux07 = j - 1 ;
+ aux04 = number[aux07] ;
+ aux05 = number[j] ;
+ if (aux05 < aux04) {
+ aux06 = j - 1 ;
+ t = number[aux06] ;
+ number[aux06] = number[j] ;
+ number[j] = t;
+ }
+ else nt = 0 ;
+ j = j + 1 ;
+ }
+ i = i - 1 ;
+ }
+ return 0 ;
+ }
+
+ // Printing method
+ public int Print(){
+ int j ;
+ j = 0 ;
+ while (j < (size)) {
+ System.out.println(number[j]);
+ j = j + 1 ;
+ }
+ return 0 ;
+ }
+
+ // Initialize array of integers
+ public int Init(int sz){
+ size = sz ;
+ number = new int[sz] ;
+
+ number[0] = 20 ;
+ number[1] = 7 ;
+ number[2] = 12 ;
+ number[3] = 18 ;
+ number[4] = 2 ;
+ number[5] = 11 ;
+ number[6] = 6 ;
+ number[7] = 9 ;
+ number[8] = 19 ;
+ number[9] = 5 ;
+
+ return 0 ;
+ }
+
+}
diff --git a/typecheck/tests/DeclareNone.java b/typecheck/tests/DeclareNone.java
new file mode 100644
index 0000000..0790056
--- /dev/null
+++ b/typecheck/tests/DeclareNone.java
@@ -0,0 +1,6 @@
+class DeclareNone{
+ public static void main(String[] a){
+ A a ; // 'A' does not exist
+ a = new A() ;
+ }
+}
diff --git a/typecheck/tests/Empty.java b/typecheck/tests/Empty.java
new file mode 100644
index 0000000..975ac63
--- /dev/null
+++ b/typecheck/tests/Empty.java
@@ -0,0 +1,5 @@
+class Empty {
+ public static void main(String[] a) {
+ System.out.println(1);
+ }
+}
diff --git a/typecheck/tests/Factorial-error.java b/typecheck/tests/Factorial-error.java
new file mode 100644
index 0000000..46ec59a
--- /dev/null
+++ b/typecheck/tests/Factorial-error.java
@@ -0,0 +1,16 @@
+class Factorial{
+ public static void main(String[] a){
+ System.out.println(new Fac().ComputeFac(10));
+ }
+}
+
+class Fac {
+ public boolean ComputeFac(int num){ //TE
+ int num_aux ;
+ if (num < 1)
+ num_aux = 1 ;
+ else
+ num_aux = num * (this.ComputeFac(num-1)) ;
+ return num_aux ;
+ }
+}
diff --git a/typecheck/tests/Factorial.java b/typecheck/tests/Factorial.java
new file mode 100644
index 0000000..d938bb6
--- /dev/null
+++ b/typecheck/tests/Factorial.java
@@ -0,0 +1,16 @@
+class Factorial{
+ public static void main(String[] a){
+ System.out.println(new Fac().ComputeFac(10));
+ }
+}
+
+class Fac {
+ public int ComputeFac(int num){
+ int num_aux ;
+ if (num < 1)
+ num_aux = 1 ;
+ else
+ num_aux = num * (this.ComputeFac(num-1)) ;
+ return num_aux ;
+ }
+}
diff --git a/typecheck/tests/IsPositive.java b/typecheck/tests/IsPositive.java
new file mode 100644
index 0000000..6adc30f
--- /dev/null
+++ b/typecheck/tests/IsPositive.java
@@ -0,0 +1,17 @@
+class IsPositive{
+ public static void main(String[] a){
+ System.out.println(new Positive().isPositive(10));
+ }
+}
+
+class Positive {
+ public bool IsPositive(int num){
+ bool positive ;
+
+ if (0 < num)
+ positive = true ;
+ else
+ positive = false ;
+ return positive ;
+ }
+}
diff --git a/typecheck/tests/LinearSearch-error.java b/typecheck/tests/LinearSearch-error.java
new file mode 100644
index 0000000..f8c48fe
--- /dev/null
+++ b/typecheck/tests/LinearSearch-error.java
@@ -0,0 +1,99 @@
+class LinearSearch{
+ public static void main(String[] a){
+ System.out.println(new LS().Start(10));
+ }
+}
+
+
+// This class contains an array of integers and
+// methods to initialize, print and search the array
+// using Linear Search
+class LS {
+ int number ; //TE
+ int size ;
+
+ // Invoke methods to initialize, print and search
+ // for elements on the array
+ public int Start(int sz){
+ int aux01 ;
+ int aux02 ;
+
+ aux01 = this.Init(sz);
+ aux02 = this.Print();
+ System.out.println(9999);
+ System.out.println(this.Search(8));
+ System.out.println(this.Search(12)) ;
+ System.out.println(this.Search(17)) ;
+ System.out.println(this.Search(50)) ;
+ return 55 ;
+ }
+
+ // Print array of integers
+ public int Print(){
+ int j ;
+
+ j = 1 ;
+ while (j < (size)) {
+ System.out.println(number[j]);
+ j = j + 1 ;
+ }
+ return 0 ;
+ }
+
+ // Search for a specific value (num) using
+ // linear search
+ public int Search(int num){
+ int j ;
+ boolean ls01 ;
+ int ifound ;
+ int aux01 ;
+ int aux02 ;
+ int nt ;
+
+ j = 1 ;
+ ls01 = false ;
+ ifound = 0 ;
+
+ //System.out.println(num);
+ while (j < (size)) {
+ aux01 = number[j] ;
+ aux02 = num + 1 ;
+ if (aux01 < num) nt = 0 ;
+ else if (!(aux01 < aux02)) nt = 0 ;
+ else {
+ ls01 = true ;
+ ifound = 1 ;
+ j = size ;
+ }
+ j = j + 1 ;
+ }
+
+ return ifound ;
+ }
+
+
+
+ // initialize array of integers with some
+ // some sequence
+ public int Init(int sz){
+ int j ;
+ int k ;
+ int aux01 ;
+ int aux02 ;
+
+ size = sz ;
+ number = new int[sz] ;
+
+ j = 1 ;
+ k = size + 1 ;
+ while (j < (size)) {
+ aux01 = 2 * j ;
+ aux02 = k - 3 ;
+ number[j] = aux01 + aux02 ;
+ j = j + 1 ;
+ k = k - 1 ;
+ }
+ return 0 ;
+ }
+
+}
diff --git a/typecheck/tests/LinearSearch.java b/typecheck/tests/LinearSearch.java
new file mode 100644
index 0000000..daddd94
--- /dev/null
+++ b/typecheck/tests/LinearSearch.java
@@ -0,0 +1,99 @@
+class LinearSearch{
+ public static void main(String[] a){
+ System.out.println(new LS().Start(10));
+ }
+}
+
+
+// This class contains an array of integers and
+// methods to initialize, print and search the array
+// using Linear Search
+class LS {
+ int[] number ;
+ int size ;
+
+ // Invoke methods to initialize, print and search
+ // for elements on the array
+ public int Start(int sz){
+ int aux01 ;
+ int aux02 ;
+
+ aux01 = this.Init(sz);
+ aux02 = this.Print();
+ System.out.println(9999);
+ System.out.println(this.Search(8));
+ System.out.println(this.Search(12)) ;
+ System.out.println(this.Search(17)) ;
+ System.out.println(this.Search(50)) ;
+ return 55 ;
+ }
+
+ // Print array of integers
+ public int Print(){
+ int j ;
+
+ j = 1 ;
+ while (j < (size)) {
+ System.out.println(number[j]);
+ j = j + 1 ;
+ }
+ return 0 ;
+ }
+
+ // Search for a specific value (num) using
+ // linear search
+ public int Search(int num){
+ int j ;
+ boolean ls01 ;
+ int ifound ;
+ int aux01 ;
+ int aux02 ;
+ int nt ;
+
+ j = 1 ;
+ ls01 = false ;
+ ifound = 0 ;
+
+ //System.out.println(num);
+ while (j < (size)) {
+ aux01 = number[j] ;
+ aux02 = num + 1 ;
+ if (aux01 < num) nt = 0 ;
+ else if (!(aux01 < aux02)) nt = 0 ;
+ else {
+ ls01 = true ;
+ ifound = 1 ;
+ j = size ;
+ }
+ j = j + 1 ;
+ }
+
+ return ifound ;
+ }
+
+
+
+ // initialize array of integers with some
+ // some sequence
+ public int Init(int sz){
+ int j ;
+ int k ;
+ int aux01 ;
+ int aux02 ;
+
+ size = sz ;
+ number = new int[sz] ;
+
+ j = 1 ;
+ k = size + 1 ;
+ while (j < (size)) {
+ aux01 = 2 * j ;
+ aux02 = k - 3 ;
+ number[j] = aux01 + aux02 ;
+ j = j + 1 ;
+ k = k - 1 ;
+ }
+ return 0 ;
+ }
+
+}
diff --git a/typecheck/tests/LinkedList-error.java b/typecheck/tests/LinkedList-error.java
new file mode 100644
index 0000000..181b599
--- /dev/null
+++ b/typecheck/tests/LinkedList-error.java
@@ -0,0 +1,278 @@
+class LinkedList{
+ public static void main(String[] a){
+ System.out.println(new LL().Start());
+ }
+}
+
+class Element {
+ int Age ;
+ int Salary ;
+ boolean Married ;
+
+ // Initialize some class variables
+ public boolean Init(int v_Age, int v_Salary, boolean v_Married){
+ Age = v_Age ;
+ Salary = v_Salary ;
+ Married = v_Married ;
+ return true ;
+ }
+
+ public int GetAge(){
+ return Age ;
+ }
+
+ public int GetSalary(){
+ return Salary ;
+ }
+
+ public boolean GetMarried(){
+ return Married ;
+ }
+
+ // This method returns true if the object "other"
+ // has the same values for age, salary and
+ public boolean Equal(Element other){
+ boolean ret_val ;
+ int aux01 ;
+ int aux02 ;
+ int nt ;
+ ret_val = true ;
+
+ aux01 = other.GetAge();
+ if (!this.Compare(aux01,Age)) ret_val = false ;
+ else {
+ aux02 = other.GetSalary();
+ if (!this.Compare(aux02,Salary)) ret_val = false ;
+ else
+ if (Married)
+ if (!other.GetMarried()) ret_val = false;
+ else nt = 0 ;
+ else
+ if (other.GetMarried()) ret_val = false;
+ else nt = 0 ;
+ }
+
+ return ret_val ;
+ }
+
+ // This method compares two integers and
+ // returns true if they are equal and false
+ // otherwise
+ public boolean Compare(int num1 , int num2){
+ boolean retval ;
+ int aux02 ;
+ retval = false ;
+ aux02 = num2 + 1 ;
+ if (num1 < num2) retval = false ;
+ else if (!(num1 < aux02)) retval = false ;
+ else retval = true ;
+ return retval ;
+ }
+
+}
+
+class List{
+ Element elem ;
+ List next ;
+ boolean end ;
+
+ // Initialize the node list as the last node
+ public boolean Init(){
+ end = true ;
+ return true ;
+ }
+
+ // Initialize the values of a new node
+ public boolean InitNew(Element v_elem, List v_next, boolean v_end){
+ end = v_end ;
+ elem = v_elem ;
+ next = v_next ;
+ return true ;
+ }
+
+ // Insert a new node at the beginning of the list
+ public List Insert(Element new_elem){
+ boolean ret_val ;
+ List aux03 ;
+ List aux02 ;
+ aux03 = this ;
+ aux02 = new List();
+ ret_val = aux02.InitNew(new_elem,aux03,false);
+ return aux02 ;
+ }
+
+
+ // Update the the pointer to the next node
+ public boolean SetNext(List v_next){
+ next = v_next ;
+ return 0 ; //TE
+ }
+
+ // Delete an element e from the list
+ public List Delete(Element e){
+ List my_head ;
+ boolean ret_val ;
+ boolean aux05;
+ List aux01 ;
+ List prev ;
+ boolean var_end ;
+ Element var_elem ;
+ int aux04 ;
+ int nt ;
+
+
+ my_head = this ;
+ ret_val = false ;
+ aux04 = 0 - 1 ;
+ aux01 = this ;
+ prev = this ;
+ var_end = end;
+ var_elem = elem ;
+ while ((!var_end) && (!ret_val)){
+ if (e.Equal(var_elem)){
+ ret_val = true ;
+ if (aux04 < 0) {
+ // delete first element
+ my_head = aux01.GetNext() ;
+ }
+ else{ // delete a non first element
+ System.out.println(0-555);
+ aux05 = prev.SetNext(aux01.GetNext());
+ System.out.println(0-555);
+
+ }
+ } else nt = 0 ;
+ if (!ret_val){
+ prev = aux01 ;
+ aux01 = aux01.GetNext() ;
+ var_end = aux01.GetEnd();
+ var_elem = aux01.GetElem();
+ aux04 = 1 ;
+ } else nt = 0 ;
+ }
+ return my_head ;
+ }
+
+
+ // Search for an element e on the list
+ public int Search(Element e){
+ int int_ret_val ;
+ List aux01 ;
+ Element var_elem ;
+ boolean var_end ;
+ int nt ;
+
+ int_ret_val = 0 ;
+ aux01 = this ;
+ var_end = end;
+ var_elem = elem ;
+ while (!var_end){
+ if (e.Equal(var_elem)){
+ int_ret_val = 1 ;
+ }
+ else nt = 0 ;
+ aux01 = aux01.GetNext() ;
+ var_end = aux01.GetEnd();
+ var_elem = aux01.GetElem();
+ }
+ return int_ret_val ;
+ }
+
+ public boolean GetEnd(){
+ return end ;
+ }
+
+ public Element GetElem(){
+ return elem ;
+ }
+
+ public List GetNext(){
+ return next ;
+ }
+
+
+ // Print the linked list
+ public boolean Print(){
+ List aux01 ;
+ boolean var_end ;
+ Element var_elem ;
+
+ aux01 = this ;
+ var_end = end ;
+ var_elem = elem ;
+ while (!var_end){
+ System.out.println(var_elem.GetAge());
+ aux01 = aux01.GetNext() ;
+ var_end = aux01.GetEnd();
+ var_elem = aux01.GetElem();
+ }
+
+ return true ;
+ }
+}
+
+
+// this class invokes the methods to insert, delete,
+// search and print the linked list
+class LL{
+
+ public int Start(){
+
+ List head ;
+ List last_elem ;
+ boolean aux01 ;
+ Element el01 ;
+ Element el02 ;
+ Element el03 ;
+
+ last_elem = new List();
+ aux01 = last_elem.Init();
+ head = last_elem ;
+ aux01 = head.Init();
+ aux01 = head.Print();
+
+ // inserting first element
+ el01 = new Element();
+ aux01 = el01.Init(25,37000,false);
+ head = head.Insert(el01);
+ aux01 = head.Print();
+ System.out.println(10000000);
+ // inserting second element
+ el01 = new Element();
+ aux01 = el01.Init(39,42000,true);
+ el02 = el01 ;
+ head = head.Insert(el01);
+ aux01 = head.Print();
+ System.out.println(10000000);
+ // inserting third element
+ el01 = new Element();
+ aux01 = el01.Init(22,34000,false);
+ head = head.Insert(el01);
+ aux01 = head.Print();
+ el03 = new Element();
+ aux01 = el03.Init(27,34000,false);
+ System.out.println(head.Search(el02));
+ System.out.println(head.Search(el03));
+ System.out.println(10000000);
+ // inserting fourth element
+ el01 = new Element();
+ aux01 = el01.Init(28,35000,false);
+ head = head.Insert(el01);
+ aux01 = head.Print();
+ System.out.println(2220000);
+
+ head = head.Delete(el02);
+ aux01 = head.Print();
+ System.out.println(33300000);
+
+
+ head = head.Delete(el01);
+ aux01 = head.Print();
+ System.out.println(44440000);
+
+ return 0 ;
+
+
+ }
+
+}
diff --git a/typecheck/tests/LinkedList.java b/typecheck/tests/LinkedList.java
new file mode 100644
index 0000000..69adc33
--- /dev/null
+++ b/typecheck/tests/LinkedList.java
@@ -0,0 +1,278 @@
+class LinkedList{
+ public static void main(String[] a){
+ System.out.println(new LL().Start());
+ }
+}
+
+class Element {
+ int Age ;
+ int Salary ;
+ boolean Married ;
+
+ // Initialize some class variables
+ public boolean Init(int v_Age, int v_Salary, boolean v_Married){
+ Age = v_Age ;
+ Salary = v_Salary ;
+ Married = v_Married ;
+ return true ;
+ }
+
+ public int GetAge(){
+ return Age ;
+ }
+
+ public int GetSalary(){
+ return Salary ;
+ }
+
+ public boolean GetMarried(){
+ return Married ;
+ }
+
+ // This method returns true if the object "other"
+ // has the same values for age, salary and
+ public boolean Equal(Element other){
+ boolean ret_val ;
+ int aux01 ;
+ int aux02 ;
+ int nt ;
+ ret_val = true ;
+
+ aux01 = other.GetAge();
+ if (!this.Compare(aux01,Age)) ret_val = false ;
+ else {
+ aux02 = other.GetSalary();
+ if (!this.Compare(aux02,Salary)) ret_val = false ;
+ else
+ if (Married)
+ if (!other.GetMarried()) ret_val = false;
+ else nt = 0 ;
+ else
+ if (other.GetMarried()) ret_val = false;
+ else nt = 0 ;
+ }
+
+ return ret_val ;
+ }
+
+ // This method compares two integers and
+ // returns true if they are equal and false
+ // otherwise
+ public boolean Compare(int num1 , int num2){
+ boolean retval ;
+ int aux02 ;
+ retval = false ;
+ aux02 = num2 + 1 ;
+ if (num1 < num2) retval = false ;
+ else if (!(num1 < aux02)) retval = false ;
+ else retval = true ;
+ return retval ;
+ }
+
+}
+
+class List{
+ Element elem ;
+ List next ;
+ boolean end ;
+
+ // Initialize the node list as the last node
+ public boolean Init(){
+ end = true ;
+ return true ;
+ }
+
+ // Initialize the values of a new node
+ public boolean InitNew(Element v_elem, List v_next, boolean v_end){
+ end = v_end ;
+ elem = v_elem ;
+ next = v_next ;
+ return true ;
+ }
+
+ // Insert a new node at the beginning of the list
+ public List Insert(Element new_elem){
+ boolean ret_val ;
+ List aux03 ;
+ List aux02 ;
+ aux03 = this ;
+ aux02 = new List();
+ ret_val = aux02.InitNew(new_elem,aux03,false);
+ return aux02 ;
+ }
+
+
+ // Update the the pointer to the next node
+ public boolean SetNext(List v_next){
+ next = v_next ;
+ return true ;
+ }
+
+ // Delete an element e from the list
+ public List Delete(Element e){
+ List my_head ;
+ boolean ret_val ;
+ boolean aux05;
+ List aux01 ;
+ List prev ;
+ boolean var_end ;
+ Element var_elem ;
+ int aux04 ;
+ int nt ;
+
+
+ my_head = this ;
+ ret_val = false ;
+ aux04 = 0 - 1 ;
+ aux01 = this ;
+ prev = this ;
+ var_end = end;
+ var_elem = elem ;
+ while ((!var_end) && (!ret_val)){
+ if (e.Equal(var_elem)){
+ ret_val = true ;
+ if (aux04 < 0) {
+ // delete first element
+ my_head = aux01.GetNext() ;
+ }
+ else{ // delete a non first element
+ System.out.println(0-555);
+ aux05 = prev.SetNext(aux01.GetNext());
+ System.out.println(0-555);
+
+ }
+ } else nt = 0 ;
+ if (!ret_val){
+ prev = aux01 ;
+ aux01 = aux01.GetNext() ;
+ var_end = aux01.GetEnd();
+ var_elem = aux01.GetElem();
+ aux04 = 1 ;
+ } else nt = 0 ;
+ }
+ return my_head ;
+ }
+
+
+ // Search for an element e on the list
+ public int Search(Element e){
+ int int_ret_val ;
+ List aux01 ;
+ Element var_elem ;
+ boolean var_end ;
+ int nt ;
+
+ int_ret_val = 0 ;
+ aux01 = this ;
+ var_end = end;
+ var_elem = elem ;
+ while (!var_end){
+ if (e.Equal(var_elem)){
+ int_ret_val = 1 ;
+ }
+ else nt = 0 ;
+ aux01 = aux01.GetNext() ;
+ var_end = aux01.GetEnd();
+ var_elem = aux01.GetElem();
+ }
+ return int_ret_val ;
+ }
+
+ public boolean GetEnd(){
+ return end ;
+ }
+
+ public Element GetElem(){
+ return elem ;
+ }
+
+ public List GetNext(){
+ return next ;
+ }
+
+
+ // Print the linked list
+ public boolean Print(){
+ List aux01 ;
+ boolean var_end ;
+ Element var_elem ;
+
+ aux01 = this ;
+ var_end = end ;
+ var_elem = elem ;
+ while (!var_end){
+ System.out.println(var_elem.GetAge());
+ aux01 = aux01.GetNext() ;
+ var_end = aux01.GetEnd();
+ var_elem = aux01.GetElem();
+ }
+
+ return true ;
+ }
+}
+
+
+// this class invokes the methods to insert, delete,
+// search and print the linked list
+class LL{
+
+ public int Start(){
+
+ List head ;
+ List last_elem ;
+ boolean aux01 ;
+ Element el01 ;
+ Element el02 ;
+ Element el03 ;
+
+ last_elem = new List();
+ aux01 = last_elem.Init();
+ head = last_elem ;
+ aux01 = head.Init();
+ aux01 = head.Print();
+
+ // inserting first element
+ el01 = new Element();
+ aux01 = el01.Init(25,37000,false);
+ head = head.Insert(el01);
+ aux01 = head.Print();
+ System.out.println(10000000);
+ // inserting second element
+ el01 = new Element();
+ aux01 = el01.Init(39,42000,true);
+ el02 = el01 ;
+ head = head.Insert(el01);
+ aux01 = head.Print();
+ System.out.println(10000000);
+ // inserting third element
+ el01 = new Element();
+ aux01 = el01.Init(22,34000,false);
+ head = head.Insert(el01);
+ aux01 = head.Print();
+ el03 = new Element();
+ aux01 = el03.Init(27,34000,false);
+ System.out.println(head.Search(el02));
+ System.out.println(head.Search(el03));
+ System.out.println(10000000);
+ // inserting fourth element
+ el01 = new Element();
+ aux01 = el01.Init(28,35000,false);
+ head = head.Insert(el01);
+ aux01 = head.Print();
+ System.out.println(2220000);
+
+ head = head.Delete(el02);
+ aux01 = head.Print();
+ System.out.println(33300000);
+
+
+ head = head.Delete(el01);
+ aux01 = head.Print();
+ System.out.println(44440000);
+
+ return 0 ;
+
+
+ }
+
+}
diff --git a/typecheck/tests/MoreThan4-error.java b/typecheck/tests/MoreThan4-error.java
new file mode 100644
index 0000000..9e8f63e
--- /dev/null
+++ b/typecheck/tests/MoreThan4-error.java
@@ -0,0 +1,29 @@
+class MoreThan4{
+ public static void main(String[] a){
+ System.out.println(new MT4().Start(1,2,3,4,5,6));
+ }
+}
+
+class MT4 {
+ public int Start(int p1, int p2, int p3 , int p4, int p5, int p6){
+ int aux ;
+ System.out.println(p1);
+ System.out.println(p2);
+ System.out.println(p3);
+ System.out.println(p4);
+ System.out.println(p5);
+ System.out.println(p6);
+ aux = this.Change(p6,p5,p4,p3,p2);//TE
+ return aux ;
+ }
+
+ public int Change(int p1, int p2, int p3 , int p4, int p5, int p6){
+ System.out.println(p1);
+ System.out.println(p2);
+ System.out.println(p3);
+ System.out.println(p4);
+ System.out.println(p5);
+ System.out.println(p6);
+ return 0 ;
+ }
+}
diff --git a/typecheck/tests/MoreThan4.java b/typecheck/tests/MoreThan4.java
new file mode 100644
index 0000000..4960f01
--- /dev/null
+++ b/typecheck/tests/MoreThan4.java
@@ -0,0 +1,29 @@
+class MoreThan4{
+ public static void main(String[] a){
+ System.out.println(new MT4().Start(1,2,3,4,5,6));
+ }
+}
+
+class MT4 {
+ public int Start(int p1, int p2, int p3 , int p4, int p5, int p6){
+ int aux ;
+ System.out.println(p1);
+ System.out.println(p2);
+ System.out.println(p3);
+ System.out.println(p4);
+ System.out.println(p5);
+ System.out.println(p6);
+ aux = this.Change(p6,p5,p4,p3,p2,p1);
+ return aux ;
+ }
+
+ public int Change(int p1, int p2, int p3 , int p4, int p5, int p6){
+ System.out.println(p1);
+ System.out.println(p2);
+ System.out.println(p3);
+ System.out.println(p4);
+ System.out.println(p5);
+ System.out.println(p6);
+ return 0 ;
+ }
+}
diff --git a/typecheck/tests/NewNone.java b/typecheck/tests/NewNone.java
new file mode 100644
index 0000000..7858853
--- /dev/null
+++ b/typecheck/tests/NewNone.java
@@ -0,0 +1,9 @@
+class NewNone{
+ public static void main(String[] a){
+ A a ;
+ a = new A() ;
+ }
+}
+
+class A {
+}
diff --git a/typecheck/tests/Printer-error.java b/typecheck/tests/Printer-error.java
new file mode 100644
index 0000000..3f6469f
--- /dev/null
+++ b/typecheck/tests/Printer-error.java
@@ -0,0 +1,5 @@
+class Printer{
+ public static void main(String[] a){
+ System.out.println(false);
+ }
+}
diff --git a/typecheck/tests/Printer.java b/typecheck/tests/Printer.java
new file mode 100644
index 0000000..3c4fa2f
--- /dev/null
+++ b/typecheck/tests/Printer.java
@@ -0,0 +1,5 @@
+class Printer{
+ public static void main(String[] a){
+ // System.out.println(0);
+ }
+}
diff --git a/typecheck/tests/QuickSort-error.java b/typecheck/tests/QuickSort-error.java
new file mode 100644
index 0000000..ff4ea2e
--- /dev/null
+++ b/typecheck/tests/QuickSort-error.java
@@ -0,0 +1,112 @@
+class QuickSort{
+ public static void main(String[] a){
+ System.out.println(new QS().Start(10));
+ }
+}
+
+
+// This class contains the array of integers and
+// methods to initialize, print and sort the array
+// using Quicksort
+class QS{
+
+ int number ; //TE
+ int size ;
+
+ // Invoke the Initialization, Sort and Printing
+ // Methods
+ public int Start(int sz){
+ int aux01 ;
+ aux01 = this.Init(sz);
+ aux01 = this.Print();
+ System.out.println(9999);
+ aux01 = size - 1 ;
+ aux01 = this.Sort(0,aux01);
+ aux01 = this.Print();
+ return 0 ;
+ }
+
+
+ // Sort array of integers using Quicksort method
+ public int Sort(int left, int right){
+ int v ;
+ int i ;
+ int j ;
+ int nt;
+ int t ;
+ boolean cont01;
+ boolean cont02;
+ int aux03 ;
+ t = 0 ;
+ if (left < right){
+ v = number[right] ;
+ i = left - 1 ;
+ j = right ;
+ cont01 = true ;
+ while (cont01){
+ cont02 = true ;
+ while (cont02){
+ i = i + 1 ;
+ aux03 = number[i] ;
+ if (!(aux03<v)) cont02 = false ;
+ else cont02 = true ;
+ }
+ cont02 = true ;
+ while (cont02){
+ j = j - 1 ;
+ aux03 = number[j] ;
+ if (!(v < aux03)) cont02 = false ;
+ else cont02 = true ;
+ }
+
+
+ t = number[i] ;
+ number[i] = number[j] ;
+ number[j] = t ;
+ //aux03 = i + 1 ;
+ if ( j < (i+1)) cont01 = false ;
+ else cont01 = true ;
+ }
+ number[j] = number[i] ;
+ number[i] = number[right] ;
+ number[right] = t ;
+ nt = this.Sort(left,i-1);
+ nt = this.Sort(i+1,right);
+ }
+ else nt = 0 ;
+ return 0 ;
+ }
+
+
+ // Print array of integers
+ public int Print(){
+ int j ;
+ j = 0 ;
+ while (j < (size)) {
+ System.out.println(number[j]);
+ j = j + 1 ;
+ }
+ return 0 ;
+ }
+
+
+ // Initialize array of integers
+ public int Init(int sz){
+ size = sz ;
+ number = new int[sz] ;
+
+ number[0] = 20 ;
+ number[1] = 7 ;
+ number[2] = 12 ;
+ number[3] = 18 ;
+ number[4] = 2 ;
+ number[5] = 11 ;
+ number[6] = 6 ;
+ number[7] = 9 ;
+ number[8] = 19 ;
+ number[9] = 5 ;
+
+ return 0 ;
+ }
+
+}
diff --git a/typecheck/tests/QuickSort.java b/typecheck/tests/QuickSort.java
new file mode 100644
index 0000000..5893390
--- /dev/null
+++ b/typecheck/tests/QuickSort.java
@@ -0,0 +1,112 @@
+class QuickSort{
+ public static void main(String[] a){
+ System.out.println(new QS().Start(10));
+ }
+}
+
+
+// This class contains the array of integers and
+// methods to initialize, print and sort the array
+// using Quicksort
+class QS{
+
+ int[] number ;
+ int size ;
+
+ // Invoke the Initialization, Sort and Printing
+ // Methods
+ public int Start(int sz){
+ int aux01 ;
+ aux01 = this.Init(sz);
+ aux01 = this.Print();
+ System.out.println(9999);
+ aux01 = size - 1 ;
+ aux01 = this.Sort(0,aux01);
+ aux01 = this.Print();
+ return 0 ;
+ }
+
+
+ // Sort array of integers using Quicksort method
+ public int Sort(int left, int right){
+ int v ;
+ int i ;
+ int j ;
+ int nt;
+ int t ;
+ boolean cont01;
+ boolean cont02;
+ int aux03 ;
+ t = 0 ;
+ if (left < right){
+ v = number[right] ;
+ i = left - 1 ;
+ j = right ;
+ cont01 = true ;
+ while (cont01){
+ cont02 = true ;
+ while (cont02){
+ i = i + 1 ;
+ aux03 = number[i] ;
+ if (!(aux03<v)) cont02 = false ;
+ else cont02 = true ;
+ }
+ cont02 = true ;
+ while (cont02){
+ j = j - 1 ;
+ aux03 = number[j] ;
+ if (!(v < aux03)) cont02 = false ;
+ else cont02 = true ;
+ }
+
+
+ t = number[i] ;
+ number[i] = number[j] ;
+ number[j] = t ;
+ //aux03 = i + 1 ;
+ if ( j < (i+1)) cont01 = false ;
+ else cont01 = true ;
+ }
+ number[j] = number[i] ;
+ number[i] = number[right] ;
+ number[right] = t ;
+ nt = this.Sort(left,i-1);
+ nt = this.Sort(i+1,right);
+ }
+ else nt = 0 ;
+ return 0 ;
+ }
+
+
+ // Print array of integers
+ public int Print(){
+ int j ;
+ j = 0 ;
+ while (j < (size)) {
+ System.out.println(number[j]);
+ j = j + 1 ;
+ }
+ return 0 ;
+ }
+
+
+ // Initialize array of integers
+ public int Init(int sz){
+ size = sz ;
+ number = new int[sz] ;
+
+ number[0] = 20 ;
+ number[1] = 7 ;
+ number[2] = 12 ;
+ number[3] = 18 ;
+ number[4] = 2 ;
+ number[5] = 11 ;
+ number[6] = 6 ;
+ number[7] = 9 ;
+ number[8] = 19 ;
+ number[9] = 5 ;
+
+ return 0 ;
+ }
+
+}
diff --git a/typecheck/tests/RetrieveInteger.java b/typecheck/tests/RetrieveInteger.java
new file mode 100644
index 0000000..036ad8b
--- /dev/null
+++ b/typecheck/tests/RetrieveInteger.java
@@ -0,0 +1,17 @@
+class RetrieveInteger{
+ public static void main(String[] a){
+ GetInteger i ;
+ int j ;
+
+ i = new GetInteger() ;
+ j = i.GetInteger() ;
+
+ System.out.println(j) ;
+ }
+}
+
+class GetInteger {
+ public int Get(){
+ return 4 ;
+ }
+}
diff --git a/typecheck/tests/SimpleArithmetic.java b/typecheck/tests/SimpleArithmetic.java
new file mode 100644
index 0000000..94c780e
--- /dev/null
+++ b/typecheck/tests/SimpleArithmetic.java
@@ -0,0 +1,6 @@
+class SimpleArithmetic{
+ public static void main(String[] a){
+ int x;
+ x = 1 + 2;
+ }
+}
diff --git a/typecheck/tests/SimpleArray-error.java b/typecheck/tests/SimpleArray-error.java
new file mode 100644
index 0000000..61ff06f
--- /dev/null
+++ b/typecheck/tests/SimpleArray-error.java
@@ -0,0 +1,6 @@
+class SimpleArithmetic{
+ public static void main(String[] a){
+ int x;
+ x[0] = 2;
+ }
+}
diff --git a/typecheck/tests/SimpleArray.java b/typecheck/tests/SimpleArray.java
new file mode 100644
index 0000000..9a98d99
--- /dev/null
+++ b/typecheck/tests/SimpleArray.java
@@ -0,0 +1,6 @@
+class SimpleArithmetic{
+ public static void main(String[] a){
+ int[] x;
+ x[0] = 2;
+ }
+}
diff --git a/typecheck/tests/TreeVisitor-error.java b/typecheck/tests/TreeVisitor-error.java
new file mode 100644
index 0000000..b41f3d1
--- /dev/null
+++ b/typecheck/tests/TreeVisitor-error.java
@@ -0,0 +1,376 @@
+// The classes are basically the same as the BinaryTree
+// file except the visitor classes and the accept method
+// in the Tree class
+
+class TreeVisitor{
+ public static void main(String[] a){
+ System.out.println(new TV().Start());
+ }
+}
+
+class TV {
+
+ public int Start(){
+ Tree root ;
+ boolean ntb ;
+ int nti ;
+ MyVisitor v ;
+
+ root = new Tree();
+ ntb = root.Init(16);
+ ntb = root.Print();
+ System.out.println(100000000);
+ ntb = root.Insert(8) ;
+ ntb = root.Insert(24) ;
+ ntb = root.Insert(4) ;
+ ntb = root.Insert(12) ;
+ ntb = root.Insert(20) ;
+ ntb = root.Insert(28) ;
+ ntb = root.Insert(14) ;
+ ntb = root.Print();
+ System.out.println(100000000);
+ v = new MyVisitor();
+ System.out.println(50000000);
+ nti = root.accept(v);
+ System.out.println(100000000);
+ System.out.println(root.Search(24));
+ System.out.println(root.Search(12));
+ System.out.println(root.Search(16));
+ System.out.println(root.Search(50));
+ System.out.println(root.Search(12));
+ ntb = root.Delete(12);
+ ntb = root.Print();
+ System.out.println(root.Search(12));
+
+ return 0 ;
+ }
+
+}
+
+
+class Tree{
+ Tree left ;
+ Tree right;
+ int key ;
+ boolean has_left ;
+ boolean has_right ;
+ Tree my_null ;
+
+
+
+ //Tree new_node ;
+ //Tree current_node ;
+ //Tree parent_node ;
+
+ // boolean ntb ;
+ //boolean cont ;
+ //boolean found ;
+ //int ifound ;
+ // boolean is_root ;
+ // int nti ;
+ // int key_aux ;
+ // int auxkey1 ;
+ // int auxkey2 ;
+
+ public boolean Init(int v_key){
+ key = v_key ;
+ has_left = false ;
+ has_right = false ;
+ return true ;
+ }
+
+ public boolean SetRight(Tree rn){
+ right = rn ;
+ return true ;
+ }
+
+ public boolean SetLeft(Tree ln){
+ left = ln ;
+ return true ;
+ }
+
+ public Tree GetRight(){
+ return right ;
+ }
+
+ public Tree GetLeft(){
+ return left;
+ }
+
+ public int GetKey(){
+ return key ;
+ }
+
+ public boolean SetKey(int v_key){
+ key = v_key ;
+ return true ;
+ }
+
+ public boolean GetHas_Right(){
+ return has_right ;
+ }
+
+ public boolean GetHas_Left(){
+ return has_left ;
+ }
+
+ public boolean SetHas_Left(boolean val){
+ has_left = val ;
+ return true ;
+ }
+
+ public boolean SetHas_Right(boolean val){
+ has_right = val ;
+ return true ;
+ }
+
+ public boolean Compare(int num1 , int num2){
+ boolean ntb ;
+ int nti ;
+
+ ntb = false ;
+ nti = num2 + 1 ;
+ if (num1 < num2) ntb = false ;
+ else if (!(num1 < nti)) ntb = false ;
+ else ntb = true ;
+ return ntb ;
+ }
+
+ public boolean Insert(int v_key){
+ Tree new_node ;
+ boolean ntb ;
+ Tree current_node ;
+ boolean cont ;
+ int key_aux ;
+
+ new_node = new Tree();
+ ntb = new_node.Init(v_key) ;
+ current_node = this ;
+ cont = true ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux){
+ if (current_node.GetHas_Left())
+ current_node = current_node.GetLeft() ;
+ else {
+ cont = false ;
+ ntb = current_node.SetHas_Left(true);
+ ntb = current_node.SetLeft(new_node);
+ }
+ }
+ else{
+ if (current_node.GetHas_Right())
+ current_node = current_node.GetRight() ;
+ else {
+ cont = false ;
+ ntb = current_node.SetHas_Right(true);
+ ntb = current_node.SetRight(new_node);
+ }
+ }
+ }
+ return true ;
+ }
+
+ public boolean Delete(int v_key){
+ Tree current_node ;
+ Tree parent_node ;
+ boolean cont ;
+ boolean found ;
+ boolean ntb ;
+ boolean is_root ;
+ int key_aux ;
+
+ current_node = this ;
+ parent_node = this ;
+ cont = true ;
+ found = false ;
+ is_root = true ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux)
+ if (current_node.GetHas_Left()){
+ parent_node = current_node ;
+ current_node = current_node.GetLeft() ;
+ }
+ else cont = false ;
+ else
+ if (key_aux < v_key)
+ if (current_node.GetHas_Right()){
+ parent_node = current_node ;
+ current_node = current_node.GetRight() ;
+ }
+ else cont = false ;
+ else {
+ if (is_root)
+ if (!current_node.GetHas_Right() &&
+ !current_node.GetHas_Left() )
+ ntb = true ;
+ else
+ ntb = this.Remove(parent_node,current_node);
+ else ntb = this.Remove(parent_node,current_node);
+ found = true ;
+ cont = false ;
+ }
+ is_root = false ;
+ }
+ return found ;
+ }
+
+ public boolean Remove(Tree p_node, Tree c_node){
+ boolean ntb ;
+ int auxkey1 ;
+ int auxkey2 ;
+
+ if (c_node.GetHas_Left())
+ ntb = this.RemoveLeft(p_node,c_node) ;
+ else
+ if (c_node.GetHas_Right())
+ ntb = this.RemoveRight(p_node,c_node) ;
+ else {
+ auxkey1 = c_node.GetKey();
+ auxkey2 = (p_node.GetLeft()).GetKey() ;
+ if (this.Compare(auxkey1,auxkey2)) {
+ ntb = p_node.SetLeft(my_null);
+ ntb = p_node.SetHas_Left(false);
+ }
+ else {
+ ntb = p_node.SetRight(my_null);
+ ntb = p_node.SetHas_Right(false);
+ }
+ }
+ return true ;
+ }
+
+ public boolean RemoveRight(Tree p_node, Tree c_node){
+ boolean ntb ;
+ while (c_node.GetHas_Right()){
+ ntb = c_node.SetKey((c_node.GetRight()).GetKey());
+ p_node = c_node ;
+ c_node = c_node.GetRight() ;
+ }
+ ntb = p_node.SetRight(my_null);
+ ntb = p_node.SetHas_Right(false);
+ return true ;
+ }
+
+ public boolean RemoveLeft(Tree p_node, Tree c_node){
+ boolean ntb ;
+ while (c_node.GetHas_Left()){
+ ntb = c_node.SetKey((c_node.GetLeft()).GetKey());
+ p_node = c_node ;
+ c_node = c_node.GetLeft() ;
+ }
+ ntb = p_node.SetLeft(my_null);
+ ntb = p_node.SetHas_Left(false);
+ return true ;
+ }
+
+
+ public int Search(int v_key){
+ Tree current_node ;
+ int ifound ;
+ boolean cont ;
+ int key_aux ;
+
+ current_node = this ;
+ cont = true ;
+ ifound = 0 ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux)
+ if (current_node.GetHas_Left())
+ current_node = current_node.GetLeft() ;
+ else cont = false ;
+ else
+ if (key_aux < v_key)
+ if (current_node.GetHas_Right())
+ current_node = current_node.GetRight() ;
+ else cont = false ;
+ else {
+ ifound = 1 ;
+ cont = false ;
+ }
+ }
+ return ifound ;
+ }
+
+ public boolean Print(){
+ boolean ntb ;
+ Tree current_node ;
+
+ current_node = this ;
+ ntb = this.RecPrint(current_node);
+ return true ;
+ }
+
+ public boolean RecPrint(Tree node){
+ boolean ntb ;
+
+ if (node.GetHas_Left()){
+ ntb = this.RecPrint(node.GetLeft());
+ } else ntb = true ;
+ System.out.println(node.GetKey());
+ if (node.GetHas_Right()){
+ ntb = this.RecPrint(node.GetRight());
+ } else ntb = true ;
+ return true ;
+ }
+
+ public int accept(Visitor v){
+ int nti ;
+
+ System.out.println(333);
+ nti = v.visit(this) ;
+ return 0 ;
+ }
+
+}
+
+
+
+class Visitor {
+ Tree l ;
+ //Tree r ; //TE
+
+ public int visit(Tree n){
+ int nti ;
+
+ if (n.GetHas_Right()){
+ r = n.GetRight() ;
+ nti = r.accept(this) ; }
+ else nti = 0 ;
+
+ if (n.GetHas_Left()) {
+ l = n.GetLeft();
+ nti = l.accept(this) ; }
+ else nti = 0 ;
+
+ return 0;
+ }
+
+}
+
+
+class MyVisitor extends Visitor {
+
+ Tree r;
+
+ public int visit(Tree n){
+ int nti ;
+
+ if (n.GetHas_Right()){
+ r = n.GetRight() ;
+ nti = r.accept(this) ; }
+ else nti = 0 ;
+
+ System.out.println(n.GetKey());
+
+ if (n.GetHas_Left()) {
+ l = n.GetLeft();
+ nti =l.accept(this) ; }
+ else nti = 0 ;
+
+ return 0;
+ }
+
+}
diff --git a/typecheck/tests/TreeVisitor.java b/typecheck/tests/TreeVisitor.java
new file mode 100644
index 0000000..8debfe6
--- /dev/null
+++ b/typecheck/tests/TreeVisitor.java
@@ -0,0 +1,374 @@
+// The classes are basically the same as the BinaryTree
+// file except the visitor classes and the accept method
+// in the Tree class
+
+class TreeVisitor{
+ public static void main(String[] a){
+ System.out.println(new TV().Start());
+ }
+}
+
+class TV {
+
+ public int Start(){
+ Tree root ;
+ boolean ntb ;
+ int nti ;
+ MyVisitor v ;
+
+ root = new Tree();
+ ntb = root.Init(16);
+ ntb = root.Print();
+ System.out.println(100000000);
+ ntb = root.Insert(8) ;
+ ntb = root.Insert(24) ;
+ ntb = root.Insert(4) ;
+ ntb = root.Insert(12) ;
+ ntb = root.Insert(20) ;
+ ntb = root.Insert(28) ;
+ ntb = root.Insert(14) ;
+ ntb = root.Print();
+ System.out.println(100000000);
+ v = new MyVisitor();
+ System.out.println(50000000);
+ nti = root.accept(v);
+ System.out.println(100000000);
+ System.out.println(root.Search(24));
+ System.out.println(root.Search(12));
+ System.out.println(root.Search(16));
+ System.out.println(root.Search(50));
+ System.out.println(root.Search(12));
+ ntb = root.Delete(12);
+ ntb = root.Print();
+ System.out.println(root.Search(12));
+
+ return 0 ;
+ }
+
+}
+
+
+class Tree{
+ Tree left ;
+ Tree right;
+ int key ;
+ boolean has_left ;
+ boolean has_right ;
+ Tree my_null ;
+
+
+
+ //Tree new_node ;
+ //Tree current_node ;
+ //Tree parent_node ;
+
+ // boolean ntb ;
+ //boolean cont ;
+ //boolean found ;
+ //int ifound ;
+ // boolean is_root ;
+ // int nti ;
+ // int key_aux ;
+ // int auxkey1 ;
+ // int auxkey2 ;
+
+ public boolean Init(int v_key){
+ key = v_key ;
+ has_left = false ;
+ has_right = false ;
+ return true ;
+ }
+
+ public boolean SetRight(Tree rn){
+ right = rn ;
+ return true ;
+ }
+
+ public boolean SetLeft(Tree ln){
+ left = ln ;
+ return true ;
+ }
+
+ public Tree GetRight(){
+ return right ;
+ }
+
+ public Tree GetLeft(){
+ return left;
+ }
+
+ public int GetKey(){
+ return key ;
+ }
+
+ public boolean SetKey(int v_key){
+ key = v_key ;
+ return true ;
+ }
+
+ public boolean GetHas_Right(){
+ return has_right ;
+ }
+
+ public boolean GetHas_Left(){
+ return has_left ;
+ }
+
+ public boolean SetHas_Left(boolean val){
+ has_left = val ;
+ return true ;
+ }
+
+ public boolean SetHas_Right(boolean val){
+ has_right = val ;
+ return true ;
+ }
+
+ public boolean Compare(int num1 , int num2){
+ boolean ntb ;
+ int nti ;
+
+ ntb = false ;
+ nti = num2 + 1 ;
+ if (num1 < num2) ntb = false ;
+ else if (!(num1 < nti)) ntb = false ;
+ else ntb = true ;
+ return ntb ;
+ }
+
+ public boolean Insert(int v_key){
+ Tree new_node ;
+ boolean ntb ;
+ Tree current_node ;
+ boolean cont ;
+ int key_aux ;
+
+ new_node = new Tree();
+ ntb = new_node.Init(v_key) ;
+ current_node = this ;
+ cont = true ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux){
+ if (current_node.GetHas_Left())
+ current_node = current_node.GetLeft() ;
+ else {
+ cont = false ;
+ ntb = current_node.SetHas_Left(true);
+ ntb = current_node.SetLeft(new_node);
+ }
+ }
+ else{
+ if (current_node.GetHas_Right())
+ current_node = current_node.GetRight() ;
+ else {
+ cont = false ;
+ ntb = current_node.SetHas_Right(true);
+ ntb = current_node.SetRight(new_node);
+ }
+ }
+ }
+ return true ;
+ }
+
+ public boolean Delete(int v_key){
+ Tree current_node ;
+ Tree parent_node ;
+ boolean cont ;
+ boolean found ;
+ boolean ntb ;
+ boolean is_root ;
+ int key_aux ;
+
+ current_node = this ;
+ parent_node = this ;
+ cont = true ;
+ found = false ;
+ is_root = true ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux)
+ if (current_node.GetHas_Left()){
+ parent_node = current_node ;
+ current_node = current_node.GetLeft() ;
+ }
+ else cont = false ;
+ else
+ if (key_aux < v_key)
+ if (current_node.GetHas_Right()){
+ parent_node = current_node ;
+ current_node = current_node.GetRight() ;
+ }
+ else cont = false ;
+ else {
+ if (is_root)
+ if (!current_node.GetHas_Right() &&
+ !current_node.GetHas_Left() )
+ ntb = true ;
+ else
+ ntb = this.Remove(parent_node,current_node);
+ else ntb = this.Remove(parent_node,current_node);
+ found = true ;
+ cont = false ;
+ }
+ is_root = false ;
+ }
+ return found ;
+ }
+
+ public boolean Remove(Tree p_node, Tree c_node){
+ boolean ntb ;
+ int auxkey1 ;
+ int auxkey2 ;
+
+ if (c_node.GetHas_Left())
+ ntb = this.RemoveLeft(p_node,c_node) ;
+ else
+ if (c_node.GetHas_Right())
+ ntb = this.RemoveRight(p_node,c_node) ;
+ else {
+ auxkey1 = c_node.GetKey();
+ auxkey2 = (p_node.GetLeft()).GetKey() ;
+ if (this.Compare(auxkey1,auxkey2)) {
+ ntb = p_node.SetLeft(my_null);
+ ntb = p_node.SetHas_Left(false);
+ }
+ else {
+ ntb = p_node.SetRight(my_null);
+ ntb = p_node.SetHas_Right(false);
+ }
+ }
+ return true ;
+ }
+
+ public boolean RemoveRight(Tree p_node, Tree c_node){
+ boolean ntb ;
+ while (c_node.GetHas_Right()){
+ ntb = c_node.SetKey((c_node.GetRight()).GetKey());
+ p_node = c_node ;
+ c_node = c_node.GetRight() ;
+ }
+ ntb = p_node.SetRight(my_null);
+ ntb = p_node.SetHas_Right(false);
+ return true ;
+ }
+
+ public boolean RemoveLeft(Tree p_node, Tree c_node){
+ boolean ntb ;
+ while (c_node.GetHas_Left()){
+ ntb = c_node.SetKey((c_node.GetLeft()).GetKey());
+ p_node = c_node ;
+ c_node = c_node.GetLeft() ;
+ }
+ ntb = p_node.SetLeft(my_null);
+ ntb = p_node.SetHas_Left(false);
+ return true ;
+ }
+
+
+ public int Search(int v_key){
+ Tree current_node ;
+ int ifound ;
+ boolean cont ;
+ int key_aux ;
+
+ current_node = this ;
+ cont = true ;
+ ifound = 0 ;
+ while (cont){
+ key_aux = current_node.GetKey();
+ if (v_key < key_aux)
+ if (current_node.GetHas_Left())
+ current_node = current_node.GetLeft() ;
+ else cont = false ;
+ else
+ if (key_aux < v_key)
+ if (current_node.GetHas_Right())
+ current_node = current_node.GetRight() ;
+ else cont = false ;
+ else {
+ ifound = 1 ;
+ cont = false ;
+ }
+ }
+ return ifound ;
+ }
+
+ public boolean Print(){
+ boolean ntb ;
+ Tree current_node ;
+
+ current_node = this ;
+ ntb = this.RecPrint(current_node);
+ return true ;
+ }
+
+ public boolean RecPrint(Tree node){
+ boolean ntb ;
+
+ if (node.GetHas_Left()){
+ ntb = this.RecPrint(node.GetLeft());
+ } else ntb = true ;
+ System.out.println(node.GetKey());
+ if (node.GetHas_Right()){
+ ntb = this.RecPrint(node.GetRight());
+ } else ntb = true ;
+ return true ;
+ }
+
+ public int accept(Visitor v){
+ int nti ;
+
+ System.out.println(333);
+ nti = v.visit(this) ;
+ return 0 ;
+ }
+
+}
+
+
+
+class Visitor {
+ Tree l ;
+ Tree r ;
+
+ public int visit(Tree n){
+ int nti ;
+
+ if (n.GetHas_Right()){
+ r = n.GetRight() ;
+ nti = r.accept(this) ; }
+ else nti = 0 ;
+
+ if (n.GetHas_Left()) {
+ l = n.GetLeft();
+ nti = l.accept(this) ; }
+ else nti = 0 ;
+
+ return 0;
+ }
+
+}
+
+
+class MyVisitor extends Visitor {
+
+ public int visit(Tree n){
+ int nti ;
+
+ if (n.GetHas_Right()){
+ r = n.GetRight() ;
+ nti = r.accept(this) ; }
+ else nti = 0 ;
+
+ System.out.println(n.GetKey());
+
+ if (n.GetHas_Left()) {
+ l = n.GetLeft();
+ nti =l.accept(this) ; }
+ else nti = 0 ;
+
+ return 0;
+ }
+
+}