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