c28ff5914aeff7dd04b2a0c2c6e9d7decff64493
[m6w6/ext-psi] / src / parser_proc.y
1 %include {
2 #include <stddef.h>
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <stdarg.h>
7
8 #include "parser.h"
9
10 void psi_error(int, const char *, int, const char *, ...);
11 }
12
13 %name PSI_ParserProc
14 %token_prefix PSI_T_
15 %token_type {PSI_Token *}
16 %token_destructor {free($$);}
17 %default_destructor {(void)P;}
18 %extra_argument {PSI_Parser *P}
19 /* TOKEN is defined inside syntax_error */
20 %syntax_error {
21 ++P->errors;
22 if (TOKEN && TOKEN->type != PSI_T_EOF) {
23 psi_error(PSI_WARNING, TOKEN->file, TOKEN->line, "PSI syntax error: Unexpected token '%s'", TOKEN->text);
24 } else {
25 psi_error(PSI_WARNING, P->psi.file.fn, P->line, "PSI syntax error: Unexpected end of input");
26 }
27 }
28
29 %nonassoc NAME.
30 %left PLUS MINUS.
31 %left SLASH ASTERISK.
32 %fallback NAME TEMP FREE SET LET RETURN CALLOC LIB STRING.
33
34 file ::= blocks.
35
36 blocks ::= block.
37 blocks ::= blocks block.
38
39 block ::= EOF.
40 block ::= EOS.
41
42 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
43 if (P->psi.file.ln) {
44 P->error(P, T, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
45 } else {
46 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
47 }
48 free(libname);
49 free(T);
50 }
51
52 block ::= decl(decl). {
53 P->decls = add_decl(P->decls, decl);
54 }
55 block ::= impl(impl). {
56 P->impls = add_impl(P->impls, impl);
57 }
58 block ::= decl_typedef(def). {
59 P->defs = add_decl_typedef(P->defs, def);
60 if (def->type->strct) {
61 P->structs = add_decl_struct(P->structs, def->type->strct);
62 }
63 if (def->type->enm) {
64 P->enums = add_decl_enum(P->enums, def->type->enm);
65 }
66 }
67 block ::= constant(constant). {
68 P->consts = add_constant(P->consts, constant);
69 }
70 block ::= decl_struct(strct). {
71 P->structs = add_decl_struct(P->structs, strct);
72 }
73 block ::= decl_union(u). {
74 P->unions = add_decl_union(P->unions, u);
75 }
76 block ::= decl_enum(e). {
77 P->enums = add_decl_enum(P->enums, e);
78 }
79
80 optional_name(n) ::= .{
81 n = NULL;
82 }
83 optional_name(n) ::= NAME(N). {
84 n = N;
85 }
86
87 enum_name(n) ::= ENUM(E) optional_name(N). {
88 if (N) {
89 n = N;
90 free(E);
91 } else {
92 char digest[17];
93
94 PSI_TokenHash(E, digest);
95 n = PSI_TokenTranslit(PSI_TokenAppend(E, 1, digest), " ", "@");
96 }
97 }
98
99 %type decl_enum {decl_enum *}
100 %destructor decl_enum {free_decl_enum($$);}
101 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
102 e = init_decl_enum(N->text, list);
103 e->token = N;
104 }
105
106 %type decl_enum_items {decl_enum_items*}
107 %destructor decl_enum_items {free_decl_enum_items($$);}
108 decl_enum_items(l) ::= decl_enum_item(i). {
109 l = init_decl_enum_items(i);
110 }
111 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
112 l = add_decl_enum_item(l_, i);
113 }
114
115 %type decl_enum_item {decl_enum_item*}
116 %destructor decl_enum_item {free_decl_enum_item($$);}
117 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
118 i = init_decl_enum_item(N->text, num);
119 i->token = N;
120 }
121 decl_enum_item(i) ::= NAME(N). {
122 i = init_decl_enum_item(N->text, NULL);
123 i->token = N;
124 }
125
126 union_name(n) ::= UNION(U) optional_name(N). {
127 if (N) {
128 n = N;
129 free(U);
130 } else {
131 char digest[17];
132
133 PSI_TokenHash(U, digest);
134 n = PSI_TokenTranslit(PSI_TokenAppend(U, 1, digest), " ", "@");
135 }
136 }
137
138 struct_name(n) ::= STRUCT(S) optional_name(N). {
139 if (N) {
140 n = N;
141 free(S);
142 } else {
143 char digest[17];
144
145 PSI_TokenHash(S, digest);
146 n = PSI_TokenTranslit(PSI_TokenAppend(S, 1, digest), " ", "@");
147 }
148 }
149
150 %type decl_struct_args_block {decl_args*}
151 %destructor decl_struct_args_block {free_decl_args($$);}
152 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
153 args_ = args;
154 }
155 %type decl_struct_args {decl_args*}
156 %destructor decl_struct_args {free_decl_args($$);}
157 decl_struct_args(args_) ::= decl_struct_args_block(args). {
158 args_ = args;
159 }
160 decl_struct_args(args_) ::= EOS. {
161 args_ = init_decl_args(NULL);
162 }
163
164
165 %type decl_struct {decl_struct*}
166 %destructor decl_struct {free_decl_struct($$);}
167 decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
168 strct = init_decl_struct(N->text, args);
169 strct->align = as.a;
170 strct->size = as.s;
171 strct->token = N;
172 }
173
174 %type align_and_size { struct {size_t a; size_t s; } }
175 align_and_size(as) ::= . {
176 as.a = 0;
177 as.s = 0;
178 }
179 align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
180 as.a = atol(A->text);
181 as.s = atol(S->text);
182 free(A);
183 free(S);
184 }
185
186 %type decl_union {decl_union*}
187 %destructor decl_union {free_decl_union($$);}
188 decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
189 u = init_decl_union(N->text, args);
190 u->align = as.a;
191 u->size = as.s;
192 u->token = N;
193 }
194
195 %token_class const_type_token BOOL INT FLOAT STRING.
196 %type const_type {const_type*}
197 %destructor const_type {free_const_type($$);}
198 const_type(type_) ::= const_type_token(T). {
199 type_ = init_const_type(T->type, T->text);
200 free(T);
201 }
202 %type constant {constant*}
203 %destructor constant {free_constant($$);}
204 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
205 constant = init_constant(type, T->text, val);
206 free(T);
207 }
208
209 %type decl_typedef {decl_arg*}
210 %destructor decl_typedef {
211 free_decl_arg($$);
212 if ($$->type->strct) {
213 free_decl_struct($$->type->strct);
214 }
215 if ($$->type->enm) {
216 free_decl_enum($$->type->enm);
217 }
218 if ($$->type->func) {
219 free_decl($$->type->func);
220 }
221 }
222 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
223 def = def_;
224 def->token = T;
225 }
226 %type decl_typedef_body_ex {decl_arg*}
227 %destructor decl_typedef_body_ex {
228 free_decl_arg($$);
229 if ($$->type->strct) {
230 free_decl_struct($$->type->strct);
231 }
232 if ($$->type->enm) {
233 free_decl_enum($$->type->enm);
234 }
235 if ($$->type->unn) {
236 free_decl_union($$->type->unn);
237 }
238 if ($$->type->func) {
239 free_decl($$->type->func);
240 }
241 }
242 decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
243 def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var);
244 def->type->token = PSI_TokenCopy(N);
245 def->type->strct = init_decl_struct(N->text, args);
246 def->type->strct->token = N;
247 def->type->strct->align = as.a;
248 def->type->strct->size = as.s;
249 }
250 decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
251 def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var);
252 def->type->token = PSI_TokenCopy(N);
253 def->type->unn = init_decl_union(N->text, args);
254 def->type->unn->token = N;
255 def->type->unn->align = as.a;
256 def->type->unn->size = as.s;
257 }
258 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
259 def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0));
260 def->var->token = ALIAS;
261 def->type->token = PSI_TokenCopy(e->token);
262 def->type->enm = e;
263 }
264 %type decl_typedef_body {decl_arg*}
265 %destructor decl_typedef_body {
266 free_decl_arg($$);
267 if ($$->type->strct) {
268 free_decl_struct($$->type->strct);
269 }
270 if ($$->type->enm) {
271 free_decl_enum($$->type->enm);
272 }
273 if ($$->type->unn) {
274 free_decl_union($$->type->unn);
275 }
276 if ($$->type->func) {
277 free_decl($$->type->func);
278 }
279 }
280 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
281 def = def_;
282 }
283 decl_typedef_body(def) ::= decl_func(func_) LPAREN decl_args(args) RPAREN. {
284 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
285 def->type->token = PSI_TokenCopy(func_->token);
286 def->type->func = init_decl(init_decl_abi("default"), func_, args);
287 }
288 decl_typedef_body(def) ::= decl_arg(arg). {
289 def = arg;
290 }
291
292 %type decl {decl*}
293 %destructor decl {free_decl($$);}
294 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
295 decl = init_decl(abi, func, args);
296 }
297
298 %type decl_func {decl_arg*}
299 %destructor decl_func {free_decl_arg($$);}
300 decl_func(func) ::= decl_arg(arg). {
301 func = arg;
302 }
303 /* special case for void functions */
304 decl_func(func) ::= VOID(T) NAME(N). {
305 func = init_decl_arg(
306 init_decl_type(T->type, T->text),
307 init_decl_var(N->text, 0, 0)
308 );
309 func->type->token = T;
310 func->var->token = N;
311 func->token = N;
312 }
313
314 %type decl_abi {decl_abi*}
315 %destructor decl_abi {free_decl_abi($$);}
316 decl_abi(abi) ::= NAME(T). {
317 abi = init_decl_abi(T->text);
318 abi->token = T;
319 }
320
321 %type decl_var {decl_var*}
322 %destructor decl_var {free_decl_var($$);}
323 decl_var(var) ::= indirection(p) NAME(T). {
324 var = init_decl_var(T->text, p, 0);
325 var->token = T;
326 }
327 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
328 var = init_decl_var(T->text, p+1, atol(D->text));
329 var->token = T;
330 free(D);
331 }
332
333 %type decl_vars {decl_vars*}
334 %destructor decl_vars {free_decl_vars($$);}
335 decl_vars(vars) ::= decl_var(var). {
336 vars = init_decl_vars(var);
337 }
338 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
339 vars = add_decl_var(vars_, var);
340 }
341
342 %type decl_arg {decl_arg*}
343 %destructor decl_arg {free_decl_arg($$);}
344 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
345 arg_ = init_decl_arg(type, var);
346 }
347 /* void pointers need a specific rule */
348 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
349 arg_ = init_decl_arg(
350 init_decl_type(T->type, T->text),
351 init_decl_var(N->text, p, 0)
352 );
353 arg_->type->token = T;
354 arg_->var->token = N;
355 arg_->token = N;
356 }
357 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
358 arg_ = init_decl_arg(
359 init_decl_type(T->type, T->text),
360 init_decl_var(N->text, p, 0)
361 );
362 arg_->type->token = T;
363 arg_->var->token = N;
364 arg_->token = N;
365 }
366
367 %type decl_args {decl_args*}
368 %destructor decl_args {free_decl_args($$);}
369 decl_args ::= .
370 decl_args ::= VOID.
371 decl_args(args) ::= decl_arg(arg). {
372 args = init_decl_args(arg);
373 }
374 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
375 args = add_decl_arg(args_, arg);
376 }
377 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
378 args = args_;
379 args->varargs = 1;
380 }
381 %type struct_args {decl_args*}
382 %destructor struct_args {free_decl_args($$);}
383 struct_args(args) ::= struct_arg(arg). {
384 args = init_decl_args(arg);
385 }
386 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
387 args = add_decl_arg(args_, arg);
388 }
389 %type struct_arg {decl_arg*}
390 %destructor struct_arg {
391 free_decl_arg($$);
392 if ($$->type->strct) {
393 free_decl_struct($$->type->strct);
394 }
395 if ($$->type->enm) {
396 free_decl_enum($$->type->enm);
397 }
398 if ($$->type->func) {
399 free_decl($$->type->func);
400 }
401 }
402 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
403 arg_ = def;
404 if (def->type->strct) {
405 P->structs = add_decl_struct(P->structs, def->type->strct);
406 }
407 if (def->type->enm) {
408 P->enums = add_decl_enum(P->enums, def->type->enm);
409 }
410 }
411 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
412 arg_->layout = layout_;
413 arg = arg_;
414 }
415
416 %type struct_layout {decl_struct_layout*}
417 %destructor struct_layout {free_decl_struct_layout($$);}
418 struct_layout(layout) ::= . {
419 layout = NULL;
420 }
421 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
422 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
423 free(POS);
424 free(SIZ);
425 }
426
427 /* un/signed, urgh */
428 decl_scalar_type(type_) ::= CHAR(C). {
429 type_ = C;
430 }
431 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
432 if (s) {
433 type_ = PSI_TokenCat(2, S, s);
434 free(S);
435 free(s);
436 } else {
437 type_ = S;
438 }
439 }
440 decl_scalar_type_short(s) ::= . {
441 s = NULL;
442 }
443
444 decl_scalar_type_short(s) ::= INT(I). {
445 s = I;
446 }
447 decl_scalar_type(type_) ::= INT(I). {
448 type_ = I;
449 }
450 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
451 if (l) {
452 type_ = PSI_TokenCat(2, L, l);
453 free(L);
454 free(l);
455 } else {
456 type_ = L;
457 }
458 }
459 decl_scalar_type_long(l) ::= . {
460 l = NULL;
461 }
462 decl_scalar_type_long(l) ::= DOUBLE(D). {
463 l = D;
464 }
465 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
466 if (ll) {
467 l = PSI_TokenCat(2, L, ll);
468 free(L);
469 free(ll);
470 } else {
471 l = L;
472 }
473 }
474 decl_scalar_type_long_long(ll) ::= . {
475 ll = NULL;
476 }
477 decl_scalar_type_long_long(ll) ::= INT(I). {
478 ll = I;
479 }
480 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
481 PSI_Token *T = PSI_TokenCat(2, U, N);
482 type_ = init_decl_type(T->type, T->text);
483 type_->token = T;
484 free(U);
485 free(N);
486 }
487 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
488 PSI_Token *T = PSI_TokenCat(2, S, N);
489 type_ = init_decl_type(T->type, T->text);
490 type_->token = T;
491 free(S);
492 free(N);
493 }
494 decl_type(type_) ::= UNSIGNED(U). {
495 type_ = init_decl_type(PSI_T_NAME, U->text);
496 type_->token = U;
497 }
498 decl_type(type_) ::= SIGNED(S). {
499 type_ = init_decl_type(PSI_T_NAME, S->text);
500 type_->token = S;
501 }
502 decl_type(type_) ::= decl_scalar_type(N). {
503 type_ = init_decl_type(N->type, N->text);
504 type_->token = N;
505 }
506 /* structs ! */
507 decl_type(type_) ::= STRUCT(S) NAME(T). {
508 type_ = init_decl_type(S->type, T->text);
509 type_->token = T;
510 free(S);
511 }
512 decl_type(type_) ::= UNION(U) NAME(T). {
513 type_ = init_decl_type(U->type, T->text);
514 type_->token = T;
515 free(U);
516 }
517 decl_type(type_) ::= ENUM(E) NAME(T). {
518 type_ = init_decl_type(E->type, T->text);
519 type_->token = T;
520 free(E);
521 }
522 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
523 %type decl_type {decl_type*}
524 %destructor decl_type {free_decl_type($$);}
525 decl_type(type_) ::= decl_type_token(T). {
526 type_ = init_decl_type(T->type, T->text);
527 type_->token = T;
528 }
529
530
531 %type const_decl_type {decl_type*}
532 %destructor const_decl_type {free_decl_type($$);}
533 const_decl_type(type) ::= decl_type(type_). {
534 type = type_;
535 }
536 const_decl_type(type) ::= CONST decl_type(type_). {
537 type = type_;
538 }
539
540 %type impl {impl*}
541 %destructor impl {free_impl($$);}
542 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
543 impl = init_impl(func, stmts);
544 }
545
546 %type impl_func {impl_func*}
547 %destructor impl_func {free_impl_func($$);}
548 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
549 func = init_impl_func(NAME->text, args, type, r);
550 func->token = NAME;
551 }
552
553 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
554 %type impl_def_val {impl_def_val*}
555 %destructor impl_def_val {free_impl_def_val($$);}
556 impl_def_val(def) ::= impl_def_val_token(T). {
557 def = init_impl_def_val(T->type, T->text);
558 free(T);
559 }
560
561 %type impl_var {impl_var*}
562 %destructor impl_var {free_impl_var($$);}
563 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
564 var = init_impl_var(T->text, r);
565 var->token = T;
566 }
567
568 %type impl_arg {impl_arg*}
569 %destructor impl_arg {free_impl_arg($$);}
570 impl_arg(arg) ::= impl_type(type) impl_var(var). {
571 arg = init_impl_arg(type, var, NULL);
572 }
573 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
574 arg = init_impl_arg(type, var, def);
575 }
576
577 %type impl_args {impl_args*}
578 %destructor impl_args {free_impl_args($$);}
579 impl_args(args) ::= LPAREN RPAREN. {
580 args = NULL;
581 }
582 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
583 args = args_;
584 }
585 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
586 args = args_;
587 args->vararg.name = va;
588 }
589
590 %type impl_vararg {impl_arg*}
591 %destructor impl_vararg {free_impl_arg($$);}
592 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
593 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
594 free(T);
595 }
596
597 %type impl_arg_list {impl_args*}
598 %destructor impl_arg_list {free_impl_args($$);}
599 impl_arg_list(args) ::= impl_arg(arg). {
600 args = init_impl_args(arg);
601 }
602 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
603 args = add_impl_arg(args_, arg);
604 }
605
606 %type impl_stmts {impl_stmts*}
607 %destructor impl_stmts {free_impl_stmts($$);}
608 impl_stmts(stmts) ::= impl_stmt(stmt). {
609 stmts = init_impl_stmts(stmt);
610 }
611 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
612 stmts = add_impl_stmt(stmts_, stmt);
613 }
614
615 %type impl_stmt {impl_stmt*}
616 %destructor impl_stmt {free_impl_stmt($$);}
617 impl_stmt(stmt) ::= let_stmt(let). {
618 stmt = init_impl_stmt(PSI_T_LET, let);
619 }
620 impl_stmt(stmt) ::= set_stmt(set). {
621 stmt = init_impl_stmt(PSI_T_SET, set);
622 }
623 impl_stmt(stmt) ::= return_stmt(ret). {
624 stmt = init_impl_stmt(PSI_T_RETURN, ret);
625 }
626 impl_stmt(stmt) ::= free_stmt(free). {
627 stmt = init_impl_stmt(PSI_T_FREE, free);
628 }
629
630 %token_class num_exp_token NUMBER NSNAME.
631 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
632 %type num_exp {num_exp*}
633 %destructor num_exp {free_num_exp($$);}
634 num_exp(exp) ::= num_exp_token(tok). {
635 exp = init_num_exp(tok->type, tok->text);
636 exp->token = tok;
637 }
638 num_exp(exp) ::= decl_var(var). {
639 exp = init_num_exp(PSI_T_NAME, var);
640 exp->token = PSI_TokenCopy(var->token);
641 }
642 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
643 exp_->operator = operator_->type;
644 exp_->operand = operand_;
645 exp = exp_;
646 free(operator_);
647 }
648
649 %type let_stmt {let_stmt*}
650 %destructor let_stmt {free_let_stmt($$);}
651 let_stmt(let) ::= LET decl_var(var) EOS. {
652 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
653 }
654 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
655 val->flags.one.is_reference = r ? 1 : 0;
656 let = init_let_stmt(var, val);
657 }
658 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
659 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
660 }
661
662 %type let_val {let_val*}
663 %destructor let_val {free_let_val($$);}
664 let_val(val) ::= NULL. {
665 val = init_let_val(PSI_LET_NULL, NULL);
666 }
667 let_val(val) ::= num_exp(exp). {
668 val = init_let_val(PSI_LET_NUMEXP, exp);
669 }
670 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
671 val = init_let_val(PSI_LET_CALLOC, alloc);
672 }
673 let_val(val) ::= let_func(func). {
674 val = init_let_val(PSI_LET_FUNC, func);
675 }
676
677 %type let_calloc {let_calloc*}
678 %destructor let_calloc {free_let_calloc($$);}
679 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
680 alloc = init_let_calloc(nmemb, size);
681 }
682 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
683 %type let_func {let_func*}
684 %destructor let_func {free_let_func($$);}
685 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
686 func = init_let_func(T->type, T->text, var);
687 free(T);
688 }
689
690
691 %type set_stmt {set_stmt*}
692 %destructor set_stmt {free_set_stmt($$);}
693 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
694 set = init_set_stmt(var, val);
695 }
696
697 %type set_value {set_value*}
698 %destructor set_value {free_set_value($$);}
699 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
700 val = init_set_value(func, init_decl_vars(var));
701 }
702 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
703 val = init_set_value(func, init_decl_vars(var));
704 val->num = num_;
705 }
706 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
707 free_set_func(func_);
708 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
709 val->func->token = T;
710 }
711 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
712 val = vals;
713 val->func = func_;
714 val->vars = init_decl_vars(var);
715 }
716 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
717 val = vals;
718 val->func = func_;
719 val->num = num_;
720 val->vars = init_decl_vars(var);
721 }
722 %type set_vals {set_value*}
723 %destructor set_vals {free_set_value($$);}
724 set_vals(vals) ::= set_value(val). {
725 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
726 }
727 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
728 vals = add_inner_set_value(vals_, val);
729 }
730
731 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
732 %type set_func {set_func*}
733 %destructor set_func {free_set_func($$);}
734 set_func(func) ::= set_func_token(T). {
735 func = init_set_func(T->type, T->text);
736 func->token = T;
737 }
738
739 %type return_stmt {return_stmt*}
740 %destructor return_stmt {free_return_stmt($$);}
741 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
742 ret = init_return_stmt(val);
743 ret->token = T;
744 }
745
746 %type free_stmt {free_stmt*}
747 %destructor free_stmt {free_free_stmt($$);}
748 free_stmt(free) ::= FREE free_calls(calls) EOS. {
749 free = init_free_stmt(calls);
750 }
751
752 %type free_calls {free_calls*}
753 %destructor free_calls {free_free_calls($$);}
754 free_calls(calls) ::= free_call(call). {
755 calls = init_free_calls(call);
756 }
757 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
758 calls = add_free_call(calls_, call);
759 }
760
761 %type free_call {free_call*}
762 %destructor free_call {free_free_call($$);}
763 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
764 call = init_free_call(F->text, vars);
765 call->token = F;
766 }
767
768 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
769 %type impl_type {impl_type*}
770 %destructor impl_type {free_impl_type($$);}
771 impl_type(type_) ::= impl_type_token(T). {
772 type_ = init_impl_type(T->type, T->text);
773 free(T);
774 }
775
776 %type reference {char}
777 reference(r) ::= . {r = 0;}
778 reference(r) ::= AMPERSAND. {r = 1;}
779
780 %type indirection {unsigned}
781 indirection(i) ::= . {i = 0;}
782 indirection(i) ::= pointers(p). {i = p;}
783
784 %type pointers {unsigned}
785 pointers(p) ::= ASTERISK. {p = 1;}
786 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}