Skip to content

Instantly share code, notes, and snippets.

@SeavantUUz
Created February 8, 2015 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeavantUUz/7bd924d2fc8585ed42d9 to your computer and use it in GitHub Desktop.
Save SeavantUUz/7bd924d2fc8585ed42d9 to your computer and use it in GitHub Desktop.
def func_environment():
env = dict()
def symbol(identify, bp=0, auto_commit = True):
try:
s = env[identify]
except Exception:
class s(Token):
pass
if auto_commit:
s.__name__ = 'symbol-{}'.format(identify)
s.id = identify
s.lbp = bp
env[identify] = s
else:
s = None
else:
s.lbp = max(s.lbp, bp)
return s
return symbol
class environment(object):
def __init__(self, parent=None):
self.env = dict()
self.parent = parent
self.child = None
def __call__(self, identify, bp=0, auto_commit = True):
s = self.env.get(identify)
if not s:
s = self._search_identify(identify)
if not s:
class s(Token):
pass
if auto_commit:
s.__name__ = 'symbol-{}'.format(identify)
s.id = identify
s.lbp = bp
self.env[identify] = s
else:
s = None
else:
s.lbp = max(s.lbp, bp)
else:
s.lbp = max(s.lbp, bp)
return s
def _search_identify(self, identify):
if not self.parent:
return None
else:
parent = self.parent
value = parent.env.get(identify)
if value:
return value
else:
return parent._search_identify(identify)
def fork_env(self):
_new_env = type(self)(self)
self.child_env = _new_env
return _new_env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment