Sketch for #50 - branch mangler
This is only slightly better. The insert point needs to walk with the for loop.
This commit is contained in:
parent
b2406a9d72
commit
256506ede0
@ -20,15 +20,22 @@
|
||||
|
||||
from hy.macros import process as mprocess
|
||||
|
||||
import hy.mangle
|
||||
|
||||
|
||||
MACROS = [
|
||||
"hy.core.bootstrap", # defn, cond
|
||||
"hy.core.bootstrap",
|
||||
"hy.core.mangles",
|
||||
]
|
||||
|
||||
|
||||
def process(tree):
|
||||
load_macros()
|
||||
return mprocess(tree)
|
||||
tree = mprocess(tree)
|
||||
for m in hy.mangle.MANGLES:
|
||||
m().mangle(tree)
|
||||
print tree
|
||||
return tree
|
||||
|
||||
|
||||
def load_macros():
|
||||
|
52
hy/core/mangles.py
Normal file
52
hy/core/mangles.py
Normal file
@ -0,0 +1,52 @@
|
||||
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a
|
||||
# copy of this software and associated documentation files (the "Software"),
|
||||
# to deal in the Software without restriction, including without limitation
|
||||
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
# and/or sell copies of the Software, and to permit persons to whom the
|
||||
# Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
# DEALINGS IN THE SOFTWARE.
|
||||
|
||||
from hy.models.expression import HyExpression
|
||||
from hy.models.symbol import HySymbol
|
||||
|
||||
import hy.mangle
|
||||
|
||||
|
||||
class FunctionMangle(hy.mangle.Mangle):
|
||||
hoistable = ["fn"]
|
||||
|
||||
def __init__(self):
|
||||
self.series = 0
|
||||
|
||||
def unique_name(self):
|
||||
self.series += 1
|
||||
return "_hy_hoisted_fn_%s" % (self.series)
|
||||
|
||||
def visit(self, tree):
|
||||
if isinstance(tree, HyExpression):
|
||||
call = tree[0]
|
||||
if isinstance(call, HyExpression) and len(call) != 0:
|
||||
what = call[0]
|
||||
if what in self.hoistable:
|
||||
name = self.unique_name()
|
||||
call = HyExpression([HySymbol("def"), name, call])
|
||||
self.hoist(call)
|
||||
tree.pop(0)
|
||||
entry = HySymbol(name)
|
||||
entry.replace(tree)
|
||||
tree.insert(0, entry)
|
||||
raise self.TreeChanged()
|
||||
|
||||
hy.mangle.MANGLES.append(FunctionMangle)
|
99
hy/mangle.py
Normal file
99
hy/mangle.py
Normal file
@ -0,0 +1,99 @@
|
||||
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a
|
||||
# copy of this software and associated documentation files (the "Software"),
|
||||
# to deal in the Software without restriction, including without limitation
|
||||
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
# and/or sell copies of the Software, and to permit persons to whom the
|
||||
# Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
# DEALINGS IN THE SOFTWARE.
|
||||
|
||||
from hy.models.expression import HyExpression
|
||||
# from hy.models.list import HyList
|
||||
|
||||
MANGLES = []
|
||||
|
||||
|
||||
class Mangle(object):
|
||||
"""
|
||||
Mangle (n.)
|
||||
|
||||
1. To mutilate or disfigure by battering, hacking, cutting,
|
||||
or tearing. See Synonyms at batter1.
|
||||
|
||||
(but mostly hacking)
|
||||
"""
|
||||
|
||||
class TreeChanged(Exception):
|
||||
pass
|
||||
|
||||
def _mangle(self, tree):
|
||||
# Things that force a scope push to go into:
|
||||
#
|
||||
# - Functions
|
||||
# - If
|
||||
scopable = ["fn", "if"]
|
||||
scoped = False
|
||||
|
||||
if isinstance(tree, HyExpression):
|
||||
what = tree[0]
|
||||
if what in scopable:
|
||||
self.push_scope(tree)
|
||||
scoped = True
|
||||
|
||||
if isinstance(tree, list):
|
||||
for element in tree:
|
||||
self.visit(element)
|
||||
self._mangle(element)
|
||||
|
||||
if scoped:
|
||||
self.pop_scope()
|
||||
|
||||
def hoist(self, what):
|
||||
#print "HOIST: "
|
||||
#print " --> (fro) ", what
|
||||
#print " --> (to) ", self.scope
|
||||
scope = self.scope
|
||||
point = 0
|
||||
|
||||
if isinstance(scope, HyExpression) and len(scope):
|
||||
if scope[0] == 'fn':
|
||||
point = 3
|
||||
|
||||
self.scope.insert(point, what)
|
||||
#print " --> (aft) ", self.scope
|
||||
|
||||
def get_scope(self):
|
||||
return self.scopes[0]
|
||||
|
||||
@property
|
||||
def scope(self):
|
||||
return self.get_scope()
|
||||
|
||||
def push_scope(self, tree):
|
||||
self.scopes.insert(0, tree)
|
||||
|
||||
def pop_scope(self):
|
||||
return self.scopes.pop(0)
|
||||
|
||||
def mangle(self, tree):
|
||||
unfinished = True
|
||||
while unfinished:
|
||||
self.root = tree
|
||||
self.scopes = []
|
||||
self.push_scope(tree)
|
||||
try:
|
||||
self._mangle(tree)
|
||||
unfinished = False
|
||||
except self.TreeChanged:
|
||||
pass
|
Loading…
x
Reference in New Issue
Block a user