1 |
/* |
2 |
* Copyright (c) 2014-2015 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 { return EMPTY_EXPR; } |
33 |
VMExpr* resultValue() { return this; } |
34 |
StmtFlags_t resultFlags() { 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() { return value; } |
49 |
VMExpr* resultValue() { return this; } |
50 |
StmtFlags_t resultFlags() { 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() { return value; } |
65 |
VMExpr* resultValue() { return this; } |
66 |
StmtFlags_t resultFlags() { 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 |
ExprType_t returnType() { return EMPTY_EXPR; } |
77 |
VMFnResult* errorResult(); |
78 |
VMFnResult* successResult(); |
79 |
bool modifiesArg(int iArg) const OVERRIDE { return false; } |
80 |
protected: |
81 |
VMEmptyResult result; |
82 |
}; |
83 |
|
84 |
/** |
85 |
* Abstract base class for built-in script functions which return an integer |
86 |
* (scalar) as their function return value. |
87 |
*/ |
88 |
class VMIntResultFunction : public VMFunction { |
89 |
protected: |
90 |
ExprType_t returnType() { return INT_EXPR; } |
91 |
VMFnResult* errorResult(int i = 0); |
92 |
VMFnResult* successResult(int i = 0); |
93 |
bool modifiesArg(int iArg) const OVERRIDE { return false; } |
94 |
protected: |
95 |
VMIntResult result; |
96 |
}; |
97 |
|
98 |
/** |
99 |
* Abstract base class for built-in script functions which return a string as |
100 |
* their function return value. |
101 |
*/ |
102 |
class VMStringResultFunction : public VMFunction { |
103 |
protected: |
104 |
ExprType_t returnType() { return STRING_EXPR; } |
105 |
VMFnResult* errorResult(const String& s = ""); |
106 |
VMFnResult* successResult(const String& s = ""); |
107 |
bool modifiesArg(int iArg) const OVERRIDE { return false; } |
108 |
protected: |
109 |
VMStringResult result; |
110 |
}; |
111 |
|
112 |
|
113 |
/////////////////////////////////////////////////////////////////////////// |
114 |
// implementations of core built-in script functions ... |
115 |
|
116 |
/** |
117 |
* Implements the built-in message() script function. |
118 |
*/ |
119 |
class CoreVMFunction_message : public VMEmptyResultFunction { |
120 |
public: |
121 |
int minRequiredArgs() const { return 1; } |
122 |
int maxAllowedArgs() const { return 1; } |
123 |
bool acceptsArgType(int iArg, ExprType_t type) const; |
124 |
ExprType_t argType(int iArg) const { return STRING_EXPR; } |
125 |
VMFnResult* exec(VMFnArgs* args); |
126 |
}; |
127 |
|
128 |
/** |
129 |
* Implements the built-in exit() script function. |
130 |
*/ |
131 |
class CoreVMFunction_exit : public VMEmptyResultFunction { |
132 |
public: |
133 |
int minRequiredArgs() const { return 0; } |
134 |
int maxAllowedArgs() const { return 0; } |
135 |
bool acceptsArgType(int iArg, ExprType_t type) const { return false; } |
136 |
ExprType_t argType(int iArg) const { return INT_EXPR; /*whatever*/ } |
137 |
VMFnResult* exec(VMFnArgs* args); |
138 |
}; |
139 |
|
140 |
/** |
141 |
* Implements the built-in wait() script function. |
142 |
*/ |
143 |
class CoreVMFunction_wait : public VMEmptyResultFunction { |
144 |
public: |
145 |
CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {} |
146 |
int minRequiredArgs() const { return 1; } |
147 |
int maxAllowedArgs() const { return 1; } |
148 |
bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; } |
149 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
150 |
VMFnResult* exec(VMFnArgs* args); |
151 |
protected: |
152 |
ScriptVM* vm; |
153 |
}; |
154 |
|
155 |
/** |
156 |
* Implements the built-in abs() script function. |
157 |
*/ |
158 |
class CoreVMFunction_abs : public VMIntResultFunction { |
159 |
public: |
160 |
int minRequiredArgs() const { return 1; } |
161 |
int maxAllowedArgs() const { return 1; } |
162 |
bool acceptsArgType(int iArg, ExprType_t type) const; |
163 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
164 |
VMFnResult* exec(VMFnArgs* args); |
165 |
}; |
166 |
|
167 |
/** |
168 |
* Implements the built-in random() script function. |
169 |
*/ |
170 |
class CoreVMFunction_random : public VMIntResultFunction { |
171 |
public: |
172 |
int minRequiredArgs() const { return 2; } |
173 |
int maxAllowedArgs() const { return 2; } |
174 |
bool acceptsArgType(int iArg, ExprType_t type) const; |
175 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
176 |
VMFnResult* exec(VMFnArgs* args); |
177 |
}; |
178 |
|
179 |
/** |
180 |
* Implements the built-in num_elements() script function. |
181 |
*/ |
182 |
class CoreVMFunction_num_elements : public VMIntResultFunction { |
183 |
public: |
184 |
int minRequiredArgs() const { return 1; } |
185 |
int maxAllowedArgs() const { return 1; } |
186 |
bool acceptsArgType(int iArg, ExprType_t type) const; |
187 |
ExprType_t argType(int iArg) const { return INT_ARR_EXPR; } |
188 |
VMFnResult* exec(VMFnArgs* args); |
189 |
}; |
190 |
|
191 |
/** |
192 |
* Implements the built-in inc() script function. |
193 |
*/ |
194 |
class CoreVMFunction_inc : public VMIntResultFunction { |
195 |
public: |
196 |
int minRequiredArgs() const { return 1; } |
197 |
int maxAllowedArgs() const { return 1; } |
198 |
bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; } |
199 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
200 |
bool modifiesArg(int iArg) const { return true; } |
201 |
VMFnResult* exec(VMFnArgs* args); |
202 |
}; |
203 |
|
204 |
/** |
205 |
* Implements the built-in dec() script function. |
206 |
*/ |
207 |
class CoreVMFunction_dec : public VMIntResultFunction { |
208 |
public: |
209 |
int minRequiredArgs() const { return 1; } |
210 |
int maxAllowedArgs() const { return 1; } |
211 |
bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; } |
212 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
213 |
bool modifiesArg(int iArg) const { return true; } |
214 |
VMFnResult* exec(VMFnArgs* args); |
215 |
}; |
216 |
|
217 |
/** |
218 |
* Implements the built-in sh_left() script function. |
219 |
*/ |
220 |
class CoreVMFunction_sh_left : public VMIntResultFunction { |
221 |
public: |
222 |
int minRequiredArgs() const { return 2; } |
223 |
int maxAllowedArgs() const { return 2; } |
224 |
bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; } |
225 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
226 |
VMFnResult* exec(VMFnArgs* args); |
227 |
}; |
228 |
|
229 |
/** |
230 |
* Implements the built-in sh_right() script function. |
231 |
*/ |
232 |
class CoreVMFunction_sh_right : public VMIntResultFunction { |
233 |
public: |
234 |
int minRequiredArgs() const { return 2; } |
235 |
int maxAllowedArgs() const { return 2; } |
236 |
bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; } |
237 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
238 |
VMFnResult* exec(VMFnArgs* args); |
239 |
}; |
240 |
|
241 |
/** |
242 |
* Implements the built-in min() script function. |
243 |
*/ |
244 |
class CoreVMFunction_min : public VMIntResultFunction { |
245 |
public: |
246 |
int minRequiredArgs() const { return 2; } |
247 |
int maxAllowedArgs() const { return 2; } |
248 |
bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; } |
249 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
250 |
VMFnResult* exec(VMFnArgs* args); |
251 |
}; |
252 |
|
253 |
/** |
254 |
* Implements the built-in max() script function. |
255 |
*/ |
256 |
class CoreVMFunction_max : public VMIntResultFunction { |
257 |
public: |
258 |
int minRequiredArgs() const { return 2; } |
259 |
int maxAllowedArgs() const { return 2; } |
260 |
bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; } |
261 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
262 |
VMFnResult* exec(VMFnArgs* args); |
263 |
}; |
264 |
|
265 |
} // namespace LinuxSampler |
266 |
|
267 |
#endif // LS_COREVMFUNCTIONS_H |