minijava.typecheck
Class TypeChecker

java.lang.Object
  extended by minijava.visitor.GJDepthFirst<Type,SymbolTable<Type>>
      extended by minijava.typecheck.TypeChecker
All Implemented Interfaces:
GJVisitor<Type,SymbolTable<Type>>

public class TypeChecker
extends GJDepthFirst<Type,SymbolTable<Type>>

A TypeChecker is a depth-first visitor that, when accepted by a node, returns the logical Type of that node, if it is an expression or certain statement, and null otherwise. Each visit method is supplied with a symbol table argument, in a pseudo-functional manner. If anything in the traversal of the tree does not typecheck, an exception is thrown.

The minijava grammar that this type checker checks is detailed here, with extra specifications here

Author:
Santoso Wijaya
See Also:
SymbolTable, Type, TypeException

Constructor Summary
TypeChecker()
           
 
Method Summary
 void setCurrentClass(Class cls)
           
 Type visit(AllocationExpression n, SymbolTable<Type> symtable)
           nodeToken -> "new" identifier -> Identifier() nodeToken1 -> "(" nodeToken2 -> ")"
 Type visit(AndExpression n, SymbolTable<Type> symtable)
           primaryExpression -> PrimaryExpression() nodeToken -> "&&" primaryExpression1 -> PrimaryExpression()
 Type visit(ArrayAllocationExpression n, SymbolTable<Type> symtable)
           nodeToken -> "new" nodeToken1 -> "int" nodeToken2 -> "[" expression -> Expression() nodeToken3 -> "]"
 Type visit(ArrayAssignmentStatement n, SymbolTable<Type> symtable)
           identifier -> Identifier() nodeToken -> "[" expression -> Expression() nodeToken1 -> "]" nodeToken2 -> "=" expression1 -> Expression() nodeToken3 -> ";"
 Type visit(ArrayLength n, SymbolTable<Type> symtable)
           primaryExpression -> PrimaryExpression() nodeToken -> "." nodeToken1 -> "length"
 Type visit(ArrayLookup n, SymbolTable<Type> symtable)
           primaryExpression -> PrimaryExpression() nodeToken -> "[" primaryExpression1 -> PrimaryExpression() nodeToken1 -> "]"
 Type visit(AssignmentStatement n, SymbolTable<Type> symtable)
           identifier -> Identifier() nodeToken -> "=" expression -> Expression() nodeToken1 -> ";"
 Type visit(Block n, SymbolTable<Type> symtable)
           nodeToken -> "{" nodeListOptional -> ( Statement() )* nodeToken1 -> "}"
 Type visit(BracketExpression n, SymbolTable<Type> symtable)
           nodeToken -> "(" expression -> Expression() nodeToken1 -> ")"
 Type visit(ClassDeclaration n, SymbolTable<Type> symtable)
           nodeToken -> "class" identifier -> Identifier() nodeToken1 -> "{" nodeListOptional -> ( VarDeclaration() )* nodeListOptional1 -> ( MethodDeclaration() )* nodeToken2 -> "}"
 Type visit(ClassExtendsDeclaration n, SymbolTable<Type> symtable)
           nodeToken -> "class" identifier -> Identifier() nodeToken1 -> "extends" identifier1 -> Identifier() nodeToken2 -> "{" nodeListOptional -> ( VarDeclaration() )* nodeListOptional1 -> ( MethodDeclaration() )* nodeToken3 -> "}"
 Type visit(CompareExpression n, SymbolTable<Type> symtable)
           primaryExpression -> PrimaryExpression() nodeToken -> "<" primaryExpression1 -> PrimaryExpression()
 Type visit(ExpressionList n, SymbolTable<Type> symtable)
           expression -> Expression() nodeListOptional -> ( ExpressionRest() )*
 Type visit(ExpressionRest n, SymbolTable<Type> symtable)
           nodeToken -> "," expression -> Expression()
 Type visit(Expression n, SymbolTable<Type> symtable)
           nodeChoice -> AndExpression() | CompareExpression() | PlusExpression() | MinusExpression() | TimesExpression() | ArrayLookup() | ArrayLength() | MessageSend() | PrimaryExpression()
 Type visit(FalseLiteral n, SymbolTable<Type> symtable)
           nodeToken -> "false"
 Type visit(Goal n, SymbolTable<Type> symtable)
           mainClass -> MainClass() nodeListOptional -> ( TypeDeclaration() )* nodeToken -> <EOF>
 Type visit(Identifier n, SymbolTable<Type> symtable)
           nodeToken -> <IDENTIFIER>
 Type visit(IfStatement n, SymbolTable<Type> symtable)
           nodeToken -> "if" nodeToken1 -> "(" expression -> Expression() nodeToken2 -> ")" statement -> Statement() nodeToken3 -> "else" statement1 -> Statement()
 Type visit(IntegerLiteral n, SymbolTable<Type> symtable)
           nodeToken -> <INTEGER_LITERAL>
 Type visit(MainClass n, SymbolTable<Type> symtable)
           nodeToken -> "class" identifier -> Identifier() nodeToken1 -> "{" nodeToken2 -> "public" nodeToken3 -> "static" nodeToken4 -> "void" nodeToken5 -> "main" nodeToken6 -> "(" nodeToken7 -> "String" nodeToken8 -> "[" nodeToken9 -> "]" identifier1 -> Identifier() nodeToken10 -> ")" nodeToken11 -> "{" nodeListOptional -> ( VarDeclaration() )* nodeListOptional1 -> ( Statement() )* nodeToken12 -> "}" nodeToken13 -> "}"
 Type visit(MessageSend n, SymbolTable<Type> symtable)
           primaryExpression -> PrimaryExpression() nodeToken -> "." identifier -> Identifier() nodeToken1 -> "(" nodeOptional -> ( ExpressionList() )? nodeToken2 -> ")"
 Type visit(MethodDeclaration n, SymbolTable<Type> symtable)
           nodeToken -> "public" type -> Type() identifier -> Identifier() nodeToken1 -> "(" nodeOptional -> ( FormalParameterList() )? nodeToken2 -> ")" nodeToken3 -> "{" nodeListOptional -> ( VarDeclaration() )* nodeListOptional1 -> ( Statement() )* nodeToken4 -> "return" expression -> Expression() nodeToken5 -> ";" nodeToken6 -> "}"
 Type visit(MinusExpression n, SymbolTable<Type> symtable)
           primaryExpression -> PrimaryExpression() nodeToken -> "-" primaryExpression1 -> PrimaryExpression()
 Type visit(NotExpression n, SymbolTable<Type> symtable)
           nodeToken -> "!" expression -> Expression()
 Type visit(PlusExpression n, SymbolTable<Type> symtable)
           primaryExpression -> PrimaryExpression() nodeToken -> "+" primaryExpression1 -> PrimaryExpression()
 Type visit(PrimaryExpression n, SymbolTable<Type> symtable)
           nodeChoice -> IntegerLiteral() | TrueLiteral() | FalseLiteral() | Identifier() | ThisExpression() | ArrayAllocationExpression() | AllocationExpression() | NotExpression() | BracketExpression()
 Type visit(PrintStatement n, SymbolTable<Type> symtable)
           nodeToken -> "System.out.println" nodeToken1 -> "(" expression -> Expression() nodeToken2 -> ")" nodeToken3 -> ";"
 Type visit(Statement n, SymbolTable<Type> symtable)
           nodeChoice -> Block() | AssignmentStatement() | ArrayAssignmentStatement() | IfStatement() | WhileStatement() | PrintStatement()
 Type visit(ThisExpression n, SymbolTable<Type> symtable)
           nodeToken -> "this"
 Type visit(TimesExpression n, SymbolTable<Type> symtable)
           primaryExpression -> PrimaryExpression() nodeToken -> "*" primaryExpression1 -> PrimaryExpression()
 Type visit(TrueLiteral n, SymbolTable<Type> symtable)
           nodeToken -> "true"
 Type visit(WhileStatement n, SymbolTable<Type> symtable)
           nodeToken -> "while" nodeToken1 -> "(" expression -> Expression() nodeToken2 -> ")" statement -> Statement()
 
Methods inherited from class minijava.visitor.GJDepthFirst
visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TypeChecker

public TypeChecker()
Method Detail

setCurrentClass

public void setCurrentClass(Class cls)

visit

public Type visit(Goal n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 mainClass -> MainClass()
 nodeListOptional -> ( TypeDeclaration() )*
 nodeToken -> <EOF>
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(MainClass n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "class"
 identifier -> Identifier()
 nodeToken1 -> "{"
 nodeToken2 -> "public"
 nodeToken3 -> "static"
 nodeToken4 -> "void"
 nodeToken5 -> "main"
 nodeToken6 -> "("
 nodeToken7 -> "String"
 nodeToken8 -> "["
 nodeToken9 -> "]"
 identifier1 -> Identifier()
 nodeToken10 -> ")"
 nodeToken11 -> "{"
 nodeListOptional -> ( VarDeclaration() )*
 nodeListOptional1 -> ( Statement() )*
 nodeToken12 -> "}"
 nodeToken13 -> "}"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(ClassDeclaration n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "class"
 identifier -> Identifier()
 nodeToken1 -> "{"
 nodeListOptional -> ( VarDeclaration() )*
 nodeListOptional1 -> ( MethodDeclaration() )*
 nodeToken2 -> "}"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(ClassExtendsDeclaration n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "class"
 identifier -> Identifier()
 nodeToken1 -> "extends"
 identifier1 -> Identifier()
 nodeToken2 -> "{"
 nodeListOptional -> ( VarDeclaration() )*
 nodeListOptional1 -> ( MethodDeclaration() )*
 nodeToken3 -> "}"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(MethodDeclaration n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "public"
 type -> Type()
 identifier -> Identifier()
 nodeToken1 -> "("
 nodeOptional -> ( FormalParameterList() )?
 nodeToken2 -> ")"
 nodeToken3 -> "{"
 nodeListOptional -> ( VarDeclaration() )*
 nodeListOptional1 -> ( Statement() )*
 nodeToken4 -> "return"
 expression -> Expression()
 nodeToken5 -> ";"
 nodeToken6 -> "}"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(Statement n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeChoice -> Block()
       | AssignmentStatement()
       | ArrayAssignmentStatement()
       | IfStatement()
       | WhileStatement()
       | PrintStatement()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(Block n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "{"
 nodeListOptional -> ( Statement() )*
 nodeToken1 -> "}"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(AssignmentStatement n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 identifier -> Identifier()
 nodeToken -> "="
 expression -> Expression()
 nodeToken1 -> ";"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(ArrayAssignmentStatement n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 identifier -> Identifier()
 nodeToken -> "["
 expression -> Expression()
 nodeToken1 -> "]"
 nodeToken2 -> "="
 expression1 -> Expression()
 nodeToken3 -> ";"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(IfStatement n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "if"
 nodeToken1 -> "("
 expression -> Expression()
 nodeToken2 -> ")"
 statement -> Statement()
 nodeToken3 -> "else"
 statement1 -> Statement()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(WhileStatement n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "while"
 nodeToken1 -> "("
 expression -> Expression()
 nodeToken2 -> ")"
 statement -> Statement()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(PrintStatement n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "System.out.println"
 nodeToken1 -> "("
 expression -> Expression()
 nodeToken2 -> ")"
 nodeToken3 -> ";"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(Expression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeChoice -> AndExpression()
       | CompareExpression()
       | PlusExpression()
       | MinusExpression()
       | TimesExpression()
       | ArrayLookup()
       | ArrayLength()
       | MessageSend()
       | PrimaryExpression()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(AndExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 primaryExpression -> PrimaryExpression()
 nodeToken -> "&&"
 primaryExpression1 -> PrimaryExpression()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(CompareExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 primaryExpression -> PrimaryExpression()
 nodeToken -> "<"
 primaryExpression1 -> PrimaryExpression()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(PlusExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 primaryExpression -> PrimaryExpression()
 nodeToken -> "+"
 primaryExpression1 -> PrimaryExpression()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(MinusExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 primaryExpression -> PrimaryExpression()
 nodeToken -> "-"
 primaryExpression1 -> PrimaryExpression()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(TimesExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 primaryExpression -> PrimaryExpression()
 nodeToken -> "*"
 primaryExpression1 -> PrimaryExpression()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(ArrayLookup n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 primaryExpression -> PrimaryExpression()
 nodeToken -> "["
 primaryExpression1 -> PrimaryExpression()
 nodeToken1 -> "]"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(ArrayLength n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 primaryExpression -> PrimaryExpression()
 nodeToken -> "."
 nodeToken1 -> "length"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(MessageSend n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 primaryExpression -> PrimaryExpression()
 nodeToken -> "."
 identifier -> Identifier()
 nodeToken1 -> "("
 nodeOptional -> ( ExpressionList() )?
 nodeToken2 -> ")"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(ExpressionList n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 expression -> Expression()
 nodeListOptional -> ( ExpressionRest() )*
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(ExpressionRest n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> ","
 expression -> Expression()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(PrimaryExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeChoice -> IntegerLiteral()
       | TrueLiteral()
       | FalseLiteral()
       | Identifier()
       | ThisExpression()
       | ArrayAllocationExpression()
       | AllocationExpression()
       | NotExpression()
       | BracketExpression()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(IntegerLiteral n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> <INTEGER_LITERAL>
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(TrueLiteral n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "true"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(FalseLiteral n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "false"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(Identifier n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> <IDENTIFIER>
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(ThisExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "this"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(ArrayAllocationExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "new"
 nodeToken1 -> "int"
 nodeToken2 -> "["
 expression -> Expression()
 nodeToken3 -> "]"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(AllocationExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "new"
 identifier -> Identifier()
 nodeToken1 -> "("
 nodeToken2 -> ")"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(NotExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "!"
 expression -> Expression()
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>

visit

public Type visit(BracketExpression n,
                  SymbolTable<Type> symtable)
Description copied from class: GJDepthFirst
 nodeToken -> "("
 expression -> Expression()
 nodeToken1 -> ")"
 

Specified by:
visit in interface GJVisitor<Type,SymbolTable<Type>>
Overrides:
visit in class GJDepthFirst<Type,SymbolTable<Type>>