1 |
/* -*- c++ -*- |
2 |
* |
3 |
* Copyright (c) 2014 - 2019 Christian Schoenebeck and Andreas Persson |
4 |
* |
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 |
#include <string.h> // for memset() |
24 |
#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 |
STMT_SYNC, |
40 |
STMT_NOOP, |
41 |
}; |
42 |
|
43 |
class Node { |
44 |
public: |
45 |
Node(); |
46 |
virtual ~Node(); |
47 |
virtual void dump(int level = 0) = 0; |
48 |
virtual bool isPolyphonic() const = 0; |
49 |
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 |
class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression { |
70 |
public: |
71 |
ExprType_t exprType() const { return INT_ARR_EXPR; } |
72 |
String evalCastToStr(); |
73 |
}; |
74 |
typedef Ref<IntArrayExpr,Node> IntArrayExprRef; |
75 |
|
76 |
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 |
bool isPolyphonic() const { return false; } |
92 |
}; |
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 |
bool isPolyphonic() const { return false; } |
103 |
}; |
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 |
int argsCount() const { return (int) args.size(); } |
112 |
VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; } |
113 |
bool isPolyphonic() const; |
114 |
}; |
115 |
typedef Ref<Args,Node> ArgsRef; |
116 |
|
117 |
class Variable : virtual public VMVariable, virtual public Expression { |
118 |
public: |
119 |
bool isConstExpr() const OVERRIDE { return bConst; } |
120 |
bool isAssignable() const OVERRIDE { return !bConst; } |
121 |
virtual void assign(Expression* expr) = 0; |
122 |
void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); } |
123 |
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 |
class BuiltInIntVariable : public IntVariable { |
159 |
String name; |
160 |
VMIntRelPtr* ptr; |
161 |
public: |
162 |
BuiltInIntVariable(const String& name, VMIntRelPtr* ptr); |
163 |
bool isAssignable() const OVERRIDE { return !ptr->readonly; } |
164 |
void assign(Expression* expr) OVERRIDE; |
165 |
int evalInt() OVERRIDE; |
166 |
void dump(int level = 0) OVERRIDE; |
167 |
}; |
168 |
typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef; |
169 |
|
170 |
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 |
class IntArrayVariable : public Variable, virtual public IntArrayExpr { |
178 |
ArrayList<int> values; |
179 |
public: |
180 |
IntArrayVariable(ParserContext* ctx, int size); |
181 |
IntArrayVariable(ParserContext* ctx, int size, ArgsRef values, bool _bConst = false); |
182 |
void assign(Expression* expr) {} // ignore scalar assignment |
183 |
String evalCastToStr() { return ""; } // ignore scalar cast to string |
184 |
ExprType_t exprType() const { return INT_ARR_EXPR; } |
185 |
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 |
bool isPolyphonic() const { return false; } |
190 |
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 OVERRIDE { return array->size; } |
201 |
int evalIntElement(uint i) OVERRIDE; |
202 |
bool isAssignable() const OVERRIDE { return !array->readonly; } |
203 |
void assignIntElement(uint i, int value) OVERRIDE; |
204 |
void dump(int level = 0) OVERRIDE; |
205 |
}; |
206 |
typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef; |
207 |
|
208 |
class IntArrayElement : public IntVariable { |
209 |
IntArrayExprRef array; |
210 |
IntExprRef index; |
211 |
public: |
212 |
IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex); |
213 |
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 |
bool isPolyphonic() const { return false; } |
226 |
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 |
bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); } |
250 |
}; |
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 |
StmtType_t statementType() const { return STMT_NOOP; } |
305 |
void dump(int level = 0) {} |
306 |
bool isPolyphonic() const { return false; } |
307 |
}; |
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 |
bool isPolyphonic() const; |
327 |
}; |
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 |
class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr, virtual public IntArrayExpr { |
338 |
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 |
void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); } |
347 |
VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); } |
348 |
int evalInt() OVERRIDE; |
349 |
String evalStr() OVERRIDE; |
350 |
String evalCastToStr() OVERRIDE; |
351 |
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) OVERRIDE { return dynVar->asIntArray()->assignIntElement(i, value); } |
354 |
void dump(int level = 0) OVERRIDE; |
355 |
}; |
356 |
typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef; |
357 |
|
358 |
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 |
void dump(int level = 0) OVERRIDE; |
366 |
StmtFlags_t exec() OVERRIDE; |
367 |
int evalInt() OVERRIDE; |
368 |
VMIntArrayExpr* asIntArray() const OVERRIDE; |
369 |
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 |
protected: |
375 |
VMFnResult* execVMFn(); |
376 |
}; |
377 |
typedef Ref<FunctionCall,Node> FunctionCallRef; |
378 |
|
379 |
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 |
class EventHandler : virtual public Statements, virtual public VMEventHandler { |
387 |
StatementsRef statements; |
388 |
bool usingPolyphonics; |
389 |
public: |
390 |
void dump(int level = 0); |
391 |
StmtFlags_t exec(); |
392 |
EventHandler(StatementsRef statements); |
393 |
Statement* statement(uint i) { return statements->statement(i); } |
394 |
bool isPolyphonic() const { return usingPolyphonics; } |
395 |
}; |
396 |
typedef Ref<EventHandler,Node> EventHandlerRef; |
397 |
|
398 |
class OnNote : public EventHandler { |
399 |
public: |
400 |
OnNote(StatementsRef statements) : EventHandler(statements) {} |
401 |
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_NOTE; } |
402 |
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 |
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_INIT; } |
410 |
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 |
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_RELEASE; } |
418 |
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 |
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_CONTROLLER; } |
426 |
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 |
inline uint size() const { return (int) args.size(); } |
441 |
bool isPolyphonic() const; |
442 |
}; |
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 |
bool isPolyphonic() const { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); } |
454 |
}; |
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 |
bool isPolyphonic() const; |
470 |
}; |
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 |
bool isPolyphonic() const; |
494 |
}; |
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 |
bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); } |
508 |
}; |
509 |
|
510 |
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 |
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 |
bool isPolyphonic() const { return expr->isPolyphonic(); } |
529 |
}; |
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 |
bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); } |
541 |
}; |
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 |
bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); } |
560 |
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 |
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 |
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 |
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 |
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 |
bool isPolyphonic() const { return expr->isPolyphonic(); } |
607 |
}; |
608 |
typedef Ref<Not,Node> NotRef; |
609 |
|
610 |
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 |
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 |
std::vector<ParserIssue> vErrors; |
632 |
std::vector<ParserIssue> vWarnings; |
633 |
std::vector<ParserIssue> vIssues; |
634 |
std::vector<CodeBlock> vPreprocessorComments; |
635 |
|
636 |
std::set<String> builtinPreprocessorConditions; |
637 |
std::set<String> userPreprocessorConditions; |
638 |
|
639 |
std::map<String,VariableRef> vartable; |
640 |
std::map<String,StatementsRef> userFnTable; |
641 |
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 |
int requiredMaxStackSize; |
655 |
|
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 |
globalIntMemory(NULL), globalStrMemory(NULL), requiredMaxStackSize(-1), |
664 |
functionProvider(parent), execContext(NULL) |
665 |
{ |
666 |
} |
667 |
virtual ~ParserContext(); |
668 |
VariableRef globalVar(const String& name); |
669 |
IntVariableRef globalIntVar(const String& name); |
670 |
StringVariableRef globalStrVar(const String& name); |
671 |
VariableRef variableByName(const String& name); |
672 |
StatementsRef userFunctionByName(const String& name); |
673 |
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 |
void addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn); |
676 |
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 |
std::vector<ParserIssue> issues() const OVERRIDE; |
682 |
std::vector<ParserIssue> errors() const OVERRIDE; |
683 |
std::vector<ParserIssue> warnings() const OVERRIDE; |
684 |
std::vector<CodeBlock> preprocessorComments() const OVERRIDE; |
685 |
VMEventHandler* eventHandler(uint index) OVERRIDE; |
686 |
VMEventHandler* eventHandlerByName(const String& name) OVERRIDE; |
687 |
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 |
void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars); |
691 |
}; |
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 |
StmtFlags_t flags; |
708 |
ArrayList<StackFrame> stack; |
709 |
int stackFrame; |
710 |
int suspendMicroseconds; |
711 |
size_t instructionsCount; |
712 |
|
713 |
ExecContext() : |
714 |
status(VM_EXEC_NOT_RUNNING), flags(STMT_SUCCESS), stackFrame(-1), |
715 |
suspendMicroseconds(0), instructionsCount(0) {} |
716 |
|
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 |
flags = STMT_SUCCESS; |
739 |
} |
740 |
|
741 |
int suspensionTimeMicroseconds() const OVERRIDE { |
742 |
return suspendMicroseconds; |
743 |
} |
744 |
|
745 |
void resetPolyphonicData() OVERRIDE { |
746 |
if (polyphonicIntMemory.empty()) return; |
747 |
memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int)); |
748 |
} |
749 |
|
750 |
size_t instructionsPerformed() const OVERRIDE { |
751 |
return instructionsCount; |
752 |
} |
753 |
|
754 |
void signalAbort() OVERRIDE { |
755 |
flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED); |
756 |
} |
757 |
|
758 |
void forkTo(VMExecContext* ectx) const OVERRIDE; |
759 |
}; |
760 |
|
761 |
} // namespace LinuxSampler |
762 |
|
763 |
#endif // LS_INSTRPARSERTREE_H |