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

some nodes

parent 1640c9da
No related branches found
No related tags found
No related merge requests found
......@@ -35,18 +35,17 @@ class CompNode(ASTNode):
def buildEnv(self, parentEnv):
env = Env(None) # global environment is not the parent Env, each file don't have access to all the stuff in global env
env.addtoEnv(self.typeDcl)
# add imports
for i in self.importNames:
pName = i.split('.*') # check if it's import all
if len(pName) == 2:
nodes = parentEnv.getNodesByPackage(pName[0])
for k, v in nodes:
if k in env.map:
raise Exception('ERROR: Declaration of {} is already in current Environment, cannot import it again'.format(pName + '.' + typeDcl.name))
env.map[k] = v
else:
node = parentEnv.getNode(pName[0])
env.addtoEnv(node)
# # add imports
# for i in self.importNames:
# pName = i.split('.*') # check if it's import all
# if len(pName) == 2:
# nodes = parentEnv.getNodesByPackage(pName[0])
# for k, v in nodes:
# if k in env.map:
# raise Exception('ERROR: Declaration of {} is already in current Environment, cannot import it again'.format(pName + '.' + typeDcl.name))
# env.map[k] = v
# else:
# node = parentEnv.getNode(pName[0])
# env.addtoEnv(node)
self.env = env
return env
from AST import ASTNode, getParseTreeNodes, getTypeName
from Environment import Env
from WordNodes import ExprNode
# expr
class ExprNode(ASTNode):
# containing line level nodes: block, for/while/if, declaration
# block
class BlockNode(ASTNode):
# always list all fields in the init method to show the class structure
def __init__(self, parseTree):
self.parseTree = parseTree
self.statements = StatementsNode(parseTree.children[1]) # block LBRACK statements RBRACK
self.env = None
self.children = self.statements
# a wrapper around statementsNode use to check double declaration within a block
class VarDclNode(ASTNode):
# always list all fields in the init method to show the class structure
def __init__(self, parseTree):
self.parseTree = parseTree
self.left = ''
self.op = ''
self.right = None # another expr
self.dclType = ''
self.name = ''
self.assign = None # AssignNode, can be None
self.statements = [] # list of nodes representing statements in a block
self.env = None
self.children = []
# input is : statement type variableDcl SEMICO
# def buildEnv(self, parentEnv):
# env = Env(parentEnv)
# # env.addtoEnv()
#
#
# self.env = env
# return env
# block
class BlockNode(ASTNode):
class ForNode(ASTNode):
# always list all fields in the init method to show the class structure
def __init__(self, parseTree):
self.parseTree = parseTree
self.statements = [] # list of statements in a block
self.statements = [] # list of nodes representing statements in a block
self.env = None
self.children = []
# def buildEnv(self, parentEnv):
# env = Env(parentEnv)
# # env.addtoEnv()
#
# self.env = env
# return env
from AST import ASTNode, getParseTreeNodes, getTypeName
from LineNodes import ExprNode, BlockNode
from LineNodes import BlockNode
from WordNodes import ExprNode
from Environment import Env
from collections import OrderedDict
......
......@@ -52,11 +52,20 @@ class ClassNode(ASTNode):
def buildEnv(self, parentEnv):
env = Env(parentEnv)
for c in self.children:
env.addtoEnv(c)
for m in self.methods:
env.addtoEnv(m)
for f in self.fields:
env.addtoEnv(f)
# not adding constructor to the environment, since it's in the type namespace
# when looking for a constructor, look for a class with the same name, and look in its constructors field
self.env = env
return env
def getConstructor(self, argTypes):
for c in self.constructors:
if c.paramTypes == argTypes:
return c
#####################################################################
# interface
class InterNode(ASTNode):
......
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