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