/[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 2727 - (hide annotations) (download) (as text)
Tue Mar 31 17:46:11 2015 UTC (9 years ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 32236 byte(s)
- Just added API doc comments to Script VM code.

1 schoenebeck 2581 /*
2 schoenebeck 2727 * Copyright (c) 2014-2015 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     // (located at other source directories).
13    
14     #ifndef LS_INSTR_SCRIPT_PARSER_COMMON_H
15     #define LS_INSTR_SCRIPT_PARSER_COMMON_H
16    
17     #include "../common/global.h"
18 schoenebeck 2588 #include <vector>
19 schoenebeck 2594 #include <map>
20     #include <stddef.h> // offsetof()
21 schoenebeck 2581
22     namespace LinuxSampler {
23 schoenebeck 2727
24     /**
25     * Identifies the type of a noteworthy issue identified by the script
26     * parser. That's either a parser error or parser warning.
27     */
28 schoenebeck 2581 enum ParserIssueType_t {
29 schoenebeck 2727 PARSER_ERROR, ///< Script parser encountered an error, the script cannot be executed.
30     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.
31 schoenebeck 2581 };
32    
33 schoenebeck 2727 /** @brief Expression's data type.
34     *
35     * Identifies to which data type an expression within a script evaluates to.
36     * This can for example reflect the data type of script function arguments,
37     * script function return values, but also the resulting data type of some
38     * mathematical formula within a script.
39     */
40 schoenebeck 2581 enum ExprType_t {
41 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)
42     INT_EXPR, ///< integer (scalar) expression
43     INT_ARR_EXPR, ///< integer array expression
44     STRING_EXPR, ///< string expression
45     STRING_ARR_EXPR, ///< string array expression
46 schoenebeck 2581 };
47    
48 schoenebeck 2727 /** @brief Result flags of a script statement or script function call.
49     *
50     * A set of bit flags which provide informations about the success or
51     * failure of a statement within a script. That's also especially used for
52     * providing informations about success / failure of a call to a built-in
53     * script function. The virtual machine evaluates these flags during runtime
54     * to decide whether it should i.e. stop or suspend execution of a script.
55     *
56     * Since these are bit flags, these constants are bitwise combined.
57     */
58 schoenebeck 2581 enum StmtFlags_t {
59     STMT_SUCCESS = 0, ///< Function / statement was executed successfully, no error occurred.
60 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).
61     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
62     STMT_ERROR_OCCURRED = (1<<2), ///< VM stopped execution due to some script runtime error that occurred
63 schoenebeck 2581 };
64    
65 schoenebeck 2727 /** @brief Virtual machine execution status.
66     *
67     * A set of bit flags which reflect the current overall execution status of
68     * the virtual machine concerning a certain script execution instance.
69     *
70     * Since these are bit flags, these constants are bitwise combined.
71     */
72 schoenebeck 2581 enum VMExecStatus_t {
73 schoenebeck 2727 VM_EXEC_NOT_RUNNING = 0, ///< Script is currently not executed by the VM.
74     VM_EXEC_RUNNING = 1, ///< The VM is currently executing the script.
75     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.
76     VM_EXEC_ERROR = (1<<2), ///< A runtime error occurred while executing the script (i.e. a call to some built-in script function failed).
77 schoenebeck 2581 };
78    
79 schoenebeck 2727 // just symbol prototyping
80 schoenebeck 2596 class VMIntExpr;
81     class VMStringExpr;
82 schoenebeck 2619 class VMIntArrayExpr;
83     class VMStringArrayExpr;
84 schoenebeck 2596
85 schoenebeck 2727 /** @brief Virtual machine expression
86     *
87     * This is the abstract base class for all expressions of scripts.
88     * Deriving classes must implement the abstract method exprType().
89     *
90     * An expression within a script is translated into one instance of this
91     * class. It allows a high level access for the virtual machine to evaluate
92     * and handle expressions appropriately during execution. Expressions are
93     * for example all kinds of formulas, function calls, statements or a
94     * combination of them. Most of them evaluate to some kind of value, which
95     * might be further processed as part of encompassing expressions to outer
96     * expression results and so forth.
97     */
98 schoenebeck 2581 class VMExpr {
99     public:
100 schoenebeck 2727 /**
101     * Identifies the data type to which the result of this expression
102     * evaluates to. This abstract method must be implemented by deriving
103     * classes.
104     */
105 schoenebeck 2581 virtual ExprType_t exprType() const = 0;
106 schoenebeck 2727
107     /**
108     * In case this expression is an integer expression, then this method
109     * returns a casted pointer to that VMIntExpr object. It returns NULL
110     * if this expression is not an integer expression.
111     *
112     * @b Note: type casting performed by this method is strict! That means
113     * if this expression is i.e. actually a string expression like "12",
114     * calling asInt() will @b not cast that numerical string expression to
115     * an integer expression 12 for you, instead this method will simply
116     * return NULL!
117     *
118     * @see exprType()
119     */
120 schoenebeck 2596 VMIntExpr* asInt() const;
121 schoenebeck 2727
122     /**
123     * In case this expression is a string expression, then this method
124     * returns a casted pointer to that VMStringExpr object. It returns NULL
125     * if this expression is not a string expression.
126     *
127     * @b Note: type casting performed by this method is strict! That means
128     * if this expression is i.e. actually an integer expression like 120,
129     * calling asString() will @b not cast that integer expression to a
130     * string expression "120" for you, instead this method will simply
131     * return NULL!
132     *
133     * @see exprType()
134     */
135 schoenebeck 2596 VMStringExpr* asString() const;
136 schoenebeck 2727
137     /**
138     * In case this expression is an integer array expression, then this
139     * method returns a casted pointer to that VMIntArrayExpr object. It
140     * returns NULL if this expression is not an integer array expression.
141     *
142     * @b Note: type casting performed by this method is strict! That means
143     * if this expression is i.e. an integer expression or a string
144     * expression, calling asIntArray() will @b not cast those scalar
145     * expressions to an array expression for you, instead this method will
146     * simply return NULL!
147     *
148     * @see exprType()
149     */
150 schoenebeck 2619 VMIntArrayExpr* asIntArray() const;
151 schoenebeck 2581 };
152    
153 schoenebeck 2727 /** @brief Virtual machine integer expression
154     *
155     * This is the abstract base class for all expressions inside scripts which
156     * evaluate to an integer (scalar) value. Deriving classes implement the
157     * abstract method evalInt() to return the actual integer result value of
158     * the expression.
159     */
160 schoenebeck 2581 class VMIntExpr : virtual public VMExpr {
161     public:
162 schoenebeck 2727 /**
163     * Returns the result of this expression as integer (scalar) value.
164     * This abstract method must be implemented by deriving classes.
165     */
166 schoenebeck 2581 virtual int evalInt() = 0;
167 schoenebeck 2727
168     /**
169     * Returns always INT_EXPR for instances of this class.
170     */
171     ExprType_t exprType() const OVERRIDE { return INT_EXPR; }
172 schoenebeck 2581 };
173    
174 schoenebeck 2727 /** @brief Virtual machine string expression
175     *
176     * This is the abstract base class for all expressions inside scripts which
177     * evaluate to a string value. Deriving classes implement the abstract
178     * method evalStr() to return the actual string result value of the
179     * expression.
180     */
181 schoenebeck 2581 class VMStringExpr : virtual public VMExpr {
182     public:
183 schoenebeck 2727 /**
184     * Returns the result of this expression as string value. This abstract
185     * method must be implemented by deriving classes.
186     */
187 schoenebeck 2581 virtual String evalStr() = 0;
188 schoenebeck 2727
189     /**
190     * Returns always STRING_EXPR for instances of this class.
191     */
192     ExprType_t exprType() const OVERRIDE { return STRING_EXPR; }
193 schoenebeck 2581 };
194    
195 schoenebeck 2727 /** @brief Virtual Machine Array Value Expression
196     *
197     * This is the abstract base class for all expressions inside scripts which
198     * evaluate to some kind of array value. Deriving classes implement the
199     * abstract method arraySize() to return the amount of elements within the
200     * array.
201     */
202 schoenebeck 2619 class VMArrayExpr : virtual public VMExpr {
203     public:
204 schoenebeck 2727 /**
205     * Returns amount of elements in this array. This abstract method must
206     * be implemented by deriving classes.
207     */
208 schoenebeck 2619 virtual int arraySize() const = 0;
209     };
210    
211 schoenebeck 2727 /** @brief Virtual Machine Integer Array Expression
212     *
213     * This is the abstract base class for all expressions inside scripts which
214     * evaluate to an array of integer values. Deriving classes implement the
215     * abstract methods arraySize(), evalIntElement() and assignIntElement() to
216     * access the individual integer array values.
217     */
218 schoenebeck 2619 class VMIntArrayExpr : virtual public VMArrayExpr {
219     public:
220 schoenebeck 2727 /**
221     * Returns the (scalar) integer value of the array element given by
222     * element index @a i.
223     *
224     * @param i - array element index (must be between 0 .. arraySize() - 1)
225     */
226 schoenebeck 2619 virtual int evalIntElement(uint i) = 0;
227 schoenebeck 2727
228     /**
229     * Changes the current value of an element (given by array element
230     * index @a i) of this integer array.
231     *
232     * @param i - array element index (must be between 0 .. arraySize() - 1)
233     * @param value - new integer scalar value to be assigned to that array element
234     */
235 schoenebeck 2619 virtual void assignIntElement(uint i, int value) = 0;
236 schoenebeck 2727
237     /**
238     * Returns always INT_ARR_EXPR for instances of this class.
239     */
240     ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; }
241 schoenebeck 2619 };
242    
243 schoenebeck 2727 /** @brief Arguments (parameters) for being passed to a built-in script function.
244     *
245     * An argument or a set of arguments passed to a script function are
246     * translated by the parser to an instance of this class. This abstract
247     * interface class is used by implementations of built-in functions to
248     * obtain the individual function argument values being passed to them at
249     * runtime.
250     */
251 schoenebeck 2581 class VMFnArgs {
252     public:
253 schoenebeck 2727 /**
254     * Returns the amount of arguments going to be passed to the script
255     * function.
256     */
257 schoenebeck 2581 virtual int argsCount() const = 0;
258 schoenebeck 2727
259     /**
260     * Returns the respective argument (requested by argument index @a i) of
261     * this set of arguments. This method is called by implementations of
262     * built-in script functions to obtain the value of each function
263     * argument passed to the function at runtime.
264     *
265     * @param i - function argument index (indexed from left to right)
266     */
267 schoenebeck 2581 virtual VMExpr* arg(int i) = 0;
268     };
269    
270 schoenebeck 2727 /** @brief Result value returned from a call to a built-in script function.
271     *
272     * Implementations of built-in script functions return an instance of this
273     * object to let the virtual machine obtain the result value of the function
274     * call, which might then be further processed by the virtual machine
275     * according to the script. It also provides informations about the success
276     * or failure of the function call.
277     */
278 schoenebeck 2581 class VMFnResult {
279     public:
280 schoenebeck 2727 /**
281     * Returns the result value of the function call, represented by a high
282     * level expression object.
283     */
284 schoenebeck 2581 virtual VMExpr* resultValue() = 0;
285 schoenebeck 2727
286     /**
287     * Provides detailed informations of the success / failure of the
288     * function call. The virtual machine is evaluating the flags returned
289     * here to decide whether it must abort or suspend execution of the
290     * script at this point.
291     */
292 schoenebeck 2581 virtual StmtFlags_t resultFlags() { return STMT_SUCCESS; }
293     };
294    
295 schoenebeck 2727 /** @brief Virtual machine built-in function.
296 schoenebeck 2612 *
297     * Abstract base class for built-in script functions, defining the interface
298 schoenebeck 2727 * for all built-in script function implementations. All built-in script
299     * functions are deriving from this abstract interface class in order to
300     * provide their functionality to the virtual machine with this unified
301     * interface.
302     *
303     * The methods of this interface class provide two purposes:
304     *
305     * 1. When a script is loaded, the script parser uses the methods of this
306     * interface to check whether the script author was calling the
307     * respective built-in script function in a correct way. For example
308     * the parser checks whether the required amount of parameters were
309     * passed to the function and whether the data types passed match the
310     * data types expected by the function. If not, loading the script will
311     * be aborted with a parser error, describing to the user (i.e. script
312     * author) the precise misusage of the respective function.
313     * 2. After the script was loaded successfully and the script is executed,
314     * the virtual machine calls the exec() method of the respective built-in
315     * function to provide the actual functionality of the built-in function
316     * call.
317 schoenebeck 2612 */
318 schoenebeck 2581 class VMFunction {
319     public:
320 schoenebeck 2612 /**
321     * Script data type of the function's return value. If the function does
322 schoenebeck 2727 * not return any value (void), then it returns EMPTY_EXPR here.
323 schoenebeck 2612 */
324 schoenebeck 2581 virtual ExprType_t returnType() = 0;
325 schoenebeck 2612
326     /**
327     * Minimum amount of function arguments this function accepts. If a
328     * script is calling this function with less arguments, the script
329     * parser will throw a parser error.
330     */
331 schoenebeck 2581 virtual int minRequiredArgs() const = 0;
332 schoenebeck 2612
333     /**
334     * Maximum amount of function arguments this functions accepts. If a
335     * script is calling this function with more arguments, the script
336     * parser will throw a parser error.
337     */
338 schoenebeck 2581 virtual int maxAllowedArgs() const = 0;
339 schoenebeck 2612
340     /**
341     * Script data type of the function's @c iArg 'th function argument.
342     * The information provided here is less strong than acceptsArgType().
343     * The parser will compare argument data types provided in scripts by
344     * calling cceptsArgType(). The return value of argType() is used by the
345     * parser instead to show an appropriate parser error which data type
346     * this function usually expects as "default" data type. Reason: a
347     * function may accept multiple data types for a certain function
348     * argument and would automatically cast the passed argument value in
349     * that case to the type it actually needs.
350     *
351     * @param iArg - index of the function argument in question
352 schoenebeck 2727 * (must be between 0 .. maxAllowedArgs() - 1)
353 schoenebeck 2612 */
354 schoenebeck 2581 virtual ExprType_t argType(int iArg) const = 0;
355 schoenebeck 2612
356     /**
357     * This function is called by the parser to check whether arguments
358     * passed in scripts to this function are accepted by this function. If
359     * a script calls this function with an argument's data type not
360 schoenebeck 2727 * accepted by this function, the parser will throw a parser error. On
361     * such errors the data type returned by argType() will be used to
362     * assemble an appropriate error message regarding the precise misusage
363     * of the built-in function.
364 schoenebeck 2612 *
365     * @param iArg - index of the function argument in question
366 schoenebeck 2727 * (must be between 0 .. maxAllowedArgs() - 1)
367 schoenebeck 2612 * @param type - script data type used for this function argument by
368     * currently parsed script
369 schoenebeck 2727 * @return true if the given data type would be accepted for the
370     * respective function argument by the function
371 schoenebeck 2612 */
372 schoenebeck 2581 virtual bool acceptsArgType(int iArg, ExprType_t type) const = 0;
373 schoenebeck 2612
374     /**
375 schoenebeck 2727 * Implements the actual function execution. This exec() method is
376     * called by the VM whenever this function implementation shall be
377     * executed at script runtime. This method blocks until the function
378     * call completed.
379 schoenebeck 2612 *
380     * @param args - function arguments for executing this built-in function
381 schoenebeck 2727 * @returns function's return value (if any) and general status
382     * informations (i.e. whether the function call caused a
383     * runtime error)
384 schoenebeck 2612 */
385 schoenebeck 2581 virtual VMFnResult* exec(VMFnArgs* args) = 0;
386 schoenebeck 2612
387     /**
388 schoenebeck 2727 * Convenience method for function implementations to show warning
389     * messages during actual execution of the built-in function.
390 schoenebeck 2612 *
391 schoenebeck 2727 * @param txt - runtime warning text to be shown to user
392 schoenebeck 2612 */
393 schoenebeck 2598 void wrnMsg(const String& txt);
394 schoenebeck 2612
395     /**
396 schoenebeck 2727 * Convenience method for function implementations to show error
397     * messages during actual execution of the built-in function.
398 schoenebeck 2612 *
399 schoenebeck 2727 * @param txt - runtime error text to be shown to user
400 schoenebeck 2612 */
401 schoenebeck 2598 void errMsg(const String& txt);
402 schoenebeck 2581 };
403    
404 schoenebeck 2727 /** @brief Virtual machine relative pointer.
405     *
406 schoenebeck 2594 * POD base of VMIntRelPtr and VMInt8RelPtr structures. Not intended to be
407     * used directly. Use VMIntRelPtr or VMInt8RelPtr instead.
408 schoenebeck 2727 *
409     * @see VMIntRelPtr, VMInt8RelPtr
410 schoenebeck 2594 */
411     struct VMRelPtr {
412     void** base; ///< Base pointer.
413 schoenebeck 2727 int offset; ///< Offset (in bytes) relative to base pointer.
414 schoenebeck 2594 };
415    
416     /** @brief Pointer to built-in VM integer variable (of C/C++ type int).
417     *
418 schoenebeck 2727 * Used for defining built-in 32 bit integer script variables.
419 schoenebeck 2594 *
420     * @b CAUTION: You may only use this class for pointing to C/C++ variables
421     * of type "int" (which on most systems is 32 bit in size). If the C/C++ int
422     * variable you want to reference is only 8 bit in size, then you @b must
423     * use VMInt8RelPtr instead!
424     *
425     * For efficiency reasons the actual native C/C++ int variable is referenced
426     * by two components here. The actual native int C/C++ variable in memory
427     * is dereferenced at VM run-time by taking the @c base pointer dereference
428     * and adding @c offset bytes. This has the advantage that for a large
429     * number of built-in int variables, only one (or few) base pointer need
430     * to be re-assigned before running a script, instead of updating each
431     * built-in variable each time before a script is executed.
432     *
433     * Refer to DECLARE_VMINT() for example code.
434     *
435     * @see VMInt8RelPtr, DECLARE_VMINT()
436     */
437     struct VMIntRelPtr : VMRelPtr {
438     VMIntRelPtr() {
439     base = NULL;
440     offset = 0;
441     }
442     VMIntRelPtr(const VMRelPtr& data) {
443     base = data.base;
444     offset = data.offset;
445     }
446     virtual int evalInt() { return *(int*)&(*(uint8_t**)base)[offset]; }
447     virtual void assign(int i) { *(int*)&(*(uint8_t**)base)[offset] = i; }
448     };
449    
450     /** @brief Pointer to built-in VM integer variable (of C/C++ type int8_t).
451     *
452 schoenebeck 2727 * Used for defining built-in 8 bit integer script variables.
453 schoenebeck 2594 *
454     * @b CAUTION: You may only use this class for pointing to C/C++ variables
455     * of type "int8_t" (8 bit integer). If the C/C++ int variable you want to
456     * reference is an "int" type (which is 32 bit on most systems), then you
457     * @b must use VMIntRelPtr instead!
458     *
459     * For efficiency reasons the actual native C/C++ int variable is referenced
460     * by two components here. The actual native int C/C++ variable in memory
461     * is dereferenced at VM run-time by taking the @c base pointer dereference
462     * and adding @c offset bytes. This has the advantage that for a large
463     * number of built-in int variables, only one (or few) base pointer need
464     * to be re-assigned before running a script, instead of updating each
465     * built-in variable each time before a script is executed.
466     *
467     * Refer to DECLARE_VMINT() for example code.
468     *
469     * @see VMIntRelPtr, DECLARE_VMINT()
470     */
471     struct VMInt8RelPtr : VMIntRelPtr {
472     VMInt8RelPtr() : VMIntRelPtr() {}
473     VMInt8RelPtr(const VMRelPtr& data) : VMIntRelPtr(data) {}
474     virtual int evalInt() OVERRIDE {
475     return *(uint8_t*)&(*(uint8_t**)base)[offset];
476     }
477     virtual void assign(int i) OVERRIDE {
478     *(uint8_t*)&(*(uint8_t**)base)[offset] = i;
479     }
480     };
481    
482     /**
483     * Convenience macro for initializing VMIntRelPtr and VMInt8RelPtr
484 schoenebeck 2727 * structures. Usage example:
485 schoenebeck 2594 * @code
486     * struct Foo {
487 schoenebeck 2727 * uint8_t a; // native representation of a built-in integer script variable
488     * int b; // native representation of another built-in integer script variable
489     * int c; // native representation of another built-in integer script variable
490     * uint8_t d; // native representation of another built-in integer script variable
491 schoenebeck 2594 * };
492     *
493 schoenebeck 2727 * // initializing the built-in script variables to some values
494     * Foo foo1 = (Foo) { 1, 2000, 3000, 4 };
495     * Foo foo2 = (Foo) { 5, 6000, 7000, 8 };
496 schoenebeck 2594 *
497     * Foo* pFoo;
498     *
499 schoenebeck 2727 * VMInt8RelPtr varA = DECLARE_VMINT(pFoo, class Foo, a);
500     * VMIntRelPtr varB = DECLARE_VMINT(pFoo, class Foo, b);
501     * VMIntRelPtr varC = DECLARE_VMINT(pFoo, class Foo, c);
502     * VMInt8RelPtr varD = DECLARE_VMINT(pFoo, class Foo, d);
503 schoenebeck 2594 *
504     * pFoo = &foo1;
505 schoenebeck 2727 * printf("%d\n", varA->evalInt()); // will print 1
506     * printf("%d\n", varB->evalInt()); // will print 2000
507     * printf("%d\n", varC->evalInt()); // will print 3000
508     * printf("%d\n", varD->evalInt()); // will print 4
509 schoenebeck 2594 *
510 schoenebeck 2727 * // same printf() code, just with pFoo pointer being changed ...
511     *
512 schoenebeck 2594 * pFoo = &foo2;
513 schoenebeck 2727 * printf("%d\n", varA->evalInt()); // will print 5
514     * printf("%d\n", varB->evalInt()); // will print 6000
515     * printf("%d\n", varC->evalInt()); // will print 7000
516     * printf("%d\n", varD->evalInt()); // will print 8
517 schoenebeck 2594 * @endcode
518 schoenebeck 2727 * As you can see above, by simply changing one single pointer, you can
519     * remap a huge bunch of built-in integer script variables to completely
520     * different native values/native variables. Which especially reduces code
521     * complexity inside the sampler engines which provide the actual script
522     * functionalities.
523 schoenebeck 2594 */
524     #define DECLARE_VMINT(basePtr, T_struct, T_member) ( \
525     (VMRelPtr) { \
526     (void**) &basePtr, \
527     offsetof(T_struct, T_member) \
528     } \
529     ) \
530    
531     /** @brief Built-in VM 8 bit integer array variable.
532     *
533 schoenebeck 2727 * Used for defining built-in integer array script variables (8 bit per
534     * array element). Currently there is no support for any other kind of array
535     * type. So all integer arrays of scripts use 8 bit data types.
536 schoenebeck 2594 */
537     struct VMInt8Array {
538     int8_t* data;
539     int size;
540    
541     VMInt8Array() : data(NULL), size(0) {}
542     };
543    
544 schoenebeck 2612 /** @brief Provider for built-in script functions and variables.
545     *
546 schoenebeck 2727 * Abstract base class defining the high-level interface for all classes
547     * which add and implement built-in script functions and built-in script
548     * variables.
549 schoenebeck 2612 */
550 schoenebeck 2581 class VMFunctionProvider {
551     public:
552 schoenebeck 2612 /**
553     * Returns pointer to the built-in function with the given function
554 schoenebeck 2727 * @a name, or NULL if there is no built-in function with that function
555     * name.
556 schoenebeck 2612 *
557 schoenebeck 2727 * @param name - function name (i.e. "wait" or "message" or "exit", etc.)
558 schoenebeck 2612 */
559 schoenebeck 2581 virtual VMFunction* functionByName(const String& name) = 0;
560 schoenebeck 2612
561     /**
562     * Returns a variable name indexed map of all built-in script variables
563 schoenebeck 2727 * which point to native "int" scalar (usually 32 bit) variables.
564 schoenebeck 2612 */
565 schoenebeck 2594 virtual std::map<String,VMIntRelPtr*> builtInIntVariables() = 0;
566 schoenebeck 2612
567     /**
568 schoenebeck 2727 * Returns a variable name indexed map of all built-in script integer
569     * array variables with array element type "int8_t" (8 bit).
570 schoenebeck 2612 */
571 schoenebeck 2594 virtual std::map<String,VMInt8Array*> builtInIntArrayVariables() = 0;
572 schoenebeck 2612
573     /**
574     * Returns a variable name indexed map of all built-in constant script
575     * variables, which never change their value at runtime.
576     */
577 schoenebeck 2594 virtual std::map<String,int> builtInConstIntVariables() = 0;
578 schoenebeck 2581 };
579    
580 schoenebeck 2594 /** @brief Execution state of a virtual machine.
581     *
582     * An instance of this abstract base class represents exactly one execution
583     * state of a virtual machine. This encompasses most notably the VM
584 schoenebeck 2612 * execution stack, and VM polyphonic variables. It does not contain global
585 schoenebeck 2727 * variables. Global variables are contained in the VMParserContext object.
586 schoenebeck 2612 * You might see a VMExecContext object as one virtual thread of the virtual
587     * machine.
588 schoenebeck 2594 *
589 schoenebeck 2612 * In contrast to a VMParserContext, a VMExecContext is not tied to a
590     * ScriptVM instance. Thus you can use a VMExecContext with different
591     * ScriptVM instances, however not concurrently at the same time.
592     *
593 schoenebeck 2594 * @see VMParserContext
594     */
595 schoenebeck 2581 class VMExecContext {
596     public:
597     virtual ~VMExecContext() {}
598 schoenebeck 2727
599     /**
600     * In case the script was suspended for some reason, this method returns
601     * the amount of microseconds before the script shall continue its
602     * execution. Note that the virtual machine itself does never put its
603     * own execution thread(s) to sleep. So the respective class (i.e. sampler
604     * engine) which is using the virtual machine classes here, must take
605     * care by itself about taking time stamps, determining the script
606     * handlers that shall be put aside for the requested amount of
607     * microseconds indicated by this method by comparing the time stamps in
608     * real-time, and to continue passing the respective handler to
609     * ScriptVM::exec() as soon as its suspension exceeded, etc. Or in other
610     * words: all classes in this directory never have an idea what time it
611     * is.
612     *
613     * You should check the return value of ScriptVM::exec() to determine
614     * whether the script was actually suspended before calling this method
615     * here.
616     *
617     * @see ScriptVM::exec()
618     */
619 schoenebeck 2581 virtual int suspensionTimeMicroseconds() const = 0;
620     };
621    
622 schoenebeck 2645 /** @brief Script callback for a certain event.
623     *
624     * Represents a script callback for a certain event, i.e.
625 schoenebeck 2727 * "on note ... end on" code block.
626 schoenebeck 2645 */
627 schoenebeck 2581 class VMEventHandler {
628     public:
629 schoenebeck 2645 /**
630     * Name of the event handler which identifies its purpose. For example
631     * for a "on note ... end on" script callback block, the name "note"
632     * would be returned here.
633     */
634 schoenebeck 2581 virtual String eventHandlerName() const = 0;
635 schoenebeck 2645
636     /**
637     * Whether or not the event handler makes any use of so called
638     * "polyphonic" variables.
639     */
640     virtual bool isPolyphonic() const = 0;
641 schoenebeck 2581 };
642    
643 schoenebeck 2727 /**
644     * Encapsulates a noteworty parser issue. This encompasses the type of the
645     * issue (either a parser error or parser warning), a human readable
646     * explanation text of the error or warning and the location of the
647     * encountered parser issue within the script.
648     */
649 schoenebeck 2581 struct ParserIssue {
650 schoenebeck 2727 String txt; ///< Human readable explanation text of the parser issue.
651     int line; ///< Line number within the script where this issue was encountered.
652     ParserIssueType_t type; ///< Whether this issue is either a parser error or just a parser warning.
653 schoenebeck 2581
654 schoenebeck 2727 /**
655     * Print this issue out to the console (stdio).
656     */
657 schoenebeck 2581 inline void dump() {
658     switch (type) {
659     case PARSER_ERROR:
660     printf("[ERROR] line %d: %s\n", line, txt.c_str());
661     break;
662     case PARSER_WARNING:
663     printf("[Warning] line %d: %s\n", line, txt.c_str());
664     break;
665     }
666     }
667 schoenebeck 2727
668     /**
669     * Returns true if this issue is a parser error. In this case the parsed
670     * script may not be executed!
671     */
672 schoenebeck 2581 inline bool isErr() const { return type == PARSER_ERROR; }
673 schoenebeck 2727
674     /**
675     * Returns true if this issue is just a parser warning. A parsed script
676     * that only raises warnings may be executed if desired, however the
677     * script may not behave exactly as intended by the script author.
678     */
679 schoenebeck 2581 inline bool isWrn() const { return type == PARSER_WARNING; }
680     };
681    
682 schoenebeck 2727 /**
683     * Convenience function used for converting an ExprType_t constant to a
684     * string, i.e. for generating error message by the parser.
685     */
686 schoenebeck 2581 inline String typeStr(const ExprType_t& type) {
687     switch (type) {
688     case EMPTY_EXPR: return "empty";
689     case INT_EXPR: return "integer";
690     case INT_ARR_EXPR: return "integer array";
691     case STRING_EXPR: return "string";
692     case STRING_ARR_EXPR: return "string array";
693     }
694     return "invalid";
695     }
696    
697 schoenebeck 2594 /** @brief Virtual machine representation of a script.
698     *
699     * An instance of this abstract base class represents a parsed script,
700 schoenebeck 2727 * translated into a virtual machine tree. You should first check if there
701     * were any parser errors. If there were any parser errors, you should
702     * refrain from executing the virtual machine. Otherwise if there were no
703     * parser errors (i.e. only warnings), then you might access one of the
704     * script's event handlers by i.e. calling eventHandlerByName() and pass the
705     * respective event handler to the ScriptVM class (or to one of the ScriptVM
706 schoenebeck 2594 * descendants) for execution.
707     *
708 schoenebeck 2727 * @see VMExecContext, ScriptVM
709 schoenebeck 2594 */
710 schoenebeck 2588 class VMParserContext {
711     public:
712     virtual ~VMParserContext() {}
713 schoenebeck 2727
714     /**
715     * Returns all noteworthy issues encountered when the script was parsed.
716     * These are parser errors and parser warnings.
717     */
718 schoenebeck 2588 virtual std::vector<ParserIssue> issues() const = 0;
719 schoenebeck 2727
720     /**
721     * Same as issues(), but this method only returns parser errors.
722     */
723 schoenebeck 2588 virtual std::vector<ParserIssue> errors() const = 0;
724 schoenebeck 2727
725     /**
726     * Same as issues(), but this method only returns parser warnings.
727     */
728 schoenebeck 2588 virtual std::vector<ParserIssue> warnings() const = 0;
729 schoenebeck 2727
730     /**
731     * Returns the translated virtual machine representation of an event
732     * handler block (i.e. "on note ... end on" code block) within the
733     * parsed script. This translated representation of the event handler
734     * can be executed by the virtual machine.
735     *
736     * @param index - index of the event handler within the script
737     */
738 schoenebeck 2588 virtual VMEventHandler* eventHandler(uint index) = 0;
739 schoenebeck 2727
740     /**
741     * Same as eventHandler(), but this method returns the event handler by
742     * its name. So for a "on note ... end on" code block of the parsed
743     * script you would pass "note" for argument @a name here.
744     *
745     * @param name - name of the event handler (i.e. "init", "note",
746     * "controller", "release")
747     */
748 schoenebeck 2588 virtual VMEventHandler* eventHandlerByName(const String& name) = 0;
749     };
750    
751 schoenebeck 2581 } // namespace LinuxSampler
752    
753     #endif // LS_INSTR_SCRIPT_PARSER_COMMON_H

  ViewVC Help
Powered by ViewVC