FILE* additionalStream, const char* eoln) :
m_fp(fopen(filename, mode)),
m_name(filename),
- m_eoln(eoln)
+ m_eoln(eoln),
+ m_additionalStream(additionalStream)
{
}
* @param op the operator id
* @param type the node type
*/
-RplASUnaryOp::RplASUnaryOp(int op, RplASItemType type) :
+RplASUnaryOp::RplASUnaryOp(UnaryOp op, RplASItemType type) :
RplASNode1(type),
m_operator(op)
{
char buffer[256];
writer.formatIndented(indent, "Unary %d op: %s (%d) expr: %d %s",
m_id,
- RplLexer::m_active->nameOfOp(m_operator).constData(),
+ nameOfOp(m_operator),
m_operator,
m_child == NULL ? 0 : m_child->id(),
positionStr(buffer, sizeof buffer) );
if (m_child != NULL)
m_child->dump(writer, indent + 1);
}
+/**
+ * @brief Returns the name (a string) of an unary operator.
+ *
+ * @param op the op to convert
+ * @return the name of the operator
+ */
+const char*RplASUnaryOp::nameOfOp(RplASUnaryOp::UnaryOp op)
+{
+ const char* rc;
+ switch (op){
+ case UOP_PLUS:
+ rc="+";
+ break;
+ case UOP_MINUS:
+ rc="-";
+ break;
+ case UOP_LOGICAL_NOT:
+ rc="!";
+ break;
+ case UOP_BIT_NOT:
+ rc="~";
+ break;
+ case UOP_INC:
+ rc="++";
+ break;
+ case UOP_DEC:
+ rc="--";
+ break;
+ default:
+ throw RplException("unknown unary operator: %d", (int) op);
+ break;
+ }
+ return rc;
+}
/** @class RplASStatement rplastree.hpp "rplexpr/rplastree.hpp"
void RplASBinaryOp::dump(RplWriter& writer, int indent)
{
- const QByteArray& opName = RplLexer::m_active->nameOfOp(m_operator);
+ const QByteArray& opName = nameOfOp(m_operator);
char buffer[256];
writer.formatIndented(indent, "BinOp id: %d op: %s (%d) left: %d right: %d %s",
m_id,
rValue.copyValue(value);
}
}
+/**
+ * @brief Returns the name (a string) of a binary operator.
+ *
+ * @param op operator to convert
+ *
+ * @return the name of the operator
+ */
+const char* RplASBinaryOp::nameOfOp(RplASBinaryOp::BinOperator op)
+{
+ const char* rc;
+ switch (op){
+ case BOP_ASSIGN:
+ rc = "=";
+ break;
+ case BOP_PLUS_ASSIGN:
+ rc = "+=";
+ break;
+ case BOP_MINUS_ASSIGN:
+ rc = "-=";
+ break;
+ case BOP_TIMES_ASSIGN:
+ rc = "*=";
+ break;
+ case BOP_DIV_ASSIGN:
+ rc = "/=";
+ break;
+ case BOP_MOD_ASSIGN:
+ rc = "%=";
+ break;
+ case BOP_POWER_ASSIGN:
+ rc = "**=";
+ break;
+ case BOP_LOG_OR_ASSIGN:
+ rc = "||=";
+ break;
+ case BOP_LOG_AND_ASSIGN:
+ rc = "&&=";
+ break;
+ case BOP_LOG_XOR_ASSIGN:
+ rc = "^^=";
+ break;
+ case BOP_BIT_OR_ASSIGN:
+ rc = "|=";
+ break;
+ case BOP_BIT_AND_ASSIGN:
+ rc = "&=";
+ break;
+ case BOP_BIT_XOR_ASSIGN:
+ rc = "^=";
+ break;
+ case BOP_LSHIFT_ASSIGN:
+ rc = "<<=";
+ break;
+ case BOP_LOG_RSHIFT_ASSIGN:
+ rc = ">>=";
+ break;
+ case BOP_ARTITH_RSHIFT_ASSIGN:
+ rc = ">>>=";
+ break;
+ case BOP_PLUS:
+ rc = "+";
+ break;
+ case BOP_MINUS:
+ rc = "-";
+ break;
+ case BOP_TIMES:
+ rc = "*";
+ break;
+ case BOP_DIV:
+ rc = "/";
+ break;
+ case BOP_MOD:
+ rc = "%";
+ break;
+ case BOP_POWER:
+ rc = "**";
+ break;
+ case BOP_LOG_OR:
+ rc = "||";
+ break;
+ case BOP_LOG_AND:
+ rc = "&&";
+ break;
+ case BOP_LOG_XOR:
+ rc = "^^";
+ break;
+ case BOP_BIT_OR:
+ rc = "|";
+ break;
+ case BOP_BIT_AND:
+ rc = "&";
+ break;
+ case BOP_BIT_XOR:
+ rc = "^";
+ break;
+ case BOP_LSHIFT:
+ rc = ""; break;
+ case BOP_LOG_RSHIFT:
+ rc = ">>";
+ break;
+ case BOP_ARTITH_RSHIFT:
+ rc = ">>>";
+ break;
+ case BOP_EQ:
+ rc = "==";
+ break;
+ case BOP_NE:
+ rc = "!=";
+ break;
+ case BOP_LE:
+ rc = "<=";
+ break;
+ case BOP_LT:
+ rc = "<";
+ break;
+ case BOP_GE:
+ rc = ">=";
+ break;
+ case BOP_GT:
+ rc = ">";
+ break;
+ default:
+ throw RplException("unknown binary op %d", (int) op);
+ }
+ return rc;
+}
/** @class RplASMethod rplastree.hpp "rplexpr/rplastree.hpp"
*
class RplASUnaryOp : public RplASNode1, RplASCalculable
{
public:
- enum {
+ enum UnaryOp {
UOP_UNDEF,
UOP_PLUS,
UOP_MINUS,
UOP_LOGICAL_NOT,
- UOP_BIT_NOT
+ UOP_BIT_NOT,
+ UOP_INC,
+ UOP_DEC
};
public:
- RplASUnaryOp(int op, RplASItemType type);
+ RplASUnaryOp(UnaryOp op, RplASItemType type);
public:
virtual void calc(RplVMThread& thread);
int getOperator() const;
void dump(RplWriter& writer, int indent);
-
+public:
+ static const char* nameOfOp(UnaryOp op);
private:
- int m_operator;
+ UnaryOp m_operator;
};
class RplASBinaryOp : public RplASNode2, public RplASCalculable
{
void dump(RplWriter& writer, int indent);
private:
void assign(RplVMThread& thread);
+ static const char* nameOfOp(BinOperator op);
private:
BinOperator m_operator;
};
}
return rc;
}
+/**
+ * @brief Converts a MF specific unary operator into one of AST.
+ *
+ * @param op operator known by MF parser
+ *
+ * @return operator known by the abstract syntax tree
+ */
+RplASUnaryOp::UnaryOp RplMFParser::convertUnaryOp(int op)
+{
+ RplASUnaryOp::UnaryOp rc;
+ switch(op){
+ case O_PLUS:
+ rc = RplASUnaryOp::UOP_PLUS;
+ break;
+ case O_MINUS:
+ rc = RplASUnaryOp::UOP_MINUS;
+ break;
+ case O_NOT:
+ rc = RplASUnaryOp::UOP_LOGICAL_NOT;
+ break;
+ case O_BIT_NOT:
+ rc = RplASUnaryOp::UOP_BIT_NOT;
+ break;
+ case O_INC:
+ rc = RplASUnaryOp::UOP_INC;
+ break;
+ case O_DEC:
+ rc = RplASUnaryOp::UOP_DEC;
+ break;
+ default:
+ throw RplException("unknown unary operator %d", op);
+ break;
+ }
+ return rc;
+}
+
/**
* @brief Parses an operand.
*
"')' expected", "(", startPosition);
}
} else if (IS_UNARY_OP(opId)){
- RplASUnaryOp* op = new RplASUnaryOp(token->id(), AST_PRE_UNARY_OP);
+ RplASUnaryOp* op = new RplASUnaryOp(convertUnaryOp(token->id()),
+ AST_PRE_UNARY_OP);
op->setPosition(m_lexer.currentPosition());
op->setChild(parseOperand(level));
readNext = false;
dynamic_cast<RplASNode1*>(rc)->setChild(indexExpr);
} else {
if (token->id() == O_INC || token->id() == O_DEC){
- RplASUnaryOp* op = new RplASUnaryOp(token->id(),
+ RplASUnaryOp* op = new RplASUnaryOp(convertUnaryOp(
+ token->id()),
AST_POST_UNARY_OP);
op->setChild(rc);
rc = op;
}
return rc;
}
-RplASBinaryOp::BinOperator encodeBinOp(int op){
- RplASBinaryOp::BinOperator rc = RplASBinaryOp::BOP_UNDEF;
+/**
+ * @brief Converts a MF specific binary operator into one of AST.
+ *
+ * @param op operator known by MF parser
+ *
+ * @return operator known by the abstract syntax tree
+ */
+RplASBinaryOp::BinOperator RplMFParser::convertBinaryOp(int op){
+ RplASBinaryOp::BinOperator rc;
switch(op){
case RplMFParser::O_ASSIGN:
rc = RplASBinaryOp::BOP_ASSIGN;
rc = RplASBinaryOp::BOP_ARTITH_RSHIFT;
break;
default:
+ throw RplException("unknown binary operator %d", op);
break;
}
return rc;
if (IS_BINARY_OP(opId)){
RplASBinaryOp* op = new RplASBinaryOp();
op->setPosition(m_lexer.currentPosition());
- op->setOperator(encodeBinOp(opId));
+ op->setOperator(convertBinaryOp(opId));
int prio = m_lexer.prioOfOp(token->id());
if (prio < lastPrio
RplASItem* parent);
RplASExprStatement*parseParameterList();
RplASItem* parseLocalVar();
+protected:
+ static RplASBinaryOp::BinOperator convertBinaryOp(int op);
+ static RplASUnaryOp::UnaryOp convertUnaryOp(int op);
private:
///syntax token builder.
/// Note: the super class contains a reference with the same name
= <test> (module) parent: $global
== Body:
Expr id: 6 expr: 2 succ: 0 <test>:1:1
- BinOp id: 2 op: + (26) left: 1 right: 4 <test>:1:1
+ BinOp id: 2 op: + (17) left: 1 right: 4 <test>:1:1
const id: 1 value: 2 <test>:0:0
- BinOp id: 4 op: * (30) left: 3 right: 5 <test>:1:3
+ BinOp id: 4 op: * (19) left: 3 right: 5 <test>:1:3
const id: 3 value: 3 <test>:1:2
const id: 5 value: 4 <test>:1:4
namedValue b id: 3 attr: 0x0 <test>:2:4
const id: 5 value: 10 <test>:2:11
const id: 6 value: 1 <test>:2:17
- Unary 7 op: - (27) expr: 8 <test>:2:24
+ Unary 7 op: - (2) expr: 8 <test>:2:24
const id: 8 value: 2 <test>:2:25
Expr id: 12 expr: 10 succ: 0 <test>:3:2
- BinOp id: 10 op: += (6) left: 9 right: 11 <test>:3:2
+ BinOp id: 10 op: += (2) left: 9 right: 11 <test>:3:2
namedValue a id: 9 attr: 0x0 <test>:3:2
const id: 11 value: 1 <test>:3:5
forC id: 3 var: 0 from: 0 to: 4 step: 0 body: 8 succ: 0 <test>:1:7
const id: 4 value: 10 <test>:1:14
Expr id: 8 expr: 6 succ: 0 <test>:1:22
- BinOp id: 6 op: += (6) left: 5 right: 7 <test>:1:22
+ BinOp id: 6 op: += (2) left: 5 right: 7 <test>:1:22
namedValue a id: 5 attr: 0x0 <test>:1:22
const id: 7 value: 1 <test>:1:25
namedValue x id: 3 attr: 0x0 <test>:2:4
namedValue a id: 5 attr: 0x0 <test>:2:11
Expr id: 9 expr: 7 succ: 0 <test>:3:2
- BinOp id: 7 op: += (6) left: 6 right: 8 <test>:3:2
+ BinOp id: 7 op: += (2) left: 6 right: 8 <test>:3:2
namedValue a id: 6 attr: 0x0 <test>:3:2
const id: 8 value: 1 <test>:3:5
varDef b id: 4 namedValue: 3 value: 0 succ: 10 <test>:2:4
namedValue b id: 3 attr: 0x0 <test>:2:4
Expr id: 10 expr: 6 succ: 11 <test>:3:2
- BinOp id: 6 op: = (5) left: 5 right: 8 <test>:3:2
+ BinOp id: 6 op: = (1) left: 5 right: 8 <test>:3:2
namedValue a id: 5 attr: 0x0 <test>:3:2
- BinOp id: 8 op: = (5) left: 7 right: 9 <test>:3:6
+ BinOp id: 8 op: = (1) left: 7 right: 9 <test>:3:6
namedValue b id: 7 attr: 0x0 <test>:3:6
const id: 9 value: 2 <test>:3:8
If id: 11 condition: 13 then: 20 else: 26 succ: 0<test>:3:11
- BinOp id: 13 op: < (21) left: 12 right: 14 <test>:4:6
+ BinOp id: 13 op: < (35) left: 12 right: 14 <test>:4:6
const id: 12 value: 11 <test>:4:3
const id: 14 value: 12 <test>:4:8
Expr id: 20 expr: 16 succ: 0 <test>:5:7
- BinOp id: 16 op: = (5) left: 15 right: 18 <test>:5:7
+ BinOp id: 16 op: = (1) left: 15 right: 18 <test>:5:7
namedValue a id: 15 attr: 0x0 <test>:5:7
- BinOp id: 18 op: * (30) left: 17 right: 19 <test>:5:12
+ BinOp id: 18 op: * (19) left: 17 right: 19 <test>:5:12
const id: 17 value: 13 <test>:5:9
const id: 19 value: 14 <test>:5:14
Expr id: 26 expr: 22 succ: 0 <test>:6:7
- BinOp id: 22 op: = (5) left: 21 right: 24 <test>:6:7
+ BinOp id: 22 op: = (1) left: 21 right: 24 <test>:6:7
namedValue a id: 21 attr: 0x0 <test>:6:7
- BinOp id: 24 op: / (28) left: 23 right: 25 <test>:6:12
+ BinOp id: 24 op: / (20) left: 23 right: 25 <test>:6:12
const id: 23 value: 15 <test>:6:9
const id: 25 value: 16 <test>:6:14
varDef x id: 2 namedValue: 1 value: 0 succ: 3 <test>:1:4
namedValue x id: 1 attr: 0x0 <test>:1:4
If id: 3 condition: 5 then: 10 else: 0 succ: 0<test>:1:7
- BinOp id: 5 op: < (21) left: 4 right: 6 <test>:2:5
+ BinOp id: 5 op: < (35) left: 4 right: 6 <test>:2:5
const id: 4 value: 7 <test>:2:3
const id: 6 value: 6 <test>:2:7
Expr id: 10 expr: 8 succ: 0 <test>:3:7
- BinOp id: 8 op: = (5) left: 7 right: 9 <test>:3:7
+ BinOp id: 8 op: = (1) left: 7 right: 9 <test>:3:7
namedValue x id: 7 attr: 0x0 <test>:3:7
const id: 9 value: '123' <test>:3:9
varDef rc id: 6 namedValue: 5 value: 0 succ: 7 <test>:2:4
namedValue rc id: 5 attr: 0x0 <test>:2:4
If id: 7 condition: 9 then: 14 else: 24 succ: 26<test>:2:8
- BinOp id: 9 op: <= (23) left: 8 right: 10 <test>:2:14
+ BinOp id: 9 op: <= (34) left: 8 right: 10 <test>:2:14
namedValue rc id: 8 attr: 0x0 <test>:2:14
const id: 10 value: 1 <test>:2:17
Expr id: 14 expr: 12 succ: 0 <test>:2:27
- BinOp id: 12 op: = (5) left: 11 right: 13 <test>:2:27
+ BinOp id: 12 op: = (1) left: 11 right: 13 <test>:2:27
namedValue rc id: 11 attr: 0x0 <test>:2:27
const id: 13 value: 1 <test>:2:29
Expr id: 24 expr: 16 succ: 0 <test>:2:39
- BinOp id: 16 op: = (5) left: 15 right: 18 <test>:2:39
+ BinOp id: 16 op: = (1) left: 15 right: 18 <test>:2:39
namedValue rc id: 15 attr: 0x0 <test>:2:39
- BinOp id: 18 op: * (30) left: 17 right: 19 <test>:2:42
+ BinOp id: 18 op: * (19) left: 17 right: 19 <test>:2:42
namedValue n id: 17 attr: 0x0 <test>:2:42
call fac Id: 19 args: 23 parent: 0 succ: 0 <test>:2:46
arg 1 id: 23 expr: 21 succ: 0
- BinOp id: 21 op: - (27) left: 20 right: 22 <test>:2:48
+ BinOp id: 21 op: - (18) left: 20 right: 22 <test>:2:48
namedValue n id: 20 attr: 0x0 <test>:2:48
const id: 22 value: 1 <test>:2:49
Expr id: 26 expr: 25 succ: 0 <test>:3:3
namedValue rc id: 18 attr: 0x0 <test>:3:5
namedValue a id: 20 attr: 0x0 <test>:3:11
If id: 21 condition: 23 then: 28 else: 0 succ: 30<test>:3:13
- BinOp id: 23 op: < (21) left: 22 right: 24 <test>:4:5
+ BinOp id: 23 op: < (35) left: 22 right: 24 <test>:4:5
namedValue a id: 22 attr: 0x0 <test>:4:5
namedValue b id: 24 attr: 0x0 <test>:4:9
Expr id: 28 expr: 26 succ: 0 <test>:4:17
- BinOp id: 26 op: = (5) left: 25 right: 27 <test>:4:17
+ BinOp id: 26 op: = (1) left: 25 right: 27 <test>:4:17
namedValue rc id: 25 attr: 0x0 <test>:4:17
namedValue b id: 27 attr: 0x0 <test>:4:20
Expr id: 30 expr: 29 succ: 0 <test>:5:3
Expr id: 20 expr: 7 succ: 0 <test>:3:3
call max Id: 7 args: 13 parent: 0 succ: 0 <test>:3:3
arg 1 id: 13 expr: 9 succ: 19
- BinOp id: 9 op: + (26) left: 8 right: 11 <test>:3:5
+ BinOp id: 9 op: + (17) left: 8 right: 11 <test>:3:5
const id: 8 value: 1 <test>:3:4
- BinOp id: 11 op: * (30) left: 10 right: 12 <test>:3:7
+ BinOp id: 11 op: * (19) left: 10 right: 12 <test>:3:7
const id: 10 value: 2 <test>:3:6
const id: 12 value: 3 <test>:3:8
arg 2 id: 19 expr: 15 succ: 0
- BinOp id: 15 op: ** (31) left: 14 right: 17 <test>:3:11
+ BinOp id: 15 op: ** (22) left: 14 right: 17 <test>:3:11
const id: 14 value: 4 <test>:3:10
- BinOp id: 17 op: - (27) left: 16 right: 18 <test>:3:15
+ BinOp id: 17 op: - (18) left: 16 right: 18 <test>:3:15
const id: 16 value: 5 <test>:3:14
const id: 18 value: 4 <test>:3:16
namedValue b id: 4 attr: 0x0 <test>:2:4
const id: 6 value: 100 <test>:2:8
Expr id: 9 expr: 7 succ: 12 <test>:2:13
- Unary 7 op: -- (41) expr: 8 <test>:2:13
+ Unary 7 op: -- (6) expr: 8 <test>:2:13
namedValue a id: 8 attr: 0x0 <test>:3:3
Expr id: 12 expr: 11 succ: 22
- Unary 11 op: ++ (40) expr: 10
+ Unary 11 op: ++ (5) expr: 10
namedValue b id: 10 attr: 0x0 <test>:4:1
Expr id: 22 expr: 15 succ: 34 <test>:5:3
- BinOp id: 15 op: * (30) left: 14 right: 18 <test>:5:3
- Unary 14 op: -- (41) expr: 13
+ BinOp id: 15 op: * (19) left: 14 right: 18 <test>:5:3
+ Unary 14 op: -- (6) expr: 13
namedValue a id: 13 attr: 0x0 <test>:5:1
- BinOp id: 18 op: ** (31) left: 16 right: 20 <test>:5:7
- Unary 16 op: ++ (40) expr: 17 <test>:5:4
+ BinOp id: 18 op: ** (22) left: 16 right: 20 <test>:5:7
+ Unary 16 op: ++ (5) expr: 17 <test>:5:4
namedValue b id: 17 attr: 0x0 <test>:5:7
- BinOp id: 20 op: - (27) left: 19 right: 21 <test>:5:11
+ BinOp id: 20 op: - (18) left: 19 right: 21 <test>:5:11
const id: 19 value: 8 <test>:5:10
const id: 21 value: 3 <test>:5:12
Expr id: 34 expr: 24 succ: 0 <test>:6:1
- BinOp id: 24 op: = (5) left: 23 right: 26 <test>:6:1
+ BinOp id: 24 op: = (1) left: 23 right: 26 <test>:6:1
namedValue a id: 23 attr: 0x0 <test>:6:1
- BinOp id: 26 op: = (5) left: 25 right: 28 <test>:6:3
+ BinOp id: 26 op: = (1) left: 25 right: 28 <test>:6:3
namedValue b id: 25 attr: 0x0 <test>:6:3
- BinOp id: 28 op: + (26) left: 27 right: 32 <test>:6:6
+ BinOp id: 28 op: + (17) left: 27 right: 32 <test>:6:6
namedValue a id: 27 attr: 0x0 <test>:6:6
- BinOp id: 32 op: * (30) left: 30 right: 33 <test>:6:12
- BinOp id: 30 op: - (27) left: 29 right: 31 <test>:6:9
+ BinOp id: 32 op: * (19) left: 30 right: 33 <test>:6:12
+ BinOp id: 30 op: - (18) left: 29 right: 31 <test>:6:9
namedValue b id: 29 attr: 0x0 <test>:6:9
const id: 31 value: 2 <test>:6:10
const id: 33 value: 3 <test>:6:13
varDef a id: 2 namedValue: 1 value: 0 succ: 3 <test>:1:4
namedValue a id: 1 attr: 0x0 <test>:1:4
repeat id: 3 condition: 8 body: 6 succ: 0 <test>:1:7
- BinOp id: 8 op: != (20) left: 7 right: 10 <test>:4:8
+ BinOp id: 8 op: != (33) left: 7 right: 10 <test>:4:8
namedValue a id: 7 attr: 0x0 <test>:4:8
- BinOp id: 10 op: * (30) left: 9 right: 11 <test>:4:13
+ BinOp id: 10 op: * (19) left: 9 right: 11 <test>:4:13
const id: 9 value: 2 <test>:4:11
const id: 11 value: 3 <test>:4:15
Expr id: 6 expr: 5 succ: 0
- Unary 5 op: ++ (40) expr: 4
+ Unary 5 op: ++ (5) expr: 4
namedValue a id: 4 attr: 0x0 <test>:3:1
namedValue a id: 1 attr: 0x0 <test>:1:4
const id: 3 value: 20 <test>:1:8
while id: 4 condition: 6 body: 11 succ: 0 <test>:1:12
- BinOp id: 6 op: < (21) left: 5 right: 7 <test>:2:8
+ BinOp id: 6 op: < (35) left: 5 right: 7 <test>:2:8
const id: 5 value: 3 <test>:2:6
const id: 7 value: 5 <test>:2:10
Expr id: 11 expr: 9 succ: 0 <test>:3:3
- BinOp id: 9 op: = (5) left: 8 right: 10 <test>:3:3
+ BinOp id: 9 op: = (1) left: 8 right: 10 <test>:3:3
namedValue a id: 8 attr: 0x0 <test>:3:3
const id: 10 value: 7 <test>:3:5