This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/python/python.c

128 lines
4.4 KiB
C
Raw Normal View History

2012-10-08 23:58:22 +01:00
2017-06-15 22:40:55 +01:00
2017-06-12 18:00:47 +01:00
#include "py4yap.h"
2017-05-27 22:54:00 +01:00
#include <VFS.h>
2018-07-23 17:13:51 +01:00
#define USES_REGS
2017-08-21 12:36:48 +01:00
#include "YapStreams.h"
2016-07-31 16:09:21 +01:00
atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t,
2017-08-21 12:36:48 +01:00
ATOM_comma, ATOM_builtin, ATOM_A, ATOM_V, ATOM_self, ATOM_nil,
ATOM_brackets, ATOM_curly_brackets;
2018-03-19 11:43:14 +00:00
functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1, FUNCTOR_as2,
2017-08-21 12:36:48 +01:00
FUNCTOR_bin1, FUNCTOR_brackets1, FUNCTOR_comma2, FUNCTOR_dir1,
FUNCTOR_float1, FUNCTOR_int1, FUNCTOR_iter1, FUNCTOR_iter2, FUNCTOR_long1,
FUNCTOR_len1, FUNCTOR_curly1, FUNCTOR_ord1, FUNCTOR_range1, FUNCTOR_range2,
FUNCTOR_range3, FUNCTOR_sum1, FUNCTOR_pointer1, FUNCTOR_complex2,
FUNCTOR_plus2, FUNCTOR_sub2, FUNCTOR_mul2, FUNCTOR_div2, FUNCTOR_hat2,
FUNCTOR_colon2, FUNCTOR_comma2, FUNCTOR_equal2, FUNCTOR_sqbrackets2,
FUNCTOR_dot2, FUNCTOR_brackets1;
2015-09-21 23:05:36 +01:00
2018-01-18 14:47:27 +00:00
X_API PyObject *py_Atoms;
2017-02-20 14:37:26 +00:00
X_API PyObject *py_Yapex;
X_API PyObject *py_Sys;
2018-07-17 11:43:57 +01:00
X_API PyObject * pYAPError;
2017-02-20 14:37:26 +00:00
PyObject *py_Context;
2012-10-25 00:33:02 +01:00
2017-08-21 12:36:48 +01:00
X_API PyObject *Py_f2p;
2012-10-25 00:33:02 +01:00
2017-06-15 22:40:55 +01:00
extern X_API bool python_in_python;
2016-09-30 23:11:13 +01:00
2017-05-02 07:38:23 +01:00
static void add_modules(void) {
2018-08-21 03:01:03 +01:00
py_Atoms= PyDict_New();
if ( PyDict_Contains(PyImport_GetModuleDict(), PyUnicode_FromString("__main__"))) {
py_Main = PyDict_GetItemString(PyImport_GetModuleDict(),"__main__");
} else {
py_Main = PyImport_ImportModule("__main__");
}
2017-05-02 07:38:23 +01:00
Py_INCREF(py_Main);
2018-08-21 03:01:03 +01:00
py_Yapex = PyImport_ImportModule("yap4py.yapi");
2017-05-02 07:38:23 +01:00
if (py_Yapex)
Py_INCREF(py_Yapex);
2017-08-21 12:36:48 +01:00
Py_f2p = PythonLookup("f2p", NULL);
2018-07-27 11:11:04 +01:00
if (!Py_f2p)
2018-08-21 03:01:03 +01:00
Py_f2p = PyDict_New();
2018-07-27 11:11:04 +01:00
Py_INCREF(Py_f2p);
2018-03-02 21:18:24 +00:00
init_python_vfs();
2017-05-02 07:38:23 +01:00
}
2016-07-31 16:09:21 +01:00
static void install_py_constants(void) {
FUNCTOR_dot2 = PL_new_functor(PL_new_atom("."), 2);
2012-10-08 23:58:22 +01:00
// FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
// FUNCTOR_boolop1 = PL_new_functor(PL_new_atom("@"), 1);
2016-07-31 16:09:21 +01:00
ATOM_A = PL_new_atom("A");
ATOM_V = PL_new_atom("V");
2015-09-21 23:05:36 +01:00
ATOM_builtin = PL_new_atom("__builtin__");
ATOM_comma = PL_new_atom(",");
2012-11-27 00:16:34 +00:00
ATOM_colon = PL_new_atom(":");
ATOM_true = PL_new_atom("true");
2012-10-17 10:56:44 +01:00
ATOM_false = PL_new_atom("false");
ATOM_dot = PL_new_atom(".");
2016-09-30 23:11:13 +01:00
ATOM_self = PL_new_atom("self");
2017-05-08 18:51:29 +01:00
ATOM_nil = PL_new_atom("[]");
ATOM_brackets = PL_new_atom("()");
ATOM_curly_brackets = PL_new_atom("{}");
2012-11-25 23:37:28 +00:00
FUNCTOR_abs1 = PL_new_functor(PL_new_atom("abs"), 1);
FUNCTOR_all1 = PL_new_functor(PL_new_atom("all"), 1);
FUNCTOR_any1 = PL_new_functor(PL_new_atom("any"), 1);
2018-03-19 11:43:14 +00:00
FUNCTOR_as2 = PL_new_functor(PL_new_atom("as"), 2);
2012-11-25 23:37:28 +00:00
FUNCTOR_bin1 = PL_new_functor(PL_new_atom("bin"), 1);
FUNCTOR_ord1 = PL_new_functor(PL_new_atom("ord"), 1);
2012-11-27 00:16:34 +00:00
FUNCTOR_int1 = PL_new_functor(PL_new_atom("int"), 1);
FUNCTOR_long1 = PL_new_functor(PL_new_atom("long"), 1);
FUNCTOR_float1 = PL_new_functor(PL_new_atom("float"), 1);
2012-11-25 23:37:28 +00:00
FUNCTOR_curly1 = PL_new_functor(PL_new_atom("{}"), 1);
2017-02-20 14:37:26 +00:00
FUNCTOR_brackets1 = PL_new_functor(PL_new_atom("()"), 1);
2012-10-25 00:33:02 +01:00
FUNCTOR_dollar1 = PL_new_functor(PL_new_atom("$"), 1);
FUNCTOR_pointer1 = PL_new_functor(PL_new_atom("__obj__"), 1);
2012-11-05 13:49:15 +00:00
FUNCTOR_dir1 = PL_new_functor(PL_new_atom("dir"), 1);
FUNCTOR_iter1 = PL_new_functor(PL_new_atom("iter"), 1);
2012-11-27 12:10:41 +00:00
FUNCTOR_iter2 = PL_new_functor(PL_new_atom("iter"), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_len1 = PL_new_functor(PL_new_atom("len"), 1);
2012-11-27 12:10:41 +00:00
FUNCTOR_range1 = PL_new_functor(PL_new_atom("range"), 1);
FUNCTOR_range2 = PL_new_functor(PL_new_atom("range"), 2);
FUNCTOR_range3 = PL_new_functor(PL_new_atom("range"), 3);
FUNCTOR_sum1 = PL_new_functor(PL_new_atom("sum"), 1);
2015-08-07 22:57:53 +01:00
FUNCTOR_complex2 = PL_new_functor(PL_new_atom("i"), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_plus2 = PL_new_functor(PL_new_atom("+"), 2);
2012-10-25 00:33:02 +01:00
FUNCTOR_sub2 = PL_new_functor(PL_new_atom("-"), 2);
FUNCTOR_mul2 = PL_new_functor(PL_new_atom("*"), 2);
FUNCTOR_div2 = PL_new_functor(PL_new_atom("/"), 2);
FUNCTOR_hat2 = PL_new_functor(PL_new_atom("^"), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_colon2 = PL_new_functor(PL_new_atom(":"), 2);
2012-11-25 23:37:28 +00:00
FUNCTOR_comma2 = PL_new_functor(PL_new_atom(","), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
FUNCTOR_sqbrackets2 = PL_new_functor(PL_new_atom("[]"), 2);
2017-08-21 12:36:48 +01:00
}
2016-07-31 16:09:21 +01:00
foreign_t end_python(void) {
2017-05-02 07:38:23 +01:00
if (!python_in_python)
Py_Finalize();
2016-07-31 16:09:21 +01:00
2017-02-20 14:37:26 +00:00
return true;
2016-07-31 16:09:21 +01:00
}
2017-02-20 14:37:26 +00:00
static bool libpython_initialized = 0;
2017-05-14 11:36:09 +01:00
X_API bool do_init_python(void) {
2017-02-20 14:37:26 +00:00
// char **argv;
2017-08-21 12:36:48 +01:00
if (libpython_initialized)
2017-02-20 14:37:26 +00:00
return true;
libpython_initialized = true;
2017-05-14 11:36:09 +01:00
2017-08-21 12:36:48 +01:00
// PyGILState_STATE gstate = PyGILState_Ensure();
2016-07-31 16:09:21 +01:00
term_t t = PL_new_term_ref();
2018-08-21 03:01:03 +01:00
if (!Py_IsInitialized())
2017-05-02 07:38:23 +01:00
Py_Initialize();
2016-07-31 16:09:21 +01:00
install_py_constants();
PL_reset_term_refs(t);
install_pl2pl();
2016-12-10 07:01:10 +00:00
// PyGILState_Release(gstate);
2017-08-21 12:36:48 +01:00
add_modules();
2017-12-20 00:29:15 +00:00
// python_output();
2017-05-14 11:36:09 +01:00
return true;
2012-10-08 23:58:22 +01:00
}