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
b8881df4
Commit
b8881df4
authored
5 years ago
by
Xun Yang
Browse files
Options
Downloads
Patches
Plain Diff
still working on it
parent
b4745ba8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ASTNode.py
+30
-2
30 additions, 2 deletions
ASTNode.py
Environment.py
+27
-28
27 additions, 28 deletions
Environment.py
with
57 additions
and
30 deletions
ASTNode.py
+
30
−
2
View file @
b8881df4
from
Environment
import
Environment
class
Environment
:
def
__init__
(
self
,
parent
):
self
.
parent
=
parent
# pointer to parent environment
self
.
map
=
{}
# 2D map containing namespaces and names under them
# add name to Env
def
addName
(
self
,
name
,
namespace
,
node
):
if
namespace
in
self
.
map
and
name
in
self
.
map
[
namespace
]:
raise
IOException
if
namespace
in
self
.
map
:
self
.
map
[
namespace
][
name
]
=
node
else
:
self
.
map
[
namespace
]
=
{
name
:
node
}
# returns AST node that is associate with the name, None if name is not defined
def
getDef
(
self
,
name
):
if
name
not
in
self
.
map
:
if
not
self
.
parent
:
raise
IOException
return
self
.
parent
.
getDef
(
name
)
return
self
.
map
[
name
]
###################################################
# base class for all ASTNodes
# define some basic operation for overriding later
class
ASTNode
:
def
__init__
(
self
,
parseTree
,
parentEnv
):
self
.
parseTree
=
parse
Nod
e
# stores the parse tree in case we want to do sth later
self
.
parseTree
=
parse
Tre
e
# stores the parse tree in case we want to do sth later
self
.
env
=
parentEnv
def
nameResolv
(
self
):
...
...
@@ -18,4 +42,8 @@ class ScopeNode(ASTNode):
def
__init__
(
self
,
parseTree
,
parentEnv
):
super
().
__init__
(
parseTree
,
parentEnv
)
self
.
env
=
Environment
(
parentEnv
)
for
n
in
parseTree
.
children
:
if
n
.
name
==
'
ID
'
:
self
.
name
=
n
.
lex
break
This diff is collapsed.
Click to expand it.
Environment.py
+
27
−
28
View file @
b8881df4
class
Enviroment
:
def
__init__
(
self
,
parent
):
self
.
parent
=
parent
# pointer to parent environment
self
.
map
=
{}
# 2D map containing namespaces and names under them
from
ASTNode
import
ScopeNode
,
Environment
# add name to Env
def
addName
(
self
,
name
,
namespace
,
node
):
if
namespace
in
self
.
map
and
name
in
self
.
map
[
namespace
]:
raise
IOException
if
namespace
in
self
.
map
:
self
.
map
[
namespace
][
name
]
=
node
else
:
self
.
map
[
namespace
]
=
{
name
:
node
}
# returns AST node that is associate with the name, None if name is not defined
def
getDef
(
self
,
name
):
if
name
not
in
self
.
map
:
if
not
self
.
parent
:
raise
IOException
return
self
.
parent
.
getDef
(
name
)
return
self
.
map
[
name
]
##################################################################
# give a list of parseTrees, returns the global Environemnt object and
# a list of classNodes with globalEnv attached to them
def
buildGlobalEnv
(
parseTrees
):
gEnv
=
Enviroment
(
None
)
gEnv
=
Enviro
n
ment
(
None
)
ASTs
=
[]
for
t
in
parseTrees
:
# add imports
imports
=
getParseTreeNodes
([
'
importDcl
'
],
t
)
for
i
in
imports
:
importNames
=
getParseTreeNodes
([
'
ID
'
,
'
COMPID
'
,
'
IMPORTALL
'
])
importNames
=
getParseTreeNodes
([
'
ID
'
,
'
COMPID
'
,
'
IMPORTALL
'
]
,
i
)
for
n
in
importNames
:
gEnv
.
addName
(
n
.
lex
,
'
package
'
,
None
)
# TODO: figure out package name
packages
=
getParseTreeNodes
(
'
packageDcl
'
,
t
)
pNames
=
[]
for
p
in
packages
:
pNames
.
extend
(
getParseTreeNodes
([
'
ID
'
,
'
COMPID
'
],
p
))
if
pNames
:
pNames
=
pNames
[
0
]
else
:
pNames
=
'
no packageName
'
# add class to global Env
packageName
=
getParseTreeNodes
(
'
packageDcl
'
,
t
)
classDcl
=
getParseTreeNodes
(
'
classDcl
'
,
t
)
interfaceDcl
=
getParseTreeNodes
(
'
interfaceDcl
'
,
t
)
if
not
classDcl
:
intNode
=
Interfac
eNode
(
interfaceDcl
[
0
],
gEnv
)
intNode
=
Scop
eNode
(
interfaceDcl
[
0
],
gEnv
)
ASTs
.
append
(
intNode
)
gEnv
.
addName
(
intNode
.
name
,
'
interface
'
,
intNode
)
gEnv
.
addName
(
pNames
+
"
"
+
intNode
.
name
,
'
interface
'
,
intNode
)
else
:
classNode
=
Class
Node
(
classDcl
[
0
],
gEnv
)
classNode
=
Scope
Node
(
classDcl
[
0
],
gEnv
)
ASTs
.
append
(
classNode
)
gEnv
.
addName
(
classNode
.
name
,
'
class
'
,
classNode
)
gEnv
.
addName
(
pNames
+
"
"
+
classNode
.
name
,
'
class
'
,
classNode
)
#prints
print
(
'
----------- global env ------------
'
)
for
g
in
gEnv
.
map
:
print
(
g
)
for
gg
in
gEnv
.
map
[
g
]:
print
(
gg
)
print
(
'
----------- ASTs -----------------
'
)
for
a
in
ASTs
:
print
(
a
.
name
)
return
gEnv
,
ASTs
# Utils: given a parseTree and a list of names, traverse the tree
...
...
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