10 static void syntax_error(const char *fn, size_t ln, const char *msg, ...) {
11 fprintf(stderr, "WARNING: Syntax error on line %zu in '%s'%s", ln, fn, msg ? ": ": "\n");
16 vfprintf(stderr, msg, argv);
25 %token_type {PSI_Token *}
26 %token_destructor {free($$);}
27 %extra_argument {PSI_Lexer *L}
28 /* TOKEN is defined inside syntax_error */
30 syntax_error(L->fn, L->line, "Unexpected token '%s'.\n", TOKEN->text);
35 blocks ::= blocks block.
39 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
41 syntax_error(L->fn, T->line, "Extra 'lib %s' statement has no effect.\n", libname->text);
43 L->lib = strndup(libname->text + 1, libname->size - 2);
49 block ::= decl(decl). {
50 L->decls = add_decl(L->decls, decl);
52 block ::= impl(impl). {
53 L->impls = add_impl(L->impls, impl);
55 block ::= decl_typedef(def). {
56 L->defs = add_decl_typedef(L->defs, def);
59 %type decl_typedef {decl_typedef*}
60 decl_typedef(def) ::= TYPEDEF NAME(ALIAS) decl_type(type) EOS. {
61 def = init_decl_typedef(ALIAS->text, type);
66 decl(decl) ::= decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
67 decl = init_decl(func, args);
69 %type decl_var {decl_var*}
70 decl_var(var) ::= NAME(T). {
71 var = init_decl_var(T->text, 0);
74 decl_var(var) ::= pointers(p) NAME(T). {
75 var = init_decl_var(T->text, p);
79 %type decl_vars {decl_vars*}
80 decl_vars(vars) ::= decl_var(var). {
81 vars = init_decl_vars(var);
83 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
84 vars = add_decl_var(vars_, var);
87 %type decl_arg {decl_arg*}
88 decl_arg(arg) ::= decl_type(type) decl_var(var). {
89 arg = init_decl_arg(type, var);
92 %type decl_args {decl_args*}
93 decl_args(args) ::= decl_arg(arg). {
94 args = init_decl_args(arg);
96 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
97 args = add_decl_arg(args_, arg);
100 %type decl_type {decl_type*}
101 decl_type(type_) ::= VOID(T). {
102 type_ = init_decl_type(T->type, T->text);
105 decl_type(type_) ::= INT(T). {
106 type_ = init_decl_type(T->type, T->text);
109 decl_type(type_) ::= FLOAT(T). {
110 type_ = init_decl_type(T->type, T->text);
113 decl_type(type_) ::= DOUBLE(T). {
114 type_ = init_decl_type(T->type, T->text);
117 decl_type(type_) ::= SINT8(T). {
118 type_ = init_decl_type(T->type, T->text);
121 decl_type(type_) ::= UINT8(T). {
122 type_ = init_decl_type(T->type, T->text);
125 decl_type(type_) ::= SINT16(T). {
126 type_ = init_decl_type(T->type, T->text);
129 decl_type(type_) ::= UINT16(T). {
130 type_ = init_decl_type(T->type, T->text);
133 decl_type(type_) ::= SINT32(T). {
134 type_ = init_decl_type(T->type, T->text);
137 decl_type(type_) ::= UINT32(T). {
138 type_ = init_decl_type(T->type, T->text);
141 decl_type(type_) ::= SINT64(T). {
142 type_ = init_decl_type(T->type, T->text);
145 decl_type(type_) ::= UINT64(T). {
146 type_ = init_decl_type(T->type, T->text);
149 decl_type(type_) ::= NAME(T). {
150 type_ = init_decl_type(T->type, T->text);
155 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
156 impl = init_impl(func, stmts);
159 %type impl_func {impl_func*}
160 impl_func(func) ::= FUNCTION NSNAME(NAME) LPAREN impl_args(args) RPAREN COLON impl_type(type). {
161 func = init_impl_func(NAME->text, args, type);
164 impl_func(func) ::= FUNCTION NSNAME(NAME) LPAREN RPAREN COLON impl_type(type). {
165 func = init_impl_func(NAME->text, NULL, type);
169 %type impl_def_val {impl_def_val*}
170 impl_def_val(def) ::= NULL. {
172 def = init_impl_def_val();
174 impl_def_val(def) ::= number. {
176 def = init_impl_def_val();
179 %type impl_var {impl_var*}
180 impl_var(var) ::= DOLLAR NAME(T). {
181 var = init_impl_var(T->text, 0);
184 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
185 var = init_impl_var(T->text, 1);
189 %type impl_arg {impl_arg*}
190 impl_arg(arg) ::= impl_type(type) impl_var(var). {
191 arg = init_impl_arg(type, var, NULL);
193 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
194 arg = init_impl_arg(type, var, def);
197 %type impl_args {impl_args*}
198 impl_args(args) ::= impl_arg(arg). {
199 args = init_impl_args(arg);
201 impl_args(args) ::= impl_args(args_) COMMA impl_arg(arg). {
202 args = add_impl_arg(args_, arg);
205 %type impl_stmts {impl_stmts*}
206 impl_stmts(stmts) ::= impl_stmt(stmt). {
207 stmts = init_impl_stmts(stmt);
209 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
210 stmts = add_impl_stmt(stmts_, stmt);
213 %type impl_stmt {impl_stmt*}
214 impl_stmt(stmt) ::= let_stmt(let). {
215 stmt = init_impl_stmt(PSI_T_LET, let);
217 impl_stmt(stmt) ::= set_stmt(set). {
218 stmt = init_impl_stmt(PSI_T_SET, set);
220 impl_stmt(stmt) ::= ret_stmt(ret). {
221 stmt = init_impl_stmt(PSI_T_RET, ret);
224 %type let_stmt {let_stmt*}
225 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
226 let = init_let_stmt(var, val);
229 %type let_value {let_value*}
230 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
231 val = init_let_value(func, var, 0);
233 let_value(val) ::= REFERENCE NULL. {
234 val = init_let_value(NULL, NULL, 1);
236 let_value(val) ::= NULL. {
237 val = init_let_value(NULL, NULL, 0);
240 %type let_func {let_func*}
241 let_func(func) ::= STRVAL(T). {
242 func = init_let_func(T->type, T->text);
245 let_func(func) ::= INTVAL(T). {
246 func = init_let_func(T->type, T->text);
249 let_func(func) ::= FLOATVAL(T). {
250 func = init_let_func(T->type, T->text);
253 let_func(func) ::= BOOLVAL(T). {
254 func = init_let_func(T->type, T->text);
258 %type set_stmt {set_stmt*}
259 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
260 set = init_set_stmt(var, val);
263 %type set_value {set_value*}
264 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
265 val = init_set_value(func, vars);
268 %type set_func {set_func*}
269 set_func(func) ::= TO_STRING(T). {
270 func = init_set_func(T->type, T->text);
273 set_func(func) ::= TO_INT(T). {
274 func = init_set_func(T->type, T->text);
277 set_func(func) ::= TO_FLOAT(T). {
278 func = init_set_func(T->type, T->text);
281 set_func(func) ::= TO_BOOL(T). {
282 func = init_set_func(T->type, T->text);
286 %type ret_stmt {ret_stmt*}
287 ret_stmt(ret) ::= RET set_func(func) LPAREN decl_var(var) RPAREN EOS. {
288 ret = init_ret_stmt(func, var);
291 %type impl_type {impl_type*}
292 impl_type(type_) ::= VOID(T). {
293 type_ = init_impl_type(T->type, T->text);
296 impl_type(type_) ::= MIXED(T). {
297 type_ = init_impl_type(T->type, T->text);
300 impl_type(type_) ::= BOOL(T). {
301 type_ = init_impl_type(T->type, T->text);
304 impl_type(type_) ::= INT(T). {
305 type_ = init_impl_type(T->type, T->text);
308 impl_type(type_) ::= FLOAT(T). {
309 type_ = init_impl_type(T->type, T->text);
312 impl_type(type_) ::= STRING(T). {
313 type_ = init_impl_type(T->type, T->text);
316 impl_type(type_) ::= ARRAY(T). {
317 type_ = init_impl_type(T->type, T->text);
322 digits ::= digits DIGIT.
323 decimals ::= digits DOT digits.
324 decimals ::= DOT digits.
325 decimals ::= digits DOT.
327 number ::= PLUS digits.
328 number ::= MINUS digits.
330 number ::= MINUS decimals.
331 number ::= PLUS decimals.
333 %type pointers {unsigned}
334 pointers(p) ::= POINTER. {++p;}
335 pointers(p) ::= pointers(P) POINTER. {p = ++P;}