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

forward ref checks

parent a58bdd2a
No related branches found
No related tags found
No related merge requests found
...@@ -37,6 +37,20 @@ class FieldNode(ASTNode): ...@@ -37,6 +37,20 @@ class FieldNode(ASTNode):
def checkType(self): def checkType(self):
self.variableDcl.checkType() self.variableDcl.checkType()
# check forward reference
if self.variableDcl.variableInit:
allNames = getForwardRefNames(self.variableDcl.variableInit)
from pprint import pprint
for n in allNames:
if n.prefixLink is self.variableDcl:
raise Exception("ERROR: Forward reference of field {} in itself is not allowed.".format(n.prefixLink.name))
if n.prefixLink.__class__.__name__ == 'FieldNode' \
and n.prefixLink.typeName == self.typeName \
and self.order <= n.prefixLink.order \
and "this" not in n.name:
raise Exception("ERROR: Forward reference of field {} is not allowed.".format(n.prefixLink.name))
########################################################### ###########################################################
...@@ -106,6 +120,12 @@ class MethodNode(ASTNode): ...@@ -106,6 +120,12 @@ class MethodNode(ASTNode):
self.env = env self.env = env
return env return env
def disambigName(self):
if self.body:
self.body.disambigName()
for p in self.params:
p.disambigName()
def checkType(self): def checkType(self):
if self.methodType: # constructor would be None if self.methodType: # constructor would be None
self.myType = self.methodType.myType self.myType = self.methodType.myType
...@@ -118,7 +138,7 @@ class MethodNode(ASTNode): ...@@ -118,7 +138,7 @@ class MethodNode(ASTNode):
# No method body: do not check type as function isn't implemented # No method body: do not check type as function isn't implemented
if not self.body: if not self.body:
return return
# With method body # With method body
returnNodes = getASTNode(["ReturnNode"], self.body) returnNodes = getASTNode(["ReturnNode"], self.body)
...@@ -127,7 +147,7 @@ class MethodNode(ASTNode): ...@@ -127,7 +147,7 @@ class MethodNode(ASTNode):
# Either a constructor or the function has type Void # Either a constructor or the function has type Void
if not self.methodType or self.myType.name == "void": if not self.methodType or self.myType.name == "void":
return return
raise Exception("ERROR: no return statement at function {}".format(self.name)) raise Exception("ERROR: no return statement at function {}".format(self.name))
# Checking for cases where there are return statements # Checking for cases where there are return statements
for n in returnNodes: for n in returnNodes:
...@@ -135,9 +155,25 @@ class MethodNode(ASTNode): ...@@ -135,9 +155,25 @@ class MethodNode(ASTNode):
# Only valid if either the function doesn't have a return statement(checked above), or the return statement is a semicolon (return;) # 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 self.myType.name == "void":
if n.myType: if n.myType:
raise Exception("ERROR: return type of function {} doesn't match with return statement.".format(self.name)) raise Exception("ERROR: return type of function {} doesn't match with return statement.".format(self.name))
# Checking for non void cases # Checking for non void cases
if not self.myType.assignable(n.myType): if not self.myType.assignable(n.myType):
raise Exception("ERROR: return type of function {} doesn't match with return statement.".format(self.name)) raise Exception("ERROR: return type of function {} doesn't match with return statement.".format(self.name))
return return
\ No newline at end of file
############# helper for forward ref checking ########
# Input: AST Node
# Output: A list of names to be check
def getForwardRefNames(node):
if node.__class__.__name__ == 'NameNode':
return [node]
result = []
if node.__class__.__name__ == 'AssignNode':
result.extend(getForwardRefNames(node.right))
else:
for c in node.children:
result.extend(getForwardRefNames(c))
return result
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