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

Contents of /linuxsampler/trunk/src/scriptvm/ScriptVM.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2889 - (show annotations) (download) (as text)
Mon Apr 25 17:28:23 2016 UTC (7 years, 11 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 10832 byte(s)
* Added new C++ API class "ScriptVMFactory".
* Instrument Scripts: extended parser issues to provide not only first
  line and first column, but also last line and last column of issue
  (thus marking the precise span of the issue within the source code).
* Bumped version (2.0.0.svn7).

1 /*
2 * Copyright (c) 2014-2016 Christian Schoenebeck
3 *
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 #ifndef LS_SCRIPTVM_H
11 #define LS_SCRIPTVM_H
12
13 #include <iostream>
14 #include <vector>
15
16 #include "../common/global.h"
17 #include "common.h"
18
19 namespace LinuxSampler {
20
21 class ParserContext;
22 class ExecContext;
23 class CoreVMFunction_message;
24 class CoreVMFunction_exit;
25 class CoreVMFunction_wait;
26 class CoreVMFunction_abs;
27 class CoreVMFunction_random;
28 class CoreVMFunction_num_elements;
29
30 /** @brief Core virtual machine for real-time instrument scripts.
31 *
32 * This is the core of the virtual machine and main entry class, used for
33 * running real-time instrument scripts. This VM core encompasses the
34 * instrument script parser, generalized virtual machine and very generic
35 * built-in script functions. Thus this class only provides functionalities
36 * which are yet independent of the actual purpose the virtual machine is
37 * going to be used for.
38 *
39 * The actual use case specific functionalites (i.e. MIDI processing) is
40 * then implemented by sampler engines' VM classes which are derived from
41 * this generalized ScriptVM class.
42 *
43 * Typical usage of this class:
44 *
45 * - 1. Create an instance of this ScriptVM class (or of one of its deriving
46 * classes).
47 * - 2. Load a script by passing its source code to method loadScript(),
48 * which will return the parsed representation of the script.
49 * - 3. Create a VM execution context by calling createExecContext().
50 * - 4. Execute the script by calling method exec().
51 *
52 * This class is re-entrant safe, but not thread safe. So you can share one
53 * instance of this class between multiple (native) threads, but you @b must
54 * @b not execute methods of the same class instance simultaniously from
55 * different (native) threads. If you want to execute scripts simultaniously
56 * multi threaded, then create a separate ScriptVM instance for each
57 * (native) thread. Also note that one VMParserContext instance is tied to
58 * exactly one ScriptVM instance. So you @b must @b not create a
59 * VMParserContext with one ScriptVM instance and run it with a different
60 * ScriptVM instance!
61 */
62 class ScriptVM : public VMFunctionProvider {
63 public:
64 ScriptVM();
65 virtual ~ScriptVM();
66
67 /**
68 * Loads a script given by its source code (passed as argument @a s to
69 * this method) and returns the parsed representation of that script.
70 * After calling this method you must check the returned VMParserContext
71 * object whether there had been any parser errors. If there were no
72 * parser errors, you may pass the VMParserContext object to method
73 * exec() for actually executing the script.
74 *
75 * It is your responsibility to free the returned VMParserContext
76 * object once you don't need it anymore.
77 *
78 * @param s - entire source code of the script to be loaded
79 * @returns parsed representation of the script
80 */
81 VMParserContext* loadScript(const String& s);
82
83 /**
84 * Same as above's loadScript() method, but this one reads the script's
85 * source code from an input stream object (i.e. stdin or a file).
86 *
87 * @param is - input stream from which the entire source code of the
88 * script is to be read and loaded from
89 * @returns parsed representation of the script
90 */
91 VMParserContext* loadScript(std::istream* is);
92
93 /**
94 * Parses a script's source code (passed as argument @a s to this
95 * method), splits that input up in its individual tokens (i.e.
96 * keyword, variable name, event name, etc.) and returns all those
97 * tokens, for the purpose that the caller can provide syntax syntax
98 * highlighting for the passed script.
99 *
100 * This method is actually not used by the sampler at all, it is rather
101 * provided for external script editor applications, to provide them a
102 * convenient backend for parsing scripts and providing syntax
103 * highlighting.
104 *
105 * @returns recognized tokens of passed script's source code
106 */
107 std::vector<VMSourceToken> syntaxHighlighting(const String& s);
108
109 /**
110 * Same as above's syntaxHighlighting() method, but this one reads the
111 * script's source code from an input stream object (i.e. stdin or a
112 * file).
113 *
114 * @param is - input stream from which the entire source code of the
115 * script is to be read and loaded from
116 * @returns recognized tokens of passed script's source code
117 */
118 std::vector<VMSourceToken> syntaxHighlighting(std::istream* is);
119
120 /**
121 * Dumps the translated tree of the already parsed script, given by
122 * argument @a context, to stdout. This method is for debugging purposes
123 * only.
124 *
125 * @param context - parsed representation of the script
126 * @see loadScript()
127 */
128 void dumpParsedScript(VMParserContext* context);
129
130 /**
131 * Creates a so called VM exceution context for a specific, already
132 * parsed script (provided by argument @a parserContext). Due to the
133 * general real-time design of this virtual machine, the VM execution
134 * context differs for every script. So you must (re)create the
135 * execution context for each script being loaded.
136 *
137 * @param parserContext - parsed representation of the script
138 * @see loadScript()
139 */
140 VMExecContext* createExecContext(VMParserContext* parserContext);
141
142 /**
143 * Execute a script by virtual machine. Since scripts are event-driven,
144 * you actually execute only one specific event handler block (i.e. a
145 * "on note ... end on" code block) by calling this method (not the
146 * entire script), and hence you must provide one precise handler of the
147 * script to be executed by this method.
148 *
149 * This method usually blocks until the entire script event handler
150 * block has been executed completely. It may however also return before
151 * completion if either a) a script runtime error occurred or b) the
152 * script was suspended by the VM (either because script execution
153 * exceeded a certain limit of time or the script called the built-in
154 * wait() function). You must check the return value of this method to
155 * find out which case applies.
156 *
157 * @param parserContext - parsed representation of the script (see loadScript())
158 * @param execContext - VM execution context (see createExecContext())
159 * @param handler - precise event handler (i.e. "on note ... end on"
160 * code block) to be executed
161 * (see VMParserContext::eventHandlerByName())
162 * @returns current status of the vitual machine (i.e. script succeeded,
163 * script runtime error occurred or script was suspended for
164 * some reason).
165 */
166 VMExecStatus_t exec(VMParserContext* parserContext, VMExecContext* execContext, VMEventHandler* handler);
167
168 /**
169 * Returns built-in script function for the given function @a name. To
170 * get the implementation of the built-in message() script function for
171 * example, you would pass "message" here).
172 *
173 * This method is re-implemented by deriving classes to add more use
174 * case specific built-in functions.
175 *
176 * @param name - name of the function to be retrieved (i.e. "wait" for the
177 * built-in wait() function).
178 */
179 VMFunction* functionByName(const String& name) OVERRIDE;
180
181 /**
182 * Returns all built-in integer script variables. This method returns a
183 * STL map, where the map's key is the variable name and the map's value
184 * is the native pointer to the actual built-in variable.
185 *
186 * This method is re-implemented by deriving classes to add more use
187 * case specific built-in variables.
188 */
189 std::map<String,VMIntRelPtr*> builtInIntVariables() OVERRIDE;
190
191 /**
192 * Returns all built-in (8 bit) integer array script variables. This
193 * method returns a STL map, where the map's key is the array variable
194 * name and the map's value is the native pointer to the actual built-in
195 * array variable.
196 *
197 * This method is re-implemented by deriving classes to add more use
198 * case specific built-in array variables.
199 */
200 std::map<String,VMInt8Array*> builtInIntArrayVariables() OVERRIDE;
201
202 /**
203 * Returns all built-in constant integer script variables, which can
204 * only be read, but not be altered by scripts. This method returns a
205 * STL map, where the map's key is the variable name and the map's value
206 * is the native pointer to the actual built-in constant variable.
207 *
208 * This method is re-implemented by deriving classes to add more use
209 * case specific built-in constant integers.
210 *
211 * @b Note: the term "constant" is a bit misleading here, since
212 * built-in constant integer variables may indeed change, i.e. for
213 * built-in constant integers which i.e. reflect some kind of status of
214 * the sampler. So rather see them as "read only" variables, not as
215 * being actually consistent in time.
216 */
217 std::map<String,int> builtInConstIntVariables() OVERRIDE;
218
219 VMEventHandler* currentVMEventHandler(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)
220 VMParserContext* currentVMParserContext(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)
221 VMExecContext* currentVMExecContext(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)
222
223 protected:
224 VMEventHandler* m_eventHandler;
225 ParserContext* m_parserContext;
226 CoreVMFunction_message* m_fnMessage;
227 CoreVMFunction_exit* m_fnExit;
228 CoreVMFunction_wait* m_fnWait;
229 CoreVMFunction_abs* m_fnAbs;
230 CoreVMFunction_random* m_fnRandom;
231 CoreVMFunction_num_elements* m_fnNumElements;
232 };
233
234 } // namespace LinuxSampler
235
236 #endif // LS_INSTRUMENTSCRIPTVM_H

  ViewVC Help
Powered by ViewVC