From: hama Date: Sat, 5 Jul 2014 23:55:24 +0000 (+0200) Subject: day's work X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=23c8a7936c817bb198ffba07ba49165f4ac05172;p=reqt day's work --- diff --git a/rplexpr/rplastree.cpp b/rplexpr/rplastree.cpp index bd6e7ef..827ce7e 100644 --- a/rplexpr/rplastree.cpp +++ b/rplexpr/rplastree.cpp @@ -397,6 +397,14 @@ unsigned int RplASItem::id() const return m_id; } +/** + * @brief Resets the static id counter. + */ +void RplASItem::reset() +{ + m_nextId = 1; +} + /** @class RplASExpr rplastree.hpp "rplexpr/rplastree.hpp" * @@ -666,6 +674,15 @@ RplASNode1::~RplASNode1() { delete m_child; m_child = NULL; +} +/** + * @brief Returns the child. + * + * @return the child of the instance + */ +RplASItem*RplASNode1::child() const +{ + } /** * @brief Sets the child. @@ -1702,14 +1719,15 @@ void RplASBinaryOp::dump(FILE* fp, int indent) DEFINE_TABS(indent); const QByteArray& opName = RplLexer::m_active->nameOfOp(m_operator); char buffer[256]; - fprintf(fp, "%sBinOp %d op: %s (%d) left: %d right: %d %s\n", tabs, m_id, + fprintf(fp, "%sBinOp id: %d op: %s (%d) left: %d right: %d %s\n", + tabs, m_id, opName.constData(), m_operator, m_child == NULL ? 0 : m_child->id(), m_child2 == NULL ? 0 : m_child2->id(), m_position->utf8(buffer, sizeof buffer)); - if (m_child != NULL) + if (indent < 32 && m_child != NULL) m_child->dump(fp, indent + 1); - if (m_child2 != NULL) + if (indent < 32 && m_child2 != NULL) m_child2->dump(fp, indent + 1); } diff --git a/rplexpr/rplastree.hpp b/rplexpr/rplastree.hpp index 2401042..cf0a566 100644 --- a/rplexpr/rplastree.hpp +++ b/rplexpr/rplastree.hpp @@ -128,6 +128,8 @@ public: * @param header NULL or the headline */ virtual void dump(FILE* fp, int indent) = 0; +public: + static void reset(); protected: unsigned int m_id:16; RplASItemType m_nodeType:8; @@ -165,6 +167,7 @@ public: RplASNode1(RplASItemType type); virtual ~RplASNode1(); public: + RplASItem* child() const; void setChild(RplASItem* child); protected: RplASItem* m_child; diff --git a/rplexpr/rplmfparser.cpp b/rplexpr/rplmfparser.cpp index 50950fc..a856d64 100644 --- a/rplexpr/rplmfparser.cpp +++ b/rplexpr/rplmfparser.cpp @@ -328,10 +328,15 @@ RplASItem* RplMFParser::parseTerm(int depth){ && ! m_lexer.isRightAssociative(opId))){ op->setChild(top); top = op; - } else{ + } else if (prio > lastPrio){ RplASBinaryOp* top2 = dynamic_cast(op); op->setChild(top2->child2()); top2->setChild2(op); + } else { + // right assoc: + RplASBinaryOp* top2 = dynamic_cast(top); + op->setChild(top2->child2()); + top2->setChild2(op); } lastPrio = prio; op->setChild2(parseOperand(depth)); @@ -378,7 +383,7 @@ RplASItem* RplMFParser::parseExpr() */ RplASItem* RplMFParser::parseExprStatement() { - RplASItem* item = parseExpr(); + RplASItem* item = parseTerm(0); RplASExprStatement* statement = new RplASExprStatement(); statement->setPosition(item->position()); statement->setChild(item); diff --git a/test/rplmfparser/ifTest1.txt b/test/rplmfparser/ifTest1.txt index 05afd0c..8c10fd5 100644 --- a/test/rplmfparser/ifTest1.txt +++ b/test/rplmfparser/ifTest1.txt @@ -12,18 +12,20 @@ fi varDef a (Int) id: 1 succ: 2 attr: 0x0 :1:4 varDef b (Int) id: 2 succ: 8 attr: 0x0 :2:4 Expr id: 8 succ: 9 expr: 4 :3:2 - BinOp 4 op: = (5) left: 3 right: 5 :3:2 + BinOp id: 4 op: = (5) left: 3 right: 6 :3:2 namedValue a id: 3 attr: 0x0 :3:2 - namedValue b id: 5 attr: 0x0 :3:6 + BinOp id: 6 op: = (5) left: 5 right: 7 :3:6 + namedValue b id: 5 attr: 0x0 :3:6 + const id: 7 value: 2 :3:8 If id: 9 condition: 11 then: 18 else: 24 :3:11 - BinOp 11 op: < (21) left: 10 right: 12 :4:6 + BinOp id: 11 op: < (21) left: 10 right: 12 :4:6 const id: 10 value: 11 :4:3 const id: 12 value: 12 :4:8 Expr id: 18 succ: 0 expr: 14 :5:7 - BinOp 14 op: = (5) left: 13 right: 15 :5:7 + BinOp id: 14 op: = (5) left: 13 right: 15 :5:7 namedValue a id: 13 attr: 0x0 :5:7 const id: 15 value: 13 :5:9 Expr id: 24 succ: 0 expr: 20 :6:7 - BinOp 20 op: = (5) left: 19 right: 21 :6:7 + BinOp id: 20 op: = (5) left: 19 right: 21 :6:7 namedValue a id: 19 attr: 0x0 :6:7 const id: 21 value: 15 :6:9 diff --git a/test/rplmfparser/ifTest2.txt b/test/rplmfparser/ifTest2.txt index 509fbe2..1393a56 100644 --- a/test/rplmfparser/ifTest2.txt +++ b/test/rplmfparser/ifTest2.txt @@ -1,14 +1,17 @@ -Str x; if 7 < 6 then x = '123'; fi +Str x; +if 7 < 6 +then x = '123'; +fi = (module) parent: global == Classes: == Variables: == Body: -varDef x (Str) id: 25 succ: 26 attr: 0x0 :1:4 -If id: 26 condition: 28 then: 33 else: 0 :1:7 - BinOp 28 op: < (21) left: 27 right: 29 :1:12 - const id: 27 value: 7 :1:10 - const id: 29 value: 6 :1:14 - Expr id: 33 succ: 0 expr: 31 :1:23 - BinOp 31 op: = (5) left: 30 right: 32 :1:23 - namedValue x id: 30 attr: 0x0 :1:23 - const id: 32 value: '123' :1:25 +varDef x (Str) id: 1 succ: 2 attr: 0x0 :1:4 +If id: 2 condition: 4 then: 9 else: 0 :1:7 + BinOp id: 4 op: < (21) left: 3 right: 5 :2:5 + const id: 3 value: 7 :2:3 + const id: 5 value: 6 :2:7 + Expr id: 9 succ: 0 expr: 7 :3:7 + BinOp id: 7 op: = (5) left: 6 right: 8 :3:7 + namedValue x id: 6 attr: 0x0 :3:7 + const id: 8 value: '123' :3:9 diff --git a/test/rplmfparser/opTest1.txt b/test/rplmfparser/opTest1.txt new file mode 100644 index 0000000..01bd9b1 --- /dev/null +++ b/test/rplmfparser/opTest1.txt @@ -0,0 +1,9 @@ +Int a; +--aa; +aa++; +a--+3*4 += (module) parent: global +== Classes: +== Variables: +== Body: +varDef a (Int) id: 1 succ: 0 attr: 0x0 :1:4 diff --git a/test/rplmfparser/whileTest.txt b/test/rplmfparser/whileTest.txt index d5744d3..bddc933 100644 --- a/test/rplmfparser/whileTest.txt +++ b/test/rplmfparser/whileTest.txt @@ -1,13 +1,18 @@ +Int a = 20; +while 3 < 5 do + a = 7 +od = (module) parent: global == Classes: == Variables: == Body: -varDef a (Int) id: 1 succ: 3 attr: 0x0 - const id: 2 value: 20 -while id: 3 condition: 5 body: 9 - BinOp 5 op: < (21) left: 4 right: 6 - const id: 4 value: 3 - const id: 6 value: 5 - Expr id: 9 succ: 0 expr: 7 - BinOp 7 op: = (5) left: 0 right: 8 - const id: 8 value: 7 +varDef a (Int) id: 1 succ: 3 attr: 0x0 :1:4 + const id: 2 value: 20 :1:8 +while id: 3 condition: 5 body: 10 :1:12 + BinOp id: 5 op: < (21) left: 4 right: 6 :2:8 + const id: 4 value: 3 :2:6 + const id: 6 value: 5 :2:10 + Expr id: 10 succ: 0 expr: 8 :3:3 + BinOp id: 8 op: = (5) left: 7 right: 9 :3:3 + namedValue a id: 7 attr: 0x0 :3:3 + const id: 9 value: 7 :3:5 diff --git a/unittests/rplmfparser_test.cpp b/unittests/rplmfparser_test.cpp index 244a0f0..b1a8c2e 100644 --- a/unittests/rplmfparser_test.cpp +++ b/unittests/rplmfparser_test.cpp @@ -27,6 +27,7 @@ public: } protected: void setSource(const char* content){ + RplASItem::reset(); m_currentSource = content; m_tree.clear(); m_source.clear(); @@ -70,25 +71,25 @@ public: RplMFParser parser(m_source, m_tree); parser.parse(); checkAST("ifTest1.txt", __LINE__); - setSource("Str x; if 7 < 6 then x = '123'; fi"); + setSource("Str x;\nif 7 < 6\nthen x = '123';\nfi"); parser.parse(); checkAST("ifTest2.txt", __LINE__); } void whileTest(){ - setSource("Int a = 20; while 3 < 5 do a = 7 od"); + setSource("Int a = 20;\nwhile 3 < 5 do\n a = 7\nod"); RplMFParser parser(m_source, m_tree); parser.parse(); checkAST("whileTest.txt", __LINE__); } void repeatTest(){ - setSource("Int a; repeat a++; until a != 2 * 3;"); + setSource("Int a;\nrepeat\na++;\nuntil a != 2 * 3;"); RplMFParser parser(m_source, m_tree); parser.parse(); checkAST("repeatTest.txt", __LINE__); } void forCTest(){ - setSource("Int a; for b from 1 to 10 step 2 do a += 1; od"); + setSource("Int a;\nfor b from 1 to 10 step 2 do\na += 1;\nod"); RplMFParser parser(m_source, m_tree); parser.parse(); checkAST("forC1.txt", __LINE__); @@ -96,8 +97,15 @@ public: parser.parse(); checkAST("forC2.txt", __LINE__); } + void opTest(){ + setSource("--aa;\naa++;\na--+3*4"); + RplMFParser parser(m_source, m_tree); + parser.parse(); + checkAST("opTest1.txt", __LINE__); + } virtual void doIt(void) { + opTest(); ifTest(); whileTest(); repeatTest();