/[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 3076 - (show annotations) (download) (as text)
Thu Jan 5 18:00:52 2017 UTC (7 years, 2 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 9305 byte(s)
* NKSP: Implemented built-in script function "in_range()".
* Bumped version (2.0.0.svn36).

1 /*
2 * Copyright (c) 2014-2016 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 int value; ///< result value of the function call
46
47 VMIntResult() : flags(STMT_SUCCESS) {}
48 int 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(int 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(int i = 0);
94 VMFnResult* successResult(int i = 0);
95 bool modifiesArg(int 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(int 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 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 /**
132 * Implements the built-in exit() script function.
133 */
134 class CoreVMFunction_exit : public VMEmptyResultFunction {
135 public:
136 int minRequiredArgs() const { return 0; }
137 int maxAllowedArgs() const { return 0; }
138 bool acceptsArgType(int iArg, ExprType_t type) const { return false; }
139 ExprType_t argType(int iArg) const { return INT_EXPR; /*whatever*/ }
140 VMFnResult* exec(VMFnArgs* args);
141 };
142
143 /**
144 * Implements the built-in wait() script function.
145 */
146 class CoreVMFunction_wait : public VMEmptyResultFunction {
147 public:
148 CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {}
149 int minRequiredArgs() const { return 1; }
150 int maxAllowedArgs() const { return 1; }
151 bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }
152 ExprType_t argType(int iArg) const { return INT_EXPR; }
153 VMFnResult* exec(VMFnArgs* args);
154 protected:
155 ScriptVM* vm;
156 };
157
158 /**
159 * Implements the built-in abs() script function.
160 */
161 class CoreVMFunction_abs : public VMIntResultFunction {
162 public:
163 int minRequiredArgs() const { return 1; }
164 int maxAllowedArgs() const { return 1; }
165 bool acceptsArgType(int iArg, ExprType_t type) const;
166 ExprType_t argType(int iArg) const { return INT_EXPR; }
167 VMFnResult* exec(VMFnArgs* args);
168 };
169
170 /**
171 * Implements the built-in random() script function.
172 */
173 class CoreVMFunction_random : public VMIntResultFunction {
174 public:
175 int minRequiredArgs() const { return 2; }
176 int maxAllowedArgs() const { return 2; }
177 bool acceptsArgType(int iArg, ExprType_t type) const;
178 ExprType_t argType(int iArg) const { return INT_EXPR; }
179 VMFnResult* exec(VMFnArgs* args);
180 };
181
182 /**
183 * Implements the built-in num_elements() script function.
184 */
185 class CoreVMFunction_num_elements : public VMIntResultFunction {
186 public:
187 int minRequiredArgs() const { return 1; }
188 int maxAllowedArgs() const { return 1; }
189 bool acceptsArgType(int iArg, ExprType_t type) const;
190 ExprType_t argType(int iArg) const { return INT_ARR_EXPR; }
191 VMFnResult* exec(VMFnArgs* args);
192 };
193
194 /**
195 * Implements the built-in inc() script function.
196 */
197 class CoreVMFunction_inc : public VMIntResultFunction {
198 public:
199 int minRequiredArgs() const { return 1; }
200 int maxAllowedArgs() const { return 1; }
201 bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }
202 ExprType_t argType(int iArg) const { return INT_EXPR; }
203 bool modifiesArg(int iArg) const { return true; }
204 VMFnResult* exec(VMFnArgs* args);
205 };
206
207 /**
208 * Implements the built-in dec() script function.
209 */
210 class CoreVMFunction_dec : public VMIntResultFunction {
211 public:
212 int minRequiredArgs() const { return 1; }
213 int maxAllowedArgs() const { return 1; }
214 bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }
215 ExprType_t argType(int iArg) const { return INT_EXPR; }
216 bool modifiesArg(int iArg) const { return true; }
217 VMFnResult* exec(VMFnArgs* args);
218 };
219
220 /**
221 * Implements the built-in in_range() script function.
222 */
223 class CoreVMFunction_in_range : public VMIntResultFunction {
224 public:
225 int minRequiredArgs() const { return 3; }
226 int maxAllowedArgs() const { return 3; }
227 bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }
228 ExprType_t argType(int iArg) const { return INT_EXPR; }
229 VMFnResult* exec(VMFnArgs* args);
230 };
231
232 /**
233 * Implements the built-in sh_left() script function.
234 */
235 class CoreVMFunction_sh_left : public VMIntResultFunction {
236 public:
237 int minRequiredArgs() const { return 2; }
238 int maxAllowedArgs() const { return 2; }
239 bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }
240 ExprType_t argType(int iArg) const { return INT_EXPR; }
241 VMFnResult* exec(VMFnArgs* args);
242 };
243
244 /**
245 * Implements the built-in sh_right() script function.
246 */
247 class CoreVMFunction_sh_right : public VMIntResultFunction {
248 public:
249 int minRequiredArgs() const { return 2; }
250 int maxAllowedArgs() const { return 2; }
251 bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }
252 ExprType_t argType(int iArg) const { return INT_EXPR; }
253 VMFnResult* exec(VMFnArgs* args);
254 };
255
256 /**
257 * Implements the built-in min() script function.
258 */
259 class CoreVMFunction_min : public VMIntResultFunction {
260 public:
261 int minRequiredArgs() const { return 2; }
262 int maxAllowedArgs() const { return 2; }
263 bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }
264 ExprType_t argType(int iArg) const { return INT_EXPR; }
265 VMFnResult* exec(VMFnArgs* args);
266 };
267
268 /**
269 * Implements the built-in max() script function.
270 */
271 class CoreVMFunction_max : public VMIntResultFunction {
272 public:
273 int minRequiredArgs() const { return 2; }
274 int maxAllowedArgs() const { return 2; }
275 bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }
276 ExprType_t argType(int iArg) const { return INT_EXPR; }
277 VMFnResult* exec(VMFnArgs* args);
278 };
279
280 } // namespace LinuxSampler
281
282 #endif // LS_COREVMFUNCTIONS_H

  ViewVC Help
Powered by ViewVC