/[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 3551 by schoenebeck, Thu Aug 1 10:22:56 2019 UTC
# Line 1  Line 1 
1  /*                                                              -*- c++ -*-  /*                                                              -*- c++ -*-
2   *   *
3   * Copyright (c) 2014 - 2016 Christian Schoenebeck and Andreas Persson   * Copyright (c) 2014 - 2019 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        STMT_NOOP,
41  };  };
42    
43  class Node {  class Node {
# Line 63  public: Line 66  public:
66  };  };
67  typedef Ref<IntExpr,Node> IntExprRef;  typedef Ref<IntExpr,Node> IntExprRef;
68    
69    class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression {
70    public:
71        ExprType_t exprType() const { return INT_ARR_EXPR; }
72        String evalCastToStr();
73    };
74    typedef Ref<IntArrayExpr,Node> IntArrayExprRef;
75    
76  class StringExpr : virtual public VMStringExpr, virtual public Expression {  class StringExpr : virtual public VMStringExpr, virtual public Expression {
77  public:  public:
78      ExprType_t exprType() const { return STRING_EXPR; }      ExprType_t exprType() const { return STRING_EXPR; }
# Line 72  public: Line 82  public:
82  typedef Ref<StringExpr,Node> StringExprRef;  typedef Ref<StringExpr,Node> StringExprRef;
83    
84  class IntLiteral : virtual public IntExpr {  class IntLiteral : virtual public IntExpr {
     int value;  
85  public:  public:
86        int value;
87      IntLiteral(int value) : value(value) { }      IntLiteral(int value) : value(value) { }
88      int evalInt();      int evalInt();
89      void dump(int level = 0);      void dump(int level = 0);
# Line 98  public: Line 108  public:
108      std::vector<ExpressionRef> args;      std::vector<ExpressionRef> args;
109      void add(ExpressionRef arg) { args.push_back(arg); }      void add(ExpressionRef arg) { args.push_back(arg); }
110      void dump(int level = 0);      void dump(int level = 0);
111      int argsCount() const { return args.size(); }      int argsCount() const { return (int) args.size(); }
112      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; }
113      bool isPolyphonic() const;      bool isPolyphonic() const;
114  };  };
115  typedef Ref<Args,Node> ArgsRef;  typedef Ref<Args,Node> ArgsRef;
116    
117  class Variable : virtual public Expression {  class Variable : virtual public VMVariable, virtual public Expression {
118  public:  public:
119      virtual bool isConstExpr() const { return bConst; }      bool isConstExpr() const OVERRIDE { return bConst; }
120        bool isAssignable() const OVERRIDE { return !bConst; }
121      virtual void assign(Expression* expr) = 0;      virtual void assign(Expression* expr) = 0;
122        void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); }
123  protected:  protected:
124      Variable(ParserContext* ctx, int _memPos, bool _bConst)      Variable(ParserContext* ctx, int _memPos, bool _bConst)
125          : context(ctx), memPos(_memPos), bConst(_bConst) {}          : context(ctx), memPos(_memPos), bConst(_bConst) {}
# Line 148  class BuiltInIntVariable : public IntVar Line 160  class BuiltInIntVariable : public IntVar
160      VMIntRelPtr* ptr;      VMIntRelPtr* ptr;
161  public:  public:
162      BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);      BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);
163      void assign(Expression* expr);      bool isAssignable() const OVERRIDE { return !ptr->readonly; }
164      int evalInt();      void assign(Expression* expr) OVERRIDE;
165      void dump(int level = 0);      int evalInt() OVERRIDE;
166        void dump(int level = 0) OVERRIDE;
167  };  };
168  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;  typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
169    
# Line 161  public: Line 174  public:
174  };  };
175  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;  typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
176    
177  class IntArrayVariable : public Variable, virtual public VMIntArrayExpr {  class IntArrayVariable : public Variable, virtual public IntArrayExpr {
178      ArrayList<int> values;      ArrayList<int> values;
179  public:  public:
180      IntArrayVariable(ParserContext* ctx, int size);      IntArrayVariable(ParserContext* ctx, int size);
181      IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);      IntArrayVariable(ParserContext* ctx, int size, ArgsRef values, bool _bConst = false);
182      void assign(Expression* expr) {} // ignore scalar assignment      void assign(Expression* expr) {} // ignore scalar assignment
183      String evalCastToStr() { return ""; } // ignore scalar cast to string      String evalCastToStr() { return ""; } // ignore scalar cast to string
184      ExprType_t exprType() const { return INT_ARR_EXPR; }      ExprType_t exprType() const { return INT_ARR_EXPR; }
# Line 184  class BuiltInIntArrayVariable : public I Line 197  class BuiltInIntArrayVariable : public I
197      VMInt8Array* array;      VMInt8Array* array;
198  public:  public:
199      BuiltInIntArrayVariable(const String& name, VMInt8Array* array);      BuiltInIntArrayVariable(const String& name, VMInt8Array* array);
200      int arraySize() const { return array->size; }      int arraySize() const OVERRIDE { return array->size; }
201      int evalIntElement(uint i);      int evalIntElement(uint i) OVERRIDE;
202      void assignIntElement(uint i, int value);      bool isAssignable() const OVERRIDE { return !array->readonly; }
203      void dump(int level = 0);      void assignIntElement(uint i, int value) OVERRIDE;
204        void dump(int level = 0) OVERRIDE;
205  };  };
206  typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;  typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;
207    
208  class IntArrayElement : public IntVariable {  class IntArrayElement : public IntVariable {
209      IntArrayVariableRef array;      IntArrayExprRef array;
210      IntExprRef index;      IntExprRef index;
211  public:  public:
212      IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex);      IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex);
213      void assign(Expression* expr);      void assign(Expression* expr);
214      int evalInt();      int evalInt();
215      void dump(int level = 0);      void dump(int level = 0);
# Line 287  typedef Ref<Statement,Node> StatementRef Line 301  typedef Ref<Statement,Node> StatementRef
301  class NoOperation : public Statement {  class NoOperation : public Statement {
302  public:  public:
303      NoOperation() : Statement() {}      NoOperation() : Statement() {}
304      StmtType_t statementType() const { return STMT_LEAF; }      StmtType_t statementType() const { return STMT_NOOP; }
305      void dump(int level = 0) {}      void dump(int level = 0) {}
306      bool isPolyphonic() const { return false; }      bool isPolyphonic() const { return false; }
307  };  };
# Line 320  public: Line 334  public:
334      virtual Statements* branch(uint i) const = 0;      virtual Statements* branch(uint i) const = 0;
335  };  };
336    
337    class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr, virtual public IntArrayExpr {
338        VMDynVar* dynVar;
339        String varName;
340    public:
341        DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v);
342        ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); }
343        bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); }
344        bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); }
345        bool isPolyphonic() const OVERRIDE { return false; }
346        void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); }
347        VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); }
348        int evalInt() OVERRIDE;
349        String evalStr() OVERRIDE;
350        String evalCastToStr() OVERRIDE;
351        int arraySize() const OVERRIDE { return dynVar->asIntArray()->arraySize(); }
352        int evalIntElement(uint i) OVERRIDE { return dynVar->asIntArray()->evalIntElement(i); }
353        void assignIntElement(uint i, int value) OVERRIDE { return dynVar->asIntArray()->assignIntElement(i, value); }
354        void dump(int level = 0) OVERRIDE;
355    };
356    typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
357    
358  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {  class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
359      String functionName;      String functionName;
360      ArgsRef args;      ArgsRef args;
# Line 327  class FunctionCall : virtual public Leaf Line 362  class FunctionCall : virtual public Leaf
362  public:  public:
363      FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :      FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :
364          functionName(function), args(args), fn(fn) { }          functionName(function), args(args), fn(fn) { }
365      void dump(int level = 0);      void dump(int level = 0) OVERRIDE;
366      StmtFlags_t exec();      StmtFlags_t exec() OVERRIDE;
367      int evalInt();      int evalInt() OVERRIDE;
368      String evalStr();      VMIntArrayExpr* asIntArray() const OVERRIDE;
369      bool isConstExpr() const { return false; }      String evalStr() OVERRIDE;
370      ExprType_t exprType() const;      bool isConstExpr() const OVERRIDE { return false; }
371      String evalCastToStr();      ExprType_t exprType() const OVERRIDE;
372      bool isPolyphonic() const { return args->isPolyphonic(); }      String evalCastToStr() OVERRIDE;
373        bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); }
374  protected:  protected:
375      VMFnResult* execVMFn();      VMFnResult* execVMFn();
376  };  };
377  typedef Ref<FunctionCall,Node> FunctionCallRef;  typedef Ref<FunctionCall,Node> FunctionCallRef;
378    
379    class NoFunctionCall : public FunctionCall {
380    public:
381        NoFunctionCall() : FunctionCall("nothing", new Args, NULL) {}
382        StmtType_t statementType() const { return STMT_NOOP; }
383    };
384    typedef Ref<NoFunctionCall,Node> NoFunctionCallRef;
385    
386  class EventHandler : virtual public Statements, virtual public VMEventHandler {  class EventHandler : virtual public Statements, virtual public VMEventHandler {
387      StatementsRef statements;      StatementsRef statements;
388      bool usingPolyphonics;      bool usingPolyphonics;
# Line 394  public: Line 437  public:
437      int evalInt() { return 0; }      int evalInt() { return 0; }
438      EventHandler* eventHandlerByName(const String& name) const;      EventHandler* eventHandlerByName(const String& name) const;
439      EventHandler* eventHandler(uint index) const;      EventHandler* eventHandler(uint index) const;
440      inline uint size() const { return args.size(); }      inline uint size() const { return (int) args.size(); }
441      bool isPolyphonic() const;      bool isPolyphonic() const;
442  };  };
443  typedef Ref<EventHandlers,Node> EventHandlersRef;  typedef Ref<EventHandlers,Node> EventHandlersRef;
# Line 464  public: Line 507  public:
507      bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }      bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
508  };  };
509    
510    class SyncBlock : public Statement {
511        StatementsRef m_statements;
512    public:
513        SyncBlock(StatementsRef statements) : m_statements(statements) {}
514        StmtType_t statementType() const { return STMT_SYNC; }
515        void dump(int level = 0);
516        Statements* statements() const;
517        bool isPolyphonic() const { return m_statements->isPolyphonic(); }
518    };
519    typedef Ref<SyncBlock,Node> SyncBlockRef;
520    
521  class Neg : public IntExpr {  class Neg : public IntExpr {
522      IntExprRef expr;      IntExprRef expr;
523  public:  public:
# Line 518  public: Line 572  public:
572  };  };
573  typedef Ref<Or,Node> OrRef;  typedef Ref<Or,Node> OrRef;
574    
575    class BitwiseOr : virtual public BinaryOp, virtual public IntExpr {
576    public:
577        BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
578        int evalInt();
579        void dump(int level = 0);
580    };
581    typedef Ref<BitwiseOr,Node> BitwiseOrRef;
582    
583  class And : virtual public BinaryOp, virtual public IntExpr {  class And : virtual public BinaryOp, virtual public IntExpr {
584  public:  public:
585      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}      And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
# Line 526  public: Line 588  public:
588  };  };
589  typedef Ref<And,Node> AndRef;  typedef Ref<And,Node> AndRef;
590    
591    class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr {
592    public:
593        BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
594        int evalInt();
595        void dump(int level = 0);
596    };
597    typedef Ref<BitwiseAnd,Node> BitwiseAndRef;
598    
599  class Not : virtual public IntExpr {  class Not : virtual public IntExpr {
600      IntExprRef expr;      IntExprRef expr;
601  public:  public:
# Line 537  public: Line 607  public:
607  };  };
608  typedef Ref<Not,Node> NotRef;  typedef Ref<Not,Node> NotRef;
609    
610    class BitwiseNot : virtual public IntExpr {
611        IntExprRef expr;
612    public:
613        BitwiseNot(IntExprRef expr) : expr(expr) {}
614        int evalInt() { return ~expr->evalInt(); }
615        void dump(int level = 0);
616        bool isConstExpr() const { return expr->isConstExpr(); }
617        bool isPolyphonic() const { return expr->isPolyphonic(); }
618    };
619    typedef Ref<BitwiseNot,Node> BitwiseNotRef;
620    
621  class ParserContext : public VMParserContext {  class ParserContext : public VMParserContext {
622  public:  public:
623      struct Error {      struct Error {
# Line 550  public: Line 631  public:
631      std::vector<ParserIssue> vErrors;      std::vector<ParserIssue> vErrors;
632      std::vector<ParserIssue> vWarnings;      std::vector<ParserIssue> vWarnings;
633      std::vector<ParserIssue> vIssues;      std::vector<ParserIssue> vIssues;
634        std::vector<CodeBlock>   vPreprocessorComments;
635    
636      std::set<String> builtinPreprocessorConditions;      std::set<String> builtinPreprocessorConditions;
637      std::set<String> userPreprocessorConditions;      std::set<String> userPreprocessorConditions;
638    
639      std::map<String,VariableRef> vartable;      std::map<String,VariableRef> vartable;
640        std::map<String,StatementsRef> userFnTable;
641      int globalIntVarCount;      int globalIntVarCount;
642      int globalStrVarCount;      int globalStrVarCount;
643      int polyphonicIntVarCount;      int polyphonicIntVarCount;
# Line 586  public: Line 669  public:
669      IntVariableRef globalIntVar(const String& name);      IntVariableRef globalIntVar(const String& name);
670      StringVariableRef globalStrVar(const String& name);      StringVariableRef globalStrVar(const String& name);
671      VariableRef variableByName(const String& name);      VariableRef variableByName(const String& name);
672      void addErr(int line, const char* txt);      StatementsRef userFunctionByName(const String& name);
673      void addWrn(int line, const char* txt);      void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
674        void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
675        void addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn);
676      void createScanner(std::istream* is);      void createScanner(std::istream* is);
677      void destroyScanner();      void destroyScanner();
678      bool setPreprocessorCondition(const char* name);      bool setPreprocessorCondition(const char* name);
# Line 596  public: Line 681  public:
681      std::vector<ParserIssue> issues() const OVERRIDE;      std::vector<ParserIssue> issues() const OVERRIDE;
682      std::vector<ParserIssue> errors() const OVERRIDE;      std::vector<ParserIssue> errors() const OVERRIDE;
683      std::vector<ParserIssue> warnings() const OVERRIDE;      std::vector<ParserIssue> warnings() const OVERRIDE;
684        std::vector<CodeBlock> preprocessorComments() const OVERRIDE;
685      VMEventHandler* eventHandler(uint index) OVERRIDE;      VMEventHandler* eventHandler(uint index) OVERRIDE;
686      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;      VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
687      void registerBuiltInConstIntVariables(const std::map<String,int>& vars);      void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
688      void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);      void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
689      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);      void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
690        void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
691  };  };
692    
693  class ExecContext : public VMExecContext {  class ExecContext : public VMExecContext {
# Line 617  public: Line 704  public:
704    
705      ArrayList<int> polyphonicIntMemory;      ArrayList<int> polyphonicIntMemory;
706      VMExecStatus_t status;      VMExecStatus_t status;
707        StmtFlags_t flags;
708      ArrayList<StackFrame> stack;      ArrayList<StackFrame> stack;
709      int stackFrame;      int stackFrame;
710      int suspendMicroseconds;      int suspendMicroseconds;
711        size_t instructionsCount;
712        struct ExitRes {
713            Expression* value;
714            IntLiteral intLiteral;
715            StringLiteral stringLiteral;
716    
717      ExecContext() :          ExitRes() : intLiteral(0), stringLiteral("") { }
718          status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {}      } exitRes;
719    
720        ExecContext();
721      virtual ~ExecContext() {}      virtual ~ExecContext() {}
722    
723      inline void pushStack(Statement* stmt) {      inline void pushStack(Statement* stmt) {
# Line 645  public: Line 739  public:
739          stack[0].statement = NULL;          stack[0].statement = NULL;
740          stack[0].subindex  = -1;          stack[0].subindex  = -1;
741          stackFrame = -1;          stackFrame = -1;
742            flags = STMT_SUCCESS;
743        }
744    
745        inline void clearExitRes() {
746            exitRes.value = NULL;
747      }      }
748    
749      int suspensionTimeMicroseconds() const OVERRIDE {      int suspensionTimeMicroseconds() const OVERRIDE {
750          return suspendMicroseconds;          return suspendMicroseconds;
751      }      }
752    
753        void resetPolyphonicData() OVERRIDE {
754            if (polyphonicIntMemory.empty()) return;
755            memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int));
756        }
757    
758        size_t instructionsPerformed() const OVERRIDE {
759            return instructionsCount;
760        }
761    
762        void signalAbort() OVERRIDE {
763            flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED);
764        }
765    
766        void forkTo(VMExecContext* ectx) const OVERRIDE;
767    
768        VMExpr* exitResult() OVERRIDE {
769            return exitRes.value;
770        }
771  };  };
772    
773  } // namespace LinuxSampler  } // namespace LinuxSampler

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

  ViewVC Help
Powered by ViewVC