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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3747 - (hide annotations) (download)
Sun Feb 16 11:31:46 2020 UTC (4 years, 2 months ago) by schoenebeck
File size: 9802 byte(s)
* NKSP: Fixed re-entrant issue with function calls which caused wrong
  result values if the same function was called multiple times in a term
  (specifically if metric prefixes were used).

* Tests: Added NKSP core language test cases to guard this fixed issue.

* Bumped version (2.1.1.svn50).

1 schoenebeck 2596 /*
2 schoenebeck 3729 * Copyright (c) 2014-2020 Christian Schoenebeck
3 schoenebeck 2596 *
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 schoenebeck 2598 #include <iostream>
12 schoenebeck 2885 #include "editor/SourceToken.h"
13 schoenebeck 2596
14     namespace LinuxSampler {
15    
16 schoenebeck 3561 ///////////////////////////////////////////////////////////////////////
17     // class 'VMUnit'
18    
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    
33     vmfloat VMUnit::unitFactor(MetricPrefix_t prefix) {
34     return _unitFactor(prefix);
35     }
36    
37     vmfloat VMUnit::unitFactor(MetricPrefix_t prefix1, MetricPrefix_t prefix2) {
38     return _unitFactor(prefix1) * _unitFactor(prefix2);
39     }
40    
41 schoenebeck 3581 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 schoenebeck 3561 return f;
46     }
47    
48 schoenebeck 3581 bool VMUnit::hasUnitFactorNow() const {
49     return unitFactor() != VM_NO_FACTOR;
50     }
51    
52 schoenebeck 3561 ///////////////////////////////////////////////////////////////////////
53     // class 'VMExpr'
54    
55 schoenebeck 2596 VMIntExpr* VMExpr::asInt() const {
56     return const_cast<VMIntExpr*>( dynamic_cast<const VMIntExpr*>(this) );
57     }
58    
59 schoenebeck 3573 VMRealExpr* VMExpr::asReal() const {
60     return const_cast<VMRealExpr*>( dynamic_cast<const VMRealExpr*>(this) );
61     }
62    
63 schoenebeck 3582 VMNumberExpr* VMExpr::asNumber() const {
64     return const_cast<VMNumberExpr*>(
65     dynamic_cast<const VMNumberExpr*>(this)
66 schoenebeck 3573 );
67     }
68    
69 schoenebeck 2596 VMStringExpr* VMExpr::asString() const {
70     return const_cast<VMStringExpr*>( dynamic_cast<const VMStringExpr*>(this) );
71     }
72    
73 schoenebeck 2619 VMIntArrayExpr* VMExpr::asIntArray() const {
74     return const_cast<VMIntArrayExpr*>( dynamic_cast<const VMIntArrayExpr*>(this) );
75     }
76    
77 schoenebeck 3573 VMRealArrayExpr* VMExpr::asRealArray() const {
78     return const_cast<VMRealArrayExpr*>(
79     dynamic_cast<const VMRealArrayExpr*>(this)
80     );
81     }
82    
83 schoenebeck 3581 VMArrayExpr* VMExpr::asArray() const {
84     return const_cast<VMArrayExpr*>(
85     dynamic_cast<const VMArrayExpr*>(this)
86     );
87     }
88    
89 schoenebeck 2945 bool VMExpr::isModifyable() const {
90     const VMVariable* var = dynamic_cast<const VMVariable*>(this);
91     return (!var) ? false : var->isAssignable();
92     }
93    
94 schoenebeck 3561 bool VMFunction::acceptsArgUnitType(vmint iArg, StdUnit_t type) const {
95     return type == VM_NO_UNIT;
96     }
97    
98 schoenebeck 3564 bool VMFunction::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const {
99 schoenebeck 3561 return false;
100     }
101    
102     bool VMFunction::acceptsArgFinal(vmint iArg) const {
103     return false;
104     }
105    
106 schoenebeck 3581 void VMFunction::checkArgs(VMFnArgs* args,
107     std::function<void(String)> err,
108     std::function<void(String)> wrn)
109     {
110     }
111    
112 schoenebeck 2598 void VMFunction::wrnMsg(const String& txt) {
113     std::cout << "[ScriptVM] " << txt << std::endl;
114     }
115    
116     void VMFunction::errMsg(const String& txt) {
117     std::cerr << "[ScriptVM] " << txt << std::endl;
118     }
119 schoenebeck 3561
120     ///////////////////////////////////////////////////////////////////////
121 schoenebeck 3582 // class 'VMNumberExpr'
122 schoenebeck 3581
123 schoenebeck 3582 vmint VMNumberExpr::evalCastInt() {
124 schoenebeck 3581 if (exprType() == INT_EXPR)
125     return asInt()->evalInt();
126     else
127     return vmint( asReal()->evalReal() );
128     }
129    
130 schoenebeck 3582 vmfloat VMNumberExpr::evalCastReal() {
131 schoenebeck 3581 if (exprType() == REAL_EXPR)
132     return asReal()->evalReal();
133     else
134     return vmfloat( asInt()->evalInt() );
135     }
136    
137 schoenebeck 3584 vmint VMNumberExpr::evalCastInt(MetricPrefix_t prefix) {
138     vmfloat f = evalCastReal();
139     vmfloat factor = unitFactor() / _unitFactor(prefix);
140     return vmint(f * factor);
141     }
142    
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    
150     vmfloat VMNumberExpr::evalCastReal(MetricPrefix_t prefix) {
151     vmfloat f = evalCastReal();
152     vmfloat factor = unitFactor() / _unitFactor(prefix);
153     return f * factor;
154     }
155    
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 schoenebeck 3581 ///////////////////////////////////////////////////////////////////////
164 schoenebeck 3561 // class 'VMIntExpr'
165    
166     vmint VMIntExpr::evalInt(MetricPrefix_t prefix) {
167     vmfloat f = (vmfloat) evalInt();
168     vmfloat factor = unitFactor() / _unitFactor(prefix);
169 schoenebeck 3584 return vmint(f * factor);
170 schoenebeck 3561 }
171    
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 schoenebeck 3584 return vmint(f * factor);
177 schoenebeck 3561 }
178 schoenebeck 3573
179 schoenebeck 2885 ///////////////////////////////////////////////////////////////////////
180 schoenebeck 3573 // class 'VMRealExpr'
181    
182     vmfloat VMRealExpr::evalReal(MetricPrefix_t prefix) {
183     vmfloat f = evalReal();
184     vmfloat factor = unitFactor() / _unitFactor(prefix);
185     return f * factor;
186     }
187    
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 schoenebeck 3747 // class 'VMFnResult'
197    
198     VMFnResult::~VMFnResult() {
199     }
200    
201     ///////////////////////////////////////////////////////////////////////
202 schoenebeck 2885 // class 'VMSourceToken'
203 schoenebeck 2598
204 schoenebeck 2885 VMSourceToken::VMSourceToken() : m_token(NULL) {
205     }
206    
207     VMSourceToken::VMSourceToken(SourceToken* ct) : m_token(ct) {
208     }
209    
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    
217     VMSourceToken::~VMSourceToken() {
218     if (m_token) {
219     delete m_token;
220     m_token = NULL;
221     }
222     }
223    
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    
233     String VMSourceToken::text() const {
234     return (m_token) ? m_token->text() : "";
235     }
236    
237     int VMSourceToken::firstLine() const {
238     return (m_token) ? m_token->firstLine() : 0;
239     }
240    
241     int VMSourceToken::firstColumn() const {
242     return (m_token) ? m_token->firstColumn() : 0;
243     }
244    
245 schoenebeck 3729 int VMSourceToken::firstByte() const {
246     return (m_token) ? m_token->firstByte() : 0;
247     }
248    
249     int VMSourceToken::lengthBytes() const {
250     return (m_token) ? m_token->lengthBytes() : 0;
251     }
252    
253 schoenebeck 2885 bool VMSourceToken::isEOF() const {
254     return (m_token) ? m_token->isEOF() : true;
255     }
256    
257     bool VMSourceToken::isNewLine() const {
258     return (m_token) ? m_token->isNewLine() : false;
259     }
260    
261     bool VMSourceToken::isKeyword() const {
262     return (m_token) ? m_token->isKeyword() : false;
263     }
264    
265     bool VMSourceToken::isVariableName() const {
266     return (m_token) ? m_token->isVariableName() : false;
267     }
268    
269     bool VMSourceToken::isIdentifier() const {
270     return (m_token) ? m_token->isIdentifier() : false;
271     }
272    
273     bool VMSourceToken::isNumberLiteral() const {
274     return (m_token) ? m_token->isNumberLiteral() : false;
275     }
276    
277     bool VMSourceToken::isStringLiteral() const {
278     return (m_token) ? m_token->isStringLiteral() : false;
279     }
280    
281     bool VMSourceToken::isComment() const {
282     return (m_token) ? m_token->isComment() : false;
283     }
284    
285     bool VMSourceToken::isPreprocessor() const {
286     return (m_token) ? m_token->isPreprocessor() : false;
287     }
288    
289 schoenebeck 3562 bool VMSourceToken::isMetricPrefix() const {
290     return (m_token) ? m_token->isMetricPrefix() : false;
291     }
292    
293     bool VMSourceToken::isStdUnit() const {
294     return (m_token) ? m_token->isStdUnit() : false;
295     }
296    
297 schoenebeck 2885 bool VMSourceToken::isOther() const {
298     return (m_token) ? m_token->isOther() : true;
299     }
300    
301     bool VMSourceToken::isIntegerVariable() const {
302     return (m_token) ? m_token->isIntegerVariable() : false;
303     }
304    
305 schoenebeck 3573 bool VMSourceToken::isRealVariable() const {
306     return (m_token) ? m_token->isRealVariable() : false;
307     }
308    
309 schoenebeck 2885 bool VMSourceToken::isStringVariable() const {
310     return (m_token) ? m_token->isStringVariable() : false;
311     }
312    
313 schoenebeck 3573 bool VMSourceToken::isIntArrayVariable() const {
314     return (m_token) ? m_token->isIntegerArrayVariable() : false;
315 schoenebeck 2885 }
316    
317 schoenebeck 3573 bool VMSourceToken::isRealArrayVariable() const {
318     return (m_token) ? m_token->isRealArrayVariable() : false;
319     }
320    
321     bool VMSourceToken::isArrayVariable() const { // deprecated API: will be removed !
322     return isIntArrayVariable();
323     }
324    
325 schoenebeck 2885 bool VMSourceToken::isEventHandlerName() const {
326     return (m_token) ? m_token->isEventHandlerName() : false;
327     }
328    
329 schoenebeck 2596 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC