/[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 2645 by schoenebeck, Wed Jun 18 00:14:57 2014 UTC revision 3557 by schoenebeck, Sun Aug 18 00:06:04 2019 UTC
# Line 1  Line 1 
1  /*                                                              -*- c++ -*-  /*                                                              -*- c++ -*-
2   *   *
3   * Copyright (c) 2014 Christian Schoenebeck and Andreas Persson   * Copyright (c) 2014 - 2019 Christian Schoenebeck and Andreas Persson
4   *   *
5   * http://www.linuxsampler.org   * http://www.linuxsampler.org
6   *   *
# Line 20  Line 20 
20  #include <iostream>  #include <iostream>
21  #include <map>  #include <map>
22  #include <set>  #include <set>
23    #include <string.h> // for memset()
24  #include "../common/global.h"  #include "../common/global.h"
25  #include "../common/Ref.h"  #include "../common/Ref.h"
26  #include "../common/ArrayList.h"  #include "../common/ArrayList.h"
# Line 35  enum StmtType_t { Line 36  enum StmtType_t {
36      STMT_LIST,      STMT_LIST,
37      STMT_BRANCH,      STMT_BRANCH,
38      STMT_LOOP,      STMT_LOOP,
39        STMT_SYNC,
40        STMT_NOOP,
41  };  };
42    
43  class Node {  class Node {
# Line 57  typedef Ref<Expression,Node> ExpressionR Line 60  typedef Ref<Expression,Node> ExpressionR
60    
61  class IntExpr : virtual public VMIntExpr, virtual public Expression {  class IntExpr : virtual public VMIntExpr, virtual public Expression {
62  public:  public:
63      ExprType_t exprType() const { return INT_EXPR; }      ExprType_t exprType() const OVERRIDE { return INT_EXPR; }
64      virtual int evalInt() = 0;      String evalCastToStr() OVERRIDE;
     String evalCastToStr();  
65  };  };
66  typedef Ref<IntExpr,Node> IntExprRef;  typedef Ref<IntExpr,Node> IntExprRef;
67    
68    class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression {
69    public:
70        ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; }
71        String evalCastToStr() OVERRIDE;
72    };
73    typedef Ref<IntArrayExpr,Node> IntArrayExprRef;
74    
75  class StringExpr : virtual public VMStringExpr, virtual public Expression {  class StringExpr : virtual public VMStringExpr, virtual public Expression {
76  public:  public:
77      ExprType_t exprType() const { return STRING_EXPR; }      ExprType_t exprType() const OVERRIDE { return STRING_EXPR; }
78      virtual String evalStr() = 0;      String evalCastToStr() OVERRIDE { return evalStr(); }
     String evalCastToStr() { return evalStr(); }  
79  };  };
80  typedef Ref<StringExpr,Node> StringExprRef;  typedef Ref<StringExpr,Node> StringExprRef;
81    
82  class IntLiteral : virtual public IntExpr {  class IntLiteral : virtual public IntExpr {
     int value;  
83  public:  public:
84      IntLiteral(int value) : value(value) { }      vmint value;
85      int evalInt();      IntLiteral(vmint value) : value(value) { }
86      void dump(int level = 0);      vmint evalInt() OVERRIDE;
87      bool isConstExpr() const { return true; }      void dump(int level = 0) OVERRIDE;
88      bool isPolyphonic() const { return false; }      bool isConstExpr() const OVERRIDE { return true; }
89        bool isPolyphonic() const OVERRIDE { return false; }
90  };  };
91  typedef Ref<IntLiteral,Node> IntLiteralRef;  typedef Ref<IntLiteral,Node> IntLiteralRef;
92    
# Line 86  class StringLiteral : virtual public Str Line 94  class StringLiteral : virtual public Str
94  public:  public:
95      String value;      String value;
96      StringLiteral(const String& value) : value(value) { }      StringLiteral(const String& value) : value(value) { }
97      bool isConstExpr() const { return true; }      bool isConstExpr() const OVERRIDE { return true; }
98      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
99      String evalStr() { return value; }      String evalStr() OVERRIDE { return value; }
100      bool isPolyphonic() const { return false; }      bool isPolyphonic() const OVERRIDE { return false; }
101  };  };
102  typedef Ref<StringLiteral,Node> StringLiteralRef;  typedef Ref<StringLiteral,Node> StringLiteralRef;
103    
# Line 97  class Args : virtual public VMFnArgs, vi Line 105  class Args : virtual public VMFnArgs, vi
105  public:  public:
106      std::vector<ExpressionRef> args;      std::vector<ExpressionRef> args;
107      void add(ExpressionRef arg) { args.push_back(arg); }      void add(ExpressionRef arg) { args.push_back(arg); }
108      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
109      int argsCount() const { return args.size(); }      vmint argsCount() const OVERRIDE { return (vmint) args.size(); }
110      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; }
111      bool isPolyphonic() const;      bool isPolyphonic() const OVERRIDE;
112  };  };
113  typedef Ref<Args,Node> ArgsRef;  typedef Ref<Args,Node> ArgsRef;
114    
115  class Variable : virtual public Expression {  class Variable : virtual public VMVariable, virtual public Expression {
116  public:  public:
117      virtual bool isConstExpr() const { return bConst; }      bool isConstExpr() const OVERRIDE { return bConst; }
118        bool isAssignable() const OVERRIDE { return !bConst; }
119      virtual void assign(Expression* expr) = 0;      virtual void assign(Expression* expr) = 0;
120        void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); }
121  protected:  protected:
122      Variable(ParserContext* ctx, int _memPos, bool _bConst)      Variable(ParserContext* ctx, vmint _memPos, bool _bConst)
123          : context(ctx), memPos(_memPos), bConst(_bConst) {}          : context(ctx), memPos(_memPos), bConst(_bConst) {}
124    
125      ParserContext* context;      ParserContext* context;
126      int memPos;      vmint memPos;
127      bool bConst;      bool bConst;
128  };  };
129  typedef Ref<Variable,Node> VariableRef;  typedef Ref<Variable,Node> VariableRef;
# Line 122  class IntVariable : public Variable, vir Line 132  class IntVariable : public Variable, vir
132      bool polyphonic;      bool polyphonic;
133  public:  public:
134      IntVariable(ParserContext* ctx);      IntVariable(ParserContext* ctx);
135      void assign(Expression* expr);      void assign(Expression* expr) OVERRIDE;
136      int evalInt();      vmint evalInt() OVERRIDE;
137      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
138      bool isPolyphonic() const { return polyphonic; }      bool isPolyphonic() const OVERRIDE { return polyphonic; }
139  protected:  protected:
140      IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size = 1);      IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, vmint size = 1);
141  };  };
142  typedef Ref<IntVariable,Node> IntVariableRef;  typedef Ref<IntVariable,Node> IntVariableRef;
143    
144  class ConstIntVariable : public IntVariable {  class ConstIntVariable : public IntVariable {
145  public:  public:
146      int value;      vmint value;
147    
148      ConstIntVariable(int value);      ConstIntVariable(vmint value);
149      //ConstIntVariable(ParserContext* ctx, int value = 0);      //ConstIntVariable(ParserContext* ctx, int value = 0);
150      void assign(Expression* expr);      void assign(Expression* expr) OVERRIDE;
151      int evalInt();      vmint evalInt() OVERRIDE;
152      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
153  };  };
154  typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;  typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;
155    
156  class BuiltInIntVariable : public IntVariable {  class BuiltInIntVariable : public IntVariable {
157      String name;      String name;
158      VMIntRelPtr* ptr;      VMIntPtr* ptr;
159  public:  public:
160      BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);      BuiltInIntVariable(const String& name, VMIntPtr* ptr);
161      void assign(Expression* expr);      bool isAssignable() const OVERRIDE { return ptr->isAssignable(); }
162      int evalInt();      void assign(Expression* expr) OVERRIDE;
163      void dump(int level = 0);      vmint evalInt() OVERRIDE;
164        void dump(int level = 0) OVERRIDE;
165  };  };
166  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
167    
168  class PolyphonicIntVariable : public IntVariable {  class PolyphonicIntVariable : public IntVariable {
169  public:  public:
170      PolyphonicIntVariable(ParserContext* ctx);      PolyphonicIntVariable(ParserContext* ctx);
171      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
172  };  };
173  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
174    
175  class IntArrayVariable : public Variable, virtual public VMIntArrayExpr {  class IntArrayVariable : public Variable, virtual public IntArrayExpr {
176      ArrayList<int> values;      ArrayList<vmint> values;
177  public:  public:
178      IntArrayVariable(ParserContext* ctx, int size);      IntArrayVariable(ParserContext* ctx, vmint size);
179      IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);      IntArrayVariable(ParserContext* ctx, vmint size, ArgsRef values, bool _bConst = false);
180      void assign(Expression* expr) {} // ignore scalar assignment      void assign(Expression* expr) OVERRIDE {} // ignore scalar assignment
181      String evalCastToStr() { return ""; } // ignore scalar cast to string      String evalCastToStr() OVERRIDE { return ""; } // ignore scalar cast to string
182      ExprType_t exprType() const { return INT_ARR_EXPR; }      ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; }
183      virtual int arraySize() const { return values.size(); }      virtual vmint arraySize() const OVERRIDE { return values.size(); }
184      virtual int evalIntElement(uint i);      virtual vmint evalIntElement(vmuint i) OVERRIDE;
185      virtual void assignIntElement(uint i, int value);      virtual void assignIntElement(vmuint i, vmint value) OVERRIDE;
186      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
187      bool isPolyphonic() const { return false; }      bool isPolyphonic() const OVERRIDE { return false; }
188  protected:  protected:
189      IntArrayVariable(ParserContext* ctx, bool bConst);      IntArrayVariable(ParserContext* ctx, bool bConst);
190  };  };
# Line 184  class BuiltInIntArrayVariable : public I Line 195  class BuiltInIntArrayVariable : public I
195      VMInt8Array* array;      VMInt8Array* array;
196  public:  public:
197      BuiltInIntArrayVariable(const String& name, VMInt8Array* array);      BuiltInIntArrayVariable(const String& name, VMInt8Array* array);
198      int arraySize() const { return array->size; }      vmint arraySize() const OVERRIDE { return array->size; }
199      int evalIntElement(uint i);      vmint evalIntElement(vmuint i) OVERRIDE;
200      void assignIntElement(uint i, int value);      bool isAssignable() const OVERRIDE { return !array->readonly; }
201      void dump(int level = 0);      void assignIntElement(vmuint i, vmint value) OVERRIDE;
202        void dump(int level = 0) OVERRIDE;
203  };  };
204  typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;  typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;
205    
206  class IntArrayElement : public IntVariable {  class IntArrayElement : public IntVariable {
207      IntArrayVariableRef array;      IntArrayExprRef array;
208      IntExprRef index;      IntExprRef index;
209  public:  public:
210      IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex);      IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex);
211      void assign(Expression* expr);      void assign(Expression* expr) OVERRIDE;
212      int evalInt();      vmint evalInt() OVERRIDE;
213      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
214  };  };
215  typedef Ref<IntArrayElement,Node> IntArrayElementRef;  typedef Ref<IntArrayElement,Node> IntArrayElementRef;
216    
217  class StringVariable : public Variable, virtual public StringExpr {  class StringVariable : public Variable, virtual public StringExpr {
218  public:  public:
219      StringVariable(ParserContext* ctx);      StringVariable(ParserContext* ctx);
220      void assign(Expression* expr);      void assign(Expression* expr) OVERRIDE;
221      String evalStr();      String evalStr() OVERRIDE;
222      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
223      bool isPolyphonic() const { return false; }      bool isPolyphonic() const OVERRIDE { return false; }
224  protected:  protected:
225      StringVariable(ParserContext* ctx, bool bConst);      StringVariable(ParserContext* ctx, bool bConst);
226  };  };
# Line 219  public: Line 231  public:
231      String value;      String value;
232    
233      ConstStringVariable(ParserContext* ctx, String value = "");      ConstStringVariable(ParserContext* ctx, String value = "");
234      void assign(Expression* expr);      void assign(Expression* expr) OVERRIDE;
235      String evalStr();      String evalStr() OVERRIDE;
236      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
237  };  };
238  typedef Ref<ConstStringVariable,Node> ConstStringVariableRef;  typedef Ref<ConstStringVariable,Node> ConstStringVariableRef;
239    
# Line 231  protected: Line 243  protected:
243      ExpressionRef rhs;      ExpressionRef rhs;
244  public:  public:
245      BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }      BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }
246      bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }      bool isConstExpr() const OVERRIDE { return lhs->isConstExpr() && rhs->isConstExpr(); }
247      bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }      bool isPolyphonic() const OVERRIDE { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
248  };  };
249  typedef Ref<BinaryOp,Node> BinaryOpRef;  typedef Ref<BinaryOp,Node> BinaryOpRef;
250    
251  class Add : virtual public BinaryOp, virtual public IntExpr {  class Add : virtual public BinaryOp, virtual public IntExpr {
252  public:  public:
253      Add(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }      Add(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
254      int evalInt();      vmint evalInt() OVERRIDE;
255      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
256  };  };
257  typedef Ref<Add,Node> AddRef;  typedef Ref<Add,Node> AddRef;
258    
259  class Sub : virtual public BinaryOp, virtual public IntExpr {  class Sub : virtual public BinaryOp, virtual public IntExpr {
260  public:  public:
261      Sub(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }      Sub(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
262      int evalInt();      vmint evalInt() OVERRIDE;
263      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
264  };  };
265  typedef Ref<Sub,Node> SubRef;  typedef Ref<Sub,Node> SubRef;
266    
267  class Mul : virtual public BinaryOp, virtual public IntExpr {  class Mul : virtual public BinaryOp, virtual public IntExpr {
268  public:  public:
269      Mul(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }      Mul(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
270      int evalInt();      vmint evalInt() OVERRIDE;
271      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
272  };  };
273  typedef Ref<Mul,Node> MulRef;  typedef Ref<Mul,Node> MulRef;
274    
275  class Div : virtual public BinaryOp, virtual public IntExpr {  class Div : virtual public BinaryOp, virtual public IntExpr {
276  public:  public:
277      Div(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }      Div(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
278      int evalInt();      vmint evalInt() OVERRIDE;
279      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
280  };  };
281  typedef Ref<Div,Node> DivRef;  typedef Ref<Div,Node> DivRef;
282    
283  class Mod : virtual public BinaryOp, virtual public IntExpr {  class Mod : virtual public BinaryOp, virtual public IntExpr {
284  public:  public:
285      Mod(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }      Mod(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
286      int evalInt();      vmint evalInt() OVERRIDE;
287      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
288  };  };
289  typedef Ref<Mod,Node> ModRef;  typedef Ref<Mod,Node> ModRef;
290    
# Line 287  typedef Ref<Statement,Node> StatementRef Line 299  typedef Ref<Statement,Node> StatementRef
299  class NoOperation : public Statement {  class NoOperation : public Statement {
300  public:  public:
301      NoOperation() : Statement() {}      NoOperation() : Statement() {}
302      StmtType_t statementType() const { return STMT_LEAF; }      StmtType_t statementType() const OVERRIDE { return STMT_NOOP; }
303      void dump(int level = 0) {}      void dump(int level = 0) OVERRIDE {}
304      bool isPolyphonic() const { return false; }      bool isPolyphonic() const OVERRIDE { return false; }
305  };  };
306  typedef Ref<NoOperation,Node> NoOperationRef;  typedef Ref<NoOperation,Node> NoOperationRef;
307    
# Line 298  bool isNoOperation(StatementRef statemen Line 310  bool isNoOperation(StatementRef statemen
310  class LeafStatement : public Statement {  class LeafStatement : public Statement {
311  public:  public:
312      virtual StmtFlags_t exec() = 0;      virtual StmtFlags_t exec() = 0;
313      virtual StmtType_t statementType() const { return STMT_LEAF; }      StmtType_t statementType() const OVERRIDE { return STMT_LEAF; }
314  };  };
315  typedef Ref<LeafStatement,Node> LeafStatementRef;  typedef Ref<LeafStatement,Node> LeafStatementRef;
316    
# Line 306  class Statements : public Statement { Line 318  class Statements : public Statement {
318      std::vector<StatementRef> args;      std::vector<StatementRef> args;
319  public:  public:
320      void add(StatementRef arg) { args.push_back(arg); }      void add(StatementRef arg) { args.push_back(arg); }
321      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
322      StmtType_t statementType() const { return STMT_LIST; }      StmtType_t statementType() const OVERRIDE { return STMT_LIST; }
323      virtual Statement* statement(uint i);      virtual Statement* statement(uint i);
324      bool isPolyphonic() const;      bool isPolyphonic() const OVERRIDE;
325  };  };
326  typedef Ref<Statements,Node> StatementsRef;  typedef Ref<Statements,Node> StatementsRef;
327    
328  class BranchStatement : public Statement {  class BranchStatement : public Statement {
329  public:  public:
330      StmtType_t statementType() const { return STMT_BRANCH; }      StmtType_t statementType() const OVERRIDE { return STMT_BRANCH; }
331      virtual int evalBranch() = 0;      virtual vmint evalBranch() = 0;
332      virtual Statements* branch(uint i) const = 0;      virtual Statements* branch(vmuint i) const = 0;
333    };
334    
335    class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr, virtual public IntArrayExpr {
336        VMDynVar* dynVar;
337        String varName;
338    public:
339        DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v);
340        ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); }
341        bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); }
342        bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); }
343        bool isPolyphonic() const OVERRIDE { return false; }
344        void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); }
345        VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); }
346        vmint evalInt() OVERRIDE;
347        String evalStr() OVERRIDE;
348        String evalCastToStr() OVERRIDE;
349        vmint arraySize() const OVERRIDE { return dynVar->asIntArray()->arraySize(); }
350        vmint evalIntElement(vmuint i) OVERRIDE { return dynVar->asIntArray()->evalIntElement(i); }
351        void assignIntElement(vmuint i, vmint value) OVERRIDE { return dynVar->asIntArray()->assignIntElement(i, value); }
352        void dump(int level = 0) OVERRIDE;
353  };  };
354    typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
355    
356  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
357      String functionName;      String functionName;
# Line 327  class FunctionCall : virtual public Leaf Line 360  class FunctionCall : virtual public Leaf
360  public:  public:
361      FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :      FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :
362          functionName(function), args(args), fn(fn) { }          functionName(function), args(args), fn(fn) { }
363      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
364      StmtFlags_t exec();      StmtFlags_t exec() OVERRIDE;
365      int evalInt();      vmint evalInt() OVERRIDE;
366      String evalStr();      VMIntArrayExpr* asIntArray() const OVERRIDE;
367      bool isConstExpr() const { return false; }      String evalStr() OVERRIDE;
368      ExprType_t exprType() const;      bool isConstExpr() const OVERRIDE { return false; }
369      String evalCastToStr();      ExprType_t exprType() const OVERRIDE;
370      bool isPolyphonic() const { return args->isPolyphonic(); }      String evalCastToStr() OVERRIDE;
371        bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); }
372  protected:  protected:
373      VMFnResult* execVMFn();      VMFnResult* execVMFn();
374  };  };
375  typedef Ref<FunctionCall,Node> FunctionCallRef;  typedef Ref<FunctionCall,Node> FunctionCallRef;
376    
377    class NoFunctionCall : public FunctionCall {
378    public:
379        NoFunctionCall() : FunctionCall("nothing", new Args, NULL) {}
380        StmtType_t statementType() const OVERRIDE { return STMT_NOOP; }
381    };
382    typedef Ref<NoFunctionCall,Node> NoFunctionCallRef;
383    
384  class EventHandler : virtual public Statements, virtual public VMEventHandler {  class EventHandler : virtual public Statements, virtual public VMEventHandler {
385      StatementsRef statements;      StatementsRef statements;
386      bool usingPolyphonics;      bool usingPolyphonics;
387  public:  public:
388      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
389      StmtFlags_t exec();      StmtFlags_t exec();
390      EventHandler(StatementsRef statements);      EventHandler(StatementsRef statements);
391      Statement* statement(uint i) { return statements->statement(i); }      Statement* statement(uint i) OVERRIDE { return statements->statement(i); }
392      bool isPolyphonic() const { return usingPolyphonics; }      bool isPolyphonic() const OVERRIDE { return usingPolyphonics; }
393  };  };
394  typedef Ref<EventHandler,Node> EventHandlerRef;  typedef Ref<EventHandler,Node> EventHandlerRef;
395    
396  class OnNote : public EventHandler {  class OnNote : public EventHandler {
397  public:  public:
398      OnNote(StatementsRef statements) : EventHandler(statements) {}      OnNote(StatementsRef statements) : EventHandler(statements) {}
399      String eventHandlerName() const { return "note"; }      VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_NOTE; }
400        String eventHandlerName() const OVERRIDE { return "note"; }
401  };  };
402  typedef Ref<OnNote,Node> OnNoteRef;  typedef Ref<OnNote,Node> OnNoteRef;
403    
404  class OnInit : public EventHandler {  class OnInit : public EventHandler {
405  public:  public:
406      OnInit(StatementsRef statements) : EventHandler(statements) {}      OnInit(StatementsRef statements) : EventHandler(statements) {}
407      String eventHandlerName() const { return "init"; }      VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_INIT; }
408        String eventHandlerName() const OVERRIDE { return "init"; }
409  };  };
410  typedef Ref<OnInit,Node> OnInitRef;  typedef Ref<OnInit,Node> OnInitRef;
411    
412  class OnRelease : public EventHandler {  class OnRelease : public EventHandler {
413  public:  public:
414      OnRelease(StatementsRef statements) : EventHandler(statements) {}      OnRelease(StatementsRef statements) : EventHandler(statements) {}
415      String eventHandlerName() const { return "release"; }      VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_RELEASE; }
416        String eventHandlerName() const OVERRIDE { return "release"; }
417  };  };
418  typedef Ref<OnRelease,Node> OnReleaseRef;  typedef Ref<OnRelease,Node> OnReleaseRef;
419    
420  class OnController : public EventHandler {  class OnController : public EventHandler {
421  public:  public:
422      OnController(StatementsRef statements) : EventHandler(statements) {}      OnController(StatementsRef statements) : EventHandler(statements) {}
423      String eventHandlerName() const { return "controller"; }      VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_CONTROLLER; }
424        String eventHandlerName() const OVERRIDE { return "controller"; }
425  };  };
426  typedef Ref<OnController,Node> OnControllerRef;  typedef Ref<OnController,Node> OnControllerRef;
427    
# Line 386  public: Line 431  public:
431      EventHandlers();      EventHandlers();
432      ~EventHandlers();      ~EventHandlers();
433      void add(EventHandlerRef arg);      void add(EventHandlerRef arg);
434      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
     int evalInt() { return 0; }  
435      EventHandler* eventHandlerByName(const String& name) const;      EventHandler* eventHandlerByName(const String& name) const;
436      EventHandler* eventHandler(uint index) const;      EventHandler* eventHandler(uint index) const;
437      inline uint size() const { return args.size(); }      inline uint size() const { return (int) args.size(); }
438      bool isPolyphonic() const;      bool isPolyphonic() const OVERRIDE;
439  };  };
440  typedef Ref<EventHandlers,Node> EventHandlersRef;  typedef Ref<EventHandlers,Node> EventHandlersRef;
441    
# Line 401  protected: Line 445  protected:
445      ExpressionRef value;      ExpressionRef value;
446  public:  public:
447      Assignment(VariableRef variable, ExpressionRef value);      Assignment(VariableRef variable, ExpressionRef value);
448      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
449      StmtFlags_t exec();      StmtFlags_t exec() OVERRIDE;
450      bool isPolyphonic() const { return variable->isPolyphonic() || value->isPolyphonic(); }      bool isPolyphonic() const OVERRIDE { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); }
451  };  };
452  typedef Ref<Assignment,Node> AssignmentRef;  typedef Ref<Assignment,Node> AssignmentRef;
453    
# Line 416  public: Line 460  public:
460          condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { }          condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { }
461      If(IntExprRef condition, StatementsRef statements) :      If(IntExprRef condition, StatementsRef statements) :
462          condition(condition), ifStatements(statements) { }          condition(condition), ifStatements(statements) { }
463      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
464      int evalBranch();      vmint evalBranch() OVERRIDE;
465      Statements* branch(uint i) const;      Statements* branch(vmuint i) const OVERRIDE;
466      bool isPolyphonic() const;      bool isPolyphonic() const OVERRIDE;
467  };  };
468  typedef Ref<If,Node> IfRef;  typedef Ref<If,Node> IfRef;
469    
# Line 436  class SelectCase : public BranchStatemen Line 480  class SelectCase : public BranchStatemen
480      CaseBranches branches;      CaseBranches branches;
481  public:  public:
482      SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { }      SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { }
483      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
484      int evalBranch();      vmint evalBranch() OVERRIDE;
485      Statements* branch(uint i) const;      Statements* branch(vmuint i) const OVERRIDE;
486      //void addBranch(IntExprRef condition, StatementsRef statements);      //void addBranch(IntExprRef condition, StatementsRef statements);
487      //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);      //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);
488      //void addBranch(CaseBranchRef branch);      //void addBranch(CaseBranchRef branch);
489      //void addBranches(CaseBranchesRef branches);      //void addBranches(CaseBranchesRef branches);
490      bool isPolyphonic() const;      bool isPolyphonic() const OVERRIDE;
491  };  };
492  typedef Ref<SelectCase,Node> SelectCaseRef;  typedef Ref<SelectCase,Node> SelectCaseRef;
493    
# Line 453  class While : public Statement { Line 497  class While : public Statement {
497  public:  public:
498      While(IntExprRef condition, StatementsRef statements) :      While(IntExprRef condition, StatementsRef statements) :
499          m_condition(condition), m_statements(statements) {}          m_condition(condition), m_statements(statements) {}
500      StmtType_t statementType() const { return STMT_LOOP; }      StmtType_t statementType() const OVERRIDE { return STMT_LOOP; }
501      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
502      bool evalLoopStartCondition();      bool evalLoopStartCondition();
503      Statements* statements() const;      Statements* statements() const;
504      bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }      bool isPolyphonic() const OVERRIDE { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
505  };  };
506    
507    class SyncBlock : public Statement {
508        StatementsRef m_statements;
509    public:
510        SyncBlock(StatementsRef statements) : m_statements(statements) {}
511        StmtType_t statementType() const OVERRIDE { return STMT_SYNC; }
512        void dump(int level = 0) OVERRIDE;
513        Statements* statements() const;
514        bool isPolyphonic() const OVERRIDE { return m_statements->isPolyphonic(); }
515    };
516    typedef Ref<SyncBlock,Node> SyncBlockRef;
517    
518  class Neg : public IntExpr {  class Neg : public IntExpr {
519      IntExprRef expr;      IntExprRef expr;
520  public:  public:
521      Neg(IntExprRef expr) : expr(expr) { }      Neg(IntExprRef expr) : expr(expr) { }
522      int evalInt() { return (expr) ? -expr->evalInt() : 0; }      vmint evalInt() OVERRIDE { return (expr) ? -expr->evalInt() : 0; }
523      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
524      bool isConstExpr() const { return expr->isConstExpr(); }      bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); }
525      bool isPolyphonic() const { return expr->isPolyphonic(); }      bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); }
526  };  };
527  typedef Ref<Neg,Node> NegRef;  typedef Ref<Neg,Node> NegRef;
528    
# Line 476  class ConcatString : public StringExpr { Line 531  class ConcatString : public StringExpr {
531      ExpressionRef rhs;      ExpressionRef rhs;
532  public:  public:
533      ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {}      ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {}
534      String evalStr();      String evalStr() OVERRIDE;
535      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
536      bool isConstExpr() const;      bool isConstExpr() const OVERRIDE;
537      bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }      bool isPolyphonic() const OVERRIDE { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
538  };  };
539  typedef Ref<ConcatString,Node> ConcatStringRef;  typedef Ref<ConcatString,Node> ConcatStringRef;
540    
# Line 495  public: Line 550  public:
550      };      };
551      Relation(IntExprRef lhs, Type type, IntExprRef rhs) :      Relation(IntExprRef lhs, Type type, IntExprRef rhs) :
552          lhs(lhs), rhs(rhs), type(type) {}          lhs(lhs), rhs(rhs), type(type) {}
553      int evalInt();      vmint evalInt() OVERRIDE;
554      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
555      bool isConstExpr() const;      bool isConstExpr() const OVERRIDE;
556      bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }      bool isPolyphonic() const OVERRIDE { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
557  private:  private:
558      IntExprRef lhs;      IntExprRef lhs;
559      IntExprRef rhs;      IntExprRef rhs;
# Line 509  typedef Ref<Relation,Node> RelationRef; Line 564  typedef Ref<Relation,Node> RelationRef;
564  class Or : virtual public BinaryOp, virtual public IntExpr {  class Or : virtual public BinaryOp, virtual public IntExpr {
565  public:  public:
566      Or(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}      Or(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
567      int evalInt();      vmint evalInt() OVERRIDE;
568      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
569  };  };
570  typedef Ref<Or,Node> OrRef;  typedef Ref<Or,Node> OrRef;
571    
572    class BitwiseOr : virtual public BinaryOp, virtual public IntExpr {
573    public:
574        BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
575        vmint evalInt() OVERRIDE;
576        void dump(int level = 0) OVERRIDE;
577    };
578    typedef Ref<BitwiseOr,Node> BitwiseOrRef;
579    
580  class And : virtual public BinaryOp, virtual public IntExpr {  class And : virtual public BinaryOp, virtual public IntExpr {
581  public:  public:
582      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
583      int evalInt();      vmint evalInt() OVERRIDE;
584      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
585  };  };
586  typedef Ref<And,Node> AndRef;  typedef Ref<And,Node> AndRef;
587    
588    class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr {
589    public:
590        BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
591        vmint evalInt() OVERRIDE;
592        void dump(int level = 0) OVERRIDE;
593    };
594    typedef Ref<BitwiseAnd,Node> BitwiseAndRef;
595    
596  class Not : virtual public IntExpr {  class Not : virtual public IntExpr {
597      IntExprRef expr;      IntExprRef expr;
598  public:  public:
599      Not(IntExprRef expr) : expr(expr) {}      Not(IntExprRef expr) : expr(expr) {}
600      int evalInt() { return !expr->evalInt(); }      vmint evalInt() OVERRIDE { return !expr->evalInt(); }
601      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
602      bool isConstExpr() const { return expr->isConstExpr(); }      bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); }
603      bool isPolyphonic() const { return expr->isPolyphonic(); }      bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); }
604  };  };
605  typedef Ref<Not,Node> NotRef;  typedef Ref<Not,Node> NotRef;
606    
607    class BitwiseNot : virtual public IntExpr {
608        IntExprRef expr;
609    public:
610        BitwiseNot(IntExprRef expr) : expr(expr) {}
611        vmint evalInt() OVERRIDE { return ~expr->evalInt(); }
612        void dump(int level = 0) OVERRIDE;
613        bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); }
614        bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); }
615    };
616    typedef Ref<BitwiseNot,Node> BitwiseNotRef;
617    
618  class ParserContext : public VMParserContext {  class ParserContext : public VMParserContext {
619  public:  public:
620      struct Error {      struct Error {
# Line 546  public: Line 628  public:
628      std::vector<ParserIssue> vErrors;      std::vector<ParserIssue> vErrors;
629      std::vector<ParserIssue> vWarnings;      std::vector<ParserIssue> vWarnings;
630      std::vector<ParserIssue> vIssues;      std::vector<ParserIssue> vIssues;
631        std::vector<CodeBlock>   vPreprocessorComments;
632    
633      std::set<String> builtinPreprocessorConditions;      std::set<String> builtinPreprocessorConditions;
634      std::set<String> userPreprocessorConditions;      std::set<String> userPreprocessorConditions;
635    
636      std::map<String,VariableRef> vartable;      std::map<String,VariableRef> vartable;
637      int globalIntVarCount;      std::map<String,StatementsRef> userFnTable;
638      int globalStrVarCount;      vmint globalIntVarCount;
639      int polyphonicIntVarCount;      vmint globalStrVarCount;
640        vmint polyphonicIntVarCount;
641    
642      EventHandlersRef handlers;      EventHandlersRef handlers;
643    
# Line 562  public: Line 646  public:
646      OnReleaseRef onRelease;      OnReleaseRef onRelease;
647      OnControllerRef onController;      OnControllerRef onController;
648    
649      ArrayList<int>* globalIntMemory;      ArrayList<vmint>* globalIntMemory;
650      ArrayList<String>* globalStrMemory;      ArrayList<String>* globalStrMemory;
651      int requiredMaxStackSize;      vmint requiredMaxStackSize;
652    
653      VMFunctionProvider* functionProvider;      VMFunctionProvider* functionProvider;
654    
# Line 582  public: Line 666  public:
666      IntVariableRef globalIntVar(const String& name);      IntVariableRef globalIntVar(const String& name);
667      StringVariableRef globalStrVar(const String& name);      StringVariableRef globalStrVar(const String& name);
668      VariableRef variableByName(const String& name);      VariableRef variableByName(const String& name);
669      void addErr(int line, const char* txt);      StatementsRef userFunctionByName(const String& name);
670      void addWrn(int line, const char* txt);      void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
671        void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
672        void addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn);
673      void createScanner(std::istream* is);      void createScanner(std::istream* is);
674      void destroyScanner();      void destroyScanner();
675      bool setPreprocessorCondition(const char* name);      bool setPreprocessorCondition(const char* name);
# Line 592  public: Line 678  public:
678      std::vector<ParserIssue> issues() const OVERRIDE;      std::vector<ParserIssue> issues() const OVERRIDE;
679      std::vector<ParserIssue> errors() const OVERRIDE;      std::vector<ParserIssue> errors() const OVERRIDE;
680      std::vector<ParserIssue> warnings() const OVERRIDE;      std::vector<ParserIssue> warnings() const OVERRIDE;
681        std::vector<CodeBlock> preprocessorComments() const OVERRIDE;
682      VMEventHandler* eventHandler(uint index) OVERRIDE;      VMEventHandler* eventHandler(uint index) OVERRIDE;
683      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
684      void registerBuiltInConstIntVariables(const std::map<String,int>& vars);      void registerBuiltInConstIntVariables(const std::map<String,vmint>& vars);
685      void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);      void registerBuiltInIntVariables(const std::map<String,VMIntPtr*>& vars);
686      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
687        void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
688  };  };
689    
690  class ExecContext : public VMExecContext {  class ExecContext : public VMExecContext {
# Line 611  public: Line 699  public:
699          }          }
700      };      };
701    
702      ArrayList<int> polyphonicIntMemory;      ArrayList<vmint> polyphonicIntMemory;
703      VMExecStatus_t status;      VMExecStatus_t status;
704        StmtFlags_t flags;
705      ArrayList<StackFrame> stack;      ArrayList<StackFrame> stack;
706      int stackFrame;      int stackFrame;
707      int suspendMicroseconds;      vmint suspendMicroseconds;
708        size_t instructionsCount;
709        struct ExitRes {
710            Expression* value;
711            IntLiteral intLiteral;
712            StringLiteral stringLiteral;
713    
714      ExecContext() :          ExitRes() : intLiteral(0), stringLiteral("") { }
715          status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {}      } exitRes;
716    
717        ExecContext();
718      virtual ~ExecContext() {}      virtual ~ExecContext() {}
719    
720      inline void pushStack(Statement* stmt) {      inline void pushStack(Statement* stmt) {
# Line 641  public: Line 736  public:
736          stack[0].statement = NULL;          stack[0].statement = NULL;
737          stack[0].subindex  = -1;          stack[0].subindex  = -1;
738          stackFrame = -1;          stackFrame = -1;
739            flags = STMT_SUCCESS;
740        }
741    
742        inline void clearExitRes() {
743            exitRes.value = NULL;
744      }      }
745    
746      int suspensionTimeMicroseconds() const OVERRIDE {      vmint suspensionTimeMicroseconds() const OVERRIDE {
747          return suspendMicroseconds;          return suspendMicroseconds;
748      }      }
749    
750        void resetPolyphonicData() OVERRIDE {
751            if (polyphonicIntMemory.empty()) return;
752            memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(vmint));
753        }
754    
755        size_t instructionsPerformed() const OVERRIDE {
756            return instructionsCount;
757        }
758    
759        void signalAbort() OVERRIDE {
760            flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED);
761        }
762    
763        void forkTo(VMExecContext* ectx) const OVERRIDE;
764    
765        VMExpr* exitResult() OVERRIDE {
766            return exitRes.value;
767        }
768  };  };
769    
770  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.2645  
changed lines
  Added in v.3557

  ViewVC Help
Powered by ViewVC