-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.cpp
More file actions
386 lines (329 loc) · 9.08 KB
/
translator.cpp
File metadata and controls
386 lines (329 loc) · 9.08 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
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
#include <sstream>
#include <iostream>
#include "check.hpp"
#include "eval.hpp"
#include "translator.hpp"
#include "util.hpp"
translator::translator( context& cxt, std::list<scope>& stack, value_map& vm )
:m_cxt( cxt ), m_stack( stack ), m_values( vm ), next_type()
{
}
expr* translator::on_cond( expr* ast_1, expr* ast_2, expr* ast_3 )
{
if( check( ast_1 ) != &m_cxt.bool_ty )
{
std::stringstream ss;
ss << "Expected bool type in conditional expression.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != check( ast_3 ) )
{
std::stringstream ss;
ss << "Expected resultant expressions to be of the same type.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new cond_expr( ast_1, ast_2, ast_3, m_cxt );
}
expr* translator::on_or( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.bool_ty )
{
std::stringstream ss;
ss << "Expected bool type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.bool_ty )
{
std::stringstream ss;
ss << "Expected bool type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new or_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_and( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.bool_ty )
{
std::stringstream ss;
ss << "Expected bool type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.bool_ty )
{
std::stringstream ss;
ss << "Expected bool type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new and_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_equal( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != check( ast_2 ) )
{
std::stringstream ss;
ss << "Expected same types in equality expression.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new equal_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_inequal( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != check( ast_2 ) )
{
std::stringstream ss;
ss << "Expected same types in inequality expression.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new inequal_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_less( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new less_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_greater( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new greater_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_lesseq( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new lesseq_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_greatereq( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new greatereq_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_add( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new add_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_sub( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new sub_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_mul( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new mul_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_div( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new div_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_rem( expr* ast_1, expr* ast_2 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 1.\n";
throw std::runtime_error( ss.str().c_str() );
}
if( check( ast_2 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in expression 2.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new rem_expr( ast_1, ast_2, m_cxt );
}
expr* translator::on_not( expr* ast_1 )
{
if( check( ast_1 ) != &m_cxt.bool_ty )
{
std::stringstream ss;
ss << "Expected bool type in not expression.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new not_expr( ast_1, m_cxt );
}
expr* translator::on_neg( expr* ast_1 )
{
if( check( ast_1 ) != &m_cxt.int_ty )
{
std::stringstream ss;
ss << "Expected int type in neg expression.\n";
throw std::runtime_error( ss.str().c_str() );
}
return new neg_expr( ast_1, m_cxt );
}
expr* translator::on_ref( id_token& identifier )
{
auto declaration = scope_lookup( m_stack, identifier.get_name() );
// if the declaration was not found in any scope
if( !declaration )
{
std::stringstream ss;
// ss << "Use of undeclared identifier " << identifier.get_name() << '\n';
throw std::runtime_error( ss.str().c_str() );
}
// downcast from decl to var_decl
auto variable = static_cast<var_decl*>( declaration );
// find the value_expr associated with this declaration
auto value = m_values.find( declaration );
// find the type associated with this declaration
auto ty = variable->m_type;
return new ref_expr( declaration, value->second, ty, m_cxt );
}
expr* translator::on_assign( expr* ast_1, expr* ast_2 )
{
// create a reference to the assigned variable (to be returned)
auto var = static_cast<ref_expr*>( ast_1 );
// find the declaration associated with that reference
auto ref = static_cast<var_decl*>( var->get_reference() );
auto declaration = scope_lookup( m_stack, ref->m_name );
// if the declaration was not found in any scope
if( !declaration )
{
std::stringstream ss;
// ss << "Use of undeclared identifier " << identifier.get_name() << '\n';
throw std::runtime_error( ss.str().c_str() );
}
// associate var's value with the expression in ast_2
var->set_value( ast_2 );
// find and set the value associated with the declaration to ast_2
auto value = m_values.find( declaration );
value->second = ast_2;
return var;
}
stmt* translator::on_decl_stmt( decl* d )
{
return new decl_stmt( d );
}
stmt* translator::on_expr_stmt( expr* e )
{
return new expr_stmt( e );
}
decl* translator::on_var_decl( const type* t, symbol* n )
{
var_decl* var = new var_decl( n, t );
// add the declaration to the scope
if( m_stack.front().insert( n, var ) )
return var;
// insertion will fail if this (name,decl) mapping already exists
else
{
std::stringstream ss;
ss << "identifier" << *n << "already exists\n";
throw std::runtime_error( ss.str().c_str() );
}
}
decl* translator::on_var_compl( decl* d, expr* e )
{
var_decl* var = static_cast<var_decl*>( d );
var->m_type = next_type;
next_type = nullptr;
var->set_init( e );
m_values.insert( {d, e} );
return var;
}
const type* translator::on_bool_type()
{
next_type = &m_cxt.bool_ty;
}
const type* translator::on_int_type()
{
next_type = &m_cxt.int_ty;
}
symbol* translator::on_id( token* t )
{
auto id = dynamic_cast<id_token*>( t );
return id->get_name();
}
decl* translator::on_program( std::vector<stmt*> sequence )
{
return new program_decl( sequence );
}