1 |
/* |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck |
* Copyright (c) 2014-2015 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
16 |
namespace LinuxSampler { |
namespace LinuxSampler { |
17 |
|
|
18 |
class ScriptVM; |
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 { |
class VMEmptyResult : public VMFnResult, public VMExpr { |
28 |
public: |
public: |
29 |
StmtFlags_t flags; |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
30 |
|
|
31 |
VMEmptyResult() : flags(STMT_SUCCESS) {} |
VMEmptyResult() : flags(STMT_SUCCESS) {} |
32 |
ExprType_t exprType() const { return EMPTY_EXPR; } |
ExprType_t exprType() const { return EMPTY_EXPR; } |
33 |
VMExpr* resultValue() { return this; } |
VMExpr* resultValue() { return this; } |
34 |
StmtFlags_t resultFlags() { return flags; } |
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 { |
class VMIntResult : public VMFnResult, public VMIntExpr { |
43 |
public: |
public: |
44 |
StmtFlags_t flags; |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
45 |
int value; |
int value; ///< result value of the function call |
46 |
|
|
47 |
VMIntResult() : flags(STMT_SUCCESS) {} |
VMIntResult() : flags(STMT_SUCCESS) {} |
48 |
int evalInt() { return value; } |
int evalInt() { return value; } |
49 |
VMExpr* resultValue() { return this; } |
VMExpr* resultValue() { return this; } |
50 |
StmtFlags_t resultFlags() { return flags; } |
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 { |
class VMStringResult : public VMFnResult, public VMStringExpr { |
59 |
public: |
public: |
60 |
StmtFlags_t flags; |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
61 |
String value; |
String value; ///< result value of the function call |
62 |
|
|
63 |
VMStringResult() : flags(STMT_SUCCESS) {} |
VMStringResult() : flags(STMT_SUCCESS) {} |
64 |
String evalStr() { return value; } |
String evalStr() { return value; } |
65 |
VMExpr* resultValue() { return this; } |
VMExpr* resultValue() { return this; } |
66 |
StmtFlags_t resultFlags() { return flags; } |
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 { |
class VMEmptyResultFunction : public VMFunction { |
75 |
protected: |
protected: |
76 |
ExprType_t returnType() { return EMPTY_EXPR; } |
ExprType_t returnType() { return EMPTY_EXPR; } |
77 |
VMFnResult* errorResult(); |
VMFnResult* errorResult(); |
78 |
VMFnResult* successResult(); |
VMFnResult* successResult(); |
79 |
|
bool modifiesArg(int iArg) const OVERRIDE { return false; } |
80 |
protected: |
protected: |
81 |
VMEmptyResult result; |
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 { |
class VMIntResultFunction : public VMFunction { |
89 |
protected: |
protected: |
90 |
ExprType_t returnType() { return INT_EXPR; } |
ExprType_t returnType() { return INT_EXPR; } |
91 |
VMFnResult* errorResult(int i = 0); |
VMFnResult* errorResult(int i = 0); |
92 |
VMFnResult* successResult(int i = 0); |
VMFnResult* successResult(int i = 0); |
93 |
|
bool modifiesArg(int iArg) const OVERRIDE { return false; } |
94 |
protected: |
protected: |
95 |
VMIntResult result; |
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 { |
class VMStringResultFunction : public VMFunction { |
103 |
protected: |
protected: |
104 |
ExprType_t returnType() { return STRING_EXPR; } |
ExprType_t returnType() { return STRING_EXPR; } |
105 |
VMFnResult* errorResult(const String& s = ""); |
VMFnResult* errorResult(const String& s = ""); |
106 |
VMFnResult* successResult(const String& s = ""); |
VMFnResult* successResult(const String& s = ""); |
107 |
|
bool modifiesArg(int iArg) const OVERRIDE { return false; } |
108 |
protected: |
protected: |
109 |
VMStringResult result; |
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 { |
class CoreVMFunction_message : public VMEmptyResultFunction { |
120 |
public: |
public: |
121 |
int minRequiredArgs() const { return 1; } |
int minRequiredArgs() const { return 1; } |
125 |
VMFnResult* exec(VMFnArgs* args); |
VMFnResult* exec(VMFnArgs* args); |
126 |
}; |
}; |
127 |
|
|
128 |
|
/** |
129 |
|
* Implements the built-in exit() script function. |
130 |
|
*/ |
131 |
class CoreVMFunction_exit : public VMEmptyResultFunction { |
class CoreVMFunction_exit : public VMEmptyResultFunction { |
132 |
public: |
public: |
133 |
int minRequiredArgs() const { return 0; } |
int minRequiredArgs() const { return 0; } |
137 |
VMFnResult* exec(VMFnArgs* args); |
VMFnResult* exec(VMFnArgs* args); |
138 |
}; |
}; |
139 |
|
|
140 |
|
/** |
141 |
|
* Implements the built-in wait() script function. |
142 |
|
*/ |
143 |
class CoreVMFunction_wait : public VMEmptyResultFunction { |
class CoreVMFunction_wait : public VMEmptyResultFunction { |
144 |
public: |
public: |
145 |
CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {} |
CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {} |
152 |
ScriptVM* vm; |
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 |
} // namespace LinuxSampler |
266 |
|
|
267 |
#endif // LS_COREVMFUNCTIONS_H |
#endif // LS_COREVMFUNCTIONS_H |