From 7c6c79a437a4c1e7cf85964d005a3cdeb59809f1 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Fri, 26 Apr 2024 17:01:49 -0600 Subject: Added skeleton files in "heat" libary, successor to TypeCheckSimp --- Makefile | 19 - Typecheck.java | 30 +- boil/BoilVisitor.java | 6 +- heat/HeatVisitor.java | 998 ++++++++++++++++++++++++++++++++++++++ heat/TypeBundle.java | 41 ++ heat/TypeCheckSimp.java | 1097 ++++++++++++++++++++++++++++++++++++++++++ heat/TypecheckException.java | 9 + runner.sh | 12 +- st/ClassInstance.java | 6 +- st/TypeInstance.java | 19 - test.sh | 4 +- typecheck/TypeCheckSimp.java | 1097 ------------------------------------------ 12 files changed, 2175 insertions(+), 1163 deletions(-) delete mode 100644 Makefile create mode 100644 heat/HeatVisitor.java create mode 100644 heat/TypeBundle.java create mode 100644 heat/TypeCheckSimp.java create mode 100644 heat/TypecheckException.java delete mode 100644 typecheck/TypeCheckSimp.java diff --git a/Makefile b/Makefile deleted file mode 100644 index 06b1ced..0000000 --- a/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -CC=javac - -FILES = Typecheck.java -OUT_EXE = Typecheck.class - -all: $(OUT_EXE) - -$(OUT_EXE): $(FILES) - $(CC) -g $(FILES) - - -run: - $(SHELL) run_tests.sh - -clean: - $(RM) *.class *.tar.gz - -# package: -# tar -czvf dunaisky-benjamin-hw1.tar.gz README.txt Makefile Bundle.java Descend.java Lex.java manifest.scm Parse.java Read.java run_tests.sh tests/* Token.java hw1-grammar.pdf diff --git a/Typecheck.java b/Typecheck.java index 8c4559b..d153f7a 100644 --- a/Typecheck.java +++ b/Typecheck.java @@ -5,7 +5,7 @@ import syntaxtree.*; import java.util.*; import st.*; import misc.*; -import typecheck.*; +import heat.*; public class Typecheck { public static void main(String[] args) { @@ -17,27 +17,29 @@ public class Typecheck { // GJDepthFirst. R=Void, A=String. PPrinter pp = new PPrinter(); root.accept(pp, ""); - MinimalLogger.info("==================================================="); // Build the symbol table. Top-down visitor, inherits from // GJDepthFirst. R=Void, A=Integer. SymbolTable symt = new SymbolTable(); - root.accept(new SymTableBottomUp(), symt); - root.accept(new SymTableTopDown(), symt); - MinimalLogger.info("==================================================="); + MinimalLogger.info("Populating classes..."); + root.accept(new SymTableClasses(), symt); + MinimalLogger.info("Populating methods..."); + root.accept(new SymTableMethods(), symt); + MinimalLogger.info("Populating variables..."); + root.accept(new SymTableVars(), symt); + MinimalLogger.info("Populating extensions..."); + root.accept(new SymTableExtend(), symt); + MinimalLogger.info(symt.toString()); - TypeCheckSimp ts = new TypeCheckSimp(); - TypeInstance res = root.accept(ts, symt); + HeatVisitor hv = new HeatVisitor(symt); - // Ugly code not to be inspired from: "my" way of storing - // type info / typecheck property: if some of my internal - // structure is empty, then things don't typecheck for - // me. This is specific to my own implementation. - // if (res != null && res.type_array.size() > 0) - if (res.getType() != TypeEnum.ERROR) + try { + root.accept(hv, null); System.out.println("Program type checked successfully"); - else + } catch (TypecheckException e) { System.out.println("Type error"); + } + } catch (ParseException e) { System.out.println(e.toString()); diff --git a/boil/BoilVisitor.java b/boil/BoilVisitor.java index e8bafe3..4ba6ea2 100644 --- a/boil/BoilVisitor.java +++ b/boil/BoilVisitor.java @@ -177,7 +177,7 @@ public class BoilVisitor extends GJDepthFirst { this.addVapor(" ret\n\n"); /////////////////////////////////////////////////////////////// this.symt.removeActive(TypeEnum.method); - symt.removeActive(TypeEnum.classname); + this.symt.removeActive(TypeEnum.classname); MinimalLogger.info(String.format("<- %s (%s)", n.getClass().getSimpleName(), id)); @@ -278,10 +278,6 @@ public class BoilVisitor extends GJDepthFirst { n.getClass().getSimpleName(), id)); /////////////////////////////////////////////////////////////// - // if (t.getClassInstance() != null) - // this.addVapor(String.format(" %s = HeapAllocZ(%d)\n", - // this.tf.alias(t.getName()), - // t.getSize())); n.f0.accept(this, argu); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<- %s (%s)", diff --git a/heat/HeatVisitor.java b/heat/HeatVisitor.java new file mode 100644 index 0000000..746166c --- /dev/null +++ b/heat/HeatVisitor.java @@ -0,0 +1,998 @@ +package heat; + +import syntaxtree.*; +import visitor.*; +import st.*; +import misc.*; +import java.util.*; + +public class HeatVisitor extends GJDepthFirst { + + private SymbolTable symt; + + public HeatVisitor(SymbolTable symt) { + this.symt = symt; + } + + // + // Auto class visitors--probably don't need to be overridden. + // + public TypeBundle visit(NodeList n, String argu) { + TypeBundle _ret=null; + int _count=0; + for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { + e.nextElement().accept(this,argu); + _count++; + } + return _ret; + } + + public TypeBundle visit(NodeListOptional n, String argu) { + if ( n.present() ) { + TypeBundle _ret=null; + int _count=0; + for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { + e.nextElement().accept(this,argu); + _count++; + } + return _ret; + } + else + return null; + } + + public TypeBundle visit(NodeOptional n, String argu) { + if ( n.present() ) + return n.node.accept(this,argu); + else + return null; + } + + public TypeBundle visit(NodeSequence n, String argu) { + TypeBundle _ret=null; + int _count=0; + for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { + e.nextElement().accept(this,argu); + _count++; + } + return _ret; + } + + public TypeBundle visit(NodeToken n, String argu) { return null; } + + // + // User-generated visitor methods below + // + + /** + * f0 -> MainClass() + * f1 -> ( TypeDeclaration() )* + * f2 -> + */ + public TypeBundle visit(Goal n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + 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 TypeBundle visit(MainClass n, String argu) { + TypeBundle _ret=null; + String id = n.f1.f0.tokenImage; + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + this.symt.setActive(TypeEnum.classname, symt.getClass(id)); + this.symt.setActive(TypeEnum.method, symt.getMethod(n.f6.tokenImage)); + /////////////////////////////////////////////////////////////// + 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.symt.removeActive(TypeEnum.method); + this.symt.removeActive(TypeEnum.classname); + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> ClassDeclaration() + * | ClassExtendsDeclaration() + */ + public TypeBundle visit(TypeDeclaration n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> ( VarDeclaration() )* + * f4 -> ( MethodDeclaration() )* + * f5 -> "}" + */ + public TypeBundle visit(ClassDeclaration n, String argu) { + TypeBundle _ret=null; + String id = n.f1.f0.tokenImage; + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + this.symt.setActive(TypeEnum.classname, symt.getClass(id)); + /////////////////////////////////////////////////////////////// + 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.symt.removeActive(TypeEnum.classname); + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "extends" + * f3 -> Identifier() + * f4 -> "{" + * f5 -> ( VarDeclaration() )* + * f6 -> ( MethodDeclaration() )* + * f7 -> "}" + */ + public TypeBundle visit(ClassExtendsDeclaration n, String argu) { + TypeBundle _ret=null; + String id = n.f1.f0.tokenImage; + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + this.symt.setActive(TypeEnum.classname, symt.getClass(id)); + /////////////////////////////////////////////////////////////// + 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.symt.removeActive(TypeEnum.classname); + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + * f2 -> ";" + */ + public TypeBundle visit(VarDeclaration n, String argu) { + TypeBundle _ret=null; + String id = n.f1.f0.tokenImage; + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> "public" + * f1 -> Type() + * f2 -> Identifier() + * f3 -> "(" + * f4 -> ( FormalParameterList() )? + * f5 -> ")" + * f6 -> "{" + * f7 -> ( VarDeclaration() )* + * f8 -> ( Statement() )* + * f9 -> "return" + * f10 -> Expression() + * f11 -> ";" + * f12 -> "}" + */ + public TypeBundle visit(MethodDeclaration n, String argu) { + TypeBundle _ret=null; + String id = n.f2.f0.tokenImage; + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + this.symt.setActive(TypeEnum.method, symt.getMethod(id)); + /////////////////////////////////////////////////////////////// + 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.symt.removeActive(TypeEnum.method); + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + return _ret; + } + + /** + * f0 -> FormalParameter() + * f1 -> ( FormalParameterRest() )* + */ + public TypeBundle visit(FormalParameterList n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + */ + public TypeBundle visit(FormalParameter n, String argu) { + TypeBundle _ret=null; + String id = n.f1.f0.tokenImage; + MinimalLogger.info(String.format("-> %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s (%s)", + n.getClass().getSimpleName(), + id)); + /////////////////////////////////////////////////////////////// + return _ret; + } + + /** + * f0 -> "," + * f1 -> FormalParameter() + */ + public TypeBundle visit(FormalParameterRest n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> ArrayType() + * | BooleanType() + * | IntegerType() + * | Identifier() + */ + public TypeBundle visit(Type n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "int" + * f1 -> "[" + * f2 -> "]" + */ + public TypeBundle visit(ArrayType n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "boolean" + */ + public TypeBundle visit(BooleanType n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "int" + */ + public TypeBundle visit(IntegerType n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> Block() + * | AssignmentStatement() + * | ArrayAssignmentStatement() + * | IfStatement() + * | WhileStatement() + * | PrintStatement() + */ + public TypeBundle visit(Statement n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "{" + * f1 -> ( Statement() )* + * f2 -> "}" + */ + public TypeBundle visit(Block n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> Identifier() + * f1 -> "=" + * f2 -> Expression() + * f3 -> ";" + */ + public TypeBundle visit(AssignmentStatement n, String argu) { + TypeBundle _ret=null; + String id = n.f0.f0.tokenImage; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> Identifier() + * f1 -> "[" + * f2 -> Expression() + * f3 -> "]" + * f4 -> "=" + * f5 -> Expression() + * f6 -> ";" + */ + public TypeBundle visit(ArrayAssignmentStatement n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + 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); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "if" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> Statement() + * f5 -> "else" + * f6 -> Statement() + */ + public TypeBundle visit(IfStatement n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + 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); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "while" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> Statement() + */ + public TypeBundle visit(WhileStatement n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + 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); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "System.out.println" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> ";" + */ + public TypeBundle visit(PrintStatement n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + 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); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> AndExpression() + * | CompareExpression() + * | PlusExpression() + * | MinusExpression() + * | TimesExpression() + * | ArrayLookup() + * | ArrayLength() + * | MessageSend() + * | PrimaryExpression() + */ + public TypeBundle visit(Expression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "&&" + * f2 -> PrimaryExpression() + */ + public TypeBundle visit(AndExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "<" + * f2 -> PrimaryExpression() + */ + public TypeBundle visit(CompareExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "+" + * f2 -> PrimaryExpression() + */ + public TypeBundle visit(PlusExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "-" + * f2 -> PrimaryExpression() + */ + public TypeBundle visit(MinusExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "*" + * f2 -> PrimaryExpression() + */ + public TypeBundle visit(TimesExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "[" + * f2 -> PrimaryExpression() + * f3 -> "]" + */ + public TypeBundle visit(ArrayLookup n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "." + * f2 -> "length" + */ + public TypeBundle visit(ArrayLength n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> PrimaryExpression() + * f1 -> "." + * f2 -> Identifier() + * f3 -> "(" + * f4 -> ( ExpressionList() )? + * f5 -> ")" + */ + public TypeBundle visit(MessageSend n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + 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); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> Expression() + * f1 -> ( ExpressionRest() )* + */ + public TypeBundle visit(ExpressionList n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "," + * f1 -> Expression() + */ + public TypeBundle visit(ExpressionRest n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> IntegerLiteral() + * | TrueLiteral() + * | FalseLiteral() + * | Identifier() + * | ThisExpression() + * | ArrayAllocationExpression() + * | AllocationExpression() + * | NotExpression() + * | BracketExpression() + */ + public TypeBundle visit(PrimaryExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> + */ + public TypeBundle visit(IntegerLiteral n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "true" + */ + public TypeBundle visit(TrueLiteral n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "false" + */ + public TypeBundle visit(FalseLiteral n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> + */ + public TypeBundle visit(Identifier n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "this" + */ + public TypeBundle visit(ThisExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "new" + * f1 -> "int" + * f2 -> "[" + * f3 -> Expression() + * f4 -> "]" + */ + public TypeBundle visit(ArrayAllocationExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + 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); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "new" + * f1 -> Identifier() + * f2 -> "(" + * f3 -> ")" + */ + public TypeBundle visit(AllocationExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + n.f3.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "!" + * f1 -> Expression() + */ + public TypeBundle visit(NotExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + + /** + * f0 -> "(" + * f1 -> Expression() + * f2 -> ")" + */ + public TypeBundle visit(BracketExpression n, String argu) { + TypeBundle _ret=null; + MinimalLogger.info(String.format("-> %s", + n.getClass().getSimpleName())); + /////////////////////////////////////////////////////////////// + n.f0.accept(this, argu); + n.f1.accept(this, argu); + n.f2.accept(this, argu); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<- %s with %s", + n.getClass().getSimpleName(), + _ret)); + return _ret; + } + +} diff --git a/heat/TypeBundle.java b/heat/TypeBundle.java new file mode 100644 index 0000000..d5f8121 --- /dev/null +++ b/heat/TypeBundle.java @@ -0,0 +1,41 @@ +package heat; +import st.TypeEnum; +import st.ClassInstance; + +/** + * A simple wrapper for TypeEnum, + * meant to provide all helper methods + * and structures for evaluating typing. + */ +class TypeBundle { + + TypeEnum type; + ClassInstance instance; + + protected TypeBundle(TypeEnum type, ClassInstance instance) { + this.type = type; + this.instance = instance; + } + + @Override public String toString() { + return String.format("%s (%s)", + type, + instance); + } + + @Override public boolean equals(Object other) { + /** + * We can say two types are equal, as + * long as they are not equal on a + * type error! + */ + return (other instanceof TypeBundle) && + this.hasChecked() && + ((TypeBundle) other).type == this.type; + } + + public boolean hasChecked() { + return type != TypeEnum.ERROR; + } + +} diff --git a/heat/TypeCheckSimp.java b/heat/TypeCheckSimp.java new file mode 100644 index 0000000..99fa187 --- /dev/null +++ b/heat/TypeCheckSimp.java @@ -0,0 +1,1097 @@ +package typecheck; + +import syntaxtree.*; +import visitor.*; +import st.*; +import misc.*; +import java.util.*; + +/** + * Provides default methods which visit each node in the tree in depth-first + * order. Your visitors may extend this class. + */ +public class TypeCheckSimp extends GJDepthFirst { + + private int offset; + + private void printNode(Node n, SymbolTable symt, boolean enter, TypeEnum consensus) { + String str = ""; + for (int i=0; i < this.offset; ++i) + str += "."; + if (enter) + str += "Visiting "; + else + str += "Leaving "; + str += "n.getClass().getSimpleName()"; + if (!enter) { + if (consensus == TypeEnum.ERROR) + str += " did not type check."; + else + str += String.format(" found type %s", consensus); + } + MinimalLogger.info(str); + } + + public TypeInstance visit(NodeList n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK); + int _count=0; + for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { + TypeInstance node = e.nextElement().accept(this,symt); + e.nextElement().accept(this,symt); + if (node.getType() == TypeEnum.ERROR) + ret = node; + _count++; + } + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + public TypeInstance visit(NodeListOptional n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret; + if ( n.present() ) { + ret = new TypeInstance(null, TypeEnum.CHECK); + int _count=0; + for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { + TypeInstance node = e.nextElement().accept(this,symt); + if (node.getType() == TypeEnum.ERROR) + ret = node; + _count++; + } + } + else + ret = new TypeInstance(null, TypeEnum.CHECK); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + // FIXME + public TypeInstance visit(NodeOptional n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret; + + if ( n.present() ) + ret = n.node.accept(this,symt); + else + ret = new TypeInstance(null, TypeEnum.CHECK); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + public TypeInstance visit(NodeSequence n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK); + int _count=0; + for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { + TypeInstance node = e.nextElement().accept(this,symt); + if (node.getType() == TypeEnum.ERROR) + ret = node; + _count++; + } + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + public TypeInstance visit(NodeToken n, SymbolTable symt) { + // A fixed string token. '⌣' + String str = ""; + for (int i=0; i < this.offset; ++i) + str += "."; + MinimalLogger.info(String.format("%sLeaving %s => %s", + str, + n.getClass().getSimpleName(), + n.toString())); + return null; + } + + // + // User-generated visitor methods below + // + + /** + * f0 -> MainClass() + * f1 -> ( TypeDeclaration() )* + * f2 -> + */ + public TypeInstance visit(Goal n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance name = n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + n.f8.accept(this, symt); + n.f9.accept(this, symt); + n.f10.accept(this, symt); + // TypeInstance args = n.f11.accept(this, symt); + n.f12.accept(this, symt); + n.f13.accept(this, symt); + TypeInstance var_dec = n.f14.accept(this, symt); + TypeInstance stmt = n.f15.accept(this, symt); + n.f16.accept(this, symt); + n.f17.accept(this, symt); + + this.printNode(n, symt, false, stmt.getType()); + --this.offset; + return stmt; + } + + /** + * f0 -> ClassDeclaration() + * | ClassExtendsDeclaration() + */ + public TypeInstance visit(TypeDeclaration n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = n.f0.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "{" + * f3 -> ( VarDeclaration() )* + * f4 -> ( MethodDeclaration() )* + * f5 -> "}" + */ + public TypeInstance visit(ClassDeclaration n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance id = n.f1.accept(this, symt); + n.f2.accept(this, symt); + TypeInstance vars = n.f3.accept(this, symt); + TypeInstance mtds = n.f4.accept(this, symt); + n.f5.accept(this, symt); + TypeInstance ret = (vars.hasChecked() && + mtds.hasChecked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> "class" + * f1 -> Identifier() + * f2 -> "extends" + * f3 -> Identifier() + * f4 -> "{" + * f5 -> ( VarDeclaration() )* + * f6 -> ( MethodDeclaration() )* + * f7 -> "}" + */ + public TypeInstance visit(ClassExtendsDeclaration n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance id = n.f1.accept(this, symt); + n.f2.accept(this, symt); + TypeInstance ext = n.f3.accept(this, symt); + n.f4.accept(this, symt); + TypeInstance vars = n.f5.accept(this, symt); + TypeInstance mehs = n.f6.accept(this, symt); + n.f7.accept(this, symt); + TypeInstance ret = (id.getType() == TypeEnum.classname && + vars.hasChecked() && + mehs.hasChecked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + // FIXME (this may be ST-only) + /** + * f0 -> Type() + * f1 -> Identifier() + * f2 -> ";" + */ + public TypeInstance visit(VarDeclaration n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + // FIXME + /** + * [7.5]: Parameters identifiers are distinct, local vars are distinct, + * (vars in scope?), all statements type check + * + * 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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + MethodInstance self = symt.getMethod(n.f2.f0.tokenImage); + n.f0.accept(this, symt); + TypeInstance ret_type = n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + n.f6.accept(this, symt); + n.f7.accept(this, symt); + TypeInstance stmt = n.f8.accept(this, symt); + n.f9.accept(this, symt); + TypeInstance rtrn = n.f10.accept(this, symt); + n.f11.accept(this, symt); + n.f12.accept(this, symt); + TypeInstance ret = (self.getReturn() == rtrn.getType() && // FIXME I am checking that the rtrn matches the method's declared return type. Is this in the document? + stmt.hasChecked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> FormalParameter() + * f1 -> ( FormalParameterRest() )* + */ + public TypeInstance visit(FormalParameterList n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance _ret=null; + TypeInstance para1 = n.f0.accept(this, symt); + TypeInstance parar = n.f1.accept(this, symt); + TypeInstance ret = (para1.hasChecked() && + parar.hasChecked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> Type() + * f1 -> Identifier() + */ + public TypeInstance visit(FormalParameter n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = n.f0.accept(this, symt); + n.f1.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> "," + * f1 -> FormalParameter() + */ + public TypeInstance visit(FormalParameterRest n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance ret = n.f1.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> ArrayType() + * | BooleanType() + * | IntegerType() + * | Identifier() + */ + public TypeInstance visit(Type n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = n.f0.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> "int" + * f1 -> "[" + * f2 -> "]" + */ + public TypeInstance visit(ArrayType n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = new TypeInstance(null, TypeEnum.intarray); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> "boolean" + */ + public TypeInstance visit(BooleanType n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = new TypeInstance(null, TypeEnum.bool); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> "int" + */ + public TypeInstance visit(IntegerType n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = new TypeInstance(null, TypeEnum.integer); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> Block() + * | AssignmentStatement() + * | ArrayAssignmentStatement() + * | IfStatement() + * | WhileStatement() + * | PrintStatement() + */ + public TypeInstance visit(Statement n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = n.f0.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> "{" + * f1 -> ( Statement() )* + * f2 -> "}" + */ + public TypeInstance visit(Block n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance ret = n.f1.accept(this, symt); + n.f2.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance lhs = n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance rhs = n.f2.accept(this, symt); + n.f3.accept(this, symt); + TypeInstance ret = (lhs.sameType(rhs)) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance id = n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance index = n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + TypeInstance value = n.f5.accept(this, symt); + n.f6.accept(this, symt); + TypeInstance ret = (id.getType() == TypeEnum.intarray && + index.getType() == TypeEnum.integer && + value.getType() == TypeEnum.integer) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance expr = n.f2.accept(this, symt); + n.f3.accept(this, symt); + TypeInstance stmt1 = n.f4.accept(this, symt); + n.f5.accept(this, symt); + TypeInstance stmt2 = n.f6.accept(this, symt); + TypeInstance ret = (expr.getType() == TypeEnum.bool && + stmt1.getType() == stmt2.getType() && + stmt1.hasChecked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance expr = n.f2.accept(this, symt); + n.f3.accept(this, symt); + TypeInstance stmt = n.f4.accept(this, symt); + TypeInstance ret = (expr.getType() == TypeEnum.bool && + stmt.hasChecked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * DONE [27]: Expression is an integer + * + * f0 -> "System.out.println" + * f1 -> "(" + * f2 -> Expression() + * f3 -> ")" + * f4 -> ";" + */ + public TypeInstance visit(PrintStatement n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance ret = n.f2.accept(this, symt); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + ret = (ret.getType() == TypeEnum.integer) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> AndExpression() + * | CompareExpression() + * | PlusExpression() + * | MinusExpression() + * | TimesExpression() + * | ArrayLookup() + * | ArrayLength() + * | MessageSend() + * | PrimaryExpression() + */ + public TypeInstance visit(Expression n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = n.f0.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance oper1 = n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance oper2 = n.f2.accept(this, symt); + TypeInstance ret = (oper1.getType() == TypeEnum.bool && + oper2.getType() == TypeEnum.bool) ? + new TypeInstance(null, TypeEnum.bool) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance oper1 = n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance oper2 = n.f2.accept(this, symt); + TypeInstance ret = (oper1.getType() == TypeEnum.integer && + oper2.getType() == TypeEnum.integer) ? + new TypeInstance(null, TypeEnum.bool) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance oper1 = n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance oper2 = n.f2.accept(this, symt); + TypeInstance ret = (oper1.getType() == TypeEnum.integer && + oper2.getType() == TypeEnum.integer) ? + new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance oper1 = n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance oper2 = n.f2.accept(this, symt); + TypeInstance ret = (oper1.getType() == TypeEnum.integer && + oper2.getType() == TypeEnum.integer) ? + new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance oper1 = n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance oper2 = n.f2.accept(this, symt); + TypeInstance ret = (oper1.getType() == TypeEnum.integer && + oper2.getType() == TypeEnum.integer) ? + new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance array = n.f0.accept(this, symt); + n.f1.accept(this, symt); + TypeInstance index = n.f2.accept(this, symt); + n.f3.accept(this, symt); + TypeInstance ret = (array.getType() == TypeEnum.intarray && + index.getType() == TypeEnum.integer) ? + new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + ret = (ret.getType() == TypeEnum.intarray) ? new TypeInstance(null, TypeEnum.integer) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + // this MUST be an instance of a class! + TypeInstance t = n.f0.accept(this, symt); + ClassInstance c = t.getClassInstance(); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + MethodInstance m = symt.getMethod(n.f2.f0.tokenImage); + n.f3.accept(this, symt); + n.f4.accept(this, symt); + n.f5.accept(this, symt); + --this.offset; + return (true) ? + new TypeInstance(null, m.getReturn()) : + new TypeInstance(null, TypeEnum.ERROR); + } + + // FIXME + /** + * f0 -> Expression() + * f1 -> ( ExpressionRest() )* + */ + public TypeInstance visit(ExpressionList n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance expr1 = n.f0.accept(this, symt); + TypeInstance exprr = n.f1.accept(this, symt); + TypeInstance ret = (expr1.hasChecked() && + exprr.hasChecked()) ? + new TypeInstance(null, TypeEnum.CHECK) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> "," + * f1 -> Expression() + */ + public TypeInstance visit(ExpressionRest n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance ret = n.f1.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * f0 -> IntegerLiteral() + * | TrueLiteral() + * | FalseLiteral() + * | Identifier() + * | ThisExpression() + * | ArrayAllocationExpression() + * | AllocationExpression() + * | NotExpression() + * | BracketExpression() + */ + public TypeInstance visit(PrimaryExpression n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance ret = n.f0.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * DONE [36]: return is an int + * + * + * f0 -> + */ + public TypeInstance visit(IntegerLiteral n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + this.printNode(n, symt, false, TypeEnum.integer); + --this.offset; + return new TypeInstance(null, TypeEnum.integer); + } + + /** + * DONE [37]: return is a bool + * + * f0 -> "true" + */ + public TypeInstance visit(TrueLiteral n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + this.printNode(n, symt, false, TypeEnum.bool); + --this.offset; + return new TypeInstance(null, TypeEnum.bool); + } + + /** + * DONE [38]: return is a bool + * + * f0 -> "false" + */ + public TypeInstance visit(FalseLiteral n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + this.printNode(n, symt, 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 -> + */ + public TypeInstance visit(Identifier n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + TypeInstance type; + TypeInstance ret; + ClassInstance cls; + if ((cls = symt.getClass(n.f0.tokenImage)) != null) { + // covers "anonymous" instance + ret = new TypeInstance(cls.getName(), + TypeEnum.classname); + ret.addClassInstance(cls); + } else { + if ((type = symt.getType(n.f0.tokenImage)) != null) { + ret = new TypeInstance(type.getName(), + type.getType()); + ret.addClassInstance(type.getClassInstance()); + } else + ret = new TypeInstance(null, TypeEnum.ERROR); + } + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + // FIXME FIXME FIXME + /** + * [40]: method exists? but where is the token? + * + * f0 -> "this" + */ + public TypeInstance visit(ThisExpression n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK); + + this.printNode(n, symt, false, ret.getType()); + --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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + n.f1.accept(this, symt); + n.f2.accept(this, symt); + TypeInstance ret = n.f3.accept(this, symt); + n.f4.accept(this, symt); + ret = (ret.getType() == TypeEnum.integer) ? new TypeInstance(null, TypeEnum.intarray) : + new TypeInstance(null, TypeEnum.ERROR); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + // FIXME + /** + * [42]: + * + * f0 -> "new" + * f1 -> Identifier() + * f2 -> "(" + * f3 -> ")" + */ + public TypeInstance visit(AllocationExpression n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance ret = n.f1.accept(this, symt); + n.f2.accept(this, symt); + n.f3.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + + /** + * [43]: if expression is a boolean, return is a boolean + * + * f0 -> "!" + * f1 -> Expression() + */ + public TypeInstance visit(NotExpression n, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance ret = n.f1.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return (ret.getType() == 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, SymbolTable symt) { + ++this.offset; + this.printNode(n, symt, true, null); + + n.f0.accept(this, symt); + TypeInstance ret = n.f1.accept(this, symt); + n.f2.accept(this, symt); + + this.printNode(n, symt, false, ret.getType()); + --this.offset; + return ret; + } + +} diff --git a/heat/TypecheckException.java b/heat/TypecheckException.java new file mode 100644 index 0000000..39919ef --- /dev/null +++ b/heat/TypecheckException.java @@ -0,0 +1,9 @@ +package heat; + +public class TypecheckException extends RuntimeException { + + public TypecheckException(String message) { + super(message); + } + +} diff --git a/runner.sh b/runner.sh index 89a1e7c..19c4960 100755 --- a/runner.sh +++ b/runner.sh @@ -16,9 +16,9 @@ function run_with_java() { java -cp $dirname $filename } -function typecheck() { - # typecheck the file - java typecheck < $1 +function heat() { + # heat the file + java Typecheck < $1 } function boil() { @@ -62,8 +62,8 @@ case "$1" in "java") run_with_java "$2" ;; - "typecheck") - typecheck "$2" + "heat") + heat "$2" ;; "boil") boil "$2" @@ -75,7 +75,7 @@ case "$1" in condense "$2" ;; *) - echo "usage $0 [java|typecheck|boil|vaporize|condense]" + echo "usage $0 [java|heat|boil|vaporize|condense]" exit 1 ;; esac diff --git a/st/ClassInstance.java b/st/ClassInstance.java index 8394a2c..789571b 100644 --- a/st/ClassInstance.java +++ b/st/ClassInstance.java @@ -13,7 +13,11 @@ public class ClassInstance extends AbstractInstance { this.mtds = new ArrayList<>(); } - public boolean equals(Object other) { + @Override public String toString() { + return String.format("%s", this.name); + } + + @Override public boolean equals(Object other) { ClassInstance o; return (other instanceof ClassInstance && ((o = (ClassInstance) other).getName() == this.getName())); diff --git a/st/TypeInstance.java b/st/TypeInstance.java index e43c5b3..330662c 100644 --- a/st/TypeInstance.java +++ b/st/TypeInstance.java @@ -26,25 +26,6 @@ public class TypeInstance extends AbstractInstance { cls.getSize() : 4; } - public boolean sameType(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.getType(); - } - - public boolean hasChecked() { - return type != TypeEnum.ERROR; - } - public void addClassInstance(ClassInstance cls) { this.cls = cls; } diff --git a/test.sh b/test.sh index 635fe71..a8c60bd 100755 --- a/test.sh +++ b/test.sh @@ -7,7 +7,7 @@ testdir="./output" case "$1" in - "typecheck") + "heat") ext="java" ;; "boil") @@ -20,7 +20,7 @@ case "$1" in ext="vaporm" ;; *) - echo "usage $0 [typecheck|boil|vaporize|condense]" + echo "usage $0 [heat|boil|vaporize|condense]" exit 1 ;; esac diff --git a/typecheck/TypeCheckSimp.java b/typecheck/TypeCheckSimp.java deleted file mode 100644 index 99fa187..0000000 --- a/typecheck/TypeCheckSimp.java +++ /dev/null @@ -1,1097 +0,0 @@ -package typecheck; - -import syntaxtree.*; -import visitor.*; -import st.*; -import misc.*; -import java.util.*; - -/** - * Provides default methods which visit each node in the tree in depth-first - * order. Your visitors may extend this class. - */ -public class TypeCheckSimp extends GJDepthFirst { - - private int offset; - - private void printNode(Node n, SymbolTable symt, boolean enter, TypeEnum consensus) { - String str = ""; - for (int i=0; i < this.offset; ++i) - str += "."; - if (enter) - str += "Visiting "; - else - str += "Leaving "; - str += "n.getClass().getSimpleName()"; - if (!enter) { - if (consensus == TypeEnum.ERROR) - str += " did not type check."; - else - str += String.format(" found type %s", consensus); - } - MinimalLogger.info(str); - } - - public TypeInstance visit(NodeList n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK); - int _count=0; - for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { - TypeInstance node = e.nextElement().accept(this,symt); - e.nextElement().accept(this,symt); - if (node.getType() == TypeEnum.ERROR) - ret = node; - _count++; - } - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - public TypeInstance visit(NodeListOptional n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret; - if ( n.present() ) { - ret = new TypeInstance(null, TypeEnum.CHECK); - int _count=0; - for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { - TypeInstance node = e.nextElement().accept(this,symt); - if (node.getType() == TypeEnum.ERROR) - ret = node; - _count++; - } - } - else - ret = new TypeInstance(null, TypeEnum.CHECK); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - // FIXME - public TypeInstance visit(NodeOptional n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret; - - if ( n.present() ) - ret = n.node.accept(this,symt); - else - ret = new TypeInstance(null, TypeEnum.CHECK); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - public TypeInstance visit(NodeSequence n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK); - int _count=0; - for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { - TypeInstance node = e.nextElement().accept(this,symt); - if (node.getType() == TypeEnum.ERROR) - ret = node; - _count++; - } - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - public TypeInstance visit(NodeToken n, SymbolTable symt) { - // A fixed string token. '⌣' - String str = ""; - for (int i=0; i < this.offset; ++i) - str += "."; - MinimalLogger.info(String.format("%sLeaving %s => %s", - str, - n.getClass().getSimpleName(), - n.toString())); - return null; - } - - // - // User-generated visitor methods below - // - - /** - * f0 -> MainClass() - * f1 -> ( TypeDeclaration() )* - * f2 -> - */ - public TypeInstance visit(Goal n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance name = n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - n.f6.accept(this, symt); - n.f7.accept(this, symt); - n.f8.accept(this, symt); - n.f9.accept(this, symt); - n.f10.accept(this, symt); - // TypeInstance args = n.f11.accept(this, symt); - n.f12.accept(this, symt); - n.f13.accept(this, symt); - TypeInstance var_dec = n.f14.accept(this, symt); - TypeInstance stmt = n.f15.accept(this, symt); - n.f16.accept(this, symt); - n.f17.accept(this, symt); - - this.printNode(n, symt, false, stmt.getType()); - --this.offset; - return stmt; - } - - /** - * f0 -> ClassDeclaration() - * | ClassExtendsDeclaration() - */ - public TypeInstance visit(TypeDeclaration n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = n.f0.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "{" - * f3 -> ( VarDeclaration() )* - * f4 -> ( MethodDeclaration() )* - * f5 -> "}" - */ - public TypeInstance visit(ClassDeclaration n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance id = n.f1.accept(this, symt); - n.f2.accept(this, symt); - TypeInstance vars = n.f3.accept(this, symt); - TypeInstance mtds = n.f4.accept(this, symt); - n.f5.accept(this, symt); - TypeInstance ret = (vars.hasChecked() && - mtds.hasChecked()) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "extends" - * f3 -> Identifier() - * f4 -> "{" - * f5 -> ( VarDeclaration() )* - * f6 -> ( MethodDeclaration() )* - * f7 -> "}" - */ - public TypeInstance visit(ClassExtendsDeclaration n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance id = n.f1.accept(this, symt); - n.f2.accept(this, symt); - TypeInstance ext = n.f3.accept(this, symt); - n.f4.accept(this, symt); - TypeInstance vars = n.f5.accept(this, symt); - TypeInstance mehs = n.f6.accept(this, symt); - n.f7.accept(this, symt); - TypeInstance ret = (id.getType() == TypeEnum.classname && - vars.hasChecked() && - mehs.hasChecked()) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - // FIXME (this may be ST-only) - /** - * f0 -> Type() - * f1 -> Identifier() - * f2 -> ";" - */ - public TypeInstance visit(VarDeclaration n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - // FIXME - /** - * [7.5]: Parameters identifiers are distinct, local vars are distinct, - * (vars in scope?), all statements type check - * - * 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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - MethodInstance self = symt.getMethod(n.f2.f0.tokenImage); - n.f0.accept(this, symt); - TypeInstance ret_type = n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - n.f6.accept(this, symt); - n.f7.accept(this, symt); - TypeInstance stmt = n.f8.accept(this, symt); - n.f9.accept(this, symt); - TypeInstance rtrn = n.f10.accept(this, symt); - n.f11.accept(this, symt); - n.f12.accept(this, symt); - TypeInstance ret = (self.getReturn() == rtrn.getType() && // FIXME I am checking that the rtrn matches the method's declared return type. Is this in the document? - stmt.hasChecked()) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> FormalParameter() - * f1 -> ( FormalParameterRest() )* - */ - public TypeInstance visit(FormalParameterList n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance _ret=null; - TypeInstance para1 = n.f0.accept(this, symt); - TypeInstance parar = n.f1.accept(this, symt); - TypeInstance ret = (para1.hasChecked() && - parar.hasChecked()) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> Type() - * f1 -> Identifier() - */ - public TypeInstance visit(FormalParameter n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = n.f0.accept(this, symt); - n.f1.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> "," - * f1 -> FormalParameter() - */ - public TypeInstance visit(FormalParameterRest n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance ret = n.f1.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> ArrayType() - * | BooleanType() - * | IntegerType() - * | Identifier() - */ - public TypeInstance visit(Type n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = n.f0.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> "int" - * f1 -> "[" - * f2 -> "]" - */ - public TypeInstance visit(ArrayType n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = new TypeInstance(null, TypeEnum.intarray); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> "boolean" - */ - public TypeInstance visit(BooleanType n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = new TypeInstance(null, TypeEnum.bool); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> "int" - */ - public TypeInstance visit(IntegerType n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = new TypeInstance(null, TypeEnum.integer); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> Block() - * | AssignmentStatement() - * | ArrayAssignmentStatement() - * | IfStatement() - * | WhileStatement() - * | PrintStatement() - */ - public TypeInstance visit(Statement n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = n.f0.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> "{" - * f1 -> ( Statement() )* - * f2 -> "}" - */ - public TypeInstance visit(Block n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance ret = n.f1.accept(this, symt); - n.f2.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance lhs = n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance rhs = n.f2.accept(this, symt); - n.f3.accept(this, symt); - TypeInstance ret = (lhs.sameType(rhs)) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance id = n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance index = n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - TypeInstance value = n.f5.accept(this, symt); - n.f6.accept(this, symt); - TypeInstance ret = (id.getType() == TypeEnum.intarray && - index.getType() == TypeEnum.integer && - value.getType() == TypeEnum.integer) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance expr = n.f2.accept(this, symt); - n.f3.accept(this, symt); - TypeInstance stmt1 = n.f4.accept(this, symt); - n.f5.accept(this, symt); - TypeInstance stmt2 = n.f6.accept(this, symt); - TypeInstance ret = (expr.getType() == TypeEnum.bool && - stmt1.getType() == stmt2.getType() && - stmt1.hasChecked()) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance expr = n.f2.accept(this, symt); - n.f3.accept(this, symt); - TypeInstance stmt = n.f4.accept(this, symt); - TypeInstance ret = (expr.getType() == TypeEnum.bool && - stmt.hasChecked()) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * DONE [27]: Expression is an integer - * - * f0 -> "System.out.println" - * f1 -> "(" - * f2 -> Expression() - * f3 -> ")" - * f4 -> ";" - */ - public TypeInstance visit(PrintStatement n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance ret = n.f2.accept(this, symt); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - ret = (ret.getType() == TypeEnum.integer) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> AndExpression() - * | CompareExpression() - * | PlusExpression() - * | MinusExpression() - * | TimesExpression() - * | ArrayLookup() - * | ArrayLength() - * | MessageSend() - * | PrimaryExpression() - */ - public TypeInstance visit(Expression n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = n.f0.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance oper1 = n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance oper2 = n.f2.accept(this, symt); - TypeInstance ret = (oper1.getType() == TypeEnum.bool && - oper2.getType() == TypeEnum.bool) ? - new TypeInstance(null, TypeEnum.bool) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance oper1 = n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance oper2 = n.f2.accept(this, symt); - TypeInstance ret = (oper1.getType() == TypeEnum.integer && - oper2.getType() == TypeEnum.integer) ? - new TypeInstance(null, TypeEnum.bool) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance oper1 = n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance oper2 = n.f2.accept(this, symt); - TypeInstance ret = (oper1.getType() == TypeEnum.integer && - oper2.getType() == TypeEnum.integer) ? - new TypeInstance(null, TypeEnum.integer) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance oper1 = n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance oper2 = n.f2.accept(this, symt); - TypeInstance ret = (oper1.getType() == TypeEnum.integer && - oper2.getType() == TypeEnum.integer) ? - new TypeInstance(null, TypeEnum.integer) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance oper1 = n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance oper2 = n.f2.accept(this, symt); - TypeInstance ret = (oper1.getType() == TypeEnum.integer && - oper2.getType() == TypeEnum.integer) ? - new TypeInstance(null, TypeEnum.integer) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance array = n.f0.accept(this, symt); - n.f1.accept(this, symt); - TypeInstance index = n.f2.accept(this, symt); - n.f3.accept(this, symt); - TypeInstance ret = (array.getType() == TypeEnum.intarray && - index.getType() == TypeEnum.integer) ? - new TypeInstance(null, TypeEnum.integer) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - ret = (ret.getType() == TypeEnum.intarray) ? new TypeInstance(null, TypeEnum.integer) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - // this MUST be an instance of a class! - TypeInstance t = n.f0.accept(this, symt); - ClassInstance c = t.getClassInstance(); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - MethodInstance m = symt.getMethod(n.f2.f0.tokenImage); - n.f3.accept(this, symt); - n.f4.accept(this, symt); - n.f5.accept(this, symt); - --this.offset; - return (true) ? - new TypeInstance(null, m.getReturn()) : - new TypeInstance(null, TypeEnum.ERROR); - } - - // FIXME - /** - * f0 -> Expression() - * f1 -> ( ExpressionRest() )* - */ - public TypeInstance visit(ExpressionList n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance expr1 = n.f0.accept(this, symt); - TypeInstance exprr = n.f1.accept(this, symt); - TypeInstance ret = (expr1.hasChecked() && - exprr.hasChecked()) ? - new TypeInstance(null, TypeEnum.CHECK) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> "," - * f1 -> Expression() - */ - public TypeInstance visit(ExpressionRest n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance ret = n.f1.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * f0 -> IntegerLiteral() - * | TrueLiteral() - * | FalseLiteral() - * | Identifier() - * | ThisExpression() - * | ArrayAllocationExpression() - * | AllocationExpression() - * | NotExpression() - * | BracketExpression() - */ - public TypeInstance visit(PrimaryExpression n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance ret = n.f0.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * DONE [36]: return is an int - * - * - * f0 -> - */ - public TypeInstance visit(IntegerLiteral n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - this.printNode(n, symt, false, TypeEnum.integer); - --this.offset; - return new TypeInstance(null, TypeEnum.integer); - } - - /** - * DONE [37]: return is a bool - * - * f0 -> "true" - */ - public TypeInstance visit(TrueLiteral n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - this.printNode(n, symt, false, TypeEnum.bool); - --this.offset; - return new TypeInstance(null, TypeEnum.bool); - } - - /** - * DONE [38]: return is a bool - * - * f0 -> "false" - */ - public TypeInstance visit(FalseLiteral n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - this.printNode(n, symt, 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 -> - */ - public TypeInstance visit(Identifier n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - TypeInstance type; - TypeInstance ret; - ClassInstance cls; - if ((cls = symt.getClass(n.f0.tokenImage)) != null) { - // covers "anonymous" instance - ret = new TypeInstance(cls.getName(), - TypeEnum.classname); - ret.addClassInstance(cls); - } else { - if ((type = symt.getType(n.f0.tokenImage)) != null) { - ret = new TypeInstance(type.getName(), - type.getType()); - ret.addClassInstance(type.getClassInstance()); - } else - ret = new TypeInstance(null, TypeEnum.ERROR); - } - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - // FIXME FIXME FIXME - /** - * [40]: method exists? but where is the token? - * - * f0 -> "this" - */ - public TypeInstance visit(ThisExpression n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance ret = new TypeInstance(null, TypeEnum.CHECK); - - this.printNode(n, symt, false, ret.getType()); - --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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - n.f1.accept(this, symt); - n.f2.accept(this, symt); - TypeInstance ret = n.f3.accept(this, symt); - n.f4.accept(this, symt); - ret = (ret.getType() == TypeEnum.integer) ? new TypeInstance(null, TypeEnum.intarray) : - new TypeInstance(null, TypeEnum.ERROR); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - // FIXME - /** - * [42]: - * - * f0 -> "new" - * f1 -> Identifier() - * f2 -> "(" - * f3 -> ")" - */ - public TypeInstance visit(AllocationExpression n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance ret = n.f1.accept(this, symt); - n.f2.accept(this, symt); - n.f3.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - - /** - * [43]: if expression is a boolean, return is a boolean - * - * f0 -> "!" - * f1 -> Expression() - */ - public TypeInstance visit(NotExpression n, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance ret = n.f1.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return (ret.getType() == 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, SymbolTable symt) { - ++this.offset; - this.printNode(n, symt, true, null); - - n.f0.accept(this, symt); - TypeInstance ret = n.f1.accept(this, symt); - n.f2.accept(this, symt); - - this.printNode(n, symt, false, ret.getType()); - --this.offset; - return ret; - } - -} -- cgit v1.2.3