Mangler cosmetic cleanup

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2013-04-19 18:21:21 -07:00
parent aadf47ed99
commit c0baea80dc

View File

@ -18,6 +18,8 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE. # DEALINGS IN THE SOFTWARE.
import abc
from hy.models.expression import HyExpression from hy.models.expression import HyExpression
# from hy.models.list import HyList # from hy.models.list import HyList
@ -34,6 +36,8 @@ class Mangle(object):
(but mostly hacking) (but mostly hacking)
""" """
__metaclass___ = abc.ABCMeta
class TreeChanged(Exception): class TreeChanged(Exception):
""" """
This exception gets raised whenver any code alters the tree. This is This exception gets raised whenver any code alters the tree. This is
@ -42,15 +46,16 @@ class Mangle(object):
""" """
pass pass
@abc.abstractmethod
def visit(self, element):
raise NotImplementedError
def _mangle(self, tree): def _mangle(self, tree):
""" """
Main function of self.mangle, which is called over and over. This Main function of self.mangle, which is called over and over. This
is used to beat the tree until it stops moving. is used to beat the tree until it stops moving.
""" """
scopable = ["fn", "if"]
# Not actually scope, more like code branch.
scoped = False scoped = False
self.push_stack(tree) self.push_stack(tree)
@ -58,7 +63,7 @@ class Mangle(object):
# If it's an expression, let's make sure we reset the "scope" # If it's an expression, let's make sure we reset the "scope"
# (code branch) if it's a scopable object. # (code branch) if it's a scopable object.
what = tree[0] what = tree[0]
if what in scopable: if what in ["fn", "if"]:
self.push_scope(tree) self.push_scope(tree)
scoped = True scoped = True
@ -114,20 +119,19 @@ class Mangle(object):
return self.stack.pop(0) return self.stack.pop(0)
def mangle(self, tree): def mangle(self, tree):
""" """Magic external entry point.
Magic external entry point.
We mangle until the tree stops moving, i.e. until we don't get a
TreeChanged Exception during mangle.
We mangle until the tree stops moving (we don't get a TreeChanged
Exception during mangle)
""" """
unfinished = True while True:
while unfinished:
self.root = tree self.root = tree
self.scopes = [] self.scopes = []
self.stack = [] self.stack = []
self.push_scope(tree) self.push_scope(tree)
try: try:
self._mangle(tree) self._mangle(tree)
unfinished = False break
except self.TreeChanged: except self.TreeChanged:
pass pass