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

still working on it

parent b4745ba8
No related branches found
No related tags found
No related merge requests found
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 = parseNode # stores the parse tree in case we want to do sth later
self.parseTree = parseTree # 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
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 = Environment(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 = InterfaceNode(interfaceDcl[0], gEnv)
intNode = ScopeNode(interfaceDcl[0], gEnv)
ASTs.append(intNode)
gEnv.addName(intNode.name, 'interface', intNode)
gEnv.addName(pNames+ " " + intNode.name, 'interface', intNode)
else:
classNode = ClassNode(classDcl[0], gEnv)
classNode = ScopeNode(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
......
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