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

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

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

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

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

  ViewVC Help
Powered by ViewVC