# HG changeset patch # User Paul Boddie # Date 1472576982 -7200 # Node ID 77a66aca54b008b0bdf7d091243e0458a3a7ee75 # Parent d40bc8b5118e28c00af2c912022b6a8abb7b7b9a Removed support for if expressions and list comprehensions. diff -r d40bc8b5118e -r 77a66aca54b0 common.py --- a/common.py Tue Aug 30 17:25:15 2016 +0200 +++ b/common.py Tue Aug 30 19:09:42 2016 +0200 @@ -465,121 +465,6 @@ self.next_iterator() self.process_structure_node(node) - def convert_ifexp_node(self, n): - - """ - Convert the given if expression node 'n'. An if expression is considered - as mapping to a function body containing an if statement as follows: - - if else - - lambda : - if : - return - else: - return - - The are populated with defaults after the node has been - processed. - """ - - node = compiler.ast.Lambda( - [], [], 0, - compiler.ast.If([ - (n.test, compiler.ast.Return(n.then)) - ], - compiler.ast.Return(n.else_) - )) - - return node - - def convert_listcomp_node(self, n): - - """ - Convert the given list comprehension node 'n'. A list comprehension is - considered as mapping to a function body containing a for loop as - follows: - - [ for in if for in if if ] - - lambda : - = [] - for in : - if : - for in : - if : - if : - .append() - return - - The are populated with defaults after the node has been - processed. - """ - - temp = "$tr" - - node = compiler.ast.Lambda( - [], [], 0, - compiler.ast.Stmt([ - - # = [] - - compiler.ast.Assign([compiler.ast.AssName(temp, "OP_ASSIGN")], - compiler.ast.List([]) - ), - - # for ... - - self.convert_listcomp_for_node(n.quals[0], n.quals[1:], n.expr, temp), - - # return - - compiler.ast.Return(compiler.ast.Name(temp)) - ])) - - return node - - def convert_listcomp_for_node(self, loop, following_loops, expr, temp): - - """ - Return a node representing 'loop', encapsulating 'following_loops' and - employing 'expr' in the innermost loop body appending to 'temp'. - """ - - if loop.ifs: - body = self.convert_listcomp_if_node(loop.ifs[0], loop.ifs[1:], following_loops, expr, temp) - elif following_loops: - body = self.convert_listcomp_for_node(following_loops[0], following_loops[1:], expr, temp) - else: - body = self.convert_listcomp_body_node(expr, temp) - - return compiler.ast.For(loop.assign, loop.list, compiler.ast.Stmt([body]), None) - - def convert_listcomp_if_node(self, if_, following_ifs, following_loops, expr, temp): - - """ - Return a node representing 'if_', encapsulating the 'following_ifs' and - 'following_loops' and employing 'expr' in the innermost loop body - appending to 'temp'. - """ - - if following_ifs: - body = self.convert_listcomp_if_node(following_ifs[0], following_ifs[1:], following_loops, expr, temp) - elif following_loops: - body = self.convert_listcomp_for_node(following_loops[0], following_loops[1:], expr, temp) - else: - body = self.convert_listcomp_body_node(expr, temp) - - return compiler.ast.If([(if_.test, compiler.ast.Stmt([body]))], None) - - def convert_listcomp_body_node(self, expr, temp): - - "Return a node appending 'expr' to 'temp'." - - return compiler.ast.Discard( - compiler.ast.CallFunc( - compiler.ast.Getattr(compiler.ast.Name(temp), "append"), [expr])) - def process_literal_sequence_node(self, n, name, ref, cls): """ diff -r d40bc8b5118e -r 77a66aca54b0 inspector.py --- a/inspector.py Tue Aug 30 17:25:15 2016 +0200 +++ b/inspector.py Tue Aug 30 19:09:42 2016 +0200 @@ -22,6 +22,7 @@ from branching import BranchTracker from common import * +from errors import InspectError from modules import * from os import listdir from os.path import extsep, split, splitext @@ -582,13 +583,16 @@ elif isinstance(n, compiler.ast.Tuple): return self.get_literal_instance(n, "tuple") - # List comprehensions and if expressions. + # Unsupported nodes. + + elif isinstance(n, compiler.ast.GenExpr): + raise InspectError("Generator expressions are not supported.", self.get_namespace_path(), n) + + elif isinstance(n, compiler.ast.IfExp): + raise InspectError("If-else expressions are not supported.", self.get_namespace_path(), n) elif isinstance(n, compiler.ast.ListComp): - self.process_listcomp_node(n) - - elif isinstance(n, compiler.ast.IfExp): - self.process_ifexp_node(n) + raise InspectError("List comprehensions are not supported.", self.get_namespace_path(), n) # All other nodes are processed depth-first. @@ -987,18 +991,6 @@ tracker.merge_branches() - def process_ifexp_node(self, n): - - "Process the given if expression node 'n'." - - name_ref = self.process_structure_node(self.convert_ifexp_node(n)) - - path = self.get_namespace_path() - self.allocate_arguments(path, self.function_defaults[name_ref.get_origin()]) - self.deallocate_arguments(path, self.function_defaults[name_ref.get_origin()]) - - return InvocationRef(name_ref) - def process_import_node(self, n): "Process the given import node 'n'." @@ -1060,18 +1052,6 @@ origin = self.get_object_path(name) return ResolvedNameRef(name, Reference("", origin)) - def process_listcomp_node(self, n): - - "Process the given list comprehension node 'n'." - - name_ref = self.process_structure_node(self.convert_listcomp_node(n)) - - path = self.get_namespace_path() - self.allocate_arguments(path, self.function_defaults[name_ref.get_origin()]) - self.deallocate_arguments(path, self.function_defaults[name_ref.get_origin()]) - - return InvocationRef(name_ref) - def process_logical_node(self, n): "Process the given operator node 'n'."