| 107 | } |
| 108 | |
| 109 | std::shared_ptr<IR::Stm> CodeParser::parse() { |
| 110 | std::vector<Token *> parseStack; |
| 111 | std::vector<IR::Stm *> stmBuf; |
| 112 | std::shared_ptr<IR::Stm> tree; |
| 113 | |
| 114 | parseStack.push_back(new BeginToken()); |
| 115 | int nextToken = 0; |
| 116 | |
| 117 | while (true) { |
| 118 | parsingTableEntry e = this->lookupParsingTable(parseStack.back()->state, this->tokenList[nextToken]->kind, parseStack.back()->pos); |
| 119 | if (e.action == parsingTableEntry::Action::SHIFT) { |
| 120 | this->tokenList[nextToken]->state = e.num; |
| 121 | parseStack.push_back(this->tokenList[nextToken]); |
| 122 | if (this->tokenList[nextToken]->kind == Token::Kind::STM) { |
| 123 | IR::SimpleStm *sstm = new IR::SimpleStm(((StmToken *)(this->tokenList[nextToken]))->sstm); |
| 124 | stmBuf.push_back(sstm); |
| 125 | } |
| 126 | nextToken++; |
| 127 | } |
| 128 | else if (e.action == parsingTableEntry::Action::REDUCE) { |
| 129 | IR::Stm *tmpStm; |
| 130 | int back = stmBuf.size() - 1; |
| 131 | if (e.num == 4) |
| 132 | { |
| 133 | assert(stmBuf[back]->kind != IR::Stm::Kind::SEQ); |
| 134 | IR::Stm *newStm = new IR::SeqStm(stmBuf[back]); |
| 135 | stmBuf.pop_back(); |
| 136 | stmBuf.push_back(newStm); |
| 137 | } |
| 138 | else if (e.num == 5) { |
| 139 | assert(stmBuf[back]->kind == IR::Stm::Kind::SEQ); |
| 140 | assert(stmBuf[back-1]->kind != IR::Stm::Kind::SEQ); |
| 141 | IR::Stm *seq = stmBuf[back]; |
| 142 | ((IR::SeqStm *)seq)->seq.insert(((IR::SeqStm *)seq)->seq.begin(), stmBuf[back - 1]); |
| 143 | stmBuf.pop_back(); |
| 144 | stmBuf.pop_back(); |
| 145 | stmBuf.push_back(seq); |
| 146 | } |
| 147 | else if (e.num == 3) { |
| 148 | assert(stmBuf[back]->kind == IR::Stm::Kind::SEQ); |
| 149 | assert(parseStack[parseStack.size() - 4]->kind == Token::Kind::COND); |
| 150 | std::string cond = ((CondToken *)(parseStack[parseStack.size() - 4]))->cond; |
| 151 | IR::Stm *newStm = new IR::WhileStm(cond, stmBuf[back]); |
| 152 | stmBuf.pop_back(); |
| 153 | stmBuf.push_back(newStm); |
| 154 | } |
| 155 | else if (e.num == 2) { |
| 156 | assert(stmBuf[back]->kind == IR::Stm::Kind::SEQ); |
| 157 | assert(stmBuf[back-1]->kind == IR::Stm::Kind::SEQ); |
| 158 | assert(parseStack[parseStack.size() - 8]->kind == Token::Kind::COND); |
| 159 | std::string cond = ((CondToken *)(parseStack[parseStack.size() - 8]))->cond; |
| 160 | IR::Stm *newStm = new IR::IfStm(cond, stmBuf[back - 1], stmBuf[back]); |
| 161 | stmBuf.pop_back(); |
| 162 | stmBuf.pop_back(); |
| 163 | stmBuf.push_back(newStm); |
| 164 | } |
| 165 | else if (e.num == 1) { |
| 166 | assert(stmBuf[back]->kind == IR::Stm::Kind::SEQ); |
no test coverage detected