fixed bug in suite grammar
[official-gcc.git] / gcc / python / py-parser.y
blob0133f2e717ffaa41d4197ea0484d1dd583e4b4c1
1 %{
2 /* This file is part of GCC.
4 GCC is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 3, or (at your option) any later
7 version.
9 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
18 /* Grammar largely bassed on
19 * - http://docs.python.org/release/2.5.2/ref/grammar.txt
22 #include "config.h"
23 #include "system.h"
24 #include "ansidecl.h"
25 #include "coretypes.h"
26 #include "opts.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "toplev.h"
30 #include "debug.h"
31 #include "options.h"
32 #include "flags.h"
33 #include "convert.h"
34 #include "diagnostic-core.h"
35 #include "langhooks.h"
36 #include "langhooks-def.h"
37 #include "target.h"
39 #include <gmp.h>
40 #include <mpfr.h>
42 #include "vec.h"
43 #include "hashtab.h"
45 #include "gpython.h"
46 #include "py-dot-codes.def"
47 #include "py-dot.h"
48 #include "py-vec.h"
49 #include "py-tree.h"
50 #include "py-runtime.h"
52 static VEC( gpy_sym,gc ) * gpy_symbol_stack;
54 extern int yylineno;
56 // yydebug = 1;
58 extern int yylex( void );
59 extern void yyerror( const char * );
62 %union {
63 char *string;
64 long int integer;
65 enum OPERATOR_T op;
66 gpy_symbol_obj *symbol;
69 %error-verbose
70 %start declarations
71 %debug
73 %token CLASS "class"
74 %token DEF "def"
75 %token BREAK "break"
76 %token CONTINUE "continue"
77 %token RETURN "return"
78 %token FOR "for"
79 %token WHILE "while"
80 %token IN "in"
81 %token PRINT "print"
83 %token EXCEPT "except"
84 %token FINALLY "finally"
85 %token TRY "try"
87 %token AS "as"
88 %token ASSERT "assert"
89 %token DEL "del"
90 %token EXEC "exec"
91 %token FROM "from"
92 %token GLOBAL "global"
93 %token IMPORT "import"
94 %token IS "is"
95 %token LAMBDA "lambda"
96 %token PASS "pass"
97 %token RAISE "raise"
98 %token WITH "with"
99 %token YIELD "yield"
101 %token IF "if"
102 %token ELIF "elif"
103 %token ELSE "else"
105 %token OR "or"
106 %token AND "and"
107 %token NOT "not"
109 %token V_TRUE "True"
110 %token V_FALSE "False"
112 %token NEWLINE
113 %token INDENT
114 %token DEDENT
116 %token EQUAL_EQUAL
117 %token NOT_EQUAL
118 %token LESS
119 %token GREATER
120 %token LESS_EQUAL
121 %token GREATER_EQUAL
123 %token NONE
124 %token<string> IDENTIFIER
125 %token<string> STRING
126 %token<integer> INTEGER
127 %token<decimal> DOUBLE
129 %type<symbol> statement
130 %type<symbol> compound_stmt
131 %type<symbol> stmt_list
132 %type<symbol> simple_stmt
133 %type<symbol> expression_stmt
134 %type<symbol> assignment_stmt
135 %type<symbol> target_list
136 %type<symbol> target
137 %type<symbol> expression_list
138 %type<symbol> funcdef
139 %type<symbol> classdef
140 %type<symbol> suite
141 %type<symbol> suite_statement_list
142 %type<symbol> indent_stmt
143 %type<symbol> literal
144 %type<symbol> m_expr
145 %type<symbol> a_expr
146 %type<symbol> u_expr
147 %type<symbol> atom
148 %type<symbol> primary
149 %type<symbol> expression
150 %type<symbol> conditional_expression
151 %type<symbol> call
152 %type<symbol> shift_expr
153 %type<symbol> comparison
154 %type<symbol> decl
155 %type<symbol> argument_list
156 %type<symbol> argument_list_stmt
157 %type<symbol> parameter_list
158 %type<symbol> parameter_list_stmt
159 %type<symbol> print_stmt
160 %type<string> funcname
161 %type<string> classname
163 %left '-' '+'
164 %left '*' '/'
165 %right '='
166 %nonassoc UMINUS
170 declarations:
171 | declarations decl
173 if( $2 )
175 debug( "passing decl <%p> type <0x%x>!\n",
176 (void*)$2, $2->type );
178 gpy_process_decl( $2 );
183 decl: NEWLINE
184 { $$ = NULL; }
185 | statement
188 compound_stmt: funcdef
189 | classdef
192 classdef: CLASS classname ':' suite
194 gpy_symbol_obj *sym;
195 Gpy_Symbol_Init( sym );
197 sym->identifier = $2;
198 sym->type = STRUCTURE_OBJECT_DEF;
199 sym->op_a_t = TYPE_SYMBOL;
201 sym->op_a.symbol_table= $4;
202 $$= sym;
206 classname: IDENTIFIER
209 funcname: IDENTIFIER
212 parameter_list_stmt:
213 { $$=NULL; }
214 | parameter_list
215 { $$ = VEC_pop( gpy_sym, gpy_symbol_stack ); }
218 parameter_list: parameter_list ',' target
220 $1->next = $3;
221 $$ = $3;
223 | target
225 VEC_safe_push( gpy_sym, gc,
226 gpy_symbol_stack, $1 );
227 $$ = $1;
231 funcdef: DEF funcname '(' parameter_list_stmt ')' ':' suite
233 gpy_symbol_obj *sym;
234 Gpy_Symbol_Init( sym );
236 sym->identifier = $2;
237 sym->type = STRUCTURE_FUNCTION_DEF;
238 sym->op_a_t = TYPE_SYMBOL;
239 sym->op_b_t = TYPE_SYMBOL;
241 sym->op_a.symbol_table= $7;
242 sym->op_b.symbol_table = $4;
244 $$= sym;
248 suite: stmt_list NEWLINE
249 | NEWLINE INDENT suite_statement_list DEDENT
251 $$ = VEC_pop( gpy_sym, gpy_symbol_stack );
252 printf("poping suite!\n");
256 suite_statement_list: suite_statement_list indent_stmt
258 $1->next = $2;
259 $$ = $2;
261 | indent_stmt
263 VEC_safe_push( gpy_sym, gc,
264 gpy_symbol_stack, $1 );
265 $$=$1;
269 indent_stmt: statement
272 statement: stmt_list NEWLINE
273 | compound_stmt
276 stmt_list: simple_stmt
279 simple_stmt: assignment_stmt
280 | expression_stmt
281 | print_stmt
284 argument_list_stmt:
285 { $$ = NULL; }
286 | argument_list
287 { $$ = VEC_pop( gpy_sym, gpy_symbol_stack ); }
290 argument_list: argument_list ',' expression
292 $1->next = $3;
293 $$ = $3;
295 | expression
297 VEC_safe_push( gpy_sym, gc,
298 gpy_symbol_stack, $1 );
299 $$ = $1;
303 print_stmt: PRINT argument_list_stmt
305 gpy_symbol_obj* sym;
306 Gpy_Symbol_Init( sym );
308 sym->type= KEY_PRINT;
309 sym->op_a_t= TYPE_SYMBOL;
311 sym->op_a.symbol_table= $2;
312 $$= sym;
317 expression_stmt: expression_list
320 assignment_stmt: target_list '=' expression_list
322 gpy_symbol_obj* sym;
323 Gpy_Symbol_Init( sym );
325 sym->exp= OP_EXPRESS;
326 sym->type= OP_ASSIGN_EVAL;
327 sym->op_a_t= TYPE_SYMBOL;
328 sym->op_b_t= TYPE_SYMBOL;
330 sym->op_a.symbol_table= $1;
331 sym->op_b.symbol_table= $3;
332 $$= sym;
336 target_list: target
339 target: IDENTIFIER
341 gpy_symbol_obj *sym;
342 Gpy_Symbol_Init( sym );
344 sym->exp = OP_EXPRESS;
345 sym->type= SYMBOL_REFERENCE;
346 sym->op_a_t= TYPE_STRING;
348 sym->op_a.string= $1;
349 $$= sym;
353 expression_list: expression
356 expression: conditional_expression
359 conditional_expression: comparison
362 u_expr: primary
365 m_expr: u_expr
366 | m_expr '*' u_expr
368 gpy_symbol_obj* sym;
369 Gpy_Symbol_Init( sym );
371 sym->exp= OP_EXPRESS;
372 sym->type= OP_BIN_MULTIPLY;
373 sym->op_a_t= TYPE_SYMBOL;
374 sym->op_b_t= TYPE_SYMBOL;
376 sym->op_a.symbol_table= $1;
377 sym->op_b.symbol_table= $3;
378 $$= sym;
380 | m_expr '/' u_expr
382 gpy_symbol_obj* sym;
383 Gpy_Symbol_Init( sym );
385 sym->exp= OP_EXPRESS;
386 sym->type= OP_BIN_DIVIDE;
387 sym->op_a_t= TYPE_SYMBOL;
388 sym->op_b_t= TYPE_SYMBOL;
390 sym->op_a.symbol_table= $1;
391 sym->op_b.symbol_table= $3;
392 $$= sym;
396 a_expr: m_expr
397 | a_expr '+' m_expr
399 gpy_symbol_obj* sym;
400 Gpy_Symbol_Init( sym );
402 sym->exp= OP_EXPRESS;
403 sym->type= OP_BIN_ADDITION;
404 sym->op_a_t= TYPE_SYMBOL;
405 sym->op_b_t= TYPE_SYMBOL;
407 sym->op_a.symbol_table= $1;
408 sym->op_b.symbol_table= $3;
409 $$= sym;
411 | a_expr '-' m_expr
413 gpy_symbol_obj* sym;
414 Gpy_Symbol_Init( sym );
416 sym->exp= OP_EXPRESS;
417 sym->type= OP_BIN_SUBTRACTION;
418 sym->op_a_t= TYPE_SYMBOL;
419 sym->op_b_t= TYPE_SYMBOL;
421 sym->op_a.symbol_table= $1;
422 sym->op_b.symbol_table= $3;
423 $$= sym;
427 shift_expr: a_expr
430 comparison: shift_expr
433 literal: INTEGER
435 gpy_symbol_obj *sym;
436 Gpy_Symbol_Init( sym );
438 sym->exp = OP_EXPRESS;
439 sym->type= SYMBOL_PRIMARY;
440 sym->op_a_t= TYPE_INTEGER;
442 sym->op_a.integer= $1;
443 $$= sym;
445 | STRING
447 gpy_symbol_obj *sym;
448 Gpy_Symbol_Init( sym );
450 sym->exp = OP_EXPRESS;
451 sym->type= SYMBOL_PRIMARY;
452 sym->op_a_t= TYPE_STRING;
454 sym->op_a.string= $1;
455 $$= sym;
457 | V_TRUE
459 gpy_symbol_obj *sym;
460 Gpy_Symbol_Init( sym );
462 sym->exp = OP_EXPRESS;
463 sym->type= SYMBOL_PRIMARY;
464 sym->op_a_t= TYPE_BOOLEAN;
466 sym->op_a.boolean= true;
467 $$= sym;
469 | V_FALSE
471 gpy_symbol_obj *sym;
472 Gpy_Symbol_Init( sym );
474 sym->exp = OP_EXPRESS;
475 sym->type= SYMBOL_PRIMARY;
476 sym->op_a_t= TYPE_BOOLEAN;
478 sym->op_a.boolean= false;
479 $$= sym;
483 atom: target
484 | literal
487 call: IDENTIFIER '(' argument_list_stmt ')'
489 gpy_symbol_obj *sym= NULL;
490 Gpy_Symbol_Init( sym );
492 sym->exp = OP_EXPRESS;
493 sym->type= OP_CALL_GOTO;
494 sym->op_a_t= TYPE_STRING;
495 sym->op_b_t= TYPE_SYMBOL;
497 sym->op_a.string = $1;
498 sym->op_b.symbol_table = $3;
500 $$= sym;
504 primary: atom
505 | call
510 void yyerror( const char *msg )
512 error( "%s at line %i\n", msg, yylineno );