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 */
18 PSI_ParserSyntaxError(P, P->fn, P->line, "Unexpected token '%s'", TOKEN->text);
23 blocks ::= blocks block.
27 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
29 PSI_ParserSyntaxError(P, P->fn, T->line, "Extra 'lib %s' statement has no effect", libname->text);
31 P->lib = strndup(libname->text + 1, libname->size - 2);
37 block ::= decl(decl). {
38 P->decls = add_decl(P->decls, decl);
40 block ::= impl(impl). {
41 P->impls = add_impl(P->impls, impl);
43 block ::= decl_typedef(def). {
44 P->defs = add_decl_typedef(P->defs, def);
46 block ::= constant(constant). {
47 P->consts = add_constant(P->consts, constant);
50 %type const_type {const_type*}
51 const_type(type_) ::= BOOL(T). {
52 type_ = init_const_type(T->type, T->text);
55 const_type(type_) ::= INT(T). {
56 type_ = init_const_type(T->type, T->text);
59 const_type(type_) ::= FLOAT(T). {
60 type_ = init_const_type(T->type, T->text);
63 const_type(type_) ::= STRING(T). {
64 type_ = init_const_type(T->type, T->text);
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);
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);
80 decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
81 decl = init_decl(abi, func, args);
84 %type decl_abi {decl_abi*}
85 decl_abi(abi) ::= NAME(T). {
86 abi = init_decl_abi(T->text);
90 %type decl_var {decl_var*}
91 decl_var(var) ::= NAME(T). {
92 var = init_decl_var(T->text, 0, 0);
95 decl_var(var) ::= pointers(p) NAME(T). {
96 var = init_decl_var(T->text, p, 0);
99 decl_var(var) ::= NAME(T) LBRACKET NUMBER(D) RBRACKET. {
100 var = init_decl_var(T->text, 1, atol(D->text));
104 decl_var(var) ::= pointers(p) NAME(T) LBRACKET DIGITS(D) RBRACKET. {
105 var = init_decl_var(T->text, p+1, atol(D->text));
110 %type decl_vars {decl_vars*}
111 decl_vars(vars) ::= decl_var(var). {
112 vars = init_decl_vars(var);
114 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
115 vars = add_decl_var(vars_, var);
118 %type decl_arg {decl_arg*}
119 decl_arg(arg) ::= decl_type(type) decl_var(var). {
120 arg = init_decl_arg(type, var);
123 %type decl_args {decl_args*}
125 decl_args(args) ::= decl_arg(arg). {
126 args = init_decl_args(arg);
128 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
129 args = add_decl_arg(args_, arg);
132 %type decl_type {decl_type*}
133 decl_type(type_) ::= VOID(T). {
134 type_ = init_decl_type(T->type, T->text);
137 decl_type(type_) ::= CHAR(T). {
138 type_ = init_decl_type(T->type, T->text);
141 decl_type(type_) ::= SHORT(T). {
142 type_ = init_decl_type(T->type, T->text);
145 decl_type(type_) ::= INT(T). {
146 type_ = init_decl_type(T->type, T->text);
149 decl_type(type_) ::= LONG(T). {
150 type_ = init_decl_type(T->type, T->text);
153 decl_type(type_) ::= FLOAT(T). {
154 type_ = init_decl_type(T->type, T->text);
157 decl_type(type_) ::= DOUBLE(T). {
158 type_ = init_decl_type(T->type, T->text);
161 decl_type(type_) ::= SINT8(T). {
162 type_ = init_decl_type(T->type, T->text);
165 decl_type(type_) ::= UINT8(T). {
166 type_ = init_decl_type(T->type, T->text);
169 decl_type(type_) ::= SINT16(T). {
170 type_ = init_decl_type(T->type, T->text);
173 decl_type(type_) ::= UINT16(T). {
174 type_ = init_decl_type(T->type, T->text);
177 decl_type(type_) ::= SINT32(T). {
178 type_ = init_decl_type(T->type, T->text);
181 decl_type(type_) ::= UINT32(T). {
182 type_ = init_decl_type(T->type, T->text);
185 decl_type(type_) ::= SINT64(T). {
186 type_ = init_decl_type(T->type, T->text);
189 decl_type(type_) ::= UINT64(T). {
190 type_ = init_decl_type(T->type, T->text);
193 decl_type(type_) ::= NAME(T). {
194 type_ = init_decl_type(T->type, T->text);
199 %destructor impl {free_impl($$);}
200 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
201 impl = init_impl(func, stmts);
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);
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);
214 %type impl_def_val {impl_def_val*}
215 impl_def_val(def) ::= NULL(T). {
216 def = init_impl_def_val(T);
219 impl_def_val(def) ::= NUMBER(T). {
220 def = init_impl_def_val(T);
223 impl_def_val(def) ::= TRUE(T). {
224 def = init_impl_def_val(T);
227 impl_def_val(def) ::= FALSE(T). {
228 def = init_impl_def_val(T);
231 impl_def_val(def) ::= QUOTED_STRING(T). {
232 def = init_impl_def_val(T);
236 %type impl_var {impl_var*}
237 impl_var(var) ::= DOLLAR NAME(T). {
238 var = init_impl_var(T->text, 0);
241 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
242 var = init_impl_var(T->text, 1);
246 %type impl_arg {impl_arg*}
247 impl_arg(arg) ::= impl_type(type) impl_var(var). {
248 arg = init_impl_arg(type, var, NULL);
250 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
251 arg = init_impl_arg(type, var, def);
254 %type impl_args {impl_args*}
255 impl_args(args) ::= LPAREN RPAREN. {
258 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
261 %type impl_arg_list {impl_args*}
262 impl_arg_list(args) ::= impl_arg(arg). {
263 args = init_impl_args(arg);
265 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
266 args = add_impl_arg(args_, arg);
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);
274 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
275 stmts = add_impl_stmt(stmts_, stmt);
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);
283 impl_stmt(stmt) ::= set_stmt(set). {
284 stmt = init_impl_stmt(PSI_T_SET, set);
286 impl_stmt(stmt) ::= return_stmt(ret). {
287 stmt = init_impl_stmt(PSI_T_RETURN, ret);
289 impl_stmt(stmt) ::= free_stmt(free). {
290 stmt = init_impl_stmt(PSI_T_FREE, free);
293 %type let_stmt {let_stmt*}
294 let_stmt(let) ::= LET decl_var(var) EOS. {
295 let = init_let_stmt(var, NULL);
297 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
298 let = init_let_stmt(var, val);
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)
312 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
313 val = init_let_value(func, var, 0);
315 let_value(val) ::= REFERENCE let_func(func) LPAREN impl_var(var) RPAREN. {
316 val = init_let_value(func, var, 1);
318 let_value(val) ::= REFERENCE NULL. {
319 val = init_let_value(NULL, NULL, 1);
321 let_value(val) ::= NULL. {
322 val = init_let_value(NULL, NULL, 0);
325 %type let_func {let_func*}
326 let_func(func) ::= STRLEN(T). {
327 func = init_let_func(T->type, T->text, 0);
330 let_func(func) ::= STRVAL(T). {
331 func = init_let_func(T->type, T->text, 0);
334 let_func(func) ::= INTVAL(T). {
335 func = init_let_func(T->type, T->text, 0);
338 let_func(func) ::= FLOATVAL(T). {
339 func = init_let_func(T->type, T->text, 0);
342 let_func(func) ::= BOOLVAL(T). {
343 func = init_let_func(T->type, T->text, 0);
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);
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);
357 %type set_func {set_func*}
358 set_func(func) ::= TO_ARRAY(T). {
359 func = init_set_func(T->type, T->text);
362 set_func(func) ::= TO_STRING(T). {
363 func = init_set_func(T->type, T->text);
366 set_func(func) ::= TO_INT(T). {
367 func = init_set_func(T->type, T->text);
370 set_func(func) ::= TO_FLOAT(T). {
371 func = init_set_func(T->type, T->text);
374 set_func(func) ::= TO_BOOL(T). {
375 func = init_set_func(T->type, T->text);
378 set_func(func) ::= VOID(T). {
379 func = init_set_func(T->type, T->text);
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);
388 %type free_stmt {free_stmt*}
389 free_stmt(free) ::= FREE decl_vars(vars) EOS. {
390 free = init_free_stmt(vars);
393 %type impl_type {impl_type*}
394 impl_type(type_) ::= VOID(T). {
395 type_ = init_impl_type(T->type, T->text);
398 impl_type(type_) ::= MIXED(T). {
399 type_ = init_impl_type(T->type, T->text);
402 impl_type(type_) ::= BOOL(T). {
403 type_ = init_impl_type(T->type, T->text);
406 impl_type(type_) ::= INT(T). {
407 type_ = init_impl_type(T->type, T->text);
410 impl_type(type_) ::= FLOAT(T). {
411 type_ = init_impl_type(T->type, T->text);
414 impl_type(type_) ::= STRING(T). {
415 type_ = init_impl_type(T->type, T->text);
418 impl_type(type_) ::= ARRAY(T). {
419 type_ = init_impl_type(T->type, T->text);
423 %type pointers {unsigned}
424 pointers(p) ::= POINTER. {++p;}
425 pointers(p) ::= pointers(P) POINTER. {p = P+1;}