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
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
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
25 #include "coretypes.h"
34 #include "diagnostic-core.h"
35 #include "langhooks.h"
36 #include "langhooks-def.h"
46 #include "py-dot-codes.def"
50 #include "py-runtime.h"
52 static VEC
( gpy_sym
,gc
) * gpy_symbol_stack
;
58 extern
int yylex( void );
59 extern
void yyerror( const char * );
66 gpy_symbol_obj
*symbol
;
76 %token CONTINUE
"continue"
77 %token RETURN
"return"
92 %token V_FALSE
"False"
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
119 %type
<symbol
> expression_list
120 %type
<symbol
> funcdef
121 %type
<symbol
> classdef
123 %type
<symbol
> suite_statement_list
124 %type
<symbol
> indent_stmt
125 %type
<symbol
> literal
130 %type
<symbol
> primary
131 %type
<symbol
> expression
132 %type
<symbol
> conditional_expression
134 %type
<symbol
> shift_expr
135 %type
<symbol
> comparison
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
157 debug
( "passing decl <%p> type <0x%x>!\n",
158 (void*)$2, $2->type
);
160 gpy_process_decl
( $2 );
170 compound_stmt: funcdef
174 classdef: CLASS classname
':' suite
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;
188 classname: IDENTIFIER
197 { $$
= VEC_pop
( gpy_sym
, gpy_symbol_stack
); }
200 parameter_list: parameter_list
',' target
207 VEC_safe_push
( gpy_sym
, gc
,
208 gpy_symbol_stack
, $1 );
213 funcdef: DEF funcname
'(' parameter_list_stmt
')' ':' suite
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;
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
245 VEC_safe_push
( gpy_sym
, gc
,
246 gpy_symbol_stack
, $1 );
251 indent_stmt: INDENT statement
255 statement: stmt_list NEWLINE
259 stmt_list: simple_stmt
262 simple_stmt: assignment_stmt
270 { $$
= VEC_pop
( gpy_sym
, gpy_symbol_stack
); }
273 argument_list: argument_list
',' expression
280 VEC_safe_push
( gpy_sym
, gc
,
281 gpy_symbol_stack
, $1 );
286 print_stmt: PRINT argument_list_stmt
289 Gpy_Symbol_Init
( sym
);
291 sym
->type
= KEY_PRINT
;
292 sym
->op_a_t
= TYPE_SYMBOL
;
294 sym
->op_a.symbol_table
= $2;
300 expression_stmt: expression_list
303 assignment_stmt: target_list
'=' expression_list
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;
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;
336 expression_list: expression
339 expression: conditional_expression
342 conditional_expression: comparison
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;
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;
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;
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;
413 comparison: shift_expr
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;
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;
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
;
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
;
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;
493 void yyerror( const char *msg
)
495 error( "%s at line %i\n", msg
, yylineno
);