/[svn]/linuxsampler/trunk/src/scriptvm/CoreVMFunctions.h
ViewVC logotype

Diff of /linuxsampler/trunk/src/scriptvm/CoreVMFunctions.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3076 by schoenebeck, Thu Jan 5 18:00:52 2017 UTC revision 3577 by schoenebeck, Wed Aug 28 15:23:23 2019 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014-2016 Christian Schoenebeck   * Copyright (c) 2014-2019 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 42  public: Line 42  public:
42  class VMIntResult : public VMFnResult, public VMIntExpr {  class VMIntResult : public VMFnResult, public VMIntExpr {
43  public:  public:
44      StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call      StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call
45      int value; ///< result value of the function call      vmint value; ///< result value of the function call
46    
47      VMIntResult() : flags(STMT_SUCCESS) {}      VMIntResult() : flags(STMT_SUCCESS), value(0) {}
48      int evalInt() OVERRIDE { return value; }      vmint evalInt() OVERRIDE { return value; }
49      VMExpr* resultValue() OVERRIDE { return this; }      VMExpr* resultValue() OVERRIDE { return this; }
50      StmtFlags_t resultFlags() OVERRIDE { return flags; }      StmtFlags_t resultFlags() OVERRIDE { return flags; }
51      bool isConstExpr() const OVERRIDE { return false; }      bool isConstExpr() const OVERRIDE { return false; }
52        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    };
56    
57    /**
58     * An instance of this class is returned by built-in function implementations
59     * 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  /**  /**
# Line 74  public: Line 96  public:
96  class VMEmptyResultFunction : public VMFunction {  class VMEmptyResultFunction : public VMFunction {
97  protected:  protected:
98      virtual ~VMEmptyResultFunction() {}      virtual ~VMEmptyResultFunction() {}
99      ExprType_t returnType() OVERRIDE { return EMPTY_EXPR; }      ExprType_t returnType(VMFnArgs* args) OVERRIDE { return EMPTY_EXPR; }
100      VMFnResult* errorResult();      VMFnResult* errorResult();
101      VMFnResult* successResult();      VMFnResult* successResult();
102      bool modifiesArg(int iArg) const OVERRIDE { return false; }      bool modifiesArg(vmint iArg) const OVERRIDE { return false; }
103  protected:  protected:
104      VMEmptyResult result;      VMEmptyResult result;
105  };  };
# Line 89  protected: Line 111  protected:
111  class VMIntResultFunction : public VMFunction {  class VMIntResultFunction : public VMFunction {
112  protected:  protected:
113      virtual ~VMIntResultFunction() {}      virtual ~VMIntResultFunction() {}
114      ExprType_t returnType() OVERRIDE { return INT_EXPR; }      ExprType_t returnType(VMFnArgs* args) OVERRIDE { return INT_EXPR; }
115      VMFnResult* errorResult(int i = 0);      VMFnResult* errorResult(vmint i = 0);
116      VMFnResult* successResult(int i = 0);      VMFnResult* successResult(vmint i = 0);
117      bool modifiesArg(int iArg) const OVERRIDE { return false; }      bool modifiesArg(vmint iArg) const OVERRIDE { return false; }
118  protected:  protected:
119      VMIntResult result;      VMIntResult result;
120  };  };
121    
122  /**  /**
123     * 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(VMFnArgs* args) 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   * Abstract base class for built-in script functions which return a string as   * Abstract base class for built-in script functions which return a string as
139   * their function return value.   * their function return value.
140   */   */
141  class VMStringResultFunction : public VMFunction {  class VMStringResultFunction : public VMFunction {
142  protected:  protected:
143      virtual ~VMStringResultFunction() {}      virtual ~VMStringResultFunction() {}
144      ExprType_t returnType() OVERRIDE { return STRING_EXPR; }      ExprType_t returnType(VMFnArgs* args) OVERRIDE { return STRING_EXPR; }
145      VMFnResult* errorResult(const String& s = "");      VMFnResult* errorResult(const String& s = "");
146      VMFnResult* successResult(const String& s = "");      VMFnResult* successResult(const String& s = "");
147      bool modifiesArg(int iArg) const OVERRIDE { return false; }      bool modifiesArg(vmint iArg) const OVERRIDE { return false; }
148  protected:  protected:
149      VMStringResult result;      VMStringResult result;
150  };  };
151    
152    /**
153     * Abstract base class for built-in script functions which either return an
154     * integer or a real number (floating point scalar) as their function return
155     * value. The actual return type is determined at parse time once after
156     * potential arguments were associated with the respective function call.
157     */
158    class VMScalarNumberResultFunction : public VMFunction {
159    protected:
160        virtual ~VMScalarNumberResultFunction() {}
161        VMFnResult* errorResult(vmint i);
162        VMFnResult* errorResult(vmfloat f);
163        VMFnResult* successResult(vmint i);
164        VMFnResult* successResult(vmfloat f);
165        bool modifiesArg(vmint iArg) const OVERRIDE { return false; }
166    protected:
167        VMIntResult intResult;
168        VMRealResult realResult;
169    };
170    
171    
172  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
173  // implementations of core built-in script functions ...  // implementations of core built-in script functions ...
# Line 121  protected: Line 177  protected:
177   */   */
178  class CoreVMFunction_message : public VMEmptyResultFunction {  class CoreVMFunction_message : public VMEmptyResultFunction {
179  public:  public:
180      int minRequiredArgs() const { return 1; }      vmint minRequiredArgs() const OVERRIDE { return 1; }
181      int maxAllowedArgs() const { return 1; }      vmint maxAllowedArgs() const OVERRIDE { return 1; }
182      bool acceptsArgType(int iArg, ExprType_t type) const;      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
183      ExprType_t argType(int iArg) const { return STRING_EXPR; }      ExprType_t argType(vmint iArg) const OVERRIDE { return STRING_EXPR; }
184      VMFnResult* exec(VMFnArgs* args);      VMFnResult* exec(VMFnArgs* args) OVERRIDE;
185  };  };
186    
187  /**  /**
# Line 133  public: Line 189  public:
189   */   */
190  class CoreVMFunction_exit : public VMEmptyResultFunction {  class CoreVMFunction_exit : public VMEmptyResultFunction {
191  public:  public:
192      int minRequiredArgs() const { return 0; }      CoreVMFunction_exit(ScriptVM* vm) : vm(vm) {}
193      int maxAllowedArgs() const { return 0; }      vmint minRequiredArgs() const OVERRIDE { return 0; }
194      bool acceptsArgType(int iArg, ExprType_t type) const { return false; }      vmint maxAllowedArgs() const OVERRIDE;
195      ExprType_t argType(int iArg) const { return INT_EXPR; /*whatever*/ }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
196      VMFnResult* exec(VMFnArgs* args);      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
197        VMFnResult* exec(VMFnArgs* args) OVERRIDE;
198    protected:
199        ScriptVM* vm;
200  };  };
201    
202  /**  /**
# Line 146  public: Line 205  public:
205  class CoreVMFunction_wait : public VMEmptyResultFunction {  class CoreVMFunction_wait : public VMEmptyResultFunction {
206  public:  public:
207      CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {}      CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {}
208      int minRequiredArgs() const { return 1; }      vmint minRequiredArgs() const OVERRIDE { return 1; }
209      int maxAllowedArgs() const { return 1; }      vmint maxAllowedArgs() const OVERRIDE { return 1; }
210      bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
211      ExprType_t argType(int iArg) const { return INT_EXPR; }      bool acceptsArgUnitType(vmint iArg, StdUnit_t type) const OVERRIDE;
212      VMFnResult* exec(VMFnArgs* args);      bool acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const OVERRIDE;
213        ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
214        VMFnResult* exec(VMFnArgs* args) OVERRIDE;
215  protected:  protected:
216      ScriptVM* vm;      ScriptVM* vm;
217  };  };
# Line 158  protected: Line 219  protected:
219  /**  /**
220   * Implements the built-in abs() script function.   * Implements the built-in abs() script function.
221   */   */
222  class CoreVMFunction_abs : public VMIntResultFunction {  class CoreVMFunction_abs : public VMScalarNumberResultFunction {
223  public:  public:
224      int minRequiredArgs() const { return 1; }      ExprType_t returnType(VMFnArgs* args) OVERRIDE;
225      int maxAllowedArgs() const { return 1; }      vmint minRequiredArgs() const OVERRIDE { return 1; }
226      bool acceptsArgType(int iArg, ExprType_t type) const;      vmint maxAllowedArgs() const OVERRIDE { return 1; }
227      ExprType_t argType(int iArg) const { return INT_EXPR; }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
228      VMFnResult* exec(VMFnArgs* args);      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
229        VMFnResult* exec(VMFnArgs* args) OVERRIDE;
230  };  };
231    
232  /**  /**
# Line 172  public: Line 234  public:
234   */   */
235  class CoreVMFunction_random : public VMIntResultFunction {  class CoreVMFunction_random : public VMIntResultFunction {
236  public:  public:
237      int minRequiredArgs() const { return 2; }      vmint minRequiredArgs() const OVERRIDE { return 2; }
238      int maxAllowedArgs() const { return 2; }      vmint maxAllowedArgs() const OVERRIDE { return 2; }
239      bool acceptsArgType(int iArg, ExprType_t type) const;      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
240      ExprType_t argType(int iArg) const { return INT_EXPR; }      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
241      VMFnResult* exec(VMFnArgs* args);      VMFnResult* exec(VMFnArgs* args) OVERRIDE;
242  };  };
243    
244  /**  /**
# Line 184  public: Line 246  public:
246   */   */
247  class CoreVMFunction_num_elements : public VMIntResultFunction {  class CoreVMFunction_num_elements : public VMIntResultFunction {
248  public:  public:
249      int minRequiredArgs() const { return 1; }      vmint minRequiredArgs() const OVERRIDE { return 1; }
250      int maxAllowedArgs() const { return 1; }      vmint maxAllowedArgs() const OVERRIDE { return 1; }
251      bool acceptsArgType(int iArg, ExprType_t type) const;      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
252      ExprType_t argType(int iArg) const { return INT_ARR_EXPR; }      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_ARR_EXPR; }
253      VMFnResult* exec(VMFnArgs* args);      VMFnResult* exec(VMFnArgs* args) OVERRIDE;
254  };  };
255    
256  /**  /**
# Line 196  public: Line 258  public:
258   */   */
259  class CoreVMFunction_inc : public VMIntResultFunction {  class CoreVMFunction_inc : public VMIntResultFunction {
260  public:  public:
261      int minRequiredArgs() const { return 1; }      vmint minRequiredArgs() const OVERRIDE { return 1; }
262      int maxAllowedArgs() const { return 1; }      vmint maxAllowedArgs() const OVERRIDE { return 1; }
263      bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
264      ExprType_t argType(int iArg) const { return INT_EXPR; }      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
265      bool modifiesArg(int iArg) const { return true; }      bool modifiesArg(vmint iArg) const OVERRIDE { return true; }
266      VMFnResult* exec(VMFnArgs* args);      VMFnResult* exec(VMFnArgs* args) OVERRIDE;
267  };  };
268    
269  /**  /**
# Line 209  public: Line 271  public:
271   */   */
272  class CoreVMFunction_dec : public VMIntResultFunction {  class CoreVMFunction_dec : public VMIntResultFunction {
273  public:  public:
274      int minRequiredArgs() const { return 1; }      vmint minRequiredArgs() const OVERRIDE { return 1; }
275      int maxAllowedArgs() const { return 1; }      vmint maxAllowedArgs() const OVERRIDE { return 1; }
276      bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
277      ExprType_t argType(int iArg) const { return INT_EXPR; }      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
278      bool modifiesArg(int iArg) const { return true; }      bool modifiesArg(vmint iArg) const OVERRIDE { return true; }
279      VMFnResult* exec(VMFnArgs* args);      VMFnResult* exec(VMFnArgs* args) OVERRIDE;
280  };  };
281    
282  /**  /**
# Line 222  public: Line 284  public:
284   */   */
285  class CoreVMFunction_in_range : public VMIntResultFunction {  class CoreVMFunction_in_range : public VMIntResultFunction {
286  public:  public:
287      int minRequiredArgs() const { return 3; }      vmint minRequiredArgs() const OVERRIDE { return 3; }
288      int maxAllowedArgs() const { return 3; }      vmint maxAllowedArgs() const OVERRIDE { return 3; }
289      bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
290      ExprType_t argType(int iArg) const { return INT_EXPR; }      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
291      VMFnResult* exec(VMFnArgs* args);      VMFnResult* exec(VMFnArgs* args) OVERRIDE;
292  };  };
293    
294  /**  /**
# Line 234  public: Line 296  public:
296   */   */
297  class CoreVMFunction_sh_left : public VMIntResultFunction {  class CoreVMFunction_sh_left : public VMIntResultFunction {
298  public:  public:
299      int minRequiredArgs() const { return 2; }      vmint minRequiredArgs() const OVERRIDE { return 2; }
300      int maxAllowedArgs() const { return 2; }      vmint maxAllowedArgs() const OVERRIDE { return 2; }
301      bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
302      ExprType_t argType(int iArg) const { return INT_EXPR; }      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
303      VMFnResult* exec(VMFnArgs* args);      VMFnResult* exec(VMFnArgs* args) OVERRIDE;
304  };  };
305    
306  /**  /**
# Line 246  public: Line 308  public:
308   */   */
309  class CoreVMFunction_sh_right : public VMIntResultFunction {  class CoreVMFunction_sh_right : public VMIntResultFunction {
310  public:  public:
311      int minRequiredArgs() const { return 2; }      vmint minRequiredArgs() const OVERRIDE { return 2; }
312      int maxAllowedArgs() const { return 2; }      vmint maxAllowedArgs() const OVERRIDE { return 2; }
313      bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
314      ExprType_t argType(int iArg) const { return INT_EXPR; }      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
315      VMFnResult* exec(VMFnArgs* args);      VMFnResult* exec(VMFnArgs* args) OVERRIDE;
316  };  };
317    
318  /**  /**
319   * Implements the built-in min() script function.   * Implements the built-in min() script function.
320   */   */
321  class CoreVMFunction_min : public VMIntResultFunction {  class CoreVMFunction_min : public VMScalarNumberResultFunction {
322  public:  public:
323      int minRequiredArgs() const { return 2; }      ExprType_t returnType(VMFnArgs* args) OVERRIDE;
324      int maxAllowedArgs() const { return 2; }      vmint minRequiredArgs() const OVERRIDE { return 2; }
325      bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }      vmint maxAllowedArgs() const OVERRIDE { return 2; }
326      ExprType_t argType(int iArg) const { return INT_EXPR; }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
327      VMFnResult* exec(VMFnArgs* args);      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
328        VMFnResult* exec(VMFnArgs* args) OVERRIDE;
329  };  };
330    
331  /**  /**
332   * Implements the built-in max() script function.   * Implements the built-in max() script function.
333   */   */
334  class CoreVMFunction_max : public VMIntResultFunction {  class CoreVMFunction_max : public VMScalarNumberResultFunction {
335    public:
336        ExprType_t returnType(VMFnArgs* args) OVERRIDE;
337        vmint minRequiredArgs() const OVERRIDE { return 2; }
338        vmint maxAllowedArgs() const OVERRIDE { return 2; }
339        bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
340        ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
341        VMFnResult* exec(VMFnArgs* args) OVERRIDE;
342    };
343    
344    /**
345     * Implements the built-in array_equal() script function.
346     */
347    class CoreVMFunction_array_equal : public VMIntResultFunction {
348    public:
349        vmint minRequiredArgs() const OVERRIDE { return 2; }
350        vmint maxAllowedArgs() const OVERRIDE { return 2; }
351        bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_ARR_EXPR; }
352        ExprType_t argType(vmint iArg) const OVERRIDE { return INT_ARR_EXPR; }
353        VMFnResult* exec(VMFnArgs* args) OVERRIDE;
354    };
355    
356    /**
357     * Implements the built-in search() script function.
358     */
359    class CoreVMFunction_search : public VMIntResultFunction {
360    public:
361        vmint minRequiredArgs() const OVERRIDE { return 2; }
362        vmint maxAllowedArgs() const OVERRIDE { return 2; }
363        bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
364        ExprType_t argType(vmint iArg) const OVERRIDE;
365        VMFnResult* exec(VMFnArgs* args) OVERRIDE;
366    };
367    
368    /**
369     * Implements the built-in sort() script function.
370     */
371    class CoreVMFunction_sort : public VMEmptyResultFunction {
372    public:
373        vmint minRequiredArgs() const OVERRIDE { return 1; }
374        vmint maxAllowedArgs() const OVERRIDE { return 2; }
375        bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE;
376        ExprType_t argType(vmint iArg) const OVERRIDE;
377        bool modifiesArg(vmint iArg) const OVERRIDE { return iArg == 0; }
378        VMFnResult* exec(VMFnArgs* args) OVERRIDE;
379    };
380    
381    /**
382     * Implements the built-in real_to_int() script function and its short hand
383     * variant int(). The behaviour of the two built-in script functions are
384     * identical ATM.
385     */
386    class CoreVMFunction_real_to_int : public VMIntResultFunction {
387    public:
388        vmint minRequiredArgs() const OVERRIDE { return 1; }
389        vmint maxAllowedArgs() const OVERRIDE { return 1; }
390        bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == REAL_EXPR; }
391        ExprType_t argType(vmint iArg) const OVERRIDE { return REAL_EXPR; }
392        VMFnResult* exec(VMFnArgs* args) OVERRIDE;
393    };
394    
395    /**
396     * Implements the built-in int_to_real() script function and its short hand
397     * variant real(). The behaviour of the two built-in script functions are
398     * identical ATM.
399     */
400    class CoreVMFunction_int_to_real : public VMRealResultFunction {
401  public:  public:
402      int minRequiredArgs() const { return 2; }      vmint minRequiredArgs() const OVERRIDE { return 1; }
403      int maxAllowedArgs() const { return 2; }      vmint maxAllowedArgs() const OVERRIDE { return 1; }
404      bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; }      bool acceptsArgType(vmint iArg, ExprType_t type) const OVERRIDE { return type == INT_EXPR; }
405      ExprType_t argType(int iArg) const { return INT_EXPR; }      ExprType_t argType(vmint iArg) const OVERRIDE { return INT_EXPR; }
406      VMFnResult* exec(VMFnArgs* args);      VMFnResult* exec(VMFnArgs* args) OVERRIDE;
407  };  };
408    
409  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.3076  
changed lines
  Added in v.3577

  ViewVC Help
Powered by ViewVC