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

Diff of /linuxsampler/trunk/src/scriptvm/ScriptVM.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2594 by schoenebeck, Thu Jun 5 00:16:25 2014 UTC revision 2885 by schoenebeck, Fri Apr 22 15:37:45 2016 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 Christian Schoenebeck   * Copyright (c) 2014 - 2016 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 10  Line 10 
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    
# Line 88  namespace LinuxSampler { Line 91  namespace LinuxSampler {
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) {
# Line 110  namespace LinuxSampler { Line 125  namespace LinuxSampler {
125          context->createScanner(is);          context->createScanner(is);
126    
127          InstrScript_parse(context);          InstrScript_parse(context);
128          std::cout << "Allocating " << context->globalIntVarCount * sizeof(int) << " bytes of global int VM memory.\n";          dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int))));
129          std::cout << "Allocating " << context->globalStrVarCount << " of global VM string variables.\n";          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>();
132          if (!context->globalStrMemory)          if (!context->globalStrMemory)
# Line 152  namespace LinuxSampler { Line 167  namespace LinuxSampler {
167                  _requiredMaxStackSizeFor(&*parserCtx->handlers);                  _requiredMaxStackSizeFor(&*parserCtx->handlers);
168          }          }
169          execCtx->stack.resize(parserCtx->requiredMaxStackSize);          execCtx->stack.resize(parserCtx->requiredMaxStackSize);
170          std::cout << "Created VM exec context with "          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))));
                   << " bytes VM stack size.\n";  
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          std::cout << "Allocated " << polySize * sizeof(int)          dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int))));
                   << " bytes polyphonic memory.\n";  
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    
# Line 184  namespace LinuxSampler { Line 218  namespace LinuxSampler {
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      }      }
# Line 200  namespace LinuxSampler { Line 238  namespace LinuxSampler {
238              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);
239          }          }
240    
241            // a ParserContext object is always tied to exactly one ScriptVM object
242            assert(m_parserContext->functionProvider == this);
243    
244          ExecContext* ctx = dynamic_cast<ExecContext*>(execContex);          ExecContext* ctx = dynamic_cast<ExecContext*>(execContex);
245          if (!ctx) {          if (!ctx) {
246              std::cerr << "Invalid VM exec context.\n";              std::cerr << "Invalid VM exec context.\n";
# Line 207  namespace LinuxSampler { Line 248  namespace LinuxSampler {
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    
# Line 302  namespace LinuxSampler { Line 344  namespace LinuxSampler {
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;

Legend:
Removed from v.2594  
changed lines
  Added in v.2885

  ViewVC Help
Powered by ViewVC