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->psi.file.fn, P->line, "Unexpected token '%s'", TOKEN->text);
22 %fallback NAME FREE SET LET RETURN LIB.
27 blocks ::= blocks block.
31 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
33 PSI_ParserSyntaxError(P, P->psi.file.ln, T->line, "Extra 'lib %s' statement has no effect", libname->text);
35 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
41 block ::= decl(decl). {
42 P->decls = add_decl(P->decls, decl);
44 block ::= impl(impl). {
45 P->impls = add_impl(P->impls, impl);
47 block ::= decl_typedef(def). {
48 P->defs = add_decl_typedef(P->defs, def);
49 if (def->type->strct) {
50 P->structs = add_decl_struct(P->structs, def->type->strct);
53 block ::= constant(constant). {
54 P->consts = add_constant(P->consts, constant);
56 block ::= decl_struct(strct). {
57 P->structs = add_decl_struct(P->structs, strct);
60 %type decl_struct {decl_struct*}
61 %destructor decl_struct {free_decl_struct($$);}
62 decl_struct(strct) ::= STRUCT NAME(N) LBRACE struct_args(args) RBRACE. {
63 strct = init_decl_struct(N->text, args);
67 %type const_type {const_type*}
68 %destructor const_type {free_const_type($$);}
69 const_type(type_) ::= BOOL(T). {
70 type_ = init_const_type(T->type, T->text);
73 const_type(type_) ::= INT(T). {
74 type_ = init_const_type(T->type, T->text);
77 const_type(type_) ::= FLOAT(T). {
78 type_ = init_const_type(T->type, T->text);
81 const_type(type_) ::= STRING(T). {
82 type_ = init_const_type(T->type, T->text);
85 %type constant {constant*}
86 %destructor constant {free_constant($$);}
87 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
88 constant = init_constant(type, T->text, val);
92 %type decl_typedef {decl_typedef*}
93 %destructor decl_typedef {free_decl_typedef($$);}
94 decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
95 def = init_decl_typedef(ALIAS->text, type);
98 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
99 def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
104 decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
105 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, s->name));
106 def->type->strct = s;
111 %destructor decl {free_decl($$);}
112 decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
113 decl = init_decl(abi, func, args);
116 %type decl_abi {decl_abi*}
117 %destructor decl_abi {free_decl_abi($$);}
118 decl_abi(abi) ::= NAME(T). {
119 abi = init_decl_abi(T->text);
123 %type decl_var {decl_var*}
124 %destructor decl_var {free_decl_var($$);}
125 decl_var(var) ::= NAME(T). {
126 var = init_decl_var(T->text, 0, 0);
129 decl_var(var) ::= pointers(p) NAME(T). {
130 var = init_decl_var(T->text, p, 0);
133 decl_var(var) ::= NAME(T) LBRACKET NUMBER(D) RBRACKET. {
134 var = init_decl_var(T->text, 1, atol(D->text));
138 decl_var(var) ::= pointers(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
139 var = init_decl_var(T->text, p+1, atol(D->text));
144 %type decl_vars {decl_vars*}
145 %destructor decl_vars {free_decl_vars($$);}
146 decl_vars(vars) ::= decl_var(var). {
147 vars = init_decl_vars(var);
149 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
150 vars = add_decl_var(vars_, var);
153 %type decl_arg {decl_arg*}
154 %destructor decl_arg {free_decl_arg($$);}
155 decl_arg(arg_) ::= decl_type(type) decl_var(var). {
156 arg_ = var->arg = init_decl_arg(type, var);
159 %type decl_args {decl_args*}
160 %destructor decl_args {free_decl_args($$);}
162 decl_args(args) ::= decl_arg(arg). {
163 args = init_decl_args(arg);
165 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
166 args = add_decl_arg(args_, arg);
168 %type struct_args {decl_args*}
169 %destructor struct_args {free_decl_args($$);}
170 struct_args(args) ::= decl_arg(arg) EOS. {
171 args = init_decl_args(arg);
173 struct_args(args) ::= struct_args(args_) decl_arg(arg) EOS. {
174 args = add_decl_arg(args_, arg);
177 %type decl_type {decl_type*}
178 %destructor decl_type {free_decl_type($$);}
179 decl_type(type_) ::= VOID(T). {
180 type_ = init_decl_type(T->type, T->text);
183 decl_type(type_) ::= FLOAT(T). {
184 type_ = init_decl_type(T->type, T->text);
187 decl_type(type_) ::= DOUBLE(T). {
188 type_ = init_decl_type(T->type, T->text);
191 /* we have to support plain int here because we have it in our lexer rules */
192 decl_type(type_) ::= INT(T). {
193 type_ = init_decl_type(PSI_T_NAME, T->text);
196 decl_type(type_) ::= INT8(T). {
197 type_ = init_decl_type(T->type, T->text);
200 decl_type(type_) ::= UINT8(T). {
201 type_ = init_decl_type(T->type, T->text);
204 decl_type(type_) ::= INT16(T). {
205 type_ = init_decl_type(T->type, T->text);
208 decl_type(type_) ::= UINT16(T). {
209 type_ = init_decl_type(T->type, T->text);
212 decl_type(type_) ::= INT32(T). {
213 type_ = init_decl_type(T->type, T->text);
216 decl_type(type_) ::= UINT32(T). {
217 type_ = init_decl_type(T->type, T->text);
220 decl_type(type_) ::= INT64(T). {
221 type_ = init_decl_type(T->type, T->text);
224 decl_type(type_) ::= UINT64(T). {
225 type_ = init_decl_type(T->type, T->text);
228 decl_type(type_) ::= NAME(T). {
229 type_ = init_decl_type(T->type, T->text);
232 decl_type(type_) ::= STRUCT(S) NAME(T). {
233 type_ = init_decl_type(S->type, T->text);
239 %destructor impl {free_impl($$);}
240 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
241 impl = init_impl(func, stmts);
244 %type impl_func {impl_func*}
245 %destructor impl_func {free_impl_func($$);}
246 impl_func(func) ::= FUNCTION NSNAME(NAME) impl_args(args) COLON impl_type(type). {
247 func = init_impl_func(NAME->text, args, type, 0);
250 impl_func(func) ::= FUNCTION REFERENCE NSNAME(NAME) impl_args(args) COLON impl_type(type). {
251 func = init_impl_func(NAME->text, args, type, 1);
255 %type impl_def_val {impl_def_val*}
256 %destructor impl_def_val {free_impl_def_val($$);}
257 impl_def_val(def) ::= NULL(T). {
258 def = init_impl_def_val(T->type, T->text);
261 impl_def_val(def) ::= NUMBER(T). {
262 def = init_impl_def_val(T->type, T->text);
265 impl_def_val(def) ::= TRUE(T). {
266 def = init_impl_def_val(T->type, T->text);
269 impl_def_val(def) ::= FALSE(T). {
270 def = init_impl_def_val(T->type, T->text);
273 impl_def_val(def) ::= QUOTED_STRING(T). {
274 def = init_impl_def_val(T->type, T->text);
278 %type impl_var {impl_var*}
279 %destructor impl_var {free_impl_var($$);}
280 impl_var(var) ::= DOLLAR NAME(T). {
281 var = init_impl_var(T->text, 0);
284 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
285 var = init_impl_var(T->text, 1);
289 %type impl_arg {impl_arg*}
290 %destructor impl_arg {free_impl_arg($$);}
291 impl_arg(arg) ::= impl_type(type) impl_var(var). {
292 arg = init_impl_arg(type, var, NULL);
294 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
295 arg = init_impl_arg(type, var, def);
298 %type impl_args {impl_args*}
299 %destructor impl_args {free_impl_args($$);}
300 impl_args(args) ::= LPAREN RPAREN. {
303 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
306 %type impl_arg_list {impl_args*}
307 %destructor impl_arg_list {free_impl_args($$);}
308 impl_arg_list(args) ::= impl_arg(arg). {
309 args = init_impl_args(arg);
311 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
312 args = add_impl_arg(args_, arg);
315 %type impl_stmts {impl_stmts*}
316 %destructor impl_stmts {free_impl_stmts($$);}
317 impl_stmts(stmts) ::= impl_stmt(stmt). {
318 stmts = init_impl_stmts(stmt);
320 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
321 stmts = add_impl_stmt(stmts_, stmt);
324 %type impl_stmt {impl_stmt*}
325 %destructor impl_stmt {free_impl_stmt($$);}
326 impl_stmt(stmt) ::= let_stmt(let). {
327 stmt = init_impl_stmt(PSI_T_LET, let);
329 impl_stmt(stmt) ::= set_stmt(set). {
330 stmt = init_impl_stmt(PSI_T_SET, set);
332 impl_stmt(stmt) ::= return_stmt(ret). {
333 stmt = init_impl_stmt(PSI_T_RETURN, ret);
335 impl_stmt(stmt) ::= free_stmt(free). {
336 stmt = init_impl_stmt(PSI_T_FREE, free);
339 %type let_stmt {let_stmt*}
340 %destructor let_stmt {free_let_stmt($$);}
341 let_stmt(let) ::= LET decl_var(var) EOS. {
342 let = init_let_stmt(var, NULL);
344 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
345 let = init_let_stmt(var, val);
348 %type let_value {let_value*}
349 %destructor let_value {free_let_value($$);}
350 let_value(val) ::= CALLOC(F) LPAREN NUMBER(N) COMMA decl_type(t) RPAREN. {
351 val = init_let_value(
352 init_let_func(F->type, F->text,
361 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
362 val = init_let_value(func, var, 0);
364 let_value(val) ::= REFERENCE let_func(func) LPAREN impl_var(var) RPAREN. {
365 val = init_let_value(func, var, 1);
367 let_value(val) ::= REFERENCE NULL. {
368 val = init_let_value(NULL, NULL, 1);
370 let_value(val) ::= NULL. {
371 val = init_let_value(NULL, NULL, 0);
374 %type let_func {let_func*}
375 %destructor let_func {free_let_func($$);}
376 let_func(func) ::= STRLEN(T). {
377 func = init_let_func(T->type, T->text, NULL);
380 let_func(func) ::= STRVAL(T). {
381 func = init_let_func(T->type, T->text, NULL);
384 let_func(func) ::= INTVAL(T). {
385 func = init_let_func(T->type, T->text, NULL);
388 let_func(func) ::= FLOATVAL(T). {
389 func = init_let_func(T->type, T->text, NULL);
392 let_func(func) ::= BOOLVAL(T). {
393 func = init_let_func(T->type, T->text, NULL);
396 let_func(func) ::= ARRVAL(T). {
397 func = init_let_func(T->type, T->text, NULL);
401 %type set_stmt {set_stmt*}
402 %destructor set_stmt {free_set_stmt($$);}
403 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
404 set = init_set_stmt(var, val);
407 %type set_value {set_value*}
408 %destructor set_value {free_set_value($$);}
409 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
410 val = init_set_value(func, vars);
412 set_value(val) ::= set_func(func_) LPAREN decl_vars(vars_) COMMA set_vals(vals) RPAREN. {
417 %type set_vals {set_value*}
418 %destructor set_vals {free_set_value($$);}
419 set_vals(vals) ::= set_value(val). {
420 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
422 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
423 vals = add_inner_set_value(vals_, val);
426 %type set_func {set_func*}
427 %destructor set_func {free_set_func($$);}
428 set_func(func) ::= TO_ARRAY(T). {
429 func = init_set_func(T->type, T->text);
432 set_func(func) ::= TO_STRING(T). {
433 func = init_set_func(T->type, T->text);
436 set_func(func) ::= TO_INT(T). {
437 func = init_set_func(T->type, T->text);
440 set_func(func) ::= TO_FLOAT(T). {
441 func = init_set_func(T->type, T->text);
444 set_func(func) ::= TO_BOOL(T). {
445 func = init_set_func(T->type, T->text);
448 set_func(func) ::= VOID(T). {
449 func = init_set_func(T->type, T->text);
453 %type return_stmt {return_stmt*}
454 %destructor return_stmt {free_return_stmt($$);}
455 return_stmt(ret) ::= RETURN set_value(val) EOS. {
456 ret = init_return_stmt(val);
459 %type free_stmt {free_stmt*}
460 %destructor free_stmt {free_free_stmt($$);}
461 free_stmt(free) ::= FREE free_calls(calls) EOS. {
462 free = init_free_stmt(calls);
465 %type free_calls {free_calls*}
466 %destructor free_calls {free_free_calls($$);}
467 free_calls(calls) ::= free_call(call). {
468 calls = init_free_calls(call);
470 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
471 calls = add_free_call(calls_, call);
474 %type free_call {free_call*}
475 %destructor free_call {free_free_call($$);}
476 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
477 call = init_free_call(F->text, vars);
480 %type impl_type {impl_type*}
481 %destructor impl_type {free_impl_type($$);}
482 impl_type(type_) ::= VOID(T). {
483 type_ = init_impl_type(T->type, T->text);
486 impl_type(type_) ::= MIXED(T). {
487 type_ = init_impl_type(T->type, T->text);
490 impl_type(type_) ::= BOOL(T). {
491 type_ = init_impl_type(T->type, T->text);
494 impl_type(type_) ::= INT(T). {
495 type_ = init_impl_type(T->type, T->text);
498 impl_type(type_) ::= FLOAT(T). {
499 type_ = init_impl_type(T->type, T->text);
502 impl_type(type_) ::= STRING(T). {
503 type_ = init_impl_type(T->type, T->text);
506 impl_type(type_) ::= ARRAY(T). {
507 type_ = init_impl_type(T->type, T->text);
511 %type pointers {unsigned}
512 pointers(p) ::= POINTER. {++p;}
513 pointers(p) ::= pointers(P) POINTER. {p = P+1;}