--- linuxsampler/trunk/src/scriptvm/common.h 2016/04/19 14:07:53 2879 +++ linuxsampler/trunk/src/scriptvm/common.h 2017/06/22 10:45:38 3285 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2016 Christian Schoenebeck + * Copyright (c) 2014-2017 Christian Schoenebeck * * http://www.linuxsampler.org * @@ -157,9 +157,47 @@ * expressions to an array expression for you, instead this method will * simply return NULL! * + * @b Note: this method is currently, and in contrast to its other + * counter parts, declared as virtual method. Some deriving classes are + * currently using this to override this default implementation in order + * to implement an "evaluate now as integer array" behavior. This has + * efficiency reasons, however this also currently makes this part of + * the API less clean and should thus be addressed in future with + * appropriate changes to the API. + * * @see exprType() */ - VMIntArrayExpr* asIntArray() const; + virtual VMIntArrayExpr* asIntArray() const; + + /** + * Returns true in case this expression can be considered to be a + * constant expression. A constant expression will retain the same + * value throughout the entire life time of a script and the + * expression's constant value may be evaluated already at script + * parse time, which may result in performance benefits during script + * runtime. + * + * @b NOTE: A constant expression is per se always also non modifyable. + * But a non modifyable expression may not necessarily be a constant + * expression! + * + * @see isModifyable() + */ + virtual bool isConstExpr() const = 0; + + /** + * Returns true in case this expression is allowed to be modified. + * If this method returns @c false then this expression must be handled + * as read-only expression, which means that assigning a new value to it + * is either not possible or not allowed. + * + * @b NOTE: A constant expression is per se always also non modifyable. + * But a non modifyable expression may not necessarily be a constant + * expression! + * + * @see isConstExpr() + */ + bool isModifyable() const; }; /** @brief Virtual machine integer expression @@ -366,7 +404,7 @@ virtual ExprType_t argType(int iArg) const = 0; /** - * This function is called by the parser to check whether arguments + * This method is called by the parser to check whether arguments * passed in scripts to this function are accepted by this function. If * a script calls this function with an argument's data type not * accepted by this function, the parser will throw a parser error. On @@ -384,6 +422,21 @@ virtual bool acceptsArgType(int iArg, ExprType_t type) const = 0; /** + * This method is called by the parser to check whether some arguments + * (and if yes which ones) passed to this script function will be + * modified by this script function. Most script functions simply use + * their arguments as inputs, that is they only read the argument's + * values. However some script function may also use passed + * argument(s) as output variables. In this case the function + * implementation must return @c true for the respective argument + * index here. + * + * @param iArg - index of the function argument in question + * (must be between 0 .. maxAllowedArgs() - 1) + */ + virtual bool modifiesArg(int iArg) const = 0; + + /** * Implements the actual function execution. This exec() method is * called by the VM whenever this function implementation shall be * executed at script runtime. This method blocks until the function @@ -423,6 +476,7 @@ struct VMRelPtr { void** base; ///< Base pointer. int offset; ///< Offset (in bytes) relative to base pointer. + bool readonly; ///< Whether the pointed data may be modified or just be read. }; /** @brief Pointer to built-in VM integer variable (of C/C++ type int). @@ -450,10 +504,12 @@ VMIntRelPtr() { base = NULL; offset = 0; + readonly = false; } VMIntRelPtr(const VMRelPtr& data) { base = data.base; offset = data.offset; + readonly = false; } virtual int evalInt() { return *(int*)&(*(uint8_t**)base)[offset]; } virtual void assign(int i) { *(int*)&(*(uint8_t**)base)[offset] = i; } @@ -491,6 +547,17 @@ } }; + #if HAVE_CXX_EMBEDDED_PRAGMA_DIAGNOSTICS + # define COMPILER_DISABLE_OFFSETOF_WARNING \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Winvalid-offsetof\"") + # define COMPILER_RESTORE_OFFSETOF_WARNING \ + _Pragma("GCC diagnostic pop") + #else + # define COMPILER_DISABLE_OFFSETOF_WARNING + # define COMPILER_RESTORE_OFFSETOF_WARNING + #endif + /** * Convenience macro for initializing VMIntRelPtr and VMInt8RelPtr * structures. Usage example: @@ -533,12 +600,40 @@ * complexity inside the sampler engines which provide the actual script * functionalities. */ - #define DECLARE_VMINT(basePtr, T_struct, T_member) ( \ - (VMRelPtr) { \ - (void**) &basePtr, \ - offsetof(T_struct, T_member) \ - } \ - ) \ + #define DECLARE_VMINT(basePtr, T_struct, T_member) ( \ + /* Disable offsetof warning, trust us, we are cautios. */ \ + COMPILER_DISABLE_OFFSETOF_WARNING \ + (VMRelPtr) { \ + (void**) &basePtr, \ + offsetof(T_struct, T_member), \ + false \ + } \ + COMPILER_RESTORE_OFFSETOF_WARNING \ + ) \ + + /** + * Same as DECLARE_VMINT(), but this one defines the VMIntRelPtr and + * VMInt8RelPtr structures to be of read-only type. That means the script + * parser will abort any script at parser time if the script is trying to + * modify such a read-only built-in variable. + * + * @b NOTE: this is only intended for built-in read-only variables that + * may change during runtime! If your built-in variable's data is rather + * already available at parser time and won't change during runtime, then + * you should rather register a built-in constant in your VM class instead! + * + * @see ScriptVM::builtInConstIntVariables() + */ + #define DECLARE_VMINT_READONLY(basePtr, T_struct, T_member) ( \ + /* Disable offsetof warning, trust us, we are cautios. */ \ + COMPILER_DISABLE_OFFSETOF_WARNING \ + (VMRelPtr) { \ + (void**) &basePtr, \ + offsetof(T_struct, T_member), \ + true \ + } \ + COMPILER_RESTORE_OFFSETOF_WARNING \ + ) \ /** @brief Built-in VM 8 bit integer array variable. * @@ -549,8 +644,128 @@ struct VMInt8Array { int8_t* data; int size; + bool readonly; ///< Whether the array data may be modified or just be read. + + VMInt8Array() : data(NULL), size(0), readonly(false) {} + }; + + /** @brief Virtual machine script variable. + * + * Common interface for all variables accessed in scripts. + */ + class VMVariable : virtual public VMExpr { + public: + /** + * Whether a script may modify the content of this variable by + * assigning a new value to it. + * + * @see isConstExpr(), assign() + */ + virtual bool isAssignable() const = 0; + + /** + * In case this variable is assignable, this method will be called to + * perform the value assignment to this variable with @a expr + * reflecting the new value to be assigned. + * + * @param expr - new value to be assigned to this variable + */ + virtual void assignExpr(VMExpr* expr) = 0; + }; + + /** @brief Dynamically executed variable (abstract base class). + * + * Interface for the implementation of a dynamically generated content of + * a built-in script variable. Most built-in variables are simply pointers + * to some native location in memory. So when a script reads them, the + * memory location is simply read to get the value of the variable. A + * dynamic variable however is not simply a memory location. For each access + * to a dynamic variable some native code is executed to actually generate + * and provide the content (value) of this type of variable. + */ + class VMDynVar : public VMVariable { + public: + /** + * Returns true in case this dynamic variable can be considered to be a + * constant expression. A constant expression will retain the same value + * throughout the entire life time of a script and the expression's + * constant value may be evaluated already at script parse time, which + * may result in performance benefits during script runtime. + * + * However due to the "dynamic" behavior of dynamic variables, almost + * all dynamic variables are probably not constant expressions. That's + * why this method returns @c false by default. If you are really sure + * that your dynamic variable implementation can be considered a + * constant expression then you may override this method and return + * @c true instead. Note that when you return @c true here, your + * dynamic variable will really just be executed once; and exectly + * already when the script is loaded! + * + * As an example you may implement a "constant" built-in dynamic + * variable that checks for a certain operating system feature and + * returns the result of that OS feature check as content (value) of + * this dynamic variable. Since the respective OS feature might become + * available/unavailable after OS updates, software migration, etc. the + * OS feature check should at least be performed once each time the + * application is launched. And since the OS feature check might take a + * certain amount of execution time, it might make sense to only + * perform the check if the respective variable name is actually + * referenced at all in the script to be loaded. Note that the dynamic + * variable will still be evaluated again though if the script is + * loaded again. So it is up to you to probably cache the result in the + * implementation of your dynamic variable. + * + * On doubt, please rather consider to use a constant built-in script + * variable instead of implementing a "constant" dynamic variable, due + * to the runtime overhead a dynamic variable may cause. + * + * @see isAssignable() + */ + bool isConstExpr() const OVERRIDE { return false; } + + /** + * In case this dynamic variable is assignable, the new value (content) + * to be assigned to this dynamic variable. + * + * By default this method does nothing. Override and implement this + * method in your subclass in case your dynamic variable allows to + * assign a new value by script. + * + * @param expr - new value to be assigned to this variable + */ + void assignExpr(VMExpr* expr) OVERRIDE {} + + virtual ~VMDynVar() {} + }; + + /** @brief Dynamically executed variable (of integer data type). + * + * This is the base class for all built-in integer script variables whose + * variable content needs to be provided dynamically by executable native + * code on each script variable access. + */ + class VMDynIntVar : virtual public VMDynVar, virtual public VMIntExpr { + public: + }; - VMInt8Array() : data(NULL), size(0) {} + /** @brief Dynamically executed variable (of string data type). + * + * This is the base class for all built-in string script variables whose + * variable content needs to be provided dynamically by executable native + * code on each script variable access. + */ + class VMDynStringVar : virtual public VMDynVar, virtual public VMStringExpr { + public: + }; + + /** @brief Dynamically executed variable (of integer array data type). + * + * This is the base class for all built-in integer array script variables + * whose variable content needs to be provided dynamically by executable + * native code on each script variable access. + */ + class VMDynIntArrayVar : virtual public VMDynVar, virtual public VMIntArrayExpr { + public: }; /** @brief Provider for built-in script functions and variables. @@ -587,6 +802,13 @@ * variables, which never change their value at runtime. */ virtual std::map builtInConstIntVariables() = 0; + + /** + * Returns a variable name indexed map of all built-in dynamic variables, + * which are not simply data stores, rather each one of them executes + * natively to provide or alter the respective script variable data. + */ + virtual std::map builtInDynamicVariables() = 0; }; /** @brief Execution state of a virtual machine. @@ -629,6 +851,34 @@ * @see ScriptVM::exec() */ virtual int suspensionTimeMicroseconds() const = 0; + + /** + * Causes all polyphonic variables to be reset to zero values. A + * polyphonic variable is expected to be zero when entering a new event + * handler instance. As an exception the values of polyphonic variables + * shall only be preserved from an note event handler instance to its + * correspending specific release handler instance. So in the latter + * case the script author may pass custom data from the note handler to + * the release handler, but only for the same specific note! + */ + virtual void resetPolyphonicData() = 0; + + /** + * Returns amount of virtual machine instructions which have been + * performed the last time when this execution context was executing a + * script. So in case you need the overall amount of instructions + * instead, then you need to add them by yourself after each + * ScriptVM::exec() call. + */ + virtual size_t instructionsPerformed() const = 0; + + /** + * Sends a signal to this script execution instance to abort its script + * execution as soon as possible. This method is called i.e. when one + * script execution instance intends to stop another script execution + * instance. + */ + virtual void signalAbort() = 0; }; /** @brief Script callback for a certain event. @@ -660,14 +910,33 @@ }; /** + * Reflects the precise position and span of a specific code block within + * a script. This is currently only used for the locations of commented + * code blocks due to preprocessor statements. + * + * @see VMParserContext::preprocessorComments() + */ + struct CodeBlock { + int firstLine; ///< The first line number of this code block within the script (indexed with 1 being the very first line). + int lastLine; ///< The last line number of this code block within the script. + int firstColumn; ///< The first column of this code block within the script (indexed with 1 being the very first column). + int lastColumn; ///< The last column of this code block within the script. + }; + + /** * Encapsulates a noteworty parser issue. This encompasses the type of the * issue (either a parser error or parser warning), a human readable * explanation text of the error or warning and the location of the * encountered parser issue within the script. + * + * @see VMSourceToken for processing syntax highlighting instead. */ struct ParserIssue { String txt; ///< Human readable explanation text of the parser issue. - int line; ///< Line number within the script where this issue was encountered. + int firstLine; ///< The first line number within the script where this issue was encountered (indexed with 1 being the very first line). + int lastLine; ///< The last line number within the script where this issue was encountered. + int firstColumn; ///< The first column within the script where this issue was encountered (indexed with 1 being the very first column). + int lastColumn; ///< The last column within the script where this issue was encountered. ParserIssueType_t type; ///< Whether this issue is either a parser error or just a parser warning. /** @@ -676,10 +945,10 @@ inline void dump() { switch (type) { case PARSER_ERROR: - printf("[ERROR] line %d: %s\n", line, txt.c_str()); + printf("[ERROR] line %d, column %d: %s\n", firstLine, firstColumn, txt.c_str()); break; case PARSER_WARNING: - printf("[Warning] line %d: %s\n", line, txt.c_str()); + printf("[Warning] line %d, column %d: %s\n", firstLine, firstColumn, txt.c_str()); break; } } @@ -747,6 +1016,12 @@ virtual std::vector warnings() const = 0; /** + * Returns all code blocks of the script which were filtered out by the + * preprocessor. + */ + virtual std::vector preprocessorComments() const = 0; + + /** * Returns the translated virtual machine representation of an event * handler block (i.e. "on note ... end on" code block) within the * parsed script. This translated representation of the event handler @@ -767,6 +1042,59 @@ virtual VMEventHandler* eventHandlerByName(const String& name) = 0; }; + class SourceToken; + + /** @brief Recognized token of a script's source code. + * + * Represents one recognized token of a script's source code, for example + * a keyword, variable name, etc. and it provides further informations about + * that particular token, i.e. the precise location (line and column) of the + * token within the original script's source code. + * + * This class is not actually used by the sampler itself. It is rather + * provided for external script editor applications. Primary purpose of + * this class is syntax highlighting for external script editors. + * + * @see ParserIssue for processing compile errors and warnings instead. + */ + class VMSourceToken { + public: + VMSourceToken(); + VMSourceToken(SourceToken* ct); + VMSourceToken(const VMSourceToken& other); + virtual ~VMSourceToken(); + + // original text of this token as it is in the script's source code + String text() const; + + // position of token in script + 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. + 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(). + + // base types + bool isEOF() const; ///< Returns true in case this source token represents the end of the source code file. + bool isNewLine() const; ///< Returns true in case this source token represents a line feed character (i.e. "\n" on Unix systems). + bool isKeyword() const; ///< Returns true in case this source token represents a language keyword (i.e. "while", "function", "declare", "on", etc.). + 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. + bool isIdentifier() const; ///< Returns true in case this source token represents an identifier, which currently always means a function name. + bool isNumberLiteral() const; ///< Returns true in case this source token represents a number literal (i.e. 123). + bool isStringLiteral() const; ///< Returns true in case this source token represents a string literal (i.e. "Some text"). + bool isComment() const; ///< Returns true in case this source token represents a source code comment. + bool isPreprocessor() const; ///< Returns true in case this source token represents a preprocessor statement. + bool isOther() const; ///< Returns true in case this source token represents anything else not covered by the token types mentioned above. + + // extended types + bool isIntegerVariable() const; ///< Returns true in case this source token represents an integer variable name (i.e. "$someIntVariable"). + bool isStringVariable() const; ///< Returns true in case this source token represents an string variable name (i.e. "\@someStringVariable"). + bool isArrayVariable() const; ///< Returns true in case this source token represents an array variable name (i.e. "%someArryVariable"). + bool isEventHandlerName() const; ///< Returns true in case this source token represents an event handler name (i.e. "note", "release", "controller"). + + VMSourceToken& operator=(const VMSourceToken& other); + + private: + SourceToken* m_token; + }; + } // namespace LinuxSampler #endif // LS_INSTR_SCRIPT_PARSER_COMMON_H