1 |
/* |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck and Andreas Persson |
* Copyright (c) 2014 - 2016 Christian Schoenebeck and Andreas Persson |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
11 |
#include <string.h> |
#include <string.h> |
12 |
#include "tree.h" |
#include "tree.h" |
13 |
#include "../common/global_private.h" |
#include "../common/global_private.h" |
14 |
|
#include <assert.h> |
15 |
|
|
16 |
namespace LinuxSampler { |
namespace LinuxSampler { |
17 |
|
|
144 |
printf(")\n"); |
printf(")\n"); |
145 |
} |
} |
146 |
|
|
147 |
|
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 |
EventHandlers::EventHandlers() { |
EventHandlers::EventHandlers() { |
155 |
//printf("EventHandlers::Constructor 0x%lx\n", (long long)this); |
//printf("EventHandlers::Constructor 0x%lx\n", (long long)this); |
156 |
} |
} |
184 |
return const_cast<EventHandler*>(&*args.at(index)); |
return const_cast<EventHandler*>(&*args.at(index)); |
185 |
} |
} |
186 |
|
|
187 |
|
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 |
Assignment::Assignment(VariableRef variable, ExpressionRef value) |
Assignment::Assignment(VariableRef variable, ExpressionRef value) |
195 |
: variable(variable), value(value) |
: variable(variable), value(value) |
196 |
{ |
{ |
208 |
return STMT_SUCCESS; |
return STMT_SUCCESS; |
209 |
} |
} |
210 |
|
|
211 |
|
EventHandler::EventHandler(StatementsRef statements) { |
212 |
|
this->statements = statements; |
213 |
|
usingPolyphonics = statements->isPolyphonic(); |
214 |
|
} |
215 |
|
|
216 |
void EventHandler::dump(int level) { |
void EventHandler::dump(int level) { |
217 |
printIndents(level); |
printIndents(level); |
218 |
printf("EventHandler {\n"); |
printf("EventHandler {\n"); |
236 |
return &*args.at(i); |
return &*args.at(i); |
237 |
} |
} |
238 |
|
|
239 |
|
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 |
void FunctionCall::dump(int level) { |
void FunctionCall::dump(int level) { |
247 |
printIndents(level); |
printIndents(level); |
248 |
printf("FunctionCall '%s' args={\n", functionName.c_str()); |
printf("FunctionCall '%s' args={\n", functionName.c_str()); |
301 |
IntVariable::IntVariable(ParserContext* ctx) |
IntVariable::IntVariable(ParserContext* ctx) |
302 |
: Variable(ctx, ctx ? ctx->globalIntVarCount++ : 0, false), polyphonic(false) |
: Variable(ctx, ctx ? ctx->globalIntVarCount++ : 0, false), polyphonic(false) |
303 |
{ |
{ |
304 |
|
//printf("globalIntVar parserctx=0x%lx memPOS=%d\n", ctx, memPos); |
305 |
|
assert(ctx); |
306 |
|
} |
307 |
|
|
308 |
|
inline static int postfixInc(int& object, int incBy) { |
309 |
|
const int i = object; |
310 |
|
object += incBy; |
311 |
|
return i; |
312 |
} |
} |
313 |
|
|
314 |
IntVariable::IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size) |
IntVariable::IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size) |
315 |
: Variable(ctx, !ctx ? 0 : polyphonic ? ctx->polyphonicIntVarCount += size : ctx->globalIntVarCount += size, bConst), |
: Variable(ctx, !ctx ? 0 : polyphonic ? postfixInc(ctx->polyphonicIntVarCount, size) : postfixInc(ctx->globalIntVarCount, size), bConst), |
316 |
polyphonic(polyphonic) |
polyphonic(polyphonic) |
317 |
{ |
{ |
318 |
|
//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 |
} |
} |
324 |
|
|
325 |
void IntVariable::assign(Expression* expr) { |
void IntVariable::assign(Expression* expr) { |
333 |
|
|
334 |
int IntVariable::evalInt() { |
int IntVariable::evalInt() { |
335 |
//printf("IntVariable::eval pos=%d\n", memPos); |
//printf("IntVariable::eval pos=%d\n", memPos); |
336 |
if (polyphonic) |
if (polyphonic) { |
337 |
|
//printf("evalInt() poly memPos=%d execCtx=0x%lx\n", memPos, (uint64_t)context->execContext); |
338 |
return context->execContext->polyphonicIntMemory[memPos]; |
return context->execContext->polyphonicIntMemory[memPos]; |
339 |
|
} |
340 |
return (*context->globalIntMemory)[memPos]; |
return (*context->globalIntMemory)[memPos]; |
341 |
} |
} |
342 |
|
|
343 |
void IntVariable::dump(int level) { |
void IntVariable::dump(int level) { |
344 |
printIndents(level); |
printIndents(level); |
345 |
printf("IntVariable memPos=%d\n", memPos); |
//printf("IntVariable memPos=%d\n", memPos); |
346 |
} |
} |
347 |
|
|
348 |
//ConstIntVariable::ConstIntVariable(ParserContext* ctx, int value) |
//ConstIntVariable::ConstIntVariable(ParserContext* ctx, int value) |
371 |
printf("ConstIntVariable val=%d\n", value); |
printf("ConstIntVariable val=%d\n", value); |
372 |
} |
} |
373 |
|
|
374 |
|
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 |
PolyphonicIntVariable::PolyphonicIntVariable(ParserContext* ctx) |
PolyphonicIntVariable::PolyphonicIntVariable(ParserContext* ctx) |
395 |
: IntVariable(ctx,true,false) |
: IntVariable(ctx,true,false) |
396 |
{ |
{ |
418 |
} |
} |
419 |
} |
} |
420 |
|
|
421 |
|
IntArrayVariable::IntArrayVariable(ParserContext* ctx, bool bConst) |
422 |
|
: Variable(ctx, 0, bConst) |
423 |
|
{ |
424 |
|
} |
425 |
|
|
426 |
int IntArrayVariable::evalIntElement(uint i) { |
int IntArrayVariable::evalIntElement(uint i) { |
427 |
if (i >= values.size()) return 0; |
if (i >= values.size()) return 0; |
428 |
return values[i]; |
return values[i]; |
447 |
printf(")\n"); |
printf(")\n"); |
448 |
} |
} |
449 |
|
|
450 |
|
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 |
IntArrayElement::IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex) |
IntArrayElement::IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex) |
470 |
: IntVariable(NULL, false, false, 0), array(array), index(arrayIndex) |
: IntVariable(NULL, false, false, 0), array(array), index(arrayIndex) |
471 |
{ |
{ |
563 |
return NULL; |
return NULL; |
564 |
} |
} |
565 |
|
|
566 |
|
bool If::isPolyphonic() const { |
567 |
|
if (condition->isPolyphonic() || ifStatements->isPolyphonic()) |
568 |
|
return true; |
569 |
|
return elseStatements ? elseStatements->isPolyphonic() : false; |
570 |
|
} |
571 |
|
|
572 |
void SelectCase::dump(int level) { |
void SelectCase::dump(int level) { |
573 |
printIndents(level); |
printIndents(level); |
574 |
if (select) |
if (select) |
619 |
return NULL; |
return NULL; |
620 |
} |
} |
621 |
|
|
622 |
|
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 |
// void Case::addBranch(IntExprRef condition, StatementsRef statements) { |
// void Case::addBranch(IntExprRef condition, StatementsRef statements) { |
631 |
// CaseBranchRef b = new CaseBranchRef; |
// CaseBranchRef b = new CaseBranchRef; |
632 |
// b->from = condition; |
// b->from = condition; |
753 |
|
|
754 |
int Or::evalInt() { |
int Or::evalInt() { |
755 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
756 |
|
if (pLHS->evalInt()) return 1; |
757 |
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
758 |
return pLHS->evalInt() || pRHS->evalInt(); |
return (pRHS->evalInt()) ? 1 : 0; |
759 |
} |
} |
760 |
|
|
761 |
void Or::dump(int level) { |
void Or::dump(int level) { |
769 |
printf(")\n"); |
printf(")\n"); |
770 |
} |
} |
771 |
|
|
772 |
|
int BitwiseOr::evalInt() { |
773 |
|
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
774 |
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs); |
775 |
|
return pLHS->evalInt() | pRHS->evalInt(); |
776 |
|
} |
777 |
|
|
778 |
|
void BitwiseOr::dump(int level) { |
779 |
|
printIndents(level); |
780 |
|
printf("BitwiseOr(\n"); |
781 |
|
lhs->dump(level+1); |
782 |
|
printIndents(level); |
783 |
|
printf(",\n"); |
784 |
|
rhs->dump(level+1); |
785 |
|
printIndents(level); |
786 |
|
printf(")\n"); |
787 |
|
} |
788 |
|
|
789 |
int And::evalInt() { |
int And::evalInt() { |
790 |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
791 |
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);; |
if (!pLHS->evalInt()) return 0; |
792 |
return pLHS->evalInt() && pRHS->evalInt(); |
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs); |
793 |
|
return (pRHS->evalInt()) ? 1 : 0; |
794 |
} |
} |
795 |
|
|
796 |
void And::dump(int level) { |
void And::dump(int level) { |
804 |
printf(")\n"); |
printf(")\n"); |
805 |
} |
} |
806 |
|
|
807 |
|
int BitwiseAnd::evalInt() { |
808 |
|
IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs); |
809 |
|
IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs); |
810 |
|
return pLHS->evalInt() & pRHS->evalInt(); |
811 |
|
} |
812 |
|
|
813 |
|
void BitwiseAnd::dump(int level) { |
814 |
|
printIndents(level); |
815 |
|
printf("BitwiseAnd(\n"); |
816 |
|
lhs->dump(level+1); |
817 |
|
printIndents(level); |
818 |
|
printf(",\n"); |
819 |
|
rhs->dump(level+1); |
820 |
|
printIndents(level); |
821 |
|
printf(")\n"); |
822 |
|
} |
823 |
|
|
824 |
void Not::dump(int level) { |
void Not::dump(int level) { |
825 |
printIndents(level); |
printIndents(level); |
826 |
printf("Not(\n"); |
printf("Not(\n"); |
829 |
printf(")\n"); |
printf(")\n"); |
830 |
} |
} |
831 |
|
|
832 |
|
void BitwiseNot::dump(int level) { |
833 |
|
printIndents(level); |
834 |
|
printf("BitwiseNot(\n"); |
835 |
|
expr->dump(level+1); |
836 |
|
printIndents(level); |
837 |
|
printf(")\n"); |
838 |
|
} |
839 |
|
|
840 |
VariableRef ParserContext::variableByName(const String& name) { |
VariableRef ParserContext::variableByName(const String& name) { |
841 |
if (!vartable.count(name)) { |
if (!vartable.count(name)) { |
842 |
return VariableRef(); |
return VariableRef(); |
862 |
return globalVar(name); |
return globalVar(name); |
863 |
} |
} |
864 |
|
|
865 |
void ParserContext::addErr(int line, const char* txt) { |
ParserContext::~ParserContext() { |
866 |
|
destroyScanner(); |
867 |
|
if (globalIntMemory) { |
868 |
|
delete globalIntMemory; |
869 |
|
globalIntMemory = NULL; |
870 |
|
} |
871 |
|
} |
872 |
|
|
873 |
|
void ParserContext::addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt) { |
874 |
ParserIssue e; |
ParserIssue e; |
875 |
e.type = PARSER_ERROR; |
e.type = PARSER_ERROR; |
876 |
e.txt = txt; |
e.txt = txt; |
877 |
e.line = line; |
e.firstLine = firstLine; |
878 |
errors.push_back(e); |
e.lastLine = lastLine; |
879 |
issues.push_back(e); |
e.firstColumn = firstColumn; |
880 |
|
e.lastColumn = lastColumn; |
881 |
|
vErrors.push_back(e); |
882 |
|
vIssues.push_back(e); |
883 |
} |
} |
884 |
|
|
885 |
void ParserContext::addWrn(int line, const char* txt) { |
void ParserContext::addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt) { |
886 |
ParserIssue w; |
ParserIssue w; |
887 |
w.type = PARSER_WARNING; |
w.type = PARSER_WARNING; |
888 |
w.txt = txt; |
w.txt = txt; |
889 |
w.line = line; |
w.firstLine = firstLine; |
890 |
warnings.push_back(w); |
w.lastLine = lastLine; |
891 |
issues.push_back(w); |
w.firstColumn = firstColumn; |
892 |
|
w.lastColumn = lastColumn; |
893 |
|
vWarnings.push_back(w); |
894 |
|
vIssues.push_back(w); |
895 |
} |
} |
896 |
|
|
897 |
bool ParserContext::setPreprocessorCondition(const char* name) { |
bool ParserContext::setPreprocessorCondition(const char* name) { |
913 |
return userPreprocessorConditions.count(name); |
return userPreprocessorConditions.count(name); |
914 |
} |
} |
915 |
|
|
916 |
|
std::vector<ParserIssue> ParserContext::issues() const { |
917 |
|
return vIssues; |
918 |
|
} |
919 |
|
|
920 |
|
std::vector<ParserIssue> ParserContext::errors() const { |
921 |
|
return vErrors; |
922 |
|
} |
923 |
|
|
924 |
|
std::vector<ParserIssue> ParserContext::warnings() const { |
925 |
|
return vWarnings; |
926 |
|
} |
927 |
|
|
928 |
|
VMEventHandler* ParserContext::eventHandler(uint index) { |
929 |
|
if (!handlers) return NULL; |
930 |
|
return handlers->eventHandler(index); |
931 |
|
} |
932 |
|
|
933 |
|
VMEventHandler* ParserContext::eventHandlerByName(const String& name) { |
934 |
|
if (!handlers) return NULL; |
935 |
|
return handlers->eventHandlerByName(name); |
936 |
|
} |
937 |
|
|
938 |
|
void ParserContext::registerBuiltInConstIntVariables(const std::map<String,int>& vars) { |
939 |
|
for (std::map<String,int>::const_iterator it = vars.begin(); |
940 |
|
it != vars.end(); ++it) |
941 |
|
{ |
942 |
|
ConstIntVariableRef ref = new ConstIntVariable(it->second); |
943 |
|
vartable[it->first] = ref; |
944 |
|
} |
945 |
|
} |
946 |
|
|
947 |
|
void ParserContext::registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars) { |
948 |
|
for (std::map<String,VMIntRelPtr*>::const_iterator it = vars.begin(); |
949 |
|
it != vars.end(); ++it) |
950 |
|
{ |
951 |
|
BuiltInIntVariableRef ref = new BuiltInIntVariable(it->first, it->second); |
952 |
|
vartable[it->first] = ref; |
953 |
|
} |
954 |
|
} |
955 |
|
|
956 |
|
void ParserContext::registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars) { |
957 |
|
for (std::map<String,VMInt8Array*>::const_iterator it = vars.begin(); |
958 |
|
it != vars.end(); ++it) |
959 |
|
{ |
960 |
|
BuiltInIntArrayVariableRef ref = new BuiltInIntArrayVariable(it->first, it->second); |
961 |
|
vartable[it->first] = ref; |
962 |
|
} |
963 |
|
} |
964 |
|
|
965 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |