flush
[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 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(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) struct_size(size_) decl_struct_args(args). {
168 strct = init_decl_struct(N->text, args);
169 strct->size = size_;
170 strct->token = N;
171 }
172
173 %type struct_size {size_t}
174 struct_size(size) ::= . {
175 size = 0;
176 }
177 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
178 size = atol(SIZ->text);
179 free(SIZ);
180 }
181
182 %type decl_union {decl_union*}
183 %destructor decl_union {free_decl_union($$);}
184 decl_union(u) ::= UNION NAME(N) struct_size(size_) decl_struct_args(args). {
185 u = init_decl_union(N->text, args);
186 u->size = size_;
187 u->token = N;
188 }
189
190 %token_class const_type_token BOOL INT FLOAT STRING.
191 %type const_type {const_type*}
192 %destructor const_type {free_const_type($$);}
193 const_type(type_) ::= const_type_token(T). {
194 type_ = init_const_type(T->type, T->text);
195 free(T);
196 }
197 %type constant {constant*}
198 %destructor constant {free_constant($$);}
199 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
200 constant = init_constant(type, T->text, val);
201 free(T);
202 }
203
204 %type decl_typedef {decl_arg*}
205 %destructor decl_typedef {
206 free_decl_arg($$);
207 if ($$->type->strct) {
208 free_decl_struct($$->type->strct);
209 }
210 if ($$->type->enm) {
211 free_decl_enum($$->type->enm);
212 }
213 if ($$->type->func) {
214 free_decl($$->type->func);
215 }
216 }
217 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
218 def = def_;
219 def->token = T;
220 }
221 %type decl_typedef_body_ex {decl_arg*}
222 %destructor decl_typedef_body_ex {
223 free_decl_arg($$);
224 if ($$->type->strct) {
225 free_decl_struct($$->type->strct);
226 }
227 if ($$->type->enm) {
228 free_decl_enum($$->type->enm);
229 }
230 if ($$->type->unn) {
231 free_decl_union($$->type->unn);
232 }
233 if ($$->type->func) {
234 free_decl($$->type->func);
235 }
236 }
237 decl_typedef_body_ex(def) ::= struct_name(N) struct_size(size_) decl_struct_args_block(args) decl_var(var). {
238 def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var);
239 def->type->token = PSI_TokenCopy(N);
240 def->type->strct = init_decl_struct(N->text, args);
241 def->type->strct->token = N;
242 def->type->strct->size = size_;
243 }
244 decl_typedef_body_ex(def) ::= union_name(N) struct_size(size_) decl_struct_args_block(args) decl_var(var). {
245 def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var);
246 def->type->token = PSI_TokenCopy(N);
247 def->type->unn = init_decl_union(N->text, args);
248 def->type->unn->token = N;
249 def->type->unn->size = size_;
250 }
251 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
252 def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0));
253 def->var->token = ALIAS;
254 def->type->token = PSI_TokenCopy(e->token);
255 def->type->enm = e;
256 }
257 %type decl_typedef_body {decl_arg*}
258 %destructor decl_typedef_body {
259 free_decl_arg($$);
260 if ($$->type->strct) {
261 free_decl_struct($$->type->strct);
262 }
263 if ($$->type->enm) {
264 free_decl_enum($$->type->enm);
265 }
266 if ($$->type->unn) {
267 free_decl_union($$->type->unn);
268 }
269 if ($$->type->func) {
270 free_decl($$->type->func);
271 }
272 }
273 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
274 def = def_;
275 }
276 decl_typedef_body(def) ::= decl_func(func_) LPAREN decl_args(args) RPAREN. {
277 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
278 def->type->token = PSI_TokenCopy(func_->token);
279 def->type->func = init_decl(init_decl_abi("default"), func_, args);
280 }
281 decl_typedef_body(def) ::= decl_arg(arg). {
282 def = arg;
283 }
284
285 %type decl {decl*}
286 %destructor decl {free_decl($$);}
287 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
288 decl = init_decl(abi, func, args);
289 }
290
291 %type decl_func {decl_arg*}
292 %destructor decl_func {free_decl_arg($$);}
293 decl_func(func) ::= decl_arg(arg). {
294 func = arg;
295 }
296 /* special case for void functions */
297 decl_func(func) ::= VOID(T) NAME(N). {
298 func = init_decl_arg(
299 init_decl_type(T->type, T->text),
300 init_decl_var(N->text, 0, 0)
301 );
302 func->type->token = T;
303 func->var->token = N;
304 func->token = N;
305 }
306
307 %type decl_abi {decl_abi*}
308 %destructor decl_abi {free_decl_abi($$);}
309 decl_abi(abi) ::= NAME(T). {
310 abi = init_decl_abi(T->text);
311 abi->token = T;
312 }
313
314 %type decl_var {decl_var*}
315 %destructor decl_var {free_decl_var($$);}
316 decl_var(var) ::= indirection(p) NAME(T). {
317 var = init_decl_var(T->text, p, 0);
318 var->token = T;
319 }
320 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
321 var = init_decl_var(T->text, p+1, atol(D->text));
322 var->token = T;
323 free(D);
324 }
325
326 %type decl_vars {decl_vars*}
327 %destructor decl_vars {free_decl_vars($$);}
328 decl_vars(vars) ::= decl_var(var). {
329 vars = init_decl_vars(var);
330 }
331 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
332 vars = add_decl_var(vars_, var);
333 }
334
335 %type decl_arg {decl_arg*}
336 %destructor decl_arg {free_decl_arg($$);}
337 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
338 arg_ = init_decl_arg(type, var);
339 }
340 /* void pointers need a specific rule */
341 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
342 arg_ = init_decl_arg(
343 init_decl_type(T->type, T->text),
344 init_decl_var(N->text, p, 0)
345 );
346 arg_->type->token = T;
347 arg_->var->token = N;
348 arg_->token = N;
349 }
350 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
351 arg_ = init_decl_arg(
352 init_decl_type(T->type, T->text),
353 init_decl_var(N->text, p, 0)
354 );
355 arg_->type->token = T;
356 arg_->var->token = N;
357 arg_->token = N;
358 }
359
360 %type decl_args {decl_args*}
361 %destructor decl_args {free_decl_args($$);}
362 decl_args ::= .
363 decl_args ::= VOID.
364 decl_args(args) ::= decl_arg(arg). {
365 args = init_decl_args(arg);
366 }
367 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
368 args = add_decl_arg(args_, arg);
369 }
370 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
371 args = args_;
372 args->varargs = 1;
373 }
374 %type struct_args {decl_args*}
375 %destructor struct_args {free_decl_args($$);}
376 struct_args(args) ::= struct_arg(arg). {
377 args = init_decl_args(arg);
378 }
379 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
380 args = add_decl_arg(args_, arg);
381 }
382 %type struct_arg {decl_arg*}
383 %destructor struct_arg {
384 free_decl_arg($$);
385 if ($$->type->strct) {
386 free_decl_struct($$->type->strct);
387 }
388 if ($$->type->enm) {
389 free_decl_enum($$->type->enm);
390 }
391 if ($$->type->func) {
392 free_decl($$->type->func);
393 }
394 }
395 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
396 arg_ = def;
397 if (def->type->strct) {
398 P->structs = add_decl_struct(P->structs, def->type->strct);
399 }
400 if (def->type->enm) {
401 P->enums = add_decl_enum(P->enums, def->type->enm);
402 }
403 }
404 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
405 arg_->layout = layout_;
406 arg = arg_;
407 }
408
409 %type struct_layout {decl_struct_layout*}
410 %destructor struct_layout {free_decl_struct_layout($$);}
411 struct_layout(layout) ::= . {
412 layout = NULL;
413 }
414 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
415 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
416 free(POS);
417 free(SIZ);
418 }
419
420 /* un/signed, urgh */
421 decl_scalar_type(type_) ::= CHAR(C). {
422 type_ = C;
423 }
424 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
425 if (s) {
426 type_ = PSI_TokenCat(2, S, s);
427 free(S);
428 free(s);
429 } else {
430 type_ = S;
431 }
432 }
433 decl_scalar_type_short(s) ::= . {
434 s = NULL;
435 }
436
437 decl_scalar_type_short(s) ::= INT(I). {
438 s = I;
439 }
440 decl_scalar_type(type_) ::= INT(I). {
441 type_ = I;
442 }
443 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
444 if (l) {
445 type_ = PSI_TokenCat(2, L, l);
446 free(L);
447 free(l);
448 } else {
449 type_ = L;
450 }
451 }
452 decl_scalar_type_long(l) ::= . {
453 l = NULL;
454 }
455 decl_scalar_type_long(l) ::= DOUBLE(D). {
456 l = D;
457 }
458 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
459 if (ll) {
460 l = PSI_TokenCat(2, L, ll);
461 free(L);
462 free(ll);
463 } else {
464 l = L;
465 }
466 }
467 decl_scalar_type_long_long(ll) ::= . {
468 ll = NULL;
469 }
470 decl_scalar_type_long_long(ll) ::= INT(I). {
471 ll = I;
472 }
473 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
474 PSI_Token *T = PSI_TokenCat(2, U, N);
475 type_ = init_decl_type(T->type, T->text);
476 type_->token = T;
477 free(U);
478 free(N);
479 }
480 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
481 PSI_Token *T = PSI_TokenCat(2, S, N);
482 type_ = init_decl_type(T->type, T->text);
483 type_->token = T;
484 free(S);
485 free(N);
486 }
487 decl_type(type_) ::= UNSIGNED(U). {
488 type_ = init_decl_type(PSI_T_NAME, U->text);
489 type_->token = U;
490 }
491 decl_type(type_) ::= SIGNED(S). {
492 type_ = init_decl_type(PSI_T_NAME, S->text);
493 type_->token = S;
494 }
495 decl_type(type_) ::= decl_scalar_type(N). {
496 type_ = init_decl_type(N->type, N->text);
497 type_->token = N;
498 }
499 /* structs ! */
500 decl_type(type_) ::= STRUCT(S) NAME(T). {
501 type_ = init_decl_type(S->type, T->text);
502 type_->token = T;
503 free(S);
504 }
505 decl_type(type_) ::= UNION(U) NAME(T). {
506 type_ = init_decl_type(U->type, T->text);
507 type_->token = T;
508 free(U);
509 }
510 decl_type(type_) ::= ENUM(E) NAME(T). {
511 type_ = init_decl_type(E->type, T->text);
512 type_->token = T;
513 free(E);
514 }
515 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
516 %type decl_type {decl_type*}
517 %destructor decl_type {free_decl_type($$);}
518 decl_type(type_) ::= decl_type_token(T). {
519 type_ = init_decl_type(T->type, T->text);
520 type_->token = T;
521 }
522
523
524 %type const_decl_type {decl_type*}
525 %destructor const_decl_type {free_decl_type($$);}
526 const_decl_type(type) ::= decl_type(type_). {
527 type = type_;
528 }
529 const_decl_type(type) ::= CONST decl_type(type_). {
530 type = type_;
531 }
532
533 %type impl {impl*}
534 %destructor impl {free_impl($$);}
535 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
536 impl = init_impl(func, stmts);
537 }
538
539 %type impl_func {impl_func*}
540 %destructor impl_func {free_impl_func($$);}
541 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
542 func = init_impl_func(NAME->text, args, type, r);
543 func->token = NAME;
544 }
545
546 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
547 %type impl_def_val {impl_def_val*}
548 %destructor impl_def_val {free_impl_def_val($$);}
549 impl_def_val(def) ::= impl_def_val_token(T). {
550 def = init_impl_def_val(T->type, T->text);
551 free(T);
552 }
553
554 %type impl_var {impl_var*}
555 %destructor impl_var {free_impl_var($$);}
556 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
557 var = init_impl_var(T->text, r);
558 var->token = T;
559 }
560
561 %type impl_arg {impl_arg*}
562 %destructor impl_arg {free_impl_arg($$);}
563 impl_arg(arg) ::= impl_type(type) impl_var(var). {
564 arg = init_impl_arg(type, var, NULL);
565 }
566 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
567 arg = init_impl_arg(type, var, def);
568 }
569
570 %type impl_args {impl_args*}
571 %destructor impl_args {free_impl_args($$);}
572 impl_args(args) ::= LPAREN RPAREN. {
573 args = NULL;
574 }
575 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
576 args = args_;
577 }
578 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
579 args = args_;
580 args->vararg.name = va;
581 }
582
583 %type impl_vararg {impl_arg*}
584 %destructor impl_vararg {free_impl_arg($$);}
585 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
586 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
587 free(T);
588 }
589
590 %type impl_arg_list {impl_args*}
591 %destructor impl_arg_list {free_impl_args($$);}
592 impl_arg_list(args) ::= impl_arg(arg). {
593 args = init_impl_args(arg);
594 }
595 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
596 args = add_impl_arg(args_, arg);
597 }
598
599 %type impl_stmts {impl_stmts*}
600 %destructor impl_stmts {free_impl_stmts($$);}
601 impl_stmts(stmts) ::= impl_stmt(stmt). {
602 stmts = init_impl_stmts(stmt);
603 }
604 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
605 stmts = add_impl_stmt(stmts_, stmt);
606 }
607
608 %type impl_stmt {impl_stmt*}
609 %destructor impl_stmt {free_impl_stmt($$);}
610 impl_stmt(stmt) ::= let_stmt(let). {
611 stmt = init_impl_stmt(PSI_T_LET, let);
612 }
613 impl_stmt(stmt) ::= set_stmt(set). {
614 stmt = init_impl_stmt(PSI_T_SET, set);
615 }
616 impl_stmt(stmt) ::= return_stmt(ret). {
617 stmt = init_impl_stmt(PSI_T_RETURN, ret);
618 }
619 impl_stmt(stmt) ::= free_stmt(free). {
620 stmt = init_impl_stmt(PSI_T_FREE, free);
621 }
622
623 %token_class num_exp_token NUMBER NSNAME.
624 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
625 %type num_exp {num_exp*}
626 %destructor num_exp {free_num_exp($$);}
627 num_exp(exp) ::= num_exp_token(tok). {
628 exp = init_num_exp(tok->type, tok->text);
629 exp->token = tok;
630 }
631 num_exp(exp) ::= decl_var(var). {
632 exp = init_num_exp(PSI_T_NAME, var);
633 exp->token = PSI_TokenCopy(var->token);
634 }
635 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
636 exp_->operator = operator_->type;
637 exp_->operand = operand_;
638 exp = exp_;
639 free(operator_);
640 }
641
642 %type let_stmt {let_stmt*}
643 %destructor let_stmt {free_let_stmt($$);}
644 let_stmt(let) ::= LET decl_var(var) EOS. {
645 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
646 }
647 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
648 val->flags.one.is_reference = r ? 1 : 0;
649 let = init_let_stmt(var, val);
650 }
651 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
652 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
653 }
654
655 %type let_val {let_val*}
656 %destructor let_val {free_let_val($$);}
657 let_val(val) ::= NULL. {
658 val = init_let_val(PSI_LET_NULL, NULL);
659 }
660 let_val(val) ::= num_exp(exp). {
661 val = init_let_val(PSI_LET_NUMEXP, exp);
662 }
663 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
664 val = init_let_val(PSI_LET_CALLOC, alloc);
665 }
666 let_val(val) ::= let_func(func). {
667 val = init_let_val(PSI_LET_FUNC, func);
668 }
669
670 %type let_calloc {let_calloc*}
671 %destructor let_calloc {free_let_calloc($$);}
672 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
673 alloc = init_let_calloc(nmemb, size);
674 }
675 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
676 %type let_func {let_func*}
677 %destructor let_func {free_let_func($$);}
678 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
679 func = init_let_func(T->type, T->text, var);
680 free(T);
681 }
682
683
684 %type set_stmt {set_stmt*}
685 %destructor set_stmt {free_set_stmt($$);}
686 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
687 set = init_set_stmt(var, val);
688 }
689
690 %type set_value {set_value*}
691 %destructor set_value {free_set_value($$);}
692 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
693 val = init_set_value(func, init_decl_vars(var));
694 }
695 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
696 val = init_set_value(func, init_decl_vars(var));
697 val->num = num_;
698 }
699 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
700 free_set_func(func_);
701 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
702 val->func->token = T;
703 }
704 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
705 val = vals;
706 val->func = func_;
707 val->vars = init_decl_vars(var);
708 }
709 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
710 val = vals;
711 val->func = func_;
712 val->num = num_;
713 val->vars = init_decl_vars(var);
714 }
715 %type set_vals {set_value*}
716 %destructor set_vals {free_set_value($$);}
717 set_vals(vals) ::= set_value(val). {
718 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
719 }
720 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
721 vals = add_inner_set_value(vals_, val);
722 }
723
724 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
725 %type set_func {set_func*}
726 %destructor set_func {free_set_func($$);}
727 set_func(func) ::= set_func_token(T). {
728 func = init_set_func(T->type, T->text);
729 func->token = T;
730 }
731
732 %type return_stmt {return_stmt*}
733 %destructor return_stmt {free_return_stmt($$);}
734 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
735 ret = init_return_stmt(val);
736 ret->token = T;
737 }
738
739 %type free_stmt {free_stmt*}
740 %destructor free_stmt {free_free_stmt($$);}
741 free_stmt(free) ::= FREE free_calls(calls) EOS. {
742 free = init_free_stmt(calls);
743 }
744
745 %type free_calls {free_calls*}
746 %destructor free_calls {free_free_calls($$);}
747 free_calls(calls) ::= free_call(call). {
748 calls = init_free_calls(call);
749 }
750 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
751 calls = add_free_call(calls_, call);
752 }
753
754 %type free_call {free_call*}
755 %destructor free_call {free_free_call($$);}
756 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
757 call = init_free_call(F->text, vars);
758 call->token = F;
759 }
760
761 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
762 %type impl_type {impl_type*}
763 %destructor impl_type {free_impl_type($$);}
764 impl_type(type_) ::= impl_type_token(T). {
765 type_ = init_impl_type(T->type, T->text);
766 free(T);
767 }
768
769 %type reference {char}
770 reference(r) ::= . {r = 0;}
771 reference(r) ::= AMPERSAND. {r = 1;}
772
773 %type indirection {unsigned}
774 indirection(i) ::= . {i = 0;}
775 indirection(i) ::= pointers(p). {i = p;}
776
777 %type pointers {unsigned}
778 pointers(p) ::= ASTERISK. {p = 1;}
779 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}