1 |
/* |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck |
* Copyright (c) 2014-2015 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
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; |
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; |
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; |
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 |
} |
} |
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)); |
104 |
return &result; |
return &result; |
105 |
} |
} |
106 |
|
|
107 |
|
/////////////////////////////////////////////////////////////////////////// |
108 |
|
// built-in script function: abs() |
109 |
|
|
110 |
|
bool CoreVMFunction_abs::acceptsArgType(int iArg, ExprType_t type) const { |
111 |
|
return type == INT_EXPR; |
112 |
|
} |
113 |
|
|
114 |
|
VMFnResult* CoreVMFunction_abs::exec(VMFnArgs* args) { |
115 |
|
return successResult( ::abs(args->arg(0)->asInt()->evalInt()) ); |
116 |
|
} |
117 |
|
|
118 |
|
/////////////////////////////////////////////////////////////////////////// |
119 |
|
// built-in script function: random() |
120 |
|
|
121 |
|
bool CoreVMFunction_random::acceptsArgType(int iArg, ExprType_t type) const { |
122 |
|
return type == INT_EXPR; |
123 |
|
} |
124 |
|
|
125 |
|
VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) { |
126 |
|
int iMin = args->arg(0)->asInt()->evalInt(); |
127 |
|
int iMax = args->arg(1)->asInt()->evalInt(); |
128 |
|
float f = float(::rand()) / float(RAND_MAX); |
129 |
|
return successResult( |
130 |
|
iMin + roundf( f * float(iMax - iMin) ) |
131 |
|
); |
132 |
|
} |
133 |
|
|
134 |
|
/////////////////////////////////////////////////////////////////////////// |
135 |
|
// built-in script function: num_elements() |
136 |
|
|
137 |
|
bool CoreVMFunction_num_elements::acceptsArgType(int iArg, ExprType_t type) const { |
138 |
|
return type == INT_ARR_EXPR; |
139 |
|
} |
140 |
|
|
141 |
|
VMFnResult* CoreVMFunction_num_elements::exec(VMFnArgs* args) { |
142 |
|
return successResult( args->arg(0)->asIntArray()->arraySize() ); |
143 |
|
} |
144 |
|
|
145 |
|
/////////////////////////////////////////////////////////////////////////// |
146 |
|
// built-in script function: inc() |
147 |
|
|
148 |
|
VMFnResult* CoreVMFunction_inc::exec(VMFnArgs* args) { |
149 |
|
VMExpr* arg = args->arg(0); |
150 |
|
VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg); |
151 |
|
VMVariable* out = dynamic_cast<VMVariable*>(arg); |
152 |
|
if (!in || !out) successResult(0); |
153 |
|
int i = in->evalInt() + 1; |
154 |
|
IntLiteral tmp(i); |
155 |
|
out->assignExpr(&tmp); |
156 |
|
return successResult(i); |
157 |
|
} |
158 |
|
|
159 |
|
/////////////////////////////////////////////////////////////////////////// |
160 |
|
// built-in script function: dec() |
161 |
|
|
162 |
|
VMFnResult* CoreVMFunction_dec::exec(VMFnArgs* args) { |
163 |
|
VMExpr* arg = args->arg(0); |
164 |
|
VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg); |
165 |
|
VMVariable* out = dynamic_cast<VMVariable*>(arg); |
166 |
|
if (!in || !out) successResult(0); |
167 |
|
int i = in->evalInt() - 1; |
168 |
|
IntLiteral tmp(i); |
169 |
|
out->assignExpr(&tmp); |
170 |
|
return successResult(i); |
171 |
|
} |
172 |
|
|
173 |
|
/////////////////////////////////////////////////////////////////////////// |
174 |
|
// built-in script function: sh_left() |
175 |
|
|
176 |
|
VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) { |
177 |
|
int i = args->arg(0)->asInt()->evalInt(); |
178 |
|
int n = args->arg(1)->asInt()->evalInt(); |
179 |
|
return successResult(i << n); |
180 |
|
} |
181 |
|
|
182 |
|
/////////////////////////////////////////////////////////////////////////// |
183 |
|
// built-in script function: sh_right() |
184 |
|
|
185 |
|
VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) { |
186 |
|
int i = args->arg(0)->asInt()->evalInt(); |
187 |
|
int n = args->arg(1)->asInt()->evalInt(); |
188 |
|
return successResult(i >> n); |
189 |
|
} |
190 |
|
|
191 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |