started adding AST, modified grammar a bit, changed logging.
[bosc.git] / src / parser.y
blob385e8139ec6b8d8ba52f38963adc78290140badb
1 %{
2 #include <cstdio>
3 #include <map>
4 #include <stack>
6 #include "logging.h"
7 #include "ast.h"
9 using namespace std;
10 extern "C"
12 extern char *curfile;
13 extern int lineno;
14 int yyparse(void);
15 int yylex(void);
16 int yywrap()
18 return 1;
20 void yyerror(const char* str)
22 fprintf(stderr, "%s:%d: %s\n", curfile, lineno, str);
27 %token SET_SIGNAL_MASK RAISE_SIGNAL DO_TURN DO_MOVE DO_SPIN
28 %token ACCELERATE SPEED ALONG AROUND TO SPEED_NOW EXPLODE_TYPE EXPLODE
29 %token DECL_STATIC_VAR DECL_VAR DECL_PIECE EMIT_SFX EMIT_FROM
30 %token START_SCRIPT CALL_SCRIPT RANDOM_NUMBER
31 %token SHADE DONT_SHADE CACHE DONT_CACHE SHOW HIDE
32 %token COB_GET COB_SET COB_SLEEP
33 %token WAIT_FOR_MOVE WAIT_FOR_TURN STOP_SPIN
34 %token ATTACH_UNIT DROP_UNIT
35 %token LINENO PRAGMA
36 %token BOOL_AND BOOL_OR
37 %token PLAY_SOUND CONTINUE BREAK RETURN GOTO
39 %token CMP_EQ CMP_GT CMP_LT CMP_GTE CMP_LTE CMP_NEQ
40 %token INCREMENT DECREMENT FOR WHILE IF ELSE DO SHIFT_LEFT SHIFT_RIGHT
42 %union {
43 ASTNode *ast;
44 double number;
45 char *string;
48 %token <string> IDENTIFIER
49 %token <number> NUMBER
50 %token <number> AXIS
52 %type <ast> expression primary_expression postfix_expression get_expression
53 %type <ast> rand_expression unary_expression additive_expression
54 %type <ast> multiplicative_expression shift_expression
55 %type <ast> relational_expression equality_expression
56 %type <ast> exclusive_or_expression inclusive_or_expression
57 %type <ast> and_expression logical_and_expression
58 %type <ast> logical_or_expression assignment_expression
59 %type <ast> argument_expression_list conditional_expression
61 %start translation_unit
62 %error-verbose
65 primary_expression
66 : IDENTIFIER { $$ = new Ident($1); }
67 | NUMBER { $$ = new Number($1); }
68 // | STRING_LITERAL
69 | '(' expression ')' { $$ = $2; }
70 | rand_expression { $$ = $1; }
71 | get_expression { $$ = $1;
72 LOG("get_expr"); }
75 postfix_expression
76 : primary_expression { $$ = $1; }
77 | postfix_expression '(' ')' { $$ = new ASTNode(); }
78 | postfix_expression '(' argument_expression_list ')' { $$ = new ASTNode(); }
79 | postfix_expression INCREMENT { $$ = new ASTNode(); }
80 | postfix_expression DECREMENT { $$ = new ASTNode(); }
83 get_expression
84 : COB_GET assignment_expression { $$ = new ASTNode(); }
85 | COB_GET assignment_expression '(' ')' { $$ = new ASTNode(); }
86 | COB_GET assignment_expression '(' assignment_expression ')' { $$ = new ASTNode(); }
87 | COB_GET assignment_expression '(' assignment_expression ','
88 assignment_expression ')' { $$ = new ASTNode(); }
89 | COB_GET assignment_expression '(' assignment_expression ','
90 assignment_expression ',' assignment_expression ')'
91 { $$ = new ASTNode(); }
92 | COB_GET assignment_expression '(' assignment_expression ','
93 assignment_expression ',' assignment_expression ','
94 assignment_expression ')' { $$ = new ASTNode(); }
97 rand_expression
98 : RANDOM_NUMBER '(' assignment_expression ',' assignment_expression ')'
99 { $$ = new ASTNode(); }
102 argument_expression_list
103 : assignment_expression { $$ = $1; }
104 | argument_expression_list ',' assignment_expression
107 unary_expression
108 : postfix_expression { $$ = $1; }
109 | INCREMENT unary_expression { $$ = new ASTNode(); }
110 | DECREMENT unary_expression { $$ = new ASTNode(); }
111 | '+' unary_expression { $$ = new ASTNode(); }
112 | '-' unary_expression { $$ = new ASTNode(); }
113 | '!' unary_expression { $$ = new ASTNode(); }
114 | '~' unary_expression { $$ = new ASTNode(); }
117 multiplicative_expression
118 : unary_expression { $$ = $1; }
119 | multiplicative_expression '*' unary_expression { $$ = new ASTNode(); }
120 | multiplicative_expression '/' unary_expression { $$ = new ASTNode(); }
121 | multiplicative_expression '%' unary_expression { $$ = new ASTNode(); }
124 additive_expression
125 : multiplicative_expression { $$ = $1; }
126 | additive_expression '+' multiplicative_expression { $$ = new ASTNode(); }
127 | additive_expression '-' multiplicative_expression { $$ = new ASTNode(); }
130 shift_expression
131 : additive_expression
132 | shift_expression SHIFT_LEFT additive_expression { $$ = new ASTNode(); }
133 | shift_expression SHIFT_RIGHT additive_expression { $$ = new ASTNode(); }
136 relational_expression
137 : shift_expression
138 | relational_expression '<' shift_expression { $$ = new ASTNode(); }
139 | relational_expression '>' shift_expression { $$ = new ASTNode(); }
140 | relational_expression CMP_GTE shift_expression { $$ = new ASTNode(); }
141 | relational_expression CMP_LTE shift_expression { $$ = new ASTNode(); }
144 equality_expression
145 : relational_expression
146 | equality_expression CMP_EQ relational_expression { $$ = new ASTNode(); }
147 | equality_expression CMP_NEQ relational_expression { $$ = new ASTNode(); }
150 and_expression
151 : equality_expression
152 | and_expression '&' equality_expression { $$ = new ASTNode(); }
155 exclusive_or_expression
156 : and_expression
157 | exclusive_or_expression '^' and_expression { $$ = new ASTNode(); }
160 inclusive_or_expression
161 : exclusive_or_expression
162 | inclusive_or_expression '|' exclusive_or_expression { $$ = new ASTNode(); }
165 logical_and_expression
166 : inclusive_or_expression
167 | logical_and_expression BOOL_AND inclusive_or_expression { $$ = new ASTNode(); }
170 logical_or_expression
171 : logical_and_expression
172 | logical_or_expression BOOL_OR logical_and_expression { $$ = new ASTNode(); }
175 conditional_expression
176 : logical_or_expression
177 | logical_or_expression '?' expression ':' conditional_expression
178 { $$ = new ASTNode(); }
181 assignment_expression
182 : conditional_expression
183 | unary_expression '=' assignment_expression
184 { $$ = new ASTNode(); }
188 assignment_operator
189 : '='
190 | MUL_ASSIGN
191 | DIV_ASSIGN
192 | MOD_ASSIGN
193 | ADD_ASSIGN
194 | SUB_ASSIGN
195 | LEFT_ASSIGN
196 | RIGHT_ASSIGN
197 | AND_ASSIGN
198 | XOR_ASSIGN
199 | OR_ASSIGN
203 expression
204 : assignment_expression
205 | expression ',' assignment_expression
208 constant_expression
209 : conditional_expression
212 declaration
213 : declaration_specifiers ';'
214 | declaration_specifiers init_declarator_list ';'
217 declaration_specifiers
218 : type_specifier
219 | type_specifier declaration_specifiers
222 init_declarator_list
223 : init_declarator
224 | init_declarator_list ',' init_declarator
227 init_declarator
228 : declarator
229 | declarator '=' initializer
232 type_specifier
233 : DECL_STATIC_VAR
234 | DECL_VAR
235 | DECL_PIECE
238 specifier_qualifier_list
239 : type_specifier specifier_qualifier_list
240 | type_specifier
243 declarator
244 : direct_declarator
247 direct_declarator
248 : IDENTIFIER
249 | '(' declarator ')'
250 | direct_declarator '(' parameter_type_list ')'
251 | direct_declarator '(' identifier_list ')'
252 | direct_declarator '(' ')'
255 parameter_type_list
256 : parameter_list
259 parameter_list
260 : parameter_declaration
261 | parameter_list ',' parameter_declaration
264 parameter_declaration
265 : declaration_specifiers declarator
266 | declaration_specifiers direct_abstract_declarator
267 | declaration_specifiers
270 identifier_list
271 : IDENTIFIER
272 | identifier_list ',' IDENTIFIER
275 type_name
276 : specifier_qualifier_list
277 | specifier_qualifier_list direct_abstract_declarator
280 direct_abstract_declarator
281 : '(' direct_abstract_declarator ')'
282 | '(' ')'
283 | '(' parameter_type_list ')'
284 | direct_abstract_declarator '(' ')'
285 | direct_abstract_declarator '(' parameter_type_list ')'
288 initializer
289 : assignment_expression
292 statement
293 : labeled_statement
294 | compound_statement
295 | expression_statement
296 | selection_statement
297 | iteration_statement
298 | jump_statement
299 | set_statement
300 | move_statement
301 | spin_statement
302 | wait_statement
303 | sleep_statement
304 | emit_sfx_statement
305 | script_statement
306 | misc_statement // cache, shade
307 | attach_statement
308 | visibility_statement
309 | explode_statement
310 | signal_statement
312 | play_sound_statement
317 ///////////////////////////////////////////////////////////////////////
318 // spring ops
319 set_statement
320 : COB_SET expression TO expression
323 move_statement
324 : DO_MOVE expression TO AXIS expression SPEED_NOW
325 | DO_MOVE expression TO AXIS expression SPEED expression
326 | DO_MOVE expression TO AXIS expression SPEED expression ACCELERATE expression
327 | DO_TURN expression TO AXIS expression SPEED_NOW
328 | DO_TURN expression TO AXIS expression SPEED expression
329 | DO_TURN expression TO AXIS expression SPEED expression ACCELERATE expression
332 spin_statement
333 : DO_SPIN expression AROUND AXIS SPEED expression
334 | DO_SPIN expression AROUND AXIS SPEED expression ACCELERATE expression
335 | STOP_SPIN expression
336 | STOP_SPIN expression AROUND AXIS
339 wait_statement
340 : WAIT_FOR_MOVE expression ALONG AXIS
341 | WAIT_FOR_TURN expression AROUND AXIS
344 sleep_statement
345 : COB_SLEEP expression
348 emit_sfx_statement
349 : EMIT_SFX expression EMIT_FROM expression
352 script_statement
353 : CALL_SCRIPT IDENTIFIER '(' argument_expression_list ')'
354 | CALL_SCRIPT IDENTIFIER '(' ')'
355 | START_SCRIPT IDENTIFIER '(' argument_expression_list ')'
356 | START_SCRIPT IDENTIFIER '(' ')'
359 // mostly no-ops in spring
360 misc_statement
361 : CACHE expression
362 | DONT_CACHE expression
363 | SHADE expression
364 | DONT_SHADE expression
367 attach_statement
368 : ATTACH_UNIT expression TO expression
369 | DROP_UNIT expression
372 visibility_statement
373 : SHOW expression
374 | HIDE expression
377 signal_statement
378 : RAISE_SIGNAL expression
379 | SET_SIGNAL_MASK expression
382 explode_statement
383 : EXPLODE expression EXPLODE_TYPE expression
386 ///////////////////////////////////////////////////////////////////////
388 labeled_statement
389 : IDENTIFIER ':' statement
391 | CASE constant_expression ':' statement
392 | DEFAULT ':' statement
396 compound_statement
397 : '{' '}'
398 | '{' statement_list '}'
399 | '{' declaration_list '}'
400 | '{' declaration_list statement_list '}'
403 declaration_list
404 : declaration
405 | declaration_list declaration
408 statement_list
409 : statement
410 | statement_list statement
411 | statement_list declaration_list
412 | statement_list declaration_list statement
415 expression_statement
416 : ';'
417 | expression ';'
420 selection_statement
421 : IF '(' expression ')' statement
422 | IF '(' expression ')' statement ELSE statement
423 /*| SWITCH '(' expression ')' statement*/
426 iteration_statement
427 : WHILE '(' expression ')' statement
428 | DO statement WHILE '(' expression ')' ';'
429 | FOR '(' expression_statement expression_statement ')' statement
430 | FOR '(' expression_statement expression_statement expression ')' statement
431 // scriptor compatibility
432 | FOR '(' expression_statement expression_statement expression_statement ')' statement
435 jump_statement
436 : GOTO IDENTIFIER ';'
437 | CONTINUE ';'
438 | BREAK ';'
439 | RETURN ';'
440 | RETURN expression ';'
443 translation_unit
444 : external_declaration
445 | translation_unit external_declaration
446 | /* empty */
449 external_declaration
450 : function_definition
451 | declaration
452 | pragma
455 function_definition
456 : declarator compound_statement
459 pragma
460 : PRAGMA IDENTIFIER NUMBER
461 | PRAGMA IDENTIFIER IDENTIFIER
462 | PRAGMA IDENTIFIER