/[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 2588 by schoenebeck, Sun Jun 1 14:44:38 2014 UTC revision 2942 by schoenebeck, Wed Jul 13 15:51:06 2016 UTC
# Line 1  Line 1 
1  /*                                                              -*- c++ -*-  /*                                                              -*- c++ -*-
2   *   *
3   * Copyright (c) 2014 Christian Schoenebeck and Andreas Persson   * Copyright (c) 2014 - 2016 Christian Schoenebeck and Andreas Persson
4   *   *
5   * http://www.linuxsampler.org   * http://www.linuxsampler.org
6   *   *
# Line 42  public: Line 42  public:
42      Node();      Node();
43      virtual ~Node();      virtual ~Node();
44      virtual void dump(int level = 0) = 0;      virtual void dump(int level = 0) = 0;
45        virtual bool isPolyphonic() const = 0;
46      void printIndents(int n);      void printIndents(int n);
47  };  };
48  typedef Ref<Node> NodeRef;  typedef Ref<Node> NodeRef;
# Line 77  public: Line 78  public:
78      int evalInt();      int evalInt();
79      void dump(int level = 0);      void dump(int level = 0);
80      bool isConstExpr() const { return true; }      bool isConstExpr() const { return true; }
81        bool isPolyphonic() const { return false; }
82  };  };
83  typedef Ref<IntLiteral,Node> IntLiteralRef;  typedef Ref<IntLiteral,Node> IntLiteralRef;
84    
# Line 87  public: Line 89  public:
89      bool isConstExpr() const { return true; }      bool isConstExpr() const { return true; }
90      void dump(int level = 0);      void dump(int level = 0);
91      String evalStr() { return value; }      String evalStr() { return value; }
92        bool isPolyphonic() const { return false; }
93  };  };
94  typedef Ref<StringLiteral,Node> StringLiteralRef;  typedef Ref<StringLiteral,Node> StringLiteralRef;
95    
# Line 97  public: Line 100  public:
100      void dump(int level = 0);      void dump(int level = 0);
101      int argsCount() const { return args.size(); }      int argsCount() const { return args.size(); }
102      VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; }      VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; }
103        bool isPolyphonic() const;
104  };  };
105  typedef Ref<Args,Node> ArgsRef;  typedef Ref<Args,Node> ArgsRef;
106    
107  class Variable : virtual public Expression {  class Variable : virtual public Expression {
108  public:  public:
109      virtual bool isConstExpr() const { return bConst; }      virtual bool isConstExpr() const { return bConst; }
110        virtual bool isAssignable() const { return !bConst; }
111      virtual void assign(Expression* expr) = 0;      virtual void assign(Expression* expr) = 0;
112  protected:  protected:
113      Variable(ParserContext* ctx, int _memPos, bool _bConst)      Variable(ParserContext* ctx, int _memPos, bool _bConst)
# Line 139  public: Line 144  public:
144  };  };
145  typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;  typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;
146    
147    class BuiltInIntVariable : public IntVariable {
148        String name;
149        VMIntRelPtr* ptr;
150    public:
151        BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);
152        void assign(Expression* expr);
153        int evalInt();
154        void dump(int level = 0);
155    };
156    typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
157    
158  class PolyphonicIntVariable : public IntVariable {  class PolyphonicIntVariable : public IntVariable {
159  public:  public:
160      PolyphonicIntVariable(ParserContext* ctx);      PolyphonicIntVariable(ParserContext* ctx);
# Line 146  public: Line 162  public:
162  };  };
163  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
164    
165  class IntArrayVariable : public Variable {  class IntArrayVariable : public Variable, virtual public VMIntArrayExpr {
166      ArrayList<int> values;      ArrayList<int> values;
167  public:  public:
168      IntArrayVariable(ParserContext* ctx, int size);      IntArrayVariable(ParserContext* ctx, int size);
169      IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);      IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);
170      void assign(Expression* expr) {} // ignore scalar assignment      void assign(Expression* expr) {} // ignore scalar assignment
171      String evalCastToStr() { return ""; } // ignore cast to string      String evalCastToStr() { return ""; } // ignore scalar cast to string
172      ExprType_t exprType() const { return INT_ARR_EXPR; }      ExprType_t exprType() const { return INT_ARR_EXPR; }
173      const int arraySize() const { return values.size(); }      virtual int arraySize() const { return values.size(); }
174        virtual int evalIntElement(uint i);
175        virtual void assignIntElement(uint i, int value);
176        void dump(int level = 0);
177        bool isPolyphonic() const { return false; }
178    protected:
179        IntArrayVariable(ParserContext* ctx, bool bConst);
180    };
181    typedef Ref<IntArrayVariable,Node> IntArrayVariableRef;
182    
183    class BuiltInIntArrayVariable : public IntArrayVariable {
184        String name;
185        VMInt8Array* array;
186    public:
187        BuiltInIntArrayVariable(const String& name, VMInt8Array* array);
188        int arraySize() const { return array->size; }
189      int evalIntElement(uint i);      int evalIntElement(uint i);
190      void assignIntElement(uint i, int value);      void assignIntElement(uint i, int value);
191      void dump(int level = 0);      void dump(int level = 0);
192  };  };
193  typedef Ref<IntArrayVariable,Node> IntArrayVariableRef;  typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;
194    
195  class IntArrayElement : public IntVariable {  class IntArrayElement : public IntVariable {
196      IntArrayVariableRef array;      IntArrayVariableRef array;
# Line 178  public: Line 209  public:
209      void assign(Expression* expr);      void assign(Expression* expr);
210      String evalStr();      String evalStr();
211      void dump(int level = 0);      void dump(int level = 0);
212        bool isPolyphonic() const { return false; }
213  protected:  protected:
214      StringVariable(ParserContext* ctx, bool bConst);      StringVariable(ParserContext* ctx, bool bConst);
215  };  };
# Line 201  protected: Line 233  protected:
233  public:  public:
234      BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }      BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }
235      bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }      bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }
236        bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
237  };  };
238  typedef Ref<BinaryOp,Node> BinaryOpRef;  typedef Ref<BinaryOp,Node> BinaryOpRef;
239    
# Line 257  public: Line 290  public:
290      NoOperation() : Statement() {}      NoOperation() : Statement() {}
291      StmtType_t statementType() const { return STMT_LEAF; }      StmtType_t statementType() const { return STMT_LEAF; }
292      void dump(int level = 0) {}      void dump(int level = 0) {}
293        bool isPolyphonic() const { return false; }
294  };  };
295  typedef Ref<NoOperation,Node> NoOperationRef;  typedef Ref<NoOperation,Node> NoOperationRef;
296    
# Line 276  public: Line 310  public:
310      void dump(int level = 0);      void dump(int level = 0);
311      StmtType_t statementType() const { return STMT_LIST; }      StmtType_t statementType() const { return STMT_LIST; }
312      virtual Statement* statement(uint i);      virtual Statement* statement(uint i);
313        bool isPolyphonic() const;
314  };  };
315  typedef Ref<Statements,Node> StatementsRef;  typedef Ref<Statements,Node> StatementsRef;
316    
# Line 286  public: Line 321  public:
321      virtual Statements* branch(uint i) const = 0;      virtual Statements* branch(uint i) const = 0;
322  };  };
323    
324    class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr {
325        VMDynVar* dynVar;
326        String varName;
327    public:
328        DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v);
329        ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); }
330        bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); }
331        bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); }
332        bool isPolyphonic() const OVERRIDE { return false; }
333        void assign(Expression* expr) OVERRIDE { dynVar->assign(expr); }
334        int evalInt() OVERRIDE;
335        String evalStr() OVERRIDE;
336        String evalCastToStr() OVERRIDE;
337        void dump(int level = 0) OVERRIDE;
338    };
339    typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
340    
341  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
342      String functionName;      String functionName;
343      ArgsRef args;      ArgsRef args;
# Line 300  public: Line 352  public:
352      bool isConstExpr() const { return false; }      bool isConstExpr() const { return false; }
353      ExprType_t exprType() const;      ExprType_t exprType() const;
354      String evalCastToStr();      String evalCastToStr();
355        bool isPolyphonic() const { return args->isPolyphonic(); }
356  protected:  protected:
357      VMFnResult* execVMFn();      VMFnResult* execVMFn();
358  };  };
# Line 307  typedef Ref<FunctionCall,Node> FunctionC Line 360  typedef Ref<FunctionCall,Node> FunctionC
360    
361  class EventHandler : virtual public Statements, virtual public VMEventHandler {  class EventHandler : virtual public Statements, virtual public VMEventHandler {
362      StatementsRef statements;      StatementsRef statements;
363        bool usingPolyphonics;
364  public:  public:
365      void dump(int level = 0);      void dump(int level = 0);
366      StmtFlags_t exec();      StmtFlags_t exec();
367      EventHandler(StatementsRef statements) { this->statements = statements; }      EventHandler(StatementsRef statements);
368      Statement* statement(uint i) { return statements->statement(i); }      Statement* statement(uint i) { return statements->statement(i); }
369        bool isPolyphonic() const { return usingPolyphonics; }
370  };  };
371  typedef Ref<EventHandler,Node> EventHandlerRef;  typedef Ref<EventHandler,Node> EventHandlerRef;
372    
373  class OnNote : public EventHandler {  class OnNote : public EventHandler {
374  public:  public:
375      OnNote(StatementsRef statements) : EventHandler(statements) {}      OnNote(StatementsRef statements) : EventHandler(statements) {}
376        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_NOTE; }
377      String eventHandlerName() const { return "note"; }      String eventHandlerName() const { return "note"; }
378  };  };
379  typedef Ref<OnNote,Node> OnNoteRef;  typedef Ref<OnNote,Node> OnNoteRef;
# Line 325  typedef Ref<OnNote,Node> OnNoteRef; Line 381  typedef Ref<OnNote,Node> OnNoteRef;
381  class OnInit : public EventHandler {  class OnInit : public EventHandler {
382  public:  public:
383      OnInit(StatementsRef statements) : EventHandler(statements) {}      OnInit(StatementsRef statements) : EventHandler(statements) {}
384        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_INIT; }
385      String eventHandlerName() const { return "init"; }      String eventHandlerName() const { return "init"; }
386  };  };
387  typedef Ref<OnInit,Node> OnInitRef;  typedef Ref<OnInit,Node> OnInitRef;
# Line 332  typedef Ref<OnInit,Node> OnInitRef; Line 389  typedef Ref<OnInit,Node> OnInitRef;
389  class OnRelease : public EventHandler {  class OnRelease : public EventHandler {
390  public:  public:
391      OnRelease(StatementsRef statements) : EventHandler(statements) {}      OnRelease(StatementsRef statements) : EventHandler(statements) {}
392        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_RELEASE; }
393      String eventHandlerName() const { return "release"; }      String eventHandlerName() const { return "release"; }
394  };  };
395  typedef Ref<OnRelease,Node> OnReleaseRef;  typedef Ref<OnRelease,Node> OnReleaseRef;
# Line 339  typedef Ref<OnRelease,Node> OnReleaseRef Line 397  typedef Ref<OnRelease,Node> OnReleaseRef
397  class OnController : public EventHandler {  class OnController : public EventHandler {
398  public:  public:
399      OnController(StatementsRef statements) : EventHandler(statements) {}      OnController(StatementsRef statements) : EventHandler(statements) {}
400        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_CONTROLLER; }
401      String eventHandlerName() const { return "controller"; }      String eventHandlerName() const { return "controller"; }
402  };  };
403  typedef Ref<OnController,Node> OnControllerRef;  typedef Ref<OnController,Node> OnControllerRef;
# Line 354  public: Line 413  public:
413      EventHandler* eventHandlerByName(const String& name) const;      EventHandler* eventHandlerByName(const String& name) const;
414      EventHandler* eventHandler(uint index) const;      EventHandler* eventHandler(uint index) const;
415      inline uint size() const { return args.size(); }      inline uint size() const { return args.size(); }
416        bool isPolyphonic() const;
417  };  };
418  typedef Ref<EventHandlers,Node> EventHandlersRef;  typedef Ref<EventHandlers,Node> EventHandlersRef;
419    
# Line 365  public: Line 425  public:
425      Assignment(VariableRef variable, ExpressionRef value);      Assignment(VariableRef variable, ExpressionRef value);
426      void dump(int level = 0);      void dump(int level = 0);
427      StmtFlags_t exec();      StmtFlags_t exec();
428        bool isPolyphonic() const { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); }
429  };  };
430  typedef Ref<Assignment,Node> AssignmentRef;  typedef Ref<Assignment,Node> AssignmentRef;
431    
# Line 380  public: Line 441  public:
441      void dump(int level = 0);      void dump(int level = 0);
442      int evalBranch();      int evalBranch();
443      Statements* branch(uint i) const;      Statements* branch(uint i) const;
444        bool isPolyphonic() const;
445  };  };
446  typedef Ref<If,Node> IfRef;  typedef Ref<If,Node> IfRef;
447    
# Line 403  public: Line 465  public:
465      //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);      //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);
466      //void addBranch(CaseBranchRef branch);      //void addBranch(CaseBranchRef branch);
467      //void addBranches(CaseBranchesRef branches);      //void addBranches(CaseBranchesRef branches);
468        bool isPolyphonic() const;
469  };  };
470  typedef Ref<SelectCase,Node> SelectCaseRef;  typedef Ref<SelectCase,Node> SelectCaseRef;
471    
# Line 416  public: Line 479  public:
479      void dump(int level = 0);      void dump(int level = 0);
480      bool evalLoopStartCondition();      bool evalLoopStartCondition();
481      Statements* statements() const;      Statements* statements() const;
482        bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
483  };  };
484    
485  class Neg : public IntExpr {  class Neg : public IntExpr {
# Line 425  public: Line 489  public:
489      int evalInt() { return (expr) ? -expr->evalInt() : 0; }      int evalInt() { return (expr) ? -expr->evalInt() : 0; }
490      void dump(int level = 0);      void dump(int level = 0);
491      bool isConstExpr() const { return expr->isConstExpr(); }      bool isConstExpr() const { return expr->isConstExpr(); }
492        bool isPolyphonic() const { return expr->isPolyphonic(); }
493  };  };
494  typedef Ref<Neg,Node> NegRef;  typedef Ref<Neg,Node> NegRef;
495    
# Line 436  public: Line 501  public:
501      String evalStr();      String evalStr();
502      void dump(int level = 0);      void dump(int level = 0);
503      bool isConstExpr() const;      bool isConstExpr() const;
504        bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
505  };  };
506  typedef Ref<ConcatString,Node> ConcatStringRef;  typedef Ref<ConcatString,Node> ConcatStringRef;
507    
# Line 454  public: Line 520  public:
520      int evalInt();      int evalInt();
521      void dump(int level = 0);      void dump(int level = 0);
522      bool isConstExpr() const;      bool isConstExpr() const;
523        bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
524  private:  private:
525      IntExprRef lhs;      IntExprRef lhs;
526      IntExprRef rhs;      IntExprRef rhs;
# Line 469  public: Line 536  public:
536  };  };
537  typedef Ref<Or,Node> OrRef;  typedef Ref<Or,Node> OrRef;
538    
539    class BitwiseOr : virtual public BinaryOp, virtual public IntExpr {
540    public:
541        BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
542        int evalInt();
543        void dump(int level = 0);
544    };
545    typedef Ref<BitwiseOr,Node> BitwiseOrRef;
546    
547  class And : virtual public BinaryOp, virtual public IntExpr {  class And : virtual public BinaryOp, virtual public IntExpr {
548  public:  public:
549      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
# Line 477  public: Line 552  public:
552  };  };
553  typedef Ref<And,Node> AndRef;  typedef Ref<And,Node> AndRef;
554    
555    class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr {
556    public:
557        BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
558        int evalInt();
559        void dump(int level = 0);
560    };
561    typedef Ref<BitwiseAnd,Node> BitwiseAndRef;
562    
563  class Not : virtual public IntExpr {  class Not : virtual public IntExpr {
564      IntExprRef expr;      IntExprRef expr;
565  public:  public:
# Line 484  public: Line 567  public:
567      int evalInt() { return !expr->evalInt(); }      int evalInt() { return !expr->evalInt(); }
568      void dump(int level = 0);      void dump(int level = 0);
569      bool isConstExpr() const { return expr->isConstExpr(); }      bool isConstExpr() const { return expr->isConstExpr(); }
570        bool isPolyphonic() const { return expr->isPolyphonic(); }
571  };  };
572  typedef Ref<Not,Node> NotRef;  typedef Ref<Not,Node> NotRef;
573    
574    class BitwiseNot : virtual public IntExpr {
575        IntExprRef expr;
576    public:
577        BitwiseNot(IntExprRef expr) : expr(expr) {}
578        int evalInt() { return ~expr->evalInt(); }
579        void dump(int level = 0);
580        bool isConstExpr() const { return expr->isConstExpr(); }
581        bool isPolyphonic() const { return expr->isPolyphonic(); }
582    };
583    typedef Ref<BitwiseNot,Node> BitwiseNotRef;
584    
585  class ParserContext : public VMParserContext {  class ParserContext : public VMParserContext {
586  public:  public:
587      struct Error {      struct Error {
# Line 536  public: Line 631  public:
631      IntVariableRef globalIntVar(const String& name);      IntVariableRef globalIntVar(const String& name);
632      StringVariableRef globalStrVar(const String& name);      StringVariableRef globalStrVar(const String& name);
633      VariableRef variableByName(const String& name);      VariableRef variableByName(const String& name);
634      void addErr(int line, const char* txt);      void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
635      void addWrn(int line, const char* txt);      void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
636      void createScanner(std::istream* is);      void createScanner(std::istream* is);
637      void destroyScanner();      void destroyScanner();
638      bool setPreprocessorCondition(const char* name);      bool setPreprocessorCondition(const char* name);
# Line 548  public: Line 643  public:
643      std::vector<ParserIssue> warnings() const OVERRIDE;      std::vector<ParserIssue> warnings() const OVERRIDE;
644      VMEventHandler* eventHandler(uint index) OVERRIDE;      VMEventHandler* eventHandler(uint index) OVERRIDE;
645      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
646        void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
647        void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
648        void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
649        void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
650  };  };
651    
652  class ExecContext : public VMExecContext {  class ExecContext : public VMExecContext {

Legend:
Removed from v.2588  
changed lines
  Added in v.2942

  ViewVC Help
Powered by ViewVC