--- linuxsampler/trunk/src/scriptvm/tree.cpp 2019/08/30 11:40:25 3581 +++ linuxsampler/trunk/src/scriptvm/tree.cpp 2020/01/08 21:21:58 3707 @@ -19,6 +19,43 @@ bool isNoOperation(StatementRef statement) { return statement->statementType() == STMT_NOOP; } + +String acceptedArgTypesStr(VMFunction* fn, vmint iArg) { + static const ExprType_t allTypes[] = { + INT_EXPR, + INT_ARR_EXPR, + REAL_EXPR, + REAL_ARR_EXPR, + STRING_EXPR, + STRING_ARR_EXPR, + }; + const size_t nTypes = sizeof(allTypes) / sizeof(ExprType_t); + + std::vector supportedTypes; + for (int iType = 0; iType < nTypes; ++iType) { + const ExprType_t& type = allTypes[iType]; + if (fn->acceptsArgType(iArg, type)) + supportedTypes.push_back(type); + } + assert(!supportedTypes.empty()); + + if (supportedTypes.size() == 1) { + return typeStr(*supportedTypes.begin()); + } else { + String s = "either "; + for (size_t i = 0; i < supportedTypes.size(); ++i) { + const ExprType_t& type = supportedTypes[i]; + if (i == 0) { + s += typeStr(type); + } else if (i == supportedTypes.size() - 1) { + s += " or " + typeStr(type); + } else { + s += ", " + typeStr(type); + } + } + return s; + } +} Node::Node() { } @@ -151,7 +188,7 @@ void IntLiteral::dump(int level) { printIndents(level); - printf("IntLiteral %lld\n", value); + printf("IntLiteral %" PRId64 "\n", (int64_t)value); } RealLiteral::RealLiteral(const RealLitDef& def) : @@ -175,7 +212,7 @@ printf("StringLiteral: '%s'\n", value.c_str()); } -Add::Add(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs) : +Add::Add(NumberExprRef lhs, NumberExprRef rhs) : VaritypeScalarBinaryOp(lhs, rhs), Unit( // lhs and rhs are forced to be same unit type at parse time, so either one is fine here @@ -215,8 +252,8 @@ } vmfloat Add::unitFactor() const { - const ScalarNumberExpr* pLHS = dynamic_cast(&*lhs); - const ScalarNumberExpr* pRHS = dynamic_cast(&*rhs); + const NumberExpr* pLHS = dynamic_cast(&*lhs); + const NumberExpr* pRHS = dynamic_cast(&*rhs); return (pLHS->unitFactor() < pRHS->unitFactor()) ? pLHS->unitFactor() : pRHS->unitFactor(); } @@ -231,7 +268,7 @@ printf(")\n"); } -Sub::Sub(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs) : +Sub::Sub(NumberExprRef lhs, NumberExprRef rhs) : VaritypeScalarBinaryOp(lhs, rhs), Unit( // lhs and rhs are forced to be same unit type at parse time, so either one is fine here @@ -271,8 +308,8 @@ } vmfloat Sub::unitFactor() const { - const ScalarNumberExpr* pLHS = dynamic_cast(&*lhs); - const ScalarNumberExpr* pRHS = dynamic_cast(&*rhs); + const NumberExpr* pLHS = dynamic_cast(&*lhs); + const NumberExpr* pRHS = dynamic_cast(&*rhs); return (pLHS->unitFactor() < pRHS->unitFactor()) ? pLHS->unitFactor() : pRHS->unitFactor(); } @@ -287,7 +324,7 @@ printf(")\n"); } -Mul::Mul(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs) : +Mul::Mul(NumberExprRef lhs, NumberExprRef rhs) : VaritypeScalarBinaryOp(lhs, rhs), Unit( // currently the NKSP parser only allows a unit type on either side on multiplications @@ -320,12 +357,12 @@ } vmfloat Mul::unitFactor() const { - const ScalarNumberExpr* pLHS = dynamic_cast(&*lhs); - const ScalarNumberExpr* pRHS = dynamic_cast(&*rhs); + const NumberExpr* pLHS = dynamic_cast(&*lhs); + const NumberExpr* pRHS = dynamic_cast(&*rhs); return pLHS->unitFactor() * pRHS->unitFactor(); } -Div::Div(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs) : +Div::Div(NumberExprRef lhs, NumberExprRef rhs) : VaritypeScalarBinaryOp(lhs, rhs), Unit( // the NKSP parser only allows either A) a unit type on left side and none @@ -367,8 +404,8 @@ } vmfloat Div::unitFactor() const { - const ScalarNumberExpr* pLHS = dynamic_cast(&*lhs); - const ScalarNumberExpr* pRHS = dynamic_cast(&*rhs); + const NumberExpr* pLHS = dynamic_cast(&*lhs); + const NumberExpr* pRHS = dynamic_cast(&*rhs); return pLHS->unitFactor() / pRHS->unitFactor(); } @@ -560,7 +597,7 @@ if (!fn || !result) return VM_NO_FACTOR; VMExpr* expr = result->resultValue(); if (!expr) return VM_NO_FACTOR; - VMScalarNumberExpr* scalar = expr->asScalarNumberExpr(); + VMNumberExpr* scalar = expr->asNumber(); if (!scalar) return VM_NO_FACTOR; return scalar->unitFactor(); } @@ -642,7 +679,7 @@ { } -ScalarNumberVariable::ScalarNumberVariable(const VariableDecl& decl) : +NumberVariable::NumberVariable(const VariableDecl& decl) : Variable(decl), Unit(decl.unitType), unitFactorMemPos(decl.unitFactorMemPos), polyphonic(decl.isPolyphonic), @@ -650,7 +687,7 @@ { } -vmfloat ScalarNumberVariable::unitFactor() const { +vmfloat NumberVariable::unitFactor() const { if (isPolyphonic()) { return context->execContext->polyphonicUnitFactorMemory[unitFactorMemPos]; } @@ -664,11 +701,10 @@ } IntVariable::IntVariable(const VariableDecl& decl) : - ScalarNumberVariable({ + NumberVariable({ .ctx = decl.ctx, .isPolyphonic = decl.isPolyphonic, .isConst = decl.isConst, - .isFinal = decl.isFinal, .elements = decl.elements, .memPos = ( (!decl.ctx) ? 0 : @@ -682,7 +718,8 @@ postfixInc(decl.ctx->polyphonicUnitFactorCount, decl.elements) : postfixInc(decl.ctx->globalUnitFactorCount, decl.elements) ), - .unitType = decl.unitType + .unitType = decl.unitType, + .isFinal = decl.isFinal, }), Unit(decl.unitType) { @@ -720,11 +757,10 @@ } RealVariable::RealVariable(const VariableDecl& decl) : - ScalarNumberVariable({ + NumberVariable({ .ctx = decl.ctx, .isPolyphonic = decl.isPolyphonic, .isConst = decl.isConst, - .isFinal = decl.isFinal, .elements = decl.elements, .memPos = ( (!decl.ctx) ? 0 : @@ -738,7 +774,8 @@ postfixInc(decl.ctx->polyphonicUnitFactorCount, decl.elements) : postfixInc(decl.ctx->globalUnitFactorCount, decl.elements) ), - .unitType = decl.unitType + .unitType = decl.unitType, + .isFinal = decl.isFinal, }), Unit(decl.unitType) { @@ -780,11 +817,11 @@ .ctx = def.ctx, .isPolyphonic = false, .isConst = true, - .isFinal = def.isFinal, .elements = 1, .memPos = def.memPos, .unitFactorMemPos = def.unitFactorMemPos, - .unitType = def.unitType + .unitType = def.unitType, + .isFinal = def.isFinal, }), Unit(def.unitType), value(def.value), unitPrefixFactor(def.unitFactor) @@ -808,7 +845,7 @@ void ConstIntVariable::dump(int level) { printIndents(level); - printf("ConstIntVariable val=%lld\n", value); + printf("ConstIntVariable val=%" PRId64 "\n", (int64_t)value); } ConstRealVariable::ConstRealVariable(const RealVarDef& def) : @@ -816,11 +853,11 @@ .ctx = def.ctx, .isPolyphonic = false, .isConst = true, - .isFinal = def.isFinal, .elements = 1, .memPos = def.memPos, .unitFactorMemPos = def.unitFactorMemPos, - .unitType = def.unitType + .unitType = def.unitType, + .isFinal = def.isFinal, }), Unit(def.unitType), value(def.value), unitPrefixFactor(def.unitFactor) @@ -845,11 +882,11 @@ .ctx = NULL, .isPolyphonic = false, .isConst = false, // may or may not be modifyable though! - .isFinal = false, .elements = 0, .memPos = 0, .unitFactorMemPos = 0, - .unitType = VM_NO_UNIT + .unitType = VM_NO_UNIT, + .isFinal = false, }), Unit(VM_NO_UNIT), name(name), ptr(ptr) @@ -876,11 +913,11 @@ .ctx = decl.ctx, .isPolyphonic = true, .isConst = decl.isConst, - .isFinal = decl.isFinal, .elements = 1, .memPos = 0, .unitFactorMemPos = 0, - .unitType = decl.unitType + .unitType = decl.unitType, + .isFinal = decl.isFinal, }), Unit(decl.unitType) { @@ -896,11 +933,11 @@ .ctx = decl.ctx, .isPolyphonic = true, .isConst = decl.isConst, - .isFinal = decl.isFinal, .elements = 1, .memPos = 0, .unitFactorMemPos = 0, - .unitType = decl.unitType + .unitType = decl.unitType, + .isFinal = decl.isFinal, }), Unit(decl.unitType) { @@ -916,11 +953,11 @@ .ctx = ctx, .isPolyphonic = false, .isConst = false, - .isFinal = false, .elements = 0, .memPos = 0, .unitFactorMemPos = 0, - .unitType = VM_NO_UNIT + .unitType = VM_NO_UNIT, + .isFinal = false, }) { values.resize(size); @@ -937,11 +974,11 @@ .ctx = ctx, .isPolyphonic = false, .isConst = _bConst, - .isFinal = false, .elements = 0, .memPos = 0, .unitFactorMemPos = 0, - .unitType = VM_NO_UNIT + .unitType = VM_NO_UNIT, + .isFinal = false, }) { this->values.resize(size); @@ -960,11 +997,11 @@ .ctx = ctx, .isPolyphonic = false, .isConst = bConst, - .isFinal = false, .elements = 0, .memPos = 0, .unitFactorMemPos = 0, - .unitType = VM_NO_UNIT + .unitType = VM_NO_UNIT, + .isFinal = false, }) { } @@ -997,7 +1034,7 @@ printf("\n"); printIndents(level+1); } - printf("%lld, ", values[i]); + printf("%" PRId64 ", ", (int64_t)values[i]); } printIndents(level); printf(")\n"); @@ -1008,11 +1045,11 @@ .ctx = ctx, .isPolyphonic = false, .isConst = false, - .isFinal = false, .elements = 0, .memPos = 0, .unitFactorMemPos = 0, - .unitType = VM_NO_UNIT + .unitType = VM_NO_UNIT, + .isFinal = false, }) { values.resize(size); @@ -1029,11 +1066,11 @@ .ctx = ctx, .isPolyphonic = false, .isConst = _bConst, - .isFinal = false, .elements = 0, .memPos = 0, .unitFactorMemPos = 0, - .unitType = VM_NO_UNIT + .unitType = VM_NO_UNIT, + .isFinal = false, }) { this->values.resize(size); @@ -1052,11 +1089,11 @@ .ctx = ctx, .isPolyphonic = false, .isConst = bConst, - .isFinal = false, .elements = 0, .memPos = 0, .unitFactorMemPos = 0, - .unitType = VM_NO_UNIT + .unitType = VM_NO_UNIT, + .isFinal = false, }) { } @@ -1121,15 +1158,15 @@ .ctx = NULL, .isPolyphonic = (array) ? array->isPolyphonic() : false, .isConst = (array) ? array->isConstExpr() : false, - .isFinal = false, .elements = 0, .memPos = 0, .unitFactorMemPos = 0, - .unitType = VM_NO_UNIT + .unitType = VM_NO_UNIT, + .isFinal = false, }), Unit(VM_NO_UNIT), array(array), index(arrayIndex), currentIndex(-1) -{ +{ } void IntArrayElement::assign(Expression* expr) { @@ -1172,11 +1209,11 @@ .ctx = NULL, .isPolyphonic = (array) ? array->isPolyphonic() : false, .isConst = (array) ? array->isConstExpr() : false, - .isFinal = false, .elements = 0, .memPos = 0, .unitFactorMemPos = 0, - .unitType = VM_NO_UNIT + .unitType = VM_NO_UNIT, + .isFinal = false, }), Unit(VM_NO_UNIT), array(array), index(arrayIndex), currentIndex(-1) @@ -1230,8 +1267,8 @@ StringVariable::StringVariable(ParserContext* ctx, bool bConst) : Variable({ .ctx = ctx, + .isConst = bConst, .memPos = 0, - .isConst = bConst }) { } @@ -1248,7 +1285,7 @@ void StringVariable::dump(int level) { printIndents(level); - printf("StringVariable memPos=%lld\n", memPos); + printf("StringVariable memPos=%" PRId64 "\n", (int64_t)memPos); } ConstStringVariable::ConstStringVariable(ParserContext* ctx, String _value) @@ -1271,9 +1308,9 @@ printf("ConstStringVariable val='%s'\n", value.c_str()); } -bool ScalarNumberBinaryOp::isFinal() const { - ScalarNumberExprRef l = (ScalarNumberExprRef) lhs; - ScalarNumberExprRef r = (ScalarNumberExprRef) rhs; +bool NumberBinaryOp::isFinal() const { + NumberExprRef l = (NumberExprRef) lhs; + NumberExprRef r = (NumberExprRef) rhs; return l->isFinal() || r->isFinal(); } @@ -1318,7 +1355,7 @@ printIndents(level); if (select) if (select->isConstExpr()) - printf("Case select %lld\n", select->evalInt()); + printf("Case select %" PRId64 "\n", (int64_t)select->evalInt()); else printf("Case select [runtime expr]\n"); else @@ -1328,16 +1365,16 @@ CaseBranch& branch = branches[i]; if (branch.from && branch.to) if (branch.from->isConstExpr() && branch.to->isConstExpr()) - printf("case %lld to %lld\n", branch.from->evalInt(), branch.to->evalInt()); + printf("case %" PRId64 " to %" PRId64 "\n", (int64_t)branch.from->evalInt(), (int64_t)branch.to->evalInt()); else if (branch.from->isConstExpr() && !branch.to->isConstExpr()) - printf("case %lld to [runtime expr]\n", branch.from->evalInt()); + printf("case %" PRId64 " to [runtime expr]\n", (int64_t)branch.from->evalInt()); else if (!branch.from->isConstExpr() && branch.to->isConstExpr()) - printf("case [runtime expr] to %lld\n", branch.to->evalInt()); + printf("case [runtime expr] to %" PRId64 "\n", (int64_t)branch.to->evalInt()); else printf("case [runtime expr] to [runtime expr]\n"); else if (branch.from) if (branch.from->isConstExpr()) - printf("case %lld\n", branch.from->evalInt()); + printf("case %" PRId64 "\n", (int64_t)branch.from->evalInt()); else printf("case [runtime expr]\n"); else @@ -1376,7 +1413,7 @@ printIndents(level); if (m_condition) if (m_condition->isConstExpr()) - printf("while (%lld) {\n", m_condition->evalInt()); + printf("while (%" PRId64 ") {\n", (int64_t)m_condition->evalInt()); else printf("while ([runtime expr]) {\n"); else @@ -1843,6 +1880,17 @@ .value = it->second }); vartable[it->first] = ref; + } +} + +void ParserContext::registerBuiltInConstRealVariables(const std::map& vars) { + for (std::map::const_iterator it = vars.begin(); + it != vars.end(); ++it) + { + ConstRealVariableRef ref = new ConstRealVariable({ + .value = it->second + }); + vartable[it->first] = ref; } }