/[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 3582 - (show annotations) (download) (as text)
Fri Aug 30 12:23:40 2019 UTC (4 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 37627 byte(s)
NKSP VM refactoring: Renamed all methods, functions and
classes matching pattern '*ScalarNumber*' to simply '*Number*':

- Renamed classes VMScalarNumberExpr -> VMNumberExpr,
  ScalarNumberExpr -> NumberExpr, ScalarNumberVariable ->
  NumberVariable, ScalarNumberBinaryOp -> NumberBinaryOp,
  VMScalarNumberResultFunction -> VMNumberResultFunction.

- Renamed method VMExpr::asScalarNumberExpr() -> VMExpr::asNumber().

- Renamed function isScalarNumber() -> isNumber().

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

  ViewVC Help
Powered by ViewVC