1 |
/* -*- c++ -*- |
/* -*- c++ -*- |
2 |
* |
* |
3 |
* Copyright (c) 2014 - 2016 Christian Schoenebeck and Andreas Persson |
* Copyright (c) 2014 - 2017 Christian Schoenebeck and Andreas Persson |
4 |
* |
* |
5 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
6 |
* |
* |
20 |
#include <iostream> |
#include <iostream> |
21 |
#include <map> |
#include <map> |
22 |
#include <set> |
#include <set> |
23 |
|
#include <string.h> // for memset() |
24 |
#include "../common/global.h" |
#include "../common/global.h" |
25 |
#include "../common/Ref.h" |
#include "../common/Ref.h" |
26 |
#include "../common/ArrayList.h" |
#include "../common/ArrayList.h" |
64 |
}; |
}; |
65 |
typedef Ref<IntExpr,Node> IntExprRef; |
typedef Ref<IntExpr,Node> IntExprRef; |
66 |
|
|
67 |
|
/*class IntArrayExpr : virtual public VMIntArrayExpr, virtual public Expression { |
68 |
|
public: |
69 |
|
ExprType_t exprType() const { return INT_ARR_EXPR; } |
70 |
|
String evalCastToStr(); |
71 |
|
}; |
72 |
|
typedef Ref<IntArrayExpr,Node> IntArrayExprRef;*/ |
73 |
|
|
74 |
class StringExpr : virtual public VMStringExpr, virtual public Expression { |
class StringExpr : virtual public VMStringExpr, virtual public Expression { |
75 |
public: |
public: |
76 |
ExprType_t exprType() const { return STRING_EXPR; } |
ExprType_t exprType() const { return STRING_EXPR; } |
106 |
std::vector<ExpressionRef> args; |
std::vector<ExpressionRef> args; |
107 |
void add(ExpressionRef arg) { args.push_back(arg); } |
void add(ExpressionRef arg) { args.push_back(arg); } |
108 |
void dump(int level = 0); |
void dump(int level = 0); |
109 |
int argsCount() const { return args.size(); } |
int argsCount() const { return (int) args.size(); } |
110 |
VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; } |
VMExpr* arg(int i) { return (i >= 0 && i < argsCount()) ? &*args.at(i) : NULL; } |
111 |
bool isPolyphonic() const; |
bool isPolyphonic() const; |
112 |
}; |
}; |
113 |
typedef Ref<Args,Node> ArgsRef; |
typedef Ref<Args,Node> ArgsRef; |
114 |
|
|
115 |
class Variable : virtual public Expression { |
class Variable : virtual public VMVariable, virtual public Expression { |
116 |
public: |
public: |
117 |
virtual bool isConstExpr() const { return bConst; } |
bool isConstExpr() const OVERRIDE { return bConst; } |
118 |
|
bool isAssignable() const OVERRIDE { return !bConst; } |
119 |
virtual void assign(Expression* expr) = 0; |
virtual void assign(Expression* expr) = 0; |
120 |
|
void assignExpr(VMExpr* expr) OVERRIDE { Expression* e = dynamic_cast<Expression*>(expr); if (e) assign(e); } |
121 |
protected: |
protected: |
122 |
Variable(ParserContext* ctx, int _memPos, bool _bConst) |
Variable(ParserContext* ctx, int _memPos, bool _bConst) |
123 |
: context(ctx), memPos(_memPos), bConst(_bConst) {} |
: context(ctx), memPos(_memPos), bConst(_bConst) {} |
158 |
VMIntRelPtr* ptr; |
VMIntRelPtr* ptr; |
159 |
public: |
public: |
160 |
BuiltInIntVariable(const String& name, VMIntRelPtr* ptr); |
BuiltInIntVariable(const String& name, VMIntRelPtr* ptr); |
161 |
void assign(Expression* expr); |
bool isAssignable() const OVERRIDE { return !ptr->readonly; } |
162 |
int evalInt(); |
void assign(Expression* expr) OVERRIDE; |
163 |
void dump(int level = 0); |
int evalInt() OVERRIDE; |
164 |
|
void dump(int level = 0) OVERRIDE; |
165 |
}; |
}; |
166 |
typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef; |
typedef Ref<BuiltInIntVariable,Node> BuiltInIntVariableRef; |
167 |
|
|
197 |
BuiltInIntArrayVariable(const String& name, VMInt8Array* array); |
BuiltInIntArrayVariable(const String& name, VMInt8Array* array); |
198 |
int arraySize() const { return array->size; } |
int arraySize() const { return array->size; } |
199 |
int evalIntElement(uint i); |
int evalIntElement(uint i); |
200 |
|
bool isAssignable() const OVERRIDE { return !array->readonly; } |
201 |
void assignIntElement(uint i, int value); |
void assignIntElement(uint i, int value); |
202 |
void dump(int level = 0); |
void dump(int level = 0); |
203 |
}; |
}; |
332 |
virtual Statements* branch(uint i) const = 0; |
virtual Statements* branch(uint i) const = 0; |
333 |
}; |
}; |
334 |
|
|
335 |
|
class DynamicVariableCall : public Variable, virtual public IntExpr, virtual public StringExpr { |
336 |
|
VMDynVar* dynVar; |
337 |
|
String varName; |
338 |
|
public: |
339 |
|
DynamicVariableCall(const String& name, ParserContext* ctx, VMDynVar* v); |
340 |
|
ExprType_t exprType() const OVERRIDE { return dynVar->exprType(); } |
341 |
|
bool isConstExpr() const OVERRIDE { return dynVar->isConstExpr(); } |
342 |
|
bool isAssignable() const OVERRIDE { return dynVar->isAssignable(); } |
343 |
|
bool isPolyphonic() const OVERRIDE { return false; } |
344 |
|
void assign(Expression* expr) OVERRIDE { dynVar->assignExpr(expr); } |
345 |
|
VMIntArrayExpr* asIntArray() const OVERRIDE { return dynVar->asIntArray(); } |
346 |
|
int evalInt() OVERRIDE; |
347 |
|
String evalStr() OVERRIDE; |
348 |
|
String evalCastToStr() OVERRIDE; |
349 |
|
void dump(int level = 0) OVERRIDE; |
350 |
|
}; |
351 |
|
typedef Ref<DynamicVariableCall,Node> DynamicVariableCallRef; |
352 |
|
|
353 |
class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr { |
class FunctionCall : virtual public LeafStatement, virtual public IntExpr, virtual public StringExpr { |
354 |
String functionName; |
String functionName; |
355 |
ArgsRef args; |
ArgsRef args; |
357 |
public: |
public: |
358 |
FunctionCall(const char* function, ArgsRef args, VMFunction* fn) : |
FunctionCall(const char* function, ArgsRef args, VMFunction* fn) : |
359 |
functionName(function), args(args), fn(fn) { } |
functionName(function), args(args), fn(fn) { } |
360 |
void dump(int level = 0); |
void dump(int level = 0) OVERRIDE; |
361 |
StmtFlags_t exec(); |
StmtFlags_t exec() OVERRIDE; |
362 |
int evalInt(); |
int evalInt() OVERRIDE; |
363 |
String evalStr(); |
VMIntArrayExpr* asIntArray() const OVERRIDE; |
364 |
bool isConstExpr() const { return false; } |
String evalStr() OVERRIDE; |
365 |
ExprType_t exprType() const; |
bool isConstExpr() const OVERRIDE { return false; } |
366 |
String evalCastToStr(); |
ExprType_t exprType() const OVERRIDE; |
367 |
bool isPolyphonic() const { return args->isPolyphonic(); } |
String evalCastToStr() OVERRIDE; |
368 |
|
bool isPolyphonic() const OVERRIDE { return args->isPolyphonic(); } |
369 |
protected: |
protected: |
370 |
VMFnResult* execVMFn(); |
VMFnResult* execVMFn(); |
371 |
}; |
}; |
386 |
class OnNote : public EventHandler { |
class OnNote : public EventHandler { |
387 |
public: |
public: |
388 |
OnNote(StatementsRef statements) : EventHandler(statements) {} |
OnNote(StatementsRef statements) : EventHandler(statements) {} |
389 |
|
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_NOTE; } |
390 |
String eventHandlerName() const { return "note"; } |
String eventHandlerName() const { return "note"; } |
391 |
}; |
}; |
392 |
typedef Ref<OnNote,Node> OnNoteRef; |
typedef Ref<OnNote,Node> OnNoteRef; |
394 |
class OnInit : public EventHandler { |
class OnInit : public EventHandler { |
395 |
public: |
public: |
396 |
OnInit(StatementsRef statements) : EventHandler(statements) {} |
OnInit(StatementsRef statements) : EventHandler(statements) {} |
397 |
|
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_INIT; } |
398 |
String eventHandlerName() const { return "init"; } |
String eventHandlerName() const { return "init"; } |
399 |
}; |
}; |
400 |
typedef Ref<OnInit,Node> OnInitRef; |
typedef Ref<OnInit,Node> OnInitRef; |
402 |
class OnRelease : public EventHandler { |
class OnRelease : public EventHandler { |
403 |
public: |
public: |
404 |
OnRelease(StatementsRef statements) : EventHandler(statements) {} |
OnRelease(StatementsRef statements) : EventHandler(statements) {} |
405 |
|
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_RELEASE; } |
406 |
String eventHandlerName() const { return "release"; } |
String eventHandlerName() const { return "release"; } |
407 |
}; |
}; |
408 |
typedef Ref<OnRelease,Node> OnReleaseRef; |
typedef Ref<OnRelease,Node> OnReleaseRef; |
410 |
class OnController : public EventHandler { |
class OnController : public EventHandler { |
411 |
public: |
public: |
412 |
OnController(StatementsRef statements) : EventHandler(statements) {} |
OnController(StatementsRef statements) : EventHandler(statements) {} |
413 |
|
VMEventHandlerType_t eventHandlerType() const { return VM_EVENT_HANDLER_CONTROLLER; } |
414 |
String eventHandlerName() const { return "controller"; } |
String eventHandlerName() const { return "controller"; } |
415 |
}; |
}; |
416 |
typedef Ref<OnController,Node> OnControllerRef; |
typedef Ref<OnController,Node> OnControllerRef; |
425 |
int evalInt() { return 0; } |
int evalInt() { return 0; } |
426 |
EventHandler* eventHandlerByName(const String& name) const; |
EventHandler* eventHandlerByName(const String& name) const; |
427 |
EventHandler* eventHandler(uint index) const; |
EventHandler* eventHandler(uint index) const; |
428 |
inline uint size() const { return args.size(); } |
inline uint size() const { return (int) args.size(); } |
429 |
bool isPolyphonic() const; |
bool isPolyphonic() const; |
430 |
}; |
}; |
431 |
typedef Ref<EventHandlers,Node> EventHandlersRef; |
typedef Ref<EventHandlers,Node> EventHandlersRef; |
549 |
}; |
}; |
550 |
typedef Ref<Or,Node> OrRef; |
typedef Ref<Or,Node> OrRef; |
551 |
|
|
552 |
|
class BitwiseOr : virtual public BinaryOp, virtual public IntExpr { |
553 |
|
public: |
554 |
|
BitwiseOr(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
555 |
|
int evalInt(); |
556 |
|
void dump(int level = 0); |
557 |
|
}; |
558 |
|
typedef Ref<BitwiseOr,Node> BitwiseOrRef; |
559 |
|
|
560 |
class And : virtual public BinaryOp, virtual public IntExpr { |
class And : virtual public BinaryOp, virtual public IntExpr { |
561 |
public: |
public: |
562 |
And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
And(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
565 |
}; |
}; |
566 |
typedef Ref<And,Node> AndRef; |
typedef Ref<And,Node> AndRef; |
567 |
|
|
568 |
|
class BitwiseAnd : virtual public BinaryOp, virtual public IntExpr { |
569 |
|
public: |
570 |
|
BitwiseAnd(IntExprRef lhs, IntExprRef rhs) : BinaryOp(lhs,rhs) {} |
571 |
|
int evalInt(); |
572 |
|
void dump(int level = 0); |
573 |
|
}; |
574 |
|
typedef Ref<BitwiseAnd,Node> BitwiseAndRef; |
575 |
|
|
576 |
class Not : virtual public IntExpr { |
class Not : virtual public IntExpr { |
577 |
IntExprRef expr; |
IntExprRef expr; |
578 |
public: |
public: |
584 |
}; |
}; |
585 |
typedef Ref<Not,Node> NotRef; |
typedef Ref<Not,Node> NotRef; |
586 |
|
|
587 |
|
class BitwiseNot : virtual public IntExpr { |
588 |
|
IntExprRef expr; |
589 |
|
public: |
590 |
|
BitwiseNot(IntExprRef expr) : expr(expr) {} |
591 |
|
int evalInt() { return ~expr->evalInt(); } |
592 |
|
void dump(int level = 0); |
593 |
|
bool isConstExpr() const { return expr->isConstExpr(); } |
594 |
|
bool isPolyphonic() const { return expr->isPolyphonic(); } |
595 |
|
}; |
596 |
|
typedef Ref<BitwiseNot,Node> BitwiseNotRef; |
597 |
|
|
598 |
class ParserContext : public VMParserContext { |
class ParserContext : public VMParserContext { |
599 |
public: |
public: |
600 |
struct Error { |
struct Error { |
613 |
std::set<String> userPreprocessorConditions; |
std::set<String> userPreprocessorConditions; |
614 |
|
|
615 |
std::map<String,VariableRef> vartable; |
std::map<String,VariableRef> vartable; |
616 |
|
std::map<String,StatementsRef> userFnTable; |
617 |
int globalIntVarCount; |
int globalIntVarCount; |
618 |
int globalStrVarCount; |
int globalStrVarCount; |
619 |
int polyphonicIntVarCount; |
int polyphonicIntVarCount; |
645 |
IntVariableRef globalIntVar(const String& name); |
IntVariableRef globalIntVar(const String& name); |
646 |
StringVariableRef globalStrVar(const String& name); |
StringVariableRef globalStrVar(const String& name); |
647 |
VariableRef variableByName(const String& name); |
VariableRef variableByName(const String& name); |
648 |
void addErr(int line, const char* txt); |
StatementsRef userFunctionByName(const String& name); |
649 |
void addWrn(int line, const char* txt); |
void addErr(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
650 |
|
void addWrn(int firstLine, int lastLine, int firstColumn, int lastColumn, const char* txt); |
651 |
void createScanner(std::istream* is); |
void createScanner(std::istream* is); |
652 |
void destroyScanner(); |
void destroyScanner(); |
653 |
bool setPreprocessorCondition(const char* name); |
bool setPreprocessorCondition(const char* name); |
661 |
void registerBuiltInConstIntVariables(const std::map<String,int>& vars); |
void registerBuiltInConstIntVariables(const std::map<String,int>& vars); |
662 |
void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars); |
void registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars); |
663 |
void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars); |
void registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars); |
664 |
|
void registerBuiltInDynVariables(const std::map<String,VMDynVar*>& vars); |
665 |
}; |
}; |
666 |
|
|
667 |
class ExecContext : public VMExecContext { |
class ExecContext : public VMExecContext { |
681 |
ArrayList<StackFrame> stack; |
ArrayList<StackFrame> stack; |
682 |
int stackFrame; |
int stackFrame; |
683 |
int suspendMicroseconds; |
int suspendMicroseconds; |
684 |
|
size_t instructionsCount; |
685 |
|
|
686 |
ExecContext() : |
ExecContext() : |
687 |
status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0) {} |
status(VM_EXEC_NOT_RUNNING), stackFrame(-1), suspendMicroseconds(0), |
688 |
|
instructionsCount(0) {} |
689 |
|
|
690 |
virtual ~ExecContext() {} |
virtual ~ExecContext() {} |
691 |
|
|
713 |
int suspensionTimeMicroseconds() const OVERRIDE { |
int suspensionTimeMicroseconds() const OVERRIDE { |
714 |
return suspendMicroseconds; |
return suspendMicroseconds; |
715 |
} |
} |
716 |
|
|
717 |
|
void resetPolyphonicData() OVERRIDE { |
718 |
|
if (polyphonicIntMemory.empty()) return; |
719 |
|
memset(&polyphonicIntMemory[0], 0, polyphonicIntMemory.size() * sizeof(int)); |
720 |
|
} |
721 |
|
|
722 |
|
size_t instructionsPerformed() const OVERRIDE { |
723 |
|
return instructionsCount; |
724 |
|
} |
725 |
}; |
}; |
726 |
|
|
727 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |