/[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 2594 by schoenebeck, Thu Jun 5 00:16:25 2014 UTC revision 3056 by schoenebeck, Fri Dec 16 12:57:59 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 62  public: Line 63  public:
63  };  };
64  typedef Ref<IntExpr,Node> IntExprRef;  typedef Ref<IntExpr,Node> IntExprRef;
65    
66    /*class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression {
67    public:
68        ExprType_t exprType() const { return INT_ARR_EXPR; }
69        String evalCastToStr();
70    };
71    typedef Ref<IntArrayExpr,Node> IntArrayExprRef;*/
72    
73  class StringExpr : virtual public VMStringExpr, virtual public Expression {  class StringExpr : virtual public VMStringExpr, virtual public Expression {
74  public:  public:
75      ExprType_t exprType() const { return STRING_EXPR; }      ExprType_t exprType() const { return STRING_EXPR; }
# Line 77  public: Line 85  public:
85      int evalInt();      int evalInt();
86      void dump(int level = 0);      void dump(int level = 0);
87      bool isConstExpr() const { return true; }      bool isConstExpr() const { return true; }
88        bool isPolyphonic() const { return false; }
89  };  };
90  typedef Ref<IntLiteral,Node> IntLiteralRef;  typedef Ref<IntLiteral,Node> IntLiteralRef;
91    
# Line 87  public: Line 96  public:
96      bool isConstExpr() const { return true; }      bool isConstExpr() const { return true; }
97      void dump(int level = 0);      void dump(int level = 0);
98      String evalStr() { return value; }      String evalStr() { return value; }
99        bool isPolyphonic() const { return false; }
100  };  };
101  typedef Ref<StringLiteral,Node> StringLiteralRef;  typedef Ref<StringLiteral,Node> StringLiteralRef;
102    
# Line 95  public: Line 105  public:
105      std::vector<ExpressionRef> args;      std::vector<ExpressionRef> args;
106      void add(ExpressionRef arg) { args.push_back(arg); }      void add(ExpressionRef arg) { args.push_back(arg); }
107      void dump(int level = 0);      void dump(int level = 0);
108      int argsCount() const { return args.size(); }      int argsCount() const { return (int) args.size(); }
109      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; }
110        bool isPolyphonic() const;
111  };  };
112  typedef Ref<Args,Node> ArgsRef;  typedef Ref<Args,Node> ArgsRef;
113    
114  class Variable : virtual public Expression {  class Variable : virtual public VMVariable, virtual public Expression {
115  public:  public:
116      virtual bool isConstExpr() const { return bConst; }      bool isConstExpr() const OVERRIDE { return bConst; }
117        bool isAssignable() const OVERRIDE { return !bConst; }
118      virtual void assign(Expression* expr) = 0;      virtual void assign(Expression* expr) = 0;
119        void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); }
120  protected:  protected:
121      Variable(ParserContext* ctx, int _memPos, bool _bConst)      Variable(ParserContext* ctx, int _memPos, bool _bConst)
122          : context(ctx), memPos(_memPos), bConst(_bConst) {}          : context(ctx), memPos(_memPos), bConst(_bConst) {}
# Line 144  class BuiltInIntVariable : public IntVar Line 157  class BuiltInIntVariable : public IntVar
157      VMIntRelPtr* ptr;      VMIntRelPtr* ptr;
158  public:  public:
159      BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);      BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);
160      void assign(Expression* expr);      bool isAssignable() const OVERRIDE { return !ptr->readonly; }
161      int evalInt();      void assign(Expression* expr) OVERRIDE;
162      void dump(int level = 0);      int evalInt() OVERRIDE;
163        void dump(int level = 0) OVERRIDE;
164  };  };
165  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
166    
# Line 157  public: Line 171  public:
171  };  };
172  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
173    
174  class IntArrayVariable : public Variable {  class IntArrayVariable : public Variable, virtual public VMIntArrayExpr {
175      ArrayList<int> values;      ArrayList<int> values;
176  public:  public:
177      IntArrayVariable(ParserContext* ctx, int size);      IntArrayVariable(ParserContext* ctx, int size);
# Line 169  public: Line 183  public:
183      virtual int evalIntElement(uint i);      virtual int evalIntElement(uint i);
184      virtual void assignIntElement(uint i, int value);      virtual void assignIntElement(uint i, int value);
185      void dump(int level = 0);      void dump(int level = 0);
186        bool isPolyphonic() const { return false; }
187  protected:  protected:
188      IntArrayVariable(ParserContext* ctx, bool bConst);      IntArrayVariable(ParserContext* ctx, bool bConst);
189  };  };
# Line 203  public: Line 218  public:
218      void assign(Expression* expr);      void assign(Expression* expr);
219      String evalStr();      String evalStr();
220      void dump(int level = 0);      void dump(int level = 0);
221        bool isPolyphonic() const { return false; }
222  protected:  protected:
223      StringVariable(ParserContext* ctx, bool bConst);      StringVariable(ParserContext* ctx, bool bConst);
224  };  };
# Line 226  protected: Line 242  protected:
242  public:  public:
243      BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }      BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }
244      bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }      bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }
245        bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
246  };  };
247  typedef Ref<BinaryOp,Node> BinaryOpRef;  typedef Ref<BinaryOp,Node> BinaryOpRef;
248    
# Line 282  public: Line 299  public:
299      NoOperation() : Statement() {}      NoOperation() : Statement() {}
300      StmtType_t statementType() const { return STMT_LEAF; }      StmtType_t statementType() const { return STMT_LEAF; }
301      void dump(int level = 0) {}      void dump(int level = 0) {}
302        bool isPolyphonic() const { return false; }
303  };  };
304  typedef Ref<NoOperation,Node> NoOperationRef;  typedef Ref<NoOperation,Node> NoOperationRef;
305    
# Line 301  public: Line 319  public:
319      void dump(int level = 0);      void dump(int level = 0);
320      StmtType_t statementType() const { return STMT_LIST; }      StmtType_t statementType() const { return STMT_LIST; }
321      virtual Statement* statement(uint i);      virtual Statement* statement(uint i);
322        bool isPolyphonic() const;
323  };  };
324  typedef Ref<Statements,Node> StatementsRef;  typedef Ref<Statements,Node> StatementsRef;
325    
# Line 311  public: Line 330  public:
330      virtual Statements* branch(uint i) const = 0;      virtual Statements* branch(uint i) const = 0;
331  };  };
332    
333    class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr {
334        VMDynVar* dynVar;
335        String varName;
336    public:
337        DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v);
338        ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); }
339        bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); }
340        bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); }
341        bool isPolyphonic() const OVERRIDE { return false; }
342        void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); }
343        int evalInt() OVERRIDE;
344        String evalStr() OVERRIDE;
345        String evalCastToStr() OVERRIDE;
346        void dump(int level = 0) OVERRIDE;
347    };
348    typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
349    
350  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
351      String functionName;      String functionName;
352      ArgsRef args;      ArgsRef args;
# Line 321  public: Line 357  public:
357      void dump(int level = 0);      void dump(int level = 0);
358      StmtFlags_t exec();      StmtFlags_t exec();
359      int evalInt();      int evalInt();
360        VMIntArrayExpr* asIntArray() const OVERRIDE;
361      String evalStr();      String evalStr();
362      bool isConstExpr() const { return false; }      bool isConstExpr() const { return false; }
363      ExprType_t exprType() const;      ExprType_t exprType() const;
364      String evalCastToStr();      String evalCastToStr();
365        bool isPolyphonic() const { return args->isPolyphonic(); }
366  protected:  protected:
367      VMFnResult* execVMFn();      VMFnResult* execVMFn();
368  };  };
# Line 332  typedef Ref<FunctionCall,Node> FunctionC Line 370  typedef Ref<FunctionCall,Node> FunctionC
370    
371  class EventHandler : virtual public Statements, virtual public VMEventHandler {  class EventHandler : virtual public Statements, virtual public VMEventHandler {
372      StatementsRef statements;      StatementsRef statements;
373        bool usingPolyphonics;
374  public:  public:
375      void dump(int level = 0);      void dump(int level = 0);
376      StmtFlags_t exec();      StmtFlags_t exec();
377      EventHandler(StatementsRef statements) { this->statements = statements; }      EventHandler(StatementsRef statements);
378      Statement* statement(uint i) { return statements->statement(i); }      Statement* statement(uint i) { return statements->statement(i); }
379        bool isPolyphonic() const { return usingPolyphonics; }
380  };  };
381  typedef Ref<EventHandler,Node> EventHandlerRef;  typedef Ref<EventHandler,Node> EventHandlerRef;
382    
383  class OnNote : public EventHandler {  class OnNote : public EventHandler {
384  public:  public:
385      OnNote(StatementsRef statements) : EventHandler(statements) {}      OnNote(StatementsRef statements) : EventHandler(statements) {}
386        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_NOTE; }
387      String eventHandlerName() const { return "note"; }      String eventHandlerName() const { return "note"; }
388  };  };
389  typedef Ref<OnNote,Node> OnNoteRef;  typedef Ref<OnNote,Node> OnNoteRef;
# Line 350  typedef Ref<OnNote,Node> OnNoteRef; Line 391  typedef Ref<OnNote,Node> OnNoteRef;
391  class OnInit : public EventHandler {  class OnInit : public EventHandler {
392  public:  public:
393      OnInit(StatementsRef statements) : EventHandler(statements) {}      OnInit(StatementsRef statements) : EventHandler(statements) {}
394        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_INIT; }
395      String eventHandlerName() const { return "init"; }      String eventHandlerName() const { return "init"; }
396  };  };
397  typedef Ref<OnInit,Node> OnInitRef;  typedef Ref<OnInit,Node> OnInitRef;
# Line 357  typedef Ref<OnInit,Node> OnInitRef; Line 399  typedef Ref<OnInit,Node> OnInitRef;
399  class OnRelease : public EventHandler {  class OnRelease : public EventHandler {
400  public:  public:
401      OnRelease(StatementsRef statements) : EventHandler(statements) {}      OnRelease(StatementsRef statements) : EventHandler(statements) {}
402        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_RELEASE; }
403      String eventHandlerName() const { return "release"; }      String eventHandlerName() const { return "release"; }
404  };  };
405  typedef Ref<OnRelease,Node> OnReleaseRef;  typedef Ref<OnRelease,Node> OnReleaseRef;
# Line 364  typedef Ref<OnRelease,Node> OnReleaseRef Line 407  typedef Ref<OnRelease,Node> OnReleaseRef
407  class OnController : public EventHandler {  class OnController : public EventHandler {
408  public:  public:
409      OnController(StatementsRef statements) : EventHandler(statements) {}      OnController(StatementsRef statements) : EventHandler(statements) {}
410        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_CONTROLLER; }
411      String eventHandlerName() const { return "controller"; }      String eventHandlerName() const { return "controller"; }
412  };  };
413  typedef Ref<OnController,Node> OnControllerRef;  typedef Ref<OnController,Node> OnControllerRef;
# Line 378  public: Line 422  public:
422      int evalInt() { return 0; }      int evalInt() { return 0; }
423      EventHandler* eventHandlerByName(const String& name) const;      EventHandler* eventHandlerByName(const String& name) const;
424      EventHandler* eventHandler(uint index) const;      EventHandler* eventHandler(uint index) const;
425      inline uint size() const { return args.size(); }      inline uint size() const { return (int) args.size(); }
426        bool isPolyphonic() const;
427  };  };
428  typedef Ref<EventHandlers,Node> EventHandlersRef;  typedef Ref<EventHandlers,Node> EventHandlersRef;
429    
# Line 390  public: Line 435  public:
435      Assignment(VariableRef variable, ExpressionRef value);      Assignment(VariableRef variable, ExpressionRef value);
436      void dump(int level = 0);      void dump(int level = 0);
437      StmtFlags_t exec();      StmtFlags_t exec();
438        bool isPolyphonic() const { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); }
439  };  };
440  typedef Ref<Assignment,Node> AssignmentRef;  typedef Ref<Assignment,Node> AssignmentRef;
441    
# Line 405  public: Line 451  public:
451      void dump(int level = 0);      void dump(int level = 0);
452      int evalBranch();      int evalBranch();
453      Statements* branch(uint i) const;      Statements* branch(uint i) const;
454        bool isPolyphonic() const;
455  };  };
456  typedef Ref<If,Node> IfRef;  typedef Ref<If,Node> IfRef;
457    
# Line 428  public: Line 475  public:
475      //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);      //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);
476      //void addBranch(CaseBranchRef branch);      //void addBranch(CaseBranchRef branch);
477      //void addBranches(CaseBranchesRef branches);      //void addBranches(CaseBranchesRef branches);
478        bool isPolyphonic() const;
479  };  };
480  typedef Ref<SelectCase,Node> SelectCaseRef;  typedef Ref<SelectCase,Node> SelectCaseRef;
481    
# Line 441  public: Line 489  public:
489      void dump(int level = 0);      void dump(int level = 0);
490      bool evalLoopStartCondition();      bool evalLoopStartCondition();
491      Statements* statements() const;      Statements* statements() const;
492        bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
493  };  };
494    
495  class Neg : public IntExpr {  class Neg : public IntExpr {
# Line 450  public: Line 499  public:
499      int evalInt() { return (expr) ? -expr->evalInt() : 0; }      int evalInt() { return (expr) ? -expr->evalInt() : 0; }
500      void dump(int level = 0);      void dump(int level = 0);
501      bool isConstExpr() const { return expr->isConstExpr(); }      bool isConstExpr() const { return expr->isConstExpr(); }
502        bool isPolyphonic() const { return expr->isPolyphonic(); }
503  };  };
504  typedef Ref<Neg,Node> NegRef;  typedef Ref<Neg,Node> NegRef;
505    
# Line 461  public: Line 511  public:
511      String evalStr();      String evalStr();
512      void dump(int level = 0);      void dump(int level = 0);
513      bool isConstExpr() const;      bool isConstExpr() const;
514        bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
515  };  };
516  typedef Ref<ConcatString,Node> ConcatStringRef;  typedef Ref<ConcatString,Node> ConcatStringRef;
517    
# Line 479  public: Line 530  public:
530      int evalInt();      int evalInt();
531      void dump(int level = 0);      void dump(int level = 0);
532      bool isConstExpr() const;      bool isConstExpr() const;
533        bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
534  private:  private:
535      IntExprRef lhs;      IntExprRef lhs;
536      IntExprRef rhs;      IntExprRef rhs;
# Line 494  public: Line 546  public:
546  };  };
547  typedef Ref<Or,Node> OrRef;  typedef Ref<Or,Node> OrRef;
548    
549    class BitwiseOr : virtual public BinaryOp, virtual public IntExpr {
550    public:
551        BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
552        int evalInt();
553        void dump(int level = 0);
554    };
555    typedef Ref<BitwiseOr,Node> BitwiseOrRef;
556    
557  class And : virtual public BinaryOp, virtual public IntExpr {  class And : virtual public BinaryOp, virtual public IntExpr {
558  public:  public:
559      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
# Line 502  public: Line 562  public:
562  };  };
563  typedef Ref<And,Node> AndRef;  typedef Ref<And,Node> AndRef;
564    
565    class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr {
566    public:
567        BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
568        int evalInt();
569        void dump(int level = 0);
570    };
571    typedef Ref<BitwiseAnd,Node> BitwiseAndRef;
572    
573  class Not : virtual public IntExpr {  class Not : virtual public IntExpr {
574      IntExprRef expr;      IntExprRef expr;
575  public:  public:
# Line 509  public: Line 577  public:
577      int evalInt() { return !expr->evalInt(); }      int evalInt() { return !expr->evalInt(); }
578      void dump(int level = 0);      void dump(int level = 0);
579      bool isConstExpr() const { return expr->isConstExpr(); }      bool isConstExpr() const { return expr->isConstExpr(); }
580        bool isPolyphonic() const { return expr->isPolyphonic(); }
581  };  };
582  typedef Ref<Not,Node> NotRef;  typedef Ref<Not,Node> NotRef;
583    
584    class BitwiseNot : virtual public IntExpr {
585        IntExprRef expr;
586    public:
587        BitwiseNot(IntExprRef expr) : expr(expr) {}
588        int evalInt() { return ~expr->evalInt(); }
589        void dump(int level = 0);
590        bool isConstExpr() const { return expr->isConstExpr(); }
591        bool isPolyphonic() const { return expr->isPolyphonic(); }
592    };
593    typedef Ref<BitwiseNot,Node> BitwiseNotRef;
594    
595  class ParserContext : public VMParserContext {  class ParserContext : public VMParserContext {
596  public:  public:
597      struct Error {      struct Error {
# Line 530  public: Line 610  public:
610      std::set<String> userPreprocessorConditions;      std::set<String> userPreprocessorConditions;
611    
612      std::map<String,VariableRef> vartable;      std::map<String,VariableRef> vartable;
613        std::map<String,StatementsRef> userFnTable;
614      int globalIntVarCount;      int globalIntVarCount;
615      int globalStrVarCount;      int globalStrVarCount;
616      int polyphonicIntVarCount;      int polyphonicIntVarCount;
# Line 561  public: Line 642  public:
642      IntVariableRef globalIntVar(const String& name);      IntVariableRef globalIntVar(const String& name);
643      StringVariableRef globalStrVar(const String& name);      StringVariableRef globalStrVar(const String& name);
644      VariableRef variableByName(const String& name);      VariableRef variableByName(const String& name);
645      void addErr(int line, const char* txt);      StatementsRef userFunctionByName(const String& name);
646      void addWrn(int line, const char* txt);      void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
647        void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
648      void createScanner(std::istream* is);      void createScanner(std::istream* is);
649      void destroyScanner();      void destroyScanner();
650      bool setPreprocessorCondition(const char* name);      bool setPreprocessorCondition(const char* name);
# Line 576  public: Line 658  public:
658      void registerBuiltInConstIntVariables(const std::map<String,int>& vars);      void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
659      void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);      void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
660      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
661        void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
662  };  };
663    
664  class ExecContext : public VMExecContext {  class ExecContext : public VMExecContext {

Legend:
Removed from v.2594  
changed lines
  Added in v.3056

  ViewVC Help
Powered by ViewVC