-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
485 lines (417 loc) · 8.28 KB
/
parser.cpp
File metadata and controls
485 lines (417 loc) · 8.28 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
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
#include <cassert>
#include <iostream>
#include "parser.hpp"
#include "util.hpp"
#include "check.hpp"
#include "eval.hpp"
#include "context.hpp"
parser::parser( lexer& l, context& cxt, std::list<scope>& stack, value_map& vm )
:m_lexer( l ), m_cxt( cxt ), sema( translator( cxt, stack, vm ) ),
m_stack( stack ), m_values( vm )
{
m_lexer.lex();
if( auto token = std::move( m_lexer.front() ) )
{
tokens.push_front( std::move( token ) );
}
}
token_kind parser::lookahead()
{
if( !tokens.empty() )
return tokens.front()->get_kind();
else
return eof_tok;
}
token_ptr parser::consume()
{
assert( !tokens.empty() );
auto token = std::move( tokens.front() );
tokens.pop_front();
if( tokens.empty() )
{
if( auto next = m_lexer.front() )
{
tokens.push_front( std::move( next ) );
}
}
return token;
}
token_ptr parser::match( token_kind tk )
{
if( lookahead() == tk )
return consume();
else
throw std::runtime_error( "matched unexpected token\n" );
}
token_ptr parser::match_if( token_kind tk )
{
if ( lookahead() == tk )
{
return consume();
}
else
{
return nullptr;
}
}
expr* parser::expression()
{
return assignment_expression();
}
expr* parser::assignment_expression()
{
auto ast_1 = conditional_expression();
while( true )
{
if( match_if( assign_tok ) )
{
auto ast_2 = logical_or_expression();
ast_1 = sema.on_assign( ast_1, ast_2 );
}
else break;
}
return ast_1;
}
expr* parser::conditional_expression()
{
auto ast_1 = logical_or_expression();
while( true )
{
if( match_if( question_tok ) )
{
auto ast_2 = expression();
match( colon_tok );
auto ast_3 = assignment_expression();
ast_1 = std::move( sema.on_cond( ast_1, ast_2, ast_3 ) );
}
else break;
}
return ast_1;
}
expr* parser::logical_or_expression()
{
auto ast_1 = logical_and_expression();
while( true )
{
if( match_if( bar_tok ) )
{
auto ast_2 = logical_and_expression();
ast_1 = sema.on_or( ast_1, ast_2 );
}
else break;
}
return ast_1;
}
expr* parser::logical_and_expression()
{
auto ast_1 = equality_expression();
while( true )
{
if( match_if( ampersand_tok ) )
{
auto ast_2 = equality_expression();
ast_1 = sema.on_and( ast_1, ast_2 );
}
else break;
}
return ast_1;
}
expr* parser::equality_expression()
{
auto ast_1 = ordering_expression();
while( true )
{
if( match_if( equal_tok ) )
{
auto ast_2 = ordering_expression();
ast_1 = sema.on_equal( ast_1, ast_2 );
}
else if( match_if( exclmeq_tok ) )
{
auto ast_2 = ordering_expression();
ast_1 = sema.on_inequal( ast_1, ast_2 );
}
else break;
}
return ast_1;
}
expr* parser::ordering_expression()
{
auto ast_1 = additive_expression();
while( true )
{
if( match_if( less_tok ) )
{
auto ast_2 = additive_expression();
ast_1 = sema.on_less( ast_1, ast_2 );
}
else if( match_if( greater_tok ) )
{
auto ast_2 = additive_expression();
ast_1 = sema.on_greater( ast_1, ast_2 );
}
else if( match_if( lesseq_tok ) )
{
auto ast_2 = additive_expression();
ast_1 = sema.on_lesseq( ast_1, ast_2 );
}
else if( match_if( greatereq_tok ) )
{
auto ast_2 = additive_expression();
ast_1 = sema.on_greatereq( ast_1, ast_2 );
}
else break;
}
return ast_1;
}
expr* parser::additive_expression()
{
auto ast_1 = multiplicative_expression();
expr* r;
while( true )
{
if( match_if( plus_tok ) )
{
auto ast_2 = multiplicative_expression();
r = sema.on_add( ast_1, ast_2 );
return r;
}
else if( match_if( minus_tok ) )
{
auto ast_2 = multiplicative_expression();
r = sema.on_sub( ast_1, ast_2 );
return r;
}
else break;
}
return ast_1;
}
expr* parser::multiplicative_expression()
{
auto ast_1 = unary_expression();
while( true )
{
if( match_if( asterix_tok ) )
{
auto ast_2 = unary_expression();
ast_1 = sema.on_mul( ast_1, ast_2 );
}
else if( match_if( slash_tok ) )
{
auto ast_2 = unary_expression();
ast_1 = sema.on_div( ast_1, ast_2 );
}
else if( match_if( percent_tok ) )
{
auto ast_2 = unary_expression();
ast_1 = sema.on_rem( ast_1, ast_2 );
}
else break;
}
return ast_1;
}
expr* parser::unary_expression()
{
if( match_if( minus_tok ) )
{
auto ast_1 = unary_expression();
return sema.on_neg( ast_1 );
}
else if( match_if( exclm_tok ) )
{
auto ast_1 = unary_expression();
return sema.on_not( ast_1 );
}
else
return primary_expression();
}
expr* parser::primary_expression()
{
switch( lookahead() )
{
case int_tok:
{
auto p = &(*consume() );
auto q = static_cast<int_token*>( p );
auto r = new int_expr( q->get_value(), m_cxt );
return r;
break;
}
case true_kw_tok:
case false_kw_tok:
{
auto next = static_cast<bool_token*>( &(*consume()) );
return new bool_expr( next->get_value(), m_cxt );
break;
}
case lparen_tok:
{
consume();
auto e = expression();
match( rparen_tok );
return e;
}
case id_tok:
{
return id_expression();
}
}
return nullptr;
}
expr* parser::id_expression()
{
auto next = &( *consume() );
auto next_id = static_cast<id_token*>( next );
return sema.on_ref( *next_id );
}
// STATEMENT PARSING
decl* parser::program()
{
std::vector<stmt*> sequence;
while( !tokens.empty() )
{
sequence.push_back( statement() );
}
return sema.on_program( sequence );
}
stmt* parser::statement()
{
switch( lookahead() )
{
case lbrace_tok:
return block_statement();
case var_kw_tok:
case def_kw_tok:
return declaration_statement();
case print_kw_tok:
return print_statement();
case while_kw_tok:
return while_statement();
case if_kw_tok:
return if_statement();
default:
return expression_statement();
}
}
stmt* parser::print_statement()
{
consume();
// analyze expressions to print until end of statement
auto e = expression();
match( semicolon_tok );
// print the evaluation of the expression
std::cout << eval( e ) << '\n';
// return null because prints do not generate actual code
return nullptr;
}
stmt* parser::while_statement()
{
consume();
match( lparen_tok );
auto conditional = expression();
match( rparen_tok );
auto block = block_statement();
return new while_stmt( conditional, block );
}
stmt* parser::if_statement()
{
consume();
match( lparen_tok );
auto conditional = expression();
match( rparen_tok );
auto block = block_statement();
return new if_stmt( conditional, block );
}
stmt* parser::block_statement()
{
// enter a new block scope
m_stack.emplace_front();
stmt_seq seq;
// consume the initial lbrace_tok
consume();
while( lookahead() != rbrace_tok )
{
seq.push_back( statement() );
}
// consume the final rbrace_tok
consume();
return new block_stmt( seq );
}
stmt* parser::declaration_statement()
{
decl* d = declaration();
return sema.on_decl_stmt( d );
}
stmt* parser::expression_statement()
{
expr* e = expression();
match( semicolon_tok );
return sema.on_expr_stmt( e );
}
decl* parser::declaration()
{
switch( lookahead() )
{
case var_kw_tok:
return variable_declaration();
case def_kw_tok:
return function_declaration();
}
throw std::runtime_error( "unexpected declaration\n" );
}
decl* parser::variable_declaration()
{
consume();
// determine type, name, and create a declaration object
auto t = type_specifier();
auto n = identifier();
auto var = sema.on_var_decl( t, n );
// find an assignment operator
match( assign_tok );
// parse the expression on the right side of the operator
auto e = expression();
// initialize the declaration
sema.on_var_compl( var, e );
// find a semicolon
match( semicolon_tok );
return var;
}
decl* parser::function_declaration()
{
consume();
auto name = identifier();
match( lparen_tok );
std::vector<decl*> parameters;
// if there are parameters
while( lookahead() != rparen_tok )
{
parameters.push_back( parameter_declaration() );
}
match( rparen_tok );
match( arrow_tok );
auto ty = type_specifier();
return new function_decl( name, ty, parameters );
}
decl* parser::parameter_declaration()
{
auto ty = type_specifier();
auto name = identifier();
return new param_decl( name, ty );
}
symbol* parser::identifier()
{
auto id = match( id_tok );
return sema.on_id( id.release() );
}
const type* parser::type_specifier()
{
switch( lookahead() )
{
case bool_kw_tok:
consume();
return sema.on_bool_type();
case int_kw_tok:
consume();
return sema.on_int_type();
}
}