I would like to create a built-in Python interpreter in my C / C ++ application. Ideally, this interpreter will behave exactly like a real Python interpreter, but it will produce a result after processing each line of input. The standard Python module codelooks exactly the same from the outside as I do, except that it is written in Python. For instance:.
>>> import code
>>> code.interact()
Python 2.7.1 (r271:86832, Jan 3 2011, 15:34:27)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
The core codeis a function that allows for potentially incomplete user input and either displays a syntax error (case 1), or expects more input (case 2), or performs user input (case 3).
try:
code = self.compile(source, filename, symbol)
except (OverflowError, SyntaxError, ValueError):
self.showsyntaxerror(filename)
return False
if code is None:
return True
self.runcode(code)
return False
Python Demo/embed/demo.c - , , , . :
#include "Python.h"
main(int argc, char **argv)
{
Py_Initialize();
[snip]
PyRun_SimpleString("import sys\n");
[snip]
Py_Exit(0);
}
C , stacktraces .. Python. .