init
[m6w6/ext-psi] / idl / parser.y
1 %include {
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "lexer.h"
6 #include "parser.h"
7 #include "types.h"
8 }
9
10 %name PSI_Parser
11 %token_prefix PSI_T_
12 %token_type {PSI_Token *}
13 %token_destructor {free($$);}
14 %extra_argument {PSI_Lexer *L}
15 %syntax_error {
16 printf("ERROR: Syntax error on line %zu in '%s': '%s...'\n", L->line, L->fn, L->tok);
17 exit(1);
18 }
19
20 file ::= blocks.
21
22 blocks ::= block.
23 blocks ::= blocks block.
24
25 block ::= decl(decl_). {
26 L->decl.list = realloc(L->decl.list, ++L->decl.count * sizeof(*L->decl.list));
27 L->decl.list[L->decl.count-1] = decl_;
28 }
29 block ::= impl(impl_). {
30 L->impl.list = realloc(L->impl.list, ++L->impl.count * sizeof(*L->impl.list));
31 L->impl.list[L->impl.count-1] = impl_;
32 }
33 block ::= COMMENT.
34
35 %type decl {decl*}
36 decl(decl) ::= decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
37 decl = init_decl(func, args);
38 }
39 %type decl_var {decl_var*}
40 decl_var(var) ::= NAME(T). {
41 var = init_decl_var(T->text, 0);
42 free(T);
43 }
44 decl_var(var) ::= pointers(p) NAME(T). {
45 var = init_decl_var(T->text, p);
46 free(T);
47 }
48
49 %type decl_vars {decl_vars*}
50 decl_vars(vars) ::= decl_var(var). {
51 vars = init_decl_vars(var);
52 }
53 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
54 vars = add_decl_var(vars_, var);
55 }
56
57 %type decl_arg {decl_arg*}
58 decl_arg(arg) ::= decl_type(type) decl_var(var). {
59 arg = init_decl_arg(type, var);
60 }
61
62 %type decl_args {decl_args*}
63 decl_args(args) ::= decl_arg(arg). {
64 args = init_decl_args(arg);
65 }
66 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
67 args = add_decl_arg(args_, arg);
68 }
69
70 %type decl_type {decl_type*}
71 decl_type(type_) ::= VOID(T). {
72 type_ = init_decl_type(T->type, T->text);
73 free(T);
74 }
75 decl_type(type_) ::= INT(T). {
76 type_ = init_decl_type(T->type, T->text);
77 free(T);
78 }
79 decl_type(type_) ::= FLOAT(T). {
80 type_ = init_decl_type(T->type, T->text);
81 free(T);
82 }
83 decl_type(type_) ::= DOUBLE(T). {
84 type_ = init_decl_type(T->type, T->text);
85 free(T);
86 }
87 decl_type(type_) ::= SINT8(T). {
88 type_ = init_decl_type(T->type, T->text);
89 free(T);
90 }
91 decl_type(type_) ::= UINT8(T). {
92 type_ = init_decl_type(T->type, T->text);
93 free(T);
94 }
95
96 %type impl {impl*}
97 impl(impl) ::= impl_func(func) LCURLY impl_stmts(stmts) RCURLY. {
98 impl = init_impl(func, stmts);
99 }
100
101 %type impl_func {impl_func*}
102 impl_func(func) ::= FUNCTION NSNAME(NAME) LPAREN impl_args(args) RPAREN COLON impl_type(type). {
103 func = init_impl_func(NAME->text, args, type);
104 free(NAME);
105 }
106
107 %type impl_def_val {impl_def_val*}
108 impl_def_val(def) ::= NULL. {
109 /* FIXME */
110 def = init_impl_def_val();
111 }
112 impl_def_val(def) ::= number. {
113 /* FIXME */
114 def = init_impl_def_val();
115 }
116
117 %type impl_var {impl_var*}
118 impl_var(var) ::= DOLLAR NAME(T). {
119 var = init_impl_var(T->text, 0);
120 free(T);
121 }
122 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
123 var = init_impl_var(T->text, 1);
124 free(T);
125 }
126
127 %type impl_arg {impl_arg*}
128 impl_arg(arg) ::= impl_type(type) impl_var(var). {
129 arg = init_impl_arg(type, var, NULL);
130 }
131 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
132 arg = init_impl_arg(type, var, def);
133 }
134
135 %type impl_args {impl_args*}
136 impl_args(args) ::= impl_arg(arg). {
137 args = init_impl_args(arg);
138 }
139 impl_args(args) ::= impl_args(args_) COMMA impl_arg(arg). {
140 args = add_impl_arg(args_, arg);
141 }
142
143 %type impl_stmts {impl_stmts*}
144 impl_stmts(stmts) ::= impl_stmt(stmt). {
145 stmts = init_impl_stmts(stmt);
146 }
147 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
148 stmts = add_impl_stmt(stmts_, stmt);
149 }
150
151 %type impl_stmt {impl_stmt*}
152 impl_stmt(stmt) ::= let_stmt(let). {
153 stmt = init_impl_stmt(PSI_T_LET, let);
154 }
155 impl_stmt(stmt) ::= set_stmt(set). {
156 stmt = init_impl_stmt(PSI_T_SET, set);
157 }
158 impl_stmt(stmt) ::= ret_stmt(ret). {
159 stmt = init_impl_stmt(PSI_T_RET, ret);
160 }
161
162 %type let_stmt {let_stmt*}
163 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
164 let = init_let_stmt(var, val);
165 }
166
167 %type let_value {let_value*}
168 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
169 val = init_let_value(func, var);
170 }
171 let_value(val) ::= let_reference_null_pointer. {
172 val = init_let_value(NULL, NULL);
173 }
174
175 let_reference_null_pointer ::= REFERENCE NULL.
176
177 %type let_func {let_func*}
178 let_func(func) ::= STRVAL(T). {
179 func = init_let_func(T->type, T->text);
180 free(T);
181 }
182 let_func(func) ::= INTVAL(T). {
183 func = init_let_func(T->type, T->text);
184 free(T);
185 }
186 let_func(func) ::= FLOATVAL(T). {
187 func = init_let_func(T->type, T->text);
188 free(T);
189 }
190 let_func(func) ::= BOOLVAL(T). {
191 func = init_let_func(T->type, T->text);
192 free(T);
193 }
194
195 %type set_stmt {set_stmt*}
196 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
197 set = init_set_stmt(var, val);
198 }
199
200 %type set_value {set_value*}
201 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
202 val = init_set_value(func, vars);
203 }
204
205 %type set_func {set_func*}
206 set_func(func) ::= TO_STRING(T). {
207 func = init_set_func(T->type, T->text);
208 free(T);
209 }
210 set_func(func) ::= TO_INT(T). {
211 func = init_set_func(T->type, T->text);
212 free(T);
213 }
214 set_func(func) ::= TO_FLOAT(T). {
215 func = init_set_func(T->type, T->text);
216 free(T);
217 }
218 set_func(func) ::= TO_BOOL(T). {
219 func = init_set_func(T->type, T->text);
220 free(T);
221 }
222
223 %type ret_stmt {ret_stmt*}
224 ret_stmt(ret) ::= RET set_func(func) LPAREN decl_var(var) RPAREN EOS. {
225 ret = init_ret_stmt(func, var);
226 }
227
228 %type impl_type {impl_type*}
229 impl_type(type_) ::= VOID(T). {
230 type_ = init_impl_type(T->type, T->text);
231 free(T);
232 }
233 impl_type(type_) ::= MIXED(T). {
234 type_ = init_impl_type(T->type, T->text);
235 free(T);
236 }
237 impl_type(type_) ::= BOOL(T). {
238 type_ = init_impl_type(T->type, T->text);
239 free(T);
240 }
241 impl_type(type_) ::= INT(T). {
242 type_ = init_impl_type(T->type, T->text);
243 free(T);
244 }
245 impl_type(type_) ::= FLOAT(T). {
246 type_ = init_impl_type(T->type, T->text);
247 free(T);
248 }
249 impl_type(type_) ::= STRING(T). {
250 type_ = init_impl_type(T->type, T->text);
251 free(T);
252 }
253 impl_type(type_) ::= ARRAY(T). {
254 type_ = init_impl_type(T->type, T->text);
255 free(T);
256 }
257
258 digits ::= DIGIT.
259 digits ::= digits DIGIT.
260 decimals ::= digits DOT digits.
261 decimals ::= DOT digits.
262 decimals ::= digits DOT.
263 number ::= digits.
264 number ::= PLUS digits.
265 number ::= MINUS digits.
266 number ::= decimals.
267 number ::= MINUS decimals.
268 number ::= PLUS decimals.
269
270 %type pointers {unsigned}
271 pointers(p) ::= POINTER. {++p;}
272 pointers(p) ::= pointers(P) POINTER. {p = ++P;}