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 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 %type decl_typedef_body_fn_args {decl_args *}
284 %destructor decl_typedef_body_fn_args {free_decl_args($$);}
285 decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. {
286 args = args_;
287 }
288 decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). {
289 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
290 def->type->token = PSI_TokenCopy(func_->token);
291 def->type->func = init_decl(init_decl_abi("default"), func_, args);
292 }
293 decl_typedef_body(def) ::= decl_arg(arg). {
294 def = arg;
295 }
296
297 %type decl {decl*}
298 %destructor decl {free_decl($$);}
299 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
300 decl = init_decl(abi, func, args);
301 }
302
303 %type decl_func {decl_arg*}
304 %destructor decl_func {free_decl_arg($$);}
305 decl_func(func) ::= decl_arg(arg). {
306 func = arg;
307 }
308 /* special case for void functions */
309 decl_func(func) ::= VOID(T) NAME(N). {
310 func = init_decl_arg(
311 init_decl_type(T->type, T->text),
312 init_decl_var(N->text, 0, 0)
313 );
314 func->type->token = T;
315 func->var->token = N;
316 func->token = N;
317 }
318 decl_typedef_body(def) ::= VOID(T) indirection(i) LPAREN ASTERISK NAME(N) RPAREN decl_typedef_body_fn_args(args). {
319 decl_arg *func_ = init_decl_arg(
320 init_decl_type(T->type, T->text),
321 init_decl_var(N->text, i, 0)
322 );
323 func_->type->token = T;
324 func_->var->token = N;
325 func_->token = N;
326 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
327 def->type->token = PSI_TokenCopy(func_->token);
328 def->type->func = init_decl(init_decl_abi("default"), func_, args);
329 }
330 decl_typedef_body(def) ::= CONST VOID(T) pointers(i) LPAREN ASTERISK NAME(N) RPAREN decl_typedef_body_fn_args(args). {
331 decl_arg *func_ = init_decl_arg(
332 init_decl_type(T->type, T->text),
333 init_decl_var(N->text, i, 0)
334 );
335 func_->type->token = T;
336 func_->var->token = N;
337 func_->token = N;
338 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
339 def->type->token = PSI_TokenCopy(func_->token);
340 def->type->func = init_decl(init_decl_abi("default"), func_, args);
341 }
342
343 %type decl_abi {decl_abi*}
344 %destructor decl_abi {free_decl_abi($$);}
345 decl_abi(abi) ::= NAME(T). {
346 abi = init_decl_abi(T->text);
347 abi->token = T;
348 }
349
350 %type decl_var {decl_var*}
351 %destructor decl_var {free_decl_var($$);}
352 decl_var(var) ::= indirection(p) NAME(T). {
353 var = init_decl_var(T->text, p, 0);
354 var->token = T;
355 }
356 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
357 var = init_decl_var(T->text, p+1, atol(D->text));
358 var->token = T;
359 free(D);
360 }
361
362 %type decl_vars {decl_vars*}
363 %destructor decl_vars {free_decl_vars($$);}
364 decl_vars(vars) ::= decl_var(var). {
365 vars = init_decl_vars(var);
366 }
367 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
368 vars = add_decl_var(vars_, var);
369 }
370
371 %type decl_arg {decl_arg*}
372 %destructor decl_arg {free_decl_arg($$);}
373 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
374 arg_ = init_decl_arg(type, var);
375 }
376 decl_typedef_body(def) ::= const_decl_type(type_) indirection(i) LPAREN ASTERISK NAME(N) RPAREN decl_typedef_body_fn_args(args). {
377 decl_arg *func_ = init_decl_arg(
378 type_,
379 init_decl_var(N->text, i, 0)
380 );
381 func_->var->token = N;
382 func_->token = N;
383 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
384 def->type->token = PSI_TokenCopy(func_->token);
385 def->type->func = init_decl(init_decl_abi("default"), func_, args);
386 }
387
388 /* void pointers need a specific rule */
389 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
390 arg_ = init_decl_arg(
391 init_decl_type(T->type, T->text),
392 init_decl_var(N->text, p, 0)
393 );
394 arg_->type->token = T;
395 arg_->var->token = N;
396 arg_->token = N;
397 }
398 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
399 arg_ = init_decl_arg(
400 init_decl_type(T->type, T->text),
401 init_decl_var(N->text, p, 0)
402 );
403 arg_->type->token = T;
404 arg_->var->token = N;
405 arg_->token = N;
406 }
407
408 %type decl_args {decl_args*}
409 %destructor decl_args {free_decl_args($$);}
410 decl_args ::= .
411 decl_args ::= VOID.
412 decl_args(args) ::= decl_arg(arg). {
413 args = init_decl_args(arg);
414 }
415 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
416 args = add_decl_arg(args_, arg);
417 }
418 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
419 args = args_;
420 args->varargs = 1;
421 }
422 %type struct_args {decl_args*}
423 %destructor struct_args {free_decl_args($$);}
424 struct_args(args) ::= struct_arg(arg). {
425 args = init_decl_args(arg);
426 }
427 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
428 args = add_decl_arg(args_, arg);
429 }
430 %type struct_arg {decl_arg*}
431 %destructor struct_arg {
432 free_decl_arg($$);
433 if ($$->type->strct) {
434 free_decl_struct($$->type->strct);
435 }
436 if ($$->type->enm) {
437 free_decl_enum($$->type->enm);
438 }
439 if ($$->type->func) {
440 free_decl($$->type->func);
441 }
442 }
443 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
444 arg_ = def;
445 if (def->type->strct) {
446 P->structs = add_decl_struct(P->structs, def->type->strct);
447 }
448 if (def->type->enm) {
449 P->enums = add_decl_enum(P->enums, def->type->enm);
450 }
451 }
452 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
453 arg_->layout = layout_;
454 arg = arg_;
455 }
456
457 %type struct_layout {decl_struct_layout*}
458 %destructor struct_layout {free_decl_struct_layout($$);}
459 struct_layout(layout) ::= . {
460 layout = NULL;
461 }
462 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
463 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
464 free(POS);
465 free(SIZ);
466 }
467
468 /* un/signed, urgh */
469 decl_scalar_type(type_) ::= CHAR(C). {
470 type_ = C;
471 }
472 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
473 if (s) {
474 type_ = PSI_TokenCat(2, S, s);
475 free(S);
476 free(s);
477 } else {
478 type_ = S;
479 }
480 }
481 decl_scalar_type_short(s) ::= . {
482 s = NULL;
483 }
484
485 decl_scalar_type_short(s) ::= INT(I). {
486 s = I;
487 }
488 decl_scalar_type(type_) ::= INT(I). {
489 type_ = I;
490 }
491 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
492 if (l) {
493 type_ = PSI_TokenCat(2, L, l);
494 free(L);
495 free(l);
496 } else {
497 type_ = L;
498 }
499 }
500 decl_scalar_type_long(l) ::= . {
501 l = NULL;
502 }
503 decl_scalar_type_long(l) ::= DOUBLE(D). {
504 l = D;
505 }
506 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
507 if (ll) {
508 l = PSI_TokenCat(2, L, ll);
509 free(L);
510 free(ll);
511 } else {
512 l = L;
513 }
514 }
515 decl_scalar_type_long_long(ll) ::= . {
516 ll = NULL;
517 }
518 decl_scalar_type_long_long(ll) ::= INT(I). {
519 ll = I;
520 }
521 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
522 PSI_Token *T = PSI_TokenCat(2, U, N);
523 type_ = init_decl_type(T->type, T->text);
524 type_->token = T;
525 free(U);
526 free(N);
527 }
528 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
529 PSI_Token *T = PSI_TokenCat(2, S, N);
530 type_ = init_decl_type(T->type, T->text);
531 type_->token = T;
532 free(S);
533 free(N);
534 }
535 decl_type(type_) ::= UNSIGNED(U). {
536 type_ = init_decl_type(PSI_T_NAME, U->text);
537 type_->token = U;
538 }
539 decl_type(type_) ::= SIGNED(S). {
540 type_ = init_decl_type(PSI_T_NAME, S->text);
541 type_->token = S;
542 }
543 decl_type(type_) ::= decl_scalar_type(N). {
544 type_ = init_decl_type(N->type, N->text);
545 type_->token = N;
546 }
547 /* structs ! */
548 decl_type(type_) ::= STRUCT(S) NAME(T). {
549 type_ = init_decl_type(S->type, T->text);
550 type_->token = T;
551 free(S);
552 }
553 decl_type(type_) ::= UNION(U) NAME(T). {
554 type_ = init_decl_type(U->type, T->text);
555 type_->token = T;
556 free(U);
557 }
558 decl_type(type_) ::= ENUM(E) NAME(T). {
559 type_ = init_decl_type(E->type, T->text);
560 type_->token = T;
561 free(E);
562 }
563 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
564 %type decl_type {decl_type*}
565 %destructor decl_type {free_decl_type($$);}
566 decl_type(type_) ::= decl_type_token(T). {
567 type_ = init_decl_type(T->type, T->text);
568 type_->token = T;
569 }
570
571
572 %type const_decl_type {decl_type*}
573 %destructor const_decl_type {free_decl_type($$);}
574 const_decl_type(type) ::= decl_type(type_). {
575 type = type_;
576 }
577 const_decl_type(type) ::= CONST decl_type(type_). {
578 type = type_;
579 }
580
581 %type impl {impl*}
582 %destructor impl {free_impl($$);}
583 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
584 impl = init_impl(func, stmts);
585 }
586
587 %type impl_func {impl_func*}
588 %destructor impl_func {free_impl_func($$);}
589 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
590 func = init_impl_func(NAME->text, args, type, r);
591 func->token = NAME;
592 }
593
594 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
595 %type impl_def_val {impl_def_val*}
596 %destructor impl_def_val {free_impl_def_val($$);}
597 impl_def_val(def) ::= impl_def_val_token(T). {
598 def = init_impl_def_val(T->type, T->text);
599 free(T);
600 }
601
602 %type impl_var {impl_var*}
603 %destructor impl_var {free_impl_var($$);}
604 impl_var(var) ::= reference(r) DOLLAR_NAME(T). {
605 var = init_impl_var(T->text, r);
606 var->token = T;
607 }
608
609 %type impl_arg {impl_arg*}
610 %destructor impl_arg {free_impl_arg($$);}
611 impl_arg(arg) ::= impl_type(type) impl_var(var). {
612 arg = init_impl_arg(type, var, NULL);
613 }
614 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
615 arg = init_impl_arg(type, var, def);
616 }
617
618 %type impl_args {impl_args*}
619 %destructor impl_args {free_impl_args($$);}
620 impl_args(args) ::= LPAREN RPAREN. {
621 args = NULL;
622 }
623 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
624 args = args_;
625 }
626 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
627 args = args_;
628 args->vararg.name = va;
629 }
630
631 %type impl_vararg {impl_arg*}
632 %destructor impl_vararg {free_impl_arg($$);}
633 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR_NAME(T). {
634 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
635 free(T);
636 }
637
638 %type impl_arg_list {impl_args*}
639 %destructor impl_arg_list {free_impl_args($$);}
640 impl_arg_list(args) ::= impl_arg(arg). {
641 args = init_impl_args(arg);
642 }
643 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
644 args = add_impl_arg(args_, arg);
645 }
646
647 %type impl_stmts {impl_stmts*}
648 %destructor impl_stmts {free_impl_stmts($$);}
649 impl_stmts(stmts) ::= impl_stmt(stmt). {
650 stmts = init_impl_stmts(stmt);
651 }
652 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
653 stmts = add_impl_stmt(stmts_, stmt);
654 }
655
656 %type impl_stmt {impl_stmt*}
657 %destructor impl_stmt {free_impl_stmt($$);}
658 impl_stmt(stmt) ::= let_stmt(let). {
659 stmt = init_impl_stmt(PSI_T_LET, let);
660 }
661 impl_stmt(stmt) ::= set_stmt(set). {
662 stmt = init_impl_stmt(PSI_T_SET, set);
663 }
664 impl_stmt(stmt) ::= return_stmt(ret). {
665 stmt = init_impl_stmt(PSI_T_RETURN, ret);
666 }
667 impl_stmt(stmt) ::= free_stmt(free). {
668 stmt = init_impl_stmt(PSI_T_FREE, free);
669 }
670
671 %token_class num_exp_token NUMBER NSNAME.
672 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
673 %type num_exp {num_exp*}
674 %destructor num_exp {free_num_exp($$);}
675 num_exp(exp) ::= num_exp_token(tok). {
676 exp = init_num_exp(tok->type, tok->text);
677 exp->token = tok;
678 }
679 num_exp(exp) ::= decl_var(var). {
680 exp = init_num_exp(PSI_T_NAME, var);
681 exp->token = PSI_TokenCopy(var->token);
682 }
683 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
684 exp_->operator = operator_->type;
685 exp_->operand = operand_;
686 exp = exp_;
687 free(operator_);
688 }
689
690 %type let_stmt {let_stmt*}
691 %destructor let_stmt {free_let_stmt($$);}
692 let_stmt(let) ::= LET decl_var(var) EOS. {
693 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
694 }
695 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
696 val->flags.one.is_reference = r ? 1 : 0;
697 let = init_let_stmt(var, val);
698 }
699 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
700 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
701 }
702
703 %type let_calloc {let_calloc*}
704 %destructor let_calloc {free_let_calloc($$);}
705 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
706 alloc = init_let_calloc(nmemb, size);
707 }
708 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
709 %type let_func {let_func*}
710 %destructor let_func {free_let_func($$);}
711 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
712 func = init_let_func(T->type, T->text, var);
713 free(T);
714 }
715
716 %type callback_arg_list {set_values *}
717 %destructor callback_arg_list {free_set_values($$);}
718 callback_arg_list ::= .
719 callback_arg_list(args) ::= COMMA callback_args(args_). {
720 args = args_;
721 }
722
723 %type callback_args {set_values *}
724 %destructor callback_args {free_set_values($$);}
725 callback_args(args) ::= set_value(val). {
726 args = init_set_values(val);
727 }
728 callback_args(args) ::= callback_args(args_) COMMA set_value(val). {
729 args = add_set_value(args_, val);
730 }
731
732 %type let_val {let_val*}
733 %destructor let_val {free_let_val($$);}
734 let_val(val) ::= NULL. {
735 val = init_let_val(PSI_LET_NULL, NULL);
736 }
737 let_val(val) ::= num_exp(exp). {
738 val = init_let_val(PSI_LET_NUMEXP, exp);
739 }
740 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
741 val = init_let_val(PSI_LET_CALLOC, alloc);
742 }
743 let_val(val) ::= let_func(func). {
744 val = init_let_val(PSI_LET_FUNC, func);
745 }
746 let_val(val) ::= CALLBACK let_func_token(F) LPAREN impl_var(var) callback_arg_list(args_) RPAREN. {
747 val = init_let_val(PSI_LET_CALLBACK, init_let_callback(
748 init_let_func(F->type, F->text, var), args_));
749 free(F);
750 }
751
752 %type set_stmt {set_stmt*}
753 %destructor set_stmt {free_set_stmt($$);}
754 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
755 set = init_set_stmt(var, val);
756 }
757
758 %type set_value {set_value*}
759 %destructor set_value {free_set_value($$);}
760 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
761 val = init_set_value(func, init_decl_vars(var));
762 }
763 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
764 val = init_set_value(func, init_decl_vars(var));
765 val->num = num_;
766 }
767 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
768 free_set_func(func_);
769 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
770 val->func->token = T;
771 }
772 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
773 val = vals;
774 val->func = func_;
775 val->vars = init_decl_vars(var);
776 }
777 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
778 val = vals;
779 val->func = func_;
780 val->num = num_;
781 val->vars = init_decl_vars(var);
782 }
783 %type set_vals {set_value*}
784 %destructor set_vals {free_set_value($$);}
785 set_vals(vals) ::= set_value(val). {
786 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
787 }
788 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
789 vals = add_inner_set_value(vals_, val);
790 }
791
792 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
793 %type set_func {set_func*}
794 %destructor set_func {free_set_func($$);}
795 set_func(func) ::= set_func_token(T). {
796 func = init_set_func(T->type, T->text);
797 func->token = T;
798 }
799
800 %type return_stmt {return_stmt*}
801 %destructor return_stmt {free_return_stmt($$);}
802 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
803 ret = init_return_stmt(val);
804 ret->token = T;
805 }
806
807 %type free_stmt {free_stmt*}
808 %destructor free_stmt {free_free_stmt($$);}
809 free_stmt(free) ::= FREE free_calls(calls) EOS. {
810 free = init_free_stmt(calls);
811 }
812
813 %type free_calls {free_calls*}
814 %destructor free_calls {free_free_calls($$);}
815 free_calls(calls) ::= free_call(call). {
816 calls = init_free_calls(call);
817 }
818 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
819 calls = add_free_call(calls_, call);
820 }
821
822 %type free_call {free_call*}
823 %destructor free_call {free_free_call($$);}
824 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
825 call = init_free_call(F->text, vars);
826 call->token = F;
827 }
828
829 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE.
830 %type impl_type {impl_type*}
831 %destructor impl_type {free_impl_type($$);}
832 impl_type(type_) ::= impl_type_token(T). {
833 type_ = init_impl_type(T->type, T->text);
834 free(T);
835 }
836
837 %type reference {char}
838 reference(r) ::= . {r = 0;}
839 reference(r) ::= AMPERSAND. {r = 1;}
840
841 %type indirection {unsigned}
842 indirection(i) ::= . {i = 0;}
843 indirection(i) ::= pointers(p). {i = p;}
844
845 %type pointers {unsigned}
846 pointers(p) ::= ASTERISK. {p = 1;}
847 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}