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