/[svn]/linuxsampler/trunk/src/scriptvm/tree.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/tree.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3576 - (show annotations) (download) (as text)
Wed Aug 28 12:56:38 2019 UTC (4 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 35274 byte(s)
NKSP: Fixed negate operator which did not work with real numbers.

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

  ViewVC Help
Powered by ViewVC