Zigbee Protocol Controller 1.6.0
attribute_mapper_ast_eval.hpp
Go to the documentation of this file.
1/******************************************************************************
2 * # License
3 * <b>Copyright 2021 Silicon Laboratories Inc. www.silabs.com</b>
4 ******************************************************************************
5 * The licensor of this software is Silicon Laboratories Inc. Your use of this
6 * software is governed by the terms of Silicon Labs Master Software License
7 * Agreement (MSLA) available at
8 * www.silabs.com/about-us/legal/master-software-license-agreement. This
9 * software is distributed to you in Source Code format and is governed by the
10 * sections of the MSLA applicable to Source Code.
11 *
12 *****************************************************************************/
13
22#ifndef ATTRIBUTE_MAPPER_AST_EVAL_HPP
23#define ATTRIBUTE_MAPPER_AST_EVAL_HPP
24
25#include <string>
26#include <map>
27#include <float.h>
28#include <boost/optional.hpp>
29#include "attribute.hpp"
33
34#include "sl_log.h"
35
36namespace ast
37{
38struct nil;
39struct signed_;
40struct expression;
41struct condition;
42struct attribute;
43struct attribute_path_subscript;
44struct mapping;
45struct assignment;
46
73template<typename T> struct eval {
74 public:
75 using result_type = boost::optional<T>;
76
82 explicit eval(const attribute_store::attribute context = 0) : context(context)
83 {}
84
88 result_type operator()(const nil &) const
89 {
90 return result_type();
91 }
92
97 {
98 return n;
99 }
100
104 result_type operator()(const float n) const
105 {
106 return n;
107 }
108
119 {
120 if (x.operator_ == operator_or) {
121 if (!context.is_valid()) {
122 return result_type();
123 }
124
125 if (lhs) {
126 return lhs;
127 } else {
128 return boost::apply_visitor(*this, x.operand_);
129 }
130 }
131
132 result_type rhs = boost::apply_visitor(*this, x.operand_);
133 if (rhs && lhs) {
134 switch (x.operator_) {
135 case operator_plus:
136 return lhs.value() + rhs.value();
137 case operator_minus:
138 return lhs.value() - rhs.value();
139 case operator_mult:
140 return lhs.value() * rhs.value();
141 case operator_div:
142 return lhs.value() / rhs.value();
143 case operator_equals:
144 return trunc(1000. * lhs.value()) == trunc(1000. * rhs.value());
146 return lhs.value() <= rhs.value();
148 return lhs.value() >= rhs.value();
150 return lhs.value() < rhs.value();
152 return lhs.value() > rhs.value();
153 case operator_bitand:
154 return ((int)round(lhs.value())) & ((int)round(rhs.value()));
155 case operator_bitor:
156 return ((int)round(lhs.value())) | ((int)round(rhs.value()));
157 case operator_bitxor:
158 return ((int)round(lhs.value())) ^ ((int)round(rhs.value()));
159 case operator_modulo:
160 return fmod(lhs.value(), rhs.value());
162 return pow(lhs.value(), rhs.value());
163 case operator_neq:
164 return lhs.value() != rhs.value();
165 case operator_or: //handled above
166 break;
167 default:
168 break;
169 }
170 }
171 return result_type();
172 }
177 {
178 result_type rhs = boost::apply_visitor(*this, x.operand_);
179 if (rhs) {
180 switch (x.sign) {
181 case '-':
182 return -rhs.value();
183 default: // Assumed to be only '+' for now.
184 return +rhs.value();
185 }
186 }
187 return result_type();
188 }
189
196 {
197 result_type state = boost::apply_visitor(*this, x.first);
198 for (operation const &oper: x.rest) {
199 state = (*this)(oper, state);
200 }
201 return state;
202 }
203
208 {
209 result_type cond = boost::apply_visitor(*this, x.cond_value);
210 if (cond && cond.value()) {
211 return boost::apply_visitor(*this, x.cond_true);
212 } else {
213 return boost::apply_visitor(*this, x.cond_false);
214 }
215 }
216
221 {
222 if (!context.is_valid()) {
223 return result_type();
224 }
225
227 attribute_store::attribute node = ape(a.attribute_path);
228
229 if (a.value_type == 'e') { //existence evaluation
230 // If all elements have bee parsed this means that we have found the attribute
231 // if the attribute gets deleted, it gets caught above in "!context.is_valid()"
232 return ape.all_elements_parsed();
233 } else if (ape.all_elements_parsed()) {
234 T result = 0;
235 if (a.value_type == 'd') {
237 } else if (a.value_type == 'r') {
239 }
240 if (result != FLT_MIN) {
241 return result;
242 }
243 }
244 return result_type();
245 }
246
251 {
252 // Evaluate all expressions in arguments and put them into a vector
253 std::vector<result_type> results;
254 for (auto &argument: f.arguments) {
255 results.push_back((*this)(argument));
256 }
257
258 // look up our built-in function library
259 const auto &functions = get_built_in_functions<T>();
260
261 if (functions.count(f.function_name) == 0) {
262 sl_log_error("attribute_mapper_ast_eval",
263 "Unknown function (%s), returning undefined",
264 f.function_name.c_str());
265 return result_type();
266 }
267 // Invoke the function
268 return functions.at(f.function_name)(results);
269 }
270
271 private:
272 attribute_store::attribute context;
273};
274
275} // namespace ast
276
277#endif //ATTRIBUTE_MAPPER_AST_EVAL_HPP
278
attribute store C++ wrapper.
number_t attribute_store_get_reported_number(attribute_store_node_t node)
Gets a numerical value from an Attribute Store node.
Definition: attribute_store_helper.c:796
number_t attribute_store_get_desired_number(attribute_store_node_t node)
Gets a numerical value from an Attribute Store node.
Definition: attribute_store_helper.c:801
#define sl_log_error(tag, fmtstr,...)
Definition: sl_log.h:145
Definition: attribute_mapper_ast.hpp:39
@ operator_modulo
Definition: attribute_mapper_ast.hpp:72
@ operator_div
Definition: attribute_mapper_ast.hpp:65
@ operator_bitand
Definition: attribute_mapper_ast.hpp:66
@ operator_bitxor
Definition: attribute_mapper_ast.hpp:68
@ operator_minus
Definition: attribute_mapper_ast.hpp:62
@ operator_bitor
Definition: attribute_mapper_ast.hpp:67
@ operator_mult
Definition: attribute_mapper_ast.hpp:64
@ operator_or
Definition: attribute_mapper_ast.hpp:71
@ operator_less_than_or_eq
Definition: attribute_mapper_ast.hpp:75
@ operator_exponent
Definition: attribute_mapper_ast.hpp:73
@ operator_greater_than_or_eq
Definition: attribute_mapper_ast.hpp:76
@ operator_greater_than
Definition: attribute_mapper_ast.hpp:70
@ operator_plus
Definition: attribute_mapper_ast.hpp:61
@ operator_less_than
Definition: attribute_mapper_ast.hpp:69
@ operator_equals
Definition: attribute_mapper_ast.hpp:63
@ operator_neq
Definition: attribute_mapper_ast.hpp:74
The attribute path evaluator evalues a full attribute path and returns the matching attribute store a...
Definition: attribute_mapper_ast_path_eval.hpp:51
bool all_elements_parsed() const
Definition: attribute_mapper_ast_path_eval.hpp:81
Attribute.
Definition: attribute_mapper_ast.hpp:177
std::vector< attribute_path_element > attribute_path
Definition: attribute_mapper_ast.hpp:179
char value_type
Definition: attribute_mapper_ast.hpp:178
Condition.
Definition: attribute_mapper_ast.hpp:147
operand cond_true
Truth value.
Definition: attribute_mapper_ast.hpp:149
operand cond_false
False value.
Definition: attribute_mapper_ast.hpp:150
operand cond_value
Selector value.
Definition: attribute_mapper_ast.hpp:148
AST evaluator.
Definition: attribute_mapper_ast_eval.hpp:73
result_type operator()(const function_invokation &f) const
Evaluates Function invokation.
Definition: attribute_mapper_ast_eval.hpp:250
result_type operator()(const expression &x) const
Condition.
Definition: attribute_mapper_ast_eval.hpp:195
attribute_store::attribute context
Definition: attribute_mapper_ast_eval.hpp:272
result_type operator()(const operation &x, result_type lhs) const
An operation.
Definition: attribute_mapper_ast_eval.hpp:118
result_type operator()(const condition &x) const
The normal entry point of the evaluator.
Definition: attribute_mapper_ast_eval.hpp:207
result_type operator()(const uint32_t n) const
a literal number
Definition: attribute_mapper_ast_eval.hpp:96
result_type operator()(const nil &) const
the undefined keyword
Definition: attribute_mapper_ast_eval.hpp:88
result_type operator()(const signed_ &x) const
Unary operation.
Definition: attribute_mapper_ast_eval.hpp:176
result_type operator()(const attribute &a) const
an attribute read from the attribute store
Definition: attribute_mapper_ast_eval.hpp:220
boost::optional< T > result_type
Definition: attribute_mapper_ast_eval.hpp:75
eval(const attribute_store::attribute context=0)
Construct a new eval object.
Definition: attribute_mapper_ast_eval.hpp:82
result_type operator()(const float n) const
a literal number
Definition: attribute_mapper_ast_eval.hpp:104
Expression.
Definition: attribute_mapper_ast.hpp:134
operand first
Definition: attribute_mapper_ast.hpp:135
std::list< operation > rest
Definition: attribute_mapper_ast.hpp:136
Built-in function invokation Example : fn_min_value(r'3, 5, d'5 or 20)
Definition: attribute_mapper_ast.hpp:209
std::vector< expression > arguments
Definition: attribute_mapper_ast.hpp:211
std::string function_name
Definition: attribute_mapper_ast.hpp:210
Definition: attribute_mapper_ast.hpp:42
Operation An operation consists of an operator and an right hand side operand. When operations are ev...
Definition: attribute_mapper_ast.hpp:123
operator_ids operator_
+,-,*,/ etc.
Definition: attribute_mapper_ast.hpp:124
operand operand_
right hand side operand
Definition: attribute_mapper_ast.hpp:125
Uniary signed operand.
Definition: attribute_mapper_ast.hpp:111
char sign
Definition: attribute_mapper_ast.hpp:112
operand operand_
Definition: attribute_mapper_ast.hpp:113
pthread_cond_t cond
Definition: uic_stdin_process.c:53