Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_lex_expression_integer():
""" Make sure expressions can produce integers """
objs = tokenize("(foo 2)")
assert objs == [HyExpression([HySymbol("foo"), HyInteger(2)])]
def test_lex_expression_symbols():
""" Make sure that expressions produce symbols """
objs = tokenize("(foo bar)")
assert objs == [HyExpression([HySymbol("foo"), HySymbol("bar")])]
def _mangle(self, tree):
# Things that force a scope push to go into:
#
# - Functions
# - If
scopable = ["fn", "if"]
scoped = False
self.push_stack(tree)
if isinstance(tree, HyExpression):
what = tree[0]
if what in scopable:
self.push_scope(tree)
scoped = True
if isinstance(tree, list):
for i, element in enumerate(tree):
nel = self.visit(element)
if nel:
tree[i] = nel
self.tree_changed()
self._mangle(element)
if scoped:
self.pop_scope()
def cond_macro(*tree):
it = iter(tree)
test, branch = next(it)
root = HyExpression([HySymbol("if"), test, branch])
ret = root
for (test, branch) in it:
n = HyExpression([HySymbol("if"), test, branch])
ret.append(n)
ret = n
return root
def router(tree, rkwargs=None):
tree = HyExpression(tree)
name = tree.pop(0)
path = tree.pop(0)
tree.insert(0, HySymbol("fn"))
tree = HyExpression([HySymbol("def"), name, tree])
route = HyExpression([HySymbol(".route"), HySymbol("app"), path])
if rkwargs:
route = HyExpression([HySymbol("kwapply"), route,
HyDict({HyString("methods"): rkwargs})])
return HyExpression([HySymbol("with_decorator"), route, tree])
def router(tree, rkwargs=None):
tree = HyExpression(tree)
name = tree.pop(0)
path = tree.pop(0)
tree.insert(0, HySymbol("fn"))
tree = HyExpression([HySymbol("def"), name, tree])
route = HyExpression([HySymbol(".route"), HySymbol("app"), path])
if rkwargs:
route = HyExpression([HySymbol("kwapply"), route,
HyDict({HyString("methods"): rkwargs})])
return HyExpression([HySymbol("with_decorator"), route, tree])
def when_macro(test, *body):
return HyExpression([
HySymbol('if'),
test,
HyExpression([HySymbol("do")] + list(body)),
])
def first_macro(lst):
return HyExpression([HySymbol('get'),
lst,
HyInteger(0)])
def cond_macro(*tree):
it = iter(tree)
test, branch = next(it)
root = HyExpression([HySymbol("if"), test, branch])
ret = root
for (test, branch) in it:
n = HyExpression([HySymbol("if"), test, branch])
ret.append(n)
ret = n
return root
# ...
tree = HyExpression(tree).replace(tree[0])
it = iter(tree.pop(0))
blocks = list(zip(it, it)) # List for Python 3.x degenerating.
key, val = blocks.pop(0)
ret = HyExpression([HySymbol("foreach"),
HyList([key, val])])
root = ret
ret.replace(tree)
for key, val in blocks:
# x, [1, 2, 3, 4]
nret = HyExpression([HySymbol("foreach"),
HyList([key, val])])
nret.replace(key)
ret.append(nret)
ret = nret
[ret.append(x) for x in tree] # we really need ~@
return root