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

type checking for comparison and operations

parent 9ecbf314
No related branches found
No related tags found
No related merge requests found
...@@ -197,24 +197,66 @@ class ExprNode(ASTNode): ...@@ -197,24 +197,66 @@ class ExprNode(ASTNode):
self.children = [] self.children = []
if parseTree.name == 'unaryNotPlusMinus' or parseTree.name == 'unaryExpr': if parseTree.name == 'unaryNotPlusMinus' or parseTree.name == 'unaryExpr':
self.op = parseTree.children[0].name self.op = parseTree.children[0].lex
self.right = makeNodeFromExpr(parseTree.children[1]) self.right = makeNodeFromExpr(parseTree.children[1])
else: else:
self.left = makeNodeFromExpr(parseTree.children[0]) self.left = makeNodeFromExpr(parseTree.children[0])
self.op = parseTree.children[1].name self.op = parseTree.children[1].lex
self.right = makeNodeFromExpr(parseTree.children[2]) self.right = makeNodeFromExpr(parseTree.children[2])
self.children.append(self.left) self.children.append(self.left)
self.children.append(self.right) self.children.append(self.right)
def checkType(self): # use wrong name to stop method from being called until we finish other implemetation
# def checkType(self):
def checkType1(self):
# steps of type checking:
# check children's types (children's myType field will be populated with a typeStruct)
# check using the rule for current node
# make a TypeStruct node and populate myType field for self
super().checkType() # check children's type first to populate their myType field super().checkType() # check children's type first to populate their myType field
if self.op == '==' or self.op == '!=': print(self.left)
if (self.left.myType == self.right.myType) or (self.left.myType.isNum() and self.right.myType.isNum()): # Unary operations:
if not self.left:
if self.op == '-' and self.right.myType.isNum():
self.myType = TypeStruct("int")
return
elif self.op == '!' and self.right.myType.name == 'boolean':
self.myType = self.myType = TypeStruct("boolean")
return
# Numeric types
elif self.left.myType.isNum() and self.right.myType.isNum():
# Comparisons:
if self.op in ['==', '!=', '<=', '>=', '>', '<']:
self.myType = TypeStruct("boolean")
return
# numeric operations:
elif self.op in ['+', '-', '*', '/']:
self.myType = TypeStruct("int")
return
#
# Other Comparisons:
elif self.left.myType.assignable(self.right.myType) or self.right.myType.assignable(self.left.myType):
if self.op == '==' or self.op == '!=':
self.myType = TypeStruct("boolean") self.myType = TypeStruct("boolean")
else: return
raise Exception('ERROR: Incompatible types for comparison.')
# TODO: type check other types of expr # String concat:
elif (self.left.myType.name =='java.lang.String' and self.right.myType.name not in ['null', 'void']) \
or (self.right.myType.name =='java.lang.String' and self.left.myType.name not in ['null', 'void']):
self.myType = TypeStruct('java.lang.String')
self.myType.link(self.env)
return
elif self.op == 'instanceof':
# assume it's correct for now, wait for runtime check
self.myType = TypeStruct("boolean")
return
raise Exception("ERROR: Incompatible types. Left of {} type can't be used with right of {} type on operation {}".format(self.op, self.left.myType.name, self.right.myType.name))
......
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