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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3690 - (hide annotations) (download) (as text)
Fri Jan 3 10:18:21 2020 UTC (4 years, 5 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 39613 byte(s)
NKSP: Added support for RPN and NRPN event handlers:

* NKSP language: Added support for RPN event handler
  ("on rpn ... end on" in instrument scripts).

* NKSP language: Added support for NRPN event handler
  ("on nrpn ... end on" in instrument scripts).

* Added built-in read-only variables "$RPN_ADDRESS" and "$RPN_VALUE" which
  may be read from the new RPN/NRPN script handlers to get the (N)RPN
  parameter that had been changed and its new value.

* Added built-in const variables "$NI_CB_TYPE_RPN" and "$NI_CB_TYPE_NRPN"
  which are identifying the new (N)RPN handlers as such at script runtime.

* Bumped version (2.1.1.svn30).

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

  ViewVC Help
Powered by ViewVC