def transpile(source, headers=False, testing=False): """ Transpile a single python translation unit (a python script) into C++ 14 code. """ tree = ast.parse(source) rewriter = PythonMainRewriter("cpp") tree = rewriter.visit(tree) 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
def parse(*args): source = ast.parse("\n".join(args)) add_scope_context(source) add_variable_context(source) return source