def main(argv): argparser = argparse.ArgumentParser( usage= "%s [PyCPython options, see below] [CPython options, see via --help]" % argv[0], description="Emulate CPython by interpreting its C code via PyCParser.", epilog= "All other options are passed on to CPython. Use --help to see them.", add_help=False # we will add our own ) argparser.add_argument('--pycpython-help', action='help', help='show this help message and exit') argparser.add_argument( '--dump-python', action='store', nargs=1, help= "Dumps the converted Python code of the specified function, e.g. Py_Main." ) argparser.add_argument( '--verbose-jit', action='store_true', help="Prints what functions and global vars we are going to translate." ) args_ns, argv_rest = argparser.parse_known_args(argv[1:]) argv = argv[:1] + argv_rest print "PyCPython -", argparser.description, print "(use --pycpython-help for help)" state = CPythonState() print "Parsing CPython...", state.parse_cpython() if state._errors: print "finished, parse errors:" for m in state._errors: print m else: print "finished, no parse errors." interpreter = cparser.interpreter.Interpreter() interpreter.register(state) if args_ns.dump_python: for fn in args_ns.dump_python: print print "PyAST of %s:" % fn interpreter.dumpFunc(fn) sys.exit() if args_ns.verbose_jit: interpreter.debug_print_getFunc = True interpreter.debug_print_getVar = True args = ("Py_Main", len(argv), argv + [None]) print "Run", args, ":" interpreter.runFunc(*args)
def main(argv): argparser = argparse.ArgumentParser( usage="%s [PyCPython options, see below] [CPython options, see via --help]" % argv[0], description="Emulate CPython by interpreting its C code via PyCParser.", epilog="All other options are passed on to CPython. Use --help to see them.", add_help=False # we will add our own ) argparser.add_argument( '--pycpython-help', action='help', help='show this help message and exit') argparser.add_argument( '--dump-python', action='store', nargs=1, help="Dumps the converted Python code of the specified function, e.g. Py_Main.") argparser.add_argument( '--verbose-jit', action='store_true', help="Prints what functions and global vars we are going to translate.") args_ns, argv_rest = argparser.parse_known_args(argv[1:]) argv = argv[:1] + argv_rest print "PyCPython -", argparser.description, print "(use --pycpython-help for help)" state = CPythonState() print "Parsing CPython...", state.parse_cpython() if state._errors: print "finished, parse errors:" for m in state._errors: print m else: print "finished, no parse errors." interpreter = cparser.interpreter.Interpreter() interpreter.register(state) if args_ns.dump_python: for fn in args_ns.dump_python: print print "PyAST of %s:" % fn interpreter.dumpFunc(fn) sys.exit() if args_ns.verbose_jit: interpreter.debug_print_getFunc = True interpreter.debug_print_getVar = True args = ("Py_Main", len(argv), argv + [None]) print "Run", args, ":" interpreter.runFunc(*args)
def main(argv): state = CPythonState() out_fn = MyDir + "/cpython_static.py" print "Compile CPython to %s." % os.path.basename(out_fn) print "Parsing CPython...", try: state.parse_cpython() except Exception: print "!!! Exception while parsing. Should not happen. Cannot recover. Please report this bug." print "The parser currently is here:", state.curPosAsStr() raise if state._errors: print "finished, parse errors:" for m in state._errors: print m else: print "finished, no parse errors." interpreter = cparser.interpreter.Interpreter() interpreter.register(state) print "Compile..." f = open(out_fn, "w") code_gen = CodeGen(f, state, interpreter) code_gen.write_header() code_gen.fix_names() code_gen.write_structs() code_gen.write_unions() code_gen.write_delayed_structs() code_gen.write_values() code_gen.write_globals() code_gen.write_footer() f.close() print "Done."
import cparser def prepareState(): state = cparser.State() state.autoSetupSystemMacros() state.autoSetupGlobalIncludeWrappers() return state state = prepareState() cparser.parse(MyDir + "/test_interpreter.c", state) import cparser.interpreter interpreter = cparser.interpreter.Interpreter() interpreter.register(state) if __name__ == '__main__': print "erros so far:" for m in state._errors: print m for f in state.contentlist: if not isinstance(f, cparser.CFunc): continue if not f.body: continue print print "parsed content of " + str(f) + ":" for c in f.body.contentlist: print c
def prepareState(): state = cparser.State() state.autoSetupSystemMacros() state.autoSetupGlobalIncludeWrappers() return state MyDir = os.path.dirname(__file__) state = prepareState() cparser.parse(MyDir + "/test_interpreter.c", state) from cparser import interpreter interpreter = interpreter.Interpreter() interpreter.register(state) if __name__ == '__main__': print("errors so far:") for m in state._errors: print(m) for f in state.contentlist: if not isinstance(f, cparser.CFunc): continue if not f.body: continue print() print("parsed content of " + str(f) + ":") for c in f.body.contentlist: print(c)