Zigbee Protocol Controller 1.6.0
attribute_mapper_ast_print.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
14#include <iostream>
15#include <iomanip>
16#include "attribute.hpp"
19
28namespace ast
29{
30class print
31{
32 public:
33 print(std::ostream &out) : _out(out) {};
34
35 inline void operator()(const nil &x)
36 {
37 _out << "undefined";
38 }
39
40 inline void operator()(float n)
41 {
42 _out << n;
43 }
44
45 inline void operator()(uint32_t n)
46 {
49 const char *name = attribute_store_get_type_name(type);
50 if (name) {
51 _out << name;
52 return;
53 }
54 }
55 _out << std::hex << "0x" << type;
56 }
57
58 inline void operator()(const attribute &a);
59 inline void operator()(const signed_ &x)
60 {
61 _out << x.sign;
62 boost::apply_visitor(*this, x.operand_);
63 }
64
65 inline void operator()(const condition &x)
66 {
67 _out << "if ";
68 boost::apply_visitor(*this, x.cond_value);
69 _out << ' ';
70 boost::apply_visitor(*this, x.cond_true);
71 _out << ' ';
72 boost::apply_visitor(*this, x.cond_false);
73 }
74
75 inline void operator()(const operation &x)
76 {
77 const char *op_str[] = {"+",
78 "-",
79 "==",
80 "*",
81 "/",
82 "&",
83 "|",
84 "^",
85 "<",
86 ">",
87 "or",
88 "%",
89 "**",
90 "!=",
91 "<=",
92 ">="};
93 _out << op_str[x.operator_] << ' ';
94 boost::apply_visitor(*this, x.operand_);
95 }
96
97 inline void operator()(const expression &x)
98 {
99 _out << "( ";
100 boost::apply_visitor(*this, x.first);
101 _out << ' ';
102 for (const auto &terms: x.rest) {
103 (*this)(terms);
104 _out << ' ';
105 }
106 _out << ")";
107 }
108
109 inline void operator()(const assignment &a)
110 {
112 _out << "i:";
113 } else if (a.type == AssignmentType::CLEARANCE) {
114 _out << "c:";
115 }
116
117 (*this)(a.lhs);
118 _out << " = ";
119 (*this)(a.rhs);
120 }
121
122 inline void operator()(const scope &x)
123 {
124 _out << "scope " << x.priority << " {" << std::endl;
125 for (auto &a: x.assignments) {
126 _out << " ";
127 (*this)(a);
128 _out << std::endl;
129 }
130 _out << "}" << std::endl;
131 }
132
133 inline void operator()(const function_invokation &f)
134 {
135 _out << f.function_name << "(";
136 for (auto &argument: f.arguments) {
137 (*this)(argument);
138 _out << ",";
139 }
140 _out << ")";
141 }
142
143 inline void operator()(const ast_tree ast)
144 {
145 for (const auto &ast_element: ast) {
146 boost::apply_visitor(*this, ast_element);
147 }
148 }
149
150 bool resolve_attr_names = false;
151
152 private:
153 std::ostream &_out;
154};
155
157{
158 public:
159 path_printer(std::ostream &out) : _out(out), _printer(out) {}
160
161 inline void operator()(const ast::operand &operand)
162 {
164 boost::apply_visitor(_printer, operand);
166 }
167 // hat operator ^ (parent)
168 inline void operator()(const nil &nul)
169 {
170 _out << '^';
171 }
172 // just given by type id
173 inline void operator()(result_type_t type_id)
174 {
175 _out << round(type_id);
176 }
177
178 // Subscript operator
179 inline void operator()(const attribute_path_subscript &subscript)
180 {
182 boost::apply_visitor(_printer, subscript.identifier);
184 _out << '[';
185 boost::apply_visitor(_printer, subscript.index);
186 _out << ']';
187 }
188 // parse a path list
189 inline void operator()(const std::vector<attribute_path_element> &paths)
190 {
191 for (auto it = paths.begin(); it != paths.end(); it++) {
192 boost::apply_visitor(*this, *it);
193 if ((it + 1) != paths.end()) {
194 _out << '.';
195 }
196 }
197 }
198
199 private:
200 std::ostream &_out;
202};
203
204inline void print::operator()(const attribute &a)
205{
206 _out << a.value_type << '\'';
207 path_printer pp(_out);
208 pp(a.attribute_path);
209}
210
211/************************ ostream helpers *************************/
212
213inline std::ostream &operator<<(std::ostream &s,
214 const attribute_dependency_t &d)
215{
216 s << d.second << "'" << attribute_store_get_type_name(d.first);
217 return s;
218}
219
220inline std::ostream &operator<<(std::ostream &s, const assignment &d)
221{
222 print printer(s);
223 printer(d);
224 return s;
225}
226
227inline std::ostream &operator<<(std::ostream &s, const attribute &d)
228{
229 print printer(s);
230 printer(d);
231 return s;
232}
233
234inline std::ostream &operator<<(std::ostream &s, const ast_tree &d)
235{
236 print printer(s);
237 printer(d);
238 return s;
239}
240
241} // namespace ast
242
attribute store C++ wrapper.
uint32_t attribute_store_type_t
Definition: attribute_store.h:50
Definition: attribute_mapper_ast_print.hpp:157
void operator()(result_type_t type_id)
Definition: attribute_mapper_ast_print.hpp:173
void operator()(const nil &nul)
Definition: attribute_mapper_ast_print.hpp:168
print _printer
Definition: attribute_mapper_ast_print.hpp:201
void operator()(const ast::operand &operand)
Definition: attribute_mapper_ast_print.hpp:161
std::ostream & _out
Definition: attribute_mapper_ast_print.hpp:200
path_printer(std::ostream &out)
Definition: attribute_mapper_ast_print.hpp:159
void operator()(const std::vector< attribute_path_element > &paths)
Definition: attribute_mapper_ast_print.hpp:189
void operator()(const attribute_path_subscript &subscript)
Definition: attribute_mapper_ast_print.hpp:179
Definition: attribute_mapper_ast_print.hpp:31
std::ostream & _out
Definition: attribute_mapper_ast_print.hpp:153
void operator()(const signed_ &x)
Definition: attribute_mapper_ast_print.hpp:59
void operator()(const nil &x)
Definition: attribute_mapper_ast_print.hpp:35
print(std::ostream &out)
Definition: attribute_mapper_ast_print.hpp:33
bool resolve_attr_names
Definition: attribute_mapper_ast_print.hpp:150
void operator()(const ast_tree ast)
Definition: attribute_mapper_ast_print.hpp:143
void operator()(const expression &x)
Definition: attribute_mapper_ast_print.hpp:97
void operator()(const function_invokation &f)
Definition: attribute_mapper_ast_print.hpp:133
void operator()(const assignment &a)
Definition: attribute_mapper_ast_print.hpp:109
void operator()(float n)
Definition: attribute_mapper_ast_print.hpp:40
void operator()(const condition &x)
Definition: attribute_mapper_ast_print.hpp:65
void operator()(const operation &x)
Definition: attribute_mapper_ast_print.hpp:75
void operator()(uint32_t n)
Definition: attribute_mapper_ast_print.hpp:45
void operator()(const scope &x)
Definition: attribute_mapper_ast_print.hpp:122
float result_type_t
Definition: attribute_mapper_ast.hpp:36
bool attribute_store_is_type_registered(attribute_store_type_t type)
Checks an attribute type is already registered.
const char * attribute_store_get_type_name(attribute_store_type_t type)
Returns the registered name of an attribute.
Definition: attribute_mapper_ast.hpp:39
@ INSTANCE
Definition: attribute_mapper_ast.hpp:203
@ CLEARANCE
Definition: attribute_mapper_ast.hpp:203
x3::variant< ast::nil, uint32_t, float, x3::forward_ast< attribute >, x3::forward_ast< signed_ >, x3::forward_ast< expression >, x3::forward_ast< condition >, x3::forward_ast< function_invokation > > operand
Operands.
Definition: attribute_mapper_ast.hpp:95
std::ostream & operator<<(std::ostream &s, const attribute_dependency_t &d)
Definition: attribute_mapper_ast_print.hpp:213
std::pair< attribute_store_type_t, value_type_t > attribute_dependency_t
Definition: attribute_mapper_ast_dep_eval.hpp:32
Assignment.
Definition: attribute_mapper_ast.hpp:193
ast::attribute lhs
left hand side of the assignment
Definition: attribute_mapper_ast.hpp:199
int type
Definition: attribute_mapper_ast.hpp:198
expression rhs
right hand side of the assignment
Definition: attribute_mapper_ast.hpp:200
Definition: attribute_mapper_ast.hpp:53
Attribute path subscript.
Definition: attribute_mapper_ast.hpp:160
operand identifier
Definition: attribute_mapper_ast.hpp:161
operand index
Definition: attribute_mapper_ast.hpp:162
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
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
Scope.
Definition: attribute_mapper_ast.hpp:234
std::vector< assignment > assignments
Definition: attribute_mapper_ast.hpp:237
int priority
Definition: attribute_mapper_ast.hpp:235
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