OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
evaluator.cpp
Go to the documentation of this file.
1#include "evaluator.hpp"
2
3#include <iostream>
4
5namespace client { namespace code_gen {
6
7 bool StackEvaluator::operator()(unsigned int x) {
8 evaluation_stack_.emplace_back(std::in_place_type<double>, static_cast<double>(x));
9 return true;
10 }
11
13 evaluation_stack_.emplace_back(std::in_place_type<double>, x);
14 return true;
15 }
16
18 evaluation_stack_.emplace_back(std::in_place_type<double>, static_cast<double>(x));
19 return true;
20 }
21
23 evaluation_stack_.emplace_back(std::in_place_type<std::string>, x.value);
24 return true;
25 }
26
28
29 auto i = variableDictionary_.find(x.name);
30 if (i == variableDictionary_.end()) {
31 std::cout << "Undefined variable " << x.name << std::endl;
32 return false;
33 }
34
35 evaluation_stack_.emplace_back(std::in_place_type<double>, i->second);
36 return true;
37 }
38
40
41 if (!ast::apply_visitor(*this, x.operand_)) {
42 return false;
43 }
44
45 double op2 = std::get<double>(evaluation_stack_.back());
46 evaluation_stack_.pop_back();
47 double op1 = std::get<double>(evaluation_stack_.back());
48 evaluation_stack_.pop_back();
49 double res = 0.0;
50
51 switch (x.operator_) {
52 case ast::op_plus : res = op1 + op2; break;
53 case ast::op_minus : res = op1 - op2; break;
54 case ast::op_times : res = op1 * op2; break;
55 case ast::op_divide : res = op1 / op2; break;
56
57 case ast::op_equal : res = op1 == op2; break;
58 case ast::op_not_equal : res = op1 != op2; break;
59 case ast::op_less : res = op1 < op2; break;
60 case ast::op_less_equal : res = op1 <= op2; break;
61 case ast::op_greater : res = op1 > op2; break;
62 case ast::op_greater_equal : res = op1 >= op2; break;
63
64 case ast::op_and : res = op1 && op2; break;
65 case ast::op_or : res = op1 || op2; break;
66 default : assert(false); return false;
67 }
68
69 evaluation_stack_.emplace_back(std::in_place_type<double>, res);
70 return true;
71 }
72
74
75 if (!ast::apply_visitor(*this, x.operand_))
76 return false;
77
78 double op = std::get<double>(evaluation_stack_.back());
79 evaluation_stack_.pop_back();
80
81 switch (x.operator_) {
82 case ast::op_negative : op = -op; break;
83 case ast::op_not : op = !op; break;
84 case ast::op_positive : break;
85 default : assert(false); return false;
86 }
87
88 evaluation_stack_.emplace_back(std::in_place_type<double>, op);
89 return true;
90 }
91
93
94 for (ast::function_call_argument const& arg : x.args) {
95 if (!ast::apply_visitor(*this, arg))
96 return false;
97 }
98
99 std::vector<client::function::argument_t> args(x.args.size());
100 for (size_t i = 0; i < x.args.size(); ++i) {
101 args[x.args.size() - 1 - i] = std::move(evaluation_stack_.back());
102 evaluation_stack_.pop_back();
103 }
104
105 std::map<std::string, client::function::type>::iterator itr =
107 if (itr == functions_.end()) {
108 std::cout << "Undefined function "
109 << x.function_name.name << std::endl;
110 return false;
111 }
112
113 auto function_eval = itr->second(args);
114 if (!std::get<1>(function_eval)) {
115 return false;
116 }
117
118 double function_result = std::get<0>(function_eval);
119 evaluation_stack_.emplace_back(std::in_place_type<double>, function_result);
120
121 return true;
122 }
123
125
126 if (!ast::apply_visitor(*this, x.first))
127 return false;
128
129 for (ast::operation const& oper : x.rest) {
130 if (!(*this)(oper))
131 return false;
132 }
133
134 return true;
135 }
136
137}}
arg(a))
decltype(auto) apply_visitor(Visitor &&vis, Variant &&var)
Definition ast.hpp:76
@ op_not_equal
Definition ast.hpp:142
@ op_less_equal
Definition ast.hpp:144
@ op_greater
Definition ast.hpp:145
@ op_greater_equal
Definition ast.hpp:146
@ op_positive
Definition ast.hpp:138
@ op_negative
Definition ast.hpp:139
@ op_divide
Definition ast.hpp:137
std::variant< expression, quoted_string > function_call_argument
Definition ast.hpp:130
std::string name
Definition ast.hpp:105
operand operand_
Definition ast.hpp:154
optoken operator_
Definition ast.hpp:153
std::list< function_call_argument > args
Definition ast.hpp:166
identifier function_name
Definition ast.hpp:165
std::list< operation > rest
Definition ast.hpp:172
client::function::arguments_t evaluation_stack_
Definition evaluator.hpp:78
std::map< std::string, client::function::type > functions_
Definition evaluator.hpp:75
std::map< std::string, double > variableDictionary_
Definition evaluator.hpp:74