コード例 #1
0
def process_ast_node(visitor, gc, call_node: ast.Call):
    r'''Inject the proper code into the output stream to deal with this C++ code.

    We expect this to be run on the back-end of the system.

    visitor - The node visitor that is converting the code into C++
    gc - the generated code object that we fill with actual code
    call_node - a Call ast node, with func being a CPPCodeValue.

    Result:
    representation - A value that represents the output
    '''

    # We write everything into a new scope to prevent conflicts. So we have to declare the result ahead of time.
    cpp_ast_node = call_node.func
    result_rep = cpp_ast_node.result_rep(gc.current_scope())

    gc.declare_variable(result_rep)

    # Include files
    for i in cpp_ast_node.include_files:
        gc.add_include(i)

    # Build the dictionary for replacement for the object we are calling
    # against, if any.
    repl_list = []
    if cpp_ast_node.replacement_instance_obj is not None:
        repl_list += [
            (cpp_ast_node.replacement_instance_obj[0],
             visitor.resolve_id(
                 cpp_ast_node.replacement_instance_obj[1]).rep.as_cpp())
        ]

    # Process the arguments that are getting passed to the function
    for arg, dest in zip(cpp_ast_node.args, call_node.args):
        rep = visitor.get_rep(dest)
        repl_list += [(arg, rep.as_cpp())]

    # Emit the statements.
    blk = statements.block()
    visitor._gc.add_statement(blk)

    for s in cpp_ast_node.running_code:
        l_s = s
        for src, dest in repl_list:
            l_s = l_s.replace(src, str(dest))
        blk.add_statement(statements.arbitrary_statement(l_s))

    # Set the result and close the scope
    blk.add_statement(
        statements.set_var(
            result_rep,
            cpp_value(cpp_ast_node.result, gc.current_scope(),
                      result_rep.cpp_type())))
    gc.pop_scope()

    return result_rep
コード例 #2
0
def test_set_rep_twice_fail():
    b = block()
    n = "dude"
    b.set_rep(n, 5)
    try:
        b.set_rep(n, 10)
        assert False
    except BlockException:
        pass
コード例 #3
0
def test_lookup_rep_not_in_block():
    b = block()
    assert None is b.get_rep("dude")
コード例 #4
0
def test_create_top_level_block():
    _ = block()
コード例 #5
0
def test_lookup_rep_in_block():
    b = block()
    n = "dude"
    b.set_rep(n, 5)
    assert 5 is b.get_rep(n)
コード例 #6
0
 def __init__(self):
     self._block = block()
     self._book_block = block()
     self._class_vars = []
     self._scope_stack = (self._block,)
     self._include_files = []