1cc466f16cb969b97c665479bae33d970f299516
[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 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(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 }
64 block ::= constant(constant). {
65 P->consts = add_constant(P->consts, constant);
66 }
67 block ::= decl_struct(strct). {
68 P->structs = add_decl_struct(P->structs, strct);
69 }
70
71 struct_name(n) ::= STRUCT NAME(N). {
72 n = N;
73 }
74
75 %type decl_struct_args {decl_args*}
76 %destructor decl_struct_args {free_decl_args($$);}
77 decl_struct_args(args_) ::= LBRACE struct_args(args) RBRACE. {
78 args_ = args;
79 }
80 decl_struct_args(args_) ::= EOS. {
81 args_ = init_decl_args(NULL);
82 }
83
84
85 %type decl_struct {decl_struct*}
86 %destructor decl_struct {free_decl_struct($$);}
87 decl_struct(strct) ::= struct_name(N) struct_size(size_) decl_struct_args(args). {
88 strct = init_decl_struct(N->text, args);
89 strct->size = size_;
90 strct->token = N;
91 }
92
93 %type struct_size {size_t}
94 struct_size(size) ::= . {
95 size = 0;
96 }
97 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
98 size = atol(SIZ->text);
99 free(SIZ);
100 }
101
102 %token_class const_type_token BOOL INT FLOAT STRING.
103 %type const_type {const_type*}
104 %destructor const_type {free_const_type($$);}
105 const_type(type_) ::= const_type_token(T). {
106 type_ = init_const_type(T->type, T->text);
107 free(T);
108 }
109 %type constant {constant*}
110 %destructor constant {free_constant($$);}
111 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
112 constant = init_constant(type, T->text, val);
113 free(T);
114 }
115
116 %type decl_typedef {decl_typedef*}
117 %destructor decl_typedef {free_decl_typedef($$);}
118 decl_typedef(def) ::= TYPEDEF decl_typedef_body(def_) EOS. {
119 def = def_;
120 }
121 %type decl_typedef_body {decl_typedef*}
122 %destructor decl_typedef_body {free_decl_typedef($$);}
123 decl_typedef_body(def) ::= decl_type(type) NAME(ALIAS). {
124 def = init_decl_typedef(ALIAS->text, type);
125 def->token = ALIAS;
126 }
127
128 /* support opaque types */
129 decl_typedef_body(def) ::= VOID(V) indirection(i) NAME(ALIAS). {
130 def = init_decl_typedef(ALIAS->text, init_decl_type(i?PSI_T_POINTER:V->type, V->text));
131 def->token = ALIAS;
132 def->type->token = V;
133 }
134
135 %type decl {decl*}
136 %destructor decl {free_decl($$);}
137 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
138 decl = init_decl(abi, func, args);
139 }
140
141 %type decl_func {decl_arg*}
142 %destructor decl_func {free_decl_arg($$);}
143 decl_func(func) ::= decl_arg(arg). {
144 func = arg;
145 }
146 /* special case for void functions */
147 decl_func(func) ::= VOID(T) NAME(N). {
148 func = init_decl_arg(
149 init_decl_type(T->type, T->text),
150 init_decl_var(N->text, 0, 0)
151 );
152 func->type->token = T;
153 //free(T);
154 free(N);
155 }
156
157 %type decl_abi {decl_abi*}
158 %destructor decl_abi {free_decl_abi($$);}
159 decl_abi(abi) ::= NAME(T). {
160 abi = init_decl_abi(T->text);
161 abi->token = T;
162 }
163
164 %type decl_var {decl_var*}
165 %destructor decl_var {free_decl_var($$);}
166 decl_var(var) ::= indirection(p) NAME(T). {
167 var = init_decl_var(T->text, p, 0);
168 var->token = T;
169 }
170 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
171 var = init_decl_var(T->text, p+1, atol(D->text));
172 var->token = T;
173 free(D);
174 }
175
176 %type decl_vars {decl_vars*}
177 %destructor decl_vars {free_decl_vars($$);}
178 decl_vars(vars) ::= decl_var(var). {
179 vars = init_decl_vars(var);
180 }
181 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
182 vars = add_decl_var(vars_, var);
183 }
184
185 %type decl_arg {decl_arg*}
186 %destructor decl_arg {free_decl_arg($$);}
187 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
188 arg_ = init_decl_arg(type, var);
189 }
190 /* void pointers need a specific rule */
191 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
192 arg_ = init_decl_arg(
193 init_decl_type(T->type, T->text),
194 init_decl_var(N->text, p, 0)
195 );
196 arg_->type->token = T;
197 arg_->var->token = N;
198 arg_->token = N;
199 }
200 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
201 arg_ = init_decl_arg(
202 init_decl_type(T->type, T->text),
203 init_decl_var(N->text, p, 0)
204 );
205 arg_->type->token = T;
206 arg_->var->token = N;
207 arg_->token = N;
208 }
209
210 %type decl_args {decl_args*}
211 %destructor decl_args {free_decl_args($$);}
212 decl_args ::= .
213 decl_args ::= VOID.
214 decl_args(args) ::= decl_arg(arg). {
215 args = init_decl_args(arg);
216 }
217 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
218 args = add_decl_arg(args_, arg);
219 }
220 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
221 args = args_;
222 args->varargs = 1;
223 }
224 %type struct_args {decl_args*}
225 %destructor struct_args {free_decl_args($$);}
226 struct_args(args) ::= struct_arg(arg). {
227 args = init_decl_args(arg);
228 }
229 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
230 args = add_decl_arg(args_, arg);
231 }
232 %type struct_arg {decl_arg*}
233 %destructor struct_arg {free_decl_arg($$);}
234 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
235 arg_->layout = layout_;
236 arg = arg_;
237 }
238
239 %type struct_layout {decl_struct_layout*}
240 %destructor struct_layout {free_decl_struct_layout($$);}
241 struct_layout(layout) ::= . {
242 layout = NULL;
243 }
244 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
245 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
246 free(POS);
247 free(SIZ);
248 }
249
250 /* un/signed, urgh */
251 decl_scalar_type(type_) ::= CHAR(C). {
252 type_ = C;
253 }
254 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
255 if (s) {
256 type_ = PSI_TokenCat(2, S, s);
257 free(S);
258 free(s);
259 } else {
260 type_ = S;
261 }
262 }
263 decl_scalar_type_short(s) ::= . {
264 s = NULL;
265 }
266
267 decl_scalar_type_short(s) ::= INT(I). {
268 s = I;
269 }
270 decl_scalar_type(type_) ::= INT(I). {
271 type_ = I;
272 }
273 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
274 if (l) {
275 type_ = PSI_TokenCat(2, L, l);
276 free(L);
277 free(l);
278 } else {
279 type_ = L;
280 }
281 }
282 decl_scalar_type_long(l) ::= . {
283 l = NULL;
284 }
285 decl_scalar_type_long(l) ::= DOUBLE(D). {
286 l = D;
287 }
288 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
289 if (ll) {
290 l = PSI_TokenCat(2, L, ll);
291 free(L);
292 free(ll);
293 } else {
294 l = L;
295 }
296 }
297 decl_scalar_type_long_long(ll) ::= . {
298 ll = NULL;
299 }
300 decl_scalar_type_long_long(ll) ::= INT(I). {
301 ll = I;
302 }
303 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
304 PSI_Token *T = PSI_TokenCat(2, U, N);
305 type_ = init_decl_type(T->type, T->text);
306 type_->token = T;
307 free(U);
308 free(N);
309 }
310 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
311 PSI_Token *T = PSI_TokenCat(2, S, N);
312 type_ = init_decl_type(T->type, T->text);
313 type_->token = T;
314 free(S);
315 free(N);
316 }
317 decl_type(type_) ::= UNSIGNED(U). {
318 type_ = init_decl_type(PSI_T_NAME, U->text);
319 type_->token = U;
320 }
321 decl_type(type_) ::= SIGNED(S). {
322 type_ = init_decl_type(PSI_T_NAME, S->text);
323 type_->token = S;
324 }
325 decl_type(type_) ::= decl_scalar_type(N). {
326 type_ = init_decl_type(N->type, N->text);
327 type_->token = N;
328 }
329 /* structs ! */
330 decl_type(type_) ::= STRUCT(S) NAME(T). {
331 type_ = init_decl_type(S->type, T->text);
332 type_->token = T;
333 free(S);
334 }
335 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
336 %type decl_type {decl_type*}
337 %destructor decl_type {free_decl_type($$);}
338 decl_type(type_) ::= decl_type_token(T). {
339 type_ = init_decl_type(T->type, T->text);
340 type_->token = T;
341 }
342
343
344 %type const_decl_type {decl_type*}
345 %destructor const_decl_type {free_decl_type($$);}
346 const_decl_type(type) ::= decl_type(type_). {
347 type = type_;
348 }
349 const_decl_type(type) ::= CONST decl_type(type_). {
350 type = type_;
351 }
352
353 %type impl {impl*}
354 %destructor impl {free_impl($$);}
355 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
356 impl = init_impl(func, stmts);
357 }
358
359 %type impl_func {impl_func*}
360 %destructor impl_func {free_impl_func($$);}
361 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
362 func = init_impl_func(NAME->text, args, type, r);
363 func->token = NAME;
364 }
365
366 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
367 %type impl_def_val {impl_def_val*}
368 %destructor impl_def_val {free_impl_def_val($$);}
369 impl_def_val(def) ::= impl_def_val_token(T). {
370 def = init_impl_def_val(T->type, T->text);
371 free(T);
372 }
373
374 %type impl_var {impl_var*}
375 %destructor impl_var {free_impl_var($$);}
376 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
377 var = init_impl_var(T->text, r);
378 var->token = T;
379 }
380
381 %type impl_arg {impl_arg*}
382 %destructor impl_arg {free_impl_arg($$);}
383 impl_arg(arg) ::= impl_type(type) impl_var(var). {
384 arg = init_impl_arg(type, var, NULL);
385 }
386 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
387 arg = init_impl_arg(type, var, def);
388 }
389
390 %type impl_args {impl_args*}
391 %destructor impl_args {free_impl_args($$);}
392 impl_args(args) ::= LPAREN RPAREN. {
393 args = NULL;
394 }
395 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
396 args = args_;
397 }
398 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
399 args = args_;
400 args->vararg.name = va;
401 }
402
403 %type impl_vararg {impl_arg*}
404 %destructor impl_vararg {free_impl_arg($$);}
405 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
406 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
407 free(T);
408 }
409
410 %type impl_arg_list {impl_args*}
411 %destructor impl_arg_list {free_impl_args($$);}
412 impl_arg_list(args) ::= impl_arg(arg). {
413 args = init_impl_args(arg);
414 }
415 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
416 args = add_impl_arg(args_, arg);
417 }
418
419 %type impl_stmts {impl_stmts*}
420 %destructor impl_stmts {free_impl_stmts($$);}
421 impl_stmts(stmts) ::= impl_stmt(stmt). {
422 stmts = init_impl_stmts(stmt);
423 }
424 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
425 stmts = add_impl_stmt(stmts_, stmt);
426 }
427
428 %type impl_stmt {impl_stmt*}
429 %destructor impl_stmt {free_impl_stmt($$);}
430 impl_stmt(stmt) ::= let_stmt(let). {
431 stmt = init_impl_stmt(PSI_T_LET, let);
432 }
433 impl_stmt(stmt) ::= set_stmt(set). {
434 stmt = init_impl_stmt(PSI_T_SET, set);
435 }
436 impl_stmt(stmt) ::= return_stmt(ret). {
437 stmt = init_impl_stmt(PSI_T_RETURN, ret);
438 }
439 impl_stmt(stmt) ::= free_stmt(free). {
440 stmt = init_impl_stmt(PSI_T_FREE, free);
441 }
442
443 %token_class num_exp_token NUMBER NSNAME.
444 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
445 %type num_exp {num_exp*}
446 %destructor num_exp {free_num_exp($$);}
447 num_exp(exp) ::= num_exp_token(tok). {
448 exp = init_num_exp(tok->type, tok->text);
449 exp->token = tok;
450 }
451 num_exp(exp) ::= decl_var(var). {
452 exp = init_num_exp(PSI_T_NAME, var);
453 exp->token = PSI_TokenCopy(var->token);
454 }
455 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
456 exp_->operator = operator_->type;
457 exp_->operand = operand_;
458 exp = exp_;
459 free(operator_);
460 }
461
462 %type let_stmt {let_stmt*}
463 %destructor let_stmt {free_let_stmt($$);}
464 let_stmt(let) ::= LET decl_var(var) EOS. {
465 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
466 }
467 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
468 val->flags.one.is_reference = r ? 1 : 0;
469 let = init_let_stmt(var, val);
470 }
471 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
472 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
473 }
474
475 %type let_val {let_val*}
476 %destructor let_val {free_let_val($$);}
477 let_val(val) ::= NULL. {
478 val = init_let_val(PSI_LET_NULL, NULL);
479 }
480 let_val(val) ::= num_exp(exp). {
481 val = init_let_val(PSI_LET_NUMEXP, exp);
482 }
483 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
484 val = init_let_val(PSI_LET_CALLOC, alloc);
485 }
486 let_val(val) ::= let_func(func). {
487 val = init_let_val(PSI_LET_FUNC, func);
488 }
489
490 %type let_calloc {let_calloc*}
491 %destructor let_calloc {free_let_calloc($$);}
492 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
493 alloc = init_let_calloc(nmemb, size);
494 }
495 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
496 %type let_func {let_func*}
497 %destructor let_func {free_let_func($$);}
498 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
499 func = init_let_func(T->type, T->text, var);
500 free(T);
501 }
502
503
504 %type set_stmt {set_stmt*}
505 %destructor set_stmt {free_set_stmt($$);}
506 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
507 set = init_set_stmt(var, val);
508 }
509
510 %type set_value {set_value*}
511 %destructor set_value {free_set_value($$);}
512 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
513 val = init_set_value(func, init_decl_vars(var));
514 }
515 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
516 val = init_set_value(func, init_decl_vars(var));
517 val->num = num_;
518 }
519 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
520 free_set_func(func_);
521 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
522 val->func->token = T;
523 }
524 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
525 val = vals;
526 val->func = func_;
527 val->vars = init_decl_vars(var);
528 }
529 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
530 val = vals;
531 val->func = func_;
532 val->num = num_;
533 val->vars = init_decl_vars(var);
534 }
535 %type set_vals {set_value*}
536 %destructor set_vals {free_set_value($$);}
537 set_vals(vals) ::= set_value(val). {
538 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
539 }
540 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
541 vals = add_inner_set_value(vals_, val);
542 }
543
544 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
545 %type set_func {set_func*}
546 %destructor set_func {free_set_func($$);}
547 set_func(func) ::= set_func_token(T). {
548 func = init_set_func(T->type, T->text);
549 func->token = T;
550 }
551
552 %type return_stmt {return_stmt*}
553 %destructor return_stmt {free_return_stmt($$);}
554 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
555 ret = init_return_stmt(val);
556 ret->token = T;
557 }
558
559 %type free_stmt {free_stmt*}
560 %destructor free_stmt {free_free_stmt($$);}
561 free_stmt(free) ::= FREE free_calls(calls) EOS. {
562 free = init_free_stmt(calls);
563 }
564
565 %type free_calls {free_calls*}
566 %destructor free_calls {free_free_calls($$);}
567 free_calls(calls) ::= free_call(call). {
568 calls = init_free_calls(call);
569 }
570 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
571 calls = add_free_call(calls_, call);
572 }
573
574 %type free_call {free_call*}
575 %destructor free_call {free_free_call($$);}
576 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
577 call = init_free_call(F->text, vars);
578 call->token = F;
579 }
580
581 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
582 %type impl_type {impl_type*}
583 %destructor impl_type {free_impl_type($$);}
584 impl_type(type_) ::= impl_type_token(T). {
585 type_ = init_impl_type(T->type, T->text);
586 free(T);
587 }
588
589 %type reference {char}
590 reference(r) ::= . {r = 0;}
591 reference(r) ::= AMPERSAND. {r = 1;}
592
593 %type indirection {unsigned}
594 indirection(i) ::= . {i = 0;}
595 indirection(i) ::= pointers(p). {i = p;}
596
597 %type pointers {unsigned}
598 pointers(p) ::= ASTERISK. {p = 1;}
599 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}