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"
83 %token EXCEPT
"except"
84 %token FINALLY
"finally"
88 %token ASSERT
"assert"
92 %token GLOBAL
"global"
93 %token IMPORT
"import"
95 %token LAMBDA
"lambda"
110 %token V_FALSE
"False"
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
137 %type
<symbol
> expression_list
138 %type
<symbol
> funcdef
139 %type
<symbol
> classdef
141 %type
<symbol
> suite_statement_list
142 %type
<symbol
> indent_stmt
143 %type
<symbol
> literal
148 %type
<symbol
> primary
149 %type
<symbol
> expression
150 %type
<symbol
> conditional_expression
152 %type
<symbol
> shift_expr
153 %type
<symbol
> comparison
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
175 debug
( "passing decl <%p> type <0x%x>!\n",
176 (void*)$2, $2->type
);
178 gpy_process_decl
( $2 );
188 compound_stmt: funcdef
192 classdef: CLASS classname
':' suite
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;
206 classname: IDENTIFIER
215 { $$
= VEC_pop
( gpy_sym
, gpy_symbol_stack
); }
218 parameter_list: parameter_list
',' target
225 VEC_safe_push
( gpy_sym
, gc
,
226 gpy_symbol_stack
, $1 );
231 funcdef: DEF funcname
'(' parameter_list_stmt
')' ':' suite
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;
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
263 VEC_safe_push
( gpy_sym
, gc
,
264 gpy_symbol_stack
, $1 );
269 indent_stmt: statement
272 statement: stmt_list NEWLINE
276 stmt_list: simple_stmt
279 simple_stmt: assignment_stmt
287 { $$
= VEC_pop
( gpy_sym
, gpy_symbol_stack
); }
290 argument_list: argument_list
',' expression
297 VEC_safe_push
( gpy_sym
, gc
,
298 gpy_symbol_stack
, $1 );
303 print_stmt: PRINT argument_list_stmt
306 Gpy_Symbol_Init
( sym
);
308 sym
->type
= KEY_PRINT
;
309 sym
->op_a_t
= TYPE_SYMBOL
;
311 sym
->op_a.symbol_table
= $2;
317 expression_stmt: expression_list
320 assignment_stmt: target_list
'=' expression_list
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;
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;
353 expression_list: expression
356 expression: conditional_expression
359 conditional_expression: comparison
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;
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;
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;
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;
430 comparison: shift_expr
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;
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;
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
;
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
;
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;
510 void yyerror( const char *msg
)
512 error( "%s at line %i\n", msg
, yylineno
);