flush
[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 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) NAME(ALIAS) EOS. {
108 def = init_decl_typedef(ALIAS->text, init_decl_type(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 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
241 %type decl_type {decl_type*}
242 %destructor decl_type {free_decl_type($$);}
243 decl_type(type_) ::= decl_type_token(T). {
244 type_ = init_decl_type(T->type, T->text);
245 type_->token = T;
246 }
247 /* unsigned, urgh */
248 decl_type(type_) ::= UNSIGNED(U) NAME(N). {
249 PSI_Token *T = PSI_TokenCat(2, U, N);
250 type_ = init_decl_type(T->type, T->text);
251 type_->token = T;
252 free(U);
253 free(N);
254 }
255 decl_type(type_) ::= SIGNED NAME(T). {
256 type_ = init_decl_type(T->type, T->text);
257 type_->token = T;
258 type_->name = realloc(type_->name, T->size + sizeof("signed"));
259 memmove(type_->name + sizeof("signed"), type_->name, T->size);
260 memcpy(type_->name, "signed", sizeof("signed")-1);
261 type_->name[sizeof("signed")] = ' ';
262 type_->name[T->size + sizeof("signed")] = 0;
263 }
264 /* we have to support plain int, long here because we have it in our lexer rules */
265 decl_type(type_) ::= INT(T). {
266 type_ = init_decl_type(PSI_T_NAME, T->text);
267 type_->token = T;
268 }
269 decl_type(type_) ::= LONG INT(T). {
270 type_ = init_decl_type(PSI_T_NAME, T->text);
271 type_->token = T;
272 }
273 /* structs ! */
274 decl_type(type_) ::= STRUCT(S) NAME(T). {
275 type_ = init_decl_type(S->type, T->text);
276 type_->token = T;
277 free(S);
278 }
279
280 %type const_decl_type {decl_type*}
281 %destructor const_decl_type {free_decl_type($$);}
282 const_decl_type(type) ::= decl_type(type_). {
283 type = type_;
284 }
285 const_decl_type(type) ::= CONST decl_type(type_). {
286 type = type_;
287 }
288
289 %type impl {impl*}
290 %destructor impl {free_impl($$);}
291 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
292 impl = init_impl(func, stmts);
293 }
294
295 %type impl_func {impl_func*}
296 %destructor impl_func {free_impl_func($$);}
297 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
298 func = init_impl_func(NAME->text, args, type, r);
299 func->token = NAME;
300 }
301
302 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
303 %type impl_def_val {impl_def_val*}
304 %destructor impl_def_val {free_impl_def_val($$);}
305 impl_def_val(def) ::= impl_def_val_token(T). {
306 def = init_impl_def_val(T->type, T->text);
307 free(T);
308 }
309
310 %type impl_var {impl_var*}
311 %destructor impl_var {free_impl_var($$);}
312 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
313 var = init_impl_var(T->text, r);
314 var->token = T;
315 }
316
317 %type impl_arg {impl_arg*}
318 %destructor impl_arg {free_impl_arg($$);}
319 impl_arg(arg) ::= impl_type(type) impl_var(var). {
320 arg = init_impl_arg(type, var, NULL);
321 }
322 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
323 arg = init_impl_arg(type, var, def);
324 }
325
326 %type impl_args {impl_args*}
327 %destructor impl_args {free_impl_args($$);}
328 impl_args(args) ::= LPAREN RPAREN. {
329 args = NULL;
330 }
331 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
332 args = args_;
333 }
334 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
335 args = args_;
336 args->vararg.name = va;
337 }
338
339 %type impl_vararg {impl_arg*}
340 %destructor impl_vararg {free_impl_arg($$);}
341 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
342 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
343 free(T);
344 }
345
346 %type impl_arg_list {impl_args*}
347 %destructor impl_arg_list {free_impl_args($$);}
348 impl_arg_list(args) ::= impl_arg(arg). {
349 args = init_impl_args(arg);
350 }
351 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
352 args = add_impl_arg(args_, arg);
353 }
354
355 %type impl_stmts {impl_stmts*}
356 %destructor impl_stmts {free_impl_stmts($$);}
357 impl_stmts(stmts) ::= impl_stmt(stmt). {
358 stmts = init_impl_stmts(stmt);
359 }
360 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
361 stmts = add_impl_stmt(stmts_, stmt);
362 }
363
364 %type impl_stmt {impl_stmt*}
365 %destructor impl_stmt {free_impl_stmt($$);}
366 impl_stmt(stmt) ::= let_stmt(let). {
367 stmt = init_impl_stmt(PSI_T_LET, let);
368 }
369 impl_stmt(stmt) ::= set_stmt(set). {
370 stmt = init_impl_stmt(PSI_T_SET, set);
371 }
372 impl_stmt(stmt) ::= return_stmt(ret). {
373 stmt = init_impl_stmt(PSI_T_RETURN, ret);
374 }
375 impl_stmt(stmt) ::= free_stmt(free). {
376 stmt = init_impl_stmt(PSI_T_FREE, free);
377 }
378
379 %token_class num_exp_token NUMBER NSNAME.
380 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
381 %type num_exp {num_exp*}
382 %destructor num_exp {free_num_exp($$);}
383 num_exp(exp) ::= num_exp_token(tok). {
384 exp = init_num_exp(tok->type, tok->text);
385 exp->token = tok;
386 }
387 num_exp(exp) ::= decl_var(var). {
388 exp = init_num_exp(PSI_T_NAME, var);
389 exp->token = PSI_TokenCopy(var->token);
390 }
391 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
392 exp_->operator = operator_->type;
393 exp_->operand = operand_;
394 exp = exp_;
395 free(operator_);
396 }
397
398 %type let_stmt {let_stmt*}
399 %destructor let_stmt {free_let_stmt($$);}
400 let_stmt(let) ::= LET decl_var(var) EOS. {
401 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
402 }
403 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
404 val->flags.one.is_reference = r ? 1 : 0;
405 let = init_let_stmt(var, val);
406 }
407 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
408 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
409 }
410
411 %type let_val {let_val*}
412 %destructor let_val {free_let_val($$);}
413 let_val(val) ::= NULL. {
414 val = init_let_val(PSI_LET_NULL, NULL);
415 }
416 let_val(val) ::= num_exp(exp). {
417 val = init_let_val(PSI_LET_NUMEXP, exp);
418 }
419 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
420 val = init_let_val(PSI_LET_CALLOC, alloc);
421 }
422 let_val(val) ::= let_func(func). {
423 val = init_let_val(PSI_LET_FUNC, func);
424 }
425
426 %type let_calloc {let_calloc*}
427 %destructor let_calloc {free_let_calloc($$);}
428 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
429 alloc = init_let_calloc(nmemb, size);
430 }
431 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
432 %type let_func {let_func*}
433 %destructor let_func {free_let_func($$);}
434 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
435 func = init_let_func(T->type, T->text, var);
436 free(T);
437 }
438
439
440 %type set_stmt {set_stmt*}
441 %destructor set_stmt {free_set_stmt($$);}
442 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
443 set = init_set_stmt(var, val);
444 }
445
446 %type set_value {set_value*}
447 %destructor set_value {free_set_value($$);}
448 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
449 val = init_set_value(func, init_decl_vars(var));
450 }
451 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
452 val = init_set_value(func, init_decl_vars(var));
453 val->num = num_;
454 }
455 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
456 free_set_func(func_);
457 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
458 val->func->token = T;
459 }
460 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
461 val = vals;
462 val->func = func_;
463 val->vars = init_decl_vars(var);
464 }
465 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
466 val = vals;
467 val->func = func_;
468 val->num = num_;
469 val->vars = init_decl_vars(var);
470 }
471 %type set_vals {set_value*}
472 %destructor set_vals {free_set_value($$);}
473 set_vals(vals) ::= set_value(val). {
474 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
475 }
476 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
477 vals = add_inner_set_value(vals_, val);
478 }
479
480 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
481 %type set_func {set_func*}
482 %destructor set_func {free_set_func($$);}
483 set_func(func) ::= set_func_token(T). {
484 func = init_set_func(T->type, T->text);
485 func->token = T;
486 }
487
488 %type return_stmt {return_stmt*}
489 %destructor return_stmt {free_return_stmt($$);}
490 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
491 ret = init_return_stmt(val);
492 ret->token = T;
493 }
494
495 %type free_stmt {free_stmt*}
496 %destructor free_stmt {free_free_stmt($$);}
497 free_stmt(free) ::= FREE free_calls(calls) EOS. {
498 free = init_free_stmt(calls);
499 }
500
501 %type free_calls {free_calls*}
502 %destructor free_calls {free_free_calls($$);}
503 free_calls(calls) ::= free_call(call). {
504 calls = init_free_calls(call);
505 }
506 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
507 calls = add_free_call(calls_, call);
508 }
509
510 %type free_call {free_call*}
511 %destructor free_call {free_free_call($$);}
512 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
513 call = init_free_call(F->text, vars);
514 call->token = F;
515 }
516
517 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
518 %type impl_type {impl_type*}
519 %destructor impl_type {free_impl_type($$);}
520 impl_type(type_) ::= impl_type_token(T). {
521 type_ = init_impl_type(T->type, T->text);
522 free(T);
523 }
524
525 %type reference {char}
526 reference(r) ::= . {r = 0;}
527 reference(r) ::= AMPERSAND. {r = 1;}
528
529 %type indirection {unsigned}
530 indirection(i) ::= . {i = 0;}
531 indirection(i) ::= pointers(p). {i = p;}
532
533 %type pointers {unsigned}
534 pointers(p) ::= ASTERISK. {p = 1;}
535 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}