Adding in an If mangle
This commit is contained in:
parent
3ab7153398
commit
4b57fd0a51
@ -20,21 +20,12 @@
|
|||||||
|
|
||||||
from hy.models.expression import HyExpression
|
from hy.models.expression import HyExpression
|
||||||
from hy.models.symbol import HySymbol
|
from hy.models.symbol import HySymbol
|
||||||
|
from hy.models.list import HyList
|
||||||
|
|
||||||
import hy.mangle
|
import hy.mangle
|
||||||
|
|
||||||
|
|
||||||
class FunctionMangle(hy.mangle.Mangle):
|
class HoistableMangle(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)
|
|
||||||
|
|
||||||
def should_hoist(self):
|
def should_hoist(self):
|
||||||
for frame in self.stack:
|
for frame in self.stack:
|
||||||
if frame is self.scope:
|
if frame is self.scope:
|
||||||
@ -47,6 +38,18 @@ class FunctionMangle(hy.mangle.Mangle):
|
|||||||
return True
|
return True
|
||||||
return False
|
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):
|
def visit(self, tree):
|
||||||
if isinstance(tree, HyExpression) and tree != []:
|
if isinstance(tree, HyExpression) and tree != []:
|
||||||
call = tree[0]
|
call = tree[0]
|
||||||
@ -60,4 +63,22 @@ class FunctionMangle(hy.mangle.Mangle):
|
|||||||
self.hoist(fn_def)
|
self.hoist(fn_def)
|
||||||
return new_name
|
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)
|
hy.mangle.MANGLES.append(FunctionMangle)
|
||||||
|
@ -312,6 +312,11 @@
|
|||||||
(assert (= (let [[x 1] [y 2] [z 3]] (+ x y z)) 6)))
|
(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 []
|
(defn test-let-scope []
|
||||||
"NATIVE: test let works rightish"
|
"NATIVE: test let works rightish"
|
||||||
(setv y 123)
|
(setv y 123)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user