/[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 3585 - (show annotations) (download) (as text)
Fri Aug 30 17:51:24 2019 UTC (4 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 38544 byte(s)
NKSP: Cleanup regarding multiple data types for built-in function args.

* NKSP VM API cleanup: Get rid of legacy method
  VMFunction::argType(vmint iArg) which was already superseded by its new
  replacement VMFunction::acceptsArgType(vmint iArg, ExprType_t type).

* NKSP parser: if wrong argument type was passed to a built-in function and
  that built-in function accepts more than one data type for the argument,
  then show all supported data types as parser error message.

* Bumped version (2.1.1.svn10).

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

  ViewVC Help
Powered by ViewVC