Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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

    print
    print "PyAST of main:"
    interpreter.dumpFunc("main")

    print
    print
    interpreter.runFunc("main", len(sys.argv), sys.argv + [None])
Пример #4
0
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)

    print()
    print("PyAST of main:")
    interpreter.dumpFunc("main")

    print()
    print()
    interpreter.runFunc("main", len(sys.argv), sys.argv + [None])
Пример #5
0
	
	return state

state = prepareState()
cparser.parse(CPythonDir + "/Modules/main.c", state) # Py_Main
cparser.parse(CPythonDir + "/Python/getopt.c", state) # _PyOS_GetOpt
cparser.parse(CPythonDir + "/Python/pythonrun.c", state) # Py_Initialize
cparser.parse(CPythonDir + "/Python/pystate.c", state) # PyInterpreterState_New
cparser.parse(CPythonDir + "/Python/sysmodule.c", state) # PySys_ResetWarnOptions
cparser.parse(CPythonDir + "/Include/structmember.h", state) # struct PyMemberDef. just for now to avoid errors :)

import cparser.interpreter

interpreter = cparser.interpreter.Interpreter()
interpreter.register(state)
interpreter.registerFinalize()

if __name__ == '__main__':
	print "erros so far:"
	for m in state._errors:
		print m
	
	print
	print "PyAST of Py_Main:"
	interpreter.dumpFunc("Py_Main")
	
	args = ("Py_Main", len(sys.argv), sys.argv + [None])
	print "Run", args, ":"
	interpreter.runFunc(*args)