1 |
/* |
/* |
2 |
* Copyright (c) 2014-2017 Christian Schoenebeck |
* Copyright (c) 2014-2019 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
15 |
#include <stdlib.h> |
#include <stdlib.h> |
16 |
#include "tree.h" |
#include "tree.h" |
17 |
#include "ScriptVM.h" |
#include "ScriptVM.h" |
18 |
|
#include "../common/RTMath.h" |
19 |
|
|
20 |
namespace LinuxSampler { |
namespace LinuxSampler { |
21 |
|
|
35 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
36 |
// class VMIntResultFunction |
// class VMIntResultFunction |
37 |
|
|
38 |
VMFnResult* VMIntResultFunction::errorResult(int i) { |
VMFnResult* VMIntResultFunction::errorResult(vmint i) { |
39 |
result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
40 |
result.value = i; |
result.value = i; |
41 |
return &result; |
return &result; |
42 |
} |
} |
43 |
|
|
44 |
VMFnResult* VMIntResultFunction::successResult(int i) { |
VMFnResult* VMIntResultFunction::successResult(vmint i) { |
45 |
result.flags = STMT_SUCCESS; |
result.flags = STMT_SUCCESS; |
46 |
result.value = i; |
result.value = i; |
47 |
return &result; |
return &result; |
48 |
} |
} |
49 |
|
|
50 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
51 |
|
// class VMRealResultFunction |
52 |
|
|
53 |
|
VMFnResult* VMRealResultFunction::errorResult(vmfloat f) { |
54 |
|
result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
55 |
|
result.value = f; |
56 |
|
return &result; |
57 |
|
} |
58 |
|
|
59 |
|
VMFnResult* VMRealResultFunction::successResult(vmfloat f) { |
60 |
|
result.flags = STMT_SUCCESS; |
61 |
|
result.value = f; |
62 |
|
return &result; |
63 |
|
} |
64 |
|
|
65 |
|
/////////////////////////////////////////////////////////////////////////// |
66 |
// class VMStringResultFunction |
// class VMStringResultFunction |
67 |
|
|
68 |
VMFnResult* VMStringResultFunction::errorResult(const String& s) { |
VMFnResult* VMStringResultFunction::errorResult(const String& s) { |
80 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
81 |
// built-in script function: message() |
// built-in script function: message() |
82 |
|
|
83 |
bool CoreVMFunction_message::acceptsArgType(int iArg, ExprType_t type) const { |
bool CoreVMFunction_message::acceptsArgType(vmint iArg, ExprType_t type) const { |
84 |
return type == INT_EXPR || type == STRING_EXPR; |
return type == INT_EXPR || type == REAL_EXPR || type == STRING_EXPR; |
85 |
} |
} |
86 |
|
|
87 |
VMFnResult* CoreVMFunction_message::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_message::exec(VMFnArgs* args) { |
88 |
if (!args->argsCount()) return errorResult(); |
if (!args->argsCount()) return errorResult(); |
89 |
|
|
90 |
|
uint64_t usecs = RTMath::unsafeMicroSeconds(RTMath::real_clock); |
91 |
|
|
92 |
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(args->arg(0)); |
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(args->arg(0)); |
93 |
if (strExpr) { |
if (strExpr) { |
94 |
std::cout << "[ScriptVM] " << strExpr->evalStr() << "\n"; |
printf("[ScriptVM %.3f] %s\n", usecs/1000000.f, strExpr->evalStr().c_str()); |
95 |
|
return successResult(); |
96 |
|
} |
97 |
|
|
98 |
|
VMRealExpr* realExpr = dynamic_cast<VMRealExpr*>(args->arg(0)); |
99 |
|
if (realExpr) { |
100 |
|
printf("[ScriptVM %.3f] %f\n", usecs/1000000.f, realExpr->evalReal()); |
101 |
return successResult(); |
return successResult(); |
102 |
} |
} |
103 |
|
|
104 |
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
105 |
if (intExpr) { |
if (intExpr) { |
106 |
std::cout << "[ScriptVM] " << intExpr->evalInt() << "\n"; |
printf("[ScriptVM %.3f] %lld\n", usecs/1000000.f, (int64_t)intExpr->evalInt()); |
107 |
return successResult(); |
return successResult(); |
108 |
} |
} |
109 |
|
|
113 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
114 |
// built-in script function: exit() |
// built-in script function: exit() |
115 |
|
|
116 |
|
vmint CoreVMFunction_exit::maxAllowedArgs() const { |
117 |
|
return (vm->isExitResultEnabled()) ? 1 : 0; |
118 |
|
} |
119 |
|
|
120 |
|
bool CoreVMFunction_exit::acceptsArgType(vmint iArg, ExprType_t type) const { |
121 |
|
if (!vm->isExitResultEnabled()) return false; |
122 |
|
return type == INT_EXPR || type == REAL_EXPR || type == STRING_EXPR; |
123 |
|
} |
124 |
|
|
125 |
VMFnResult* CoreVMFunction_exit::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_exit::exec(VMFnArgs* args) { |
126 |
this->result.flags = STMT_ABORT_SIGNALLED; |
this->result.flags = STMT_ABORT_SIGNALLED; |
127 |
|
if (vm->isExitResultEnabled() && args->argsCount()) { |
128 |
|
ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext()); |
129 |
|
switch (args->arg(0)->exprType()) { |
130 |
|
case INT_EXPR: |
131 |
|
ctx->exitRes.intLiteral.value = args->arg(0)->asInt()->evalInt(); |
132 |
|
ctx->exitRes.value = &ctx->exitRes.intLiteral; |
133 |
|
break; |
134 |
|
case REAL_EXPR: |
135 |
|
ctx->exitRes.realLiteral.value = args->arg(0)->asReal()->evalReal(); |
136 |
|
ctx->exitRes.value = &ctx->exitRes.realLiteral; |
137 |
|
break; |
138 |
|
case STRING_EXPR: |
139 |
|
ctx->exitRes.stringLiteral.value = args->arg(0)->asString()->evalStr(); |
140 |
|
ctx->exitRes.value = &ctx->exitRes.stringLiteral; |
141 |
|
break; |
142 |
|
default: |
143 |
|
; // noop - just to shut up the compiler |
144 |
|
} |
145 |
|
} |
146 |
return &result; |
return &result; |
147 |
} |
} |
148 |
|
|
149 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
150 |
// built-in script function: wait() |
// built-in script function: wait() |
151 |
|
|
152 |
|
bool CoreVMFunction_wait::acceptsArgUnitType(vmint iArg, StdUnit_t type) const { |
153 |
|
if (iArg == 0) |
154 |
|
return type == VM_NO_UNIT || type == VM_SECOND; |
155 |
|
else |
156 |
|
return type == VM_NO_UNIT; |
157 |
|
} |
158 |
|
|
159 |
|
bool CoreVMFunction_wait::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const { |
160 |
|
return iArg == 0 && type == VM_SECOND; |
161 |
|
} |
162 |
|
|
163 |
VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) { |
164 |
ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext()); |
ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext()); |
165 |
VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
166 |
int us = expr->evalInt(); |
StdUnit_t unit = expr->unitType(); |
167 |
|
vmint us = (unit) ? expr->evalInt(VM_MICRO) : expr->evalInt(); |
168 |
if (us < 0) { |
if (us < 0) { |
169 |
wrnMsg("wait(): argument may not be negative! Aborting script!"); |
wrnMsg("wait(): argument may not be negative! Aborting script!"); |
170 |
this->result.flags = STMT_ABORT_SIGNALLED; |
this->result.flags = STMT_ABORT_SIGNALLED; |
181 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
182 |
// built-in script function: abs() |
// built-in script function: abs() |
183 |
|
|
184 |
bool CoreVMFunction_abs::acceptsArgType(int iArg, ExprType_t type) const { |
bool CoreVMFunction_abs::acceptsArgType(vmint iArg, ExprType_t type) const { |
185 |
return type == INT_EXPR; |
return type == INT_EXPR; |
186 |
} |
} |
187 |
|
|
192 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
193 |
// built-in script function: random() |
// built-in script function: random() |
194 |
|
|
195 |
bool CoreVMFunction_random::acceptsArgType(int iArg, ExprType_t type) const { |
bool CoreVMFunction_random::acceptsArgType(vmint iArg, ExprType_t type) const { |
196 |
return type == INT_EXPR; |
return type == INT_EXPR; |
197 |
} |
} |
198 |
|
|
199 |
VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) { |
200 |
int iMin = args->arg(0)->asInt()->evalInt(); |
vmint iMin = args->arg(0)->asInt()->evalInt(); |
201 |
int iMax = args->arg(1)->asInt()->evalInt(); |
vmint iMax = args->arg(1)->asInt()->evalInt(); |
202 |
float f = float(::rand()) / float(RAND_MAX); |
float f = float(::rand()) / float(RAND_MAX); |
203 |
return successResult( |
return successResult( |
204 |
iMin + roundf( f * float(iMax - iMin) ) |
iMin + roundf( f * float(iMax - iMin) ) |
208 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
209 |
// built-in script function: num_elements() |
// built-in script function: num_elements() |
210 |
|
|
211 |
bool CoreVMFunction_num_elements::acceptsArgType(int iArg, ExprType_t type) const { |
bool CoreVMFunction_num_elements::acceptsArgType(vmint iArg, ExprType_t type) const { |
212 |
return type == INT_ARR_EXPR; |
return type == INT_ARR_EXPR; |
213 |
} |
} |
214 |
|
|
224 |
VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg); |
VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg); |
225 |
VMVariable* out = dynamic_cast<VMVariable*>(arg); |
VMVariable* out = dynamic_cast<VMVariable*>(arg); |
226 |
if (!in || !out) successResult(0); |
if (!in || !out) successResult(0); |
227 |
int i = in->evalInt() + 1; |
vmint i = in->evalInt() + 1; |
228 |
IntLiteral tmp(i); |
IntLiteral tmp(i); |
229 |
out->assignExpr(&tmp); |
out->assignExpr(&tmp); |
230 |
return successResult(i); |
return successResult(i); |
238 |
VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg); |
VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg); |
239 |
VMVariable* out = dynamic_cast<VMVariable*>(arg); |
VMVariable* out = dynamic_cast<VMVariable*>(arg); |
240 |
if (!in || !out) successResult(0); |
if (!in || !out) successResult(0); |
241 |
int i = in->evalInt() - 1; |
vmint i = in->evalInt() - 1; |
242 |
IntLiteral tmp(i); |
IntLiteral tmp(i); |
243 |
out->assignExpr(&tmp); |
out->assignExpr(&tmp); |
244 |
return successResult(i); |
return successResult(i); |
248 |
// built-in script function: in_range() |
// built-in script function: in_range() |
249 |
|
|
250 |
VMFnResult* CoreVMFunction_in_range::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_in_range::exec(VMFnArgs* args) { |
251 |
int i = args->arg(0)->asInt()->evalInt(); |
vmint i = args->arg(0)->asInt()->evalInt(); |
252 |
int lo = args->arg(1)->asInt()->evalInt(); |
vmint lo = args->arg(1)->asInt()->evalInt(); |
253 |
int hi = args->arg(2)->asInt()->evalInt(); |
vmint hi = args->arg(2)->asInt()->evalInt(); |
254 |
if (lo > hi) { // swap lo and hi |
if (lo > hi) { // swap lo and hi |
255 |
int tmp = lo; |
vmint tmp = lo; |
256 |
lo = hi; |
lo = hi; |
257 |
hi = tmp; |
hi = tmp; |
258 |
} |
} |
263 |
// built-in script function: sh_left() |
// built-in script function: sh_left() |
264 |
|
|
265 |
VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) { |
266 |
int i = args->arg(0)->asInt()->evalInt(); |
vmint i = args->arg(0)->asInt()->evalInt(); |
267 |
int n = args->arg(1)->asInt()->evalInt(); |
vmint n = args->arg(1)->asInt()->evalInt(); |
268 |
return successResult(i << n); |
return successResult(i << n); |
269 |
} |
} |
270 |
|
|
272 |
// built-in script function: sh_right() |
// built-in script function: sh_right() |
273 |
|
|
274 |
VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) { |
275 |
int i = args->arg(0)->asInt()->evalInt(); |
vmint i = args->arg(0)->asInt()->evalInt(); |
276 |
int n = args->arg(1)->asInt()->evalInt(); |
vmint n = args->arg(1)->asInt()->evalInt(); |
277 |
return successResult(i >> n); |
return successResult(i >> n); |
278 |
} |
} |
279 |
|
|
281 |
// built-in script function: min() |
// built-in script function: min() |
282 |
|
|
283 |
VMFnResult* CoreVMFunction_min::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_min::exec(VMFnArgs* args) { |
284 |
int l = args->arg(0)->asInt()->evalInt(); |
vmint l = args->arg(0)->asInt()->evalInt(); |
285 |
int r = args->arg(1)->asInt()->evalInt(); |
vmint r = args->arg(1)->asInt()->evalInt(); |
286 |
return successResult(l < r ? l : r); |
return successResult(l < r ? l : r); |
287 |
} |
} |
288 |
|
|
290 |
// built-in script function: max() |
// built-in script function: max() |
291 |
|
|
292 |
VMFnResult* CoreVMFunction_max::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_max::exec(VMFnArgs* args) { |
293 |
int l = args->arg(0)->asInt()->evalInt(); |
vmint l = args->arg(0)->asInt()->evalInt(); |
294 |
int r = args->arg(1)->asInt()->evalInt(); |
vmint r = args->arg(1)->asInt()->evalInt(); |
295 |
return successResult(l > r ? l : r); |
return successResult(l > r ? l : r); |
296 |
} |
} |
297 |
|
|
305 |
wrnMsg("array_equal(): the two arrays differ in size"); |
wrnMsg("array_equal(): the two arrays differ in size"); |
306 |
return successResult(0); // false |
return successResult(0); // false |
307 |
} |
} |
308 |
const int n = l->arraySize(); |
const vmint n = l->arraySize(); |
309 |
for (int i = 0; i < n; ++i) |
for (vmint i = 0; i < n; ++i) |
310 |
if (l->evalIntElement(i) != r->evalIntElement(i)) |
if (l->evalIntElement(i) != r->evalIntElement(i)) |
311 |
return successResult(0); // false |
return successResult(0); // false |
312 |
return successResult(1); // true |
return successResult(1); // true |
315 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
316 |
// built-in script function: search() |
// built-in script function: search() |
317 |
|
|
318 |
ExprType_t CoreVMFunction_search::argType(int iArg) const { |
ExprType_t CoreVMFunction_search::argType(vmint iArg) const { |
319 |
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
320 |
} |
} |
321 |
|
|
322 |
bool CoreVMFunction_search::acceptsArgType(int iArg, ExprType_t type) const { |
bool CoreVMFunction_search::acceptsArgType(vmint iArg, ExprType_t type) const { |
323 |
if (iArg == 0) |
if (iArg == 0) |
324 |
return type == INT_ARR_EXPR; |
return type == INT_ARR_EXPR; |
325 |
else |
else |
328 |
|
|
329 |
VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) { |
330 |
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
331 |
const int needle = args->arg(1)->asInt()->evalInt(); |
const vmint needle = args->arg(1)->asInt()->evalInt(); |
332 |
const int n = a->arraySize(); |
const vmint n = a->arraySize(); |
333 |
for (int i = 0; i < n; ++i) |
for (vmint i = 0; i < n; ++i) |
334 |
if (a->evalIntElement(i) == needle) |
if (a->evalIntElement(i) == needle) |
335 |
return successResult(i); |
return successResult(i); |
336 |
return successResult(-1); // not found |
return successResult(-1); // not found |
339 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
340 |
// built-in script function: sort() |
// built-in script function: sort() |
341 |
|
|
342 |
ExprType_t CoreVMFunction_sort::argType(int iArg) const { |
ExprType_t CoreVMFunction_sort::argType(vmint iArg) const { |
343 |
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
344 |
} |
} |
345 |
|
|
346 |
bool CoreVMFunction_sort::acceptsArgType(int iArg, ExprType_t type) const { |
bool CoreVMFunction_sort::acceptsArgType(vmint iArg, ExprType_t type) const { |
347 |
if (iArg == 0) |
if (iArg == 0) |
348 |
return type == INT_ARR_EXPR; |
return type == INT_ARR_EXPR; |
349 |
else |
else |
352 |
|
|
353 |
struct ArrElemPOD { |
struct ArrElemPOD { |
354 |
VMIntArrayExpr* m_array; |
VMIntArrayExpr* m_array; |
355 |
int m_index; |
vmint m_index; |
356 |
}; |
}; |
357 |
|
|
358 |
static inline void swap(class ArrElemRef a, class ArrElemRef b); |
static inline void swap(class ArrElemRef a, class ArrElemRef b); |
363 |
m_array = NULL; |
m_array = NULL; |
364 |
m_index = 0; |
m_index = 0; |
365 |
} |
} |
366 |
ArrElemRef(VMIntArrayExpr* a, int index) { |
ArrElemRef(VMIntArrayExpr* a, vmint index) { |
367 |
m_array = a; |
m_array = a; |
368 |
m_index = index; |
m_index = index; |
369 |
} |
} |
371 |
setValue(e.getValue()); |
setValue(e.getValue()); |
372 |
return *this; |
return *this; |
373 |
} |
} |
374 |
inline ArrElemRef& operator=(int val) { |
inline ArrElemRef& operator=(vmint val) { |
375 |
setValue(val); |
setValue(val); |
376 |
return *this; |
return *this; |
377 |
} |
} |
380 |
return true; |
return true; |
381 |
return getValue() == e.getValue(); |
return getValue() == e.getValue(); |
382 |
} |
} |
383 |
inline bool operator==(int val) const { |
inline bool operator==(vmint val) const { |
384 |
return getValue() == val; |
return getValue() == val; |
385 |
} |
} |
386 |
inline bool operator!=(const ArrElemRef& e) const { |
inline bool operator!=(const ArrElemRef& e) const { |
387 |
return !(operator==(e)); |
return !(operator==(e)); |
388 |
} |
} |
389 |
inline bool operator!=(int val) const { |
inline bool operator!=(vmint val) const { |
390 |
return !(operator==(val)); |
return !(operator==(val)); |
391 |
} |
} |
392 |
inline bool operator<(const ArrElemRef& e) const { |
inline bool operator<(const ArrElemRef& e) const { |
394 |
return false; |
return false; |
395 |
return getValue() < e.getValue(); |
return getValue() < e.getValue(); |
396 |
} |
} |
397 |
inline bool operator<(int val) const { |
inline bool operator<(vmint val) const { |
398 |
return getValue() < val; |
return getValue() < val; |
399 |
} |
} |
400 |
inline bool operator>(const ArrElemRef& e) const { |
inline bool operator>(const ArrElemRef& e) const { |
402 |
return false; |
return false; |
403 |
return getValue() > e.getValue(); |
return getValue() > e.getValue(); |
404 |
} |
} |
405 |
inline bool operator>(int val) const { |
inline bool operator>(vmint val) const { |
406 |
return getValue() > val; |
return getValue() > val; |
407 |
} |
} |
408 |
inline bool operator<=(const ArrElemRef& e) const { |
inline bool operator<=(const ArrElemRef& e) const { |
410 |
return true; |
return true; |
411 |
return getValue() <= e.getValue(); |
return getValue() <= e.getValue(); |
412 |
} |
} |
413 |
inline bool operator<=(int val) const { |
inline bool operator<=(vmint val) const { |
414 |
return getValue() <= val; |
return getValue() <= val; |
415 |
} |
} |
416 |
inline bool operator>=(const ArrElemRef& e) const { |
inline bool operator>=(const ArrElemRef& e) const { |
418 |
return true; |
return true; |
419 |
return getValue() >= e.getValue(); |
return getValue() >= e.getValue(); |
420 |
} |
} |
421 |
inline bool operator>=(int val) const { |
inline bool operator>=(vmint val) const { |
422 |
return getValue() >= val; |
return getValue() >= val; |
423 |
} |
} |
424 |
inline operator int() const { |
inline operator vmint() const { |
425 |
return getValue(); |
return getValue(); |
426 |
} |
} |
427 |
protected: |
protected: |
428 |
inline int getValue() const { |
inline vmint getValue() const { |
429 |
return m_array->evalIntElement(m_index); |
return m_array->evalIntElement(m_index); |
430 |
} |
} |
431 |
inline void setValue(int value) { |
inline void setValue(vmint value) { |
432 |
m_array->assignIntElement(m_index, value); |
m_array->assignIntElement(m_index, value); |
433 |
} |
} |
434 |
|
|
441 |
m_array = NULL; |
m_array = NULL; |
442 |
m_index = 0; |
m_index = 0; |
443 |
} |
} |
444 |
ArrElemPtr(VMIntArrayExpr* a, int index) { |
ArrElemPtr(VMIntArrayExpr* a, vmint index) { |
445 |
m_array = a; |
m_array = a; |
446 |
m_index = index; |
m_index = index; |
447 |
} |
} |
451 |
}; |
}; |
452 |
|
|
453 |
static inline void swap(ArrElemRef a, ArrElemRef b) { |
static inline void swap(ArrElemRef a, ArrElemRef b) { |
454 |
int valueA = a.getValue(); |
vmint valueA = a.getValue(); |
455 |
int valueB = b.getValue(); |
vmint valueB = b.getValue(); |
456 |
a.setValue(valueB); |
a.setValue(valueB); |
457 |
b.setValue(valueA); |
b.setValue(valueA); |
458 |
} |
} |
460 |
class ArrExprIter : public ArrElemPOD { |
class ArrExprIter : public ArrElemPOD { |
461 |
public: |
public: |
462 |
typedef std::random_access_iterator_tag iterator_category; |
typedef std::random_access_iterator_tag iterator_category; |
463 |
typedef int value_type; |
typedef vmint value_type; |
464 |
typedef ssize_t difference_type; |
typedef ssize_t difference_type; |
465 |
typedef ArrElemPtr pointer; |
typedef ArrElemPtr pointer; |
466 |
typedef ArrElemRef reference; |
typedef ArrElemRef reference; |
467 |
|
|
468 |
ArrExprIter(VMIntArrayExpr* a, int index) { |
ArrExprIter(VMIntArrayExpr* a, vmint index) { |
469 |
m_array = a; |
m_array = a; |
470 |
m_index = index; |
m_index = index; |
471 |
} |
} |
490 |
--m_index; |
--m_index; |
491 |
return it; |
return it; |
492 |
} |
} |
493 |
|
inline ArrExprIter& operator+=(difference_type d) { |
494 |
|
m_index += d; |
495 |
|
return *this; |
496 |
|
} |
497 |
|
inline ArrExprIter& operator-=(difference_type d) { |
498 |
|
m_index -= d; |
499 |
|
return *this; |
500 |
|
} |
501 |
inline bool operator==(const ArrExprIter& other) const { |
inline bool operator==(const ArrExprIter& other) const { |
502 |
return m_index == other.m_index; |
return m_index == other.m_index; |
503 |
} |
} |
534 |
}; |
}; |
535 |
|
|
536 |
struct DescArrExprSorter { |
struct DescArrExprSorter { |
537 |
inline bool operator()(const int& a, const int& b) const { |
inline bool operator()(const vmint& a, const vmint& b) const { |
538 |
return a > b; |
return a > b; |
539 |
} |
} |
540 |
}; |
}; |
543 |
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
544 |
bool bAscending = |
bool bAscending = |
545 |
(args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt(); |
(args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt(); |
546 |
int n = a->arraySize(); |
vmint n = a->arraySize(); |
547 |
ArrExprIter itBegin(a, 0); |
ArrExprIter itBegin(a, 0); |
548 |
ArrExprIter itEnd(a, n); |
ArrExprIter itEnd(a, n); |
549 |
if (bAscending) { |
if (bAscending) { |
555 |
return successResult(); |
return successResult(); |
556 |
} |
} |
557 |
|
|
558 |
|
/////////////////////////////////////////////////////////////////////////// |
559 |
|
// built-in script function: real_to_int() and int() |
560 |
|
|
561 |
|
VMFnResult* CoreVMFunction_real_to_int::exec(VMFnArgs* args) { |
562 |
|
vmfloat f = args->arg(0)->asReal()->evalReal(); |
563 |
|
return successResult(vmint(f)); |
564 |
|
} |
565 |
|
|
566 |
|
/////////////////////////////////////////////////////////////////////////// |
567 |
|
// built-in script function: int_to_real() and real() |
568 |
|
|
569 |
|
VMFnResult* CoreVMFunction_int_to_real::exec(VMFnArgs* args) { |
570 |
|
vmint i = args->arg(0)->asInt()->evalInt(); |
571 |
|
return successResult(i); |
572 |
|
} |
573 |
|
|
574 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |