/[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 3804 - (hide annotations) (download) (as text)
Thu Aug 6 12:15:02 2020 UTC (3 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 40754 byte(s)
NKSP: Fixed built-in exit() function to behave as return statement.

* VM API: Introduced new signal STMT_RETURN_SIGNALLED.

* NKSP exit() function: signal STMT_RETURN_SIGNALLED instead of
  STMT_ABORT_SIGNALLED.

* NKSP AST: Introduced common base class 'Subroutine' for 'EventHandler'
  and for new 'UserFunction' class.

* NKSP parser: Use 'UserFunction' class instead of 'Statements' class
  for user declared NKSP functions.

* ScriptVM: Handle new STMT_RETURN_SIGNALLED signal by unwinding the
  stack to previous, calling subroutine.

* NKSP tests: Added test cases for exit() acting as return statement.

* Bumped version (2.1.1.svn62).

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 3586 #include <assert.h>
25 schoenebeck 2581 #include "../common/global.h"
26     #include "../common/Ref.h"
27     #include "../common/ArrayList.h"
28 schoenebeck 3733 #include "../common/optional.h"
29 schoenebeck 2581 #include "common.h"
30    
31     namespace LinuxSampler {
32    
33     class ParserContext;
34     class ExecContext;
35    
36     enum StmtType_t {
37     STMT_LEAF,
38     STMT_LIST,
39     STMT_BRANCH,
40     STMT_LOOP,
41 schoenebeck 3260 STMT_SYNC,
42 schoenebeck 3311 STMT_NOOP,
43 schoenebeck 2581 };
44    
45 schoenebeck 3728 enum Qualifier_t {
46     QUALIFIER_NONE = 0,
47     QUALIFIER_CONST = 1,
48     QUALIFIER_POLYPHONIC = (1<<1),
49 schoenebeck 3733 QUALIFIER_PATCH = (1<<2),
50 schoenebeck 3728 };
51    
52 schoenebeck 3733 struct PatchVarBlock {
53     CodeBlock nameBlock;
54     optional<CodeBlock> exprBlock;
55     };
56    
57 schoenebeck 3583 /**
58     * Convenience function used for retrieving the (assumed) data type of a given
59     * script variable name.
60     *
61     * @param name - some script variable name (e.g. "$foo")
62     * @return variable's assumed data type (e.g. INT_EXPR for example above)
63     */
64     inline ExprType_t exprTypeOfVarName(const String& name) {
65     if (name.empty()) return (ExprType_t) -1;
66     const char prefix = name[0];
67     switch (prefix) {
68     case '$': return INT_EXPR;
69     case '%': return INT_ARR_EXPR;
70     case '~': return REAL_EXPR;
71     case '?': return REAL_ARR_EXPR;
72     case '@': return STRING_EXPR;
73     case '!': return STRING_ARR_EXPR;
74     }
75     return (ExprType_t) -1;
76     }
77    
78 schoenebeck 3586 inline ExprType_t scalarTypeOfArray(ExprType_t arrayType) {
79     if (arrayType == INT_ARR_EXPR) return INT_EXPR;
80     if (arrayType == REAL_ARR_EXPR) return REAL_EXPR;
81     if (arrayType == STRING_ARR_EXPR) return STRING_EXPR;
82     assert(false);
83     return EMPTY_EXPR; // just to shut up the compiler
84     }
85    
86 schoenebeck 3728 inline String qualifierStr(Qualifier_t qualifier) {
87     switch (qualifier) {
88     case QUALIFIER_NONE: return "none";
89     case QUALIFIER_CONST: return "const";
90     case QUALIFIER_POLYPHONIC: return "polyphonic";
91 schoenebeck 3733 case QUALIFIER_PATCH: return "patch";
92 schoenebeck 3728 }
93     return "unknown";
94     }
95    
96 schoenebeck 3585 /**
97     * Used by parser for parser error messages to provide a text with all data
98     * types accepted by the given built-in function @a fn for the respective
99     * function argument @a iArg.
100     */
101     String acceptedArgTypesStr(VMFunction* fn, vmint iArg);
102    
103 schoenebeck 2581 class Node {
104     public:
105     Node();
106     virtual ~Node();
107     virtual void dump(int level = 0) = 0;
108 schoenebeck 2645 virtual bool isPolyphonic() const = 0;
109 schoenebeck 2581 void printIndents(int n);
110     };
111     typedef Ref<Node> NodeRef;
112    
113     class Expression : virtual public VMExpr, virtual public Node {
114     public:
115     virtual ExprType_t exprType() const = 0;
116     virtual bool isConstExpr() const = 0;
117     virtual String evalCastToStr() = 0;
118     };
119     typedef Ref<Expression,Node> ExpressionRef;
120    
121 schoenebeck 3573 class Unit : virtual public VMUnit, virtual public Node {
122     StdUnit_t unit;
123 schoenebeck 2581 public:
124 schoenebeck 3581 Unit(StdUnit_t type) : unit(type) {}
125 schoenebeck 3573 StdUnit_t unitType() const OVERRIDE { return unit; }
126 schoenebeck 3581 static vmint convIntToUnitFactor(vmint iValue, VMUnit* srcUnit, VMUnit* dstUnit);
127     static vmint convIntToUnitFactor(vmint iValue, vmfloat srcFactor, vmfloat dstFactor);
128     static vmfloat convRealToUnitFactor(vmfloat fValue, VMUnit* srcUnit, VMUnit* dstUnit);
129     static vmfloat convRealToUnitFactor(vmfloat fValue, vmfloat srcFactor, vmfloat dstFactor);
130 schoenebeck 3573 };
131     typedef Ref<Unit,Node> UnitRef;
132    
133 schoenebeck 3582 class NumberExpr : virtual public Unit, virtual public VMNumberExpr, virtual public Expression {
134 schoenebeck 3573 public:
135     };
136 schoenebeck 3582 typedef Ref<NumberExpr,Node> NumberExprRef;
137 schoenebeck 3573
138 schoenebeck 3582 class IntExpr : virtual public NumberExpr, virtual public VMIntExpr {
139 schoenebeck 3573 public:
140 schoenebeck 3557 ExprType_t exprType() const OVERRIDE { return INT_EXPR; }
141 schoenebeck 3581 vmint evalIntToUnitFactor(vmfloat unitFactor);
142 schoenebeck 3557 String evalCastToStr() OVERRIDE;
143 schoenebeck 2581 };
144     typedef Ref<IntExpr,Node> IntExprRef;
145    
146 schoenebeck 3582 class RealExpr : virtual public NumberExpr, virtual public VMRealExpr {
147 schoenebeck 3056 public:
148 schoenebeck 3573 ExprType_t exprType() const OVERRIDE { return REAL_EXPR; }
149 schoenebeck 3581 vmfloat evalRealToUnitFactor(vmfloat unitFactor);
150 schoenebeck 3573 String evalCastToStr() OVERRIDE;
151     };
152     typedef Ref<RealExpr,Node> RealExprRef;
153    
154     class ArrayExpr : virtual public VMArrayExpr, virtual public Expression {
155     public:
156     };
157     typedef Ref<ArrayExpr,Node> ArrayExprRef;
158    
159     class IntArrayExpr : virtual public VMIntArrayExpr, virtual public ArrayExpr {
160     public:
161 schoenebeck 3557 ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; }
162     String evalCastToStr() OVERRIDE;
163 schoenebeck 3056 };
164 schoenebeck 3293 typedef Ref<IntArrayExpr,Node> IntArrayExprRef;
165 schoenebeck 3056
166 schoenebeck 3573 class RealArrayExpr : virtual public VMRealArrayExpr, virtual public ArrayExpr {
167     public:
168     ExprType_t exprType() const OVERRIDE { return REAL_ARR_EXPR; }
169     String evalCastToStr() OVERRIDE;
170     };
171     typedef Ref<RealArrayExpr,Node> RealArrayExprRef;
172    
173 schoenebeck 2581 class StringExpr : virtual public VMStringExpr, virtual public Expression {
174     public:
175 schoenebeck 3557 ExprType_t exprType() const OVERRIDE { return STRING_EXPR; }
176     String evalCastToStr() OVERRIDE { return evalStr(); }
177 schoenebeck 2581 };
178     typedef Ref<StringExpr,Node> StringExprRef;
179    
180 schoenebeck 3581 struct IntLitDef {
181     vmint value; //NOTE: sequence matters! Since this is usually initialized with VMIntExpr::evalInt() it should be before member unitFactor, since the latter is usually initialized with VMIntExpr::unitFactor() which does not evaluate the expression.
182     vmfloat unitFactor = VM_NO_FACTOR;
183     StdUnit_t unitType = VM_NO_UNIT;
184     bool isFinal;
185     };
186    
187 schoenebeck 3574 class IntLiteral FINAL : public IntExpr {
188 schoenebeck 3561 bool finalVal;
189 schoenebeck 3581 vmfloat unitPrefixFactor;
190     vmint value;
191 schoenebeck 3561 public:
192 schoenebeck 3581 IntLiteral(const IntLitDef& def);
193 schoenebeck 3557 vmint evalInt() OVERRIDE;
194 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return unitPrefixFactor; }
195 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
196     bool isConstExpr() const OVERRIDE { return true; }
197     bool isPolyphonic() const OVERRIDE { return false; }
198 schoenebeck 3561 bool isFinal() const OVERRIDE { return finalVal; }
199 schoenebeck 2581 };
200     typedef Ref<IntLiteral,Node> IntLiteralRef;
201    
202 schoenebeck 3581 struct RealLitDef {
203     vmfloat value; //NOTE: sequence matters! Since this is usually initialized with VMRealExpr::evalReal() it should be before member unitFactor, since the latter is usually initialized with VMRealExpr::unitFactor() which does not evaluate the expression.
204     vmfloat unitFactor = VM_NO_FACTOR;
205     StdUnit_t unitType = VM_NO_UNIT;
206     bool isFinal;
207     };
208    
209 schoenebeck 3574 class RealLiteral FINAL : public RealExpr {
210 schoenebeck 3573 bool finalVal;
211 schoenebeck 3581 vmfloat unitPrefixFactor;
212     vmfloat value;
213 schoenebeck 3573 public:
214 schoenebeck 3581 RealLiteral(const RealLitDef& def);
215 schoenebeck 3573 vmfloat evalReal() OVERRIDE;
216 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return unitPrefixFactor; }
217 schoenebeck 3573 void dump(int level = 0) OVERRIDE;
218     bool isConstExpr() const OVERRIDE { return true; }
219     bool isPolyphonic() const OVERRIDE { return false; }
220     bool isFinal() const OVERRIDE { return finalVal; }
221     };
222     typedef Ref<RealLiteral,Node> RealLiteralRef;
223    
224 schoenebeck 3574 class StringLiteral FINAL : public StringExpr {
225 schoenebeck 3581 String value;
226 schoenebeck 2581 public:
227     StringLiteral(const String& value) : value(value) { }
228 schoenebeck 3557 bool isConstExpr() const OVERRIDE { return true; }
229     void dump(int level = 0) OVERRIDE;
230     String evalStr() OVERRIDE { return value; }
231     bool isPolyphonic() const OVERRIDE { return false; }
232 schoenebeck 2581 };
233     typedef Ref<StringLiteral,Node> StringLiteralRef;
234    
235 schoenebeck 3574 class Args FINAL : virtual public VMFnArgs, virtual public Node {
236 schoenebeck 2581 public:
237     std::vector<ExpressionRef> args;
238     void add(ExpressionRef arg) { args.push_back(arg); }
239 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
240     vmint argsCount() const OVERRIDE { return (vmint) args.size(); }
241     VMExpr* arg(vmint i) OVERRIDE { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; }
242     bool isPolyphonic() const OVERRIDE;
243 schoenebeck 2581 };
244     typedef Ref<Args,Node> ArgsRef;
245    
246 schoenebeck 3581 struct VariableDecl {
247     ParserContext* ctx;
248     bool isPolyphonic;
249     bool isConst;
250     vmint elements = 1;
251     vmint memPos;
252     vmint unitFactorMemPos;
253     StdUnit_t unitType = VM_NO_UNIT;
254 schoenebeck 3595 bool isFinal;
255 schoenebeck 3581 };
256    
257 schoenebeck 2945 class Variable : virtual public VMVariable, virtual public Expression {
258 schoenebeck 2581 public:
259 schoenebeck 2945 bool isConstExpr() const OVERRIDE { return bConst; }
260     bool isAssignable() const OVERRIDE { return !bConst; }
261 schoenebeck 2581 virtual void assign(Expression* expr) = 0;
262 schoenebeck 2945 void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); }
263 schoenebeck 2581 protected:
264 schoenebeck 3581 Variable(const VariableDecl& decl);
265 schoenebeck 2581
266     ParserContext* context;
267 schoenebeck 3557 vmint memPos;
268 schoenebeck 2581 bool bConst;
269     };
270     typedef Ref<Variable,Node> VariableRef;
271    
272 schoenebeck 3582 class NumberVariable : public Variable, virtual public NumberExpr {
273 schoenebeck 2581 bool polyphonic;
274 schoenebeck 3561 bool finalVal;
275 schoenebeck 3581 protected:
276     vmint unitFactorMemPos;
277 schoenebeck 2581 public:
278 schoenebeck 3573 bool isPolyphonic() const OVERRIDE { return polyphonic; }
279     bool isFinal() const OVERRIDE { return finalVal; }
280 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE;
281 schoenebeck 3573 protected:
282 schoenebeck 3582 NumberVariable(const VariableDecl& decl);
283 schoenebeck 3573 };
284 schoenebeck 3582 typedef Ref<NumberVariable,Node> NumberVariableRef;
285 schoenebeck 3573
286 schoenebeck 3582 class IntVariable : public NumberVariable, virtual public IntExpr {
287 schoenebeck 3573 public:
288 schoenebeck 3581 IntVariable(const VariableDecl& decl);
289 schoenebeck 3557 void assign(Expression* expr) OVERRIDE;
290     vmint evalInt() OVERRIDE;
291     void dump(int level = 0) OVERRIDE;
292 schoenebeck 2581 };
293     typedef Ref<IntVariable,Node> IntVariableRef;
294    
295 schoenebeck 3582 class RealVariable : public NumberVariable, virtual public RealExpr {
296 schoenebeck 3573 public:
297 schoenebeck 3581 RealVariable(const VariableDecl& decl);
298 schoenebeck 3573 void assign(Expression* expr) OVERRIDE;
299     vmfloat evalReal() OVERRIDE;
300     void dump(int level = 0) OVERRIDE;
301     };
302     typedef Ref<RealVariable,Node> RealVariableRef;
303    
304 schoenebeck 3581 struct IntVarDef /* : VariableDecl*/ { //NOTE: derived, aggregate initializer-lists requires C++17
305 schoenebeck 3595 // additions for RealVarDef
306     vmint value = 0; //NOTE: sequence matters! Since this is usually initialized with VMIntExpr::evalInt() it should be before member unitFactor, since the latter is usually initialized with VMIntExpr::unitFactor() which does not evaluate the expression.
307     vmfloat unitFactor = VM_NO_FACTOR;
308 schoenebeck 3581 // copied from VariableDecl
309     ParserContext* ctx;
310     bool isPolyphonic;
311     bool isConst;
312     vmint elements = 1;
313     vmint memPos;
314     vmint unitFactorMemPos;
315     StdUnit_t unitType = VM_NO_UNIT;
316 schoenebeck 3595 bool isFinal;
317 schoenebeck 3581 };
318    
319 schoenebeck 3574 class ConstIntVariable FINAL : public IntVariable {
320 schoenebeck 3581 vmfloat unitPrefixFactor;
321     vmint value;
322 schoenebeck 2581 public:
323 schoenebeck 3581 ConstIntVariable(const IntVarDef& def);
324 schoenebeck 3557 void assign(Expression* expr) OVERRIDE;
325     vmint evalInt() OVERRIDE;
326 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return unitPrefixFactor; }
327 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
328 schoenebeck 2581 };
329     typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;
330    
331 schoenebeck 3581 struct RealVarDef /* : VariableDecl*/ { //NOTE: derived, aggregate initializer-lists requires C++17
332 schoenebeck 3595 // additions for RealVarDef
333     vmfloat value = vmfloat(0); //NOTE: sequence matters! Since this is usually initialized with VMRealExpr::evalReal() it should be before member unitFactor, since the latter is usually initialized with VMRealExpr::unitFactor() which does not evaluate the expression.
334     vmfloat unitFactor = VM_NO_FACTOR;
335 schoenebeck 3581 // copied from VariableDecl
336     ParserContext* ctx;
337     bool isPolyphonic;
338     bool isConst;
339     vmint elements = 1;
340     vmint memPos;
341     vmint unitFactorMemPos;
342     StdUnit_t unitType = VM_NO_UNIT;
343 schoenebeck 3595 bool isFinal;
344 schoenebeck 3581 };
345    
346 schoenebeck 3574 class ConstRealVariable FINAL : public RealVariable {
347 schoenebeck 3581 vmfloat unitPrefixFactor;
348     vmfloat value;
349 schoenebeck 3573 public:
350 schoenebeck 3581 ConstRealVariable(const RealVarDef& def);
351 schoenebeck 3573 void assign(Expression* expr) OVERRIDE;
352     vmfloat evalReal() OVERRIDE;
353 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return unitPrefixFactor; }
354 schoenebeck 3573 void dump(int level = 0) OVERRIDE;
355     };
356     typedef Ref<ConstRealVariable,Node> ConstRealVariableRef;
357    
358 schoenebeck 3574 class BuiltInIntVariable FINAL : public IntVariable {
359 schoenebeck 2594 String name;
360 schoenebeck 3557 VMIntPtr* ptr;
361 schoenebeck 2594 public:
362 schoenebeck 3557 BuiltInIntVariable(const String& name, VMIntPtr* ptr);
363     bool isAssignable() const OVERRIDE { return ptr->isAssignable(); }
364 schoenebeck 3054 void assign(Expression* expr) OVERRIDE;
365 schoenebeck 3557 vmint evalInt() OVERRIDE;
366 schoenebeck 3695 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
367 schoenebeck 3054 void dump(int level = 0) OVERRIDE;
368 schoenebeck 2594 };
369     typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef;
370    
371 schoenebeck 3574 class PolyphonicIntVariable FINAL : public IntVariable {
372 schoenebeck 2581 public:
373 schoenebeck 3581 PolyphonicIntVariable(const VariableDecl& decl);
374 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
375 schoenebeck 2581 };
376     typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
377    
378 schoenebeck 3574 class PolyphonicRealVariable FINAL : public RealVariable {
379 schoenebeck 3573 public:
380 schoenebeck 3581 PolyphonicRealVariable(const VariableDecl& decl);
381 schoenebeck 3573 void dump(int level = 0) OVERRIDE;
382     };
383     typedef Ref<PolyphonicRealVariable,Node> PolyphonicRealVariableRef;
384    
385 schoenebeck 3293 class IntArrayVariable : public Variable, virtual public IntArrayExpr {
386 schoenebeck 3557 ArrayList<vmint> values;
387 schoenebeck 3581 ArrayList<vmfloat> unitFactors;
388 schoenebeck 2581 public:
389 schoenebeck 3557 IntArrayVariable(ParserContext* ctx, vmint size);
390     IntArrayVariable(ParserContext* ctx, vmint size, ArgsRef values, bool _bConst = false);
391     void assign(Expression* expr) OVERRIDE {} // ignore scalar assignment
392     ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; }
393     virtual vmint arraySize() const OVERRIDE { return values.size(); }
394     virtual vmint evalIntElement(vmuint i) OVERRIDE;
395     virtual void assignIntElement(vmuint i, vmint value) OVERRIDE;
396 schoenebeck 3581 vmfloat unitFactorOfElement(vmuint i) const OVERRIDE;
397     void assignElementUnitFactor(vmuint i, vmfloat factor) OVERRIDE;
398 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
399     bool isPolyphonic() const OVERRIDE { return false; }
400 schoenebeck 2594 protected:
401     IntArrayVariable(ParserContext* ctx, bool bConst);
402     };
403     typedef Ref<IntArrayVariable,Node> IntArrayVariableRef;
404    
405 schoenebeck 3574 class RealArrayVariable FINAL : public Variable, virtual public RealArrayExpr {
406 schoenebeck 3573 ArrayList<vmfloat> values;
407 schoenebeck 3581 ArrayList<vmfloat> unitFactors;
408 schoenebeck 3573 public:
409     RealArrayVariable(ParserContext* ctx, vmint size);
410     RealArrayVariable(ParserContext* ctx, vmint size, ArgsRef values, bool _bConst = false);
411     void assign(Expression* expr) OVERRIDE {} // ignore scalar assignment
412     ExprType_t exprType() const OVERRIDE { return REAL_ARR_EXPR; }
413     virtual vmint arraySize() const OVERRIDE { return values.size(); }
414     virtual vmfloat evalRealElement(vmuint i) OVERRIDE;
415     virtual void assignRealElement(vmuint i, vmfloat value) OVERRIDE;
416 schoenebeck 3581 vmfloat unitFactorOfElement(vmuint i) const OVERRIDE;
417     void assignElementUnitFactor(vmuint i, vmfloat factor) OVERRIDE;
418 schoenebeck 3573 void dump(int level = 0) OVERRIDE;
419     bool isPolyphonic() const OVERRIDE { return false; }
420     protected:
421     RealArrayVariable(ParserContext* ctx, bool bConst);
422     };
423     typedef Ref<RealArrayVariable,Node> RealArrayVariableRef;
424    
425 schoenebeck 3574 class BuiltInIntArrayVariable FINAL : public IntArrayVariable {
426 schoenebeck 2594 String name;
427     VMInt8Array* array;
428     public:
429     BuiltInIntArrayVariable(const String& name, VMInt8Array* array);
430 schoenebeck 3557 vmint arraySize() const OVERRIDE { return array->size; }
431     vmint evalIntElement(vmuint i) OVERRIDE;
432 schoenebeck 3581 void assignIntElement(vmuint i, vmint value) OVERRIDE;
433     vmfloat unitFactorOfElement(vmuint i) const OVERRIDE { return VM_NO_FACTOR; }
434     void assignElementUnitFactor(vmuint i, vmfloat factor) OVERRIDE {}
435 schoenebeck 3253 bool isAssignable() const OVERRIDE { return !array->readonly; }
436 persson 3455 void dump(int level = 0) OVERRIDE;
437 schoenebeck 2581 };
438 schoenebeck 2594 typedef Ref<BuiltInIntArrayVariable,Node> BuiltInIntArrayVariableRef;
439 schoenebeck 2581
440 schoenebeck 3574 class IntArrayElement FINAL : public IntVariable {
441 schoenebeck 3293 IntArrayExprRef array;
442 schoenebeck 2581 IntExprRef index;
443 schoenebeck 3581 vmint currentIndex;
444 schoenebeck 2581 public:
445 schoenebeck 3293 IntArrayElement(IntArrayExprRef array, IntExprRef arrayIndex);
446 schoenebeck 3557 void assign(Expression* expr) OVERRIDE;
447     vmint evalInt() OVERRIDE;
448 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE;
449 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
450 schoenebeck 2581 };
451     typedef Ref<IntArrayElement,Node> IntArrayElementRef;
452    
453 schoenebeck 3574 class RealArrayElement FINAL : public RealVariable {
454 schoenebeck 3573 RealArrayExprRef array;
455     IntExprRef index;
456 schoenebeck 3581 vmint currentIndex;
457 schoenebeck 3573 public:
458     RealArrayElement(RealArrayExprRef array, IntExprRef arrayIndex);
459     void assign(Expression* expr) OVERRIDE;
460     vmfloat evalReal() OVERRIDE;
461 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE;
462 schoenebeck 3573 void dump(int level = 0) OVERRIDE;
463     };
464     typedef Ref<RealArrayElement,Node> RealArrayElementRef;
465    
466 schoenebeck 2581 class StringVariable : public Variable, virtual public StringExpr {
467     public:
468     StringVariable(ParserContext* ctx);
469 schoenebeck 3557 void assign(Expression* expr) OVERRIDE;
470     String evalStr() OVERRIDE;
471     void dump(int level = 0) OVERRIDE;
472     bool isPolyphonic() const OVERRIDE { return false; }
473 schoenebeck 2581 protected:
474     StringVariable(ParserContext* ctx, bool bConst);
475     };
476     typedef Ref<StringVariable,Node> StringVariableRef;
477    
478 schoenebeck 3574 class ConstStringVariable FINAL : public StringVariable {
479 schoenebeck 3581 String value;
480 schoenebeck 2581 public:
481     ConstStringVariable(ParserContext* ctx, String value = "");
482 schoenebeck 3557 void assign(Expression* expr) OVERRIDE;
483     String evalStr() OVERRIDE;
484     void dump(int level = 0) OVERRIDE;
485 schoenebeck 2581 };
486     typedef Ref<ConstStringVariable,Node> ConstStringVariableRef;
487    
488     class BinaryOp : virtual public Expression {
489     protected:
490     ExpressionRef lhs;
491     ExpressionRef rhs;
492     public:
493     BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }
494 schoenebeck 3557 bool isConstExpr() const OVERRIDE { return lhs->isConstExpr() && rhs->isConstExpr(); }
495     bool isPolyphonic() const OVERRIDE { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
496 schoenebeck 2581 };
497     typedef Ref<BinaryOp,Node> BinaryOpRef;
498    
499 schoenebeck 3582 class NumberBinaryOp : public BinaryOp, virtual public NumberExpr {
500 schoenebeck 2581 public:
501 schoenebeck 3582 NumberBinaryOp(NumberExprRef lhs, NumberExprRef rhs) : BinaryOp(lhs, rhs) { }
502 schoenebeck 3561 bool isFinal() const OVERRIDE;
503     };
504 schoenebeck 3582 typedef Ref<NumberBinaryOp,Node> NumberBinaryOpRef;
505 schoenebeck 3573
506 schoenebeck 3582 class IntBinaryOp : public NumberBinaryOp, virtual public IntExpr {
507 schoenebeck 3573 public:
508 schoenebeck 3582 IntBinaryOp(IntExprRef lhs, IntExprRef rhs) : NumberBinaryOp(lhs, rhs) { }
509 schoenebeck 3573 };
510 schoenebeck 3561 typedef Ref<IntBinaryOp,Node> IntBinaryOpRef;
511    
512 schoenebeck 3582 class VaritypeScalarBinaryOp : public NumberBinaryOp, virtual public IntExpr, virtual public RealExpr {
513 schoenebeck 3561 public:
514 schoenebeck 3582 VaritypeScalarBinaryOp(NumberExprRef lhs, NumberExprRef rhs) : NumberBinaryOp(lhs, rhs) { }
515 schoenebeck 3573 ExprType_t exprType() const OVERRIDE;
516     String evalCastToStr() OVERRIDE;
517     };
518     typedef Ref<VaritypeScalarBinaryOp,Node> VaritypeScalarBinaryOpRef;
519    
520 schoenebeck 3574 class Add FINAL : public VaritypeScalarBinaryOp {
521 schoenebeck 3573 public:
522 schoenebeck 3582 Add(NumberExprRef lhs, NumberExprRef rhs);
523 schoenebeck 3557 vmint evalInt() OVERRIDE;
524 schoenebeck 3573 vmfloat evalReal() OVERRIDE;
525 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE;
526 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
527 schoenebeck 2581 };
528     typedef Ref<Add,Node> AddRef;
529    
530 schoenebeck 3574 class Sub FINAL : public VaritypeScalarBinaryOp {
531 schoenebeck 2581 public:
532 schoenebeck 3582 Sub(NumberExprRef lhs, NumberExprRef rhs);
533 schoenebeck 3557 vmint evalInt() OVERRIDE;
534 schoenebeck 3573 vmfloat evalReal() OVERRIDE;
535 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE;
536 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
537 schoenebeck 2581 };
538     typedef Ref<Sub,Node> SubRef;
539    
540 schoenebeck 3574 class Mul FINAL : public VaritypeScalarBinaryOp {
541 schoenebeck 2581 public:
542 schoenebeck 3582 Mul(NumberExprRef lhs, NumberExprRef rhs);
543 schoenebeck 3557 vmint evalInt() OVERRIDE;
544 schoenebeck 3573 vmfloat evalReal() OVERRIDE;
545 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE;
546 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
547 schoenebeck 2581 };
548     typedef Ref<Mul,Node> MulRef;
549    
550 schoenebeck 3574 class Div FINAL : public VaritypeScalarBinaryOp {
551 schoenebeck 2581 public:
552 schoenebeck 3582 Div(NumberExprRef lhs, NumberExprRef rhs);
553 schoenebeck 3557 vmint evalInt() OVERRIDE;
554 schoenebeck 3573 vmfloat evalReal() OVERRIDE;
555 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
556 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE;
557 schoenebeck 2581 };
558     typedef Ref<Div,Node> DivRef;
559    
560 schoenebeck 3574 class Mod FINAL : public IntBinaryOp {
561 schoenebeck 2581 public:
562 schoenebeck 3581 Mod(IntExprRef lhs, IntExprRef rhs) : IntBinaryOp(lhs, rhs), Unit(VM_NO_UNIT) { }
563 schoenebeck 3557 vmint evalInt() OVERRIDE;
564 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
565 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
566 schoenebeck 2581 };
567     typedef Ref<Mod,Node> ModRef;
568    
569     class Statement : virtual public Node {
570     public:
571     virtual StmtType_t statementType() const = 0;
572     };
573     typedef Ref<Statement,Node> StatementRef;
574    
575     // Just used by parser to avoid "not a statement" parser warning, will be
576     // filtered out by parser. So it will not be part of the VM tree after parsing.
577 schoenebeck 3574 class NoOperation FINAL : public Statement {
578 schoenebeck 2581 public:
579     NoOperation() : Statement() {}
580 schoenebeck 3557 StmtType_t statementType() const OVERRIDE { return STMT_NOOP; }
581     void dump(int level = 0) OVERRIDE {}
582     bool isPolyphonic() const OVERRIDE { return false; }
583 schoenebeck 2581 };
584     typedef Ref<NoOperation,Node> NoOperationRef;
585    
586     bool isNoOperation(StatementRef statement);
587    
588     class LeafStatement : public Statement {
589     public:
590     virtual StmtFlags_t exec() = 0;
591 schoenebeck 3557 StmtType_t statementType() const OVERRIDE { return STMT_LEAF; }
592 schoenebeck 2581 };
593     typedef Ref<LeafStatement,Node> LeafStatementRef;
594    
595     class Statements : public Statement {
596     std::vector<StatementRef> args;
597     public:
598     void add(StatementRef arg) { args.push_back(arg); }
599 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
600     StmtType_t statementType() const OVERRIDE { return STMT_LIST; }
601 schoenebeck 2581 virtual Statement* statement(uint i);
602 schoenebeck 3557 bool isPolyphonic() const OVERRIDE;
603 schoenebeck 2581 };
604     typedef Ref<Statements,Node> StatementsRef;
605    
606     class BranchStatement : public Statement {
607     public:
608 schoenebeck 3557 StmtType_t statementType() const OVERRIDE { return STMT_BRANCH; }
609     virtual vmint evalBranch() = 0;
610     virtual Statements* branch(vmuint i) const = 0;
611 schoenebeck 2581 };
612    
613 schoenebeck 3574 class DynamicVariableCall FINAL : public Variable, virtual public IntExpr, virtual public StringExpr, virtual public IntArrayExpr {
614 schoenebeck 2942 VMDynVar* dynVar;
615     String varName;
616     public:
617     DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v);
618     ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); }
619     bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); }
620     bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); }
621     bool isPolyphonic() const OVERRIDE { return false; }
622 schoenebeck 2945 void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); }
623 schoenebeck 3118 VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); }
624 schoenebeck 3557 vmint evalInt() OVERRIDE;
625 schoenebeck 2942 String evalStr() OVERRIDE;
626     String evalCastToStr() OVERRIDE;
627 schoenebeck 3557 vmint arraySize() const OVERRIDE { return dynVar->asIntArray()->arraySize(); }
628     vmint evalIntElement(vmuint i) OVERRIDE { return dynVar->asIntArray()->evalIntElement(i); }
629     void assignIntElement(vmuint i, vmint value) OVERRIDE { return dynVar->asIntArray()->assignIntElement(i, value); }
630 schoenebeck 3581 vmfloat unitFactorOfElement(vmuint i) const OVERRIDE { return VM_NO_FACTOR; }
631     void assignElementUnitFactor(vmuint i, vmfloat factor) OVERRIDE {}
632 schoenebeck 2942 void dump(int level = 0) OVERRIDE;
633 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
634 schoenebeck 3561 bool isFinal() const OVERRIDE { return false; }
635 schoenebeck 2942 };
636     typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef;
637    
638 schoenebeck 3573 class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public RealExpr, virtual public StringExpr {
639 schoenebeck 2581 String functionName;
640     ArgsRef args;
641     VMFunction* fn;
642 schoenebeck 3581 VMFnResult* result;
643 schoenebeck 2581 public:
644 schoenebeck 3581 FunctionCall(const char* function, ArgsRef args, VMFunction* fn);
645 schoenebeck 3747 virtual ~FunctionCall();
646 schoenebeck 3060 void dump(int level = 0) OVERRIDE;
647     StmtFlags_t exec() OVERRIDE;
648 schoenebeck 3557 vmint evalInt() OVERRIDE;
649 schoenebeck 3573 vmfloat evalReal() OVERRIDE;
650 schoenebeck 3056 VMIntArrayExpr* asIntArray() const OVERRIDE;
651 schoenebeck 3573 VMRealArrayExpr* asRealArray() const OVERRIDE;
652 schoenebeck 3060 String evalStr() OVERRIDE;
653     bool isConstExpr() const OVERRIDE { return false; }
654     ExprType_t exprType() const OVERRIDE;
655     String evalCastToStr() OVERRIDE;
656     bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); }
657 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE;
658     bool isFinal() const OVERRIDE;
659 schoenebeck 2581 protected:
660     VMFnResult* execVMFn();
661     };
662     typedef Ref<FunctionCall,Node> FunctionCallRef;
663    
664 schoenebeck 3574 class NoFunctionCall FINAL : public FunctionCall {
665 schoenebeck 3311 public:
666 schoenebeck 3581 NoFunctionCall() : FunctionCall("nothing", new Args, NULL), Unit(VM_NO_UNIT) {}
667 schoenebeck 3557 StmtType_t statementType() const OVERRIDE { return STMT_NOOP; }
668 schoenebeck 3311 };
669     typedef Ref<NoFunctionCall,Node> NoFunctionCallRef;
670    
671 schoenebeck 3804 class Subroutine : public Statements {
672 schoenebeck 2581 StatementsRef statements;
673 schoenebeck 3804 public:
674     Subroutine(StatementsRef statements);
675     Statement* statement(uint i) OVERRIDE { return statements->statement(i); }
676     void dump(int level = 0) OVERRIDE;
677     };
678     typedef Ref<Subroutine,Node> SubroutineRef;
679    
680     class UserFunction : public Subroutine {
681     public:
682     UserFunction(StatementsRef statements);
683     };
684     typedef Ref<UserFunction,Node> UserFunctionRef;
685    
686     class EventHandler : public Subroutine, virtual public VMEventHandler {
687 schoenebeck 2645 bool usingPolyphonics;
688 schoenebeck 2581 public:
689 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
690 schoenebeck 2645 EventHandler(StatementsRef statements);
691 schoenebeck 3557 bool isPolyphonic() const OVERRIDE { return usingPolyphonics; }
692 schoenebeck 2581 };
693     typedef Ref<EventHandler,Node> EventHandlerRef;
694    
695 schoenebeck 3574 class OnNote FINAL : public EventHandler {
696 schoenebeck 2581 public:
697     OnNote(StatementsRef statements) : EventHandler(statements) {}
698 schoenebeck 3557 VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_NOTE; }
699     String eventHandlerName() const OVERRIDE { return "note"; }
700 schoenebeck 2581 };
701     typedef Ref<OnNote,Node> OnNoteRef;
702    
703 schoenebeck 3574 class OnInit FINAL : public EventHandler {
704 schoenebeck 2581 public:
705     OnInit(StatementsRef statements) : EventHandler(statements) {}
706 schoenebeck 3557 VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_INIT; }
707     String eventHandlerName() const OVERRIDE { return "init"; }
708 schoenebeck 2581 };
709     typedef Ref<OnInit,Node> OnInitRef;
710    
711 schoenebeck 3574 class OnRelease FINAL : public EventHandler {
712 schoenebeck 2581 public:
713     OnRelease(StatementsRef statements) : EventHandler(statements) {}
714 schoenebeck 3557 VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_RELEASE; }
715     String eventHandlerName() const OVERRIDE { return "release"; }
716 schoenebeck 2581 };
717     typedef Ref<OnRelease,Node> OnReleaseRef;
718    
719 schoenebeck 3574 class OnController FINAL : public EventHandler {
720 schoenebeck 2581 public:
721     OnController(StatementsRef statements) : EventHandler(statements) {}
722 schoenebeck 3557 VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_CONTROLLER; }
723     String eventHandlerName() const OVERRIDE { return "controller"; }
724 schoenebeck 2581 };
725     typedef Ref<OnController,Node> OnControllerRef;
726    
727 schoenebeck 3690 class OnRpn FINAL : public EventHandler {
728     public:
729     OnRpn(StatementsRef statements) : EventHandler(statements) {}
730     VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_RPN; }
731     String eventHandlerName() const OVERRIDE { return "rpn"; }
732     };
733     typedef Ref<OnRpn,Node> OnRpnRef;
734    
735     class OnNrpn FINAL : public EventHandler {
736     public:
737     OnNrpn(StatementsRef statements) : EventHandler(statements) {}
738     VMEventHandlerType_t eventHandlerType() const OVERRIDE { return VM_EVENT_HANDLER_NRPN; }
739     String eventHandlerName() const OVERRIDE { return "nrpn"; }
740     };
741     typedef Ref<OnNrpn,Node> OnNrpnRef;
742    
743 schoenebeck 3574 class EventHandlers FINAL : virtual public Node {
744 schoenebeck 2581 std::vector<EventHandlerRef> args;
745     public:
746     EventHandlers();
747     ~EventHandlers();
748     void add(EventHandlerRef arg);
749 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
750 schoenebeck 2581 EventHandler* eventHandlerByName(const String& name) const;
751     EventHandler* eventHandler(uint index) const;
752 schoenebeck 3054 inline uint size() const { return (int) args.size(); }
753 schoenebeck 3557 bool isPolyphonic() const OVERRIDE;
754 schoenebeck 2581 };
755     typedef Ref<EventHandlers,Node> EventHandlersRef;
756    
757 schoenebeck 3574 class Assignment FINAL : public LeafStatement {
758 schoenebeck 2581 protected:
759     VariableRef variable;
760     ExpressionRef value;
761     public:
762     Assignment(VariableRef variable, ExpressionRef value);
763 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
764     StmtFlags_t exec() OVERRIDE;
765     bool isPolyphonic() const OVERRIDE { return (variable && variable->isPolyphonic()) || (value && value->isPolyphonic()); }
766 schoenebeck 2581 };
767     typedef Ref<Assignment,Node> AssignmentRef;
768    
769 schoenebeck 3574 class If FINAL : public BranchStatement {
770 schoenebeck 2581 IntExprRef condition;
771     StatementsRef ifStatements;
772     StatementsRef elseStatements;
773     public:
774     If(IntExprRef condition, StatementsRef ifStatements, StatementsRef elseStatements) :
775     condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { }
776     If(IntExprRef condition, StatementsRef statements) :
777     condition(condition), ifStatements(statements) { }
778 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
779     vmint evalBranch() OVERRIDE;
780     Statements* branch(vmuint i) const OVERRIDE;
781     bool isPolyphonic() const OVERRIDE;
782 schoenebeck 2581 };
783     typedef Ref<If,Node> IfRef;
784    
785 schoenebeck 3574 struct CaseBranch FINAL {
786 schoenebeck 2581 IntExprRef from;
787     IntExprRef to;
788     StatementsRef statements;
789     };
790     typedef std::vector<CaseBranch> CaseBranches;
791    
792 schoenebeck 3574 class SelectCase FINAL : public BranchStatement {
793 schoenebeck 2581 IntExprRef select;
794     CaseBranches branches;
795     public:
796     SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { }
797 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
798     vmint evalBranch() OVERRIDE;
799     Statements* branch(vmuint i) const OVERRIDE;
800     bool isPolyphonic() const OVERRIDE;
801 schoenebeck 2581 };
802     typedef Ref<SelectCase,Node> SelectCaseRef;
803    
804 schoenebeck 3574 class While FINAL : public Statement {
805 schoenebeck 2581 IntExprRef m_condition;
806     StatementsRef m_statements;
807     public:
808     While(IntExprRef condition, StatementsRef statements) :
809     m_condition(condition), m_statements(statements) {}
810 schoenebeck 3557 StmtType_t statementType() const OVERRIDE { return STMT_LOOP; }
811     void dump(int level = 0) OVERRIDE;
812 schoenebeck 2581 bool evalLoopStartCondition();
813     Statements* statements() const;
814 schoenebeck 3557 bool isPolyphonic() const OVERRIDE { return m_condition->isPolyphonic() || m_statements->isPolyphonic(); }
815 schoenebeck 2581 };
816    
817 schoenebeck 3574 class SyncBlock FINAL : public Statement {
818 schoenebeck 3260 StatementsRef m_statements;
819     public:
820     SyncBlock(StatementsRef statements) : m_statements(statements) {}
821 schoenebeck 3557 StmtType_t statementType() const OVERRIDE { return STMT_SYNC; }
822     void dump(int level = 0) OVERRIDE;
823 schoenebeck 3260 Statements* statements() const;
824 schoenebeck 3557 bool isPolyphonic() const OVERRIDE { return m_statements->isPolyphonic(); }
825 schoenebeck 3260 };
826     typedef Ref<SyncBlock,Node> SyncBlockRef;
827    
828 schoenebeck 3576 class Neg FINAL : virtual public IntExpr, virtual public RealExpr {
829 schoenebeck 3582 NumberExprRef expr;
830 schoenebeck 2581 public:
831 schoenebeck 3582 Neg(NumberExprRef expr) : Unit(expr->unitType()), expr(expr) { }
832 schoenebeck 3576 ExprType_t exprType() const OVERRIDE { return expr->exprType(); }
833     vmint evalInt() OVERRIDE { return (expr) ? -(expr->asInt()->evalInt()) : 0; }
834     vmfloat evalReal() OVERRIDE { return (expr) ? -(expr->asReal()->evalReal()) : vmfloat(0); }
835     String evalCastToStr() OVERRIDE;
836 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
837     bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); }
838     bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); }
839 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return expr->unitFactor(); }
840 schoenebeck 3561 bool isFinal() const OVERRIDE { return expr->isFinal(); }
841 schoenebeck 2581 };
842     typedef Ref<Neg,Node> NegRef;
843    
844 schoenebeck 3574 class ConcatString FINAL : public StringExpr {
845 schoenebeck 2581 ExpressionRef lhs;
846     ExpressionRef rhs;
847     public:
848     ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {}
849 schoenebeck 3557 String evalStr() OVERRIDE;
850     void dump(int level = 0) OVERRIDE;
851     bool isConstExpr() const OVERRIDE;
852     bool isPolyphonic() const OVERRIDE { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
853 schoenebeck 2581 };
854     typedef Ref<ConcatString,Node> ConcatStringRef;
855    
856 schoenebeck 3574 class Relation FINAL : public IntExpr {
857 schoenebeck 2581 public:
858     enum Type {
859     LESS_THAN,
860     GREATER_THAN,
861     LESS_OR_EQUAL,
862     GREATER_OR_EQUAL,
863     EQUAL,
864     NOT_EQUAL
865     };
866 schoenebeck 3581 Relation(ExpressionRef lhs, Type type, ExpressionRef rhs);
867 schoenebeck 3557 vmint evalInt() OVERRIDE;
868     void dump(int level = 0) OVERRIDE;
869     bool isConstExpr() const OVERRIDE;
870     bool isPolyphonic() const OVERRIDE { return lhs->isPolyphonic() || rhs->isPolyphonic(); }
871 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
872 schoenebeck 3561 bool isFinal() const OVERRIDE { return false; }
873 schoenebeck 2581 private:
874 schoenebeck 3573 ExpressionRef lhs;
875     ExpressionRef rhs;
876 schoenebeck 2581 Type type;
877     };
878     typedef Ref<Relation,Node> RelationRef;
879    
880 schoenebeck 3574 class Or FINAL : public IntBinaryOp {
881 schoenebeck 2581 public:
882 schoenebeck 3581 Or(IntExprRef lhs, IntExprRef rhs) : IntBinaryOp(lhs,rhs), Unit(VM_NO_UNIT) {}
883 schoenebeck 3557 vmint evalInt() OVERRIDE;
884 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
885 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
886 schoenebeck 2581 };
887     typedef Ref<Or,Node> OrRef;
888    
889 schoenebeck 3574 class BitwiseOr FINAL : public IntBinaryOp {
890 schoenebeck 2935 public:
891 schoenebeck 3581 BitwiseOr(IntExprRef lhs, IntExprRef rhs) : IntBinaryOp(lhs,rhs), Unit(VM_NO_UNIT) {}
892 schoenebeck 3557 vmint evalInt() OVERRIDE;
893     void dump(int level = 0) OVERRIDE;
894 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
895 schoenebeck 2935 };
896     typedef Ref<BitwiseOr,Node> BitwiseOrRef;
897    
898 schoenebeck 3574 class And FINAL : public IntBinaryOp {
899 schoenebeck 2581 public:
900 schoenebeck 3581 And(IntExprRef lhs, IntExprRef rhs) : IntBinaryOp(lhs,rhs), Unit(VM_NO_UNIT) {}
901 schoenebeck 3557 vmint evalInt() OVERRIDE;
902 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
903 schoenebeck 3557 void dump(int level = 0) OVERRIDE;
904 schoenebeck 2581 };
905     typedef Ref<And,Node> AndRef;
906    
907 schoenebeck 3574 class BitwiseAnd FINAL : public IntBinaryOp {
908 schoenebeck 2935 public:
909 schoenebeck 3581 BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : IntBinaryOp(lhs,rhs), Unit(VM_NO_UNIT) {}
910 schoenebeck 3557 vmint evalInt() OVERRIDE;
911     void dump(int level = 0) OVERRIDE;
912 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
913 schoenebeck 2935 };
914     typedef Ref<BitwiseAnd,Node> BitwiseAndRef;
915    
916 schoenebeck 3574 class Not FINAL : virtual public IntExpr {
917 schoenebeck 2581 IntExprRef expr;
918     public:
919 schoenebeck 3581 Not(IntExprRef expr) : Unit(VM_NO_UNIT), expr(expr) {}
920 schoenebeck 3557 vmint evalInt() OVERRIDE { return !expr->evalInt(); }
921     void dump(int level = 0) OVERRIDE;
922     bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); }
923     bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); }
924 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
925 schoenebeck 3561 bool isFinal() const OVERRIDE { return expr->isFinal(); }
926 schoenebeck 2581 };
927     typedef Ref<Not,Node> NotRef;
928    
929 schoenebeck 3574 class BitwiseNot FINAL : virtual public IntExpr {
930 schoenebeck 2935 IntExprRef expr;
931     public:
932 schoenebeck 3581 BitwiseNot(IntExprRef expr) : Unit(VM_NO_UNIT), expr(expr) {}
933 schoenebeck 3557 vmint evalInt() OVERRIDE { return ~expr->evalInt(); }
934     void dump(int level = 0) OVERRIDE;
935     bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); }
936     bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); }
937 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; }
938 schoenebeck 3561 bool isFinal() const OVERRIDE { return expr->isFinal(); }
939 schoenebeck 2935 };
940     typedef Ref<BitwiseNot,Node> BitwiseNotRef;
941    
942 schoenebeck 3574 class Final FINAL : virtual public IntExpr, virtual public RealExpr {
943 schoenebeck 3582 NumberExprRef expr;
944 schoenebeck 3561 public:
945 schoenebeck 3582 Final(NumberExprRef expr) : Unit(expr->unitType()), expr(expr) {}
946 schoenebeck 3573 ExprType_t exprType() const OVERRIDE { return expr->exprType(); }
947     vmint evalInt() OVERRIDE { return expr->asInt()->evalInt(); }
948     vmfloat evalReal() OVERRIDE { return expr->asReal()->evalReal(); }
949     String evalCastToStr() OVERRIDE;
950 schoenebeck 3561 void dump(int level = 0) OVERRIDE;
951     bool isConstExpr() const OVERRIDE { return expr->isConstExpr(); }
952     bool isPolyphonic() const OVERRIDE { return expr->isPolyphonic(); }
953 schoenebeck 3581 vmfloat unitFactor() const OVERRIDE { return expr->unitFactor(); }
954 schoenebeck 3561 bool isFinal() const OVERRIDE { return true; }
955     };
956     typedef Ref<Final,Node> FinalRef;
957    
958 schoenebeck 3574 class ParserContext FINAL : public VMParserContext {
959 schoenebeck 2581 public:
960     struct Error {
961     String txt;
962     int line;
963     };
964     typedef Error Warning;
965    
966     void* scanner;
967     std::istream* is;
968 schoenebeck 3729 int nbytes;
969 schoenebeck 2588 std::vector<ParserIssue> vErrors;
970     std::vector<ParserIssue> vWarnings;
971     std::vector<ParserIssue> vIssues;
972 schoenebeck 3285 std::vector<CodeBlock> vPreprocessorComments;
973 schoenebeck 3733 std::map<String,PatchVarBlock> patchVars;
974 schoenebeck 2581
975     std::set<String> builtinPreprocessorConditions;
976     std::set<String> userPreprocessorConditions;
977    
978     std::map<String,VariableRef> vartable;
979 schoenebeck 3804 std::map<String,UserFunctionRef> userFnTable;
980 schoenebeck 3557 vmint globalIntVarCount;
981 schoenebeck 3573 vmint globalRealVarCount;
982 schoenebeck 3557 vmint globalStrVarCount;
983 schoenebeck 3581 vmint globalUnitFactorCount;
984 schoenebeck 3557 vmint polyphonicIntVarCount;
985 schoenebeck 3573 vmint polyphonicRealVarCount;
986 schoenebeck 3581 vmint polyphonicUnitFactorCount;
987 schoenebeck 2581
988     EventHandlersRef handlers;
989    
990     OnInitRef onInit;
991     OnNoteRef onNote;
992     OnReleaseRef onRelease;
993     OnControllerRef onController;
994 schoenebeck 3690 OnRpnRef onRpn;
995     OnNrpnRef onNrpn;
996 schoenebeck 2581
997 schoenebeck 3557 ArrayList<vmint>* globalIntMemory;
998 schoenebeck 3573 ArrayList<vmfloat>* globalRealMemory;
999 schoenebeck 2581 ArrayList<String>* globalStrMemory;
1000 schoenebeck 3581 ArrayList<vmfloat>* globalUnitFactorMemory;
1001 schoenebeck 3557 vmint requiredMaxStackSize;
1002 schoenebeck 2581
1003     VMFunctionProvider* functionProvider;
1004    
1005     ExecContext* execContext;
1006    
1007     ParserContext(VMFunctionProvider* parent) :
1008 schoenebeck 3729 scanner(NULL), is(NULL), nbytes(0),
1009 schoenebeck 3573 globalIntVarCount(0), globalRealVarCount(0), globalStrVarCount(0),
1010 schoenebeck 3581 globalUnitFactorCount(0),
1011 schoenebeck 3573 polyphonicIntVarCount(0), polyphonicRealVarCount(0),
1012 schoenebeck 3581 polyphonicUnitFactorCount(0),
1013 schoenebeck 3573 globalIntMemory(NULL), globalRealMemory(NULL), globalStrMemory(NULL),
1014 schoenebeck 3581 globalUnitFactorMemory(NULL),
1015 schoenebeck 3573 requiredMaxStackSize(-1),
1016 schoenebeck 2588 functionProvider(parent), execContext(NULL)
1017 schoenebeck 2581 {
1018     }
1019 schoenebeck 2588 virtual ~ParserContext();
1020 schoenebeck 2581 VariableRef globalVar(const String& name);
1021     IntVariableRef globalIntVar(const String& name);
1022 schoenebeck 3573 RealVariableRef globalRealVar(const String& name);
1023 schoenebeck 2581 StringVariableRef globalStrVar(const String& name);
1024     VariableRef variableByName(const String& name);
1025 schoenebeck 3804 UserFunctionRef userFunctionByName(const String& name);
1026 schoenebeck 3729 void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn,
1027     int firstByte, int lengthBytes, const char* txt);
1028     void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn,
1029     int firstByte, int lengthBytes, const char* txt);
1030     void addPreprocessorComment(int firstLine, int lastLine, int firstColumn,
1031     int lastColumn, int firstByte, int lengthBytes);
1032 schoenebeck 2581 void createScanner(std::istream* is);
1033     void destroyScanner();
1034     bool setPreprocessorCondition(const char* name);
1035     bool resetPreprocessorCondition(const char* name);
1036     bool isPreprocessorConditionSet(const char* name);
1037 schoenebeck 2588 std::vector<ParserIssue> issues() const OVERRIDE;
1038     std::vector<ParserIssue> errors() const OVERRIDE;
1039     std::vector<ParserIssue> warnings() const OVERRIDE;
1040 schoenebeck 3285 std::vector<CodeBlock> preprocessorComments() const OVERRIDE;
1041 schoenebeck 2588 VMEventHandler* eventHandler(uint index) OVERRIDE;
1042     VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
1043 schoenebeck 3557 void registerBuiltInConstIntVariables(const std::map<String,vmint>& vars);
1044 schoenebeck 3590 void registerBuiltInConstRealVariables(const std::map<String,vmfloat>& vars);
1045 schoenebeck 3557 void registerBuiltInIntVariables(const std::map<String,VMIntPtr*>& vars);
1046 schoenebeck 2594 void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars);
1047 schoenebeck 2942 void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars);
1048 schoenebeck 2581 };
1049    
1050 schoenebeck 3574 class ExecContext FINAL : public VMExecContext {
1051 schoenebeck 2581 public:
1052     struct StackFrame {
1053     Statement* statement;
1054     int subindex;
1055    
1056     StackFrame() {
1057     statement = NULL;
1058     subindex = -1;
1059     }
1060     };
1061    
1062 schoenebeck 3557 ArrayList<vmint> polyphonicIntMemory;
1063 schoenebeck 3573 ArrayList<vmfloat> polyphonicRealMemory;
1064 schoenebeck 3581 ArrayList<vmfloat> polyphonicUnitFactorMemory;
1065 schoenebeck 2581 VMExecStatus_t status;
1066 schoenebeck 3277 StmtFlags_t flags;
1067 schoenebeck 2581 ArrayList<StackFrame> stack;
1068     int stackFrame;
1069 schoenebeck 3557 vmint suspendMicroseconds;
1070 schoenebeck 3221 size_t instructionsCount;
1071 schoenebeck 3551 struct ExitRes {
1072     Expression* value;
1073     IntLiteral intLiteral;
1074 schoenebeck 3575 RealLiteral realLiteral;
1075 schoenebeck 3551 StringLiteral stringLiteral;
1076 schoenebeck 2581
1077 schoenebeck 3581 ExitRes() :
1078     intLiteral({ .value = 0 }), realLiteral({ .value = 0.0 }),
1079     stringLiteral("") { }
1080 schoenebeck 3551 } exitRes;
1081 schoenebeck 2581
1082 schoenebeck 3551 ExecContext();
1083 schoenebeck 2581 virtual ~ExecContext() {}
1084    
1085     inline void pushStack(Statement* stmt) {
1086     stackFrame++;
1087     //printf("pushStack() -> %d\n", stackFrame);
1088     if (stackFrame >= stack.size()) return;
1089     stack[stackFrame].statement = stmt;
1090     stack[stackFrame].subindex = 0;
1091     }
1092    
1093     inline void popStack() {
1094     stack[stackFrame].statement = NULL;
1095     stack[stackFrame].subindex = -1;
1096     stackFrame--;
1097     //printf("popStack() -> %d\n", stackFrame);
1098     }
1099    
1100     inline void reset() {
1101     stack[0].statement = NULL;
1102     stack[0].subindex = -1;
1103     stackFrame = -1;
1104 schoenebeck 3277 flags = STMT_SUCCESS;
1105 schoenebeck 2581 }
1106    
1107 schoenebeck 3551 inline void clearExitRes() {
1108     exitRes.value = NULL;
1109     }
1110    
1111 schoenebeck 3557 vmint suspensionTimeMicroseconds() const OVERRIDE {
1112 schoenebeck 2581 return suspendMicroseconds;
1113     }
1114 schoenebeck 3207
1115     void resetPolyphonicData() OVERRIDE {
1116 schoenebeck 3573 if (!polyphonicIntMemory.empty())
1117     memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(vmint));
1118     if (!polyphonicRealMemory.empty())
1119     memset(&polyphonicRealMemory[0], 0, polyphonicRealMemory.size() * sizeof(vmfloat));
1120 schoenebeck 3581 if (!polyphonicUnitFactorMemory.empty()) {
1121     const vmint sz = polyphonicUnitFactorMemory.size();
1122     for (vmint i = 0; i < sz; ++i)
1123     polyphonicUnitFactorMemory[i] = VM_NO_FACTOR;
1124     }
1125 schoenebeck 3207 }
1126 schoenebeck 3221
1127     size_t instructionsPerformed() const OVERRIDE {
1128     return instructionsCount;
1129     }
1130 schoenebeck 3277
1131     void signalAbort() OVERRIDE {
1132     flags = StmtFlags_t(flags | STMT_ABORT_SIGNALLED);
1133     }
1134 schoenebeck 3293
1135     void forkTo(VMExecContext* ectx) const OVERRIDE;
1136 schoenebeck 3551
1137     VMExpr* exitResult() OVERRIDE {
1138     return exitRes.value;
1139     }
1140 schoenebeck 2581 };
1141    
1142     } // namespace LinuxSampler
1143    
1144     #endif // LS_INSTRPARSERTREE_H

  ViewVC Help
Powered by ViewVC