예제 #1
0
파일: marshal.py 프로젝트: bsdinis/marshal
def main():
    """ main compiler routine """
    cins, headers, codes, what_to_print, namespaces, public = options()
    for cin, header, code, namespace in zip(cins, headers, codes, namespaces):
        astree = ast.make_ast(scanner.scan(cin), public)
        if header:
            if header.name != '<stdout>':
                print(f'/**\n * {header.name}\n */' + '\n', file=header)
                print('#pragma once\n', file=header)

            print('/***\n * headers\n */', file=header)
            print(visit_c_header.generate(astree, header.name != '<stdout>',
                                          namespace),
                  file=header)
        if code:
            if header and header.name != '<stdout>':
                print(f'/**\n * {code.name}\n */' + '\n', file=code)
                print(f'#include "{header.name}"', file=code)
                print('#include <string.h>', file=code)
                print('\n', file=code)
            if what_to_print in [2, 3]:
                print('/***\n * protoyped declarations\n */', file=code)
                print(visit_c_prot.generate(astree, namespace), file=code)
            if what_to_print in [1, 3]:
                print('/***\n * code\n */', file=code)
                print(visit_c_code.generate(astree, namespace), file=code)

    return 0
예제 #2
0
파일: ast.py 프로젝트: bsdinis/marshal
        else:
            raise ValueError(str(typename) + ' does not name a node type')

    for s in ast['structs']:
        for m in s['members']:
            if any(struct['typedef'] == m[0] for struct in ast['structs']):
                pass
            elif m[0] not in ast['private_types'].union(ast['exported_types']):
                add_private_type(ast, m[0])

    for f in ast['funcs']:
        if f['return_t'] != 'void' and not any(f['return_t'] == s['typedef']
                                               for s in ast['structs']):
            add_private_type(ast, f['return_t'])

        for t in f['args']:
            if t[0] != 'void' and not any(t[0] == s['typedef']
                                          for s in ast['structs']):
                add_private_type(ast, t[0])

    if ast['funcs']:
        add_private_type(ast, 'int32_t')

    return ast


if __name__ == '__main__':
    stmts = scanner.scan(sys.stdin)
    print(make_ast(stmts, True))
    sys.exit(0)
예제 #3
0
            func_resp_sz(ast, namespace),
            func_parse_exec(ast, namespace),
        ]))

        for code, func in enumerate(ast['funcs']):
            funcs.append('\n'.join([
                '\n\n// function {f}'.format(f=func['name']),
                gen_func(ast, namespace, func, code + 1)
            ]))

    return funcs


def generate(ast, namespace):
    types = gen_types(ast, namespace, real_types(ast))
    structs = gen_structs(ast)
    funcs = gen_funcs(ast, namespace)

    code = str()
    for frag in [types, structs, funcs]:
        if frag:
            code += '\n' + '\n'.join(frag)

    return code


if __name__ == '__main__':
    ast = ast.make_ast(scanner.scan(sys.stdin))
    print(generate(ast, ''), end='')
    sys.exit(0)