Clean up _render_quoted_form

This commit is contained in:
Kodi Arfer 2018-06-17 16:09:47 -07:00
parent 8a70d5c90f
commit 217fc2a487

View File

@ -495,63 +495,55 @@ class HyASTCompiler(object):
We need to distinguish them as want to concatenate them instead of We need to distinguish them as want to concatenate them instead of
just nesting them. just nesting them.
""" """
if level == 0:
if isinstance(form, HyExpression):
if form and form[0] in ("unquote", "unquote-splice"):
if len(form) != 2:
raise HyTypeError(form,
("`%s' needs 1 argument, got %s" %
form[0], len(form) - 1))
return set(), form[1], (form[0] == "unquote-splice")
if isinstance(form, HyExpression): op = None
if form and form[0] == "quasiquote": if isinstance(form, HyExpression) and form and (
level += 1 isinstance(form[0], HySymbol)):
if form and form[0] in ("unquote", "unquote-splice"): op = unmangle(ast_str(form[0]))
level -= 1 if level == 0 and op in ("unquote", "unquote-splice"):
if len(form) != 2:
raise HyTypeError(form,
("`%s' needs 1 argument, got %s" %
op, len(form) - 1))
return set(), form[1], op == "unquote-splice"
elif op == "quasiquote":
level += 1
elif op in ("unquote", "unquote-splice"):
level -= 1
name = form.__class__.__name__ name = form.__class__.__name__
imports = set([name]) imports = set([name])
body = [form]
if isinstance(form, HySequence): if isinstance(form, HySequence):
if not form: contents = []
contents = HyList() for x in form:
else: 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 # If there are arguments, they can be spliced
# so we build a sum... # so we build a sum...
contents = HyExpression([HySymbol("+"), HyList()]) body = [HyExpression([HySymbol("+"), HyList()] + contents)]
else:
for x in form: body = [HyList()]
f_imports, f_contents, splice = self._render_quoted_form(x,
level)
imports.update(f_imports)
if splice:
to_add = HyExpression([
HySymbol("list"),
HyExpression([HySymbol("or"), f_contents, HyList()])])
else:
to_add = HyList([f_contents])
contents.append(to_add)
return imports, HyExpression([HySymbol(name),
contents]).replace(form), False
elif isinstance(form, HySymbol): elif isinstance(form, HySymbol):
return imports, HyExpression([HySymbol(name), body = [HyString(form)]
HyString(form)]).replace(form), False
elif isinstance(form, HyKeyword): elif isinstance(form, HyKeyword):
return imports, form, False body = [HyString(form.name)]
elif isinstance(form, HyString): elif isinstance(form, HyString) and form.brackets is not None:
x = [HySymbol(name), form] body.extend([HyKeyword("brackets"), form.brackets])
if form.brackets is not None:
x.extend([HyKeyword("brackets"), form.brackets])
return imports, HyExpression(x).replace(form), False
return imports, HyExpression([HySymbol(name), ret = HyExpression([HySymbol(name)] + body).replace(form)
form]).replace(form), False return imports, ret, False
@special(["quote", "quasiquote"], [FORM]) @special(["quote", "quasiquote"], [FORM])
def compile_quote(self, expr, root, arg): def compile_quote(self, expr, root, arg):