summaryrefslogtreecommitdiff
path: root/src/pyinterp.h
blob: f32f4811af24c0c00c082aedb3b7d8d4657c7084 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef _PY_EVAL_H
#define _PY_EVAL_H

#include "xpath.h"

#if defined(USE_BOOST_PYTHON)

#include <boost/python.hpp>
#include <Python.h>

#include "pyfstream.h"

namespace ledger {

class python_interpreter_t : public xml::xpath_t::scope_t
{
  boost::python::handle<> mmodule;

 public:
  boost::python::dict nspace;

  python_interpreter_t(xml::xpath_t::scope_t * parent);

  virtual ~python_interpreter_t() {
    Py_Finalize();
  }

  boost::python::object import(const string& name);

  enum py_eval_mode_t {
    PY_EVAL_EXPR,
    PY_EVAL_STMT,
    PY_EVAL_MULTI
  };

  boost::python::object eval(std::istream& in,
			     py_eval_mode_t mode = PY_EVAL_EXPR);
  boost::python::object eval(const string& str,
			     py_eval_mode_t mode = PY_EVAL_EXPR);
  boost::python::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:
    boost::python::object func;
  public:
    functor_t(const string& name, boost::python::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) {
    boost::python::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(boost::python::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