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 string value as function return value. |
60 |
*/ |
61 |
class VMStringResult : public VMFnResult, public VMStringExpr { |
62 |
public: |
63 |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
64 |
String value; ///< result value of the function call |
65 |
|
66 |
VMStringResult() : flags(STMT_SUCCESS) {} |
67 |
String evalStr() OVERRIDE { return value; } |
68 |
VMExpr* resultValue() OVERRIDE { return this; } |
69 |
StmtFlags_t resultFlags() OVERRIDE { return flags; } |
70 |
bool isConstExpr() const OVERRIDE { return false; } |
71 |
}; |
72 |
|
73 |
/** |
74 |
* Abstract base class for built-in script functions which do not return any |
75 |
* function return value (void). |
76 |
*/ |
77 |
class VMEmptyResultFunction : public VMFunction { |
78 |
protected: |
79 |
virtual ~VMEmptyResultFunction() {} |
80 |
ExprType_t returnType() OVERRIDE { return EMPTY_EXPR; } |
81 |
VMFnResult* errorResult(); |
82 |
VMFnResult* successResult(); |
83 |
bool modifiesArg(vmint iArg) const OVERRIDE { return false; } |
84 |
protected: |
85 |
VMEmptyResult result; |
86 |
}; |
87 |
|
88 |
/** |
89 |
* Abstract base class for built-in script functions which return an integer |
90 |
* (scalar) as their function return value. |
91 |
*/ |
92 |
class VMIntResultFunction : public VMFunction { |
93 |
protected: |
94 |
virtual ~VMIntResultFunction() {} |
95 |
ExprType_t returnType() OVERRIDE { return INT_EXPR; } |
96 |
VMFnResult* errorResult(vmint i = 0); |
97 |
VMFnResult* successResult(vmint i = 0); |
98 |
bool modifiesArg(vmint iArg) const OVERRIDE { return false; } |
99 |
protected: |
100 |
VMIntResult result; |
101 |
}; |
102 |
|
103 |
/** |
104 |
* Abstract base class for built-in script functions which return a string as |
105 |
* their function return value. |
106 |
*/ |
107 |
class VMStringResultFunction : public VMFunction { |
108 |
protected: |
109 |
virtual ~VMStringResultFunction() {} |
110 |
ExprType_t returnType() OVERRIDE { return STRING_EXPR; } |
111 |
VMFnResult* errorResult(const String& s = ""); |
112 |
VMFnResult* successResult(const String& s = ""); |
113 |
bool modifiesArg(vmint iArg) const OVERRIDE { return false; } |
114 |
protected: |
115 |
VMStringResult result; |
116 |
}; |
117 |
|
118 |
|
119 |
/////////////////////////////////////////////////////////////////////////// |
120 |
// implementations of core built-in script functions ... |
121 |
|
122 |
/** |
123 |
* Implements the built-in message() script function. |
124 |
*/ |
125 |
class CoreVMFunction_message : public VMEmptyResultFunction { |
126 |
public: |
127 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
128 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
129 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
130 |
ExprType_t argType(vmint iArg) const OVERRIDE { return STRING_EXPR; } |
131 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
132 |
}; |
133 |
|
134 |
/** |
135 |
* Implements the built-in exit() script function. |
136 |
*/ |
137 |
class CoreVMFunction_exit : public VMEmptyResultFunction { |
138 |
public: |
139 |
CoreVMFunction_exit(ScriptVM* vm) : vm(vm) {} |
140 |
vmint minRequiredArgs() const OVERRIDE { return 0; } |
141 |
vmint maxAllowedArgs() const OVERRIDE; |
142 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
143 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
144 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
145 |
protected: |
146 |
ScriptVM* vm; |
147 |
}; |
148 |
|
149 |
/** |
150 |
* Implements the built-in wait() script function. |
151 |
*/ |
152 |
class CoreVMFunction_wait : public VMEmptyResultFunction { |
153 |
public: |
154 |
CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {} |
155 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
156 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
157 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
158 |
bool acceptsArgUnitType(vmint iArg, StdUnit_t type) const OVERRIDE; |
159 |
bool acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const OVERRIDE; |
160 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
161 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
162 |
protected: |
163 |
ScriptVM* vm; |
164 |
}; |
165 |
|
166 |
/** |
167 |
* Implements the built-in abs() script function. |
168 |
*/ |
169 |
class CoreVMFunction_abs : public VMIntResultFunction { |
170 |
public: |
171 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
172 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
173 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
174 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
175 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
176 |
}; |
177 |
|
178 |
/** |
179 |
* Implements the built-in random() script function. |
180 |
*/ |
181 |
class CoreVMFunction_random : public VMIntResultFunction { |
182 |
public: |
183 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
184 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
185 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
186 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
187 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
188 |
}; |
189 |
|
190 |
/** |
191 |
* Implements the built-in num_elements() script function. |
192 |
*/ |
193 |
class CoreVMFunction_num_elements : public VMIntResultFunction { |
194 |
public: |
195 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
196 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
197 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
198 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_ARR_EXPR; } |
199 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
200 |
}; |
201 |
|
202 |
/** |
203 |
* Implements the built-in inc() script function. |
204 |
*/ |
205 |
class CoreVMFunction_inc : public VMIntResultFunction { |
206 |
public: |
207 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
208 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
209 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
210 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
211 |
bool modifiesArg(vmint iArg) const OVERRIDE { return true; } |
212 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
213 |
}; |
214 |
|
215 |
/** |
216 |
* Implements the built-in dec() script function. |
217 |
*/ |
218 |
class CoreVMFunction_dec : public VMIntResultFunction { |
219 |
public: |
220 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
221 |
vmint maxAllowedArgs() const OVERRIDE { return 1; } |
222 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
223 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
224 |
bool modifiesArg(vmint iArg) const OVERRIDE { return true; } |
225 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
226 |
}; |
227 |
|
228 |
/** |
229 |
* Implements the built-in in_range() script function. |
230 |
*/ |
231 |
class CoreVMFunction_in_range : public VMIntResultFunction { |
232 |
public: |
233 |
vmint minRequiredArgs() const OVERRIDE { return 3; } |
234 |
vmint maxAllowedArgs() const OVERRIDE { return 3; } |
235 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
236 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
237 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
238 |
}; |
239 |
|
240 |
/** |
241 |
* Implements the built-in sh_left() script function. |
242 |
*/ |
243 |
class CoreVMFunction_sh_left : public VMIntResultFunction { |
244 |
public: |
245 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
246 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
247 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
248 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
249 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
250 |
}; |
251 |
|
252 |
/** |
253 |
* Implements the built-in sh_right() script function. |
254 |
*/ |
255 |
class CoreVMFunction_sh_right : public VMIntResultFunction { |
256 |
public: |
257 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
258 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
259 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
260 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
261 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
262 |
}; |
263 |
|
264 |
/** |
265 |
* Implements the built-in min() script function. |
266 |
*/ |
267 |
class CoreVMFunction_min : public VMIntResultFunction { |
268 |
public: |
269 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
270 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
271 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
272 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
273 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
274 |
}; |
275 |
|
276 |
/** |
277 |
* Implements the built-in max() script function. |
278 |
*/ |
279 |
class CoreVMFunction_max : public VMIntResultFunction { |
280 |
public: |
281 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
282 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
283 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; } |
284 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; } |
285 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
286 |
}; |
287 |
|
288 |
/** |
289 |
* Implements the built-in array_equal() script function. |
290 |
*/ |
291 |
class CoreVMFunction_array_equal : public VMIntResultFunction { |
292 |
public: |
293 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
294 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
295 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_ARR_EXPR; } |
296 |
ExprType_t argType(vmint iArg) const OVERRIDE { return INT_ARR_EXPR; } |
297 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
298 |
}; |
299 |
|
300 |
/** |
301 |
* Implements the built-in search() script function. |
302 |
*/ |
303 |
class CoreVMFunction_search : public VMIntResultFunction { |
304 |
public: |
305 |
vmint minRequiredArgs() const OVERRIDE { return 2; } |
306 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
307 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
308 |
ExprType_t argType(vmint iArg) const OVERRIDE; |
309 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
310 |
}; |
311 |
|
312 |
/** |
313 |
* Implements the built-in sort() script function. |
314 |
*/ |
315 |
class CoreVMFunction_sort : public VMEmptyResultFunction { |
316 |
public: |
317 |
vmint minRequiredArgs() const OVERRIDE { return 1; } |
318 |
vmint maxAllowedArgs() const OVERRIDE { return 2; } |
319 |
bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE; |
320 |
ExprType_t argType(vmint iArg) const OVERRIDE; |
321 |
bool modifiesArg(vmint iArg) const OVERRIDE { return iArg == 0; } |
322 |
VMFnResult* exec(VMFnArgs* args) OVERRIDE; |
323 |
}; |
324 |
|
325 |
} // namespace LinuxSampler |
326 |
|
327 |
#endif // LS_COREVMFUNCTIONS_H |