diff options
author | John Wiegley <johnw@newartisans.com> | 2007-04-30 06:26:38 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:38:33 -0400 |
commit | c8899addfd2deed3d84be2de234681db64987722 (patch) | |
tree | 07f9a5eb603ff4ec83fe18c83083575d2b7a439a /src/pyinterp.h | |
parent | aa9cc125796711afcaa459898e95527fdae8e912 (diff) | |
download | fork-ledger-c8899addfd2deed3d84be2de234681db64987722.tar.gz fork-ledger-c8899addfd2deed3d84be2de234681db64987722.tar.bz2 fork-ledger-c8899addfd2deed3d84be2de234681db64987722.zip |
Rearranged the sources a bit.
Diffstat (limited to 'src/pyinterp.h')
-rw-r--r-- | src/pyinterp.h | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/pyinterp.h b/src/pyinterp.h new file mode 100644 index 00000000..a75ef78b --- /dev/null +++ b/src/pyinterp.h @@ -0,0 +1,83 @@ +#ifndef _PY_EVAL_H +#define _PY_EVAL_H + +#include "xpath.h" + +#if defined(USE_BOOST_PYTHON) + +#include <boost/python.hpp> +#include <boost/python/detail/api_placeholder.hpp> +#include <boost/python/exception_translator.hpp> +#include <boost/python/suite/indexing/map_indexing_suite.hpp> + +#include <Python.h> + +#include "pyfstream.h" + +using namespace boost::python; + +namespace ledger { + +class python_interpreter_t : public xml::xpath_t::scope_t +{ + handle<> mmodule; + + public: + dict nspace; + + python_interpreter_t(xml::xpath_t::scope_t * parent); + + virtual ~python_interpreter_t() { + Py_Finalize(); + } + + object import(const string& name); + + enum py_eval_mode_t { + PY_EVAL_EXPR, + PY_EVAL_STMT, + PY_EVAL_MULTI + }; + + object eval(std::istream& in, py_eval_mode_t mode = PY_EVAL_EXPR); + object eval(const string& str, py_eval_mode_t mode = PY_EVAL_EXPR); + object eval(const char * c_str, py_eval_mode_t mode = PY_EVAL_EXPR) { + string str(c_str); + return eval(str, mode); + } + + class functor_t : public xml::xpath_t::functor_t { + protected: + object func; + public: + functor_t(const string& name, object _func) + : xml::xpath_t::functor_t(name), func(_func) {} + + virtual void operator()(value_t& result, xml::xpath_t::scope_t * locals); + }; + + virtual void define(const string& name, xml::xpath_t::op_t * def) { + // Pass any definitions up to our parent + parent->define(name, def); + } + + virtual xml::xpath_t::op_t * lookup(const string& name) { + object func = eval(name); + if (! func) + return parent ? parent->lookup(name) : NULL; + return xml::xpath_t::wrap_functor(new functor_t(name, func)); + } + + class lambda_t : public functor_t { + public: + lambda_t(object code) : functor_t("<lambda>", code) {} + + virtual void operator()(value_t& result, xml::xpath_t::scope_t * locals); + }; +}; + +} // namespace ledger + +#endif // USE_BOOST_PYTHON + +#endif // _PY_EVAL_H |