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 OVERRIDE { return INT_EXPR; } |
64 |
String evalCastToStr() OVERRIDE; |
65 |
}; |
66 |
typedef Ref<IntExpr,Node> IntExprRef; |
67 |
|
68 |
class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression { |
69 |
public: |
70 |
ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; } |
71 |
String evalCastToStr() OVERRIDE; |
72 |
}; |
73 |
typedef Ref<IntArrayExpr,Node> IntArrayExprRef; |
74 |
|
75 |
class StringExpr : virtual public VMStringExpr, virtual public Expression { |
76 |
public: |
77 |
ExprType_t exprType() const OVERRIDE { return STRING_EXPR; } |
78 |
String evalCastToStr() OVERRIDE { return evalStr(); } |
79 |
}; |
80 |
typedef Ref<StringExpr,Node> StringExprRef; |
81 |
|
82 |
class IntLiteral : virtual public IntExpr { |
83 |
public: |
84 |
vmint value; |
85 |
IntLiteral(vmint value) : value(value) { } |
86 |
vmint evalInt() OVERRIDE; |
87 |
void dump(int level = 0) OVERRIDE; |
88 |
bool isConstExpr() const OVERRIDE { return true; } |
89 |
bool isPolyphonic() const OVERRIDE { return false; } |
90 |
}; |
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 OVERRIDE { return true; } |
98 |
void dump(int level = 0) OVERRIDE; |
99 |
String evalStr() OVERRIDE { return value; } |
100 |
bool isPolyphonic() const OVERRIDE { return false; } |
101 |
}; |
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) OVERRIDE; |
109 |
vmint argsCount() const OVERRIDE { return (vmint) args.size(); } |
110 |
VMExpr* arg(vmint i) OVERRIDE { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; } |
111 |
bool isPolyphonic() const OVERRIDE; |
112 |
}; |
113 |
typedef Ref<Args,Node> ArgsRef; |
114 |
|
115 |
class Variable : virtual public VMVariable, virtual public Expression { |
116 |
public: |
117 |
bool isConstExpr() const OVERRIDE { return bConst; } |
118 |
bool isAssignable() const OVERRIDE { return !bConst; } |
119 |
virtual void assign(Expression* expr) = 0; |
120 |
void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); } |
121 |
protected: |
122 |
Variable(ParserContext* ctx, vmint _memPos, bool _bConst) |
123 |
: context(ctx), memPos(_memPos), bConst(_bConst) {} |
124 |
|
125 |
ParserContext* context; |
126 |
vmint 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) OVERRIDE; |
136 |
vmint evalInt() OVERRIDE; |
137 |
void dump(int level = 0) OVERRIDE; |
138 |
bool isPolyphonic() const OVERRIDE { return polyphonic; } |
139 |
protected: |
140 |
IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, vmint size = 1); |
141 |
}; |
142 |
typedef Ref<IntVariable,Node> IntVariableRef; |
143 |
|
144 |
class ConstIntVariable : public IntVariable { |
145 |
public: |
146 |
vmint value; |
147 |
|
148 |
ConstIntVariable(vmint value); |
149 |
//ConstIntVariable(ParserContext* ctx, int value = 0); |
150 |
void assign(Expression* expr) OVERRIDE; |
151 |
vmint evalInt() OVERRIDE; |
152 |
void dump(int level = 0) OVERRIDE; |
153 |
}; |
154 |
typedef Ref<ConstIntVariable,Node> ConstIntVariableRef; |
155 |
|
156 |
class BuiltInIntVariable : public IntVariable { |
157 |
String name; |
158 |
VMIntPtr* ptr; |
159 |
public: |
160 |
BuiltInIntVariable(const String& name, VMIntPtr* ptr); |
161 |
bool isAssignable() const OVERRIDE { return ptr->isAssignable(); } |
162 |
void assign(Expression* expr) OVERRIDE; |
163 |
vmint evalInt() OVERRIDE; |
164 |
void dump(int level = 0) OVERRIDE; |
165 |
}; |
166 |
typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef; |
167 |
|
168 |
class PolyphonicIntVariable : public IntVariable { |
169 |
public: |
170 |
PolyphonicIntVariable(ParserContext* ctx); |
171 |
void dump(int level = 0) OVERRIDE; |
172 |
}; |
173 |
typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef; |
174 |
|
175 |
class IntArrayVariable : public Variable, virtual public IntArrayExpr { |
176 |
ArrayList<vmint> values; |
177 |
public: |
178 |
IntArrayVariable(ParserContext* ctx, vmint size); |
179 |
IntArrayVariable(ParserContext* ctx, vmint size, ArgsRef values, bool _bConst = false); |
180 |
void assign(Expression* expr) OVERRIDE {} // ignore scalar assignment |
181 |
String evalCastToStr() OVERRIDE { return ""; } // ignore scalar cast to string |
182 |
ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; } |
183 |
virtual vmint arraySize() const OVERRIDE { return values.size(); } |
184 |
virtual vmint evalIntElement(vmuint i) OVERRIDE; |
185 |
virtual void assignIntElement(vmuint i, vmint value) OVERRIDE; |
186 |
void dump(int level = 0) OVERRIDE; |
187 |
bool isPolyphonic() const OVERRIDE { return false; } |
188 |
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 |
vmint arraySize() const OVERRIDE { return array->size; } |
199 |
vmint evalIntElement(vmuint i) OVERRIDE; |
200 |
bool isAssignable() const OVERRIDE { return !array->readonly; } |
201 |
void assignIntElement(vmuint i, vmint value) OVERRIDE; |
202 |
void dump(int level = 0) OVERRIDE; |
203 |
}; |
204 |
typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef; |
205 |
|
206 |
class IntArrayElement : public IntVariable { |
207 |
IntArrayExprRef array; |
208 |
IntExprRef index; |
209 |
public: |
210 |
IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex); |
211 |
void assign(Expression* expr) OVERRIDE; |
212 |
vmint evalInt() OVERRIDE; |
213 |
void dump(int level = 0) OVERRIDE; |
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) OVERRIDE; |
221 |
String evalStr() OVERRIDE; |
222 |
void dump(int level = 0) OVERRIDE; |
223 |
bool isPolyphonic() const OVERRIDE { return false; } |
224 |
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) OVERRIDE; |
235 |
String evalStr() OVERRIDE; |
236 |
void dump(int level = 0) OVERRIDE; |
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 OVERRIDE { return lhs->isConstExpr() && rhs->isConstExpr(); } |
247 |
bool isPolyphonic() const OVERRIDE { return lhs->isPolyphonic() || rhs->isPolyphonic(); } |
248 |
}; |
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 |
vmint evalInt() OVERRIDE; |
255 |
void dump(int level = 0) OVERRIDE; |
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 |
vmint evalInt() OVERRIDE; |
263 |
void dump(int level = 0) OVERRIDE; |
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 |
vmint evalInt() OVERRIDE; |
271 |
void dump(int level = 0) OVERRIDE; |
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 |
vmint evalInt() OVERRIDE; |
279 |
void dump(int level = 0) OVERRIDE; |
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 |
vmint evalInt() OVERRIDE; |
287 |
void dump(int level = 0) OVERRIDE; |
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 OVERRIDE { return STMT_NOOP; } |
303 |
void dump(int level = 0) OVERRIDE {} |
304 |
bool isPolyphonic() const OVERRIDE { return false; } |
305 |
}; |
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 |
StmtType_t statementType() const OVERRIDE { 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) OVERRIDE; |
322 |
StmtType_t statementType() const OVERRIDE { return STMT_LIST; } |
323 |
virtual Statement* statement(uint i); |
324 |
bool isPolyphonic() const OVERRIDE; |
325 |
}; |
326 |
typedef Ref<Statements,Node> StatementsRef; |
327 |
|
328 |
class BranchStatement : public Statement { |
329 |
public: |
330 |
StmtType_t statementType() const OVERRIDE { return STMT_BRANCH; } |
331 |
virtual vmint evalBranch() = 0; |
332 |
virtual Statements* branch(vmuint i) const = 0; |
333 |
}; |
334 |
|
335 |
class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr, virtual public IntArrayExpr { |
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 |
void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); } |
345 |
VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); } |
346 |
vmint evalInt() OVERRIDE; |
347 |
String evalStr() OVERRIDE; |
348 |
String evalCastToStr() OVERRIDE; |
349 |
vmint arraySize() const OVERRIDE { return dynVar->asIntArray()->arraySize(); } |
350 |
vmint evalIntElement(vmuint i) OVERRIDE { return dynVar->asIntArray()->evalIntElement(i); } |
351 |
void assignIntElement(vmuint i, vmint value) OVERRIDE { return dynVar->asIntArray()->assignIntElement(i, value); } |
352 |
void dump(int level = 0) OVERRIDE; |
353 |
}; |
354 |
typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef; |
355 |
|
356 |
class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr { |
357 |
String functionName; |
358 |
ArgsRef args; |
359 |
VMFunction* fn; |
360 |
public: |
361 |
FunctionCall(const char* function, ArgsRef args, VMFunction* fn) : |
362 |
functionName(function), args(args), fn(fn) { } |
363 |
void dump(int level = 0) OVERRIDE; |
364 |
StmtFlags_t exec() OVERRIDE; |
365 |
vmint evalInt() OVERRIDE; |
366 |
VMIntArrayExpr* asIntArray() const OVERRIDE; |
367 |
String evalStr() OVERRIDE; |
368 |
bool isConstExpr() const OVERRIDE { return false; } |
369 |
ExprType_t exprType() const OVERRIDE; |
370 |
String evalCastToStr() OVERRIDE; |
371 |
bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); } |
372 |
protected: |
373 |
VMFnResult* execVMFn(); |
374 |
}; |
375 |
typedef Ref<FunctionCall,Node> FunctionCallRef; |
376 |
|
377 |
class NoFunctionCall : public FunctionCall { |
378 |
public: |
379 |
NoFunctionCall() : FunctionCall("nothing", new Args, NULL) {} |
380 |
StmtType_t statementType() const OVERRIDE { return STMT_NOOP; } |
381 |
}; |
382 |
typedef Ref<NoFunctionCall,Node> NoFunctionCallRef; |
383 |
|
384 |
class EventHandler : virtual public Statements, virtual public VMEventHandler { |
385 |
StatementsRef statements; |
386 |
bool usingPolyphonics; |
387 |
public: |
388 |
void dump(int level = 0) OVERRIDE; |
389 |
StmtFlags_t exec(); |
390 |
EventHandler(StatementsRef statements); |
391 |
Statement* statement(uint i) OVERRIDE { return statements->statement(i); } |
392 |
bool isPolyphonic() const OVERRIDE { return usingPolyphonics; } |
393 |
}; |
394 |
typedef Ref<EventHandler,Node> EventHandlerRef; |
395 |
|
396 |
class OnNote : public EventHandler { |
397 |
public: |
398 |
OnNote(StatementsRef statements) : EventHandler(statements) {} |
399 |
VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_NOTE; } |
400 |
String eventHandlerName() const OVERRIDE { return "note"; } |
401 |
}; |
402 |
typedef Ref<OnNote,Node> OnNoteRef; |
403 |
|
404 |
class OnInit : public EventHandler { |
405 |
public: |
406 |
OnInit(StatementsRef statements) : EventHandler(statements) {} |
407 |
VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_INIT; } |
408 |
String eventHandlerName() const OVERRIDE { return "init"; } |
409 |
}; |
410 |
typedef Ref<OnInit,Node> OnInitRef; |
411 |
|
412 |
class OnRelease : public EventHandler { |
413 |
public: |
414 |
OnRelease(StatementsRef statements) : EventHandler(statements) {} |
415 |
VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_RELEASE; } |
416 |
String eventHandlerName() const OVERRIDE { return "release"; } |
417 |
}; |
418 |
typedef Ref<OnRelease,Node> OnReleaseRef; |
419 |
|
420 |
class OnController : public EventHandler { |
421 |
public: |
422 |
OnController(StatementsRef statements) : EventHandler(statements) {} |
423 |
VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_CONTROLLER; } |
424 |
String eventHandlerName() const OVERRIDE { return "controller"; } |
425 |
}; |
426 |
typedef Ref<OnController,Node> OnControllerRef; |
427 |
|
428 |
class EventHandlers : virtual public Node { |
429 |
std::vector<EventHandlerRef> args; |
430 |
public: |
431 |
EventHandlers(); |
432 |
~EventHandlers(); |
433 |
void add(EventHandlerRef arg); |
434 |
void dump(int level = 0) OVERRIDE; |
435 |
EventHandler* eventHandlerByName(const String& name) const; |
436 |
EventHandler* eventHandler(uint index) const; |
437 |
inline uint size() const { return (int) args.size(); } |
438 |
bool isPolyphonic() const OVERRIDE; |
439 |
}; |
440 |
typedef Ref<EventHandlers,Node> EventHandlersRef; |
441 |
|
442 |
class Assignment : public LeafStatement { |
443 |
protected: |
444 |
VariableRef variable; |
445 |
ExpressionRef value; |
446 |
public: |
447 |
Assignment(VariableRef variable, ExpressionRef value); |
448 |
void dump(int level = 0) OVERRIDE; |
449 |
StmtFlags_t exec() OVERRIDE; |
450 |
bool isPolyphonic() const OVERRIDE { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); } |
451 |
}; |
452 |
typedef Ref<Assignment,Node> AssignmentRef; |
453 |
|
454 |
class If : public BranchStatement { |
455 |
IntExprRef condition; |
456 |
StatementsRef ifStatements; |
457 |
StatementsRef elseStatements; |
458 |
public: |
459 |
If(IntExprRef condition, StatementsRef ifStatements, StatementsRef elseStatements) : |
460 |
condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { } |
461 |
If(IntExprRef condition, StatementsRef statements) : |
462 |
condition(condition), ifStatements(statements) { } |
463 |
void dump(int level = 0) OVERRIDE; |
464 |
vmint evalBranch() OVERRIDE; |
465 |
Statements* branch(vmuint i) const OVERRIDE; |
466 |
bool isPolyphonic() const OVERRIDE; |
467 |
}; |
468 |
typedef Ref<If,Node> IfRef; |
469 |
|
470 |
struct CaseBranch { |
471 |
IntExprRef from; |
472 |
IntExprRef to; |
473 |
StatementsRef statements; |
474 |
}; |
475 |
|
476 |
typedef std::vector<CaseBranch> CaseBranches; |
477 |
|
478 |
class SelectCase : public BranchStatement { |
479 |
IntExprRef select; |
480 |
CaseBranches branches; |
481 |
public: |
482 |
SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { } |
483 |
void dump(int level = 0) OVERRIDE; |
484 |
vmint evalBranch() OVERRIDE; |
485 |
Statements* branch(vmuint i) const OVERRIDE; |
486 |
//void addBranch(IntExprRef condition, StatementsRef statements); |
487 |
//void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements); |
488 |
//void addBranch(CaseBranchRef branch); |
489 |
//void addBranches(CaseBranchesRef branches); |
490 |
bool isPolyphonic() const OVERRIDE; |
491 |
}; |
492 |
typedef Ref<SelectCase,Node> SelectCaseRef; |
493 |
|
494 |
class While : public Statement { |
495 |
IntExprRef m_condition; |
496 |
StatementsRef m_statements; |
497 |
public: |
498 |
While(IntExprRef condition, StatementsRef statements) : |
499 |
m_condition(condition), m_statements(statements) {} |
500 |
StmtType_t statementType() const OVERRIDE { return STMT_LOOP; } |
501 |
void dump(int level = 0) OVERRIDE; |
502 |
bool evalLoopStartCondition(); |
503 |
Statements* statements() const; |
504 |
bool isPolyphonic() const OVERRIDE { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); } |
505 |
}; |
506 |
|
507 |
class SyncBlock : public Statement { |
508 |
StatementsRef m_statements; |
509 |
public: |
510 |
SyncBlock(StatementsRef statements) : m_statements(statements) {} |
511 |
StmtType_t statementType() const OVERRIDE { return STMT_SYNC; } |
512 |
void dump(int level = 0) OVERRIDE; |
513 |
Statements* statements() const; |
514 |
bool isPolyphonic() const OVERRIDE { return m_statements->isPolyphonic(); } |
515 |
}; |
516 |
typedef Ref<SyncBlock,Node> SyncBlockRef; |
517 |
|
518 |
class Neg : public IntExpr { |
519 |
IntExprRef expr; |
520 |
public: |
521 |
Neg(IntExprRef expr) : expr(expr) { } |
522 |
vmint evalInt() OVERRIDE { return (expr) ? -expr->evalInt() : 0; } |
523 |
void dump(int level = 0) OVERRIDE; |
524 |
bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); } |
525 |
bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); } |
526 |
}; |
527 |
typedef Ref<Neg,Node> NegRef; |
528 |
|
529 |
class ConcatString : public StringExpr { |
530 |
ExpressionRef lhs; |
531 |
ExpressionRef rhs; |
532 |
public: |
533 |
ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {} |
534 |
String evalStr() OVERRIDE; |
535 |
void dump(int level = 0) OVERRIDE; |
536 |
bool isConstExpr() const OVERRIDE; |
537 |
bool isPolyphonic() const OVERRIDE { return lhs->isPolyphonic() || rhs->isPolyphonic(); } |
538 |
}; |
539 |
typedef Ref<ConcatString,Node> ConcatStringRef; |
540 |
|
541 |
class Relation : public IntExpr { |
542 |
public: |
543 |
enum Type { |
544 |
LESS_THAN, |
545 |
GREATER_THAN, |
546 |
LESS_OR_EQUAL, |
547 |
GREATER_OR_EQUAL, |
548 |
EQUAL, |
549 |
NOT_EQUAL |
550 |
}; |
551 |
Relation(IntExprRef lhs, Type type, IntExprRef rhs) : |
552 |
lhs(lhs), rhs(rhs), type(type) {} |
553 |
vmint evalInt() OVERRIDE; |
554 |
void dump(int level = 0) OVERRIDE; |
555 |
bool isConstExpr() const OVERRIDE; |
556 |
bool isPolyphonic() const OVERRIDE { return lhs->isPolyphonic() || rhs->isPolyphonic(); } |
557 |
private: |
558 |
IntExprRef lhs; |
559 |
IntExprRef rhs; |
560 |
Type type; |
561 |
}; |
562 |
typedef Ref<Relation,Node> RelationRef; |
563 |
|
564 |
class Or : virtual public BinaryOp, virtual public IntExpr { |
565 |
public: |
566 |
Or(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
567 |
vmint evalInt() OVERRIDE; |
568 |
void dump(int level = 0) OVERRIDE; |
569 |
}; |
570 |
typedef Ref<Or,Node> OrRef; |
571 |
|
572 |
class BitwiseOr : virtual public BinaryOp, virtual public IntExpr { |
573 |
public: |
574 |
BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
575 |
vmint evalInt() OVERRIDE; |
576 |
void dump(int level = 0) OVERRIDE; |
577 |
}; |
578 |
typedef Ref<BitwiseOr,Node> BitwiseOrRef; |
579 |
|
580 |
class And : virtual public BinaryOp, virtual public IntExpr { |
581 |
public: |
582 |
And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
583 |
vmint evalInt() OVERRIDE; |
584 |
void dump(int level = 0) OVERRIDE; |
585 |
}; |
586 |
typedef Ref<And,Node> AndRef; |
587 |
|
588 |
class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr { |
589 |
public: |
590 |
BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
591 |
vmint evalInt() OVERRIDE; |
592 |
void dump(int level = 0) OVERRIDE; |
593 |
}; |
594 |
typedef Ref<BitwiseAnd,Node> BitwiseAndRef; |
595 |
|
596 |
class Not : virtual public IntExpr { |
597 |
IntExprRef expr; |
598 |
public: |
599 |
Not(IntExprRef expr) : expr(expr) {} |
600 |
vmint evalInt() OVERRIDE { return !expr->evalInt(); } |
601 |
void dump(int level = 0) OVERRIDE; |
602 |
bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); } |
603 |
bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); } |
604 |
}; |
605 |
typedef Ref<Not,Node> NotRef; |
606 |
|
607 |
class BitwiseNot : virtual public IntExpr { |
608 |
IntExprRef expr; |
609 |
public: |
610 |
BitwiseNot(IntExprRef expr) : expr(expr) {} |
611 |
vmint evalInt() OVERRIDE { return ~expr->evalInt(); } |
612 |
void dump(int level = 0) OVERRIDE; |
613 |
bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); } |
614 |
bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); } |
615 |
}; |
616 |
typedef Ref<BitwiseNot,Node> BitwiseNotRef; |
617 |
|
618 |
class ParserContext : public VMParserContext { |
619 |
public: |
620 |
struct Error { |
621 |
String txt; |
622 |
int line; |
623 |
}; |
624 |
typedef Error Warning; |
625 |
|
626 |
void* scanner; |
627 |
std::istream* is; |
628 |
std::vector<ParserIssue> vErrors; |
629 |
std::vector<ParserIssue> vWarnings; |
630 |
std::vector<ParserIssue> vIssues; |
631 |
std::vector<CodeBlock> vPreprocessorComments; |
632 |
|
633 |
std::set<String> builtinPreprocessorConditions; |
634 |
std::set<String> userPreprocessorConditions; |
635 |
|
636 |
std::map<String,VariableRef> vartable; |
637 |
std::map<String,StatementsRef> userFnTable; |
638 |
vmint globalIntVarCount; |
639 |
vmint globalStrVarCount; |
640 |
vmint polyphonicIntVarCount; |
641 |
|
642 |
EventHandlersRef handlers; |
643 |
|
644 |
OnInitRef onInit; |
645 |
OnNoteRef onNote; |
646 |
OnReleaseRef onRelease; |
647 |
OnControllerRef onController; |
648 |
|
649 |
ArrayList<vmint>* globalIntMemory; |
650 |
ArrayList<String>* globalStrMemory; |
651 |
vmint requiredMaxStackSize; |
652 |
|
653 |
VMFunctionProvider* functionProvider; |
654 |
|
655 |
ExecContext* execContext; |
656 |
|
657 |
ParserContext(VMFunctionProvider* parent) : |
658 |
scanner(NULL), is(NULL), |
659 |
globalIntVarCount(0), globalStrVarCount(0), polyphonicIntVarCount(0), |
660 |
globalIntMemory(NULL), globalStrMemory(NULL), requiredMaxStackSize(-1), |
661 |
functionProvider(parent), execContext(NULL) |
662 |
{ |
663 |
} |
664 |
virtual ~ParserContext(); |
665 |
VariableRef globalVar(const String& name); |
666 |
IntVariableRef globalIntVar(const String& name); |
667 |
StringVariableRef globalStrVar(const String& name); |
668 |
VariableRef variableByName(const String& name); |
669 |
StatementsRef userFunctionByName(const String& name); |
670 |
void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
671 |
void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
672 |
void addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn); |
673 |
void createScanner(std::istream* is); |
674 |
void destroyScanner(); |
675 |
bool setPreprocessorCondition(const char* name); |
676 |
bool resetPreprocessorCondition(const char* name); |
677 |
bool isPreprocessorConditionSet(const char* name); |
678 |
std::vector<ParserIssue> issues() const OVERRIDE; |
679 |
std::vector<ParserIssue> errors() const OVERRIDE; |
680 |
std::vector<ParserIssue> warnings() const OVERRIDE; |
681 |
std::vector<CodeBlock> preprocessorComments() const OVERRIDE; |
682 |
VMEventHandler* eventHandler(uint index) OVERRIDE; |
683 |
VMEventHandler* eventHandlerByName(const String& name) OVERRIDE; |
684 |
void registerBuiltInConstIntVariables(const std::map<String,vmint>& vars); |
685 |
void registerBuiltInIntVariables(const std::map<String,VMIntPtr*>& vars); |
686 |
void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars); |
687 |
void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars); |
688 |
}; |
689 |
|
690 |
class ExecContext : public VMExecContext { |
691 |
public: |
692 |
struct StackFrame { |
693 |
Statement* statement; |
694 |
int subindex; |
695 |
|
696 |
StackFrame() { |
697 |
statement = NULL; |
698 |
subindex = -1; |
699 |
} |
700 |
}; |
701 |
|
702 |
ArrayList<vmint> polyphonicIntMemory; |
703 |
VMExecStatus_t status; |
704 |
StmtFlags_t flags; |
705 |
ArrayList<StackFrame> stack; |
706 |
int stackFrame; |
707 |
vmint suspendMicroseconds; |
708 |
size_t instructionsCount; |
709 |
struct ExitRes { |
710 |
Expression* value; |
711 |
IntLiteral intLiteral; |
712 |
StringLiteral stringLiteral; |
713 |
|
714 |
ExitRes() : intLiteral(0), stringLiteral("") { } |
715 |
} exitRes; |
716 |
|
717 |
ExecContext(); |
718 |
virtual ~ExecContext() {} |
719 |
|
720 |
inline void pushStack(Statement* stmt) { |
721 |
stackFrame++; |
722 |
//printf("pushStack() -> %d\n", stackFrame); |
723 |
if (stackFrame >= stack.size()) return; |
724 |
stack[stackFrame].statement = stmt; |
725 |
stack[stackFrame].subindex = 0; |
726 |
} |
727 |
|
728 |
inline void popStack() { |
729 |
stack[stackFrame].statement = NULL; |
730 |
stack[stackFrame].subindex = -1; |
731 |
stackFrame--; |
732 |
//printf("popStack() -> %d\n", stackFrame); |
733 |
} |
734 |
|
735 |
inline void reset() { |
736 |
stack[0].statement = NULL; |
737 |
stack[0].subindex = -1; |
738 |
stackFrame = -1; |
739 |
flags = STMT_SUCCESS; |
740 |
} |
741 |
|
742 |
inline void clearExitRes() { |
743 |
exitRes.value = NULL; |
744 |
} |
745 |
|
746 |
vmint suspensionTimeMicroseconds() const OVERRIDE { |
747 |
return suspendMicroseconds; |
748 |
} |
749 |
|
750 |
void resetPolyphonicData() OVERRIDE { |
751 |
if (polyphonicIntMemory.empty()) return; |
752 |
memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(vmint)); |
753 |
} |
754 |
|
755 |
size_t instructionsPerformed() const OVERRIDE { |
756 |
return instructionsCount; |
757 |
} |
758 |
|
759 |
void signalAbort() OVERRIDE { |
760 |
flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED); |
761 |
} |
762 |
|
763 |
void forkTo(VMExecContext* ectx) const OVERRIDE; |
764 |
|
765 |
VMExpr* exitResult() OVERRIDE { |
766 |
return exitRes.value; |
767 |
} |
768 |
}; |
769 |
|
770 |
} // namespace LinuxSampler |
771 |
|
772 |
#endif // LS_INSTRPARSERTREE_H |