コード例 #1
0
ファイル: transpile.py プロジェクト: BenWeber42/dusk
def transpile(in_path: str,
              out_path: str,
              backend: str = default_backend,
              dump_sir: bool = False) -> None:

    with open(in_path, "r") as in_file:
        in_str = in_file.read()
        in_ast = ast.parse(in_str, filename=in_path, type_comments=True)

        grammar = Grammar()

        # TODO: handle errors in different stencils separately
        stencils = [grammar.stencil(node) for node in iter_stencils(in_ast)]

        sir = make_sir(in_path, GridType.Value("Unstructured"), stencils)

        if dump_sir:
            out_name = os.path.splitext(out_path)[0]
            with open(out_name + ".json", "w+") as f:
                f.write(sir_to_json(sir))

        out_code = compile(sir, backend=backend_map[backend])

        with open(out_path, "w") as out_file:
            out_file.write(out_code)
コード例 #2
0
def pyast_to_sir(stencils: List[ast.FunctionDef],
                 filename: str = "<unknown>") -> SIR:

    grammar = Grammar()
    # TODO: should probably throw instead
    assert all(grammar.is_stencil(stencil) for stencil in stencils)

    # TODO: handle errors in different stencils separately
    stencils = [grammar.stencil(stencil) for stencil in stencils]

    return make_sir(filename, GridType.Value("Unstructured"), stencils)