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

Legend:
Removed from v.2581  
changed lines
  Added in v.3728

  ViewVC Help
Powered by ViewVC