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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2889 - (hide annotations) (download)
Mon Apr 25 17:28:23 2016 UTC (5 years, 4 months ago) by schoenebeck
File size: 24994 byte(s)
* Added new C++ API class "ScriptVMFactory".
* Instrument Scripts: extended parser issues to provide not only first
  line and first column, but also last line and last column of issue
  (thus marking the precise span of the issue within the source code).
* Bumped version (2.0.0.svn7).

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

  ViewVC Help
Powered by ViewVC