/[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 3557 - (hide annotations) (download) (as text)
Sun Aug 18 00:06:04 2019 UTC (4 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 58010 byte(s)
* NKSP: Introducing 64 bit support for NKSP integer scripts
  variables (declare $foo).
* Require C++11 compiler support.
* Autoconf: Added m4/ax_cxx_compile_stdcxx.m4 macro which is used
  for checking in configure for C++11 support (as mandatory
  requirement) and automatically adds compiler argument if required
  (e.g. -std=C++11).
* Bumped version (2.1.1.svn3).

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 2581
23     namespace LinuxSampler {
24 schoenebeck 2727
25     /**
26 schoenebeck 3557 * Native data type used by the script engine both internally, as well as
27     * for all integer data types used by scripts (i.e. for all $foo variables
28     * in NKSP scripts). Note that this is different from the original KSP which
29     * is limited to 32 bit for integer variables in KSP scripts.
30     */
31     typedef int64_t vmint;
32    
33     /**
34     * Native data type used internally by the script engine for all unsigned
35     * integer types. This type is currently not exposed to scripts.
36     */
37     typedef uint64_t vmuint;
38    
39     /**
40 schoenebeck 2727 * Identifies the type of a noteworthy issue identified by the script
41     * parser. That's either a parser error or parser warning.
42     */
43 schoenebeck 2581 enum ParserIssueType_t {
44 schoenebeck 2727 PARSER_ERROR, ///< Script parser encountered an error, the script cannot be executed.
45     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.
46 schoenebeck 2581 };
47    
48 schoenebeck 2727 /** @brief Expression's data type.
49     *
50     * Identifies to which data type an expression within a script evaluates to.
51     * This can for example reflect the data type of script function arguments,
52     * script function return values, but also the resulting data type of some
53     * mathematical formula within a script.
54     */
55 schoenebeck 2581 enum ExprType_t {
56 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)
57     INT_EXPR, ///< integer (scalar) expression
58     INT_ARR_EXPR, ///< integer array expression
59     STRING_EXPR, ///< string expression
60     STRING_ARR_EXPR, ///< string array expression
61 schoenebeck 2581 };
62    
63 schoenebeck 2727 /** @brief Result flags of a script statement or script function call.
64     *
65     * A set of bit flags which provide informations about the success or
66     * failure of a statement within a script. That's also especially used for
67     * providing informations about success / failure of a call to a built-in
68     * script function. The virtual machine evaluates these flags during runtime
69     * to decide whether it should i.e. stop or suspend execution of a script.
70     *
71     * Since these are bit flags, these constants are bitwise combined.
72     */
73 schoenebeck 2581 enum StmtFlags_t {
74     STMT_SUCCESS = 0, ///< Function / statement was executed successfully, no error occurred.
75 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).
76     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
77     STMT_ERROR_OCCURRED = (1<<2), ///< VM stopped execution due to some script runtime error that occurred
78 schoenebeck 2581 };
79    
80 schoenebeck 2727 /** @brief Virtual machine execution status.
81     *
82     * A set of bit flags which reflect the current overall execution status of
83     * the virtual machine concerning a certain script execution instance.
84     *
85     * Since these are bit flags, these constants are bitwise combined.
86     */
87 schoenebeck 2581 enum VMExecStatus_t {
88 schoenebeck 2727 VM_EXEC_NOT_RUNNING = 0, ///< Script is currently not executed by the VM.
89     VM_EXEC_RUNNING = 1, ///< The VM is currently executing the script.
90     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.
91     VM_EXEC_ERROR = (1<<2), ///< A runtime error occurred while executing the script (i.e. a call to some built-in script function failed).
92 schoenebeck 2581 };
93    
94 schoenebeck 2879 /** @brief Script event handler type.
95     *
96     * Identifies one of the possible event handler callback types defined by
97     * the NKSP script language.
98 schoenebeck 3557 *
99     * IMPORTANT: this type is forced to be emitted as int32_t type ATM, because
100     * that's the native size expected by the built-in instrument script
101     * variable bindings (see occurrences of VMInt32RelPtr and DECLARE_VMINT
102     * respectively. A native type mismatch between the two could lead to
103     * undefined behavior! Background: By definition the C/C++ compiler is free
104     * to choose a bit size for individual enums which it might find
105     * appropriate, which is usually decided by the compiler according to the
106     * biggest enum constant value defined (in practice it is usually 32 bit).
107 schoenebeck 2879 */
108 schoenebeck 3557 enum VMEventHandlerType_t : int32_t {
109 schoenebeck 2879 VM_EVENT_HANDLER_INIT, ///< Initilization event handler, that is script's "on init ... end on" code block.
110     VM_EVENT_HANDLER_NOTE, ///< Note event handler, that is script's "on note ... end on" code block.
111     VM_EVENT_HANDLER_RELEASE, ///< Release event handler, that is script's "on release ... end on" code block.
112     VM_EVENT_HANDLER_CONTROLLER, ///< Controller event handler, that is script's "on controller ... end on" code block.
113     };
114    
115 schoenebeck 2727 // just symbol prototyping
116 schoenebeck 2596 class VMIntExpr;
117     class VMStringExpr;
118 schoenebeck 2619 class VMIntArrayExpr;
119     class VMStringArrayExpr;
120 schoenebeck 3311 class VMParserContext;
121 schoenebeck 2596
122 schoenebeck 2727 /** @brief Virtual machine expression
123     *
124     * This is the abstract base class for all expressions of scripts.
125     * Deriving classes must implement the abstract method exprType().
126     *
127     * An expression within a script is translated into one instance of this
128     * class. It allows a high level access for the virtual machine to evaluate
129     * and handle expressions appropriately during execution. Expressions are
130     * for example all kinds of formulas, function calls, statements or a
131     * combination of them. Most of them evaluate to some kind of value, which
132     * might be further processed as part of encompassing expressions to outer
133     * expression results and so forth.
134     */
135 schoenebeck 2581 class VMExpr {
136     public:
137 schoenebeck 2727 /**
138     * Identifies the data type to which the result of this expression
139     * evaluates to. This abstract method must be implemented by deriving
140     * classes.
141     */
142 schoenebeck 2581 virtual ExprType_t exprType() const = 0;
143 schoenebeck 2727
144     /**
145     * In case this expression is an integer expression, then this method
146     * returns a casted pointer to that VMIntExpr object. It returns NULL
147     * if this expression is not an integer expression.
148     *
149     * @b Note: type casting performed by this method is strict! That means
150     * if this expression is i.e. actually a string expression like "12",
151     * calling asInt() will @b not cast that numerical string expression to
152     * an integer expression 12 for you, instead this method will simply
153     * return NULL!
154     *
155     * @see exprType()
156     */
157 schoenebeck 2596 VMIntExpr* asInt() const;
158 schoenebeck 2727
159     /**
160     * In case this expression is a string expression, then this method
161     * returns a casted pointer to that VMStringExpr object. It returns NULL
162     * if this expression is not a string expression.
163     *
164     * @b Note: type casting performed by this method is strict! That means
165     * if this expression is i.e. actually an integer expression like 120,
166     * calling asString() will @b not cast that integer expression to a
167     * string expression "120" for you, instead this method will simply
168     * return NULL!
169     *
170     * @see exprType()
171     */
172 schoenebeck 2596 VMStringExpr* asString() const;
173 schoenebeck 2727
174     /**
175     * In case this expression is an integer array expression, then this
176     * method returns a casted pointer to that VMIntArrayExpr object. It
177     * returns NULL if this expression is not an integer array expression.
178     *
179     * @b Note: type casting performed by this method is strict! That means
180     * if this expression is i.e. an integer expression or a string
181     * expression, calling asIntArray() will @b not cast those scalar
182     * expressions to an array expression for you, instead this method will
183     * simply return NULL!
184     *
185 schoenebeck 3056 * @b Note: this method is currently, and in contrast to its other
186     * counter parts, declared as virtual method. Some deriving classes are
187     * currently using this to override this default implementation in order
188     * to implement an "evaluate now as integer array" behavior. This has
189     * efficiency reasons, however this also currently makes this part of
190     * the API less clean and should thus be addressed in future with
191     * appropriate changes to the API.
192     *
193 schoenebeck 2727 * @see exprType()
194     */
195 schoenebeck 3056 virtual VMIntArrayExpr* asIntArray() const;
196 schoenebeck 2945
197     /**
198     * Returns true in case this expression can be considered to be a
199     * constant expression. A constant expression will retain the same
200     * value throughout the entire life time of a script and the
201     * expression's constant value may be evaluated already at script
202     * parse time, which may result in performance benefits during script
203     * runtime.
204 schoenebeck 2960 *
205     * @b NOTE: A constant expression is per se always also non modifyable.
206     * But a non modifyable expression may not necessarily be a constant
207     * expression!
208     *
209     * @see isModifyable()
210 schoenebeck 2945 */
211     virtual bool isConstExpr() const = 0;
212    
213 schoenebeck 2960 /**
214     * Returns true in case this expression is allowed to be modified.
215     * If this method returns @c false then this expression must be handled
216     * as read-only expression, which means that assigning a new value to it
217     * is either not possible or not allowed.
218     *
219     * @b NOTE: A constant expression is per se always also non modifyable.
220     * But a non modifyable expression may not necessarily be a constant
221     * expression!
222     *
223     * @see isConstExpr()
224     */
225 schoenebeck 2945 bool isModifyable() const;
226 schoenebeck 2581 };
227    
228 schoenebeck 2727 /** @brief Virtual machine integer expression
229     *
230     * This is the abstract base class for all expressions inside scripts which
231     * evaluate to an integer (scalar) value. Deriving classes implement the
232     * abstract method evalInt() to return the actual integer result value of
233     * the expression.
234     */
235 schoenebeck 2581 class VMIntExpr : virtual public VMExpr {
236     public:
237 schoenebeck 2727 /**
238     * Returns the result of this expression as integer (scalar) value.
239     * This abstract method must be implemented by deriving classes.
240     */
241 schoenebeck 3557 virtual vmint evalInt() = 0;
242 schoenebeck 2727
243     /**
244     * Returns always INT_EXPR for instances of this class.
245     */
246     ExprType_t exprType() const OVERRIDE { return INT_EXPR; }
247 schoenebeck 2581 };
248    
249 schoenebeck 2727 /** @brief Virtual machine string expression
250     *
251     * This is the abstract base class for all expressions inside scripts which
252     * evaluate to a string value. Deriving classes implement the abstract
253     * method evalStr() to return the actual string result value of the
254     * expression.
255     */
256 schoenebeck 2581 class VMStringExpr : virtual public VMExpr {
257     public:
258 schoenebeck 2727 /**
259     * Returns the result of this expression as string value. This abstract
260     * method must be implemented by deriving classes.
261     */
262 schoenebeck 2581 virtual String evalStr() = 0;
263 schoenebeck 2727
264     /**
265     * Returns always STRING_EXPR for instances of this class.
266     */
267     ExprType_t exprType() const OVERRIDE { return STRING_EXPR; }
268 schoenebeck 2581 };
269    
270 schoenebeck 2727 /** @brief Virtual Machine Array Value Expression
271     *
272     * This is the abstract base class for all expressions inside scripts which
273     * evaluate to some kind of array value. Deriving classes implement the
274     * abstract method arraySize() to return the amount of elements within the
275     * array.
276     */
277 schoenebeck 2619 class VMArrayExpr : virtual public VMExpr {
278     public:
279 schoenebeck 2727 /**
280     * Returns amount of elements in this array. This abstract method must
281     * be implemented by deriving classes.
282     */
283 schoenebeck 3557 virtual vmint arraySize() const = 0;
284 schoenebeck 2619 };
285    
286 schoenebeck 2727 /** @brief Virtual Machine Integer Array Expression
287     *
288     * This is the abstract base class for all expressions inside scripts which
289     * evaluate to an array of integer values. Deriving classes implement the
290     * abstract methods arraySize(), evalIntElement() and assignIntElement() to
291     * access the individual integer array values.
292     */
293 schoenebeck 2619 class VMIntArrayExpr : virtual public VMArrayExpr {
294     public:
295 schoenebeck 2727 /**
296     * Returns the (scalar) integer value of the array element given by
297     * element index @a i.
298     *
299     * @param i - array element index (must be between 0 .. arraySize() - 1)
300     */
301 schoenebeck 3557 virtual vmint evalIntElement(vmuint i) = 0;
302 schoenebeck 2727
303     /**
304     * Changes the current value of an element (given by array element
305     * index @a i) of this integer array.
306     *
307     * @param i - array element index (must be between 0 .. arraySize() - 1)
308     * @param value - new integer scalar value to be assigned to that array element
309     */
310 schoenebeck 3557 virtual void assignIntElement(vmuint i, vmint value) = 0;
311 schoenebeck 2727
312     /**
313     * Returns always INT_ARR_EXPR for instances of this class.
314     */
315     ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; }
316 schoenebeck 2619 };
317    
318 schoenebeck 2727 /** @brief Arguments (parameters) for being passed to a built-in script function.
319     *
320     * An argument or a set of arguments passed to a script function are
321     * translated by the parser to an instance of this class. This abstract
322     * interface class is used by implementations of built-in functions to
323     * obtain the individual function argument values being passed to them at
324     * runtime.
325     */
326 schoenebeck 2581 class VMFnArgs {
327     public:
328 schoenebeck 2727 /**
329     * Returns the amount of arguments going to be passed to the script
330     * function.
331     */
332 schoenebeck 3557 virtual vmint argsCount() const = 0;
333 schoenebeck 2727
334     /**
335     * Returns the respective argument (requested by argument index @a i) of
336     * this set of arguments. This method is called by implementations of
337     * built-in script functions to obtain the value of each function
338     * argument passed to the function at runtime.
339     *
340     * @param i - function argument index (indexed from left to right)
341     */
342 schoenebeck 3557 virtual VMExpr* arg(vmint i) = 0;
343 schoenebeck 2581 };
344    
345 schoenebeck 2727 /** @brief Result value returned from a call to a built-in script function.
346     *
347     * Implementations of built-in script functions return an instance of this
348     * object to let the virtual machine obtain the result value of the function
349     * call, which might then be further processed by the virtual machine
350     * according to the script. It also provides informations about the success
351     * or failure of the function call.
352     */
353 schoenebeck 2581 class VMFnResult {
354     public:
355 schoenebeck 2727 /**
356     * Returns the result value of the function call, represented by a high
357     * level expression object.
358     */
359 schoenebeck 2581 virtual VMExpr* resultValue() = 0;
360 schoenebeck 2727
361     /**
362     * Provides detailed informations of the success / failure of the
363     * function call. The virtual machine is evaluating the flags returned
364     * here to decide whether it must abort or suspend execution of the
365     * script at this point.
366     */
367 schoenebeck 2581 virtual StmtFlags_t resultFlags() { return STMT_SUCCESS; }
368     };
369    
370 schoenebeck 2727 /** @brief Virtual machine built-in function.
371 schoenebeck 2612 *
372     * Abstract base class for built-in script functions, defining the interface
373 schoenebeck 2727 * for all built-in script function implementations. All built-in script
374     * functions are deriving from this abstract interface class in order to
375     * provide their functionality to the virtual machine with this unified
376     * interface.
377     *
378     * The methods of this interface class provide two purposes:
379     *
380     * 1. When a script is loaded, the script parser uses the methods of this
381     * interface to check whether the script author was calling the
382     * respective built-in script function in a correct way. For example
383     * the parser checks whether the required amount of parameters were
384     * passed to the function and whether the data types passed match the
385     * data types expected by the function. If not, loading the script will
386     * be aborted with a parser error, describing to the user (i.e. script
387     * author) the precise misusage of the respective function.
388     * 2. After the script was loaded successfully and the script is executed,
389     * the virtual machine calls the exec() method of the respective built-in
390     * function to provide the actual functionality of the built-in function
391     * call.
392 schoenebeck 2612 */
393 schoenebeck 2581 class VMFunction {
394     public:
395 schoenebeck 2612 /**
396     * Script data type of the function's return value. If the function does
397 schoenebeck 2727 * not return any value (void), then it returns EMPTY_EXPR here.
398 schoenebeck 2612 */
399 schoenebeck 2581 virtual ExprType_t returnType() = 0;
400 schoenebeck 2612
401     /**
402     * Minimum amount of function arguments this function accepts. If a
403     * script is calling this function with less arguments, the script
404     * parser will throw a parser error.
405     */
406 schoenebeck 3557 virtual vmint minRequiredArgs() const = 0;
407 schoenebeck 2612
408     /**
409     * Maximum amount of function arguments this functions accepts. If a
410     * script is calling this function with more arguments, the script
411     * parser will throw a parser error.
412     */
413 schoenebeck 3557 virtual vmint maxAllowedArgs() const = 0;
414 schoenebeck 2612
415     /**
416     * Script data type of the function's @c iArg 'th function argument.
417     * The information provided here is less strong than acceptsArgType().
418     * The parser will compare argument data types provided in scripts by
419 schoenebeck 2871 * calling acceptsArgType(). The return value of argType() is used by the
420 schoenebeck 2612 * parser instead to show an appropriate parser error which data type
421     * this function usually expects as "default" data type. Reason: a
422     * function may accept multiple data types for a certain function
423     * argument and would automatically cast the passed argument value in
424     * that case to the type it actually needs.
425     *
426     * @param iArg - index of the function argument in question
427 schoenebeck 2727 * (must be between 0 .. maxAllowedArgs() - 1)
428 schoenebeck 2612 */
429 schoenebeck 3557 virtual ExprType_t argType(vmint iArg) const = 0;
430 schoenebeck 2612
431     /**
432 schoenebeck 2945 * This method is called by the parser to check whether arguments
433 schoenebeck 2612 * passed in scripts to this function are accepted by this function. If
434     * a script calls this function with an argument's data type not
435 schoenebeck 2727 * accepted by this function, the parser will throw a parser error. On
436     * such errors the data type returned by argType() will be used to
437     * assemble an appropriate error message regarding the precise misusage
438     * of the built-in function.
439 schoenebeck 2612 *
440     * @param iArg - index of the function argument in question
441 schoenebeck 2727 * (must be between 0 .. maxAllowedArgs() - 1)
442 schoenebeck 2612 * @param type - script data type used for this function argument by
443     * currently parsed script
444 schoenebeck 2727 * @return true if the given data type would be accepted for the
445     * respective function argument by the function
446 schoenebeck 2612 */
447 schoenebeck 3557 virtual bool acceptsArgType(vmint iArg, ExprType_t type) const = 0;
448 schoenebeck 2612
449     /**
450 schoenebeck 2945 * This method is called by the parser to check whether some arguments
451     * (and if yes which ones) passed to this script function will be
452     * modified by this script function. Most script functions simply use
453     * their arguments as inputs, that is they only read the argument's
454     * values. However some script function may also use passed
455     * argument(s) as output variables. In this case the function
456     * implementation must return @c true for the respective argument
457     * index here.
458     *
459     * @param iArg - index of the function argument in question
460     * (must be between 0 .. maxAllowedArgs() - 1)
461     */
462 schoenebeck 3557 virtual bool modifiesArg(vmint iArg) const = 0;
463 schoenebeck 2945
464     /**
465 schoenebeck 2727 * Implements the actual function execution. This exec() method is
466     * called by the VM whenever this function implementation shall be
467     * executed at script runtime. This method blocks until the function
468     * call completed.
469 schoenebeck 2612 *
470     * @param args - function arguments for executing this built-in function
471 schoenebeck 2727 * @returns function's return value (if any) and general status
472     * informations (i.e. whether the function call caused a
473     * runtime error)
474 schoenebeck 2612 */
475 schoenebeck 2581 virtual VMFnResult* exec(VMFnArgs* args) = 0;
476 schoenebeck 2612
477     /**
478 schoenebeck 2727 * Convenience method for function implementations to show warning
479     * messages during actual execution of the built-in function.
480 schoenebeck 2612 *
481 schoenebeck 2727 * @param txt - runtime warning text to be shown to user
482 schoenebeck 2612 */
483 schoenebeck 2598 void wrnMsg(const String& txt);
484 schoenebeck 2612
485     /**
486 schoenebeck 2727 * Convenience method for function implementations to show error
487     * messages during actual execution of the built-in function.
488 schoenebeck 2612 *
489 schoenebeck 2727 * @param txt - runtime error text to be shown to user
490 schoenebeck 2612 */
491 schoenebeck 2598 void errMsg(const String& txt);
492 schoenebeck 2581 };
493    
494 schoenebeck 2727 /** @brief Virtual machine relative pointer.
495     *
496 schoenebeck 3557 * POD base of VMInt64RelPtr, VMInt32RelPtr and VMInt8RelPtr structures. Not
497     * intended to be used directly. Use VMInt64RelPtr, VMInt32RelPtr,
498     * VMInt8RelPtr instead.
499 schoenebeck 2727 *
500 schoenebeck 3557 * @see VMInt64RelPtr, VMInt32RelPtr, VMInt8RelPtr
501 schoenebeck 2594 */
502     struct VMRelPtr {
503     void** base; ///< Base pointer.
504 schoenebeck 3557 vmint offset; ///< Offset (in bytes) relative to base pointer.
505 schoenebeck 2948 bool readonly; ///< Whether the pointed data may be modified or just be read.
506 schoenebeck 2594 };
507    
508 schoenebeck 3557 /** @brief Pointer to built-in VM integer variable (interface class).
509 schoenebeck 2594 *
510 schoenebeck 3557 * This class acts as an abstract interface to all built-in integer script
511     * variables, independent of their actual native size (i.e. some built-in
512     * script variables are internally using a native int size of 64 bit or 32
513     * bit or 8 bit). The virtual machine is using this interface class instead
514     * of its implementing descendants (VMInt64RelPtr, VMInt32RelPtr,
515     * VMInt8RelPtr) in order for the virtual machine for not being required to
516     * handle each of them differently.
517     */
518     struct VMIntPtr {
519     virtual vmint evalInt() = 0;
520     virtual void assign(vmint i) = 0;
521     virtual bool isAssignable() const = 0;
522     };
523    
524     /** @brief Pointer to built-in VM integer variable (of C/C++ type int64_t).
525     *
526     * Used for defining built-in 64 bit integer script variables.
527     *
528     * @b CAUTION: You may only use this class for pointing to C/C++ variables
529     * of type "int64_t" (thus being exactly 64 bit in size). If the C/C++ int
530     * variable you want to reference is only 32 bit in size then you @b must
531     * use VMInt32RelPtr instead! Respectively for a referenced native variable
532     * with only 8 bit in size you @b must use VMInt8RelPtr instead!
533     *
534     * For efficiency reasons the actual native C/C++ int variable is referenced
535     * by two components here. The actual native int C/C++ variable in memory
536     * is dereferenced at VM run-time by taking the @c base pointer dereference
537     * and adding @c offset bytes. This has the advantage that for a large
538     * number of built-in int variables, only one (or few) base pointer need
539     * to be re-assigned before running a script, instead of updating each
540     * built-in variable each time before a script is executed.
541     *
542     * Refer to DECLARE_VMINT() for example code.
543     *
544     * @see VMInt32RelPtr, VMInt8RelPtr, DECLARE_VMINT()
545     */
546     struct VMInt64RelPtr : VMRelPtr, VMIntPtr {
547     VMInt64RelPtr() {
548     base = NULL;
549     offset = 0;
550     readonly = false;
551     }
552     VMInt64RelPtr(const VMRelPtr& data) {
553     base = data.base;
554     offset = data.offset;
555     readonly = false;
556     }
557     vmint evalInt() OVERRIDE {
558     return (vmint)*(int64_t*)&(*(uint8_t**)base)[offset];
559     }
560     void assign(vmint i) OVERRIDE {
561     *(int64_t*)&(*(uint8_t**)base)[offset] = (int64_t)i;
562     }
563     bool isAssignable() const OVERRIDE { return !readonly; }
564     };
565    
566     /** @brief Pointer to built-in VM integer variable (of C/C++ type int32_t).
567     *
568 schoenebeck 2727 * Used for defining built-in 32 bit integer script variables.
569 schoenebeck 2594 *
570     * @b CAUTION: You may only use this class for pointing to C/C++ variables
571 schoenebeck 3557 * of type "int32_t" (thus being exactly 32 bit in size). If the C/C++ int
572     * variable you want to reference is 64 bit in size then you @b must use
573     * VMInt64RelPtr instead! Respectively for a referenced native variable with
574     * only 8 bit in size you @b must use VMInt8RelPtr instead!
575 schoenebeck 2594 *
576     * For efficiency reasons the actual native C/C++ int variable is referenced
577     * by two components here. The actual native int C/C++ variable in memory
578     * is dereferenced at VM run-time by taking the @c base pointer dereference
579     * and adding @c offset bytes. This has the advantage that for a large
580     * number of built-in int variables, only one (or few) base pointer need
581     * to be re-assigned before running a script, instead of updating each
582     * built-in variable each time before a script is executed.
583     *
584     * Refer to DECLARE_VMINT() for example code.
585     *
586 schoenebeck 3557 * @see VMInt64RelPtr, VMInt8RelPtr, DECLARE_VMINT()
587 schoenebeck 2594 */
588 schoenebeck 3557 struct VMInt32RelPtr : VMRelPtr, VMIntPtr {
589     VMInt32RelPtr() {
590 schoenebeck 2594 base = NULL;
591     offset = 0;
592 schoenebeck 2948 readonly = false;
593 schoenebeck 2594 }
594 schoenebeck 3557 VMInt32RelPtr(const VMRelPtr& data) {
595 schoenebeck 2594 base = data.base;
596     offset = data.offset;
597 schoenebeck 2948 readonly = false;
598 schoenebeck 2594 }
599 schoenebeck 3557 vmint evalInt() OVERRIDE {
600     return (vmint)*(int32_t*)&(*(uint8_t**)base)[offset];
601     }
602     void assign(vmint i) OVERRIDE {
603     *(int32_t*)&(*(uint8_t**)base)[offset] = (int32_t)i;
604     }
605     bool isAssignable() const OVERRIDE { return !readonly; }
606 schoenebeck 2594 };
607    
608     /** @brief Pointer to built-in VM integer variable (of C/C++ type int8_t).
609     *
610 schoenebeck 2727 * Used for defining built-in 8 bit integer script variables.
611 schoenebeck 2594 *
612     * @b CAUTION: You may only use this class for pointing to C/C++ variables
613     * of type "int8_t" (8 bit integer). If the C/C++ int variable you want to
614 schoenebeck 3557 * reference is not exactly 8 bit in size then you @b must respectively use
615     * either VMInt32RelPtr for native 32 bit variables or VMInt64RelPtrl for
616     * native 64 bit variables instead!
617 schoenebeck 2594 *
618     * For efficiency reasons the actual native C/C++ int variable is referenced
619     * by two components here. The actual native int C/C++ variable in memory
620     * is dereferenced at VM run-time by taking the @c base pointer dereference
621     * and adding @c offset bytes. This has the advantage that for a large
622     * number of built-in int variables, only one (or few) base pointer need
623     * to be re-assigned before running a script, instead of updating each
624     * built-in variable each time before a script is executed.
625     *
626     * Refer to DECLARE_VMINT() for example code.
627     *
628 schoenebeck 3557 * @see VMIntRel32Ptr, VMIntRel64Ptr, DECLARE_VMINT()
629 schoenebeck 2594 */
630 schoenebeck 3557 struct VMInt8RelPtr : VMRelPtr, VMIntPtr {
631     VMInt8RelPtr() {
632     base = NULL;
633     offset = 0;
634     readonly = false;
635 schoenebeck 2594 }
636 schoenebeck 3557 VMInt8RelPtr(const VMRelPtr& data) {
637     base = data.base;
638     offset = data.offset;
639     readonly = false;
640 schoenebeck 2594 }
641 schoenebeck 3557 vmint evalInt() OVERRIDE {
642     return (vmint)*(uint8_t*)&(*(uint8_t**)base)[offset];
643     }
644     void assign(vmint i) OVERRIDE {
645     *(uint8_t*)&(*(uint8_t**)base)[offset] = (uint8_t)i;
646     }
647     bool isAssignable() const OVERRIDE { return !readonly; }
648 schoenebeck 2594 };
649    
650 schoenebeck 3557 /** @brief Pointer to built-in VM integer variable (of C/C++ type vmint).
651     *
652     * Use this typedef if the native variable to be pointed to is using the
653     * typedef vmint. If the native C/C++ variable to be pointed to is using
654     * another C/C++ type then better use one of VMInt64RelPtr or VMInt32RelPtr
655     * instead.
656     */
657     typedef VMInt64RelPtr VMIntRelPtr;
658    
659 schoenebeck 3035 #if HAVE_CXX_EMBEDDED_PRAGMA_DIAGNOSTICS
660     # define COMPILER_DISABLE_OFFSETOF_WARNING \
661     _Pragma("GCC diagnostic push") \
662     _Pragma("GCC diagnostic ignored \"-Winvalid-offsetof\"")
663     # define COMPILER_RESTORE_OFFSETOF_WARNING \
664     _Pragma("GCC diagnostic pop")
665     #else
666     # define COMPILER_DISABLE_OFFSETOF_WARNING
667     # define COMPILER_RESTORE_OFFSETOF_WARNING
668     #endif
669    
670 schoenebeck 2594 /**
671 schoenebeck 3557 * Convenience macro for initializing VMInt64RelPtr, VMInt32RelPtr and
672     * VMInt8RelPtr structures. Usage example:
673 schoenebeck 2594 * @code
674     * struct Foo {
675 schoenebeck 2727 * uint8_t a; // native representation of a built-in integer script variable
676 schoenebeck 3557 * int64_t b; // native representation of another built-in integer script variable
677     * int64_t c; // native representation of another built-in integer script variable
678 schoenebeck 2727 * uint8_t d; // native representation of another built-in integer script variable
679 schoenebeck 2594 * };
680     *
681 schoenebeck 2727 * // initializing the built-in script variables to some values
682     * Foo foo1 = (Foo) { 1, 2000, 3000, 4 };
683     * Foo foo2 = (Foo) { 5, 6000, 7000, 8 };
684 schoenebeck 2594 *
685     * Foo* pFoo;
686     *
687 schoenebeck 2727 * VMInt8RelPtr varA = DECLARE_VMINT(pFoo, class Foo, a);
688 schoenebeck 3557 * VMInt64RelPtr varB = DECLARE_VMINT(pFoo, class Foo, b);
689     * VMInt64RelPtr varC = DECLARE_VMINT(pFoo, class Foo, c);
690 schoenebeck 2727 * VMInt8RelPtr varD = DECLARE_VMINT(pFoo, class Foo, d);
691 schoenebeck 2594 *
692     * pFoo = &foo1;
693 schoenebeck 2727 * printf("%d\n", varA->evalInt()); // will print 1
694     * printf("%d\n", varB->evalInt()); // will print 2000
695     * printf("%d\n", varC->evalInt()); // will print 3000
696     * printf("%d\n", varD->evalInt()); // will print 4
697 schoenebeck 2594 *
698 schoenebeck 2727 * // same printf() code, just with pFoo pointer being changed ...
699     *
700 schoenebeck 2594 * pFoo = &foo2;
701 schoenebeck 2727 * printf("%d\n", varA->evalInt()); // will print 5
702     * printf("%d\n", varB->evalInt()); // will print 6000
703     * printf("%d\n", varC->evalInt()); // will print 7000
704     * printf("%d\n", varD->evalInt()); // will print 8
705 schoenebeck 2594 * @endcode
706 schoenebeck 2727 * As you can see above, by simply changing one single pointer, you can
707     * remap a huge bunch of built-in integer script variables to completely
708     * different native values/native variables. Which especially reduces code
709     * complexity inside the sampler engines which provide the actual script
710     * functionalities.
711 schoenebeck 2594 */
712 schoenebeck 3034 #define DECLARE_VMINT(basePtr, T_struct, T_member) ( \
713     /* Disable offsetof warning, trust us, we are cautios. */ \
714 schoenebeck 3035 COMPILER_DISABLE_OFFSETOF_WARNING \
715 schoenebeck 3034 (VMRelPtr) { \
716     (void**) &basePtr, \
717     offsetof(T_struct, T_member), \
718     false \
719     } \
720 schoenebeck 3035 COMPILER_RESTORE_OFFSETOF_WARNING \
721 schoenebeck 3034 ) \
722 schoenebeck 2594
723 schoenebeck 2948 /**
724 schoenebeck 3557 * Same as DECLARE_VMINT(), but this one defines the VMInt64RelPtr,
725     * VMInt32RelPtr and VMInt8RelPtr structures to be of read-only type.
726     * That means the script parser will abort any script at parser time if the
727     * script is trying to modify such a read-only built-in variable.
728 schoenebeck 2948 *
729     * @b NOTE: this is only intended for built-in read-only variables that
730     * may change during runtime! If your built-in variable's data is rather
731     * already available at parser time and won't change during runtime, then
732     * you should rather register a built-in constant in your VM class instead!
733     *
734     * @see ScriptVM::builtInConstIntVariables()
735     */
736     #define DECLARE_VMINT_READONLY(basePtr, T_struct, T_member) ( \
737 schoenebeck 3034 /* Disable offsetof warning, trust us, we are cautios. */ \
738 schoenebeck 3035 COMPILER_DISABLE_OFFSETOF_WARNING \
739 schoenebeck 3034 (VMRelPtr) { \
740     (void**) &basePtr, \
741     offsetof(T_struct, T_member), \
742     true \
743     } \
744 schoenebeck 3035 COMPILER_RESTORE_OFFSETOF_WARNING \
745 schoenebeck 3034 ) \
746 schoenebeck 2948
747 schoenebeck 2594 /** @brief Built-in VM 8 bit integer array variable.
748     *
749 schoenebeck 2727 * Used for defining built-in integer array script variables (8 bit per
750     * array element). Currently there is no support for any other kind of array
751     * type. So all integer arrays of scripts use 8 bit data types.
752 schoenebeck 2594 */
753     struct VMInt8Array {
754     int8_t* data;
755 schoenebeck 3557 vmint size;
756 schoenebeck 3253 bool readonly; ///< Whether the array data may be modified or just be read.
757 schoenebeck 2594
758 schoenebeck 3253 VMInt8Array() : data(NULL), size(0), readonly(false) {}
759 schoenebeck 2594 };
760    
761 schoenebeck 2945 /** @brief Virtual machine script variable.
762     *
763     * Common interface for all variables accessed in scripts.
764     */
765     class VMVariable : virtual public VMExpr {
766     public:
767     /**
768     * Whether a script may modify the content of this variable by
769     * assigning a new value to it.
770     *
771     * @see isConstExpr(), assign()
772     */
773     virtual bool isAssignable() const = 0;
774    
775     /**
776     * In case this variable is assignable, this method will be called to
777     * perform the value assignment to this variable with @a expr
778     * reflecting the new value to be assigned.
779     *
780     * @param expr - new value to be assigned to this variable
781     */
782     virtual void assignExpr(VMExpr* expr) = 0;
783     };
784    
785 schoenebeck 2942 /** @brief Dynamically executed variable (abstract base class).
786     *
787     * Interface for the implementation of a dynamically generated content of
788     * a built-in script variable. Most built-in variables are simply pointers
789     * to some native location in memory. So when a script reads them, the
790     * memory location is simply read to get the value of the variable. A
791     * dynamic variable however is not simply a memory location. For each access
792     * to a dynamic variable some native code is executed to actually generate
793     * and provide the content (value) of this type of variable.
794     */
795 schoenebeck 2945 class VMDynVar : public VMVariable {
796 schoenebeck 2942 public:
797     /**
798     * Returns true in case this dynamic variable can be considered to be a
799     * constant expression. A constant expression will retain the same value
800     * throughout the entire life time of a script and the expression's
801     * constant value may be evaluated already at script parse time, which
802     * may result in performance benefits during script runtime.
803     *
804     * However due to the "dynamic" behavior of dynamic variables, almost
805     * all dynamic variables are probably not constant expressions. That's
806     * why this method returns @c false by default. If you are really sure
807     * that your dynamic variable implementation can be considered a
808     * constant expression then you may override this method and return
809     * @c true instead. Note that when you return @c true here, your
810     * dynamic variable will really just be executed once; and exectly
811     * already when the script is loaded!
812     *
813     * As an example you may implement a "constant" built-in dynamic
814     * variable that checks for a certain operating system feature and
815     * returns the result of that OS feature check as content (value) of
816     * this dynamic variable. Since the respective OS feature might become
817     * available/unavailable after OS updates, software migration, etc. the
818     * OS feature check should at least be performed once each time the
819     * application is launched. And since the OS feature check might take a
820     * certain amount of execution time, it might make sense to only
821     * perform the check if the respective variable name is actually
822     * referenced at all in the script to be loaded. Note that the dynamic
823     * variable will still be evaluated again though if the script is
824     * loaded again. So it is up to you to probably cache the result in the
825     * implementation of your dynamic variable.
826     *
827     * On doubt, please rather consider to use a constant built-in script
828     * variable instead of implementing a "constant" dynamic variable, due
829     * to the runtime overhead a dynamic variable may cause.
830     *
831     * @see isAssignable()
832     */
833 schoenebeck 2945 bool isConstExpr() const OVERRIDE { return false; }
834 schoenebeck 2942
835     /**
836     * In case this dynamic variable is assignable, the new value (content)
837     * to be assigned to this dynamic variable.
838     *
839     * By default this method does nothing. Override and implement this
840     * method in your subclass in case your dynamic variable allows to
841     * assign a new value by script.
842     *
843     * @param expr - new value to be assigned to this variable
844     */
845 schoenebeck 2945 void assignExpr(VMExpr* expr) OVERRIDE {}
846 schoenebeck 3034
847     virtual ~VMDynVar() {}
848 schoenebeck 2942 };
849    
850     /** @brief Dynamically executed variable (of integer data type).
851     *
852     * This is the base class for all built-in integer script variables whose
853     * variable content needs to be provided dynamically by executable native
854     * code on each script variable access.
855     */
856     class VMDynIntVar : virtual public VMDynVar, virtual public VMIntExpr {
857     public:
858     };
859    
860     /** @brief Dynamically executed variable (of string data type).
861     *
862     * This is the base class for all built-in string script variables whose
863     * variable content needs to be provided dynamically by executable native
864     * code on each script variable access.
865     */
866     class VMDynStringVar : virtual public VMDynVar, virtual public VMStringExpr {
867     public:
868     };
869    
870 schoenebeck 3073 /** @brief Dynamically executed variable (of integer array data type).
871     *
872     * This is the base class for all built-in integer array script variables
873     * whose variable content needs to be provided dynamically by executable
874     * native code on each script variable access.
875     */
876     class VMDynIntArrayVar : virtual public VMDynVar, virtual public VMIntArrayExpr {
877     public:
878     };
879    
880 schoenebeck 2612 /** @brief Provider for built-in script functions and variables.
881     *
882 schoenebeck 2727 * Abstract base class defining the high-level interface for all classes
883     * which add and implement built-in script functions and built-in script
884     * variables.
885 schoenebeck 2612 */
886 schoenebeck 2581 class VMFunctionProvider {
887     public:
888 schoenebeck 2612 /**
889     * Returns pointer to the built-in function with the given function
890 schoenebeck 2727 * @a name, or NULL if there is no built-in function with that function
891     * name.
892 schoenebeck 2612 *
893 schoenebeck 2727 * @param name - function name (i.e. "wait" or "message" or "exit", etc.)
894 schoenebeck 2612 */
895 schoenebeck 2581 virtual VMFunction* functionByName(const String& name) = 0;
896 schoenebeck 2612
897     /**
898 schoenebeck 3311 * Returns @c true if the passed built-in function is disabled and
899     * should be ignored by the parser. This method is called by the
900     * parser on preprocessor level for each built-in function call within
901     * a script. Accordingly if this method returns @c true, then the
902     * respective function call is completely filtered out on preprocessor
903     * level, so that built-in function won't make into the result virtual
904     * machine representation, nor would expressions of arguments passed to
905     * that built-in function call be evaluated, nor would any check
906     * regarding correct usage of the built-in function be performed.
907     * In other words: a disabled function call ends up as a comment block.
908     *
909     * @param fn - built-in function to be checked
910     * @param ctx - parser context at the position where the built-in
911     * function call is located within the script
912     */
913     virtual bool isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) = 0;
914    
915     /**
916 schoenebeck 2612 * Returns a variable name indexed map of all built-in script variables
917 schoenebeck 2727 * which point to native "int" scalar (usually 32 bit) variables.
918 schoenebeck 2612 */
919 schoenebeck 3557 virtual std::map<String,VMIntPtr*> builtInIntVariables() = 0;
920 schoenebeck 2612
921     /**
922 schoenebeck 2727 * Returns a variable name indexed map of all built-in script integer
923     * array variables with array element type "int8_t" (8 bit).
924 schoenebeck 2612 */
925 schoenebeck 2594 virtual std::map<String,VMInt8Array*> builtInIntArrayVariables() = 0;
926 schoenebeck 2612
927     /**
928     * Returns a variable name indexed map of all built-in constant script
929     * variables, which never change their value at runtime.
930     */
931 schoenebeck 3557 virtual std::map<String,vmint> builtInConstIntVariables() = 0;
932 schoenebeck 2942
933     /**
934     * Returns a variable name indexed map of all built-in dynamic variables,
935     * which are not simply data stores, rather each one of them executes
936     * natively to provide or alter the respective script variable data.
937     */
938     virtual std::map<String,VMDynVar*> builtInDynamicVariables() = 0;
939 schoenebeck 2581 };
940    
941 schoenebeck 2594 /** @brief Execution state of a virtual machine.
942     *
943     * An instance of this abstract base class represents exactly one execution
944     * state of a virtual machine. This encompasses most notably the VM
945 schoenebeck 2612 * execution stack, and VM polyphonic variables. It does not contain global
946 schoenebeck 2727 * variables. Global variables are contained in the VMParserContext object.
947 schoenebeck 2612 * You might see a VMExecContext object as one virtual thread of the virtual
948     * machine.
949 schoenebeck 2594 *
950 schoenebeck 2612 * In contrast to a VMParserContext, a VMExecContext is not tied to a
951     * ScriptVM instance. Thus you can use a VMExecContext with different
952     * ScriptVM instances, however not concurrently at the same time.
953     *
954 schoenebeck 2594 * @see VMParserContext
955     */
956 schoenebeck 2581 class VMExecContext {
957     public:
958     virtual ~VMExecContext() {}
959 schoenebeck 2727
960     /**
961     * In case the script was suspended for some reason, this method returns
962     * the amount of microseconds before the script shall continue its
963     * execution. Note that the virtual machine itself does never put its
964     * own execution thread(s) to sleep. So the respective class (i.e. sampler
965     * engine) which is using the virtual machine classes here, must take
966     * care by itself about taking time stamps, determining the script
967     * handlers that shall be put aside for the requested amount of
968 schoenebeck 2871 * microseconds, indicated by this method by comparing the time stamps in
969 schoenebeck 2727 * real-time, and to continue passing the respective handler to
970     * ScriptVM::exec() as soon as its suspension exceeded, etc. Or in other
971     * words: all classes in this directory never have an idea what time it
972     * is.
973     *
974     * You should check the return value of ScriptVM::exec() to determine
975     * whether the script was actually suspended before calling this method
976     * here.
977     *
978     * @see ScriptVM::exec()
979     */
980 schoenebeck 3557 virtual vmint suspensionTimeMicroseconds() const = 0;
981 schoenebeck 3207
982     /**
983     * Causes all polyphonic variables to be reset to zero values. A
984     * polyphonic variable is expected to be zero when entering a new event
985     * handler instance. As an exception the values of polyphonic variables
986     * shall only be preserved from an note event handler instance to its
987     * correspending specific release handler instance. So in the latter
988     * case the script author may pass custom data from the note handler to
989     * the release handler, but only for the same specific note!
990     */
991     virtual void resetPolyphonicData() = 0;
992 schoenebeck 3221
993     /**
994     * Returns amount of virtual machine instructions which have been
995     * performed the last time when this execution context was executing a
996     * script. So in case you need the overall amount of instructions
997     * instead, then you need to add them by yourself after each
998     * ScriptVM::exec() call.
999     */
1000     virtual size_t instructionsPerformed() const = 0;
1001 schoenebeck 3277
1002     /**
1003     * Sends a signal to this script execution instance to abort its script
1004     * execution as soon as possible. This method is called i.e. when one
1005     * script execution instance intends to stop another script execution
1006     * instance.
1007     */
1008     virtual void signalAbort() = 0;
1009 schoenebeck 3293
1010     /**
1011     * Copies the current entire execution state from this object to the
1012     * given object. So this can be used to "fork" a new script thread which
1013     * then may run independently with its own polyphonic data for instance.
1014     */
1015     virtual void forkTo(VMExecContext* ectx) const = 0;
1016 schoenebeck 3551
1017     /**
1018     * In case the script called the built-in exit() function and passed a
1019     * value as argument to the exit() function, then this method returns
1020     * the value that had been passed as argument to the exit() function.
1021     * Otherwise if the exit() function has not been called by the script
1022     * or no argument had been passed to the exit() function, then this
1023     * method returns NULL instead.
1024     *
1025     * Currently this is only used for automated test cases against the
1026     * script engine, which return some kind of value in the individual
1027     * test case scripts to check their behaviour in automated way. There
1028     * is no purpose for this mechanism in production use. Accordingly this
1029     * exit result value is @b always completely ignored by the sampler
1030     * engines.
1031     *
1032     * Officially the built-in exit() function does not expect any arguments
1033     * to be passed to its function call, and by default this feature is
1034     * hence disabled and will yield in a parser error unless
1035     * ScriptVM::setExitResultEnabled() was explicitly set.
1036     *
1037     * @see ScriptVM::setExitResultEnabled()
1038     */
1039     virtual VMExpr* exitResult() = 0;
1040 schoenebeck 2581 };
1041    
1042 schoenebeck 2645 /** @brief Script callback for a certain event.
1043     *
1044     * Represents a script callback for a certain event, i.e.
1045 schoenebeck 2727 * "on note ... end on" code block.
1046 schoenebeck 2645 */
1047 schoenebeck 2581 class VMEventHandler {
1048     public:
1049 schoenebeck 2645 /**
1050 schoenebeck 2879 * Type of this event handler, which identifies its purpose. For example
1051     * for a "on note ... end on" script callback block,
1052     * @c VM_EVENT_HANDLER_NOTE would be returned here.
1053     */
1054     virtual VMEventHandlerType_t eventHandlerType() const = 0;
1055    
1056     /**
1057 schoenebeck 2645 * Name of the event handler which identifies its purpose. For example
1058     * for a "on note ... end on" script callback block, the name "note"
1059     * would be returned here.
1060     */
1061 schoenebeck 2581 virtual String eventHandlerName() const = 0;
1062 schoenebeck 2645
1063     /**
1064     * Whether or not the event handler makes any use of so called
1065     * "polyphonic" variables.
1066     */
1067     virtual bool isPolyphonic() const = 0;
1068 schoenebeck 2581 };
1069    
1070 schoenebeck 2727 /**
1071 schoenebeck 3285 * Reflects the precise position and span of a specific code block within
1072     * a script. This is currently only used for the locations of commented
1073 schoenebeck 3292 * code blocks due to preprocessor statements, and for parser errors and
1074     * parser warnings.
1075 schoenebeck 3285 *
1076 schoenebeck 3292 * @see ParserIssue for code locations of parser errors and parser warnings
1077     *
1078     * @see VMParserContext::preprocessorComments() for locations of code which
1079     * have been filtered out by preprocessor statements
1080 schoenebeck 3285 */
1081     struct CodeBlock {
1082     int firstLine; ///< The first line number of this code block within the script (indexed with 1 being the very first line).
1083     int lastLine; ///< The last line number of this code block within the script.
1084     int firstColumn; ///< The first column of this code block within the script (indexed with 1 being the very first column).
1085     int lastColumn; ///< The last column of this code block within the script.
1086     };
1087    
1088     /**
1089 schoenebeck 2727 * Encapsulates a noteworty parser issue. This encompasses the type of the
1090     * issue (either a parser error or parser warning), a human readable
1091     * explanation text of the error or warning and the location of the
1092     * encountered parser issue within the script.
1093 schoenebeck 3012 *
1094     * @see VMSourceToken for processing syntax highlighting instead.
1095 schoenebeck 2727 */
1096 schoenebeck 3292 struct ParserIssue : CodeBlock {
1097 schoenebeck 2727 String txt; ///< Human readable explanation text of the parser issue.
1098     ParserIssueType_t type; ///< Whether this issue is either a parser error or just a parser warning.
1099 schoenebeck 2581
1100 schoenebeck 2727 /**
1101     * Print this issue out to the console (stdio).
1102     */
1103 schoenebeck 2581 inline void dump() {
1104     switch (type) {
1105     case PARSER_ERROR:
1106 schoenebeck 2889 printf("[ERROR] line %d, column %d: %s\n", firstLine, firstColumn, txt.c_str());
1107 schoenebeck 2581 break;
1108     case PARSER_WARNING:
1109 schoenebeck 2889 printf("[Warning] line %d, column %d: %s\n", firstLine, firstColumn, txt.c_str());
1110 schoenebeck 2581 break;
1111     }
1112     }
1113 schoenebeck 2727
1114     /**
1115     * Returns true if this issue is a parser error. In this case the parsed
1116     * script may not be executed!
1117     */
1118 schoenebeck 2581 inline bool isErr() const { return type == PARSER_ERROR; }
1119 schoenebeck 2727
1120     /**
1121     * Returns true if this issue is just a parser warning. A parsed script
1122     * that only raises warnings may be executed if desired, however the
1123     * script may not behave exactly as intended by the script author.
1124     */
1125 schoenebeck 2581 inline bool isWrn() const { return type == PARSER_WARNING; }
1126     };
1127    
1128 schoenebeck 2727 /**
1129     * Convenience function used for converting an ExprType_t constant to a
1130     * string, i.e. for generating error message by the parser.
1131     */
1132 schoenebeck 2581 inline String typeStr(const ExprType_t& type) {
1133     switch (type) {
1134     case EMPTY_EXPR: return "empty";
1135     case INT_EXPR: return "integer";
1136     case INT_ARR_EXPR: return "integer array";
1137     case STRING_EXPR: return "string";
1138     case STRING_ARR_EXPR: return "string array";
1139     }
1140     return "invalid";
1141     }
1142    
1143 schoenebeck 2594 /** @brief Virtual machine representation of a script.
1144     *
1145     * An instance of this abstract base class represents a parsed script,
1146 schoenebeck 2727 * translated into a virtual machine tree. You should first check if there
1147     * were any parser errors. If there were any parser errors, you should
1148     * refrain from executing the virtual machine. Otherwise if there were no
1149     * parser errors (i.e. only warnings), then you might access one of the
1150     * script's event handlers by i.e. calling eventHandlerByName() and pass the
1151     * respective event handler to the ScriptVM class (or to one of the ScriptVM
1152 schoenebeck 2594 * descendants) for execution.
1153     *
1154 schoenebeck 2727 * @see VMExecContext, ScriptVM
1155 schoenebeck 2594 */
1156 schoenebeck 2588 class VMParserContext {
1157     public:
1158     virtual ~VMParserContext() {}
1159 schoenebeck 2727
1160     /**
1161     * Returns all noteworthy issues encountered when the script was parsed.
1162     * These are parser errors and parser warnings.
1163     */
1164 schoenebeck 2588 virtual std::vector<ParserIssue> issues() const = 0;
1165 schoenebeck 2727
1166     /**
1167     * Same as issues(), but this method only returns parser errors.
1168     */
1169 schoenebeck 2588 virtual std::vector<ParserIssue> errors() const = 0;
1170 schoenebeck 2727
1171     /**
1172     * Same as issues(), but this method only returns parser warnings.
1173     */
1174 schoenebeck 2588 virtual std::vector<ParserIssue> warnings() const = 0;
1175 schoenebeck 2727
1176     /**
1177 schoenebeck 3285 * Returns all code blocks of the script which were filtered out by the
1178     * preprocessor.
1179     */
1180     virtual std::vector<CodeBlock> preprocessorComments() const = 0;
1181    
1182     /**
1183 schoenebeck 2727 * Returns the translated virtual machine representation of an event
1184     * handler block (i.e. "on note ... end on" code block) within the
1185     * parsed script. This translated representation of the event handler
1186     * can be executed by the virtual machine.
1187     *
1188     * @param index - index of the event handler within the script
1189     */
1190 schoenebeck 2588 virtual VMEventHandler* eventHandler(uint index) = 0;
1191 schoenebeck 2727
1192     /**
1193     * Same as eventHandler(), but this method returns the event handler by
1194     * its name. So for a "on note ... end on" code block of the parsed
1195     * script you would pass "note" for argument @a name here.
1196     *
1197     * @param name - name of the event handler (i.e. "init", "note",
1198     * "controller", "release")
1199     */
1200 schoenebeck 2588 virtual VMEventHandler* eventHandlerByName(const String& name) = 0;
1201     };
1202    
1203 schoenebeck 2885 class SourceToken;
1204    
1205     /** @brief Recognized token of a script's source code.
1206     *
1207     * Represents one recognized token of a script's source code, for example
1208     * a keyword, variable name, etc. and it provides further informations about
1209     * that particular token, i.e. the precise location (line and column) of the
1210     * token within the original script's source code.
1211     *
1212     * This class is not actually used by the sampler itself. It is rather
1213     * provided for external script editor applications. Primary purpose of
1214     * this class is syntax highlighting for external script editors.
1215 schoenebeck 3012 *
1216     * @see ParserIssue for processing compile errors and warnings instead.
1217 schoenebeck 2885 */
1218     class VMSourceToken {
1219     public:
1220     VMSourceToken();
1221     VMSourceToken(SourceToken* ct);
1222     VMSourceToken(const VMSourceToken& other);
1223     virtual ~VMSourceToken();
1224    
1225     // original text of this token as it is in the script's source code
1226     String text() const;
1227    
1228     // position of token in script
1229 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.
1230     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().
1231 schoenebeck 2885
1232     // base types
1233 schoenebeck 3012 bool isEOF() const; ///< Returns true in case this source token represents the end of the source code file.
1234     bool isNewLine() const; ///< Returns true in case this source token represents a line feed character (i.e. "\n" on Unix systems).
1235     bool isKeyword() const; ///< Returns true in case this source token represents a language keyword (i.e. "while", "function", "declare", "on", etc.).
1236     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.
1237     bool isIdentifier() const; ///< Returns true in case this source token represents an identifier, which currently always means a function name.
1238     bool isNumberLiteral() const; ///< Returns true in case this source token represents a number literal (i.e. 123).
1239     bool isStringLiteral() const; ///< Returns true in case this source token represents a string literal (i.e. "Some text").
1240     bool isComment() const; ///< Returns true in case this source token represents a source code comment.
1241     bool isPreprocessor() const; ///< Returns true in case this source token represents a preprocessor statement.
1242     bool isOther() const; ///< Returns true in case this source token represents anything else not covered by the token types mentioned above.
1243 schoenebeck 2885
1244     // extended types
1245 schoenebeck 3012 bool isIntegerVariable() const; ///< Returns true in case this source token represents an integer variable name (i.e. "$someIntVariable").
1246     bool isStringVariable() const; ///< Returns true in case this source token represents an string variable name (i.e. "\@someStringVariable").
1247     bool isArrayVariable() const; ///< Returns true in case this source token represents an array variable name (i.e. "%someArryVariable").
1248     bool isEventHandlerName() const; ///< Returns true in case this source token represents an event handler name (i.e. "note", "release", "controller").
1249 schoenebeck 2885
1250     VMSourceToken& operator=(const VMSourceToken& other);
1251    
1252     private:
1253     SourceToken* m_token;
1254     };
1255    
1256 schoenebeck 2581 } // namespace LinuxSampler
1257    
1258     #endif // LS_INSTR_SCRIPT_PARSER_COMMON_H

  ViewVC Help
Powered by ViewVC