/[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 3729 - (hide annotations) (download) (as text)
Fri Jan 31 10:57:53 2020 UTC (4 years, 4 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 40183 byte(s)
NKSP parser: track code locations also by raw byte position and length.

* NKSP VM API: Added member variables 'firstByte' and 'lengthBytes' to
  struct 'CodeBlock'.

* NKSP Editor API: Added methods firstByte() and lengthBytes() to
  class 'VMSourceToken'.

* NKSP VM language parser: track all positions also by raw byte offset
  and length (along to the already existing tracking by line/column).

* NKSP editor syntax highlighting scanner: track all positions also by
  raw byte offset and length (along to the already existing tracking by
  line/column).

* Bumped version (2.1.1.svn45).

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

  ViewVC Help
Powered by ViewVC