calc: boolean expressions
[m6w6/ext-psi] / src / parser_proc.y
1 %include {
2 #include "php_psi_stdinc.h"
3
4 #include <assert.h>
5 #include <stdarg.h>
6
7 #include "plist.h"
8 #include "parser.h"
9
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
15
16 /* fwd decls */
17 void *ParseAlloc(void *(*mallocProc)(size_t));
18 void ParseFree(void *p, void (*freeProc)(void*));
19
20 /* wrappers */
21 void *psi_parser_proc_init(void)
22 {
23 return ParseAlloc(malloc);
24 }
25
26 void psi_parser_proc_free(void **parser_proc)
27 {
28 if (*parser_proc) {
29 ParseFree(*parser_proc, free);
30 *parser_proc = NULL;
31 }
32 }
33
34 }
35 %token_prefix PSI_T_
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 binary_op_token PIPE CARET AMPERSAND LSHIFT RSHIFT PLUS MINUS ASTERISK SLASH MODULO RCHEVR LCHEVR CMP_GE CMP_LE OR AND CMP_EQ CMP_NE .
46 %token_class 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 .
50 %nonassoc NAME.
51 %right NOT TILDE.
52 %left AND OR.
53 %left PIPE.
54 %left CARET.
55 %left AMPERSAND.
56 %left CMP_EQ CMP_NE.
57 %left LCHEVR CMP_LE RCHEVR CMP_GE.
58 %left LSHIFT RSHIFT.
59 %left PLUS MINUS.
60 %left ASTERISK SLASH MODULO.
61 %fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING COUNT.
62 %type decl_enum {struct psi_decl_enum *}
63 %destructor decl_enum {psi_decl_enum_free(&$$);}
64 %type decl_enum_items {struct psi_plist*}
65 %destructor decl_enum_items {psi_plist_free($$);}
66 %type decl_enum_item {struct psi_decl_enum_item*}
67 %destructor decl_enum_item {psi_decl_enum_item_free(&$$);}
68 %type decl_struct_args_block {struct psi_plist*}
69 %destructor decl_struct_args_block {psi_plist_free($$);}
70 %type decl_struct_args {struct psi_plist*}
71 %destructor decl_struct_args {psi_plist_free($$);}
72 %type decl_struct {struct psi_decl_struct*}
73 %destructor decl_struct {psi_decl_struct_free(&$$);}
74 %type align_and_size {struct psi_layout}
75 %type decl_union {struct psi_decl_union*}
76 %destructor decl_union {psi_decl_union_free(&$$);}
77 %type const_type {struct psi_const_type*}
78 %destructor const_type {psi_const_type_free(&$$);}
79 %type constant {struct psi_const*}
80 %destructor constant {psi_const_free(&$$);}
81 %type decl_typedef {struct psi_decl_arg*}
82 %destructor decl_typedef {psi_decl_arg_free(&$$);}
83 %type decl_typedef_body_ex {struct psi_decl_arg*}
84 %destructor decl_typedef_body_ex {psi_decl_arg_free(&$$);}
85 %type decl_typedef_body {struct psi_decl_arg*}
86 %destructor decl_typedef_body {psi_decl_arg_free(&$$);}
87 %type decl_typedef_body_fn_args {struct psi_plist*}
88 %destructor decl_typedef_body_fn_args {psi_plist_free($$);}
89 %type decl {struct psi_decl*}
90 %destructor decl {psi_decl_free(&$$);}
91 %type decl_func {struct psi_decl_arg*}
92 %destructor decl_func {psi_decl_arg_free(&$$);}
93 %type decl_abi {struct psi_decl_abi*}
94 %destructor decl_abi {psi_decl_abi_free(&$$);}
95 %type decl_var {struct psi_decl_var*}
96 %destructor decl_var {psi_decl_var_free(&$$);}
97 %type decl_vars {struct psi_plist*}
98 %destructor decl_vars {psi_plist_free($$);}
99 %type decl_arg {struct psi_decl_arg*}
100 %destructor decl_arg {psi_decl_arg_free(&$$);}
101 %type decl_args {struct psi_plist*}
102 %destructor decl_args {psi_plist_free($$);}
103 %type struct_args {struct psi_plist*}
104 %destructor struct_args {psi_plist_free($$);}
105 %type struct_arg {struct psi_decl_arg*}
106 %destructor struct_arg {psi_decl_arg_free(&$$);}
107 %type decl_layout {struct psi_layout*}
108 %destructor decl_layout {psi_layout_free(&$$);}
109 %type decl_type {struct psi_decl_type*}
110 %destructor decl_type {psi_decl_type_free(&$$);}
111 %type const_decl_type {struct psi_decl_type*}
112 %destructor const_decl_type {psi_decl_type_free(&$$);}
113 %type impl {struct psi_impl*}
114 %destructor impl {psi_impl_free(&$$);}
115 %type impl_func {struct psi_impl_func*}
116 %destructor impl_func {psi_impl_func_free(&$$);}
117 %type impl_def_val {struct psi_impl_def_val*}
118 %destructor impl_def_val {psi_impl_def_val_free(&$$);}
119 %type impl_var {struct psi_impl_var*}
120 %destructor impl_var {psi_impl_var_free(&$$);}
121 %type impl_arg {struct psi_impl_arg*}
122 %destructor impl_arg {psi_impl_arg_free(&$$);}
123 %type impl_args {struct psi_plist*}
124 %destructor impl_args {psi_plist_free($$);}
125 %type impl_vararg {struct psi_impl_arg*}
126 %destructor impl_vararg {psi_impl_arg_free(&$$);}
127 %type impl_stmts {struct psi_plist*}
128 %destructor impl_stmts {psi_plist_free($$);}
129 %type impl_stmt {struct psi_token**}
130 %destructor impl_stmt {psi_impl_stmt_free(&$$);}
131 %type number {struct psi_number*}
132 %destructor number {psi_number_free(&$$);}
133 %type num_exp {struct psi_num_exp*}
134 %destructor num_exp {psi_num_exp_free(&$$);}
135 %type let_stmt {struct psi_let_stmt*}
136 %destructor let_stmt {psi_let_stmt_free(&$$);}
137 %type let_calloc {struct psi_let_calloc*}
138 %destructor let_calloc {psi_let_calloc_free(&$$);}
139 %type let_func {struct psi_let_func*}
140 %destructor let_func {psi_let_func_free(&$$);}
141 %type callback_arg_list {struct psi_plist *}
142 %destructor callback_arg_list {psi_plist_free($$);}
143 %type callback_args {struct psi_plist *}
144 %destructor callback_args {psi_plist_free($$);}
145 %type let_callback {struct psi_let_callback*}
146 %destructor let_callback {psi_let_callback_free(&$$);}
147 %type let_exp {struct psi_let_exp*}
148 %destructor let_exp {psi_let_exp_free(&$$);}
149 %type let_exps {struct psi_plist*}
150 %destructor let_exps {psi_plist_free($$);}
151 %type set_stmt {struct psi_set_stmt*}
152 %destructor set_stmt {psi_set_stmt_free(&$$);}
153 %type set_exp {struct psi_set_exp*}
154 %destructor set_exp {psi_set_exp_free(&$$);}
155 %type set_exps {struct psi_plist*}
156 %destructor set_exps {psi_plist_free($$);}
157 %type set_func {struct psi_set_func*}
158 %destructor set_func {psi_set_func_free(&$$);}
159 %type return_stmt {struct psi_return_stmt*}
160 %destructor return_stmt {psi_return_stmt_free(&$$);}
161 %type free_stmt {struct psi_free_stmt*}
162 %destructor free_stmt {psi_free_stmt_free(&$$);}
163 %type free_exps {struct psi_plist*}
164 %destructor free_exps {psi_plist_free($$);}
165 %type free_exp {struct psi_free_exp*}
166 %destructor free_exp {psi_free_exp_free(&$$);}
167 %type impl_type {struct psi_impl_type*}
168 %destructor impl_type {psi_impl_type_free(&$$);}
169 %type reference {bool}
170 %type indirection {unsigned}
171 %type pointers {unsigned}
172 file ::= blocks.
173 blocks ::= block.
174 blocks ::= blocks block.
175 block ::= EOF.
176 block ::= EOS.
177 block ::= LIB(token) QUOTED_STRING(libname) EOS. {
178 if (P->file.ln) {
179 P->error(PSI_DATA(P), token, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
180 } else {
181 P->file.ln = strndup(libname->text + 1, libname->size - 2);
182 }
183 free(libname);
184 free(token);
185 }
186 block ::= decl(decl). {
187 if (!P->decls) {
188 P->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
189 }
190 P->decls = psi_plist_add(P->decls, &decl);
191 }
192 block ::= impl(impl). {
193 if (!P->impls) {
194 P->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
195 }
196 P->impls = psi_plist_add(P->impls, &impl);
197 }
198 block ::= decl_typedef(def). {
199 if (!P->types) {
200 P->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
201 }
202 P->types = psi_plist_add(P->types, &def);
203 switch (def->type->type) {
204 case PSI_T_STRUCT:
205 if (def->type->real.strct) {
206 if (!P->structs) {
207 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
208 }
209 P->structs = psi_plist_add(P->structs, &def->type->real.strct);
210 }
211 break;
212 case PSI_T_UNION:
213 if (def->type->real.unn) {
214 if (!P->unions) {
215 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
216 }
217 P->unions = psi_plist_add(P->unions, &def->type->real.unn);
218 }
219 break;
220 case PSI_T_ENUM:
221 if (def->type->real.enm) {
222 if (!P->enums) {
223 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
224 }
225 P->enums = psi_plist_add(P->enums, &def->type->real.enm);
226 }
227 break;
228 }
229 }
230 block ::= constant(constant). {
231 if (!P->consts) {
232 P->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
233 }
234 P->consts = psi_plist_add(P->consts, &constant);
235 }
236 block ::= decl_struct(strct). {
237 if (!P->structs) {
238 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
239 }
240 P->structs = psi_plist_add(P->structs, &strct);
241 }
242 block ::= decl_union(u). {
243 if (!P->unions) {
244 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
245 }
246 P->unions = psi_plist_add(P->unions, &u);
247 }
248 block ::= decl_enum(e). {
249 if (!P->enums) {
250 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
251 }
252 P->enums = psi_plist_add(P->enums, &e);
253 }
254 optional_name(n) ::= . {
255 n = NULL;
256 }
257 optional_name(n) ::= NAME(N). {
258 n = N;
259 }
260 align_and_size(as) ::= . {
261 as.pos = 0;
262 as.len = 0;
263 }
264 align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
265 as.pos = atol(A->text);
266 as.len = atol(S->text);
267 free(A);
268 free(S);
269 }
270 enum_name(n) ::= ENUM(E) optional_name(N). {
271 if (N) {
272 n = N;
273 free(E);
274 } else {
275 char digest[17];
276 psi_token_hash(E, digest);
277 n = psi_token_translit(psi_token_append(E, 1, digest), " ", "@");
278 }
279 }
280 struct_name(n) ::= STRUCT(S) optional_name(N). {
281 if (N) {
282 n = N;
283 free(S);
284 } else {
285 char digest[17];
286 psi_token_hash(S, digest);
287 n = psi_token_translit(psi_token_append(S, 1, digest), " ", "@");
288 }
289 }
290 union_name(n) ::= UNION(U) optional_name(N). {
291 if (N) {
292 n = N;
293 free(U);
294 } else {
295 char digest[17];
296 psi_token_hash(U, digest);
297 n = psi_token_translit(psi_token_append(U, 1, digest), " ", "@");
298 }
299 }
300 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
301 e = psi_decl_enum_init(N->text, list);
302 e->token = N;
303 }
304 decl_enum_items(l) ::= decl_enum_item(i). {
305 l = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_enum_item_free),
306 &i);
307 }
308 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
309 l = psi_plist_add(l_, &i);
310 }
311 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
312 i = psi_decl_enum_item_init(N->text, num);
313 i->token = N;
314 }
315 decl_enum_item(i) ::= NAME(N). {
316 i = psi_decl_enum_item_init(N->text, NULL);
317 i->token = N;
318 }
319 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
320 args_ = args;
321 }
322 decl_struct_args(args_) ::= decl_struct_args_block(args). {
323 args_ = args;
324 }
325 decl_struct_args(args_) ::= EOS. {
326 args_ = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
327 }
328 decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
329 strct = psi_decl_struct_init(N->text, args);
330 strct->align = as.pos;
331 strct->size = as.len;
332 strct->token = N;
333 }
334 decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
335 u = psi_decl_union_init(N->text, args);
336 u->align = as.pos;
337 u->size = as.len;
338 u->token = N;
339 }
340 const_type(type_) ::= const_type_token(T). {
341 type_ = psi_const_type_init(T->type, T->text);
342 free(T);
343 }
344 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
345 constant = psi_const_init(type, T->text, val);
346 constant->token = T;
347 }
348 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
349 def = def_;
350 def->token = T;
351 }
352 decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
353 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_STRUCT, N->text), var);
354 def->type->token = psi_token_copy(N);
355 def->type->real.strct = psi_decl_struct_init(N->text, args);
356 def->type->real.strct->token = N;
357 def->type->real.strct->align = as.pos;
358 def->type->real.strct->size = as.len;
359 }
360 decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
361 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_UNION, N->text), var);
362 def->type->token = psi_token_copy(N);
363 def->type->real.unn = psi_decl_union_init(N->text, args);
364 def->type->real.unn->token = N;
365 def->type->real.unn->align = as.pos;
366 def->type->real.unn->size = as.len;
367 }
368 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
369 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_ENUM, e->name), psi_decl_var_init(ALIAS->text, 0, 0));
370 def->var->token = ALIAS;
371 def->type->token = psi_token_copy(e->token);
372 def->type->real.enm = e;
373 }
374 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
375 def = def_;
376 }
377 decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. {
378 args = args_;
379 }
380 decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). {
381 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_FUNCTION, func_->var->name), psi_decl_var_copy(func_->var));
382 def->type->token = psi_token_copy(func_->token);
383 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
384 }
385 decl_typedef_body(def) ::= decl_arg(arg). {
386 def = arg;
387 }
388 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
389 decl = psi_decl_init(abi, func, args);
390 }
391 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) COMMA ELLIPSIS RPAREN EOS. {
392 decl = psi_decl_init(abi, func, args);
393 decl->varargs = 1;
394 }
395 decl_func(func) ::= decl_arg(arg). {
396 func = arg;
397 }
398 decl_func(func) ::= VOID(T) NAME(N). {
399 func = psi_decl_arg_init(
400 psi_decl_type_init(T->type, T->text),
401 psi_decl_var_init(N->text, 0, 0)
402 );
403 func->type->token = T;
404 func->var->token = N;
405 func->token = N;
406 }
407 decl_typedef_body(def) ::= VOID(T) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
408 struct psi_decl_arg *func_ = psi_decl_arg_init(
409 psi_decl_type_init(T->type, T->text),
410 psi_decl_var_init(N->text, decl_i, 0)
411 );
412 func_->type->token = T;
413 func_->var->token = N;
414 func_->token = N;
415 def = psi_decl_arg_init(
416 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
417 psi_decl_var_copy(func_->var)
418 );
419 def->var->pointer_level = type_i;
420 def->type->token = psi_token_copy(func_->token);
421 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
422 }
423 decl_typedef_body(def) ::= CONST VOID(T) pointers(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
424 struct psi_decl_arg *func_ = psi_decl_arg_init(
425 psi_decl_type_init(T->type, T->text),
426 psi_decl_var_init(N->text, decl_i, 0)
427 );
428 func_->type->token = T;
429 func_->var->token = N;
430 func_->token = N;
431 def = psi_decl_arg_init(
432 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
433 psi_decl_var_copy(func_->var)
434 );
435 def->var->pointer_level = type_i;
436 def->type->token = psi_token_copy(func_->token);
437 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
438 }
439 decl_abi(abi) ::= NAME(T). {
440 abi = psi_decl_abi_init(T->text);
441 abi->token = T;
442 }
443 decl_var_array_size(as) ::= . {
444 as = NULL;
445 }
446 decl_var_array_size(as) ::= LBRACKET NUMBER(D) RBRACKET. {
447 as = D;
448 }
449 decl_var(var) ::= NAME(T) decl_var_array_size(as). {
450 var = psi_decl_var_init(T->text, 0, as?atol(as->text):0);
451 var->token = T;
452 if (as) {
453 free(as);
454 }
455 }
456 decl_var(var) ::= pointers(p) NAME(T) decl_var_array_size(as). {
457 var = psi_decl_var_init(T->text, p+!!as, as?atol(as->text):0);
458 var->token = T;
459 if (as) {
460 free(as);
461 }
462 }
463 decl_vars(vars) ::= decl_var(var). {
464 vars = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_var_free),
465 &var);
466 }
467 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
468 vars = psi_plist_add(vars_, &var);
469 }
470 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
471 arg_ = psi_decl_arg_init(type, var);
472 }
473 decl_typedef_body(def) ::= const_decl_type(type_) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
474 struct psi_decl_arg *func_ = psi_decl_arg_init(
475 type_,
476 psi_decl_var_init(N->text, decl_i, 0)
477 );
478 func_->var->token = N;
479 func_->token = N;
480 def = psi_decl_arg_init(
481 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
482 psi_decl_var_copy(func_->var)
483 );
484 def->var->pointer_level = type_i;
485 def->type->token = psi_token_copy(func_->token);
486 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
487 }
488 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
489 arg_ = psi_decl_arg_init(
490 psi_decl_type_init(T->type, T->text),
491 psi_decl_var_init(N->text, p, 0)
492 );
493 arg_->type->token = T;
494 arg_->var->token = N;
495 arg_->token = N;
496 }
497 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
498 arg_ = psi_decl_arg_init(
499 psi_decl_type_init(T->type, T->text),
500 psi_decl_var_init(N->text, p, 0)
501 );
502 arg_->type->token = T;
503 arg_->var->token = N;
504 arg_->token = N;
505 }
506 decl_args(args) ::= . {
507 args = NULL;
508 }
509 decl_args(args) ::= VOID. {
510 args = NULL;
511 }
512 decl_args(args) ::= decl_arg(arg). {
513 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free),
514 &arg);
515 }
516 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
517 args = psi_plist_add(args_, &arg);
518 }
519 struct_args(args) ::= struct_arg(arg). {
520 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free),
521 &arg);
522 }
523 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
524 args = psi_plist_add(args_, &arg);
525 }
526 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
527 arg_ = def;
528 switch (def->type->type) {
529 case PSI_T_STRUCT:
530 if (def->type->real.strct) {
531 if (!P->structs) {
532 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
533 }
534 P->structs = psi_plist_add(P->structs, &def->type->real.strct);
535 }
536 break;
537 case PSI_T_UNION:
538 if (def->type->real.unn) {
539 if (!P->unions) {
540 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
541 }
542 P->unions = psi_plist_add(P->unions, &def->type->real.unn);
543 }
544 break;
545 case PSI_T_ENUM:
546 if (def->type->real.enm) {
547 if (!P->enums) {
548 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
549 }
550 P->enums = psi_plist_add(P->enums, &def->type->real.enm);
551 }
552 break;
553 }
554 }
555 struct_arg(arg) ::= decl_arg(arg_) decl_layout(layout_) EOS. {
556 arg_->layout = layout_;
557 arg = arg_;
558 }
559 decl_layout(layout) ::= . {
560 layout = NULL;
561 }
562 decl_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
563 layout = psi_layout_init(atol(POS->text), atol(SIZ->text));
564 free(POS);
565 free(SIZ);
566 }
567 decl_scalar_type(type_) ::= CHAR(C). {
568 type_ = C;
569 }
570 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
571 if (s) {
572 type_ = psi_token_cat(2, S, s);
573 free(S);
574 free(s);
575 } else {
576 type_ = S;
577 }
578 }
579 decl_scalar_type_short(s) ::= . {
580 s = NULL;
581 }
582 decl_scalar_type_short(s) ::= INT(I). {
583 s = I;
584 }
585 decl_scalar_type(type_) ::= INT(I). {
586 type_ = I;
587 }
588 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
589 if (l) {
590 type_ = psi_token_cat(2, L, l);
591 free(L);
592 free(l);
593 } else {
594 type_ = L;
595 }
596 }
597 decl_scalar_type_long(l) ::= . {
598 l = NULL;
599 }
600 decl_scalar_type_long(l) ::= DOUBLE(D). {
601 l = D;
602 }
603 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
604 if (ll) {
605 l = psi_token_cat(2, L, ll);
606 free(L);
607 free(ll);
608 } else {
609 l = L;
610 }
611 }
612 decl_scalar_type_long_long(ll) ::= . {
613 ll = NULL;
614 }
615 decl_scalar_type_long_long(ll) ::= INT(I). {
616 ll = I;
617 }
618 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
619 struct psi_token *T = psi_token_cat(2, U, N);
620 type_ = psi_decl_type_init(T->type, T->text);
621 type_->token = T;
622 free(U);
623 free(N);
624 }
625 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
626 struct psi_token *T = psi_token_cat(2, S, N);
627 type_ = psi_decl_type_init(T->type, T->text);
628 type_->token = T;
629 free(S);
630 free(N);
631 }
632 decl_type(type_) ::= UNSIGNED(U). {
633 type_ = psi_decl_type_init(PSI_T_NAME, U->text);
634 type_->token = U;
635 }
636 decl_type(type_) ::= SIGNED(S). {
637 type_ = psi_decl_type_init(PSI_T_NAME, S->text);
638 type_->token = S;
639 }
640 decl_type(type_) ::= decl_scalar_type(N). {
641 type_ = psi_decl_type_init(N->type, N->text);
642 type_->token = N;
643 }
644 decl_type(type_) ::= STRUCT(S) NAME(T). {
645 type_ = psi_decl_type_init(S->type, T->text);
646 type_->token = T;
647 free(S);
648 }
649 decl_type(type_) ::= UNION(U) NAME(T). {
650 type_ = psi_decl_type_init(U->type, T->text);
651 type_->token = T;
652 free(U);
653 }
654 decl_type(type_) ::= ENUM(E) NAME(T). {
655 type_ = psi_decl_type_init(E->type, T->text);
656 type_->token = T;
657 free(E);
658 }
659 decl_type(type_) ::= decl_type_token(T). {
660 type_ = psi_decl_type_init(T->type, T->text);
661 type_->token = T;
662 }
663 const_decl_type(type) ::= decl_type(type_). {
664 type = type_;
665 }
666 const_decl_type(type) ::= CONST decl_type(type_). {
667 type = type_;
668 }
669 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
670 impl = psi_impl_init(func, stmts);
671 }
672 impl(impl) ::= STATIC impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
673 func->static_memory = 1;
674 impl = psi_impl_init(func, stmts);
675 }
676 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) LPAREN RPAREN COLON impl_type(type). {
677 func = psi_impl_func_init(NAME->text, NULL, type);
678 func->token = NAME;
679 func->return_reference = r;
680 }
681 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) LPAREN impl_args(args) RPAREN COLON impl_type(type). {
682 func = psi_impl_func_init(NAME->text, args, type);
683 func->token = NAME;
684 func->return_reference = r;
685 }
686 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). {
687 func = psi_impl_func_init(NAME->text, args, func_type);
688 func->token = NAME;
689 func->return_reference = func_r;
690 func->vararg = psi_impl_arg_init(va_type, psi_impl_var_init(T->text, va_r), NULL);
691 free(T);
692 }
693 impl_def_val(def) ::= impl_def_val_token(T). {
694 def = psi_impl_def_val_init(T->type, T->text);
695 def->token = T;
696 }
697 impl_var(var) ::= reference(r) DOLLAR_NAME(T). {
698 var = psi_impl_var_init(T->text, r);
699 var->token = T;
700 }
701 impl_type(type_) ::= impl_type_token(T). {
702 type_ = psi_impl_type_init(T->type, T->text);
703 free(T);
704 }
705 impl_arg(arg) ::= impl_type(type) impl_var(var). {
706 arg = psi_impl_arg_init(type, var, NULL);
707 }
708 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
709 arg = psi_impl_arg_init(type, var, def);
710 }
711 impl_args(args) ::= impl_arg(arg). {
712 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_arg_free),
713 &arg);
714 }
715 impl_args(args) ::= impl_args(args_) COMMA impl_arg(arg). {
716 args = psi_plist_add(args_, &arg);
717 }
718 impl_stmts(stmts) ::= impl_stmt(stmt). {
719 stmts = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_stmt_free),
720 &stmt);
721 }
722 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
723 stmts = psi_plist_add(stmts_, &stmt);
724 }
725 impl_stmt(i) ::= return_stmt(r). {
726 i = (struct psi_token**) r;
727 }
728 impl_stmt(i) ::= let_stmt(l). {
729 i = (struct psi_token**) l;
730 }
731 impl_stmt(i) ::= set_stmt(s). {
732 i = (struct psi_token**) s;
733 }
734 impl_stmt(i) ::= free_stmt(f). {
735 i = (struct psi_token**) f;
736 }
737 number(exp) ::= number_token(tok). {
738 exp = psi_number_init(tok->type, tok->text);
739 exp->token = tok;
740 }
741 number(exp) ::= decl_var(var). {
742 exp = psi_number_init(PSI_T_NAME, var);
743 exp->token = psi_token_copy(var->token);
744 }
745 num_exp(exp) ::= number(num). {
746 exp = psi_num_exp_init_num(num);
747 exp->token = psi_token_copy(num->token);
748 }
749 num_exp(exp) ::= LPAREN(L) num_exp(exp_) RPAREN. {
750 exp = psi_num_exp_init_unary(PSI_T_LPAREN, exp_);
751 exp->token = L;
752 }
753 num_exp(exp) ::= num_exp(lhs_) binary_op_token(OP) num_exp(rhs_). {
754 exp = psi_num_exp_init_binary(OP->type, lhs_, rhs_);
755 exp->token = OP;
756 }
757 num_exp(exp) ::= unary_op_token(OP) num_exp(exp_). {
758 exp = psi_num_exp_init_unary(OP->type, exp_);
759 exp->token = OP;
760 }
761 let_exp(val) ::= NULL. {
762 val = psi_let_exp_init(PSI_LET_NULL, NULL);
763 }
764 let_exp(val) ::= AMPERSAND NULL. {
765 val = psi_let_exp_init(PSI_LET_NULL, NULL);
766 val->is_reference = 1;
767 }
768 let_exp(val) ::= let_callback(cb). {
769 val = psi_let_exp_init(PSI_LET_CALLBACK, cb);
770 }
771 let_exp(val) ::= let_calloc(ca). {
772 val = psi_let_exp_init(PSI_LET_CALLOC, ca);
773 }
774 let_exp(val) ::= AMPERSAND let_calloc(ca). {
775 val = psi_let_exp_init(PSI_LET_CALLOC, ca);
776 val->is_reference = 1;
777 }
778 let_exp(val) ::= let_func(fn). {
779 val = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, fn);
780 }
781 let_exp(val) ::= AMPERSAND let_func(fn). {
782 val = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, fn);
783 val->is_reference = 1;
784 }
785 let_exp(val) ::= num_exp(exp). {
786 val = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, exp);
787 }
788 let_exp(val) ::= AMPERSAND num_exp(exp). {
789 val = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, exp);
790 val->is_reference = 1;
791 }
792 let_exp(exp) ::= decl_var(var_) EQUALS let_exp(val). {
793 exp = val;
794 exp->var = var_;
795 }
796 let_stmt(let) ::= LET(T) let_exp(val) EOS. {
797 let = psi_let_stmt_init(val);
798 let->token = T;
799 }
800 let_stmt(let) ::= TEMP(T) decl_var(var) EQUALS reference(r) decl_var(val_) EOS. {
801 let = psi_let_stmt_init(psi_let_exp_init_ex(var, PSI_LET_TMP, val_));
802 let->token = T;
803 let->exp->is_reference = r ? 1 : 0;
804 }
805 let_callback(cb) ::= CALLBACK(T) callback_rval(F) LPAREN impl_var(var) LPAREN callback_arg_list(args_) RPAREN RPAREN. {
806 cb = psi_let_callback_init(psi_let_func_init(F->type, F->text, var), args_);
807 cb->token = T;
808 free(F);
809 }
810 let_calloc(alloc) ::= CALLOC(T) LPAREN num_exp(nmemb) COMMA num_exp(size) RPAREN. {
811 alloc = psi_let_calloc_init(nmemb, size);
812 alloc->token = T;
813 }
814 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
815 func = psi_let_func_init(T->type, T->text, var);
816 func->token = T;
817 }
818 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) COMMA let_exps(vals) RPAREN. {
819 func = psi_let_func_init(T->type, T->text, var);
820 func->token = T;
821 func->inner = vals;
822 }
823 let_exps(exps) ::= let_exp(exp). {
824 exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_let_exp_free),
825 &exp);
826 }
827 let_exps(exps) ::= let_exps(exps_) COMMA let_exp(exp). {
828 exps = psi_plist_add(exps_, &exp);
829 }
830 callback_arg_list(args) ::= . {
831 args = NULL;
832 }
833 callback_arg_list(args) ::= callback_args(args_). {
834 args = args_;
835 }
836 callback_args(args) ::= set_exp(val). {
837 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free),
838 &val);
839 }
840 callback_args(args) ::= callback_args(args_) COMMA set_exp(val). {
841 args = psi_plist_add(args_, &val);
842 }
843 callback_rval(rval) ::= let_func_token(F). {
844 rval = F;
845 }
846 callback_rval(rval) ::= VOID(V). {
847 rval = V;
848 }
849 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) RPAREN. {
850 func = psi_set_func_init(T->type, T->text, var);
851 func->token = T;
852 }
853 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) COMMA set_exps(vals) RPAREN. {
854 func = psi_set_func_init(T->type, T->text, var);
855 func->token = T;
856 func->inner = vals;
857 }
858 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) COMMA ELLIPSIS RPAREN. {
859 func = psi_set_func_init(T->type, T->text, var);
860 func->token = T;
861 func->recursive = 1;
862 }
863 set_exp(val) ::= set_func(fn). {
864 val = psi_set_exp_init(PSI_SET_FUNC, fn);
865 }
866 set_exp(val) ::= num_exp(num). {
867 val = psi_set_exp_init(PSI_SET_NUMEXP, num);
868 }
869 set_exps(exps) ::= set_exp(exp). {
870 exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free),
871 &exp);
872 }
873 set_exps(exps) ::= set_exps(exps_) COMMA set_exp(exp). {
874 exps = psi_plist_add(exps_, &exp);
875 }
876 set_exp(exp) ::= impl_var(var_) EQUALS set_exp(val). {
877 exp = val;
878 exp->var = var_;
879 }
880 set_stmt(set) ::= SET(T) set_exp(exp) EOS. {
881 set = psi_set_stmt_init(exp);
882 set->token = T;
883 }
884 return_stmt(ret) ::= RETURN(T) set_func(func) EOS. {
885 ret = psi_return_stmt_init(psi_set_exp_init(PSI_SET_FUNC, func));
886 ret->token = T;
887 }
888 free_stmt(free) ::= FREE(T) free_exps(calls) EOS. {
889 free = psi_free_stmt_init(calls);
890 free->token = T;
891 }
892 free_exps(calls) ::= free_exp(call). {
893 calls = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_free_exp_free),
894 &call);
895 }
896 free_exps(calls) ::= free_exps(calls_) COMMA free_exp(call). {
897 calls = psi_plist_add(calls_, &call);
898 }
899 free_exp(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
900 call = psi_free_exp_init(F->text, vars);
901 call->token = F;
902 }
903 reference(r) ::= . {
904 r = false;
905 }
906 reference(r) ::= AMPERSAND. {
907 r = true;
908 }
909 indirection(i) ::= .{
910 i = 0;
911 }
912 indirection(i) ::= pointers(p). {
913 i = p;
914 }
915 pointers(p) ::= ASTERISK. {
916 p = 1;
917 }
918 pointers(p) ::= pointers(p_) ASTERISK. {
919 p = p_+1;
920 }