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