Example #1
0
def run_it():
    arch = get_arch("example")
    bsp = io.StringIO(
        """
    module bsp;
    public function void sleep(int ms);
    public function void putc(byte c);
    public function bool get_key(int* key);
    """
    )

    ircode = c3_to_ir(
        ["../src/snake/game.c3", "../src/snake/main.c3", "../../librt/io.c3"],
        [bsp],
        arch,
    )

    with open("python_snake2.py", "w") as f:
        print("import time", file=f)
        print("import sys", file=f)
        print("import threading", file=f)
        ir_to_python([ircode], f)

        print("", file=f)
        print("def bsp_putc(c):", file=f)
        print('    print(chr(c), end="")', file=f)
        print("def bsp_get_key(x):", file=f)
        print("    return 0", file=f)
        print("def bsp_sleep(x):", file=f)
        print("    time.sleep(x*0.001)", file=f)
        print("main_main()", file=f)

    print("Now run python_snake2.py !")
    def do(self, src, expected_output, lang='c3'):
        base_filename = make_filename(self.id())
        sample_filename = base_filename + '.py'
        list_filename = base_filename + '.html'

        bsp = io.StringIO("""
           module bsp;
           public function void putc(byte c);
           """)
        march = 'arm'
        with HtmlReportGenerator(open(list_filename, 'w')) as reporter:
            if lang == 'c3':
                ir_modules = [
                    c3_to_ir([
                        relpath('..', 'librt', 'io.c3'), bsp,
                        io.StringIO(src)
                    ], [],
                             march,
                             reporter=reporter)
                ]
            elif lang == 'bf':
                ir_modules = [bf_to_ir(src, march)]
            elif lang == 'c':
                coptions = COptions()
                include_path1 = relpath('..', 'librt', 'libc')
                lib = relpath('..', 'librt', 'libc', 'lib.c')
                coptions.add_include_path(include_path1)
                with open(lib, 'r') as f:
                    mod1 = c_to_ir(f,
                                   march,
                                   coptions=coptions,
                                   reporter=reporter)
                mod2 = c_to_ir(io.StringIO(src),
                               march,
                               coptions=coptions,
                               reporter=reporter)
                ir_modules = [mod1, mod2]
            else:  # pragma: no cover
                raise NotImplementedError(
                    'Language {} not implemented'.format(lang))

            # Test roundtrip of ir_modules

            for ir_module in ir_modules:
                serialization_roundtrip(ir_module)
                optimize(ir_module, level=self.opt_level, reporter=reporter)

            with open(sample_filename, 'w') as f:
                ir_to_python(ir_modules, f, reporter=reporter)

                # Add glue:
                print('', file=f)
                print('def bsp_putc(c):', file=f)
                print('    print(chr(c), end="")', file=f)
                print('main_main()', file=f)

        res = run_python(sample_filename)
        self.assertEqual(expected_output, res)
Example #3
0
    def do(self, src, expected_output, lang="c3"):
        base_filename = make_filename(self.id())
        sample_filename = base_filename + ".py"
        list_filename = base_filename + ".html"

        bsp_c3 = io.StringIO("""
           module bsp;
           public function void putc(byte c);
           """)
        march = "arm"
        with html_reporter(list_filename) as reporter:
            ir_modules = build_sample_to_ir(src, lang, bsp_c3, march, reporter)

            # Test roundtrip of ir_modules
            for ir_module in ir_modules:
                verify_module(ir_module)
                serialization_roundtrip(ir_module)
                api.optimize(ir_module,
                             level=self.opt_level,
                             reporter=reporter)

            with open(sample_filename, "w") as f:
                api.ir_to_python(ir_modules, f, reporter=reporter)

                # Expose all functions as external symbols:
                for ir_module in ir_modules:
                    for routine in ir_module.functions:
                        print('_irpy_externals["{0}"] = {0}'.format(
                            routine.name),
                              file=f)

                # Add glue:
                print("", file=f)
                print("def bsp_putc(c):", file=f)
                print('    print(chr(c), end="")', file=f)
                print("", file=f)
                # print('_irpy_externals["printf"] = printf', file=f)
                print('_irpy_externals["bsp_putc"] = bsp_putc', file=f)
                print("", file=f)
                print("main_main()", file=f)
                print("", file=f)

        res = run_python(sample_filename)
        self.assertEqual(expected_output, res)
Example #4
0
from ppci.api import ir_to_python, c_to_ir, get_arch, COptions, optimize

arch = get_arch('example')
bsp = io.StringIO("""
module bsp;
public function void sleep(int ms);
public function void putc(byte c);
public function bool get_key(int* key);
""")

coptions = COptions()
coptions.add_include_path('../../librt/libc/include')

sources = ['../src/structs/structs.c', '../../librt/libc/lib.c']
ir_modules = []
for source in sources:
    with open(source, 'r') as f:
        ir_module = c_to_ir(f, arch, coptions=coptions)
        optimize(ir_module, level=2)
        ir_modules.append(ir_module)

with open('generated_python_structs.py', 'w') as f:
    print('import time', file=f)
    print('import sys', file=f)
    print('import threading', file=f)
    ir_to_python(ir_modules, f)
    # Invoke main:
    print('def bsp_putc(c):', file=f)
    print('    print(chr(c), end="")', file=f)
    print('main_main()', file=f)