def convolute(ast_g, ast_f): 'Return an AST that represents g(f(args))' #TODO: fix up the ast.Calls to use lambda_call if possible # Sanity checks. For example, g can have only one input argument (e.g. f's result) if (not lambda_test(ast_g)) or (not lambda_test(ast_f)): raise BaseException("Only lambdas in Selects!") # Combine the lambdas into a single call by calling g with f as an argument l_g = copy.deepcopy(lambda_unwrap(ast_g)) l_f = copy.deepcopy(lambda_unwrap(ast_f)) x = arg_name() f_arg = ast.Name(x, ast.Load()) call_g = ast.Call(l_g, [ast.Call(l_f, [f_arg], [])], []) # TODO: Rewrite with lambda_build args = ast.arguments(args=[ast.arg(arg=x)]) call_g_lambda = ast.Lambda(args=args, body=call_g) # Build a new call to nest the functions return call_g_lambda
def parse_as_ast(ast_source: Union[str, ast.AST]) -> ast.Lambda: r'''Return an AST for a lambda function from several sources. We are handed one of several things: - An AST that is a lambda function - An AST that is a pointer to a Module that wraps an AST - Text that contains properly formatted ast code for a lambda function. In all cases, return a lambda function as an AST starting from the AST top node, and one where calls to Select, SelectMany, etc., have been replaced with proper AST nodes. Args: ast_source: An AST or text string that represnets the lambda. Returns: An ast starting from the Lambda AST node. ''' if isinstance(ast_source, str): a = ast.parse(ast_source.strip()) return lambda_unwrap(replace_LINQ_operators().visit(a)) else: return lambda_unwrap(ast_source)
def visit_Select(self, select_ast): 'Transform the iterable from one form to another' # Make sure we are in a loop seq = self.as_sequence(select_ast.source) # Simulate this as a "call" selection = lambda_unwrap(select_ast.selection) c = ast.Call(func=selection, args=[seq.sequence_value().as_ast()]) new_sequence_value = self.get_rep(c) # We need to build a new sequence. # TODO: figure out how to get pyright to not flag new_sequence_value as an error rep = crep.cpp_sequence(new_sequence_value, seq.iterator_value()) select_ast.rep = rep self._result = rep
def visit_SelectMany(self, node): r''' Apply the selection function to the base to generate a collection, and then loop over that collection. ''' # Make sure the source is around. We have to do this because code generation in this # framework is lazy. And if the `selection` function does not use the source, and # looking at that source might generate a loop, that loop won't be generated! Ops! _ = self.as_sequence(node.source) # We need to "call" the source with the function. So build up a new # call, and then visit it. c = ast.Call(func=lambda_unwrap(node.selection), args=[node.source]) # Get the collection, and then generate the loop over it. # It could be that this comes back from something that is already iterating (like a Select statement), # in which case we are already looping. seq = self.as_sequence(c) node.rep = seq self._result = seq
def visit_Where(self, node): 'Apply a filtering to the current loop.' # Make sure we are in a loop seq = self.as_sequence(node.source) # Simulate the filtering call - we want the resulting value to test. filter = lambda_unwrap(node.filter) c = ast.Call(func=filter, args=[seq.sequence_value().as_ast()]) rep = self.get_rep(c) # Create an if statement self._gc.add_statement(statement.iftest(rep)) # Ok - new sequence. This the same as the old sequence, only the sequence value is updated. # Protect against sequence of sequences (LOVE type checkers, which caught this as a possibility) w_val = seq.sequence_value() if isinstance(w_val, crep.cpp_sequence): raise BaseException( "Internal error: don't know how to look at a sequence") new_sequence_var = w_val.copy_with_new_scope(self._gc.current_scope()) node.rep = crep.cpp_sequence(new_sequence_var, seq.iterator_value()) self._result = node.rep
def test_parse_as_ast_lambda(): l = lambda_unwrap(ast.parse("lambda x: x + 1")) r = parse_as_ast(l) assert isinstance(r, ast.Lambda)