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

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

  ViewVC Help
Powered by ViewVC