/[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 3583 - (show annotations) (download) (as text)
Fri Aug 30 12:39:18 2019 UTC (4 years, 6 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 38299 byte(s)
Moved function exprTypeOfVarName() from public API file common.h to
implementation internal file tree.h (since this function is only
intended to be called by the generated NKSP parser).

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

  ViewVC Help
Powered by ViewVC