OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
evaluator.cpp
Go to the documentation of this file.
1 /*=============================================================================
2  Distributed under the Boost Software License, Version 1.0. (See accompanying
3  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4 =============================================================================*/
5 #include "evaluator.hpp"
6 
7 #include <boost/variant/apply_visitor.hpp>
8 
9 namespace client { namespace code_gen {
10 
11  bool StackEvaluator::operator()(unsigned int x) {
12 
13  evaluation_stack_.push_back(static_cast<double>(x));
14  return true;
15  }
16 
17  bool StackEvaluator::operator()(double x) {
18 
19  evaluation_stack_.push_back(x);
20  return true;
21  }
22 
24 
25  evaluation_stack_.push_back(static_cast<double>(x));
26  return true;
27  }
28 
30 
31  evaluation_stack_.push_back(x.value);
32  return true;
33  }
34 
36 
38  variableDictionary_.find(x.name);
39  if(i == variableDictionary_.end()) {
40  std::cout << "Undefined variable " << x.name << std::endl;
41  return false;
42  }
43 
44  evaluation_stack_.push_back(i->second);
45  return true;
46  }
47 
49 
50  if (!boost::apply_visitor(*this, x.operand_))
51  return false;
52 
53  double op2 = boost::get<double>(evaluation_stack_.back());
54  evaluation_stack_.pop_back();
55  double op1 = boost::get<double>(evaluation_stack_.back());
56  evaluation_stack_.pop_back();
57  double res = 0.0;
58 
59  switch (x.operator_) {
60  case ast::op_plus : res = op1 + op2; break;
61  case ast::op_minus : res = op1 - op2; break;
62  case ast::op_times : res = op1 * op2; break;
63  case ast::op_divide : res = op1 / op2; break;
64 
65  case ast::op_equal : res = op1 == op2; break;
66  case ast::op_not_equal : res = op1 != op2; break;
67  case ast::op_less : res = op1 < op2; break;
68  case ast::op_less_equal : res = op1 <= op2; break;
69  case ast::op_greater : res = op1 > op2; break;
70  case ast::op_greater_equal : res = op1 >= op2; break;
71 
72  case ast::op_and : res = op1 && op2; break;
73  case ast::op_or : res = op1 || op2; break;
74  default : BOOST_ASSERT(0); return false;
75  }
76 
77  evaluation_stack_.push_back(res);
78  return true;
79  }
80 
82 
83  if (!boost::apply_visitor(*this, x.operand_))
84  return false;
85 
86  double op = boost::get<double>(evaluation_stack_.back());
87  evaluation_stack_.pop_back();
88 
89  switch (x.operator_) {
90  case ast::op_negative : op = -op; break;
91  case ast::op_not : op = !op; break;
92  case ast::op_positive : break;
93  default : BOOST_ASSERT(0); return false;
94  }
95 
96 
97  evaluation_stack_.push_back(op);
98  return true;
99  }
100 
102 
103  for(ast::function_call_argument const& arg : x.args) {
104  if (!boost::apply_visitor(*this, arg))
105  return false;
106  }
107 
108  std::vector<client::function::argument_t> args(x.args.size());
109  for(size_t i=0; i < x.args.size(); i++) {
111  args[x.args.size()-1-i] = arg;
112  evaluation_stack_.pop_back();
113  }
114 
116  functions_.find(x.function_name.name);
117  if(itr == functions_.end()) {
118  std::cout << "Undefined function "
119  << x.function_name.name << std::endl;
120  return false;
121  }
122 
123  if(! boost::get<1>(itr->second(args))) return false;
124 
125  double function_result = boost::get<0>(itr->second(args));
126  evaluation_stack_.push_back(function_result);
127 
128  return true;
129  }
130 
132 
133  if (!boost::apply_visitor(*this, x.first))
134  return false;
135 
136  for(ast::operation const& oper : x.rest) {
137  if (!(*this)(oper))
138  return false;
139  }
140 
141  return true;
142  }
143 
144 }}
145 
boost::variant< double, bool, std::string > argument_t
Definition: function.hpp:17
std::list< function_call_argument > args
Definition: ast.hpp:95
boost::variant< expression, quoted_string > function_call_argument
Definition: ast.hpp:59
operand operand_
Definition: ast.hpp:83
std::list< operation > rest
Definition: ast.hpp:101
operand operand_
Definition: ast.hpp:89
optoken operator_
Definition: ast.hpp:82
std::string name
Definition: ast.hpp:34
std::string value
Definition: ast.hpp:40
optoken operator_
Definition: ast.hpp:88
arg(a))
std::map< std::string, double > variableDictionary_
Definition: evaluator.hpp:82
std::map< std::string, client::function::type > functions_
Definition: evaluator.hpp:83
identifier function_name
Definition: ast.hpp:94
std::string::iterator iterator
Definition: MSLang.h:16
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
client::function::arguments_t evaluation_stack_
Definition: evaluator.hpp:86