1 |
schoenebeck |
2581 |
/* |
2 |
schoenebeck |
3551 |
* Copyright (c) 2014-2019 Christian Schoenebeck |
3 |
schoenebeck |
2581 |
* |
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 "CoreVMFunctions.h" |
11 |
|
|
|
12 |
|
|
#include <iostream> |
13 |
schoenebeck |
3221 |
#include <algorithm> // for std::sort() |
14 |
schoenebeck |
2619 |
#include <math.h> |
15 |
|
|
#include <stdlib.h> |
16 |
schoenebeck |
2581 |
#include "tree.h" |
17 |
|
|
#include "ScriptVM.h" |
18 |
schoenebeck |
3285 |
#include "../common/RTMath.h" |
19 |
schoenebeck |
2581 |
|
20 |
|
|
namespace LinuxSampler { |
21 |
|
|
|
22 |
schoenebeck |
2727 |
/////////////////////////////////////////////////////////////////////////// |
23 |
|
|
// class VMEmptyResultFunction |
24 |
|
|
|
25 |
schoenebeck |
2581 |
VMFnResult* VMEmptyResultFunction::errorResult() { |
26 |
|
|
result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
27 |
|
|
return &result; |
28 |
|
|
} |
29 |
|
|
|
30 |
schoenebeck |
2598 |
VMFnResult* VMEmptyResultFunction::successResult() { |
31 |
|
|
result.flags = STMT_SUCCESS; |
32 |
|
|
return &result; |
33 |
schoenebeck |
2596 |
} |
34 |
|
|
|
35 |
schoenebeck |
2727 |
/////////////////////////////////////////////////////////////////////////// |
36 |
|
|
// class VMIntResultFunction |
37 |
|
|
|
38 |
schoenebeck |
3557 |
VMFnResult* VMIntResultFunction::errorResult(vmint i) { |
39 |
schoenebeck |
2598 |
result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
40 |
|
|
result.value = i; |
41 |
|
|
return &result; |
42 |
schoenebeck |
2596 |
} |
43 |
|
|
|
44 |
schoenebeck |
3557 |
VMFnResult* VMIntResultFunction::successResult(vmint i) { |
45 |
schoenebeck |
2581 |
result.flags = STMT_SUCCESS; |
46 |
schoenebeck |
2598 |
result.value = i; |
47 |
schoenebeck |
2581 |
return &result; |
48 |
|
|
} |
49 |
|
|
|
50 |
schoenebeck |
2727 |
/////////////////////////////////////////////////////////////////////////// |
51 |
schoenebeck |
3573 |
// 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 |
schoenebeck |
2727 |
// class VMStringResultFunction |
67 |
|
|
|
68 |
schoenebeck |
2581 |
VMFnResult* VMStringResultFunction::errorResult(const String& s) { |
69 |
|
|
result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
70 |
|
|
result.value = s; |
71 |
|
|
return &result; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
VMFnResult* VMStringResultFunction::successResult(const String& s) { |
75 |
|
|
result.flags = STMT_SUCCESS; |
76 |
|
|
result.value = s; |
77 |
|
|
return &result; |
78 |
|
|
} |
79 |
|
|
|
80 |
schoenebeck |
2727 |
/////////////////////////////////////////////////////////////////////////// |
81 |
|
|
// built-in script function: message() |
82 |
|
|
|
83 |
schoenebeck |
3557 |
bool CoreVMFunction_message::acceptsArgType(vmint iArg, ExprType_t type) const { |
84 |
schoenebeck |
3573 |
return type == INT_EXPR || type == REAL_EXPR || type == STRING_EXPR; |
85 |
schoenebeck |
2581 |
} |
86 |
|
|
|
87 |
|
|
VMFnResult* CoreVMFunction_message::exec(VMFnArgs* args) { |
88 |
|
|
if (!args->argsCount()) return errorResult(); |
89 |
|
|
|
90 |
schoenebeck |
3285 |
uint64_t usecs = RTMath::unsafeMicroSeconds(RTMath::real_clock); |
91 |
|
|
|
92 |
schoenebeck |
2581 |
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(args->arg(0)); |
93 |
|
|
if (strExpr) { |
94 |
schoenebeck |
3285 |
printf("[ScriptVM %.3f] %s\n", usecs/1000000.f, strExpr->evalStr().c_str()); |
95 |
schoenebeck |
2581 |
return successResult(); |
96 |
|
|
} |
97 |
|
|
|
98 |
schoenebeck |
3573 |
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(); |
102 |
|
|
} |
103 |
|
|
|
104 |
schoenebeck |
2581 |
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
105 |
|
|
if (intExpr) { |
106 |
schoenebeck |
3557 |
printf("[ScriptVM %.3f] %lld\n", usecs/1000000.f, (int64_t)intExpr->evalInt()); |
107 |
schoenebeck |
2581 |
return successResult(); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
return errorResult(); |
111 |
|
|
} |
112 |
|
|
|
113 |
schoenebeck |
2727 |
/////////////////////////////////////////////////////////////////////////// |
114 |
|
|
// built-in script function: exit() |
115 |
|
|
|
116 |
schoenebeck |
3557 |
vmint CoreVMFunction_exit::maxAllowedArgs() const { |
117 |
schoenebeck |
3551 |
return (vm->isExitResultEnabled()) ? 1 : 0; |
118 |
|
|
} |
119 |
|
|
|
120 |
schoenebeck |
3557 |
bool CoreVMFunction_exit::acceptsArgType(vmint iArg, ExprType_t type) const { |
121 |
schoenebeck |
3551 |
if (!vm->isExitResultEnabled()) return false; |
122 |
|
|
return type == INT_EXPR || type == STRING_EXPR; |
123 |
|
|
} |
124 |
|
|
|
125 |
schoenebeck |
2581 |
VMFnResult* CoreVMFunction_exit::exec(VMFnArgs* args) { |
126 |
|
|
this->result.flags = STMT_ABORT_SIGNALLED; |
127 |
schoenebeck |
3551 |
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 STRING_EXPR: |
135 |
|
|
ctx->exitRes.stringLiteral.value = args->arg(0)->asString()->evalStr(); |
136 |
|
|
ctx->exitRes.value = &ctx->exitRes.stringLiteral; |
137 |
|
|
break; |
138 |
|
|
default: |
139 |
|
|
; // noop - just to shut up the compiler |
140 |
|
|
} |
141 |
|
|
} |
142 |
schoenebeck |
2581 |
return &result; |
143 |
|
|
} |
144 |
|
|
|
145 |
schoenebeck |
2727 |
/////////////////////////////////////////////////////////////////////////// |
146 |
|
|
// built-in script function: wait() |
147 |
|
|
|
148 |
schoenebeck |
3561 |
bool CoreVMFunction_wait::acceptsArgUnitType(vmint iArg, StdUnit_t type) const { |
149 |
|
|
if (iArg == 0) |
150 |
|
|
return type == VM_NO_UNIT || type == VM_SECOND; |
151 |
|
|
else |
152 |
|
|
return type == VM_NO_UNIT; |
153 |
|
|
} |
154 |
|
|
|
155 |
schoenebeck |
3564 |
bool CoreVMFunction_wait::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const { |
156 |
|
|
return iArg == 0 && type == VM_SECOND; |
157 |
schoenebeck |
3561 |
} |
158 |
|
|
|
159 |
schoenebeck |
2581 |
VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) { |
160 |
|
|
ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext()); |
161 |
|
|
VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
162 |
schoenebeck |
3564 |
StdUnit_t unit = expr->unitType(); |
163 |
|
|
vmint us = (unit) ? expr->evalInt(VM_MICRO) : expr->evalInt(); |
164 |
schoenebeck |
2972 |
if (us < 0) { |
165 |
|
|
wrnMsg("wait(): argument may not be negative! Aborting script!"); |
166 |
|
|
this->result.flags = STMT_ABORT_SIGNALLED; |
167 |
|
|
} else if (us == 0) { |
168 |
|
|
wrnMsg("wait(): argument may not be zero! Aborting script!"); |
169 |
|
|
this->result.flags = STMT_ABORT_SIGNALLED; |
170 |
|
|
} else { |
171 |
|
|
ctx->suspendMicroseconds = us; |
172 |
|
|
this->result.flags = STMT_SUSPEND_SIGNALLED; |
173 |
|
|
} |
174 |
schoenebeck |
2581 |
return &result; |
175 |
|
|
} |
176 |
|
|
|
177 |
schoenebeck |
2727 |
/////////////////////////////////////////////////////////////////////////// |
178 |
|
|
// built-in script function: abs() |
179 |
|
|
|
180 |
schoenebeck |
3557 |
bool CoreVMFunction_abs::acceptsArgType(vmint iArg, ExprType_t type) const { |
181 |
schoenebeck |
2619 |
return type == INT_EXPR; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
VMFnResult* CoreVMFunction_abs::exec(VMFnArgs* args) { |
185 |
|
|
return successResult( ::abs(args->arg(0)->asInt()->evalInt()) ); |
186 |
|
|
} |
187 |
|
|
|
188 |
schoenebeck |
2727 |
/////////////////////////////////////////////////////////////////////////// |
189 |
|
|
// built-in script function: random() |
190 |
|
|
|
191 |
schoenebeck |
3557 |
bool CoreVMFunction_random::acceptsArgType(vmint iArg, ExprType_t type) const { |
192 |
schoenebeck |
2619 |
return type == INT_EXPR; |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) { |
196 |
schoenebeck |
3557 |
vmint iMin = args->arg(0)->asInt()->evalInt(); |
197 |
|
|
vmint iMax = args->arg(1)->asInt()->evalInt(); |
198 |
schoenebeck |
2620 |
float f = float(::rand()) / float(RAND_MAX); |
199 |
schoenebeck |
2619 |
return successResult( |
200 |
|
|
iMin + roundf( f * float(iMax - iMin) ) |
201 |
|
|
); |
202 |
|
|
} |
203 |
|
|
|
204 |
schoenebeck |
2727 |
/////////////////////////////////////////////////////////////////////////// |
205 |
|
|
// built-in script function: num_elements() |
206 |
|
|
|
207 |
schoenebeck |
3557 |
bool CoreVMFunction_num_elements::acceptsArgType(vmint iArg, ExprType_t type) const { |
208 |
schoenebeck |
2619 |
return type == INT_ARR_EXPR; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
VMFnResult* CoreVMFunction_num_elements::exec(VMFnArgs* args) { |
212 |
|
|
return successResult( args->arg(0)->asIntArray()->arraySize() ); |
213 |
|
|
} |
214 |
|
|
|
215 |
schoenebeck |
2945 |
/////////////////////////////////////////////////////////////////////////// |
216 |
|
|
// built-in script function: inc() |
217 |
|
|
|
218 |
|
|
VMFnResult* CoreVMFunction_inc::exec(VMFnArgs* args) { |
219 |
|
|
VMExpr* arg = args->arg(0); |
220 |
|
|
VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg); |
221 |
|
|
VMVariable* out = dynamic_cast<VMVariable*>(arg); |
222 |
|
|
if (!in || !out) successResult(0); |
223 |
schoenebeck |
3557 |
vmint i = in->evalInt() + 1; |
224 |
schoenebeck |
2945 |
IntLiteral tmp(i); |
225 |
|
|
out->assignExpr(&tmp); |
226 |
|
|
return successResult(i); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
/////////////////////////////////////////////////////////////////////////// |
230 |
|
|
// built-in script function: dec() |
231 |
|
|
|
232 |
|
|
VMFnResult* CoreVMFunction_dec::exec(VMFnArgs* args) { |
233 |
|
|
VMExpr* arg = args->arg(0); |
234 |
|
|
VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg); |
235 |
|
|
VMVariable* out = dynamic_cast<VMVariable*>(arg); |
236 |
|
|
if (!in || !out) successResult(0); |
237 |
schoenebeck |
3557 |
vmint i = in->evalInt() - 1; |
238 |
schoenebeck |
2945 |
IntLiteral tmp(i); |
239 |
|
|
out->assignExpr(&tmp); |
240 |
|
|
return successResult(i); |
241 |
|
|
} |
242 |
|
|
|
243 |
schoenebeck |
2965 |
/////////////////////////////////////////////////////////////////////////// |
244 |
schoenebeck |
3076 |
// built-in script function: in_range() |
245 |
|
|
|
246 |
|
|
VMFnResult* CoreVMFunction_in_range::exec(VMFnArgs* args) { |
247 |
schoenebeck |
3557 |
vmint i = args->arg(0)->asInt()->evalInt(); |
248 |
|
|
vmint lo = args->arg(1)->asInt()->evalInt(); |
249 |
|
|
vmint hi = args->arg(2)->asInt()->evalInt(); |
250 |
schoenebeck |
3076 |
if (lo > hi) { // swap lo and hi |
251 |
schoenebeck |
3557 |
vmint tmp = lo; |
252 |
schoenebeck |
3076 |
lo = hi; |
253 |
|
|
hi = tmp; |
254 |
|
|
} |
255 |
|
|
return successResult(i >= lo && i <= hi); |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
/////////////////////////////////////////////////////////////////////////// |
259 |
schoenebeck |
2965 |
// built-in script function: sh_left() |
260 |
|
|
|
261 |
|
|
VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) { |
262 |
schoenebeck |
3557 |
vmint i = args->arg(0)->asInt()->evalInt(); |
263 |
|
|
vmint n = args->arg(1)->asInt()->evalInt(); |
264 |
schoenebeck |
2965 |
return successResult(i << n); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
/////////////////////////////////////////////////////////////////////////// |
268 |
|
|
// built-in script function: sh_right() |
269 |
|
|
|
270 |
|
|
VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) { |
271 |
schoenebeck |
3557 |
vmint i = args->arg(0)->asInt()->evalInt(); |
272 |
|
|
vmint n = args->arg(1)->asInt()->evalInt(); |
273 |
schoenebeck |
2965 |
return successResult(i >> n); |
274 |
|
|
} |
275 |
|
|
|
276 |
schoenebeck |
2970 |
/////////////////////////////////////////////////////////////////////////// |
277 |
|
|
// built-in script function: min() |
278 |
|
|
|
279 |
|
|
VMFnResult* CoreVMFunction_min::exec(VMFnArgs* args) { |
280 |
schoenebeck |
3557 |
vmint l = args->arg(0)->asInt()->evalInt(); |
281 |
|
|
vmint r = args->arg(1)->asInt()->evalInt(); |
282 |
schoenebeck |
2970 |
return successResult(l < r ? l : r); |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
/////////////////////////////////////////////////////////////////////////// |
286 |
|
|
// built-in script function: max() |
287 |
|
|
|
288 |
|
|
VMFnResult* CoreVMFunction_max::exec(VMFnArgs* args) { |
289 |
schoenebeck |
3557 |
vmint l = args->arg(0)->asInt()->evalInt(); |
290 |
|
|
vmint r = args->arg(1)->asInt()->evalInt(); |
291 |
schoenebeck |
2970 |
return successResult(l > r ? l : r); |
292 |
|
|
} |
293 |
|
|
|
294 |
schoenebeck |
3221 |
/////////////////////////////////////////////////////////////////////////// |
295 |
|
|
// built-in script function: array_equal() |
296 |
|
|
|
297 |
|
|
VMFnResult* CoreVMFunction_array_equal::exec(VMFnArgs* args) { |
298 |
|
|
VMIntArrayExpr* l = args->arg(0)->asIntArray(); |
299 |
|
|
VMIntArrayExpr* r = args->arg(1)->asIntArray(); |
300 |
|
|
if (l->arraySize() != r->arraySize()) { |
301 |
|
|
wrnMsg("array_equal(): the two arrays differ in size"); |
302 |
|
|
return successResult(0); // false |
303 |
|
|
} |
304 |
schoenebeck |
3557 |
const vmint n = l->arraySize(); |
305 |
|
|
for (vmint i = 0; i < n; ++i) |
306 |
schoenebeck |
3221 |
if (l->evalIntElement(i) != r->evalIntElement(i)) |
307 |
|
|
return successResult(0); // false |
308 |
|
|
return successResult(1); // true |
309 |
|
|
} |
310 |
|
|
|
311 |
|
|
/////////////////////////////////////////////////////////////////////////// |
312 |
|
|
// built-in script function: search() |
313 |
|
|
|
314 |
schoenebeck |
3557 |
ExprType_t CoreVMFunction_search::argType(vmint iArg) const { |
315 |
schoenebeck |
3221 |
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
316 |
|
|
} |
317 |
|
|
|
318 |
schoenebeck |
3557 |
bool CoreVMFunction_search::acceptsArgType(vmint iArg, ExprType_t type) const { |
319 |
schoenebeck |
3221 |
if (iArg == 0) |
320 |
|
|
return type == INT_ARR_EXPR; |
321 |
|
|
else |
322 |
|
|
return type == INT_EXPR; |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) { |
326 |
|
|
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
327 |
schoenebeck |
3557 |
const vmint needle = args->arg(1)->asInt()->evalInt(); |
328 |
|
|
const vmint n = a->arraySize(); |
329 |
|
|
for (vmint i = 0; i < n; ++i) |
330 |
schoenebeck |
3221 |
if (a->evalIntElement(i) == needle) |
331 |
|
|
return successResult(i); |
332 |
|
|
return successResult(-1); // not found |
333 |
|
|
} |
334 |
|
|
|
335 |
|
|
/////////////////////////////////////////////////////////////////////////// |
336 |
|
|
// built-in script function: sort() |
337 |
|
|
|
338 |
schoenebeck |
3557 |
ExprType_t CoreVMFunction_sort::argType(vmint iArg) const { |
339 |
schoenebeck |
3221 |
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
340 |
|
|
} |
341 |
|
|
|
342 |
schoenebeck |
3557 |
bool CoreVMFunction_sort::acceptsArgType(vmint iArg, ExprType_t type) const { |
343 |
schoenebeck |
3221 |
if (iArg == 0) |
344 |
|
|
return type == INT_ARR_EXPR; |
345 |
|
|
else |
346 |
|
|
return type == INT_EXPR; |
347 |
|
|
} |
348 |
|
|
|
349 |
|
|
struct ArrElemPOD { |
350 |
|
|
VMIntArrayExpr* m_array; |
351 |
schoenebeck |
3557 |
vmint m_index; |
352 |
schoenebeck |
3221 |
}; |
353 |
|
|
|
354 |
schoenebeck |
3222 |
static inline void swap(class ArrElemRef a, class ArrElemRef b); |
355 |
schoenebeck |
3221 |
|
356 |
|
|
class ArrElemRef : protected ArrElemPOD { |
357 |
|
|
public: |
358 |
|
|
ArrElemRef() { |
359 |
|
|
m_array = NULL; |
360 |
|
|
m_index = 0; |
361 |
|
|
} |
362 |
schoenebeck |
3557 |
ArrElemRef(VMIntArrayExpr* a, vmint index) { |
363 |
schoenebeck |
3221 |
m_array = a; |
364 |
|
|
m_index = index; |
365 |
|
|
} |
366 |
|
|
inline ArrElemRef& operator=(const ArrElemRef& e) { |
367 |
|
|
setValue(e.getValue()); |
368 |
|
|
return *this; |
369 |
|
|
} |
370 |
schoenebeck |
3557 |
inline ArrElemRef& operator=(vmint val) { |
371 |
schoenebeck |
3221 |
setValue(val); |
372 |
|
|
return *this; |
373 |
|
|
} |
374 |
|
|
inline bool operator==(const ArrElemRef& e) const { |
375 |
|
|
if (m_index == e.m_index) |
376 |
|
|
return true; |
377 |
|
|
return getValue() == e.getValue(); |
378 |
|
|
} |
379 |
schoenebeck |
3557 |
inline bool operator==(vmint val) const { |
380 |
schoenebeck |
3221 |
return getValue() == val; |
381 |
|
|
} |
382 |
|
|
inline bool operator!=(const ArrElemRef& e) const { |
383 |
|
|
return !(operator==(e)); |
384 |
|
|
} |
385 |
schoenebeck |
3557 |
inline bool operator!=(vmint val) const { |
386 |
schoenebeck |
3221 |
return !(operator==(val)); |
387 |
|
|
} |
388 |
|
|
inline bool operator<(const ArrElemRef& e) const { |
389 |
|
|
if (m_index == e.m_index) |
390 |
|
|
return false; |
391 |
|
|
return getValue() < e.getValue(); |
392 |
|
|
} |
393 |
schoenebeck |
3557 |
inline bool operator<(vmint val) const { |
394 |
schoenebeck |
3221 |
return getValue() < val; |
395 |
|
|
} |
396 |
|
|
inline bool operator>(const ArrElemRef& e) const { |
397 |
|
|
if (m_index == e.m_index) |
398 |
|
|
return false; |
399 |
|
|
return getValue() > e.getValue(); |
400 |
|
|
} |
401 |
schoenebeck |
3557 |
inline bool operator>(vmint val) const { |
402 |
schoenebeck |
3221 |
return getValue() > val; |
403 |
|
|
} |
404 |
|
|
inline bool operator<=(const ArrElemRef& e) const { |
405 |
|
|
if (m_index == e.m_index) |
406 |
|
|
return true; |
407 |
|
|
return getValue() <= e.getValue(); |
408 |
|
|
} |
409 |
schoenebeck |
3557 |
inline bool operator<=(vmint val) const { |
410 |
schoenebeck |
3221 |
return getValue() <= val; |
411 |
|
|
} |
412 |
|
|
inline bool operator>=(const ArrElemRef& e) const { |
413 |
|
|
if (m_index == e.m_index) |
414 |
|
|
return true; |
415 |
|
|
return getValue() >= e.getValue(); |
416 |
|
|
} |
417 |
schoenebeck |
3557 |
inline bool operator>=(vmint val) const { |
418 |
schoenebeck |
3221 |
return getValue() >= val; |
419 |
|
|
} |
420 |
schoenebeck |
3557 |
inline operator vmint() const { |
421 |
schoenebeck |
3221 |
return getValue(); |
422 |
|
|
} |
423 |
|
|
protected: |
424 |
schoenebeck |
3557 |
inline vmint getValue() const { |
425 |
schoenebeck |
3221 |
return m_array->evalIntElement(m_index); |
426 |
|
|
} |
427 |
schoenebeck |
3557 |
inline void setValue(vmint value) { |
428 |
schoenebeck |
3221 |
m_array->assignIntElement(m_index, value); |
429 |
|
|
} |
430 |
|
|
|
431 |
schoenebeck |
3222 |
friend void swap(class ArrElemRef a, class ArrElemRef b); |
432 |
schoenebeck |
3221 |
}; |
433 |
|
|
|
434 |
|
|
class ArrElemPtr : protected ArrElemPOD { |
435 |
|
|
public: |
436 |
|
|
ArrElemPtr() { |
437 |
|
|
m_array = NULL; |
438 |
|
|
m_index = 0; |
439 |
|
|
} |
440 |
schoenebeck |
3557 |
ArrElemPtr(VMIntArrayExpr* a, vmint index) { |
441 |
schoenebeck |
3221 |
m_array = a; |
442 |
|
|
m_index = index; |
443 |
|
|
} |
444 |
|
|
inline ArrElemRef operator*() { |
445 |
|
|
return *(ArrElemRef*)this; |
446 |
|
|
} |
447 |
|
|
}; |
448 |
|
|
|
449 |
schoenebeck |
3222 |
static inline void swap(ArrElemRef a, ArrElemRef b) { |
450 |
schoenebeck |
3557 |
vmint valueA = a.getValue(); |
451 |
|
|
vmint valueB = b.getValue(); |
452 |
schoenebeck |
3221 |
a.setValue(valueB); |
453 |
|
|
b.setValue(valueA); |
454 |
|
|
} |
455 |
|
|
|
456 |
|
|
class ArrExprIter : public ArrElemPOD { |
457 |
|
|
public: |
458 |
|
|
typedef std::random_access_iterator_tag iterator_category; |
459 |
schoenebeck |
3557 |
typedef vmint value_type; |
460 |
schoenebeck |
3221 |
typedef ssize_t difference_type; |
461 |
|
|
typedef ArrElemPtr pointer; |
462 |
|
|
typedef ArrElemRef reference; |
463 |
|
|
|
464 |
schoenebeck |
3557 |
ArrExprIter(VMIntArrayExpr* a, vmint index) { |
465 |
schoenebeck |
3221 |
m_array = a; |
466 |
|
|
m_index = index; |
467 |
|
|
} |
468 |
|
|
inline ArrElemRef operator*() { |
469 |
|
|
return *(ArrElemRef*)this; |
470 |
|
|
} |
471 |
|
|
inline ArrExprIter& operator++() { // prefix increment |
472 |
|
|
++m_index; |
473 |
|
|
return *this; |
474 |
|
|
} |
475 |
|
|
inline ArrExprIter& operator--() { // prefix decrement |
476 |
|
|
--m_index; |
477 |
|
|
return *this; |
478 |
|
|
} |
479 |
|
|
inline ArrExprIter operator++(int) { // postfix increment |
480 |
|
|
ArrExprIter it = *this; |
481 |
|
|
++m_index; |
482 |
|
|
return it; |
483 |
|
|
} |
484 |
|
|
inline ArrExprIter operator--(int) { // postfix decrement |
485 |
|
|
ArrExprIter it = *this; |
486 |
|
|
--m_index; |
487 |
|
|
return it; |
488 |
|
|
} |
489 |
schoenebeck |
3345 |
inline ArrExprIter& operator+=(difference_type d) { |
490 |
|
|
m_index += d; |
491 |
|
|
return *this; |
492 |
|
|
} |
493 |
|
|
inline ArrExprIter& operator-=(difference_type d) { |
494 |
|
|
m_index -= d; |
495 |
|
|
return *this; |
496 |
|
|
} |
497 |
schoenebeck |
3221 |
inline bool operator==(const ArrExprIter& other) const { |
498 |
|
|
return m_index == other.m_index; |
499 |
|
|
} |
500 |
|
|
inline bool operator!=(const ArrExprIter& other) const { |
501 |
|
|
return m_index != other.m_index; |
502 |
|
|
} |
503 |
|
|
inline bool operator<(const ArrExprIter& other) const { |
504 |
|
|
return m_index < other.m_index; |
505 |
|
|
} |
506 |
|
|
inline bool operator>(const ArrExprIter& other) const { |
507 |
|
|
return m_index > other.m_index; |
508 |
|
|
} |
509 |
|
|
inline bool operator<=(const ArrExprIter& other) const { |
510 |
|
|
return m_index <= other.m_index; |
511 |
|
|
} |
512 |
|
|
inline bool operator>=(const ArrExprIter& other) const { |
513 |
|
|
return m_index >= other.m_index; |
514 |
|
|
} |
515 |
|
|
inline difference_type operator+(const ArrExprIter& other) const { |
516 |
|
|
return m_index + other.m_index; |
517 |
|
|
} |
518 |
|
|
inline difference_type operator-(const ArrExprIter& other) const { |
519 |
|
|
return m_index - other.m_index; |
520 |
|
|
} |
521 |
|
|
inline ArrExprIter operator-(difference_type d) const { |
522 |
|
|
return ArrExprIter(m_array, m_index - d); |
523 |
|
|
} |
524 |
|
|
inline ArrExprIter operator+(difference_type d) const { |
525 |
|
|
return ArrExprIter(m_array, m_index + d); |
526 |
|
|
} |
527 |
|
|
inline ArrExprIter operator*(difference_type factor) const { |
528 |
|
|
return ArrExprIter(m_array, m_index * factor); |
529 |
|
|
} |
530 |
|
|
}; |
531 |
|
|
|
532 |
|
|
struct DescArrExprSorter { |
533 |
schoenebeck |
3557 |
inline bool operator()(const vmint& a, const vmint& b) const { |
534 |
schoenebeck |
3221 |
return a > b; |
535 |
|
|
} |
536 |
|
|
}; |
537 |
|
|
|
538 |
|
|
VMFnResult* CoreVMFunction_sort::exec(VMFnArgs* args) { |
539 |
|
|
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
540 |
|
|
bool bAscending = |
541 |
|
|
(args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt(); |
542 |
schoenebeck |
3557 |
vmint n = a->arraySize(); |
543 |
schoenebeck |
3221 |
ArrExprIter itBegin(a, 0); |
544 |
|
|
ArrExprIter itEnd(a, n); |
545 |
|
|
if (bAscending) { |
546 |
|
|
std::sort(itBegin, itEnd); |
547 |
|
|
} else { |
548 |
|
|
DescArrExprSorter sorter; |
549 |
|
|
std::sort(itBegin, itEnd, sorter); |
550 |
|
|
} |
551 |
|
|
return successResult(); |
552 |
|
|
} |
553 |
|
|
|
554 |
schoenebeck |
3573 |
/////////////////////////////////////////////////////////////////////////// |
555 |
|
|
// built-in script function: real_to_int() and int() |
556 |
|
|
|
557 |
|
|
VMFnResult* CoreVMFunction_real_to_int::exec(VMFnArgs* args) { |
558 |
|
|
vmfloat f = args->arg(0)->asReal()->evalReal(); |
559 |
|
|
return successResult(vmint(f)); |
560 |
|
|
} |
561 |
|
|
|
562 |
|
|
/////////////////////////////////////////////////////////////////////////// |
563 |
|
|
// built-in script function: int_to_real() and real() |
564 |
|
|
|
565 |
|
|
VMFnResult* CoreVMFunction_int_to_real::exec(VMFnArgs* args) { |
566 |
|
|
vmint i = args->arg(0)->asInt()->evalInt(); |
567 |
|
|
return successResult(i); |
568 |
|
|
} |
569 |
|
|
|
570 |
schoenebeck |
2581 |
} // namespace LinuxSampler |