1 /* 2 * Copyright (c) 2014-2020 Christian Schoenebeck 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 "common.h" 11 #include <iostream> 12 #include "editor/SourceToken.h" 13 14 namespace LinuxSampler { 15 16 /////////////////////////////////////////////////////////////////////// 17 // class 'VMUnit' 18 _unitFactor(MetricPrefix_t prefix)19 static vmfloat _unitFactor(MetricPrefix_t prefix) { 20 switch (prefix) { 21 case VM_NO_PREFIX: return 1.f; 22 case VM_KILO: return 1000.f; 23 case VM_HECTO: return 100.f; 24 case VM_DECA: return 10.f; 25 case VM_DECI: return 0.1f; 26 case VM_CENTI: return 0.01f; 27 case VM_MILLI: return 0.001f; 28 case VM_MICRO: return 0.000001f; 29 } 30 return 1.f; 31 } 32 unitFactor(MetricPrefix_t prefix)33 vmfloat VMUnit::unitFactor(MetricPrefix_t prefix) { 34 return _unitFactor(prefix); 35 } 36 unitFactor(MetricPrefix_t prefix1,MetricPrefix_t prefix2)37 vmfloat VMUnit::unitFactor(MetricPrefix_t prefix1, MetricPrefix_t prefix2) { 38 return _unitFactor(prefix1) * _unitFactor(prefix2); 39 } 40 unitFactor(const MetricPrefix_t * prefixes,vmuint size)41 vmfloat VMUnit::unitFactor(const MetricPrefix_t* prefixes, vmuint size) { 42 vmfloat f = VM_NO_FACTOR; 43 for (vmuint i = 0; i < size && prefixes[i]; ++i) 44 f *= _unitFactor(prefixes[i]); 45 return f; 46 } 47 hasUnitFactorNow() const48 bool VMUnit::hasUnitFactorNow() const { 49 return unitFactor() != VM_NO_FACTOR; 50 } 51 52 /////////////////////////////////////////////////////////////////////// 53 // class 'VMExpr' 54 asInt() const55 VMIntExpr* VMExpr::asInt() const { 56 return const_cast<VMIntExpr*>( dynamic_cast<const VMIntExpr*>(this) ); 57 } 58 asReal() const59 VMRealExpr* VMExpr::asReal() const { 60 return const_cast<VMRealExpr*>( dynamic_cast<const VMRealExpr*>(this) ); 61 } 62 asNumber() const63 VMNumberExpr* VMExpr::asNumber() const { 64 return const_cast<VMNumberExpr*>( 65 dynamic_cast<const VMNumberExpr*>(this) 66 ); 67 } 68 asString() const69 VMStringExpr* VMExpr::asString() const { 70 return const_cast<VMStringExpr*>( dynamic_cast<const VMStringExpr*>(this) ); 71 } 72 asIntArray() const73 VMIntArrayExpr* VMExpr::asIntArray() const { 74 return const_cast<VMIntArrayExpr*>( dynamic_cast<const VMIntArrayExpr*>(this) ); 75 } 76 asRealArray() const77 VMRealArrayExpr* VMExpr::asRealArray() const { 78 return const_cast<VMRealArrayExpr*>( 79 dynamic_cast<const VMRealArrayExpr*>(this) 80 ); 81 } 82 asArray() const83 VMArrayExpr* VMExpr::asArray() const { 84 return const_cast<VMArrayExpr*>( 85 dynamic_cast<const VMArrayExpr*>(this) 86 ); 87 } 88 isModifyable() const89 bool VMExpr::isModifyable() const { 90 const VMVariable* var = dynamic_cast<const VMVariable*>(this); 91 return (!var) ? false : var->isAssignable(); 92 } 93 acceptsArgUnitType(vmint iArg,StdUnit_t type) const94 bool VMFunction::acceptsArgUnitType(vmint iArg, StdUnit_t type) const { 95 return type == VM_NO_UNIT; 96 } 97 acceptsArgUnitPrefix(vmint iArg,StdUnit_t type) const98 bool VMFunction::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const { 99 return false; 100 } 101 acceptsArgFinal(vmint iArg) const102 bool VMFunction::acceptsArgFinal(vmint iArg) const { 103 return false; 104 } 105 checkArgs(VMFnArgs * args,std::function<void (String)> err,std::function<void (String)> wrn)106 void VMFunction::checkArgs(VMFnArgs* args, 107 std::function<void(String)> err, 108 std::function<void(String)> wrn) 109 { 110 } 111 wrnMsg(const String & txt)112 void VMFunction::wrnMsg(const String& txt) { 113 std::cout << "[ScriptVM] " << txt << std::endl; 114 } 115 errMsg(const String & txt)116 void VMFunction::errMsg(const String& txt) { 117 std::cerr << "[ScriptVM] " << txt << std::endl; 118 } 119 120 /////////////////////////////////////////////////////////////////////// 121 // class 'VMNumberExpr' 122 evalCastInt()123 vmint VMNumberExpr::evalCastInt() { 124 if (exprType() == INT_EXPR) 125 return asInt()->evalInt(); 126 else 127 return vmint( asReal()->evalReal() ); 128 } 129 evalCastReal()130 vmfloat VMNumberExpr::evalCastReal() { 131 if (exprType() == REAL_EXPR) 132 return asReal()->evalReal(); 133 else 134 return vmfloat( asInt()->evalInt() ); 135 } 136 evalCastInt(MetricPrefix_t prefix)137 vmint VMNumberExpr::evalCastInt(MetricPrefix_t prefix) { 138 vmfloat f = evalCastReal(); 139 vmfloat factor = unitFactor() / _unitFactor(prefix); 140 return vmint(f * factor); 141 } 142 evalCastInt(MetricPrefix_t prefix1,MetricPrefix_t prefix2)143 vmint VMNumberExpr::evalCastInt(MetricPrefix_t prefix1, MetricPrefix_t prefix2) { 144 vmfloat f = evalCastReal(); 145 vmfloat factor = unitFactor() / 146 ( _unitFactor(prefix1) * _unitFactor(prefix2) ); 147 return vmint(f * factor); 148 } 149 evalCastReal(MetricPrefix_t prefix)150 vmfloat VMNumberExpr::evalCastReal(MetricPrefix_t prefix) { 151 vmfloat f = evalCastReal(); 152 vmfloat factor = unitFactor() / _unitFactor(prefix); 153 return f * factor; 154 } 155 evalCastReal(MetricPrefix_t prefix1,MetricPrefix_t prefix2)156 vmfloat VMNumberExpr::evalCastReal(MetricPrefix_t prefix1, MetricPrefix_t prefix2) { 157 vmfloat f = evalCastReal(); 158 vmfloat factor = unitFactor() / 159 ( _unitFactor(prefix1) * _unitFactor(prefix2) ); 160 return f * factor; 161 } 162 163 /////////////////////////////////////////////////////////////////////// 164 // class 'VMIntExpr' 165 evalInt(MetricPrefix_t prefix)166 vmint VMIntExpr::evalInt(MetricPrefix_t prefix) { 167 vmfloat f = (vmfloat) evalInt(); 168 vmfloat factor = unitFactor() / _unitFactor(prefix); 169 return vmint(f * factor); 170 } 171 evalInt(MetricPrefix_t prefix1,MetricPrefix_t prefix2)172 vmint VMIntExpr::evalInt(MetricPrefix_t prefix1, MetricPrefix_t prefix2) { 173 vmfloat f = (vmfloat) evalInt(); 174 vmfloat factor = unitFactor() / 175 ( _unitFactor(prefix1) * _unitFactor(prefix2) ); 176 return vmint(f * factor); 177 } 178 179 /////////////////////////////////////////////////////////////////////// 180 // class 'VMRealExpr' 181 evalReal(MetricPrefix_t prefix)182 vmfloat VMRealExpr::evalReal(MetricPrefix_t prefix) { 183 vmfloat f = evalReal(); 184 vmfloat factor = unitFactor() / _unitFactor(prefix); 185 return f * factor; 186 } 187 evalReal(MetricPrefix_t prefix1,MetricPrefix_t prefix2)188 vmfloat VMRealExpr::evalReal(MetricPrefix_t prefix1, MetricPrefix_t prefix2) { 189 vmfloat f = evalReal(); 190 vmfloat factor = unitFactor() / 191 ( _unitFactor(prefix1) * _unitFactor(prefix2) ); 192 return f * factor; 193 } 194 195 /////////////////////////////////////////////////////////////////////// 196 // class 'VMFnResult' 197 ~VMFnResult()198 VMFnResult::~VMFnResult() { 199 } 200 201 /////////////////////////////////////////////////////////////////////// 202 // class 'VMSourceToken' 203 VMSourceToken()204 VMSourceToken::VMSourceToken() : m_token(NULL) { 205 } 206 VMSourceToken(SourceToken * ct)207 VMSourceToken::VMSourceToken(SourceToken* ct) : m_token(ct) { 208 } 209 VMSourceToken(const VMSourceToken & other)210 VMSourceToken::VMSourceToken(const VMSourceToken& other) { 211 if (other.m_token) { 212 m_token = new SourceToken; 213 *m_token = *other.m_token; 214 } else m_token = NULL; 215 } 216 ~VMSourceToken()217 VMSourceToken::~VMSourceToken() { 218 if (m_token) { 219 delete m_token; 220 m_token = NULL; 221 } 222 } 223 operator =(const VMSourceToken & other)224 VMSourceToken& VMSourceToken::operator=(const VMSourceToken& other) { 225 if (m_token) delete m_token; 226 if (other.m_token) { 227 m_token = new SourceToken; 228 *m_token = *other.m_token; 229 } else m_token = NULL; 230 return *this; 231 } 232 text() const233 String VMSourceToken::text() const { 234 return (m_token) ? m_token->text() : ""; 235 } 236 firstLine() const237 int VMSourceToken::firstLine() const { 238 return (m_token) ? m_token->firstLine() : 0; 239 } 240 firstColumn() const241 int VMSourceToken::firstColumn() const { 242 return (m_token) ? m_token->firstColumn() : 0; 243 } 244 firstByte() const245 int VMSourceToken::firstByte() const { 246 return (m_token) ? m_token->firstByte() : 0; 247 } 248 lengthBytes() const249 int VMSourceToken::lengthBytes() const { 250 return (m_token) ? m_token->lengthBytes() : 0; 251 } 252 isEOF() const253 bool VMSourceToken::isEOF() const { 254 return (m_token) ? m_token->isEOF() : true; 255 } 256 isNewLine() const257 bool VMSourceToken::isNewLine() const { 258 return (m_token) ? m_token->isNewLine() : false; 259 } 260 isKeyword() const261 bool VMSourceToken::isKeyword() const { 262 return (m_token) ? m_token->isKeyword() : false; 263 } 264 isVariableName() const265 bool VMSourceToken::isVariableName() const { 266 return (m_token) ? m_token->isVariableName() : false; 267 } 268 isIdentifier() const269 bool VMSourceToken::isIdentifier() const { 270 return (m_token) ? m_token->isIdentifier() : false; 271 } 272 isNumberLiteral() const273 bool VMSourceToken::isNumberLiteral() const { 274 return (m_token) ? m_token->isNumberLiteral() : false; 275 } 276 isStringLiteral() const277 bool VMSourceToken::isStringLiteral() const { 278 return (m_token) ? m_token->isStringLiteral() : false; 279 } 280 isComment() const281 bool VMSourceToken::isComment() const { 282 return (m_token) ? m_token->isComment() : false; 283 } 284 isPreprocessor() const285 bool VMSourceToken::isPreprocessor() const { 286 return (m_token) ? m_token->isPreprocessor() : false; 287 } 288 isMetricPrefix() const289 bool VMSourceToken::isMetricPrefix() const { 290 return (m_token) ? m_token->isMetricPrefix() : false; 291 } 292 isStdUnit() const293 bool VMSourceToken::isStdUnit() const { 294 return (m_token) ? m_token->isStdUnit() : false; 295 } 296 isOther() const297 bool VMSourceToken::isOther() const { 298 return (m_token) ? m_token->isOther() : true; 299 } 300 isIntegerVariable() const301 bool VMSourceToken::isIntegerVariable() const { 302 return (m_token) ? m_token->isIntegerVariable() : false; 303 } 304 isRealVariable() const305 bool VMSourceToken::isRealVariable() const { 306 return (m_token) ? m_token->isRealVariable() : false; 307 } 308 isStringVariable() const309 bool VMSourceToken::isStringVariable() const { 310 return (m_token) ? m_token->isStringVariable() : false; 311 } 312 isIntArrayVariable() const313 bool VMSourceToken::isIntArrayVariable() const { 314 return (m_token) ? m_token->isIntegerArrayVariable() : false; 315 } 316 isRealArrayVariable() const317 bool VMSourceToken::isRealArrayVariable() const { 318 return (m_token) ? m_token->isRealArrayVariable() : false; 319 } 320 isArrayVariable() const321 bool VMSourceToken::isArrayVariable() const { // deprecated API: will be removed ! 322 return isIntArrayVariable(); 323 } 324 isEventHandlerName() const325 bool VMSourceToken::isEventHandlerName() const { 326 return (m_token) ? m_token->isEventHandlerName() : false; 327 } 328 329 } // namespace LinuxSampler 330