-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.cpp
396 lines (338 loc) · 11.8 KB
/
translator.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// ****************************************************************************
// ****************************************************************************
// translator.cpp
// ****************************************************************************
//
// ****************************************************************************
// ****************************************************************************
// ****************************************************************************
// Includes
// ****************************************************************************
#include "common.h"
// ****************************************************************************
// Initialize Static Members
// ****************************************************************************
comString Translator::m_typeNames[];
// ****************************************************************************
// Translator::init()
// ****************************************************************************
void
Translator::init()
{
m_typeNames[Translator::Int] = "int";
m_typeNames[Translator::Str] = "string";
m_typeNames[Translator::Bool] = "bool";
m_typeNames[Translator::Float] = "real";
m_typeNames[Translator::Any] = "any";
m_typeNames[Translator::None] = "none";
}
// ****************************************************************************
// Translator::getType()
// ****************************************************************************
Translator::Type
Translator::getType(const comString& spelling)
{
if (spelling == "i" || spelling == "int") {
return Translator::Int;
} else if (spelling == "r" || spelling == "real") {
return Translator::Float;
} else if (spelling == "b" || spelling == "bool") {
return Translator::Bool;
} else if (spelling == "s" || spelling == "string") {
return Translator::Str;
} else if (spelling == "@" || spelling == "any") {
return Translator::Any;
} else if (spelling == "~" || spelling == "none") {
return Translator::None;
} else {
return (Translator::Type) -1;
}
}
// ****************************************************************************
// Translator::run()
// ****************************************************************************
void
Translator::run()
{
// Run the type check before we translate. We do this here because the
// parser can successfully parse without getting types matched.
Parser::typeCheck();
printf("Running translator.\n");
FILE* output = fopen("translated.forth", "w");
VarDef::printVarDefHeader(output);
FuncDef::printFuncDefHeader(output);
fprintf(output, ": main\n");
run(Parser::m_tree, output);
fprintf(output, "\n; main");
fclose(output);
printf("Done running translator.\n");
}
void
Translator::run(ParseTree* tree,
FILE* output)
{
// If this node is a function invocation, we need to assign parameters.
if (!tree->isTerminal() && tree->m_production->isFuncInvocation()) {
translateFuncInvocation(tree, output);
return;
}
// If this node does not have a scheme...
if (!tree->getScheme()) {
// ... and it's a terminal, just print it.
if (tree->isTerminal()) {
comString buf;
fprintf(output, "%s ", (const char*) translateSpelling(tree, buf));
return;
}
// If there's no token, then this is an epsilon branch.
if (!tree->getToken())
return;
// Otherwise error out.
comString buf;
fprintf(stderr, "Nonliteral tree node without scheme \"%s\"\n", (const char*) tree->getProduction()->printable(buf));
Global::fail();
}
// Loop through the translation scheme's elements.
PoVec& post = tree->getScheme()->getPostVec();
for (UINT i = 0; i < post.getNumEntries(); i++) {
PoUnit* p = post[i];
if (p->m_isKeyword) {
// If the unit is a keyword, just print it.
fprintf(output, "%s ", (const char*) p->m_keyword);
} else {
// If the unit is an index, recurse into that child of the tree.
run(tree->getChild(p->m_index), output);
}
}
}
// ****************************************************************************
// Translator::translateSpelling()
// ****************************************************************************
comString&
Translator::translateSpelling(ParseTree* tree,
comString& buf)
{
Token* token = tree->getToken();
switch (token->getType()) {
case Token::BoolConst:
if (token->getSpelling() == "true")
return buf = "-1";
else
return buf = "0";
break;
case Token::StrConst:
{
char cBuf[64];
sprintf(cBuf, "s\" %s", ((const char*) token->getSpelling()) + 1);
return buf = cBuf;
}
break;
case Token::Identifier:
{
VarDef* vd = tree->findVarDef(token->getSpelling());
if (vd) {
return buf = vd->m_postName;
} else {
fprintf(stderr, "Failed to find variable definition for identifier \"%s\" on line %d.\n", (const char*) token->getSpelling(), token->getLine());
Global::fail();
}
}
case Token::RealConst:
if (!strstr(token->getSpelling(), "e")) {
char cBuf[64];
sprintf(cBuf, "%se", ((const char*) token->getSpelling()));
return buf = cBuf;
}
break;
default:
break;
}
return buf = token->getSpelling();
}
// ****************************************************************************
// Translator::getPrimType()
// ****************************************************************************
Translator::Type
Translator::getPrimType(const comString& spelling)
{
if (spelling == "int")
return Translator::Int;
else if (spelling == "real")
return Translator::Float;
else if (spelling == "bool")
return Translator::Bool;
else if (spelling == "string")
return Translator::Str;
else {
fprintf(stderr, "WARNING: Tried to parse \"%s\" into primitive type.\n", (const char*) spelling);
return Translator::None;
}
}
// ****************************************************************************
// Translator::buildFunction()
// ****************************************************************************
void
Translator::buildFunction(ParseTree* tree,
UINT& lastLine)
{
// Admittedly a hack.
tree->forceScopeEdge();
// We know that this is a parse tree using the production
// let ( ( id FUNLIST ) ( type FUNTYPE ) ) EXPR EXPRLIST
// Immediately create a function definition.
const comString& fId = tree->getChild(3)->getToken()->getSpelling();
Translator::Type type = Translator::getPrimType(tree->getChild(7)->getToken()->getSpelling());
char prefix[64];
sprintf(prefix, "f%p_%s", tree, (const char*) fId);
FuncDef* def = new FuncDef(fId, prefix, type, tree);
tree->addFuncDef(def);
// Look for parameters.
const comString* identifier;
VarDef* vDef = NULL;
ParseTree* iRec = tree->getChild(4);
ParseTree* tRec = tree->getChild(8);
bool foundIdentifier = false;
bool foundType = false;
char postName[64];
while (true) {
foundIdentifier = false;
foundType = false;
// If this node has children, this node has a parameter id. Its
// production looks like
// id FUNLIST
if (iRec->getNumChildren()) {
identifier = &iRec->getChild(0)->getToken()->getSpelling();
iRec = iRec->getChild(1);
foundIdentifier = true;
}
// If this node has children, this node has a parameter type. Its
// production looks like
// type FUNTYPE
if (tRec->getNumChildren()) {
type = Translator::getPrimType(tRec->getChild(0)->getToken()->getSpelling());
tRec = tRec->getChild(1);
foundType = true;
}
if (foundType && foundIdentifier) {
// We got a type and an identifier. Make them into a parameter.
sprintf(postName, "p%s_%s", prefix, (const char*) *identifier);
vDef = new VarDef(*identifier, postName, type);
tree->forceVarDef(vDef);
def->m_params.append(vDef);
} else if (foundType || foundIdentifier) {
// We got one or the other but not both. Error out.
fprintf(stderr, "Mismatched number of parameter types and identifiers for function \"%s\"\n", (const char*) fId);
Global::fail();
} else {
// Otherwise we didn't get either and will stop looking for parameters.
break;
}
}
// Now that we've made the declaration. Let's typecheck the definition
// itself.
tree->getChild(11)->typeCheck(lastLine);
tree->getChild(12)->typeCheck(lastLine);
}
// ****************************************************************************
// Translator::typeCheckFunction()
// ****************************************************************************
void
Translator::typeCheckFunction(ParseTree* tree,
UINT& lastLine)
{
// Find the function definition. This node looks like
// id PARAMLIST
// So the first child will be the function's identifier token.
FuncDef* def = tree->findFuncDef(tree->getChild(0)->getToken()->getSpelling());
tree->m_type = def->m_type;
// Now that we have the definition, make sure that all of the parameters
// are supplied.
if (def) {
VDVec& params = def->m_params;
ParseTree* pRec = tree->getChild(1);
UINT pThis = 0;
UINT pNext = 1;
// The param list nodes look like
// <param> PARAMLIST
// Use the automatic typechecker for the parameter and then loop on the
// PARAMLIST
for (UINT i = 0; i < params.getNumEntries(); i++) {
if (!pRec) {
fprintf(stderr, "Call to function \"%s\" on line %d has too few parameters.\n",
(const char*) tree->getChild(0)->getToken()->getSpelling(),
tree->getChild(0)->getToken()->getLine());
Global::fail();
}
// It is possible that the first token is ( in the production
// ( OPER ) PARAMLIST
// If this is the case, just typecheck on the next token.
if (pRec->getChild(0)->getToken()->getType() == Token::Paren) {
pThis = 1;
pNext = 3;
}
pRec->getChild(pThis)->typeCheck(lastLine);
if (pRec->getChild(pThis)->m_type != params[i]->m_type) {
fprintf(stderr, "Parameter \"%s\" (%d) for call to function \"%s\" on line %d failed typecheck. Got %s, expected %s\n",
(const char*) params[i]->m_preName,
i,
(const char*) tree->getChild(pThis)->getToken()->getSpelling(),
tree->getChild(pThis)->getToken()->getLine(),
(const char*) typeToString(pRec->getChild(pThis)->m_type),
(const char*) typeToString(params[i]->m_type));
Global::fail();
}
pRec = pRec->getChild(pNext);
}
} else {
fprintf(stderr, "Attempted to call undeclared function \"%s\" on line %d.\n",
(const char*) tree->getChild(0)->getToken()->getSpelling(),
tree->getChild(0)->getToken()->getLine());
Global::fail();
}
}
// ****************************************************************************
// Translator::translateFuncInvocation()
// ****************************************************************************
void
Translator::translateFuncInvocation(ParseTree* tree,
FILE* output)
{
FuncDef* def = tree->findFuncDef(tree->getChild(0)->getToken()->getSpelling());
VDVec& params = def->m_params;
ParseTree* pRec = tree->getChild(1);
UINT pThis = 0;
UINT pNext = 1;
// The param list nodes look like
// <param> PARAMLIST
// Use the automatic translator to translate the first param, then print
// the post-translation identifier and then finally print the assignment
// operator appropriate for the type.
for (UINT i = 0; i < params.getNumEntries(); i++) {
// It is possible that the first token is ( in the production
// ( OPER ) PARAMLIST
// If this is the case, just typecheck on the next token.
if (pRec->getChild(0)->getToken()->getType() == Token::Paren) {
pThis = 1;
pNext = 3;
}
run(pRec->getChild(pThis), output);
fprintf(output, "%s ", (const char*) params[i]->m_postName);
switch (params[i]->m_type) {
case Translator::Bool:
case Translator::Int:
fprintf(output, "! ");
break;
case Translator::Str:
fprintf(output, "2! ");
break;
case Translator::Float:
fprintf(output, "f! ");
break;
default:
fprintf(stderr, "Unexpected param type");
Global::fail();
}
pRec = pRec->getChild(pNext);
}
fprintf(output, "%s ", (const char*) def->m_postName);
}