/[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 3221 by schoenebeck, Fri May 26 18:30:42 2017 UTC
# Line 1  Line 1 
1  /*                                                              -*- c++ -*-  /*                                                              -*- c++ -*-
2   *   *
3   * Copyright (c) 2014 Christian Schoenebeck and Andreas Persson   * Copyright (c) 2014 - 2017 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 63  public: Line 64  public:
64  };  };
65  typedef Ref<IntExpr,Node> IntExprRef;  typedef Ref<IntExpr,Node> IntExprRef;
66    
67    /*class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression {
68    public:
69        ExprType_t exprType() const { return INT_ARR_EXPR; }
70        String evalCastToStr();
71    };
72    typedef Ref<IntArrayExpr,Node> IntArrayExprRef;*/
73    
74  class StringExpr : virtual public VMStringExpr, virtual public Expression {  class StringExpr : virtual public VMStringExpr, virtual public Expression {
75  public:  public:
76      ExprType_t exprType() const { return STRING_EXPR; }      ExprType_t exprType() const { return STRING_EXPR; }
# Line 98  public: Line 106  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);
109      int argsCount() const { return args.size(); }      int argsCount() const { return (int) args.size(); }
110      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; }
111      bool isPolyphonic() const;      bool isPolyphonic() const;
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, int _memPos, bool _bConst)
123          : context(ctx), memPos(_memPos), bConst(_bConst) {}          : context(ctx), memPos(_memPos), bConst(_bConst) {}
# Line 148  class BuiltInIntVariable : public IntVar Line 158  class BuiltInIntVariable : public IntVar
158      VMIntRelPtr* ptr;      VMIntRelPtr* ptr;
159  public:  public:
160      BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);      BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);
161      void assign(Expression* expr);      bool isAssignable() const OVERRIDE { return !ptr->readonly; }
162      int evalInt();      void assign(Expression* expr) OVERRIDE;
163      void dump(int level = 0);      int evalInt() OVERRIDE;
164        void dump(int level = 0) OVERRIDE;
165  };  };
166  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
167    
# Line 320  public: Line 331  public:
331      virtual Statements* branch(uint i) const = 0;      virtual Statements* branch(uint i) const = 0;
332  };  };
333    
334    class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr {
335        VMDynVar* dynVar;
336        String varName;
337    public:
338        DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v);
339        ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); }
340        bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); }
341        bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); }
342        bool isPolyphonic() const OVERRIDE { return false; }
343        void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); }
344        VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); }
345        int evalInt() OVERRIDE;
346        String evalStr() OVERRIDE;
347        String evalCastToStr() OVERRIDE;
348        void dump(int level = 0) OVERRIDE;
349    };
350    typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
351    
352  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
353      String functionName;      String functionName;
354      ArgsRef args;      ArgsRef args;
# Line 327  class FunctionCall : virtual public Leaf Line 356  class FunctionCall : virtual public Leaf
356  public:  public:
357      FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :      FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :
358          functionName(function), args(args), fn(fn) { }          functionName(function), args(args), fn(fn) { }
359      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
360      StmtFlags_t exec();      StmtFlags_t exec() OVERRIDE;
361      int evalInt();      int evalInt() OVERRIDE;
362      String evalStr();      VMIntArrayExpr* asIntArray() const OVERRIDE;
363      bool isConstExpr() const { return false; }      String evalStr() OVERRIDE;
364      ExprType_t exprType() const;      bool isConstExpr() const OVERRIDE { return false; }
365      String evalCastToStr();      ExprType_t exprType() const OVERRIDE;
366      bool isPolyphonic() const { return args->isPolyphonic(); }      String evalCastToStr() OVERRIDE;
367        bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); }
368  protected:  protected:
369      VMFnResult* execVMFn();      VMFnResult* execVMFn();
370  };  };
# Line 355  typedef Ref<EventHandler,Node> EventHand Line 385  typedef Ref<EventHandler,Node> EventHand
385  class OnNote : public EventHandler {  class OnNote : public EventHandler {
386  public:  public:
387      OnNote(StatementsRef statements) : EventHandler(statements) {}      OnNote(StatementsRef statements) : EventHandler(statements) {}
388        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_NOTE; }
389      String eventHandlerName() const { return "note"; }      String eventHandlerName() const { return "note"; }
390  };  };
391  typedef Ref<OnNote,Node> OnNoteRef;  typedef Ref<OnNote,Node> OnNoteRef;
# Line 362  typedef Ref<OnNote,Node> OnNoteRef; Line 393  typedef Ref<OnNote,Node> OnNoteRef;
393  class OnInit : public EventHandler {  class OnInit : public EventHandler {
394  public:  public:
395      OnInit(StatementsRef statements) : EventHandler(statements) {}      OnInit(StatementsRef statements) : EventHandler(statements) {}
396        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_INIT; }
397      String eventHandlerName() const { return "init"; }      String eventHandlerName() const { return "init"; }
398  };  };
399  typedef Ref<OnInit,Node> OnInitRef;  typedef Ref<OnInit,Node> OnInitRef;
# Line 369  typedef Ref<OnInit,Node> OnInitRef; Line 401  typedef Ref<OnInit,Node> OnInitRef;
401  class OnRelease : public EventHandler {  class OnRelease : public EventHandler {
402  public:  public:
403      OnRelease(StatementsRef statements) : EventHandler(statements) {}      OnRelease(StatementsRef statements) : EventHandler(statements) {}
404        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_RELEASE; }
405      String eventHandlerName() const { return "release"; }      String eventHandlerName() const { return "release"; }
406  };  };
407  typedef Ref<OnRelease,Node> OnReleaseRef;  typedef Ref<OnRelease,Node> OnReleaseRef;
# Line 376  typedef Ref<OnRelease,Node> OnReleaseRef Line 409  typedef Ref<OnRelease,Node> OnReleaseRef
409  class OnController : public EventHandler {  class OnController : public EventHandler {
410  public:  public:
411      OnController(StatementsRef statements) : EventHandler(statements) {}      OnController(StatementsRef statements) : EventHandler(statements) {}
412        VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_CONTROLLER; }
413      String eventHandlerName() const { return "controller"; }      String eventHandlerName() const { return "controller"; }
414  };  };
415  typedef Ref<OnController,Node> OnControllerRef;  typedef Ref<OnController,Node> OnControllerRef;
# Line 390  public: Line 424  public:
424      int evalInt() { return 0; }      int evalInt() { return 0; }
425      EventHandler* eventHandlerByName(const String& name) const;      EventHandler* eventHandlerByName(const String& name) const;
426      EventHandler* eventHandler(uint index) const;      EventHandler* eventHandler(uint index) const;
427      inline uint size() const { return args.size(); }      inline uint size() const { return (int) args.size(); }
428      bool isPolyphonic() const;      bool isPolyphonic() const;
429  };  };
430  typedef Ref<EventHandlers,Node> EventHandlersRef;  typedef Ref<EventHandlers,Node> EventHandlersRef;
# Line 403  public: Line 437  public:
437      Assignment(VariableRef variable, ExpressionRef value);      Assignment(VariableRef variable, ExpressionRef value);
438      void dump(int level = 0);      void dump(int level = 0);
439      StmtFlags_t exec();      StmtFlags_t exec();
440      bool isPolyphonic() const { return variable->isPolyphonic() || value->isPolyphonic(); }      bool isPolyphonic() const { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); }
441  };  };
442  typedef Ref<Assignment,Node> AssignmentRef;  typedef Ref<Assignment,Node> AssignmentRef;
443    
# Line 514  public: Line 548  public:
548  };  };
549  typedef Ref<Or,Node> OrRef;  typedef Ref<Or,Node> OrRef;
550    
551    class BitwiseOr : virtual public BinaryOp, virtual public IntExpr {
552    public:
553        BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
554        int evalInt();
555        void dump(int level = 0);
556    };
557    typedef Ref<BitwiseOr,Node> BitwiseOrRef;
558    
559  class And : virtual public BinaryOp, virtual public IntExpr {  class And : virtual public BinaryOp, virtual public IntExpr {
560  public:  public:
561      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
# Line 522  public: Line 564  public:
564  };  };
565  typedef Ref<And,Node> AndRef;  typedef Ref<And,Node> AndRef;
566    
567    class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr {
568    public:
569        BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
570        int evalInt();
571        void dump(int level = 0);
572    };
573    typedef Ref<BitwiseAnd,Node> BitwiseAndRef;
574    
575  class Not : virtual public IntExpr {  class Not : virtual public IntExpr {
576      IntExprRef expr;      IntExprRef expr;
577  public:  public:
# Line 533  public: Line 583  public:
583  };  };
584  typedef Ref<Not,Node> NotRef;  typedef Ref<Not,Node> NotRef;
585    
586    class BitwiseNot : virtual public IntExpr {
587        IntExprRef expr;
588    public:
589        BitwiseNot(IntExprRef expr) : expr(expr) {}
590        int evalInt() { return ~expr->evalInt(); }
591        void dump(int level = 0);
592        bool isConstExpr() const { return expr->isConstExpr(); }
593        bool isPolyphonic() const { return expr->isPolyphonic(); }
594    };
595    typedef Ref<BitwiseNot,Node> BitwiseNotRef;
596    
597  class ParserContext : public VMParserContext {  class ParserContext : public VMParserContext {
598  public:  public:
599      struct Error {      struct Error {
# Line 551  public: Line 612  public:
612      std::set<String> userPreprocessorConditions;      std::set<String> userPreprocessorConditions;
613    
614      std::map<String,VariableRef> vartable;      std::map<String,VariableRef> vartable;
615        std::map<String,StatementsRef> userFnTable;
616      int globalIntVarCount;      int globalIntVarCount;
617      int globalStrVarCount;      int globalStrVarCount;
618      int polyphonicIntVarCount;      int polyphonicIntVarCount;
# Line 582  public: Line 644  public:
644      IntVariableRef globalIntVar(const String& name);      IntVariableRef globalIntVar(const String& name);
645      StringVariableRef globalStrVar(const String& name);      StringVariableRef globalStrVar(const String& name);
646      VariableRef variableByName(const String& name);      VariableRef variableByName(const String& name);
647      void addErr(int line, const char* txt);      StatementsRef userFunctionByName(const String& name);
648      void addWrn(int line, const char* txt);      void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
649        void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
650      void createScanner(std::istream* is);      void createScanner(std::istream* is);
651      void destroyScanner();      void destroyScanner();
652      bool setPreprocessorCondition(const char* name);      bool setPreprocessorCondition(const char* name);
# Line 597  public: Line 660  public:
660      void registerBuiltInConstIntVariables(const std::map<String,int>& vars);      void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
661      void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);      void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
662      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
663        void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
664  };  };
665    
666  class ExecContext : public VMExecContext {  class ExecContext : public VMExecContext {
# Line 616  public: Line 680  public:
680      ArrayList<StackFrame> stack;      ArrayList<StackFrame> stack;
681      int stackFrame;      int stackFrame;
682      int suspendMicroseconds;      int suspendMicroseconds;
683        size_t instructionsCount;
684    
685      ExecContext() :      ExecContext() :
686          status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {}          status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0),
687            instructionsCount(0) {}
688    
689      virtual ~ExecContext() {}      virtual ~ExecContext() {}
690    
# Line 646  public: Line 712  public:
712      int suspensionTimeMicroseconds() const OVERRIDE {      int suspensionTimeMicroseconds() const OVERRIDE {
713          return suspendMicroseconds;          return suspendMicroseconds;
714      }      }
715    
716        void resetPolyphonicData() OVERRIDE {
717            if (polyphonicIntMemory.empty()) return;
718            memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int));
719        }
720    
721        size_t instructionsPerformed() const OVERRIDE {
722            return instructionsCount;
723        }
724  };  };
725    
726  } // namespace LinuxSampler  } // namespace LinuxSampler

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

  ViewVC Help
Powered by ViewVC