1 |
schoenebeck |
2581 |
/* -*- c++ -*- |
2 |
|
|
* |
3 |
schoenebeck |
3118 |
* Copyright (c) 2014 - 2017 Christian Schoenebeck and Andreas Persson |
4 |
schoenebeck |
2581 |
* |
5 |
|
|
* http://www.linuxsampler.org |
6 |
|
|
* |
7 |
|
|
* This file is part of LinuxSampler and released under the same terms. |
8 |
|
|
* See README file for details. |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
// This header defines VM core implementation internal data types only used |
12 |
|
|
// inside the parser and core VM implementation of this source directory. Not |
13 |
|
|
// intended to be used in other source code parts (other source code |
14 |
|
|
// directories) of the sampler. |
15 |
|
|
|
16 |
|
|
#ifndef LS_INSTRPARSERTREE_H |
17 |
|
|
#define LS_INSTRPARSERTREE_H |
18 |
|
|
|
19 |
|
|
#include <vector> |
20 |
|
|
#include <iostream> |
21 |
|
|
#include <map> |
22 |
|
|
#include <set> |
23 |
schoenebeck |
3208 |
#include <string.h> // for memset() |
24 |
schoenebeck |
2581 |
#include "../common/global.h" |
25 |
|
|
#include "../common/Ref.h" |
26 |
|
|
#include "../common/ArrayList.h" |
27 |
|
|
#include "common.h" |
28 |
|
|
|
29 |
|
|
namespace LinuxSampler { |
30 |
|
|
|
31 |
|
|
class ParserContext; |
32 |
|
|
class ExecContext; |
33 |
|
|
|
34 |
|
|
enum StmtType_t { |
35 |
|
|
STMT_LEAF, |
36 |
|
|
STMT_LIST, |
37 |
|
|
STMT_BRANCH, |
38 |
|
|
STMT_LOOP, |
39 |
schoenebeck |
3260 |
STMT_SYNC, |
40 |
schoenebeck |
3311 |
STMT_NOOP, |
41 |
schoenebeck |
2581 |
}; |
42 |
|
|
|
43 |
|
|
class Node { |
44 |
|
|
public: |
45 |
|
|
Node(); |
46 |
|
|
virtual ~Node(); |
47 |
|
|
virtual void dump(int level = 0) = 0; |
48 |
schoenebeck |
2645 |
virtual bool isPolyphonic() const = 0; |
49 |
schoenebeck |
2581 |
void printIndents(int n); |
50 |
|
|
}; |
51 |
|
|
typedef Ref<Node> NodeRef; |
52 |
|
|
|
53 |
|
|
class Expression : virtual public VMExpr, virtual public Node { |
54 |
|
|
public: |
55 |
|
|
virtual ExprType_t exprType() const = 0; |
56 |
|
|
virtual bool isConstExpr() const = 0; |
57 |
|
|
virtual String evalCastToStr() = 0; |
58 |
|
|
}; |
59 |
|
|
typedef Ref<Expression,Node> ExpressionRef; |
60 |
|
|
|
61 |
|
|
class IntExpr : virtual public VMIntExpr, virtual public Expression { |
62 |
|
|
public: |
63 |
|
|
ExprType_t exprType() const { return INT_EXPR; } |
64 |
|
|
virtual int evalInt() = 0; |
65 |
|
|
String evalCastToStr(); |
66 |
|
|
}; |
67 |
|
|
typedef Ref<IntExpr,Node> IntExprRef; |
68 |
|
|
|
69 |
schoenebeck |
3293 |
class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression { |
70 |
schoenebeck |
3056 |
public: |
71 |
|
|
ExprType_t exprType() const { return INT_ARR_EXPR; } |
72 |
|
|
String evalCastToStr(); |
73 |
|
|
}; |
74 |
schoenebeck |
3293 |
typedef Ref<IntArrayExpr,Node> IntArrayExprRef; |
75 |
schoenebeck |
3056 |
|
76 |
schoenebeck |
2581 |
class StringExpr : virtual public VMStringExpr, virtual public Expression { |
77 |
|
|
public: |
78 |
|
|
ExprType_t exprType() const { return STRING_EXPR; } |
79 |
|
|
virtual String evalStr() = 0; |
80 |
|
|
String evalCastToStr() { return evalStr(); } |
81 |
|
|
}; |
82 |
|
|
typedef Ref<StringExpr,Node> StringExprRef; |
83 |
|
|
|
84 |
|
|
class IntLiteral : virtual public IntExpr { |
85 |
|
|
int value; |
86 |
|
|
public: |
87 |
|
|
IntLiteral(int value) : value(value) { } |
88 |
|
|
int evalInt(); |
89 |
|
|
void dump(int level = 0); |
90 |
|
|
bool isConstExpr() const { return true; } |
91 |
schoenebeck |
2645 |
bool isPolyphonic() const { return false; } |
92 |
schoenebeck |
2581 |
}; |
93 |
|
|
typedef Ref<IntLiteral,Node> IntLiteralRef; |
94 |
|
|
|
95 |
|
|
class StringLiteral : virtual public StringExpr { |
96 |
|
|
public: |
97 |
|
|
String value; |
98 |
|
|
StringLiteral(const String& value) : value(value) { } |
99 |
|
|
bool isConstExpr() const { return true; } |
100 |
|
|
void dump(int level = 0); |
101 |
|
|
String evalStr() { return value; } |
102 |
schoenebeck |
2645 |
bool isPolyphonic() const { return false; } |
103 |
schoenebeck |
2581 |
}; |
104 |
|
|
typedef Ref<StringLiteral,Node> StringLiteralRef; |
105 |
|
|
|
106 |
|
|
class Args : virtual public VMFnArgs, virtual public Node { |
107 |
|
|
public: |
108 |
|
|
std::vector<ExpressionRef> args; |
109 |
|
|
void add(ExpressionRef arg) { args.push_back(arg); } |
110 |
|
|
void dump(int level = 0); |
111 |
schoenebeck |
3054 |
int argsCount() const { return (int) args.size(); } |
112 |
schoenebeck |
2581 |
VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; } |
113 |
schoenebeck |
2645 |
bool isPolyphonic() const; |
114 |
schoenebeck |
2581 |
}; |
115 |
|
|
typedef Ref<Args,Node> ArgsRef; |
116 |
|
|
|
117 |
schoenebeck |
2945 |
class Variable : virtual public VMVariable, virtual public Expression { |
118 |
schoenebeck |
2581 |
public: |
119 |
schoenebeck |
2945 |
bool isConstExpr() const OVERRIDE { return bConst; } |
120 |
|
|
bool isAssignable() const OVERRIDE { return !bConst; } |
121 |
schoenebeck |
2581 |
virtual void assign(Expression* expr) = 0; |
122 |
schoenebeck |
2945 |
void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); } |
123 |
schoenebeck |
2581 |
protected: |
124 |
|
|
Variable(ParserContext* ctx, int _memPos, bool _bConst) |
125 |
|
|
: context(ctx), memPos(_memPos), bConst(_bConst) {} |
126 |
|
|
|
127 |
|
|
ParserContext* context; |
128 |
|
|
int memPos; |
129 |
|
|
bool bConst; |
130 |
|
|
}; |
131 |
|
|
typedef Ref<Variable,Node> VariableRef; |
132 |
|
|
|
133 |
|
|
class IntVariable : public Variable, virtual public IntExpr { |
134 |
|
|
bool polyphonic; |
135 |
|
|
public: |
136 |
|
|
IntVariable(ParserContext* ctx); |
137 |
|
|
void assign(Expression* expr); |
138 |
|
|
int evalInt(); |
139 |
|
|
void dump(int level = 0); |
140 |
|
|
bool isPolyphonic() const { return polyphonic; } |
141 |
|
|
protected: |
142 |
|
|
IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size = 1); |
143 |
|
|
}; |
144 |
|
|
typedef Ref<IntVariable,Node> IntVariableRef; |
145 |
|
|
|
146 |
|
|
class ConstIntVariable : public IntVariable { |
147 |
|
|
public: |
148 |
|
|
int value; |
149 |
|
|
|
150 |
|
|
ConstIntVariable(int value); |
151 |
|
|
//ConstIntVariable(ParserContext* ctx, int value = 0); |
152 |
|
|
void assign(Expression* expr); |
153 |
|
|
int evalInt(); |
154 |
|
|
void dump(int level = 0); |
155 |
|
|
}; |
156 |
|
|
typedef Ref<ConstIntVariable,Node> ConstIntVariableRef; |
157 |
|
|
|
158 |
schoenebeck |
2594 |
class BuiltInIntVariable : public IntVariable { |
159 |
|
|
String name; |
160 |
|
|
VMIntRelPtr* ptr; |
161 |
|
|
public: |
162 |
|
|
BuiltInIntVariable(const String& name, VMIntRelPtr* ptr); |
163 |
schoenebeck |
2948 |
bool isAssignable() const OVERRIDE { return !ptr->readonly; } |
164 |
schoenebeck |
3054 |
void assign(Expression* expr) OVERRIDE; |
165 |
|
|
int evalInt() OVERRIDE; |
166 |
|
|
void dump(int level = 0) OVERRIDE; |
167 |
schoenebeck |
2594 |
}; |
168 |
|
|
typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef; |
169 |
|
|
|
170 |
schoenebeck |
2581 |
class PolyphonicIntVariable : public IntVariable { |
171 |
|
|
public: |
172 |
|
|
PolyphonicIntVariable(ParserContext* ctx); |
173 |
|
|
void dump(int level = 0); |
174 |
|
|
}; |
175 |
|
|
typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef; |
176 |
|
|
|
177 |
schoenebeck |
3293 |
class IntArrayVariable : public Variable, virtual public IntArrayExpr { |
178 |
schoenebeck |
2581 |
ArrayList<int> values; |
179 |
|
|
public: |
180 |
|
|
IntArrayVariable(ParserContext* ctx, int size); |
181 |
schoenebeck |
3257 |
IntArrayVariable(ParserContext* ctx, int size, ArgsRef values, bool _bConst = false); |
182 |
schoenebeck |
2581 |
void assign(Expression* expr) {} // ignore scalar assignment |
183 |
schoenebeck |
2594 |
String evalCastToStr() { return ""; } // ignore scalar cast to string |
184 |
schoenebeck |
2581 |
ExprType_t exprType() const { return INT_ARR_EXPR; } |
185 |
schoenebeck |
2594 |
virtual int arraySize() const { return values.size(); } |
186 |
|
|
virtual int evalIntElement(uint i); |
187 |
|
|
virtual void assignIntElement(uint i, int value); |
188 |
|
|
void dump(int level = 0); |
189 |
schoenebeck |
2645 |
bool isPolyphonic() const { return false; } |
190 |
schoenebeck |
2594 |
protected: |
191 |
|
|
IntArrayVariable(ParserContext* ctx, bool bConst); |
192 |
|
|
}; |
193 |
|
|
typedef Ref<IntArrayVariable,Node> IntArrayVariableRef; |
194 |
|
|
|
195 |
|
|
class BuiltInIntArrayVariable : public IntArrayVariable { |
196 |
|
|
String name; |
197 |
|
|
VMInt8Array* array; |
198 |
|
|
public: |
199 |
|
|
BuiltInIntArrayVariable(const String& name, VMInt8Array* array); |
200 |
|
|
int arraySize() const { return array->size; } |
201 |
schoenebeck |
2581 |
int evalIntElement(uint i); |
202 |
schoenebeck |
3253 |
bool isAssignable() const OVERRIDE { return !array->readonly; } |
203 |
schoenebeck |
2581 |
void assignIntElement(uint i, int value); |
204 |
|
|
void dump(int level = 0); |
205 |
|
|
}; |
206 |
schoenebeck |
2594 |
typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef; |
207 |
schoenebeck |
2581 |
|
208 |
|
|
class IntArrayElement : public IntVariable { |
209 |
schoenebeck |
3293 |
IntArrayExprRef array; |
210 |
schoenebeck |
2581 |
IntExprRef index; |
211 |
|
|
public: |
212 |
schoenebeck |
3293 |
IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex); |
213 |
schoenebeck |
2581 |
void assign(Expression* expr); |
214 |
|
|
int evalInt(); |
215 |
|
|
void dump(int level = 0); |
216 |
|
|
}; |
217 |
|
|
typedef Ref<IntArrayElement,Node> IntArrayElementRef; |
218 |
|
|
|
219 |
|
|
class StringVariable : public Variable, virtual public StringExpr { |
220 |
|
|
public: |
221 |
|
|
StringVariable(ParserContext* ctx); |
222 |
|
|
void assign(Expression* expr); |
223 |
|
|
String evalStr(); |
224 |
|
|
void dump(int level = 0); |
225 |
schoenebeck |
2645 |
bool isPolyphonic() const { return false; } |
226 |
schoenebeck |
2581 |
protected: |
227 |
|
|
StringVariable(ParserContext* ctx, bool bConst); |
228 |
|
|
}; |
229 |
|
|
typedef Ref<StringVariable,Node> StringVariableRef; |
230 |
|
|
|
231 |
|
|
class ConstStringVariable : public StringVariable { |
232 |
|
|
public: |
233 |
|
|
String value; |
234 |
|
|
|
235 |
|
|
ConstStringVariable(ParserContext* ctx, String value = ""); |
236 |
|
|
void assign(Expression* expr); |
237 |
|
|
String evalStr(); |
238 |
|
|
void dump(int level = 0); |
239 |
|
|
}; |
240 |
|
|
typedef Ref<ConstStringVariable,Node> ConstStringVariableRef; |
241 |
|
|
|
242 |
|
|
class BinaryOp : virtual public Expression { |
243 |
|
|
protected: |
244 |
|
|
ExpressionRef lhs; |
245 |
|
|
ExpressionRef rhs; |
246 |
|
|
public: |
247 |
|
|
BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { } |
248 |
|
|
bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); } |
249 |
schoenebeck |
2645 |
bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); } |
250 |
schoenebeck |
2581 |
}; |
251 |
|
|
typedef Ref<BinaryOp,Node> BinaryOpRef; |
252 |
|
|
|
253 |
|
|
class Add : virtual public BinaryOp, virtual public IntExpr { |
254 |
|
|
public: |
255 |
|
|
Add(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { } |
256 |
|
|
int evalInt(); |
257 |
|
|
void dump(int level = 0); |
258 |
|
|
}; |
259 |
|
|
typedef Ref<Add,Node> AddRef; |
260 |
|
|
|
261 |
|
|
class Sub : virtual public BinaryOp, virtual public IntExpr { |
262 |
|
|
public: |
263 |
|
|
Sub(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { } |
264 |
|
|
int evalInt(); |
265 |
|
|
void dump(int level = 0); |
266 |
|
|
}; |
267 |
|
|
typedef Ref<Sub,Node> SubRef; |
268 |
|
|
|
269 |
|
|
class Mul : virtual public BinaryOp, virtual public IntExpr { |
270 |
|
|
public: |
271 |
|
|
Mul(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { } |
272 |
|
|
int evalInt(); |
273 |
|
|
void dump(int level = 0); |
274 |
|
|
}; |
275 |
|
|
typedef Ref<Mul,Node> MulRef; |
276 |
|
|
|
277 |
|
|
class Div : virtual public BinaryOp, virtual public IntExpr { |
278 |
|
|
public: |
279 |
|
|
Div(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { } |
280 |
|
|
int evalInt(); |
281 |
|
|
void dump(int level = 0); |
282 |
|
|
}; |
283 |
|
|
typedef Ref<Div,Node> DivRef; |
284 |
|
|
|
285 |
|
|
class Mod : virtual public BinaryOp, virtual public IntExpr { |
286 |
|
|
public: |
287 |
|
|
Mod(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { } |
288 |
|
|
int evalInt(); |
289 |
|
|
void dump(int level = 0); |
290 |
|
|
}; |
291 |
|
|
typedef Ref<Mod,Node> ModRef; |
292 |
|
|
|
293 |
|
|
class Statement : virtual public Node { |
294 |
|
|
public: |
295 |
|
|
virtual StmtType_t statementType() const = 0; |
296 |
|
|
}; |
297 |
|
|
typedef Ref<Statement,Node> StatementRef; |
298 |
|
|
|
299 |
|
|
// Just used by parser to avoid "not a statement" parser warning, will be |
300 |
|
|
// filtered out by parser. So it will not be part of the VM tree after parsing. |
301 |
|
|
class NoOperation : public Statement { |
302 |
|
|
public: |
303 |
|
|
NoOperation() : Statement() {} |
304 |
schoenebeck |
3311 |
StmtType_t statementType() const { return STMT_NOOP; } |
305 |
schoenebeck |
2581 |
void dump(int level = 0) {} |
306 |
schoenebeck |
2645 |
bool isPolyphonic() const { return false; } |
307 |
schoenebeck |
2581 |
}; |
308 |
|
|
typedef Ref<NoOperation,Node> NoOperationRef; |
309 |
|
|
|
310 |
|
|
bool isNoOperation(StatementRef statement); |
311 |
|
|
|
312 |
|
|
class LeafStatement : public Statement { |
313 |
|
|
public: |
314 |
|
|
virtual StmtFlags_t exec() = 0; |
315 |
|
|
virtual StmtType_t statementType() const { return STMT_LEAF; } |
316 |
|
|
}; |
317 |
|
|
typedef Ref<LeafStatement,Node> LeafStatementRef; |
318 |
|
|
|
319 |
|
|
class Statements : public Statement { |
320 |
|
|
std::vector<StatementRef> args; |
321 |
|
|
public: |
322 |
|
|
void add(StatementRef arg) { args.push_back(arg); } |
323 |
|
|
void dump(int level = 0); |
324 |
|
|
StmtType_t statementType() const { return STMT_LIST; } |
325 |
|
|
virtual Statement* statement(uint i); |
326 |
schoenebeck |
2645 |
bool isPolyphonic() const; |
327 |
schoenebeck |
2581 |
}; |
328 |
|
|
typedef Ref<Statements,Node> StatementsRef; |
329 |
|
|
|
330 |
|
|
class BranchStatement : public Statement { |
331 |
|
|
public: |
332 |
|
|
StmtType_t statementType() const { return STMT_BRANCH; } |
333 |
|
|
virtual int evalBranch() = 0; |
334 |
|
|
virtual Statements* branch(uint i) const = 0; |
335 |
|
|
}; |
336 |
|
|
|
337 |
schoenebeck |
3293 |
class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr, virtual public IntArrayExpr { |
338 |
schoenebeck |
2942 |
VMDynVar* dynVar; |
339 |
|
|
String varName; |
340 |
|
|
public: |
341 |
|
|
DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v); |
342 |
|
|
ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); } |
343 |
|
|
bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); } |
344 |
|
|
bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); } |
345 |
|
|
bool isPolyphonic() const OVERRIDE { return false; } |
346 |
schoenebeck |
2945 |
void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); } |
347 |
schoenebeck |
3118 |
VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); } |
348 |
schoenebeck |
2942 |
int evalInt() OVERRIDE; |
349 |
|
|
String evalStr() OVERRIDE; |
350 |
|
|
String evalCastToStr() OVERRIDE; |
351 |
schoenebeck |
3293 |
int arraySize() const OVERRIDE { return dynVar->asIntArray()->arraySize(); } |
352 |
|
|
int evalIntElement(uint i) OVERRIDE { return dynVar->asIntArray()->evalIntElement(i); } |
353 |
|
|
void assignIntElement(uint i, int value) { return dynVar->asIntArray()->assignIntElement(i, value); } |
354 |
schoenebeck |
2942 |
void dump(int level = 0) OVERRIDE; |
355 |
|
|
}; |
356 |
|
|
typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef; |
357 |
|
|
|
358 |
schoenebeck |
2581 |
class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr { |
359 |
|
|
String functionName; |
360 |
|
|
ArgsRef args; |
361 |
|
|
VMFunction* fn; |
362 |
|
|
public: |
363 |
|
|
FunctionCall(const char* function, ArgsRef args, VMFunction* fn) : |
364 |
|
|
functionName(function), args(args), fn(fn) { } |
365 |
schoenebeck |
3060 |
void dump(int level = 0) OVERRIDE; |
366 |
|
|
StmtFlags_t exec() OVERRIDE; |
367 |
|
|
int evalInt() OVERRIDE; |
368 |
schoenebeck |
3056 |
VMIntArrayExpr* asIntArray() const OVERRIDE; |
369 |
schoenebeck |
3060 |
String evalStr() OVERRIDE; |
370 |
|
|
bool isConstExpr() const OVERRIDE { return false; } |
371 |
|
|
ExprType_t exprType() const OVERRIDE; |
372 |
|
|
String evalCastToStr() OVERRIDE; |
373 |
|
|
bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); } |
374 |
schoenebeck |
2581 |
protected: |
375 |
|
|
VMFnResult* execVMFn(); |
376 |
|
|
}; |
377 |
|
|
typedef Ref<FunctionCall,Node> FunctionCallRef; |
378 |
|
|
|
379 |
schoenebeck |
3311 |
class NoFunctionCall : public FunctionCall { |
380 |
|
|
public: |
381 |
|
|
NoFunctionCall() : FunctionCall("nothing", new Args, NULL) {} |
382 |
|
|
StmtType_t statementType() const { return STMT_NOOP; } |
383 |
|
|
}; |
384 |
|
|
typedef Ref<NoFunctionCall,Node> NoFunctionCallRef; |
385 |
|
|
|
386 |
schoenebeck |
2581 |
class EventHandler : virtual public Statements, virtual public VMEventHandler { |
387 |
|
|
StatementsRef statements; |
388 |
schoenebeck |
2645 |
bool usingPolyphonics; |
389 |
schoenebeck |
2581 |
public: |
390 |
|
|
void dump(int level = 0); |
391 |
|
|
StmtFlags_t exec(); |
392 |
schoenebeck |
2645 |
EventHandler(StatementsRef statements); |
393 |
schoenebeck |
2581 |
Statement* statement(uint i) { return statements->statement(i); } |
394 |
schoenebeck |
2645 |
bool isPolyphonic() const { return usingPolyphonics; } |
395 |
schoenebeck |
2581 |
}; |
396 |
|
|
typedef Ref<EventHandler,Node> EventHandlerRef; |
397 |
|
|
|
398 |
|
|
class OnNote : public EventHandler { |
399 |
|
|
public: |
400 |
|
|
OnNote(StatementsRef statements) : EventHandler(statements) {} |
401 |
schoenebeck |
2879 |
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_NOTE; } |
402 |
schoenebeck |
2581 |
String eventHandlerName() const { return "note"; } |
403 |
|
|
}; |
404 |
|
|
typedef Ref<OnNote,Node> OnNoteRef; |
405 |
|
|
|
406 |
|
|
class OnInit : public EventHandler { |
407 |
|
|
public: |
408 |
|
|
OnInit(StatementsRef statements) : EventHandler(statements) {} |
409 |
schoenebeck |
2879 |
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_INIT; } |
410 |
schoenebeck |
2581 |
String eventHandlerName() const { return "init"; } |
411 |
|
|
}; |
412 |
|
|
typedef Ref<OnInit,Node> OnInitRef; |
413 |
|
|
|
414 |
|
|
class OnRelease : public EventHandler { |
415 |
|
|
public: |
416 |
|
|
OnRelease(StatementsRef statements) : EventHandler(statements) {} |
417 |
schoenebeck |
2879 |
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_RELEASE; } |
418 |
schoenebeck |
2581 |
String eventHandlerName() const { return "release"; } |
419 |
|
|
}; |
420 |
|
|
typedef Ref<OnRelease,Node> OnReleaseRef; |
421 |
|
|
|
422 |
|
|
class OnController : public EventHandler { |
423 |
|
|
public: |
424 |
|
|
OnController(StatementsRef statements) : EventHandler(statements) {} |
425 |
schoenebeck |
2879 |
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_CONTROLLER; } |
426 |
schoenebeck |
2581 |
String eventHandlerName() const { return "controller"; } |
427 |
|
|
}; |
428 |
|
|
typedef Ref<OnController,Node> OnControllerRef; |
429 |
|
|
|
430 |
|
|
class EventHandlers : virtual public Node { |
431 |
|
|
std::vector<EventHandlerRef> args; |
432 |
|
|
public: |
433 |
|
|
EventHandlers(); |
434 |
|
|
~EventHandlers(); |
435 |
|
|
void add(EventHandlerRef arg); |
436 |
|
|
void dump(int level = 0); |
437 |
|
|
int evalInt() { return 0; } |
438 |
|
|
EventHandler* eventHandlerByName(const String& name) const; |
439 |
|
|
EventHandler* eventHandler(uint index) const; |
440 |
schoenebeck |
3054 |
inline uint size() const { return (int) args.size(); } |
441 |
schoenebeck |
2645 |
bool isPolyphonic() const; |
442 |
schoenebeck |
2581 |
}; |
443 |
|
|
typedef Ref<EventHandlers,Node> EventHandlersRef; |
444 |
|
|
|
445 |
|
|
class Assignment : public LeafStatement { |
446 |
|
|
protected: |
447 |
|
|
VariableRef variable; |
448 |
|
|
ExpressionRef value; |
449 |
|
|
public: |
450 |
|
|
Assignment(VariableRef variable, ExpressionRef value); |
451 |
|
|
void dump(int level = 0); |
452 |
|
|
StmtFlags_t exec(); |
453 |
schoenebeck |
2871 |
bool isPolyphonic() const { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); } |
454 |
schoenebeck |
2581 |
}; |
455 |
|
|
typedef Ref<Assignment,Node> AssignmentRef; |
456 |
|
|
|
457 |
|
|
class If : public BranchStatement { |
458 |
|
|
IntExprRef condition; |
459 |
|
|
StatementsRef ifStatements; |
460 |
|
|
StatementsRef elseStatements; |
461 |
|
|
public: |
462 |
|
|
If(IntExprRef condition, StatementsRef ifStatements, StatementsRef elseStatements) : |
463 |
|
|
condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { } |
464 |
|
|
If(IntExprRef condition, StatementsRef statements) : |
465 |
|
|
condition(condition), ifStatements(statements) { } |
466 |
|
|
void dump(int level = 0); |
467 |
|
|
int evalBranch(); |
468 |
|
|
Statements* branch(uint i) const; |
469 |
schoenebeck |
2645 |
bool isPolyphonic() const; |
470 |
schoenebeck |
2581 |
}; |
471 |
|
|
typedef Ref<If,Node> IfRef; |
472 |
|
|
|
473 |
|
|
struct CaseBranch { |
474 |
|
|
IntExprRef from; |
475 |
|
|
IntExprRef to; |
476 |
|
|
StatementsRef statements; |
477 |
|
|
}; |
478 |
|
|
|
479 |
|
|
typedef std::vector<CaseBranch> CaseBranches; |
480 |
|
|
|
481 |
|
|
class SelectCase : public BranchStatement { |
482 |
|
|
IntExprRef select; |
483 |
|
|
CaseBranches branches; |
484 |
|
|
public: |
485 |
|
|
SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { } |
486 |
|
|
void dump(int level = 0); |
487 |
|
|
int evalBranch(); |
488 |
|
|
Statements* branch(uint i) const; |
489 |
|
|
//void addBranch(IntExprRef condition, StatementsRef statements); |
490 |
|
|
//void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements); |
491 |
|
|
//void addBranch(CaseBranchRef branch); |
492 |
|
|
//void addBranches(CaseBranchesRef branches); |
493 |
schoenebeck |
2645 |
bool isPolyphonic() const; |
494 |
schoenebeck |
2581 |
}; |
495 |
|
|
typedef Ref<SelectCase,Node> SelectCaseRef; |
496 |
|
|
|
497 |
|
|
class While : public Statement { |
498 |
|
|
IntExprRef m_condition; |
499 |
|
|
StatementsRef m_statements; |
500 |
|
|
public: |
501 |
|
|
While(IntExprRef condition, StatementsRef statements) : |
502 |
|
|
m_condition(condition), m_statements(statements) {} |
503 |
|
|
StmtType_t statementType() const { return STMT_LOOP; } |
504 |
|
|
void dump(int level = 0); |
505 |
|
|
bool evalLoopStartCondition(); |
506 |
|
|
Statements* statements() const; |
507 |
schoenebeck |
2645 |
bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); } |
508 |
schoenebeck |
2581 |
}; |
509 |
|
|
|
510 |
schoenebeck |
3260 |
class SyncBlock : public Statement { |
511 |
|
|
StatementsRef m_statements; |
512 |
|
|
public: |
513 |
|
|
SyncBlock(StatementsRef statements) : m_statements(statements) {} |
514 |
|
|
StmtType_t statementType() const { return STMT_SYNC; } |
515 |
|
|
void dump(int level = 0); |
516 |
|
|
Statements* statements() const; |
517 |
|
|
bool isPolyphonic() const { return m_statements->isPolyphonic(); } |
518 |
|
|
}; |
519 |
|
|
typedef Ref<SyncBlock,Node> SyncBlockRef; |
520 |
|
|
|
521 |
schoenebeck |
2581 |
class Neg : public IntExpr { |
522 |
|
|
IntExprRef expr; |
523 |
|
|
public: |
524 |
|
|
Neg(IntExprRef expr) : expr(expr) { } |
525 |
|
|
int evalInt() { return (expr) ? -expr->evalInt() : 0; } |
526 |
|
|
void dump(int level = 0); |
527 |
|
|
bool isConstExpr() const { return expr->isConstExpr(); } |
528 |
schoenebeck |
2645 |
bool isPolyphonic() const { return expr->isPolyphonic(); } |
529 |
schoenebeck |
2581 |
}; |
530 |
|
|
typedef Ref<Neg,Node> NegRef; |
531 |
|
|
|
532 |
|
|
class ConcatString : public StringExpr { |
533 |
|
|
ExpressionRef lhs; |
534 |
|
|
ExpressionRef rhs; |
535 |
|
|
public: |
536 |
|
|
ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {} |
537 |
|
|
String evalStr(); |
538 |
|
|
void dump(int level = 0); |
539 |
|
|
bool isConstExpr() const; |
540 |
schoenebeck |
2645 |
bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); } |
541 |
schoenebeck |
2581 |
}; |
542 |
|
|
typedef Ref<ConcatString,Node> ConcatStringRef; |
543 |
|
|
|
544 |
|
|
class Relation : public IntExpr { |
545 |
|
|
public: |
546 |
|
|
enum Type { |
547 |
|
|
LESS_THAN, |
548 |
|
|
GREATER_THAN, |
549 |
|
|
LESS_OR_EQUAL, |
550 |
|
|
GREATER_OR_EQUAL, |
551 |
|
|
EQUAL, |
552 |
|
|
NOT_EQUAL |
553 |
|
|
}; |
554 |
|
|
Relation(IntExprRef lhs, Type type, IntExprRef rhs) : |
555 |
|
|
lhs(lhs), rhs(rhs), type(type) {} |
556 |
|
|
int evalInt(); |
557 |
|
|
void dump(int level = 0); |
558 |
|
|
bool isConstExpr() const; |
559 |
schoenebeck |
2645 |
bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); } |
560 |
schoenebeck |
2581 |
private: |
561 |
|
|
IntExprRef lhs; |
562 |
|
|
IntExprRef rhs; |
563 |
|
|
Type type; |
564 |
|
|
}; |
565 |
|
|
typedef Ref<Relation,Node> RelationRef; |
566 |
|
|
|
567 |
|
|
class Or : virtual public BinaryOp, virtual public IntExpr { |
568 |
|
|
public: |
569 |
|
|
Or(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
570 |
|
|
int evalInt(); |
571 |
|
|
void dump(int level = 0); |
572 |
|
|
}; |
573 |
|
|
typedef Ref<Or,Node> OrRef; |
574 |
|
|
|
575 |
schoenebeck |
2935 |
class BitwiseOr : virtual public BinaryOp, virtual public IntExpr { |
576 |
|
|
public: |
577 |
|
|
BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
578 |
|
|
int evalInt(); |
579 |
|
|
void dump(int level = 0); |
580 |
|
|
}; |
581 |
|
|
typedef Ref<BitwiseOr,Node> BitwiseOrRef; |
582 |
|
|
|
583 |
schoenebeck |
2581 |
class And : virtual public BinaryOp, virtual public IntExpr { |
584 |
|
|
public: |
585 |
|
|
And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
586 |
|
|
int evalInt(); |
587 |
|
|
void dump(int level = 0); |
588 |
|
|
}; |
589 |
|
|
typedef Ref<And,Node> AndRef; |
590 |
|
|
|
591 |
schoenebeck |
2935 |
class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr { |
592 |
|
|
public: |
593 |
|
|
BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
594 |
|
|
int evalInt(); |
595 |
|
|
void dump(int level = 0); |
596 |
|
|
}; |
597 |
|
|
typedef Ref<BitwiseAnd,Node> BitwiseAndRef; |
598 |
|
|
|
599 |
schoenebeck |
2581 |
class Not : virtual public IntExpr { |
600 |
|
|
IntExprRef expr; |
601 |
|
|
public: |
602 |
|
|
Not(IntExprRef expr) : expr(expr) {} |
603 |
|
|
int evalInt() { return !expr->evalInt(); } |
604 |
|
|
void dump(int level = 0); |
605 |
|
|
bool isConstExpr() const { return expr->isConstExpr(); } |
606 |
schoenebeck |
2645 |
bool isPolyphonic() const { return expr->isPolyphonic(); } |
607 |
schoenebeck |
2581 |
}; |
608 |
|
|
typedef Ref<Not,Node> NotRef; |
609 |
|
|
|
610 |
schoenebeck |
2935 |
class BitwiseNot : virtual public IntExpr { |
611 |
|
|
IntExprRef expr; |
612 |
|
|
public: |
613 |
|
|
BitwiseNot(IntExprRef expr) : expr(expr) {} |
614 |
|
|
int evalInt() { return ~expr->evalInt(); } |
615 |
|
|
void dump(int level = 0); |
616 |
|
|
bool isConstExpr() const { return expr->isConstExpr(); } |
617 |
|
|
bool isPolyphonic() const { return expr->isPolyphonic(); } |
618 |
|
|
}; |
619 |
|
|
typedef Ref<BitwiseNot,Node> BitwiseNotRef; |
620 |
|
|
|
621 |
schoenebeck |
2581 |
class ParserContext : public VMParserContext { |
622 |
|
|
public: |
623 |
|
|
struct Error { |
624 |
|
|
String txt; |
625 |
|
|
int line; |
626 |
|
|
}; |
627 |
|
|
typedef Error Warning; |
628 |
|
|
|
629 |
|
|
void* scanner; |
630 |
|
|
std::istream* is; |
631 |
schoenebeck |
2588 |
std::vector<ParserIssue> vErrors; |
632 |
|
|
std::vector<ParserIssue> vWarnings; |
633 |
|
|
std::vector<ParserIssue> vIssues; |
634 |
schoenebeck |
3285 |
std::vector<CodeBlock> vPreprocessorComments; |
635 |
schoenebeck |
2581 |
|
636 |
|
|
std::set<String> builtinPreprocessorConditions; |
637 |
|
|
std::set<String> userPreprocessorConditions; |
638 |
|
|
|
639 |
|
|
std::map<String,VariableRef> vartable; |
640 |
schoenebeck |
2951 |
std::map<String,StatementsRef> userFnTable; |
641 |
schoenebeck |
2581 |
int globalIntVarCount; |
642 |
|
|
int globalStrVarCount; |
643 |
|
|
int polyphonicIntVarCount; |
644 |
|
|
|
645 |
|
|
EventHandlersRef handlers; |
646 |
|
|
|
647 |
|
|
OnInitRef onInit; |
648 |
|
|
OnNoteRef onNote; |
649 |
|
|
OnReleaseRef onRelease; |
650 |
|
|
OnControllerRef onController; |
651 |
|
|
|
652 |
|
|
ArrayList<int>* globalIntMemory; |
653 |
|
|
ArrayList<String>* globalStrMemory; |
654 |
schoenebeck |
2588 |
int requiredMaxStackSize; |
655 |
schoenebeck |
2581 |
|
656 |
|
|
VMFunctionProvider* functionProvider; |
657 |
|
|
|
658 |
|
|
ExecContext* execContext; |
659 |
|
|
|
660 |
|
|
ParserContext(VMFunctionProvider* parent) : |
661 |
|
|
scanner(NULL), is(NULL), |
662 |
|
|
globalIntVarCount(0), globalStrVarCount(0), polyphonicIntVarCount(0), |
663 |
schoenebeck |
2588 |
globalIntMemory(NULL), globalStrMemory(NULL), requiredMaxStackSize(-1), |
664 |
|
|
functionProvider(parent), execContext(NULL) |
665 |
schoenebeck |
2581 |
{ |
666 |
|
|
} |
667 |
schoenebeck |
2588 |
virtual ~ParserContext(); |
668 |
schoenebeck |
2581 |
VariableRef globalVar(const String& name); |
669 |
|
|
IntVariableRef globalIntVar(const String& name); |
670 |
|
|
StringVariableRef globalStrVar(const String& name); |
671 |
|
|
VariableRef variableByName(const String& name); |
672 |
schoenebeck |
2951 |
StatementsRef userFunctionByName(const String& name); |
673 |
schoenebeck |
2889 |
void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
674 |
|
|
void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
675 |
schoenebeck |
3285 |
void addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn); |
676 |
schoenebeck |
2581 |
void createScanner(std::istream* is); |
677 |
|
|
void destroyScanner(); |
678 |
|
|
bool setPreprocessorCondition(const char* name); |
679 |
|
|
bool resetPreprocessorCondition(const char* name); |
680 |
|
|
bool isPreprocessorConditionSet(const char* name); |
681 |
schoenebeck |
2588 |
std::vector<ParserIssue> issues() const OVERRIDE; |
682 |
|
|
std::vector<ParserIssue> errors() const OVERRIDE; |
683 |
|
|
std::vector<ParserIssue> warnings() const OVERRIDE; |
684 |
schoenebeck |
3285 |
std::vector<CodeBlock> preprocessorComments() const OVERRIDE; |
685 |
schoenebeck |
2588 |
VMEventHandler* eventHandler(uint index) OVERRIDE; |
686 |
|
|
VMEventHandler* eventHandlerByName(const String& name) OVERRIDE; |
687 |
schoenebeck |
2594 |
void registerBuiltInConstIntVariables(const std::map<String,int>& vars); |
688 |
|
|
void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars); |
689 |
|
|
void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars); |
690 |
schoenebeck |
2942 |
void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars); |
691 |
schoenebeck |
2581 |
}; |
692 |
|
|
|
693 |
|
|
class ExecContext : public VMExecContext { |
694 |
|
|
public: |
695 |
|
|
struct StackFrame { |
696 |
|
|
Statement* statement; |
697 |
|
|
int subindex; |
698 |
|
|
|
699 |
|
|
StackFrame() { |
700 |
|
|
statement = NULL; |
701 |
|
|
subindex = -1; |
702 |
|
|
} |
703 |
|
|
}; |
704 |
|
|
|
705 |
|
|
ArrayList<int> polyphonicIntMemory; |
706 |
|
|
VMExecStatus_t status; |
707 |
schoenebeck |
3277 |
StmtFlags_t flags; |
708 |
schoenebeck |
2581 |
ArrayList<StackFrame> stack; |
709 |
|
|
int stackFrame; |
710 |
|
|
int suspendMicroseconds; |
711 |
schoenebeck |
3221 |
size_t instructionsCount; |
712 |
schoenebeck |
2581 |
|
713 |
|
|
ExecContext() : |
714 |
schoenebeck |
3277 |
status(VM_EXEC_NOT_RUNNING), flags(STMT_SUCCESS), stackFrame(-1), |
715 |
|
|
suspendMicroseconds(0), instructionsCount(0) {} |
716 |
schoenebeck |
2581 |
|
717 |
|
|
virtual ~ExecContext() {} |
718 |
|
|
|
719 |
|
|
inline void pushStack(Statement* stmt) { |
720 |
|
|
stackFrame++; |
721 |
|
|
//printf("pushStack() -> %d\n", stackFrame); |
722 |
|
|
if (stackFrame >= stack.size()) return; |
723 |
|
|
stack[stackFrame].statement = stmt; |
724 |
|
|
stack[stackFrame].subindex = 0; |
725 |
|
|
} |
726 |
|
|
|
727 |
|
|
inline void popStack() { |
728 |
|
|
stack[stackFrame].statement = NULL; |
729 |
|
|
stack[stackFrame].subindex = -1; |
730 |
|
|
stackFrame--; |
731 |
|
|
//printf("popStack() -> %d\n", stackFrame); |
732 |
|
|
} |
733 |
|
|
|
734 |
|
|
inline void reset() { |
735 |
|
|
stack[0].statement = NULL; |
736 |
|
|
stack[0].subindex = -1; |
737 |
|
|
stackFrame = -1; |
738 |
schoenebeck |
3277 |
flags = STMT_SUCCESS; |
739 |
schoenebeck |
2581 |
} |
740 |
|
|
|
741 |
schoenebeck |
2588 |
int suspensionTimeMicroseconds() const OVERRIDE { |
742 |
schoenebeck |
2581 |
return suspendMicroseconds; |
743 |
|
|
} |
744 |
schoenebeck |
3207 |
|
745 |
|
|
void resetPolyphonicData() OVERRIDE { |
746 |
|
|
if (polyphonicIntMemory.empty()) return; |
747 |
|
|
memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int)); |
748 |
|
|
} |
749 |
schoenebeck |
3221 |
|
750 |
|
|
size_t instructionsPerformed() const OVERRIDE { |
751 |
|
|
return instructionsCount; |
752 |
|
|
} |
753 |
schoenebeck |
3277 |
|
754 |
|
|
void signalAbort() OVERRIDE { |
755 |
|
|
flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED); |
756 |
|
|
} |
757 |
schoenebeck |
3293 |
|
758 |
|
|
void forkTo(VMExecContext* ectx) const OVERRIDE; |
759 |
schoenebeck |
2581 |
}; |
760 |
|
|
|
761 |
|
|
} // namespace LinuxSampler |
762 |
|
|
|
763 |
|
|
#endif // LS_INSTRPARSERTREE_H |