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

Legend:
Removed from v.3257  
changed lines
  Added in v.3585

  ViewVC Help
Powered by ViewVC