/[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 2598 by schoenebeck, Fri Jun 6 12:38:54 2014 UTC revision 3076 by schoenebeck, Thu Jan 5 18:00:52 2017 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 Christian Schoenebeck   * Copyright (c) 2014-2015 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 10  Line 10 
10  #include "CoreVMFunctions.h"  #include "CoreVMFunctions.h"
11    
12  #include <iostream>  #include <iostream>
13    #include <math.h>
14    #include <stdlib.h>
15  #include "tree.h"  #include "tree.h"
16  #include "ScriptVM.h"  #include "ScriptVM.h"
17    
18  namespace LinuxSampler {  namespace LinuxSampler {
19    
20    ///////////////////////////////////////////////////////////////////////////
21    // class VMEmptyResultFunction
22    
23  VMFnResult* VMEmptyResultFunction::errorResult() {  VMFnResult* VMEmptyResultFunction::errorResult() {
24      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
25      return &result;      return &result;
# Line 25  VMFnResult* VMEmptyResultFunction::succe Line 30  VMFnResult* VMEmptyResultFunction::succe
30      return &result;      return &result;
31  }  }
32    
33    ///////////////////////////////////////////////////////////////////////////
34    // class VMIntResultFunction
35    
36  VMFnResult* VMIntResultFunction::errorResult(int i) {  VMFnResult* VMIntResultFunction::errorResult(int i) {
37      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
38      result.value = i;      result.value = i;
# Line 37  VMFnResult* VMIntResultFunction::success Line 45  VMFnResult* VMIntResultFunction::success
45      return &result;      return &result;
46  }  }
47    
48    ///////////////////////////////////////////////////////////////////////////
49    // class VMStringResultFunction
50    
51  VMFnResult* VMStringResultFunction::errorResult(const String& s) {  VMFnResult* VMStringResultFunction::errorResult(const String& s) {
52      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
53      result.value = s;      result.value = s;
# Line 49  VMFnResult* VMStringResultFunction::succ Line 60  VMFnResult* VMStringResultFunction::succ
60      return &result;      return &result;
61  }  }
62    
63    ///////////////////////////////////////////////////////////////////////////
64    // built-in script function:  message()
65    
66  bool CoreVMFunction_message::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_message::acceptsArgType(int iArg, ExprType_t type) const {
67      return type == INT_EXPR || type == STRING_EXPR;      return type == INT_EXPR || type == STRING_EXPR;
68  }  }
# Line 71  VMFnResult* CoreVMFunction_message::exec Line 85  VMFnResult* CoreVMFunction_message::exec
85      return errorResult();      return errorResult();
86  }  }
87    
88    ///////////////////////////////////////////////////////////////////////////
89    // built-in script function:  exit()
90    
91  VMFnResult* CoreVMFunction_exit::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_exit::exec(VMFnArgs* args) {
92      this->result.flags = STMT_ABORT_SIGNALLED;      this->result.flags = STMT_ABORT_SIGNALLED;
93      return &result;      return &result;
94  }  }
95    
96    ///////////////////////////////////////////////////////////////////////////
97    // built-in script function:  wait()
98    
99  VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) {
100      ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext());      ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext());
101      VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0));      VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0));
102      ctx->suspendMicroseconds = expr->evalInt();      int us = expr->evalInt();
103      this->result.flags = STMT_SUSPEND_SIGNALLED;      if (us < 0) {
104            wrnMsg("wait(): argument may not be negative! Aborting script!");
105            this->result.flags = STMT_ABORT_SIGNALLED;
106        } else if (us == 0) {
107            wrnMsg("wait(): argument may not be zero! Aborting script!");
108            this->result.flags = STMT_ABORT_SIGNALLED;
109        } else {
110            ctx->suspendMicroseconds = us;
111            this->result.flags = STMT_SUSPEND_SIGNALLED;
112        }
113      return &result;      return &result;
114  }  }
115    
116    ///////////////////////////////////////////////////////////////////////////
117    // built-in script function:  abs()
118    
119    bool CoreVMFunction_abs::acceptsArgType(int iArg, ExprType_t type) const {
120        return type == INT_EXPR;
121    }
122    
123    VMFnResult* CoreVMFunction_abs::exec(VMFnArgs* args) {
124        return successResult( ::abs(args->arg(0)->asInt()->evalInt()) );
125    }
126    
127    ///////////////////////////////////////////////////////////////////////////
128    // built-in script function:  random()
129    
130    bool CoreVMFunction_random::acceptsArgType(int iArg, ExprType_t type) const {
131        return type == INT_EXPR;
132    }
133    
134    VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) {
135        int iMin = args->arg(0)->asInt()->evalInt();
136        int iMax = args->arg(1)->asInt()->evalInt();
137        float f = float(::rand()) / float(RAND_MAX);
138        return successResult(
139            iMin + roundf( f * float(iMax - iMin) )
140        );
141    }
142    
143    ///////////////////////////////////////////////////////////////////////////
144    // built-in script function:  num_elements()
145    
146    bool CoreVMFunction_num_elements::acceptsArgType(int iArg, ExprType_t type) const {
147        return type == INT_ARR_EXPR;
148    }
149    
150    VMFnResult* CoreVMFunction_num_elements::exec(VMFnArgs* args) {
151        return successResult( args->arg(0)->asIntArray()->arraySize() );
152    }
153    
154    ///////////////////////////////////////////////////////////////////////////
155    // built-in script function:  inc()
156    
157    VMFnResult* CoreVMFunction_inc::exec(VMFnArgs* args) {
158        VMExpr* arg = args->arg(0);
159        VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);
160        VMVariable* out = dynamic_cast<VMVariable*>(arg);
161        if (!in || !out) successResult(0);
162        int i = in->evalInt() + 1;
163        IntLiteral tmp(i);
164        out->assignExpr(&tmp);
165        return successResult(i);
166    }
167    
168    ///////////////////////////////////////////////////////////////////////////
169    // built-in script function:  dec()
170    
171    VMFnResult* CoreVMFunction_dec::exec(VMFnArgs* args) {
172        VMExpr* arg = args->arg(0);
173        VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);
174        VMVariable* out = dynamic_cast<VMVariable*>(arg);
175        if (!in || !out) successResult(0);
176        int i = in->evalInt() - 1;
177        IntLiteral tmp(i);
178        out->assignExpr(&tmp);
179        return successResult(i);
180    }
181    
182    ///////////////////////////////////////////////////////////////////////////
183    // built-in script function:  in_range()
184    
185    VMFnResult* CoreVMFunction_in_range::exec(VMFnArgs* args) {
186        int i  = args->arg(0)->asInt()->evalInt();
187        int lo = args->arg(1)->asInt()->evalInt();
188        int hi = args->arg(2)->asInt()->evalInt();
189        if (lo > hi) { // swap lo and hi
190            int tmp = lo;
191            lo = hi;
192            hi = tmp;
193        }
194        return successResult(i >= lo && i <= hi);
195    }
196    
197    ///////////////////////////////////////////////////////////////////////////
198    // built-in script function:  sh_left()
199    
200    VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) {
201        int i = args->arg(0)->asInt()->evalInt();
202        int n = args->arg(1)->asInt()->evalInt();
203        return successResult(i << n);
204    }
205    
206    ///////////////////////////////////////////////////////////////////////////
207    // built-in script function:  sh_right()
208    
209    VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) {
210        int i = args->arg(0)->asInt()->evalInt();
211        int n = args->arg(1)->asInt()->evalInt();
212        return successResult(i >> n);
213    }
214    
215    ///////////////////////////////////////////////////////////////////////////
216    // built-in script function:  min()
217    
218    VMFnResult* CoreVMFunction_min::exec(VMFnArgs* args) {
219        int l = args->arg(0)->asInt()->evalInt();
220        int r = args->arg(1)->asInt()->evalInt();
221        return successResult(l < r ? l : r);
222    }
223    
224    ///////////////////////////////////////////////////////////////////////////
225    // built-in script function:  max()
226    
227    VMFnResult* CoreVMFunction_max::exec(VMFnArgs* args) {
228        int l = args->arg(0)->asInt()->evalInt();
229        int r = args->arg(1)->asInt()->evalInt();
230        return successResult(l > r ? l : r);
231    }
232    
233  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.2598  
changed lines
  Added in v.3076

  ViewVC Help
Powered by ViewVC