/[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 3551 - (hide annotations) (download) (as text)
Thu Aug 1 10:22:56 2019 UTC (4 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 10577 byte(s)
* Added test cases for NKSP core language aspects and core built-in
  functions.
* NKSP: Added method ScriptVM::setExitResultEnabled() which allows
  to explicitly enable the built-in exit() function to optionally
  accept one function argument; the value of the passed exit()
  function argument will then become available by calling
  VMExecContext::exitResult() after script execution.
* Bumped version (2.1.1.svn2).

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

  ViewVC Help
Powered by ViewVC