21 |
#include <map> |
#include <map> |
22 |
#include <set> |
#include <set> |
23 |
#include <string.h> // for memset() |
#include <string.h> // for memset() |
24 |
|
#include <assert.h> |
25 |
#include "../common/global.h" |
#include "../common/global.h" |
26 |
#include "../common/Ref.h" |
#include "../common/Ref.h" |
27 |
#include "../common/ArrayList.h" |
#include "../common/ArrayList.h" |
41 |
STMT_NOOP, |
STMT_NOOP, |
42 |
}; |
}; |
43 |
|
|
44 |
|
/** |
45 |
|
* Convenience function used for retrieving the (assumed) data type of a given |
46 |
|
* script variable name. |
47 |
|
* |
48 |
|
* @param name - some script variable name (e.g. "$foo") |
49 |
|
* @return variable's assumed data type (e.g. INT_EXPR for example above) |
50 |
|
*/ |
51 |
|
inline ExprType_t exprTypeOfVarName(const String& name) { |
52 |
|
if (name.empty()) return (ExprType_t) -1; |
53 |
|
const char prefix = name[0]; |
54 |
|
switch (prefix) { |
55 |
|
case '$': return INT_EXPR; |
56 |
|
case '%': return INT_ARR_EXPR; |
57 |
|
case '~': return REAL_EXPR; |
58 |
|
case '?': return REAL_ARR_EXPR; |
59 |
|
case '@': return STRING_EXPR; |
60 |
|
case '!': return STRING_ARR_EXPR; |
61 |
|
} |
62 |
|
return (ExprType_t) -1; |
63 |
|
} |
64 |
|
|
65 |
|
inline ExprType_t scalarTypeOfArray(ExprType_t arrayType) { |
66 |
|
if (arrayType == INT_ARR_EXPR) return INT_EXPR; |
67 |
|
if (arrayType == REAL_ARR_EXPR) return REAL_EXPR; |
68 |
|
if (arrayType == STRING_ARR_EXPR) return STRING_EXPR; |
69 |
|
assert(false); |
70 |
|
return EMPTY_EXPR; // just to shut up the compiler |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
|
* Used by parser for parser error messages to provide a text with all data |
75 |
|
* types accepted by the given built-in function @a fn for the respective |
76 |
|
* function argument @a iArg. |
77 |
|
*/ |
78 |
|
String acceptedArgTypesStr(VMFunction* fn, vmint iArg); |
79 |
|
|
80 |
class Node { |
class Node { |
81 |
public: |
public: |
82 |
Node(); |
Node(); |
107 |
}; |
}; |
108 |
typedef Ref<Unit,Node> UnitRef; |
typedef Ref<Unit,Node> UnitRef; |
109 |
|
|
110 |
class ScalarNumberExpr : virtual public Unit, virtual public VMScalarNumberExpr, virtual public Expression { |
class NumberExpr : virtual public Unit, virtual public VMNumberExpr, virtual public Expression { |
111 |
public: |
public: |
112 |
}; |
}; |
113 |
typedef Ref<ScalarNumberExpr,Node> ScalarNumberExprRef; |
typedef Ref<NumberExpr,Node> NumberExprRef; |
114 |
|
|
115 |
class IntExpr : virtual public ScalarNumberExpr, virtual public VMIntExpr { |
class IntExpr : virtual public NumberExpr, virtual public VMIntExpr { |
116 |
public: |
public: |
117 |
ExprType_t exprType() const OVERRIDE { return INT_EXPR; } |
ExprType_t exprType() const OVERRIDE { return INT_EXPR; } |
118 |
vmint evalIntToUnitFactor(vmfloat unitFactor); |
vmint evalIntToUnitFactor(vmfloat unitFactor); |
120 |
}; |
}; |
121 |
typedef Ref<IntExpr,Node> IntExprRef; |
typedef Ref<IntExpr,Node> IntExprRef; |
122 |
|
|
123 |
class RealExpr : virtual public ScalarNumberExpr, virtual public VMRealExpr { |
class RealExpr : virtual public NumberExpr, virtual public VMRealExpr { |
124 |
public: |
public: |
125 |
ExprType_t exprType() const OVERRIDE { return REAL_EXPR; } |
ExprType_t exprType() const OVERRIDE { return REAL_EXPR; } |
126 |
vmfloat evalRealToUnitFactor(vmfloat unitFactor); |
vmfloat evalRealToUnitFactor(vmfloat unitFactor); |
246 |
}; |
}; |
247 |
typedef Ref<Variable,Node> VariableRef; |
typedef Ref<Variable,Node> VariableRef; |
248 |
|
|
249 |
class ScalarNumberVariable : public Variable, virtual public ScalarNumberExpr { |
class NumberVariable : public Variable, virtual public NumberExpr { |
250 |
bool polyphonic; |
bool polyphonic; |
251 |
bool finalVal; |
bool finalVal; |
252 |
protected: |
protected: |
256 |
bool isFinal() const OVERRIDE { return finalVal; } |
bool isFinal() const OVERRIDE { return finalVal; } |
257 |
vmfloat unitFactor() const OVERRIDE; |
vmfloat unitFactor() const OVERRIDE; |
258 |
protected: |
protected: |
259 |
ScalarNumberVariable(const VariableDecl& decl); |
NumberVariable(const VariableDecl& decl); |
260 |
}; |
}; |
261 |
typedef Ref<ScalarNumberVariable,Node> ScalarNumberVariableRef; |
typedef Ref<NumberVariable,Node> NumberVariableRef; |
262 |
|
|
263 |
class IntVariable : public ScalarNumberVariable, virtual public IntExpr { |
class IntVariable : public NumberVariable, virtual public IntExpr { |
264 |
public: |
public: |
265 |
IntVariable(const VariableDecl& decl); |
IntVariable(const VariableDecl& decl); |
266 |
void assign(Expression* expr) OVERRIDE; |
void assign(Expression* expr) OVERRIDE; |
269 |
}; |
}; |
270 |
typedef Ref<IntVariable,Node> IntVariableRef; |
typedef Ref<IntVariable,Node> IntVariableRef; |
271 |
|
|
272 |
class RealVariable : public ScalarNumberVariable, virtual public RealExpr { |
class RealVariable : public NumberVariable, virtual public RealExpr { |
273 |
public: |
public: |
274 |
RealVariable(const VariableDecl& decl); |
RealVariable(const VariableDecl& decl); |
275 |
void assign(Expression* expr) OVERRIDE; |
void assign(Expression* expr) OVERRIDE; |
473 |
}; |
}; |
474 |
typedef Ref<BinaryOp,Node> BinaryOpRef; |
typedef Ref<BinaryOp,Node> BinaryOpRef; |
475 |
|
|
476 |
class ScalarNumberBinaryOp : public BinaryOp, virtual public ScalarNumberExpr { |
class NumberBinaryOp : public BinaryOp, virtual public NumberExpr { |
477 |
public: |
public: |
478 |
ScalarNumberBinaryOp(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs) : BinaryOp(lhs, rhs) { } |
NumberBinaryOp(NumberExprRef lhs, NumberExprRef rhs) : BinaryOp(lhs, rhs) { } |
479 |
bool isFinal() const OVERRIDE; |
bool isFinal() const OVERRIDE; |
480 |
}; |
}; |
481 |
typedef Ref<ScalarNumberBinaryOp,Node> ScalarNumberBinaryOpRef; |
typedef Ref<NumberBinaryOp,Node> NumberBinaryOpRef; |
482 |
|
|
483 |
class IntBinaryOp : public ScalarNumberBinaryOp, virtual public IntExpr { |
class IntBinaryOp : public NumberBinaryOp, virtual public IntExpr { |
484 |
public: |
public: |
485 |
IntBinaryOp(IntExprRef lhs, IntExprRef rhs) : ScalarNumberBinaryOp(lhs, rhs) { } |
IntBinaryOp(IntExprRef lhs, IntExprRef rhs) : NumberBinaryOp(lhs, rhs) { } |
486 |
}; |
}; |
487 |
typedef Ref<IntBinaryOp,Node> IntBinaryOpRef; |
typedef Ref<IntBinaryOp,Node> IntBinaryOpRef; |
488 |
|
|
489 |
class VaritypeScalarBinaryOp : public ScalarNumberBinaryOp, virtual public IntExpr, virtual public RealExpr { |
class VaritypeScalarBinaryOp : public NumberBinaryOp, virtual public IntExpr, virtual public RealExpr { |
490 |
public: |
public: |
491 |
VaritypeScalarBinaryOp(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs) : ScalarNumberBinaryOp(lhs, rhs) { } |
VaritypeScalarBinaryOp(NumberExprRef lhs, NumberExprRef rhs) : NumberBinaryOp(lhs, rhs) { } |
492 |
ExprType_t exprType() const OVERRIDE; |
ExprType_t exprType() const OVERRIDE; |
493 |
String evalCastToStr() OVERRIDE; |
String evalCastToStr() OVERRIDE; |
494 |
}; |
}; |
496 |
|
|
497 |
class Add FINAL : public VaritypeScalarBinaryOp { |
class Add FINAL : public VaritypeScalarBinaryOp { |
498 |
public: |
public: |
499 |
Add(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs); |
Add(NumberExprRef lhs, NumberExprRef rhs); |
500 |
vmint evalInt() OVERRIDE; |
vmint evalInt() OVERRIDE; |
501 |
vmfloat evalReal() OVERRIDE; |
vmfloat evalReal() OVERRIDE; |
502 |
vmfloat unitFactor() const OVERRIDE; |
vmfloat unitFactor() const OVERRIDE; |
506 |
|
|
507 |
class Sub FINAL : public VaritypeScalarBinaryOp { |
class Sub FINAL : public VaritypeScalarBinaryOp { |
508 |
public: |
public: |
509 |
Sub(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs); |
Sub(NumberExprRef lhs, NumberExprRef rhs); |
510 |
vmint evalInt() OVERRIDE; |
vmint evalInt() OVERRIDE; |
511 |
vmfloat evalReal() OVERRIDE; |
vmfloat evalReal() OVERRIDE; |
512 |
vmfloat unitFactor() const OVERRIDE; |
vmfloat unitFactor() const OVERRIDE; |
516 |
|
|
517 |
class Mul FINAL : public VaritypeScalarBinaryOp { |
class Mul FINAL : public VaritypeScalarBinaryOp { |
518 |
public: |
public: |
519 |
Mul(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs); |
Mul(NumberExprRef lhs, NumberExprRef rhs); |
520 |
vmint evalInt() OVERRIDE; |
vmint evalInt() OVERRIDE; |
521 |
vmfloat evalReal() OVERRIDE; |
vmfloat evalReal() OVERRIDE; |
522 |
vmfloat unitFactor() const OVERRIDE; |
vmfloat unitFactor() const OVERRIDE; |
526 |
|
|
527 |
class Div FINAL : public VaritypeScalarBinaryOp { |
class Div FINAL : public VaritypeScalarBinaryOp { |
528 |
public: |
public: |
529 |
Div(ScalarNumberExprRef lhs, ScalarNumberExprRef rhs); |
Div(NumberExprRef lhs, NumberExprRef rhs); |
530 |
vmint evalInt() OVERRIDE; |
vmint evalInt() OVERRIDE; |
531 |
vmfloat evalReal() OVERRIDE; |
vmfloat evalReal() OVERRIDE; |
532 |
void dump(int level = 0) OVERRIDE; |
void dump(int level = 0) OVERRIDE; |
774 |
typedef Ref<SyncBlock,Node> SyncBlockRef; |
typedef Ref<SyncBlock,Node> SyncBlockRef; |
775 |
|
|
776 |
class Neg FINAL : virtual public IntExpr, virtual public RealExpr { |
class Neg FINAL : virtual public IntExpr, virtual public RealExpr { |
777 |
ScalarNumberExprRef expr; |
NumberExprRef expr; |
778 |
public: |
public: |
779 |
Neg(ScalarNumberExprRef expr) : Unit(expr->unitType()), expr(expr) { } |
Neg(NumberExprRef expr) : Unit(expr->unitType()), expr(expr) { } |
780 |
ExprType_t exprType() const OVERRIDE { return expr->exprType(); } |
ExprType_t exprType() const OVERRIDE { return expr->exprType(); } |
781 |
vmint evalInt() OVERRIDE { return (expr) ? -(expr->asInt()->evalInt()) : 0; } |
vmint evalInt() OVERRIDE { return (expr) ? -(expr->asInt()->evalInt()) : 0; } |
782 |
vmfloat evalReal() OVERRIDE { return (expr) ? -(expr->asReal()->evalReal()) : vmfloat(0); } |
vmfloat evalReal() OVERRIDE { return (expr) ? -(expr->asReal()->evalReal()) : vmfloat(0); } |
888 |
typedef Ref<BitwiseNot,Node> BitwiseNotRef; |
typedef Ref<BitwiseNot,Node> BitwiseNotRef; |
889 |
|
|
890 |
class Final FINAL : virtual public IntExpr, virtual public RealExpr { |
class Final FINAL : virtual public IntExpr, virtual public RealExpr { |
891 |
ScalarNumberExprRef expr; |
NumberExprRef expr; |
892 |
public: |
public: |
893 |
Final(ScalarNumberExprRef expr) : Unit(expr->unitType()), expr(expr) {} |
Final(NumberExprRef expr) : Unit(expr->unitType()), expr(expr) {} |
894 |
ExprType_t exprType() const OVERRIDE { return expr->exprType(); } |
ExprType_t exprType() const OVERRIDE { return expr->exprType(); } |
895 |
vmint evalInt() OVERRIDE { return expr->asInt()->evalInt(); } |
vmint evalInt() OVERRIDE { return expr->asInt()->evalInt(); } |
896 |
vmfloat evalReal() OVERRIDE { return expr->asReal()->evalReal(); } |
vmfloat evalReal() OVERRIDE { return expr->asReal()->evalReal(); } |