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