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