/[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 3583 - (hide annotations) (download) (as text)
Fri Aug 30 12:39:18 2019 UTC (4 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 38299 byte(s)
Moved function exprTypeOfVarName() from public API file common.h to
implementation internal file tree.h (since this function is only
intended to be called by the generated NKSP parser).

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

  ViewVC Help
Powered by ViewVC