Reflowing the list-comp bits.

This commit is contained in:
Paul R. Tagliamonte 2013-04-02 20:46:32 -04:00
parent a878a7f7d5
commit c8d3a1859e

View File

@ -134,8 +134,7 @@ class HyASTCompiler(object):
name = str(name) name = str(name)
else: else:
# Python2 requires an ast.Name, set to ctx Store. # Python2 requires an ast.Name, set to ctx Store.
name = self.compile(name) name = self._storeize(self.compile(name))
name.ctx = ast.Store()
return ast.ExceptHandler( return ast.ExceptHandler(
lineno=expr.start_line, lineno=expr.start_line,
@ -324,12 +323,7 @@ class HyASTCompiler(object):
def compile_with_as_expression(self, expr): def compile_with_as_expression(self, expr):
expr.pop(0) # with-as expr.pop(0) # with-as
ctx = self.compile(expr.pop(0)) ctx = self.compile(expr.pop(0))
thing = self.compile(expr.pop(0)) thing = self._storeize(self.compile(expr.pop(0)))
if isinstance(thing, ast.Tuple):
for x in thing.elts:
x.ctx = ast.Store()
thing.ctx = ast.Store()
ret = ast.With(context_expr=ctx, ret = ast.With(context_expr=ctx,
lineno=expr.start_line, lineno=expr.start_line,
@ -353,29 +347,40 @@ class HyASTCompiler(object):
@builds("list_comp") @builds("list_comp")
def compile_list_comprehension(self, expr): def compile_list_comprehension(self, expr):
# (list-comp expr (target iter)) # (list-comp expr (target iter) cond?)
expr.pop(0) expr.pop(0)
thing = self.compile(expr.pop(0)) expression = expr.pop(0)
ident, gen = expr.pop(0) tar_it = iter(expr.pop(0))
targets = zip(tar_it, tar_it)
ident = self.compile(ident) cond = None
gen = self.compile(gen) if expr != []:
cond = self.compile(expr.pop(0))
if isinstance(ident, ast.Tuple): lcmp = None
for x in ident.elts: ret = ast.ListComp(
x.ctx = ast.Store()
ident.ctx = ast.Store()
return ast.ListComp(
lineno=expr.start_line, lineno=expr.start_line,
col_offset=expr.start_column, col_offset=expr.start_column,
elt=thing, elt=self.compile(expression),
generators=[ generators=[])
ast.comprehension(
target=ident, for target, iterable in targets:
iter=gen, ret.generators.append(ast.comprehension(
ifs=[self.compile(x) for x in expr]) target=self._storeize(self.compile(target)),
]) iter=self.compile(iterable),
ifs=[]))
if cond:
ret.generators[-1].ifs.append(cond)
return ret
def _storeize(self, name):
if isinstance(name, ast.Tuple):
for x in name.elts:
x.ctx = ast.Store()
name.ctx = ast.Store()
return name
@builds("kwapply") @builds("kwapply")
def compile_kwapply_expression(self, expr): def compile_kwapply_expression(self, expr):
@ -505,12 +510,7 @@ class HyASTCompiler(object):
what.name = str(name) what.name = str(name)
return what return what
name = self.compile(name) name = self._storeize(self.compile(name))
if isinstance(name, ast.Tuple):
for x in name.elts:
x.ctx = ast.Store()
name.ctx = ast.Store()
return ast.Assign( return ast.Assign(
lineno=expression.start_line, lineno=expression.start_line,
@ -524,13 +524,7 @@ class HyASTCompiler(object):
expression.pop(0) # for expression.pop(0) # for
name, iterable = expression.pop(0) name, iterable = expression.pop(0)
target = self.compile_symbol(name) target = self._storeize(self.compile_symbol(name))
if isinstance(target, ast.Tuple):
for x in target.elts:
x.ctx = ast.Store()
target.ctx = ast.Store()
# support stuff like: # support stuff like:
# (for [x [1 2 3 4] # (for [x [1 2 3 4]
# y [a b c d]] ...) # y [a b c d]] ...)