/[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 3285 - (hide annotations) (download) (as text)
Thu Jun 22 10:45:38 2017 UTC (6 years, 9 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 24081 byte(s)
* NKSP: Print a time stamp along to each call of built-in function
  "message()".
* ScriptVM API: Added VMParserContext::preprocessorComments() which
  allows to retrieve all code blocks filtered out by the
  preprocessor.
* Bumped version (2.0.0.svn63).

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 3056 /*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 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 2619 class IntArrayVariable : public Variable, virtual public VMIntArrayExpr {
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     IntArrayVariableRef array;
209     IntExprRef index;
210     public:
211     IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex);
212     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 2942 class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr {
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 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     void dump(int level = 0) OVERRIDE;
351     };
352     typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
353    
354 schoenebeck 2581 class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
355     String functionName;
356     ArgsRef args;
357     VMFunction* fn;
358     public:
359     FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :
360     functionName(function), args(args), fn(fn) { }
361 schoenebeck 3060 void dump(int level = 0) OVERRIDE;
362     StmtFlags_t exec() OVERRIDE;
363     int evalInt() OVERRIDE;
364 schoenebeck 3056 VMIntArrayExpr* asIntArray() const OVERRIDE;
365 schoenebeck 3060 String evalStr() OVERRIDE;
366     bool isConstExpr() const OVERRIDE { return false; }
367     ExprType_t exprType() const OVERRIDE;
368     String evalCastToStr() OVERRIDE;
369     bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); }
370 schoenebeck 2581 protected:
371     VMFnResult* execVMFn();
372     };
373     typedef Ref<FunctionCall,Node> FunctionCallRef;
374    
375     class EventHandler : virtual public Statements, virtual public VMEventHandler {
376     StatementsRef statements;
377 schoenebeck 2645 bool usingPolyphonics;
378 schoenebeck 2581 public:
379     void dump(int level = 0);
380     StmtFlags_t exec();
381 schoenebeck 2645 EventHandler(StatementsRef statements);
382 schoenebeck 2581 Statement* statement(uint i) { return statements->statement(i); }
383 schoenebeck 2645 bool isPolyphonic() const { return usingPolyphonics; }
384 schoenebeck 2581 };
385     typedef Ref<EventHandler,Node> EventHandlerRef;
386    
387     class OnNote : public EventHandler {
388     public:
389     OnNote(StatementsRef statements) : EventHandler(statements) {}
390 schoenebeck 2879 VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_NOTE; }
391 schoenebeck 2581 String eventHandlerName() const { return "note"; }
392     };
393     typedef Ref<OnNote,Node> OnNoteRef;
394    
395     class OnInit : public EventHandler {
396     public:
397     OnInit(StatementsRef statements) : EventHandler(statements) {}
398 schoenebeck 2879 VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_INIT; }
399 schoenebeck 2581 String eventHandlerName() const { return "init"; }
400     };
401     typedef Ref<OnInit,Node> OnInitRef;
402    
403     class OnRelease : public EventHandler {
404     public:
405     OnRelease(StatementsRef statements) : EventHandler(statements) {}
406 schoenebeck 2879 VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_RELEASE; }
407 schoenebeck 2581 String eventHandlerName() const { return "release"; }
408     };
409     typedef Ref<OnRelease,Node> OnReleaseRef;
410    
411     class OnController : public EventHandler {
412     public:
413     OnController(StatementsRef statements) : EventHandler(statements) {}
414 schoenebeck 2879 VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_CONTROLLER; }
415 schoenebeck 2581 String eventHandlerName() const { return "controller"; }
416     };
417     typedef Ref<OnController,Node> OnControllerRef;
418    
419     class EventHandlers : virtual public Node {
420     std::vector<EventHandlerRef> args;
421     public:
422     EventHandlers();
423     ~EventHandlers();
424     void add(EventHandlerRef arg);
425     void dump(int level = 0);
426     int evalInt() { return 0; }
427     EventHandler* eventHandlerByName(const String& name) const;
428     EventHandler* eventHandler(uint index) const;
429 schoenebeck 3054 inline uint size() const { return (int) args.size(); }
430 schoenebeck 2645 bool isPolyphonic() const;
431 schoenebeck 2581 };
432     typedef Ref<EventHandlers,Node> EventHandlersRef;
433    
434     class Assignment : public LeafStatement {
435     protected:
436     VariableRef variable;
437     ExpressionRef value;
438     public:
439     Assignment(VariableRef variable, ExpressionRef value);
440     void dump(int level = 0);
441     StmtFlags_t exec();
442 schoenebeck 2871 bool isPolyphonic() const { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); }
443 schoenebeck 2581 };
444     typedef Ref<Assignment,Node> AssignmentRef;
445    
446     class If : public BranchStatement {
447     IntExprRef condition;
448     StatementsRef ifStatements;
449     StatementsRef elseStatements;
450     public:
451     If(IntExprRef condition, StatementsRef ifStatements, StatementsRef elseStatements) :
452     condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { }
453     If(IntExprRef condition, StatementsRef statements) :
454     condition(condition), ifStatements(statements) { }
455     void dump(int level = 0);
456     int evalBranch();
457     Statements* branch(uint i) const;
458 schoenebeck 2645 bool isPolyphonic() const;
459 schoenebeck 2581 };
460     typedef Ref<If,Node> IfRef;
461    
462     struct CaseBranch {
463     IntExprRef from;
464     IntExprRef to;
465     StatementsRef statements;
466     };
467    
468     typedef std::vector<CaseBranch> CaseBranches;
469    
470     class SelectCase : public BranchStatement {
471     IntExprRef select;
472     CaseBranches branches;
473     public:
474     SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { }
475     void dump(int level = 0);
476     int evalBranch();
477     Statements* branch(uint i) const;
478     //void addBranch(IntExprRef condition, StatementsRef statements);
479     //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);
480     //void addBranch(CaseBranchRef branch);
481     //void addBranches(CaseBranchesRef branches);
482 schoenebeck 2645 bool isPolyphonic() const;
483 schoenebeck 2581 };
484     typedef Ref<SelectCase,Node> SelectCaseRef;
485    
486     class While : public Statement {
487     IntExprRef m_condition;
488     StatementsRef m_statements;
489     public:
490     While(IntExprRef condition, StatementsRef statements) :
491     m_condition(condition), m_statements(statements) {}
492     StmtType_t statementType() const { return STMT_LOOP; }
493     void dump(int level = 0);
494     bool evalLoopStartCondition();
495     Statements* statements() const;
496 schoenebeck 2645 bool isPolyphonic() const { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
497 schoenebeck 2581 };
498    
499 schoenebeck 3260 class SyncBlock : public Statement {
500     StatementsRef m_statements;
501     public:
502     SyncBlock(StatementsRef statements) : m_statements(statements) {}
503     StmtType_t statementType() const { return STMT_SYNC; }
504     void dump(int level = 0);
505     Statements* statements() const;
506     bool isPolyphonic() const { return m_statements->isPolyphonic(); }
507     };
508     typedef Ref<SyncBlock,Node> SyncBlockRef;
509    
510 schoenebeck 2581 class Neg : public IntExpr {
511     IntExprRef expr;
512     public:
513     Neg(IntExprRef expr) : expr(expr) { }
514     int evalInt() { return (expr) ? -expr->evalInt() : 0; }
515     void dump(int level = 0);
516     bool isConstExpr() const { return expr->isConstExpr(); }
517 schoenebeck 2645 bool isPolyphonic() const { return expr->isPolyphonic(); }
518 schoenebeck 2581 };
519     typedef Ref<Neg,Node> NegRef;
520    
521     class ConcatString : public StringExpr {
522     ExpressionRef lhs;
523     ExpressionRef rhs;
524     public:
525     ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {}
526     String evalStr();
527     void dump(int level = 0);
528     bool isConstExpr() const;
529 schoenebeck 2645 bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
530 schoenebeck 2581 };
531     typedef Ref<ConcatString,Node> ConcatStringRef;
532    
533     class Relation : public IntExpr {
534     public:
535     enum Type {
536     LESS_THAN,
537     GREATER_THAN,
538     LESS_OR_EQUAL,
539     GREATER_OR_EQUAL,
540     EQUAL,
541     NOT_EQUAL
542     };
543     Relation(IntExprRef lhs, Type type, IntExprRef rhs) :
544     lhs(lhs), rhs(rhs), type(type) {}
545     int evalInt();
546     void dump(int level = 0);
547     bool isConstExpr() const;
548 schoenebeck 2645 bool isPolyphonic() const { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
549 schoenebeck 2581 private:
550     IntExprRef lhs;
551     IntExprRef rhs;
552     Type type;
553     };
554     typedef Ref<Relation,Node> RelationRef;
555    
556     class Or : virtual public BinaryOp, virtual public IntExpr {
557     public:
558     Or(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
559     int evalInt();
560     void dump(int level = 0);
561     };
562     typedef Ref<Or,Node> OrRef;
563    
564 schoenebeck 2935 class BitwiseOr : virtual public BinaryOp, virtual public IntExpr {
565     public:
566     BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
567     int evalInt();
568     void dump(int level = 0);
569     };
570     typedef Ref<BitwiseOr,Node> BitwiseOrRef;
571    
572 schoenebeck 2581 class And : virtual public BinaryOp, virtual public IntExpr {
573     public:
574     And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
575     int evalInt();
576     void dump(int level = 0);
577     };
578     typedef Ref<And,Node> AndRef;
579    
580 schoenebeck 2935 class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr {
581     public:
582     BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
583     int evalInt();
584     void dump(int level = 0);
585     };
586     typedef Ref<BitwiseAnd,Node> BitwiseAndRef;
587    
588 schoenebeck 2581 class Not : virtual public IntExpr {
589     IntExprRef expr;
590     public:
591     Not(IntExprRef expr) : expr(expr) {}
592     int evalInt() { return !expr->evalInt(); }
593     void dump(int level = 0);
594     bool isConstExpr() const { return expr->isConstExpr(); }
595 schoenebeck 2645 bool isPolyphonic() const { return expr->isPolyphonic(); }
596 schoenebeck 2581 };
597     typedef Ref<Not,Node> NotRef;
598    
599 schoenebeck 2935 class BitwiseNot : virtual public IntExpr {
600     IntExprRef expr;
601     public:
602     BitwiseNot(IntExprRef expr) : expr(expr) {}
603     int evalInt() { return ~expr->evalInt(); }
604     void dump(int level = 0);
605     bool isConstExpr() const { return expr->isConstExpr(); }
606     bool isPolyphonic() const { return expr->isPolyphonic(); }
607     };
608     typedef Ref<BitwiseNot,Node> BitwiseNotRef;
609    
610 schoenebeck 2581 class ParserContext : public VMParserContext {
611     public:
612     struct Error {
613     String txt;
614     int line;
615     };
616     typedef Error Warning;
617    
618     void* scanner;
619     std::istream* is;
620 schoenebeck 2588 std::vector<ParserIssue> vErrors;
621     std::vector<ParserIssue> vWarnings;
622     std::vector<ParserIssue> vIssues;
623 schoenebeck 3285 std::vector<CodeBlock> vPreprocessorComments;
624 schoenebeck 2581
625     std::set<String> builtinPreprocessorConditions;
626     std::set<String> userPreprocessorConditions;
627    
628     std::map<String,VariableRef> vartable;
629 schoenebeck 2951 std::map<String,StatementsRef> userFnTable;
630 schoenebeck 2581 int globalIntVarCount;
631     int globalStrVarCount;
632     int polyphonicIntVarCount;
633    
634     EventHandlersRef handlers;
635    
636     OnInitRef onInit;
637     OnNoteRef onNote;
638     OnReleaseRef onRelease;
639     OnControllerRef onController;
640    
641     ArrayList<int>* globalIntMemory;
642     ArrayList<String>* globalStrMemory;
643 schoenebeck 2588 int requiredMaxStackSize;
644 schoenebeck 2581
645     VMFunctionProvider* functionProvider;
646    
647     ExecContext* execContext;
648    
649     ParserContext(VMFunctionProvider* parent) :
650     scanner(NULL), is(NULL),
651     globalIntVarCount(0), globalStrVarCount(0), polyphonicIntVarCount(0),
652 schoenebeck 2588 globalIntMemory(NULL), globalStrMemory(NULL), requiredMaxStackSize(-1),
653     functionProvider(parent), execContext(NULL)
654 schoenebeck 2581 {
655     }
656 schoenebeck 2588 virtual ~ParserContext();
657 schoenebeck 2581 VariableRef globalVar(const String& name);
658     IntVariableRef globalIntVar(const String& name);
659     StringVariableRef globalStrVar(const String& name);
660     VariableRef variableByName(const String& name);
661 schoenebeck 2951 StatementsRef userFunctionByName(const String& name);
662 schoenebeck 2889 void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
663     void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt);
664 schoenebeck 3285 void addPreprocessorComment(int firstLine, int lastLine, int firstColumn, int lastColumn);
665 schoenebeck 2581 void createScanner(std::istream* is);
666     void destroyScanner();
667     bool setPreprocessorCondition(const char* name);
668     bool resetPreprocessorCondition(const char* name);
669     bool isPreprocessorConditionSet(const char* name);
670 schoenebeck 2588 std::vector<ParserIssue> issues() const OVERRIDE;
671     std::vector<ParserIssue> errors() const OVERRIDE;
672     std::vector<ParserIssue> warnings() const OVERRIDE;
673 schoenebeck 3285 std::vector<CodeBlock> preprocessorComments() const OVERRIDE;
674 schoenebeck 2588 VMEventHandler* eventHandler(uint index) OVERRIDE;
675     VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
676 schoenebeck 2594 void registerBuiltInConstIntVariables(const std::map<String,int>& vars);
677     void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars);
678     void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
679 schoenebeck 2942 void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
680 schoenebeck 2581 };
681    
682     class ExecContext : public VMExecContext {
683     public:
684     struct StackFrame {
685     Statement* statement;
686     int subindex;
687    
688     StackFrame() {
689     statement = NULL;
690     subindex = -1;
691     }
692     };
693    
694     ArrayList<int> polyphonicIntMemory;
695     VMExecStatus_t status;
696 schoenebeck 3277 StmtFlags_t flags;
697 schoenebeck 2581 ArrayList<StackFrame> stack;
698     int stackFrame;
699     int suspendMicroseconds;
700 schoenebeck 3221 size_t instructionsCount;
701 schoenebeck 2581
702     ExecContext() :
703 schoenebeck 3277 status(VM_EXEC_NOT_RUNNING), flags(STMT_SUCCESS), stackFrame(-1),
704     suspendMicroseconds(0), instructionsCount(0) {}
705 schoenebeck 2581
706     virtual ~ExecContext() {}
707    
708     inline void pushStack(Statement* stmt) {
709     stackFrame++;
710     //printf("pushStack() -> %d\n", stackFrame);
711     if (stackFrame >= stack.size()) return;
712     stack[stackFrame].statement = stmt;
713     stack[stackFrame].subindex = 0;
714     }
715    
716     inline void popStack() {
717     stack[stackFrame].statement = NULL;
718     stack[stackFrame].subindex = -1;
719     stackFrame--;
720     //printf("popStack() -> %d\n", stackFrame);
721     }
722    
723     inline void reset() {
724     stack[0].statement = NULL;
725     stack[0].subindex = -1;
726     stackFrame = -1;
727 schoenebeck 3277 flags = STMT_SUCCESS;
728 schoenebeck 2581 }
729    
730 schoenebeck 2588 int suspensionTimeMicroseconds() const OVERRIDE {
731 schoenebeck 2581 return suspendMicroseconds;
732     }
733 schoenebeck 3207
734     void resetPolyphonicData() OVERRIDE {
735     if (polyphonicIntMemory.empty()) return;
736     memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int));
737     }
738 schoenebeck 3221
739     size_t instructionsPerformed() const OVERRIDE {
740     return instructionsCount;
741     }
742 schoenebeck 3277
743     void signalAbort() OVERRIDE {
744     flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED);
745     }
746 schoenebeck 2581 };
747    
748     } // namespace LinuxSampler
749    
750     #endif // LS_INSTRPARSERTREE_H

  ViewVC Help
Powered by ViewVC