/[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 3585 - (hide annotations) (download) (as text)
Fri Aug 30 17:51:24 2019 UTC (4 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 38544 byte(s)
NKSP: Cleanup regarding multiple data types for built-in function args.

* NKSP VM API cleanup: Get rid of legacy method
  VMFunction::argType(vmint iArg) which was already superseded by its new
  replacement VMFunction::acceptsArgType(vmint iArg, ExprType_t type).

* NKSP parser: if wrong argument type was passed to a built-in function and
  that built-in function accepts more than one data type for the argument,
  then show all supported data types as parser error message.

* Bumped version (2.1.1.svn10).

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

  ViewVC Help
Powered by ViewVC