Adding in an If mangle

This commit is contained in:
Paul R. Tagliamonte 2013-04-06 11:10:33 -04:00
parent 3ab7153398
commit 4b57fd0a51
2 changed files with 37 additions and 11 deletions

View File

@ -20,21 +20,12 @@
from hy.models.expression import HyExpression
from hy.models.symbol import HySymbol
from hy.models.list import HyList
import hy.mangle
class FunctionMangle(hy.mangle.Mangle):
hoistable = ["fn"]
ignore = ["def", "decorate_with", "setf", "setv"]
def __init__(self):
self.series = 0
def unique_name(self):
self.series += 1
return "_hy_hoisted_fn_%s" % (self.series)
class HoistableMangle(hy.mangle.Mangle):
def should_hoist(self):
for frame in self.stack:
if frame is self.scope:
@ -47,6 +38,18 @@ class FunctionMangle(hy.mangle.Mangle):
return True
return False
class FunctionMangle(HoistableMangle):
hoistable = ["fn"]
ignore = ["def", "decorate_with", "setf", "setv"]
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) and tree != []:
call = tree[0]
@ -60,4 +63,22 @@ class FunctionMangle(hy.mangle.Mangle):
self.hoist(fn_def)
return new_name
class IfMangle(HoistableMangle):
ignore = []
def __init__(self):
self.series = 0
def visit(self, tree):
if isinstance(tree, HyExpression) and tree != []:
call = tree[0]
if call == "if" and self.should_hoist():
fn = HyExpression([HyExpression([HySymbol("fn"),
HyList([]),
tree])])
fn.replace(tree)
return fn
hy.mangle.MANGLES.append(IfMangle)
hy.mangle.MANGLES.append(FunctionMangle)

View File

@ -312,6 +312,11 @@
(assert (= (let [[x 1] [y 2] [z 3]] (+ x y z)) 6)))
(defn test-if-mangler []
"NATIVE: test that we return ifs"
(assert (= true (if true true true))))
(defn test-let-scope []
"NATIVE: test let works rightish"
(setv y 123)