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);
45 if (def->type->strct) {
46 P->structs = add_decl_struct(P->structs, def->type->strct);
49 block ::= constant(constant). {
50 P->consts = add_constant(P->consts, constant);
52 block ::= decl_struct(strct). {
53 P->structs = add_decl_struct(P->structs, strct);
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);
61 %type const_type {const_type*}
62 const_type(type_) ::= BOOL(T). {
63 type_ = init_const_type(T->type, T->text);
66 const_type(type_) ::= INT(T). {
67 type_ = init_const_type(T->type, T->text);
70 const_type(type_) ::= FLOAT(T). {
71 type_ = init_const_type(T->type, T->text);
74 const_type(type_) ::= STRING(T). {
75 type_ = init_const_type(T->type, T->text);
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);
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);
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));
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));
102 decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
103 decl = init_decl(abi, func, args);
106 %type decl_abi {decl_abi*}
107 decl_abi(abi) ::= NAME(T). {
108 abi = init_decl_abi(T->text);
112 %type decl_var {decl_var*}
113 decl_var(var) ::= NAME(T). {
114 var = init_decl_var(T->text, 0, 0);
117 decl_var(var) ::= pointers(p) NAME(T). {
118 var = init_decl_var(T->text, p, 0);
121 decl_var(var) ::= NAME(T) LBRACKET NUMBER(D) RBRACKET. {
122 var = init_decl_var(T->text, 1, atol(D->text));
126 decl_var(var) ::= pointers(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
127 var = init_decl_var(T->text, p+1, atol(D->text));
132 %type decl_vars {decl_vars*}
133 decl_vars(vars) ::= decl_var(var). {
134 vars = init_decl_vars(var);
136 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
137 vars = add_decl_var(vars_, var);
140 %type decl_arg {decl_arg*}
141 decl_arg(arg) ::= decl_type(type) decl_var(var). {
142 arg = init_decl_arg(type, var);
145 %type decl_args {decl_args*}
147 decl_args(args) ::= decl_arg(arg). {
148 args = init_decl_args(arg);
150 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
151 args = add_decl_arg(args_, arg);
153 %type struct_args {decl_args*}
154 struct_args(args) ::= decl_arg(arg) EOS. {
155 args = init_decl_args(arg);
157 struct_args(args) ::= struct_args(args_) decl_arg(arg) EOS. {
158 args = add_decl_arg(args_, arg);
161 %type decl_type {decl_type*}
162 decl_type(type_) ::= VOID(T). {
163 type_ = init_decl_type(T->type, T->text);
166 decl_type(type_) ::= CHAR(T). {
167 type_ = init_decl_type(T->type, T->text);
170 decl_type(type_) ::= SHORT(T). {
171 type_ = init_decl_type(T->type, T->text);
174 decl_type(type_) ::= INT(T). {
175 type_ = init_decl_type(T->type, T->text);
178 decl_type(type_) ::= LONG(T). {
179 type_ = init_decl_type(T->type, T->text);
182 decl_type(type_) ::= FLOAT(T). {
183 type_ = init_decl_type(T->type, T->text);
186 decl_type(type_) ::= DOUBLE(T). {
187 type_ = init_decl_type(T->type, T->text);
190 decl_type(type_) ::= SIZE_T(T). {
191 type_ = init_decl_type(T->type, T->text);
194 decl_type(type_) ::= SINT8(T). {
195 type_ = init_decl_type(T->type, T->text);
198 decl_type(type_) ::= UINT8(T). {
199 type_ = init_decl_type(T->type, T->text);
202 decl_type(type_) ::= SINT16(T). {
203 type_ = init_decl_type(T->type, T->text);
206 decl_type(type_) ::= UINT16(T). {
207 type_ = init_decl_type(T->type, T->text);
210 decl_type(type_) ::= SINT32(T). {
211 type_ = init_decl_type(T->type, T->text);
214 decl_type(type_) ::= UINT32(T). {
215 type_ = init_decl_type(T->type, T->text);
218 decl_type(type_) ::= SINT64(T). {
219 type_ = init_decl_type(T->type, T->text);
222 decl_type(type_) ::= UINT64(T). {
223 type_ = init_decl_type(T->type, T->text);
226 decl_type(type_) ::= NAME(T). {
227 type_ = init_decl_type(T->type, T->text);
232 %destructor impl {free_impl($$);}
233 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
234 impl = init_impl(func, stmts);
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);
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);
247 %type impl_def_val {impl_def_val*}
248 impl_def_val(def) ::= NULL(T). {
249 def = init_impl_def_val(T);
252 impl_def_val(def) ::= NUMBER(T). {
253 def = init_impl_def_val(T);
256 impl_def_val(def) ::= TRUE(T). {
257 def = init_impl_def_val(T);
260 impl_def_val(def) ::= FALSE(T). {
261 def = init_impl_def_val(T);
264 impl_def_val(def) ::= QUOTED_STRING(T). {
265 def = init_impl_def_val(T);
269 %type impl_var {impl_var*}
270 impl_var(var) ::= DOLLAR NAME(T). {
271 var = init_impl_var(T->text, 0);
274 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
275 var = init_impl_var(T->text, 1);
279 %type impl_arg {impl_arg*}
280 impl_arg(arg) ::= impl_type(type) impl_var(var). {
281 arg = init_impl_arg(type, var, NULL);
283 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
284 arg = init_impl_arg(type, var, def);
287 %type impl_args {impl_args*}
288 impl_args(args) ::= LPAREN RPAREN. {
291 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
294 %type impl_arg_list {impl_args*}
295 impl_arg_list(args) ::= impl_arg(arg). {
296 args = init_impl_args(arg);
298 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
299 args = add_impl_arg(args_, arg);
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);
307 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
308 stmts = add_impl_stmt(stmts_, stmt);
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);
316 impl_stmt(stmt) ::= set_stmt(set). {
317 stmt = init_impl_stmt(PSI_T_SET, set);
319 impl_stmt(stmt) ::= return_stmt(ret). {
320 stmt = init_impl_stmt(PSI_T_RETURN, ret);
322 impl_stmt(stmt) ::= free_stmt(free). {
323 stmt = init_impl_stmt(PSI_T_FREE, free);
326 %type let_stmt {let_stmt*}
327 let_stmt(let) ::= LET decl_var(var) EOS. {
328 let = init_let_stmt(var, NULL);
330 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
331 let = init_let_stmt(var, val);
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)
345 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
346 val = init_let_value(func, var, 0);
348 let_value(val) ::= REFERENCE let_func(func) LPAREN impl_var(var) RPAREN. {
349 val = init_let_value(func, var, 1);
351 let_value(val) ::= REFERENCE NULL. {
352 val = init_let_value(NULL, NULL, 1);
354 let_value(val) ::= NULL. {
355 val = init_let_value(NULL, NULL, 0);
358 %type let_func {let_func*}
359 let_func(func) ::= STRLEN(T). {
360 func = init_let_func(T->type, T->text, 0);
363 let_func(func) ::= STRVAL(T). {
364 func = init_let_func(T->type, T->text, 0);
367 let_func(func) ::= INTVAL(T). {
368 func = init_let_func(T->type, T->text, 0);
371 let_func(func) ::= FLOATVAL(T). {
372 func = init_let_func(T->type, T->text, 0);
375 let_func(func) ::= BOOLVAL(T). {
376 func = init_let_func(T->type, T->text, 0);
379 let_func(func) ::= ARRVAL(T). {
380 func = init_let_func(T->type, T->text, 0);
384 %type set_stmt {set_stmt*}
385 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
386 set = init_set_stmt(var, val);
389 %type set_value {set_value*}
390 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
391 val = init_set_value(func, vars);
394 %type set_func {set_func*}
395 set_func(func) ::= TO_ARRAY(T). {
396 func = init_set_func(T->type, T->text);
399 set_func(func) ::= TO_STRING(T). {
400 func = init_set_func(T->type, T->text);
403 set_func(func) ::= TO_INT(T). {
404 func = init_set_func(T->type, T->text);
407 set_func(func) ::= TO_FLOAT(T). {
408 func = init_set_func(T->type, T->text);
411 set_func(func) ::= TO_BOOL(T). {
412 func = init_set_func(T->type, T->text);
415 set_func(func) ::= VOID(T). {
416 func = init_set_func(T->type, T->text);
420 %type return_stmt {return_stmt*}
421 return_stmt(ret) ::= RETURN set_func(func) LPAREN decl_var(var) RPAREN EOS. {
422 ret = init_return_stmt(func, var);
425 %type free_stmt {free_stmt*}
426 free_stmt(free) ::= FREE decl_vars(vars) EOS. {
427 free = init_free_stmt(vars);
430 %type impl_type {impl_type*}
431 impl_type(type_) ::= VOID(T). {
432 type_ = init_impl_type(T->type, T->text);
435 impl_type(type_) ::= MIXED(T). {
436 type_ = init_impl_type(T->type, T->text);
439 impl_type(type_) ::= BOOL(T). {
440 type_ = init_impl_type(T->type, T->text);
443 impl_type(type_) ::= INT(T). {
444 type_ = init_impl_type(T->type, T->text);
447 impl_type(type_) ::= FLOAT(T). {
448 type_ = init_impl_type(T->type, T->text);
451 impl_type(type_) ::= STRING(T). {
452 type_ = init_impl_type(T->type, T->text);
455 impl_type(type_) ::= ARRAY(T). {
456 type_ = init_impl_type(T->type, T->text);
460 %type pointers {unsigned}
461 pointers(p) ::= POINTER. {++p;}
462 pointers(p) ::= pointers(P) POINTER. {p = P+1;}