예제 #1
0
def transpile(source, transpiler):
    """
    Transpile a single python translation unit (a python script) into
    Rust code.
    """
    tree = ast.parse(source)
    add_variable_context(tree)
    add_scope_context(tree)
    add_list_calls(tree)
    infer_meta = infer_types(tree)
    detect_mutable_vars(tree)
    detect_nesting_levels(tree)
    add_annotation_flags(tree)
    add_imports(tree)

    out = []
    code = transpiler.visit(tree) + "\n"
    headers = transpiler.headers(infer_meta)
    if headers:
        out.append(headers)
    usings = transpiler.usings()
    if usings:
        out.append(usings)
    out.append(code)
    return "\n".join(out)
예제 #2
0
파일: transpiler.py 프로젝트: adsharma/pyrs
def transpile(source, headers=False, testing=False):
    """
    Transpile a single python translation unit (a python script) into
    C++ 14 code.
    """
    tree = ast.parse(source)
    add_variable_context(tree)
    add_scope_context(tree)
    add_list_calls(tree)
    add_imports(tree)

    transpiler = CppTranspiler()

    buf = []
    if testing:
        buf += ['#include "catch.hpp"']
        transpiler.use_catch_test_cases = True

    if headers:
        buf += transpiler.headers
        buf += transpiler.usings

    if testing or headers:
        buf.append("")  # Force empty line

    cpp = transpiler.visit(tree)
    return "\n".join(buf) + cpp
예제 #3
0
def transpile(source):
    """
    Transpile a single python translation unit (a python script) into
    Kotlin code.
    """
    tree = ast.parse(source)
    add_variable_context(tree)
    add_scope_context(tree)
    add_list_calls(tree)
    detect_mutable_vars(tree)
    add_annotation_flags(tree)
    add_imports(tree)

    transpiler = KotlinTranspiler()

    return transpiler.visit(tree)
예제 #4
0
 def test_call_added(self):
     source = parse("results = []", "results.append(x)")
     add_list_calls(source)
     assert len(source.scopes[-1].vars[0].calls) == 1
예제 #5
0
 def test_global_list_assignment_with_later_append(self):
     source = parse("results = []", "def add_x():", "   results.append(2)")
     add_list_calls(source)
     results = source.body[0]
     t = value_type(results)
     assert t == "2"
예제 #6
0
 def test_list_assignment_with_append_unknown_value(self):
     source = parse("results = []", "x = 3", "results.append(x)")
     add_list_calls(source)
     results = source.body[0]
     t = value_type(results)
     assert t == "3"
예제 #7
0
 def test_list_assignment_based_on_later_append(self):
     source = parse("x = 3", "results = []", "results.append(x)")
     add_list_calls(source)
     results = source.body[1]
     t = value_type(results)
     assert t == "3"
예제 #8
0
def test_is_list():
    source = parse("list1 = []", "list2 = list1")
    add_list_calls(source)
    list2 = source.body[1]
    assert is_list(list2)
예제 #9
0
def test_decltype_list_var():
    source = parse("results = []", "x = 3", "results.append(x)")
    add_list_calls(source)
    results = source.body[0]
    t = decltype(results)
    assert t == "std::vector<decltype(3)>"