-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParser.cpp
More file actions
236 lines (212 loc) · 4.31 KB
/
Parser.cpp
File metadata and controls
236 lines (212 loc) · 4.31 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
#include "Parser.h"
const char* PREFIX =
"#include <iostream>\n"
"int main()\n"
"{\n"
"char buffers[131072];\n"
"for(int i = 0; i < 131072; i++) buffers[i] = 0;\n"
"unsigned long p = 0;\n"
"\n\n";
const char* SUFFIX =
"std::cout << std::endl;\n"
"return 0;\n"
"}";
Parser::Parser()
{
}
Parser::Parser(std::string& input)
{
m_input = input;
}
void Parser::Parse()
{
size_t len = m_input.length();
for(size_t i = 0; i < len; i++) ///< Loop through all the instructions.
{
char inst = m_input.at(i); ///< Fetch the next instruction.
switch(inst)
{
case '+':
m_instructions.push_back(ADD);
break;
case '-':
m_instructions.push_back(SUB);
break;
case '>':
m_instructions.push_back(NEXT);
break;
case '<':
m_instructions.push_back(PREV);
break;
case '[':
m_instructions.push_back(L_BEG);
break;
case ']':
m_instructions.push_back(L_END);
break;
case '.':
m_instructions.push_back(OUTPUT);
break;
case ',':
m_instructions.push_back(INPUT);
break;
case '{':
m_instructions.push_back(I_BEG);
break;
case '}':
m_instructions.push_back(I_END);
break;
case '(':
m_instructions.push_back(I_ELSE);
break;
case ')':
m_instructions.push_back(I_ENDELSE);
break;
case '!':
m_instructions.push_back(I_NOT);
break;
case '/':
m_instructions.push_back(I_GT);
break;
case '\\':
m_instructions.push_back(I_LT);
break;
case '=':
m_instructions.push_back(I_EQ);
break;
case '#':
m_instructions.push_back(I_NEQ);
break;
case ';':
m_instructions.push_back(BREAK);
break;
case ':':
m_instructions.push_back(CONTINUE);
default:
break;
}
}
}
void Parser::Build(Stdin in)
{
m_output.append(PREFIX);
size_t len = m_instructions.size();
for(size_t i = 0; i < len; i++)
{
Instruction inst = m_instructions[i];
switch(inst)
{
case ADD:
if(m_instructions[i + 1] != ADD) ///< If there's no point in optimizing.
{
m_output.append("buffers[p]++;\n");
break;
}
int c = 0;
bool br = false;
for(int ci = 1; !br; ci++) ///< Loop through the ADD instructions
{
if(m_instructions[i + ci] == ADD)
c++;
else
br = true;
}
std::stringstream ss;
std::string s;
ss << "buffers[p] += " << (c + 1) << ";\n";
ss >> s;
m_output.append(s);
i += c;
break;
case SUB:
if(m_instructions[i + 1] != ADD) ///< If there's no point in optimizing.
{
m_output.append("buffers[p]++;\n");
break;
}
int c = 0;
bool br = false;
for(int ci = 1; !br; ci++) ///< Loop through the ADD instructions
{
if(m_instructions[i + ci] == ADD)
c++;
else
br = true;
}
std::stringstream ss;
std::string s;
ss << "buffers[p] += " << (c + 1) << ";\n";
ss >> s;
m_output.append(s);
i += c;
break;
case NEXT:
m_output.append("if(p<131072)p++;else p=0;\n");
break;
case PREV:
m_output.append("if(p>0)p--;else p=131072;\n");
break;
case L_BEG:
m_output.append("while(buffers[p] != 0) {\n");
break;
case L_END:
m_output.append("}\n");
break;
case I_BEG:
m_output.append("if(buffers[p] ");
switch(m_instructions[i-1]) ///< If modifiers.
{
case I_NOT:
m_output.append("!= 0");
break;
case I_GT:
m_output.append(" > buffers[p+1]");
break;
case I_LT:
m_output.append(" < buffers[p+1]");
break;
case I_EQ:
m_output.append(" == buffers[p+1]");
break;
case I_NEQ:
m_output.append(" != buffers[p+1]");
break;
default:
break;
}
m_output.append(")\n\t{\n");
break;
case I_END:
m_output.append("}\n");
break;
case I_ELSE:
m_output.append("else\n{\n");
break;
case I_ENDELSE:
m_output.append("}");
break;
case OUTPUT:
m_output.append("std::cout << (char)buffers[p];\n");
break;
case INPUT:
m_output.append("std::cin >> buffers[p];\n");
break;
case BREAK:
m_output.append("break;\n");
break;
case CONTINUE:
m_output.append("continue;\n");
default:
break;
}
}
m_output.append(SUFFIX);
}
void Parser::SetInput(std::string& input)
{
m_input = input;
}
std::string& Parser::GetOutput()
{
return m_output;
}