From a4e9410c076c288710eb0e519031d67dfbde1a32 Mon Sep 17 00:00:00 2001 From: rocky Date: Wed, 6 Dec 2017 12:14:42 -0500 Subject: [PATCH] Start to reduce singleton reductions --- uncompyle6/parser.py | 14 +++++++++++++- uncompyle6/semantics/pysource.py | 11 +++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/uncompyle6/parser.py b/uncompyle6/parser.py index 925f4ec8..58f8bf44 100644 --- a/uncompyle6/parser.py +++ b/uncompyle6/parser.py @@ -45,6 +45,10 @@ class PythonParser(GenericASTBuilder): ] self.collect = frozenset(nt_list) + # Reduce singleton reductions in these nonterminals: + self.singleton = frozenset(('str', 'joined_str', 'expr', 'store', + 'inplace_op')) + def ast_first_offset(self, ast): if hasattr(ast, 'offset'): return ast.offset @@ -154,7 +158,13 @@ class PythonParser(GenericASTBuilder): return token.kind def nonterminal(self, nt, args): - if nt in self.collect and len(args) > 1: + n = len(args) + + # Use this to find lots of singleton rule + # if n == 1 and nt not in self.singleton: + # print("XXX", nt) + + if nt in self.collect and n > 1: # # Collect iterated thingies together. That is rather than # stmts -> stmts stmt -> stmts stmt -> ... @@ -162,6 +172,8 @@ class PythonParser(GenericASTBuilder): # rv = args[0] rv.append(args[1]) + elif n == 1 and args[0] in self.singleton: + rv = GenericASTBuilder.nonterminal(self, nt, args[0]) else: rv = GenericASTBuilder.nonterminal(self, nt, args) return rv diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index 1b2a327a..6b694f48 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -898,11 +898,13 @@ class SourceWalker(GenericASTTraversal, object): """ self.write(self.indent, 'exec ') self.preorder(node[0]) - if not node[1][0].isNone(): + + if not node[1].isNone(): sep = ' in ' for subnode in node[1]: self.write(sep); sep = ", " self.preorder(subnode) + self.println() self.prune() # stop recursing @@ -1316,8 +1318,8 @@ class SourceWalker(GenericASTTraversal, object): pass pass else: - ast = ast[0][0] - n = ast[iter_index] + list_comp = ast[0] + n = list_comp[iter_index] assert n == 'list_iter', n # FIXME: I'm not totally sure this is right. @@ -1406,8 +1408,9 @@ class SourceWalker(GenericASTTraversal, object): if node == 'set_comp': ast = ast[0][0][0] else: - ast = ast[0][0][0][0][0] + ast = ast[0][0][0][0] + assert ast == 'list_comp' n = ast[1] collection = node[-3] list_if = None