/[svn]/linuxsampler/trunk/src/scriptvm/tree.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/scriptvm/tree.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2594 - (hide annotations) (download) (as text)
Thu Jun 5 00:16:25 2014 UTC (9 years, 9 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 18638 byte(s)
* ScriptVM (WIP): started to integrate real-time instrument script
  support into the sampler engine implementations. The code is
  shared among all sampler engines, however currently only the gig
  file format supports storing instrument scripts (as LinuxSampler
  extension to the original GigaStudio 4 file format).
* gig engine: Added support for loading instrument scripts from .gig
  files.
* ScriptVM (WIP): Implemented built-in script variables %CC, $CC_NUM,
  $EVENT_NOTE, $EVENT_VELOCITY, $VCC_MONO_AT, $VCC_PITCH_BEND.
* ScriptVM (WIP): Implemented execution of script event handler "init".
* ScriptVM (WIP): Implemented execution of script event handler
  "controller".
* Bumped version (1.0.0.svn42).

1 schoenebeck 2581 /* -*- c++ -*-
2     *
3     * Copyright (c) 2014 Christian Schoenebeck and Andreas Persson
4     *
5     * http://www.linuxsampler.org
6     *
7     * This file is part of LinuxSampler and released under the same terms.
8     * See README file for details.
9     */
10    
11     // This header defines VM core implementation internal data types only used
12     // inside the parser and core VM implementation of this source directory. Not
13     // intended to be used in other source code parts (other source code
14     // directories) of the sampler.
15    
16     #ifndef LS_INSTRPARSERTREE_H
17     #define LS_INSTRPARSERTREE_H
18    
19     #include <vector>
20     #include <iostream>
21     #include <map>
22     #include <set>
23     #include "../common/global.h"
24     #include "../common/Ref.h"
25     #include "../common/ArrayList.h"
26     #include "common.h"
27    
28     namespace LinuxSampler {
29    
30     class ParserContext;
31     class ExecContext;
32    
33     enum StmtType_t {
34     STMT_LEAF,
35     STMT_LIST,
36     STMT_BRANCH,
37     STMT_LOOP,
38     };
39    
40     class Node {
41     public:
42     Node();
43     virtual ~Node();
44     virtual void dump(int level = 0) = 0;
45     void printIndents(int n);
46     };
47     typedef Ref<Node> NodeRef;
48    
49     class Expression : virtual public VMExpr, virtual public Node {
50     public:
51     virtual ExprType_t exprType() const = 0;
52     virtual bool isConstExpr() const = 0;
53     virtual String evalCastToStr() = 0;
54     };
55     typedef Ref<Expression,Node> ExpressionRef;
56    
57     class IntExpr : virtual public VMIntExpr, virtual public Expression {
58     public:
59     ExprType_t exprType() const { return INT_EXPR; }
60     virtual int evalInt() = 0;
61     String evalCastToStr();
62     };
63     typedef Ref<IntExpr,Node> IntExprRef;
64    
65     class StringExpr : virtual public VMStringExpr, virtual public Expression {
66     public:
67     ExprType_t exprType() const { return STRING_EXPR; }
68     virtual String evalStr() = 0;
69     String evalCastToStr() { return evalStr(); }
70     };
71     typedef Ref<StringExpr,Node> StringExprRef;
72    
73     class IntLiteral : virtual public IntExpr {
74     int value;
75     public:
76     IntLiteral(int value) : value(value) { }
77     int evalInt();
78     void dump(int level = 0);
79     bool isConstExpr() const { return true; }
80     };
81     typedef Ref<IntLiteral,Node> IntLiteralRef;
82    
83     class StringLiteral : virtual public StringExpr {
84     public:
85     String value;
86     StringLiteral(const String& value) : value(value) { }
87     bool isConstExpr() const { return true; }
88     void dump(int level = 0);
89     String evalStr() { return value; }
90     };
91     typedef Ref<StringLiteral,Node> StringLiteralRef;
92    
93     class Args : virtual public VMFnArgs, virtual public Node {
94     public:
95     std::vector<ExpressionRef> args;
96     void add(ExpressionRef arg) { args.push_back(arg); }
97     void dump(int level = 0);
98     int argsCount() const { return args.size(); }
99     VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; }
100     };
101     typedef Ref<Args,Node> ArgsRef;
102    
103     class Variable : virtual public Expression {
104     public:
105     virtual bool isConstExpr() const { return bConst; }
106     virtual void assign(Expression* expr) = 0;
107     protected:
108     Variable(ParserContext* ctx, int _memPos, bool _bConst)
109     : context(ctx), memPos(_memPos), bConst(_bConst) {}
110    
111     ParserContext* context;
112     int memPos;
113     bool bConst;
114     };
115     typedef Ref<Variable,Node> VariableRef;
116    
117     class IntVariable : public Variable, virtual public IntExpr {
118     bool polyphonic;
119     public:
120     IntVariable(ParserContext* ctx);
121     void assign(Expression* expr);
122     int evalInt();
123     void dump(int level = 0);
124     bool isPolyphonic() const { return polyphonic; }
125     protected:
126     IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size = 1);
127     };
128     typedef Ref<IntVariable,Node> IntVariableRef;
129    
130     class ConstIntVariable : public IntVariable {
131     public:
132     int value;
133    
134     ConstIntVariable(int value);
135     //ConstIntVariable(ParserContext* ctx, int value = 0);
136     void assign(Expression* expr);
137     int evalInt();
138     void dump(int level = 0);
139     };
140     typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;
141    
142 schoenebeck 2594 class BuiltInIntVariable : public IntVariable {
143     String name;
144     VMIntRelPtr* ptr;
145     public:
146     BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);
147     void assign(Expression* expr);
148     int evalInt();
149     void dump(int level = 0);
150     };
151     typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
152    
153 schoenebeck 2581 class PolyphonicIntVariable : public IntVariable {
154     public:
155     PolyphonicIntVariable(ParserContext* ctx);
156     void dump(int level = 0);
157     };
158     typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
159    
160     class IntArrayVariable : public Variable {
161     ArrayList<int> values;
162     public:
163     IntArrayVariable(ParserContext* ctx, int size);
164     IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);
165     void assign(Expression* expr) {} // ignore scalar assignment
166 schoenebeck 2594 String evalCastToStr() { return ""; } // ignore scalar cast to string
167 schoenebeck 2581 ExprType_t exprType() const { return INT_ARR_EXPR; }
168 schoenebeck 2594 virtual int arraySize() const { return values.size(); }
169     virtual int evalIntElement(uint i);
170     virtual void assignIntElement(uint i, int value);
171     void dump(int level = 0);
172     protected:
173     IntArrayVariable(ParserContext* ctx, bool bConst);
174     };
175     typedef Ref<IntArrayVariable,Node> IntArrayVariableRef;
176    
177     class BuiltInIntArrayVariable : public IntArrayVariable {
178     String name;
179     VMInt8Array* array;
180     public:
181     BuiltInIntArrayVariable(const String& name, VMInt8Array* array);
182     int arraySize() const { return array->size; }
183 schoenebeck 2581 int evalIntElement(uint i);
184     void assignIntElement(uint i, int value);
185     void dump(int level = 0);
186     };
187 schoenebeck 2594 typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;
188 schoenebeck 2581
189     class IntArrayElement : public IntVariable {
190     IntArrayVariableRef array;
191     IntExprRef index;
192     public:
193     IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex);
194     void assign(Expression* expr);
195     int evalInt();
196     void dump(int level = 0);
197     };
198     typedef Ref<IntArrayElement,Node> IntArrayElementRef;
199    
200     class StringVariable : public Variable, virtual public StringExpr {
201     public:
202     StringVariable(ParserContext* ctx);
203     void assign(Expression* expr);
204     String evalStr();
205     void dump(int level = 0);
206     protected:
207     StringVariable(ParserContext* ctx, bool bConst);
208     };
209     typedef Ref<StringVariable,Node> StringVariableRef;
210    
211     class ConstStringVariable : public StringVariable {
212     public:
213     String value;
214    
215     ConstStringVariable(ParserContext* ctx, String value = "");
216     void assign(Expression* expr);
217     String evalStr();
218     void dump(int level = 0);
219     };
220     typedef Ref<ConstStringVariable,Node> ConstStringVariableRef;
221    
222     class BinaryOp : virtual public Expression {
223     protected:
224     ExpressionRef lhs;
225     ExpressionRef rhs;
226     public:
227     BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }
228     bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }
229     };
230     typedef Ref<BinaryOp,Node> BinaryOpRef;
231    
232     class Add : virtual public BinaryOp, virtual public IntExpr {
233     public:
234     Add(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
235     int evalInt();
236     void dump(int level = 0);
237     };
238     typedef Ref<Add,Node> AddRef;
239    
240     class Sub : virtual public BinaryOp, virtual public IntExpr {
241     public:
242     Sub(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
243     int evalInt();
244     void dump(int level = 0);
245     };
246     typedef Ref<Sub,Node> SubRef;
247    
248     class Mul : virtual public BinaryOp, virtual public IntExpr {
249     public:
250     Mul(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
251     int evalInt();
252     void dump(int level = 0);
253     };
254     typedef Ref<Mul,Node> MulRef;
255    
256     class Div : virtual public BinaryOp, virtual public IntExpr {
257     public:
258     Div(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
259     int evalInt();
260     void dump(int level = 0);
261     };
262     typedef Ref<Div,Node> DivRef;
263    
264     class Mod : virtual public BinaryOp, virtual public IntExpr {
265     public:
266     Mod(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
267     int evalInt();
268     void dump(int level = 0);
269     };
270     typedef Ref<Mod,Node> ModRef;
271    
272     class Statement : virtual public Node {
273     public:
274     virtual StmtType_t statementType() const = 0;
275     };
276     typedef Ref<Statement,Node> StatementRef;
277    
278     // Just used by parser to avoid "not a statement" parser warning, will be
279     // filtered out by parser. So it will not be part of the VM tree after parsing.
280     class NoOperation : public Statement {
281     public:
282     NoOperation() : Statement() {}
283     StmtType_t statementType() const { return STMT_LEAF; }
284     void dump(int level = 0) {}
285     };
286     typedef Ref<NoOperation,Node> NoOperationRef;
287    
288     bool isNoOperation(StatementRef statement);
289    
290     class LeafStatement : public Statement {
291     public:
292     virtual StmtFlags_t exec() = 0;
293     virtual StmtType_t statementType() const { return STMT_LEAF; }
294     };
295     typedef Ref<LeafStatement,Node> LeafStatementRef;
296    
297     class Statements : public Statement {
298     std::vector<StatementRef> args;
299     public:
300     void add(StatementRef arg) { args.push_back(arg); }
301     void dump(int level = 0);
302     StmtType_t statementType() const { return STMT_LIST; }
303     virtual Statement* statement(uint i);
304     };
305     typedef Ref<Statements,Node> StatementsRef;
306    
307     class BranchStatement : public Statement {
308     public:
309     StmtType_t statementType() const { return STMT_BRANCH; }
310     virtual int evalBranch() = 0;
311     virtual Statements* branch(uint i) const = 0;
312     };
313    
314     class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
315     String functionName;
316     ArgsRef args;
317     VMFunction* fn;
318     public:
319     FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :
320     functionName(function), args(args), fn(fn) { }
321     void dump(int level = 0);
322     StmtFlags_t exec();
323     int evalInt();
324     String evalStr();
325     bool isConstExpr() const { return false; }
326     ExprType_t exprType() const;
327     String evalCastToStr();
328     protected:
329     VMFnResult* execVMFn();
330     };
331     typedef Ref<FunctionCall,Node> FunctionCallRef;
332    
333     class EventHandler : virtual public Statements, virtual public VMEventHandler {
334     StatementsRef statements;
335     public:
336     void dump(int level = 0);
337     StmtFlags_t exec();
338     EventHandler(StatementsRef statements) { this->statements = statements; }
339     Statement* statement(uint i) { return statements->statement(i); }
340     };
341     typedef Ref<EventHandler,Node> EventHandlerRef;
342    
343     class OnNote : public EventHandler {
344     public:
345     OnNote(StatementsRef statements) : EventHandler(statements) {}
346     String eventHandlerName() const { return "note"; }
347     };
348     typedef Ref<OnNote,Node> OnNoteRef;
349    
350     class OnInit : public EventHandler {
351     public:
352     OnInit(StatementsRef statements) : EventHandler(statements) {}
353     String eventHandlerName() const { return "init"; }
354     };
355     typedef Ref<OnInit,Node> OnInitRef;
356    
357     class OnRelease : public EventHandler {
358     public:
359     OnRelease(StatementsRef statements) : EventHandler(statements) {}
360     String eventHandlerName() const { return "release"; }
361     };
362     typedef Ref<OnRelease,Node> OnReleaseRef;
363    
364     class OnController : public EventHandler {
365     public:
366     OnController(StatementsRef statements) : EventHandler(statements) {}
367     String eventHandlerName() const { return "controller"; }
368     };
369     typedef Ref<OnController,Node> OnControllerRef;
370    
371     class EventHandlers : virtual public Node {
372     std::vector<EventHandlerRef> args;
373     public:
374     EventHandlers();
375     ~EventHandlers();
376     void add(EventHandlerRef arg);
377     void dump(int level = 0);
378     int evalInt() { return 0; }
379     EventHandler* eventHandlerByName(const String& name) const;
380     EventHandler* eventHandler(uint index) const;
381     inline uint size() const { return args.size(); }
382     };
383     typedef Ref<EventHandlers,Node> EventHandlersRef;
384    
385     class Assignment : public LeafStatement {
386     protected:
387     VariableRef variable;
388     ExpressionRef value;
389     public:
390     Assignment(VariableRef variable, ExpressionRef value);
391     void dump(int level = 0);
392     StmtFlags_t exec();
393     };
394     typedef Ref<Assignment,Node> AssignmentRef;
395    
396     class If : public BranchStatement {
397     IntExprRef condition;
398     StatementsRef ifStatements;
399     StatementsRef elseStatements;
400     public:
401     If(IntExprRef condition, StatementsRef ifStatements, StatementsRef elseStatements) :
402     condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { }
403     If(IntExprRef condition, StatementsRef statements) :
404     condition(condition), ifStatements(statements) { }
405     void dump(int level = 0);
406     int evalBranch();
407     Statements* branch(uint i) const;
408     };
409     typedef Ref<If,Node> IfRef;
410    
411     struct CaseBranch {
412     IntExprRef from;
413     IntExprRef to;
414     StatementsRef statements;
415     };
416    
417     typedef std::vector<CaseBranch> CaseBranches;
418    
419     class SelectCase : public BranchStatement {
420     IntExprRef select;
421     CaseBranches branches;
422     public:
423     SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { }
424     void dump(int level = 0);
425     int evalBranch();
426     Statements* branch(uint i) const;
427     //void addBranch(IntExprRef condition, StatementsRef statements);
428     //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);
429     //void addBranch(CaseBranchRef branch);
430     //void addBranches(CaseBranchesRef branches);
431     };
432     typedef Ref<SelectCase,Node> SelectCaseRef;
433    
434     class While : public Statement {
435     IntExprRef m_condition;
436     StatementsRef m_statements;
437     public:
438     While(IntExprRef condition, StatementsRef statements) :
439     m_condition(condition), m_statements(statements) {}
440     StmtType_t statementType() const { return STMT_LOOP; }
441     void dump(int level = 0);
442     bool evalLoopStartCondition();
443     Statements* statements() const;
444     };
445    
446     class Neg : public IntExpr {
447     IntExprRef expr;
448     public:
449     Neg(IntExprRef expr) : expr(expr) { }
450     int evalInt() { return (expr) ? -expr->evalInt() : 0; }
451     void dump(int level = 0);
452     bool isConstExpr() const { return expr->isConstExpr(); }
453     };
454     typedef Ref<Neg,Node> NegRef;
455    
456     class ConcatString : public StringExpr {
457     ExpressionRef lhs;
458     ExpressionRef rhs;
459     public:
460     ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {}
461     String evalStr();
462     void dump(int level = 0);
463     bool isConstExpr() const;
464     };
465     typedef Ref<ConcatString,Node> ConcatStringRef;
466    
467     class Relation : public IntExpr {
468     public:
469     enum Type {
470     LESS_THAN,
471     GREATER_THAN,
472     LESS_OR_EQUAL,
473     GREATER_OR_EQUAL,
474     EQUAL,
475     NOT_EQUAL
476     };
477     Relation(IntExprRef lhs, Type type, IntExprRef rhs) :
478     lhs(lhs), rhs(rhs), type(type) {}
479     int evalInt();
480     void dump(int level = 0);
481     bool isConstExpr() const;
482     private:
483     IntExprRef lhs;
484     IntExprRef rhs;
485     Type type;
486     };
487     typedef Ref<Relation,Node> RelationRef;
488    
489     class Or : virtual public BinaryOp, virtual public IntExpr {
490     public:
491     Or(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
492     int evalInt();
493     void dump(int level = 0);
494     };
495     typedef Ref<Or,Node> OrRef;
496    
497     class And : virtual public BinaryOp, virtual public IntExpr {
498     public:
499     And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
500     int evalInt();
501     void dump(int level = 0);
502     };
503     typedef Ref<And,Node> AndRef;
504    
505     class Not : virtual public IntExpr {
506     IntExprRef expr;
507     public:
508     Not(IntExprRef expr) : expr(expr) {}
509     int evalInt() { return !expr->evalInt(); }
510     void dump(int level = 0);
511     bool isConstExpr() const { return expr->isConstExpr(); }
512     };
513     typedef Ref<Not,Node> NotRef;
514    
515     class ParserContext : public VMParserContext {
516     public:
517     struct Error {
518     String txt;
519     int line;
520     };
521     typedef Error Warning;
522    
523     void* scanner;
524     std::istream* is;
525 schoenebeck 2588 std::vector<ParserIssue> vErrors;
526     std::vector<ParserIssue> vWarnings;
527     std::vector<ParserIssue> vIssues;
528 schoenebeck 2581
529     std::set<String> builtinPreprocessorConditions;
530     std::set<String> userPreprocessorConditions;
531    
532     std::map<String,VariableRef> vartable;
533     int globalIntVarCount;
534     int globalStrVarCount;
535     int polyphonicIntVarCount;
536    
537     EventHandlersRef handlers;
538    
539     OnInitRef onInit;
540     OnNoteRef onNote;
541     OnReleaseRef onRelease;
542     OnControllerRef onController;
543    
544     ArrayList<int>* globalIntMemory;
545     ArrayList<String>* globalStrMemory;
546 schoenebeck 2588 int requiredMaxStackSize;
547 schoenebeck 2581
548     VMFunctionProvider* functionProvider;
549    
550     ExecContext* execContext;
551    
552     ParserContext(VMFunctionProvider* parent) :
553     scanner(NULL), is(NULL),
554     globalIntVarCount(0), globalStrVarCount(0), polyphonicIntVarCount(0),
555 schoenebeck 2588 globalIntMemory(NULL), globalStrMemory(NULL), requiredMaxStackSize(-1),
556     functionProvider(parent), execContext(NULL)
557 schoenebeck 2581 {
558     }
559 schoenebeck 2588 virtual ~ParserContext();
560 schoenebeck 2581 VariableRef globalVar(const String& name);
561     IntVariableRef globalIntVar(const String& name);
562     StringVariableRef globalStrVar(const String& name);
563     VariableRef variableByName(const String& name);
564     void addErr(int line, const char* txt);
565     void addWrn(int line, const char* txt);
566     void createScanner(std::istream* is);
567     void destroyScanner();
568     bool setPreprocessorCondition(const char* name);
569     bool resetPreprocessorCondition(const char* name);
570     bool isPreprocessorConditionSet(const char* name);
571 schoenebeck 2588 std::vector<ParserIssue> issues() const OVERRIDE;
572     std::vector<ParserIssue> errors() const OVERRIDE;
573     std::vector<ParserIssue> warnings() const OVERRIDE;
574     VMEventHandler* eventHandler(uint index) OVERRIDE;
575     VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
576 schoenebeck 2594 void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
577     void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
578     void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
579 schoenebeck 2581 };
580    
581     class ExecContext : public VMExecContext {
582     public:
583     struct StackFrame {
584     Statement* statement;
585     int subindex;
586    
587     StackFrame() {
588     statement = NULL;
589     subindex = -1;
590     }
591     };
592    
593     ArrayList<int> polyphonicIntMemory;
594     VMExecStatus_t status;
595     ArrayList<StackFrame> stack;
596     int stackFrame;
597     int suspendMicroseconds;
598    
599     ExecContext() :
600     status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {}
601    
602     virtual ~ExecContext() {}
603    
604     inline void pushStack(Statement* stmt) {
605     stackFrame++;
606     //printf("pushStack() -> %d\n", stackFrame);
607     if (stackFrame >= stack.size()) return;
608     stack[stackFrame].statement = stmt;
609     stack[stackFrame].subindex = 0;
610     }
611    
612     inline void popStack() {
613     stack[stackFrame].statement = NULL;
614     stack[stackFrame].subindex = -1;
615     stackFrame--;
616     //printf("popStack() -> %d\n", stackFrame);
617     }
618    
619     inline void reset() {
620     stack[0].statement = NULL;
621     stack[0].subindex = -1;
622     stackFrame = -1;
623     }
624    
625 schoenebeck 2588 int suspensionTimeMicroseconds() const OVERRIDE {
626 schoenebeck 2581 return suspendMicroseconds;
627     }
628     };
629    
630     } // namespace LinuxSampler
631    
632     #endif // LS_INSTRPARSERTREE_H

  ViewVC Help
Powered by ViewVC