Skip to content
Snippets Groups Projects
Commit 80f03bdb authored by Xun Yang's avatar Xun Yang
Browse files

type checking lineNodes, fix classCreate type check

parent 5cca2174
No related branches found
No related tags found
No related merge requests found
...@@ -230,7 +230,9 @@ class ClassCreateNode(ASTNode): ...@@ -230,7 +230,9 @@ class ClassCreateNode(ASTNode):
self.cons = None # the constructor used to create the class self.cons = None # the constructor used to create the class
def checkType(self): def checkType(self):
return # TO REMOVE after name node type checking is done # return # TO REMOVE after name node type checking is done
self.args.checkType()
classDef = self.className.myType.typePointer classDef = self.className.myType.typePointer
# check class is not abstract # check class is not abstract
if 'abstract' in classDef.mods: if 'abstract' in classDef.mods:
......
from AST import ASTNode, getParseTreeNodes from AST import ASTNode, getParseTreeNodes
from Environment import Env from Environment import Env
from ExprPrimaryNodes import makeNodeFromExpr, makeNodeFromAllPrimary, MethodInvNode from ExprPrimaryNodes import makeNodeFromExpr, makeNodeFromAllPrimary, MethodInvNode
from TheTypeNode import TypeNode from TheTypeNode import TypeNode, TypeStruct
# Contains: # Contains:
# block # block
...@@ -121,6 +121,14 @@ class VarDclNode(ASTNode): ...@@ -121,6 +121,14 @@ class VarDclNode(ASTNode):
env.addtoEnv(self) env.addtoEnv(self)
return self.env return self.env
def checkType1(self):
self.myType = self.dclType.myType
if self.variableInit:
self.variableInit.checkType()
if not self.myType.assignable(self.variableInit.myType):
raise Exception("ERROR: Cannot initialize variable of type {} with type {}".format(self.myType.name, self.variableInit.myType.name))
# ifStatement, ifElseStatement, ifElseStatementNoShortIf # ifStatement, ifElseStatement, ifElseStatementNoShortIf
# Rules: # Rules:
# 1. ifStatement IF LPAREN expr RPAREN statement # 1. ifStatement IF LPAREN expr RPAREN statement
...@@ -145,6 +153,15 @@ class IfNode(ASTNode): ...@@ -145,6 +153,15 @@ class IfNode(ASTNode):
self.children.append(self.ifBody) self.children.append(self.ifBody)
self.children.append(self.elseBody) self.children.append(self.elseBody)
def checkType1(self):
self.ifConditional.checkType()
if self.ifConditional.myType.name != 'boolean':
raise Exception("ERROR: Cannot use non-boolean type for ifConditional.")
self.ifBody.checkType()
if self.elseBody:
self.elseBody.checkType()
# whileStatement, whileStatementNoShortIf # whileStatement, whileStatementNoShortIf
# Rules: # Rules:
# 1. whileStatement WHILE LPAREN expr RPAREN statement # 1. whileStatement WHILE LPAREN expr RPAREN statement
...@@ -164,6 +181,12 @@ class WhileNode(ASTNode): ...@@ -164,6 +181,12 @@ class WhileNode(ASTNode):
self.children.append(self.whileBound) self.children.append(self.whileBound)
self.children.append(self.whileBody) self.children.append(self.whileBody)
def checkType1(self):
self.whileBound.checkType()
if self.whileBound.myType.name != 'boolean':
raise Exception("ERROR: Cannot use non-boolean type for whileBound.")
self.whileBody.checkType()
# returnStatement # returnStatement
# Rules: # Rules:
# 1. returnStatement RETURN expr SEMICO # 1. returnStatement RETURN expr SEMICO
...@@ -181,6 +204,13 @@ class ReturnNode(ASTNode): ...@@ -181,6 +204,13 @@ class ReturnNode(ASTNode):
self.children.append(self.expr) self.children.append(self.expr)
def checkType1(self):
if self.expr:
self.expr.checkType()
self.myType = self.expr.myType
else:
self.myType = TypeStruct("void", None)
# forStatement and forStatementNoShortIf # forStatement and forStatementNoShortIf
# Rules: # Rules:
# 1. forStatement FOR LPAREN forInit SEMICO forExpr SEMICO forInit RPAREN statement # 1. forStatement FOR LPAREN forInit SEMICO forExpr SEMICO forInit RPAREN statement
...@@ -235,3 +265,11 @@ class ForNode(ASTNode): ...@@ -235,3 +265,11 @@ class ForNode(ASTNode):
self.children.append(self.forBound) self.children.append(self.forBound)
self.children.append(self.forUpdate) self.children.append(self.forUpdate)
self.children.append(self.bodyStatement) self.children.append(self.bodyStatement)
def checkType1(self):
self.forInit.checkType()
self.forBound.checkType() # need resolving var declared in forInit to use it in forBound
if self.forBound.myType.name != 'boolean':
raise Exception("ERROR: Cannot use non-boolean type for forBound.")
self.forUpdate.checkType()
self.bodyStatement.checkType()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment