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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3577 - (hide 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 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     #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 schoenebeck 2727
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 schoenebeck 2581 class VMEmptyResult : public VMFnResult, public VMExpr {
28     public:
29 schoenebeck 2727 StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call
30 schoenebeck 2581
31     VMEmptyResult() : flags(STMT_SUCCESS) {}
32 schoenebeck 3054 ExprType_t exprType() const OVERRIDE { return EMPTY_EXPR; }
33     VMExpr* resultValue() OVERRIDE { return this; }
34     StmtFlags_t resultFlags() OVERRIDE { return flags; }
35 schoenebeck 2945 bool isConstExpr() const OVERRIDE { return false; }
36 schoenebeck 2581 };
37    
38 schoenebeck 2727 /**
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 schoenebeck 2598 class VMIntResult : public VMFnResult, public VMIntExpr {
43     public:
44 schoenebeck 2727 StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call
45 schoenebeck 3557 vmint value; ///< result value of the function call
46 schoenebeck 2598
47 schoenebeck 3561 VMIntResult() : flags(STMT_SUCCESS), value(0) {}
48 schoenebeck 3557 vmint evalInt() OVERRIDE { return value; }
49 schoenebeck 3054 VMExpr* resultValue() OVERRIDE { return this; }
50     StmtFlags_t resultFlags() OVERRIDE { return flags; }
51 schoenebeck 2945 bool isConstExpr() const OVERRIDE { return false; }
52 schoenebeck 3561 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 schoenebeck 2598 };
56    
57 schoenebeck 2727 /**
58     * An instance of this class is returned by built-in function implementations
59 schoenebeck 3573 * 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 schoenebeck 2727 * which return a string value as function return value.
79     */
80 schoenebeck 2581 class VMStringResult : public VMFnResult, public VMStringExpr {
81     public:
82 schoenebeck 2727 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 schoenebeck 2581
85     VMStringResult() : flags(STMT_SUCCESS) {}
86 schoenebeck 3054 String evalStr() OVERRIDE { return value; }
87     VMExpr* resultValue() OVERRIDE { return this; }
88     StmtFlags_t resultFlags() OVERRIDE { return flags; }
89 schoenebeck 2945 bool isConstExpr() const OVERRIDE { return false; }
90 schoenebeck 2581 };
91    
92 schoenebeck 2727 /**
93     * Abstract base class for built-in script functions which do not return any
94     * function return value (void).
95     */
96 schoenebeck 2581 class VMEmptyResultFunction : public VMFunction {
97     protected:
98 schoenebeck 3034 virtual ~VMEmptyResultFunction() {}
99 schoenebeck 3577 ExprType_t returnType(VMFnArgs* args) OVERRIDE { return EMPTY_EXPR; }
100 schoenebeck 2581 VMFnResult* errorResult();
101     VMFnResult* successResult();
102 schoenebeck 3557 bool modifiesArg(vmint iArg) const OVERRIDE { return false; }
103 schoenebeck 2581 protected:
104     VMEmptyResult result;
105     };
106    
107 schoenebeck 2727 /**
108     * Abstract base class for built-in script functions which return an integer
109     * (scalar) as their function return value.
110     */
111 schoenebeck 2598 class VMIntResultFunction : public VMFunction {
112     protected:
113 schoenebeck 3034 virtual ~VMIntResultFunction() {}
114 schoenebeck 3577 ExprType_t returnType(VMFnArgs* args) OVERRIDE { return INT_EXPR; }
115 schoenebeck 3557 VMFnResult* errorResult(vmint i = 0);
116     VMFnResult* successResult(vmint i = 0);
117     bool modifiesArg(vmint iArg) const OVERRIDE { return false; }
118 schoenebeck 2598 protected:
119     VMIntResult result;
120     };
121    
122 schoenebeck 2727 /**
123 schoenebeck 3573 * 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 schoenebeck 3577 ExprType_t returnType(VMFnArgs* args) OVERRIDE { return REAL_EXPR; }
130 schoenebeck 3573 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 schoenebeck 2727 * Abstract base class for built-in script functions which return a string as
139     * their function return value.
140     */
141 schoenebeck 2581 class VMStringResultFunction : public VMFunction {
142     protected:
143 schoenebeck 3034 virtual ~VMStringResultFunction() {}
144 schoenebeck 3577 ExprType_t returnType(VMFnArgs* args) OVERRIDE { return STRING_EXPR; }
145 schoenebeck 2581 VMFnResult* errorResult(const String& s = "");
146     VMFnResult* successResult(const String& s = "");
147 schoenebeck 3557 bool modifiesArg(vmint iArg) const OVERRIDE { return false; }
148 schoenebeck 2581 protected:
149     VMStringResult result;
150     };
151    
152 schoenebeck 3577 /**
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 schoenebeck 2727
171 schoenebeck 3577
172 schoenebeck 2727 ///////////////////////////////////////////////////////////////////////////
173     // implementations of core built-in script functions ...
174    
175     /**
176     * Implements the built-in message() script function.
177     */
178 schoenebeck 2581 class CoreVMFunction_message : public VMEmptyResultFunction {
179     public:
180 schoenebeck 3557 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 schoenebeck 2581 };
186    
187 schoenebeck 2727 /**
188     * Implements the built-in exit() script function.
189     */
190 schoenebeck 2581 class CoreVMFunction_exit : public VMEmptyResultFunction {
191     public:
192 schoenebeck 3551 CoreVMFunction_exit(ScriptVM* vm) : vm(vm) {}
193 schoenebeck 3557 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 schoenebeck 3551 protected:
199     ScriptVM* vm;
200 schoenebeck 2581 };
201    
202 schoenebeck 2727 /**
203     * Implements the built-in wait() script function.
204     */
205 schoenebeck 2581 class CoreVMFunction_wait : public VMEmptyResultFunction {
206     public:
207     CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {}
208 schoenebeck 3557 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 schoenebeck 3561 bool acceptsArgUnitType(vmint iArg, StdUnit_t type) const OVERRIDE;
212 schoenebeck 3564 bool acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const OVERRIDE;
213 schoenebeck 3557 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
214     VMFnResult* exec(VMFnArgs* args) OVERRIDE;
215 schoenebeck 2581 protected:
216     ScriptVM* vm;
217     };
218    
219 schoenebeck 2727 /**
220     * Implements the built-in abs() script function.
221     */
222 schoenebeck 3577 class CoreVMFunction_abs : public VMScalarNumberResultFunction {
223 schoenebeck 2619 public:
224 schoenebeck 3577 ExprType_t returnType(VMFnArgs* args) OVERRIDE;
225 schoenebeck 3557 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 schoenebeck 2619 };
231    
232 schoenebeck 2727 /**
233     * Implements the built-in random() script function.
234     */
235 schoenebeck 2619 class CoreVMFunction_random : public VMIntResultFunction {
236     public:
237 schoenebeck 3557 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 schoenebeck 2619 };
243    
244 schoenebeck 2727 /**
245     * Implements the built-in num_elements() script function.
246     */
247 schoenebeck 2619 class CoreVMFunction_num_elements : public VMIntResultFunction {
248     public:
249 schoenebeck 3557 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 schoenebeck 2619 };
255    
256 schoenebeck 2945 /**
257     * Implements the built-in inc() script function.
258     */
259     class CoreVMFunction_inc : public VMIntResultFunction {
260     public:
261 schoenebeck 3557 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 schoenebeck 2945 };
268    
269     /**
270     * Implements the built-in dec() script function.
271     */
272     class CoreVMFunction_dec : public VMIntResultFunction {
273     public:
274 schoenebeck 3557 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 schoenebeck 2945 };
281    
282 schoenebeck 2965 /**
283 schoenebeck 3076 * Implements the built-in in_range() script function.
284     */
285     class CoreVMFunction_in_range : public VMIntResultFunction {
286     public:
287 schoenebeck 3557 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 schoenebeck 3076 };
293    
294     /**
295 schoenebeck 2965 * Implements the built-in sh_left() script function.
296     */
297     class CoreVMFunction_sh_left : public VMIntResultFunction {
298     public:
299 schoenebeck 3557 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 schoenebeck 2965 };
305    
306     /**
307     * Implements the built-in sh_right() script function.
308     */
309     class CoreVMFunction_sh_right : public VMIntResultFunction {
310     public:
311 schoenebeck 3557 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 schoenebeck 2965 };
317    
318 schoenebeck 2970 /**
319     * Implements the built-in min() script function.
320     */
321 schoenebeck 3577 class CoreVMFunction_min : public VMScalarNumberResultFunction {
322 schoenebeck 2970 public:
323 schoenebeck 3577 ExprType_t returnType(VMFnArgs* args) OVERRIDE;
324 schoenebeck 3557 vmint minRequiredArgs() const OVERRIDE { return 2; }
325     vmint maxAllowedArgs() const OVERRIDE { return 2; }
326 schoenebeck 3577 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
327 schoenebeck 3557 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
328     VMFnResult* exec(VMFnArgs* args) OVERRIDE;
329 schoenebeck 2970 };
330    
331     /**
332     * Implements the built-in max() script function.
333     */
334 schoenebeck 3577 class CoreVMFunction_max : public VMScalarNumberResultFunction {
335 schoenebeck 2970 public:
336 schoenebeck 3577 ExprType_t returnType(VMFnArgs* args) OVERRIDE;
337 schoenebeck 3557 vmint minRequiredArgs() const OVERRIDE { return 2; }
338     vmint maxAllowedArgs() const OVERRIDE { return 2; }
339 schoenebeck 3577 bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
340 schoenebeck 3557 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
341     VMFnResult* exec(VMFnArgs* args) OVERRIDE;
342 schoenebeck 2970 };
343    
344 schoenebeck 3221 /**
345     * Implements the built-in array_equal() script function.
346     */
347     class CoreVMFunction_array_equal : public VMIntResultFunction {
348     public:
349 schoenebeck 3557 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 schoenebeck 3221 };
355    
356     /**
357     * Implements the built-in search() script function.
358     */
359     class CoreVMFunction_search : public VMIntResultFunction {
360     public:
361 schoenebeck 3557 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 schoenebeck 3221 };
367    
368     /**
369     * Implements the built-in sort() script function.
370     */
371     class CoreVMFunction_sort : public VMEmptyResultFunction {
372     public:
373 schoenebeck 3557 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 schoenebeck 3221 };
380    
381 schoenebeck 3573 /**
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 schoenebeck 2581 } // namespace LinuxSampler
410    
411     #endif // LS_COREVMFUNCTIONS_H

  ViewVC Help
Powered by ViewVC