Use model patterns for comprehensions

I haven't bothered to refine the patterns for these since I intend to completely overhaul the comprehension forms in the near future.
This commit is contained in:
Kodi Arfer 2018-04-21 15:30:47 -07:00
parent 41d3f26001
commit 8b2733e921

View File

@ -1108,21 +1108,18 @@ class HyASTCompiler(object):
return gen_res + cond, gen return gen_res + cond, gen
@builds("list-comp", "set-comp", "genexpr") @special(["list-comp", "set-comp", "genexpr"], [FORM, FORM, maybe(FORM)])
@checkargs(min=2, max=3) def compile_comprehension(self, expr, form, expression, gen, cond):
def compile_comprehension(self, expr): # (list-comp expr [target iter] cond?)
# (list-comp expr (target iter) cond?)
form = expr.pop(0)
expression = expr.pop(0)
gen_gen = expr[0]
if not isinstance(gen_gen, HyList): if not isinstance(gen, HyList):
raise HyTypeError(gen_gen, "Generator expression must be a list.") raise HyTypeError(gen, "Generator expression must be a list.")
gen_res, gen = self._compile_generator_iterables(expr) gen_res, gen = self._compile_generator_iterables(
[gen] + ([] if cond is None else [cond]))
if len(gen) == 0: if len(gen) == 0:
raise HyTypeError(gen_gen, "Generator expression cannot be empty.") raise HyTypeError(expr, "Generator expression cannot be empty.")
ret = self.compile(expression) ret = self.compile(expression)
node_class = ( node_class = (
@ -1132,14 +1129,13 @@ class HyASTCompiler(object):
return ret + gen_res + node_class( return ret + gen_res + node_class(
expr, elt=ret.force_expr, generators=gen) expr, elt=ret.force_expr, generators=gen)
@builds("dict-comp") @special("dict-comp", [FORM, FORM, FORM, maybe(FORM)])
@checkargs(min=3, max=4) def compile_dict_comprehension(self, expr, root, key, value, gen, cond):
def compile_dict_comprehension(self, expr): key = self.compile(key)
expr.pop(0) # dict-comp value = self.compile(value)
key = self.compile(expr.pop(0))
value = self.compile(expr.pop(0))
gen_res, gen = self._compile_generator_iterables(expr) gen_res, gen = self._compile_generator_iterables(
[gen] + ([] if cond is None else [cond]))
return key + value + gen_res + asty.DictComp( return key + value + gen_res + asty.DictComp(
expr, expr,