/[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 3573 - (hide annotations) (download) (as text)
Tue Aug 27 21:36:53 2019 UTC (4 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 14311 byte(s)
NKSP: Introducing floating point support.

* NKSP language: Added support for NKSP real number literals and
  arithmetic operations on them (e.g. "(3.9 + 2.9) / 12.3 - 42.0").

* NKSP language: Added support for NKSP real number (floating point)
  script variables (declare ~foo := 3.4).

* NKSP language: Added support for NKSP real number (floating point)
  array script variables (declare ?foo[3] := ( 1.1, 2.7, 49.0 )).

* NKSP built-in script function "message()" accepts now real number
  argument as well.

* Added built-in NKSP script function "real_to_int()" and its short
  hand form "int()" for casting from real number to integer in NKSP
  scripts.

* Added built-in NKSP script function "int_to_real()" and its short
  hand form "real()" for casting from integer to real number in NKSP
  scripts.

* Bumped version (2.1.1.svn6).

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 3054 ExprType_t returnType() 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 3054 ExprType_t returnType() 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     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 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 3054 ExprType_t returnType() 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 2727
153     ///////////////////////////////////////////////////////////////////////////
154     // implementations of core built-in script functions ...
155    
156     /**
157     * Implements the built-in message() script function.
158     */
159 schoenebeck 2581 class CoreVMFunction_message : public VMEmptyResultFunction {
160     public:
161 schoenebeck 3557 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 schoenebeck 2581 };
167    
168 schoenebeck 2727 /**
169     * Implements the built-in exit() script function.
170     */
171 schoenebeck 2581 class CoreVMFunction_exit : public VMEmptyResultFunction {
172     public:
173 schoenebeck 3551 CoreVMFunction_exit(ScriptVM* vm) : vm(vm) {}
174 schoenebeck 3557 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 schoenebeck 3551 protected:
180     ScriptVM* vm;
181 schoenebeck 2581 };
182    
183 schoenebeck 2727 /**
184     * Implements the built-in wait() script function.
185     */
186 schoenebeck 2581 class CoreVMFunction_wait : public VMEmptyResultFunction {
187     public:
188     CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {}
189 schoenebeck 3557 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 schoenebeck 3561 bool acceptsArgUnitType(vmint iArg, StdUnit_t type) const OVERRIDE;
193 schoenebeck 3564 bool acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const OVERRIDE;
194 schoenebeck 3557 ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
195     VMFnResult* exec(VMFnArgs* args) OVERRIDE;
196 schoenebeck 2581 protected:
197     ScriptVM* vm;
198     };
199    
200 schoenebeck 2727 /**
201     * Implements the built-in abs() script function.
202     */
203 schoenebeck 2619 class CoreVMFunction_abs : public VMIntResultFunction {
204     public:
205 schoenebeck 3557 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 schoenebeck 2619 };
211    
212 schoenebeck 2727 /**
213     * Implements the built-in random() script function.
214     */
215 schoenebeck 2619 class CoreVMFunction_random : public VMIntResultFunction {
216     public:
217 schoenebeck 3557 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 schoenebeck 2619 };
223    
224 schoenebeck 2727 /**
225     * Implements the built-in num_elements() script function.
226     */
227 schoenebeck 2619 class CoreVMFunction_num_elements : public VMIntResultFunction {
228     public:
229 schoenebeck 3557 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 schoenebeck 2619 };
235    
236 schoenebeck 2945 /**
237     * Implements the built-in inc() script function.
238     */
239     class CoreVMFunction_inc : public VMIntResultFunction {
240     public:
241 schoenebeck 3557 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 schoenebeck 2945 };
248    
249     /**
250     * Implements the built-in dec() script function.
251     */
252     class CoreVMFunction_dec : public VMIntResultFunction {
253     public:
254 schoenebeck 3557 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 schoenebeck 2945 };
261    
262 schoenebeck 2965 /**
263 schoenebeck 3076 * Implements the built-in in_range() script function.
264     */
265     class CoreVMFunction_in_range : public VMIntResultFunction {
266     public:
267 schoenebeck 3557 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 schoenebeck 3076 };
273    
274     /**
275 schoenebeck 2965 * Implements the built-in sh_left() script function.
276     */
277     class CoreVMFunction_sh_left : public VMIntResultFunction {
278     public:
279 schoenebeck 3557 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 schoenebeck 2965 };
285    
286     /**
287     * Implements the built-in sh_right() script function.
288     */
289     class CoreVMFunction_sh_right : public VMIntResultFunction {
290     public:
291 schoenebeck 3557 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 schoenebeck 2965 };
297    
298 schoenebeck 2970 /**
299     * Implements the built-in min() script function.
300     */
301     class CoreVMFunction_min : public VMIntResultFunction {
302     public:
303 schoenebeck 3557 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 schoenebeck 2970 };
309    
310     /**
311     * Implements the built-in max() script function.
312     */
313     class CoreVMFunction_max : public VMIntResultFunction {
314     public:
315 schoenebeck 3557 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 schoenebeck 2970 };
321    
322 schoenebeck 3221 /**
323     * Implements the built-in array_equal() script function.
324     */
325     class CoreVMFunction_array_equal : public VMIntResultFunction {
326     public:
327 schoenebeck 3557 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 schoenebeck 3221 };
333    
334     /**
335     * Implements the built-in search() script function.
336     */
337     class CoreVMFunction_search : public VMIntResultFunction {
338     public:
339 schoenebeck 3557 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 schoenebeck 3221 };
345    
346     /**
347     * Implements the built-in sort() script function.
348     */
349     class CoreVMFunction_sort : public VMEmptyResultFunction {
350     public:
351 schoenebeck 3557 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 schoenebeck 3221 };
358    
359 schoenebeck 3573 /**
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 schoenebeck 2581 } // namespace LinuxSampler
388    
389     #endif // LS_COREVMFUNCTIONS_H

  ViewVC Help
Powered by ViewVC