/[svn]/linuxsampler/trunk/src/scriptvm/tree.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/tree.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2945 - (show annotations) (download)
Thu Jul 14 00:22:26 2016 UTC (2 years, 11 months ago) by schoenebeck
File size: 27349 byte(s)
* NKSP: Implemented built-in script function "inc()".
* NKSP: Implemented built-in script function "dec()".
* NKSP language fix: division expressions were evaluated too often.
* NKSP language fix: string concatenation operator was right
  associative instead of left (to right).
* Bumped version (2.0.0.svn15).

1 /*
2 * Copyright (c) 2014 - 2016 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 #include <assert.h>
15
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 if (!pLHS || !pRHS) return 0;
107 int l = pLHS->evalInt();
108 int r = pRHS->evalInt();
109 if (r == 0) return 0;
110 return l / r;
111 }
112
113 void Div::dump(int level) {
114 printIndents(level);
115 printf("Div(\n");
116 lhs->dump(level+1);
117 printIndents(level);
118 printf(",\n");
119 rhs->dump(level+1);
120 printIndents(level);
121 printf(")\n");
122 }
123
124 int Mod::evalInt() {
125 IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs);
126 IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);;
127 return (pLHS && pRHS) ? pLHS->evalInt() % pRHS->evalInt() : 0;
128 }
129
130 void Mod::dump(int level) {
131 printIndents(level);
132 printf("Mod(\n");
133 lhs->dump(level+1);
134 printIndents(level);
135 printf(",\n");
136 rhs->dump(level+1);
137 printIndents(level);
138 printf(")\n");
139 }
140
141 void Args::dump(int level) {
142 printIndents(level);
143 printf("Args(\n");
144 for (std::vector<ExpressionRef>::iterator it = args.begin() ; it != args.end() ; ++it) {
145 (*it)->dump(level+1);
146 }
147 printIndents(level);
148 printf(")\n");
149 }
150
151 bool Args::isPolyphonic() const {
152 for (int i = 0; i < args.size(); ++i)
153 if (args[i]->isPolyphonic())
154 return true;
155 return false;
156 }
157
158 EventHandlers::EventHandlers() {
159 //printf("EventHandlers::Constructor 0x%lx\n", (long long)this);
160 }
161
162 EventHandlers::~EventHandlers() {
163 }
164
165 void EventHandlers::add(EventHandlerRef arg) {
166 args.push_back(arg);
167 }
168
169 void EventHandlers::dump(int level) {
170 printIndents(level);
171 printf("EventHandlers {\n");
172 for (std::vector<EventHandlerRef>::iterator it = args.begin() ; it != args.end() ; ++it) {
173 (*it)->dump(level+1);
174 }
175 printIndents(level);
176 printf("}\n");
177 }
178
179 EventHandler* EventHandlers::eventHandlerByName(const String& name) const {
180 for (int i = 0; i < args.size(); ++i)
181 if (args.at(i)->eventHandlerName() == name)
182 return const_cast<EventHandler*>(&*args.at(i));
183 return NULL;
184 }
185
186 EventHandler* EventHandlers::eventHandler(uint index) const {
187 if (index >= args.size()) return NULL;
188 return const_cast<EventHandler*>(&*args.at(index));
189 }
190
191 bool EventHandlers::isPolyphonic() const {
192 for (int i = 0; i < args.size(); ++i)
193 if (args[i]->isPolyphonic())
194 return true;
195 return false;
196 }
197
198 Assignment::Assignment(VariableRef variable, ExpressionRef value)
199 : variable(variable), value(value)
200 {
201 }
202
203 void Assignment::dump(int level) {
204 printIndents(level);
205 printf("Assignment\n");
206 }
207
208 StmtFlags_t Assignment::exec() {
209 if (!variable)
210 return StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
211 variable->assign(&*value);
212 return STMT_SUCCESS;
213 }
214
215 EventHandler::EventHandler(StatementsRef statements) {
216 this->statements = statements;
217 usingPolyphonics = statements->isPolyphonic();
218 }
219
220 void EventHandler::dump(int level) {
221 printIndents(level);
222 printf("EventHandler {\n");
223 statements->dump(level+1);
224 printIndents(level);
225 printf("}\n");
226 }
227
228 void Statements::dump(int level) {
229 printIndents(level);
230 printf("Statements {\n");
231 for (std::vector<StatementRef>::iterator it = args.begin() ; it != args.end() ; ++it) {
232 (*it)->dump(level+1);
233 }
234 printIndents(level);
235 printf("}\n");
236 }
237
238 Statement* Statements::statement(uint i) {
239 if (i >= args.size()) return NULL;
240 return &*args.at(i);
241 }
242
243 bool Statements::isPolyphonic() const {
244 for (int i = 0; i < args.size(); ++i)
245 if (args[i]->isPolyphonic())
246 return true;
247 return false;
248 }
249
250 DynamicVariableCall::DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v)
251 : Variable(ctx, 0, false), dynVar(v), varName(name)
252 {
253 }
254
255 int DynamicVariableCall::evalInt() {
256 VMIntExpr* expr = dynamic_cast<VMIntExpr*>(dynVar);
257 if (!expr) return 0;
258 return expr->evalInt();
259 }
260
261 String DynamicVariableCall::evalStr() {
262 VMStringExpr* expr = dynamic_cast<VMStringExpr*>(dynVar);
263 if (!expr) return "";
264 return expr->evalStr();
265 }
266
267 String DynamicVariableCall::evalCastToStr() {
268 if (dynVar->exprType() == STRING_EXPR) {
269 return evalStr();
270 } else {
271 VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(dynVar);
272 return intExpr ? ToString(intExpr->evalInt()) : "";
273 }
274 }
275
276 void DynamicVariableCall::dump(int level) {
277 printIndents(level);
278 printf("Dynamic Variable '%s'\n", varName.c_str());
279 }
280
281 void FunctionCall::dump(int level) {
282 printIndents(level);
283 printf("FunctionCall '%s' args={\n", functionName.c_str());
284 args->dump(level+1);
285 printIndents(level);
286 printf("}\n");
287 }
288
289 ExprType_t FunctionCall::exprType() const {
290 if (!fn) return EMPTY_EXPR;
291 return fn->returnType();
292 }
293
294 VMFnResult* FunctionCall::execVMFn() {
295 if (!fn) return NULL;
296 // assuming here that all argument checks (amount and types) have been made
297 // at parse time, to avoid time intensive checks on each function call
298 return fn->exec(dynamic_cast<VMFnArgs*>(&*args));
299 }
300
301 StmtFlags_t FunctionCall::exec() {
302 VMFnResult* result = execVMFn();
303 if (!result)
304 return StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
305 return result->resultFlags();
306 }
307
308 int FunctionCall::evalInt() {
309 VMFnResult* result = execVMFn();
310 if (!result) return 0;
311 VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(result->resultValue());
312 if (!intExpr) return 0;
313 return intExpr->evalInt();
314 }
315
316 String FunctionCall::evalStr() {
317 VMFnResult* result = execVMFn();
318 if (!result) return "";
319 VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(result->resultValue());
320 if (!strExpr) return "";
321 return strExpr->evalStr();
322 }
323
324 String FunctionCall::evalCastToStr() {
325 VMFnResult* result = execVMFn();
326 if (!result) return "";
327 if (result->resultValue()->exprType() == STRING_EXPR) {
328 VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(result->resultValue());
329 return strExpr ? strExpr->evalStr() : "";
330 } else {
331 VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(result->resultValue());
332 return intExpr ? ToString(intExpr->evalInt()) : "";
333 }
334 }
335
336 IntVariable::IntVariable(ParserContext* ctx)
337 : Variable(ctx, ctx ? ctx->globalIntVarCount++ : 0, false), polyphonic(false)
338 {
339 //printf("globalIntVar parserctx=0x%lx memPOS=%d\n", ctx, memPos);
340 assert(ctx);
341 }
342
343 inline static int postfixInc(int& object, int incBy) {
344 const int i = object;
345 object += incBy;
346 return i;
347 }
348
349 IntVariable::IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size)
350 : Variable(ctx, !ctx ? 0 : polyphonic ? postfixInc(ctx->polyphonicIntVarCount, size) : postfixInc(ctx->globalIntVarCount, size), bConst),
351 polyphonic(polyphonic)
352 {
353 //printf("InvVar size=%d parserCtx=0x%lx\n", size, (uint64_t)ctx);
354 if (polyphonic) {
355 //printf("polyIntVar memPOS=%d\n", memPos);
356 assert(ctx);
357 }
358 }
359
360 void IntVariable::assign(Expression* expr) {
361 IntExpr* intExpr = dynamic_cast<IntExpr*>(expr);
362 if (intExpr)
363 if (polyphonic)
364 context->execContext->polyphonicIntMemory[memPos] = intExpr->evalInt();
365 else
366 (*context->globalIntMemory)[memPos] = intExpr->evalInt();
367 }
368
369 int IntVariable::evalInt() {
370 //printf("IntVariable::eval pos=%d\n", memPos);
371 if (polyphonic) {
372 //printf("evalInt() poly memPos=%d execCtx=0x%lx\n", memPos, (uint64_t)context->execContext);
373 return context->execContext->polyphonicIntMemory[memPos];
374 }
375 return (*context->globalIntMemory)[memPos];
376 }
377
378 void IntVariable::dump(int level) {
379 printIndents(level);
380 printf("IntVariable\n");
381 //printf("IntVariable memPos=%d\n", memPos);
382 }
383
384 //ConstIntVariable::ConstIntVariable(ParserContext* ctx, int value)
385 ConstIntVariable::ConstIntVariable(int value)
386 : IntVariable(NULL,false,true), value(value)
387 {
388 }
389
390 void ConstIntVariable::assign(Expression* expr) {
391 // ignore assignment
392 /*
393 printf("ConstIntVariable::assign()\n");
394 IntExpr* intExpr = dynamic_cast<IntExpr*>(expr);
395 if (intExpr) {
396 value = intExpr->evalInt();
397 }
398 */
399 }
400
401 int ConstIntVariable::evalInt() {
402 return value;
403 }
404
405 void ConstIntVariable::dump(int level) {
406 printIndents(level);
407 printf("ConstIntVariable val=%d\n", value);
408 }
409
410 BuiltInIntVariable::BuiltInIntVariable(const String& name, VMIntRelPtr* ptr)
411 : IntVariable(NULL,false,false), name(name), ptr(ptr)
412 {
413 }
414
415 void BuiltInIntVariable::assign(Expression* expr) {
416 IntExpr* valueExpr = dynamic_cast<IntExpr*>(expr);
417 if (!valueExpr) return;
418 ptr->assign(valueExpr->evalInt());
419 }
420
421 int BuiltInIntVariable::evalInt() {
422 return ptr->evalInt();
423 }
424
425 void BuiltInIntVariable::dump(int level) {
426 printIndents(level);
427 printf("Built-in IntVar '%s'\n", name.c_str());
428 }
429
430 PolyphonicIntVariable::PolyphonicIntVariable(ParserContext* ctx)
431 : IntVariable(ctx,true,false)
432 {
433 }
434
435 void PolyphonicIntVariable::dump(int level) {
436 printIndents(level);
437 printf("PolyphonicIntVariable\n");
438 }
439
440 IntArrayVariable::IntArrayVariable(ParserContext* ctx, int size)
441 : Variable(ctx, 0, false)
442 {
443 values.resize(size);
444 memset(&values[0], 0, size * sizeof(int));
445 }
446
447 IntArrayVariable::IntArrayVariable(ParserContext* ctx, int size, ArgsRef values)
448 : Variable(ctx, 0, false)
449 {
450 this->values.resize(size);
451 for (int i = 0; i < values->argsCount(); ++i) {
452 VMIntExpr* expr = dynamic_cast<VMIntExpr*>(values->arg(i));
453 if (expr) this->values[i] = expr->evalInt();
454 }
455 }
456
457 IntArrayVariable::IntArrayVariable(ParserContext* ctx, bool bConst)
458 : Variable(ctx, 0, bConst)
459 {
460 }
461
462 int IntArrayVariable::evalIntElement(uint i) {
463 if (i >= values.size()) return 0;
464 return values[i];
465 }
466
467 void IntArrayVariable::assignIntElement(uint i, int value) {
468 if (i >= values.size()) return;
469 values[i] = value;
470 }
471
472 void IntArrayVariable::dump(int level) {
473 printIndents(level);
474 printf("IntArray(");
475 for (int i = 0; i < values.size(); ++i) {
476 if (i % 12 == 0) {
477 printf("\n");
478 printIndents(level+1);
479 }
480 printf("%d, ", values[i]);
481 }
482 printIndents(level);
483 printf(")\n");
484 }
485
486 BuiltInIntArrayVariable::BuiltInIntArrayVariable(const String& name, VMInt8Array* array)
487 : IntArrayVariable(NULL, false), name(name), array(array)
488 {
489 }
490
491 int BuiltInIntArrayVariable::evalIntElement(uint i) {
492 return i >= array->size ? 0 : array->data[i];
493 }
494
495 void BuiltInIntArrayVariable::assignIntElement(uint i, int value) {
496 if (i >= array->size) return;
497 array->data[i] = value;
498 }
499
500 void BuiltInIntArrayVariable::dump(int level) {
501 printIndents(level);
502 printf("Built-In Int Array Variable '%s'\n", name.c_str());
503 }
504
505 IntArrayElement::IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex)
506 : IntVariable(NULL, false, false, 0), array(array), index(arrayIndex)
507 {
508 }
509
510 void IntArrayElement::assign(Expression* expr) {
511 IntExpr* valueExpr = dynamic_cast<IntExpr*>(expr);
512 if (!valueExpr) return;
513 int value = valueExpr->evalInt();
514
515 if (!index) return;
516 int idx = index->evalInt();
517 if (idx < 0 || idx >= array->arraySize()) return;
518
519 array->assignIntElement(idx, value);
520 }
521
522 int IntArrayElement::evalInt() {
523 if (!index) return 0;
524 int idx = index->evalInt();
525 if (idx < 0 || idx >= array->arraySize()) return 0;
526
527 return array->evalIntElement(idx);
528 }
529
530 void IntArrayElement::dump(int level) {
531 printIndents(level);
532 printf("IntArrayElement\n");
533 }
534
535 StringVariable::StringVariable(ParserContext* ctx)
536 : Variable(ctx,ctx->globalStrVarCount++,false)
537 {
538 }
539
540 StringVariable::StringVariable(ParserContext* ctx, bool bConst)
541 : Variable(ctx,0,bConst)
542 {
543 }
544
545 void StringVariable::assign(Expression* expr) {
546 StringExpr* strExpr = dynamic_cast<StringExpr*>(expr);
547 (*context->globalStrMemory)[memPos] = strExpr->evalStr();
548 }
549
550 String StringVariable::evalStr() {
551 //printf("StringVariable::eval pos=%d\n", memPos);
552 return (*context->globalStrMemory)[memPos];
553 }
554
555 void StringVariable::dump(int level) {
556 printIndents(level);
557 printf("StringVariable memPos=%d\n", memPos);
558 }
559
560 ConstStringVariable::ConstStringVariable(ParserContext* ctx, String _value)
561 : StringVariable(ctx,true), value(_value)
562 {
563 }
564
565 void ConstStringVariable::assign(Expression* expr) {
566 // ignore assignment
567 // StringExpr* strExpr = dynamic_cast<StringExpr*>(expr);
568 // if (strExpr) value = strExpr->evalStr();
569 }
570
571 String ConstStringVariable::evalStr() {
572 return value;
573 }
574
575 void ConstStringVariable::dump(int level) {
576 printIndents(level);
577 printf("ConstStringVariable val='%s'\n", value.c_str());
578 }
579
580 void If::dump(int level) {
581 printIndents(level);
582 if (ifStatements && elseStatements)
583 printf("if cond stmts1 else stmts2 end if\n");
584 else if (ifStatements)
585 printf("if cond statements end if\n");
586 else
587 printf("if [INVALID]\n");
588 }
589
590 int If::evalBranch() {
591 if (condition->evalInt()) return 0;
592 if (elseStatements) return 1;
593 return -1;
594 }
595
596 Statements* If::branch(uint i) const {
597 if (i == 0) return (Statements*) &*ifStatements;
598 if (i == 1) return (elseStatements) ? (Statements*) &*elseStatements : NULL;
599 return NULL;
600 }
601
602 bool If::isPolyphonic() const {
603 if (condition->isPolyphonic() || ifStatements->isPolyphonic())
604 return true;
605 return elseStatements ? elseStatements->isPolyphonic() : false;
606 }
607
608 void SelectCase::dump(int level) {
609 printIndents(level);
610 if (select)
611 if (select->isConstExpr())
612 printf("Case select %d\n", select->evalInt());
613 else
614 printf("Case select [runtime expr]\n");
615 else
616 printf("Case select NULL\n");
617 for (int i = 0; i < branches.size(); ++i) {
618 printIndents(level+1);
619 CaseBranch& branch = branches[i];
620 if (branch.from && branch.to)
621 if (branch.from->isConstExpr() && branch.to->isConstExpr())
622 printf("case %d to %d\n", branch.from->evalInt(), branch.to->evalInt());
623 else if (branch.from->isConstExpr() && !branch.to->isConstExpr())
624 printf("case %d to [runtime expr]\n", branch.from->evalInt());
625 else if (!branch.from->isConstExpr() && branch.to->isConstExpr())
626 printf("case [runtime expr] to %d\n", branch.to->evalInt());
627 else
628 printf("case [runtime expr] to [runtime expr]\n");
629 else if (branch.from)
630 if (branch.from->isConstExpr())
631 printf("case %d\n", branch.from->evalInt());
632 else
633 printf("case [runtime expr]\n");
634 else
635 printf("case NULL\n");
636 }
637 }
638
639 int SelectCase::evalBranch() {
640 int value = select->evalInt();
641 for (int i = 0; i < branches.size(); ++i) {
642 if (branches.at(i).from && branches.at(i).to) { // i.e. "case 4 to 7" ...
643 if (branches.at(i).from->evalInt() <= value &&
644 branches.at(i).to->evalInt() >= value) return i;
645 } else { // i.e. "case 5" ...
646 if (branches.at(i).from->evalInt() == value) return i;
647 }
648 }
649 return -1;
650 }
651
652 Statements* SelectCase::branch(uint i) const {
653 if (i < branches.size())
654 return const_cast<Statements*>( &*branches[i].statements );
655 return NULL;
656 }
657
658 bool SelectCase::isPolyphonic() const {
659 if (select->isPolyphonic()) return true;
660 for (int i = 0; i < branches.size(); ++i)
661 if (branches[i].statements->isPolyphonic())
662 return true;
663 return false;
664 }
665
666 // void Case::addBranch(IntExprRef condition, StatementsRef statements) {
667 // CaseBranchRef b = new CaseBranchRef;
668 // b->from = condition;
669 // b->statements = statements;
670 // branches.push_back(b);
671 // }
672 //
673 // void Case::addBranch(IntExprRef from, IntExprRef to, StatementsRef statements) {
674 // CaseBranchRef b = new CaseBranchRef;
675 // b->from = from;
676 // b->to = to;
677 // b->statements = statements;
678 // branches.push_back(b);
679 // }
680 //
681 // void Case::addBranch(CaseBranchRef branch) {
682 // branches.push_back(branch);
683 // }
684
685 void While::dump(int level) {
686 printIndents(level);
687 if (m_condition)
688 if (m_condition->isConstExpr())
689 printf("while (%d) {\n", m_condition->evalInt());
690 else
691 printf("while ([runtime expr]) {\n");
692 else
693 printf("while ([INVALID]) {\n");
694 m_statements->dump(level+1);
695 printIndents(level);
696 printf("}\n");
697 }
698
699 Statements* While::statements() const {
700 return (m_statements) ? const_cast<Statements*>( &*m_statements ) : NULL;
701 }
702
703 bool While::evalLoopStartCondition() {
704 if (!m_condition) return false;
705 return m_condition->evalInt();
706 }
707
708 void Neg::dump(int level) {
709 printIndents(level);
710 printf("Negative Expr\n");
711 }
712
713 String ConcatString::evalStr() {
714 // temporaries required here to enforce the associative left (to right) order
715 String l = lhs->evalCastToStr();
716 String r = rhs->evalCastToStr();
717 return l + r;
718 }
719
720 void ConcatString::dump(int level) {
721 printIndents(level);
722 printf("ConcatString(\n");
723 lhs->dump(level+1);
724 printIndents(level);
725 printf(",\n");
726 rhs->dump(level+1);
727 printIndents(level);
728 printf(")");
729 }
730
731 bool ConcatString::isConstExpr() const {
732 return lhs->isConstExpr() && rhs->isConstExpr();
733 }
734
735 int Relation::evalInt() {
736 switch (type) {
737 case LESS_THAN:
738 return lhs->evalInt() < rhs->evalInt();
739 case GREATER_THAN:
740 return lhs->evalInt() > rhs->evalInt();
741 case LESS_OR_EQUAL:
742 return lhs->evalInt() <= rhs->evalInt();
743 case GREATER_OR_EQUAL:
744 return lhs->evalInt() >= rhs->evalInt();
745 case EQUAL:
746 if (lhs->exprType() == STRING_EXPR || rhs->exprType() == STRING_EXPR)
747 return lhs->evalCastToStr() == rhs->evalCastToStr();
748 else
749 return lhs->evalInt() == rhs->evalInt();
750 case NOT_EQUAL:
751 if (lhs->exprType() == STRING_EXPR || rhs->exprType() == STRING_EXPR)
752 return lhs->evalCastToStr() != rhs->evalCastToStr();
753 else
754 return lhs->evalInt() != rhs->evalInt();
755 }
756 return 0;
757 }
758
759 void Relation::dump(int level) {
760 printIndents(level);
761 printf("Relation(\n");
762 lhs->dump(level+1);
763 printIndents(level);
764 switch (type) {
765 case LESS_THAN:
766 printf("LESS_THAN\n");
767 break;
768 case GREATER_THAN:
769 printf("GREATER_THAN\n");
770 break;
771 case LESS_OR_EQUAL:
772 printf("LESS_OR_EQUAL\n");
773 break;
774 case GREATER_OR_EQUAL:
775 printf("GREATER_OR_EQUAL\n");
776 break;
777 case EQUAL:
778 printf("EQUAL\n");
779 break;
780 case NOT_EQUAL:
781 printf("NOT_EQUAL\n");
782 break;
783 }
784 rhs->dump(level+1);
785 printIndents(level);
786 printf(")\n");
787 }
788
789 bool Relation::isConstExpr() const {
790 return lhs->isConstExpr() && rhs->isConstExpr();
791 }
792
793 int Or::evalInt() {
794 IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs);
795 if (pLHS->evalInt()) return 1;
796 IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);;
797 return (pRHS->evalInt()) ? 1 : 0;
798 }
799
800 void Or::dump(int level) {
801 printIndents(level);
802 printf("Or(\n");
803 lhs->dump(level+1);
804 printIndents(level);
805 printf(",\n");
806 rhs->dump(level+1);
807 printIndents(level);
808 printf(")\n");
809 }
810
811 int BitwiseOr::evalInt() {
812 IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs);
813 IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);
814 return pLHS->evalInt() | pRHS->evalInt();
815 }
816
817 void BitwiseOr::dump(int level) {
818 printIndents(level);
819 printf("BitwiseOr(\n");
820 lhs->dump(level+1);
821 printIndents(level);
822 printf(",\n");
823 rhs->dump(level+1);
824 printIndents(level);
825 printf(")\n");
826 }
827
828 int And::evalInt() {
829 IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs);
830 if (!pLHS->evalInt()) return 0;
831 IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);
832 return (pRHS->evalInt()) ? 1 : 0;
833 }
834
835 void And::dump(int level) {
836 printIndents(level);
837 printf("And(\n");
838 lhs->dump(level+1);
839 printIndents(level);
840 printf(",\n");
841 rhs->dump(level+1);
842 printIndents(level);
843 printf(")\n");
844 }
845
846 int BitwiseAnd::evalInt() {
847 IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs);
848 IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);
849 return pLHS->evalInt() & pRHS->evalInt();
850 }
851
852 void BitwiseAnd::dump(int level) {
853 printIndents(level);
854 printf("BitwiseAnd(\n");
855 lhs->dump(level+1);
856 printIndents(level);
857 printf(",\n");
858 rhs->dump(level+1);
859 printIndents(level);
860 printf(")\n");
861 }
862
863 void Not::dump(int level) {
864 printIndents(level);
865 printf("Not(\n");
866 expr->dump(level+1);
867 printIndents(level);
868 printf(")\n");
869 }
870
871 void BitwiseNot::dump(int level) {
872 printIndents(level);
873 printf("BitwiseNot(\n");
874 expr->dump(level+1);
875 printIndents(level);
876 printf(")\n");
877 }
878
879 VariableRef ParserContext::variableByName(const String& name) {
880 if (!vartable.count(name)) {
881 return VariableRef();
882 }
883 return vartable.find(name)->second;
884 }
885
886 VariableRef ParserContext::globalVar(const String& name) {
887 if (!vartable.count(name)) {
888 //printf("No global var '%s'\n", name.c_str());
889 //for (std::map<String,VariableRef>::const_iterator it = vartable.begin(); it != vartable.end(); ++it)
890 // printf("-> var '%s'\n", it->first.c_str());
891 return VariableRef();
892 }
893 return vartable.find(name)->second;
894 }
895
896 IntVariableRef ParserContext::globalIntVar(const String& name) {
897 return globalVar(name);
898 }
899
900 StringVariableRef ParserContext::globalStrVar(const String& name) {
901 return globalVar(name);
902 }
903
904 ParserContext::~ParserContext() {
905 destroyScanner();
906 if (globalIntMemory) {
907 delete globalIntMemory;
908 globalIntMemory = NULL;
909 }
910 }
911
912 void ParserContext::addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt) {
913 ParserIssue e;
914 e.type = PARSER_ERROR;
915 e.txt = txt;
916 e.firstLine = firstLine;
917 e.lastLine = lastLine;
918 e.firstColumn = firstColumn;
919 e.lastColumn = lastColumn;
920 vErrors.push_back(e);
921 vIssues.push_back(e);
922 }
923
924 void ParserContext::addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt) {
925 ParserIssue w;
926 w.type = PARSER_WARNING;
927 w.txt = txt;
928 w.firstLine = firstLine;
929 w.lastLine = lastLine;
930 w.firstColumn = firstColumn;
931 w.lastColumn = lastColumn;
932 vWarnings.push_back(w);
933 vIssues.push_back(w);
934 }
935
936 bool ParserContext::setPreprocessorCondition(const char* name) {
937 if (builtinPreprocessorConditions.count(name)) return false;
938 if (userPreprocessorConditions.count(name)) return false;
939 userPreprocessorConditions.insert(name);
940 return true;
941 }
942
943 bool ParserContext::resetPreprocessorCondition(const char* name) {
944 if (builtinPreprocessorConditions.count(name)) return false;
945 if (!userPreprocessorConditions.count(name)) return false;
946 userPreprocessorConditions.erase(name);
947 return true;
948 }
949
950 bool ParserContext::isPreprocessorConditionSet(const char* name) {
951 if (builtinPreprocessorConditions.count(name)) return true;
952 return userPreprocessorConditions.count(name);
953 }
954
955 std::vector<ParserIssue> ParserContext::issues() const {
956 return vIssues;
957 }
958
959 std::vector<ParserIssue> ParserContext::errors() const {
960 return vErrors;
961 }
962
963 std::vector<ParserIssue> ParserContext::warnings() const {
964 return vWarnings;
965 }
966
967 VMEventHandler* ParserContext::eventHandler(uint index) {
968 if (!handlers) return NULL;
969 return handlers->eventHandler(index);
970 }
971
972 VMEventHandler* ParserContext::eventHandlerByName(const String& name) {
973 if (!handlers) return NULL;
974 return handlers->eventHandlerByName(name);
975 }
976
977 void ParserContext::registerBuiltInConstIntVariables(const std::map<String,int>& vars) {
978 for (std::map<String,int>::const_iterator it = vars.begin();
979 it != vars.end(); ++it)
980 {
981 ConstIntVariableRef ref = new ConstIntVariable(it->second);
982 vartable[it->first] = ref;
983 }
984 }
985
986 void ParserContext::registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars) {
987 for (std::map<String,VMIntRelPtr*>::const_iterator it = vars.begin();
988 it != vars.end(); ++it)
989 {
990 BuiltInIntVariableRef ref = new BuiltInIntVariable(it->first, it->second);
991 vartable[it->first] = ref;
992 }
993 }
994
995 void ParserContext::registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars) {
996 for (std::map<String,VMInt8Array*>::const_iterator it = vars.begin();
997 it != vars.end(); ++it)
998 {
999 BuiltInIntArrayVariableRef ref = new BuiltInIntArrayVariable(it->first, it->second);
1000 vartable[it->first] = ref;
1001 }
1002 }
1003
1004 void ParserContext::registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars) {
1005 for (std::map<String,VMDynVar*>::const_iterator it = vars.begin();
1006 it != vars.end(); ++it)
1007 {
1008 DynamicVariableCallRef ref = new DynamicVariableCall(it->first, this, it->second);
1009 vartable[it->first] = ref;
1010 }
1011 }
1012
1013 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC