MicroModelicaCCompiler  4.5.3
statement.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #include "statement.hpp"
21 
22 #include <list>
23 #include <sstream>
24 
25 #include "../util/ast_util.hpp"
26 #include "element.hpp"
27 #include "expression.hpp"
28 
29 /* Statement Class */
30 
31 GET_AS_IMP(Statement, While);
32 GET_AS_IMP(Statement, If);
33 GET_AS_IMP(Statement, For);
34 GET_AS_IMP(Statement, When);
35 GET_AS_IMP(Statement, Assign);
36 GET_AS_IMP(Statement, OutputAssigment);
37 
38 CLASS_PRINTER_IMP(AST_Statement);
39 
41 {
42  visitor->visit(this);
43  visitor->leave(this);
44 }
45 
46 /* Return statement Class */
47 
49 
51 {
52  stringstream ret(stringstream::out);
53  MAKE_SPACE;
54  ret << "return" << endl;
55  return ret.str();
56 }
57 
58 /* Break statement Class */
59 
61 
63 {
64  stringstream ret(stringstream::out);
65  MAKE_SPACE;
66  ret << "break" << endl;
67  return ret.str();
68 }
69 
70 /* Assign Class */
71 
72 AST_Statement_Assign_::AST_Statement_Assign_(AST_Expression_ComponentReference lhs, AST_Expression exp) : _lhs(lhs), _exp(exp) {}
73 
74 AST_Expression AST_Statement_Assign_::exp() const { return _exp; }
75 
76 AST_Expression_ComponentReference AST_Statement_Assign_::lhs() const { return _lhs; }
77 
79 
81 {
82  stringstream ret(stringstream::out);
83  AST_ExpressionListIterator it;
84  MAKE_SPACE;
85  if (exp()->expressionType() != EXPCALLARG)
86  ret << lhs() << ":=" << exp() << ";" << endl;
87  else if (exp()->expressionType() == EXPCALLARG) {
88  ret << lhs() << "(";
89  int size = exp()->getAsCallArgs()->arguments()->size(), i = 0;
90  foreach (it, exp()->getAsCallArgs()->arguments()) {
91  ret << current_element(it) << (++i < size ? "," : "");
92  }
93  ret << ");" << endl;
94  }
95 
96  return ret.str();
97 }
98 
99 void AST_Statement_Assign_::setExp(AST_Expression exp)
100 {
101  // @TODO: review why this memleak tleads to a segfault.
102  /*if(_exp != nullptr) {
103  delete _exp;
104  }*/
105  _exp = exp;
106 }
107 
108 void AST_Statement_Assign_::setLHS(AST_Expression_ComponentReference lhs)
109 {
110  /*if(_lhs != nullptr) {
111  delete _lhs;
112  }*/
113  _lhs = lhs;
114 }
115 
116 /* For statement Class */
117 
118 AST_Statement_For_::AST_Statement_For_(AST_ForIndexList index, AST_StatementList sts) : _sts(sts), _ind(index) {}
119 
121 {
122  stringstream ret(stringstream::out);
123  AST_StatementListIterator it;
124  MAKE_SPACE;
125  AST_ListPrint(forIndexList(), ret, "for ", ",", "", " loop\n", false);
126  BEGIN_BLOCK;
127  foreach (it, statements())
128  ret << current_element(it);
129  END_BLOCK;
130  MAKE_SPACE;
131  ret << "end for;" << endl;
132  return ret.str();
133 }
134 
135 AST_StatementList AST_Statement_For_::statements() const { return _sts; }
136 
137 AST_ForIndexList AST_Statement_For_::forIndexList() const { return _ind; }
138 
140 
141 /* When Class */
142 
143 AST_Statement_When_::AST_Statement_When_(AST_Expression cond, AST_StatementList sts, AST_Statement_ElseList else_st, AST_Comment c)
144  : _cond(cond), _sts(sts), _else_list(else_st), _comm(c)
145 {
146 }
147 
149 
150 AST_Expression AST_Statement_When_::condition() const { return _cond; }
151 
152 AST_StatementList AST_Statement_When_::statements() const { return _sts; }
153 
154 AST_Statement_ElseList AST_Statement_When_::else_when() const { return _else_list; }
155 
156 AST_Comment AST_Statement_When_::comment() const { return _comm; }
157 
158 bool AST_Statement_When_::hasComment() { return _comm != nullptr; }
159 
160 bool AST_Statement_When_::hasElsewhen() { return _else_list->size() > 0; }
161 
163 {
164  stringstream ret(stringstream::out);
165  AST_StatementListIterator it;
166  AST_Statement_ElseListIterator else_it;
167 
168  MAKE_SPACE;
169  ret << "when " << condition() << " then" << endl;
170 
171  BEGIN_BLOCK;
172  foreach (it, statements())
173  ret << current_element(it);
174  END_BLOCK;
175  foreach (else_it, else_when()) {
176  MAKE_SPACE;
177  ret << "elsewhen " << current_element(else_it)->condition() << " then" << endl;
178  BEGIN_BLOCK;
179  foreach (it, current_element(else_it)->statements())
180  ret << current_element(it);
181  END_BLOCK;
182  }
183 
184  MAKE_SPACE;
185  ret << "end when;" << endl;
186  return ret.str();
187 }
188 
189 /* While Class */
190 
191 AST_Statement_While_::AST_Statement_While_(AST_Expression cond, AST_StatementList sts) : _cond(cond), _sts(sts) {}
192 
193 AST_Expression AST_Statement_While_::condition() const { return _cond; }
194 
196 
197 AST_StatementList AST_Statement_While_::statements() const { return _sts; }
198 
200 {
201  stringstream ret(stringstream::out);
202  AST_StatementListIterator it;
203  MAKE_SPACE;
204  ret << "while " << condition() << " loop" << endl;
205  BEGIN_BLOCK;
206  foreach (it, statements())
207  ret << current_element(it);
208  END_BLOCK;
209  MAKE_SPACE;
210  ret << "end while;" << endl;
211  return ret.str();
212 }
213 
214 /* If Class */
215 
216 AST_Statement_If_::AST_Statement_If_(AST_Expression cond, AST_StatementList true_st, AST_Statement_ElseList else_list,
217  AST_StatementList false_st)
218  : _cond(cond), _true_st(true_st), _false_st(false_st), _else_list(else_list)
219 {
220 }
221 
222 AST_Expression AST_Statement_If_::condition() const { return _cond; }
223 
224 AST_StatementList AST_Statement_If_::statements() const { return _true_st; }
225 
226 AST_StatementList AST_Statement_If_::else_statements() const { return _false_st; }
227 
228 AST_Statement_ElseList AST_Statement_If_::else_if() const { return _else_list; }
229 
231 
233 {
234  stringstream ret(stringstream::out);
235  AST_StatementListIterator it;
236  AST_Statement_ElseListIterator else_it;
237  MAKE_SPACE;
238  ret << "if " << condition() << " then" << endl;
239  BEGIN_BLOCK;
240  foreach (it, statements())
241  ret << current_element(it);
242  END_BLOCK;
243  foreach (else_it, else_if()) {
244  MAKE_SPACE;
245  ret << "elseif " << current_element(else_it)->condition() << " then" << endl;
246  BEGIN_BLOCK;
247  foreach (it, current_element(else_it)->statements())
248  ret << current_element(it);
249  END_BLOCK;
250  }
251 
252  if (else_statements()->size()) {
253  MAKE_SPACE;
254  ret << "else" << endl;
255  }
256  BEGIN_BLOCK;
257  foreach (it, else_statements())
258  ret << current_element(it);
259  END_BLOCK;
260  MAKE_SPACE;
261  ret << "end if;" << endl;
262  return ret.str();
263 }
264 
265 /* Output assigment Classes */
266 
267 AST_Statement_OutputAssigment_::AST_Statement_OutputAssigment_(AST_ExpressionList out_exps, AST_Expression_ComponentReference func,
268  AST_ExpressionList args)
269  : _out_exps(out_exps), _func(func), _args(args), _function_name(func->print())
270 {
271 }
272 
274 {
275  stringstream ret(stringstream::out);
276  AST_ExpressionListIterator it;
277  MAKE_SPACE;
278  ret << "(";
279  int size = out_expressions()->size(), i = 0;
280  foreach (it, out_expressions()) {
281  i++;
282  ret << current_element(it);
283  ret << (i < size ? "," : "");
284  }
285  ret << "):=";
286  ret << function();
287  ret << "(";
288  size = arguments()->size();
289  i = 0;
290  foreach (it, arguments()) {
291  i++;
292  ret << current_element(it);
293  ret << (i < size ? "," : "");
294  }
295  ret << ");" << endl;
296  return ret.str();
297 }
298 
299 AST_ExpressionList AST_Statement_OutputAssigment_::out_expressions() const { return _out_exps; }
300 
301 AST_Expression_ComponentReference AST_Statement_OutputAssigment_::function() const { return _func; }
302 
303 AST_ExpressionList AST_Statement_OutputAssigment_::arguments() const { return _args; }
304 
306 
308 
309 /* Else statement Class */
310 
311 AST_Statement_Else_::AST_Statement_Else_(AST_Expression cond, AST_StatementList sts) : _cond(cond), _sts(sts) {}
312 
313 AST_Expression AST_Statement_Else_::condition() const { return _cond; }
314 
315 AST_StatementList AST_Statement_Else_::statements() const { return _sts; }
316 
318 {
319  visitor->visit(this);
320  _cond->accept(visitor);
321  AST_StatementListIterator it;
322  foreach (it, _sts) {
323  current_element(it)->accept(visitor);
324  }
325 }
AST_Statement_Else_::accept
void accept(AST_Visitor *visitor)
Definition: statement.cpp:317
AST_Statement_If_::condition
AST_Expression condition() const
Definition: statement.cpp:222
AST_Statement_Assign_::print
string print() const
Definition: statement.cpp:80
AST_Statement_OutputAssigment_::AST_Statement_OutputAssigment_
AST_Statement_OutputAssigment_(AST_ExpressionList, AST_Expression_ComponentReference, AST_ExpressionList)
Definition: statement.cpp:267
AST_ListPrint
void AST_ListPrint(list< T1 > *l1, ostream &ret, string sec_name="", string separator=" ", string opener="", string closer="", bool block=false)
Definition: ast_types.hpp:319
END_BLOCK
#define END_BLOCK
Definition: ast_types.hpp:33
AST_Statement_::accept
void accept(AST_Visitor *visitor)
Definition: statement.cpp:40
AST_Statement_Assign_::setExp
void setExp(AST_Expression exp)
Definition: statement.cpp:99
STWHILE
@ STWHILE
Definition: ast_types.hpp:191
AST_Statement_OutputAssigment_::arguments
AST_ExpressionList arguments() const
Definition: statement.cpp:303
AST_Statement_OutputAssigment_::functionName
std::string functionName()
Definition: statement.cpp:307
AST_Statement_If_::statementType
virtual StatementType statementType()
Definition: statement.cpp:230
EXPCALLARG
@ EXPCALLARG
Definition: ast_types.hpp:171
AST_Statement_When_::_else_list
AST_Statement_ElseList _else_list
Definition: statement.hpp:72
AST_Statement_If_::statements
AST_StatementList statements() const
Definition: statement.cpp:224
AST_Statement_When_::comment
AST_Comment comment() const
Definition: statement.cpp:156
AST_Statement_If_::_true_st
AST_StatementList _true_st
Definition: statement.hpp:103
AST_Statement_If_::else_if
AST_Statement_ElseList else_if() const
Definition: statement.cpp:228
AST_Statement_For_::statementType
virtual StatementType statementType()
Definition: statement.cpp:139
AST_Statement_When_::hasElsewhen
bool hasElsewhen()
Definition: statement.cpp:160
element.hpp
AST_Statement_OutputAssigment_::_out_exps
AST_ExpressionList _out_exps
Definition: statement.hpp:144
AST_Statement_For_::statements
AST_StatementList statements() const
Definition: statement.cpp:135
AST_Statement_OutputAssigment_::function
AST_Expression_ComponentReference function() const
Definition: statement.cpp:301
AST_Statement_Else_::AST_Statement_Else_
AST_Statement_Else_(AST_Expression cond, AST_StatementList sts)
Definition: statement.cpp:311
AST_Statement_While_::_cond
AST_Expression _cond
Definition: statement.hpp:116
AST_Statement_While_::AST_Statement_While_
AST_Statement_While_(AST_Expression cond, AST_StatementList)
Definition: statement.cpp:191
AST_Statement_When_::_sts
AST_StatementList _sts
Definition: statement.hpp:71
AST_Statement_If_::_else_list
AST_Statement_ElseList _else_list
Definition: statement.hpp:104
AST_Statement_When_::AST_Statement_When_
AST_Statement_When_(AST_Expression cond, AST_StatementList, AST_Statement_ElseList, AST_Comment)
Definition: statement.cpp:143
AST_Statement_Assign_::statementType
virtual StatementType statementType()
Definition: statement.cpp:78
AST_Statement_While_::statements
AST_StatementList statements() const
Definition: statement.cpp:197
GET_AS_IMP
GET_AS_IMP(Statement, While)
AST_Statement_While_::_sts
AST_StatementList _sts
Definition: statement.hpp:117
STBREAK
@ STBREAK
Definition: ast_types.hpp:191
BEGIN_BLOCK
#define BEGIN_BLOCK
Definition: ast_types.hpp:32
AST_Statement_OutputAssigment_::_func
AST_Expression_ComponentReference _func
Definition: statement.hpp:145
CLASS_PRINTER_IMP
CLASS_PRINTER_IMP(AST_Statement)
MAKE_SPACE
#define MAKE_SPACE
Definition: ast_types.hpp:30
STFOR
@ STFOR
Definition: ast_types.hpp:191
AST_Statement_When_::condition
AST_Expression condition() const
Definition: statement.cpp:150
AST_Statement_OutputAssigment_::_function_name
std::string _function_name
Definition: statement.hpp:147
AST_Statement_OutputAssigment_::out_expressions
AST_ExpressionList out_expressions() const
Definition: statement.cpp:299
AST_Statement_Assign_::_lhs
AST_Expression_ComponentReference _lhs
Definition: statement.hpp:87
AST_Statement_Return_::statementType
virtual StatementType statementType()
Definition: statement.cpp:48
AST_Statement_Assign_::exp
AST_Expression exp() const
Definition: statement.cpp:74
AST_Statement_When_::_comm
AST_Comment _comm
Definition: statement.hpp:73
AST_Statement_Assign_::lhs
AST_Expression_ComponentReference lhs() const
Definition: statement.cpp:76
AST_Statement_OutputAssigment_::_args
AST_ExpressionList _args
Definition: statement.hpp:146
AST_Statement_When_::_cond
AST_Expression _cond
Definition: statement.hpp:70
AST_Statement_If_::_false_st
AST_StatementList _false_st
Definition: statement.hpp:103
AST_Statement_Assign_::setLHS
void setLHS(AST_Expression_ComponentReference comp_ref)
Definition: statement.cpp:108
STASSIGN
@ STASSIGN
Definition: ast_types.hpp:191
AST_Statement_If_::print
string print() const
Definition: statement.cpp:232
AST_Statement_Assign_::_exp
AST_Expression _exp
Definition: statement.hpp:88
STOUTASSING
@ STOUTASSING
Definition: ast_types.hpp:191
AST_Statement_While_::print
string print() const
Definition: statement.cpp:199
AST_Statement_Break_::statementType
virtual StatementType statementType()
Definition: statement.cpp:60
AST_Statement_For_::_ind
AST_ForIndexList _ind
Definition: statement.hpp:130
STIF
@ STIF
Definition: ast_types.hpp:191
AST_Statement_When_::hasComment
bool hasComment()
Definition: statement.cpp:158
MicroModelica::IR::RANGE::For
@ For
Definition: index.hpp:102
AST_Visitor::visit
virtual void visit(AST_Class x)=0
STRETURN
@ STRETURN
Definition: ast_types.hpp:191
AST_Statement_OutputAssigment_::print
string print() const
Definition: statement.cpp:273
StatementType
StatementType
Definition: ast_types.hpp:191
AST_Statement_OutputAssigment_::statementType
virtual StatementType statementType()
Definition: statement.cpp:305
AST_Statement_If_::_cond
AST_Expression _cond
Definition: statement.hpp:102
AST_Statement_While_::condition
AST_Expression condition() const
Definition: statement.cpp:193
AST_Statement_For_::print
string print() const
Definition: statement.cpp:120
AST_Statement_Else_::statements
AST_StatementList statements() const
Definition: statement.cpp:315
AST_Statement_When_::print
string print() const
Definition: statement.cpp:162
AST_Statement_Else_::condition
AST_Expression condition() const
Definition: statement.cpp:313
AST_Statement_If_::else_statements
AST_StatementList else_statements() const
Definition: statement.cpp:226
AST_Statement_Return_::print
string print() const
Definition: statement.cpp:50
STWHEN
@ STWHEN
Definition: ast_types.hpp:191
AST_Visitor
Definition: ast_util.hpp:224
AST_Statement_Assign_::AST_Statement_Assign_
AST_Statement_Assign_(AST_Expression_ComponentReference cr, AST_Expression exp)
Definition: statement.cpp:72
AST_Statement_For_::_sts
AST_StatementList _sts
Definition: statement.hpp:129
MicroModelica::IR::RANGE::If
@ If
Definition: index.hpp:102
AST_Statement_For_::forIndexList
AST_ForIndexList forIndexList() const
Definition: statement.cpp:137
AST_Statement_When_::else_when
AST_Statement_ElseList else_when() const
Definition: statement.cpp:154
AST_Visitor::leave
virtual void leave(AST_Class x)=0
AST_Statement_When_::statements
AST_StatementList statements() const
Definition: statement.cpp:152
AST_Statement_If_::AST_Statement_If_
AST_Statement_If_(AST_Expression cond, AST_StatementList true_st, AST_Statement_ElseList, AST_StatementList false_st)
Definition: statement.cpp:216
AST_Statement_For_::AST_Statement_For_
AST_Statement_For_(AST_ForIndexList index, AST_StatementList)
Definition: statement.cpp:118
AST_Statement_Else_::_cond
AST_Expression _cond
Definition: statement.hpp:158
AST_Statement_Break_::print
string print() const
Definition: statement.cpp:62
AST_Statement_Else_::_sts
AST_StatementList _sts
Definition: statement.hpp:159
current_element
#define current_element(it)
Definition: ast_types.hpp:34
AST_Statement_While_::statementType
virtual StatementType statementType()
Definition: statement.cpp:195
expression.hpp
AST_Statement_When_::statementType
virtual StatementType statementType()
Definition: statement.cpp:148
statement.hpp