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