Free Electron
Evaluator.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __evaluator_Evaluator_h__
8 #define __evaluator_Evaluator_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 class Debug : public Counted
16 {
17  public:
18  Debug(void)
19  {
20  m_depth = 0;
21  m_pushed = false;
22  }
23 virtual ~Debug(void) {}
24  void push(void) { m_pushed = true; m_depth++; }
25  void pop(void) { m_depth--; }
26  void log(const char *a_group, const char *fmt, ...)
27  {
28  std::string prefix;
29  std::string group = a_group;
30  if(group == "info")
31  {
32  prefix = "|";
33  for(unsigned int i = 1; i < m_depth; i++)
34  {
35  prefix += " |";
36  }
37  if(m_depth>0)
38  {
39  if(m_pushed)
40  {
41  prefix += "-->|";
42  }
43  else
44  {
45  prefix += " |";
46  }
47  }
48  prefix += " ";
49  m_pushed = false;
50  }
51 
52  t_stdstring str;
53  va_list ap;
54  int sz=0;
55  while(sz>=0)
56  {
57  va_start(ap, fmt);
58  vsPrintf(str,fmt,ap,sz);
59  va_end(ap);
60  }
61 
62  feLogGroup(a_group, "%s%s\n", prefix.c_str(), str.c_str());
63  }
64  unsigned int m_depth;
65  bool m_pushed;
66 };
67 
68 
69 class FE_DL_EXPORT AsVariable :
70  public AccessorSet,
71  public Initialize<AsVariable>
72 {
73  public:
74  void initialize(void)
75  {
76  add(value, FE_SPEC("eval:value", "evaluate real"));
77  }
78  Accessor<t_eval_real> value;
79 };
80 
81 class FE_DL_EXPORT AsString :
82  public AccessorSet,
83  public Initialize<AsString>
84 {
85  public:
86  void initialize(void)
87  {
88  add(value, FE_SPEC("eval:string", "evaluate string"));
89  }
90  Accessor<String> value;
91 };
92 
93 
94 class FE_DL_EXPORT RealFunction :
95  virtual public FunctionI,
96  public Initialize<RealFunction>
97 {
98  public:
99  RealFunction(void)
100  {
101  }
102  ~RealFunction(void) {}
103 
104  void initialize(void);
105 
106 virtual bool compile(sp<RecordGroup> a_rg, Record &a_return, t_stdvector<WeakRecord> &a_argv, t_stdstring &a_msg);
107 virtual sp<Aggregate> &returnType(void) { return m_aggregate; }
108 
109  sp<AsVariable> m_asVariable;
110  sp<Aggregate> m_aggregate;
111  bool m_valid;
112 };
113 
114 
115 typedef Registry::FactoryLocation *t_factory_location;
116 
117 class Evaluator;
118 
119 class Token
120 {
121  public:
122  Token(void) {}
123  Token(const t_stdstring &a_str, unsigned int a_line)
124  {
125  m_str = a_str;
126  m_line = a_line;
127  }
128  operator t_stdstring &()
129  {
130  return m_str;
131  }
132  const char *c_str(void)
133  {
134  return m_str.c_str();
135  }
136  t_stdstring m_str;
137  unsigned int m_line;
138 };
139 
140 typedef Token t_token;
141 
142 class Node : public Counted
143 {
144  public:
145  Node(void);
146 virtual ~Node(void);
147 
148  void compile(t_stdlist<t_token> &a_tokens,
149  Evaluator *a_evaluator,
150  Node *a_parent,
151  sp<Debug> a_debug);
152 
153  void preEvaluate(Evaluator *a_evaluator, sp<Debug> a_debug);
154  void evaluate(sp<t_variables> &a_variables);
155  Record &value(void) { return m_value; }
156 
157 virtual sp<Aggregate> &returnType(void) { return m_aggregate; }
158 
159  t_stdvector< sp<Node> > m_children;
160 
161  unsigned int m_leaf;
162 
163  t_token m_token;
164 
165  sp<FunctionI> m_function;
166  t_stdvector< WeakRecord > m_argv;
167  Record m_value;
168  sp< Aggregate > m_aggregate;
169 
170  bool m_is_define;
171  bool m_is_jump;
172 
173  sp<Node> m_jump_node;
174 
175  typedef enum
176  {
177  e_eval_none = -1,
178  e_eval_function = 0,
179  e_eval_assign_matched,
180  e_eval_assign_mismatch,
181  e_eval_single_child,
182  e_eval_childless,
183  e_eval_jump,
184  e_eval_impossible
185  } t_eval;
186 
187  t_eval m_t_eval;
188 
189  private:
190  void leafMake(Evaluator *a_evaluator, sp<Node> a_node);
191 
192 };
193 
194 class JumpLocation
195 {
196  public:
197  sp<Node> m_node;
198 };
199 
200 typedef JumpLocation t_jump_location;
201 
202 class FE_DL_EXPORT Evaluator :
203  virtual public EvaluatorI,
204  public Initialize<Evaluator>
205 {
206  public:
207  Evaluator(void);
208 virtual ~Evaluator(void);
209 
210  void initialize(void);
211 
212 virtual void bind(sp<RecordGroup> a_rg);
213 
214  // manage
215 virtual void compile(const t_stdstring &a_buffer);
216 virtual void evaluate(void);
217 
218 virtual void addFunction(const t_stdstring &a_function,
219  const t_stdstring &a_component);
220 virtual sp<FunctionI> createFunction(const t_stdstring &a_function);
221 
222  // access variables (input and output)
223 virtual unsigned int index(const t_stdstring &a_name);
224 virtual const Record &variable(unsigned int a_index)
225  { return m_variables->m_varray[a_index]; }
226 virtual const sp< Aggregate > &aggregate(unsigned int a_index)
227  { return m_aggregates[a_index]; }
228 virtual void assignVariable(unsigned int a_index,
229  Record a_record,
230  sp< Aggregate > a_aggregate)
231  {
232  m_variables->m_varray[a_index] = a_record;
233  m_aggregates[a_index] = a_aggregate;
234  }
235 virtual void makeVariable(Node *a_node);
236 virtual t_variables &variables(void) { return *m_variables; }
237 
238 virtual Record create(const t_stdstring &a_name,
239  sp< AccessorSet > a_spSA);
240 
241  void dump(void);
242 
243  Record createRecord(sp<Layout> a_layout);
244 
245  private:
246  void tokenize(t_stdlist<t_token> &a_tokens, const t_stdstring &a_buffer);
247 
248  t_stdmap< sp< AccessorSet >, sp< Layout > > m_layouts;
249  sp<t_variables> m_variables;
250  t_stdvector< sp< Aggregate > > m_aggregates;
251  t_stdvector< sp<Node> > m_nodes;
252  t_stdmap<t_stdstring, t_factory_location> m_factory_locations;
253  t_stdmap<t_stdstring, unsigned int> m_var_names;
254  unsigned int m_output_index;
255  sp< Scope > m_scope;
256  sp< RecordGroup > m_rg;
257  sp< AsVariable > m_asVariable;
258  sp< AsString > m_asString;
259  sp< Aggregate > m_stringAggregate;
260  sp< Aggregate > m_literalAggregate;
261  public:
262  t_stdmap<t_stdstring, t_jump_location > m_jump_locations;
263  sp< AsVariable > &asVariable(void) { return m_asVariable; }
264  sp< AsString > &asString(void) { return m_asString; }
265  sp< Scope > &scope(void) { return m_scope; }
266  sp< RecordGroup > &recordGroup(void) { return m_rg; }
267  sp< Aggregate > &literalAggregate(void) { return m_literalAggregate; }
268  sp< Aggregate > &stringAggregate(void) { return m_stringAggregate; }
269 };
270 
271 class FE_DL_EXPORT EvaluatorLog : public AnnotatedLog
272 {
273  public:
274  EvaluatorLog(void) {}
275 virtual ~EvaluatorLog(void) {}
276 
277  using AnnotatedLog::log;
278 
279 virtual void log(const std::string &a_message,
280  std::map<std::string,std::string> &a_attributes);
281 };
282 
283 
284 } /* namespace ext */
285 } /* namespace fe */
286 
287 #endif /* __evaluator_Evaluator_h__ */
288 
kernel
Definition: namespace.dox:3
void vsPrintf(std::string &target, const char *fmt, va_list ap, int &size)
Format text into a fe::String.
Definition: debug.cc:252