-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostfix.hpp
More file actions
108 lines (98 loc) · 2.88 KB
/
postfix.hpp
File metadata and controls
108 lines (98 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#include <vector>
#include <stack>
#include <iostream>
#include "classes.hpp"
#include "priority.hpp"
namespace pl {
auto CpClearOpratorStack(std::vector<pl::RtData>& CpPostfixing, std::stack<std::string>& CpOperator) -> void {
while (!CpOperator.empty()) {
if (CpOperator.top() == "(") return;
if (CpOperator.top() == "{") return;
CpPostfixing.push_back(RtData(1, CpOperator.top()));
CpOperator.pop();
}
}
auto CpPostFix(std::vector<pl::ParseTree>& parsedTree) -> void {
for (size_t pst = 0; pst < parsedTree.size(); pst++) {
auto& nowline = parsedTree[pst];
std::vector<pl::RtData> CpPostfixing;
std::stack<std::string> CpOperator;
std::stack<size_t> CpParenthese;
std::stack<size_t> CpBraces;
for (size_t i = 0; i < nowline.codes.size(); i++) {
auto& now = nowline.codes[i];
if (now.type == 1) {
if (now.data == ",") {
size_t paramCnt = CpParenthese.top();
CpParenthese.pop();
CpParenthese.push(paramCnt + 1);
CpClearOpratorStack(CpPostfixing, CpOperator);
}
else if (now.data == "{") {
CpBraces.push(CpPostfixing.size());
CpOperator.push("{");
}
else if (now.data == "}") {
CpClearOpratorStack(CpPostfixing, CpOperator);
RtVar commands(6, "<Commands>");
size_t len = CpPostfixing.size() - CpBraces.top();
size_t start = CpBraces.top();
CpBraces.pop();
for (size_t i = 0; i < len; i++) {
commands.commands.push_back(CpPostfixing[start]);
CpPostfixing.erase(CpPostfixing.begin() + start);
}
CpOperator.pop();
CpPostfixing.push_back(RtData(3, commands));
}
else if (now.data == ";") {
CpClearOpratorStack(CpPostfixing, CpOperator);
}
else if (now.data == "(") {
CpOperator.push("(");
CpParenthese.push(-1);
}
else if (now.data == ")") {
size_t paramCnt = CpParenthese.top();
CpParenthese.pop();
if (paramCnt == -1) {
CpClearOpratorStack(CpPostfixing, CpOperator);
CpOperator.pop();
continue;
}
CpClearOpratorStack(CpPostfixing, CpOperator);
CpPostfixing.push_back(RtData(5, std::to_string(paramCnt)));
CpOperator.pop();
CpPostfixing.push_back(RtData(4, CpOperator.top()));
CpOperator.pop();
}
else {
while (true) {
if (CpOperator.empty()) break;
if (pl::priority(CpOperator.top()) >= pl::priority(now.data)) {
CpPostfixing.push_back(RtData(1, CpOperator.top()));
CpOperator.pop();
}
else break;
}
CpOperator.push(now.data);
continue;
}
}
else if (now.type == 4) {
CpOperator.push(now.data);
CpOperator.push("(");
CpParenthese.push(1);
i++;
}
else {
CpPostfixing.push_back(now);
}
}
CpClearOpratorStack(CpPostfixing, CpOperator);
nowline.codes.clear();
nowline.codes = CpPostfixing;
}
}
}