1 |
/* |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck |
* Copyright (c) 2014 - 2016 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
10 |
#include "ScriptVM.h" |
#include "ScriptVM.h" |
11 |
|
|
12 |
#include <string.h> |
#include <string.h> |
13 |
|
#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 "editor/NkspScanner.h" |
18 |
|
|
19 |
#define DEBUG_SCRIPTVM_CORE 0 |
#define DEBUG_SCRIPTVM_CORE 0 |
20 |
|
|
91 |
return max; |
return max; |
92 |
} |
} |
93 |
|
|
94 |
ScriptVM::ScriptVM() : m_parserContext(NULL), fnWait(this) { |
ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) { |
95 |
|
m_fnMessage = new CoreVMFunction_message; |
96 |
|
m_fnExit = new CoreVMFunction_exit; |
97 |
|
m_fnWait = new CoreVMFunction_wait(this); |
98 |
|
m_fnAbs = new CoreVMFunction_abs; |
99 |
|
m_fnRandom = new CoreVMFunction_random; |
100 |
|
m_fnNumElements = new CoreVMFunction_num_elements; |
101 |
} |
} |
102 |
|
|
103 |
ScriptVM::~ScriptVM() { |
ScriptVM::~ScriptVM() { |
104 |
|
delete m_fnMessage; |
105 |
|
delete m_fnExit; |
106 |
|
delete m_fnWait; |
107 |
|
delete m_fnAbs; |
108 |
|
delete m_fnRandom; |
109 |
|
delete m_fnNumElements; |
110 |
} |
} |
111 |
|
|
112 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
VMParserContext* ScriptVM::loadScript(const String& s) { |
125 |
context->createScanner(is); |
context->createScanner(is); |
126 |
|
|
127 |
InstrScript_parse(context); |
InstrScript_parse(context); |
128 |
dmsg(2,("Allocating %d bytes of global int VM memory.\n", context->globalIntVarCount * sizeof(int))); |
dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int)))); |
129 |
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
130 |
if (!context->globalIntMemory) |
if (!context->globalIntMemory) |
131 |
context->globalIntMemory = new ArrayList<int>(); |
context->globalIntMemory = new ArrayList<int>(); |
167 |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
168 |
} |
} |
169 |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
170 |
dmsg(2,("Created VM exec context with %d bytes VM stack size.\n", |
dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n", |
171 |
parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))); |
long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
172 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
173 |
const int polySize = parserCtx->polyphonicIntVarCount; |
const int polySize = parserCtx->polyphonicIntVarCount; |
174 |
execCtx->polyphonicIntMemory.resize(polySize); |
execCtx->polyphonicIntMemory.resize(polySize); |
175 |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
176 |
|
|
177 |
dmsg(2,("Allocated %d bytes polyphonic memory.\n", polySize * sizeof(int))); |
dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); |
178 |
return execCtx; |
return execCtx; |
179 |
} |
} |
180 |
|
|
181 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) { |
182 |
|
std::istringstream iss(s); |
183 |
|
return syntaxHighlighting(&iss); |
184 |
|
} |
185 |
|
|
186 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
187 |
|
NkspScanner scanner(is); |
188 |
|
std::vector<SourceToken> tokens = scanner.tokens(); |
189 |
|
std::vector<VMSourceToken> result; |
190 |
|
result.resize(tokens.size()); |
191 |
|
for (int i = 0; i < tokens.size(); ++i) { |
192 |
|
SourceToken* st = new SourceToken; |
193 |
|
*st = tokens[i]; |
194 |
|
result[i] = VMSourceToken(st); |
195 |
|
} |
196 |
|
return result; |
197 |
|
} |
198 |
|
|
199 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
200 |
if (name == "message") return &fnMessage; |
if (name == "message") return m_fnMessage; |
201 |
else if (name == "exit") return &fnExit; |
else if (name == "exit") return m_fnExit; |
202 |
else if (name == "wait") return &fnWait; |
else if (name == "wait") return m_fnWait; |
203 |
|
else if (name == "abs") return m_fnAbs; |
204 |
|
else if (name == "random") return m_fnRandom; |
205 |
|
else if (name == "num_elements") return m_fnNumElements; |
206 |
return NULL; |
return NULL; |
207 |
} |
} |
208 |
|
|
218 |
return std::map<String,int>(); |
return std::map<String,int>(); |
219 |
} |
} |
220 |
|
|
221 |
|
VMEventHandler* ScriptVM::currentVMEventHandler() { |
222 |
|
return m_eventHandler; |
223 |
|
} |
224 |
|
|
225 |
VMParserContext* ScriptVM::currentVMParserContext() { |
VMParserContext* ScriptVM::currentVMParserContext() { |
226 |
return m_parserContext; |
return m_parserContext; |
227 |
} |
} |
248 |
} |
} |
249 |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
250 |
if (!h) return VM_EXEC_NOT_RUNNING; |
if (!h) return VM_EXEC_NOT_RUNNING; |
251 |
|
m_eventHandler = handler; |
252 |
|
|
253 |
m_parserContext->execContext = ctx; |
m_parserContext->execContext = ctx; |
254 |
|
|
344 |
ctx->reset(); |
ctx->reset(); |
345 |
} |
} |
346 |
|
|
347 |
|
m_eventHandler = NULL; |
348 |
m_parserContext->execContext = NULL; |
m_parserContext->execContext = NULL; |
349 |
m_parserContext = NULL; |
m_parserContext = NULL; |
350 |
return ctx->status; |
return ctx->status; |