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