diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index 9f62d5954e8843e039d20ff8405fbdba03a74f09..b13252af1ff850e84da36eec22afde51d59996a0 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -197,24 +197,66 @@ class ExprNode(ASTNode): self.children = [] 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]) else: 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.children.append(self.left) 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 - if self.op == '==' or self.op == '!=': - if (self.left.myType == self.right.myType) or (self.left.myType.isNum() and self.right.myType.isNum()): + print(self.left) + # 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") - else: - raise Exception('ERROR: Incompatible types for comparison.') - # TODO: type check other types of expr + return + + # 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)) +