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 %token_class const_type_token BOOL INT FLOAT STRING.
68 %type const_type {const_type*}
69 %destructor const_type {free_const_type($$);}
70 const_type(type_) ::= const_type_token(T). {
71 type_ = init_const_type(T->type, T->text);
74 %type constant {constant*}
75 %destructor constant {free_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 %destructor decl_typedef {free_decl_typedef($$);}
83 decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
84 def = init_decl_typedef(ALIAS->text, type);
87 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
88 def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
93 decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
94 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, s->name));
100 %destructor decl {free_decl($$);}
101 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
102 decl = init_decl(abi, func, args);
105 %type decl_func {decl_arg*}
106 %destructor decl_func {free_decl_arg($$);}
107 decl_func(func) ::= decl_arg(arg). {
110 /* special case for void functions */
111 decl_func(func) ::= VOID(T) NAME(N). {
112 func = init_decl_arg(
113 init_decl_type(T->type, T->text),
114 init_decl_var(N->text, 0, 0)
120 %type decl_abi {decl_abi*}
121 %destructor decl_abi {free_decl_abi($$);}
122 decl_abi(abi) ::= NAME(T). {
123 abi = init_decl_abi(T->text);
127 %type decl_var {decl_var*}
128 %destructor decl_var {free_decl_var($$);}
129 decl_var(var) ::= indirection(p) NAME(T). {
130 var = init_decl_var(T->text, p, 0);
133 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
134 var = init_decl_var(T->text, p+1, atol(D->text));
139 %type decl_vars {decl_vars*}
140 %destructor decl_vars {free_decl_vars($$);}
141 decl_vars(vars) ::= decl_var(var). {
142 vars = init_decl_vars(var);
144 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
145 vars = add_decl_var(vars_, var);
148 %type decl_arg {decl_arg*}
149 %destructor decl_arg {free_decl_arg($$);}
150 decl_arg(arg_) ::= decl_type(type) decl_var(var). {
151 arg_ = var->arg = init_decl_arg(type, var);
153 /* void pointers need a specific rule */
154 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
155 arg_ = init_decl_arg(
156 init_decl_type(T->type, T->text),
157 init_decl_var(N->text, p, 0)
159 arg_->var->arg = arg_;
164 %type decl_args {decl_args*}
165 %destructor decl_args {free_decl_args($$);}
168 decl_args(args) ::= decl_arg(arg). {
169 args = init_decl_args(arg);
171 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
172 args = add_decl_arg(args_, arg);
174 %type struct_args {decl_args*}
175 %destructor struct_args {free_decl_args($$);}
176 struct_args(args) ::= decl_arg(arg) EOS. {
177 args = init_decl_args(arg);
179 struct_args(args) ::= struct_args(args_) decl_arg(arg) EOS. {
180 args = add_decl_arg(args_, arg);
183 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
184 %type decl_type {decl_type*}
185 %destructor decl_type {free_decl_type($$);}
186 decl_type(type_) ::= decl_type_token(T). {
187 type_ = init_decl_type(T->type, T->text);
190 /* we have to support plain int here because we have it in our lexer rules */
191 decl_type(type_) ::= INT(T). {
192 type_ = init_decl_type(PSI_T_NAME, T->text);
196 decl_type(type_) ::= STRUCT(S) NAME(T). {
197 type_ = init_decl_type(S->type, T->text);
203 %destructor impl {free_impl($$);}
204 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
205 impl = init_impl(func, stmts);
208 %type impl_func {impl_func*}
209 %destructor impl_func {free_impl_func($$);}
210 impl_func(func) ::= FUNCTION NSNAME(NAME) impl_args(args) COLON impl_type(type). {
211 func = init_impl_func(NAME->text, args, type, 0);
214 impl_func(func) ::= FUNCTION REFERENCE NSNAME(NAME) impl_args(args) COLON impl_type(type). {
215 func = init_impl_func(NAME->text, args, type, 1);
219 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
220 %type impl_def_val {impl_def_val*}
221 %destructor impl_def_val {free_impl_def_val($$);}
222 impl_def_val(def) ::= impl_def_val_token(T). {
223 def = init_impl_def_val(T->type, T->text);
227 %type impl_var {impl_var*}
228 %destructor impl_var {free_impl_var($$);}
229 impl_var(var) ::= DOLLAR NAME(T). {
230 var = init_impl_var(T->text, 0);
233 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
234 var = init_impl_var(T->text, 1);
238 %type impl_arg {impl_arg*}
239 %destructor impl_arg {free_impl_arg($$);}
240 impl_arg(arg) ::= impl_type(type) impl_var(var). {
241 arg = init_impl_arg(type, var, NULL);
243 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
244 arg = init_impl_arg(type, var, def);
247 %type impl_args {impl_args*}
248 %destructor impl_args {free_impl_args($$);}
249 impl_args(args) ::= LPAREN RPAREN. {
252 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
255 %type impl_arg_list {impl_args*}
256 %destructor impl_arg_list {free_impl_args($$);}
257 impl_arg_list(args) ::= impl_arg(arg). {
258 args = init_impl_args(arg);
260 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
261 args = add_impl_arg(args_, arg);
264 %type impl_stmts {impl_stmts*}
265 %destructor impl_stmts {free_impl_stmts($$);}
266 impl_stmts(stmts) ::= impl_stmt(stmt). {
267 stmts = init_impl_stmts(stmt);
269 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
270 stmts = add_impl_stmt(stmts_, stmt);
273 %type impl_stmt {impl_stmt*}
274 %destructor impl_stmt {free_impl_stmt($$);}
275 impl_stmt(stmt) ::= let_stmt(let). {
276 stmt = init_impl_stmt(PSI_T_LET, let);
278 impl_stmt(stmt) ::= set_stmt(set). {
279 stmt = init_impl_stmt(PSI_T_SET, set);
281 impl_stmt(stmt) ::= return_stmt(ret). {
282 stmt = init_impl_stmt(PSI_T_RETURN, ret);
284 impl_stmt(stmt) ::= free_stmt(free). {
285 stmt = init_impl_stmt(PSI_T_FREE, free);
288 %type let_stmt {let_stmt*}
289 %destructor let_stmt {free_let_stmt($$);}
290 let_stmt(let) ::= LET decl_var(var) EOS. {
291 let = init_let_stmt(var, NULL);
293 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
294 let = init_let_stmt(var, val);
297 %type let_value {let_value*}
298 %destructor let_value {free_let_value($$);}
299 let_value(val) ::= CALLOC(F) LPAREN NUMBER(N) COMMA decl_type(t) RPAREN. {
300 val = init_let_value(
301 init_let_func(F->type, F->text,
310 let_value(val) ::= reference(r) let_func(func) LPAREN impl_var(var) RPAREN. {
311 val = init_let_value(func, var, r);
313 let_value(val) ::= reference(r) NULL. {
314 val = init_let_value(NULL, NULL, r);
317 %token_class let_func_token ARRVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
318 %type let_func {let_func*}
319 %destructor let_func {free_let_func($$);}
320 let_func(func) ::= let_func_token(T). {
321 func = init_let_func(T->type, T->text, NULL);
325 %type set_stmt {set_stmt*}
326 %destructor set_stmt {free_set_stmt($$);}
327 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
328 set = init_set_stmt(var, val);
331 %type set_value {set_value*}
332 %destructor set_value {free_set_value($$);}
333 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
334 val = init_set_value(func, vars);
336 set_value(val) ::= set_func(func_) LPAREN decl_vars(vars_) COMMA set_vals(vals) RPAREN. {
341 %type set_vals {set_value*}
342 %destructor set_vals {free_set_value($$);}
343 set_vals(vals) ::= set_value(val). {
344 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
346 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
347 vals = add_inner_set_value(vals_, val);
350 %token_class set_func_token TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
351 %type set_func {set_func*}
352 %destructor set_func {free_set_func($$);}
353 set_func(func) ::= set_func_token(T). {
354 func = init_set_func(T->type, T->text);
358 %type return_stmt {return_stmt*}
359 %destructor return_stmt {free_return_stmt($$);}
360 return_stmt(ret) ::= RETURN set_value(val) EOS. {
361 ret = init_return_stmt(val);
364 %type free_stmt {free_stmt*}
365 %destructor free_stmt {free_free_stmt($$);}
366 free_stmt(free) ::= FREE free_calls(calls) EOS. {
367 free = init_free_stmt(calls);
370 %type free_calls {free_calls*}
371 %destructor free_calls {free_free_calls($$);}
372 free_calls(calls) ::= free_call(call). {
373 calls = init_free_calls(call);
375 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
376 calls = add_free_call(calls_, call);
379 %type free_call {free_call*}
380 %destructor free_call {free_free_call($$);}
381 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
382 call = init_free_call(F->text, vars);
385 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY.
386 %type impl_type {impl_type*}
387 %destructor impl_type {free_impl_type($$);}
388 impl_type(type_) ::= impl_type_token(T). {
389 type_ = init_impl_type(T->type, T->text);
393 %type reference {char}
394 reference(r) ::= . {r = 0;}
395 reference(r) ::= REFERENCE. {r = 1;}
397 %type indirection {unsigned}
398 indirection(i) ::= . {i = 0;}
399 indirection(i) ::= pointers(p). {i = p;}
401 %type pointers {unsigned}
402 pointers(p) ::= POINTER. {p = 1;}
403 pointers(p) ::= pointers(P) POINTER. {p = P+1;}