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

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

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

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

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

  ViewVC Help
Powered by ViewVC