1 |
/* |
2 |
* Copyright (c) 2014-2019 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 |
#ifndef LS_COREVMFUNCTIONS_H |
11 |
#define LS_COREVMFUNCTIONS_H |
12 |
|
13 |
#include "../common/global.h" |
14 |
#include "common.h" |
15 |
|
16 |
namespace LinuxSampler { |
17 |
|
18 |
class ScriptVM; |
19 |
|
20 |
/////////////////////////////////////////////////////////////////////////// |
21 |
// convenience base classes for built-in script functions ... |
22 |
|
23 |
/** |
24 |
* An instance of this class is returned by built-in function implementations |
25 |
* which do not return a function return value. |
26 |
*/ |
27 |
class VMEmptyResult : public VMFnResult, public VMExpr { |
28 |
public: |
29 |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
30 |
|
31 |
VMEmptyResult() : flags(STMT_SUCCESS) {} |
32 |
ExprType_t exprType() const OVERRIDE { return EMPTY_EXPR; } |
33 |
VMExpr* resultValue() OVERRIDE { return this; } |
34 |
StmtFlags_t resultFlags() OVERRIDE { return flags; } |
35 |
bool isConstExpr() const OVERRIDE { return false; } |
36 |
}; |
37 |
|
38 |
/** |
39 |
* An instance of this class is returned by built-in function implementations |
40 |
* which return an integer value as function return value. |
41 |
*/ |
42 |
class VMIntResult : public VMFnResult, public VMIntExpr { |
43 |
public: |
44 |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
45 |
vmint value; ///< result value of the function call |
46 |
|
47 |
VMIntResult() : flags(STMT_SUCCESS), value(0) {} |
48 |
vmint evalInt() OVERRIDE { return value; } |
49 |
VMExpr* resultValue() OVERRIDE { return this; } |
50 |
StmtFlags_t resultFlags() OVERRIDE { return flags; } |
51 |
bool isConstExpr() const OVERRIDE { return false; } |
52 |
MetricPrefix_t unitPrefix(vmuint i) const OVERRIDE { return VM_NO_PREFIX; } |
53 |
StdUnit_t unitType() const OVERRIDE { return VM_NO_UNIT; } |
54 |
bool isFinal() const OVERRIDE { return false; } |
55 |
}; |
56 |
|
57 |
/** |
58 |
* An instance of this class is returned by built-in function implementations |
59 |
* which return a real number (floating point) value as function return value. |
60 |
*/ |
61 |
class VMRealResult : public VMFnResult, public VMRealExpr { |
62 |
public: |
63 |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
64 |
vmfloat value; ///< result value of the function call |
65 |
|
66 |
VMRealResult() : flags(STMT_SUCCESS), value(0) {} |
67 |
vmfloat evalReal() OVERRIDE { return value; } |
68 |
VMExpr* resultValue() OVERRIDE { return this; } |
69 |
StmtFlags_t resultFlags() OVERRIDE { return flags; } |
70 |
bool isConstExpr() const OVERRIDE { return false; } |
71 |
MetricPrefix_t unitPrefix(vmuint i) const OVERRIDE { return VM_NO_PREFIX; } |
72 |
StdUnit_t unitType() const OVERRIDE { return VM_NO_UNIT; } |
73 |
bool isFinal() const OVERRIDE { return false; } |
74 |
}; |
75 |
|
76 |
/** |
77 |
* An instance of this class is returned by built-in function implementations |
78 |
* which return a string value as function return value. |
79 |
*/ |
80 |
class VMStringResult : public VMFnResult, public VMStringExpr { |
81 |
public: |
82 |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
83 |
String value; ///< result value of the function call |
84 |
|
85 |
VMStringResult() : flags(STMT_SUCCESS) {} |
86 |
String evalStr() OVERRIDE { return value; } |
87 |
VMExpr* resultValue() OVERRIDE { return this; } |
88 |
StmtFlags_t resultFlags() OVERRIDE { return flags; } |
89 |
bool isConstExpr() const OVERRIDE { return false; } |
90 |
}; |
91 |
|
92 |
/** |
93 |
* Abstract base class for built-in script functions which do not return any |
94 |
* function return value (void). |
95 |
*/ |
96 |
class VMEmptyResultFunction : public VMFunction { |
97 |
protected: |
98 |
virtual ~VMEmptyResultFunction() {} |
99 |
ExprType_t returnType() OVERRIDE { return EMPTY_EXPR; } |
100 |
VMFnResult* errorResult(); |
101 |
VMFnResult* successResult(); |
102 |
bool modifiesArg(vmint iArg) const OVERRIDE { return false; } |
103 |
protected: |
104 |
VMEmptyResult result; |
105 |
}; |
106 |
|
107 |
/** |
108 |
* Abstract base class for built-in script functions which return an integer |
109 |
* (scalar) as their function return value. |
110 |
*/ |
111 |
class VMIntResultFunction : public VMFunction { |
112 |
protected: |
113 |
virtual ~VMIntResultFunction() {} |
114 |
ExprType_t returnType() OVERRIDE { return INT_EXPR; } |
115 |
VMFnResult* errorResult(vmint i = 0); |
116 |
VMFnResult* successResult(vmint i = 0); |
117 |
bool modifiesArg(vmint iArg) const OVERRIDE { return false; } |
118 |
protected: |
119 |
VMIntResult result; |
120 |
}; |
121 |
|
122 |
/** |
123 |
* Abstract base class for built-in script functions which return a real number |
124 |
* (floating point scalar) as their function return value. |
125 |
*/ |
126 |
class VMRealResultFunction : public VMFunction { |
127 |
protected: |
128 |
virtual ~VMRealResultFunction() {} |
129 |
ExprType_t returnType() OVERRIDE { return REAL_EXPR; } |
130 |
VMFnResult* errorResult(vmfloat f = 0); |
131 |
VMFnResult* successResult(vmfloat f = 0); |
132 |
bool modifiesArg(vmint iArg) const OVERRIDE { return false; } |
133 |
protected: |
134 |
VMRealResult result; |
135 |
}; |
136 |
|
137 |
/** |
138 |
* Abstract base class for built-in script functions which return a string as |
139 |
* their function return value. |
140 |
*/ |
141 |
class VMStringResultFunction : public VMFunction { |
142 |
protected: |
143 |
virtual ~VMStringResultFunction() {} |
144 |
ExprType_t returnType() OVERRIDE { return STRING_EXPR; } |
145 |
VMFnResult* errorResult(const String& s = ""); |
146 |
VMFnResult* successResult(const String& s = ""); |
147 |
bool modifiesArg(vmint iArg) const OVERRIDE { return false; } |
148 |
protected: |
149 |
VMStringResult result; |
150 |
}; |
151 |
|
152 |
|
153 |
/////////////////////////////////////////////////////////////////////////// |
154 |
// implementations of core built-in script functions ... |
155 |
|
156 |
/** |
157 |
* Implements the built-in message() script function. |
158 |
*/ |
159 |
class CoreVMFunction_message : public VMEmptyResultFunction { |
160 |
public: |
161 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
162 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
163 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
164 |
ExprType_t argType(vmint iArg) const OVERRIDE { return STRING_EXPR; } |
165 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
166 |
}; |
167 |
|
168 |
/** |
169 |
* Implements the built-in exit() script function. |
170 |
*/ |
171 |
class CoreVMFunction_exit : public VMEmptyResultFunction { |
172 |
public: |
173 |
CoreVMFunction_exit(ScriptVM* vm) : vm(vm) {} |
174 |
vmint minRequiredArgs() const OVERRIDE { return 0; } |
175 |
vmint maxAllowedArgs() const OVERRIDE; |
176 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
177 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
178 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
179 |
protected: |
180 |
ScriptVM* vm; |
181 |
}; |
182 |
|
183 |
/** |
184 |
* Implements the built-in wait() script function. |
185 |
*/ |
186 |
class CoreVMFunction_wait : public VMEmptyResultFunction { |
187 |
public: |
188 |
CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {} |
189 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
190 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
191 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
192 |
bool acceptsArgUnitType(vmint iArg, StdUnit_t type) const OVERRIDE; |
193 |
bool acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const OVERRIDE; |
194 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
195 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
196 |
protected: |
197 |
ScriptVM* vm; |
198 |
}; |
199 |
|
200 |
/** |
201 |
* Implements the built-in abs() script function. |
202 |
*/ |
203 |
class CoreVMFunction_abs : public VMIntResultFunction { |
204 |
public: |
205 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
206 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
207 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
208 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
209 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
210 |
}; |
211 |
|
212 |
/** |
213 |
* Implements the built-in random() script function. |
214 |
*/ |
215 |
class CoreVMFunction_random : public VMIntResultFunction { |
216 |
public: |
217 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
218 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
219 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
220 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
221 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
222 |
}; |
223 |
|
224 |
/** |
225 |
* Implements the built-in num_elements() script function. |
226 |
*/ |
227 |
class CoreVMFunction_num_elements : public VMIntResultFunction { |
228 |
public: |
229 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
230 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
231 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
232 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_ARR_EXPR; } |
233 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
234 |
}; |
235 |
|
236 |
/** |
237 |
* Implements the built-in inc() script function. |
238 |
*/ |
239 |
class CoreVMFunction_inc : public VMIntResultFunction { |
240 |
public: |
241 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
242 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
243 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
244 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
245 |
bool modifiesArg(vmint iArg) const OVERRIDE { return true; } |
246 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
247 |
}; |
248 |
|
249 |
/** |
250 |
* Implements the built-in dec() script function. |
251 |
*/ |
252 |
class CoreVMFunction_dec : public VMIntResultFunction { |
253 |
public: |
254 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
255 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
256 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
257 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
258 |
bool modifiesArg(vmint iArg) const OVERRIDE { return true; } |
259 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
260 |
}; |
261 |
|
262 |
/** |
263 |
* Implements the built-in in_range() script function. |
264 |
*/ |
265 |
class CoreVMFunction_in_range : public VMIntResultFunction { |
266 |
public: |
267 |
vmint minRequiredArgs() const OVERRIDE { return 3; } |
268 |
vmint maxAllowedArgs() const OVERRIDE { return 3; } |
269 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
270 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
271 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
272 |
}; |
273 |
|
274 |
/** |
275 |
* Implements the built-in sh_left() script function. |
276 |
*/ |
277 |
class CoreVMFunction_sh_left : public VMIntResultFunction { |
278 |
public: |
279 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
280 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
281 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
282 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
283 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
284 |
}; |
285 |
|
286 |
/** |
287 |
* Implements the built-in sh_right() script function. |
288 |
*/ |
289 |
class CoreVMFunction_sh_right : public VMIntResultFunction { |
290 |
public: |
291 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
292 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
293 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
294 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
295 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
296 |
}; |
297 |
|
298 |
/** |
299 |
* Implements the built-in min() script function. |
300 |
*/ |
301 |
class CoreVMFunction_min : public VMIntResultFunction { |
302 |
public: |
303 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
304 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
305 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
306 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
307 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
308 |
}; |
309 |
|
310 |
/** |
311 |
* Implements the built-in max() script function. |
312 |
*/ |
313 |
class CoreVMFunction_max : public VMIntResultFunction { |
314 |
public: |
315 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
316 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
317 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
318 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
319 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
320 |
}; |
321 |
|
322 |
/** |
323 |
* Implements the built-in array_equal() script function. |
324 |
*/ |
325 |
class CoreVMFunction_array_equal : public VMIntResultFunction { |
326 |
public: |
327 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
328 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
329 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_ARR_EXPR; } |
330 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_ARR_EXPR; } |
331 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
332 |
}; |
333 |
|
334 |
/** |
335 |
* Implements the built-in search() script function. |
336 |
*/ |
337 |
class CoreVMFunction_search : public VMIntResultFunction { |
338 |
public: |
339 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
340 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
341 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
342 |
ExprType_t argType(vmint iArg) const OVERRIDE; |
343 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
344 |
}; |
345 |
|
346 |
/** |
347 |
* Implements the built-in sort() script function. |
348 |
*/ |
349 |
class CoreVMFunction_sort : public VMEmptyResultFunction { |
350 |
public: |
351 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
352 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
353 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
354 |
ExprType_t argType(vmint iArg) const OVERRIDE; |
355 |
bool modifiesArg(vmint iArg) const OVERRIDE { return iArg == 0; } |
356 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
357 |
}; |
358 |
|
359 |
/** |
360 |
* Implements the built-in real_to_int() script function and its short hand |
361 |
* variant int(). The behaviour of the two built-in script functions are |
362 |
* identical ATM. |
363 |
*/ |
364 |
class CoreVMFunction_real_to_int : public VMIntResultFunction { |
365 |
public: |
366 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
367 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
368 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == REAL_EXPR; } |
369 |
ExprType_t argType(vmint iArg) const OVERRIDE { return REAL_EXPR; } |
370 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
371 |
}; |
372 |
|
373 |
/** |
374 |
* Implements the built-in int_to_real() script function and its short hand |
375 |
* variant real(). The behaviour of the two built-in script functions are |
376 |
* identical ATM. |
377 |
*/ |
378 |
class CoreVMFunction_int_to_real : public VMRealResultFunction { |
379 |
public: |
380 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
381 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
382 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
383 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
384 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
385 |
}; |
386 |
|
387 |
} // namespace LinuxSampler |
388 |
|
389 |
#endif // LS_COREVMFUNCTIONS_H |