/[svn]/linuxsampler/trunk/src/scriptvm/tree.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/tree.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3574 - (show annotations) (download) (as text)
Wed Aug 28 08:26:54 2019 UTC (4 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 34958 byte(s)
NKSP: Cleanup of class inheritance scheme.

* scriptvm/tree.h: Mark all relevant classes with C++11 'final' keyword.

* scriptvm/tree.h: StringLiteral class has no need for virtually.

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

  ViewVC Help
Powered by ViewVC