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

Annotation of /linuxsampler/trunk/src/scriptvm/common.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3584 - (hide annotations) (download) (as text)
Fri Aug 30 13:33:32 2019 UTC (4 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 89040 byte(s)
* NKSP VM API: Added 4 overridden methods to class VMNumberExpr:
  evalCastInt(MetricPrefix_t), evalCastInt(MetricPrefix_t,MetricPrefix_t),
  evalCastReal(MetricPrefix_t), evalCastReal(MetricPrefix_t,MetricPrefix_t)
  as convenient methods for automatically converting values to expected
  metric value basis.

* NKSP built-in function "wait()" accepts now both integers and real numbers
  as argument.

* Bumped version (2.1.1.svn9).

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     // This header defines data types shared between the VM core implementation
11     // (inside the current source directory) and other parts of the sampler
12 schoenebeck 3292 // (located at other source directories). It also acts as public API of the
13     // Real-Time script engine for other applications.
14 schoenebeck 2581
15     #ifndef LS_INSTR_SCRIPT_PARSER_COMMON_H
16     #define LS_INSTR_SCRIPT_PARSER_COMMON_H
17    
18     #include "../common/global.h"
19 schoenebeck 2588 #include <vector>
20 schoenebeck 2594 #include <map>
21     #include <stddef.h> // offsetof()
22 schoenebeck 3581 #include <functional> // std::function<>
23 schoenebeck 2581
24     namespace LinuxSampler {
25 schoenebeck 2727
26     /**
27 schoenebeck 3557 * Native data type used by the script engine both internally, as well as
28     * for all integer data types used by scripts (i.e. for all $foo variables
29     * in NKSP scripts). Note that this is different from the original KSP which
30     * is limited to 32 bit for integer variables in KSP scripts.
31     */
32     typedef int64_t vmint;
33    
34     /**
35     * Native data type used internally by the script engine for all unsigned
36     * integer types. This type is currently not exposed to scripts.
37     */
38     typedef uint64_t vmuint;
39    
40     /**
41 schoenebeck 3573 * Native data type used by the script engine both internally for floating
42     * point data, as well as for all @c real data types used by scripts (i.e.
43     * for all ~foo variables in NKSP scripts).
44 schoenebeck 3561 */
45     typedef float vmfloat;
46    
47     /**
48 schoenebeck 2727 * Identifies the type of a noteworthy issue identified by the script
49     * parser. That's either a parser error or parser warning.
50     */
51 schoenebeck 2581 enum ParserIssueType_t {
52 schoenebeck 2727 PARSER_ERROR, ///< Script parser encountered an error, the script cannot be executed.
53     PARSER_WARNING ///< Script parser encountered a warning, the script may be executed if desired, but the script may not necessarily behave as originally intended by the script author.
54 schoenebeck 2581 };
55    
56 schoenebeck 2727 /** @brief Expression's data type.
57     *
58     * Identifies to which data type an expression within a script evaluates to.
59     * This can for example reflect the data type of script function arguments,
60     * script function return values, but also the resulting data type of some
61     * mathematical formula within a script.
62     */
63 schoenebeck 2581 enum ExprType_t {
64 schoenebeck 2727 EMPTY_EXPR, ///< i.e. on invalid expressions or i.e. a function call that does not return a result value (the built-in wait() or message() functions for instance)
65     INT_EXPR, ///< integer (scalar) expression
66     INT_ARR_EXPR, ///< integer array expression
67     STRING_EXPR, ///< string expression
68     STRING_ARR_EXPR, ///< string array expression
69 schoenebeck 3573 REAL_EXPR, ///< floating point (scalar) expression
70     REAL_ARR_EXPR, ///< floating point array expression
71 schoenebeck 2581 };
72    
73 schoenebeck 2727 /** @brief Result flags of a script statement or script function call.
74     *
75     * A set of bit flags which provide informations about the success or
76     * failure of a statement within a script. That's also especially used for
77     * providing informations about success / failure of a call to a built-in
78     * script function. The virtual machine evaluates these flags during runtime
79     * to decide whether it should i.e. stop or suspend execution of a script.
80     *
81     * Since these are bit flags, these constants are bitwise combined.
82     */
83 schoenebeck 2581 enum StmtFlags_t {
84     STMT_SUCCESS = 0, ///< Function / statement was executed successfully, no error occurred.
85 schoenebeck 2727 STMT_ABORT_SIGNALLED = 1, ///< VM should stop the current callback execution (usually because of an error, but might also be without an error reason, i.e. when the built-in script function exit() was called).
86     STMT_SUSPEND_SIGNALLED = (1<<1), ///< VM supended execution, either because the script called the built-in wait() script function or because the script consumed too much execution time and was forced by the VM to be suspended for some time
87     STMT_ERROR_OCCURRED = (1<<2), ///< VM stopped execution due to some script runtime error that occurred
88 schoenebeck 2581 };
89    
90 schoenebeck 2727 /** @brief Virtual machine execution status.
91     *
92     * A set of bit flags which reflect the current overall execution status of
93     * the virtual machine concerning a certain script execution instance.
94     *
95     * Since these are bit flags, these constants are bitwise combined.
96     */
97 schoenebeck 2581 enum VMExecStatus_t {
98 schoenebeck 2727 VM_EXEC_NOT_RUNNING = 0, ///< Script is currently not executed by the VM.
99     VM_EXEC_RUNNING = 1, ///< The VM is currently executing the script.
100     VM_EXEC_SUSPENDED = (1<<1), ///< Script is currently suspended by the VM, either because the script called the built-in wait() script function or because the script consumed too much execution time and was forced by the VM to be suspended for some time.
101     VM_EXEC_ERROR = (1<<2), ///< A runtime error occurred while executing the script (i.e. a call to some built-in script function failed).
102 schoenebeck 2581 };
103    
104 schoenebeck 2879 /** @brief Script event handler type.
105     *
106     * Identifies one of the possible event handler callback types defined by
107     * the NKSP script language.
108 schoenebeck 3557 *
109     * IMPORTANT: this type is forced to be emitted as int32_t type ATM, because
110     * that's the native size expected by the built-in instrument script
111     * variable bindings (see occurrences of VMInt32RelPtr and DECLARE_VMINT
112     * respectively. A native type mismatch between the two could lead to
113     * undefined behavior! Background: By definition the C/C++ compiler is free
114     * to choose a bit size for individual enums which it might find
115     * appropriate, which is usually decided by the compiler according to the
116     * biggest enum constant value defined (in practice it is usually 32 bit).
117 schoenebeck 2879 */
118 schoenebeck 3557 enum VMEventHandlerType_t : int32_t {
119 schoenebeck 2879 VM_EVENT_HANDLER_INIT, ///< Initilization event handler, that is script's "on init ... end on" code block.
120     VM_EVENT_HANDLER_NOTE, ///< Note event handler, that is script's "on note ... end on" code block.
121     VM_EVENT_HANDLER_RELEASE, ///< Release event handler, that is script's "on release ... end on" code block.
122     VM_EVENT_HANDLER_CONTROLLER, ///< Controller event handler, that is script's "on controller ... end on" code block.
123     };
124    
125 schoenebeck 3561 /**
126     * All metric unit prefixes (actually just scale factors) supported by this
127     * script engine.
128     */
129     enum MetricPrefix_t {
130     VM_NO_PREFIX = 0, ///< = 1
131     VM_KILO, ///< = 10^3, short 'k'
132     VM_HECTO, ///< = 10^2, short 'h'
133     VM_DECA, ///< = 10, short 'da'
134     VM_DECI, ///< = 10^-1, short 'd'
135     VM_CENTI, ///< = 10^-2, short 'c' (this is also used for tuning "cents")
136     VM_MILLI, ///< = 10^-3, short 'm'
137     VM_MICRO, ///< = 10^-6, short 'u'
138     };
139    
140     /**
141 schoenebeck 3581 * This constant is used for comparison with Unit::unitFactor() to check
142     * whether a number does have any metric unit prefix at all.
143     *
144     * @see Unit::unitFactor()
145     */
146     static const vmfloat VM_NO_FACTOR = vmfloat(1);
147    
148     /**
149 schoenebeck 3561 * All measurement unit types supported by this script engine.
150     *
151     * @e Note: there is no standard unit "cents" here (for pitch/tuning), use
152     * @c VM_CENTI for the latter instad. That's because the commonly cited
153     * "cents" unit is actually no measurement unit type but rather a metric
154     * unit prefix.
155     *
156     * @see MetricPrefix_t
157     */
158     enum StdUnit_t {
159     VM_NO_UNIT = 0, ///< No unit used, the number is just an abstract number.
160     VM_SECOND, ///< Measuring time.
161     VM_HERTZ, ///< Measuring frequency.
162     VM_BEL, ///< Measuring relation between two energy levels (in logarithmic scale). Since we are using it for accoustics, we are always referring to A-weighted Bels (i.e. dBA).
163     };
164    
165 schoenebeck 3581 //TODO: see Unit::hasUnitFactorEver()
166     enum EverTriState_t {
167     VM_NEVER = 0,
168     VM_MAYBE,
169     VM_ALWAYS,
170     };
171    
172 schoenebeck 2727 // just symbol prototyping
173 schoenebeck 2596 class VMIntExpr;
174 schoenebeck 3573 class VMRealExpr;
175 schoenebeck 2596 class VMStringExpr;
176 schoenebeck 3582 class VMNumberExpr;
177 schoenebeck 3581 class VMArrayExpr;
178 schoenebeck 2619 class VMIntArrayExpr;
179 schoenebeck 3573 class VMRealArrayExpr;
180 schoenebeck 2619 class VMStringArrayExpr;
181 schoenebeck 3311 class VMParserContext;
182 schoenebeck 2596
183 schoenebeck 3581 /** @brief Virtual machine standard measuring unit.
184 schoenebeck 3561 *
185     * Abstract base class representing standard measurement units throughout
186 schoenebeck 3581 * the script engine. These might be e.g. "dB" (deci Bel) for loudness,
187     * "Hz" (Hertz) for frequencies or "s" for "seconds". These unit types can
188     * combined with metric prefixes, for instance "kHz" (kilo Hertz),
189     * "us" (micro second), etc.
190 schoenebeck 3561 *
191     * Originally the script engine only supported abstract integer values for
192     * controlling any synthesis parameter or built-in function argument or
193     * variable. Under certain situations it makes sense though for an
194     * instrument script author to provide values in real, standard measurement
195 schoenebeck 3581 * units to provide a more natural and intuitive approach for writing
196     * instrument scripts, for example by setting the frequency of some LFO
197     * directly to "20Hz" or reducing loudness by "-4.2dB". Hence support for
198     * standard units in scripts was added as an extension to the NKSP script
199     * engine.
200     *
201     * So a unit consists of 1) a sequence of metric prefixes as scale factor
202     * (e.g. "k" for kilo) and 2) the actual unit type (e.g. "Hz" for Hertz).
203     * The unit type is a constant feature of number literals and variables, so
204     * once a variable was declared with a unit type (or no unit type at all)
205     * then that unit type of that variable cannot be changed for the entire
206     * life time of the script. This is different from the unit's metric
207     * prefix(es) of variables which may freely be changed at runtime.
208 schoenebeck 3561 */
209     class VMUnit {
210     public:
211     /**
212 schoenebeck 3581 * Returns the metric prefix(es) of this unit as unit factor. A metric
213     * prefix essentially is just a mathematical scale factor that should be
214     * applied to the number associated with the measurement unit. Consider
215     * a string literal in an NKSP script like '3kHz' where 'k' (kilo) is
216     * the metric prefix, which essentically is a scale factor of 1000.
217 schoenebeck 3561 *
218 schoenebeck 3581 * Usually a unit either has exactly none or one metric prefix, but note
219     * that there might also be units with more than one prefix, for example
220     * @c mdB (milli deci Bel) is used sometimes which has two prefixes. The
221     * latter is an exception though and more than two prefixes is currently
222     * not supported by the script engine.
223 schoenebeck 3561 *
224 schoenebeck 3581 * The factor returned by this method is the final mathematical factor
225     * that should be multiplied against the number associated with this
226     * unit. This factor results from the sequence of metric prefixes of
227     * this unit.
228     *
229     * @see MetricPrefix_t, hasUnitFactorNow(), hasUnitFactorEver(),
230     * VM_NO_FACTOR
231     * @returns current metric unit factor
232 schoenebeck 3561 */
233 schoenebeck 3581 virtual vmfloat unitFactor() const = 0;
234 schoenebeck 3561
235 schoenebeck 3581 //TODO: this still needs to be implemented in tree.h/.pp, built-in functions and as 2nd pass of parser appropriately
236     /*virtual*/ EverTriState_t hasUnitFactorEver() const { return VM_NEVER; }
237    
238 schoenebeck 3561 /**
239 schoenebeck 3581 * Whether this unit currently does have any metric unit prefix.
240 schoenebeck 3561 *
241 schoenebeck 3581 * This is actually just a convenience method which returns @c true if
242     * unitFactor() is not @c 1.0.
243     *
244     * @see MetricPrefix_t, unitFactor(), hasUnitFactorEver(), VM_NO_FACTOR
245     * @returns @c true if this unit currently has any metric prefix
246 schoenebeck 3561 */
247 schoenebeck 3581 bool hasUnitFactorNow() const;
248 schoenebeck 3561
249     /**
250     * This is the actual fundamental measuring unit base type of this unit,
251     * which might be either Hertz, second or Bel.
252     *
253 schoenebeck 3581 * Note that a number without a unit type may still have metric
254     * prefixes.
255     *
256     * @returns standard unit type identifier or VM_NO_UNIT if no unit type
257     * is used for this object
258 schoenebeck 3561 */
259     virtual StdUnit_t unitType() const = 0;
260    
261     /**
262     * Returns the actual mathematical factor represented by the passed
263     * @a prefix argument.
264     */
265     static vmfloat unitFactor(MetricPrefix_t prefix);
266    
267     /**
268     * Returns the actual mathematical factor represented by the passed
269     * two @a prefix1 and @a prefix2 arguments.
270 schoenebeck 3581 *
271     * @returns scale factor of given metric unit prefixes
272 schoenebeck 3561 */
273     static vmfloat unitFactor(MetricPrefix_t prefix1, MetricPrefix_t prefix2);
274 schoenebeck 3581
275     /**
276     * Returns the actual mathematical factor represented by the passed
277     * @a prefixes array. The passed array should always be terminated by a
278     * VM_NO_PREFIX value as last element.
279     *
280     * @param prefixes - sequence of metric prefixes
281     * @param size - max. amount of elements of array @a prefixes
282     * @returns scale factor of given metric unit prefixes
283     */
284     static vmfloat unitFactor(const MetricPrefix_t* prefixes, vmuint size = 2);
285 schoenebeck 3561 };
286    
287 schoenebeck 2727 /** @brief Virtual machine expression
288     *
289     * This is the abstract base class for all expressions of scripts.
290     * Deriving classes must implement the abstract method exprType().
291     *
292     * An expression within a script is translated into one instance of this
293     * class. It allows a high level access for the virtual machine to evaluate
294     * and handle expressions appropriately during execution. Expressions are
295     * for example all kinds of formulas, function calls, statements or a
296     * combination of them. Most of them evaluate to some kind of value, which
297     * might be further processed as part of encompassing expressions to outer
298     * expression results and so forth.
299     */
300 schoenebeck 2581 class VMExpr {
301     public:
302 schoenebeck 2727 /**
303     * Identifies the data type to which the result of this expression
304     * evaluates to. This abstract method must be implemented by deriving
305     * classes.
306     */
307 schoenebeck 2581 virtual ExprType_t exprType() const = 0;
308 schoenebeck 2727
309     /**
310     * In case this expression is an integer expression, then this method
311     * returns a casted pointer to that VMIntExpr object. It returns NULL
312     * if this expression is not an integer expression.
313     *
314     * @b Note: type casting performed by this method is strict! That means
315     * if this expression is i.e. actually a string expression like "12",
316     * calling asInt() will @b not cast that numerical string expression to
317     * an integer expression 12 for you, instead this method will simply
318 schoenebeck 3573 * return NULL! Same applies if this expression is actually a real
319     * number expression: asInt() would return NULL in that case as well.
320 schoenebeck 2727 *
321 schoenebeck 3582 * @see exprType(), asReal(), asNumber()
322 schoenebeck 2727 */
323 schoenebeck 2596 VMIntExpr* asInt() const;
324 schoenebeck 2727
325     /**
326 schoenebeck 3573 * In case this expression is a real number (floating point) expression,
327     * then this method returns a casted pointer to that VMRealExpr object.
328     * It returns NULL if this expression is not a real number expression.
329     *
330     * @b Note: type casting performed by this method is strict! That means
331     * if this expression is i.e. actually a string expression like "12",
332     * calling asReal() will @b not cast that numerical string expression to
333     * a real number expression 12.0 for you, instead this method will
334     * simply return NULL! Same applies if this expression is actually an
335     * integer expression: asReal() would return NULL in that case as well.
336     *
337 schoenebeck 3582 * @see exprType(), asInt(), asNumber()
338 schoenebeck 3573 */
339     VMRealExpr* asReal() const;
340    
341     /**
342     * In case this expression is a scalar number expression, that is either
343     * an integer (scalar) expression or a real number (floating point
344     * scalar) expression, then this method returns a casted pointer to that
345 schoenebeck 3582 * VMNumberExpr base class object. It returns NULL if this
346 schoenebeck 3573 * expression is neither an integer (scalar), nor a real number (scalar)
347     * expression.
348     *
349     * Since the methods asInt() and asReal() are very strict, this method
350     * is provided as convenience access in case only very general
351     * information (e.g. which standard measurement unit is being used or
352     * whether final operator being effective to this expression) is
353     * intended to be retrieved of this scalar number expression independent
354     * from whether this expression is actually an integer or a real number
355     * expression.
356     *
357     * @see exprType(), asInt(), asReal()
358     */
359 schoenebeck 3582 VMNumberExpr* asNumber() const;
360 schoenebeck 3573
361     /**
362 schoenebeck 2727 * In case this expression is a string expression, then this method
363     * returns a casted pointer to that VMStringExpr object. It returns NULL
364     * if this expression is not a string expression.
365     *
366     * @b Note: type casting performed by this method is strict! That means
367     * if this expression is i.e. actually an integer expression like 120,
368     * calling asString() will @b not cast that integer expression to a
369     * string expression "120" for you, instead this method will simply
370     * return NULL!
371     *
372     * @see exprType()
373     */
374 schoenebeck 2596 VMStringExpr* asString() const;
375 schoenebeck 2727
376     /**
377     * In case this expression is an integer array expression, then this
378     * method returns a casted pointer to that VMIntArrayExpr object. It
379     * returns NULL if this expression is not an integer array expression.
380     *
381     * @b Note: type casting performed by this method is strict! That means
382 schoenebeck 3573 * if this expression is i.e. an integer scalar expression, a real
383     * number expression or a string expression, calling asIntArray() will
384     * @b not cast those expressions to an integer array expression for you,
385     * instead this method will simply return NULL!
386 schoenebeck 2727 *
387 schoenebeck 3056 * @b Note: this method is currently, and in contrast to its other
388     * counter parts, declared as virtual method. Some deriving classes are
389     * currently using this to override this default implementation in order
390     * to implement an "evaluate now as integer array" behavior. This has
391     * efficiency reasons, however this also currently makes this part of
392     * the API less clean and should thus be addressed in future with
393     * appropriate changes to the API.
394     *
395 schoenebeck 2727 * @see exprType()
396     */
397 schoenebeck 3056 virtual VMIntArrayExpr* asIntArray() const;
398 schoenebeck 2945
399     /**
400 schoenebeck 3573 * In case this expression is a real number (floating point) array
401     * expression, then this method returns a casted pointer to that
402     * VMRealArrayExpr object. It returns NULL if this expression is not a
403     * real number array expression.
404     *
405     * @b Note: type casting performed by this method is strict! That means
406     * if this expression is i.e. a real number scalar expression, an
407     * integer expression or a string expression, calling asRealArray() will
408     * @b not cast those scalar expressions to a real number array
409     * expression for you, instead this method will simply return NULL!
410     *
411     * @b Note: this method is currently, and in contrast to its other
412     * counter parts, declared as virtual method. Some deriving classes are
413     * currently using this to override this default implementation in order
414     * to implement an "evaluate now as real number array" behavior. This
415     * has efficiency reasons, however this also currently makes this part
416     * of the API less clean and should thus be addressed in future with
417     * appropriate changes to the API.
418     *
419     * @see exprType()
420     */
421     virtual VMRealArrayExpr* asRealArray() const;
422    
423     /**
424 schoenebeck 3581 * This is an alternative to calling either asIntArray() or
425     * asRealArray(). This method here might be used if the fundamental
426     * scalar data type (real or integer) of the array is not relevant,
427     * i.e. for just getting the size of the array. Since all as*() methods
428     * here are very strict regarding type casting, this asArray() method
429     * sometimes can reduce code complexity.
430     *
431     * Likewise calling this method only returns a valid pointer if the
432     * expression is some array type (currently either integer array or real
433     * number array). For any other expression type this method will return
434     * NULL instead.
435     *
436     * @see exprType()
437     */
438     VMArrayExpr* asArray() const;
439    
440     /**
441 schoenebeck 2945 * Returns true in case this expression can be considered to be a
442     * constant expression. A constant expression will retain the same
443     * value throughout the entire life time of a script and the
444     * expression's constant value may be evaluated already at script
445     * parse time, which may result in performance benefits during script
446     * runtime.
447 schoenebeck 2960 *
448     * @b NOTE: A constant expression is per se always also non modifyable.
449     * But a non modifyable expression may not necessarily be a constant
450     * expression!
451     *
452     * @see isModifyable()
453 schoenebeck 2945 */
454     virtual bool isConstExpr() const = 0;
455    
456 schoenebeck 2960 /**
457     * Returns true in case this expression is allowed to be modified.
458     * If this method returns @c false then this expression must be handled
459     * as read-only expression, which means that assigning a new value to it
460     * is either not possible or not allowed.
461     *
462     * @b NOTE: A constant expression is per se always also non modifyable.
463     * But a non modifyable expression may not necessarily be a constant
464     * expression!
465     *
466     * @see isConstExpr()
467     */
468 schoenebeck 2945 bool isModifyable() const;
469 schoenebeck 2581 };
470    
471 schoenebeck 3573 /** @brief Virtual machine scalar number expression
472     *
473     * This is the abstract base class for integer (scalar) expressions and
474     * real number (floating point scalar) expressions of scripts.
475     */
476 schoenebeck 3582 class VMNumberExpr : virtual public VMExpr, virtual public VMUnit {
477 schoenebeck 3573 public:
478     /**
479     * Returns @c true if the value of this expression should be applied
480     * as final value to the respective destination synthesis chain
481     * parameter.
482     *
483     * This property is somewhat special and dedicated for the purpose of
484     * this expression's (integer or real number) value to be applied as
485     * parameter to the synthesis chain of the sampler (i.e. for altering a
486     * filter cutoff frequency). Now historically and by default all values
487     * of scripts are applied relatively to the sampler's synthesis chain,
488     * that is the synthesis parameter value of a script is multiplied
489     * against other sources for the same synthesis parameter (i.e. an LFO
490     * or a dedicated MIDI controller either hard wired in the engine or
491     * defined by the instrument patch). So by default the resulting actual
492     * final synthesis parameter is a combination of all these sources. This
493     * has the advantage that it creates a very living and dynamic overall
494     * sound.
495     *
496     * However sometimes there are requirements by script authors where this
497     * is not what you want. Therefore the NKSP script engine added a
498     * language extension by prefixing a value in scripts with a @c !
499     * character the value will be defined as being the "final" value of the
500     * destination synthesis parameter, so that causes this value to be
501     * applied exclusively, and the values of all other sources are thus
502     * entirely ignored by the sampler's synthesis core as long as this
503     * value is assigned by the script engine as "final" value for the
504     * requested synthesis parameter.
505     */
506     virtual bool isFinal() const = 0;
507 schoenebeck 3581
508     /**
509     * Calling this method evaluates the expression and returns the value
510     * of the expression as integer. If this scalar number expression is a
511     * real number expression then this method automatically casts the value
512     * from real number to integer.
513     */
514     vmint evalCastInt();
515    
516     /**
517     * Calling this method evaluates the expression and returns the value
518 schoenebeck 3584 * of the expression as integer and thus behaves similar to the previous
519     * method, however this overridden method automatically takes unit
520     * prefixes into account and returns a converted value corresponding to
521     * the given unit @a prefix expected by the caller.
522     *
523     * Example: Assume this expression was an integer expression '12kHz'
524     * then calling this method as @c evalCastInt(VM_MILLI) would return
525     * the value @c 12000000.
526     *
527     * @param prefix - measuring unit prefix expected for result by caller
528     */
529     vmint evalCastInt(MetricPrefix_t prefix);
530    
531     /**
532     * This method behaves like the previous method, just that it takes a
533     * measuring unit prefix with two elements (e.g. "milli cents" for
534     * tuning).
535     *
536     * @param prefix1 - 1st measuring unit prefix element expected by caller
537     * @param prefix2 - 2nd measuring unit prefix element expected by caller
538     */
539     vmint evalCastInt(MetricPrefix_t prefix1, MetricPrefix_t prefix2);
540    
541     /**
542     * Calling this method evaluates the expression and returns the value
543 schoenebeck 3581 * of the expression as real number. If this scalar number expression is
544     * an integer expression then this method automatically casts the value
545     * from integer to real number.
546     */
547     vmfloat evalCastReal();
548 schoenebeck 3584
549     /**
550     * Calling this method evaluates the expression and returns the value
551     * of the expression as real number and thus behaves similar to the
552     * previous method, however this overridden method automatically takes
553     * unit prefixes into account and returns a converted value
554     * corresponding to the given unit @a prefix expected by the caller.
555     *
556     * Example: Assume this expression was an integer expression '8ms' then
557     * calling this method as @c evalCastReal(VM_NO_PREFIX) would return the
558     * value @c 0.008.
559     *
560     * @param prefix - measuring unit prefix expected for result by caller
561     */
562     vmfloat evalCastReal(MetricPrefix_t prefix);
563    
564     /**
565     * This method behaves like the previous method, just that it takes a
566     * measuring unit prefix with two elements (e.g. "milli cents" for
567     * tuning).
568     *
569     * @param prefix1 - 1st measuring unit prefix element expected by caller
570     * @param prefix2 - 2nd measuring unit prefix element expected by caller
571     */
572     vmfloat evalCastReal(MetricPrefix_t prefix1, MetricPrefix_t prefix2);
573 schoenebeck 3573 };
574    
575 schoenebeck 2727 /** @brief Virtual machine integer expression
576     *
577     * This is the abstract base class for all expressions inside scripts which
578     * evaluate to an integer (scalar) value. Deriving classes implement the
579     * abstract method evalInt() to return the actual integer result value of
580     * the expression.
581     */
582 schoenebeck 3582 class VMIntExpr : virtual public VMNumberExpr {
583 schoenebeck 2581 public:
584 schoenebeck 2727 /**
585     * Returns the result of this expression as integer (scalar) value.
586     * This abstract method must be implemented by deriving classes.
587     */
588 schoenebeck 3557 virtual vmint evalInt() = 0;
589 schoenebeck 2727
590     /**
591 schoenebeck 3561 * Returns the result of this expression as integer (scalar) value and
592     * thus behaves similar to the previous method, however this overridden
593     * method automatically takes unit prefixes into account and returns a
594     * value corresponding to the expected given unit @a prefix.
595     *
596     * @param prefix - default measurement unit prefix expected by caller
597     */
598     vmint evalInt(MetricPrefix_t prefix);
599    
600     /**
601     * This method behaves like the previous method, just that it takes
602     * a default measurement prefix with two elements (i.e. "milli cents"
603     * for tuning).
604     */
605     vmint evalInt(MetricPrefix_t prefix1, MetricPrefix_t prefix2);
606    
607     /**
608 schoenebeck 2727 * Returns always INT_EXPR for instances of this class.
609     */
610     ExprType_t exprType() const OVERRIDE { return INT_EXPR; }
611 schoenebeck 3573 };
612 schoenebeck 3561
613 schoenebeck 3573 /** @brief Virtual machine real number (floating point scalar) expression
614     *
615     * This is the abstract base class for all expressions inside scripts which
616     * evaluate to a real number (floating point scalar) value. Deriving classes
617     * implement the abstract method evalReal() to return the actual floating
618     * point result value of the expression.
619     */
620 schoenebeck 3582 class VMRealExpr : virtual public VMNumberExpr {
621 schoenebeck 3573 public:
622 schoenebeck 3561 /**
623 schoenebeck 3573 * Returns the result of this expression as real number (floating point
624     * scalar) value. This abstract method must be implemented by deriving
625     * classes.
626     */
627     virtual vmfloat evalReal() = 0;
628    
629     /**
630     * Returns the result of this expression as real number (floating point
631     * scalar) value and thus behaves similar to the previous method,
632     * however this overridden method automatically takes unit prefixes into
633     * account and returns a value corresponding to the expected given unit
634     * @a prefix.
635 schoenebeck 3561 *
636 schoenebeck 3573 * @param prefix - default measurement unit prefix expected by caller
637 schoenebeck 3561 */
638 schoenebeck 3573 vmfloat evalReal(MetricPrefix_t prefix);
639    
640     /**
641     * This method behaves like the previous method, just that it takes
642     * a default measurement prefix with two elements (i.e. "milli cents"
643     * for tuning).
644     */
645     vmfloat evalReal(MetricPrefix_t prefix1, MetricPrefix_t prefix2);
646    
647     /**
648     * Returns always REAL_EXPR for instances of this class.
649     */
650     ExprType_t exprType() const OVERRIDE { return REAL_EXPR; }
651 schoenebeck 2581 };
652    
653 schoenebeck 2727 /** @brief Virtual machine string expression
654     *
655     * This is the abstract base class for all expressions inside scripts which
656     * evaluate to a string value. Deriving classes implement the abstract
657     * method evalStr() to return the actual string result value of the
658     * expression.
659     */
660 schoenebeck 2581 class VMStringExpr : virtual public VMExpr {
661     public:
662 schoenebeck 2727 /**
663     * Returns the result of this expression as string value. This abstract
664     * method must be implemented by deriving classes.
665     */
666 schoenebeck 2581 virtual String evalStr() = 0;
667 schoenebeck 2727
668     /**
669     * Returns always STRING_EXPR for instances of this class.
670     */
671     ExprType_t exprType() const OVERRIDE { return STRING_EXPR; }
672 schoenebeck 2581 };
673    
674 schoenebeck 2727 /** @brief Virtual Machine Array Value Expression
675     *
676     * This is the abstract base class for all expressions inside scripts which
677     * evaluate to some kind of array value. Deriving classes implement the
678     * abstract method arraySize() to return the amount of elements within the
679     * array.
680     */
681 schoenebeck 2619 class VMArrayExpr : virtual public VMExpr {
682     public:
683 schoenebeck 2727 /**
684     * Returns amount of elements in this array. This abstract method must
685     * be implemented by deriving classes.
686     */
687 schoenebeck 3557 virtual vmint arraySize() const = 0;
688 schoenebeck 2619 };
689    
690 schoenebeck 3581 /** @brief Virtual Machine Number Array Expression
691     *
692     * This is the abstract base class for all expressions which either evaluate
693     * to an integer array or real number array.
694     */
695     class VMNumberArrayExpr : virtual public VMArrayExpr {
696     public:
697     /**
698     * Returns the metric unit factor of the requested array element.
699     *
700     * @param i - array element index (must be between 0 .. arraySize() - 1)
701     * @see VMUnit::unitFactor() for details about metric unit factors
702     */
703     virtual vmfloat unitFactorOfElement(vmuint i) const = 0;
704    
705     /**
706     * Changes the current unit factor of the array element given by element
707     * index @a i.
708     *
709     * @param i - array element index (must be between 0 .. arraySize() - 1)
710     * @param factor - new unit factor to be assigned
711     * @see VMUnit::unitFactor() for details about metric unit factors
712     */
713     virtual void assignElementUnitFactor(vmuint i, vmfloat factor) = 0;
714     };
715    
716 schoenebeck 2727 /** @brief Virtual Machine Integer Array Expression
717     *
718     * This is the abstract base class for all expressions inside scripts which
719     * evaluate to an array of integer values. Deriving classes implement the
720     * abstract methods arraySize(), evalIntElement() and assignIntElement() to
721     * access the individual integer array values.
722     */
723 schoenebeck 3581 class VMIntArrayExpr : virtual public VMNumberArrayExpr {
724 schoenebeck 2619 public:
725 schoenebeck 2727 /**
726     * Returns the (scalar) integer value of the array element given by
727     * element index @a i.
728     *
729     * @param i - array element index (must be between 0 .. arraySize() - 1)
730     */
731 schoenebeck 3557 virtual vmint evalIntElement(vmuint i) = 0;
732 schoenebeck 2727
733     /**
734     * Changes the current value of an element (given by array element
735     * index @a i) of this integer array.
736     *
737     * @param i - array element index (must be between 0 .. arraySize() - 1)
738     * @param value - new integer scalar value to be assigned to that array element
739     */
740 schoenebeck 3557 virtual void assignIntElement(vmuint i, vmint value) = 0;
741 schoenebeck 2727
742     /**
743     * Returns always INT_ARR_EXPR for instances of this class.
744     */
745     ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; }
746 schoenebeck 2619 };
747    
748 schoenebeck 3573 /** @brief Virtual Machine Real Number Array Expression
749     *
750     * This is the abstract base class for all expressions inside scripts which
751     * evaluate to an array of real numbers (floating point values). Deriving
752     * classes implement the abstract methods arraySize(), evalRealElement() and
753     * assignRealElement() to access the array's individual real numbers.
754     */
755 schoenebeck 3581 class VMRealArrayExpr : virtual public VMNumberArrayExpr {
756 schoenebeck 3573 public:
757     /**
758     * Returns the (scalar) real mumber (floating point value) of the array
759     * element given by element index @a i.
760     *
761     * @param i - array element index (must be between 0 .. arraySize() - 1)
762     */
763     virtual vmfloat evalRealElement(vmuint i) = 0;
764    
765     /**
766     * Changes the current value of an element (given by array element
767     * index @a i) of this real number array.
768     *
769     * @param i - array element index (must be between 0 .. arraySize() - 1)
770     * @param value - new real number value to be assigned to that array element
771     */
772     virtual void assignRealElement(vmuint i, vmfloat value) = 0;
773    
774     /**
775     * Returns always REAL_ARR_EXPR for instances of this class.
776     */
777     ExprType_t exprType() const OVERRIDE { return REAL_ARR_EXPR; }
778     };
779    
780 schoenebeck 2727 /** @brief Arguments (parameters) for being passed to a built-in script function.
781     *
782     * An argument or a set of arguments passed to a script function are
783     * translated by the parser to an instance of this class. This abstract
784     * interface class is used by implementations of built-in functions to
785     * obtain the individual function argument values being passed to them at
786     * runtime.
787     */
788 schoenebeck 2581 class VMFnArgs {
789     public:
790 schoenebeck 2727 /**
791     * Returns the amount of arguments going to be passed to the script
792     * function.
793     */
794 schoenebeck 3557 virtual vmint argsCount() const = 0;
795 schoenebeck 2727
796     /**
797     * Returns the respective argument (requested by argument index @a i) of
798     * this set of arguments. This method is called by implementations of
799     * built-in script functions to obtain the value of each function
800     * argument passed to the function at runtime.
801     *
802     * @param i - function argument index (indexed from left to right)
803     */
804 schoenebeck 3557 virtual VMExpr* arg(vmint i) = 0;
805 schoenebeck 2581 };
806    
807 schoenebeck 2727 /** @brief Result value returned from a call to a built-in script function.
808     *
809     * Implementations of built-in script functions return an instance of this
810     * object to let the virtual machine obtain the result value of the function
811     * call, which might then be further processed by the virtual machine
812     * according to the script. It also provides informations about the success
813     * or failure of the function call.
814     */
815 schoenebeck 2581 class VMFnResult {
816     public:
817 schoenebeck 2727 /**
818     * Returns the result value of the function call, represented by a high
819     * level expression object.
820     */
821 schoenebeck 2581 virtual VMExpr* resultValue() = 0;
822 schoenebeck 2727
823     /**
824     * Provides detailed informations of the success / failure of the
825     * function call. The virtual machine is evaluating the flags returned
826     * here to decide whether it must abort or suspend execution of the
827     * script at this point.
828     */
829 schoenebeck 2581 virtual StmtFlags_t resultFlags() { return STMT_SUCCESS; }
830     };
831    
832 schoenebeck 2727 /** @brief Virtual machine built-in function.
833 schoenebeck 2612 *
834     * Abstract base class for built-in script functions, defining the interface
835 schoenebeck 2727 * for all built-in script function implementations. All built-in script
836     * functions are deriving from this abstract interface class in order to
837     * provide their functionality to the virtual machine with this unified
838     * interface.
839     *
840     * The methods of this interface class provide two purposes:
841     *
842     * 1. When a script is loaded, the script parser uses the methods of this
843     * interface to check whether the script author was calling the
844     * respective built-in script function in a correct way. For example
845     * the parser checks whether the required amount of parameters were
846     * passed to the function and whether the data types passed match the
847     * data types expected by the function. If not, loading the script will
848     * be aborted with a parser error, describing to the user (i.e. script
849     * author) the precise misusage of the respective function.
850     * 2. After the script was loaded successfully and the script is executed,
851     * the virtual machine calls the exec() method of the respective built-in
852     * function to provide the actual functionality of the built-in function
853     * call.
854 schoenebeck 2612 */
855 schoenebeck 2581 class VMFunction {
856     public:
857 schoenebeck 2612 /**
858     * Script data type of the function's return value. If the function does
859 schoenebeck 2727 * not return any value (void), then it returns EMPTY_EXPR here.
860 schoenebeck 3577 *
861     * Some functions may have a different return type depending on the
862     * arguments to be passed to this function. That's what the @a args
863     * parameter is for, so that the method implementation can look ahead
864     * of what kind of parameters are going to be passed to the built-in
865     * function later on in order to decide which return value type would
866     * be used and returned by the function accordingly in that case.
867     *
868     * @param args - function arguments going to be passed for executing
869     * this built-in function later on
870 schoenebeck 2612 */
871 schoenebeck 3577 virtual ExprType_t returnType(VMFnArgs* args) = 0;
872 schoenebeck 2612
873     /**
874 schoenebeck 3581 * Standard measuring unit type of the function's result value
875     * (e.g. second, Hertz).
876     *
877     * Some functions may have a different standard measuring unit type for
878     * their return value depending on the arguments to be passed to this
879     * function. That's what the @a args parameter is for, so that the
880     * method implementation can look ahead of what kind of parameters are
881     * going to be passed to the built-in function later on in order to
882     * decide which return value type would be used and returned by the
883     * function accordingly in that case.
884     *
885     * @param args - function arguments going to be passed for executing
886     * this built-in function later on
887     * @see Unit for details about standard measuring units
888     */
889     virtual StdUnit_t returnUnitType(VMFnArgs* args) = 0;
890    
891     /**
892     * Whether the result value returned by this built-in function is
893     * considered to be a 'final' value.
894     *
895     * Some functions may have a different 'final' feature for their return
896     * value depending on the arguments to be passed to this function.
897     * That's what the @a args parameter is for, so that the method
898     * implementation can look ahead of what kind of parameters are going to
899     * be passed to the built-in function later on in order to decide which
900     * return value type would be used and returned by the function
901     * accordingly in that case.
902     *
903     * @param args - function arguments going to be passed for executing
904     * this built-in function later on
905 schoenebeck 3582 * @see VMNumberExpr::isFinal() for details about 'final' values
906 schoenebeck 3581 */
907     virtual bool returnsFinal(VMFnArgs* args) = 0;
908    
909     /**
910 schoenebeck 2612 * Minimum amount of function arguments this function accepts. If a
911     * script is calling this function with less arguments, the script
912     * parser will throw a parser error.
913     */
914 schoenebeck 3557 virtual vmint minRequiredArgs() const = 0;
915 schoenebeck 2612
916     /**
917     * Maximum amount of function arguments this functions accepts. If a
918     * script is calling this function with more arguments, the script
919     * parser will throw a parser error.
920     */
921 schoenebeck 3557 virtual vmint maxAllowedArgs() const = 0;
922 schoenebeck 2612
923     /**
924     * Script data type of the function's @c iArg 'th function argument.
925     * The information provided here is less strong than acceptsArgType().
926     * The parser will compare argument data types provided in scripts by
927 schoenebeck 2871 * calling acceptsArgType(). The return value of argType() is used by the
928 schoenebeck 2612 * parser instead to show an appropriate parser error which data type
929     * this function usually expects as "default" data type. Reason: a
930     * function may accept multiple data types for a certain function
931     * argument and would automatically cast the passed argument value in
932     * that case to the type it actually needs.
933     *
934     * @param iArg - index of the function argument in question
935 schoenebeck 2727 * (must be between 0 .. maxAllowedArgs() - 1)
936 schoenebeck 2612 */
937 schoenebeck 3557 virtual ExprType_t argType(vmint iArg) const = 0;
938 schoenebeck 2612
939     /**
940 schoenebeck 2945 * This method is called by the parser to check whether arguments
941 schoenebeck 2612 * passed in scripts to this function are accepted by this function. If
942     * a script calls this function with an argument's data type not
943 schoenebeck 2727 * accepted by this function, the parser will throw a parser error. On
944     * such errors the data type returned by argType() will be used to
945     * assemble an appropriate error message regarding the precise misusage
946     * of the built-in function.
947 schoenebeck 2612 *
948     * @param iArg - index of the function argument in question
949 schoenebeck 2727 * (must be between 0 .. maxAllowedArgs() - 1)
950 schoenebeck 2612 * @param type - script data type used for this function argument by
951     * currently parsed script
952 schoenebeck 2727 * @return true if the given data type would be accepted for the
953     * respective function argument by the function
954 schoenebeck 2612 */
955 schoenebeck 3557 virtual bool acceptsArgType(vmint iArg, ExprType_t type) const = 0;
956 schoenebeck 2612
957     /**
958 schoenebeck 3561 * This method is called by the parser to check whether arguments
959     * passed in scripts to this function are accepted by this function. If
960     * a script calls this function with an argument's measuremnt unit type
961     * not accepted by this function, the parser will throw a parser error.
962     *
963     * This default implementation of this method does not accept any
964     * measurement unit. Deriving subclasses would override this method
965     * implementation in case they do accept any measurement unit for its
966     * function arguments.
967     *
968     * @param iArg - index of the function argument in question
969     * (must be between 0 .. maxAllowedArgs() - 1)
970     * @param type - standard measurement unit data type used for this
971     * function argument by currently parsed script
972     * @return true if the given standard measurement unit type would be
973     * accepted for the respective function argument by the function
974     */
975     virtual bool acceptsArgUnitType(vmint iArg, StdUnit_t type) const;
976    
977     /**
978     * This method is called by the parser to check whether arguments
979     * passed in scripts to this function are accepted by this function. If
980     * a script calls this function with a metric unit prefix and metric
981     * prefixes are not accepted for that argument by this function, then
982     * the parser will throw a parser error.
983     *
984     * This default implementation of this method does not accept any
985     * metric prefix. Deriving subclasses would override this method
986     * implementation in case they do accept any metric prefix for its
987     * function arguments.
988     *
989     * @param iArg - index of the function argument in question
990     * (must be between 0 .. maxAllowedArgs() - 1)
991 schoenebeck 3564 * @param type - standard measurement unit data type used for that
992     * function argument by currently parsed script
993 schoenebeck 3561 *
994     * @return true if a metric prefix would be accepted for the respective
995     * function argument by this function
996     *
997     * @see MetricPrefix_t
998     */
999 schoenebeck 3564 virtual bool acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const;
1000 schoenebeck 3561
1001     /**
1002     * This method is called by the parser to check whether arguments
1003     * passed in scripts to this function are accepted by this function. If
1004     * a script calls this function with an argument that is declared to be
1005     * a "final" value and this is not accepted by this function, the parser
1006     * will throw a parser error.
1007     *
1008     * This default implementation of this method does not accept a "final"
1009     * value. Deriving subclasses would override this method implementation
1010     * in case they do accept a "final" value for its function arguments.
1011     *
1012     * @param iArg - index of the function argument in question
1013     * (must be between 0 .. maxAllowedArgs() - 1)
1014     * @return true if a "final" value would be accepted for the respective
1015     * function argument by the function
1016     *
1017 schoenebeck 3582 * @see VMNumberExpr::isFinal(), returnsFinal()
1018 schoenebeck 3561 */
1019     virtual bool acceptsArgFinal(vmint iArg) const;
1020    
1021     /**
1022 schoenebeck 2945 * This method is called by the parser to check whether some arguments
1023     * (and if yes which ones) passed to this script function will be
1024     * modified by this script function. Most script functions simply use
1025     * their arguments as inputs, that is they only read the argument's
1026     * values. However some script function may also use passed
1027     * argument(s) as output variables. In this case the function
1028     * implementation must return @c true for the respective argument
1029     * index here.
1030     *
1031     * @param iArg - index of the function argument in question
1032     * (must be between 0 .. maxAllowedArgs() - 1)
1033     */
1034 schoenebeck 3557 virtual bool modifiesArg(vmint iArg) const = 0;
1035 schoenebeck 2945
1036     /**
1037 schoenebeck 3581 * This method is called by the parser to let the built-in function
1038     * perform its own, individual parse time checks on the arguments to be
1039     * passed to the built-in function. So this method is the place for
1040     * implementing custom checks which are very specific to the individual
1041     * built-in function's purpose and its individual requirements.
1042     *
1043     * For instance the built-in 'in_range()' function uses this method to
1044     * check whether the last 2 of their 3 arguments are of same data type
1045     * and if not it triggers a parser error. 'in_range()' also checks
1046     * whether all of its 3 arguments do have the same standard measuring
1047     * unit type and likewise raises a parser error if not.
1048     *
1049     * For less critical issues built-in functions may also raise parser
1050     * warnings instead.
1051     *
1052     * It is recommended that classes implementing (that is overriding) this
1053     * method should always call their super class's implementation of this
1054     * method to ensure their potential parse time checks are always
1055     * performed as well.
1056     *
1057     * @param args - function arguments going to be passed for executing
1058     * this built-in function later on
1059     * @param err - the parser's error handler to be called by this method
1060     * implementation to trigger a parser error with the
1061     * respective error message text
1062     * @param wrn - the parser's warning handler to be called by this method
1063     * implementation to trigger a parser warning with the
1064     * respective warning message text
1065     */
1066     virtual void checkArgs(VMFnArgs* args,
1067     std::function<void(String)> err,
1068     std::function<void(String)> wrn);
1069    
1070     /**
1071 schoenebeck 2727 * Implements the actual function execution. This exec() method is
1072     * called by the VM whenever this function implementation shall be
1073     * executed at script runtime. This method blocks until the function
1074     * call completed.
1075 schoenebeck 2612 *
1076     * @param args - function arguments for executing this built-in function
1077 schoenebeck 2727 * @returns function's return value (if any) and general status
1078     * informations (i.e. whether the function call caused a
1079     * runtime error)
1080 schoenebeck 2612 */
1081 schoenebeck 2581 virtual VMFnResult* exec(VMFnArgs* args) = 0;
1082 schoenebeck 2612
1083     /**
1084 schoenebeck 2727 * Convenience method for function implementations to show warning
1085     * messages during actual execution of the built-in function.
1086 schoenebeck 2612 *
1087 schoenebeck 2727 * @param txt - runtime warning text to be shown to user
1088 schoenebeck 2612 */
1089 schoenebeck 2598 void wrnMsg(const String& txt);
1090 schoenebeck 2612
1091     /**
1092 schoenebeck 2727 * Convenience method for function implementations to show error
1093     * messages during actual execution of the built-in function.
1094 schoenebeck 2612 *
1095 schoenebeck 2727 * @param txt - runtime error text to be shown to user
1096 schoenebeck 2612 */
1097 schoenebeck 2598 void errMsg(const String& txt);
1098 schoenebeck 2581 };
1099    
1100 schoenebeck 2727 /** @brief Virtual machine relative pointer.
1101     *
1102 schoenebeck 3557 * POD base of VMInt64RelPtr, VMInt32RelPtr and VMInt8RelPtr structures. Not
1103     * intended to be used directly. Use VMInt64RelPtr, VMInt32RelPtr,
1104     * VMInt8RelPtr instead.
1105 schoenebeck 2727 *
1106 schoenebeck 3557 * @see VMInt64RelPtr, VMInt32RelPtr, VMInt8RelPtr
1107 schoenebeck 2594 */
1108     struct VMRelPtr {
1109     void** base; ///< Base pointer.
1110 schoenebeck 3557 vmint offset; ///< Offset (in bytes) relative to base pointer.
1111 schoenebeck 2948 bool readonly; ///< Whether the pointed data may be modified or just be read.
1112 schoenebeck 2594 };
1113    
1114 schoenebeck 3557 /** @brief Pointer to built-in VM integer variable (interface class).
1115 schoenebeck 2594 *
1116 schoenebeck 3557 * This class acts as an abstract interface to all built-in integer script
1117     * variables, independent of their actual native size (i.e. some built-in
1118     * script variables are internally using a native int size of 64 bit or 32
1119     * bit or 8 bit). The virtual machine is using this interface class instead
1120     * of its implementing descendants (VMInt64RelPtr, VMInt32RelPtr,
1121     * VMInt8RelPtr) in order for the virtual machine for not being required to
1122     * handle each of them differently.
1123     */
1124     struct VMIntPtr {
1125     virtual vmint evalInt() = 0;
1126     virtual void assign(vmint i) = 0;
1127     virtual bool isAssignable() const = 0;
1128     };
1129    
1130     /** @brief Pointer to built-in VM integer variable (of C/C++ type int64_t).
1131     *
1132     * Used for defining built-in 64 bit integer script variables.
1133     *
1134     * @b CAUTION: You may only use this class for pointing to C/C++ variables
1135     * of type "int64_t" (thus being exactly 64 bit in size). If the C/C++ int
1136     * variable you want to reference is only 32 bit in size then you @b must
1137     * use VMInt32RelPtr instead! Respectively for a referenced native variable
1138     * with only 8 bit in size you @b must use VMInt8RelPtr instead!
1139     *
1140     * For efficiency reasons the actual native C/C++ int variable is referenced
1141     * by two components here. The actual native int C/C++ variable in memory
1142     * is dereferenced at VM run-time by taking the @c base pointer dereference
1143     * and adding @c offset bytes. This has the advantage that for a large
1144     * number of built-in int variables, only one (or few) base pointer need
1145     * to be re-assigned before running a script, instead of updating each
1146     * built-in variable each time before a script is executed.
1147     *
1148     * Refer to DECLARE_VMINT() for example code.
1149     *
1150     * @see VMInt32RelPtr, VMInt8RelPtr, DECLARE_VMINT()
1151     */
1152     struct VMInt64RelPtr : VMRelPtr, VMIntPtr {
1153     VMInt64RelPtr() {
1154     base = NULL;
1155     offset = 0;
1156     readonly = false;
1157     }
1158     VMInt64RelPtr(const VMRelPtr& data) {
1159     base = data.base;
1160     offset = data.offset;
1161     readonly = false;
1162     }
1163     vmint evalInt() OVERRIDE {
1164     return (vmint)*(int64_t*)&(*(uint8_t**)base)[offset];
1165     }
1166     void assign(vmint i) OVERRIDE {
1167     *(int64_t*)&(*(uint8_t**)base)[offset] = (int64_t)i;
1168     }
1169     bool isAssignable() const OVERRIDE { return !readonly; }
1170     };
1171    
1172     /** @brief Pointer to built-in VM integer variable (of C/C++ type int32_t).
1173     *
1174 schoenebeck 2727 * Used for defining built-in 32 bit integer script variables.
1175 schoenebeck 2594 *
1176     * @b CAUTION: You may only use this class for pointing to C/C++ variables
1177 schoenebeck 3557 * of type "int32_t" (thus being exactly 32 bit in size). If the C/C++ int
1178     * variable you want to reference is 64 bit in size then you @b must use
1179     * VMInt64RelPtr instead! Respectively for a referenced native variable with
1180     * only 8 bit in size you @b must use VMInt8RelPtr instead!
1181 schoenebeck 2594 *
1182     * For efficiency reasons the actual native C/C++ int variable is referenced
1183     * by two components here. The actual native int C/C++ variable in memory
1184     * is dereferenced at VM run-time by taking the @c base pointer dereference
1185     * and adding @c offset bytes. This has the advantage that for a large
1186     * number of built-in int variables, only one (or few) base pointer need
1187     * to be re-assigned before running a script, instead of updating each
1188     * built-in variable each time before a script is executed.
1189     *
1190     * Refer to DECLARE_VMINT() for example code.
1191     *
1192 schoenebeck 3557 * @see VMInt64RelPtr, VMInt8RelPtr, DECLARE_VMINT()
1193 schoenebeck 2594 */
1194 schoenebeck 3557 struct VMInt32RelPtr : VMRelPtr, VMIntPtr {
1195     VMInt32RelPtr() {
1196 schoenebeck 2594 base = NULL;
1197     offset = 0;
1198 schoenebeck 2948 readonly = false;
1199 schoenebeck 2594 }
1200 schoenebeck 3557 VMInt32RelPtr(const VMRelPtr& data) {
1201 schoenebeck 2594 base = data.base;
1202     offset = data.offset;
1203 schoenebeck 2948 readonly = false;
1204 schoenebeck 2594 }
1205 schoenebeck 3557 vmint evalInt() OVERRIDE {
1206     return (vmint)*(int32_t*)&(*(uint8_t**)base)[offset];
1207     }
1208     void assign(vmint i) OVERRIDE {
1209     *(int32_t*)&(*(uint8_t**)base)[offset] = (int32_t)i;
1210     }
1211     bool isAssignable() const OVERRIDE { return !readonly; }
1212 schoenebeck 2594 };
1213    
1214     /** @brief Pointer to built-in VM integer variable (of C/C++ type int8_t).
1215     *
1216 schoenebeck 2727 * Used for defining built-in 8 bit integer script variables.
1217 schoenebeck 2594 *
1218     * @b CAUTION: You may only use this class for pointing to C/C++ variables
1219     * of type "int8_t" (8 bit integer). If the C/C++ int variable you want to
1220 schoenebeck 3557 * reference is not exactly 8 bit in size then you @b must respectively use
1221     * either VMInt32RelPtr for native 32 bit variables or VMInt64RelPtrl for
1222     * native 64 bit variables instead!
1223 schoenebeck 2594 *
1224     * For efficiency reasons the actual native C/C++ int variable is referenced
1225     * by two components here. The actual native int C/C++ variable in memory
1226     * is dereferenced at VM run-time by taking the @c base pointer dereference
1227     * and adding @c offset bytes. This has the advantage that for a large
1228     * number of built-in int variables, only one (or few) base pointer need
1229     * to be re-assigned before running a script, instead of updating each
1230     * built-in variable each time before a script is executed.
1231     *
1232     * Refer to DECLARE_VMINT() for example code.
1233     *
1234 schoenebeck 3557 * @see VMIntRel32Ptr, VMIntRel64Ptr, DECLARE_VMINT()
1235 schoenebeck 2594 */
1236 schoenebeck 3557 struct VMInt8RelPtr : VMRelPtr, VMIntPtr {
1237     VMInt8RelPtr() {
1238     base = NULL;
1239     offset = 0;
1240     readonly = false;
1241 schoenebeck 2594 }
1242 schoenebeck 3557 VMInt8RelPtr(const VMRelPtr& data) {
1243     base = data.base;
1244     offset = data.offset;
1245     readonly = false;
1246 schoenebeck 2594 }
1247 schoenebeck 3557 vmint evalInt() OVERRIDE {
1248     return (vmint)*(uint8_t*)&(*(uint8_t**)base)[offset];
1249     }
1250     void assign(vmint i) OVERRIDE {
1251     *(uint8_t*)&(*(uint8_t**)base)[offset] = (uint8_t)i;
1252     }
1253     bool isAssignable() const OVERRIDE { return !readonly; }
1254 schoenebeck 2594 };
1255    
1256 schoenebeck 3557 /** @brief Pointer to built-in VM integer variable (of C/C++ type vmint).
1257     *
1258     * Use this typedef if the native variable to be pointed to is using the
1259     * typedef vmint. If the native C/C++ variable to be pointed to is using
1260     * another C/C++ type then better use one of VMInt64RelPtr or VMInt32RelPtr
1261     * instead.
1262     */
1263     typedef VMInt64RelPtr VMIntRelPtr;
1264    
1265 schoenebeck 3035 #if HAVE_CXX_EMBEDDED_PRAGMA_DIAGNOSTICS
1266     # define COMPILER_DISABLE_OFFSETOF_WARNING \
1267     _Pragma("GCC diagnostic push") \
1268     _Pragma("GCC diagnostic ignored \"-Winvalid-offsetof\"")
1269     # define COMPILER_RESTORE_OFFSETOF_WARNING \
1270     _Pragma("GCC diagnostic pop")
1271     #else
1272     # define COMPILER_DISABLE_OFFSETOF_WARNING
1273     # define COMPILER_RESTORE_OFFSETOF_WARNING
1274     #endif
1275    
1276 schoenebeck 2594 /**
1277 schoenebeck 3557 * Convenience macro for initializing VMInt64RelPtr, VMInt32RelPtr and
1278     * VMInt8RelPtr structures. Usage example:
1279 schoenebeck 2594 * @code
1280     * struct Foo {
1281 schoenebeck 2727 * uint8_t a; // native representation of a built-in integer script variable
1282 schoenebeck 3557 * int64_t b; // native representation of another built-in integer script variable
1283     * int64_t c; // native representation of another built-in integer script variable
1284 schoenebeck 2727 * uint8_t d; // native representation of another built-in integer script variable
1285 schoenebeck 2594 * };
1286     *
1287 schoenebeck 2727 * // initializing the built-in script variables to some values
1288     * Foo foo1 = (Foo) { 1, 2000, 3000, 4 };
1289     * Foo foo2 = (Foo) { 5, 6000, 7000, 8 };
1290 schoenebeck 2594 *
1291     * Foo* pFoo;
1292     *
1293 schoenebeck 2727 * VMInt8RelPtr varA = DECLARE_VMINT(pFoo, class Foo, a);
1294 schoenebeck 3557 * VMInt64RelPtr varB = DECLARE_VMINT(pFoo, class Foo, b);
1295     * VMInt64RelPtr varC = DECLARE_VMINT(pFoo, class Foo, c);
1296 schoenebeck 2727 * VMInt8RelPtr varD = DECLARE_VMINT(pFoo, class Foo, d);
1297 schoenebeck 2594 *
1298     * pFoo = &foo1;
1299 schoenebeck 2727 * printf("%d\n", varA->evalInt()); // will print 1
1300     * printf("%d\n", varB->evalInt()); // will print 2000
1301     * printf("%d\n", varC->evalInt()); // will print 3000
1302     * printf("%d\n", varD->evalInt()); // will print 4
1303 schoenebeck 2594 *
1304 schoenebeck 2727 * // same printf() code, just with pFoo pointer being changed ...
1305     *
1306 schoenebeck 2594 * pFoo = &foo2;
1307 schoenebeck 2727 * printf("%d\n", varA->evalInt()); // will print 5
1308     * printf("%d\n", varB->evalInt()); // will print 6000
1309     * printf("%d\n", varC->evalInt()); // will print 7000
1310     * printf("%d\n", varD->evalInt()); // will print 8
1311 schoenebeck 2594 * @endcode
1312 schoenebeck 2727 * As you can see above, by simply changing one single pointer, you can
1313     * remap a huge bunch of built-in integer script variables to completely
1314     * different native values/native variables. Which especially reduces code
1315     * complexity inside the sampler engines which provide the actual script
1316     * functionalities.
1317 schoenebeck 2594 */
1318 schoenebeck 3034 #define DECLARE_VMINT(basePtr, T_struct, T_member) ( \
1319     /* Disable offsetof warning, trust us, we are cautios. */ \
1320 schoenebeck 3035 COMPILER_DISABLE_OFFSETOF_WARNING \
1321 schoenebeck 3034 (VMRelPtr) { \
1322     (void**) &basePtr, \
1323     offsetof(T_struct, T_member), \
1324     false \
1325     } \
1326 schoenebeck 3035 COMPILER_RESTORE_OFFSETOF_WARNING \
1327 schoenebeck 3034 ) \
1328 schoenebeck 2594
1329 schoenebeck 2948 /**
1330 schoenebeck 3557 * Same as DECLARE_VMINT(), but this one defines the VMInt64RelPtr,
1331     * VMInt32RelPtr and VMInt8RelPtr structures to be of read-only type.
1332     * That means the script parser will abort any script at parser time if the
1333     * script is trying to modify such a read-only built-in variable.
1334 schoenebeck 2948 *
1335     * @b NOTE: this is only intended for built-in read-only variables that
1336     * may change during runtime! If your built-in variable's data is rather
1337     * already available at parser time and won't change during runtime, then
1338     * you should rather register a built-in constant in your VM class instead!
1339     *
1340     * @see ScriptVM::builtInConstIntVariables()
1341     */
1342     #define DECLARE_VMINT_READONLY(basePtr, T_struct, T_member) ( \
1343 schoenebeck 3034 /* Disable offsetof warning, trust us, we are cautios. */ \
1344 schoenebeck 3035 COMPILER_DISABLE_OFFSETOF_WARNING \
1345 schoenebeck 3034 (VMRelPtr) { \
1346     (void**) &basePtr, \
1347     offsetof(T_struct, T_member), \
1348     true \
1349     } \
1350 schoenebeck 3035 COMPILER_RESTORE_OFFSETOF_WARNING \
1351 schoenebeck 3034 ) \
1352 schoenebeck 2948
1353 schoenebeck 2594 /** @brief Built-in VM 8 bit integer array variable.
1354     *
1355 schoenebeck 2727 * Used for defining built-in integer array script variables (8 bit per
1356 schoenebeck 3573 * array element). Currently there is no support for any other kind of
1357     * built-in array type. So all built-in integer arrays accessed by scripts
1358     * use 8 bit data types.
1359 schoenebeck 2594 */
1360     struct VMInt8Array {
1361     int8_t* data;
1362 schoenebeck 3557 vmint size;
1363 schoenebeck 3253 bool readonly; ///< Whether the array data may be modified or just be read.
1364 schoenebeck 2594
1365 schoenebeck 3253 VMInt8Array() : data(NULL), size(0), readonly(false) {}
1366 schoenebeck 2594 };
1367    
1368 schoenebeck 2945 /** @brief Virtual machine script variable.
1369     *
1370 schoenebeck 3573 * Common interface for all variables accessed in scripts, independent of
1371     * their precise data type.
1372 schoenebeck 2945 */
1373     class VMVariable : virtual public VMExpr {
1374     public:
1375     /**
1376     * Whether a script may modify the content of this variable by
1377     * assigning a new value to it.
1378     *
1379     * @see isConstExpr(), assign()
1380     */
1381     virtual bool isAssignable() const = 0;
1382    
1383     /**
1384     * In case this variable is assignable, this method will be called to
1385     * perform the value assignment to this variable with @a expr
1386     * reflecting the new value to be assigned.
1387     *
1388     * @param expr - new value to be assigned to this variable
1389     */
1390     virtual void assignExpr(VMExpr* expr) = 0;
1391     };
1392 schoenebeck 3573
1393 schoenebeck 2942 /** @brief Dynamically executed variable (abstract base class).
1394     *
1395     * Interface for the implementation of a dynamically generated content of
1396     * a built-in script variable. Most built-in variables are simply pointers
1397     * to some native location in memory. So when a script reads them, the
1398     * memory location is simply read to get the value of the variable. A
1399     * dynamic variable however is not simply a memory location. For each access
1400     * to a dynamic variable some native code is executed to actually generate
1401     * and provide the content (value) of this type of variable.
1402     */
1403 schoenebeck 2945 class VMDynVar : public VMVariable {
1404 schoenebeck 2942 public:
1405     /**
1406     * Returns true in case this dynamic variable can be considered to be a
1407     * constant expression. A constant expression will retain the same value
1408     * throughout the entire life time of a script and the expression's
1409     * constant value may be evaluated already at script parse time, which
1410     * may result in performance benefits during script runtime.
1411     *
1412     * However due to the "dynamic" behavior of dynamic variables, almost
1413     * all dynamic variables are probably not constant expressions. That's
1414     * why this method returns @c false by default. If you are really sure
1415     * that your dynamic variable implementation can be considered a
1416     * constant expression then you may override this method and return
1417     * @c true instead. Note that when you return @c true here, your
1418     * dynamic variable will really just be executed once; and exectly
1419     * already when the script is loaded!
1420     *
1421     * As an example you may implement a "constant" built-in dynamic
1422     * variable that checks for a certain operating system feature and
1423     * returns the result of that OS feature check as content (value) of
1424     * this dynamic variable. Since the respective OS feature might become
1425     * available/unavailable after OS updates, software migration, etc. the
1426     * OS feature check should at least be performed once each time the
1427     * application is launched. And since the OS feature check might take a
1428     * certain amount of execution time, it might make sense to only
1429     * perform the check if the respective variable name is actually
1430     * referenced at all in the script to be loaded. Note that the dynamic
1431     * variable will still be evaluated again though if the script is
1432     * loaded again. So it is up to you to probably cache the result in the
1433     * implementation of your dynamic variable.
1434     *
1435     * On doubt, please rather consider to use a constant built-in script
1436     * variable instead of implementing a "constant" dynamic variable, due
1437     * to the runtime overhead a dynamic variable may cause.
1438     *
1439     * @see isAssignable()
1440     */
1441 schoenebeck 2945 bool isConstExpr() const OVERRIDE { return false; }
1442 schoenebeck 2942
1443     /**
1444     * In case this dynamic variable is assignable, the new value (content)
1445     * to be assigned to this dynamic variable.
1446     *
1447     * By default this method does nothing. Override and implement this
1448     * method in your subclass in case your dynamic variable allows to
1449     * assign a new value by script.
1450     *
1451     * @param expr - new value to be assigned to this variable
1452     */
1453 schoenebeck 2945 void assignExpr(VMExpr* expr) OVERRIDE {}
1454 schoenebeck 3034
1455     virtual ~VMDynVar() {}
1456 schoenebeck 2942 };
1457    
1458     /** @brief Dynamically executed variable (of integer data type).
1459     *
1460     * This is the base class for all built-in integer script variables whose
1461     * variable content needs to be provided dynamically by executable native
1462     * code on each script variable access.
1463     */
1464     class VMDynIntVar : virtual public VMDynVar, virtual public VMIntExpr {
1465     public:
1466 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
1467 schoenebeck 3561 StdUnit_t unitType() const OVERRIDE { return VM_NO_UNIT; }
1468     bool isFinal() const OVERRIDE { return false; }
1469 schoenebeck 2942 };
1470    
1471     /** @brief Dynamically executed variable (of string data type).
1472     *
1473     * This is the base class for all built-in string script variables whose
1474     * variable content needs to be provided dynamically by executable native
1475     * code on each script variable access.
1476     */
1477     class VMDynStringVar : virtual public VMDynVar, virtual public VMStringExpr {
1478     public:
1479     };
1480    
1481 schoenebeck 3073 /** @brief Dynamically executed variable (of integer array data type).
1482     *
1483     * This is the base class for all built-in integer array script variables
1484     * whose variable content needs to be provided dynamically by executable
1485     * native code on each script variable access.
1486     */
1487     class VMDynIntArrayVar : virtual public VMDynVar, virtual public VMIntArrayExpr {
1488     public:
1489     };
1490    
1491 schoenebeck 2612 /** @brief Provider for built-in script functions and variables.
1492     *
1493 schoenebeck 2727 * Abstract base class defining the high-level interface for all classes
1494     * which add and implement built-in script functions and built-in script
1495     * variables.
1496 schoenebeck 2612 */
1497 schoenebeck 2581 class VMFunctionProvider {
1498     public:
1499 schoenebeck 2612 /**
1500     * Returns pointer to the built-in function with the given function
1501 schoenebeck 2727 * @a name, or NULL if there is no built-in function with that function
1502     * name.
1503 schoenebeck 2612 *
1504 schoenebeck 2727 * @param name - function name (i.e. "wait" or "message" or "exit", etc.)
1505 schoenebeck 2612 */
1506 schoenebeck 2581 virtual VMFunction* functionByName(const String& name) = 0;
1507 schoenebeck 2612
1508     /**
1509 schoenebeck 3311 * Returns @c true if the passed built-in function is disabled and
1510     * should be ignored by the parser. This method is called by the
1511     * parser on preprocessor level for each built-in function call within
1512     * a script. Accordingly if this method returns @c true, then the
1513     * respective function call is completely filtered out on preprocessor
1514     * level, so that built-in function won't make into the result virtual
1515     * machine representation, nor would expressions of arguments passed to
1516     * that built-in function call be evaluated, nor would any check
1517     * regarding correct usage of the built-in function be performed.
1518     * In other words: a disabled function call ends up as a comment block.
1519     *
1520     * @param fn - built-in function to be checked
1521     * @param ctx - parser context at the position where the built-in
1522     * function call is located within the script
1523     */
1524     virtual bool isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) = 0;
1525    
1526     /**
1527 schoenebeck 2612 * Returns a variable name indexed map of all built-in script variables
1528 schoenebeck 2727 * which point to native "int" scalar (usually 32 bit) variables.
1529 schoenebeck 2612 */
1530 schoenebeck 3557 virtual std::map<String,VMIntPtr*> builtInIntVariables() = 0;
1531 schoenebeck 2612
1532     /**
1533 schoenebeck 2727 * Returns a variable name indexed map of all built-in script integer
1534     * array variables with array element type "int8_t" (8 bit).
1535 schoenebeck 2612 */
1536 schoenebeck 2594 virtual std::map<String,VMInt8Array*> builtInIntArrayVariables() = 0;
1537 schoenebeck 2612
1538     /**
1539     * Returns a variable name indexed map of all built-in constant script
1540     * variables, which never change their value at runtime.
1541     */
1542 schoenebeck 3557 virtual std::map<String,vmint> builtInConstIntVariables() = 0;
1543 schoenebeck 2942
1544     /**
1545     * Returns a variable name indexed map of all built-in dynamic variables,
1546     * which are not simply data stores, rather each one of them executes
1547     * natively to provide or alter the respective script variable data.
1548     */
1549     virtual std::map<String,VMDynVar*> builtInDynamicVariables() = 0;
1550 schoenebeck 2581 };
1551    
1552 schoenebeck 2594 /** @brief Execution state of a virtual machine.
1553     *
1554     * An instance of this abstract base class represents exactly one execution
1555     * state of a virtual machine. This encompasses most notably the VM
1556 schoenebeck 2612 * execution stack, and VM polyphonic variables. It does not contain global
1557 schoenebeck 2727 * variables. Global variables are contained in the VMParserContext object.
1558 schoenebeck 2612 * You might see a VMExecContext object as one virtual thread of the virtual
1559     * machine.
1560 schoenebeck 2594 *
1561 schoenebeck 2612 * In contrast to a VMParserContext, a VMExecContext is not tied to a
1562     * ScriptVM instance. Thus you can use a VMExecContext with different
1563     * ScriptVM instances, however not concurrently at the same time.
1564     *
1565 schoenebeck 2594 * @see VMParserContext
1566     */
1567 schoenebeck 2581 class VMExecContext {
1568     public:
1569     virtual ~VMExecContext() {}
1570 schoenebeck 2727
1571     /**
1572     * In case the script was suspended for some reason, this method returns
1573     * the amount of microseconds before the script shall continue its
1574     * execution. Note that the virtual machine itself does never put its
1575     * own execution thread(s) to sleep. So the respective class (i.e. sampler
1576     * engine) which is using the virtual machine classes here, must take
1577     * care by itself about taking time stamps, determining the script
1578     * handlers that shall be put aside for the requested amount of
1579 schoenebeck 2871 * microseconds, indicated by this method by comparing the time stamps in
1580 schoenebeck 2727 * real-time, and to continue passing the respective handler to
1581     * ScriptVM::exec() as soon as its suspension exceeded, etc. Or in other
1582     * words: all classes in this directory never have an idea what time it
1583     * is.
1584     *
1585     * You should check the return value of ScriptVM::exec() to determine
1586     * whether the script was actually suspended before calling this method
1587     * here.
1588     *
1589     * @see ScriptVM::exec()
1590     */
1591 schoenebeck 3557 virtual vmint suspensionTimeMicroseconds() const = 0;
1592 schoenebeck 3207
1593     /**
1594     * Causes all polyphonic variables to be reset to zero values. A
1595     * polyphonic variable is expected to be zero when entering a new event
1596     * handler instance. As an exception the values of polyphonic variables
1597     * shall only be preserved from an note event handler instance to its
1598     * correspending specific release handler instance. So in the latter
1599     * case the script author may pass custom data from the note handler to
1600     * the release handler, but only for the same specific note!
1601     */
1602     virtual void resetPolyphonicData() = 0;
1603 schoenebeck 3221
1604     /**
1605     * Returns amount of virtual machine instructions which have been
1606     * performed the last time when this execution context was executing a
1607     * script. So in case you need the overall amount of instructions
1608     * instead, then you need to add them by yourself after each
1609     * ScriptVM::exec() call.
1610     */
1611     virtual size_t instructionsPerformed() const = 0;
1612 schoenebeck 3277
1613     /**
1614     * Sends a signal to this script execution instance to abort its script
1615     * execution as soon as possible. This method is called i.e. when one
1616     * script execution instance intends to stop another script execution
1617     * instance.
1618     */
1619     virtual void signalAbort() = 0;
1620 schoenebeck 3293
1621     /**
1622     * Copies the current entire execution state from this object to the
1623     * given object. So this can be used to "fork" a new script thread which
1624     * then may run independently with its own polyphonic data for instance.
1625     */
1626     virtual void forkTo(VMExecContext* ectx) const = 0;
1627 schoenebeck 3551
1628     /**
1629     * In case the script called the built-in exit() function and passed a
1630     * value as argument to the exit() function, then this method returns
1631     * the value that had been passed as argument to the exit() function.
1632     * Otherwise if the exit() function has not been called by the script
1633     * or no argument had been passed to the exit() function, then this
1634     * method returns NULL instead.
1635     *
1636     * Currently this is only used for automated test cases against the
1637     * script engine, which return some kind of value in the individual
1638     * test case scripts to check their behaviour in automated way. There
1639     * is no purpose for this mechanism in production use. Accordingly this
1640     * exit result value is @b always completely ignored by the sampler
1641     * engines.
1642     *
1643     * Officially the built-in exit() function does not expect any arguments
1644     * to be passed to its function call, and by default this feature is
1645     * hence disabled and will yield in a parser error unless
1646     * ScriptVM::setExitResultEnabled() was explicitly set.
1647     *
1648     * @see ScriptVM::setExitResultEnabled()
1649     */
1650     virtual VMExpr* exitResult() = 0;
1651 schoenebeck 2581 };
1652    
1653 schoenebeck 2645 /** @brief Script callback for a certain event.
1654     *
1655     * Represents a script callback for a certain event, i.e.
1656 schoenebeck 2727 * "on note ... end on" code block.
1657 schoenebeck 2645 */
1658 schoenebeck 2581 class VMEventHandler {
1659     public:
1660 schoenebeck 2645 /**
1661 schoenebeck 2879 * Type of this event handler, which identifies its purpose. For example
1662     * for a "on note ... end on" script callback block,
1663     * @c VM_EVENT_HANDLER_NOTE would be returned here.
1664     */
1665     virtual VMEventHandlerType_t eventHandlerType() const = 0;
1666    
1667     /**
1668 schoenebeck 2645 * Name of the event handler which identifies its purpose. For example
1669     * for a "on note ... end on" script callback block, the name "note"
1670     * would be returned here.
1671     */
1672 schoenebeck 2581 virtual String eventHandlerName() const = 0;
1673 schoenebeck 2645
1674     /**
1675     * Whether or not the event handler makes any use of so called
1676     * "polyphonic" variables.
1677     */
1678     virtual bool isPolyphonic() const = 0;
1679 schoenebeck 2581 };
1680    
1681 schoenebeck 2727 /**
1682 schoenebeck 3285 * Reflects the precise position and span of a specific code block within
1683     * a script. This is currently only used for the locations of commented
1684 schoenebeck 3292 * code blocks due to preprocessor statements, and for parser errors and
1685     * parser warnings.
1686 schoenebeck 3285 *
1687 schoenebeck 3292 * @see ParserIssue for code locations of parser errors and parser warnings
1688     *
1689     * @see VMParserContext::preprocessorComments() for locations of code which
1690     * have been filtered out by preprocessor statements
1691 schoenebeck 3285 */
1692     struct CodeBlock {
1693     int firstLine; ///< The first line number of this code block within the script (indexed with 1 being the very first line).
1694     int lastLine; ///< The last line number of this code block within the script.
1695     int firstColumn; ///< The first column of this code block within the script (indexed with 1 being the very first column).
1696     int lastColumn; ///< The last column of this code block within the script.
1697     };
1698    
1699     /**
1700 schoenebeck 2727 * Encapsulates a noteworty parser issue. This encompasses the type of the
1701     * issue (either a parser error or parser warning), a human readable
1702     * explanation text of the error or warning and the location of the
1703     * encountered parser issue within the script.
1704 schoenebeck 3012 *
1705     * @see VMSourceToken for processing syntax highlighting instead.
1706 schoenebeck 2727 */
1707 schoenebeck 3292 struct ParserIssue : CodeBlock {
1708 schoenebeck 2727 String txt; ///< Human readable explanation text of the parser issue.
1709     ParserIssueType_t type; ///< Whether this issue is either a parser error or just a parser warning.
1710 schoenebeck 2581
1711 schoenebeck 2727 /**
1712     * Print this issue out to the console (stdio).
1713     */
1714 schoenebeck 2581 inline void dump() {
1715     switch (type) {
1716     case PARSER_ERROR:
1717 schoenebeck 2889 printf("[ERROR] line %d, column %d: %s\n", firstLine, firstColumn, txt.c_str());
1718 schoenebeck 2581 break;
1719     case PARSER_WARNING:
1720 schoenebeck 2889 printf("[Warning] line %d, column %d: %s\n", firstLine, firstColumn, txt.c_str());
1721 schoenebeck 2581 break;
1722     }
1723     }
1724 schoenebeck 2727
1725     /**
1726     * Returns true if this issue is a parser error. In this case the parsed
1727     * script may not be executed!
1728     */
1729 schoenebeck 2581 inline bool isErr() const { return type == PARSER_ERROR; }
1730 schoenebeck 2727
1731     /**
1732     * Returns true if this issue is just a parser warning. A parsed script
1733     * that only raises warnings may be executed if desired, however the
1734     * script may not behave exactly as intended by the script author.
1735     */
1736 schoenebeck 2581 inline bool isWrn() const { return type == PARSER_WARNING; }
1737     };
1738    
1739 schoenebeck 2727 /**
1740     * Convenience function used for converting an ExprType_t constant to a
1741     * string, i.e. for generating error message by the parser.
1742     */
1743 schoenebeck 2581 inline String typeStr(const ExprType_t& type) {
1744     switch (type) {
1745     case EMPTY_EXPR: return "empty";
1746     case INT_EXPR: return "integer";
1747     case INT_ARR_EXPR: return "integer array";
1748 schoenebeck 3573 case REAL_EXPR: return "real number";
1749     case REAL_ARR_EXPR: return "real number array";
1750 schoenebeck 2581 case STRING_EXPR: return "string";
1751     case STRING_ARR_EXPR: return "string array";
1752     }
1753     return "invalid";
1754     }
1755    
1756 schoenebeck 3561 /**
1757 schoenebeck 3573 * Returns @c true in case the passed data type is some array data type.
1758     */
1759     inline bool isArray(const ExprType_t& type) {
1760     return type == INT_ARR_EXPR || type == REAL_ARR_EXPR ||
1761     type == STRING_ARR_EXPR;
1762     }
1763    
1764     /**
1765     * Returns @c true in case the passed data type is some scalar number type
1766     * (i.e. not an array and not a string).
1767     */
1768 schoenebeck 3582 inline bool isNumber(const ExprType_t& type) {
1769 schoenebeck 3573 return type == INT_EXPR || type == REAL_EXPR;
1770     }
1771    
1772     /**
1773 schoenebeck 3561 * Convenience function used for converting an StdUnit_t constant to a
1774     * string, i.e. for generating error message by the parser.
1775     */
1776     inline String unitTypeStr(const StdUnit_t& type) {
1777     switch (type) {
1778     case VM_NO_UNIT: return "none";
1779     case VM_SECOND: return "seconds";
1780     case VM_HERTZ: return "Hz";
1781     case VM_BEL: return "Bel";
1782     }
1783     return "invalid";
1784     }
1785    
1786 schoenebeck 2594 /** @brief Virtual machine representation of a script.
1787     *
1788     * An instance of this abstract base class represents a parsed script,
1789 schoenebeck 2727 * translated into a virtual machine tree. You should first check if there
1790     * were any parser errors. If there were any parser errors, you should
1791     * refrain from executing the virtual machine. Otherwise if there were no
1792     * parser errors (i.e. only warnings), then you might access one of the
1793     * script's event handlers by i.e. calling eventHandlerByName() and pass the
1794     * respective event handler to the ScriptVM class (or to one of the ScriptVM
1795 schoenebeck 2594 * descendants) for execution.
1796     *
1797 schoenebeck 2727 * @see VMExecContext, ScriptVM
1798 schoenebeck 2594 */
1799 schoenebeck 2588 class VMParserContext {
1800     public:
1801     virtual ~VMParserContext() {}
1802 schoenebeck 2727
1803     /**
1804     * Returns all noteworthy issues encountered when the script was parsed.
1805     * These are parser errors and parser warnings.
1806     */
1807 schoenebeck 2588 virtual std::vector<ParserIssue> issues() const = 0;
1808 schoenebeck 2727
1809     /**
1810     * Same as issues(), but this method only returns parser errors.
1811     */
1812 schoenebeck 2588 virtual std::vector<ParserIssue> errors() const = 0;
1813 schoenebeck 2727
1814     /**
1815     * Same as issues(), but this method only returns parser warnings.
1816     */
1817 schoenebeck 2588 virtual std::vector<ParserIssue> warnings() const = 0;
1818 schoenebeck 2727
1819     /**
1820 schoenebeck 3285 * Returns all code blocks of the script which were filtered out by the
1821     * preprocessor.
1822     */
1823     virtual std::vector<CodeBlock> preprocessorComments() const = 0;
1824    
1825     /**
1826 schoenebeck 2727 * Returns the translated virtual machine representation of an event
1827     * handler block (i.e. "on note ... end on" code block) within the
1828     * parsed script. This translated representation of the event handler
1829     * can be executed by the virtual machine.
1830     *
1831     * @param index - index of the event handler within the script
1832     */
1833 schoenebeck 2588 virtual VMEventHandler* eventHandler(uint index) = 0;
1834 schoenebeck 2727
1835     /**
1836     * Same as eventHandler(), but this method returns the event handler by
1837     * its name. So for a "on note ... end on" code block of the parsed
1838     * script you would pass "note" for argument @a name here.
1839     *
1840     * @param name - name of the event handler (i.e. "init", "note",
1841     * "controller", "release")
1842     */
1843 schoenebeck 2588 virtual VMEventHandler* eventHandlerByName(const String& name) = 0;
1844     };
1845    
1846 schoenebeck 2885 class SourceToken;
1847    
1848     /** @brief Recognized token of a script's source code.
1849     *
1850     * Represents one recognized token of a script's source code, for example
1851     * a keyword, variable name, etc. and it provides further informations about
1852     * that particular token, i.e. the precise location (line and column) of the
1853     * token within the original script's source code.
1854     *
1855     * This class is not actually used by the sampler itself. It is rather
1856     * provided for external script editor applications. Primary purpose of
1857     * this class is syntax highlighting for external script editors.
1858 schoenebeck 3012 *
1859     * @see ParserIssue for processing compile errors and warnings instead.
1860 schoenebeck 2885 */
1861     class VMSourceToken {
1862     public:
1863     VMSourceToken();
1864     VMSourceToken(SourceToken* ct);
1865     VMSourceToken(const VMSourceToken& other);
1866     virtual ~VMSourceToken();
1867    
1868     // original text of this token as it is in the script's source code
1869     String text() const;
1870    
1871     // position of token in script
1872 schoenebeck 3012 int firstLine() const; ///< First line this source token is located at in script source code (indexed with 0 being the very first line). Most source code tokens are not spanning over multiple lines, the only current exception are comments, in the latter case you need to process text() to get the last line and last column for the comment.
1873     int firstColumn() const; ///< First column on the first line this source token is located at in script source code (indexed with 0 being the very first column). To get the length of this token use text().length().
1874 schoenebeck 2885
1875     // base types
1876 schoenebeck 3012 bool isEOF() const; ///< Returns true in case this source token represents the end of the source code file.
1877     bool isNewLine() const; ///< Returns true in case this source token represents a line feed character (i.e. "\n" on Unix systems).
1878     bool isKeyword() const; ///< Returns true in case this source token represents a language keyword (i.e. "while", "function", "declare", "on", etc.).
1879     bool isVariableName() const; ///< Returns true in case this source token represents a variable name (i.e. "$someIntVariable", "%someArrayVariable", "\@someStringVariable"). @see isIntegerVariable(), isStringVariable(), isArrayVariable() for the precise variable type.
1880     bool isIdentifier() const; ///< Returns true in case this source token represents an identifier, which currently always means a function name.
1881     bool isNumberLiteral() const; ///< Returns true in case this source token represents a number literal (i.e. 123).
1882     bool isStringLiteral() const; ///< Returns true in case this source token represents a string literal (i.e. "Some text").
1883     bool isComment() const; ///< Returns true in case this source token represents a source code comment.
1884     bool isPreprocessor() const; ///< Returns true in case this source token represents a preprocessor statement.
1885 schoenebeck 3562 bool isMetricPrefix() const;
1886     bool isStdUnit() const;
1887 schoenebeck 3012 bool isOther() const; ///< Returns true in case this source token represents anything else not covered by the token types mentioned above.
1888 schoenebeck 2885
1889     // extended types
1890 schoenebeck 3012 bool isIntegerVariable() const; ///< Returns true in case this source token represents an integer variable name (i.e. "$someIntVariable").
1891 schoenebeck 3573 bool isRealVariable() const; ///< Returns true in case this source token represents a floating point variable name (i.e. "~someRealVariable").
1892 schoenebeck 3012 bool isStringVariable() const; ///< Returns true in case this source token represents an string variable name (i.e. "\@someStringVariable").
1893 schoenebeck 3573 bool isIntArrayVariable() const; ///< Returns true in case this source token represents an integer array variable name (i.e. "%someArrayVariable").
1894     bool isRealArrayVariable() const; ///< Returns true in case this source token represents a real number array variable name (i.e. "?someArrayVariable").
1895     bool isArrayVariable() const DEPRECATED_API; ///< Returns true in case this source token represents an @b integer array variable name (i.e. "%someArrayVariable"). @deprecated This method will be removed, use isIntArrayVariable() instead.
1896 schoenebeck 3012 bool isEventHandlerName() const; ///< Returns true in case this source token represents an event handler name (i.e. "note", "release", "controller").
1897 schoenebeck 2885
1898     VMSourceToken& operator=(const VMSourceToken& other);
1899    
1900     private:
1901     SourceToken* m_token;
1902     };
1903    
1904 schoenebeck 2581 } // namespace LinuxSampler
1905    
1906     #endif // LS_INSTR_SCRIPT_PARSER_COMMON_H

  ViewVC Help
Powered by ViewVC