/[svn]/linuxsampler/trunk/src/scriptvm/CoreVMFunctions.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/CoreVMFunctions.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3577 - (show annotations) (download) (as text)
Wed Aug 28 15:23:23 2019 UTC (4 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 15213 byte(s)
NKSP: Introducing variable return type for built-in functions.

* Changed method signature VMFunction::returnType() ->
  VMFunction::returnType(VMFnArgs* args) to allow built-in
  functions to proclaim a different result value type depending
  on the arguments to be passed to the function.

* Built-in script function abs() optionally accepts and returns
  real number.

* Built-in script functions min() and max() optionally accept
  real number arguments and return real number as result in that
  case.

* Added real number test cases for the built-in abs(), min() and
  max() functions.

* Bumped version (2.1.1.svn7).

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(VMFnArgs* args) 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(VMFnArgs* args) 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(VMFnArgs* args) 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(VMFnArgs* args) 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 * Abstract base class for built-in script functions which either return an
154 * integer or a real number (floating point scalar) as their function return
155 * value. The actual return type is determined at parse time once after
156 * potential arguments were associated with the respective function call.
157 */
158 class VMScalarNumberResultFunction : public VMFunction {
159 protected:
160 virtual ~VMScalarNumberResultFunction() {}
161 VMFnResult* errorResult(vmint i);
162 VMFnResult* errorResult(vmfloat f);
163 VMFnResult* successResult(vmint i);
164 VMFnResult* successResult(vmfloat f);
165 bool modifiesArg(vmint iArg) const OVERRIDE { return false; }
166 protected:
167 VMIntResult intResult;
168 VMRealResult realResult;
169 };
170
171
172 ///////////////////////////////////////////////////////////////////////////
173 // implementations of core built-in script functions ...
174
175 /**
176 * Implements the built-in message() script function.
177 */
178 class CoreVMFunction_message : public VMEmptyResultFunction {
179 public:
180 vmint minRequiredArgs() const OVERRIDE { return 1; }
181 vmint maxAllowedArgs() const OVERRIDE { return 1; }
182 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
183 ExprType_t argType(vmint iArg) const OVERRIDE { return STRING_EXPR; }
184 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
185 };
186
187 /**
188 * Implements the built-in exit() script function.
189 */
190 class CoreVMFunction_exit : public VMEmptyResultFunction {
191 public:
192 CoreVMFunction_exit(ScriptVM* vm) : vm(vm) {}
193 vmint minRequiredArgs() const OVERRIDE { return 0; }
194 vmint maxAllowedArgs() const OVERRIDE;
195 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
196 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
197 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
198 protected:
199 ScriptVM* vm;
200 };
201
202 /**
203 * Implements the built-in wait() script function.
204 */
205 class CoreVMFunction_wait : public VMEmptyResultFunction {
206 public:
207 CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {}
208 vmint minRequiredArgs() const OVERRIDE { return 1; }
209 vmint maxAllowedArgs() const OVERRIDE { return 1; }
210 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
211 bool acceptsArgUnitType(vmint iArg, StdUnit_t type) const OVERRIDE;
212 bool acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const OVERRIDE;
213 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
214 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
215 protected:
216 ScriptVM* vm;
217 };
218
219 /**
220 * Implements the built-in abs() script function.
221 */
222 class CoreVMFunction_abs : public VMScalarNumberResultFunction {
223 public:
224 ExprType_t returnType(VMFnArgs* args) OVERRIDE;
225 vmint minRequiredArgs() const OVERRIDE { return 1; }
226 vmint maxAllowedArgs() const OVERRIDE { return 1; }
227 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
228 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
229 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
230 };
231
232 /**
233 * Implements the built-in random() script function.
234 */
235 class CoreVMFunction_random : public VMIntResultFunction {
236 public:
237 vmint minRequiredArgs() const OVERRIDE { return 2; }
238 vmint maxAllowedArgs() const OVERRIDE { return 2; }
239 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
240 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
241 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
242 };
243
244 /**
245 * Implements the built-in num_elements() script function.
246 */
247 class CoreVMFunction_num_elements : public VMIntResultFunction {
248 public:
249 vmint minRequiredArgs() const OVERRIDE { return 1; }
250 vmint maxAllowedArgs() const OVERRIDE { return 1; }
251 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
252 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_ARR_EXPR; }
253 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
254 };
255
256 /**
257 * Implements the built-in inc() script function.
258 */
259 class CoreVMFunction_inc : public VMIntResultFunction {
260 public:
261 vmint minRequiredArgs() const OVERRIDE { return 1; }
262 vmint maxAllowedArgs() const OVERRIDE { return 1; }
263 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
264 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
265 bool modifiesArg(vmint iArg) const OVERRIDE { return true; }
266 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
267 };
268
269 /**
270 * Implements the built-in dec() script function.
271 */
272 class CoreVMFunction_dec : public VMIntResultFunction {
273 public:
274 vmint minRequiredArgs() const OVERRIDE { return 1; }
275 vmint maxAllowedArgs() const OVERRIDE { return 1; }
276 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
277 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
278 bool modifiesArg(vmint iArg) const OVERRIDE { return true; }
279 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
280 };
281
282 /**
283 * Implements the built-in in_range() script function.
284 */
285 class CoreVMFunction_in_range : public VMIntResultFunction {
286 public:
287 vmint minRequiredArgs() const OVERRIDE { return 3; }
288 vmint maxAllowedArgs() const OVERRIDE { return 3; }
289 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
290 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
291 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
292 };
293
294 /**
295 * Implements the built-in sh_left() script function.
296 */
297 class CoreVMFunction_sh_left : public VMIntResultFunction {
298 public:
299 vmint minRequiredArgs() const OVERRIDE { return 2; }
300 vmint maxAllowedArgs() const OVERRIDE { return 2; }
301 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
302 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
303 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
304 };
305
306 /**
307 * Implements the built-in sh_right() script function.
308 */
309 class CoreVMFunction_sh_right : public VMIntResultFunction {
310 public:
311 vmint minRequiredArgs() const OVERRIDE { return 2; }
312 vmint maxAllowedArgs() const OVERRIDE { return 2; }
313 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
314 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
315 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
316 };
317
318 /**
319 * Implements the built-in min() script function.
320 */
321 class CoreVMFunction_min : public VMScalarNumberResultFunction {
322 public:
323 ExprType_t returnType(VMFnArgs* args) OVERRIDE;
324 vmint minRequiredArgs() const OVERRIDE { return 2; }
325 vmint maxAllowedArgs() const OVERRIDE { return 2; }
326 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
327 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
328 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
329 };
330
331 /**
332 * Implements the built-in max() script function.
333 */
334 class CoreVMFunction_max : public VMScalarNumberResultFunction {
335 public:
336 ExprType_t returnType(VMFnArgs* args) OVERRIDE;
337 vmint minRequiredArgs() const OVERRIDE { return 2; }
338 vmint maxAllowedArgs() const OVERRIDE { return 2; }
339 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
340 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
341 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
342 };
343
344 /**
345 * Implements the built-in array_equal() script function.
346 */
347 class CoreVMFunction_array_equal : public VMIntResultFunction {
348 public:
349 vmint minRequiredArgs() const OVERRIDE { return 2; }
350 vmint maxAllowedArgs() const OVERRIDE { return 2; }
351 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_ARR_EXPR; }
352 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_ARR_EXPR; }
353 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
354 };
355
356 /**
357 * Implements the built-in search() script function.
358 */
359 class CoreVMFunction_search : public VMIntResultFunction {
360 public:
361 vmint minRequiredArgs() const OVERRIDE { return 2; }
362 vmint maxAllowedArgs() const OVERRIDE { return 2; }
363 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
364 ExprType_t argType(vmint iArg) const OVERRIDE;
365 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
366 };
367
368 /**
369 * Implements the built-in sort() script function.
370 */
371 class CoreVMFunction_sort : public VMEmptyResultFunction {
372 public:
373 vmint minRequiredArgs() const OVERRIDE { return 1; }
374 vmint maxAllowedArgs() const OVERRIDE { return 2; }
375 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
376 ExprType_t argType(vmint iArg) const OVERRIDE;
377 bool modifiesArg(vmint iArg) const OVERRIDE { return iArg == 0; }
378 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
379 };
380
381 /**
382 * Implements the built-in real_to_int() script function and its short hand
383 * variant int(). The behaviour of the two built-in script functions are
384 * identical ATM.
385 */
386 class CoreVMFunction_real_to_int : public VMIntResultFunction {
387 public:
388 vmint minRequiredArgs() const OVERRIDE { return 1; }
389 vmint maxAllowedArgs() const OVERRIDE { return 1; }
390 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == REAL_EXPR; }
391 ExprType_t argType(vmint iArg) const OVERRIDE { return REAL_EXPR; }
392 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
393 };
394
395 /**
396 * Implements the built-in int_to_real() script function and its short hand
397 * variant real(). The behaviour of the two built-in script functions are
398 * identical ATM.
399 */
400 class CoreVMFunction_int_to_real : public VMRealResultFunction {
401 public:
402 vmint minRequiredArgs() const OVERRIDE { return 1; }
403 vmint maxAllowedArgs() const OVERRIDE { return 1; }
404 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
405 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
406 VMFnResult* exec(VMFnArgs* args) OVERRIDE;
407 };
408
409 } // namespace LinuxSampler
410
411 #endif // LS_COREVMFUNCTIONS_H

  ViewVC Help
Powered by ViewVC