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
d5a4bcd8
Commit
d5a4bcd8
authored
5 years ago
by
pycsham
Browse files
Options
Downloads
Patches
Plain Diff
implemented method return type checking
parent
af6723ea
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
AST.py
+18
-0
18 additions, 0 deletions
AST.py
LineNodes.py
+1
-1
1 addition, 1 deletion
LineNodes.py
MemberNodes.py
+30
-2
30 additions, 2 deletions
MemberNodes.py
with
49 additions
and
3 deletions
AST.py
+
18
−
0
View file @
d5a4bcd8
...
...
@@ -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
This diff is collapsed.
Click to expand it.
LineNodes.py
+
1
−
1
View file @
d5a4bcd8
...
...
@@ -209,7 +209,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:
...
...
This diff is collapsed.
Click to expand it.
MemberNodes.py
+
30
−
2
View file @
d5a4bcd8
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
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