/[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 3054 by schoenebeck, Thu Dec 15 12:47:45 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 95  public: Line 98  public:
98      std::vector<ExpressionRef> args;      std::vector<ExpressionRef> args;
99      void add(ExpressionRef arg) { args.push_back(arg); }      void add(ExpressionRef arg) { args.push_back(arg); }
100      void dump(int level = 0);      void dump(int level = 0);
101      int argsCount() const { return args.size(); }      int argsCount() const { return (int) 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 VMVariable, virtual public Expression {
108  public:  public:
109      virtual bool isConstExpr() const { return bConst; }      bool isConstExpr() const OVERRIDE { return bConst; }
110        bool isAssignable() const OVERRIDE { return !bConst; }
111      virtual void assign(Expression* expr) = 0;      virtual void assign(Expression* expr) = 0;
112        void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); }
113  protected:  protected:
114      Variable(ParserContext* ctx, int _memPos, bool _bConst)      Variable(ParserContext* ctx, int _memPos, bool _bConst)
115          : context(ctx), memPos(_memPos), bConst(_bConst) {}          : context(ctx), memPos(_memPos), bConst(_bConst) {}
# Line 139  public: Line 145  public:
145  };  };
146  typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;  typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;
147    
148    class BuiltInIntVariable : public IntVariable {
149        String name;
150        VMIntRelPtr* ptr;
151    public:
152        BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);
153        bool isAssignable() const OVERRIDE { return !ptr->readonly; }
154        void assign(Expression* expr) OVERRIDE;
155        int evalInt() OVERRIDE;
156        void dump(int level = 0) OVERRIDE;
157    };
158    typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
159    
160  class PolyphonicIntVariable : public IntVariable {  class PolyphonicIntVariable : public IntVariable {
161  public:  public:
162      PolyphonicIntVariable(ParserContext* ctx);      PolyphonicIntVariable(ParserContext* ctx);
# Line 146  public: Line 164  public:
164  };  };
165  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
166    
167  class IntArrayVariable : public Variable {  class IntArrayVariable : public Variable, virtual public VMIntArrayExpr {
168      ArrayList<int> values;      ArrayList<int> values;
169  public:  public:
170      IntArrayVariable(ParserContext* ctx, int size);      IntArrayVariable(ParserContext* ctx, int size);
171      IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);      IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);
172      void assign(Expression* expr) {} // ignore scalar assignment      void assign(Expression* expr) {} // ignore scalar assignment
173      String evalCastToStr() { return ""; } // ignore cast to string      String evalCastToStr() { return ""; } // ignore scalar cast to string
174      ExprType_t exprType() const { return INT_ARR_EXPR; }      ExprType_t exprType() const { return INT_ARR_EXPR; }
175      const int arraySize() const { return values.size(); }      virtual int arraySize() const { return values.size(); }
176        virtual int evalIntElement(uint i);
177        virtual void assignIntElement(uint i, int value);
178        void dump(int level = 0);
179        bool isPolyphonic() const { return false; }
180    protected:
181        IntArrayVariable(ParserContext* ctx, bool bConst);
182    };
183    typedef Ref<IntArrayVariable,Node> IntArrayVariableRef;
184    
185    class BuiltInIntArrayVariable : public IntArrayVariable {
186        String name;
187        VMInt8Array* array;
188    public:
189        BuiltInIntArrayVariable(const String& name, VMInt8Array* array);
190        int arraySize() const { return array->size; }
191      int evalIntElement(uint i);      int evalIntElement(uint i);
192      void assignIntElement(uint i, int value);      void assignIntElement(uint i, int value);
193      void dump(int level = 0);      void dump(int level = 0);
194  };  };
195  typedef Ref<IntArrayVariable,Node> IntArrayVariableRef;  typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;
196    
197  class IntArrayElement : public IntVariable {  class IntArrayElement : public IntVariable {
198      IntArrayVariableRef array;      IntArrayVariableRef array;
# Line 178  public: Line 211  public:
211      void assign(Expression* expr);      void assign(Expression* expr);
212      String evalStr();      String evalStr();
213      void dump(int level = 0);      void dump(int level = 0);
214        bool isPolyphonic() const { return false; }
215  protected:  protected:
216      StringVariable(ParserContext* ctx, bool bConst);      StringVariable(ParserContext* ctx, bool bConst);
217  };  };
# Line 201  protected: Line 235  protected:
235  public:  public:
236      BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }      BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }
237      bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }      bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }
238        bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
239  };  };
240  typedef Ref<BinaryOp,Node> BinaryOpRef;  typedef Ref<BinaryOp,Node> BinaryOpRef;
241    
# Line 257  public: Line 292  public:
292      NoOperation() : Statement() {}      NoOperation() : Statement() {}
293      StmtType_t statementType() const { return STMT_LEAF; }      StmtType_t statementType() const { return STMT_LEAF; }
294      void dump(int level = 0) {}      void dump(int level = 0) {}
295        bool isPolyphonic() const { return false; }
296  };  };
297  typedef Ref<NoOperation,Node> NoOperationRef;  typedef Ref<NoOperation,Node> NoOperationRef;
298    
# Line 276  public: Line 312  public:
312      void dump(int level = 0);      void dump(int level = 0);
313      StmtType_t statementType() const { return STMT_LIST; }      StmtType_t statementType() const { return STMT_LIST; }
314      virtual Statement* statement(uint i);      virtual Statement* statement(uint i);
315        bool isPolyphonic() const;
316  };  };
317  typedef Ref<Statements,Node> StatementsRef;  typedef Ref<Statements,Node> StatementsRef;
318    
# Line 286  public: Line 323  public:
323      virtual Statements* branch(uint i) const = 0;      virtual Statements* branch(uint i) const = 0;
324  };  };
325    
326    class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr {
327        VMDynVar* dynVar;
328        String varName;
329    public:
330        DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v);
331        ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); }
332        bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); }
333        bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); }
334        bool isPolyphonic() const OVERRIDE { return false; }
335        void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); }
336        int evalInt() OVERRIDE;
337        String evalStr() OVERRIDE;
338        String evalCastToStr() OVERRIDE;
339        void dump(int level = 0) OVERRIDE;
340    };
341    typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
342    
343  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
344      String functionName;      String functionName;
345      ArgsRef args;      ArgsRef args;
# Line 300  public: Line 354  public:
354      bool isConstExpr() const { return false; }      bool isConstExpr() const { return false; }
355      ExprType_t exprType() const;      ExprType_t exprType() const;
356      String evalCastToStr();      String evalCastToStr();
357        bool isPolyphonic() const { return args->isPolyphonic(); }
358  protected:  protected:
359      VMFnResult* execVMFn();      VMFnResult* execVMFn();
360  };  };
# Line 307  typedef Ref<FunctionCall,Node> FunctionC Line 362  typedef Ref<FunctionCall,Node> FunctionC
362    
363  class EventHandler : virtual public Statements, virtual public VMEventHandler {  class EventHandler : virtual public Statements, virtual public VMEventHandler {
364      StatementsRef statements;      StatementsRef statements;
365        bool usingPolyphonics;
366  public:  public:
367      void dump(int level = 0);      void dump(int level = 0);
368      StmtFlags_t exec();      StmtFlags_t exec();
369      EventHandler(StatementsRef statements) { this->statements = statements; }      EventHandler(StatementsRef statements);
370      Statement* statement(uint i) { return statements->statement(i); }      Statement* statement(uint i) { return statements->statement(i); }
371        bool isPolyphonic() const { return usingPolyphonics; }
372  };  };
373  typedef Ref<EventHandler,Node> EventHandlerRef;  typedef Ref<EventHandler,Node> EventHandlerRef;
374    
375  class OnNote : public EventHandler {  class OnNote : public EventHandler {
376  public:  public:
377      OnNote(StatementsRef statements) : EventHandler(statements) {}      OnNote(StatementsRef statements) : EventHandler(statements) {}
378        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_NOTE; }
379      String eventHandlerName() const { return "note"; }      String eventHandlerName() const { return "note"; }
380  };  };
381  typedef Ref<OnNote,Node> OnNoteRef;  typedef Ref<OnNote,Node> OnNoteRef;
# Line 325  typedef Ref<OnNote,Node> OnNoteRef; Line 383  typedef Ref<OnNote,Node> OnNoteRef;
383  class OnInit : public EventHandler {  class OnInit : public EventHandler {
384  public:  public:
385      OnInit(StatementsRef statements) : EventHandler(statements) {}      OnInit(StatementsRef statements) : EventHandler(statements) {}
386        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_INIT; }
387      String eventHandlerName() const { return "init"; }      String eventHandlerName() const { return "init"; }
388  };  };
389  typedef Ref<OnInit,Node> OnInitRef;  typedef Ref<OnInit,Node> OnInitRef;
# Line 332  typedef Ref<OnInit,Node> OnInitRef; Line 391  typedef Ref<OnInit,Node> OnInitRef;
391  class OnRelease : public EventHandler {  class OnRelease : public EventHandler {
392  public:  public:
393      OnRelease(StatementsRef statements) : EventHandler(statements) {}      OnRelease(StatementsRef statements) : EventHandler(statements) {}
394        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_RELEASE; }
395      String eventHandlerName() const { return "release"; }      String eventHandlerName() const { return "release"; }
396  };  };
397  typedef Ref<OnRelease,Node> OnReleaseRef;  typedef Ref<OnRelease,Node> OnReleaseRef;
# Line 339  typedef Ref<OnRelease,Node> OnReleaseRef Line 399  typedef Ref<OnRelease,Node> OnReleaseRef
399  class OnController : public EventHandler {  class OnController : public EventHandler {
400  public:  public:
401      OnController(StatementsRef statements) : EventHandler(statements) {}      OnController(StatementsRef statements) : EventHandler(statements) {}
402        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_CONTROLLER; }
403      String eventHandlerName() const { return "controller"; }      String eventHandlerName() const { return "controller"; }
404  };  };
405  typedef Ref<OnController,Node> OnControllerRef;  typedef Ref<OnController,Node> OnControllerRef;
# Line 353  public: Line 414  public:
414      int evalInt() { return 0; }      int evalInt() { return 0; }
415      EventHandler* eventHandlerByName(const String& name) const;      EventHandler* eventHandlerByName(const String& name) const;
416      EventHandler* eventHandler(uint index) const;      EventHandler* eventHandler(uint index) const;
417      inline uint size() const { return args.size(); }      inline uint size() const { return (int) args.size(); }
418        bool isPolyphonic() const;
419  };  };
420  typedef Ref<EventHandlers,Node> EventHandlersRef;  typedef Ref<EventHandlers,Node> EventHandlersRef;
421    
# Line 365  public: Line 427  public:
427      Assignment(VariableRef variable, ExpressionRef value);      Assignment(VariableRef variable, ExpressionRef value);
428      void dump(int level = 0);      void dump(int level = 0);
429      StmtFlags_t exec();      StmtFlags_t exec();
430        bool isPolyphonic() const { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); }
431  };  };
432  typedef Ref<Assignment,Node> AssignmentRef;  typedef Ref<Assignment,Node> AssignmentRef;
433    
# Line 380  public: Line 443  public:
443      void dump(int level = 0);      void dump(int level = 0);
444      int evalBranch();      int evalBranch();
445      Statements* branch(uint i) const;      Statements* branch(uint i) const;
446        bool isPolyphonic() const;
447  };  };
448  typedef Ref<If,Node> IfRef;  typedef Ref<If,Node> IfRef;
449    
# Line 403  public: Line 467  public:
467      //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);      //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);
468      //void addBranch(CaseBranchRef branch);      //void addBranch(CaseBranchRef branch);
469      //void addBranches(CaseBranchesRef branches);      //void addBranches(CaseBranchesRef branches);
470        bool isPolyphonic() const;
471  };  };
472  typedef Ref<SelectCase,Node> SelectCaseRef;  typedef Ref<SelectCase,Node> SelectCaseRef;
473    
# Line 416  public: Line 481  public:
481      void dump(int level = 0);      void dump(int level = 0);
482      bool evalLoopStartCondition();      bool evalLoopStartCondition();
483      Statements* statements() const;      Statements* statements() const;
484        bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
485  };  };
486    
487  class Neg : public IntExpr {  class Neg : public IntExpr {
# Line 425  public: Line 491  public:
491      int evalInt() { return (expr) ? -expr->evalInt() : 0; }      int evalInt() { return (expr) ? -expr->evalInt() : 0; }
492      void dump(int level = 0);      void dump(int level = 0);
493      bool isConstExpr() const { return expr->isConstExpr(); }      bool isConstExpr() const { return expr->isConstExpr(); }
494        bool isPolyphonic() const { return expr->isPolyphonic(); }
495  };  };
496  typedef Ref<Neg,Node> NegRef;  typedef Ref<Neg,Node> NegRef;
497    
# Line 436  public: Line 503  public:
503      String evalStr();      String evalStr();
504      void dump(int level = 0);      void dump(int level = 0);
505      bool isConstExpr() const;      bool isConstExpr() const;
506        bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
507  };  };
508  typedef Ref<ConcatString,Node> ConcatStringRef;  typedef Ref<ConcatString,Node> ConcatStringRef;
509    
# Line 454  public: Line 522  public:
522      int evalInt();      int evalInt();
523      void dump(int level = 0);      void dump(int level = 0);
524      bool isConstExpr() const;      bool isConstExpr() const;
525        bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
526  private:  private:
527      IntExprRef lhs;      IntExprRef lhs;
528      IntExprRef rhs;      IntExprRef rhs;
# Line 469  public: Line 538  public:
538  };  };
539  typedef Ref<Or,Node> OrRef;  typedef Ref<Or,Node> OrRef;
540    
541    class BitwiseOr : virtual public BinaryOp, virtual public IntExpr {
542    public:
543        BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
544        int evalInt();
545        void dump(int level = 0);
546    };
547    typedef Ref<BitwiseOr,Node> BitwiseOrRef;
548    
549  class And : virtual public BinaryOp, virtual public IntExpr {  class And : virtual public BinaryOp, virtual public IntExpr {
550  public:  public:
551      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
# Line 477  public: Line 554  public:
554  };  };
555  typedef Ref<And,Node> AndRef;  typedef Ref<And,Node> AndRef;
556    
557    class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr {
558    public:
559        BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
560        int evalInt();
561        void dump(int level = 0);
562    };
563    typedef Ref<BitwiseAnd,Node> BitwiseAndRef;
564    
565  class Not : virtual public IntExpr {  class Not : virtual public IntExpr {
566      IntExprRef expr;      IntExprRef expr;
567  public:  public:
# Line 484  public: Line 569  public:
569      int evalInt() { return !expr->evalInt(); }      int evalInt() { return !expr->evalInt(); }
570      void dump(int level = 0);      void dump(int level = 0);
571      bool isConstExpr() const { return expr->isConstExpr(); }      bool isConstExpr() const { return expr->isConstExpr(); }
572        bool isPolyphonic() const { return expr->isPolyphonic(); }
573  };  };
574  typedef Ref<Not,Node> NotRef;  typedef Ref<Not,Node> NotRef;
575    
576    class BitwiseNot : virtual public IntExpr {
577        IntExprRef expr;
578    public:
579        BitwiseNot(IntExprRef expr) : expr(expr) {}
580        int evalInt() { return ~expr->evalInt(); }
581        void dump(int level = 0);
582        bool isConstExpr() const { return expr->isConstExpr(); }
583        bool isPolyphonic() const { return expr->isPolyphonic(); }
584    };
585    typedef Ref<BitwiseNot,Node> BitwiseNotRef;
586    
587  class ParserContext : public VMParserContext {  class ParserContext : public VMParserContext {
588  public:  public:
589      struct Error {      struct Error {
# Line 505  public: Line 602  public:
602      std::set<String> userPreprocessorConditions;      std::set<String> userPreprocessorConditions;
603    
604      std::map<String,VariableRef> vartable;      std::map<String,VariableRef> vartable;
605        std::map<String,StatementsRef> userFnTable;
606      int globalIntVarCount;      int globalIntVarCount;
607      int globalStrVarCount;      int globalStrVarCount;
608      int polyphonicIntVarCount;      int polyphonicIntVarCount;
# Line 536  public: Line 634  public:
634      IntVariableRef globalIntVar(const String& name);      IntVariableRef globalIntVar(const String& name);
635      StringVariableRef globalStrVar(const String& name);      StringVariableRef globalStrVar(const String& name);
636      VariableRef variableByName(const String& name);      VariableRef variableByName(const String& name);
637      void addErr(int line, const char* txt);      StatementsRef userFunctionByName(const String& name);
638      void addWrn(int line, const char* txt);      void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
639        void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
640      void createScanner(std::istream* is);      void createScanner(std::istream* is);
641      void destroyScanner();      void destroyScanner();
642      bool setPreprocessorCondition(const char* name);      bool setPreprocessorCondition(const char* name);
# Line 548  public: Line 647  public:
647      std::vector<ParserIssue> warnings() const OVERRIDE;      std::vector<ParserIssue> warnings() const OVERRIDE;
648      VMEventHandler* eventHandler(uint index) OVERRIDE;      VMEventHandler* eventHandler(uint index) OVERRIDE;
649      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
650        void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
651        void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
652        void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
653        void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
654  };  };
655    
656  class ExecContext : public VMExecContext {  class ExecContext : public VMExecContext {

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

  ViewVC Help
Powered by ViewVC