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 decl_struct(strct) ::= STRUCT NAME(N) LBRACE struct_args(args) RBRACE. {
58 strct = init_decl_struct(N->text, args);
59 }
60
61 %type const_type {const_type*}
62 const_type(type_) ::= BOOL(T). {
63 type_ = init_const_type(T->type, T->text);
64 free(T);
65 }
66 const_type(type_) ::= INT(T). {
67 type_ = init_const_type(T->type, T->text);
68 free(T);
69 }
70 const_type(type_) ::= FLOAT(T). {
71 type_ = init_const_type(T->type, T->text);
72 free(T);
73 }
74 const_type(type_) ::= STRING(T). {
75 type_ = init_const_type(T->type, T->text);
76 free(T);
77 }
78 %type constant {constant*}
79 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
80 constant = init_constant(type, T->text, val);
81 free(T);
82 }
83
84 %type decl_typedef {decl_typedef*}
85 decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
86 def = init_decl_typedef(ALIAS->text, type);
87 free(ALIAS);
88 }
89 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
90 def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
91 free(ALIAS);
92 free(S);
93 free(N);
94 }
95 decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
96 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, s->name));
97 def->type->strct = s;
98 free(ALIAS);
99 }
100
101 %type decl {decl*}
102 decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
103 decl = init_decl(abi, func, args);
104 }
105
106 %type decl_abi {decl_abi*}
107 decl_abi(abi) ::= NAME(T). {
108 abi = init_decl_abi(T->text);
109 free(T);
110 }
111
112 %type decl_var {decl_var*}
113 decl_var(var) ::= NAME(T). {
114 var = init_decl_var(T->text, 0, 0);
115 free(T);
116 }
117 decl_var(var) ::= pointers(p) NAME(T). {
118 var = init_decl_var(T->text, p, 0);
119 free(T);
120 }
121 decl_var(var) ::= NAME(T) LBRACKET NUMBER(D) RBRACKET. {
122 var = init_decl_var(T->text, 1, atol(D->text));
123 free(T);
124 free(D);
125 }
126 decl_var(var) ::= pointers(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
127 var = init_decl_var(T->text, p+1, atol(D->text));
128 free(T);
129 free(D);
130 }
131
132 %type decl_vars {decl_vars*}
133 decl_vars(vars) ::= decl_var(var). {
134 vars = init_decl_vars(var);
135 }
136 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
137 vars = add_decl_var(vars_, var);
138 }
139
140 %type decl_arg {decl_arg*}
141 decl_arg(arg) ::= decl_type(type) decl_var(var). {
142 arg = init_decl_arg(type, var);
143 }
144
145 %type decl_args {decl_args*}
146 decl_args ::= VOID.
147 decl_args(args) ::= decl_arg(arg). {
148 args = init_decl_args(arg);
149 }
150 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
151 args = add_decl_arg(args_, arg);
152 }
153 %type struct_args {decl_args*}
154 struct_args(args) ::= decl_arg(arg) EOS. {
155 args = init_decl_args(arg);
156 }
157 struct_args(args) ::= struct_args(args_) decl_arg(arg) EOS. {
158 args = add_decl_arg(args_, arg);
159 }
160
161 %type decl_type {decl_type*}
162 decl_type(type_) ::= VOID(T). {
163 type_ = init_decl_type(T->type, T->text);
164 free(T);
165 }
166 decl_type(type_) ::= CHAR(T). {
167 type_ = init_decl_type(T->type, T->text);
168 free(T);
169 }
170 decl_type(type_) ::= SHORT(T). {
171 type_ = init_decl_type(T->type, T->text);
172 free(T);
173 }
174 decl_type(type_) ::= INT(T). {
175 type_ = init_decl_type(T->type, T->text);
176 free(T);
177 }
178 decl_type(type_) ::= LONG(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 decl_type(type_) ::= SIZE_T(T). {
191 type_ = init_decl_type(T->type, T->text);
192 free(T);
193 }
194 decl_type(type_) ::= SINT8(T). {
195 type_ = init_decl_type(T->type, T->text);
196 free(T);
197 }
198 decl_type(type_) ::= UINT8(T). {
199 type_ = init_decl_type(T->type, T->text);
200 free(T);
201 }
202 decl_type(type_) ::= SINT16(T). {
203 type_ = init_decl_type(T->type, T->text);
204 free(T);
205 }
206 decl_type(type_) ::= UINT16(T). {
207 type_ = init_decl_type(T->type, T->text);
208 free(T);
209 }
210 decl_type(type_) ::= SINT32(T). {
211 type_ = init_decl_type(T->type, T->text);
212 free(T);
213 }
214 decl_type(type_) ::= UINT32(T). {
215 type_ = init_decl_type(T->type, T->text);
216 free(T);
217 }
218 decl_type(type_) ::= SINT64(T). {
219 type_ = init_decl_type(T->type, T->text);
220 free(T);
221 }
222 decl_type(type_) ::= UINT64(T). {
223 type_ = init_decl_type(T->type, T->text);
224 free(T);
225 }
226 decl_type(type_) ::= NAME(T). {
227 type_ = init_decl_type(T->type, T->text);
228 free(T);
229 }
230
231 %type impl {impl*}
232 %destructor impl {free_impl($$);}
233 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
234 impl = init_impl(func, stmts);
235 }
236
237 %type impl_func {impl_func*}
238 impl_func(func) ::= FUNCTION NSNAME(NAME) impl_args(args) COLON impl_type(type). {
239 func = init_impl_func(NAME->text, args, type, 0);
240 free(NAME);
241 }
242 impl_func(func) ::= FUNCTION REFERENCE NSNAME(NAME) impl_args(args) COLON impl_type(type). {
243 func = init_impl_func(NAME->text, args, type, 1);
244 free(NAME);
245 }
246
247 %type impl_def_val {impl_def_val*}
248 impl_def_val(def) ::= NULL(T). {
249 def = init_impl_def_val(T);
250 free(T);
251 }
252 impl_def_val(def) ::= NUMBER(T). {
253 def = init_impl_def_val(T);
254 free(T);
255 }
256 impl_def_val(def) ::= TRUE(T). {
257 def = init_impl_def_val(T);
258 free(T);
259 }
260 impl_def_val(def) ::= FALSE(T). {
261 def = init_impl_def_val(T);
262 free(T);
263 }
264 impl_def_val(def) ::= QUOTED_STRING(T). {
265 def = init_impl_def_val(T);
266 free(T);
267 }
268
269 %type impl_var {impl_var*}
270 impl_var(var) ::= DOLLAR NAME(T). {
271 var = init_impl_var(T->text, 0);
272 free(T);
273 }
274 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
275 var = init_impl_var(T->text, 1);
276 free(T);
277 }
278
279 %type impl_arg {impl_arg*}
280 impl_arg(arg) ::= impl_type(type) impl_var(var). {
281 arg = init_impl_arg(type, var, NULL);
282 }
283 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
284 arg = init_impl_arg(type, var, def);
285 }
286
287 %type impl_args {impl_args*}
288 impl_args(args) ::= LPAREN RPAREN. {
289 args = NULL;
290 }
291 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
292 args = args_;
293 }
294 %type impl_arg_list {impl_args*}
295 impl_arg_list(args) ::= impl_arg(arg). {
296 args = init_impl_args(arg);
297 }
298 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
299 args = add_impl_arg(args_, arg);
300 }
301
302 %type impl_stmts {impl_stmts*}
303 %destructor impl_stmts {free_impl_stmts($$);}
304 impl_stmts(stmts) ::= impl_stmt(stmt). {
305 stmts = init_impl_stmts(stmt);
306 }
307 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
308 stmts = add_impl_stmt(stmts_, stmt);
309 }
310
311 %type impl_stmt {impl_stmt*}
312 %destructor impl_stmt {free_impl_stmt($$);}
313 impl_stmt(stmt) ::= let_stmt(let). {
314 stmt = init_impl_stmt(PSI_T_LET, let);
315 }
316 impl_stmt(stmt) ::= set_stmt(set). {
317 stmt = init_impl_stmt(PSI_T_SET, set);
318 }
319 impl_stmt(stmt) ::= return_stmt(ret). {
320 stmt = init_impl_stmt(PSI_T_RETURN, ret);
321 }
322 impl_stmt(stmt) ::= free_stmt(free). {
323 stmt = init_impl_stmt(PSI_T_FREE, free);
324 }
325
326 %type let_stmt {let_stmt*}
327 let_stmt(let) ::= LET decl_var(var) EOS. {
328 let = init_let_stmt(var, NULL);
329 }
330 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
331 let = init_let_stmt(var, val);
332 }
333
334 %type let_value {let_value*}
335 let_value(val) ::= CALLOC(F) LPAREN NUMBER(N) COMMA decl_type(t) RPAREN. {
336 val = init_let_value(
337 init_let_func(F->type, F->text,
338 atol(N->text) * psi_t_size(real_decl_type(t)->type)
339 ), NULL, 0
340 );
341 free_decl_type(t);
342 free(F);
343 free(N);
344 }
345 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
346 val = init_let_value(func, var, 0);
347 }
348 let_value(val) ::= REFERENCE let_func(func) LPAREN impl_var(var) RPAREN. {
349 val = init_let_value(func, var, 1);
350 }
351 let_value(val) ::= REFERENCE NULL. {
352 val = init_let_value(NULL, NULL, 1);
353 }
354 let_value(val) ::= NULL. {
355 val = init_let_value(NULL, NULL, 0);
356 }
357
358 %type let_func {let_func*}
359 let_func(func) ::= STRLEN(T). {
360 func = init_let_func(T->type, T->text, 0);
361 free(T);
362 }
363 let_func(func) ::= STRVAL(T). {
364 func = init_let_func(T->type, T->text, 0);
365 free(T);
366 }
367 let_func(func) ::= INTVAL(T). {
368 func = init_let_func(T->type, T->text, 0);
369 free(T);
370 }
371 let_func(func) ::= FLOATVAL(T). {
372 func = init_let_func(T->type, T->text, 0);
373 free(T);
374 }
375 let_func(func) ::= BOOLVAL(T). {
376 func = init_let_func(T->type, T->text, 0);
377 free(T);
378 }
379
380 %type set_stmt {set_stmt*}
381 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
382 set = init_set_stmt(var, val);
383 }
384
385 %type set_value {set_value*}
386 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
387 val = init_set_value(func, vars);
388 }
389
390 %type set_func {set_func*}
391 set_func(func) ::= TO_ARRAY(T). {
392 func = init_set_func(T->type, T->text);
393 free(T);
394 }
395 set_func(func) ::= TO_STRING(T). {
396 func = init_set_func(T->type, T->text);
397 free(T);
398 }
399 set_func(func) ::= TO_INT(T). {
400 func = init_set_func(T->type, T->text);
401 free(T);
402 }
403 set_func(func) ::= TO_FLOAT(T). {
404 func = init_set_func(T->type, T->text);
405 free(T);
406 }
407 set_func(func) ::= TO_BOOL(T). {
408 func = init_set_func(T->type, T->text);
409 free(T);
410 }
411 set_func(func) ::= VOID(T). {
412 func = init_set_func(T->type, T->text);
413 free(T);
414 }
415
416 %type return_stmt {return_stmt*}
417 return_stmt(ret) ::= RETURN set_func(func) LPAREN decl_var(var) RPAREN EOS. {
418 ret = init_return_stmt(func, var);
419 }
420
421 %type free_stmt {free_stmt*}
422 free_stmt(free) ::= FREE decl_vars(vars) EOS. {
423 free = init_free_stmt(vars);
424 }
425
426 %type impl_type {impl_type*}
427 impl_type(type_) ::= VOID(T). {
428 type_ = init_impl_type(T->type, T->text);
429 free(T);
430 }
431 impl_type(type_) ::= MIXED(T). {
432 type_ = init_impl_type(T->type, T->text);
433 free(T);
434 }
435 impl_type(type_) ::= BOOL(T). {
436 type_ = init_impl_type(T->type, T->text);
437 free(T);
438 }
439 impl_type(type_) ::= INT(T). {
440 type_ = init_impl_type(T->type, T->text);
441 free(T);
442 }
443 impl_type(type_) ::= FLOAT(T). {
444 type_ = init_impl_type(T->type, T->text);
445 free(T);
446 }
447 impl_type(type_) ::= STRING(T). {
448 type_ = init_impl_type(T->type, T->text);
449 free(T);
450 }
451 impl_type(type_) ::= ARRAY(T). {
452 type_ = init_impl_type(T->type, T->text);
453 free(T);
454 }
455
456 %type pointers {unsigned}
457 pointers(p) ::= POINTER. {++p;}
458 pointers(p) ::= pointers(P) POINTER. {p = P+1;}