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