Factor out compiler subs for constructing HyExprs

This commit is contained in:
Kodi Arfer 2019-07-08 15:40:01 -04:00
parent e777f25796
commit 349da353d6

View File

@ -324,6 +324,18 @@ def is_unpack(kind, x):
and x[0] == "unpack-" + kind) and x[0] == "unpack-" + kind)
def make_hy_model(outer, x, rest):
return outer(
[HySymbol(a) if type(a) is str else
a[0] if type(a) is list else a
for a in x] +
(rest or []))
def mkexpr(*items, **kwargs):
return make_hy_model(HyExpression, items, kwargs.get('rest'))
def mklist(*items, **kwargs):
return make_hy_model(HyList, items, kwargs.get('rest'))
class HyASTCompiler(object): class HyASTCompiler(object):
"""A Hy-to-Python AST compiler""" """A Hy-to-Python AST compiler"""
@ -1364,19 +1376,17 @@ class HyASTCompiler(object):
# We need to ensure the statements for the condition are # We need to ensure the statements for the condition are
# executed on every iteration. Rewrite the loop to use a # executed on every iteration. Rewrite the loop to use a
# single anonymous variable as the condition. # single anonymous variable as the condition.
def e(*x): return HyExpression(x) cond_var = self.get_anon_var()
s = HySymbol return self.compile(mkexpr(
cond_var = s(self.get_anon_var()) 'do',
return self.compile(e( mkexpr('setv', cond_var, 'True'),
s('do'), mkexpr('while', cond_var,
e(s('setv'), cond_var, 1),
e(s('while'), cond_var,
# Cast the condition to a bool in case it's mutable and # Cast the condition to a bool in case it's mutable and
# changes its truth value, but use (not (not ...)) instead of # changes its truth value, but use (not (not ...)) instead of
# `bool` in case `bool` has been redefined. # `bool` in case `bool` has been redefined.
e(s('setv'), cond_var, e(s('not'), e(s('not'), cond))), mkexpr('setv', cond_var, mkexpr('not', mkexpr('not', [cond]))),
e(s('if*'), cond_var, e(s('do'), *body)), mkexpr('if*', cond_var, mkexpr('do', rest=body)),
*([e(s('else'), *else_expr)] if else_expr is not None else []))).replace(expr)) # noqa *([mkexpr('else', rest=else_expr)] if else_expr is not None else []))).replace(expr)) # noqa
orel = Result() orel = Result()
if else_expr is not None: if else_expr is not None: