/[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 2879 by schoenebeck, Tue Apr 19 14:07:53 2016 UTC revision 3293 by schoenebeck, Tue Jun 27 22:19:19 2017 UTC
# Line 1  Line 1 
1  /*                                                              -*- c++ -*-  /*                                                              -*- c++ -*-
2   *   *
3   * Copyright (c) 2014 - 2016 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 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  };  };
41    
42  class Node {  class Node {
# Line 63  public: Line 65  public:
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 { return INT_ARR_EXPR; }
71        String evalCastToStr();
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 { return STRING_EXPR; }
# Line 98  public: Line 107  public:
107      std::vector<ExpressionRef> args;      std::vector<ExpressionRef> args;
108      void add(ExpressionRef arg) { args.push_back(arg); }      void add(ExpressionRef arg) { args.push_back(arg); }
109      void dump(int level = 0);      void dump(int level = 0);
110      int argsCount() const { return args.size(); }      int argsCount() const { return (int) args.size(); }
111      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; }
112      bool isPolyphonic() const;      bool isPolyphonic() const;
113  };  };
114  typedef Ref<Args,Node> ArgsRef;  typedef Ref<Args,Node> ArgsRef;
115    
116  class Variable : virtual public Expression {  class Variable : virtual public VMVariable, virtual public Expression {
117  public:  public:
118      virtual bool isConstExpr() const { return bConst; }      bool isConstExpr() const OVERRIDE { return bConst; }
119        bool isAssignable() const OVERRIDE { return !bConst; }
120      virtual void assign(Expression* expr) = 0;      virtual void assign(Expression* expr) = 0;
121        void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); }
122  protected:  protected:
123      Variable(ParserContext* ctx, int _memPos, bool _bConst)      Variable(ParserContext* ctx, int _memPos, bool _bConst)
124          : context(ctx), memPos(_memPos), bConst(_bConst) {}          : context(ctx), memPos(_memPos), bConst(_bConst) {}
# Line 148  class BuiltInIntVariable : public IntVar Line 159  class BuiltInIntVariable : public IntVar
159      VMIntRelPtr* ptr;      VMIntRelPtr* ptr;
160  public:  public:
161      BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);      BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);
162      void assign(Expression* expr);      bool isAssignable() const OVERRIDE { return !ptr->readonly; }
163      int evalInt();      void assign(Expression* expr) OVERRIDE;
164      void dump(int level = 0);      int evalInt() OVERRIDE;
165        void dump(int level = 0) OVERRIDE;
166  };  };
167  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
168    
# Line 161  public: Line 173  public:
173  };  };
174  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
175    
176  class IntArrayVariable : public Variable, virtual public VMIntArrayExpr {  class IntArrayVariable : public Variable, virtual public IntArrayExpr {
177      ArrayList<int> values;      ArrayList<int> values;
178  public:  public:
179      IntArrayVariable(ParserContext* ctx, int size);      IntArrayVariable(ParserContext* ctx, int size);
180      IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);      IntArrayVariable(ParserContext* ctx, int size, ArgsRef values, bool _bConst = false);
181      void assign(Expression* expr) {} // ignore scalar assignment      void assign(Expression* expr) {} // ignore scalar assignment
182      String evalCastToStr() { return ""; } // ignore scalar cast to string      String evalCastToStr() { return ""; } // ignore scalar cast to string
183      ExprType_t exprType() const { return INT_ARR_EXPR; }      ExprType_t exprType() const { return INT_ARR_EXPR; }
# Line 186  public: Line 198  public:
198      BuiltInIntArrayVariable(const String& name, VMInt8Array* array);      BuiltInIntArrayVariable(const String& name, VMInt8Array* array);
199      int arraySize() const { return array->size; }      int arraySize() const { return array->size; }
200      int evalIntElement(uint i);      int evalIntElement(uint i);
201        bool isAssignable() const OVERRIDE { return !array->readonly; }
202      void assignIntElement(uint i, int value);      void assignIntElement(uint i, int value);
203      void dump(int level = 0);      void dump(int level = 0);
204  };  };
205  typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;  typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;
206    
207  class IntArrayElement : public IntVariable {  class IntArrayElement : public IntVariable {
208      IntArrayVariableRef array;      IntArrayExprRef array;
209      IntExprRef index;      IntExprRef index;
210  public:  public:
211      IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex);      IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex);
212      void assign(Expression* expr);      void assign(Expression* expr);
213      int evalInt();      int evalInt();
214      void dump(int level = 0);      void dump(int level = 0);
# Line 320  public: Line 333  public:
333      virtual Statements* branch(uint i) const = 0;      virtual Statements* branch(uint i) const = 0;
334  };  };
335    
336    class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr, virtual public IntArrayExpr {
337        VMDynVar* dynVar;
338        String varName;
339    public:
340        DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v);
341        ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); }
342        bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); }
343        bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); }
344        bool isPolyphonic() const OVERRIDE { return false; }
345        void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); }
346        VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); }
347        int evalInt() OVERRIDE;
348        String evalStr() OVERRIDE;
349        String evalCastToStr() OVERRIDE;
350        int arraySize() const OVERRIDE { return dynVar->asIntArray()->arraySize(); }
351        int evalIntElement(uint i) OVERRIDE { return dynVar->asIntArray()->evalIntElement(i); }
352        void assignIntElement(uint i, int value) { return dynVar->asIntArray()->assignIntElement(i, value); }
353        void dump(int level = 0) OVERRIDE;
354    };
355    typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
356    
357  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
358      String functionName;      String functionName;
359      ArgsRef args;      ArgsRef args;
# Line 327  class FunctionCall : virtual public Leaf Line 361  class FunctionCall : virtual public Leaf
361  public:  public:
362      FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :      FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :
363          functionName(function), args(args), fn(fn) { }          functionName(function), args(args), fn(fn) { }
364      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
365      StmtFlags_t exec();      StmtFlags_t exec() OVERRIDE;
366      int evalInt();      int evalInt() OVERRIDE;
367      String evalStr();      VMIntArrayExpr* asIntArray() const OVERRIDE;
368      bool isConstExpr() const { return false; }      String evalStr() OVERRIDE;
369      ExprType_t exprType() const;      bool isConstExpr() const OVERRIDE { return false; }
370      String evalCastToStr();      ExprType_t exprType() const OVERRIDE;
371      bool isPolyphonic() const { return args->isPolyphonic(); }      String evalCastToStr() OVERRIDE;
372        bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); }
373  protected:  protected:
374      VMFnResult* execVMFn();      VMFnResult* execVMFn();
375  };  };
# Line 394  public: Line 429  public:
429      int evalInt() { return 0; }      int evalInt() { return 0; }
430      EventHandler* eventHandlerByName(const String& name) const;      EventHandler* eventHandlerByName(const String& name) const;
431      EventHandler* eventHandler(uint index) const;      EventHandler* eventHandler(uint index) const;
432      inline uint size() const { return args.size(); }      inline uint size() const { return (int) args.size(); }
433      bool isPolyphonic() const;      bool isPolyphonic() const;
434  };  };
435  typedef Ref<EventHandlers,Node> EventHandlersRef;  typedef Ref<EventHandlers,Node> EventHandlersRef;
# Line 464  public: Line 499  public:
499      bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }      bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
500  };  };
501    
502    class SyncBlock : public Statement {
503        StatementsRef m_statements;
504    public:
505        SyncBlock(StatementsRef statements) : m_statements(statements) {}
506        StmtType_t statementType() const { return STMT_SYNC; }
507        void dump(int level = 0);
508        Statements* statements() const;
509        bool isPolyphonic() const { return m_statements->isPolyphonic(); }
510    };
511    typedef Ref<SyncBlock,Node> SyncBlockRef;
512    
513  class Neg : public IntExpr {  class Neg : public IntExpr {
514      IntExprRef expr;      IntExprRef expr;
515  public:  public:
# Line 518  public: Line 564  public:
564  };  };
565  typedef Ref<Or,Node> OrRef;  typedef Ref<Or,Node> OrRef;
566    
567    class BitwiseOr : virtual public BinaryOp, virtual public IntExpr {
568    public:
569        BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
570        int evalInt();
571        void dump(int level = 0);
572    };
573    typedef Ref<BitwiseOr,Node> BitwiseOrRef;
574    
575  class And : virtual public BinaryOp, virtual public IntExpr {  class And : virtual public BinaryOp, virtual public IntExpr {
576  public:  public:
577      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
# Line 526  public: Line 580  public:
580  };  };
581  typedef Ref<And,Node> AndRef;  typedef Ref<And,Node> AndRef;
582    
583    class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr {
584    public:
585        BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
586        int evalInt();
587        void dump(int level = 0);
588    };
589    typedef Ref<BitwiseAnd,Node> BitwiseAndRef;
590    
591  class Not : virtual public IntExpr {  class Not : virtual public IntExpr {
592      IntExprRef expr;      IntExprRef expr;
593  public:  public:
# Line 537  public: Line 599  public:
599  };  };
600  typedef Ref<Not,Node> NotRef;  typedef Ref<Not,Node> NotRef;
601    
602    class BitwiseNot : virtual public IntExpr {
603        IntExprRef expr;
604    public:
605        BitwiseNot(IntExprRef expr) : expr(expr) {}
606        int evalInt() { return ~expr->evalInt(); }
607        void dump(int level = 0);
608        bool isConstExpr() const { return expr->isConstExpr(); }
609        bool isPolyphonic() const { return expr->isPolyphonic(); }
610    };
611    typedef Ref<BitwiseNot,Node> BitwiseNotRef;
612    
613  class ParserContext : public VMParserContext {  class ParserContext : public VMParserContext {
614  public:  public:
615      struct Error {      struct Error {
# Line 550  public: Line 623  public:
623      std::vector<ParserIssue> vErrors;      std::vector<ParserIssue> vErrors;
624      std::vector<ParserIssue> vWarnings;      std::vector<ParserIssue> vWarnings;
625      std::vector<ParserIssue> vIssues;      std::vector<ParserIssue> vIssues;
626        std::vector<CodeBlock>   vPreprocessorComments;
627    
628      std::set<String> builtinPreprocessorConditions;      std::set<String> builtinPreprocessorConditions;
629      std::set<String> userPreprocessorConditions;      std::set<String> userPreprocessorConditions;
630    
631      std::map<String,VariableRef> vartable;      std::map<String,VariableRef> vartable;
632        std::map<String,StatementsRef> userFnTable;
633      int globalIntVarCount;      int globalIntVarCount;
634      int globalStrVarCount;      int globalStrVarCount;
635      int polyphonicIntVarCount;      int polyphonicIntVarCount;
# Line 586  public: Line 661  public:
661      IntVariableRef globalIntVar(const String& name);      IntVariableRef globalIntVar(const String& name);
662      StringVariableRef globalStrVar(const String& name);      StringVariableRef globalStrVar(const String& name);
663      VariableRef variableByName(const String& name);      VariableRef variableByName(const String& name);
664      void addErr(int line, const char* txt);      StatementsRef userFunctionByName(const String& name);
665      void addWrn(int line, const char* txt);      void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
666        void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
667        void addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn);
668      void createScanner(std::istream* is);      void createScanner(std::istream* is);
669      void destroyScanner();      void destroyScanner();
670      bool setPreprocessorCondition(const char* name);      bool setPreprocessorCondition(const char* name);
# Line 596  public: Line 673  public:
673      std::vector<ParserIssue> issues() const OVERRIDE;      std::vector<ParserIssue> issues() const OVERRIDE;
674      std::vector<ParserIssue> errors() const OVERRIDE;      std::vector<ParserIssue> errors() const OVERRIDE;
675      std::vector<ParserIssue> warnings() const OVERRIDE;      std::vector<ParserIssue> warnings() const OVERRIDE;
676        std::vector<CodeBlock> preprocessorComments() const OVERRIDE;
677      VMEventHandler* eventHandler(uint index) OVERRIDE;      VMEventHandler* eventHandler(uint index) OVERRIDE;
678      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
679      void registerBuiltInConstIntVariables(const std::map<String,int>& vars);      void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
680      void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);      void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
681      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
682        void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
683  };  };
684    
685  class ExecContext : public VMExecContext {  class ExecContext : public VMExecContext {
# Line 617  public: Line 696  public:
696    
697      ArrayList<int> polyphonicIntMemory;      ArrayList<int> polyphonicIntMemory;
698      VMExecStatus_t status;      VMExecStatus_t status;
699        StmtFlags_t flags;
700      ArrayList<StackFrame> stack;      ArrayList<StackFrame> stack;
701      int stackFrame;      int stackFrame;
702      int suspendMicroseconds;      int suspendMicroseconds;
703        size_t instructionsCount;
704    
705      ExecContext() :      ExecContext() :
706          status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {}          status(VM_EXEC_NOT_RUNNING), flags(STMT_SUCCESS), stackFrame(-1),
707            suspendMicroseconds(0), instructionsCount(0) {}
708    
709      virtual ~ExecContext() {}      virtual ~ExecContext() {}
710    
# Line 645  public: Line 727  public:
727          stack[0].statement = NULL;          stack[0].statement = NULL;
728          stack[0].subindex  = -1;          stack[0].subindex  = -1;
729          stackFrame = -1;          stackFrame = -1;
730            flags = STMT_SUCCESS;
731      }      }
732    
733      int suspensionTimeMicroseconds() const OVERRIDE {      int suspensionTimeMicroseconds() const OVERRIDE {
734          return suspendMicroseconds;          return suspendMicroseconds;
735      }      }
736    
737        void resetPolyphonicData() OVERRIDE {
738            if (polyphonicIntMemory.empty()) return;
739            memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int));
740        }
741    
742        size_t instructionsPerformed() const OVERRIDE {
743            return instructionsCount;
744        }
745    
746        void signalAbort() OVERRIDE {
747            flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED);
748        }
749    
750        void forkTo(VMExecContext* ectx) const OVERRIDE;
751  };  };
752    
753  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.2879  
changed lines
  Added in v.3293

  ViewVC Help
Powered by ViewVC