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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.2879  
changed lines
  Added in v.3576

  ViewVC Help
Powered by ViewVC