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