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