1 |
schoenebeck |
2581 |
/* |
2 |
|
|
* Copyright (c) 2014 Christian Schoenebeck and Andreas Persson |
3 |
|
|
* |
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 |
|
|
NoOperation* noOp = dynamic_cast<NoOperation*>(&*statement); |
20 |
|
|
return noOp; |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
Node::Node() { |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
Node::~Node() { |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
void Node::printIndents(int n) { |
30 |
|
|
for (int i = 0; i < n; ++i) printf(" "); |
31 |
|
|
fflush(stdout); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
String IntExpr::evalCastToStr() { |
35 |
|
|
return ToString(evalInt()); |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
int IntLiteral::evalInt() { |
39 |
|
|
return value; |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
void IntLiteral::dump(int level) { |
43 |
|
|
printIndents(level); |
44 |
|
|
printf("IntLiteral %d\n", value); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
void StringLiteral::dump(int level) { |
48 |
|
|
printIndents(level); |
49 |
|
|
printf("StringLiteral: '%s'\n", value.c_str()); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
int Add::evalInt() { |
53 |
|
|
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
54 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
55 |
|
|
return (pLHS && pRHS) ? pLHS->evalInt() + pRHS->evalInt() : 0; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
void Add::dump(int level) { |
59 |
|
|
printIndents(level); |
60 |
|
|
printf("Add(\n"); |
61 |
|
|
lhs->dump(level+1); |
62 |
|
|
printIndents(level); |
63 |
|
|
printf(",\n"); |
64 |
|
|
rhs->dump(level+1); |
65 |
|
|
printIndents(level); |
66 |
|
|
printf(")\n"); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
int Sub::evalInt() { |
70 |
|
|
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
71 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
72 |
|
|
return (pLHS && pRHS) ? pLHS->evalInt() - pRHS->evalInt() : 0; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
void Sub::dump(int level) { |
76 |
|
|
printIndents(level); |
77 |
|
|
printf("Sub(\n"); |
78 |
|
|
lhs->dump(level+1); |
79 |
|
|
printIndents(level); |
80 |
|
|
printf(",\n"); |
81 |
|
|
rhs->dump(level+1); |
82 |
|
|
printIndents(level); |
83 |
|
|
printf(")\n"); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
int Mul::evalInt() { |
87 |
|
|
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
88 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
89 |
|
|
return (pLHS && pRHS) ? pLHS->evalInt() * pRHS->evalInt() : 0; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
void Mul::dump(int level) { |
93 |
|
|
printIndents(level); |
94 |
|
|
printf("Mul(\n"); |
95 |
|
|
lhs->dump(level+1); |
96 |
|
|
printIndents(level); |
97 |
|
|
printf(",\n"); |
98 |
|
|
rhs->dump(level+1); |
99 |
|
|
printIndents(level); |
100 |
|
|
printf(")\n"); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
int Div::evalInt() { |
104 |
|
|
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
105 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
106 |
|
|
return (pLHS && pRHS) ? pRHS->evalInt() == 0 ? 0 : pLHS->evalInt() / pRHS->evalInt() : 0; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
void Div::dump(int level) { |
110 |
|
|
printIndents(level); |
111 |
|
|
printf("Div(\n"); |
112 |
|
|
lhs->dump(level+1); |
113 |
|
|
printIndents(level); |
114 |
|
|
printf(",\n"); |
115 |
|
|
rhs->dump(level+1); |
116 |
|
|
printIndents(level); |
117 |
|
|
printf(")\n"); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
int Mod::evalInt() { |
121 |
|
|
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
122 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
123 |
|
|
return (pLHS && pRHS) ? pLHS->evalInt() % pRHS->evalInt() : 0; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
void Mod::dump(int level) { |
127 |
|
|
printIndents(level); |
128 |
|
|
printf("Mod(\n"); |
129 |
|
|
lhs->dump(level+1); |
130 |
|
|
printIndents(level); |
131 |
|
|
printf(",\n"); |
132 |
|
|
rhs->dump(level+1); |
133 |
|
|
printIndents(level); |
134 |
|
|
printf(")\n"); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
void Args::dump(int level) { |
138 |
|
|
printIndents(level); |
139 |
|
|
printf("Args(\n"); |
140 |
|
|
for (std::vector<ExpressionRef>::iterator it = args.begin() ; it != args.end() ; ++it) { |
141 |
|
|
(*it)->dump(level+1); |
142 |
|
|
} |
143 |
|
|
printIndents(level); |
144 |
|
|
printf(")\n"); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
EventHandlers::EventHandlers() { |
148 |
|
|
//printf("EventHandlers::Constructor 0x%lx\n", (long long)this); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
EventHandlers::~EventHandlers() { |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
void EventHandlers::add(EventHandlerRef arg) { |
155 |
|
|
args.push_back(arg); |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
void EventHandlers::dump(int level) { |
159 |
|
|
printIndents(level); |
160 |
|
|
printf("EventHandlers {\n"); |
161 |
|
|
for (std::vector<EventHandlerRef>::iterator it = args.begin() ; it != args.end() ; ++it) { |
162 |
|
|
(*it)->dump(level+1); |
163 |
|
|
} |
164 |
|
|
printIndents(level); |
165 |
|
|
printf("}\n"); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
EventHandler* EventHandlers::eventHandlerByName(const String& name) const { |
169 |
|
|
for (int i = 0; i < args.size(); ++i) |
170 |
|
|
if (args.at(i)->eventHandlerName() == name) |
171 |
|
|
return const_cast<EventHandler*>(&*args.at(i)); |
172 |
|
|
return NULL; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
EventHandler* EventHandlers::eventHandler(uint index) const { |
176 |
|
|
if (index >= args.size()) return NULL; |
177 |
|
|
return const_cast<EventHandler*>(&*args.at(index)); |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
Assignment::Assignment(VariableRef variable, ExpressionRef value) |
181 |
|
|
: variable(variable), value(value) |
182 |
|
|
{ |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
void Assignment::dump(int level) { |
186 |
|
|
printIndents(level); |
187 |
|
|
printf("Assignment\n"); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
StmtFlags_t Assignment::exec() { |
191 |
|
|
if (!variable) |
192 |
|
|
return StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
193 |
|
|
variable->assign(&*value); |
194 |
|
|
return STMT_SUCCESS; |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
void EventHandler::dump(int level) { |
198 |
|
|
printIndents(level); |
199 |
|
|
printf("EventHandler {\n"); |
200 |
|
|
statements->dump(level+1); |
201 |
|
|
printIndents(level); |
202 |
|
|
printf("}\n"); |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
void Statements::dump(int level) { |
206 |
|
|
printIndents(level); |
207 |
|
|
printf("Statements {\n"); |
208 |
|
|
for (std::vector<StatementRef>::iterator it = args.begin() ; it != args.end() ; ++it) { |
209 |
|
|
(*it)->dump(level+1); |
210 |
|
|
} |
211 |
|
|
printIndents(level); |
212 |
|
|
printf("}\n"); |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
Statement* Statements::statement(uint i) { |
216 |
|
|
if (i >= args.size()) return NULL; |
217 |
|
|
return &*args.at(i); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
void FunctionCall::dump(int level) { |
221 |
|
|
printIndents(level); |
222 |
|
|
printf("FunctionCall '%s' args={\n", functionName.c_str()); |
223 |
|
|
args->dump(level+1); |
224 |
|
|
printIndents(level); |
225 |
|
|
printf("}\n"); |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
ExprType_t FunctionCall::exprType() const { |
229 |
|
|
if (!fn) return EMPTY_EXPR; |
230 |
|
|
return fn->returnType(); |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
VMFnResult* FunctionCall::execVMFn() { |
234 |
|
|
if (!fn) return NULL; |
235 |
|
|
// assuming here that all argument checks (amount and types) have been made |
236 |
|
|
// at parse time, to avoid time intensive checks on each function call |
237 |
|
|
return fn->exec(dynamic_cast<VMFnArgs*>(&*args)); |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
StmtFlags_t FunctionCall::exec() { |
241 |
|
|
VMFnResult* result = execVMFn(); |
242 |
|
|
if (!result) |
243 |
|
|
return StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
244 |
|
|
return result->resultFlags(); |
245 |
|
|
} |
246 |
|
|
|
247 |
|
|
int FunctionCall::evalInt() { |
248 |
|
|
VMFnResult* result = execVMFn(); |
249 |
|
|
if (!result) return 0; |
250 |
|
|
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(result->resultValue()); |
251 |
|
|
if (!intExpr) return 0; |
252 |
|
|
return intExpr->evalInt(); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
String FunctionCall::evalStr() { |
256 |
|
|
VMFnResult* result = execVMFn(); |
257 |
|
|
if (!result) return ""; |
258 |
|
|
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(result->resultValue()); |
259 |
|
|
if (!strExpr) return ""; |
260 |
|
|
return strExpr->evalStr(); |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
String FunctionCall::evalCastToStr() { |
264 |
|
|
VMFnResult* result = execVMFn(); |
265 |
|
|
if (!result) return ""; |
266 |
|
|
if (result->resultValue()->exprType() == STRING_EXPR) { |
267 |
|
|
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(result->resultValue()); |
268 |
|
|
return strExpr ? strExpr->evalStr() : ""; |
269 |
|
|
} else { |
270 |
|
|
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(result->resultValue()); |
271 |
|
|
return intExpr ? ToString(intExpr->evalInt()) : ""; |
272 |
|
|
} |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
IntVariable::IntVariable(ParserContext* ctx) |
276 |
|
|
: Variable(ctx, ctx ? ctx->globalIntVarCount++ : 0, false), polyphonic(false) |
277 |
|
|
{ |
278 |
schoenebeck |
2588 |
//printf("globalIntVar parserctx=0x%lx memPOS=%d\n", ctx, memPos); |
279 |
|
|
assert(ctx); |
280 |
schoenebeck |
2581 |
} |
281 |
|
|
|
282 |
schoenebeck |
2588 |
inline static int postfixInc(int& object, int incBy) { |
283 |
|
|
const int i = object; |
284 |
|
|
object += incBy; |
285 |
|
|
return i; |
286 |
|
|
} |
287 |
|
|
|
288 |
schoenebeck |
2581 |
IntVariable::IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size) |
289 |
schoenebeck |
2588 |
: Variable(ctx, !ctx ? 0 : polyphonic ? postfixInc(ctx->polyphonicIntVarCount, size) : postfixInc(ctx->globalIntVarCount, size), bConst), |
290 |
schoenebeck |
2581 |
polyphonic(polyphonic) |
291 |
|
|
{ |
292 |
schoenebeck |
2588 |
//printf("InvVar size=%d parserCtx=0x%lx\n", size, (uint64_t)ctx); |
293 |
|
|
if (polyphonic) { |
294 |
|
|
//printf("polyIntVar memPOS=%d\n", memPos); |
295 |
|
|
assert(ctx); |
296 |
|
|
} |
297 |
schoenebeck |
2581 |
} |
298 |
|
|
|
299 |
|
|
void IntVariable::assign(Expression* expr) { |
300 |
|
|
IntExpr* intExpr = dynamic_cast<IntExpr*>(expr); |
301 |
|
|
if (intExpr) |
302 |
|
|
if (polyphonic) |
303 |
|
|
context->execContext->polyphonicIntMemory[memPos] = intExpr->evalInt(); |
304 |
|
|
else |
305 |
|
|
(*context->globalIntMemory)[memPos] = intExpr->evalInt(); |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
int IntVariable::evalInt() { |
309 |
|
|
//printf("IntVariable::eval pos=%d\n", memPos); |
310 |
schoenebeck |
2588 |
if (polyphonic) { |
311 |
|
|
//printf("evalInt() poly memPos=%d execCtx=0x%lx\n", memPos, (uint64_t)context->execContext); |
312 |
schoenebeck |
2581 |
return context->execContext->polyphonicIntMemory[memPos]; |
313 |
schoenebeck |
2588 |
} |
314 |
schoenebeck |
2581 |
return (*context->globalIntMemory)[memPos]; |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
void IntVariable::dump(int level) { |
318 |
|
|
printIndents(level); |
319 |
schoenebeck |
2588 |
//printf("IntVariable memPos=%d\n", memPos); |
320 |
schoenebeck |
2581 |
} |
321 |
|
|
|
322 |
|
|
//ConstIntVariable::ConstIntVariable(ParserContext* ctx, int value) |
323 |
|
|
ConstIntVariable::ConstIntVariable(int value) |
324 |
|
|
: IntVariable(NULL,false,true), value(value) |
325 |
|
|
{ |
326 |
|
|
} |
327 |
|
|
|
328 |
|
|
void ConstIntVariable::assign(Expression* expr) { |
329 |
|
|
// ignore assignment |
330 |
|
|
/* |
331 |
|
|
printf("ConstIntVariable::assign()\n"); |
332 |
|
|
IntExpr* intExpr = dynamic_cast<IntExpr*>(expr); |
333 |
|
|
if (intExpr) { |
334 |
|
|
value = intExpr->evalInt(); |
335 |
|
|
} |
336 |
|
|
*/ |
337 |
|
|
} |
338 |
|
|
|
339 |
|
|
int ConstIntVariable::evalInt() { |
340 |
|
|
return value; |
341 |
|
|
} |
342 |
|
|
|
343 |
|
|
void ConstIntVariable::dump(int level) { |
344 |
|
|
printIndents(level); |
345 |
|
|
printf("ConstIntVariable val=%d\n", value); |
346 |
|
|
} |
347 |
|
|
|
348 |
|
|
PolyphonicIntVariable::PolyphonicIntVariable(ParserContext* ctx) |
349 |
|
|
: IntVariable(ctx,true,false) |
350 |
|
|
{ |
351 |
|
|
} |
352 |
|
|
|
353 |
|
|
void PolyphonicIntVariable::dump(int level) { |
354 |
|
|
printIndents(level); |
355 |
|
|
printf("PolyphonicIntVariable\n"); |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
IntArrayVariable::IntArrayVariable(ParserContext* ctx, int size) |
359 |
|
|
: Variable(ctx, 0, false) |
360 |
|
|
{ |
361 |
|
|
values.resize(size); |
362 |
|
|
memset(&values[0], 0, size * sizeof(int)); |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
IntArrayVariable::IntArrayVariable(ParserContext* ctx, int size, ArgsRef values) |
366 |
|
|
: Variable(ctx, 0, false) |
367 |
|
|
{ |
368 |
|
|
this->values.resize(size); |
369 |
|
|
for (int i = 0; i < values->argsCount(); ++i) { |
370 |
|
|
VMIntExpr* expr = dynamic_cast<VMIntExpr*>(values->arg(i)); |
371 |
|
|
if (expr) this->values[i] = expr->evalInt(); |
372 |
|
|
} |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
int IntArrayVariable::evalIntElement(uint i) { |
376 |
|
|
if (i >= values.size()) return 0; |
377 |
|
|
return values[i]; |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
void IntArrayVariable::assignIntElement(uint i, int value) { |
381 |
|
|
if (i >= values.size()) return; |
382 |
|
|
values[i] = value; |
383 |
|
|
} |
384 |
|
|
|
385 |
|
|
void IntArrayVariable::dump(int level) { |
386 |
|
|
printIndents(level); |
387 |
|
|
printf("IntArray("); |
388 |
|
|
for (int i = 0; i < values.size(); ++i) { |
389 |
|
|
if (i % 12 == 0) { |
390 |
|
|
printf("\n"); |
391 |
|
|
printIndents(level+1); |
392 |
|
|
} |
393 |
|
|
printf("%d, ", values[i]); |
394 |
|
|
} |
395 |
|
|
printIndents(level); |
396 |
|
|
printf(")\n"); |
397 |
|
|
} |
398 |
|
|
|
399 |
|
|
IntArrayElement::IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex) |
400 |
|
|
: IntVariable(NULL, false, false, 0), array(array), index(arrayIndex) |
401 |
|
|
{ |
402 |
|
|
} |
403 |
|
|
|
404 |
|
|
void IntArrayElement::assign(Expression* expr) { |
405 |
|
|
IntExpr* valueExpr = dynamic_cast<IntExpr*>(expr); |
406 |
|
|
if (!valueExpr) return; |
407 |
|
|
int value = valueExpr->evalInt(); |
408 |
|
|
|
409 |
|
|
if (!index) return; |
410 |
|
|
int idx = index->evalInt(); |
411 |
|
|
if (idx < 0 || idx >= array->arraySize()) return; |
412 |
|
|
|
413 |
|
|
array->assignIntElement(idx, value); |
414 |
|
|
} |
415 |
|
|
|
416 |
|
|
int IntArrayElement::evalInt() { |
417 |
|
|
if (!index) return 0; |
418 |
|
|
int idx = index->evalInt(); |
419 |
|
|
if (idx < 0 || idx >= array->arraySize()) return 0; |
420 |
|
|
|
421 |
|
|
return array->evalIntElement(idx); |
422 |
|
|
} |
423 |
|
|
|
424 |
|
|
void IntArrayElement::dump(int level) { |
425 |
|
|
printIndents(level); |
426 |
|
|
printf("IntArrayElement\n"); |
427 |
|
|
} |
428 |
|
|
|
429 |
|
|
StringVariable::StringVariable(ParserContext* ctx) |
430 |
|
|
: Variable(ctx,ctx->globalStrVarCount++,false) |
431 |
|
|
{ |
432 |
|
|
} |
433 |
|
|
|
434 |
|
|
StringVariable::StringVariable(ParserContext* ctx, bool bConst) |
435 |
|
|
: Variable(ctx,0,bConst) |
436 |
|
|
{ |
437 |
|
|
} |
438 |
|
|
|
439 |
|
|
void StringVariable::assign(Expression* expr) { |
440 |
|
|
StringExpr* strExpr = dynamic_cast<StringExpr*>(expr); |
441 |
|
|
(*context->globalStrMemory)[memPos] = strExpr->evalStr(); |
442 |
|
|
} |
443 |
|
|
|
444 |
|
|
String StringVariable::evalStr() { |
445 |
|
|
//printf("StringVariable::eval pos=%d\n", memPos); |
446 |
|
|
return (*context->globalStrMemory)[memPos]; |
447 |
|
|
} |
448 |
|
|
|
449 |
|
|
void StringVariable::dump(int level) { |
450 |
|
|
printIndents(level); |
451 |
|
|
printf("StringVariable memPos=%d\n", memPos); |
452 |
|
|
} |
453 |
|
|
|
454 |
|
|
ConstStringVariable::ConstStringVariable(ParserContext* ctx, String _value) |
455 |
|
|
: StringVariable(ctx,true), value(_value) |
456 |
|
|
{ |
457 |
|
|
} |
458 |
|
|
|
459 |
|
|
void ConstStringVariable::assign(Expression* expr) { |
460 |
|
|
// ignore assignment |
461 |
|
|
// StringExpr* strExpr = dynamic_cast<StringExpr*>(expr); |
462 |
|
|
// if (strExpr) value = strExpr->evalStr(); |
463 |
|
|
} |
464 |
|
|
|
465 |
|
|
String ConstStringVariable::evalStr() { |
466 |
|
|
return value; |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
void ConstStringVariable::dump(int level) { |
470 |
|
|
printIndents(level); |
471 |
|
|
printf("ConstStringVariable val='%s'\n", value.c_str()); |
472 |
|
|
} |
473 |
|
|
|
474 |
|
|
void If::dump(int level) { |
475 |
|
|
printIndents(level); |
476 |
|
|
if (ifStatements && elseStatements) |
477 |
|
|
printf("if cond stmts1 else stmts2 end if\n"); |
478 |
|
|
else if (ifStatements) |
479 |
|
|
printf("if cond statements end if\n"); |
480 |
|
|
else |
481 |
|
|
printf("if [INVALID]\n"); |
482 |
|
|
} |
483 |
|
|
|
484 |
|
|
int If::evalBranch() { |
485 |
|
|
if (condition->evalInt()) return 0; |
486 |
|
|
if (elseStatements) return 1; |
487 |
|
|
return -1; |
488 |
|
|
} |
489 |
|
|
|
490 |
|
|
Statements* If::branch(uint i) const { |
491 |
|
|
if (i == 0) return (Statements*) &*ifStatements; |
492 |
|
|
if (i == 1) return (elseStatements) ? (Statements*) &*elseStatements : NULL; |
493 |
|
|
return NULL; |
494 |
|
|
} |
495 |
|
|
|
496 |
|
|
void SelectCase::dump(int level) { |
497 |
|
|
printIndents(level); |
498 |
|
|
if (select) |
499 |
|
|
if (select->isConstExpr()) |
500 |
|
|
printf("Case select %d\n", select->evalInt()); |
501 |
|
|
else |
502 |
|
|
printf("Case select [runtime expr]\n"); |
503 |
|
|
else |
504 |
|
|
printf("Case select NULL\n"); |
505 |
|
|
for (int i = 0; i < branches.size(); ++i) { |
506 |
|
|
printIndents(level+1); |
507 |
|
|
CaseBranch& branch = branches[i]; |
508 |
|
|
if (branch.from && branch.to) |
509 |
|
|
if (branch.from->isConstExpr() && branch.to->isConstExpr()) |
510 |
|
|
printf("case %d to %d\n", branch.from->evalInt(), branch.to->evalInt()); |
511 |
|
|
else if (branch.from->isConstExpr() && !branch.to->isConstExpr()) |
512 |
|
|
printf("case %d to [runtime expr]\n", branch.from->evalInt()); |
513 |
|
|
else if (!branch.from->isConstExpr() && branch.to->isConstExpr()) |
514 |
|
|
printf("case [runtime expr] to %d\n", branch.to->evalInt()); |
515 |
|
|
else |
516 |
|
|
printf("case [runtime expr] to [runtime expr]\n"); |
517 |
|
|
else if (branch.from) |
518 |
|
|
if (branch.from->isConstExpr()) |
519 |
|
|
printf("case %d\n", branch.from->evalInt()); |
520 |
|
|
else |
521 |
|
|
printf("case [runtime expr]\n"); |
522 |
|
|
else |
523 |
|
|
printf("case NULL\n"); |
524 |
|
|
} |
525 |
|
|
} |
526 |
|
|
|
527 |
|
|
int SelectCase::evalBranch() { |
528 |
|
|
int value = select->evalInt(); |
529 |
|
|
for (int i = 0; i < branches.size(); ++i) { |
530 |
|
|
if (branches.at(i).from && branches.at(i).to) { // i.e. "case 4 to 7" ... |
531 |
|
|
if (branches.at(i).from->evalInt() <= value && |
532 |
|
|
branches.at(i).to->evalInt() >= value) return i; |
533 |
|
|
} else { // i.e. "case 5" ... |
534 |
|
|
if (branches.at(i).from->evalInt() == value) return i; |
535 |
|
|
} |
536 |
|
|
} |
537 |
|
|
return -1; |
538 |
|
|
} |
539 |
|
|
|
540 |
|
|
Statements* SelectCase::branch(uint i) const { |
541 |
|
|
if (i < branches.size()) |
542 |
|
|
return const_cast<Statements*>( &*branches[i].statements ); |
543 |
|
|
return NULL; |
544 |
|
|
} |
545 |
|
|
|
546 |
|
|
// void Case::addBranch(IntExprRef condition, StatementsRef statements) { |
547 |
|
|
// CaseBranchRef b = new CaseBranchRef; |
548 |
|
|
// b->from = condition; |
549 |
|
|
// b->statements = statements; |
550 |
|
|
// branches.push_back(b); |
551 |
|
|
// } |
552 |
|
|
// |
553 |
|
|
// void Case::addBranch(IntExprRef from, IntExprRef to, StatementsRef statements) { |
554 |
|
|
// CaseBranchRef b = new CaseBranchRef; |
555 |
|
|
// b->from = from; |
556 |
|
|
// b->to = to; |
557 |
|
|
// b->statements = statements; |
558 |
|
|
// branches.push_back(b); |
559 |
|
|
// } |
560 |
|
|
// |
561 |
|
|
// void Case::addBranch(CaseBranchRef branch) { |
562 |
|
|
// branches.push_back(branch); |
563 |
|
|
// } |
564 |
|
|
|
565 |
|
|
void While::dump(int level) { |
566 |
|
|
printIndents(level); |
567 |
|
|
if (m_condition) |
568 |
|
|
if (m_condition->isConstExpr()) |
569 |
|
|
printf("while (%d) {\n", m_condition->evalInt()); |
570 |
|
|
else |
571 |
|
|
printf("while ([runtime expr]) {\n"); |
572 |
|
|
else |
573 |
|
|
printf("while ([INVALID]) {\n"); |
574 |
|
|
m_statements->dump(level+1); |
575 |
|
|
printIndents(level); |
576 |
|
|
printf("}\n"); |
577 |
|
|
} |
578 |
|
|
|
579 |
|
|
Statements* While::statements() const { |
580 |
|
|
return (m_statements) ? const_cast<Statements*>( &*m_statements ) : NULL; |
581 |
|
|
} |
582 |
|
|
|
583 |
|
|
bool While::evalLoopStartCondition() { |
584 |
|
|
if (!m_condition) return false; |
585 |
|
|
return m_condition->evalInt(); |
586 |
|
|
} |
587 |
|
|
|
588 |
|
|
void Neg::dump(int level) { |
589 |
|
|
printIndents(level); |
590 |
|
|
printf("Negative Expr\n"); |
591 |
|
|
} |
592 |
|
|
|
593 |
|
|
String ConcatString::evalStr() { |
594 |
|
|
return lhs->evalCastToStr() + rhs->evalCastToStr(); |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
void ConcatString::dump(int level) { |
598 |
|
|
printIndents(level); |
599 |
|
|
printf("ConcatString(\n"); |
600 |
|
|
lhs->dump(level+1); |
601 |
|
|
printIndents(level); |
602 |
|
|
printf(",\n"); |
603 |
|
|
rhs->dump(level+1); |
604 |
|
|
printIndents(level); |
605 |
|
|
printf(")"); |
606 |
|
|
} |
607 |
|
|
|
608 |
|
|
bool ConcatString::isConstExpr() const { |
609 |
|
|
return lhs->isConstExpr() && rhs->isConstExpr(); |
610 |
|
|
} |
611 |
|
|
|
612 |
|
|
int Relation::evalInt() { |
613 |
|
|
switch (type) { |
614 |
|
|
case LESS_THAN: |
615 |
|
|
return lhs->evalInt() < rhs->evalInt(); |
616 |
|
|
case GREATER_THAN: |
617 |
|
|
return lhs->evalInt() > rhs->evalInt(); |
618 |
|
|
case LESS_OR_EQUAL: |
619 |
|
|
return lhs->evalInt() <= rhs->evalInt(); |
620 |
|
|
case GREATER_OR_EQUAL: |
621 |
|
|
return lhs->evalInt() >= rhs->evalInt(); |
622 |
|
|
case EQUAL: |
623 |
|
|
if (lhs->exprType() == STRING_EXPR || rhs->exprType() == STRING_EXPR) |
624 |
|
|
return lhs->evalCastToStr() == rhs->evalCastToStr(); |
625 |
|
|
else |
626 |
|
|
return lhs->evalInt() == rhs->evalInt(); |
627 |
|
|
case NOT_EQUAL: |
628 |
|
|
if (lhs->exprType() == STRING_EXPR || rhs->exprType() == STRING_EXPR) |
629 |
|
|
return lhs->evalCastToStr() != rhs->evalCastToStr(); |
630 |
|
|
else |
631 |
|
|
return lhs->evalInt() != rhs->evalInt(); |
632 |
|
|
} |
633 |
|
|
return 0; |
634 |
|
|
} |
635 |
|
|
|
636 |
|
|
void Relation::dump(int level) { |
637 |
|
|
printIndents(level); |
638 |
|
|
printf("Relation(\n"); |
639 |
|
|
lhs->dump(level+1); |
640 |
|
|
printIndents(level); |
641 |
|
|
switch (type) { |
642 |
|
|
case LESS_THAN: |
643 |
|
|
printf("LESS_THAN\n"); |
644 |
|
|
break; |
645 |
|
|
case GREATER_THAN: |
646 |
|
|
printf("GREATER_THAN\n"); |
647 |
|
|
break; |
648 |
|
|
case LESS_OR_EQUAL: |
649 |
|
|
printf("LESS_OR_EQUAL\n"); |
650 |
|
|
break; |
651 |
|
|
case GREATER_OR_EQUAL: |
652 |
|
|
printf("GREATER_OR_EQUAL\n"); |
653 |
|
|
break; |
654 |
|
|
case EQUAL: |
655 |
|
|
printf("EQUAL\n"); |
656 |
|
|
break; |
657 |
|
|
case NOT_EQUAL: |
658 |
|
|
printf("NOT_EQUAL\n"); |
659 |
|
|
break; |
660 |
|
|
} |
661 |
|
|
rhs->dump(level+1); |
662 |
|
|
printIndents(level); |
663 |
|
|
printf(")\n"); |
664 |
|
|
} |
665 |
|
|
|
666 |
|
|
bool Relation::isConstExpr() const { |
667 |
|
|
return lhs->isConstExpr() && rhs->isConstExpr(); |
668 |
|
|
} |
669 |
|
|
|
670 |
|
|
int Or::evalInt() { |
671 |
|
|
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
672 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
673 |
|
|
return pLHS->evalInt() || pRHS->evalInt(); |
674 |
|
|
} |
675 |
|
|
|
676 |
|
|
void Or::dump(int level) { |
677 |
|
|
printIndents(level); |
678 |
|
|
printf("Or(\n"); |
679 |
|
|
lhs->dump(level+1); |
680 |
|
|
printIndents(level); |
681 |
|
|
printf(",\n"); |
682 |
|
|
rhs->dump(level+1); |
683 |
|
|
printIndents(level); |
684 |
|
|
printf(")\n"); |
685 |
|
|
} |
686 |
|
|
|
687 |
|
|
int And::evalInt() { |
688 |
|
|
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
689 |
|
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
690 |
|
|
return pLHS->evalInt() && pRHS->evalInt(); |
691 |
|
|
} |
692 |
|
|
|
693 |
|
|
void And::dump(int level) { |
694 |
|
|
printIndents(level); |
695 |
|
|
printf("And(\n"); |
696 |
|
|
lhs->dump(level+1); |
697 |
|
|
printIndents(level); |
698 |
|
|
printf(",\n"); |
699 |
|
|
rhs->dump(level+1); |
700 |
|
|
printIndents(level); |
701 |
|
|
printf(")\n"); |
702 |
|
|
} |
703 |
|
|
|
704 |
|
|
void Not::dump(int level) { |
705 |
|
|
printIndents(level); |
706 |
|
|
printf("Not(\n"); |
707 |
|
|
expr->dump(level+1); |
708 |
|
|
printIndents(level); |
709 |
|
|
printf(")\n"); |
710 |
|
|
} |
711 |
|
|
|
712 |
|
|
VariableRef ParserContext::variableByName(const String& name) { |
713 |
|
|
if (!vartable.count(name)) { |
714 |
|
|
return VariableRef(); |
715 |
|
|
} |
716 |
|
|
return vartable.find(name)->second; |
717 |
|
|
} |
718 |
|
|
|
719 |
|
|
VariableRef ParserContext::globalVar(const String& name) { |
720 |
|
|
if (!vartable.count(name)) { |
721 |
|
|
//printf("No global var '%s'\n", name.c_str()); |
722 |
|
|
//for (std::map<String,VariableRef>::const_iterator it = vartable.begin(); it != vartable.end(); ++it) |
723 |
|
|
// printf("-> var '%s'\n", it->first.c_str()); |
724 |
|
|
return VariableRef(); |
725 |
|
|
} |
726 |
|
|
return vartable.find(name)->second; |
727 |
|
|
} |
728 |
|
|
|
729 |
|
|
IntVariableRef ParserContext::globalIntVar(const String& name) { |
730 |
|
|
return globalVar(name); |
731 |
|
|
} |
732 |
|
|
|
733 |
|
|
StringVariableRef ParserContext::globalStrVar(const String& name) { |
734 |
|
|
return globalVar(name); |
735 |
|
|
} |
736 |
|
|
|
737 |
schoenebeck |
2588 |
ParserContext::~ParserContext() { |
738 |
|
|
destroyScanner(); |
739 |
|
|
if (globalIntMemory) { |
740 |
|
|
delete globalIntMemory; |
741 |
|
|
globalIntMemory = NULL; |
742 |
|
|
} |
743 |
|
|
} |
744 |
|
|
|
745 |
schoenebeck |
2581 |
void ParserContext::addErr(int line, const char* txt) { |
746 |
|
|
ParserIssue e; |
747 |
|
|
e.type = PARSER_ERROR; |
748 |
|
|
e.txt = txt; |
749 |
|
|
e.line = line; |
750 |
schoenebeck |
2588 |
vErrors.push_back(e); |
751 |
|
|
vIssues.push_back(e); |
752 |
schoenebeck |
2581 |
} |
753 |
|
|
|
754 |
|
|
void ParserContext::addWrn(int line, const char* txt) { |
755 |
|
|
ParserIssue w; |
756 |
|
|
w.type = PARSER_WARNING; |
757 |
|
|
w.txt = txt; |
758 |
|
|
w.line = line; |
759 |
schoenebeck |
2588 |
vWarnings.push_back(w); |
760 |
|
|
vIssues.push_back(w); |
761 |
schoenebeck |
2581 |
} |
762 |
|
|
|
763 |
|
|
bool ParserContext::setPreprocessorCondition(const char* name) { |
764 |
|
|
if (builtinPreprocessorConditions.count(name)) return false; |
765 |
|
|
if (userPreprocessorConditions.count(name)) return false; |
766 |
|
|
userPreprocessorConditions.insert(name); |
767 |
|
|
return true; |
768 |
|
|
} |
769 |
|
|
|
770 |
|
|
bool ParserContext::resetPreprocessorCondition(const char* name) { |
771 |
|
|
if (builtinPreprocessorConditions.count(name)) return false; |
772 |
|
|
if (!userPreprocessorConditions.count(name)) return false; |
773 |
|
|
userPreprocessorConditions.erase(name); |
774 |
|
|
return true; |
775 |
|
|
} |
776 |
|
|
|
777 |
|
|
bool ParserContext::isPreprocessorConditionSet(const char* name) { |
778 |
|
|
if (builtinPreprocessorConditions.count(name)) return true; |
779 |
|
|
return userPreprocessorConditions.count(name); |
780 |
|
|
} |
781 |
|
|
|
782 |
schoenebeck |
2588 |
std::vector<ParserIssue> ParserContext::issues() const { |
783 |
|
|
return vIssues; |
784 |
|
|
} |
785 |
|
|
|
786 |
|
|
std::vector<ParserIssue> ParserContext::errors() const { |
787 |
|
|
return vErrors; |
788 |
|
|
} |
789 |
|
|
|
790 |
|
|
std::vector<ParserIssue> ParserContext::warnings() const { |
791 |
|
|
return vWarnings; |
792 |
|
|
} |
793 |
|
|
|
794 |
|
|
VMEventHandler* ParserContext::eventHandler(uint index) { |
795 |
|
|
if (!handlers) return NULL; |
796 |
|
|
return handlers->eventHandler(index); |
797 |
|
|
} |
798 |
|
|
|
799 |
|
|
VMEventHandler* ParserContext::eventHandlerByName(const String& name) { |
800 |
|
|
if (!handlers) return NULL; |
801 |
|
|
return handlers->eventHandlerByName(name); |
802 |
|
|
} |
803 |
|
|
|
804 |
schoenebeck |
2581 |
} // namespace LinuxSampler |