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