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

Merge branch 'master' of git.uwaterloo.ca:x299yang/cs444

parents c00e7ad8 46a36c31
No related branches found
No related tags found
No related merge requests found
......@@ -108,3 +108,21 @@ def getParseTreeNodes(names, tree, terminateList = []):
else:
result.extend(getParseTreeNodes(names, n, terminateList))
return result
def getASTNode(names, AST):
result = []
if not AST:
return result
if AST.__class__.__name__ in names:
result.append(AST)
return result
if not AST.children:
return []
for n in AST.children:
if not n:
continue
if n.__class__.__name__ in names:
result.append(n)
else:
result.extend(getASTNode(names, n))
return result
......@@ -213,7 +213,7 @@ class ReturnNode(ASTNode):
self.expr.checkType()
self.myType = self.expr.myType
else:
self.myType = TypeStruct("void", None)
self.myType = None # this is None as returning a value of type Void is invalid even in a function with type Void
# forStatement and forStatementNoShortIf
# Rules:
......
from AST import ASTNode, getParseTreeNodes
from AST import ASTNode, getParseTreeNodes, getASTNode
from LineNodes import BlockNode, VarDclNode
from ExprPrimaryNodes import makeNodeFromExpr
from UnitNodes import ParamNode
......@@ -107,9 +107,37 @@ class MethodNode(ASTNode):
return env
def checkType(self):
if self.methodType: # constructor
if self.methodType: # constructor would be None
self.myType = self.methodType.myType
for p in self.params:
p.checkType()
if self.body:
self.body.checkType()
# Checking return types against the function type
# No method body: do not check type as function isn't implemented
if not self.body:
return
# With method body
returnNodes = getASTNode(["ReturnNode"], self.body)
# Checking for cases where there are no return statements
if not returnNodes:
# Either a constructor or the function has type Void
if not self.methodType or self.myType.name == "void":
return
raise Exception("ERROR: no return statement at function {}".format(self.name))
# Checking for cases where there are return statements
for n in returnNodes:
# Checking for functions of type void
# Only valid if either the function doesn't have a return statement(checked above), or the return statement is a semicolon (return;)
if self.myType.name == "void":
if n.myType:
raise Exception("ERROR: return type of function {} doesn't match with return statement.".format(self.name))
# Checking for non void cases
if not self.myType.assignable(n.myType):
raise Exception("ERROR: return type of function {} doesn't match with return statement.".format(self.name))
return
\ No newline at end of file
......@@ -25,9 +25,9 @@ class ClassInterNode(ASTNode):
if self.superInter:
unique = []
for inter in self.superInter:
if inter.name in unique:
raise Exception("ERROR: Class/Interface '{}' implements duplicate interfaces '{}'".format(self.name, inter.name))
unique.append(inter.name)
if inter.canonName in unique:
raise Exception("ERROR: Class/Interface '{}' implements duplicate interfaces '{}'".format(self.name, inter.canonName))
unique.append(inter.canonName)
# 7. A class or interface must not declare two methods with the same signature (name and parameter types).
# 9. A class/interface must not contain two methods with the same signature but different return types
......
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