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

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

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

revision 3556 by schoenebeck, Thu Aug 1 10:22:56 2019 UTC revision 3557 by schoenebeck, Sun Aug 18 00:06:04 2019 UTC
# Line 35  VMFnResult* VMEmptyResultFunction::succe Line 35  VMFnResult* VMEmptyResultFunction::succe
35  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
36  // class VMIntResultFunction  // class VMIntResultFunction
37    
38  VMFnResult* VMIntResultFunction::errorResult(int i) {  VMFnResult* VMIntResultFunction::errorResult(vmint i) {
39      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
40      result.value = i;      result.value = i;
41      return &result;      return &result;
42  }  }
43    
44  VMFnResult* VMIntResultFunction::successResult(int i) {  VMFnResult* VMIntResultFunction::successResult(vmint i) {
45      result.flags = STMT_SUCCESS;      result.flags = STMT_SUCCESS;
46      result.value = i;      result.value = i;
47      return &result;      return &result;
# Line 65  VMFnResult* VMStringResultFunction::succ Line 65  VMFnResult* VMStringResultFunction::succ
65  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
66  // built-in script function:  message()  // built-in script function:  message()
67    
68  bool CoreVMFunction_message::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_message::acceptsArgType(vmint iArg, ExprType_t type) const {
69      return type == INT_EXPR || type == STRING_EXPR;      return type == INT_EXPR || type == STRING_EXPR;
70  }  }
71    
# Line 82  VMFnResult* CoreVMFunction_message::exec Line 82  VMFnResult* CoreVMFunction_message::exec
82    
83      VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0));      VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0));
84      if (intExpr) {      if (intExpr) {
85          printf("[ScriptVM %.3f] %d\n", usecs/1000000.f, intExpr->evalInt());          printf("[ScriptVM %.3f] %lld\n", usecs/1000000.f, (int64_t)intExpr->evalInt());
86          return successResult();          return successResult();
87      }      }
88    
# Line 92  VMFnResult* CoreVMFunction_message::exec Line 92  VMFnResult* CoreVMFunction_message::exec
92  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
93  // built-in script function:  exit()  // built-in script function:  exit()
94    
95  int CoreVMFunction_exit::maxAllowedArgs() const {  vmint CoreVMFunction_exit::maxAllowedArgs() const {
96      return (vm->isExitResultEnabled()) ? 1 : 0;      return (vm->isExitResultEnabled()) ? 1 : 0;
97  }  }
98    
99  bool CoreVMFunction_exit::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_exit::acceptsArgType(vmint iArg, ExprType_t type) const {
100      if (!vm->isExitResultEnabled()) return false;      if (!vm->isExitResultEnabled()) return false;
101      return type == INT_EXPR || type == STRING_EXPR;      return type == INT_EXPR || type == STRING_EXPR;
102  }  }
# Line 127  VMFnResult* CoreVMFunction_exit::exec(VM Line 127  VMFnResult* CoreVMFunction_exit::exec(VM
127  VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) {
128      ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext());      ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext());
129      VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0));      VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0));
130      int us = expr->evalInt();      vmint us = expr->evalInt();
131      if (us < 0) {      if (us < 0) {
132          wrnMsg("wait(): argument may not be negative! Aborting script!");          wrnMsg("wait(): argument may not be negative! Aborting script!");
133          this->result.flags = STMT_ABORT_SIGNALLED;          this->result.flags = STMT_ABORT_SIGNALLED;
# Line 144  VMFnResult* CoreVMFunction_wait::exec(VM Line 144  VMFnResult* CoreVMFunction_wait::exec(VM
144  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
145  // built-in script function:  abs()  // built-in script function:  abs()
146    
147  bool CoreVMFunction_abs::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_abs::acceptsArgType(vmint iArg, ExprType_t type) const {
148      return type == INT_EXPR;      return type == INT_EXPR;
149  }  }
150    
# Line 155  VMFnResult* CoreVMFunction_abs::exec(VMF Line 155  VMFnResult* CoreVMFunction_abs::exec(VMF
155  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
156  // built-in script function:  random()  // built-in script function:  random()
157    
158  bool CoreVMFunction_random::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_random::acceptsArgType(vmint iArg, ExprType_t type) const {
159      return type == INT_EXPR;      return type == INT_EXPR;
160  }  }
161    
162  VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) {
163      int iMin = args->arg(0)->asInt()->evalInt();      vmint iMin = args->arg(0)->asInt()->evalInt();
164      int iMax = args->arg(1)->asInt()->evalInt();      vmint iMax = args->arg(1)->asInt()->evalInt();
165      float f = float(::rand()) / float(RAND_MAX);      float f = float(::rand()) / float(RAND_MAX);
166      return successResult(      return successResult(
167          iMin + roundf( f * float(iMax - iMin) )          iMin + roundf( f * float(iMax - iMin) )
# Line 171  VMFnResult* CoreVMFunction_random::exec( Line 171  VMFnResult* CoreVMFunction_random::exec(
171  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
172  // built-in script function:  num_elements()  // built-in script function:  num_elements()
173    
174  bool CoreVMFunction_num_elements::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_num_elements::acceptsArgType(vmint iArg, ExprType_t type) const {
175      return type == INT_ARR_EXPR;      return type == INT_ARR_EXPR;
176  }  }
177    
# Line 187  VMFnResult* CoreVMFunction_inc::exec(VMF Line 187  VMFnResult* CoreVMFunction_inc::exec(VMF
187      VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);      VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);
188      VMVariable* out = dynamic_cast<VMVariable*>(arg);      VMVariable* out = dynamic_cast<VMVariable*>(arg);
189      if (!in || !out) successResult(0);      if (!in || !out) successResult(0);
190      int i = in->evalInt() + 1;      vmint i = in->evalInt() + 1;
191      IntLiteral tmp(i);      IntLiteral tmp(i);
192      out->assignExpr(&tmp);      out->assignExpr(&tmp);
193      return successResult(i);      return successResult(i);
# Line 201  VMFnResult* CoreVMFunction_dec::exec(VMF Line 201  VMFnResult* CoreVMFunction_dec::exec(VMF
201      VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);      VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);
202      VMVariable* out = dynamic_cast<VMVariable*>(arg);      VMVariable* out = dynamic_cast<VMVariable*>(arg);
203      if (!in || !out) successResult(0);      if (!in || !out) successResult(0);
204      int i = in->evalInt() - 1;      vmint i = in->evalInt() - 1;
205      IntLiteral tmp(i);      IntLiteral tmp(i);
206      out->assignExpr(&tmp);      out->assignExpr(&tmp);
207      return successResult(i);      return successResult(i);
# Line 211  VMFnResult* CoreVMFunction_dec::exec(VMF Line 211  VMFnResult* CoreVMFunction_dec::exec(VMF
211  // built-in script function:  in_range()  // built-in script function:  in_range()
212    
213  VMFnResult* CoreVMFunction_in_range::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_in_range::exec(VMFnArgs* args) {
214      int i  = args->arg(0)->asInt()->evalInt();      vmint i  = args->arg(0)->asInt()->evalInt();
215      int lo = args->arg(1)->asInt()->evalInt();      vmint lo = args->arg(1)->asInt()->evalInt();
216      int hi = args->arg(2)->asInt()->evalInt();      vmint hi = args->arg(2)->asInt()->evalInt();
217      if (lo > hi) { // swap lo and hi      if (lo > hi) { // swap lo and hi
218          int tmp = lo;          vmint tmp = lo;
219          lo = hi;          lo = hi;
220          hi = tmp;          hi = tmp;
221      }      }
# Line 226  VMFnResult* CoreVMFunction_in_range::exe Line 226  VMFnResult* CoreVMFunction_in_range::exe
226  // built-in script function:  sh_left()  // built-in script function:  sh_left()
227    
228  VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) {
229      int i = args->arg(0)->asInt()->evalInt();      vmint i = args->arg(0)->asInt()->evalInt();
230      int n = args->arg(1)->asInt()->evalInt();      vmint n = args->arg(1)->asInt()->evalInt();
231      return successResult(i << n);      return successResult(i << n);
232  }  }
233    
# Line 235  VMFnResult* CoreVMFunction_sh_left::exec Line 235  VMFnResult* CoreVMFunction_sh_left::exec
235  // built-in script function:  sh_right()  // built-in script function:  sh_right()
236    
237  VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) {
238      int i = args->arg(0)->asInt()->evalInt();      vmint i = args->arg(0)->asInt()->evalInt();
239      int n = args->arg(1)->asInt()->evalInt();      vmint n = args->arg(1)->asInt()->evalInt();
240      return successResult(i >> n);      return successResult(i >> n);
241  }  }
242    
# Line 244  VMFnResult* CoreVMFunction_sh_right::exe Line 244  VMFnResult* CoreVMFunction_sh_right::exe
244  // built-in script function:  min()  // built-in script function:  min()
245    
246  VMFnResult* CoreVMFunction_min::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_min::exec(VMFnArgs* args) {
247      int l = args->arg(0)->asInt()->evalInt();      vmint l = args->arg(0)->asInt()->evalInt();
248      int r = args->arg(1)->asInt()->evalInt();      vmint r = args->arg(1)->asInt()->evalInt();
249      return successResult(l < r ? l : r);      return successResult(l < r ? l : r);
250  }  }
251    
# Line 253  VMFnResult* CoreVMFunction_min::exec(VMF Line 253  VMFnResult* CoreVMFunction_min::exec(VMF
253  // built-in script function:  max()  // built-in script function:  max()
254    
255  VMFnResult* CoreVMFunction_max::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_max::exec(VMFnArgs* args) {
256      int l = args->arg(0)->asInt()->evalInt();      vmint l = args->arg(0)->asInt()->evalInt();
257      int r = args->arg(1)->asInt()->evalInt();      vmint r = args->arg(1)->asInt()->evalInt();
258      return successResult(l > r ? l : r);      return successResult(l > r ? l : r);
259  }  }
260    
# Line 268  VMFnResult* CoreVMFunction_array_equal:: Line 268  VMFnResult* CoreVMFunction_array_equal::
268          wrnMsg("array_equal(): the two arrays differ in size");          wrnMsg("array_equal(): the two arrays differ in size");
269          return successResult(0); // false          return successResult(0); // false
270      }      }
271      const int n = l->arraySize();      const vmint n = l->arraySize();
272      for (int i = 0; i < n; ++i)      for (vmint i = 0; i < n; ++i)
273          if (l->evalIntElement(i) != r->evalIntElement(i))          if (l->evalIntElement(i) != r->evalIntElement(i))
274              return successResult(0); // false              return successResult(0); // false
275      return successResult(1); // true      return successResult(1); // true
# Line 278  VMFnResult* CoreVMFunction_array_equal:: Line 278  VMFnResult* CoreVMFunction_array_equal::
278  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
279  // built-in script function:  search()  // built-in script function:  search()
280    
281  ExprType_t CoreVMFunction_search::argType(int iArg) const {  ExprType_t CoreVMFunction_search::argType(vmint iArg) const {
282      return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR;      return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR;
283  }  }
284    
285  bool CoreVMFunction_search::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_search::acceptsArgType(vmint iArg, ExprType_t type) const {
286      if (iArg == 0)      if (iArg == 0)
287          return type == INT_ARR_EXPR;          return type == INT_ARR_EXPR;
288      else      else
# Line 291  bool CoreVMFunction_search::acceptsArgTy Line 291  bool CoreVMFunction_search::acceptsArgTy
291    
292  VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) {
293      VMIntArrayExpr* a = args->arg(0)->asIntArray();      VMIntArrayExpr* a = args->arg(0)->asIntArray();
294      const int needle = args->arg(1)->asInt()->evalInt();      const vmint needle = args->arg(1)->asInt()->evalInt();
295      const int n = a->arraySize();      const vmint n = a->arraySize();
296      for (int i = 0; i < n; ++i)      for (vmint i = 0; i < n; ++i)
297          if (a->evalIntElement(i) == needle)          if (a->evalIntElement(i) == needle)
298              return successResult(i);              return successResult(i);
299      return successResult(-1); // not found      return successResult(-1); // not found
# Line 302  VMFnResult* CoreVMFunction_search::exec( Line 302  VMFnResult* CoreVMFunction_search::exec(
302  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
303  // built-in script function:  sort()  // built-in script function:  sort()
304    
305  ExprType_t CoreVMFunction_sort::argType(int iArg) const {  ExprType_t CoreVMFunction_sort::argType(vmint iArg) const {
306      return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR;      return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR;
307  }  }
308    
309  bool CoreVMFunction_sort::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_sort::acceptsArgType(vmint iArg, ExprType_t type) const {
310      if (iArg == 0)      if (iArg == 0)
311          return type == INT_ARR_EXPR;          return type == INT_ARR_EXPR;
312      else      else
# Line 315  bool CoreVMFunction_sort::acceptsArgType Line 315  bool CoreVMFunction_sort::acceptsArgType
315    
316  struct ArrElemPOD {  struct ArrElemPOD {
317      VMIntArrayExpr* m_array;      VMIntArrayExpr* m_array;
318      int m_index;      vmint m_index;
319  };  };
320    
321  static inline void swap(class ArrElemRef a, class ArrElemRef b);  static inline void swap(class ArrElemRef a, class ArrElemRef b);
# Line 326  public: Line 326  public:
326          m_array = NULL;          m_array = NULL;
327          m_index = 0;          m_index = 0;
328      }      }
329      ArrElemRef(VMIntArrayExpr* a, int index) {      ArrElemRef(VMIntArrayExpr* a, vmint index) {
330          m_array = a;          m_array = a;
331          m_index = index;          m_index = index;
332      }      }
# Line 334  public: Line 334  public:
334          setValue(e.getValue());          setValue(e.getValue());
335          return *this;          return *this;
336      }      }
337      inline ArrElemRef& operator=(int val) {      inline ArrElemRef& operator=(vmint val) {
338          setValue(val);          setValue(val);
339          return *this;          return *this;
340      }      }
# Line 343  public: Line 343  public:
343              return true;              return true;
344          return getValue() == e.getValue();          return getValue() == e.getValue();
345      }      }
346      inline bool operator==(int val) const {      inline bool operator==(vmint val) const {
347          return getValue() == val;          return getValue() == val;
348      }      }
349      inline bool operator!=(const ArrElemRef& e) const {      inline bool operator!=(const ArrElemRef& e) const {
350          return !(operator==(e));          return !(operator==(e));
351      }      }
352      inline bool operator!=(int val) const {      inline bool operator!=(vmint val) const {
353          return !(operator==(val));          return !(operator==(val));
354      }      }
355      inline bool operator<(const ArrElemRef& e) const {      inline bool operator<(const ArrElemRef& e) const {
# Line 357  public: Line 357  public:
357              return false;              return false;
358          return getValue() < e.getValue();          return getValue() < e.getValue();
359      }      }
360      inline bool operator<(int val) const {      inline bool operator<(vmint val) const {
361          return getValue() < val;          return getValue() < val;
362      }      }
363      inline bool operator>(const ArrElemRef& e) const {      inline bool operator>(const ArrElemRef& e) const {
# Line 365  public: Line 365  public:
365              return false;              return false;
366          return getValue() > e.getValue();          return getValue() > e.getValue();
367      }      }
368      inline bool operator>(int val) const {      inline bool operator>(vmint val) const {
369          return getValue() > val;          return getValue() > val;
370      }      }
371      inline bool operator<=(const ArrElemRef& e) const {      inline bool operator<=(const ArrElemRef& e) const {
# Line 373  public: Line 373  public:
373              return true;              return true;
374          return getValue() <= e.getValue();          return getValue() <= e.getValue();
375      }      }
376      inline bool operator<=(int val) const {      inline bool operator<=(vmint val) const {
377          return getValue() <= val;          return getValue() <= val;
378      }      }
379      inline bool operator>=(const ArrElemRef& e) const {      inline bool operator>=(const ArrElemRef& e) const {
# Line 381  public: Line 381  public:
381              return true;              return true;
382          return getValue() >= e.getValue();          return getValue() >= e.getValue();
383      }      }
384      inline bool operator>=(int val) const {      inline bool operator>=(vmint val) const {
385          return getValue() >= val;          return getValue() >= val;
386      }      }
387      inline operator int() const {      inline operator vmint() const {
388          return getValue();          return getValue();
389      }      }
390  protected:  protected:
391      inline int getValue() const {      inline vmint getValue() const {
392          return m_array->evalIntElement(m_index);          return m_array->evalIntElement(m_index);
393      }      }
394      inline void setValue(int value) {      inline void setValue(vmint value) {
395          m_array->assignIntElement(m_index, value);          m_array->assignIntElement(m_index, value);
396      }      }
397    
# Line 404  public: Line 404  public:
404          m_array = NULL;          m_array = NULL;
405          m_index = 0;          m_index = 0;
406      }      }
407      ArrElemPtr(VMIntArrayExpr* a, int index) {      ArrElemPtr(VMIntArrayExpr* a, vmint index) {
408          m_array = a;          m_array = a;
409          m_index = index;          m_index = index;
410      }      }
# Line 414  public: Line 414  public:
414  };  };
415    
416  static inline void swap(ArrElemRef a, ArrElemRef b) {  static inline void swap(ArrElemRef a, ArrElemRef b) {
417      int valueA = a.getValue();      vmint valueA = a.getValue();
418      int valueB = b.getValue();      vmint valueB = b.getValue();
419      a.setValue(valueB);      a.setValue(valueB);
420      b.setValue(valueA);      b.setValue(valueA);
421  }  }
# Line 423  static inline void swap(ArrElemRef a, Ar Line 423  static inline void swap(ArrElemRef a, Ar
423  class ArrExprIter : public ArrElemPOD {  class ArrExprIter : public ArrElemPOD {
424  public:  public:
425      typedef std::random_access_iterator_tag iterator_category;      typedef std::random_access_iterator_tag iterator_category;
426      typedef int value_type;      typedef vmint value_type;
427      typedef ssize_t difference_type;      typedef ssize_t difference_type;
428      typedef ArrElemPtr pointer;      typedef ArrElemPtr pointer;
429      typedef ArrElemRef reference;      typedef ArrElemRef reference;
430    
431      ArrExprIter(VMIntArrayExpr* a, int index) {      ArrExprIter(VMIntArrayExpr* a, vmint index) {
432          m_array = a;          m_array = a;
433          m_index = index;          m_index = index;
434      }      }
# Line 497  public: Line 497  public:
497  };  };
498    
499  struct DescArrExprSorter {  struct DescArrExprSorter {
500      inline bool operator()(const int& a, const int& b) const {      inline bool operator()(const vmint& a, const vmint& b) const {
501          return a > b;          return a > b;
502      }      }
503  };  };
# Line 506  VMFnResult* CoreVMFunction_sort::exec(VM Line 506  VMFnResult* CoreVMFunction_sort::exec(VM
506      VMIntArrayExpr* a = args->arg(0)->asIntArray();      VMIntArrayExpr* a = args->arg(0)->asIntArray();
507      bool bAscending =      bool bAscending =
508          (args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt();          (args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt();
509      int n = a->arraySize();      vmint n = a->arraySize();
510      ArrExprIter itBegin(a, 0);      ArrExprIter itBegin(a, 0);
511      ArrExprIter itEnd(a, n);      ArrExprIter itEnd(a, n);
512      if (bAscending) {      if (bAscending) {

Legend:
Removed from v.3556  
changed lines
  Added in v.3557

  ViewVC Help
Powered by ViewVC