initial commit.
[bosc.git] / src / parser.y
blobbbb69b61c71fde3e59d46c874e7e556b288b546b
1 %{
2 #include <cstdio>
3 #include <map>
4 #include <stack>
6 #include "ast.h"
8 using namespace std;
9 extern "C"
11 extern char *curfile;
12 extern int lineno;
13 int yyparse(void);
14 int yylex(void);
15 int yywrap()
17 return 1;
19 void yyerror(const char* str)
21 fprintf(stderr, "%s:%d: %s\n", curfile, lineno, str);
26 %token SET_SIGNAL_MASK RAISE_SIGNAL DO_TURN DO_MOVE DO_SPIN
27 %token ACCELERATE SPEED ALONG AROUND TO SPEED_NOW EXPLODE_TYPE EXPLODE
28 %token DECL_STATIC_VAR DECL_VAR DECL_PIECE EMIT_SFX EMIT_FROM
29 %token START_SCRIPT CALL_SCRIPT RANDOM_NUMBER
30 %token SHADE DONT_SHADE CACHE DONT_CACHE SHOW HIDE
31 %token COB_GET COB_SET COB_SLEEP
32 %token WAIT_FOR_MOVE WAIT_FOR_TURN STOP_SPIN
33 %token ATTACH_UNIT DROP_UNIT
34 %token LINENO PRAGMA
35 %token BOOL_AND BOOL_OR
36 %token PLAY_SOUND CONTINUE BREAK RETURN GOTO
38 %token CMP_EQ CMP_GT CMP_LT CMP_GTE CMP_LTE CMP_NEQ
39 %token INCREMENT DECREMENT FOR WHILE IF ELSE DO SHIFT_LEFT SHIFT_RIGHT
41 %union {
42 double number;
43 char *string;
46 %token <string> IDENTIFIER
47 %token <number> NUMBER
48 %token <number> AXIS
50 %start translation_unit
51 %error-verbose
54 primary_expression
55 : IDENTIFIER
56 | NUMBER
57 // | STRING_LITERAL
58 | '(' expression ')'
59 | rand_expression
60 | get_expression
63 postfix_expression
64 : primary_expression
65 | postfix_expression '(' ')'
66 | postfix_expression '(' argument_expression_list ')'
67 | postfix_expression INCREMENT
68 | postfix_expression DECREMENT
71 get_expression
72 : COB_GET assignment_expression
73 | COB_GET assignment_expression '(' ')'
74 | COB_GET assignment_expression '(' assignment_expression ')'
75 | COB_GET assignment_expression '(' assignment_expression ','
76 assignment_expression ')'
77 | COB_GET assignment_expression '(' assignment_expression ','
78 assignment_expression ',' assignment_expression ')'
79 | COB_GET assignment_expression '(' assignment_expression ','
80 assignment_expression ',' assignment_expression ','
81 assignment_expression ')'
84 rand_expression
85 : RANDOM_NUMBER '(' assignment_expression ',' assignment_expression ')'
88 argument_expression_list
89 : assignment_expression
90 | argument_expression_list ',' assignment_expression
93 unary_expression
94 : postfix_expression
95 | INCREMENT unary_expression
96 | DECREMENT unary_expression
97 | unary_operator unary_expression
100 unary_operator
101 : '+'
102 | '-'
103 | '~'
104 | '!'
107 multiplicative_expression
108 : unary_expression
109 | multiplicative_expression '*' unary_expression
110 | multiplicative_expression '/' unary_expression
111 | multiplicative_expression '%' unary_expression
114 additive_expression
115 : multiplicative_expression
116 | additive_expression '+' multiplicative_expression
117 | additive_expression '-' multiplicative_expression
120 shift_expression
121 : additive_expression
122 | shift_expression SHIFT_LEFT additive_expression
123 | shift_expression SHIFT_RIGHT additive_expression
126 relational_expression
127 : shift_expression
128 | relational_expression '<' shift_expression
129 | relational_expression '>' shift_expression
130 | relational_expression CMP_GTE shift_expression
131 | relational_expression CMP_LTE shift_expression
134 equality_expression
135 : relational_expression
136 | equality_expression CMP_EQ relational_expression
137 | equality_expression CMP_NEQ relational_expression
140 and_expression
141 : equality_expression
142 | and_expression '&' equality_expression
145 exclusive_or_expression
146 : and_expression
147 | exclusive_or_expression '^' and_expression
150 inclusive_or_expression
151 : exclusive_or_expression
152 | inclusive_or_expression '|' exclusive_or_expression
155 logical_and_expression
156 : inclusive_or_expression
157 | logical_and_expression BOOL_AND inclusive_or_expression
160 logical_or_expression
161 : logical_and_expression
162 | logical_or_expression BOOL_OR logical_and_expression
165 conditional_expression
166 : logical_or_expression
167 | logical_or_expression '?' expression ':' conditional_expression
170 assignment_expression
171 : conditional_expression
172 | unary_expression assignment_operator assignment_expression
175 assignment_operator
176 : '='
178 | MUL_ASSIGN
179 | DIV_ASSIGN
180 | MOD_ASSIGN
181 | ADD_ASSIGN
182 | SUB_ASSIGN
183 | LEFT_ASSIGN
184 | RIGHT_ASSIGN
185 | AND_ASSIGN
186 | XOR_ASSIGN
187 | OR_ASSIGN
191 expression
192 : assignment_expression
193 | expression ',' assignment_expression
196 constant_expression
197 : conditional_expression
200 declaration
201 : declaration_specifiers ';'
202 | declaration_specifiers init_declarator_list ';'
205 declaration_specifiers
206 : type_specifier
207 | type_specifier declaration_specifiers
210 init_declarator_list
211 : init_declarator
212 | init_declarator_list ',' init_declarator
215 init_declarator
216 : declarator
217 | declarator '=' initializer
220 type_specifier
221 : DECL_STATIC_VAR
222 | DECL_VAR
223 | DECL_PIECE
226 specifier_qualifier_list
227 : type_specifier specifier_qualifier_list
228 | type_specifier
231 declarator
232 : direct_declarator
235 direct_declarator
236 : IDENTIFIER
237 | '(' declarator ')'
238 | direct_declarator '(' parameter_type_list ')'
239 | direct_declarator '(' identifier_list ')'
240 | direct_declarator '(' ')'
243 parameter_type_list
244 : parameter_list
247 parameter_list
248 : parameter_declaration
249 | parameter_list ',' parameter_declaration
252 parameter_declaration
253 : declaration_specifiers declarator
254 | declaration_specifiers direct_abstract_declarator
255 | declaration_specifiers
258 identifier_list
259 : IDENTIFIER
260 | identifier_list ',' IDENTIFIER
263 type_name
264 : specifier_qualifier_list
265 | specifier_qualifier_list direct_abstract_declarator
268 direct_abstract_declarator
269 : '(' direct_abstract_declarator ')'
270 | '(' ')'
271 | '(' parameter_type_list ')'
272 | direct_abstract_declarator '(' ')'
273 | direct_abstract_declarator '(' parameter_type_list ')'
276 initializer
277 : assignment_expression
280 statement
281 : labeled_statement
282 | compound_statement
283 | expression_statement
284 | selection_statement
285 | iteration_statement
286 | jump_statement
287 | set_statement
288 | move_statement
289 | spin_statement
290 | wait_statement
291 | sleep_statement
292 | emit_sfx_statement
293 | script_statement
294 | misc_statement // cache, shade
295 | attach_statement
296 | visibility_statement
297 | explode_statement
298 | signal_statement
300 | play_sound_statement
305 ///////////////////////////////////////////////////////////////////////
306 // spring ops
307 set_statement
308 : COB_SET expression TO expression
311 move_statement
312 : DO_MOVE expression TO AXIS expression SPEED_NOW
313 | DO_MOVE expression TO AXIS expression SPEED expression
314 | DO_MOVE expression TO AXIS expression SPEED expression ACCELERATE expression
315 | DO_TURN expression TO AXIS expression SPEED_NOW
316 | DO_TURN expression TO AXIS expression SPEED expression
317 | DO_TURN expression TO AXIS expression SPEED expression ACCELERATE expression
320 spin_statement
321 : DO_SPIN expression AROUND AXIS SPEED expression
322 | DO_SPIN expression AROUND AXIS SPEED expression ACCELERATE expression
323 | STOP_SPIN expression
324 | STOP_SPIN expression AROUND AXIS
327 wait_statement
328 : WAIT_FOR_MOVE expression ALONG AXIS
329 | WAIT_FOR_TURN expression AROUND AXIS
332 sleep_statement
333 : COB_SLEEP expression
336 emit_sfx_statement
337 : EMIT_SFX expression EMIT_FROM expression
340 script_statement
341 : CALL_SCRIPT IDENTIFIER '(' argument_expression_list ')'
342 | CALL_SCRIPT IDENTIFIER '(' ')'
343 | START_SCRIPT IDENTIFIER '(' argument_expression_list ')'
344 | START_SCRIPT IDENTIFIER '(' ')'
347 // mostly no-ops in spring
348 misc_statement
349 : CACHE expression
350 | DONT_CACHE expression
351 | SHADE expression
352 | DONT_SHADE expression
355 attach_statement
356 : ATTACH_UNIT expression TO expression
357 | DROP_UNIT expression
360 visibility_statement
361 : SHOW expression
362 | HIDE expression
365 signal_statement
366 : RAISE_SIGNAL expression
367 | SET_SIGNAL_MASK expression
370 explode_statement
371 : EXPLODE expression EXPLODE_TYPE expression
374 ///////////////////////////////////////////////////////////////////////
376 labeled_statement
377 : IDENTIFIER ':' statement
379 | CASE constant_expression ':' statement
380 | DEFAULT ':' statement
384 compound_statement
385 : '{' '}'
386 | '{' statement_list '}'
387 | '{' declaration_list '}'
388 | '{' declaration_list statement_list '}'
391 declaration_list
392 : declaration
393 | declaration_list declaration
396 statement_list
397 : statement
398 | statement_list statement
399 | statement_list declaration_list
400 | statement_list declaration_list statement
403 expression_statement
404 : ';'
405 | expression ';'
408 selection_statement
409 : IF '(' expression ')' statement
410 | IF '(' expression ')' statement ELSE statement
411 /*| SWITCH '(' expression ')' statement*/
414 iteration_statement
415 : WHILE '(' expression ')' statement
416 | DO statement WHILE '(' expression ')' ';'
417 | FOR '(' expression_statement expression_statement ')' statement
418 | FOR '(' expression_statement expression_statement expression ')' statement
419 // scriptor compatibility
420 | FOR '(' expression_statement expression_statement expression_statement ')' statement
423 jump_statement
424 : GOTO IDENTIFIER ';'
425 | CONTINUE ';'
426 | BREAK ';'
427 | RETURN ';'
428 | RETURN expression ';'
431 translation_unit
432 : external_declaration
433 | translation_unit external_declaration
434 | /* empty */
437 external_declaration
438 : function_definition
439 | declaration
440 | pragma
443 function_definition
444 : declaration_specifiers declarator declaration_list compound_statement
445 | declaration_specifiers declarator compound_statement
446 | declarator declaration_list compound_statement
447 | declarator compound_statement
450 pragma
451 : PRAGMA IDENTIFIER NUMBER
452 | PRAGMA IDENTIFIER IDENTIFIER
453 | PRAGMA IDENTIFIER