-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.cpp
492 lines (472 loc) · 16.3 KB
/
translate.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include "ast.h"
#include <sstream>
#include <cassert>
#include <stack>
#include <set>
#include <iostream>
#include <fstream>
#include <map>
set<string> builtinFunctionNames = { "print", "input", "push", "pop", "insert", "remove", "parseInt", "parseReal" };
string AST::translate() const {
stringstream ss;
TranslatePath translatePath;
ss << "#include \"./../y_lib.h\"\n"; // WILL BE REPLACED WITH <> ONCE STUCTURE IS ADDED TO THE PATH
// spaces
SpaceReference spaceReference;
pullSpaces( ss, translatePath, spaceReference );
referenceSpaces( ss, spaceReference );
// functions
pullFunctions( ss, translatePath );
// main
ss << "int main( int argc, char **argv ) {\n";
// do global stuff
ss << "\tv_argc = argc;\n\tfor(int i = 0; i < argc; i++ ) v_argv.push_back(std::move(t_string(argv[i])));\n\trandom_generator.seed(std::chrono::high_resolution_clock::now().time_since_epoch().count());\n\t";
// translate main
translateBlock( ss, translatePath, 1 );
ss<<"\n}\n";
return ss.str();
}
string AST::decodeTypename( const TranslatePath& translatePath ) const {
string r;
bool comma = false;
if( type == AT_ARRAY ) {
r = "t_tuple<";
for( AST* child : children ) {
if( comma )
r += ",";
comma = true;
r += child->decodeTypename( translatePath );
}
r += ">";
} else if( type == AT_DATATYPE ) {
if( val == "set" || val == "list" ) {
r = "t_" + val + "<";
for( AST* child : children ) {
if( comma )
r += ",";
comma = true;
r += child->decodeTypename( translatePath );
}
r += ">";
} else
r = findSpaceName( val, translatePath );
} else
translate_exception( "Node " + to_string(type) + " is not a typename" );
return r;
}
bool AST::isSpace() const {
return ( type == AT_DATATYPE && !( nonFinalTypenames.count( val ) || finalTypenames.count( val ) ) );
}
void AST::printFunctionHeader( stringstream& ss, string x, const TranslatePath& translatePath ) const {
assert( type == AT_FUNCTIONDEF );
ss << children.at(0)->decodeTypename( translatePath );
if( val.at(0) == '@' )
ss << " " << val.substr( 1 ) << "( ";
else
ss << " f_" << x << "_" << val << "( ";
bool comma = false;
for( AST* argument : children.at(1)->children ) {
if( comma )
ss << ", ";
comma = true;
ss << argument->children.at(0)->decodeTypename( translatePath );
ss << " v_" << argument->val;
}
ss << " )";
}
void AST::inducedFunctions( stringstream& ss, const string& f, const TranslatePath& translatePath ) {
assert( type == AT_ARRAY );
if( f == "@operator<" ) {
ss << "t_bool operator>( " << children.at(1)->children.at(0)->decodeTypename( translatePath ) << " y, "
<< children.at(0)->children.at(0)->decodeTypename( translatePath ) << " x ) { return x < y; }\n";
ss << "t_bool operator<=( " << children.at(0)->children.at(0)->decodeTypename( translatePath ) << " x, "
<< children.at(0)->children.at(0)->decodeTypename( translatePath ) << " y ) { return !(y < x); }\n";
ss << "t_bool operator>=( " << children.at(0)->children.at(0)->decodeTypename( translatePath ) << " x, "
<< children.at(0)->children.at(0)->decodeTypename( translatePath ) << " y ) { return !(x < y); }\n";
ss << "t_bool operator!=( " << children.at(0)->children.at(0)->decodeTypename( translatePath ) << " x, "
<< children.at(0)->children.at(0)->decodeTypename( translatePath ) << " y ) { return (x<y)&&(y<x); }\n";
ss << "t_bool operator==( " << children.at(0)->children.at(0)->decodeTypename( translatePath ) << " x, "
<< children.at(0)->children.at(0)->decodeTypename( translatePath ) << " y ) { return (!(x<y))&&(!(y<x)); }\n";
} else if( f == "@operator==" ) {
ss << "t_bool operator!=( " << children.at(0)->children.at(0)->decodeTypename( translatePath ) << " x, "
<< children.at(0)->children.at(0)->decodeTypename( translatePath ) << " y ) { return !(x==y); }\n";
}
}
void AST::pullFunctions( stringstream& ss, TranslatePath& translatePath ) const {
assert( type == AT_SYNCBLOCK || type == AT_ASYNCBLOCK );
translatePath.push( this );
for( AST* node : children ) {
if( node->type == AT_FUNCTIONDEF ) {
node->printFunctionHeader( ss, val, translatePath );
ss << ";" << endl;
node->children.at(1)->inducedFunctions( ss, node->val, translatePath );
node->children.at(2)->pullFunctions( ss, translatePath );
} else if( node->type == AT_SYNCBLOCK || node->type == AT_ASYNCBLOCK )
node->pullFunctions( ss, translatePath );
}
for( AST* node : children ) {
if( node->type == AT_FUNCTIONDEF ) {
node->printFunctionHeader( ss, val, translatePath );
node->children.at(2)->translateBlock( ss, translatePath, 0 );
ss << endl;
}
}
translatePath.pop();
}
void AST::pullSpaces( stringstream& ss, TranslatePath& translatePath, SpaceReference& spaceReference ) const {
assert( type == AT_SYNCBLOCK || type == AT_ASYNCBLOCK );
translatePath.push( this );
for( AST* node : children ) {
if( node->type == AT_SPACEDEF ) {
ss << "struct s_" << val << "_" << node->val << ";\n";
} else if( node->type == AT_SYNCBLOCK || node->type == AT_ASYNCBLOCK )
node->pullSpaces( ss, translatePath, spaceReference );
}
// all typenames are known here
for( AST* node : children ) {
if( node->type == AT_SPACEDEF ) {
vector<pair<string,string>> memberNames;
assert( node->children.at(0)->type == AT_ARRAY );
string name = "s_" + val + "_" + node->val;
ss << "struct " << name << "{ ";
size_t i = 0;
for( AST* member : node->children.at(0)->children ) {
assert( member->type == AT_VARIABLEDEF );
string sTypename = member->children.at(0)->decodeTypename( translatePath );
ss << sTypename << " v_" << member->val << "; ";
memberNames.push_back( make_pair( sTypename, "v_" + member->val ) );
if( spaceReference.size() <= i )
spaceReference.emplace_back();
spaceReference.at(i).insert( make_pair( name, make_pair( sTypename, "v_" + member->val ) ) );
i++;
}
ss << "\n\t" << name << "()=default; " << name << "(" << name << "&&)=default; " << name << "(const " << name << "&)=default; ";
ss << name << "& operator=(const " << name << "&)=default; " << name << "& operator=(" << name << "&&)=default; ~" << name << "()=default;";
ss << "\n\t" << name << "(const t_tuple<";
bool comma = false;
for( auto& param : memberNames ) {
if( comma )
ss << ",";
comma = true;
ss << param.first;
}
ss << ">& x) : ";
i = 0;
comma = false;
for( auto& param : memberNames ) {
if( comma )
ss << ",";
comma = true;
ss << param.second << "(std::get<" << i << ">(x))";
i++;
}
ss << "{}\n\t";
ss << name << "& operator=(t_tuple<";
comma = false;
for( auto& param : memberNames ) {
if( comma )
ss << "&&,";
comma = true;
ss << param.first;
}
ss << "> x) { ";
i = 0;
for( auto& param : memberNames )
ss << param.second << "=std::get<" << i++ << ">(x); ";
ss << "return *this; }\n};\n";
}
}
translatePath.pop();
}
void referenceSpaces( stringstream& ss, const SpaceReference& spaceReference ) {
ss << "template<size_t> struct space {};\n";
for( size_t i = 0; i < spaceReference.size(); i++ ) {
ss << "template<> struct space<" << i << "> {\n";
for( const auto& p : spaceReference.at( i ) )
ss << "\tstatic constexpr " << p.second.first << "& from( " << p.first << "& x ) { return x." << p.second.second << "; }\n";
ss << "};\n";
}
}
void AST::translateBlock( stringstream& ss, TranslatePath& translatePath, int indent ) const {
assert( type == AT_SYNCBLOCK || type == AT_ASYNCBLOCK );
ss << " {\n"; // add stuff for threading here
translatePath.push( this );
for( AST *node : children ) {
if( node->type != AT_FUNCTIONDEF && node->type != AT_SPACEDEF )
node->translateItem( ss, translatePath, indent + 1 );
}
for( int i = 0; i < indent; i++ )
ss << "\t";
ss << "}";
translatePath.pop();
}
string AST::findFunctionName( string name, TranslatePath translatePath ) {
if( builtinFunctionNames.count( name ) )
return "f_0_" + name;
while( !translatePath.empty() ) {
for( const AST* item: translatePath.top()->children ) {
if( item->type == AT_FUNCTIONDEF && item->val == name )
return "f_" + translatePath.top()->val + "_" + name;
}
translatePath.pop();
}
return "";
} // empty string means not found
string AST::findSpaceName( string name, TranslatePath translatePath ) {
if( finalTypenames.count( name ) || nonFinalTypenames.count( name ) )
return "t_" + name;
while( !translatePath.empty() ) {
for( const AST* item: translatePath.top()->children ) {
if( item->type == AT_SPACEDEF && item->val == name )
return "s_" + translatePath.top()->val + "_" + name;
}
translatePath.pop();
}
return "";
} // empty string means not found
AST* AST::findFunctionType( string name, TranslatePath translatePath ) {
while( !translatePath.empty() ) {
for( const AST* item: translatePath.top()->children ) {
if( item->type == AT_FUNCTIONDEF && item->val == name )
return new AST( *item->children.at(0) );
}
translatePath.pop();
}
return nullptr;
} // nullptr means not found
AST* AST::findVariableType( string name, TranslatePath translatePath ) { // does not handle function arguments yet
while( !translatePath.empty() ) {
for( const AST* item: translatePath.top()->children ) {
if( item->type == AT_VARIABLEDEF && item->val == name )
return new AST( *item->children.at(0) );
}
translatePath.pop();
}
return nullptr;
} // nullptr means not found
void AST::translateItem( stringstream& ss, TranslatePath& translatePath, int indent, bool typeless ) const {
if( typeless )
for( int i = 0; i < indent; i++ )
ss << "\t";
bool comma = false;
switch( type ) {
case AT_FUNCTIONCALL: {
if( val == "=" ) {
ss << "((";
children.at(0)->translateItem( ss, translatePath, indent, false );
ss << ")=(";
children.at(1)->translateItem( ss, translatePath, indent, false );
ss << "))";
} else if( val == "@" && children.at(1)->type == AT_NUMBER ) { // TO FIX
AST* typeTree = nullptr;
typeTree = children.at(0)->getType( translatePath );
if( typeTree && typeTree->isSpace() ) {
ss << "space<" << children.at(1)->val << ">::from( ";
children.at(0)->translateItem( ss, translatePath, indent, false );
ss << ")";
} else {
ss << "special_at<" << children.at(1)->val << ">( ";
children.at(0)->translateItem( ss, translatePath, indent, false );
ss << ")";
}
delete typeTree;
} else if( val == "~" ) {
ss << "cast<";
children.at(1)->translateItem( ss, translatePath, indent, false );
ss << ">( ";
children.at(0)->translateItem( ss, translatePath, indent, false );
ss << " )";
} else if( val == "." ) {
if( children.at(1)->type == AT_FUNCTIONCALL ) {
ss << findFunctionName( children.at(1)->val, translatePath ) << "( ";
children.at(0)->translateItem( ss, translatePath, indent, false );
for( AST* child: children.at(1)->children ) {
ss << ", ";
child->translateItem( ss, translatePath, indent, false );
}
ss << " )";
} else
throw translate_exception( "Expected function after '.'" );
} else if( val == "->" ) {
ss << "((";
children.at(0)->translateItem( ss, translatePath, indent, false );
ss << ").";
children.at(1)->translateItem( ss, translatePath, indent, false );
ss << ")";
} else {
string functionName;
if( val == ":" ) {
functionName = "t_range<";
AST* type = children.at(0)->getType( translatePath );
functionName += type->decodeTypename( translatePath ) + ">";
delete type->cascade();
} else if( val == "@" )
functionName = "at";
else if( val.at(0) == '@' )
functionName = val.substr( 1 );
else
functionName = findFunctionName( val, translatePath ); // this ruins function overloading
if( functionName == "" )
throw translate_exception( "Unknown function " + val );
ss << functionName << "( ";
for( AST* argument : children ) {
if( comma )
ss << ", ";
comma = true;
argument->translateItem( ss, translatePath, indent, false );
}
ss << " )";
}
}
break;
case AT_CONDITIONAL:
if( typeless ) {
ss << "if( ";
children.at(0)->translateItem( ss, translatePath, indent, false );
ss << " )";
children.at(1)->translateBlock( ss, translatePath, indent );
if( children.size() > 2 ) { // exists an else condition
ss << " else";
children.at(2)->translateBlock( ss, translatePath, indent );
}
} else
throw translate_exception( "Unexpected block" );
break;
case AT_LOOP:
if( typeless ) {
if( val == "while" ) {
ss << "while( ";
children.at(0)->translateItem( ss, translatePath, indent, false );
ss << " )";
children.at(1)->translateBlock( ss, translatePath, indent );
} else if( val == "for" ) {
ss << "for( auto& ";
children.at(0)->translateItem( ss, translatePath, indent, false );
ss << ": ";
children.at(1)->translateItem( ss, translatePath, indent, false );
ss << " )";
children.at(2)->translateBlock( ss, translatePath, indent );
} else {
throw translate_exception( "Unknown loop " + val );
}
} else
throw translate_exception( "Unexpected block" );
break;
case AT_VARIABLEDEF:
ss << children.at(0)->decodeTypename( translatePath ) << " v_" << val;
break;
case AT_NUMBER:
if( val.find('.') == string::npos )
ss << "t_int";
else
ss << "t_real";
ss << "(" << val << ")"; //temp
break;
case AT_STRING:
ss << "t_string(" << val << ")"; //temp
break;
case AT_WORD:
ss << "v_" << val;
break;
case AT_INLINE_LIST: case AT_INLINE_SET: {
AST* r = getType( translatePath );
ss << "std::move(" << r->decodeTypename( translatePath ) << "({ ";
for( AST* child: children ) {
if( comma )
ss << ", ";
comma = true;
child->translateItem( ss, translatePath, indent, false );
}
ss << " }))";
delete r->cascade();
}
break;
case AT_ARRAY:
ss << "std::forward_as_tuple( ";
for( AST* child: children ) {
if( comma )
ss << ", ";
comma = true;
child->translateItem( ss, translatePath, indent, false );
}
ss << " )";
break;
case AT_SYNCBLOCK: case AT_ASYNCBLOCK:
if( typeless )
translateBlock( ss, translatePath, indent );
else
throw translate_exception( "Unexpected block" );
break;
case AT_FLOW:
if( typeless ) {
if( val == "return" ) {
ss << "return ";
children.at(0)->translateItem( ss, translatePath, indent, false );
}
} else
throw translate_exception( "Unexpected flow statement" );
break;
case AT_DATATYPE:
ss << decodeTypename( translatePath );
break;
default:
throw translate_exception( "Unexpected node " + to_string( type ) + " as translation item" );
}
if( typeless )
ss << ";\n";
}
AST* AST::getType( const TranslatePath& translatePath ) const {
AST* x;
switch( type ) {
case AT_WORD:
x = findVariableType( val, translatePath );
/*if( x == nullptr ) // TO BE ENABLED
throw translate_exception( "Unknown variable" );*/
return x;
break;
case AT_FUNCTIONCALL:
x = findFunctionType( val, translatePath );
/*if( x == nullptr ) // TO BE ENABLED
throw translate_exception( "Unknown function" );*/
return x;
break;
case AT_NUMBER:
return new AST( AT_DATATYPE, val.find('.') == string::npos ? "int": "real" , {} );
break;
case AT_STRING:
return new AST( AT_DATATYPE, "string", {} );
break;
case AT_ARRAY: {
std::vector<AST*> temp;
for( auto x : children )
temp.push_back( x->getType( translatePath ) );
return new AST( AT_ARRAY, "tuple", std::move( temp ) );
} break;
case AT_INLINE_SET: case AT_INLINE_LIST:
if( children.size() == 0 )
throw compile_exception( "Type Error: Could not deduce type of empty set", -1 );
else {
AST *r = children.at(0)->getType( translatePath ), *c;
bool b;
for( size_t i = 1; i < children.size(); i++ ) {
c = children.at(1)->getType( translatePath );
b = r->compare( c );
delete c->cascade();
if( !b )
throw compile_exception( "Type Error: Could not deduce type of inline list/set", -1 );
}
return new AST( AT_DATATYPE, type == AT_INLINE_LIST ? "list" : "set", {r} );
}
break;
default:
throw compile_exception( "Type Error: Node " + to_string(type) + " has no datatype", -1 );
}
return nullptr;
}
const char* translate_exception::what() const noexcept {
string s = "Translation Error: " + err_str + "!";
return s.c_str();
}
translate_exception::translate_exception( string err ) {
err_str = move( err );
}