Beispiel #1
0
    def _print_ast(self, src):
        # Try to parse ast as well:
        tree = self.builder._create_ast(src, None)
        print(tree)
        print("C-AST:")
        print_ast(tree)

        # Print rendered c:
        print("re-rendered C:")
        render_ast(tree)
Beispiel #2
0
def test_c(prog):
    """ Test various randomly generated slabs of C-ish code. """
    print(prog)
    try:
        ast = parse_text(prog)
    except CompilerError as ex:
        print("Compilation error", ex)
    else:
        print(ast)
        print_ast(ast)
Beispiel #3
0
    def _print_ast(self, src):
        # Try to parse ast as well:
        f = io.StringIO(src)
        tree = self.builder._create_ast(src, None)
        print(tree)
        print('C-AST:')
        print_ast(tree)

        # Print rendered c:
        print('re-rendered C:')
        render_ast(tree)
Beispiel #4
0
    def compile(self, source):
        """ Compile the given source with current settings. """
        srcfile = io.StringIO(source)
        outfile = io.StringIO()
        if self.stage == "ast":
            src_ast = create_ast(srcfile, api.get_arch(self.arch).info)
            print_ast(src_ast, file=outfile)
        else:
            ir_module = api.c_to_ir(srcfile, self.arch)

            if self.optimize:
                api.optimize(ir_module, level=2)

            if self.stage == "ir":
                print_module(ir_module, file=outfile)
            else:
                text_stream = TextOutputStream(f=outfile, add_binary=True)
                api.ir_to_stream(ir_module, self.arch, text_stream)
        return outfile.getvalue()