/[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 3557 - (hide annotations) (download) (as text)
Sun Aug 18 00:06:04 2019 UTC (4 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 26049 byte(s)
* NKSP: Introducing 64 bit support for NKSP integer scripts
  variables (declare $foo).
* Require C++11 compiler support.
* Autoconf: Added m4/ax_cxx_compile_stdcxx.m4 macro which is used
  for checking in configure for C++11 support (as mandatory
  requirement) and automatically adds compiler argument if required
  (e.g. -std=C++11).
* Bumped version (2.1.1.svn3).

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

  ViewVC Help
Powered by ViewVC