/[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 3595 - (hide annotations) (download) (as text)
Tue Sep 3 11:06:33 2019 UTC (4 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 38947 byte(s)
* Fixed compiler errors with GCC 8.x.
* Bumped version (2.1.1.svn16).

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

  ViewVC Help
Powered by ViewVC