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 if (TOKEN && TOKEN->type != PSI_T_EOF) {
19 PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected token '%s'", TOKEN->text);
21 PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected end of input");
28 %fallback NAME TEMP FREE SET LET RETURN LIB INT UNSIGNED.
33 blocks ::= blocks block.
37 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
39 PSI_ParserSyntaxError(P, P->psi.file.ln, T->line, "Extra 'lib %s' statement has no effect", libname->text);
41 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
47 block ::= decl(decl). {
48 P->decls = add_decl(P->decls, decl);
50 block ::= impl(impl). {
51 P->impls = add_impl(P->impls, impl);
53 block ::= decl_typedef(def). {
54 P->defs = add_decl_typedef(P->defs, def);
55 if (def->type->strct) {
56 P->structs = add_decl_struct(P->structs, def->type->strct);
59 block ::= constant(constant). {
60 P->consts = add_constant(P->consts, constant);
62 block ::= decl_struct(strct). {
63 P->structs = add_decl_struct(P->structs, strct);
66 %type decl_struct {decl_struct*}
67 %destructor decl_struct {free_decl_struct($$);}
68 decl_struct(strct) ::= STRUCT NAME(N) struct_size(size_) LBRACE struct_args(args) RBRACE. {
69 strct = init_decl_struct(N->text, args);
74 %type struct_size {size_t}
75 struct_size(size) ::= . {
78 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
79 size = atol(SIZ->text);
83 %token_class const_type_token BOOL INT FLOAT STRING.
84 %type const_type {const_type*}
85 %destructor const_type {free_const_type($$);}
86 const_type(type_) ::= const_type_token(T). {
87 type_ = init_const_type(T->type, T->text);
90 %type constant {constant*}
91 %destructor constant {free_constant($$);}
92 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
93 constant = init_constant(type, T->text, val);
97 %type decl_typedef {decl_typedef*}
98 %destructor decl_typedef {free_decl_typedef($$);}
99 decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
100 def = init_decl_typedef(ALIAS->text, type);
103 /* support opaque types */
104 decl_typedef(def) ::= TYPEDEF VOID(V) NAME(ALIAS) EOS. {
105 def = init_decl_typedef(ALIAS->text, init_decl_type(V->type, V->text));
106 def->type->token = V;
110 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
111 def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
112 def->type->token = N;
117 decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
118 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, s->name));
119 def->type->token = ALIAS;
120 def->type->strct = s;
125 %destructor decl {free_decl($$);}
126 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
127 decl = init_decl(abi, func, args);
131 %type decl_func {decl_arg*}
132 %destructor decl_func {free_decl_arg($$);}
133 decl_func(func) ::= decl_arg(arg). {
136 /* special case for void functions */
137 decl_func(func) ::= VOID(T) NAME(N). {
138 func = init_decl_arg(
139 init_decl_type(T->type, T->text),
140 init_decl_var(N->text, 0, 0)
142 func->type->token = T;
147 %type decl_abi {decl_abi*}
148 %destructor decl_abi {free_decl_abi($$);}
149 decl_abi(abi) ::= NAME(T). {
150 abi = init_decl_abi(T->text);
154 %type decl_var {decl_var*}
155 %destructor decl_var {free_decl_var($$);}
156 decl_var(var) ::= indirection(p) NAME(T). {
157 var = init_decl_var(T->text, p, 0);
160 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
161 var = init_decl_var(T->text, p+1, atol(D->text));
166 %type decl_vars {decl_vars*}
167 %destructor decl_vars {free_decl_vars($$);}
168 decl_vars(vars) ::= decl_var(var). {
169 vars = init_decl_vars(var);
171 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
172 vars = add_decl_var(vars_, var);
175 %type decl_arg {decl_arg*}
176 %destructor decl_arg {free_decl_arg($$);}
177 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
178 arg_ = init_decl_arg(type, var);
180 /* void pointers need a specific rule */
181 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
182 arg_ = init_decl_arg(
183 init_decl_type(T->type, T->text),
184 init_decl_var(N->text, p, 0)
186 arg_->type->token = T;
190 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
191 arg_ = init_decl_arg(
192 init_decl_type(T->type, T->text),
193 init_decl_var(N->text, p, 0)
195 arg_->type->token = T;
200 %type decl_args {decl_args*}
201 %destructor decl_args {free_decl_args($$);}
204 decl_args(args) ::= decl_arg(arg). {
205 args = init_decl_args(arg);
207 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
208 args = add_decl_arg(args_, arg);
210 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
214 %type struct_args {decl_args*}
215 %destructor struct_args {free_decl_args($$);}
216 struct_args(args) ::= struct_arg(arg). {
217 args = init_decl_args(arg);
219 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
220 args = add_decl_arg(args_, arg);
222 %type struct_arg {decl_arg*}
223 %destructor struct_arg {free_decl_arg($$);}
224 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
225 arg_->layout = layout_;
229 %type struct_layout {decl_struct_layout*}
230 %destructor struct_layout {free_decl_struct_layout($$);}
231 struct_layout(layout) ::= . {
234 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
235 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
240 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
241 %type decl_type {decl_type*}
242 %destructor decl_type {free_decl_type($$);}
243 decl_type(type_) ::= decl_type_token(T). {
244 type_ = init_decl_type(T->type, T->text);
249 decl_type(type_) ::= UNSIGNED NAME(T). {
250 type_ = init_decl_type(T->type, T->text);
252 type_->name = realloc(type_->name, T->size + sizeof("unsigned"));
253 memmove(type_->name + sizeof("unsigned"), type_->name, T->size);
254 memcpy(type_->name, "unsigned", sizeof("unsigned")-1);
255 type_->name[sizeof("unsigned")] = ' ';
256 type_->name[T->size + sizeof("unsigned")] = 0;
259 /* we have to support plain int here because we have it in our lexer rules */
260 decl_type(type_) ::= INT(T). {
261 type_ = init_decl_type(PSI_T_NAME, T->text);
266 decl_type(type_) ::= STRUCT(S) NAME(T). {
267 type_ = init_decl_type(S->type, T->text);
273 %type const_decl_type {decl_type*}
274 %destructor const_decl_type {free_decl_type($$);}
275 const_decl_type(type) ::= decl_type(type_). {
278 const_decl_type(type) ::= CONST decl_type(type_). {
283 %destructor impl {free_impl($$);}
284 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
285 impl = init_impl(func, stmts);
288 %type impl_func {impl_func*}
289 %destructor impl_func {free_impl_func($$);}
290 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
291 func = init_impl_func(NAME->text, args, type, r);
295 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
296 %type impl_def_val {impl_def_val*}
297 %destructor impl_def_val {free_impl_def_val($$);}
298 impl_def_val(def) ::= impl_def_val_token(T). {
299 def = init_impl_def_val(T->type, T->text);
303 %type impl_var {impl_var*}
304 %destructor impl_var {free_impl_var($$);}
305 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
306 var = init_impl_var(T->text, r);
310 %type impl_arg {impl_arg*}
311 %destructor impl_arg {free_impl_arg($$);}
312 impl_arg(arg) ::= impl_type(type) impl_var(var). {
313 arg = init_impl_arg(type, var, NULL);
315 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
316 arg = init_impl_arg(type, var, def);
319 %type impl_args {impl_args*}
320 %destructor impl_args {free_impl_args($$);}
321 impl_args(args) ::= LPAREN RPAREN. {
324 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
327 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
329 args->vararg.name = va;
332 %type impl_vararg {impl_arg*}
333 %destructor impl_vararg {free_impl_arg($$);}
334 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
335 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
339 %type impl_arg_list {impl_args*}
340 %destructor impl_arg_list {free_impl_args($$);}
341 impl_arg_list(args) ::= impl_arg(arg). {
342 args = init_impl_args(arg);
344 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
345 args = add_impl_arg(args_, arg);
348 %type impl_stmts {impl_stmts*}
349 %destructor impl_stmts {free_impl_stmts($$);}
350 impl_stmts(stmts) ::= impl_stmt(stmt). {
351 stmts = init_impl_stmts(stmt);
353 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
354 stmts = add_impl_stmt(stmts_, stmt);
357 %type impl_stmt {impl_stmt*}
358 %destructor impl_stmt {free_impl_stmt($$);}
359 impl_stmt(stmt) ::= let_stmt(let). {
360 stmt = init_impl_stmt(PSI_T_LET, let);
362 impl_stmt(stmt) ::= set_stmt(set). {
363 stmt = init_impl_stmt(PSI_T_SET, set);
365 impl_stmt(stmt) ::= return_stmt(ret). {
366 stmt = init_impl_stmt(PSI_T_RETURN, ret);
368 impl_stmt(stmt) ::= free_stmt(free). {
369 stmt = init_impl_stmt(PSI_T_FREE, free);
372 %token_class num_exp_token NUMBER NSNAME.
373 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
374 %type num_exp {num_exp*}
375 %destructor num_exp {free_num_exp($$);}
376 num_exp(exp) ::= num_exp_token(tok). {
377 exp = init_num_exp(tok->type, tok->text);
380 num_exp(exp) ::= decl_var(var). {
381 exp = init_num_exp(PSI_T_NAME, var);
383 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
384 exp_->operator = operator_->type;
385 exp_->operand = operand_;
390 %type let_stmt {let_stmt*}
391 %destructor let_stmt {free_let_stmt($$);}
392 let_stmt(let) ::= LET decl_var(var) EOS. {
393 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
395 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
396 val->flags.one.is_reference = r ? 1 : 0;
397 let = init_let_stmt(var, val);
399 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
400 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
403 %type let_val {let_val*}
404 %destructor let_val {free_let_val($$);}
405 let_val(val) ::= NULL. {
406 val = init_let_val(PSI_LET_NULL, NULL);
408 let_val(val) ::= num_exp(exp). {
409 val = init_let_val(PSI_LET_NUMEXP, exp);
411 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
412 val = init_let_val(PSI_LET_CALLOC, alloc);
414 let_val(val) ::= let_func(func). {
415 val = init_let_val(PSI_LET_FUNC, func);
418 %type let_calloc {let_calloc*}
419 %destructor let_calloc {free_let_calloc($$);}
420 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
421 alloc = init_let_calloc(nmemb, size);
423 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
424 %type let_func {let_func*}
425 %destructor let_func {free_let_func($$);}
426 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
427 func = init_let_func(T->type, T->text, var);
432 %type set_stmt {set_stmt*}
433 %destructor set_stmt {free_set_stmt($$);}
434 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
435 set = init_set_stmt(var, val);
438 %type set_value {set_value*}
439 %destructor set_value {free_set_value($$);}
440 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
441 val = init_set_value(func, init_decl_vars(var));
443 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
444 val = init_set_value(func, init_decl_vars(var));
447 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
450 val->vars = init_decl_vars(var);
452 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
456 val->vars = init_decl_vars(var);
458 %type set_vals {set_value*}
459 %destructor set_vals {free_set_value($$);}
460 set_vals(vals) ::= set_value(val). {
461 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
463 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
464 vals = add_inner_set_value(vals_, val);
467 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
468 %type set_func {set_func*}
469 %destructor set_func {free_set_func($$);}
470 set_func(func) ::= set_func_token(T). {
471 func = init_set_func(T->type, T->text);
475 %type return_stmt {return_stmt*}
476 %destructor return_stmt {free_return_stmt($$);}
477 return_stmt(ret) ::= RETURN set_value(val) EOS. {
478 ret = init_return_stmt(val);
481 %type free_stmt {free_stmt*}
482 %destructor free_stmt {free_free_stmt($$);}
483 free_stmt(free) ::= FREE free_calls(calls) EOS. {
484 free = init_free_stmt(calls);
487 %type free_calls {free_calls*}
488 %destructor free_calls {free_free_calls($$);}
489 free_calls(calls) ::= free_call(call). {
490 calls = init_free_calls(call);
492 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
493 calls = add_free_call(calls_, call);
496 %type free_call {free_call*}
497 %destructor free_call {free_free_call($$);}
498 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
499 call = init_free_call(F->text, vars);
503 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
504 %type impl_type {impl_type*}
505 %destructor impl_type {free_impl_type($$);}
506 impl_type(type_) ::= impl_type_token(T). {
507 type_ = init_impl_type(T->type, T->text);
511 %type reference {char}
512 reference(r) ::= . {r = 0;}
513 reference(r) ::= AMPERSAND. {r = 1;}
515 %type indirection {unsigned}
516 indirection(i) ::= . {i = 0;}
517 indirection(i) ::= pointers(p). {i = p;}
519 %type pointers {unsigned}
520 pointers(p) ::= ASTERISK. {p = 1;}
521 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}