ug4
lua_parser_class.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef LUAParserClass_H
34 #define LUAParserClass_H
35 
36 #include <map>
37 #include <set>
38 #include <vector>
39 #include <sstream>
40 #include <string>
41 #include <math.h>
42 
43 #include "parser_node.h"
44 #include "parser.hpp"
45 #include <iostream>
48 
49 #include "vm.h"
50 
51 #define THE_PREFIX ug4_lua_YY_
52 #define yyerror ug4_lua_YY_error
53 
54 
55 void yyerror(const char *s);
56 namespace ug{
57 class LUAParserClass;
58 }
59 void yaccparse(const char*command, ug::LUAParserClass *p);
60 
61 namespace ug{
62 
63 
65 {
66  std::map<std::string, size_t> variables;
67  std::map<size_t, std::string> id2variable;
68  std::set<size_t> localVariables;
69  std::set<size_t> localFunctions;
70 
71  std::vector<nodeType *> nodes;
72  std::string name;
73  int numOut;
75 
76 public:
77  enum
78  {
80  };
81 
82  int iLineAdd;
83  std::string filename;
84 
86  {
89  };
91 public:
93  {
94  iLineAdd = 0;
95  filename = "";
96 
97  numOut = -1;
99  args = NULL;
100  }
101 
102  void set_name(const char *s)
103  {
104  name = s;
105  }
106 
107  void set_name(int id)
108  {
110  }
111 
112 
113 
115  {
116  args = p;
117  }
118 
119  std::stringstream err;
120 
121  void
123  {
124  nodes.push_back(p);
125  }
126 
127  void parse(const char *command)
128  {
129  clear();
130  yaccparse(command, this);
131  }
132 
133  void
135  {
136  variables.clear();
137  id2variable.clear();
138  localVariables.clear();
139  err.clear();
140  nodes.clear();
141  }
142 
143  const char *
144  get_name_for_id(size_t id)
145  {
146  return id2variable[id].c_str();
147  }
148 
149  int
150  get_id_for_name(const char*name);
151 
152  bool
153  set_local(size_t id)
154  {
155  localVariables.insert(id);
156  return true;
157  }
158 
159  bool
160  is_global(size_t id)
161  {
162  return !is_local(id) && !is_arg(id);
163  }
164 
165  bool
166  is_local(size_t id)
167  {
168  return localVariables.find(id) != localVariables.end();
169  }
170 
171  bool
172  is_arg(size_t id)
173  {
174  nodeType *a = args;
175  while(a->type == typeOpr)
176  {
177  if(a->opr.op[0]->id.i == (int)id) return true;
178  a = a->opr.op[1];
179  }
180  if(a->id.i == (int)id) return true;
181  return false;
182  }
183 
184 
185  void
186  assert_local(size_t id)
187  {
188  if (!is_local(id))
189  err << "Error: " << get_name_for_id(id) << " not local! Try adding \"local " << get_name_for_id(id) << "\"\n";
190  }
191 
192  bool
194  {
195  return err.str().length() != 0;
196  }
197 
198  int num_in()
199  {
200  nodeType *a = args;
201  int i=0;
202  while(a->type == typeOpr)
203  {
204  i++;
205  a = a->opr.op[1];
206  }
207  return i;
208  }
209 
210  int num_out()
211  {
212  return numOut;
213  }
214 
215  void print_variable_names();
216 
217  int add_subfunctions(std::map<std::string, SmartPtr<LUAParserClass> > &subfunctions);
218  int add_subfunction(std::string name, std::map<std::string, SmartPtr<LUAParserClass> > &subfunctions);
219 
220  int add_subfunctions(std::set<std::string> &knownFunctions, std::stringstream &declarations, std::stringstream &definitions);
221  static int addfunctionC(std::string name, std::set<std::string> &knownFunctions, std::stringstream &declarations, std::stringstream &definitions);
222 
223  void getVar(int i, std::ostream &out);
224  int parse_luaFunction(const char *name);
226  int parse_luaFunction_StackTop(const char *name);
227 
228  int declare(std::ostream &out);
229  int createC_inline(std::ostream &out);
230 
231  int createRT(nodeType *a, std::ostream &out, const char **rt, int nr, int indent);
232 
233 
234  int createVMSub(VMAdd &vm, std::map<std::string, SmartPtr<VMAdd> > &subVM);
235  int createVMHeader(VMAdd &vm);
236  int createVM(nodeType *p, VMAdd &vm, std::map<std::string, SmartPtr<VMAdd> > &subVM);
237 
238  int createVM(VMAdd &vm);
239 
240  int createC(nodeType *p, std::ostream &out, int indent);
241  int createJITSG(std::ostream &out, eReturnType r, std::set<std::string> &subfunctions);
242  int createLUA(nodeType *p, std::ostream &out);
243  void reduce();
244 
245  int createC(std::ostream &out);
246  int createLUA(std::ostream &out);
247 
248  nodeType *reduce(nodeType *p);
249 
251  void print_locals(std::ostream &out);
252  void print_globals(std::ostream &out);
253 
255  static nodeType *con(double value)
256  {
257  nodeType *p;
258 
259  /* allocate node */
260  if ((p = new nodeType) == NULL)
261  yyerror("out of memory");
262 
263  /* copy information */
264  p->type = typeCon;
265  p->con.value = value;
266 
267  return p;
268  }
269 
271  static nodeType *opr0(int i)
272  {
273  nodeType *p;
274  p = new nodeType;
275  p->type = typeOpr;
276  p->opr.oper = i;
277  p->opr.nops = 0;
278  return p;
279  }
280 
281  static nodeType *id(int i)
282  {
283  nodeType *p;
284 
285  /* allocate node */
286  if ((p = new nodeType) == NULL)
287  yyerror("out of memory");
288 
289  /* copy information */
290  p->type = typeId;
291  p->id.i = i;
292 
293  return p;
294  }
295 
296 
297  void setRet(int i)
298  {
299  if(numOut == -1)
300  numOut = i;
301  else if(numOut != i)
302  err << "different return values: was " << numOut << ", now " << i << "\n";
303  }
304 
305  nodeType *opr1(int oper, nodeType *op)
306  {
307  nodeType *p;
308  p = new nodeType;
309 
310  p->type = typeOpr;
311  p->opr.oper = oper;
312  p->opr.nops = 1;
313  p->opr.op = new nodeType*[1];
314  p->opr.op[0] = op;
315 
316  if(oper == 'R')
317  {
318  nodeType *aa = op;
319  int i=1;
320  while(aa->type == typeOpr && aa->opr.oper == ',')
321  {
322  i++;
323  aa = aa->opr.op[1];
324  }
325  setRet(i);
326  }
327  return p;
328  }
329 
330  static nodeType *opr2(int oper, nodeType *op1, nodeType *op2)
331  {
332  nodeType *p;
333  p = new nodeType;
334 
335  p->type = typeOpr;
336  p->opr.oper = oper;
337  p->opr.nops = 2;
338  p->opr.op = new nodeType*[2];
339  p->opr.op[0] = op1;
340  p->opr.op[1] = op2;
341  return p;
342  }
343 
344  static nodeType *opr3(int oper, nodeType *op1, nodeType *op2, nodeType *op3)
345  {
346  nodeType *p;
347  p = new nodeType;
348 
349  p->type = typeOpr;
350  p->opr.oper = oper;
351  p->opr.nops = 3;
352  p->opr.op = new nodeType*[3];
353  p->opr.op[0] = op1;
354  p->opr.op[1] = op2;
355  p->opr.op[2] = op3;
356  return p;
357  }
358 
359  static nodeType *forOp(nodeType *_var, nodeType *_start, nodeType *_stop, nodeType *_step, nodeType *_expr)
360  {
361  nodeType *p;
362  p = new nodeType;
363 
364  p->type = typeOpr;
365  p->opr.oper = LUAPARSER_FOR;
366  p->opr.nops = 5;
367  p->opr.op = new nodeType*[5];
368  p->opr.op[0] = _var;
369  p->opr.op[1] = _start;
370  p->opr.op[2] = _stop;
371  p->opr.op[3] = _step;
372  p->opr.op[4] = _expr;
373  return p;
374  }
375 
376  nodeType *function(nodeType *op1, nodeType *op2)
377  {
378 // std::cout << "local function " << id2variable[op1->id.i] << "\n";
379  localFunctions.insert(op1->id.i);
380  return opr2('C', op1, op2);
381  }
382 
383  static void freeNode(nodeType *p)
384  {
385  int i;
386 
387  if (!p) return;
388  if (p->type == typeOpr)
389  {
390  for (i = 0; i < p->opr.nops; i++)
391  delete p->opr.op[i];
392  delete[] p->opr.op;
393  }
394  delete p;
395  }
396 
397 };
398 
399 } // namespace ug
400 #endif /* LUAParserClass_H */
401 
parameterString p
parameterString s
Definition: smart_pointer.h:108
Definition: lua_parser_class.h:65
int declare(std::ostream &out)
Definition: lua_parser_class_create_c.cpp:415
int numOut
Definition: lua_parser_class.h:73
static nodeType * id(int i)
Definition: lua_parser_class.h:281
int num_out()
Definition: lua_parser_class.h:210
void print_variable_names()
Definition: lua_parser_class.cpp:77
int num_in()
Definition: lua_parser_class.h:198
static void freeNode(nodeType *p)
Definition: lua_parser_class.h:383
const char * get_name_for_id(size_t id)
Definition: lua_parser_class.h:144
int createC(nodeType *p, std::ostream &out, int indent)
Definition: lua_parser_class_create_c.cpp:41
void set_arguments(nodeType *p)
Definition: lua_parser_class.h:114
int iLineAdd
Definition: lua_parser_class.h:82
void add(nodeType *p)
Definition: lua_parser_class.h:122
int createLUA(nodeType *p, std::ostream &out)
Definition: lua_parser_class_create_lua.cpp:40
eReturnType
Definition: lua_parser_class.h:86
@ RT_SOURCE
Definition: lua_parser_class.h:88
@ RT_VELOCITY
Definition: lua_parser_class.h:87
@ RT_CALLBACK
Definition: lua_parser_class.h:87
@ RT_NEUMANN
Definition: lua_parser_class.h:88
@ RT_SUBFUNCTION
Definition: lua_parser_class.h:87
@ RT_DIFFUSION
Definition: lua_parser_class.h:87
@ RT_DIRICHLET
Definition: lua_parser_class.h:87
bool set_local(size_t id)
Definition: lua_parser_class.h:153
void parse(const char *command)
Definition: lua_parser_class.h:127
std::string name
Definition: lua_parser_class.h:72
int createC_inline(std::ostream &out)
Definition: lua_parser_class_create_c.cpp:353
std::set< size_t > localVariables
Definition: lua_parser_class.h:68
std::stringstream err
Definition: lua_parser_class.h:119
nodeType * args
Definition: lua_parser_class.h:74
bool is_global(size_t id)
Definition: lua_parser_class.h:160
int get_id_for_name(const char *name)
Definition: lua_parser_class.cpp:63
int createVMSub(VMAdd &vm, std::map< std::string, SmartPtr< VMAdd > > &subVM)
Definition: lua_parser_class_create_vm.cpp:274
static nodeType * opr2(int oper, nodeType *op1, nodeType *op2)
Definition: lua_parser_class.h:330
void print_locals(std::ostream &out)
Definition: lua_parser_class.cpp:265
std::set< size_t > localFunctions
Definition: lua_parser_class.h:69
int add_subfunction(std::string name, std::map< std::string, SmartPtr< LUAParserClass > > &subfunctions)
Definition: lua_parser_class_create_vm.cpp:289
std::string filename
Definition: lua_parser_class.h:83
void set_name(int id)
Definition: lua_parser_class.h:107
void reduce()
Definition: lua_parser_class.cpp:125
LUAParserClass()
Definition: lua_parser_class.h:92
void setRet(int i)
Definition: lua_parser_class.h:297
static nodeType * forOp(nodeType *_var, nodeType *_start, nodeType *_stop, nodeType *_step, nodeType *_expr)
Definition: lua_parser_class.h:359
bool has_errors()
Definition: lua_parser_class.h:193
void assert_local(size_t id)
Definition: lua_parser_class.h:186
std::map< size_t, std::string > id2variable
Definition: lua_parser_class.h:67
int createVM(nodeType *p, VMAdd &vm, std::map< std::string, SmartPtr< VMAdd > > &subVM)
Definition: lua_parser_class_create_vm.cpp:42
static int addfunctionC(std::string name, std::set< std::string > &knownFunctions, std::stringstream &declarations, std::stringstream &definitions)
Definition: lua_parser_class_create_c.cpp:372
nodeType * opr1(int oper, nodeType *op)
Definition: lua_parser_class.h:305
void print_globals(std::ostream &out)
Definition: lua_parser_class.cpp:273
int createJITSG(std::ostream &out, eReturnType r, std::set< std::string > &subfunctions)
Definition: lua_parser_class_create_jitsg.cpp:42
void clear()
Definition: lua_parser_class.h:134
int createVMHeader(VMAdd &vm)
Definition: lua_parser_class_create_vm.cpp:255
int add_subfunctions(std::map< std::string, SmartPtr< LUAParserClass > > &subfunctions)
Definition: lua_parser_class_create_vm.cpp:282
std::vector< nodeType * > nodes
Definition: lua_parser_class.h:71
bool is_local(size_t id)
Definition: lua_parser_class.h:166
int parse_luaFunction(const char *name)
Definition: lua_parser_class.cpp:169
int parse_luaFunction_StackTop(const char *name)
Definition: lua_parser_class.cpp:190
std::map< std::string, size_t > variables
Definition: lua_parser_class.h:66
static nodeType * opr0(int i)
Definition: lua_parser_class.h:271
int createRT(nodeType *a, std::ostream &out, const char **rt, int nr, int indent)
Definition: lua_parser_class.cpp:104
static nodeType * opr3(int oper, nodeType *op1, nodeType *op2, nodeType *op3)
Definition: lua_parser_class.h:344
bool is_arg(size_t id)
Definition: lua_parser_class.h:172
eReturnType returnType
Definition: lua_parser_class.h:90
static nodeType * con(double value)
Definition: lua_parser_class.h:255
void set_name(const char *s)
Definition: lua_parser_class.h:102
void getVar(int i, std::ostream &out)
Definition: lua_parser_class.cpp:85
int add_subfunctions(std::set< std::string > &knownFunctions, std::stringstream &declarations, std::stringstream &definitions)
@ LUAParserIgnore
Definition: lua_parser_class.h:79
@ LUAParserError
Definition: lua_parser_class.h:79
@ LUAParserOK
Definition: lua_parser_class.h:79
Handle for a lua reference.
Definition: lua_function_handle.h:40
--> documentation in vm.doxygen <–///
Definition: vm.h:49
void yaccparse(const char *command, ug::LUAParserClass *p)
Definition: parser.cpp:2102
#define yyerror
Definition: lua_parser_class.h:52
the ug namespace
#define LUAPARSER_FOR
Definition: parser.cpp:140
@ typeOpr
Definition: parser_node.h:37
@ typeId
Definition: parser_node.h:37
@ typeCon
Definition: parser_node.h:37
int i
Definition: parser_node.h:46
Definition: parser_node.h:59
nodeEnum type
Definition: parser_node.h:61
idNodeType id
Definition: parser_node.h:65
oprNodeType opr
Definition: parser_node.h:66
nodeType ** op
Definition: parser_node.h:55
int oper
Definition: parser_node.h:53