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_wrap_nested_expr():
""" Test conversion of HyExpressions with embedded non-HyObjects."""
wrapped = wrap_value(HyExpression([0]))
assert type(wrapped) == HyExpression
assert type(wrapped[0]) == HyInteger
assert wrapped == HyExpression([HyInteger(0)])
def f(x): return [HyExpression([HySymbol("foo"), x])]
def test_preprocessor_expression():
""" Test that macro expansion doesn't recurse"""
obj = macroexpand(tokenize('(test (test "one" "two"))')[0],
HyASTCompiler(__name__))
assert type(obj) == HyList
assert type(obj[0]) == HyExpression
assert obj[0] == HyExpression([HySymbol("test"),
HyString("one"),
HyString("two")])
obj = HyList([HyString("one"), HyString("two")])
obj = tokenize('(shill ["one" "two"])')[0][1]
assert obj == macroexpand(obj, HyASTCompiler(""))
if isinstance(form, HySequence):
contents = []
for x in form:
f_imps, f_contents, splice = self._render_quoted_form(x, level)
imports.update(f_imps)
if splice:
contents.append(HyExpression([
HySymbol("list"),
HyExpression([HySymbol("or"), f_contents, HyList()])]))
else:
contents.append(HyList([f_contents]))
if form:
# If there are arguments, they can be spliced
# so we build a sum...
body = [HyExpression([HySymbol("+"), HyList()] + contents)]
else:
body = [HyList()]
elif isinstance(form, HySymbol):
body = [HyString(form)]
elif isinstance(form, HyKeyword):
body = [HyString(form.name)]
elif isinstance(form, HyString):
if form.is_format:
body.extend([HyKeyword("is_format"), form.is_format])
if form.brackets is not None:
body.extend([HyKeyword("brackets"), form.brackets])
ret = HyExpression([HySymbol(name)] + body).replace(form)
def compile_eval_and_compile(self, expr, root, body):
new_expr = HyExpression([HySymbol("do").replace(expr[0])]).replace(expr)
try:
hy_eval(new_expr + body,
self.module.__dict__,
self.module,
filename=self.filename,
source=self.source)
except HyInternalError:
# Unexpected "meta" compilation errors need to be treated
# like normal (unexpected) compilation errors at this level
# (or the compilation level preceding this one).
raise
except Exception as e:
# These could be expected Hy language errors (e.g. syntax errors)
# or regular Python runtime errors that do not signify errors in
# the compilation *process* (although compilation did technically
def macroexpand(tree, compiler, once=False):
"""Expand the toplevel macros for the `tree`.
Load the macros from the given `module_name`, then expand the (top-level)
macros in `tree` until we no longer can.
"""
load_macros(compiler.module_name)
while True:
if not isinstance(tree, HyExpression) or tree == []:
break
fn = tree[0]
if fn in ("quote", "quasiquote") or not isinstance(fn, HySymbol):
break
fn = mangle(fn)
m = _hy_macros[compiler.module_name].get(fn) or _hy_macros[None].get(fn)
if not m:
break
opts = {}
if m._hy_macro_pass_compiler:
opts['compiler'] = compiler
try:
def mkexpr(*items, **kwargs):
return make_hy_model(HyExpression, items, kwargs.get('rest'))
def mklist(*items, **kwargs):
def paren(state, p):
return HyExpression(p[1])
def term_quote(state, p):
return HyExpression([HySymbol("quote"), p[1]])
elif op == "quasiquote":
level += 1
elif op in ("unquote", "unquote-splice"):
level -= 1
name = form.__class__.__name__
imports = set([name])
body = [form]
if isinstance(form, HySequence):
contents = []
for x in form:
f_imps, f_contents, splice = self._render_quoted_form(x, level)
imports.update(f_imps)
if splice:
contents.append(HyExpression([
HySymbol("list"),
HyExpression([HySymbol("or"), f_contents, HyList()])]))
else:
contents.append(HyList([f_contents]))
if form:
# If there are arguments, they can be spliced
# so we build a sum...
body = [HyExpression([HySymbol("+"), HyList()] + contents)]
else:
body = [HyList()]
elif isinstance(form, HySymbol):
body = [HyString(form)]
elif isinstance(form, HyKeyword):
body = [HyString(form.name)]