forked from AbhiTaker/Pseudo-Code-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
288 lines (250 loc) · 5.89 KB
/
main.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include<bits/stdc++.h>
using namespace std;
set<string> data_type, condition, loop;
set<char> useless, bracket;
map<string, string> map_variable, keyword,condmap;
int indent;
void intialize()
{
/* store the appropriate keyword in appropriate set*/
data_type = {"int", "float", "string", "char", "double"};
useless = {'<', '>' , ';' , ',', ' ', '"' };
condition = {"if", "else"};
loop = {"for", "while", "do"};
keyword = { {"int", "Integer"}, {"char", "Character"}, {"string", "String"}, {"float", "Float"} };
condmap = {{">"," greater "}, {"<", " less than "}, {"!=", " not equal "}, {"==", " equals "}, {">=", " greater than or equal to "}, {"<=", "less than or equal to"}};
bracket = {'(', ')', '{', '}'};
}
void func_cin(string line) {
int i = 0;
while(i<line.size() && useless.find(line[i])!=useless.end()) //Covering up the spaces
i++;
i = i + 3; // covering up 'cin' i.e 3 words;
int flag = 0;
while(i < line.size())
{
string temp = "";
while(i < line.size() && useless.find(line[i])!=useless.end())
i++;
if(i>=line.size())
break;
while(i < line.size() && !(useless.find(line[i])!=useless.end()))
{
temp += line[i];
i++;
}
for(int j=0; j<indent; j++)
cout<<" ";
cout << "Enter ";
cout<< map_variable[temp]<<" " <<temp<<"\n";
}
}
void func_cout(string line) {
int i = 0;
while(i<line.size() && line[i]==' ') //Covering up the spaces
i++;
i = i + 4; // covering up 'cin' i.e 3 words;
while(i < line.size())
{
string temp = "";
while(i < line.size() && useless.find(line[i])!=useless.end())
i++;
if(i>=line.size())
break;
while(i < line.size() && !(useless.find(line[i])!=useless.end()))
{
temp += line[i];
i++;
}
for(int j=0; j<indent; j++)
cout<<" ";
cout << "Print ";
cout<< map_variable[temp]<<" "<<temp<<"\n";
}
}
void func_return(string line)
{
int i = 0;
while(i<line.size() && line[i]==' ') //Covering up the spaces
i++;
for(int j=0; j<indent; j++)
cout<<" ";
cout << "function Returns " ;
i = i + 6; //covering up return
string temp;
while(i < line.size() && useless.find(line[i])!=useless.end())
i++;
temp = line[i];
cout << temp << "\n";
}
void func_dt(string line)
{
int i = 0;
while(i<line.size() && useless.find(line[i])!=useless.end()) //Covering up the spaces
i++;
string Data_type="";
while(i<line.size() && !(useless.find(line[i])!=useless.end()))
{
Data_type += line[i];
i++;
}
int ctr = 0;
vector<string> var_name;
while(i < line.size())
{
string temp = "";
while(i<line.size() && useless.find(line[i])!=useless.end())
i++;
if(i>=line.size())
break;
int detect_function = 0;
while(i<line.size() && !(useless.find(line[i])!=useless.end()))
{
if(line[i]=='(')
{
detect_function = 1;
break;
}
temp += line[i];
i++;
}
if(detect_function)
{
for(i=0; i<indent; i++)
cout<<" ";
cout<<"Declare Function "<<temp<<"\n";
break;
}
map_variable[temp] = keyword[Data_type];
var_name.push_back(temp);
}
if(!var_name.empty())
{
for(i=0; i<indent; i++)
cout<<" ";
cout<<"Declare "<<var_name.size()<<" "<<keyword[Data_type]<<" variable ";
for(auto value: var_name)
cout<<value<<" ";
cout<<"\n";
}
}
void func_cond(string line)
{
int i = 0;
for(int j=0; j<indent; j++)
cout<<" ";
while(line[i]!='(')
i++;
i++;
cout<<"Check if ";
while(line[i] != ')')
{
cout<<line[i];
i++;
}
cout<<"\n";
for(int j=0; j<indent+1; j++)
cout<<" ";
cout<<"If True then : ";
cout<<"\n";
}
void func_loop(string line)
{
int i=0;
for(int j=0; j<indent; j++)
cout<<" ";
cout<<"Start a loop from ";
while(line[i]!='(')
i++;
i++;
while(line[i]!=';')
{
cout<<line[i];
i++;
}
i++;
cout<<" till ";
while(line[i]!=';')
{
cout<<line[i];
i++;
}
cout<<"\n";
}
void func_oper(string line)
{
int i=0;
for(int j=0; j<indent; j++)
cout<<" ";
cout<<"Perform operation: ";
string temp="";
while(line[i]==' ')
i++;
while(i<line.size() && (!(useless.find(line[i])!=useless.end()) || line[i]==' '))
{
temp += line[i];
i++;
}
cout<<temp<<"\n";
}
void check(string word, string line)
{
if(word == "cin") {
func_cin(line);
}
else if(word == "cout") {
func_cout(line);
}
else if(word == "return") {
func_return(line);
}
else if(data_type.find(word) != data_type.end()) {
func_dt(line);
}
else if(condition.find(word) != condition.end()) {
func_cond(line);
}
else if(loop.find(word) != loop.end()) {
func_loop(line);
}
else if(map_variable.find(word) != map_variable.end())
{
func_oper(line);
}
else {
//cout<<"Expand your code \n";
}
}
int main()
{
ifstream inFile("prog.cpp");
intialize();
string line;
int i = 0;
while (inFile)
{
getline(inFile, line);
i = 0;
string word = "";
while(i<line.size() && line[i]==' ')
i++;
while(i<line.size() && line[i]!=' ')
{
word += line[i];
i++;
}
if(word=="{")
{
indent++;
continue;
}
if(word=="}")
{
indent--;
continue;
}
check(word, line);
}
inFile.close();
return 0;
}