2 #include "php_psi_stdinc.h"
10 /* rename lemon symbols, works better than DEF(%name) */
11 #define ParseAlloc psi_parser_proc_init_ex
12 #define Parse psi_parser_proc_parse
13 #define ParseTrace psi_parser_proc_trace
14 #define ParseFree psi_parser_proc_free_ex
17 void *ParseAlloc(void *(*mallocProc)(size_t));
18 void ParseFree(void *p, void (*freeProc)(void*));
21 void *psi_parser_proc_init(void)
23 return ParseAlloc(malloc);
26 void psi_parser_proc_free(void **parser_proc)
29 ParseFree(*parser_proc, free);
36 %token_type {struct psi_token *}
37 %token_destructor {free($$);}
38 %default_destructor {(void)P;}
39 %extra_argument {struct psi_parser *P}
40 %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->file.fn, P->line, "PSI syntax error: Unexpected end of input"); } }
41 %token_class const_type_token BOOL INT FLOAT STRING .
42 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME .
43 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING .
44 %token_class number_token NUMBER NSNAME .
45 %token_class num_exp_binary_op_token PIPE CARET AMPERSAND LSHIFT RSHIFT PLUS MINUS ASTERISK SLASH MODULO .
46 %token_class num_exp_unary_op_token TILDE NOT PLUS MINUS .
47 %token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL COUNT .
48 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID .
49 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE .
57 %left ASTERISK SLASH MODULO.
58 %fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING COUNT.
59 %type decl_enum {struct psi_decl_enum *}
60 %destructor decl_enum {psi_decl_enum_free(&$$);}
61 %type decl_enum_items {struct psi_plist*}
62 %destructor decl_enum_items {psi_plist_free($$);}
63 %type decl_enum_item {struct psi_decl_enum_item*}
64 %destructor decl_enum_item {psi_decl_enum_item_free(&$$);}
65 %type decl_struct_args_block {struct psi_plist*}
66 %destructor decl_struct_args_block {psi_plist_free($$);}
67 %type decl_struct_args {struct psi_plist*}
68 %destructor decl_struct_args {psi_plist_free($$);}
69 %type decl_struct {struct psi_decl_struct*}
70 %destructor decl_struct {psi_decl_struct_free(&$$);}
71 %type align_and_size {struct psi_layout}
72 %type decl_union {struct psi_decl_union*}
73 %destructor decl_union {psi_decl_union_free(&$$);}
74 %type const_type {struct psi_const_type*}
75 %destructor const_type {psi_const_type_free(&$$);}
76 %type constant {struct psi_const*}
77 %destructor constant {psi_const_free(&$$);}
78 %type decl_typedef {struct psi_decl_arg*}
79 %destructor decl_typedef {psi_decl_arg_free(&$$);}
80 %type decl_typedef_body_ex {struct psi_decl_arg*}
81 %destructor decl_typedef_body_ex {psi_decl_arg_free(&$$);}
82 %type decl_typedef_body {struct psi_decl_arg*}
83 %destructor decl_typedef_body {psi_decl_arg_free(&$$);}
84 %type decl_typedef_body_fn_args {struct psi_plist*}
85 %destructor decl_typedef_body_fn_args {psi_plist_free($$);}
86 %type decl {struct psi_decl*}
87 %destructor decl {psi_decl_free(&$$);}
88 %type decl_func {struct psi_decl_arg*}
89 %destructor decl_func {psi_decl_arg_free(&$$);}
90 %type decl_abi {struct psi_decl_abi*}
91 %destructor decl_abi {psi_decl_abi_free(&$$);}
92 %type decl_var {struct psi_decl_var*}
93 %destructor decl_var {psi_decl_var_free(&$$);}
94 %type decl_vars {struct psi_plist*}
95 %destructor decl_vars {psi_plist_free($$);}
96 %type decl_arg {struct psi_decl_arg*}
97 %destructor decl_arg {psi_decl_arg_free(&$$);}
98 %type decl_args {struct psi_plist*}
99 %destructor decl_args {psi_plist_free($$);}
100 %type struct_args {struct psi_plist*}
101 %destructor struct_args {psi_plist_free($$);}
102 %type struct_arg {struct psi_decl_arg*}
103 %destructor struct_arg {psi_decl_arg_free(&$$);}
104 %type decl_layout {struct psi_layout*}
105 %destructor decl_layout {psi_layout_free(&$$);}
106 %type decl_type {struct psi_decl_type*}
107 %destructor decl_type {psi_decl_type_free(&$$);}
108 %type const_decl_type {struct psi_decl_type*}
109 %destructor const_decl_type {psi_decl_type_free(&$$);}
110 %type impl {struct psi_impl*}
111 %destructor impl {psi_impl_free(&$$);}
112 %type impl_func {struct psi_impl_func*}
113 %destructor impl_func {psi_impl_func_free(&$$);}
114 %type impl_def_val {struct psi_impl_def_val*}
115 %destructor impl_def_val {psi_impl_def_val_free(&$$);}
116 %type impl_var {struct psi_impl_var*}
117 %destructor impl_var {psi_impl_var_free(&$$);}
118 %type impl_arg {struct psi_impl_arg*}
119 %destructor impl_arg {psi_impl_arg_free(&$$);}
120 %type impl_args {struct psi_plist*}
121 %destructor impl_args {psi_plist_free($$);}
122 %type impl_vararg {struct psi_impl_arg*}
123 %destructor impl_vararg {psi_impl_arg_free(&$$);}
124 %type impl_stmts {struct psi_plist*}
125 %destructor impl_stmts {psi_plist_free($$);}
126 %type impl_stmt {struct psi_token**}
127 %destructor impl_stmt {psi_impl_stmt_free(&$$);}
128 %type number {struct psi_number*}
129 %destructor number {psi_number_free(&$$);}
130 %type num_exp {struct psi_num_exp*}
131 %destructor num_exp {psi_num_exp_free(&$$);}
132 %type let_stmt {struct psi_let_stmt*}
133 %destructor let_stmt {psi_let_stmt_free(&$$);}
134 %type let_calloc {struct psi_let_calloc*}
135 %destructor let_calloc {psi_let_calloc_free(&$$);}
136 %type let_func {struct psi_let_func*}
137 %destructor let_func {psi_let_func_free(&$$);}
138 %type callback_arg_list {struct psi_plist *}
139 %destructor callback_arg_list {psi_plist_free($$);}
140 %type callback_args {struct psi_plist *}
141 %destructor callback_args {psi_plist_free($$);}
142 %type let_callback {struct psi_let_callback*}
143 %destructor let_callback {psi_let_callback_free(&$$);}
144 %type let_exp {struct psi_let_exp*}
145 %destructor let_exp {psi_let_exp_free(&$$);}
146 %type let_exps {struct psi_plist*}
147 %destructor let_exps {psi_plist_free($$);}
148 %type set_stmt {struct psi_set_stmt*}
149 %destructor set_stmt {psi_set_stmt_free(&$$);}
150 %type set_exp {struct psi_set_exp*}
151 %destructor set_exp {psi_set_exp_free(&$$);}
152 %type set_exps {struct psi_plist*}
153 %destructor set_exps {psi_plist_free($$);}
154 %type set_func {struct psi_set_func*}
155 %destructor set_func {psi_set_func_free(&$$);}
156 %type return_stmt {struct psi_return_stmt*}
157 %destructor return_stmt {psi_return_stmt_free(&$$);}
158 %type free_stmt {struct psi_free_stmt*}
159 %destructor free_stmt {psi_free_stmt_free(&$$);}
160 %type free_exps {struct psi_plist*}
161 %destructor free_exps {psi_plist_free($$);}
162 %type free_exp {struct psi_free_exp*}
163 %destructor free_exp {psi_free_exp_free(&$$);}
164 %type impl_type {struct psi_impl_type*}
165 %destructor impl_type {psi_impl_type_free(&$$);}
166 %type reference {bool}
167 %type indirection {unsigned}
168 %type pointers {unsigned}
171 blocks ::= blocks block.
174 block ::= LIB(token) QUOTED_STRING(libname) EOS. {
176 P->error(PSI_DATA(P), token, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
178 P->file.ln = strndup(libname->text + 1, libname->size - 2);
183 block ::= decl(decl). {
185 P->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
187 P->decls = psi_plist_add(P->decls, &decl);
189 block ::= impl(impl). {
191 P->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
193 P->impls = psi_plist_add(P->impls, &impl);
195 block ::= decl_typedef(def). {
197 P->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
199 P->types = psi_plist_add(P->types, &def);
200 switch (def->type->type) {
202 if (def->type->real.strct) {
204 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
206 P->structs = psi_plist_add(P->structs, &def->type->real.strct);
210 if (def->type->real.unn) {
212 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
214 P->unions = psi_plist_add(P->unions, &def->type->real.unn);
218 if (def->type->real.enm) {
220 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
222 P->enums = psi_plist_add(P->enums, &def->type->real.enm);
227 block ::= constant(constant). {
229 P->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
231 P->consts = psi_plist_add(P->consts, &constant);
233 block ::= decl_struct(strct). {
235 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
237 P->structs = psi_plist_add(P->structs, &strct);
239 block ::= decl_union(u). {
241 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
243 P->unions = psi_plist_add(P->unions, &u);
245 block ::= decl_enum(e). {
247 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
249 P->enums = psi_plist_add(P->enums, &e);
251 optional_name(n) ::= . {
254 optional_name(n) ::= NAME(N). {
257 align_and_size(as) ::= . {
261 align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
262 as.pos = atol(A->text);
263 as.len = atol(S->text);
267 enum_name(n) ::= ENUM(E) optional_name(N). {
273 psi_token_hash(E, digest);
274 n = psi_token_translit(psi_token_append(E, 1, digest), " ", "@");
277 struct_name(n) ::= STRUCT(S) optional_name(N). {
283 psi_token_hash(S, digest);
284 n = psi_token_translit(psi_token_append(S, 1, digest), " ", "@");
287 union_name(n) ::= UNION(U) optional_name(N). {
293 psi_token_hash(U, digest);
294 n = psi_token_translit(psi_token_append(U, 1, digest), " ", "@");
297 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
298 e = psi_decl_enum_init(N->text, list);
301 decl_enum_items(l) ::= decl_enum_item(i). {
302 l = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_enum_item_free),
305 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
306 l = psi_plist_add(l_, &i);
308 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
309 i = psi_decl_enum_item_init(N->text, num);
312 decl_enum_item(i) ::= NAME(N). {
313 i = psi_decl_enum_item_init(N->text, NULL);
316 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
319 decl_struct_args(args_) ::= decl_struct_args_block(args). {
322 decl_struct_args(args_) ::= EOS. {
323 args_ = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
325 decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
326 strct = psi_decl_struct_init(N->text, args);
327 strct->align = as.pos;
328 strct->size = as.len;
331 decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
332 u = psi_decl_union_init(N->text, args);
337 const_type(type_) ::= const_type_token(T). {
338 type_ = psi_const_type_init(T->type, T->text);
341 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
342 constant = psi_const_init(type, T->text, val);
345 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
349 decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
350 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_STRUCT, N->text), var);
351 def->type->token = psi_token_copy(N);
352 def->type->real.strct = psi_decl_struct_init(N->text, args);
353 def->type->real.strct->token = N;
354 def->type->real.strct->align = as.pos;
355 def->type->real.strct->size = as.len;
357 decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
358 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_UNION, N->text), var);
359 def->type->token = psi_token_copy(N);
360 def->type->real.unn = psi_decl_union_init(N->text, args);
361 def->type->real.unn->token = N;
362 def->type->real.unn->align = as.pos;
363 def->type->real.unn->size = as.len;
365 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
366 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_ENUM, e->name), psi_decl_var_init(ALIAS->text, 0, 0));
367 def->var->token = ALIAS;
368 def->type->token = psi_token_copy(e->token);
369 def->type->real.enm = e;
371 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
374 decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. {
377 decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). {
378 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_FUNCTION, func_->var->name), psi_decl_var_copy(func_->var));
379 def->type->token = psi_token_copy(func_->token);
380 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
382 decl_typedef_body(def) ::= decl_arg(arg). {
385 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
386 decl = psi_decl_init(abi, func, args);
388 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) COMMA ELLIPSIS RPAREN EOS. {
389 decl = psi_decl_init(abi, func, args);
392 decl_func(func) ::= decl_arg(arg). {
395 decl_func(func) ::= VOID(T) NAME(N). {
396 func = psi_decl_arg_init(
397 psi_decl_type_init(T->type, T->text),
398 psi_decl_var_init(N->text, 0, 0)
400 func->type->token = T;
401 func->var->token = N;
404 decl_typedef_body(def) ::= VOID(T) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
405 struct psi_decl_arg *func_ = psi_decl_arg_init(
406 psi_decl_type_init(T->type, T->text),
407 psi_decl_var_init(N->text, decl_i, 0)
409 func_->type->token = T;
410 func_->var->token = N;
412 def = psi_decl_arg_init(
413 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
414 psi_decl_var_copy(func_->var)
416 def->var->pointer_level = type_i;
417 def->type->token = psi_token_copy(func_->token);
418 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
420 decl_typedef_body(def) ::= CONST VOID(T) pointers(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
421 struct psi_decl_arg *func_ = psi_decl_arg_init(
422 psi_decl_type_init(T->type, T->text),
423 psi_decl_var_init(N->text, decl_i, 0)
425 func_->type->token = T;
426 func_->var->token = N;
428 def = psi_decl_arg_init(
429 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
430 psi_decl_var_copy(func_->var)
432 def->var->pointer_level = type_i;
433 def->type->token = psi_token_copy(func_->token);
434 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
436 decl_abi(abi) ::= NAME(T). {
437 abi = psi_decl_abi_init(T->text);
440 decl_var_array_size(as) ::= . {
443 decl_var_array_size(as) ::= LBRACKET NUMBER(D) RBRACKET. {
446 decl_var(var) ::= NAME(T) decl_var_array_size(as). {
447 var = psi_decl_var_init(T->text, 0, as?atol(as->text):0);
453 decl_var(var) ::= pointers(p) NAME(T) decl_var_array_size(as). {
454 var = psi_decl_var_init(T->text, p+!!as, as?atol(as->text):0);
460 decl_vars(vars) ::= decl_var(var). {
461 vars = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_var_free),
464 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
465 vars = psi_plist_add(vars_, &var);
467 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
468 arg_ = psi_decl_arg_init(type, var);
470 decl_typedef_body(def) ::= const_decl_type(type_) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
471 struct psi_decl_arg *func_ = psi_decl_arg_init(
473 psi_decl_var_init(N->text, decl_i, 0)
475 func_->var->token = N;
477 def = psi_decl_arg_init(
478 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
479 psi_decl_var_copy(func_->var)
481 def->var->pointer_level = type_i;
482 def->type->token = psi_token_copy(func_->token);
483 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
485 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
486 arg_ = psi_decl_arg_init(
487 psi_decl_type_init(T->type, T->text),
488 psi_decl_var_init(N->text, p, 0)
490 arg_->type->token = T;
491 arg_->var->token = N;
494 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
495 arg_ = psi_decl_arg_init(
496 psi_decl_type_init(T->type, T->text),
497 psi_decl_var_init(N->text, p, 0)
499 arg_->type->token = T;
500 arg_->var->token = N;
503 decl_args(args) ::= . {
506 decl_args(args) ::= VOID. {
509 decl_args(args) ::= decl_arg(arg). {
510 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free),
513 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
514 args = psi_plist_add(args_, &arg);
516 struct_args(args) ::= struct_arg(arg). {
517 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free),
520 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
521 args = psi_plist_add(args_, &arg);
523 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
525 switch (def->type->type) {
527 if (def->type->real.strct) {
529 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
531 P->structs = psi_plist_add(P->structs, &def->type->real.strct);
535 if (def->type->real.unn) {
537 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
539 P->unions = psi_plist_add(P->unions, &def->type->real.unn);
543 if (def->type->real.enm) {
545 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
547 P->enums = psi_plist_add(P->enums, &def->type->real.enm);
552 struct_arg(arg) ::= decl_arg(arg_) decl_layout(layout_) EOS. {
553 arg_->layout = layout_;
556 decl_layout(layout) ::= . {
559 decl_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
560 layout = psi_layout_init(atol(POS->text), atol(SIZ->text));
564 decl_scalar_type(type_) ::= CHAR(C). {
567 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
569 type_ = psi_token_cat(2, S, s);
576 decl_scalar_type_short(s) ::= . {
579 decl_scalar_type_short(s) ::= INT(I). {
582 decl_scalar_type(type_) ::= INT(I). {
585 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
587 type_ = psi_token_cat(2, L, l);
594 decl_scalar_type_long(l) ::= . {
597 decl_scalar_type_long(l) ::= DOUBLE(D). {
600 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
602 l = psi_token_cat(2, L, ll);
609 decl_scalar_type_long_long(ll) ::= . {
612 decl_scalar_type_long_long(ll) ::= INT(I). {
615 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
616 struct psi_token *T = psi_token_cat(2, U, N);
617 type_ = psi_decl_type_init(T->type, T->text);
622 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
623 struct psi_token *T = psi_token_cat(2, S, N);
624 type_ = psi_decl_type_init(T->type, T->text);
629 decl_type(type_) ::= UNSIGNED(U). {
630 type_ = psi_decl_type_init(PSI_T_NAME, U->text);
633 decl_type(type_) ::= SIGNED(S). {
634 type_ = psi_decl_type_init(PSI_T_NAME, S->text);
637 decl_type(type_) ::= decl_scalar_type(N). {
638 type_ = psi_decl_type_init(N->type, N->text);
641 decl_type(type_) ::= STRUCT(S) NAME(T). {
642 type_ = psi_decl_type_init(S->type, T->text);
646 decl_type(type_) ::= UNION(U) NAME(T). {
647 type_ = psi_decl_type_init(U->type, T->text);
651 decl_type(type_) ::= ENUM(E) NAME(T). {
652 type_ = psi_decl_type_init(E->type, T->text);
656 decl_type(type_) ::= decl_type_token(T). {
657 type_ = psi_decl_type_init(T->type, T->text);
660 const_decl_type(type) ::= decl_type(type_). {
663 const_decl_type(type) ::= CONST decl_type(type_). {
666 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
667 impl = psi_impl_init(func, stmts);
669 impl(impl) ::= STATIC impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
670 func->static_memory = 1;
671 impl = psi_impl_init(func, stmts);
673 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) LPAREN RPAREN COLON impl_type(type). {
674 func = psi_impl_func_init(NAME->text, NULL, type);
676 func->return_reference = r;
678 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) LPAREN impl_args(args) RPAREN COLON impl_type(type). {
679 func = psi_impl_func_init(NAME->text, args, type);
681 func->return_reference = r;
683 impl_func(func) ::= FUNCTION reference(func_r) NSNAME(NAME) LPAREN impl_args(args) COMMA impl_type(va_type) reference(va_r) ELLIPSIS DOLLAR_NAME(T) RPAREN COLON impl_type(func_type). {
684 func = psi_impl_func_init(NAME->text, args, func_type);
686 func->return_reference = func_r;
687 func->vararg = psi_impl_arg_init(va_type, psi_impl_var_init(T->text, va_r), NULL);
690 impl_def_val(def) ::= impl_def_val_token(T). {
691 def = psi_impl_def_val_init(T->type, T->text);
694 impl_var(var) ::= reference(r) DOLLAR_NAME(T). {
695 var = psi_impl_var_init(T->text, r);
698 impl_type(type_) ::= impl_type_token(T). {
699 type_ = psi_impl_type_init(T->type, T->text);
702 impl_arg(arg) ::= impl_type(type) impl_var(var). {
703 arg = psi_impl_arg_init(type, var, NULL);
705 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
706 arg = psi_impl_arg_init(type, var, def);
708 impl_args(args) ::= impl_arg(arg). {
709 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_arg_free),
712 impl_args(args) ::= impl_args(args_) COMMA impl_arg(arg). {
713 args = psi_plist_add(args_, &arg);
715 impl_stmts(stmts) ::= impl_stmt(stmt). {
716 stmts = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_stmt_free),
719 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
720 stmts = psi_plist_add(stmts_, &stmt);
722 impl_stmt(i) ::= return_stmt(r). {
723 i = (struct psi_token**) r;
725 impl_stmt(i) ::= let_stmt(l). {
726 i = (struct psi_token**) l;
728 impl_stmt(i) ::= set_stmt(s). {
729 i = (struct psi_token**) s;
731 impl_stmt(i) ::= free_stmt(f). {
732 i = (struct psi_token**) f;
734 number(exp) ::= number_token(tok). {
735 exp = psi_number_init(tok->type, tok->text);
738 number(exp) ::= decl_var(var). {
739 exp = psi_number_init(PSI_T_NAME, var);
740 exp->token = psi_token_copy(var->token);
742 num_exp(exp) ::= number(num). {
743 exp = psi_num_exp_init_num(num);
744 exp->token = psi_token_copy(num->token);
746 num_exp(exp) ::= LPAREN(L) num_exp(exp_) RPAREN. {
747 exp = psi_num_exp_init_unary(PSI_T_LPAREN, exp_);
750 num_exp(exp) ::= num_exp(lhs_) num_exp_binary_op_token(OP) num_exp(rhs_). {
751 exp = psi_num_exp_init_binary(OP->type, lhs_, rhs_);
754 num_exp(exp) ::= num_exp_unary_op_token(OP) num_exp(exp_). {
755 exp = psi_num_exp_init_unary(OP->type, exp_);
758 let_exp(val) ::= NULL. {
759 val = psi_let_exp_init(PSI_LET_NULL, NULL);
761 let_exp(val) ::= AMPERSAND NULL. {
762 val = psi_let_exp_init(PSI_LET_NULL, NULL);
763 val->is_reference = 1;
765 let_exp(val) ::= let_callback(cb). {
766 val = psi_let_exp_init(PSI_LET_CALLBACK, cb);
768 let_exp(val) ::= let_calloc(ca). {
769 val = psi_let_exp_init(PSI_LET_CALLOC, ca);
771 let_exp(val) ::= AMPERSAND let_calloc(ca). {
772 val = psi_let_exp_init(PSI_LET_CALLOC, ca);
773 val->is_reference = 1;
775 let_exp(val) ::= let_func(fn). {
776 val = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, fn);
778 let_exp(val) ::= AMPERSAND let_func(fn). {
779 val = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, fn);
780 val->is_reference = 1;
782 let_exp(val) ::= num_exp(exp). {
783 val = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, exp);
785 let_exp(val) ::= AMPERSAND num_exp(exp). {
786 val = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, exp);
787 val->is_reference = 1;
789 let_exp(exp) ::= decl_var(var_) EQUALS let_exp(val). {
793 let_stmt(let) ::= LET(T) let_exp(val) EOS. {
794 let = psi_let_stmt_init(val);
797 let_stmt(let) ::= TEMP(T) decl_var(var) EQUALS reference(r) decl_var(val_) EOS. {
798 let = psi_let_stmt_init(psi_let_exp_init_ex(var, PSI_LET_TMP, val_));
800 let->exp->is_reference = r ? 1 : 0;
802 let_callback(cb) ::= CALLBACK(T) callback_rval(F) LPAREN impl_var(var) LPAREN callback_arg_list(args_) RPAREN RPAREN. {
803 cb = psi_let_callback_init(psi_let_func_init(F->type, F->text, var), args_);
807 let_calloc(alloc) ::= CALLOC(T) LPAREN num_exp(nmemb) COMMA num_exp(size) RPAREN. {
808 alloc = psi_let_calloc_init(nmemb, size);
811 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
812 func = psi_let_func_init(T->type, T->text, var);
815 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) COMMA let_exps(vals) RPAREN. {
816 func = psi_let_func_init(T->type, T->text, var);
820 let_exps(exps) ::= let_exp(exp). {
821 exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_let_exp_free),
824 let_exps(exps) ::= let_exps(exps_) COMMA let_exp(exp). {
825 exps = psi_plist_add(exps_, &exp);
827 callback_arg_list(args) ::= . {
830 callback_arg_list(args) ::= callback_args(args_). {
833 callback_args(args) ::= set_exp(val). {
834 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free),
837 callback_args(args) ::= callback_args(args_) COMMA set_exp(val). {
838 args = psi_plist_add(args_, &val);
840 callback_rval(rval) ::= let_func_token(F). {
843 callback_rval(rval) ::= VOID(V). {
846 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) RPAREN. {
847 func = psi_set_func_init(T->type, T->text, var);
850 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) COMMA set_exps(vals) RPAREN. {
851 func = psi_set_func_init(T->type, T->text, var);
855 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) COMMA ELLIPSIS RPAREN. {
856 func = psi_set_func_init(T->type, T->text, var);
860 set_exp(val) ::= set_func(fn). {
861 val = psi_set_exp_init(PSI_SET_FUNC, fn);
863 set_exp(val) ::= num_exp(num). {
864 val = psi_set_exp_init(PSI_SET_NUMEXP, num);
866 set_exps(exps) ::= set_exp(exp). {
867 exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free),
870 set_exps(exps) ::= set_exps(exps_) COMMA set_exp(exp). {
871 exps = psi_plist_add(exps_, &exp);
873 set_exp(exp) ::= impl_var(var_) EQUALS set_exp(val). {
877 set_stmt(set) ::= SET(T) set_exp(exp) EOS. {
878 set = psi_set_stmt_init(exp);
881 return_stmt(ret) ::= RETURN(T) set_func(func) EOS. {
882 ret = psi_return_stmt_init(psi_set_exp_init(PSI_SET_FUNC, func));
885 free_stmt(free) ::= FREE(T) free_exps(calls) EOS. {
886 free = psi_free_stmt_init(calls);
889 free_exps(calls) ::= free_exp(call). {
890 calls = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_free_exp_free),
893 free_exps(calls) ::= free_exps(calls_) COMMA free_exp(call). {
894 calls = psi_plist_add(calls_, &call);
896 free_exp(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
897 call = psi_free_exp_init(F->text, vars);
903 reference(r) ::= AMPERSAND. {
906 indirection(i) ::= .{
909 indirection(i) ::= pointers(p). {
912 pointers(p) ::= ASTERISK. {
915 pointers(p) ::= pointers(p_) ASTERISK. {