OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
evaluator.hpp
Go to the documentation of this file.
1 /*=============================================================================
2  Adapted from boost spirit mini_c example.
3 
4  Distributed under the Boost Software License, Version 1.0. (See accompanying
5  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(STACKEVALUATOR_HPP)
8 #define STACKEVALUATOR_HPP
9 
10 #include <map>
11 #include <vector>
12 
13 #include <boost/function.hpp>
14 #include <boost/assert.hpp>
15 #include <boost/variant/get.hpp>
16 #include <boost/variant/variant.hpp>
17 #include <boost/spirit/include/phoenix_core.hpp>
18 #include <boost/spirit/include/phoenix_function.hpp>
19 #include <boost/spirit/include/phoenix_operator.hpp>
20 
21 #include "ast.hpp"
22 #include "function.hpp"
23 #include "error_handler.hpp"
24 
25 namespace client { namespace code_gen
26 {
27  struct StackEvaluator {
28 
29  typedef bool result_type;
30 
31  template <typename ErrorHandler>
32  StackEvaluator(ErrorHandler& error_handler_)
33  {
34  namespace phx = boost::phoenix;
35  using boost::phoenix::function;
36 
37  error_handler = function<ErrorHandler>(error_handler_)
38  (std::string("Error! "),
39  phx::arg_names::_2,
40  phx::cref(error_handler_.iters)[phx::arg_names::_1]);
41  }
42 
43  double result() {
44  BOOST_ASSERT(evaluation_stack_.size() == 1);
46  double result = boost::get<double>(res);
47  evaluation_stack_.pop_back();
48  return result;
49  }
50 
51  void registerFunction(std::string name,
52  client::function::type callback) {
53  functions_.insert(client::function::named_t(name, callback));
54  }
55 
57  std::map<std::string, client::function::type> functions) {
58  functions_.insert(functions.begin(), functions.end());
59  }
60 
62  std::map<std::string, double> variableDictionary) {
63  variableDictionary_ = variableDictionary;
64  }
65 
66  // visitor
67  bool operator()(ast::nil) { BOOST_ASSERT(0); return false; }
68  bool operator()(unsigned int x);
69  bool operator()(double x);
70  bool operator()(bool x);
71  bool operator()(ast::quoted_string const& x);
72  bool operator()(ast::identifier const& x);
73  bool operator()(ast::operation const& x);
74  bool operator()(ast::unary const& x);
75  bool operator()(ast::function_call const& x);
76  bool operator()(ast::expression const& x);
77 
78  private:
79 
80  boost::function<void(int tag, std::string const& what)> error_handler;
81 
82  std::map<std::string, double> variableDictionary_;
83  std::map<std::string, client::function::type> functions_;
84 
85  //our stack is conform to function call arguments
87  };
88 }}
89 
90 #endif
boost::variant< double, bool, std::string > argument_t
Definition: function.hpp:17
void registerFunctions(std::map< std::string, client::function::type > functions)
Definition: evaluator.hpp:56
std::vector< argument_t > arguments_t
Definition: function.hpp:19
std::pair< std::string, type > named_t
Definition: function.hpp:23
boost::function< void(int tag, std::string const &what)> error_handler
Definition: evaluator.hpp:80
std::map< std::string, double > variableDictionary_
Definition: evaluator.hpp:82
std::map< std::string, client::function::type > functions_
Definition: evaluator.hpp:83
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
const std::string name
void registerVariables(std::map< std::string, double > variableDictionary)
Definition: evaluator.hpp:61
void registerFunction(std::string name, client::function::type callback)
Definition: evaluator.hpp:51
StackEvaluator(ErrorHandler &error_handler_)
Definition: evaluator.hpp:32
client::function::arguments_t evaluation_stack_
Definition: evaluator.hpp:86