Created
February 8, 2015 15:54
-
-
Save SeavantUUz/7bd924d2fc8585ed42d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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