def util_test_conv(f1, f2, f_expected):
    a_1 = lambda_unwrap(ast.parse(f1))
    a_2 = lambda_unwrap(ast.parse(f2))
    a_expected = lambda_unwrap(ast.parse(f_expected))

    s_1 = ast.dump(normalize_ast().start_visit(a_1))
    s_2 = ast.dump(normalize_ast().start_visit(a_2))
    s_expected = ast.dump(normalize_ast().start_visit(a_expected))

    # Do the convolution
    a_conv = convolute(a_1, a_2)
    a_conv_reduced = simplify_chained_calls().visit(a_conv)

    s_conv_reduced = ast.dump(normalize_ast().start_visit(a_conv_reduced))
    s_1_after = ast.dump(normalize_ast().start_visit(a_1))
    s_2_after = ast.dump(normalize_ast().start_visit(a_2))

    # Make sure things match up with expected and nothing has changed.
    assert s_1 == s_1_after
    assert s_2 == s_2_after
    assert s_conv_reduced == s_expected
Пример #2
0
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_ast(ast_text):
    '''Parse a string as a LINQ ast
    
    NOTE: This must be called for every AST that the framework is converting from text.

    ast_text: String containing a lambda function

    returns:

    ast: The python AST representing the function, with Select, SelectMany, etc., properly converted
         to function call AST's.
    '''
    a = ast.parse(ast_text.strip())
    return lambda_unwrap(replace_LINQ_operators().visit(a))
    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.
        rep = crep.cpp_sequence(new_sequence_value, seq.iterator_value())

        select_ast.rep = rep
        self._result = rep
    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.
        new_sequence_var = seq.sequence_value().copy_with_new_scope(
            self._gc.current_scope())
        node.rep = crep.cpp_sequence(new_sequence_var, seq.iterator_value())

        self._result = node.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 test_lambda_replace_simple_unwrapped():
    t = lambda_unwrap(ast.parse("lambda b: b+1"))
    b = ast.parse("2*b").body[0].value
    expected = lambda_unwrap(ast.parse("lambda b: 2*b"))

    assert ast.dump(expected) == ast.dump(lambda_body_replace(t, b))
def test_unwrap_bad_lambda():
    try:
        lambda_unwrap(ast.parse('x+1'))
        assert False
    except:
        pass