/[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 3561 - (hide annotations) (download) (as text)
Fri Aug 23 11:44:00 2019 UTC (4 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 29178 byte(s)
NKSP: Added standard units support for numbers and final "!" operator:

* NKSP strictness: Variable names, function names and preprocessor condition
  names must start with a regular character (a-z or A-Z); starting them with
  a digit or underscore is no longer allowed.

* NKSP parser fix: equal comparison operator "=" and not equal comparison
  operator "#" must only accept integer operands.

* NKSP language: Implemented support for standard units like Hertz, seconds,
  Bel including support for metric unit prefixes; so one can now e.g.
  conveniently use numbers in scripts like "5us" meaning "5 microseconds",
  or e.g. "12kHz" meaning "12 kilo Hertz", or e.g. "-14mdB" meaning
  "minus 14 Millidecibel", or e.g. "28c" meaning "28 cents" (for tuning).

* NKSP language: Introduced "final" operator "!" which is specifically
  intended for synthesis parameter values to denote that the synthesis
  parameter value is intended to be the "final" value for that synthesis
  parameter that should explicitly be used by the engine and thus causing
  the sampler engine to ignore all other modulation sources for the same
  synthesis parameter (like e.g. LFO, EG); by simply prefixing a value,
  variable or formula with this new "!" operator the expression is marked as
  being "final".

* Bumped version (2.1.1.svn4).

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

  ViewVC Help
Powered by ViewVC