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

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

  ViewVC Help
Powered by ViewVC