Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs444
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Xun Yang
cs444
Commits
69a84e4c
Commit
69a84e4c
authored
5 years ago
by
Xun Yang
Browse files
Options
Downloads
Patches
Plain Diff
forward ref checks
parent
a58bdd2a
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
MemberNodes.py
+40
-4
40 additions, 4 deletions
MemberNodes.py
with
40 additions
and
4 deletions
MemberNodes.py
+
40
−
4
View file @
69a84e4c
...
...
@@ -37,6 +37,20 @@ class FieldNode(ASTNode):
def
checkType
(
self
):
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):
self
.
env
=
env
return
env
def
disambigName
(
self
):
if
self
.
body
:
self
.
body
.
disambigName
()
for
p
in
self
.
params
:
p
.
disambigName
()
def
checkType
(
self
):
if
self
.
methodType
:
# constructor would be None
self
.
myType
=
self
.
methodType
.
myType
...
...
@@ -118,7 +138,7 @@ class MethodNode(ASTNode):
# 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
)
...
...
@@ -127,7 +147,7 @@ class MethodNode(ASTNode):
# 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
))
raise
Exception
(
"
ERROR: no return statement at function {}
"
.
format
(
self
.
name
))
# Checking for cases where there are return statements
for
n
in
returnNodes
:
...
...
@@ -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;)
if
self
.
myType
.
name
==
"
void
"
:
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
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
return
############# 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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment