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);
49 block ::= decl_struct(strct). {
50 P->structs = add_decl_struct(P->structs, strct);
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);
58 %type const_type {const_type*}
59 const_type(type_) ::= BOOL(T). {
60 type_ = init_const_type(T->type, T->text);
63 const_type(type_) ::= INT(T). {
64 type_ = init_const_type(T->type, T->text);
67 const_type(type_) ::= FLOAT(T). {
68 type_ = init_const_type(T->type, T->text);
71 const_type(type_) ::= STRING(T). {
72 type_ = init_const_type(T->type, T->text);
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);
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);
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));
94 decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
95 decl = init_decl(abi, func, args);
98 %type decl_abi {decl_abi*}
99 decl_abi(abi) ::= NAME(T). {
100 abi = init_decl_abi(T->text);
104 %type decl_var {decl_var*}
105 decl_var(var) ::= NAME(T). {
106 var = init_decl_var(T->text, 0, 0);
109 decl_var(var) ::= pointers(p) NAME(T). {
110 var = init_decl_var(T->text, p, 0);
113 decl_var(var) ::= NAME(T) LBRACKET NUMBER(D) RBRACKET. {
114 var = init_decl_var(T->text, 1, atol(D->text));
118 decl_var(var) ::= pointers(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
119 var = init_decl_var(T->text, p+1, atol(D->text));
124 %type decl_vars {decl_vars*}
125 decl_vars(vars) ::= decl_var(var). {
126 vars = init_decl_vars(var);
128 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
129 vars = add_decl_var(vars_, var);
132 %type decl_arg {decl_arg*}
133 decl_arg(arg) ::= decl_type(type) decl_var(var). {
134 arg = init_decl_arg(type, var);
137 %type decl_args {decl_args*}
139 decl_args(args) ::= decl_arg(arg). {
140 args = init_decl_args(arg);
142 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
143 args = add_decl_arg(args_, arg);
145 %type struct_args {decl_args*}
146 struct_args(args) ::= decl_arg(arg) EOS. {
147 args = init_decl_args(arg);
149 struct_args(args) ::= struct_args(args_) decl_arg(arg) EOS. {
150 args = add_decl_arg(args_, arg);
153 %type decl_type {decl_type*}
154 decl_type(type_) ::= VOID(T). {
155 type_ = init_decl_type(T->type, T->text);
158 decl_type(type_) ::= CHAR(T). {
159 type_ = init_decl_type(T->type, T->text);
162 decl_type(type_) ::= SHORT(T). {
163 type_ = init_decl_type(T->type, T->text);
166 decl_type(type_) ::= INT(T). {
167 type_ = init_decl_type(T->type, T->text);
170 decl_type(type_) ::= LONG(T). {
171 type_ = init_decl_type(T->type, T->text);
174 decl_type(type_) ::= FLOAT(T). {
175 type_ = init_decl_type(T->type, T->text);
178 decl_type(type_) ::= DOUBLE(T). {
179 type_ = init_decl_type(T->type, T->text);
182 decl_type(type_) ::= SINT8(T). {
183 type_ = init_decl_type(T->type, T->text);
186 decl_type(type_) ::= UINT8(T). {
187 type_ = init_decl_type(T->type, T->text);
190 decl_type(type_) ::= SINT16(T). {
191 type_ = init_decl_type(T->type, T->text);
194 decl_type(type_) ::= UINT16(T). {
195 type_ = init_decl_type(T->type, T->text);
198 decl_type(type_) ::= SINT32(T). {
199 type_ = init_decl_type(T->type, T->text);
202 decl_type(type_) ::= UINT32(T). {
203 type_ = init_decl_type(T->type, T->text);
206 decl_type(type_) ::= SINT64(T). {
207 type_ = init_decl_type(T->type, T->text);
210 decl_type(type_) ::= UINT64(T). {
211 type_ = init_decl_type(T->type, T->text);
214 decl_type(type_) ::= NAME(T). {
215 type_ = init_decl_type(T->type, T->text);
220 %destructor impl {free_impl($$);}
221 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
222 impl = init_impl(func, stmts);
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);
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);
235 %type impl_def_val {impl_def_val*}
236 impl_def_val(def) ::= NULL(T). {
237 def = init_impl_def_val(T);
240 impl_def_val(def) ::= NUMBER(T). {
241 def = init_impl_def_val(T);
244 impl_def_val(def) ::= TRUE(T). {
245 def = init_impl_def_val(T);
248 impl_def_val(def) ::= FALSE(T). {
249 def = init_impl_def_val(T);
252 impl_def_val(def) ::= QUOTED_STRING(T). {
253 def = init_impl_def_val(T);
257 %type impl_var {impl_var*}
258 impl_var(var) ::= DOLLAR NAME(T). {
259 var = init_impl_var(T->text, 0);
262 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
263 var = init_impl_var(T->text, 1);
267 %type impl_arg {impl_arg*}
268 impl_arg(arg) ::= impl_type(type) impl_var(var). {
269 arg = init_impl_arg(type, var, NULL);
271 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
272 arg = init_impl_arg(type, var, def);
275 %type impl_args {impl_args*}
276 impl_args(args) ::= LPAREN RPAREN. {
279 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
282 %type impl_arg_list {impl_args*}
283 impl_arg_list(args) ::= impl_arg(arg). {
284 args = init_impl_args(arg);
286 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
287 args = add_impl_arg(args_, arg);
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);
295 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
296 stmts = add_impl_stmt(stmts_, stmt);
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);
304 impl_stmt(stmt) ::= set_stmt(set). {
305 stmt = init_impl_stmt(PSI_T_SET, set);
307 impl_stmt(stmt) ::= return_stmt(ret). {
308 stmt = init_impl_stmt(PSI_T_RETURN, ret);
310 impl_stmt(stmt) ::= free_stmt(free). {
311 stmt = init_impl_stmt(PSI_T_FREE, free);
314 %type let_stmt {let_stmt*}
315 let_stmt(let) ::= LET decl_var(var) EOS. {
316 let = init_let_stmt(var, NULL);
318 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
319 let = init_let_stmt(var, val);
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)
333 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
334 val = init_let_value(func, var, 0);
336 let_value(val) ::= REFERENCE let_func(func) LPAREN impl_var(var) RPAREN. {
337 val = init_let_value(func, var, 1);
339 let_value(val) ::= REFERENCE NULL. {
340 val = init_let_value(NULL, NULL, 1);
342 let_value(val) ::= NULL. {
343 val = init_let_value(NULL, NULL, 0);
346 %type let_func {let_func*}
347 let_func(func) ::= STRLEN(T). {
348 func = init_let_func(T->type, T->text, 0);
351 let_func(func) ::= STRVAL(T). {
352 func = init_let_func(T->type, T->text, 0);
355 let_func(func) ::= INTVAL(T). {
356 func = init_let_func(T->type, T->text, 0);
359 let_func(func) ::= FLOATVAL(T). {
360 func = init_let_func(T->type, T->text, 0);
363 let_func(func) ::= BOOLVAL(T). {
364 func = init_let_func(T->type, T->text, 0);
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);
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);
378 %type set_func {set_func*}
379 set_func(func) ::= TO_ARRAY(T). {
380 func = init_set_func(T->type, T->text);
383 set_func(func) ::= TO_STRING(T). {
384 func = init_set_func(T->type, T->text);
387 set_func(func) ::= TO_INT(T). {
388 func = init_set_func(T->type, T->text);
391 set_func(func) ::= TO_FLOAT(T). {
392 func = init_set_func(T->type, T->text);
395 set_func(func) ::= TO_BOOL(T). {
396 func = init_set_func(T->type, T->text);
399 set_func(func) ::= VOID(T). {
400 func = init_set_func(T->type, T->text);
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);
409 %type free_stmt {free_stmt*}
410 free_stmt(free) ::= FREE decl_vars(vars) EOS. {
411 free = init_free_stmt(vars);
414 %type impl_type {impl_type*}
415 impl_type(type_) ::= VOID(T). {
416 type_ = init_impl_type(T->type, T->text);
419 impl_type(type_) ::= MIXED(T). {
420 type_ = init_impl_type(T->type, T->text);
423 impl_type(type_) ::= BOOL(T). {
424 type_ = init_impl_type(T->type, T->text);
427 impl_type(type_) ::= INT(T). {
428 type_ = init_impl_type(T->type, T->text);
431 impl_type(type_) ::= FLOAT(T). {
432 type_ = init_impl_type(T->type, T->text);
435 impl_type(type_) ::= STRING(T). {
436 type_ = init_impl_type(T->type, T->text);
439 impl_type(type_) ::= ARRAY(T). {
440 type_ = init_impl_type(T->type, T->text);
444 %type pointers {unsigned}
445 pointers(p) ::= POINTER. {++p;}
446 pointers(p) ::= pointers(P) POINTER. {p = P+1;}