/[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 3293 - (hide annotations) (download) (as text)
Tue Jun 27 22:19:19 2017 UTC (6 years, 9 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 24429 byte(s)
* NKSP: Added built-in script function "fork()".
* NKSP: Added built-in array variable %NKSP_CALLBACK_CHILD_ID[].
* NKSP: Added built-in variable $NKSP_CALLBACK_PARENT_ID.
* NKSP: Fixed potential crash when accessing dynamic built-in
  array variables.
* Bumped version (2.0.0.svn65).

1 schoenebeck 2581 /* -*- c++ -*-
2     *
3 schoenebeck 3118 * Copyright (c) 2014 - 2017 Christian Schoenebeck and Andreas Persson
4 schoenebeck 2581 *
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 schoenebeck 3208 #include <string.h> // for memset()
24 schoenebeck 2581 #include "../common/global.h"
25     #include "../common/Ref.h"
26     #include "../common/ArrayList.h"
27     #include "common.h"
28    
29     namespace LinuxSampler {
30    
31     class ParserContext;
32     class ExecContext;
33    
34     enum StmtType_t {
35     STMT_LEAF,
36     STMT_LIST,
37     STMT_BRANCH,
38     STMT_LOOP,
39 schoenebeck 3260 STMT_SYNC,
40 schoenebeck 2581 };
41    
42     class Node {
43     public:
44     Node();
45     virtual ~Node();
46     virtual void dump(int level = 0) = 0;
47 schoenebeck 2645 virtual bool isPolyphonic() const = 0;
48 schoenebeck 2581 void printIndents(int n);
49     };
50     typedef Ref<Node> NodeRef;
51    
52     class Expression : virtual public VMExpr, virtual public Node {
53     public:
54     virtual ExprType_t exprType() const = 0;
55     virtual bool isConstExpr() const = 0;
56     virtual String evalCastToStr() = 0;
57     };
58     typedef Ref<Expression,Node> ExpressionRef;
59    
60     class IntExpr : virtual public VMIntExpr, virtual public Expression {
61     public:
62     ExprType_t exprType() const { return INT_EXPR; }
63     virtual int evalInt() = 0;
64     String evalCastToStr();
65     };
66     typedef Ref<IntExpr,Node> IntExprRef;
67    
68 schoenebeck 3293 class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression {
69 schoenebeck 3056 public:
70     ExprType_t exprType() const { return INT_ARR_EXPR; }
71     String evalCastToStr();
72     };
73 schoenebeck 3293 typedef Ref<IntArrayExpr,Node> IntArrayExprRef;
74 schoenebeck 3056
75 schoenebeck 2581 class StringExpr : virtual public VMStringExpr, virtual public Expression {
76     public:
77     ExprType_t exprType() const { return STRING_EXPR; }
78     virtual String evalStr() = 0;
79     String evalCastToStr() { return evalStr(); }
80     };
81     typedef Ref<StringExpr,Node> StringExprRef;
82    
83     class IntLiteral : virtual public IntExpr {
84     int value;
85     public:
86     IntLiteral(int value) : value(value) { }
87     int evalInt();
88     void dump(int level = 0);
89     bool isConstExpr() const { return true; }
90 schoenebeck 2645 bool isPolyphonic() const { return false; }
91 schoenebeck 2581 };
92     typedef Ref<IntLiteral,Node> IntLiteralRef;
93    
94     class StringLiteral : virtual public StringExpr {
95     public:
96     String value;
97     StringLiteral(const String& value) : value(value) { }
98     bool isConstExpr() const { return true; }
99     void dump(int level = 0);
100     String evalStr() { return value; }
101 schoenebeck 2645 bool isPolyphonic() const { return false; }
102 schoenebeck 2581 };
103     typedef Ref<StringLiteral,Node> StringLiteralRef;
104    
105     class Args : virtual public VMFnArgs, virtual public Node {
106     public:
107     std::vector<ExpressionRef> args;
108     void add(ExpressionRef arg) { args.push_back(arg); }
109     void dump(int level = 0);
110 schoenebeck 3054 int argsCount() const { return (int) args.size(); }
111 schoenebeck 2581 VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; }
112 schoenebeck 2645 bool isPolyphonic() const;
113 schoenebeck 2581 };
114     typedef Ref<Args,Node> ArgsRef;
115    
116 schoenebeck 2945 class Variable : virtual public VMVariable, virtual public Expression {
117 schoenebeck 2581 public:
118 schoenebeck 2945 bool isConstExpr() const OVERRIDE { return bConst; }
119     bool isAssignable() const OVERRIDE { return !bConst; }
120 schoenebeck 2581 virtual void assign(Expression* expr) = 0;
121 schoenebeck 2945 void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); }
122 schoenebeck 2581 protected:
123     Variable(ParserContext* ctx, int _memPos, bool _bConst)
124     : context(ctx), memPos(_memPos), bConst(_bConst) {}
125    
126     ParserContext* context;
127     int memPos;
128     bool bConst;
129     };
130     typedef Ref<Variable,Node> VariableRef;
131    
132     class IntVariable : public Variable, virtual public IntExpr {
133     bool polyphonic;
134     public:
135     IntVariable(ParserContext* ctx);
136     void assign(Expression* expr);
137     int evalInt();
138     void dump(int level = 0);
139     bool isPolyphonic() const { return polyphonic; }
140     protected:
141     IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size = 1);
142     };
143     typedef Ref<IntVariable,Node> IntVariableRef;
144    
145     class ConstIntVariable : public IntVariable {
146     public:
147     int value;
148    
149     ConstIntVariable(int value);
150     //ConstIntVariable(ParserContext* ctx, int value = 0);
151     void assign(Expression* expr);
152     int evalInt();
153     void dump(int level = 0);
154     };
155     typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;
156    
157 schoenebeck 2594 class BuiltInIntVariable : public IntVariable {
158     String name;
159     VMIntRelPtr* ptr;
160     public:
161     BuiltInIntVariable(const String& name, VMIntRelPtr* ptr);
162 schoenebeck 2948 bool isAssignable() const OVERRIDE { return !ptr->readonly; }
163 schoenebeck 3054 void assign(Expression* expr) OVERRIDE;
164     int evalInt() OVERRIDE;
165     void dump(int level = 0) OVERRIDE;
166 schoenebeck 2594 };
167     typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
168    
169 schoenebeck 2581 class PolyphonicIntVariable : public IntVariable {
170     public:
171     PolyphonicIntVariable(ParserContext* ctx);
172     void dump(int level = 0);
173     };
174     typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
175    
176 schoenebeck 3293 class IntArrayVariable : public Variable, virtual public IntArrayExpr {
177 schoenebeck 2581 ArrayList<int> values;
178     public:
179     IntArrayVariable(ParserContext* ctx, int size);
180 schoenebeck 3257 IntArrayVariable(ParserContext* ctx, int size, ArgsRef values, bool _bConst = false);
181 schoenebeck 2581 void assign(Expression* expr) {} // ignore scalar assignment
182 schoenebeck 2594 String evalCastToStr() { return ""; } // ignore scalar cast to string
183 schoenebeck 2581 ExprType_t exprType() const { return INT_ARR_EXPR; }
184 schoenebeck 2594 virtual int arraySize() const { return values.size(); }
185     virtual int evalIntElement(uint i);
186     virtual void assignIntElement(uint i, int value);
187     void dump(int level = 0);
188 schoenebeck 2645 bool isPolyphonic() const { return false; }
189 schoenebeck 2594 protected:
190     IntArrayVariable(ParserContext* ctx, bool bConst);
191     };
192     typedef Ref<IntArrayVariable,Node> IntArrayVariableRef;
193    
194     class BuiltInIntArrayVariable : public IntArrayVariable {
195     String name;
196     VMInt8Array* array;
197     public:
198     BuiltInIntArrayVariable(const String& name, VMInt8Array* array);
199     int arraySize() const { return array->size; }
200 schoenebeck 2581 int evalIntElement(uint i);
201 schoenebeck 3253 bool isAssignable() const OVERRIDE { return !array->readonly; }
202 schoenebeck 2581 void assignIntElement(uint i, int value);
203     void dump(int level = 0);
204     };
205 schoenebeck 2594 typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;
206 schoenebeck 2581
207     class IntArrayElement : public IntVariable {
208 schoenebeck 3293 IntArrayExprRef array;
209 schoenebeck 2581 IntExprRef index;
210     public:
211 schoenebeck 3293 IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex);
212 schoenebeck 2581 void assign(Expression* expr);
213     int evalInt();
214     void dump(int level = 0);
215     };
216     typedef Ref<IntArrayElement,Node> IntArrayElementRef;
217    
218     class StringVariable : public Variable, virtual public StringExpr {
219     public:
220     StringVariable(ParserContext* ctx);
221     void assign(Expression* expr);
222     String evalStr();
223     void dump(int level = 0);
224 schoenebeck 2645 bool isPolyphonic() const { return false; }
225 schoenebeck 2581 protected:
226     StringVariable(ParserContext* ctx, bool bConst);
227     };
228     typedef Ref<StringVariable,Node> StringVariableRef;
229    
230     class ConstStringVariable : public StringVariable {
231     public:
232     String value;
233    
234     ConstStringVariable(ParserContext* ctx, String value = "");
235     void assign(Expression* expr);
236     String evalStr();
237     void dump(int level = 0);
238     };
239     typedef Ref<ConstStringVariable,Node> ConstStringVariableRef;
240    
241     class BinaryOp : virtual public Expression {
242     protected:
243     ExpressionRef lhs;
244     ExpressionRef rhs;
245     public:
246     BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }
247     bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }
248 schoenebeck 2645 bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
249 schoenebeck 2581 };
250     typedef Ref<BinaryOp,Node> BinaryOpRef;
251    
252     class Add : virtual public BinaryOp, virtual public IntExpr {
253     public:
254     Add(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
255     int evalInt();
256     void dump(int level = 0);
257     };
258     typedef Ref<Add,Node> AddRef;
259    
260     class Sub : virtual public BinaryOp, virtual public IntExpr {
261     public:
262     Sub(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
263     int evalInt();
264     void dump(int level = 0);
265     };
266     typedef Ref<Sub,Node> SubRef;
267    
268     class Mul : virtual public BinaryOp, virtual public IntExpr {
269     public:
270     Mul(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
271     int evalInt();
272     void dump(int level = 0);
273     };
274     typedef Ref<Mul,Node> MulRef;
275    
276     class Div : virtual public BinaryOp, virtual public IntExpr {
277     public:
278     Div(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
279     int evalInt();
280     void dump(int level = 0);
281     };
282     typedef Ref<Div,Node> DivRef;
283    
284     class Mod : virtual public BinaryOp, virtual public IntExpr {
285     public:
286     Mod(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
287     int evalInt();
288     void dump(int level = 0);
289     };
290     typedef Ref<Mod,Node> ModRef;
291    
292     class Statement : virtual public Node {
293     public:
294     virtual StmtType_t statementType() const = 0;
295     };
296     typedef Ref<Statement,Node> StatementRef;
297    
298     // Just used by parser to avoid "not a statement" parser warning, will be
299     // filtered out by parser. So it will not be part of the VM tree after parsing.
300     class NoOperation : public Statement {
301     public:
302     NoOperation() : Statement() {}
303     StmtType_t statementType() const { return STMT_LEAF; }
304     void dump(int level = 0) {}
305 schoenebeck 2645 bool isPolyphonic() const { return false; }
306 schoenebeck 2581 };
307     typedef Ref<NoOperation,Node> NoOperationRef;
308    
309     bool isNoOperation(StatementRef statement);
310    
311     class LeafStatement : public Statement {
312     public:
313     virtual StmtFlags_t exec() = 0;
314     virtual StmtType_t statementType() const { return STMT_LEAF; }
315     };
316     typedef Ref<LeafStatement,Node> LeafStatementRef;
317    
318     class Statements : public Statement {
319     std::vector<StatementRef> args;
320     public:
321     void add(StatementRef arg) { args.push_back(arg); }
322     void dump(int level = 0);
323     StmtType_t statementType() const { return STMT_LIST; }
324     virtual Statement* statement(uint i);
325 schoenebeck 2645 bool isPolyphonic() const;
326 schoenebeck 2581 };
327     typedef Ref<Statements,Node> StatementsRef;
328    
329     class BranchStatement : public Statement {
330     public:
331     StmtType_t statementType() const { return STMT_BRANCH; }
332     virtual int evalBranch() = 0;
333     virtual Statements* branch(uint i) const = 0;
334     };
335    
336 schoenebeck 3293 class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr, virtual public IntArrayExpr {
337 schoenebeck 2942 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 schoenebeck 2945 void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); }
346 schoenebeck 3118 VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); }
347 schoenebeck 2942 int evalInt() OVERRIDE;
348     String evalStr() OVERRIDE;
349     String evalCastToStr() OVERRIDE;
350 schoenebeck 3293 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 schoenebeck 2942 void dump(int level = 0) OVERRIDE;
354     };
355     typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
356    
357 schoenebeck 2581 class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
358     String functionName;
359     ArgsRef args;
360     VMFunction* fn;
361     public:
362     FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :
363     functionName(function), args(args), fn(fn) { }
364 schoenebeck 3060 void dump(int level = 0) OVERRIDE;
365     StmtFlags_t exec() OVERRIDE;
366     int evalInt() OVERRIDE;
367 schoenebeck 3056 VMIntArrayExpr* asIntArray() const OVERRIDE;
368 schoenebeck 3060 String evalStr() OVERRIDE;
369     bool isConstExpr() const OVERRIDE { return false; }
370     ExprType_t exprType() const OVERRIDE;
371     String evalCastToStr() OVERRIDE;
372     bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); }
373 schoenebeck 2581 protected:
374     VMFnResult* execVMFn();
375     };
376     typedef Ref<FunctionCall,Node> FunctionCallRef;
377    
378     class EventHandler : virtual public Statements, virtual public VMEventHandler {
379     StatementsRef statements;
380 schoenebeck 2645 bool usingPolyphonics;
381 schoenebeck 2581 public:
382     void dump(int level = 0);
383     StmtFlags_t exec();
384 schoenebeck 2645 EventHandler(StatementsRef statements);
385 schoenebeck 2581 Statement* statement(uint i) { return statements->statement(i); }
386 schoenebeck 2645 bool isPolyphonic() const { return usingPolyphonics; }
387 schoenebeck 2581 };
388     typedef Ref<EventHandler,Node> EventHandlerRef;
389    
390     class OnNote : public EventHandler {
391     public:
392     OnNote(StatementsRef statements) : EventHandler(statements) {}
393 schoenebeck 2879 VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_NOTE; }
394 schoenebeck 2581 String eventHandlerName() const { return "note"; }
395     };
396     typedef Ref<OnNote,Node> OnNoteRef;
397    
398     class OnInit : public EventHandler {
399     public:
400     OnInit(StatementsRef statements) : EventHandler(statements) {}
401 schoenebeck 2879 VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_INIT; }
402 schoenebeck 2581 String eventHandlerName() const { return "init"; }
403     };
404     typedef Ref<OnInit,Node> OnInitRef;
405    
406     class OnRelease : public EventHandler {
407     public:
408     OnRelease(StatementsRef statements) : EventHandler(statements) {}
409 schoenebeck 2879 VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_RELEASE; }
410 schoenebeck 2581 String eventHandlerName() const { return "release"; }
411     };
412     typedef Ref<OnRelease,Node> OnReleaseRef;
413    
414     class OnController : public EventHandler {
415     public:
416     OnController(StatementsRef statements) : EventHandler(statements) {}
417 schoenebeck 2879 VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_CONTROLLER; }
418 schoenebeck 2581 String eventHandlerName() const { return "controller"; }
419     };
420     typedef Ref<OnController,Node> OnControllerRef;
421    
422     class EventHandlers : virtual public Node {
423     std::vector<EventHandlerRef> args;
424     public:
425     EventHandlers();
426     ~EventHandlers();
427     void add(EventHandlerRef arg);
428     void dump(int level = 0);
429     int evalInt() { return 0; }
430     EventHandler* eventHandlerByName(const String& name) const;
431     EventHandler* eventHandler(uint index) const;
432 schoenebeck 3054 inline uint size() const { return (int) args.size(); }
433 schoenebeck 2645 bool isPolyphonic() const;
434 schoenebeck 2581 };
435     typedef Ref<EventHandlers,Node> EventHandlersRef;
436    
437     class Assignment : public LeafStatement {
438     protected:
439     VariableRef variable;
440     ExpressionRef value;
441     public:
442     Assignment(VariableRef variable, ExpressionRef value);
443     void dump(int level = 0);
444     StmtFlags_t exec();
445 schoenebeck 2871 bool isPolyphonic() const { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); }
446 schoenebeck 2581 };
447     typedef Ref<Assignment,Node> AssignmentRef;
448    
449     class If : public BranchStatement {
450     IntExprRef condition;
451     StatementsRef ifStatements;
452     StatementsRef elseStatements;
453     public:
454     If(IntExprRef condition, StatementsRef ifStatements, StatementsRef elseStatements) :
455     condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { }
456     If(IntExprRef condition, StatementsRef statements) :
457     condition(condition), ifStatements(statements) { }
458     void dump(int level = 0);
459     int evalBranch();
460     Statements* branch(uint i) const;
461 schoenebeck 2645 bool isPolyphonic() const;
462 schoenebeck 2581 };
463     typedef Ref<If,Node> IfRef;
464    
465     struct CaseBranch {
466     IntExprRef from;
467     IntExprRef to;
468     StatementsRef statements;
469     };
470    
471     typedef std::vector<CaseBranch> CaseBranches;
472    
473     class SelectCase : public BranchStatement {
474     IntExprRef select;
475     CaseBranches branches;
476     public:
477     SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { }
478     void dump(int level = 0);
479     int evalBranch();
480     Statements* branch(uint i) const;
481     //void addBranch(IntExprRef condition, StatementsRef statements);
482     //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);
483     //void addBranch(CaseBranchRef branch);
484     //void addBranches(CaseBranchesRef branches);
485 schoenebeck 2645 bool isPolyphonic() const;
486 schoenebeck 2581 };
487     typedef Ref<SelectCase,Node> SelectCaseRef;
488    
489     class While : public Statement {
490     IntExprRef m_condition;
491     StatementsRef m_statements;
492     public:
493     While(IntExprRef condition, StatementsRef statements) :
494     m_condition(condition), m_statements(statements) {}
495     StmtType_t statementType() const { return STMT_LOOP; }
496     void dump(int level = 0);
497     bool evalLoopStartCondition();
498     Statements* statements() const;
499 schoenebeck 2645 bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
500 schoenebeck 2581 };
501    
502 schoenebeck 3260 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 schoenebeck 2581 class Neg : public IntExpr {
514     IntExprRef expr;
515     public:
516     Neg(IntExprRef expr) : expr(expr) { }
517     int evalInt() { return (expr) ? -expr->evalInt() : 0; }
518     void dump(int level = 0);
519     bool isConstExpr() const { return expr->isConstExpr(); }
520 schoenebeck 2645 bool isPolyphonic() const { return expr->isPolyphonic(); }
521 schoenebeck 2581 };
522     typedef Ref<Neg,Node> NegRef;
523    
524     class ConcatString : public StringExpr {
525     ExpressionRef lhs;
526     ExpressionRef rhs;
527     public:
528     ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {}
529     String evalStr();
530     void dump(int level = 0);
531     bool isConstExpr() const;
532 schoenebeck 2645 bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
533 schoenebeck 2581 };
534     typedef Ref<ConcatString,Node> ConcatStringRef;
535    
536     class Relation : public IntExpr {
537     public:
538     enum Type {
539     LESS_THAN,
540     GREATER_THAN,
541     LESS_OR_EQUAL,
542     GREATER_OR_EQUAL,
543     EQUAL,
544     NOT_EQUAL
545     };
546     Relation(IntExprRef lhs, Type type, IntExprRef rhs) :
547     lhs(lhs), rhs(rhs), type(type) {}
548     int evalInt();
549     void dump(int level = 0);
550     bool isConstExpr() const;
551 schoenebeck 2645 bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
552 schoenebeck 2581 private:
553     IntExprRef lhs;
554     IntExprRef rhs;
555     Type type;
556     };
557     typedef Ref<Relation,Node> RelationRef;
558    
559     class Or : virtual public BinaryOp, virtual public IntExpr {
560     public:
561     Or(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
562     int evalInt();
563     void dump(int level = 0);
564     };
565     typedef Ref<Or,Node> OrRef;
566    
567 schoenebeck 2935 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 schoenebeck 2581 class And : virtual public BinaryOp, virtual public IntExpr {
576     public:
577     And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
578     int evalInt();
579     void dump(int level = 0);
580     };
581     typedef Ref<And,Node> AndRef;
582    
583 schoenebeck 2935 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 schoenebeck 2581 class Not : virtual public IntExpr {
592     IntExprRef expr;
593     public:
594     Not(IntExprRef expr) : expr(expr) {}
595     int evalInt() { return !expr->evalInt(); }
596     void dump(int level = 0);
597     bool isConstExpr() const { return expr->isConstExpr(); }
598 schoenebeck 2645 bool isPolyphonic() const { return expr->isPolyphonic(); }
599 schoenebeck 2581 };
600     typedef Ref<Not,Node> NotRef;
601    
602 schoenebeck 2935 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 schoenebeck 2581 class ParserContext : public VMParserContext {
614     public:
615     struct Error {
616     String txt;
617     int line;
618     };
619     typedef Error Warning;
620    
621     void* scanner;
622     std::istream* is;
623 schoenebeck 2588 std::vector<ParserIssue> vErrors;
624     std::vector<ParserIssue> vWarnings;
625     std::vector<ParserIssue> vIssues;
626 schoenebeck 3285 std::vector<CodeBlock> vPreprocessorComments;
627 schoenebeck 2581
628     std::set<String> builtinPreprocessorConditions;
629     std::set<String> userPreprocessorConditions;
630    
631     std::map<String,VariableRef> vartable;
632 schoenebeck 2951 std::map<String,StatementsRef> userFnTable;
633 schoenebeck 2581 int globalIntVarCount;
634     int globalStrVarCount;
635     int polyphonicIntVarCount;
636    
637     EventHandlersRef handlers;
638    
639     OnInitRef onInit;
640     OnNoteRef onNote;
641     OnReleaseRef onRelease;
642     OnControllerRef onController;
643    
644     ArrayList<int>* globalIntMemory;
645     ArrayList<String>* globalStrMemory;
646 schoenebeck 2588 int requiredMaxStackSize;
647 schoenebeck 2581
648     VMFunctionProvider* functionProvider;
649    
650     ExecContext* execContext;
651    
652     ParserContext(VMFunctionProvider* parent) :
653     scanner(NULL), is(NULL),
654     globalIntVarCount(0), globalStrVarCount(0), polyphonicIntVarCount(0),
655 schoenebeck 2588 globalIntMemory(NULL), globalStrMemory(NULL), requiredMaxStackSize(-1),
656     functionProvider(parent), execContext(NULL)
657 schoenebeck 2581 {
658     }
659 schoenebeck 2588 virtual ~ParserContext();
660 schoenebeck 2581 VariableRef globalVar(const String& name);
661     IntVariableRef globalIntVar(const String& name);
662     StringVariableRef globalStrVar(const String& name);
663     VariableRef variableByName(const String& name);
664 schoenebeck 2951 StatementsRef userFunctionByName(const String& name);
665 schoenebeck 2889 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 schoenebeck 3285 void addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn);
668 schoenebeck 2581 void createScanner(std::istream* is);
669     void destroyScanner();
670     bool setPreprocessorCondition(const char* name);
671     bool resetPreprocessorCondition(const char* name);
672     bool isPreprocessorConditionSet(const char* name);
673 schoenebeck 2588 std::vector<ParserIssue> issues() const OVERRIDE;
674     std::vector<ParserIssue> errors() const OVERRIDE;
675     std::vector<ParserIssue> warnings() const OVERRIDE;
676 schoenebeck 3285 std::vector<CodeBlock> preprocessorComments() const OVERRIDE;
677 schoenebeck 2588 VMEventHandler* eventHandler(uint index) OVERRIDE;
678     VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
679 schoenebeck 2594 void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
680     void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
681     void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
682 schoenebeck 2942 void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
683 schoenebeck 2581 };
684    
685     class ExecContext : public VMExecContext {
686     public:
687     struct StackFrame {
688     Statement* statement;
689     int subindex;
690    
691     StackFrame() {
692     statement = NULL;
693     subindex = -1;
694     }
695     };
696    
697     ArrayList<int> polyphonicIntMemory;
698     VMExecStatus_t status;
699 schoenebeck 3277 StmtFlags_t flags;
700 schoenebeck 2581 ArrayList<StackFrame> stack;
701     int stackFrame;
702     int suspendMicroseconds;
703 schoenebeck 3221 size_t instructionsCount;
704 schoenebeck 2581
705     ExecContext() :
706 schoenebeck 3277 status(VM_EXEC_NOT_RUNNING), flags(STMT_SUCCESS), stackFrame(-1),
707     suspendMicroseconds(0), instructionsCount(0) {}
708 schoenebeck 2581
709     virtual ~ExecContext() {}
710    
711     inline void pushStack(Statement* stmt) {
712     stackFrame++;
713     //printf("pushStack() -> %d\n", stackFrame);
714     if (stackFrame >= stack.size()) return;
715     stack[stackFrame].statement = stmt;
716     stack[stackFrame].subindex = 0;
717     }
718    
719     inline void popStack() {
720     stack[stackFrame].statement = NULL;
721     stack[stackFrame].subindex = -1;
722     stackFrame--;
723     //printf("popStack() -> %d\n", stackFrame);
724     }
725    
726     inline void reset() {
727     stack[0].statement = NULL;
728     stack[0].subindex = -1;
729     stackFrame = -1;
730 schoenebeck 3277 flags = STMT_SUCCESS;
731 schoenebeck 2581 }
732    
733 schoenebeck 2588 int suspensionTimeMicroseconds() const OVERRIDE {
734 schoenebeck 2581 return suspendMicroseconds;
735     }
736 schoenebeck 3207
737     void resetPolyphonicData() OVERRIDE {
738     if (polyphonicIntMemory.empty()) return;
739     memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int));
740     }
741 schoenebeck 3221
742     size_t instructionsPerformed() const OVERRIDE {
743     return instructionsCount;
744     }
745 schoenebeck 3277
746     void signalAbort() OVERRIDE {
747     flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED);
748     }
749 schoenebeck 3293
750     void forkTo(VMExecContext* ectx) const OVERRIDE;
751 schoenebeck 2581 };
752    
753     } // namespace LinuxSampler
754    
755     #endif // LS_INSTRPARSERTREE_H

  ViewVC Help
Powered by ViewVC