-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyab2cpp.cpp
285 lines (265 loc) · 5.74 KB
/
yab2cpp.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
/*
** Yab2Cpp
**
** Transpiler by Samuel D. Crow
**
** Based on Yab
**
*/
#include "yab2cpp.h"
#include <cassert>
unordered_map<string, unique_ptr<variableType> >globals;
unordered_map<string, unique_ptr<variableType> >locals;
unordered_map<string, unique_ptr<variableType> >statics;
/* These correspond to the enum COMPILE_ERRORS. */
const char *COMPILE_ERROR_NAMES[]={
"no error",
"incorrect syntax",
"wrong type",
"failed allocation",
"stack underflow",
"internal compiler error",
"duplicated label",
"previous subroutine didn't end",
"value returned from gosub call",
"undefined subroutine name",
"too many parameters in function call",
"value cannot be assigned",
"undimensioned array or undeclared function",
"return code was not specified on function",
"wrong number of dimensions when accessing array"
};
/* These correspond to the types of enum TYPES. */
const string TYPENAMES[]={
"unknown",
"none",
"string constant",
"integer constant",
"floating point constant",
"string variable",
"integer variable",
"floating point variable",
"string array or function",
"integer array or function",
"floating point array or function",
"string array or function",
"function"
};
const string CODETYPES[]={
"print sequence",
"print segment",
"while loop",
"for loop",
"repeat loop",
"do loop",
"if statement",
"procedure statement",
"function statement",
"assignment",
"label",
"parameter list or array index",
"data item",
"function returning string",
"function returning floating point",
"function returning integer",
"function returning nothing",
"function"
};
enum COMPILE_ERRORS errorLevel=E_OK;
unsigned int indentLevel=0;
bool scopeGlobal=true;
fn *currentFunc=nullptr;
bool COMPILE=false;
bool DUMP=false;
bool DEBUG=false;
bool TRACE=false;
ifstream src;
ofstream output_cpp;
ofstream funcs_h;
ofstream heap_h;
ofstream consts_h;
ofstream logfile;
ofstream varNames;
/* private prototypes */
void helpText(const string);
void setup();
void compile();
void shutDown();
/* process command line parameters */
int main(int argc, char *argv[])
{
switch (argc)
{
case 1:
COMPILE=true;
cout << "\nCompile initiated." << endl;
compile();
break;
case 2:
if (argv[1][0]=='-')
{
switch (argv[1][1])
{
case 'd':
cout << "\nIdentifier dump initiated." << endl;
DUMP=true;
compile();
break;
case 'v':
cout << "\n" << argv[0] << " version "
<< VER_MAJOR << "." << VER_MINOR << "." << VER_RELEASE << endl;
break;
case 'V':
cout << "\nVerbose compile initiated." << endl;
DUMP=true;
COMPILE=true;
compile();
break;
case 'D':
cout << "\nCompiler debug and dump mode initiated." << endl;
DUMP=true;
DEBUG=true;
compile();
break;
case 'G':
cout << "\nDebug, dump and compile initiated." << endl;
DUMP=true;
DEBUG=true;
COMPILE=true;
compile();
break;
case 't':
cout << "\nDebug, dump and trace initiated." << endl;
DEBUG=true;
DUMP=true;
TRACE=true;
compile();
break;
default:
helpText(argv[0]);
break;
}
}
break;
default:
helpText(argv[0]);
break;
}
cout << "program exiting" <<endl;
return 0;
}
/* print the help text to stdout */
void helpText(const string commandname)
{
cout << commandname << "[-d|D|V|v|G|t] < filename.mb\n" <<
"Compiles filename.mb by default unless a flag is specified.\n" <<
"\n The optional flags are as follows:\n" <<
"-d is a dump of build to the parse.log file.\n" <<
"-D is a dump of identifiers and logged build.\n" <<
"-V is for a verbose build where the compiler logs and compiles.\n" <<
"-v prints the version and exits.\n" <<
"-t activates dump, debug and trace\n" <<
"-G activates dump, debug and compile all at once.\n" << endl;
}
/* open files and initialize them*/
void setUp()
{
if (COMPILE)
{
/* compile mode */
output_cpp.open("output/output.cpp");
funcs_h.open("output/functions.h");
consts_h.open("output/consts.h");
heap_h.open("output/heap.h");
output_cpp << "#include \"../runtime/runtime.h\"\n#include \"consts.h\"\n"
<< "#include \"heap.h\"\n#include \"functions.h\"\n"
<< "unsigned int state=START;\nunsigned int run(){\n"
<< "while (state>=START){\n"
<< "switch(state){\ncase START:" << endl;
}
else
{
output_cpp.open("/dev/null");
funcs_h.open("/dev/null");
consts_h.open("/dev/null");
heap_h.open("/dev/null");
}
if (DUMP)
{
varNames.open("varnames.log");
}
else
{
varNames.open("/dev/null");
}
if (DEBUG)
{
/* dump identifier mode */
logfile.open("parse.log");
logger("Setup complete.");
}
else
{
logfile.open("/dev/null");
}
}
[[noreturn]] void error(enum COMPILE_ERRORS e)
{
errorLevel=e;
shutDown();
exit(1);
}
void indent()
{
unsigned int count=indentLevel;
while (count > 0)
{
logfile << '\t';
--count;
}
}
/* write a note in the logfile */
void logger(string s)
{
if (DEBUG)
{
indent();
logfile << s << "\n";
}
}
/* shutdown the compiler and exit */
void shutDown()
{
if (errorLevel != E_OK) cerr << "\nERROR: "
<< COMPILE_ERROR_NAMES[errorLevel] << "\n\n" << endl;
logger("Shutting Down\n");
if (DUMP)
{
varNames << "Global Variables\n";
for(auto iter=globals.begin(); iter!=globals.end(); ++iter)
{
varNames << "variable " << iter->first
<< " has ID " << iter->second->getID() << "\n";
}
varNames << endl;
label::dumpLabels();
}
if (COMPILE)
{
output_cpp << "default:\nstate=UNDEFINED_STATE_ERROR;\n"
<< "break;\n}\n}\nreturn state;\n}"<< endl;
funcs_h.flush();
consts_h.flush();
heap_h.flush();
}
globals.clear();
locals.clear();
statics.clear();
tempVar::eraseQueues();
}
/* open files and compile */
void compile()
{
setUp();
shutDown();
}