flush
[m6w6/ext-psi] / src / parser_proc.y
1 %include {
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "parser.h"
7
8 }
9
10 %name PSI_ParserProc
11 %token_prefix PSI_T_
12 %token_type {PSI_Token *}
13 %token_destructor {free($$);}
14 %default_destructor {(void)P;}
15 %extra_argument {PSI_Parser *P}
16 /* TOKEN is defined inside syntax_error */
17 %syntax_error {
18 PSI_ParserSyntaxError(P, P->fn, P->line, "Unexpected token '%s'", TOKEN->text);
19 }
20
21 %nonassoc NAME.
22
23 file ::= blocks.
24
25 blocks ::= block.
26 blocks ::= blocks block.
27
28 block ::= COMMENT.
29
30 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
31 if (P->lib) {
32 PSI_ParserSyntaxError(P, P->fn, T->line, "Extra 'lib %s' statement has no effect", libname->text);
33 } else {
34 P->lib = strndup(libname->text + 1, libname->size - 2);
35 }
36 free(libname);
37 free(T);
38 }
39
40 block ::= decl(decl). {
41 P->decls = add_decl(P->decls, decl);
42 }
43 block ::= impl(impl). {
44 P->impls = add_impl(P->impls, impl);
45 }
46 block ::= decl_typedef(def). {
47 P->defs = add_decl_typedef(P->defs, def);
48 if (def->type->strct) {
49 P->structs = add_decl_struct(P->structs, def->type->strct);
50 }
51 }
52 block ::= constant(constant). {
53 P->consts = add_constant(P->consts, constant);
54 }
55 block ::= decl_struct(strct). {
56 P->structs = add_decl_struct(P->structs, strct);
57 }
58
59 %type decl_struct {decl_struct*}
60 %destructor decl_struct {free_decl_struct($$);}
61 decl_struct(strct) ::= STRUCT NAME(N) LBRACE struct_args(args) RBRACE. {
62 strct = init_decl_struct(N->text, args);
63 free(N);
64 }
65
66 %type const_type {const_type*}
67 %destructor const_type {free_const_type($$);}
68 const_type(type_) ::= BOOL(T). {
69 type_ = init_const_type(T->type, T->text);
70 free(T);
71 }
72 const_type(type_) ::= INT(T). {
73 type_ = init_const_type(T->type, T->text);
74 free(T);
75 }
76 const_type(type_) ::= FLOAT(T). {
77 type_ = init_const_type(T->type, T->text);
78 free(T);
79 }
80 const_type(type_) ::= STRING(T). {
81 type_ = init_const_type(T->type, T->text);
82 free(T);
83 }
84 %type constant {constant*}
85 %destructor constant {free_constant($$);}
86 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
87 constant = init_constant(type, T->text, val);
88 free(T);
89 }
90
91 %type decl_typedef {decl_typedef*}
92 %destructor decl_typedef {free_decl_typedef($$);}
93 decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
94 def = init_decl_typedef(ALIAS->text, type);
95 free(ALIAS);
96 }
97 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
98 def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
99 free(ALIAS);
100 free(S);
101 free(N);
102 }
103 decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
104 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, s->name));
105 def->type->strct = s;
106 free(ALIAS);
107 }
108
109 %type decl {decl*}
110 %destructor decl {free_decl($$);}
111 decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
112 decl = init_decl(abi, func, args);
113 }
114
115 %type decl_abi {decl_abi*}
116 %destructor decl_abi {free_decl_abi($$);}
117 decl_abi(abi) ::= NAME(T). {
118 abi = init_decl_abi(T->text);
119 free(T);
120 }
121
122 %type decl_var {decl_var*}
123 %destructor decl_var {free_decl_var($$);}
124 decl_var(var) ::= NAME(T). {
125 var = init_decl_var(T->text, 0, 0);
126 free(T);
127 }
128 decl_var(var) ::= pointers(p) NAME(T). {
129 var = init_decl_var(T->text, p, 0);
130 free(T);
131 }
132 decl_var(var) ::= NAME(T) LBRACKET NUMBER(D) RBRACKET. {
133 var = init_decl_var(T->text, 1, atol(D->text));
134 free(T);
135 free(D);
136 }
137 decl_var(var) ::= pointers(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
138 var = init_decl_var(T->text, p+1, atol(D->text));
139 free(T);
140 free(D);
141 }
142
143 %type decl_vars {decl_vars*}
144 %destructor decl_vars {free_decl_vars($$);}
145 decl_vars(vars) ::= decl_var(var). {
146 vars = init_decl_vars(var);
147 }
148 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
149 vars = add_decl_var(vars_, var);
150 }
151
152 %type decl_arg {decl_arg*}
153 %destructor decl_arg {free_decl_arg($$);}
154 decl_arg(arg_) ::= decl_type(type) decl_var(var). {
155 arg_ = var->arg = init_decl_arg(type, var);
156 }
157
158 %type decl_args {decl_args*}
159 %destructor decl_args {free_decl_args($$);}
160 decl_args ::= VOID.
161 decl_args(args) ::= decl_arg(arg). {
162 args = init_decl_args(arg);
163 }
164 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
165 args = add_decl_arg(args_, arg);
166 }
167 %type struct_args {decl_args*}
168 %destructor struct_args {free_decl_args($$);}
169 struct_args(args) ::= decl_arg(arg) EOS. {
170 args = init_decl_args(arg);
171 }
172 struct_args(args) ::= struct_args(args_) decl_arg(arg) EOS. {
173 args = add_decl_arg(args_, arg);
174 }
175
176 %type decl_type {decl_type*}
177 %destructor decl_type {free_decl_type($$);}
178 decl_type(type_) ::= VOID(T). {
179 type_ = init_decl_type(T->type, T->text);
180 free(T);
181 }
182 decl_type(type_) ::= CHAR(T). {
183 type_ = init_decl_type(T->type, T->text);
184 free(T);
185 }
186 decl_type(type_) ::= SHORT(T). {
187 type_ = init_decl_type(T->type, T->text);
188 free(T);
189 }
190 decl_type(type_) ::= INT(T). {
191 type_ = init_decl_type(T->type, T->text);
192 free(T);
193 }
194 decl_type(type_) ::= LONG(T). {
195 type_ = init_decl_type(T->type, T->text);
196 free(T);
197 }
198 decl_type(type_) ::= FLOAT(T). {
199 type_ = init_decl_type(T->type, T->text);
200 free(T);
201 }
202 decl_type(type_) ::= DOUBLE(T). {
203 type_ = init_decl_type(T->type, T->text);
204 free(T);
205 }
206 decl_type(type_) ::= SIZE_T(T). {
207 type_ = init_decl_type(T->type, T->text);
208 free(T);
209 }
210 decl_type(type_) ::= INT8(T). {
211 type_ = init_decl_type(T->type, T->text);
212 free(T);
213 }
214 decl_type(type_) ::= UINT8(T). {
215 type_ = init_decl_type(T->type, T->text);
216 free(T);
217 }
218 decl_type(type_) ::= INT16(T). {
219 type_ = init_decl_type(T->type, T->text);
220 free(T);
221 }
222 decl_type(type_) ::= UINT16(T). {
223 type_ = init_decl_type(T->type, T->text);
224 free(T);
225 }
226 decl_type(type_) ::= INT32(T). {
227 type_ = init_decl_type(T->type, T->text);
228 free(T);
229 }
230 decl_type(type_) ::= UINT32(T). {
231 type_ = init_decl_type(T->type, T->text);
232 free(T);
233 }
234 decl_type(type_) ::= INT64(T). {
235 type_ = init_decl_type(T->type, T->text);
236 free(T);
237 }
238 decl_type(type_) ::= UINT64(T). {
239 type_ = init_decl_type(T->type, T->text);
240 free(T);
241 }
242 decl_type(type_) ::= NAME(T). {
243 type_ = init_decl_type(T->type, T->text);
244 free(T);
245 }
246 decl_type(type_) ::= STRUCT(S) NAME(T). {
247 type_ = init_decl_type(S->type, T->text);
248 free(S);
249 free(T);
250 }
251
252 %type impl {impl*}
253 %destructor impl {free_impl($$);}
254 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
255 impl = init_impl(func, stmts);
256 }
257
258 %type impl_func {impl_func*}
259 %destructor impl_func {free_impl_func($$);}
260 impl_func(func) ::= FUNCTION NSNAME(NAME) impl_args(args) COLON impl_type(type). {
261 func = init_impl_func(NAME->text, args, type, 0);
262 free(NAME);
263 }
264 impl_func(func) ::= FUNCTION REFERENCE NSNAME(NAME) impl_args(args) COLON impl_type(type). {
265 func = init_impl_func(NAME->text, args, type, 1);
266 free(NAME);
267 }
268
269 %type impl_def_val {impl_def_val*}
270 %destructor impl_def_val {free_impl_def_val($$);}
271 impl_def_val(def) ::= NULL(T). {
272 def = init_impl_def_val(T->type, T->text);
273 free(T);
274 }
275 impl_def_val(def) ::= NUMBER(T). {
276 def = init_impl_def_val(T->type, T->text);
277 free(T);
278 }
279 impl_def_val(def) ::= TRUE(T). {
280 def = init_impl_def_val(T->type, T->text);
281 free(T);
282 }
283 impl_def_val(def) ::= FALSE(T). {
284 def = init_impl_def_val(T->type, T->text);
285 free(T);
286 }
287 impl_def_val(def) ::= QUOTED_STRING(T). {
288 def = init_impl_def_val(T->type, T->text);
289 free(T);
290 }
291
292 %type impl_var {impl_var*}
293 %destructor impl_var {free_impl_var($$);}
294 impl_var(var) ::= DOLLAR NAME(T). {
295 var = init_impl_var(T->text, 0);
296 free(T);
297 }
298 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
299 var = init_impl_var(T->text, 1);
300 free(T);
301 }
302
303 %type impl_arg {impl_arg*}
304 %destructor impl_arg {free_impl_arg($$);}
305 impl_arg(arg) ::= impl_type(type) impl_var(var). {
306 arg = init_impl_arg(type, var, NULL);
307 }
308 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
309 arg = init_impl_arg(type, var, def);
310 }
311
312 %type impl_args {impl_args*}
313 %destructor impl_args {free_impl_args($$);}
314 impl_args(args) ::= LPAREN RPAREN. {
315 args = NULL;
316 }
317 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
318 args = args_;
319 }
320 %type impl_arg_list {impl_args*}
321 %destructor impl_arg_list {free_impl_args($$);}
322 impl_arg_list(args) ::= impl_arg(arg). {
323 args = init_impl_args(arg);
324 }
325 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
326 args = add_impl_arg(args_, arg);
327 }
328
329 %type impl_stmts {impl_stmts*}
330 %destructor impl_stmts {free_impl_stmts($$);}
331 impl_stmts(stmts) ::= impl_stmt(stmt). {
332 stmts = init_impl_stmts(stmt);
333 }
334 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
335 stmts = add_impl_stmt(stmts_, stmt);
336 }
337
338 %type impl_stmt {impl_stmt*}
339 %destructor impl_stmt {free_impl_stmt($$);}
340 impl_stmt(stmt) ::= let_stmt(let). {
341 stmt = init_impl_stmt(PSI_T_LET, let);
342 }
343 impl_stmt(stmt) ::= set_stmt(set). {
344 stmt = init_impl_stmt(PSI_T_SET, set);
345 }
346 impl_stmt(stmt) ::= return_stmt(ret). {
347 stmt = init_impl_stmt(PSI_T_RETURN, ret);
348 }
349 impl_stmt(stmt) ::= free_stmt(free). {
350 stmt = init_impl_stmt(PSI_T_FREE, free);
351 }
352
353 %type let_stmt {let_stmt*}
354 %destructor let_stmt {free_let_stmt($$);}
355 let_stmt(let) ::= LET decl_var(var) EOS. {
356 let = init_let_stmt(var, NULL);
357 }
358 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
359 let = init_let_stmt(var, val);
360 }
361
362 %type let_value {let_value*}
363 %destructor let_value {free_let_value($$);}
364 let_value(val) ::= CALLOC(F) LPAREN NUMBER(N) COMMA decl_type(t) RPAREN. {
365 val = init_let_value(
366 init_let_func(F->type, F->text,
367 init_let_calloc(
368 atol(N->text), t
369 )
370 ), NULL, 0
371 );
372 free(F);
373 free(N);
374 }
375 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
376 val = init_let_value(func, var, 0);
377 }
378 let_value(val) ::= REFERENCE let_func(func) LPAREN impl_var(var) RPAREN. {
379 val = init_let_value(func, var, 1);
380 }
381 let_value(val) ::= REFERENCE NULL. {
382 val = init_let_value(NULL, NULL, 1);
383 }
384 let_value(val) ::= NULL. {
385 val = init_let_value(NULL, NULL, 0);
386 }
387
388 %type let_func {let_func*}
389 %destructor let_func {free_let_func($$);}
390 let_func(func) ::= STRLEN(T). {
391 func = init_let_func(T->type, T->text, NULL);
392 free(T);
393 }
394 let_func(func) ::= STRVAL(T). {
395 func = init_let_func(T->type, T->text, NULL);
396 free(T);
397 }
398 let_func(func) ::= INTVAL(T). {
399 func = init_let_func(T->type, T->text, NULL);
400 free(T);
401 }
402 let_func(func) ::= FLOATVAL(T). {
403 func = init_let_func(T->type, T->text, NULL);
404 free(T);
405 }
406 let_func(func) ::= BOOLVAL(T). {
407 func = init_let_func(T->type, T->text, NULL);
408 free(T);
409 }
410 let_func(func) ::= ARRVAL(T). {
411 func = init_let_func(T->type, T->text, NULL);
412 free(T);
413 }
414
415 %type set_stmt {set_stmt*}
416 %destructor set_stmt {free_set_stmt($$);}
417 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
418 set = init_set_stmt(var, val);
419 }
420
421 %type set_value {set_value*}
422 %destructor set_value {free_set_value($$);}
423 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
424 val = init_set_value(func, vars);
425 }
426
427 %type set_func {set_func*}
428 %destructor set_func {free_set_func($$);}
429 set_func(func) ::= TO_ARRAY(T). {
430 func = init_set_func(T->type, T->text);
431 free(T);
432 }
433 set_func(func) ::= TO_STRING(T). {
434 func = init_set_func(T->type, T->text);
435 free(T);
436 }
437 set_func(func) ::= TO_INT(T). {
438 func = init_set_func(T->type, T->text);
439 free(T);
440 }
441 set_func(func) ::= TO_FLOAT(T). {
442 func = init_set_func(T->type, T->text);
443 free(T);
444 }
445 set_func(func) ::= TO_BOOL(T). {
446 func = init_set_func(T->type, T->text);
447 free(T);
448 }
449 set_func(func) ::= VOID(T). {
450 func = init_set_func(T->type, T->text);
451 free(T);
452 }
453
454 %type return_stmt {return_stmt*}
455 %destructor return_stmt {free_return_stmt($$);}
456 return_stmt(ret) ::= RETURN set_func(func) LPAREN decl_var(var) RPAREN EOS. {
457 ret = init_return_stmt(func, var);
458 }
459
460 %type free_stmt {free_stmt*}
461 %destructor free_stmt {free_free_stmt($$);}
462 free_stmt(free) ::= FREE decl_vars(vars) EOS. {
463 free = init_free_stmt(vars);
464 }
465
466 %type impl_type {impl_type*}
467 %destructor impl_type {free_impl_type($$);}
468 impl_type(type_) ::= VOID(T). {
469 type_ = init_impl_type(T->type, T->text);
470 free(T);
471 }
472 impl_type(type_) ::= MIXED(T). {
473 type_ = init_impl_type(T->type, T->text);
474 free(T);
475 }
476 impl_type(type_) ::= BOOL(T). {
477 type_ = init_impl_type(T->type, T->text);
478 free(T);
479 }
480 impl_type(type_) ::= INT(T). {
481 type_ = init_impl_type(T->type, T->text);
482 free(T);
483 }
484 impl_type(type_) ::= FLOAT(T). {
485 type_ = init_impl_type(T->type, T->text);
486 free(T);
487 }
488 impl_type(type_) ::= STRING(T). {
489 type_ = init_impl_type(T->type, T->text);
490 free(T);
491 }
492 impl_type(type_) ::= ARRAY(T). {
493 type_ = init_impl_type(T->type, T->text);
494 free(T);
495 }
496
497 %type pointers {unsigned}
498 pointers(p) ::= POINTER. {++p;}
499 pointers(p) ::= pointers(P) POINTER. {p = P+1;}