--- linuxsampler/trunk/src/scriptvm/ScriptVM.cpp 2014/06/09 19:20:37 2611 +++ linuxsampler/trunk/src/scriptvm/ScriptVM.cpp 2016/07/13 15:51:06 2942 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Christian Schoenebeck + * Copyright (c) 2014 - 2016 Christian Schoenebeck * * http://www.linuxsampler.org * @@ -10,8 +10,12 @@ #include "ScriptVM.h" #include +#include #include "../common/global_private.h" #include "tree.h" +#include "CoreVMFunctions.h" +#include "CoreVMDynVars.h" +#include "editor/NkspScanner.h" #define DEBUG_SCRIPTVM_CORE 0 @@ -88,10 +92,26 @@ return max; } - ScriptVM::ScriptVM() : m_parserContext(NULL), fnWait(this) { + ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) { + m_fnMessage = new CoreVMFunction_message; + m_fnExit = new CoreVMFunction_exit; + m_fnWait = new CoreVMFunction_wait(this); + m_fnAbs = new CoreVMFunction_abs; + m_fnRandom = new CoreVMFunction_random; + m_fnNumElements = new CoreVMFunction_num_elements; + m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER; + m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER; } ScriptVM::~ScriptVM() { + delete m_fnMessage; + delete m_fnExit; + delete m_fnWait; + delete m_fnAbs; + delete m_fnRandom; + delete m_fnNumElements; + delete m_varRealTimer; + delete m_varPerfTimer; } VMParserContext* ScriptVM::loadScript(const String& s) { @@ -106,11 +126,12 @@ context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); context->registerBuiltInIntVariables( builtInIntVariables() ); context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); + context->registerBuiltInDynVariables( builtInDynamicVariables() ); context->createScanner(is); InstrScript_parse(context); - 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)))); dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); if (!context->globalIntMemory) context->globalIntMemory = new ArrayList(); @@ -152,21 +173,42 @@ _requiredMaxStackSizeFor(&*parserCtx->handlers); } execCtx->stack.resize(parserCtx->requiredMaxStackSize); - dmsg(2,("Created VM exec context with %d bytes VM stack size.\n", - parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))); + dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n", + long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); //printf("execCtx=0x%lx\n", (uint64_t)execCtx); const int polySize = parserCtx->polyphonicIntVarCount; execCtx->polyphonicIntMemory.resize(polySize); memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); - dmsg(2,("Allocated %d bytes polyphonic memory.\n", polySize * sizeof(int))); + dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); return execCtx; } + std::vector ScriptVM::syntaxHighlighting(const String& s) { + std::istringstream iss(s); + return syntaxHighlighting(&iss); + } + + std::vector ScriptVM::syntaxHighlighting(std::istream* is) { + NkspScanner scanner(is); + std::vector tokens = scanner.tokens(); + std::vector result; + result.resize(tokens.size()); + for (int i = 0; i < tokens.size(); ++i) { + SourceToken* st = new SourceToken; + *st = tokens[i]; + result[i] = VMSourceToken(st); + } + return result; + } + VMFunction* ScriptVM::functionByName(const String& name) { - if (name == "message") return &fnMessage; - else if (name == "exit") return &fnExit; - else if (name == "wait") return &fnWait; + if (name == "message") return m_fnMessage; + else if (name == "exit") return m_fnExit; + else if (name == "wait") return m_fnWait; + else if (name == "abs") return m_fnAbs; + else if (name == "random") return m_fnRandom; + else if (name == "num_elements") return m_fnNumElements; return NULL; } @@ -178,10 +220,24 @@ return std::map(); } + std::map ScriptVM::builtInDynamicVariables() { + std::map m; + + m["$NKSP_PERF_TIMER"] = m_varPerfTimer; + m["$NKSP_REAL_TIMER"] = m_varRealTimer; + m["$KSP_TIMER"] = m_varRealTimer; + + return m; + } + std::map ScriptVM::builtInConstIntVariables() { return std::map(); } + VMEventHandler* ScriptVM::currentVMEventHandler() { + return m_eventHandler; + } + VMParserContext* ScriptVM::currentVMParserContext() { return m_parserContext; } @@ -208,6 +264,7 @@ } EventHandler* h = dynamic_cast(handler); if (!h) return VM_EXEC_NOT_RUNNING; + m_eventHandler = handler; m_parserContext->execContext = ctx; @@ -303,6 +360,7 @@ ctx->reset(); } + m_eventHandler = NULL; m_parserContext->execContext = NULL; m_parserContext = NULL; return ctx->status;