1 |
/* |
/* |
2 |
* Copyright (c) 2014 - 2015 Christian Schoenebeck |
* Copyright (c) 2014 - 2016 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
13 |
#include <assert.h> |
#include <assert.h> |
14 |
#include "../common/global_private.h" |
#include "../common/global_private.h" |
15 |
#include "tree.h" |
#include "tree.h" |
16 |
|
#include "CoreVMFunctions.h" |
17 |
|
#include "CoreVMDynVars.h" |
18 |
|
#include "editor/NkspScanner.h" |
19 |
|
|
20 |
#define DEBUG_SCRIPTVM_CORE 0 |
#define DEBUG_SCRIPTVM_CORE 0 |
21 |
|
|
92 |
return max; |
return max; |
93 |
} |
} |
94 |
|
|
95 |
ScriptVM::ScriptVM() : m_parserContext(NULL), fnWait(this) { |
ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) { |
96 |
|
m_fnMessage = new CoreVMFunction_message; |
97 |
|
m_fnExit = new CoreVMFunction_exit; |
98 |
|
m_fnWait = new CoreVMFunction_wait(this); |
99 |
|
m_fnAbs = new CoreVMFunction_abs; |
100 |
|
m_fnRandom = new CoreVMFunction_random; |
101 |
|
m_fnNumElements = new CoreVMFunction_num_elements; |
102 |
|
m_fnInc = new CoreVMFunction_inc; |
103 |
|
m_fnDec = new CoreVMFunction_dec; |
104 |
|
m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER; |
105 |
|
m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER; |
106 |
|
m_fnShLeft = new CoreVMFunction_sh_left; |
107 |
|
m_fnShRight = new CoreVMFunction_sh_right; |
108 |
} |
} |
109 |
|
|
110 |
ScriptVM::~ScriptVM() { |
ScriptVM::~ScriptVM() { |
111 |
|
delete m_fnMessage; |
112 |
|
delete m_fnExit; |
113 |
|
delete m_fnWait; |
114 |
|
delete m_fnAbs; |
115 |
|
delete m_fnRandom; |
116 |
|
delete m_fnNumElements; |
117 |
|
delete m_fnInc; |
118 |
|
delete m_fnDec; |
119 |
|
delete m_fnShLeft; |
120 |
|
delete m_fnShRight; |
121 |
|
delete m_varRealTimer; |
122 |
|
delete m_varPerfTimer; |
123 |
} |
} |
124 |
|
|
125 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
VMParserContext* ScriptVM::loadScript(const String& s) { |
134 |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
135 |
context->registerBuiltInIntVariables( builtInIntVariables() ); |
context->registerBuiltInIntVariables( builtInIntVariables() ); |
136 |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
137 |
|
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
138 |
|
|
139 |
context->createScanner(is); |
context->createScanner(is); |
140 |
|
|
192 |
return execCtx; |
return execCtx; |
193 |
} |
} |
194 |
|
|
195 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) { |
196 |
|
std::istringstream iss(s); |
197 |
|
return syntaxHighlighting(&iss); |
198 |
|
} |
199 |
|
|
200 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
201 |
|
NkspScanner scanner(is); |
202 |
|
std::vector<SourceToken> tokens = scanner.tokens(); |
203 |
|
std::vector<VMSourceToken> result; |
204 |
|
result.resize(tokens.size()); |
205 |
|
for (int i = 0; i < tokens.size(); ++i) { |
206 |
|
SourceToken* st = new SourceToken; |
207 |
|
*st = tokens[i]; |
208 |
|
result[i] = VMSourceToken(st); |
209 |
|
} |
210 |
|
return result; |
211 |
|
} |
212 |
|
|
213 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
214 |
if (name == "message") return &fnMessage; |
if (name == "message") return m_fnMessage; |
215 |
else if (name == "exit") return &fnExit; |
else if (name == "exit") return m_fnExit; |
216 |
else if (name == "wait") return &fnWait; |
else if (name == "wait") return m_fnWait; |
217 |
else if (name == "abs") return &fnAbs; |
else if (name == "abs") return m_fnAbs; |
218 |
else if (name == "random") return &fnRandom; |
else if (name == "random") return m_fnRandom; |
219 |
else if (name == "num_elements") return &fnNumElements; |
else if (name == "num_elements") return m_fnNumElements; |
220 |
|
else if (name == "inc") return m_fnInc; |
221 |
|
else if (name == "dec") return m_fnDec; |
222 |
|
else if (name == "sh_left") return m_fnShLeft; |
223 |
|
else if (name == "sh_right") return m_fnShRight; |
224 |
return NULL; |
return NULL; |
225 |
} |
} |
226 |
|
|
232 |
return std::map<String,VMInt8Array*>(); |
return std::map<String,VMInt8Array*>(); |
233 |
} |
} |
234 |
|
|
235 |
|
std::map<String,VMDynVar*> ScriptVM::builtInDynamicVariables() { |
236 |
|
std::map<String,VMDynVar*> m; |
237 |
|
|
238 |
|
m["$NKSP_PERF_TIMER"] = m_varPerfTimer; |
239 |
|
m["$NKSP_REAL_TIMER"] = m_varRealTimer; |
240 |
|
m["$KSP_TIMER"] = m_varRealTimer; |
241 |
|
|
242 |
|
return m; |
243 |
|
} |
244 |
|
|
245 |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
246 |
return std::map<String,int>(); |
std::map<String,int> m; |
247 |
|
|
248 |
|
m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT; |
249 |
|
m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE; |
250 |
|
m["$NI_CB_TYPE_RELEASE"] = VM_EVENT_HANDLER_RELEASE; |
251 |
|
m["$NI_CB_TYPE_CONTROLLER"] = VM_EVENT_HANDLER_CONTROLLER; |
252 |
|
|
253 |
|
return m; |
254 |
|
} |
255 |
|
|
256 |
|
VMEventHandler* ScriptVM::currentVMEventHandler() { |
257 |
|
return m_eventHandler; |
258 |
} |
} |
259 |
|
|
260 |
VMParserContext* ScriptVM::currentVMParserContext() { |
VMParserContext* ScriptVM::currentVMParserContext() { |
283 |
} |
} |
284 |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
285 |
if (!h) return VM_EXEC_NOT_RUNNING; |
if (!h) return VM_EXEC_NOT_RUNNING; |
286 |
|
m_eventHandler = handler; |
287 |
|
|
288 |
m_parserContext->execContext = ctx; |
m_parserContext->execContext = ctx; |
289 |
|
|
379 |
ctx->reset(); |
ctx->reset(); |
380 |
} |
} |
381 |
|
|
382 |
|
m_eventHandler = NULL; |
383 |
m_parserContext->execContext = NULL; |
m_parserContext->execContext = NULL; |
384 |
m_parserContext = NULL; |
m_parserContext = NULL; |
385 |
return ctx->status; |
return ctx->status; |