-
Notifications
You must be signed in to change notification settings - Fork 8
/
console.cpp
57 lines (44 loc) · 1.34 KB
/
console.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include "cppy3/cppy3.hpp"
static PyObject* hello(PyObject *self, PyObject *args, PyObject *keywds)
{
const char *name = "";
static char *kwlist[] = {(char*)"name", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "s", kwlist, &name))
return NULL;
printf("Hello! I am %s.\n", name);
Py_RETURN_NONE;
}
static PyMethodDef EmbMethods[] = {
{"hello", (PyCFunction)(void(*)(void))hello, METH_VARARGS | METH_KEYWORDS, "Say hello."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static PyModuleDef EmbModule = {
PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods
};
static PyObject*
PyInit_emb(void)
{
return PyModule_Create(&EmbModule);
}
int main(int argc, char *argv[])
{
cppy3::PythonVM instance("emb", PyInit_emb);
std::cout << "Hey, type in command line, e.g. print(2+2*2)" << std::endl
<< std::endl;
size_t i = 0;
for (std::string line; std::getline(std::cin, line); i++)
{
try
{
const cppy3::Var result = cppy3::eval(line.c_str());
std::cout << std::endl
<< "Out[#" << i << " " << result.typeName() << "] " << result.toUTF8String() << std::endl;
}
catch (const cppy3::PythonException &e)
{
std::cerr << e.what() << std::endl;
}
}
return 0;
}