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