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