-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
239 lines (205 loc) · 5.69 KB
/
compiler.cpp
File metadata and controls
239 lines (205 loc) · 5.69 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
232
233
234
235
236
237
238
239
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <cstdlib>
///
using namespace std;
# define LEN(ITEM) ((int)ITEM.size())
#define TERMENATOR ':'
set<string> BLOCK_ELEMENTS={"address","article","aside","blockquote","body","br","caption","div","dl","fieldset","footer","form","h1","h2","h3","h4","h5","h6","header","hr","main","nav","ol","p","pre","section","table","tbody","td","th","thead","ul"};
int getLeftCount(string s){
int c=0;
for(auto i:s){if(i!=' ') break; if(i==' ') c++;}
return c;
}
int CheckTAG(string s){
int n=(int)s.size()-1;int i=n;
{
// removing all the trailing spaces
while(i>=0 ){if(s[i]==' ') i--;else break; }
if(s[i]==TERMENATOR) return i;
}
return -1;
}
string getTAG(string s){
int n=LEN(s);string tag="";
int i=0;
// remove l spaces
while (i<n && s[i]==' ')
{i++;}
while(i<n) {
if(s[i]==' '||s[i]==TERMENATOR) break;
tag+=s[i];i++;}
return tag;
}
string getCompleteStartTag(string s){
int n=LEN(s);
int terminator=CheckTAG(s);
int start=0;
string ans="";
string tag="";
// remove l spaces
while (start<n && s[start]==' ')
{start++;}
for(int i=start;i<(terminator);i++){
ans+=s[i];
}
return ans;
}
string addPadding(string input,bool end=false){
string toadd="<";
if(end) toadd="</";
return toadd+input+">";
}
string sanitizeLine(string input){
string ans="";
for(auto i:input){
// if(i=='"') ans+=;
// if(i=='\'') ans+="\";
ans+=i;
}
return ans;
}
string GetPyIndentaion(string s,int min_indent){
string ans="";
for(int i=0;i<LEN(s);i++){
while(i<min_indent) i++;
ans+=s[i];
}
return ans;
}
int runPY(){
int returnValue = std::system("python3 pyrun.py");
// Check the return value to determine if the execution was successful
if (returnValue == 0) {
// Python script executed successfully
// You can add further logic here
} else {
// An error occurred during execution
// You can handle the error or log it
}
return returnValue;
}
void processString(list<string>::iterator itr, list<pair<string,int>> <,list<pair<string,int>>::iterator &it){
string s=(*itr);
int count=getLeftCount(s);
//means we are deaing with tag
auto startTag=addPadding( getCompleteStartTag(s));
auto endTag=addPadding(getTAG(s),true);
if(lt.begin()!=it){
if((*it).second<count){
// meaning we are inserting inside the outer parent
// nothing special here
}
else if((*it).second==count){
// dealing with sibling
//finding parent
while((*it).second==count){advance(it,1);}
}
else{
// dealing with new parent
//get the upper parrent
// count<(*it).second
while((*it).second>=count){advance(it,1);}
}
}
else{
// no special condition is applied
// handling the first insertion
}
// doing the nessacery insertion
lt.insert(it,{startTag,count});
lt.insert(it,{endTag,count});
advance(it,-1);
}
void makePYrunFILE(vector<string> v,int pyindent){
ofstream myfile;
myfile.open("pyrun.py",ios::out|ios::trunc);
myfile<<"\nimport sys\noriginal_stdout = sys.stdout\noutput_file = open('pyout.txt', 'w')\nsys.stdout = output_file\n";
for(auto i:v) {myfile<<GetPyIndentaion(i,pyindent)<<endl;}
myfile<<"\nsys.stdout = original_stdout\noutput_file.close()";
myfile.close();
}
string processIndentaionPython(string s,int pycount){
string ans="";
for(int i=0;i<=pycount;i++){
ans+=" ";
}
ans+=s;
return ans;
}
void processPythonFILE(list<string>::iterator &it,list<string> &input, int pycount){
ifstream inputfile("pyout.txt");
int cnt=0;
string s;
while(getline(inputfile,s)){
string ans=(processIndentaionPython(s,pycount));
input.insert(it,ans);
cnt++;
}
advance(it,cnt*-1);
inputfile.close();
}
string processPythonString(list<string>::iterator &it,list<string> &input){
string s=(*it);
int count=getLeftCount(s);
vector<string> pyLines;
string py=getTAG(s);
int pyCount=count;
int pyindent=INT_MAX;
//cout<<py;
// getline(cin,py);
++it;
py=(*it);
do{
count=getLeftCount(py);
pyindent=min(count,pyindent);
pyLines.push_back(py);
++it;
py=(*it);
count=getLeftCount(py);
}while((count>pyCount));
s=py;// giving the last line for further processing;
makePYrunFILE(pyLines,pyindent);
if(runPY()==0){
// successful execution of python file
processPythonFILE(it,input,pyCount);
}
return s;
}
int main(int n , char** args){
// Check if Python was initialized successfully
// Finalize the Python interpreter when done
auto file=freopen("html.html", "r", stdin);
auto out_file=freopen("output.html", "w", stdout);
string s;int count=0;
list<pair<string,int>> lt;
auto it=lt.begin();
int preTagIndentaion=0;
list<string> input;
while(getline(cin,s)){
input.push_back(s);
}
for(auto itr=input.begin();itr!=input.end();itr++){
string s=(*itr);
if(LEN(s)==0) continue;
// getting indentaion count
//s=sanitizeLine(s);
//printf("%s %d\n",s.c_str(),count);
if(CheckTAG(s)!=-1){
string Tag=getTAG(s);
if(Tag=="python"){
processPythonString(itr,input);}
else if(Tag=="global"){
}
processString(itr, lt,it);}
else{
// simple line is recieved and we will add it as it is
lt.insert(it,{s,count});
}
// detect and seprate python
//$
}
for(auto [i,j]:lt) cout<<i<<endl;
return 0;
}