-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
395 lines (346 loc) · 5.66 KB
/
parser.y
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
%define parse.assert
%define parse.error verbose
%locations
%code requires
{
// TODO: remove includes..
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include <errno.h>
#include <stdarg.h>
#include <assert.h>
#include <time.h>
#include <math.h>
#include <stdint.h>
#include "macros.h"
#include "map.h"
#include "vector.h"
#include "expr.h"
#include "prim.h"
#include "decl.h"
#include "scope.h"
#include "func.h"
#include "ctx.h"
extern int32_t yyerror(char const* msg, ...);
extern int32_t yywrap(void);
extern char const* g_current_filename;
extern int32_t g_line_number;
extern int32_t g_column_number;
extern char* yytext;
extern int32_t yyleng;
}
%token IDENT
%token I8_LIT
%token I16_LIT
%token I32_LIT
%token I64_LIT
%token U8_LIT
%token U16_LIT
%token U32_LIT
%token U64_LIT
%token R32_LIT
%token R64_LIT
%token STR_LIT
%token PTYPE_I8
%token PTYPE_I16
%token PTYPE_I32
%token PTYPE_I64
%token PTYPE_U8
%token PTYPE_U16
%token PTYPE_U32
%token PTYPE_U64
%token PTYPE_R32
%token PTYPE_R64
%token COMMA
%token EQUALS
%token SEMICOLON
%token LEFT_PAREN
%token RIGHT_PAREN
%token LEFT_BRACE
%token RIGHT_BRACE
%union
{
string_t string;
int8_t i8;
int16_t i16;
int32_t i32;
int64_t i64;
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
float_t r32;
double_t r64;
prim_type_t ptype;
decl_t decl;
expr_t expr;
func_t func;
}
%type <string> IDENT
%type <string> STR_LIT
%type <i8> I8_LIT
%type <i16> I16_LIT
%type <i32> I32_LIT
%type <i64> I64_LIT
%type <u8> U8_LIT
%type <u16> U16_LIT
%type <u32> U32_LIT
%type <u64> U64_LIT
%type <r32> R32_LIT
%type <r64> R64_LIT
%type <ptype> PTYPE
%type <ptype> PTYPE_I8
%type <ptype> PTYPE_I16
%type <ptype> PTYPE_I32
%type <ptype> PTYPE_I64
%type <ptype> PTYPE_U8
%type <ptype> PTYPE_U16
%type <ptype> PTYPE_U32
%type <ptype> PTYPE_U64
%type <ptype> PTYPE_R32
%type <ptype> PTYPE_R64
%type <decl> DECL
%type <args> ARGS
%type <arg> ARG
%type <func> FUNC_DECL
%type <expr> EXPR
%type <expr> VAR_DECL
%type <expr> STMT
%%
PROGRAM:
PROGRAM VAR_DECL
{
ctx_push_expr($2);
}
| PROGRAM FUNC_DECL
{
ctx_push_func($2);
}
| %empty
;
DECL:
PTYPE IDENT
{
decl_t decl;
decl_alloc(&decl, $1, $2);
ctx_insert_decl(decl);
string_free(&$2);
$$ = decl;
}
;
VAR_DECL:
DECL SEMICOLON
{
$$ = expr_var($1);
}
| DECL EQUALS EXPR SEMICOLON
{
$$ = expr_copy(expr_var($1), $3);
}
;
FUNC_DECL:
{
func_t func;
func_begin(&func);
}
DECL ARGS COMP_STMT
{
func_end(&func);
$$ = func;
// TODO: context_remove_arg_identifiers();
}
;
ARGS:
{
args_t args;
args_begin(&args);
}
LEFT_PAREN ARG RIGHT_PAREN
{
args_end(&args);
$$ = args;
}
;
ARG:
DECL
{
args_push($1);
}
| ARG COMMA DECL
{
args_push($1);
}
| %empty
;
COMP_STMT:
{
ctx_push_scope();
}
LEFT_BRACE STMT RIGHT_BRACE
{
ctx_pop_scope();
}
;
STMT:
VAR_DECL
{
expression_pack_push($1);
}
| COMP_STMT
{
// TODO
}
| STMT VAR_DECL
{
expression_pack_push($2);
}
| STMT COMP_STMT
{
// TODO
}
| %empty
;
PTYPE:
PTYPE_I8
{
$$ = PRIM_TYPE_I8;
}
| PTYPE_I16
{
$$ = PRIM_TYPE_I16;
}
| PTYPE_I32
{
$$ = PRIM_TYPE_I32;
}
| PTYPE_I64
{
$$ = PRIM_TYPE_I64;
}
| PTYPE_U8
{
$$ = PRIM_TYPE_U8;
}
| PTYPE_U16
{
$$ = PRIM_TYPE_U16;
}
| PTYPE_U32
{
$$ = PRIM_TYPE_U32;
}
| PTYPE_U64
{
$$ = PRIM_TYPE_U64;
}
| PTYPE_R32
{
$$ = PRIM_TYPE_R32;
}
| PTYPE_R64
{
$$ = PRIM_TYPE_R64;
}
;
EXPR:
IDENT
{
decl_t decl = ctx_get_decl_by_ident($1);
$$ = expr_ident(decl.ident); // TODO
}
| STR_LIT
{
$$ = expr_string($1);
}
| I8_LIT
{
$$ = expr_i8($1);
}
| I16_LIT
{
$$ = expr_i16($1);
}
| I32_LIT
{
$$ = expr_i32($1);
}
| I64_LIT
{
$$ = expr_i64($1);
}
| U8_LIT
{
$$ = expr_u8($1);
}
| U16_LIT
{
$$ = expr_u16($1);
}
| U32_LIT
{
$$ = expr_u32($1);
}
| U64_LIT
{
$$ = expr_u64($1);
}
| R32_LIT
{
$$ = expr_r32($1);
}
| R64_LIT
{
$$ = expr_r64($1);
}
;
%%
#include "config.h"
char const* g_current_filename;
int32_t g_line_number;
int32_t g_column_number;
int32_t main(int32_t argc, char** argv)
{
g_current_filename = argv[1];
g_line_number = 1;
g_column_number = 1;
FILE* file = fopen(argv[1], "r");
if (file)
{
ctx_alloc();
ctx_push_scope();
struct timespec start;
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &start);
yyrestart(file);
yyparse();
clock_gettime(CLOCK_MONOTONIC, &end);
ctx_print();
ctx_pop_scope();
ctx_free();
int64_t elapsed_ns = (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec);
int64_t elapsed_us = elapsed_ns / 1e3;
int64_t elapsed_ms = elapsed_ns / 1e6;
printf("elapsed time %zdns\n", elapsed_ns);
printf("elapsed time %zdus\n", elapsed_us);
printf("elapsed time %zdms\n", elapsed_ms);
fclose(file);
}
heap_check_leaks();
return 0;
}
int32_t yyerror(char const* msg, ...)
{
static char string_buffer[ERROR_FORMAT_BUFFER_SIZE];
va_list args;
va_start(args, msg);
vsnprintf(string_buffer, ERROR_FORMAT_BUFFER_SIZE, msg, args);
va_end(args);
printf("%s:%d:%d: %s\n", g_current_filename, yylloc.first_line, yylloc.first_column, string_buffer);
return 0;
}
int32_t yywrap(void)
{
return 1;
}