1 |
schoenebeck |
2581 |
/* |
2 |
schoenebeck |
3551 |
* Copyright (c) 2014 - 2019 Christian Schoenebeck and Andreas Persson |
3 |
schoenebeck |
2581 |
* |
4 |
|
|
* http://www.linuxsampler.org |
5 |
|
|
* |
6 |
|
|
* This file is part of LinuxSampler and released under the same terms. |
7 |
|
|
* See README file for details. |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include <cstdio> |
11 |
|
|
#include <string.h> |
12 |
|
|
#include "tree.h" |
13 |
|
|
#include "../common/global_private.h" |
14 |
schoenebeck |
2588 |
#include <assert.h> |
15 |
schoenebeck |
2581 |
|
16 |
|
|
namespace LinuxSampler { |
17 |
|
|
|
18 |
|
|
bool isNoOperation(StatementRef statement) { |
19 |
schoenebeck |
3311 |
return statement->statementType() == STMT_NOOP; |
20 |
schoenebeck |
2581 |
} |
21 |
|
|
|
22 |
|
|
Node::Node() { |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
Node::~Node() { |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
void Node::printIndents(int n) { |
29 |
|
|
for (int i = 0; i < n; ++i) printf(" "); |
30 |
|
|
fflush(stdout); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
String IntExpr::evalCastToStr() { |
34 |
|
|
return ToString(evalInt()); |
35 |
|
|
} |
36 |
|
|
|
37 |
schoenebeck |
3573 |
String RealExpr::evalCastToStr() { |
38 |
|
|
return ToString(evalReal()); |
39 |
|
|
} |
40 |
|
|
|
41 |
schoenebeck |
3293 |
String IntArrayExpr::evalCastToStr() { |
42 |
schoenebeck |
3056 |
String s = "{"; |
43 |
schoenebeck |
3557 |
for (vmint i = 0; i < arraySize(); ++i) { |
44 |
|
|
vmint val = evalIntElement(i); |
45 |
schoenebeck |
3056 |
if (i) s += ","; |
46 |
|
|
s += ToString(val); |
47 |
|
|
} |
48 |
|
|
s += "}"; |
49 |
|
|
return s; |
50 |
schoenebeck |
3293 |
} |
51 |
schoenebeck |
3056 |
|
52 |
schoenebeck |
3573 |
String RealArrayExpr::evalCastToStr() { |
53 |
|
|
String s = "{"; |
54 |
|
|
for (vmint i = 0; i < arraySize(); ++i) { |
55 |
|
|
vmfloat val = evalRealElement(i); |
56 |
|
|
if (i) s += ","; |
57 |
|
|
s += ToString(val); |
58 |
|
|
} |
59 |
|
|
s += "}"; |
60 |
|
|
return s; |
61 |
|
|
} |
62 |
|
|
|
63 |
schoenebeck |
3561 |
MetricPrefix_t Unit::unitPrefix(vmuint i) const { |
64 |
|
|
if (i >= prefix.size()) return VM_NO_PREFIX; |
65 |
|
|
return prefix[i]; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
void Unit::setUnit(const std::vector<MetricPrefix_t>& prefix, StdUnit_t type) { |
69 |
|
|
this->prefix.resize( prefix.size() ); |
70 |
|
|
for (vmuint i = 0; i < prefix.size(); ++i) |
71 |
|
|
this->prefix[i] = prefix[i]; |
72 |
|
|
|
73 |
|
|
unit = type; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
void Unit::setUnit(const MetricPrefix_t* prefixes, StdUnit_t type) { |
77 |
|
|
unit = type; |
78 |
|
|
prefix.clear(); |
79 |
|
|
for (int i = 0; i < 2 && prefixes[i]; ++i) |
80 |
|
|
prefix.add(prefixes[i]); |
81 |
|
|
} |
82 |
|
|
|
83 |
schoenebeck |
3573 |
void Unit::copyUnitFrom(const UnitRef& src) { |
84 |
schoenebeck |
3561 |
unit = src->unitType(); |
85 |
|
|
prefix.clear(); |
86 |
|
|
for (int i = 0; true; ++i) { |
87 |
|
|
MetricPrefix_t p = src->unitPrefix(i); |
88 |
|
|
if (!p) return; |
89 |
|
|
prefix.add(p); |
90 |
|
|
} |
91 |
|
|
} |
92 |
|
|
|
93 |
schoenebeck |
3557 |
vmint IntLiteral::evalInt() { |
94 |
schoenebeck |
2581 |
return value; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
void IntLiteral::dump(int level) { |
98 |
|
|
printIndents(level); |
99 |
schoenebeck |
3557 |
printf("IntLiteral %lld\n", value); |
100 |
schoenebeck |
2581 |
} |
101 |
|
|
|
102 |
schoenebeck |
3573 |
vmfloat RealLiteral::evalReal() { |
103 |
|
|
return value; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
void RealLiteral::dump(int level) { |
107 |
|
|
printIndents(level); |
108 |
|
|
printf("RealLiteral %f\n", value); |
109 |
|
|
} |
110 |
|
|
|
111 |
schoenebeck |
2581 |
void StringLiteral::dump(int level) { |
112 |
|
|
printIndents(level); |
113 |
|
|
printf("StringLiteral: '%s'\n", value.c_str()); |
114 |
|
|
} |
115 |
|
|
|
116 |
schoenebeck |
3557 |
vmint Add::evalInt() { |
117 |
schoenebeck |
2581 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
118 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
119 |
|
|
return (pLHS && pRHS) ? pLHS->evalInt() + pRHS->evalInt() : 0; |
120 |
|
|
} |
121 |
|
|
|
122 |
schoenebeck |
3573 |
vmfloat Add::evalReal() { |
123 |
|
|
RealExpr* pLHS = dynamic_cast<RealExpr*>(&*lhs); |
124 |
|
|
RealExpr* pRHS = dynamic_cast<RealExpr*>(&*rhs);; |
125 |
|
|
return (pLHS && pRHS) ? pLHS->evalReal() + pRHS->evalReal() : 0; |
126 |
|
|
} |
127 |
|
|
|
128 |
schoenebeck |
2581 |
void Add::dump(int level) { |
129 |
|
|
printIndents(level); |
130 |
|
|
printf("Add(\n"); |
131 |
|
|
lhs->dump(level+1); |
132 |
|
|
printIndents(level); |
133 |
|
|
printf(",\n"); |
134 |
|
|
rhs->dump(level+1); |
135 |
|
|
printIndents(level); |
136 |
|
|
printf(")\n"); |
137 |
|
|
} |
138 |
|
|
|
139 |
schoenebeck |
3557 |
vmint Sub::evalInt() { |
140 |
schoenebeck |
2581 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
141 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
142 |
|
|
return (pLHS && pRHS) ? pLHS->evalInt() - pRHS->evalInt() : 0; |
143 |
|
|
} |
144 |
|
|
|
145 |
schoenebeck |
3573 |
vmfloat Sub::evalReal() { |
146 |
|
|
RealExpr* pLHS = dynamic_cast<RealExpr*>(&*lhs); |
147 |
|
|
RealExpr* pRHS = dynamic_cast<RealExpr*>(&*rhs);; |
148 |
|
|
return (pLHS && pRHS) ? pLHS->evalReal() - pRHS->evalReal() : 0; |
149 |
|
|
} |
150 |
|
|
|
151 |
schoenebeck |
2581 |
void Sub::dump(int level) { |
152 |
|
|
printIndents(level); |
153 |
|
|
printf("Sub(\n"); |
154 |
|
|
lhs->dump(level+1); |
155 |
|
|
printIndents(level); |
156 |
|
|
printf(",\n"); |
157 |
|
|
rhs->dump(level+1); |
158 |
|
|
printIndents(level); |
159 |
|
|
printf(")\n"); |
160 |
|
|
} |
161 |
|
|
|
162 |
schoenebeck |
3557 |
vmint Mul::evalInt() { |
163 |
schoenebeck |
2581 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
164 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
165 |
|
|
return (pLHS && pRHS) ? pLHS->evalInt() * pRHS->evalInt() : 0; |
166 |
|
|
} |
167 |
|
|
|
168 |
schoenebeck |
3573 |
vmfloat Mul::evalReal() { |
169 |
|
|
RealExpr* pLHS = dynamic_cast<RealExpr*>(&*lhs); |
170 |
|
|
RealExpr* pRHS = dynamic_cast<RealExpr*>(&*rhs);; |
171 |
|
|
return (pLHS && pRHS) ? pLHS->evalReal() * pRHS->evalReal() : 0; |
172 |
|
|
} |
173 |
|
|
|
174 |
schoenebeck |
2581 |
void Mul::dump(int level) { |
175 |
|
|
printIndents(level); |
176 |
|
|
printf("Mul(\n"); |
177 |
|
|
lhs->dump(level+1); |
178 |
|
|
printIndents(level); |
179 |
|
|
printf(",\n"); |
180 |
|
|
rhs->dump(level+1); |
181 |
|
|
printIndents(level); |
182 |
|
|
printf(")\n"); |
183 |
|
|
} |
184 |
|
|
|
185 |
schoenebeck |
3561 |
MetricPrefix_t Mul::unitPrefix(vmuint i) const { |
186 |
schoenebeck |
3573 |
const ScalarNumberExpr* pLHS = dynamic_cast<const ScalarNumberExpr*>(&*lhs); |
187 |
|
|
const ScalarNumberExpr* pRHS = dynamic_cast<const ScalarNumberExpr*>(&*rhs); |
188 |
schoenebeck |
3561 |
// currently the NKSP parser only allows a unit prefix on either side |
189 |
|
|
return (pLHS->unitPrefix(0)) ? pLHS->unitPrefix(i) : pRHS->unitPrefix(i); |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
StdUnit_t Mul::unitType() const { |
193 |
schoenebeck |
3573 |
const ScalarNumberExpr* pLHS = dynamic_cast<const ScalarNumberExpr*>(&*lhs); |
194 |
|
|
const ScalarNumberExpr* pRHS = dynamic_cast<const ScalarNumberExpr*>(&*rhs); |
195 |
schoenebeck |
3561 |
// currently the NKSP parser only allows a unit type on either side |
196 |
|
|
return (pLHS->unitType()) ? pLHS->unitType() : pRHS->unitType(); |
197 |
|
|
} |
198 |
|
|
|
199 |
schoenebeck |
3557 |
vmint Div::evalInt() { |
200 |
schoenebeck |
2581 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
201 |
schoenebeck |
2945 |
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs); |
202 |
|
|
if (!pLHS || !pRHS) return 0; |
203 |
schoenebeck |
3557 |
vmint l = pLHS->evalInt(); |
204 |
|
|
vmint r = pRHS->evalInt(); |
205 |
schoenebeck |
2945 |
if (r == 0) return 0; |
206 |
|
|
return l / r; |
207 |
schoenebeck |
2581 |
} |
208 |
|
|
|
209 |
schoenebeck |
3573 |
vmfloat Div::evalReal() { |
210 |
|
|
RealExpr* pLHS = dynamic_cast<RealExpr*>(&*lhs); |
211 |
|
|
RealExpr* pRHS = dynamic_cast<RealExpr*>(&*rhs);; |
212 |
|
|
if (!pLHS || !pRHS) return 0; |
213 |
|
|
vmfloat l = pLHS->evalReal(); |
214 |
|
|
vmfloat r = pRHS->evalReal(); |
215 |
|
|
if (r == vmfloat(0)) return 0; |
216 |
|
|
return l / r; |
217 |
|
|
} |
218 |
|
|
|
219 |
schoenebeck |
2581 |
void Div::dump(int level) { |
220 |
|
|
printIndents(level); |
221 |
|
|
printf("Div(\n"); |
222 |
|
|
lhs->dump(level+1); |
223 |
|
|
printIndents(level); |
224 |
|
|
printf(",\n"); |
225 |
|
|
rhs->dump(level+1); |
226 |
|
|
printIndents(level); |
227 |
|
|
printf(")\n"); |
228 |
|
|
} |
229 |
|
|
|
230 |
schoenebeck |
3561 |
MetricPrefix_t Div::unitPrefix(vmuint i) const { |
231 |
schoenebeck |
3573 |
const ScalarNumberExpr* pLHS = dynamic_cast<const ScalarNumberExpr*>(&*lhs); |
232 |
|
|
const ScalarNumberExpr* pRHS = dynamic_cast<const ScalarNumberExpr*>(&*rhs); |
233 |
schoenebeck |
3561 |
// currently the NKSP parser only allows either A) a unit prefix on left |
234 |
|
|
// side and none on right side or B) an identical unit prefix on both sides |
235 |
|
|
return (pLHS->unitPrefix(0) && pRHS->unitPrefix(0)) ? VM_NO_PREFIX : pLHS->unitPrefix(i); |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
StdUnit_t Div::unitType() const { |
239 |
schoenebeck |
3573 |
const ScalarNumberExpr* pLHS = dynamic_cast<const ScalarNumberExpr*>(&*lhs); |
240 |
|
|
const ScalarNumberExpr* pRHS = dynamic_cast<const ScalarNumberExpr*>(&*rhs); |
241 |
schoenebeck |
3561 |
// the NKSP parser only allows either A) a unit type on left side and none |
242 |
|
|
// on right side or B) an identical unit type on both sides |
243 |
|
|
return (pLHS->unitType() && pRHS->unitType()) ? VM_NO_UNIT : pLHS->unitType(); |
244 |
|
|
} |
245 |
|
|
|
246 |
schoenebeck |
3557 |
vmint Mod::evalInt() { |
247 |
schoenebeck |
2581 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
248 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
249 |
|
|
return (pLHS && pRHS) ? pLHS->evalInt() % pRHS->evalInt() : 0; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
void Mod::dump(int level) { |
253 |
|
|
printIndents(level); |
254 |
|
|
printf("Mod(\n"); |
255 |
|
|
lhs->dump(level+1); |
256 |
|
|
printIndents(level); |
257 |
|
|
printf(",\n"); |
258 |
|
|
rhs->dump(level+1); |
259 |
|
|
printIndents(level); |
260 |
|
|
printf(")\n"); |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
void Args::dump(int level) { |
264 |
|
|
printIndents(level); |
265 |
|
|
printf("Args(\n"); |
266 |
|
|
for (std::vector<ExpressionRef>::iterator it = args.begin() ; it != args.end() ; ++it) { |
267 |
|
|
(*it)->dump(level+1); |
268 |
|
|
} |
269 |
|
|
printIndents(level); |
270 |
|
|
printf(")\n"); |
271 |
|
|
} |
272 |
|
|
|
273 |
schoenebeck |
2645 |
bool Args::isPolyphonic() const { |
274 |
schoenebeck |
3557 |
for (vmint i = 0; i < args.size(); ++i) |
275 |
schoenebeck |
2645 |
if (args[i]->isPolyphonic()) |
276 |
|
|
return true; |
277 |
|
|
return false; |
278 |
|
|
} |
279 |
|
|
|
280 |
schoenebeck |
2581 |
EventHandlers::EventHandlers() { |
281 |
|
|
//printf("EventHandlers::Constructor 0x%lx\n", (long long)this); |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
EventHandlers::~EventHandlers() { |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
void EventHandlers::add(EventHandlerRef arg) { |
288 |
|
|
args.push_back(arg); |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
void EventHandlers::dump(int level) { |
292 |
|
|
printIndents(level); |
293 |
|
|
printf("EventHandlers {\n"); |
294 |
|
|
for (std::vector<EventHandlerRef>::iterator it = args.begin() ; it != args.end() ; ++it) { |
295 |
|
|
(*it)->dump(level+1); |
296 |
|
|
} |
297 |
|
|
printIndents(level); |
298 |
|
|
printf("}\n"); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
EventHandler* EventHandlers::eventHandlerByName(const String& name) const { |
302 |
schoenebeck |
3557 |
for (vmint i = 0; i < args.size(); ++i) |
303 |
schoenebeck |
2581 |
if (args.at(i)->eventHandlerName() == name) |
304 |
|
|
return const_cast<EventHandler*>(&*args.at(i)); |
305 |
|
|
return NULL; |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
EventHandler* EventHandlers::eventHandler(uint index) const { |
309 |
|
|
if (index >= args.size()) return NULL; |
310 |
|
|
return const_cast<EventHandler*>(&*args.at(index)); |
311 |
|
|
} |
312 |
|
|
|
313 |
schoenebeck |
2645 |
bool EventHandlers::isPolyphonic() const { |
314 |
schoenebeck |
3557 |
for (vmint i = 0; i < args.size(); ++i) |
315 |
schoenebeck |
2645 |
if (args[i]->isPolyphonic()) |
316 |
|
|
return true; |
317 |
|
|
return false; |
318 |
|
|
} |
319 |
|
|
|
320 |
schoenebeck |
2581 |
Assignment::Assignment(VariableRef variable, ExpressionRef value) |
321 |
|
|
: variable(variable), value(value) |
322 |
|
|
{ |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
void Assignment::dump(int level) { |
326 |
|
|
printIndents(level); |
327 |
|
|
printf("Assignment\n"); |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
StmtFlags_t Assignment::exec() { |
331 |
|
|
if (!variable) |
332 |
|
|
return StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
333 |
|
|
variable->assign(&*value); |
334 |
|
|
return STMT_SUCCESS; |
335 |
|
|
} |
336 |
|
|
|
337 |
schoenebeck |
2645 |
EventHandler::EventHandler(StatementsRef statements) { |
338 |
|
|
this->statements = statements; |
339 |
|
|
usingPolyphonics = statements->isPolyphonic(); |
340 |
|
|
} |
341 |
|
|
|
342 |
schoenebeck |
2581 |
void EventHandler::dump(int level) { |
343 |
|
|
printIndents(level); |
344 |
|
|
printf("EventHandler {\n"); |
345 |
|
|
statements->dump(level+1); |
346 |
|
|
printIndents(level); |
347 |
|
|
printf("}\n"); |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
void Statements::dump(int level) { |
351 |
|
|
printIndents(level); |
352 |
|
|
printf("Statements {\n"); |
353 |
|
|
for (std::vector<StatementRef>::iterator it = args.begin() ; it != args.end() ; ++it) { |
354 |
|
|
(*it)->dump(level+1); |
355 |
|
|
} |
356 |
|
|
printIndents(level); |
357 |
|
|
printf("}\n"); |
358 |
|
|
} |
359 |
|
|
|
360 |
|
|
Statement* Statements::statement(uint i) { |
361 |
|
|
if (i >= args.size()) return NULL; |
362 |
|
|
return &*args.at(i); |
363 |
|
|
} |
364 |
|
|
|
365 |
schoenebeck |
2645 |
bool Statements::isPolyphonic() const { |
366 |
schoenebeck |
3557 |
for (vmint i = 0; i < args.size(); ++i) |
367 |
schoenebeck |
2645 |
if (args[i]->isPolyphonic()) |
368 |
|
|
return true; |
369 |
|
|
return false; |
370 |
|
|
} |
371 |
|
|
|
372 |
schoenebeck |
2942 |
DynamicVariableCall::DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v) |
373 |
|
|
: Variable(ctx, 0, false), dynVar(v), varName(name) |
374 |
|
|
{ |
375 |
|
|
} |
376 |
|
|
|
377 |
schoenebeck |
3557 |
vmint DynamicVariableCall::evalInt() { |
378 |
schoenebeck |
2942 |
VMIntExpr* expr = dynamic_cast<VMIntExpr*>(dynVar); |
379 |
|
|
if (!expr) return 0; |
380 |
|
|
return expr->evalInt(); |
381 |
|
|
} |
382 |
|
|
|
383 |
|
|
String DynamicVariableCall::evalStr() { |
384 |
|
|
VMStringExpr* expr = dynamic_cast<VMStringExpr*>(dynVar); |
385 |
|
|
if (!expr) return ""; |
386 |
|
|
return expr->evalStr(); |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
String DynamicVariableCall::evalCastToStr() { |
390 |
|
|
if (dynVar->exprType() == STRING_EXPR) { |
391 |
|
|
return evalStr(); |
392 |
|
|
} else { |
393 |
|
|
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(dynVar); |
394 |
|
|
return intExpr ? ToString(intExpr->evalInt()) : ""; |
395 |
|
|
} |
396 |
|
|
} |
397 |
|
|
|
398 |
|
|
void DynamicVariableCall::dump(int level) { |
399 |
|
|
printIndents(level); |
400 |
|
|
printf("Dynamic Variable '%s'\n", varName.c_str()); |
401 |
|
|
} |
402 |
|
|
|
403 |
schoenebeck |
2581 |
void FunctionCall::dump(int level) { |
404 |
|
|
printIndents(level); |
405 |
|
|
printf("FunctionCall '%s' args={\n", functionName.c_str()); |
406 |
|
|
args->dump(level+1); |
407 |
|
|
printIndents(level); |
408 |
|
|
printf("}\n"); |
409 |
|
|
} |
410 |
|
|
|
411 |
|
|
ExprType_t FunctionCall::exprType() const { |
412 |
|
|
if (!fn) return EMPTY_EXPR; |
413 |
|
|
return fn->returnType(); |
414 |
|
|
} |
415 |
|
|
|
416 |
|
|
VMFnResult* FunctionCall::execVMFn() { |
417 |
|
|
if (!fn) return NULL; |
418 |
|
|
// assuming here that all argument checks (amount and types) have been made |
419 |
|
|
// at parse time, to avoid time intensive checks on each function call |
420 |
|
|
return fn->exec(dynamic_cast<VMFnArgs*>(&*args)); |
421 |
|
|
} |
422 |
|
|
|
423 |
|
|
StmtFlags_t FunctionCall::exec() { |
424 |
|
|
VMFnResult* result = execVMFn(); |
425 |
|
|
if (!result) |
426 |
|
|
return StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
427 |
|
|
return result->resultFlags(); |
428 |
|
|
} |
429 |
|
|
|
430 |
schoenebeck |
3557 |
vmint FunctionCall::evalInt() { |
431 |
schoenebeck |
2581 |
VMFnResult* result = execVMFn(); |
432 |
|
|
if (!result) return 0; |
433 |
|
|
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(result->resultValue()); |
434 |
|
|
if (!intExpr) return 0; |
435 |
|
|
return intExpr->evalInt(); |
436 |
|
|
} |
437 |
|
|
|
438 |
schoenebeck |
3573 |
vmfloat FunctionCall::evalReal() { |
439 |
|
|
VMFnResult* result = execVMFn(); |
440 |
|
|
if (!result) return 0; |
441 |
|
|
VMRealExpr* realExpr = dynamic_cast<VMRealExpr*>(result->resultValue()); |
442 |
|
|
if (!realExpr) return 0; |
443 |
|
|
return realExpr->evalReal(); |
444 |
|
|
} |
445 |
|
|
|
446 |
schoenebeck |
3056 |
VMIntArrayExpr* FunctionCall::asIntArray() const { |
447 |
|
|
VMFnResult* result = const_cast<FunctionCall*>(this)->execVMFn(); |
448 |
|
|
if (!result) return 0; |
449 |
|
|
VMIntArrayExpr* intArrExpr = dynamic_cast<VMIntArrayExpr*>(result->resultValue()); |
450 |
|
|
return intArrExpr; |
451 |
|
|
} |
452 |
|
|
|
453 |
schoenebeck |
3573 |
VMRealArrayExpr* FunctionCall::asRealArray() const { |
454 |
|
|
VMFnResult* result = const_cast<FunctionCall*>(this)->execVMFn(); |
455 |
|
|
if (!result) return 0; |
456 |
|
|
VMRealArrayExpr* realArrExpr = dynamic_cast<VMRealArrayExpr*>(result->resultValue()); |
457 |
|
|
return realArrExpr; |
458 |
|
|
} |
459 |
|
|
|
460 |
schoenebeck |
2581 |
String FunctionCall::evalStr() { |
461 |
|
|
VMFnResult* result = execVMFn(); |
462 |
|
|
if (!result) return ""; |
463 |
|
|
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(result->resultValue()); |
464 |
|
|
if (!strExpr) return ""; |
465 |
|
|
return strExpr->evalStr(); |
466 |
|
|
} |
467 |
|
|
|
468 |
|
|
String FunctionCall::evalCastToStr() { |
469 |
|
|
VMFnResult* result = execVMFn(); |
470 |
|
|
if (!result) return ""; |
471 |
schoenebeck |
3573 |
const ExprType_t resultType = result->resultValue()->exprType(); |
472 |
|
|
if (resultType == STRING_EXPR) { |
473 |
schoenebeck |
2581 |
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(result->resultValue()); |
474 |
|
|
return strExpr ? strExpr->evalStr() : ""; |
475 |
schoenebeck |
3573 |
} else if (resultType == REAL_EXPR) { |
476 |
|
|
VMRealExpr* realExpr = dynamic_cast<VMRealExpr*>(result->resultValue()); |
477 |
|
|
return realExpr ? ToString(realExpr->evalReal()) : ""; |
478 |
schoenebeck |
2581 |
} else { |
479 |
|
|
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(result->resultValue()); |
480 |
|
|
return intExpr ? ToString(intExpr->evalInt()) : ""; |
481 |
|
|
} |
482 |
|
|
} |
483 |
|
|
|
484 |
schoenebeck |
3573 |
ScalarNumberVariable::ScalarNumberVariable(ParserContext* ctx, vmint _memPos, |
485 |
|
|
bool _bConst, bool _bPolyphonic, |
486 |
|
|
bool _bFinal) |
487 |
|
|
: Variable(ctx, _memPos, _bConst), |
488 |
schoenebeck |
3561 |
Unit(), |
489 |
schoenebeck |
3573 |
polyphonic(_bPolyphonic), finalVal(_bFinal) |
490 |
schoenebeck |
2581 |
{ |
491 |
schoenebeck |
3573 |
} |
492 |
|
|
|
493 |
|
|
IntVariable::IntVariable(ParserContext* ctx) |
494 |
|
|
: ScalarNumberVariable(ctx, ctx ? ctx->globalIntVarCount++ : 0) |
495 |
|
|
{ |
496 |
schoenebeck |
2588 |
//printf("globalIntVar parserctx=0x%lx memPOS=%d\n", ctx, memPos); |
497 |
|
|
assert(ctx); |
498 |
schoenebeck |
2581 |
} |
499 |
|
|
|
500 |
schoenebeck |
3557 |
inline static vmint postfixInc(vmint& object, vmint incBy) { |
501 |
|
|
const vmint i = object; |
502 |
schoenebeck |
2588 |
object += incBy; |
503 |
|
|
return i; |
504 |
|
|
} |
505 |
|
|
|
506 |
schoenebeck |
3573 |
IntVariable::IntVariable(ParserContext* ctx, bool bPolyphonic, bool bConst, vmint size) |
507 |
|
|
: ScalarNumberVariable( |
508 |
|
|
ctx, |
509 |
|
|
!ctx ? 0 : bPolyphonic ? postfixInc(ctx->polyphonicIntVarCount, size) : |
510 |
|
|
postfixInc(ctx->globalIntVarCount, size), |
511 |
|
|
bConst, bPolyphonic |
512 |
|
|
) |
513 |
schoenebeck |
2581 |
{ |
514 |
schoenebeck |
3573 |
//printf("IntVar size=%d parserCtx=0x%lx\n", size, (uint64_t)ctx); |
515 |
|
|
if (bPolyphonic) { |
516 |
schoenebeck |
2588 |
//printf("polyIntVar memPOS=%d\n", memPos); |
517 |
|
|
assert(ctx); |
518 |
|
|
} |
519 |
schoenebeck |
2581 |
} |
520 |
|
|
|
521 |
|
|
void IntVariable::assign(Expression* expr) { |
522 |
|
|
IntExpr* intExpr = dynamic_cast<IntExpr*>(expr); |
523 |
schoenebeck |
3034 |
if (intExpr) { |
524 |
schoenebeck |
3573 |
if (isPolyphonic()) |
525 |
schoenebeck |
2581 |
context->execContext->polyphonicIntMemory[memPos] = intExpr->evalInt(); |
526 |
|
|
else |
527 |
|
|
(*context->globalIntMemory)[memPos] = intExpr->evalInt(); |
528 |
schoenebeck |
3034 |
} |
529 |
schoenebeck |
2581 |
} |
530 |
|
|
|
531 |
schoenebeck |
3557 |
vmint IntVariable::evalInt() { |
532 |
schoenebeck |
2581 |
//printf("IntVariable::eval pos=%d\n", memPos); |
533 |
schoenebeck |
3573 |
if (isPolyphonic()) { |
534 |
schoenebeck |
2588 |
//printf("evalInt() poly memPos=%d execCtx=0x%lx\n", memPos, (uint64_t)context->execContext); |
535 |
schoenebeck |
2581 |
return context->execContext->polyphonicIntMemory[memPos]; |
536 |
schoenebeck |
2588 |
} |
537 |
schoenebeck |
2581 |
return (*context->globalIntMemory)[memPos]; |
538 |
|
|
} |
539 |
|
|
|
540 |
|
|
void IntVariable::dump(int level) { |
541 |
|
|
printIndents(level); |
542 |
schoenebeck |
2945 |
printf("IntVariable\n"); |
543 |
schoenebeck |
2588 |
//printf("IntVariable memPos=%d\n", memPos); |
544 |
schoenebeck |
2581 |
} |
545 |
|
|
|
546 |
schoenebeck |
3573 |
RealVariable::RealVariable(ParserContext* ctx) |
547 |
|
|
: ScalarNumberVariable(ctx, ctx ? ctx->globalRealVarCount++ : 0) |
548 |
|
|
{ |
549 |
|
|
//printf("globalRealVar parserctx=0x%lx memPOS=%d\n", ctx, memPos); |
550 |
|
|
assert(ctx); |
551 |
|
|
} |
552 |
|
|
|
553 |
|
|
RealVariable::RealVariable(ParserContext* ctx, bool bPolyphonic, bool bConst, vmint size) |
554 |
|
|
: ScalarNumberVariable( |
555 |
|
|
ctx, |
556 |
|
|
!ctx ? 0 : bPolyphonic ? postfixInc(ctx->polyphonicRealVarCount, size) : |
557 |
|
|
postfixInc(ctx->globalRealVarCount, size), |
558 |
|
|
bConst, bPolyphonic |
559 |
|
|
) |
560 |
|
|
{ |
561 |
|
|
//printf("RealVar size=%d parserCtx=0x%lx\n", size, (uint64_t)ctx); |
562 |
|
|
if (bPolyphonic) { |
563 |
|
|
//printf("polyRealVar memPOS=%d\n", memPos); |
564 |
|
|
assert(ctx); |
565 |
|
|
} |
566 |
|
|
} |
567 |
|
|
|
568 |
|
|
void RealVariable::assign(Expression* expr) { |
569 |
|
|
RealExpr* realExpr = dynamic_cast<RealExpr*>(expr); |
570 |
|
|
if (realExpr) { |
571 |
|
|
if (isPolyphonic()) |
572 |
|
|
context->execContext->polyphonicRealMemory[memPos] = realExpr->evalReal(); |
573 |
|
|
else |
574 |
|
|
(*context->globalRealMemory)[memPos] = realExpr->evalReal(); |
575 |
|
|
} |
576 |
|
|
} |
577 |
|
|
|
578 |
|
|
vmfloat RealVariable::evalReal() { |
579 |
|
|
//printf("RealVariable::eval pos=%d\n", memPos); |
580 |
|
|
if (isPolyphonic()) { |
581 |
|
|
//printf("evalReal() poly memPos=%d execCtx=0x%lx\n", memPos, (uint64_t)context->execContext); |
582 |
|
|
return context->execContext->polyphonicRealMemory[memPos]; |
583 |
|
|
} |
584 |
|
|
return (*context->globalRealMemory)[memPos]; |
585 |
|
|
} |
586 |
|
|
|
587 |
|
|
void RealVariable::dump(int level) { |
588 |
|
|
printIndents(level); |
589 |
|
|
printf("RealVariable\n"); |
590 |
|
|
//printf("RealVariable memPos=%d\n", memPos); |
591 |
|
|
} |
592 |
|
|
|
593 |
schoenebeck |
3557 |
ConstIntVariable::ConstIntVariable(vmint value) |
594 |
schoenebeck |
2581 |
: IntVariable(NULL,false,true), value(value) |
595 |
|
|
{ |
596 |
|
|
} |
597 |
|
|
|
598 |
|
|
void ConstIntVariable::assign(Expression* expr) { |
599 |
|
|
// ignore assignment |
600 |
|
|
/* |
601 |
|
|
printf("ConstIntVariable::assign()\n"); |
602 |
|
|
IntExpr* intExpr = dynamic_cast<IntExpr*>(expr); |
603 |
|
|
if (intExpr) { |
604 |
|
|
value = intExpr->evalInt(); |
605 |
|
|
} |
606 |
|
|
*/ |
607 |
|
|
} |
608 |
|
|
|
609 |
schoenebeck |
3557 |
vmint ConstIntVariable::evalInt() { |
610 |
schoenebeck |
2581 |
return value; |
611 |
|
|
} |
612 |
|
|
|
613 |
|
|
void ConstIntVariable::dump(int level) { |
614 |
|
|
printIndents(level); |
615 |
schoenebeck |
3557 |
printf("ConstIntVariable val=%lld\n", value); |
616 |
schoenebeck |
2581 |
} |
617 |
|
|
|
618 |
schoenebeck |
3573 |
ConstRealVariable::ConstRealVariable(vmfloat value) |
619 |
|
|
: RealVariable(NULL,false,true), value(value) |
620 |
|
|
{ |
621 |
|
|
} |
622 |
|
|
|
623 |
|
|
void ConstRealVariable::assign(Expression* expr) { |
624 |
|
|
// ignore assignment |
625 |
|
|
} |
626 |
|
|
|
627 |
|
|
vmfloat ConstRealVariable::evalReal() { |
628 |
|
|
return value; |
629 |
|
|
} |
630 |
|
|
|
631 |
|
|
void ConstRealVariable::dump(int level) { |
632 |
|
|
printIndents(level); |
633 |
|
|
printf("ConstRealVariable val=%f\n", value); |
634 |
|
|
} |
635 |
|
|
|
636 |
schoenebeck |
3557 |
BuiltInIntVariable::BuiltInIntVariable(const String& name, VMIntPtr* ptr) |
637 |
schoenebeck |
2594 |
: IntVariable(NULL,false,false), name(name), ptr(ptr) |
638 |
|
|
{ |
639 |
|
|
} |
640 |
|
|
|
641 |
|
|
void BuiltInIntVariable::assign(Expression* expr) { |
642 |
|
|
IntExpr* valueExpr = dynamic_cast<IntExpr*>(expr); |
643 |
|
|
if (!valueExpr) return; |
644 |
|
|
ptr->assign(valueExpr->evalInt()); |
645 |
|
|
} |
646 |
|
|
|
647 |
schoenebeck |
3557 |
vmint BuiltInIntVariable::evalInt() { |
648 |
schoenebeck |
2594 |
return ptr->evalInt(); |
649 |
|
|
} |
650 |
|
|
|
651 |
|
|
void BuiltInIntVariable::dump(int level) { |
652 |
|
|
printIndents(level); |
653 |
|
|
printf("Built-in IntVar '%s'\n", name.c_str()); |
654 |
|
|
} |
655 |
|
|
|
656 |
schoenebeck |
2581 |
PolyphonicIntVariable::PolyphonicIntVariable(ParserContext* ctx) |
657 |
|
|
: IntVariable(ctx,true,false) |
658 |
|
|
{ |
659 |
|
|
} |
660 |
|
|
|
661 |
|
|
void PolyphonicIntVariable::dump(int level) { |
662 |
|
|
printIndents(level); |
663 |
|
|
printf("PolyphonicIntVariable\n"); |
664 |
|
|
} |
665 |
|
|
|
666 |
schoenebeck |
3573 |
PolyphonicRealVariable::PolyphonicRealVariable(ParserContext* ctx) |
667 |
|
|
: RealVariable(ctx,true,false) |
668 |
|
|
{ |
669 |
|
|
} |
670 |
|
|
|
671 |
|
|
void PolyphonicRealVariable::dump(int level) { |
672 |
|
|
printIndents(level); |
673 |
|
|
printf("PolyphonicRealVariable\n"); |
674 |
|
|
} |
675 |
|
|
|
676 |
schoenebeck |
3557 |
IntArrayVariable::IntArrayVariable(ParserContext* ctx, vmint size) |
677 |
schoenebeck |
2581 |
: Variable(ctx, 0, false) |
678 |
|
|
{ |
679 |
|
|
values.resize(size); |
680 |
schoenebeck |
3557 |
memset(&values[0], 0, size * sizeof(vmint)); |
681 |
schoenebeck |
2581 |
} |
682 |
|
|
|
683 |
schoenebeck |
3557 |
IntArrayVariable::IntArrayVariable(ParserContext* ctx, vmint size, ArgsRef values, bool _bConst) |
684 |
schoenebeck |
3257 |
: Variable(ctx, 0, _bConst) |
685 |
schoenebeck |
2581 |
{ |
686 |
|
|
this->values.resize(size); |
687 |
schoenebeck |
3557 |
for (vmint i = 0; i < values->argsCount(); ++i) { |
688 |
schoenebeck |
2581 |
VMIntExpr* expr = dynamic_cast<VMIntExpr*>(values->arg(i)); |
689 |
|
|
if (expr) this->values[i] = expr->evalInt(); |
690 |
|
|
} |
691 |
|
|
} |
692 |
|
|
|
693 |
schoenebeck |
2594 |
IntArrayVariable::IntArrayVariable(ParserContext* ctx, bool bConst) |
694 |
|
|
: Variable(ctx, 0, bConst) |
695 |
|
|
{ |
696 |
|
|
} |
697 |
|
|
|
698 |
schoenebeck |
3557 |
vmint IntArrayVariable::evalIntElement(vmuint i) { |
699 |
schoenebeck |
2581 |
if (i >= values.size()) return 0; |
700 |
|
|
return values[i]; |
701 |
|
|
} |
702 |
|
|
|
703 |
schoenebeck |
3557 |
void IntArrayVariable::assignIntElement(vmuint i, vmint value) { |
704 |
schoenebeck |
2581 |
if (i >= values.size()) return; |
705 |
|
|
values[i] = value; |
706 |
|
|
} |
707 |
|
|
|
708 |
|
|
void IntArrayVariable::dump(int level) { |
709 |
|
|
printIndents(level); |
710 |
|
|
printf("IntArray("); |
711 |
schoenebeck |
3557 |
for (vmint i = 0; i < values.size(); ++i) { |
712 |
schoenebeck |
2581 |
if (i % 12 == 0) { |
713 |
|
|
printf("\n"); |
714 |
|
|
printIndents(level+1); |
715 |
|
|
} |
716 |
schoenebeck |
3557 |
printf("%lld, ", values[i]); |
717 |
schoenebeck |
2581 |
} |
718 |
|
|
printIndents(level); |
719 |
|
|
printf(")\n"); |
720 |
|
|
} |
721 |
|
|
|
722 |
schoenebeck |
3573 |
RealArrayVariable::RealArrayVariable(ParserContext* ctx, vmint size) |
723 |
|
|
: Variable(ctx, 0, false) |
724 |
|
|
{ |
725 |
|
|
values.resize(size); |
726 |
|
|
memset(&values[0], 0, size * sizeof(vmfloat)); |
727 |
|
|
} |
728 |
|
|
|
729 |
|
|
RealArrayVariable::RealArrayVariable(ParserContext* ctx, vmint size, ArgsRef values, bool _bConst) |
730 |
|
|
: Variable(ctx, 0, _bConst) |
731 |
|
|
{ |
732 |
|
|
this->values.resize(size); |
733 |
|
|
for (vmint i = 0; i < values->argsCount(); ++i) { |
734 |
|
|
VMRealExpr* expr = dynamic_cast<VMRealExpr*>(values->arg(i)); |
735 |
|
|
if (expr) this->values[i] = expr->evalReal(); |
736 |
|
|
} |
737 |
|
|
} |
738 |
|
|
|
739 |
|
|
RealArrayVariable::RealArrayVariable(ParserContext* ctx, bool bConst) |
740 |
|
|
: Variable(ctx, 0, bConst) |
741 |
|
|
{ |
742 |
|
|
} |
743 |
|
|
|
744 |
|
|
vmfloat RealArrayVariable::evalRealElement(vmuint i) { |
745 |
|
|
if (i >= values.size()) return 0; |
746 |
|
|
return values[i]; |
747 |
|
|
} |
748 |
|
|
|
749 |
|
|
void RealArrayVariable::assignRealElement(vmuint i, vmfloat value) { |
750 |
|
|
if (i >= values.size()) return; |
751 |
|
|
values[i] = value; |
752 |
|
|
} |
753 |
|
|
|
754 |
|
|
void RealArrayVariable::dump(int level) { |
755 |
|
|
printIndents(level); |
756 |
|
|
printf("RealArray("); |
757 |
|
|
for (vmint i = 0; i < values.size(); ++i) { |
758 |
|
|
if (i % 12 == 0) { |
759 |
|
|
printf("\n"); |
760 |
|
|
printIndents(level+1); |
761 |
|
|
} |
762 |
|
|
printf("%f, ", values[i]); |
763 |
|
|
} |
764 |
|
|
printIndents(level); |
765 |
|
|
printf(")\n"); |
766 |
|
|
} |
767 |
|
|
|
768 |
schoenebeck |
2594 |
BuiltInIntArrayVariable::BuiltInIntArrayVariable(const String& name, VMInt8Array* array) |
769 |
|
|
: IntArrayVariable(NULL, false), name(name), array(array) |
770 |
|
|
{ |
771 |
|
|
} |
772 |
|
|
|
773 |
schoenebeck |
3557 |
vmint BuiltInIntArrayVariable::evalIntElement(vmuint i) { |
774 |
schoenebeck |
2594 |
return i >= array->size ? 0 : array->data[i]; |
775 |
|
|
} |
776 |
|
|
|
777 |
schoenebeck |
3557 |
void BuiltInIntArrayVariable::assignIntElement(vmuint i, vmint value) { |
778 |
schoenebeck |
2594 |
if (i >= array->size) return; |
779 |
|
|
array->data[i] = value; |
780 |
|
|
} |
781 |
|
|
|
782 |
|
|
void BuiltInIntArrayVariable::dump(int level) { |
783 |
|
|
printIndents(level); |
784 |
|
|
printf("Built-In Int Array Variable '%s'\n", name.c_str()); |
785 |
|
|
} |
786 |
|
|
|
787 |
schoenebeck |
3293 |
IntArrayElement::IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex) |
788 |
schoenebeck |
2581 |
: IntVariable(NULL, false, false, 0), array(array), index(arrayIndex) |
789 |
|
|
{ |
790 |
|
|
} |
791 |
|
|
|
792 |
|
|
void IntArrayElement::assign(Expression* expr) { |
793 |
|
|
IntExpr* valueExpr = dynamic_cast<IntExpr*>(expr); |
794 |
|
|
if (!valueExpr) return; |
795 |
schoenebeck |
3557 |
vmint value = valueExpr->evalInt(); |
796 |
schoenebeck |
2581 |
|
797 |
|
|
if (!index) return; |
798 |
schoenebeck |
3557 |
vmint idx = index->evalInt(); |
799 |
schoenebeck |
2581 |
if (idx < 0 || idx >= array->arraySize()) return; |
800 |
|
|
|
801 |
|
|
array->assignIntElement(idx, value); |
802 |
|
|
} |
803 |
|
|
|
804 |
schoenebeck |
3557 |
vmint IntArrayElement::evalInt() { |
805 |
schoenebeck |
2581 |
if (!index) return 0; |
806 |
schoenebeck |
3557 |
vmint idx = index->evalInt(); |
807 |
schoenebeck |
2581 |
if (idx < 0 || idx >= array->arraySize()) return 0; |
808 |
|
|
|
809 |
|
|
return array->evalIntElement(idx); |
810 |
|
|
} |
811 |
|
|
|
812 |
|
|
void IntArrayElement::dump(int level) { |
813 |
|
|
printIndents(level); |
814 |
|
|
printf("IntArrayElement\n"); |
815 |
|
|
} |
816 |
|
|
|
817 |
schoenebeck |
3573 |
RealArrayElement::RealArrayElement(RealArrayExprRef array, IntExprRef arrayIndex) |
818 |
|
|
: RealVariable(NULL, false, false, 0), array(array), index(arrayIndex) |
819 |
|
|
{ |
820 |
|
|
} |
821 |
|
|
|
822 |
|
|
void RealArrayElement::assign(Expression* expr) { |
823 |
|
|
RealExpr* valueExpr = dynamic_cast<RealExpr*>(expr); |
824 |
|
|
if (!valueExpr) return; |
825 |
|
|
vmfloat value = valueExpr->evalReal(); |
826 |
|
|
|
827 |
|
|
if (!index) return; |
828 |
|
|
vmint idx = index->evalInt(); |
829 |
|
|
if (idx < 0 || idx >= array->arraySize()) return; |
830 |
|
|
|
831 |
|
|
array->assignRealElement(idx, value); |
832 |
|
|
} |
833 |
|
|
|
834 |
|
|
vmfloat RealArrayElement::evalReal() { |
835 |
|
|
if (!index) return 0; |
836 |
|
|
vmint idx = index->evalInt(); |
837 |
|
|
if (idx < 0 || idx >= array->arraySize()) return 0; |
838 |
|
|
|
839 |
|
|
return array->evalRealElement(idx); |
840 |
|
|
} |
841 |
|
|
|
842 |
|
|
void RealArrayElement::dump(int level) { |
843 |
|
|
printIndents(level); |
844 |
|
|
printf("RealArrayElement\n"); |
845 |
|
|
} |
846 |
|
|
|
847 |
schoenebeck |
2581 |
StringVariable::StringVariable(ParserContext* ctx) |
848 |
|
|
: Variable(ctx,ctx->globalStrVarCount++,false) |
849 |
|
|
{ |
850 |
|
|
} |
851 |
|
|
|
852 |
|
|
StringVariable::StringVariable(ParserContext* ctx, bool bConst) |
853 |
|
|
: Variable(ctx,0,bConst) |
854 |
|
|
{ |
855 |
|
|
} |
856 |
|
|
|
857 |
|
|
void StringVariable::assign(Expression* expr) { |
858 |
|
|
StringExpr* strExpr = dynamic_cast<StringExpr*>(expr); |
859 |
|
|
(*context->globalStrMemory)[memPos] = strExpr->evalStr(); |
860 |
|
|
} |
861 |
|
|
|
862 |
|
|
String StringVariable::evalStr() { |
863 |
|
|
//printf("StringVariable::eval pos=%d\n", memPos); |
864 |
|
|
return (*context->globalStrMemory)[memPos]; |
865 |
|
|
} |
866 |
|
|
|
867 |
|
|
void StringVariable::dump(int level) { |
868 |
|
|
printIndents(level); |
869 |
schoenebeck |
3557 |
printf("StringVariable memPos=%lld\n", memPos); |
870 |
schoenebeck |
2581 |
} |
871 |
|
|
|
872 |
|
|
ConstStringVariable::ConstStringVariable(ParserContext* ctx, String _value) |
873 |
|
|
: StringVariable(ctx,true), value(_value) |
874 |
|
|
{ |
875 |
|
|
} |
876 |
|
|
|
877 |
|
|
void ConstStringVariable::assign(Expression* expr) { |
878 |
|
|
// ignore assignment |
879 |
|
|
// StringExpr* strExpr = dynamic_cast<StringExpr*>(expr); |
880 |
|
|
// if (strExpr) value = strExpr->evalStr(); |
881 |
|
|
} |
882 |
|
|
|
883 |
|
|
String ConstStringVariable::evalStr() { |
884 |
|
|
return value; |
885 |
|
|
} |
886 |
|
|
|
887 |
|
|
void ConstStringVariable::dump(int level) { |
888 |
|
|
printIndents(level); |
889 |
|
|
printf("ConstStringVariable val='%s'\n", value.c_str()); |
890 |
|
|
} |
891 |
|
|
|
892 |
schoenebeck |
3573 |
MetricPrefix_t ScalarNumberBinaryOp::unitPrefix(vmuint i) const { |
893 |
|
|
ScalarNumberExprRef l = (ScalarNumberExprRef) lhs; |
894 |
|
|
ScalarNumberExprRef r = (ScalarNumberExprRef) rhs; |
895 |
schoenebeck |
3561 |
return (r->unitFactor() < l->unitFactor()) ? r->unitPrefix(i) : l->unitPrefix(i); |
896 |
|
|
} |
897 |
|
|
|
898 |
schoenebeck |
3573 |
StdUnit_t ScalarNumberBinaryOp::unitType() const { |
899 |
|
|
ScalarNumberExprRef l = (ScalarNumberExprRef) lhs; |
900 |
|
|
ScalarNumberExprRef r = (ScalarNumberExprRef) rhs; |
901 |
schoenebeck |
3561 |
return (l->unitType()) ? l->unitType() : r->unitType(); |
902 |
|
|
} |
903 |
|
|
|
904 |
schoenebeck |
3573 |
bool ScalarNumberBinaryOp::isFinal() const { |
905 |
|
|
ScalarNumberExprRef l = (ScalarNumberExprRef) lhs; |
906 |
|
|
ScalarNumberExprRef r = (ScalarNumberExprRef) rhs; |
907 |
schoenebeck |
3561 |
return l->isFinal() || r->isFinal(); |
908 |
|
|
} |
909 |
|
|
|
910 |
schoenebeck |
3573 |
ExprType_t VaritypeScalarBinaryOp::exprType() const { |
911 |
|
|
return (lhs->exprType() == REAL_EXPR || rhs->exprType() == REAL_EXPR) ? REAL_EXPR : INT_EXPR; |
912 |
|
|
} |
913 |
|
|
|
914 |
|
|
String VaritypeScalarBinaryOp::evalCastToStr() { |
915 |
|
|
return (exprType() == REAL_EXPR) ? |
916 |
|
|
RealExpr::evalCastToStr() : IntExpr::evalCastToStr(); |
917 |
|
|
} |
918 |
|
|
|
919 |
schoenebeck |
2581 |
void If::dump(int level) { |
920 |
|
|
printIndents(level); |
921 |
|
|
if (ifStatements && elseStatements) |
922 |
|
|
printf("if cond stmts1 else stmts2 end if\n"); |
923 |
|
|
else if (ifStatements) |
924 |
|
|
printf("if cond statements end if\n"); |
925 |
|
|
else |
926 |
|
|
printf("if [INVALID]\n"); |
927 |
|
|
} |
928 |
|
|
|
929 |
schoenebeck |
3557 |
vmint If::evalBranch() { |
930 |
schoenebeck |
2581 |
if (condition->evalInt()) return 0; |
931 |
|
|
if (elseStatements) return 1; |
932 |
|
|
return -1; |
933 |
|
|
} |
934 |
|
|
|
935 |
schoenebeck |
3557 |
Statements* If::branch(vmuint i) const { |
936 |
schoenebeck |
2581 |
if (i == 0) return (Statements*) &*ifStatements; |
937 |
|
|
if (i == 1) return (elseStatements) ? (Statements*) &*elseStatements : NULL; |
938 |
|
|
return NULL; |
939 |
|
|
} |
940 |
|
|
|
941 |
schoenebeck |
2645 |
bool If::isPolyphonic() const { |
942 |
|
|
if (condition->isPolyphonic() || ifStatements->isPolyphonic()) |
943 |
|
|
return true; |
944 |
|
|
return elseStatements ? elseStatements->isPolyphonic() : false; |
945 |
|
|
} |
946 |
|
|
|
947 |
schoenebeck |
2581 |
void SelectCase::dump(int level) { |
948 |
|
|
printIndents(level); |
949 |
|
|
if (select) |
950 |
|
|
if (select->isConstExpr()) |
951 |
schoenebeck |
3557 |
printf("Case select %lld\n", select->evalInt()); |
952 |
schoenebeck |
2581 |
else |
953 |
|
|
printf("Case select [runtime expr]\n"); |
954 |
|
|
else |
955 |
|
|
printf("Case select NULL\n"); |
956 |
schoenebeck |
3557 |
for (vmint i = 0; i < branches.size(); ++i) { |
957 |
schoenebeck |
2581 |
printIndents(level+1); |
958 |
|
|
CaseBranch& branch = branches[i]; |
959 |
|
|
if (branch.from && branch.to) |
960 |
|
|
if (branch.from->isConstExpr() && branch.to->isConstExpr()) |
961 |
schoenebeck |
3557 |
printf("case %lld to %lld\n", branch.from->evalInt(), branch.to->evalInt()); |
962 |
schoenebeck |
2581 |
else if (branch.from->isConstExpr() && !branch.to->isConstExpr()) |
963 |
schoenebeck |
3557 |
printf("case %lld to [runtime expr]\n", branch.from->evalInt()); |
964 |
schoenebeck |
2581 |
else if (!branch.from->isConstExpr() && branch.to->isConstExpr()) |
965 |
schoenebeck |
3557 |
printf("case [runtime expr] to %lld\n", branch.to->evalInt()); |
966 |
schoenebeck |
2581 |
else |
967 |
|
|
printf("case [runtime expr] to [runtime expr]\n"); |
968 |
|
|
else if (branch.from) |
969 |
|
|
if (branch.from->isConstExpr()) |
970 |
schoenebeck |
3557 |
printf("case %lld\n", branch.from->evalInt()); |
971 |
schoenebeck |
2581 |
else |
972 |
|
|
printf("case [runtime expr]\n"); |
973 |
|
|
else |
974 |
|
|
printf("case NULL\n"); |
975 |
|
|
} |
976 |
|
|
} |
977 |
|
|
|
978 |
schoenebeck |
3557 |
vmint SelectCase::evalBranch() { |
979 |
|
|
vmint value = select->evalInt(); |
980 |
|
|
for (vmint i = 0; i < branches.size(); ++i) { |
981 |
schoenebeck |
2581 |
if (branches.at(i).from && branches.at(i).to) { // i.e. "case 4 to 7" ... |
982 |
|
|
if (branches.at(i).from->evalInt() <= value && |
983 |
|
|
branches.at(i).to->evalInt() >= value) return i; |
984 |
|
|
} else { // i.e. "case 5" ... |
985 |
|
|
if (branches.at(i).from->evalInt() == value) return i; |
986 |
|
|
} |
987 |
|
|
} |
988 |
|
|
return -1; |
989 |
|
|
} |
990 |
|
|
|
991 |
schoenebeck |
3557 |
Statements* SelectCase::branch(vmuint i) const { |
992 |
schoenebeck |
2581 |
if (i < branches.size()) |
993 |
|
|
return const_cast<Statements*>( &*branches[i].statements ); |
994 |
|
|
return NULL; |
995 |
|
|
} |
996 |
|
|
|
997 |
schoenebeck |
2645 |
bool SelectCase::isPolyphonic() const { |
998 |
|
|
if (select->isPolyphonic()) return true; |
999 |
schoenebeck |
3557 |
for (vmint i = 0; i < branches.size(); ++i) |
1000 |
schoenebeck |
2645 |
if (branches[i].statements->isPolyphonic()) |
1001 |
|
|
return true; |
1002 |
|
|
return false; |
1003 |
|
|
} |
1004 |
|
|
|
1005 |
schoenebeck |
2581 |
// void Case::addBranch(IntExprRef condition, StatementsRef statements) { |
1006 |
|
|
// CaseBranchRef b = new CaseBranchRef; |
1007 |
|
|
// b->from = condition; |
1008 |
|
|
// b->statements = statements; |
1009 |
|
|
// branches.push_back(b); |
1010 |
|
|
// } |
1011 |
|
|
// |
1012 |
|
|
// void Case::addBranch(IntExprRef from, IntExprRef to, StatementsRef statements) { |
1013 |
|
|
// CaseBranchRef b = new CaseBranchRef; |
1014 |
|
|
// b->from = from; |
1015 |
|
|
// b->to = to; |
1016 |
|
|
// b->statements = statements; |
1017 |
|
|
// branches.push_back(b); |
1018 |
|
|
// } |
1019 |
|
|
// |
1020 |
|
|
// void Case::addBranch(CaseBranchRef branch) { |
1021 |
|
|
// branches.push_back(branch); |
1022 |
|
|
// } |
1023 |
|
|
|
1024 |
|
|
void While::dump(int level) { |
1025 |
|
|
printIndents(level); |
1026 |
|
|
if (m_condition) |
1027 |
|
|
if (m_condition->isConstExpr()) |
1028 |
schoenebeck |
3557 |
printf("while (%lld) {\n", m_condition->evalInt()); |
1029 |
schoenebeck |
2581 |
else |
1030 |
|
|
printf("while ([runtime expr]) {\n"); |
1031 |
|
|
else |
1032 |
|
|
printf("while ([INVALID]) {\n"); |
1033 |
|
|
m_statements->dump(level+1); |
1034 |
|
|
printIndents(level); |
1035 |
|
|
printf("}\n"); |
1036 |
|
|
} |
1037 |
|
|
|
1038 |
|
|
Statements* While::statements() const { |
1039 |
|
|
return (m_statements) ? const_cast<Statements*>( &*m_statements ) : NULL; |
1040 |
|
|
} |
1041 |
|
|
|
1042 |
|
|
bool While::evalLoopStartCondition() { |
1043 |
|
|
if (!m_condition) return false; |
1044 |
|
|
return m_condition->evalInt(); |
1045 |
|
|
} |
1046 |
|
|
|
1047 |
schoenebeck |
3260 |
void SyncBlock::dump(int level) { |
1048 |
|
|
printIndents(level); |
1049 |
|
|
printf("sync {\n"); |
1050 |
|
|
m_statements->dump(level+1); |
1051 |
|
|
printIndents(level); |
1052 |
|
|
printf("}\n"); |
1053 |
|
|
} |
1054 |
|
|
|
1055 |
|
|
Statements* SyncBlock::statements() const { |
1056 |
|
|
return (m_statements) ? const_cast<Statements*>( &*m_statements ) : NULL; |
1057 |
|
|
} |
1058 |
|
|
|
1059 |
schoenebeck |
2581 |
void Neg::dump(int level) { |
1060 |
|
|
printIndents(level); |
1061 |
|
|
printf("Negative Expr\n"); |
1062 |
|
|
} |
1063 |
|
|
|
1064 |
|
|
String ConcatString::evalStr() { |
1065 |
schoenebeck |
2945 |
// temporaries required here to enforce the associative left (to right) order |
1066 |
schoenebeck |
2948 |
// ( required for GCC and Visual Studio, see: |
1067 |
|
|
// http://stackoverflow.com/questions/25842902/why-stdstring-concatenation-operator-works-like-right-associative-one |
1068 |
|
|
// Personally I am not convinced that this is "not a bug" of the |
1069 |
|
|
// compiler/STL implementation and the allegedly underlying "function call" |
1070 |
|
|
// nature causing this is IMO no profound reason that the C++ language's |
1071 |
|
|
// "+" operator's left associativity is ignored. -- Christian, 2016-07-14 ) |
1072 |
schoenebeck |
2945 |
String l = lhs->evalCastToStr(); |
1073 |
|
|
String r = rhs->evalCastToStr(); |
1074 |
|
|
return l + r; |
1075 |
schoenebeck |
2581 |
} |
1076 |
|
|
|
1077 |
|
|
void ConcatString::dump(int level) { |
1078 |
|
|
printIndents(level); |
1079 |
|
|
printf("ConcatString(\n"); |
1080 |
|
|
lhs->dump(level+1); |
1081 |
|
|
printIndents(level); |
1082 |
|
|
printf(",\n"); |
1083 |
|
|
rhs->dump(level+1); |
1084 |
|
|
printIndents(level); |
1085 |
|
|
printf(")"); |
1086 |
|
|
} |
1087 |
|
|
|
1088 |
|
|
bool ConcatString::isConstExpr() const { |
1089 |
|
|
return lhs->isConstExpr() && rhs->isConstExpr(); |
1090 |
|
|
} |
1091 |
|
|
|
1092 |
schoenebeck |
3573 |
template<class T_LHS, class T_RHS> |
1093 |
|
|
static inline vmint _evalRelation(Relation::Type type, T_LHS lhs, T_RHS rhs) { |
1094 |
|
|
switch (type) { |
1095 |
|
|
case Relation::LESS_THAN: |
1096 |
|
|
return lhs < rhs; |
1097 |
|
|
case Relation::GREATER_THAN: |
1098 |
|
|
return lhs > rhs; |
1099 |
|
|
case Relation::LESS_OR_EQUAL: |
1100 |
|
|
return lhs <= rhs; |
1101 |
|
|
case Relation::GREATER_OR_EQUAL: |
1102 |
|
|
return lhs >= rhs; |
1103 |
|
|
case Relation::EQUAL: |
1104 |
|
|
return lhs == rhs; |
1105 |
|
|
case Relation::NOT_EQUAL: |
1106 |
|
|
return lhs != rhs; |
1107 |
|
|
} |
1108 |
|
|
return 0; |
1109 |
|
|
} |
1110 |
|
|
|
1111 |
schoenebeck |
3557 |
vmint Relation::evalInt() { |
1112 |
schoenebeck |
3573 |
const ExprType_t lType = lhs->exprType(); |
1113 |
|
|
const ExprType_t rType = rhs->exprType(); |
1114 |
|
|
if (lType == STRING_EXPR || rType == STRING_EXPR) { |
1115 |
|
|
switch (type) { |
1116 |
|
|
case EQUAL: |
1117 |
schoenebeck |
2581 |
return lhs->evalCastToStr() == rhs->evalCastToStr(); |
1118 |
schoenebeck |
3573 |
case NOT_EQUAL: |
1119 |
schoenebeck |
2581 |
return lhs->evalCastToStr() != rhs->evalCastToStr(); |
1120 |
schoenebeck |
3573 |
default: |
1121 |
|
|
return 0; |
1122 |
|
|
} |
1123 |
|
|
} else if (lType == REAL_EXPR && rType == REAL_EXPR) { |
1124 |
|
|
return _evalRelation( |
1125 |
|
|
type, lhs->asReal()->evalReal(), rhs->asReal()->evalReal() |
1126 |
|
|
); |
1127 |
|
|
} else if (lType == REAL_EXPR && rType == INT_EXPR) { |
1128 |
|
|
return _evalRelation( |
1129 |
|
|
type, lhs->asReal()->evalReal(), rhs->asInt()->evalInt() |
1130 |
|
|
); |
1131 |
|
|
} else if (lType == INT_EXPR && rType == REAL_EXPR) { |
1132 |
|
|
return _evalRelation( |
1133 |
|
|
type, lhs->asInt()->evalInt(), rhs->asReal()->evalReal() |
1134 |
|
|
); |
1135 |
|
|
} else { |
1136 |
|
|
return _evalRelation( |
1137 |
|
|
type, lhs->asInt()->evalInt(), rhs->asInt()->evalInt() |
1138 |
|
|
); |
1139 |
schoenebeck |
2581 |
} |
1140 |
|
|
} |
1141 |
|
|
|
1142 |
|
|
void Relation::dump(int level) { |
1143 |
|
|
printIndents(level); |
1144 |
|
|
printf("Relation(\n"); |
1145 |
|
|
lhs->dump(level+1); |
1146 |
|
|
printIndents(level); |
1147 |
|
|
switch (type) { |
1148 |
|
|
case LESS_THAN: |
1149 |
|
|
printf("LESS_THAN\n"); |
1150 |
|
|
break; |
1151 |
|
|
case GREATER_THAN: |
1152 |
|
|
printf("GREATER_THAN\n"); |
1153 |
|
|
break; |
1154 |
|
|
case LESS_OR_EQUAL: |
1155 |
|
|
printf("LESS_OR_EQUAL\n"); |
1156 |
|
|
break; |
1157 |
|
|
case GREATER_OR_EQUAL: |
1158 |
|
|
printf("GREATER_OR_EQUAL\n"); |
1159 |
|
|
break; |
1160 |
|
|
case EQUAL: |
1161 |
|
|
printf("EQUAL\n"); |
1162 |
|
|
break; |
1163 |
|
|
case NOT_EQUAL: |
1164 |
|
|
printf("NOT_EQUAL\n"); |
1165 |
|
|
break; |
1166 |
|
|
} |
1167 |
|
|
rhs->dump(level+1); |
1168 |
|
|
printIndents(level); |
1169 |
|
|
printf(")\n"); |
1170 |
|
|
} |
1171 |
|
|
|
1172 |
|
|
bool Relation::isConstExpr() const { |
1173 |
|
|
return lhs->isConstExpr() && rhs->isConstExpr(); |
1174 |
|
|
} |
1175 |
|
|
|
1176 |
schoenebeck |
3557 |
vmint Or::evalInt() { |
1177 |
schoenebeck |
2581 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
1178 |
schoenebeck |
2611 |
if (pLHS->evalInt()) return 1; |
1179 |
schoenebeck |
2581 |
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
1180 |
schoenebeck |
2611 |
return (pRHS->evalInt()) ? 1 : 0; |
1181 |
schoenebeck |
2581 |
} |
1182 |
|
|
|
1183 |
|
|
void Or::dump(int level) { |
1184 |
|
|
printIndents(level); |
1185 |
|
|
printf("Or(\n"); |
1186 |
|
|
lhs->dump(level+1); |
1187 |
|
|
printIndents(level); |
1188 |
|
|
printf(",\n"); |
1189 |
|
|
rhs->dump(level+1); |
1190 |
|
|
printIndents(level); |
1191 |
|
|
printf(")\n"); |
1192 |
|
|
} |
1193 |
|
|
|
1194 |
schoenebeck |
3557 |
vmint BitwiseOr::evalInt() { |
1195 |
schoenebeck |
2935 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
1196 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs); |
1197 |
|
|
return pLHS->evalInt() | pRHS->evalInt(); |
1198 |
|
|
} |
1199 |
|
|
|
1200 |
|
|
void BitwiseOr::dump(int level) { |
1201 |
|
|
printIndents(level); |
1202 |
|
|
printf("BitwiseOr(\n"); |
1203 |
|
|
lhs->dump(level+1); |
1204 |
|
|
printIndents(level); |
1205 |
|
|
printf(",\n"); |
1206 |
|
|
rhs->dump(level+1); |
1207 |
|
|
printIndents(level); |
1208 |
|
|
printf(")\n"); |
1209 |
|
|
} |
1210 |
|
|
|
1211 |
schoenebeck |
3557 |
vmint And::evalInt() { |
1212 |
schoenebeck |
2581 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
1213 |
schoenebeck |
2611 |
if (!pLHS->evalInt()) return 0; |
1214 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs); |
1215 |
|
|
return (pRHS->evalInt()) ? 1 : 0; |
1216 |
schoenebeck |
2581 |
} |
1217 |
|
|
|
1218 |
|
|
void And::dump(int level) { |
1219 |
|
|
printIndents(level); |
1220 |
|
|
printf("And(\n"); |
1221 |
|
|
lhs->dump(level+1); |
1222 |
|
|
printIndents(level); |
1223 |
|
|
printf(",\n"); |
1224 |
|
|
rhs->dump(level+1); |
1225 |
|
|
printIndents(level); |
1226 |
|
|
printf(")\n"); |
1227 |
|
|
} |
1228 |
|
|
|
1229 |
schoenebeck |
3557 |
vmint BitwiseAnd::evalInt() { |
1230 |
schoenebeck |
2935 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
1231 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs); |
1232 |
|
|
return pLHS->evalInt() & pRHS->evalInt(); |
1233 |
|
|
} |
1234 |
|
|
|
1235 |
|
|
void BitwiseAnd::dump(int level) { |
1236 |
|
|
printIndents(level); |
1237 |
|
|
printf("BitwiseAnd(\n"); |
1238 |
|
|
lhs->dump(level+1); |
1239 |
|
|
printIndents(level); |
1240 |
|
|
printf(",\n"); |
1241 |
|
|
rhs->dump(level+1); |
1242 |
|
|
printIndents(level); |
1243 |
|
|
printf(")\n"); |
1244 |
|
|
} |
1245 |
|
|
|
1246 |
schoenebeck |
2581 |
void Not::dump(int level) { |
1247 |
|
|
printIndents(level); |
1248 |
|
|
printf("Not(\n"); |
1249 |
|
|
expr->dump(level+1); |
1250 |
|
|
printIndents(level); |
1251 |
|
|
printf(")\n"); |
1252 |
|
|
} |
1253 |
|
|
|
1254 |
schoenebeck |
2935 |
void BitwiseNot::dump(int level) { |
1255 |
|
|
printIndents(level); |
1256 |
|
|
printf("BitwiseNot(\n"); |
1257 |
|
|
expr->dump(level+1); |
1258 |
|
|
printIndents(level); |
1259 |
|
|
printf(")\n"); |
1260 |
|
|
} |
1261 |
|
|
|
1262 |
schoenebeck |
3573 |
String Final::evalCastToStr() { |
1263 |
|
|
if (exprType() == REAL_EXPR) |
1264 |
|
|
return ToString(evalReal()); |
1265 |
|
|
else |
1266 |
|
|
return ToString(evalInt()); |
1267 |
|
|
} |
1268 |
|
|
|
1269 |
schoenebeck |
3561 |
void Final::dump(int level) { |
1270 |
|
|
printIndents(level); |
1271 |
|
|
printf("Final(\n"); |
1272 |
|
|
expr->dump(level+1); |
1273 |
|
|
printIndents(level); |
1274 |
|
|
printf(")\n"); |
1275 |
|
|
} |
1276 |
|
|
|
1277 |
schoenebeck |
2951 |
StatementsRef ParserContext::userFunctionByName(const String& name) { |
1278 |
|
|
if (!userFnTable.count(name)) { |
1279 |
|
|
return StatementsRef(); |
1280 |
|
|
} |
1281 |
|
|
return userFnTable.find(name)->second; |
1282 |
|
|
} |
1283 |
|
|
|
1284 |
schoenebeck |
2581 |
VariableRef ParserContext::variableByName(const String& name) { |
1285 |
|
|
if (!vartable.count(name)) { |
1286 |
|
|
return VariableRef(); |
1287 |
|
|
} |
1288 |
|
|
return vartable.find(name)->second; |
1289 |
|
|
} |
1290 |
|
|
|
1291 |
|
|
VariableRef ParserContext::globalVar(const String& name) { |
1292 |
|
|
if (!vartable.count(name)) { |
1293 |
|
|
//printf("No global var '%s'\n", name.c_str()); |
1294 |
|
|
//for (std::map<String,VariableRef>::const_iterator it = vartable.begin(); it != vartable.end(); ++it) |
1295 |
|
|
// printf("-> var '%s'\n", it->first.c_str()); |
1296 |
|
|
return VariableRef(); |
1297 |
|
|
} |
1298 |
|
|
return vartable.find(name)->second; |
1299 |
|
|
} |
1300 |
|
|
|
1301 |
|
|
IntVariableRef ParserContext::globalIntVar(const String& name) { |
1302 |
|
|
return globalVar(name); |
1303 |
|
|
} |
1304 |
|
|
|
1305 |
schoenebeck |
3573 |
RealVariableRef ParserContext::globalRealVar(const String& name) { |
1306 |
|
|
return globalVar(name); |
1307 |
|
|
} |
1308 |
|
|
|
1309 |
schoenebeck |
2581 |
StringVariableRef ParserContext::globalStrVar(const String& name) { |
1310 |
|
|
return globalVar(name); |
1311 |
|
|
} |
1312 |
|
|
|
1313 |
schoenebeck |
2588 |
ParserContext::~ParserContext() { |
1314 |
|
|
destroyScanner(); |
1315 |
|
|
if (globalIntMemory) { |
1316 |
|
|
delete globalIntMemory; |
1317 |
|
|
globalIntMemory = NULL; |
1318 |
|
|
} |
1319 |
schoenebeck |
3573 |
if (globalRealMemory) { |
1320 |
|
|
delete globalRealMemory; |
1321 |
|
|
globalRealMemory = NULL; |
1322 |
|
|
} |
1323 |
schoenebeck |
2588 |
} |
1324 |
|
|
|
1325 |
schoenebeck |
2889 |
void ParserContext::addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt) { |
1326 |
schoenebeck |
2581 |
ParserIssue e; |
1327 |
|
|
e.type = PARSER_ERROR; |
1328 |
|
|
e.txt = txt; |
1329 |
schoenebeck |
2889 |
e.firstLine = firstLine; |
1330 |
|
|
e.lastLine = lastLine; |
1331 |
|
|
e.firstColumn = firstColumn; |
1332 |
|
|
e.lastColumn = lastColumn; |
1333 |
schoenebeck |
2588 |
vErrors.push_back(e); |
1334 |
|
|
vIssues.push_back(e); |
1335 |
schoenebeck |
2581 |
} |
1336 |
|
|
|
1337 |
schoenebeck |
2889 |
void ParserContext::addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt) { |
1338 |
schoenebeck |
2581 |
ParserIssue w; |
1339 |
|
|
w.type = PARSER_WARNING; |
1340 |
|
|
w.txt = txt; |
1341 |
schoenebeck |
2889 |
w.firstLine = firstLine; |
1342 |
|
|
w.lastLine = lastLine; |
1343 |
|
|
w.firstColumn = firstColumn; |
1344 |
|
|
w.lastColumn = lastColumn; |
1345 |
schoenebeck |
2588 |
vWarnings.push_back(w); |
1346 |
|
|
vIssues.push_back(w); |
1347 |
schoenebeck |
2581 |
} |
1348 |
|
|
|
1349 |
schoenebeck |
3285 |
void ParserContext::addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn) { |
1350 |
|
|
CodeBlock block; |
1351 |
|
|
block.firstLine = firstLine; |
1352 |
|
|
block.lastLine = lastLine; |
1353 |
|
|
block.firstColumn = firstColumn; |
1354 |
|
|
block.lastColumn = lastColumn; |
1355 |
|
|
vPreprocessorComments.push_back(block); |
1356 |
|
|
} |
1357 |
|
|
|
1358 |
schoenebeck |
2581 |
bool ParserContext::setPreprocessorCondition(const char* name) { |
1359 |
|
|
if (builtinPreprocessorConditions.count(name)) return false; |
1360 |
|
|
if (userPreprocessorConditions.count(name)) return false; |
1361 |
|
|
userPreprocessorConditions.insert(name); |
1362 |
|
|
return true; |
1363 |
|
|
} |
1364 |
|
|
|
1365 |
|
|
bool ParserContext::resetPreprocessorCondition(const char* name) { |
1366 |
|
|
if (builtinPreprocessorConditions.count(name)) return false; |
1367 |
|
|
if (!userPreprocessorConditions.count(name)) return false; |
1368 |
|
|
userPreprocessorConditions.erase(name); |
1369 |
|
|
return true; |
1370 |
|
|
} |
1371 |
|
|
|
1372 |
|
|
bool ParserContext::isPreprocessorConditionSet(const char* name) { |
1373 |
|
|
if (builtinPreprocessorConditions.count(name)) return true; |
1374 |
|
|
return userPreprocessorConditions.count(name); |
1375 |
|
|
} |
1376 |
|
|
|
1377 |
schoenebeck |
2588 |
std::vector<ParserIssue> ParserContext::issues() const { |
1378 |
|
|
return vIssues; |
1379 |
|
|
} |
1380 |
|
|
|
1381 |
|
|
std::vector<ParserIssue> ParserContext::errors() const { |
1382 |
|
|
return vErrors; |
1383 |
|
|
} |
1384 |
|
|
|
1385 |
|
|
std::vector<ParserIssue> ParserContext::warnings() const { |
1386 |
|
|
return vWarnings; |
1387 |
|
|
} |
1388 |
|
|
|
1389 |
schoenebeck |
3285 |
std::vector<CodeBlock> ParserContext::preprocessorComments() const { |
1390 |
|
|
return vPreprocessorComments; |
1391 |
|
|
} |
1392 |
|
|
|
1393 |
schoenebeck |
2588 |
VMEventHandler* ParserContext::eventHandler(uint index) { |
1394 |
|
|
if (!handlers) return NULL; |
1395 |
|
|
return handlers->eventHandler(index); |
1396 |
|
|
} |
1397 |
|
|
|
1398 |
|
|
VMEventHandler* ParserContext::eventHandlerByName(const String& name) { |
1399 |
|
|
if (!handlers) return NULL; |
1400 |
|
|
return handlers->eventHandlerByName(name); |
1401 |
|
|
} |
1402 |
|
|
|
1403 |
schoenebeck |
3557 |
void ParserContext::registerBuiltInConstIntVariables(const std::map<String,vmint>& vars) { |
1404 |
|
|
for (std::map<String,vmint>::const_iterator it = vars.begin(); |
1405 |
schoenebeck |
2594 |
it != vars.end(); ++it) |
1406 |
|
|
{ |
1407 |
|
|
ConstIntVariableRef ref = new ConstIntVariable(it->second); |
1408 |
|
|
vartable[it->first] = ref; |
1409 |
|
|
} |
1410 |
|
|
} |
1411 |
|
|
|
1412 |
schoenebeck |
3557 |
void ParserContext::registerBuiltInIntVariables(const std::map<String,VMIntPtr*>& vars) { |
1413 |
|
|
for (std::map<String,VMIntPtr*>::const_iterator it = vars.begin(); |
1414 |
schoenebeck |
2594 |
it != vars.end(); ++it) |
1415 |
|
|
{ |
1416 |
|
|
BuiltInIntVariableRef ref = new BuiltInIntVariable(it->first, it->second); |
1417 |
|
|
vartable[it->first] = ref; |
1418 |
|
|
} |
1419 |
|
|
} |
1420 |
|
|
|
1421 |
|
|
void ParserContext::registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars) { |
1422 |
|
|
for (std::map<String,VMInt8Array*>::const_iterator it = vars.begin(); |
1423 |
|
|
it != vars.end(); ++it) |
1424 |
|
|
{ |
1425 |
|
|
BuiltInIntArrayVariableRef ref = new BuiltInIntArrayVariable(it->first, it->second); |
1426 |
|
|
vartable[it->first] = ref; |
1427 |
|
|
} |
1428 |
|
|
} |
1429 |
|
|
|
1430 |
schoenebeck |
2942 |
void ParserContext::registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars) { |
1431 |
|
|
for (std::map<String,VMDynVar*>::const_iterator it = vars.begin(); |
1432 |
|
|
it != vars.end(); ++it) |
1433 |
|
|
{ |
1434 |
|
|
DynamicVariableCallRef ref = new DynamicVariableCall(it->first, this, it->second); |
1435 |
|
|
vartable[it->first] = ref; |
1436 |
|
|
} |
1437 |
|
|
} |
1438 |
|
|
|
1439 |
schoenebeck |
3551 |
ExecContext::ExecContext() : |
1440 |
|
|
status(VM_EXEC_NOT_RUNNING), flags(STMT_SUCCESS), stackFrame(-1), |
1441 |
|
|
suspendMicroseconds(0), instructionsCount(0) |
1442 |
|
|
{ |
1443 |
|
|
exitRes.value = NULL; |
1444 |
|
|
} |
1445 |
|
|
|
1446 |
schoenebeck |
3293 |
void ExecContext::forkTo(VMExecContext* ectx) const { |
1447 |
|
|
ExecContext* child = dynamic_cast<ExecContext*>(ectx); |
1448 |
|
|
|
1449 |
|
|
child->polyphonicIntMemory.copyFlatFrom(polyphonicIntMemory); |
1450 |
schoenebeck |
3573 |
child->polyphonicRealMemory.copyFlatFrom(polyphonicRealMemory); |
1451 |
schoenebeck |
3293 |
child->status = VM_EXEC_SUSPENDED; |
1452 |
|
|
child->flags = STMT_SUCCESS; |
1453 |
|
|
child->stack.copyFlatFrom(stack); |
1454 |
|
|
child->stackFrame = stackFrame; |
1455 |
|
|
child->suspendMicroseconds = 0; |
1456 |
|
|
child->instructionsCount = 0; |
1457 |
|
|
} |
1458 |
|
|
|
1459 |
schoenebeck |
2581 |
} // namespace LinuxSampler |