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