Improve set comprehension for Python 3.0

This commit is contained in:
rocky
2023-01-16 03:40:55 -05:00
parent 137dd64a46
commit 50697bb79e
3 changed files with 9 additions and 4 deletions

View File

@@ -74,8 +74,8 @@ class Python30Parser(Python31Parser):
# Need to keep LOAD_FAST as index 1 # Need to keep LOAD_FAST as index 1
set_comp_header ::= BUILD_SET_0 DUP_TOP STORE_FAST set_comp_header ::= BUILD_SET_0 DUP_TOP STORE_FAST
set_comp_func ::= set_comp_header set_comp_func ::= set_comp_header
LOAD_FAST FOR_ITER store comp_iter LOAD_ARG FOR_ITER store comp_iter
JUMP_BACK POP_TOP JUMP_BACK RETURN_VALUE RETURN_LAST JUMP_BACK COME_FROM POP_TOP JUMP_BACK RETURN_VALUE RETURN_LAST
list_comp_header ::= BUILD_LIST_0 DUP_TOP STORE_FAST list_comp_header ::= BUILD_LIST_0 DUP_TOP STORE_FAST
list_comp ::= list_comp_header list_comp ::= list_comp_header

View File

@@ -465,7 +465,7 @@ class ComprehensionMixin:
self.write(": ") self.write(": ")
self.preorder(n[1]) self.preorder(n[1])
else: else:
if self.version == (3, 0): if self.version == (3, 0) and len(n) > 1:
body = n[1] body = n[1]
else: else:
body = n[0] body = n[0]

View File

@@ -1093,7 +1093,12 @@ class NonterminalActions:
self.write("{") self.write("{")
if node[0] in ["LOAD_SETCOMP", "LOAD_DICTCOMP"]: if node[0] in ["LOAD_SETCOMP", "LOAD_DICTCOMP"]:
if self.version == (3, 0): if self.version == (3, 0):
iter_index = 6 if len(node) >= 6:
iter_index = 6
else:
assert node[1].kind.startswith("MAKE_FUNCTION")
iter_index = 2
pass
else: else:
iter_index = 1 iter_index = 1
self.comprehension_walk_newer(node, iter_index=iter_index, code_index=0) self.comprehension_walk_newer(node, iter_index=iter_index, code_index=0)