-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugcode_future.cpp
More file actions
231 lines (228 loc) · 9.48 KB
/
debugcode_future.cpp
File metadata and controls
231 lines (228 loc) · 9.48 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <fstream>
std::shared_ptr<LexicalAnalyzer> l = nullptr;
SyntaxAnalyzer* s = nullptr;
std::unordered_map<int,std::string> revMap;
#include "debugdata_future.cpp"
std::string debugLexemes(std::vector<LexicalAnalyzer::Lexeme> const& lexemes, bool print = true) {
std::stringstream sstream;
size_t ptr = 0;
for (auto i :lexemes) {
sstream << ptr++ << ":\t{ type:" << typetable[i.type] << ","
<< "value:";
switch (i.type) {
case CPlus::Keyword:
sstream << keywordtable[i.value];
break;
case CPlus::SymbolOperator:
sstream << operatortable[i.value] << " [WARNING!!!!] ";
break;
case CPlus::Operator:
sstream << operatortable[i.value];
break;
case CPlus::Symbol:
sstream << symboltable[i.value];
break;
case CPlus::Identifier:{
if (revMap[i.value] != "")
sstream << revMap[i.value];
else
sstream << i.value;
} break;
default:
sstream << i.value;
break;
}
sstream << " }" << std::endl;
}
if (print)
std::cerr << sstream.str();
return sstream.str();
};
std::string debugIdentifier(std::shared_ptr<SyntaxAnalyzer::IdentifierASTNode>& idkp, bool print = true) {
std::stringstream sstream;
auto& idk = *idkp;
sstream << "{\"type\":\"" << identifiertypetable[idk.getIdentifierType()] << "\",\"id\":" << idk.getIdentifierId() << ",\"name\":\"" << revMap[idk.getIdentifierId()] << "\"";
if (!idk.getDimensions().empty()) {
sstream << ",\"dimensions\":[";
bool first = true;
for (uint32_t dim :idk.getDimensions()) {
sstream << (first ? "" : ",");
sstream << dim;
first = false;
}
sstream << "]";
}
sstream << ",\"baseOffset\":" << idk.getBaseOffsetRef() << ",\"size\":" << idk.getSize() << ",\"static\":" << idk.isStatic();
sstream << "}";
if (print)
std::cerr << sstream.str();
return sstream.str();
};
std::string debugAST(std::shared_ptr<SyntaxAnalyzer::BaseASTNode>& ast, bool print = true) {
std::stringstream sstream;
std::string prefix = "";
if (ast == nullptr) {
sstream << "null";
return sstream.str();
}
sstream << "{\"type\":\"" << astnodetypetable[ast->getNodeType()] << "\",";
switch (ast->getNodeType()) {
case CPlus::BinaryExpressionASTNode:{
SyntaxAnalyzer::BinaryExpressionASTNode& bn = *std::dynamic_pointer_cast<SyntaxAnalyzer::BinaryExpressionASTNode>(ast);
sstream << "\"op\":\"" << operatortable[bn.getOperatorRef()] << "\",";
sstream << "\"left\":";
sstream << debugAST(bn.getLeftRef(),false);
sstream << ",\"right\":";
sstream << debugAST(bn.getRightRef(),false);
sstream << "}";
} break;
case CPlus::IdentifierASTNode:{
std::shared_ptr<SyntaxAnalyzer::IdentifierASTNode> inp = std::dynamic_pointer_cast<SyntaxAnalyzer::IdentifierASTNode>(ast);
sstream << "\"identifier\":";
sstream << debugIdentifier(inp,false);
sstream << "}";
} break;
case CPlus::IntConstantASTNode:{
std::shared_ptr<SyntaxAnalyzer::IntConstantASTNode> icp = std::dynamic_pointer_cast<SyntaxAnalyzer::IntConstantASTNode>(ast);
sstream << "\"value\":" << icp->getValue() << "}";
} break;
case CPlus::FunctionCallASTNode:{
std::shared_ptr<SyntaxAnalyzer::FunctionCallASTNode> fcp = std::dynamic_pointer_cast<SyntaxAnalyzer::FunctionCallASTNode>(ast);
sstream << "\"function\":\"" << revMap[fcp->getFuncId()] << "\",\"funcId\":" << fcp->getFuncId() << ",";
sstream << "\"arguments\":[";
bool first = true;
for (auto arg :fcp->getArgumentsRef()) {
sstream << (first ? "" : ",");
sstream << debugAST(arg,false);
first = false;
}
sstream << "]}";
} break;
case CPlus::IfStatementASTNode:{
SyntaxAnalyzer::IfStatementASTNode& stmt = *std::dynamic_pointer_cast<SyntaxAnalyzer::IfStatementASTNode>(ast);
sstream << "\"condition\":"
<< debugAST(stmt.getConditionRef(),false);
sstream << ",\"truthy\":";
sstream << debugAST(stmt.getTruthyStmtRef(),false);
sstream << ",\"falsy\":";
sstream << debugAST(stmt.getFalsyStmtRef(),false);
sstream << "}";
} break;
case CPlus::WhileStatementASTNode:{
SyntaxAnalyzer::WhileStatementASTNode& stmt = *std::dynamic_pointer_cast<SyntaxAnalyzer::WhileStatementASTNode>(ast);
sstream << "\"condition\":"
<< debugAST(stmt.getConditionRef(),false);
sstream << ",\"loop\":";
sstream << debugAST(stmt.getLoopStmtRef(),false);
sstream << "}";
} break;
case CPlus::ForStatementASTNode:{
SyntaxAnalyzer::ForStatementASTNode& stmt = *std::dynamic_pointer_cast<SyntaxAnalyzer::ForStatementASTNode>(ast);
sstream << "\"init\":";
sstream << debugAST(stmt.getInitRef(),false);
sstream << ",\"condition\":"
<< debugAST(stmt.getConditionRef(),false);
sstream << ",\"iter\":";
sstream << debugAST(stmt.getIterationRef(),false);
sstream << ",\"loop\":";
sstream << debugAST(stmt.getLoopStmtRef(),false);
sstream << "}";
} break;
case CPlus::BlockASTNode:{
SyntaxAnalyzer::BlockASTNode& stmt = *std::dynamic_pointer_cast<SyntaxAnalyzer::BlockASTNode>(ast);
sstream << "\"stmts\":[";
bool first = true;
for (auto arg :stmt.getStatementsRef()) {
sstream << (first ? "" : ",");
sstream << debugAST(arg, false);
first = false;
}
sstream << "]}";
} break;
case CPlus::ReturnStatementASTNode:{
SyntaxAnalyzer::ReturnStatementASTNode& stmt = *std::dynamic_pointer_cast<SyntaxAnalyzer::ReturnStatementASTNode>(ast);
sstream << "\"expr\":";
sstream << debugAST(stmt.getExprRef(),false);
sstream << "}";
} break;
default:
sstream << "\"unknown\": true}";
break;
}
if (print)
std::cerr << sstream.str();
return sstream.str();
};
std::string debugASTs(std::vector<std::shared_ptr<SyntaxAnalyzer::BaseASTNode> >& asts, bool print = true) {
std::stringstream sstream;
int i = 0;
for (auto ast :asts) {
sstream << i++ << ":\t" << debugAST(ast, false) << std::endl;
}
if (print)
std::cerr << sstream.str();
return sstream.str();
}
int main() {
int n = 0;
std::cin >> n;
std::queue<int> inputData;
for (size_t i = 0, k; i < n; i++) {
std::cin >> k;
inputData.push(k);
}
std::stringstream testCpp;
testCpp << std::cin.rdbuf();
std::cerr << "Source: \n" << testCpp.str() << std::endl;
l = std::make_shared<LexicalAnalyzer>(testCpp.str());
l->analyze();
for (auto kv : l->getIdentifierMap()) {
revMap[kv.second] = kv.first;
}
debugLexemes(l->getLexemesRef());
s = new SyntaxAnalyzer(l);
/*std::cerr << "BeginDeclarationAnalyze" << std::endl;
size_t ptr = 0;
auto x = SyntaxAnalyzer::declarationAnalyze(l->getLexemesRef(),ptr,0);
for (auto idkp :x.second) {
std::cerr << debugIdentifier(idkp, false) << std::endl;
}
std::cerr << "EndDeclarationAnalyze,ptr=" << ptr << std::endl;
std::cerr << "BeginExpressionAnalyze" << std::endl;
std::shared_ptr<SyntaxAnalyzer::Scope> scope = std::make_shared<SyntaxAnalyzer::Scope>(nullptr);
{
scope->setIdentifier(CPlus::Cin,std::make_shared<SyntaxAnalyzer::IdentifierASTNode>(CPlus::BuiltInObjects,CPlus::Cin,0,true));
scope->setIdentifier(CPlus::Cout,std::make_shared<SyntaxAnalyzer::IdentifierASTNode>(CPlus::BuiltInObjects,CPlus::Cout,0,true));
scope->setIdentifier(CPlus::Endl,std::make_shared<SyntaxAnalyzer::IdentifierASTNode>(CPlus::BuiltInObjects,CPlus::Endl,0,true));
scope->setIdentifier(CPlus::Putchar,std::make_shared<SyntaxAnalyzer::IdentifierASTNode>(CPlus::Function,CPlus::Putchar,0,true));
std::vector<std::shared_ptr<SyntaxAnalyzer::IdentifierASTNode>> putcharArgs;
std::vector<std::shared_ptr<SyntaxAnalyzer::BaseASTNode>> putcharStmt;
putcharArgs.push_back(std::make_shared<SyntaxAnalyzer::IdentifierASTNode>(0,0,false));
scope->setFunction(CPlus::Putchar,std::make_shared<SyntaxAnalyzer::Function>(CPlus::Putchar,std::move(putcharArgs),nullptr));
}
for (auto idk :x.second) {
int id = idk->getIdentifierId();
if (id >= CPlus::_PredefinedIdentifierUpperBound)
scope->setIdentifier(id,std::move(idk));
}
ptr = 40;
auto y = SyntaxAnalyzer::expressionAnalyze(l->getLexemesRef(),ptr,scope);
debugAST(y);
std::cerr << "EndExpressionAnalyze,ptr=" << ptr << std::endl;
*/
s->analyze();
std::cerr << "Build succeeded!" << std::endl;
bool first = true;
std::cout << "{";
for (auto it = s->getGlobalScope()->funcBegin(); it != s->getGlobalScope()->funcEnd(); it++) {
auto f = (*it).second;
if (!first) std::cout << ",";
first = false;
std::cout << "\"" << revMap[f->getFuncId()] << "\":{\"id\":" << f->getFuncId() << ",\"name\":\"" << revMap[f->getFuncId()]
<< "\",\"stackSize\":" << f->getMaxStackSizeRef() << ",\"ast\":";
auto t = std::dynamic_pointer_cast<SyntaxAnalyzer::BaseASTNode>
(f->getStatementsRef());
std::cout << debugAST(t,false) << "}";
}
std::cout << "}";
}