/[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 2588 - (show annotations) (download) (as text)
Sun Jun 1 14:44:38 2014 UTC (9 years, 9 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 17590 byte(s)
* ScriptVM: refactoring and fixes.

1 /* -*- c++ -*-
2 *
3 * Copyright (c) 2014 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 "../common/global.h"
24 #include "../common/Ref.h"
25 #include "../common/ArrayList.h"
26 #include "common.h"
27
28 namespace LinuxSampler {
29
30 class ParserContext;
31 class ExecContext;
32
33 enum StmtType_t {
34 STMT_LEAF,
35 STMT_LIST,
36 STMT_BRANCH,
37 STMT_LOOP,
38 };
39
40 class Node {
41 public:
42 Node();
43 virtual ~Node();
44 virtual void dump(int level = 0) = 0;
45 void printIndents(int n);
46 };
47 typedef Ref<Node> NodeRef;
48
49 class Expression : virtual public VMExpr, virtual public Node {
50 public:
51 virtual ExprType_t exprType() const = 0;
52 virtual bool isConstExpr() const = 0;
53 virtual String evalCastToStr() = 0;
54 };
55 typedef Ref<Expression,Node> ExpressionRef;
56
57 class IntExpr : virtual public VMIntExpr, virtual public Expression {
58 public:
59 ExprType_t exprType() const { return INT_EXPR; }
60 virtual int evalInt() = 0;
61 String evalCastToStr();
62 };
63 typedef Ref<IntExpr,Node> IntExprRef;
64
65 class StringExpr : virtual public VMStringExpr, virtual public Expression {
66 public:
67 ExprType_t exprType() const { return STRING_EXPR; }
68 virtual String evalStr() = 0;
69 String evalCastToStr() { return evalStr(); }
70 };
71 typedef Ref<StringExpr,Node> StringExprRef;
72
73 class IntLiteral : virtual public IntExpr {
74 int value;
75 public:
76 IntLiteral(int value) : value(value) { }
77 int evalInt();
78 void dump(int level = 0);
79 bool isConstExpr() const { return true; }
80 };
81 typedef Ref<IntLiteral,Node> IntLiteralRef;
82
83 class StringLiteral : virtual public StringExpr {
84 public:
85 String value;
86 StringLiteral(const String& value) : value(value) { }
87 bool isConstExpr() const { return true; }
88 void dump(int level = 0);
89 String evalStr() { return value; }
90 };
91 typedef Ref<StringLiteral,Node> StringLiteralRef;
92
93 class Args : virtual public VMFnArgs, virtual public Node {
94 public:
95 std::vector<ExpressionRef> args;
96 void add(ExpressionRef arg) { args.push_back(arg); }
97 void dump(int level = 0);
98 int argsCount() const { return args.size(); }
99 VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; }
100 };
101 typedef Ref<Args,Node> ArgsRef;
102
103 class Variable : virtual public Expression {
104 public:
105 virtual bool isConstExpr() const { return bConst; }
106 virtual void assign(Expression* expr) = 0;
107 protected:
108 Variable(ParserContext* ctx, int _memPos, bool _bConst)
109 : context(ctx), memPos(_memPos), bConst(_bConst) {}
110
111 ParserContext* context;
112 int memPos;
113 bool bConst;
114 };
115 typedef Ref<Variable,Node> VariableRef;
116
117 class IntVariable : public Variable, virtual public IntExpr {
118 bool polyphonic;
119 public:
120 IntVariable(ParserContext* ctx);
121 void assign(Expression* expr);
122 int evalInt();
123 void dump(int level = 0);
124 bool isPolyphonic() const { return polyphonic; }
125 protected:
126 IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size = 1);
127 };
128 typedef Ref<IntVariable,Node> IntVariableRef;
129
130 class ConstIntVariable : public IntVariable {
131 public:
132 int value;
133
134 ConstIntVariable(int value);
135 //ConstIntVariable(ParserContext* ctx, int value = 0);
136 void assign(Expression* expr);
137 int evalInt();
138 void dump(int level = 0);
139 };
140 typedef Ref<ConstIntVariable,Node> ConstIntVariableRef;
141
142 class PolyphonicIntVariable : public IntVariable {
143 public:
144 PolyphonicIntVariable(ParserContext* ctx);
145 void dump(int level = 0);
146 };
147 typedef Ref<PolyphonicIntVariable,Node> PolyphonicIntVariableRef;
148
149 class IntArrayVariable : public Variable {
150 ArrayList<int> values;
151 public:
152 IntArrayVariable(ParserContext* ctx, int size);
153 IntArrayVariable(ParserContext* ctx, int size, ArgsRef values);
154 void assign(Expression* expr) {} // ignore scalar assignment
155 String evalCastToStr() { return ""; } // ignore cast to string
156 ExprType_t exprType() const { return INT_ARR_EXPR; }
157 const int arraySize() const { return values.size(); }
158 int evalIntElement(uint i);
159 void assignIntElement(uint i, int value);
160 void dump(int level = 0);
161 };
162 typedef Ref<IntArrayVariable,Node> IntArrayVariableRef;
163
164 class IntArrayElement : public IntVariable {
165 IntArrayVariableRef array;
166 IntExprRef index;
167 public:
168 IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex);
169 void assign(Expression* expr);
170 int evalInt();
171 void dump(int level = 0);
172 };
173 typedef Ref<IntArrayElement,Node> IntArrayElementRef;
174
175 class StringVariable : public Variable, virtual public StringExpr {
176 public:
177 StringVariable(ParserContext* ctx);
178 void assign(Expression* expr);
179 String evalStr();
180 void dump(int level = 0);
181 protected:
182 StringVariable(ParserContext* ctx, bool bConst);
183 };
184 typedef Ref<StringVariable,Node> StringVariableRef;
185
186 class ConstStringVariable : public StringVariable {
187 public:
188 String value;
189
190 ConstStringVariable(ParserContext* ctx, String value = "");
191 void assign(Expression* expr);
192 String evalStr();
193 void dump(int level = 0);
194 };
195 typedef Ref<ConstStringVariable,Node> ConstStringVariableRef;
196
197 class BinaryOp : virtual public Expression {
198 protected:
199 ExpressionRef lhs;
200 ExpressionRef rhs;
201 public:
202 BinaryOp(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) { }
203 bool isConstExpr() const { return lhs->isConstExpr() && rhs->isConstExpr(); }
204 };
205 typedef Ref<BinaryOp,Node> BinaryOpRef;
206
207 class Add : virtual public BinaryOp, virtual public IntExpr {
208 public:
209 Add(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
210 int evalInt();
211 void dump(int level = 0);
212 };
213 typedef Ref<Add,Node> AddRef;
214
215 class Sub : virtual public BinaryOp, virtual public IntExpr {
216 public:
217 Sub(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
218 int evalInt();
219 void dump(int level = 0);
220 };
221 typedef Ref<Sub,Node> SubRef;
222
223 class Mul : virtual public BinaryOp, virtual public IntExpr {
224 public:
225 Mul(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
226 int evalInt();
227 void dump(int level = 0);
228 };
229 typedef Ref<Mul,Node> MulRef;
230
231 class Div : virtual public BinaryOp, virtual public IntExpr {
232 public:
233 Div(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
234 int evalInt();
235 void dump(int level = 0);
236 };
237 typedef Ref<Div,Node> DivRef;
238
239 class Mod : virtual public BinaryOp, virtual public IntExpr {
240 public:
241 Mod(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs, rhs) { }
242 int evalInt();
243 void dump(int level = 0);
244 };
245 typedef Ref<Mod,Node> ModRef;
246
247 class Statement : virtual public Node {
248 public:
249 virtual StmtType_t statementType() const = 0;
250 };
251 typedef Ref<Statement,Node> StatementRef;
252
253 // Just used by parser to avoid "not a statement" parser warning, will be
254 // filtered out by parser. So it will not be part of the VM tree after parsing.
255 class NoOperation : public Statement {
256 public:
257 NoOperation() : Statement() {}
258 StmtType_t statementType() const { return STMT_LEAF; }
259 void dump(int level = 0) {}
260 };
261 typedef Ref<NoOperation,Node> NoOperationRef;
262
263 bool isNoOperation(StatementRef statement);
264
265 class LeafStatement : public Statement {
266 public:
267 virtual StmtFlags_t exec() = 0;
268 virtual StmtType_t statementType() const { return STMT_LEAF; }
269 };
270 typedef Ref<LeafStatement,Node> LeafStatementRef;
271
272 class Statements : public Statement {
273 std::vector<StatementRef> args;
274 public:
275 void add(StatementRef arg) { args.push_back(arg); }
276 void dump(int level = 0);
277 StmtType_t statementType() const { return STMT_LIST; }
278 virtual Statement* statement(uint i);
279 };
280 typedef Ref<Statements,Node> StatementsRef;
281
282 class BranchStatement : public Statement {
283 public:
284 StmtType_t statementType() const { return STMT_BRANCH; }
285 virtual int evalBranch() = 0;
286 virtual Statements* branch(uint i) const = 0;
287 };
288
289 class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr {
290 String functionName;
291 ArgsRef args;
292 VMFunction* fn;
293 public:
294 FunctionCall(const char* function, ArgsRef args, VMFunction* fn) :
295 functionName(function), args(args), fn(fn) { }
296 void dump(int level = 0);
297 StmtFlags_t exec();
298 int evalInt();
299 String evalStr();
300 bool isConstExpr() const { return false; }
301 ExprType_t exprType() const;
302 String evalCastToStr();
303 protected:
304 VMFnResult* execVMFn();
305 };
306 typedef Ref<FunctionCall,Node> FunctionCallRef;
307
308 class EventHandler : virtual public Statements, virtual public VMEventHandler {
309 StatementsRef statements;
310 public:
311 void dump(int level = 0);
312 StmtFlags_t exec();
313 EventHandler(StatementsRef statements) { this->statements = statements; }
314 Statement* statement(uint i) { return statements->statement(i); }
315 };
316 typedef Ref<EventHandler,Node> EventHandlerRef;
317
318 class OnNote : public EventHandler {
319 public:
320 OnNote(StatementsRef statements) : EventHandler(statements) {}
321 String eventHandlerName() const { return "note"; }
322 };
323 typedef Ref<OnNote,Node> OnNoteRef;
324
325 class OnInit : public EventHandler {
326 public:
327 OnInit(StatementsRef statements) : EventHandler(statements) {}
328 String eventHandlerName() const { return "init"; }
329 };
330 typedef Ref<OnInit,Node> OnInitRef;
331
332 class OnRelease : public EventHandler {
333 public:
334 OnRelease(StatementsRef statements) : EventHandler(statements) {}
335 String eventHandlerName() const { return "release"; }
336 };
337 typedef Ref<OnRelease,Node> OnReleaseRef;
338
339 class OnController : public EventHandler {
340 public:
341 OnController(StatementsRef statements) : EventHandler(statements) {}
342 String eventHandlerName() const { return "controller"; }
343 };
344 typedef Ref<OnController,Node> OnControllerRef;
345
346 class EventHandlers : virtual public Node {
347 std::vector<EventHandlerRef> args;
348 public:
349 EventHandlers();
350 ~EventHandlers();
351 void add(EventHandlerRef arg);
352 void dump(int level = 0);
353 int evalInt() { return 0; }
354 EventHandler* eventHandlerByName(const String& name) const;
355 EventHandler* eventHandler(uint index) const;
356 inline uint size() const { return args.size(); }
357 };
358 typedef Ref<EventHandlers,Node> EventHandlersRef;
359
360 class Assignment : public LeafStatement {
361 protected:
362 VariableRef variable;
363 ExpressionRef value;
364 public:
365 Assignment(VariableRef variable, ExpressionRef value);
366 void dump(int level = 0);
367 StmtFlags_t exec();
368 };
369 typedef Ref<Assignment,Node> AssignmentRef;
370
371 class If : public BranchStatement {
372 IntExprRef condition;
373 StatementsRef ifStatements;
374 StatementsRef elseStatements;
375 public:
376 If(IntExprRef condition, StatementsRef ifStatements, StatementsRef elseStatements) :
377 condition(condition), ifStatements(ifStatements), elseStatements(elseStatements) { }
378 If(IntExprRef condition, StatementsRef statements) :
379 condition(condition), ifStatements(statements) { }
380 void dump(int level = 0);
381 int evalBranch();
382 Statements* branch(uint i) const;
383 };
384 typedef Ref<If,Node> IfRef;
385
386 struct CaseBranch {
387 IntExprRef from;
388 IntExprRef to;
389 StatementsRef statements;
390 };
391
392 typedef std::vector<CaseBranch> CaseBranches;
393
394 class SelectCase : public BranchStatement {
395 IntExprRef select;
396 CaseBranches branches;
397 public:
398 SelectCase(IntExprRef select, const CaseBranches& branches) : select(select), branches(branches) { }
399 void dump(int level = 0);
400 int evalBranch();
401 Statements* branch(uint i) const;
402 //void addBranch(IntExprRef condition, StatementsRef statements);
403 //void addBranch(IntExprRef from, IntExprRef to, StatementsRef statements);
404 //void addBranch(CaseBranchRef branch);
405 //void addBranches(CaseBranchesRef branches);
406 };
407 typedef Ref<SelectCase,Node> SelectCaseRef;
408
409 class While : public Statement {
410 IntExprRef m_condition;
411 StatementsRef m_statements;
412 public:
413 While(IntExprRef condition, StatementsRef statements) :
414 m_condition(condition), m_statements(statements) {}
415 StmtType_t statementType() const { return STMT_LOOP; }
416 void dump(int level = 0);
417 bool evalLoopStartCondition();
418 Statements* statements() const;
419 };
420
421 class Neg : public IntExpr {
422 IntExprRef expr;
423 public:
424 Neg(IntExprRef expr) : expr(expr) { }
425 int evalInt() { return (expr) ? -expr->evalInt() : 0; }
426 void dump(int level = 0);
427 bool isConstExpr() const { return expr->isConstExpr(); }
428 };
429 typedef Ref<Neg,Node> NegRef;
430
431 class ConcatString : public StringExpr {
432 ExpressionRef lhs;
433 ExpressionRef rhs;
434 public:
435 ConcatString(ExpressionRef lhs, ExpressionRef rhs) : lhs(lhs), rhs(rhs) {}
436 String evalStr();
437 void dump(int level = 0);
438 bool isConstExpr() const;
439 };
440 typedef Ref<ConcatString,Node> ConcatStringRef;
441
442 class Relation : public IntExpr {
443 public:
444 enum Type {
445 LESS_THAN,
446 GREATER_THAN,
447 LESS_OR_EQUAL,
448 GREATER_OR_EQUAL,
449 EQUAL,
450 NOT_EQUAL
451 };
452 Relation(IntExprRef lhs, Type type, IntExprRef rhs) :
453 lhs(lhs), rhs(rhs), type(type) {}
454 int evalInt();
455 void dump(int level = 0);
456 bool isConstExpr() const;
457 private:
458 IntExprRef lhs;
459 IntExprRef rhs;
460 Type type;
461 };
462 typedef Ref<Relation,Node> RelationRef;
463
464 class Or : virtual public BinaryOp, virtual public IntExpr {
465 public:
466 Or(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
467 int evalInt();
468 void dump(int level = 0);
469 };
470 typedef Ref<Or,Node> OrRef;
471
472 class And : virtual public BinaryOp, virtual public IntExpr {
473 public:
474 And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {}
475 int evalInt();
476 void dump(int level = 0);
477 };
478 typedef Ref<And,Node> AndRef;
479
480 class Not : virtual public IntExpr {
481 IntExprRef expr;
482 public:
483 Not(IntExprRef expr) : expr(expr) {}
484 int evalInt() { return !expr->evalInt(); }
485 void dump(int level = 0);
486 bool isConstExpr() const { return expr->isConstExpr(); }
487 };
488 typedef Ref<Not,Node> NotRef;
489
490 class ParserContext : public VMParserContext {
491 public:
492 struct Error {
493 String txt;
494 int line;
495 };
496 typedef Error Warning;
497
498 void* scanner;
499 std::istream* is;
500 std::vector<ParserIssue> vErrors;
501 std::vector<ParserIssue> vWarnings;
502 std::vector<ParserIssue> vIssues;
503
504 std::set<String> builtinPreprocessorConditions;
505 std::set<String> userPreprocessorConditions;
506
507 std::map<String,VariableRef> vartable;
508 int globalIntVarCount;
509 int globalStrVarCount;
510 int polyphonicIntVarCount;
511
512 EventHandlersRef handlers;
513
514 OnInitRef onInit;
515 OnNoteRef onNote;
516 OnReleaseRef onRelease;
517 OnControllerRef onController;
518
519 ArrayList<int>* globalIntMemory;
520 ArrayList<String>* globalStrMemory;
521 int requiredMaxStackSize;
522
523 VMFunctionProvider* functionProvider;
524
525 ExecContext* execContext;
526
527 ParserContext(VMFunctionProvider* parent) :
528 scanner(NULL), is(NULL),
529 globalIntVarCount(0), globalStrVarCount(0), polyphonicIntVarCount(0),
530 globalIntMemory(NULL), globalStrMemory(NULL), requiredMaxStackSize(-1),
531 functionProvider(parent), execContext(NULL)
532 {
533 }
534 virtual ~ParserContext();
535 VariableRef globalVar(const String& name);
536 IntVariableRef globalIntVar(const String& name);
537 StringVariableRef globalStrVar(const String& name);
538 VariableRef variableByName(const String& name);
539 void addErr(int line, const char* txt);
540 void addWrn(int line, const char* txt);
541 void createScanner(std::istream* is);
542 void destroyScanner();
543 bool setPreprocessorCondition(const char* name);
544 bool resetPreprocessorCondition(const char* name);
545 bool isPreprocessorConditionSet(const char* name);
546 std::vector<ParserIssue> issues() const OVERRIDE;
547 std::vector<ParserIssue> errors() const OVERRIDE;
548 std::vector<ParserIssue> warnings() const OVERRIDE;
549 VMEventHandler* eventHandler(uint index) OVERRIDE;
550 VMEventHandler* eventHandlerByName(const String& name) OVERRIDE;
551 };
552
553 class ExecContext : public VMExecContext {
554 public:
555 struct StackFrame {
556 Statement* statement;
557 int subindex;
558
559 StackFrame() {
560 statement = NULL;
561 subindex = -1;
562 }
563 };
564
565 ArrayList<int> polyphonicIntMemory;
566 VMExecStatus_t status;
567 ArrayList<StackFrame> stack;
568 int stackFrame;
569 int suspendMicroseconds;
570
571 ExecContext() :
572 status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {}
573
574 virtual ~ExecContext() {}
575
576 inline void pushStack(Statement* stmt) {
577 stackFrame++;
578 //printf("pushStack() -> %d\n", stackFrame);
579 if (stackFrame >= stack.size()) return;
580 stack[stackFrame].statement = stmt;
581 stack[stackFrame].subindex = 0;
582 }
583
584 inline void popStack() {
585 stack[stackFrame].statement = NULL;
586 stack[stackFrame].subindex = -1;
587 stackFrame--;
588 //printf("popStack() -> %d\n", stackFrame);
589 }
590
591 inline void reset() {
592 stack[0].statement = NULL;
593 stack[0].subindex = -1;
594 stackFrame = -1;
595 }
596
597 int suspensionTimeMicroseconds() const OVERRIDE {
598 return suspendMicroseconds;
599 }
600 };
601
602 } // namespace LinuxSampler
603
604 #endif // LS_INSTRPARSERTREE_H

  ViewVC Help
Powered by ViewVC