fixed many errors and cleaned up includes
[official-gcc.git] / gcc / python / py-parser.y
blob58779a84ad9dc380dd516d6a353f24757536975c
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 IF "if"
84 %token ELIF "elif"
85 %token ELSE "else"
87 %token OR "or"
88 %token AND "and"
89 %token NOT "not"
91 %token V_TRUE "True"
92 %token V_FALSE "False"
94 %token NEWLINE
95 %token INDENT
96 %token DEDENT
98 %token EQUAL_EQUAL
99 %token NOT_EQUAL
100 %token LESS
101 %token GREATER
102 %token LESS_EQUAL
103 %token GREATER_EQUAL
105 %token NONE
106 %token<string> IDENTIFIER
107 %token<string> STRING
108 %token<integer> INTEGER
109 %token<decimal> DOUBLE
111 %type<symbol> statement
112 %type<symbol> compound_stmt
113 %type<symbol> stmt_list
114 %type<symbol> simple_stmt
115 %type<symbol> expression_stmt
116 %type<symbol> assignment_stmt
117 %type<symbol> target_list
118 %type<symbol> target
119 %type<symbol> expression_list
120 %type<symbol> funcdef
121 %type<symbol> classdef
122 %type<symbol> suite
123 %type<symbol> suite_statement_list
124 %type<symbol> indent_stmt
125 %type<symbol> literal
126 %type<symbol> m_expr
127 %type<symbol> a_expr
128 %type<symbol> u_expr
129 %type<symbol> atom
130 %type<symbol> primary
131 %type<symbol> expression
132 %type<symbol> conditional_expression
133 %type<symbol> call
134 %type<symbol> shift_expr
135 %type<symbol> comparison
136 %type<symbol> decl
137 %type<symbol> argument_list
138 %type<symbol> argument_list_stmt
139 %type<symbol> parameter_list
140 %type<symbol> parameter_list_stmt
141 %type<symbol> print_stmt
142 %type<string> funcname
143 %type<string> classname
145 %left '-' '+'
146 %left '*' '/'
147 %right '='
148 %nonassoc UMINUS
152 declarations:
153 | declarations decl
155 if( $2 )
157 debug( "passing decl <%p> type <0x%x>!\n",
158 (void*)$2, $2->type );
160 gpy_process_decl( $2 );
165 decl: NEWLINE
166 { $$ = NULL; }
167 | statement
170 compound_stmt: funcdef
171 | classdef
174 classdef: CLASS classname ':' suite
176 gpy_symbol_obj *sym;
177 Gpy_Symbol_Init( sym );
179 sym->identifier = $2;
180 sym->type = STRUCTURE_OBJECT_DEF;
181 sym->op_a_t = TYPE_SYMBOL;
183 sym->op_a.symbol_table= $4;
184 $$= sym;
188 classname: IDENTIFIER
191 funcname: IDENTIFIER
194 parameter_list_stmt:
195 { $$=NULL; }
196 | parameter_list
197 { $$ = VEC_pop( gpy_sym, gpy_symbol_stack ); }
200 parameter_list: parameter_list ',' target
202 $1->next = $3;
203 $$ = $3;
205 | target
207 VEC_safe_push( gpy_sym, gc,
208 gpy_symbol_stack, $1 );
209 $$ = $1;
213 funcdef: DEF funcname '(' parameter_list_stmt ')' ':' suite
215 gpy_symbol_obj *sym;
216 Gpy_Symbol_Init( sym );
218 sym->identifier = $2;
219 sym->type = STRUCTURE_FUNCTION_DEF;
220 sym->op_a_t = TYPE_SYMBOL;
221 sym->op_b_t = TYPE_SYMBOL;
223 sym->op_a.symbol_table= $7;
224 sym->op_b.symbol_table = $4;
226 $$= sym;
230 suite: stmt_list NEWLINE
231 | NEWLINE suite_statement_list DEDENT
233 $$ = VEC_pop( gpy_sym, gpy_symbol_stack );
234 printf("poping suite!\n");
238 suite_statement_list: suite_statement_list indent_stmt
240 $1->next = $2;
241 $$ = $2;
243 | indent_stmt
245 VEC_safe_push( gpy_sym, gc,
246 gpy_symbol_stack, $1 );
247 $$=$1;
251 indent_stmt: INDENT statement
252 { $$=$2; }
255 statement: stmt_list NEWLINE
256 | compound_stmt
259 stmt_list: simple_stmt
262 simple_stmt: assignment_stmt
263 | expression_stmt
264 | print_stmt
267 argument_list_stmt:
268 { $$ = NULL; }
269 | argument_list
270 { $$ = VEC_pop( gpy_sym, gpy_symbol_stack ); }
273 argument_list: argument_list ',' expression
275 $1->next = $3;
276 $$ = $3;
278 | expression
280 VEC_safe_push( gpy_sym, gc,
281 gpy_symbol_stack, $1 );
282 $$ = $1;
286 print_stmt: PRINT argument_list_stmt
288 gpy_symbol_obj* sym;
289 Gpy_Symbol_Init( sym );
291 sym->type= KEY_PRINT;
292 sym->op_a_t= TYPE_SYMBOL;
294 sym->op_a.symbol_table= $2;
295 $$= sym;
300 expression_stmt: expression_list
303 assignment_stmt: target_list '=' expression_list
305 gpy_symbol_obj* sym;
306 Gpy_Symbol_Init( sym );
308 sym->exp= OP_EXPRESS;
309 sym->type= OP_ASSIGN_EVAL;
310 sym->op_a_t= TYPE_SYMBOL;
311 sym->op_b_t= TYPE_SYMBOL;
313 sym->op_a.symbol_table= $1;
314 sym->op_b.symbol_table= $3;
315 $$= sym;
319 target_list: target
322 target: IDENTIFIER
324 gpy_symbol_obj *sym;
325 Gpy_Symbol_Init( sym );
327 sym->exp = OP_EXPRESS;
328 sym->type= SYMBOL_REFERENCE;
329 sym->op_a_t= TYPE_STRING;
331 sym->op_a.string= $1;
332 $$= sym;
336 expression_list: expression
339 expression: conditional_expression
342 conditional_expression: comparison
345 u_expr: primary
348 m_expr: u_expr
349 | m_expr '*' u_expr
351 gpy_symbol_obj* sym;
352 Gpy_Symbol_Init( sym );
354 sym->exp= OP_EXPRESS;
355 sym->type= OP_BIN_MULTIPLY;
356 sym->op_a_t= TYPE_SYMBOL;
357 sym->op_b_t= TYPE_SYMBOL;
359 sym->op_a.symbol_table= $1;
360 sym->op_b.symbol_table= $3;
361 $$= sym;
363 | m_expr '/' u_expr
365 gpy_symbol_obj* sym;
366 Gpy_Symbol_Init( sym );
368 sym->exp= OP_EXPRESS;
369 sym->type= OP_BIN_DIVIDE;
370 sym->op_a_t= TYPE_SYMBOL;
371 sym->op_b_t= TYPE_SYMBOL;
373 sym->op_a.symbol_table= $1;
374 sym->op_b.symbol_table= $3;
375 $$= sym;
379 a_expr: m_expr
380 | a_expr '+' m_expr
382 gpy_symbol_obj* sym;
383 Gpy_Symbol_Init( sym );
385 sym->exp= OP_EXPRESS;
386 sym->type= OP_BIN_ADDITION;
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;
394 | a_expr '-' m_expr
396 gpy_symbol_obj* sym;
397 Gpy_Symbol_Init( sym );
399 sym->exp= OP_EXPRESS;
400 sym->type= OP_BIN_SUBTRACTION;
401 sym->op_a_t= TYPE_SYMBOL;
402 sym->op_b_t= TYPE_SYMBOL;
404 sym->op_a.symbol_table= $1;
405 sym->op_b.symbol_table= $3;
406 $$= sym;
410 shift_expr: a_expr
413 comparison: shift_expr
416 literal: INTEGER
418 gpy_symbol_obj *sym;
419 Gpy_Symbol_Init( sym );
421 sym->exp = OP_EXPRESS;
422 sym->type= SYMBOL_PRIMARY;
423 sym->op_a_t= TYPE_INTEGER;
425 sym->op_a.integer= $1;
426 $$= sym;
428 | STRING
430 gpy_symbol_obj *sym;
431 Gpy_Symbol_Init( sym );
433 sym->exp = OP_EXPRESS;
434 sym->type= SYMBOL_PRIMARY;
435 sym->op_a_t= TYPE_STRING;
437 sym->op_a.string= $1;
438 $$= sym;
440 | V_TRUE
442 gpy_symbol_obj *sym;
443 Gpy_Symbol_Init( sym );
445 sym->exp = OP_EXPRESS;
446 sym->type= SYMBOL_PRIMARY;
447 sym->op_a_t= TYPE_BOOLEAN;
449 sym->op_a.boolean= true;
450 $$= sym;
452 | V_FALSE
454 gpy_symbol_obj *sym;
455 Gpy_Symbol_Init( sym );
457 sym->exp = OP_EXPRESS;
458 sym->type= SYMBOL_PRIMARY;
459 sym->op_a_t= TYPE_BOOLEAN;
461 sym->op_a.boolean= false;
462 $$= sym;
466 atom: target
467 | literal
470 call: IDENTIFIER '(' argument_list_stmt ')'
472 gpy_symbol_obj *sym= NULL;
473 Gpy_Symbol_Init( sym );
475 sym->exp = OP_EXPRESS;
476 sym->type= OP_CALL_GOTO;
477 sym->op_a_t= TYPE_STRING;
478 sym->op_b_t= TYPE_SYMBOL;
480 sym->op_a.string = $1;
481 sym->op_b.symbol_table = $3;
483 $$= sym;
487 primary: atom
488 | call
493 void yyerror( const char *msg )
495 error( "%s at line %i\n", msg, yylineno );