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