diff options
author | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:35:00 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:35:00 -0400 |
commit | 42f43b7686038e4cbca16d8d2118b139544e6de3 (patch) | |
tree | 52c5473401c57282242d66b8dd75f4c07bf41d07 /py_eval.h | |
parent | c7b4370ff9c8ab5c96f15b1e712e6db6bdab6324 (diff) | |
download | fork-ledger-42f43b7686038e4cbca16d8d2118b139544e6de3.tar.gz fork-ledger-42f43b7686038e4cbca16d8d2118b139544e6de3.tar.bz2 fork-ledger-42f43b7686038e4cbca16d8d2118b139544e6de3.zip |
Check in all changes made so far toward 3.0.
Diffstat (limited to 'py_eval.h')
-rw-r--r-- | py_eval.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/py_eval.h b/py_eval.h new file mode 100644 index 00000000..109f5fc4 --- /dev/null +++ b/py_eval.h @@ -0,0 +1,77 @@ +#ifndef _PY_EVAL_H +#define _PY_EVAL_H + +#include "valexpr.h" +#include "pyfstream.h" + +#include <string> +#include <iostream> + +#include <boost/python.hpp> + +using namespace boost::python; + +namespace ledger { + +void shutdown_ledger_for_python(); + +class python_interpreter_t : public valexpr_t::scope_t +{ + handle<> mmodule; + dict nspace; + + public: + python_interpreter_t(valexpr_t::scope_t * parent); + + virtual ~python_interpreter_t() { + Py_Finalize(); + } + + object import(const std::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 std::string& str, py_eval_mode_t mode = PY_EVAL_EXPR); + object eval(const char * c_str, py_eval_mode_t mode = PY_EVAL_EXPR) { + std::string str(c_str); + return eval(str, mode); + } + + class functor_t : public valexpr_t::functor_t { + protected: + object func; + public: + python_functor_t(const std::string& name, object _func) + : valexpr_t::functor_t(name), func(_func) {} + + virtual void operator()(value_t& result, valexpr_t::scope_t * locals); + }; + + virtual void define(const std::string& name, valexpr_t::node_t * def) { + // Pass any definitions up to our parent + parent->define(name, def); + } + + virtual node_t * lookup(const std::string& name) { + object func = eval(name); + if (! func) + return parent ? parent->lookup(name) : NULL; + return valexpr_t::wrap_functor(new python_functor_t(name, func)); + } + + class lambda_t : public functor_t { + public: + python_lambda_t(object code) : python_functor_t("<lambda>"> code) {} + + virtual void operator()(value_t& result, valexpr_t::scope_t * locals); + }; +; + +} // namespace ledger + +#endif // _PY_EVAL_H |