blob: be58f53cc51f5f874957e30b3f9a9b491269fb1b [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* Data structure used internally */
19struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000020 char *c_encoding; /* source encoding */
21 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022};
23
24static asdl_seq *seq_for_testlist(struct compiling *, const node *);
25static expr_ty ast_for_expr(struct compiling *, const node *);
26static stmt_ty ast_for_stmt(struct compiling *, const node *);
27static asdl_seq *ast_for_suite(struct compiling *, const node *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000028static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
29 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000030static expr_ty ast_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032
33/* Note different signature for ast_for_call */
34static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
35
36static PyObject *parsenumber(const char *);
37static PyObject *parsestr(const char *s, const char *encoding);
38static PyObject *parsestrplus(struct compiling *, const node *n);
39
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000041#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042#endif
43
Neal Norwitzadb69fc2005-12-17 20:54:49 +000044static identifier
45new_identifier(const char* n, PyArena *arena) {
46 PyObject* id = PyString_InternFromString(n);
47 PyArena_AddPyObject(arena, id);
48 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000049}
50
Neal Norwitzadb69fc2005-12-17 20:54:49 +000051#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052
53/* This routine provides an invalid object for the syntax error.
54 The outermost routine must unpack this error and create the
55 proper object. We do this so that we don't have to pass
56 the filename to everything function.
57
58 XXX Maybe we should just pass the filename...
59*/
60
61static int
62ast_error(const node *n, const char *errstr)
63{
64 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
65 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000066 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000067 PyErr_SetObject(PyExc_SyntaxError, u);
68 Py_DECREF(u);
69 return 0;
70}
71
72static void
73ast_error_finish(const char *filename)
74{
75 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000076 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077
78 assert(PyErr_Occurred());
79 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000080 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
82 PyErr_Fetch(&type, &value, &tback);
83 errstr = PyTuple_GetItem(value, 0);
84 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000085 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086 Py_INCREF(errstr);
87 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000088 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000089 Py_DECREF(errstr);
90 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000091 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092 Py_DECREF(value);
93
94 loc = PyErr_ProgramText(filename, lineno);
95 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000096 Py_INCREF(Py_None);
97 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +000099 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000101 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000102 Py_DECREF(errstr);
103 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000104 }
Georg Brandl7784f122006-05-26 20:04:44 +0000105 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106 Py_DECREF(errstr);
107 Py_DECREF(tmp);
108 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000109 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 PyErr_Restore(type, value, tback);
111}
112
113/* num_stmts() returns number of contained statements.
114
115 Use this routine to determine how big a sequence is needed for
116 the statements in a parse tree. Its raison d'etre is this bit of
117 grammar:
118
119 stmt: simple_stmt | compound_stmt
120 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
121
122 A simple_stmt can contain multiple small_stmt elements joined
123 by semicolons. If the arg is a simple_stmt, the number of
124 small_stmt elements is returned.
125*/
126
127static int
128num_stmts(const node *n)
129{
130 int i, l;
131 node *ch;
132
133 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000134 case single_input:
135 if (TYPE(CHILD(n, 0)) == NEWLINE)
136 return 0;
137 else
138 return num_stmts(CHILD(n, 0));
139 case file_input:
140 l = 0;
141 for (i = 0; i < NCH(n); i++) {
142 ch = CHILD(n, i);
143 if (TYPE(ch) == stmt)
144 l += num_stmts(ch);
145 }
146 return l;
147 case stmt:
148 return num_stmts(CHILD(n, 0));
149 case compound_stmt:
150 return 1;
151 case simple_stmt:
152 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
153 case suite:
154 if (NCH(n) == 1)
155 return num_stmts(CHILD(n, 0));
156 else {
157 l = 0;
158 for (i = 2; i < (NCH(n) - 1); i++)
159 l += num_stmts(CHILD(n, i));
160 return l;
161 }
162 default: {
163 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000165 sprintf(buf, "Non-statement found: %d %d\n",
166 TYPE(n), NCH(n));
167 Py_FatalError(buf);
168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169 }
170 assert(0);
171 return 0;
172}
173
174/* Transform the CST rooted at node * to the appropriate AST
175*/
176
177mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000178PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000179 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000181 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182 asdl_seq *stmts = NULL;
183 stmt_ty s;
184 node *ch;
185 struct compiling c;
186
187 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000188 c.c_encoding = "utf-8";
189 if (TYPE(n) == encoding_decl) {
190 ast_error(n, "encoding declaration in Unicode string");
191 goto error;
192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000194 c.c_encoding = STR(n);
195 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000197 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000199 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
Jeremy Hyltona8293132006-02-28 17:58:27 +0000201 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000203 case file_input:
204 stmts = asdl_seq_new(num_stmts(n), arena);
205 if (!stmts)
206 return NULL;
207 for (i = 0; i < NCH(n) - 1; i++) {
208 ch = CHILD(n, i);
209 if (TYPE(ch) == NEWLINE)
210 continue;
211 REQ(ch, stmt);
212 num = num_stmts(ch);
213 if (num == 1) {
214 s = ast_for_stmt(&c, ch);
215 if (!s)
216 goto error;
217 asdl_seq_SET(stmts, k++, s);
218 }
219 else {
220 ch = CHILD(ch, 0);
221 REQ(ch, simple_stmt);
222 for (j = 0; j < num; j++) {
223 s = ast_for_stmt(&c, CHILD(ch, j * 2));
224 if (!s)
225 goto error;
226 asdl_seq_SET(stmts, k++, s);
227 }
228 }
229 }
230 return Module(stmts, arena);
231 case eval_input: {
232 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000234 /* XXX Why not gen_for here? */
235 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
236 if (!testlist_ast)
237 goto error;
238 return Expression(testlist_ast, arena);
239 }
240 case single_input:
241 if (TYPE(CHILD(n, 0)) == NEWLINE) {
242 stmts = asdl_seq_new(1, arena);
243 if (!stmts)
244 goto error;
245 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
246 arena));
247 return Interactive(stmts, arena);
248 }
249 else {
250 n = CHILD(n, 0);
251 num = num_stmts(n);
252 stmts = asdl_seq_new(num, arena);
253 if (!stmts)
254 goto error;
255 if (num == 1) {
256 s = ast_for_stmt(&c, n);
257 if (!s)
258 goto error;
259 asdl_seq_SET(stmts, 0, s);
260 }
261 else {
262 /* Only a simple_stmt can contain multiple statements. */
263 REQ(n, simple_stmt);
264 for (i = 0; i < NCH(n); i += 2) {
265 if (TYPE(CHILD(n, i)) == NEWLINE)
266 break;
267 s = ast_for_stmt(&c, CHILD(n, i));
268 if (!s)
269 goto error;
270 asdl_seq_SET(stmts, i / 2, s);
271 }
272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000274 return Interactive(stmts, arena);
275 }
276 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000277 PyErr_Format(PyExc_SystemError,
278 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000279 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 }
281 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282 ast_error_finish(filename);
283 return NULL;
284}
285
286/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
287*/
288
289static operator_ty
290get_operator(const node *n)
291{
292 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000293 case VBAR:
294 return BitOr;
295 case CIRCUMFLEX:
296 return BitXor;
297 case AMPER:
298 return BitAnd;
299 case LEFTSHIFT:
300 return LShift;
301 case RIGHTSHIFT:
302 return RShift;
303 case PLUS:
304 return Add;
305 case MINUS:
306 return Sub;
307 case STAR:
308 return Mult;
309 case SLASH:
310 return Div;
311 case DOUBLESLASH:
312 return FloorDiv;
313 case PERCENT:
314 return Mod;
315 default:
316 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317 }
318}
319
Jeremy Hyltona8293132006-02-28 17:58:27 +0000320/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321
322 Only sets context for expr kinds that "can appear in assignment context"
323 (according to ../Parser/Python.asdl). For other expr kinds, it sets
324 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325*/
326
327static int
328set_context(expr_ty e, expr_context_ty ctx, const node *n)
329{
330 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000331 /* If a particular expression type can't be used for assign / delete,
332 set expr_name to its name and an error message will be generated.
333 */
334 const char* expr_name = NULL;
335
336 /* The ast defines augmented store and load contexts, but the
337 implementation here doesn't actually use them. The code may be
338 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000339 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000340 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000341 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000342 */
343 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344
345 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000346 case Attribute_kind:
347 if (ctx == Store &&
348 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
349 return ast_error(n, "assignment to None");
350 }
351 e->v.Attribute.ctx = ctx;
352 break;
353 case Subscript_kind:
354 e->v.Subscript.ctx = ctx;
355 break;
356 case Name_kind:
357 if (ctx == Store &&
358 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
359 return ast_error(n, "assignment to None");
360 }
361 e->v.Name.ctx = ctx;
362 break;
363 case List_kind:
364 e->v.List.ctx = ctx;
365 s = e->v.List.elts;
366 break;
367 case Tuple_kind:
368 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
369 return ast_error(n, "can't assign to ()");
370 e->v.Tuple.ctx = ctx;
371 s = e->v.Tuple.elts;
372 break;
373 case Lambda_kind:
374 expr_name = "lambda";
375 break;
376 case Call_kind:
377 expr_name = "function call";
378 break;
379 case BoolOp_kind:
380 case BinOp_kind:
381 case UnaryOp_kind:
382 expr_name = "operator";
383 break;
384 case GeneratorExp_kind:
385 expr_name = "generator expression";
386 break;
387 case Yield_kind:
388 expr_name = "yield expression";
389 break;
390 case ListComp_kind:
391 expr_name = "list comprehension";
392 break;
393 case Dict_kind:
394 case Num_kind:
395 case Str_kind:
396 expr_name = "literal";
397 break;
398 case Compare_kind:
399 expr_name = "comparison";
400 break;
401 case Repr_kind:
402 expr_name = "repr";
403 break;
404 case IfExp_kind:
405 expr_name = "conditional expression";
406 break;
407 default:
408 PyErr_Format(PyExc_SystemError,
409 "unexpected expression in assignment %d (line %d)",
410 e->kind, e->lineno);
411 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000413 /* Check for error string set by switch */
414 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000415 char buf[300];
416 PyOS_snprintf(buf, sizeof(buf),
417 "can't %s %s",
418 ctx == Store ? "assign to" : "delete",
419 expr_name);
420 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000421 }
422
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000424 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425 */
426 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000427 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000429 for (i = 0; i < asdl_seq_LEN(s); i++) {
430 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
431 return 0;
432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433 }
434 return 1;
435}
436
437static operator_ty
438ast_for_augassign(const node *n)
439{
440 REQ(n, augassign);
441 n = CHILD(n, 0);
442 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000443 case '+':
444 return Add;
445 case '-':
446 return Sub;
447 case '/':
448 if (STR(n)[1] == '/')
449 return FloorDiv;
450 else
451 return Div;
452 case '%':
453 return Mod;
454 case '<':
455 return LShift;
456 case '>':
457 return RShift;
458 case '&':
459 return BitAnd;
460 case '^':
461 return BitXor;
462 case '|':
463 return BitOr;
464 case '*':
465 if (STR(n)[1] == '*')
466 return Pow;
467 else
468 return Mult;
469 default:
470 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
471 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472 }
473}
474
475static cmpop_ty
476ast_for_comp_op(const node *n)
477{
478 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000479 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480 */
481 REQ(n, comp_op);
482 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000483 n = CHILD(n, 0);
484 switch (TYPE(n)) {
485 case LESS:
486 return Lt;
487 case GREATER:
488 return Gt;
489 case EQEQUAL: /* == */
490 return Eq;
491 case LESSEQUAL:
492 return LtE;
493 case GREATEREQUAL:
494 return GtE;
495 case NOTEQUAL:
496 return NotEq;
497 case NAME:
498 if (strcmp(STR(n), "in") == 0)
499 return In;
500 if (strcmp(STR(n), "is") == 0)
501 return Is;
502 default:
503 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
504 STR(n));
505 return (cmpop_ty)0;
506 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507 }
508 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000509 /* handle "not in" and "is not" */
510 switch (TYPE(CHILD(n, 0))) {
511 case NAME:
512 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
513 return NotIn;
514 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
515 return IsNot;
516 default:
517 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
518 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
519 return (cmpop_ty)0;
520 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 }
Neal Norwitz79792652005-11-14 04:25:03 +0000522 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000523 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000524 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525}
526
527static asdl_seq *
528seq_for_testlist(struct compiling *c, const node *n)
529{
530 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000531 asdl_seq *seq;
532 expr_ty expression;
533 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000534 assert(TYPE(n) == testlist ||
535 TYPE(n) == listmaker ||
536 TYPE(n) == testlist_gexp ||
537 TYPE(n) == testlist_safe ||
538 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000540 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000542 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
544 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000545 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000547 expression = ast_for_expr(c, CHILD(n, i));
548 if (!expression)
549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000551 assert(i / 2 < seq->size);
552 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553 }
554 return seq;
555}
556
557static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000558compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559{
560 int i, len = (NCH(n) + 1) / 2;
561 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000562 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000564 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565
Neal Norwitz3a230172006-09-22 08:18:10 +0000566 /* fpdef: NAME | '(' fplist ')'
567 fplist: fpdef (',' fpdef)* [',']
568 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000571 const node *fpdef_node = CHILD(n, 2*i);
572 const node *child;
573 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000574set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000575 /* fpdef_node is either a NAME or an fplist */
576 child = CHILD(fpdef_node, 0);
577 if (TYPE(child) == NAME) {
578 if (!strcmp(STR(child), "None")) {
579 ast_error(child, "assignment to None");
580 return NULL;
581 }
582 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
583 child->n_col_offset, c->c_arena);
584 }
585 else {
586 assert(TYPE(fpdef_node) == fpdef);
587 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
588 child = CHILD(fpdef_node, 1);
589 assert(TYPE(child) == fplist);
590 /* NCH == 1 means we have (x), we need to elide the extra parens */
591 if (NCH(child) == 1) {
592 fpdef_node = CHILD(child, 0);
593 assert(TYPE(fpdef_node) == fpdef);
594 goto set_name;
595 }
596 arg = compiler_complex_args(c, child);
597 }
598 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 }
600
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000601 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000602 if (!set_context(result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000603 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 return result;
605}
606
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607
Jeremy Hyltona8293132006-02-28 17:58:27 +0000608/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609
610static arguments_ty
611ast_for_arguments(struct compiling *c, const node *n)
612{
613 /* parameters: '(' [varargslist] ')'
614 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000615 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000617 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 asdl_seq *args, *defaults;
619 identifier vararg = NULL, kwarg = NULL;
620 node *ch;
621
622 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000623 if (NCH(n) == 2) /* () as argument list */
624 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
625 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626 }
627 REQ(n, varargslist);
628
629 /* first count the number of normal args & defaults */
630 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000631 ch = CHILD(n, i);
632 if (TYPE(ch) == fpdef)
633 n_args++;
634 if (TYPE(ch) == EQUAL)
635 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000637 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000639 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000640 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000642 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643
644 /* fpdef: NAME | '(' fplist ')'
645 fplist: fpdef (',' fpdef)* [',']
646 */
647 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000648 j = 0; /* index for defaults */
649 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000651 ch = CHILD(n, i);
652 switch (TYPE(ch)) {
653 case fpdef:
654 handle_fpdef:
655 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
656 anything other than EQUAL or a comma? */
657 /* XXX Should NCH(n) check be made a separate check? */
658 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
659 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
660 if (!expression)
661 goto error;
662 assert(defaults != NULL);
663 asdl_seq_SET(defaults, j++, expression);
664 i += 2;
665 found_default = 1;
666 }
667 else if (found_default) {
668 ast_error(n,
669 "non-default argument follows default argument");
670 goto error;
671 }
672 if (NCH(ch) == 3) {
673 ch = CHILD(ch, 1);
674 /* def foo((x)): is not complex, special case. */
675 if (NCH(ch) != 1) {
676 /* We have complex arguments, setup for unpacking. */
677 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
678 } else {
679 /* def foo((x)): setup for checking NAME below. */
680 /* Loop because there can be many parens and tuple
681 unpacking mixed in. */
682 ch = CHILD(ch, 0);
683 assert(TYPE(ch) == fpdef);
684 goto handle_fpdef;
685 }
686 }
687 if (TYPE(CHILD(ch, 0)) == NAME) {
688 expr_ty name;
689 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
690 ast_error(CHILD(ch, 0), "assignment to None");
691 goto error;
692 }
693 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
694 Param, LINENO(ch), ch->n_col_offset,
695 c->c_arena);
696 if (!name)
697 goto error;
698 asdl_seq_SET(args, k++, name);
699
700 }
701 i += 2; /* the name and the comma */
702 break;
703 case STAR:
704 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
705 ast_error(CHILD(n, i+1), "assignment to None");
706 goto error;
707 }
708 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
709 i += 3;
710 break;
711 case DOUBLESTAR:
712 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
713 ast_error(CHILD(n, i+1), "assignment to None");
714 goto error;
715 }
716 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
717 i += 3;
718 break;
719 default:
720 PyErr_Format(PyExc_SystemError,
721 "unexpected node in varargslist: %d @ %d",
722 TYPE(ch), i);
723 goto error;
724 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725 }
726
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000727 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728
729 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000730 Py_XDECREF(vararg);
731 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 return NULL;
733}
734
735static expr_ty
736ast_for_dotted_name(struct compiling *c, const node *n)
737{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000738 expr_ty e;
739 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000740 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741 int i;
742
743 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000744
745 lineno = LINENO(n);
746 col_offset = n->n_col_offset;
747
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 id = NEW_IDENTIFIER(CHILD(n, 0));
749 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000750 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000751 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000753 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
755 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000756 id = NEW_IDENTIFIER(CHILD(n, i));
757 if (!id)
758 return NULL;
759 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
760 if (!e)
761 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762 }
763
764 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765}
766
767static expr_ty
768ast_for_decorator(struct compiling *c, const node *n)
769{
770 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
771 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000772 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773
774 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000775 REQ(CHILD(n, 0), AT);
776 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
778 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
779 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000780 return NULL;
781
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000783 d = name_expr;
784 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 }
786 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000787 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
788 n->n_col_offset, c->c_arena);
789 if (!d)
790 return NULL;
791 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 }
793 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000794 d = ast_for_call(c, CHILD(n, 3), name_expr);
795 if (!d)
796 return NULL;
797 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 }
799
800 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801}
802
803static asdl_seq*
804ast_for_decorators(struct compiling *c, const node *n)
805{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000806 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000807 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 int i;
809
810 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000811 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000813 return NULL;
814
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000816 d = ast_for_decorator(c, CHILD(n, i));
817 if (!d)
818 return NULL;
819 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822}
823
824static stmt_ty
825ast_for_funcdef(struct compiling *c, const node *n)
826{
827 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000828 identifier name;
829 arguments_ty args;
830 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 asdl_seq *decorator_seq = NULL;
832 int name_i;
833
834 REQ(n, funcdef);
835
836 if (NCH(n) == 6) { /* decorators are present */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000837 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
838 if (!decorator_seq)
839 return NULL;
840 name_i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 }
842 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000843 name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 }
845
846 name = NEW_IDENTIFIER(CHILD(n, name_i));
847 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000848 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000850 ast_error(CHILD(n, name_i), "assignment to None");
851 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 }
853 args = ast_for_arguments(c, CHILD(n, name_i + 1));
854 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 body = ast_for_suite(c, CHILD(n, name_i + 3));
857 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000858 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000860 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000861 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862}
863
864static expr_ty
865ast_for_lambdef(struct compiling *c, const node *n)
866{
867 /* lambdef: 'lambda' [varargslist] ':' test */
868 arguments_ty args;
869 expr_ty expression;
870
871 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000872 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
873 if (!args)
874 return NULL;
875 expression = ast_for_expr(c, CHILD(n, 2));
876 if (!expression)
877 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 }
879 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000880 args = ast_for_arguments(c, CHILD(n, 1));
881 if (!args)
882 return NULL;
883 expression = ast_for_expr(c, CHILD(n, 3));
884 if (!expression)
885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 }
887
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000888 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889}
890
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000891static expr_ty
892ast_for_ifexpr(struct compiling *c, const node *n)
893{
894 /* test: or_test 'if' or_test 'else' test */
895 expr_ty expression, body, orelse;
896
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000897 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000898 body = ast_for_expr(c, CHILD(n, 0));
899 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000900 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000901 expression = ast_for_expr(c, CHILD(n, 2));
902 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000903 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000904 orelse = ast_for_expr(c, CHILD(n, 4));
905 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000906 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000907 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000908 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000909}
910
Neal Norwitze4d4f002006-09-05 03:58:26 +0000911/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
912 so there is only a single version. Possibly for loops can also re-use
913 the code.
914*/
915
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916/* Count the number of 'for' loop in a list comprehension.
917
918 Helper for ast_for_listcomp().
919*/
920
921static int
922count_list_fors(const node *n)
923{
924 int n_fors = 0;
925 node *ch = CHILD(n, 1);
926
927 count_list_for:
928 n_fors++;
929 REQ(ch, list_for);
930 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000931 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000933 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934 count_list_iter:
935 REQ(ch, list_iter);
936 ch = CHILD(ch, 0);
937 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000938 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000940 if (NCH(ch) == 3) {
941 ch = CHILD(ch, 2);
942 goto count_list_iter;
943 }
944 else
945 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000947
948 /* Should never be reached */
949 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
950 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951}
952
953/* Count the number of 'if' statements in a list comprehension.
954
955 Helper for ast_for_listcomp().
956*/
957
958static int
959count_list_ifs(const node *n)
960{
961 int n_ifs = 0;
962
963 count_list_iter:
964 REQ(n, list_iter);
965 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000966 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 n = CHILD(n, 0);
968 REQ(n, list_if);
969 n_ifs++;
970 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000971 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972 n = CHILD(n, 2);
973 goto count_list_iter;
974}
975
976static expr_ty
977ast_for_listcomp(struct compiling *c, const node *n)
978{
979 /* listmaker: test ( list_for | (',' test)* [','] )
980 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
981 list_iter: list_for | list_if
982 list_if: 'if' test [list_iter]
983 testlist_safe: test [(',' test)+ [',']]
984 */
985 expr_ty elt;
986 asdl_seq *listcomps;
987 int i, n_fors;
988 node *ch;
989
990 REQ(n, listmaker);
991 assert(NCH(n) > 1);
992
993 elt = ast_for_expr(c, CHILD(n, 0));
994 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996
997 n_fors = count_list_fors(n);
998 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000999 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001001 listcomps = asdl_seq_new(n_fors, c->c_arena);
1002 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001003 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001004
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 ch = CHILD(n, 1);
1006 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001007 comprehension_ty lc;
1008 asdl_seq *t;
1009 expr_ty expression;
1010 node *for_ch;
1011
1012 REQ(ch, list_for);
1013
1014 for_ch = CHILD(ch, 1);
1015 t = ast_for_exprlist(c, for_ch, Store);
1016 if (!t)
1017 return NULL;
1018 expression = ast_for_testlist(c, CHILD(ch, 3));
1019 if (!expression)
1020 return NULL;
1021
1022 /* Check the # of children rather than the length of t, since
1023 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1024 */
1025 if (NCH(for_ch) == 1)
1026 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1027 c->c_arena);
1028 else
1029 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1030 c->c_arena),
1031 expression, NULL, c->c_arena);
1032 if (!lc)
1033 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001035 if (NCH(ch) == 5) {
1036 int j, n_ifs;
1037 asdl_seq *ifs;
1038 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001040 ch = CHILD(ch, 4);
1041 n_ifs = count_list_ifs(ch);
1042 if (n_ifs == -1)
1043 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001045 ifs = asdl_seq_new(n_ifs, c->c_arena);
1046 if (!ifs)
1047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001049 for (j = 0; j < n_ifs; j++) {
1050 REQ(ch, list_iter);
1051 ch = CHILD(ch, 0);
1052 REQ(ch, list_if);
1053
1054 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1055 if (!list_for_expr)
1056 return NULL;
1057
1058 asdl_seq_SET(ifs, j, list_for_expr);
1059 if (NCH(ch) == 3)
1060 ch = CHILD(ch, 2);
1061 }
1062 /* on exit, must guarantee that ch is a list_for */
1063 if (TYPE(ch) == list_iter)
1064 ch = CHILD(ch, 0);
1065 lc->ifs = ifs;
1066 }
1067 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 }
1069
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001070 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071}
1072
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001073/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074
1075 Helper for ast_for_genexp().
1076*/
1077
1078static int
1079count_gen_fors(const node *n)
1080{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001081 int n_fors = 0;
1082 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083
1084 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001085 n_fors++;
1086 REQ(ch, gen_for);
1087 if (NCH(ch) == 5)
1088 ch = CHILD(ch, 4);
1089 else
1090 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001092 REQ(ch, gen_iter);
1093 ch = CHILD(ch, 0);
1094 if (TYPE(ch) == gen_for)
1095 goto count_gen_for;
1096 else if (TYPE(ch) == gen_if) {
1097 if (NCH(ch) == 3) {
1098 ch = CHILD(ch, 2);
1099 goto count_gen_iter;
1100 }
1101 else
1102 return n_fors;
1103 }
1104
1105 /* Should never be reached */
1106 PyErr_SetString(PyExc_SystemError,
1107 "logic error in count_gen_fors");
1108 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109}
1110
1111/* Count the number of 'if' statements in a generator expression.
1112
1113 Helper for ast_for_genexp().
1114*/
1115
1116static int
1117count_gen_ifs(const node *n)
1118{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001119 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001121 while (1) {
1122 REQ(n, gen_iter);
1123 if (TYPE(CHILD(n, 0)) == gen_for)
1124 return n_ifs;
1125 n = CHILD(n, 0);
1126 REQ(n, gen_if);
1127 n_ifs++;
1128 if (NCH(n) == 2)
1129 return n_ifs;
1130 n = CHILD(n, 2);
1131 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132}
1133
Jeremy Hyltona8293132006-02-28 17:58:27 +00001134/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135static expr_ty
1136ast_for_genexp(struct compiling *c, const node *n)
1137{
1138 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001139 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 expr_ty elt;
1141 asdl_seq *genexps;
1142 int i, n_fors;
1143 node *ch;
1144
1145 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1146 assert(NCH(n) > 1);
1147
1148 elt = ast_for_expr(c, CHILD(n, 0));
1149 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001150 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151
1152 n_fors = count_gen_fors(n);
1153 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001154 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001155
1156 genexps = asdl_seq_new(n_fors, c->c_arena);
1157 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001158 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 ch = CHILD(n, 1);
1161 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001162 comprehension_ty ge;
1163 asdl_seq *t;
1164 expr_ty expression;
1165 node *for_ch;
1166
1167 REQ(ch, gen_for);
1168
1169 for_ch = CHILD(ch, 1);
1170 t = ast_for_exprlist(c, for_ch, Store);
1171 if (!t)
1172 return NULL;
1173 expression = ast_for_expr(c, CHILD(ch, 3));
1174 if (!expression)
1175 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001176
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001177 /* Check the # of children rather than the length of t, since
1178 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1179 if (NCH(for_ch) == 1)
1180 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1181 NULL, c->c_arena);
1182 else
1183 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1184 c->c_arena),
1185 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001186
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001187 if (!ge)
1188 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001189
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001190 if (NCH(ch) == 5) {
1191 int j, n_ifs;
1192 asdl_seq *ifs;
1193
1194 ch = CHILD(ch, 4);
1195 n_ifs = count_gen_ifs(ch);
1196 if (n_ifs == -1)
1197 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001198
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001199 ifs = asdl_seq_new(n_ifs, c->c_arena);
1200 if (!ifs)
1201 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001202
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001203 for (j = 0; j < n_ifs; j++) {
1204 REQ(ch, gen_iter);
1205 ch = CHILD(ch, 0);
1206 REQ(ch, gen_if);
1207
1208 expression = ast_for_expr(c, CHILD(ch, 1));
1209 if (!expression)
1210 return NULL;
1211 asdl_seq_SET(ifs, j, expression);
1212 if (NCH(ch) == 3)
1213 ch = CHILD(ch, 2);
1214 }
1215 /* on exit, must guarantee that ch is a gen_for */
1216 if (TYPE(ch) == gen_iter)
1217 ch = CHILD(ch, 0);
1218 ge->ifs = ifs;
1219 }
1220 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 }
1222
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001223 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224}
1225
1226static expr_ty
1227ast_for_atom(struct compiling *c, const node *n)
1228{
1229 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1230 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1231 */
1232 node *ch = CHILD(n, 0);
1233
1234 switch (TYPE(ch)) {
1235 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001236 /* All names start in Load context, but may later be
1237 changed. */
1238 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1239 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001241 PyObject *str = parsestrplus(c, n);
1242 if (!str)
1243 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001244
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001245 PyArena_AddPyObject(c->c_arena, str);
1246 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 }
1248 case NUMBER: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001249 PyObject *pynum = parsenumber(STR(ch));
1250 if (!pynum)
1251 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001253 PyArena_AddPyObject(c->c_arena, pynum);
1254 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 }
1256 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001257 ch = CHILD(n, 1);
1258
1259 if (TYPE(ch) == RPAR)
1260 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1261
1262 if (TYPE(ch) == yield_expr)
1263 return ast_for_expr(c, ch);
1264
1265 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1266 return ast_for_genexp(c, ch);
1267
1268 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001270 ch = CHILD(n, 1);
1271
1272 if (TYPE(ch) == RSQB)
1273 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1274
1275 REQ(ch, listmaker);
1276 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1277 asdl_seq *elts = seq_for_testlist(c, ch);
1278 if (!elts)
1279 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001280
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001281 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1282 }
1283 else
1284 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001286 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1287 int i, size;
1288 asdl_seq *keys, *values;
1289
1290 ch = CHILD(n, 1);
1291 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1292 keys = asdl_seq_new(size, c->c_arena);
1293 if (!keys)
1294 return NULL;
1295
1296 values = asdl_seq_new(size, c->c_arena);
1297 if (!values)
1298 return NULL;
1299
1300 for (i = 0; i < NCH(ch); i += 4) {
1301 expr_ty expression;
1302
1303 expression = ast_for_expr(c, CHILD(ch, i));
1304 if (!expression)
1305 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001306
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001307 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001308
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001309 expression = ast_for_expr(c, CHILD(ch, i + 2));
1310 if (!expression)
1311 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001312
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001313 asdl_seq_SET(values, i / 4, expression);
1314 }
1315 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 }
1317 case BACKQUOTE: { /* repr */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001318 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
1319 if (!expression)
1320 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001321
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001322 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 }
1324 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001325 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1326 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 }
1328}
1329
1330static slice_ty
1331ast_for_slice(struct compiling *c, const node *n)
1332{
1333 node *ch;
1334 expr_ty lower = NULL, upper = NULL, step = NULL;
1335
1336 REQ(n, subscript);
1337
1338 /*
1339 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1340 sliceop: ':' [test]
1341 */
1342 ch = CHILD(n, 0);
1343 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001344 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345
1346 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001347 /* 'step' variable hold no significance in terms of being used over
1348 other vars */
1349 step = ast_for_expr(c, ch);
1350 if (!step)
1351 return NULL;
1352
1353 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 }
1355
1356 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001357 lower = ast_for_expr(c, ch);
1358 if (!lower)
1359 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 }
1361
1362 /* If there's an upper bound it's in the second or third position. */
1363 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001364 if (NCH(n) > 1) {
1365 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001367 if (TYPE(n2) == test) {
1368 upper = ast_for_expr(c, n2);
1369 if (!upper)
1370 return NULL;
1371 }
1372 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001374 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001376 if (TYPE(n2) == test) {
1377 upper = ast_for_expr(c, n2);
1378 if (!upper)
1379 return NULL;
1380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 }
1382
1383 ch = CHILD(n, NCH(n) - 1);
1384 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001385 if (NCH(ch) == 1) {
1386 /* No expression, so step is None */
1387 ch = CHILD(ch, 0);
1388 step = Name(new_identifier("None", c->c_arena), Load,
1389 LINENO(ch), ch->n_col_offset, c->c_arena);
1390 if (!step)
1391 return NULL;
1392 } else {
1393 ch = CHILD(ch, 1);
1394 if (TYPE(ch) == test) {
1395 step = ast_for_expr(c, ch);
1396 if (!step)
1397 return NULL;
1398 }
1399 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 }
1401
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001402 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403}
1404
1405static expr_ty
1406ast_for_binop(struct compiling *c, const node *n)
1407{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001408 /* Must account for a sequence of expressions.
1409 How should A op B op C by represented?
1410 BinOp(BinOp(A, op, B), op, C).
1411 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001413 int i, nops;
1414 expr_ty expr1, expr2, result;
1415 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001417 expr1 = ast_for_expr(c, CHILD(n, 0));
1418 if (!expr1)
1419 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001421 expr2 = ast_for_expr(c, CHILD(n, 2));
1422 if (!expr2)
1423 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001425 newoperator = get_operator(CHILD(n, 1));
1426 if (!newoperator)
1427 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001429 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1430 c->c_arena);
1431 if (!result)
1432 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001434 nops = (NCH(n) - 1) / 2;
1435 for (i = 1; i < nops; i++) {
1436 expr_ty tmp_result, tmp;
1437 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001439 newoperator = get_operator(next_oper);
1440 if (!newoperator)
1441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001443 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1444 if (!tmp)
1445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001447 tmp_result = BinOp(result, newoperator, tmp,
1448 LINENO(next_oper), next_oper->n_col_offset,
1449 c->c_arena);
1450 if (!tmp)
1451 return NULL;
1452 result = tmp_result;
1453 }
1454 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455}
1456
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001457static expr_ty
1458ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1459{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001460 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1461 subscriptlist: subscript (',' subscript)* [',']
1462 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1463 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001464 REQ(n, trailer);
1465 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001466 if (NCH(n) == 2)
1467 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1468 n->n_col_offset, c->c_arena);
1469 else
1470 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001471 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001472 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001473 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1474 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001475 }
1476 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001477 REQ(CHILD(n, 0), LSQB);
1478 REQ(CHILD(n, 2), RSQB);
1479 n = CHILD(n, 1);
1480 if (NCH(n) == 1) {
1481 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1482 if (!slc)
1483 return NULL;
1484 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1485 c->c_arena);
1486 }
1487 else {
1488 /* The grammar is ambiguous here. The ambiguity is resolved
1489 by treating the sequence as a tuple literal if there are
1490 no slice features.
1491 */
1492 int j;
1493 slice_ty slc;
1494 expr_ty e;
1495 bool simple = true;
1496 asdl_seq *slices, *elts;
1497 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1498 if (!slices)
1499 return NULL;
1500 for (j = 0; j < NCH(n); j += 2) {
1501 slc = ast_for_slice(c, CHILD(n, j));
1502 if (!slc)
1503 return NULL;
1504 if (slc->kind != Index_kind)
1505 simple = false;
1506 asdl_seq_SET(slices, j / 2, slc);
1507 }
1508 if (!simple) {
1509 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1510 Load, LINENO(n), n->n_col_offset, c->c_arena);
1511 }
1512 /* extract Index values and put them in a Tuple */
1513 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1514 if (!elts)
1515 return NULL;
1516 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1517 slc = (slice_ty)asdl_seq_GET(slices, j);
1518 assert(slc->kind == Index_kind && slc->v.Index.value);
1519 asdl_seq_SET(elts, j, slc->v.Index.value);
1520 }
1521 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1522 if (!e)
1523 return NULL;
1524 return Subscript(left_expr, Index(e, c->c_arena),
1525 Load, LINENO(n), n->n_col_offset, c->c_arena);
1526 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001527 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001528}
1529
1530static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001531ast_for_factor(struct compiling *c, const node *n)
1532{
1533 node *pfactor, *ppower, *patom, *pnum;
1534 expr_ty expression;
1535
1536 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001537 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001538 constant. The peephole optimizer already does something like
1539 this but it doesn't handle the case where the constant is
1540 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1541 PyLongObject.
1542 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001543 if (TYPE(CHILD(n, 0)) == MINUS &&
1544 NCH(n) == 2 &&
1545 TYPE((pfactor = CHILD(n, 1))) == factor &&
1546 NCH(pfactor) == 1 &&
1547 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1548 NCH(ppower) == 1 &&
1549 TYPE((patom = CHILD(ppower, 0))) == atom &&
1550 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1551 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1552 if (s == NULL)
1553 return NULL;
1554 s[0] = '-';
1555 strcpy(s + 1, STR(pnum));
1556 PyObject_FREE(STR(pnum));
1557 STR(pnum) = s;
1558 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001559 }
1560
1561 expression = ast_for_expr(c, CHILD(n, 1));
1562 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001563 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001564
1565 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001566 case PLUS:
1567 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1568 c->c_arena);
1569 case MINUS:
1570 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1571 c->c_arena);
1572 case TILDE:
1573 return UnaryOp(Invert, expression, LINENO(n),
1574 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001575 }
1576 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001577 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001578 return NULL;
1579}
1580
1581static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001582ast_for_power(struct compiling *c, const node *n)
1583{
1584 /* power: atom trailer* ('**' factor)*
1585 */
1586 int i;
1587 expr_ty e, tmp;
1588 REQ(n, power);
1589 e = ast_for_atom(c, CHILD(n, 0));
1590 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001591 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001592 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001593 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001594 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001595 node *ch = CHILD(n, i);
1596 if (TYPE(ch) != trailer)
1597 break;
1598 tmp = ast_for_trailer(c, ch, e);
1599 if (!tmp)
1600 return NULL;
1601 tmp->lineno = e->lineno;
1602 tmp->col_offset = e->col_offset;
1603 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001604 }
1605 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001606 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1607 if (!f)
1608 return NULL;
1609 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1610 if (!tmp)
1611 return NULL;
1612 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001613 }
1614 return e;
1615}
1616
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617/* Do not name a variable 'expr'! Will cause a compile error.
1618*/
1619
1620static expr_ty
1621ast_for_expr(struct compiling *c, const node *n)
1622{
1623 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001624 test: or_test ['if' or_test 'else' test] | lambdef
1625 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 and_test: not_test ('and' not_test)*
1627 not_test: 'not' not_test | comparison
1628 comparison: expr (comp_op expr)*
1629 expr: xor_expr ('|' xor_expr)*
1630 xor_expr: and_expr ('^' and_expr)*
1631 and_expr: shift_expr ('&' shift_expr)*
1632 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1633 arith_expr: term (('+'|'-') term)*
1634 term: factor (('*'|'/'|'%'|'//') factor)*
1635 factor: ('+'|'-'|'~') factor | power
1636 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001637
1638 As well as modified versions that exist for backward compatibility,
1639 to explicitly allow:
1640 [ x for x in lambda: 0, lambda: 1 ]
1641 (which would be ambiguous without these extra rules)
1642
1643 old_test: or_test | old_lambdef
1644 old_lambdef: 'lambda' [vararglist] ':' old_test
1645
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646 */
1647
1648 asdl_seq *seq;
1649 int i;
1650
1651 loop:
1652 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001653 case test:
1654 case old_test:
1655 if (TYPE(CHILD(n, 0)) == lambdef ||
1656 TYPE(CHILD(n, 0)) == old_lambdef)
1657 return ast_for_lambdef(c, CHILD(n, 0));
1658 else if (NCH(n) > 1)
1659 return ast_for_ifexpr(c, n);
1660 /* Fallthrough */
1661 case or_test:
1662 case and_test:
1663 if (NCH(n) == 1) {
1664 n = CHILD(n, 0);
1665 goto loop;
1666 }
1667 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1668 if (!seq)
1669 return NULL;
1670 for (i = 0; i < NCH(n); i += 2) {
1671 expr_ty e = ast_for_expr(c, CHILD(n, i));
1672 if (!e)
1673 return NULL;
1674 asdl_seq_SET(seq, i / 2, e);
1675 }
1676 if (!strcmp(STR(CHILD(n, 1)), "and"))
1677 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1678 c->c_arena);
1679 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1680 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1681 case not_test:
1682 if (NCH(n) == 1) {
1683 n = CHILD(n, 0);
1684 goto loop;
1685 }
1686 else {
1687 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1688 if (!expression)
1689 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001691 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1692 c->c_arena);
1693 }
1694 case comparison:
1695 if (NCH(n) == 1) {
1696 n = CHILD(n, 0);
1697 goto loop;
1698 }
1699 else {
1700 expr_ty expression;
1701 asdl_int_seq *ops;
1702 asdl_seq *cmps;
1703 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1704 if (!ops)
1705 return NULL;
1706 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1707 if (!cmps) {
1708 return NULL;
1709 }
1710 for (i = 1; i < NCH(n); i += 2) {
1711 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001713 newoperator = ast_for_comp_op(CHILD(n, i));
1714 if (!newoperator) {
1715 return NULL;
1716 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001718 expression = ast_for_expr(c, CHILD(n, i + 1));
1719 if (!expression) {
1720 return NULL;
1721 }
1722
1723 asdl_seq_SET(ops, i / 2, newoperator);
1724 asdl_seq_SET(cmps, i / 2, expression);
1725 }
1726 expression = ast_for_expr(c, CHILD(n, 0));
1727 if (!expression) {
1728 return NULL;
1729 }
1730
1731 return Compare(expression, ops, cmps, LINENO(n),
1732 n->n_col_offset, c->c_arena);
1733 }
1734 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001736 /* The next five cases all handle BinOps. The main body of code
1737 is the same in each case, but the switch turned inside out to
1738 reuse the code for each type of operator.
1739 */
1740 case expr:
1741 case xor_expr:
1742 case and_expr:
1743 case shift_expr:
1744 case arith_expr:
1745 case term:
1746 if (NCH(n) == 1) {
1747 n = CHILD(n, 0);
1748 goto loop;
1749 }
1750 return ast_for_binop(c, n);
1751 case yield_expr: {
1752 expr_ty exp = NULL;
1753 if (NCH(n) == 2) {
1754 exp = ast_for_testlist(c, CHILD(n, 1));
1755 if (!exp)
1756 return NULL;
1757 }
1758 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1759 }
1760 case factor:
1761 if (NCH(n) == 1) {
1762 n = CHILD(n, 0);
1763 goto loop;
1764 }
1765 return ast_for_factor(c, n);
1766 case power:
1767 return ast_for_power(c, n);
1768 default:
1769 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1770 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001772 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773 return NULL;
1774}
1775
1776static expr_ty
1777ast_for_call(struct compiling *c, const node *n, expr_ty func)
1778{
1779 /*
1780 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001781 | '**' test)
1782 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783 */
1784
1785 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001786 asdl_seq *args;
1787 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788 expr_ty vararg = NULL, kwarg = NULL;
1789
1790 REQ(n, arglist);
1791
1792 nargs = 0;
1793 nkeywords = 0;
1794 ngens = 0;
1795 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001796 node *ch = CHILD(n, i);
1797 if (TYPE(ch) == argument) {
1798 if (NCH(ch) == 1)
1799 nargs++;
1800 else if (TYPE(CHILD(ch, 1)) == gen_for)
1801 ngens++;
1802 else
1803 nkeywords++;
1804 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 }
1806 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001807 ast_error(n, "Generator expression must be parenthesized "
1808 "if not sole argument");
1809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 }
1811
1812 if (nargs + nkeywords + ngens > 255) {
1813 ast_error(n, "more than 255 arguments");
1814 return NULL;
1815 }
1816
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001819 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001820 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001822 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 nargs = 0;
1824 nkeywords = 0;
1825 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001826 node *ch = CHILD(n, i);
1827 if (TYPE(ch) == argument) {
1828 expr_ty e;
1829 if (NCH(ch) == 1) {
1830 if (nkeywords) {
1831 ast_error(CHILD(ch, 0),
1832 "non-keyword arg after keyword arg");
1833 return NULL;
1834 }
1835 e = ast_for_expr(c, CHILD(ch, 0));
1836 if (!e)
1837 return NULL;
1838 asdl_seq_SET(args, nargs++, e);
1839 }
1840 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1841 e = ast_for_genexp(c, ch);
1842 if (!e)
1843 return NULL;
1844 asdl_seq_SET(args, nargs++, e);
1845 }
1846 else {
1847 keyword_ty kw;
1848 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001850 /* CHILD(ch, 0) is test, but must be an identifier? */
1851 e = ast_for_expr(c, CHILD(ch, 0));
1852 if (!e)
1853 return NULL;
1854 /* f(lambda x: x[0] = 3) ends up getting parsed with
1855 * LHS test = lambda x: x[0], and RHS test = 3.
1856 * SF bug 132313 points out that complaining about a keyword
1857 * then is very confusing.
1858 */
1859 if (e->kind == Lambda_kind) {
1860 ast_error(CHILD(ch, 0),
1861 "lambda cannot contain assignment");
1862 return NULL;
1863 } else if (e->kind != Name_kind) {
1864 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1865 return NULL;
1866 }
1867 key = e->v.Name.id;
1868 e = ast_for_expr(c, CHILD(ch, 2));
1869 if (!e)
1870 return NULL;
1871 kw = keyword(key, e, c->c_arena);
1872 if (!kw)
1873 return NULL;
1874 asdl_seq_SET(keywords, nkeywords++, kw);
1875 }
1876 }
1877 else if (TYPE(ch) == STAR) {
1878 vararg = ast_for_expr(c, CHILD(n, i+1));
1879 i++;
1880 }
1881 else if (TYPE(ch) == DOUBLESTAR) {
1882 kwarg = ast_for_expr(c, CHILD(n, i+1));
1883 i++;
1884 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 }
1886
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001887 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1888 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889}
1890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001892ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001894 /* testlist_gexp: test (',' test)* [','] */
1895 /* testlist: test (',' test)* [','] */
1896 /* testlist_safe: test (',' test)+ [','] */
1897 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001899 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001900 if (NCH(n) > 1)
1901 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001902 }
1903 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001904 assert(TYPE(n) == testlist ||
1905 TYPE(n) == testlist_safe ||
1906 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001907 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001909 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001911 asdl_seq *tmp = seq_for_testlist(c, n);
1912 if (!tmp)
1913 return NULL;
1914 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001916}
1917
1918static expr_ty
1919ast_for_testlist_gexp(struct compiling *c, const node* n)
1920{
1921 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1922 /* argument: test [ gen_for ] */
1923 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001924 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001925 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001926 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001927}
1928
1929/* like ast_for_testlist() but returns a sequence */
1930static asdl_seq*
1931ast_for_class_bases(struct compiling *c, const node* n)
1932{
1933 /* testlist: test (',' test)* [','] */
1934 assert(NCH(n) > 0);
1935 REQ(n, testlist);
1936 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001937 expr_ty base;
1938 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
1939 if (!bases)
1940 return NULL;
1941 base = ast_for_expr(c, CHILD(n, 0));
1942 if (!base)
1943 return NULL;
1944 asdl_seq_SET(bases, 0, base);
1945 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001946 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001947
1948 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949}
1950
1951static stmt_ty
1952ast_for_expr_stmt(struct compiling *c, const node *n)
1953{
1954 REQ(n, expr_stmt);
1955 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001956 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 testlist: test (',' test)* [',']
1958 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001959 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 test: ... here starts the operator precendence dance
1961 */
1962
1963 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001964 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
1965 if (!e)
1966 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001968 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 }
1970 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001971 expr_ty expr1, expr2;
1972 operator_ty newoperator;
1973 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001975 expr1 = ast_for_testlist(c, ch);
1976 if (!expr1)
1977 return NULL;
1978 /* TODO(nas): Remove duplicated error checks (set_context does it) */
1979 switch (expr1->kind) {
1980 case GeneratorExp_kind:
1981 ast_error(ch, "augmented assignment to generator "
1982 "expression not possible");
1983 return NULL;
1984 case Yield_kind:
1985 ast_error(ch, "augmented assignment to yield "
1986 "expression not possible");
1987 return NULL;
1988 case Name_kind: {
1989 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1990 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1991 ast_error(ch, "assignment to None");
1992 return NULL;
1993 }
1994 break;
1995 }
1996 case Attribute_kind:
1997 case Subscript_kind:
1998 break;
1999 default:
2000 ast_error(ch, "illegal expression for augmented "
2001 "assignment");
2002 return NULL;
2003 }
2004 if(!set_context(expr1, Store, ch))
2005 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002007 ch = CHILD(n, 2);
2008 if (TYPE(ch) == testlist)
2009 expr2 = ast_for_testlist(c, ch);
2010 else
2011 expr2 = ast_for_expr(c, ch);
2012 if (!expr2)
2013 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002015 newoperator = ast_for_augassign(CHILD(n, 1));
2016 if (!newoperator)
2017 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002019 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2020 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 }
2022 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002023 int i;
2024 asdl_seq *targets;
2025 node *value;
2026 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002028 /* a normal assignment */
2029 REQ(CHILD(n, 1), EQUAL);
2030 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2031 if (!targets)
2032 return NULL;
2033 for (i = 0; i < NCH(n) - 2; i += 2) {
2034 expr_ty e;
2035 node *ch = CHILD(n, i);
2036 if (TYPE(ch) == yield_expr) {
2037 ast_error(ch, "assignment to yield expression not possible");
2038 return NULL;
2039 }
2040 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002042 /* set context to assign */
2043 if (!e)
2044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002046 if (!set_context(e, Store, CHILD(n, i)))
2047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002049 asdl_seq_SET(targets, i / 2, e);
2050 }
2051 value = CHILD(n, NCH(n) - 1);
2052 if (TYPE(value) == testlist)
2053 expression = ast_for_testlist(c, value);
2054 else
2055 expression = ast_for_expr(c, value);
2056 if (!expression)
2057 return NULL;
2058 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2059 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061}
2062
2063static stmt_ty
2064ast_for_print_stmt(struct compiling *c, const node *n)
2065{
2066 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002067 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 */
2069 expr_ty dest = NULL, expression;
2070 asdl_seq *seq;
2071 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002072 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073
2074 REQ(n, print_stmt);
2075 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002076 dest = ast_for_expr(c, CHILD(n, 2));
2077 if (!dest)
2078 return NULL;
2079 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002081 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002083 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002084 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002085 expression = ast_for_expr(c, CHILD(n, i));
2086 if (!expression)
2087 return NULL;
2088 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 }
2090 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002091 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092}
2093
2094static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002095ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096{
2097 asdl_seq *seq;
2098 int i;
2099 expr_ty e;
2100
2101 REQ(n, exprlist);
2102
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002103 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002105 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002107 e = ast_for_expr(c, CHILD(n, i));
2108 if (!e)
2109 return NULL;
2110 asdl_seq_SET(seq, i / 2, e);
2111 if (context && !set_context(e, context, CHILD(n, i)))
2112 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 }
2114 return seq;
2115}
2116
2117static stmt_ty
2118ast_for_del_stmt(struct compiling *c, const node *n)
2119{
2120 asdl_seq *expr_list;
2121
2122 /* del_stmt: 'del' exprlist */
2123 REQ(n, del_stmt);
2124
2125 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2126 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002127 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002128 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129}
2130
2131static stmt_ty
2132ast_for_flow_stmt(struct compiling *c, const node *n)
2133{
2134 /*
2135 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002136 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 break_stmt: 'break'
2138 continue_stmt: 'continue'
2139 return_stmt: 'return' [testlist]
2140 yield_stmt: yield_expr
2141 yield_expr: 'yield' testlist
2142 raise_stmt: 'raise' [test [',' test [',' test]]]
2143 */
2144 node *ch;
2145
2146 REQ(n, flow_stmt);
2147 ch = CHILD(n, 0);
2148 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002149 case break_stmt:
2150 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2151 case continue_stmt:
2152 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2153 case yield_stmt: { /* will reduce to yield_expr */
2154 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2155 if (!exp)
2156 return NULL;
2157 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2158 }
2159 case return_stmt:
2160 if (NCH(ch) == 1)
2161 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2162 else {
2163 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2164 if (!expression)
2165 return NULL;
2166 return Return(expression, LINENO(n), n->n_col_offset,
2167 c->c_arena);
2168 }
2169 case raise_stmt:
2170 if (NCH(ch) == 1)
2171 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2172 c->c_arena);
2173 else if (NCH(ch) == 2) {
2174 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2175 if (!expression)
2176 return NULL;
2177 return Raise(expression, NULL, NULL, LINENO(n),
2178 n->n_col_offset, c->c_arena);
2179 }
2180 else if (NCH(ch) == 4) {
2181 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002183 expr1 = ast_for_expr(c, CHILD(ch, 1));
2184 if (!expr1)
2185 return NULL;
2186 expr2 = ast_for_expr(c, CHILD(ch, 3));
2187 if (!expr2)
2188 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002190 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2191 c->c_arena);
2192 }
2193 else if (NCH(ch) == 6) {
2194 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002196 expr1 = ast_for_expr(c, CHILD(ch, 1));
2197 if (!expr1)
2198 return NULL;
2199 expr2 = ast_for_expr(c, CHILD(ch, 3));
2200 if (!expr2)
2201 return NULL;
2202 expr3 = ast_for_expr(c, CHILD(ch, 5));
2203 if (!expr3)
2204 return NULL;
2205
2206 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2207 c->c_arena);
2208 }
2209 default:
2210 PyErr_Format(PyExc_SystemError,
2211 "unexpected flow_stmt: %d", TYPE(ch));
2212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002214
2215 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2216 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217}
2218
2219static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002220alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221{
2222 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002223 import_as_name: NAME ['as' NAME]
2224 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 dotted_name: NAME ('.' NAME)*
2226 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002227 PyObject *str;
2228
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 loop:
2230 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002231 case import_as_name:
2232 str = NULL;
2233 if (NCH(n) == 3) {
2234 str = NEW_IDENTIFIER(CHILD(n, 2));
2235 }
2236 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2237 case dotted_as_name:
2238 if (NCH(n) == 1) {
2239 n = CHILD(n, 0);
2240 goto loop;
2241 }
2242 else {
2243 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2244 if (!a)
2245 return NULL;
2246 assert(!a->asname);
2247 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2248 return a;
2249 }
2250 break;
2251 case dotted_name:
2252 if (NCH(n) == 1)
2253 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2254 else {
2255 /* Create a string of the form "a.b.c" */
2256 int i;
2257 size_t len;
2258 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002260 len = 0;
2261 for (i = 0; i < NCH(n); i += 2)
2262 /* length of string plus one for the dot */
2263 len += strlen(STR(CHILD(n, i))) + 1;
2264 len--; /* the last name doesn't have a dot */
2265 str = PyString_FromStringAndSize(NULL, len);
2266 if (!str)
2267 return NULL;
2268 s = PyString_AS_STRING(str);
2269 if (!s)
2270 return NULL;
2271 for (i = 0; i < NCH(n); i += 2) {
2272 char *sch = STR(CHILD(n, i));
2273 strcpy(s, STR(CHILD(n, i)));
2274 s += strlen(sch);
2275 *s++ = '.';
2276 }
2277 --s;
2278 *s = '\0';
2279 PyString_InternInPlace(&str);
2280 PyArena_AddPyObject(c->c_arena, str);
2281 return alias(str, NULL, c->c_arena);
2282 }
2283 break;
2284 case STAR:
2285 str = PyString_InternFromString("*");
2286 PyArena_AddPyObject(c->c_arena, str);
2287 return alias(str, NULL, c->c_arena);
2288 default:
2289 PyErr_Format(PyExc_SystemError,
2290 "unexpected import name: %d", TYPE(n));
2291 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002293
2294 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 return NULL;
2296}
2297
2298static stmt_ty
2299ast_for_import_stmt(struct compiling *c, const node *n)
2300{
2301 /*
2302 import_stmt: import_name | import_from
2303 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002304 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002305 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002307 int lineno;
2308 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 int i;
2310 asdl_seq *aliases;
2311
2312 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002313 lineno = LINENO(n);
2314 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002316 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002317 n = CHILD(n, 1);
2318 REQ(n, dotted_as_names);
2319 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2320 if (!aliases)
2321 return NULL;
2322 for (i = 0; i < NCH(n); i += 2) {
2323 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2324 if (!import_alias)
2325 return NULL;
2326 asdl_seq_SET(aliases, i / 2, import_alias);
2327 }
2328 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002329 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002330 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002331 int n_children;
2332 int idx, ndots = 0;
2333 alias_ty mod = NULL;
2334 identifier modname;
2335
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002336 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002337 optional module name */
2338 for (idx = 1; idx < NCH(n); idx++) {
2339 if (TYPE(CHILD(n, idx)) == dotted_name) {
2340 mod = alias_for_import_name(c, CHILD(n, idx));
2341 idx++;
2342 break;
2343 } else if (TYPE(CHILD(n, idx)) != DOT) {
2344 break;
2345 }
2346 ndots++;
2347 }
2348 idx++; /* skip over the 'import' keyword */
2349 switch (TYPE(CHILD(n, idx))) {
2350 case STAR:
2351 /* from ... import * */
2352 n = CHILD(n, idx);
2353 n_children = 1;
2354 if (ndots) {
2355 ast_error(n, "'import *' not allowed with 'from .'");
2356 return NULL;
2357 }
2358 break;
2359 case LPAR:
2360 /* from ... import (x, y, z) */
2361 n = CHILD(n, idx + 1);
2362 n_children = NCH(n);
2363 break;
2364 case import_as_names:
2365 /* from ... import x, y, z */
2366 n = CHILD(n, idx);
2367 n_children = NCH(n);
2368 if (n_children % 2 == 0) {
2369 ast_error(n, "trailing comma not allowed without"
2370 " surrounding parentheses");
2371 return NULL;
2372 }
2373 break;
2374 default:
2375 ast_error(n, "Unexpected node-type in from-import");
2376 return NULL;
2377 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002379 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2380 if (!aliases)
2381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002383 /* handle "from ... import *" special b/c there's no children */
2384 if (TYPE(n) == STAR) {
2385 alias_ty import_alias = alias_for_import_name(c, n);
2386 if (!import_alias)
2387 return NULL;
2388 asdl_seq_SET(aliases, 0, import_alias);
2389 }
2390 else {
2391 for (i = 0; i < NCH(n); i += 2) {
2392 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2393 if (!import_alias)
2394 return NULL;
2395 asdl_seq_SET(aliases, i / 2, import_alias);
2396 }
2397 }
2398 if (mod != NULL)
2399 modname = mod->name;
2400 else
2401 modname = new_identifier("", c->c_arena);
2402 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2403 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 }
Neal Norwitz79792652005-11-14 04:25:03 +00002405 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002406 "unknown import statement: starts with command '%s'",
2407 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 return NULL;
2409}
2410
2411static stmt_ty
2412ast_for_global_stmt(struct compiling *c, const node *n)
2413{
2414 /* global_stmt: 'global' NAME (',' NAME)* */
2415 identifier name;
2416 asdl_seq *s;
2417 int i;
2418
2419 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002420 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002422 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002424 name = NEW_IDENTIFIER(CHILD(n, i));
2425 if (!name)
2426 return NULL;
2427 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002429 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430}
2431
2432static stmt_ty
2433ast_for_exec_stmt(struct compiling *c, const node *n)
2434{
2435 expr_ty expr1, globals = NULL, locals = NULL;
2436 int n_children = NCH(n);
2437 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002438 PyErr_Format(PyExc_SystemError,
2439 "poorly formed 'exec' statement: %d parts to statement",
2440 n_children);
2441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 }
2443
2444 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2445 REQ(n, exec_stmt);
2446 expr1 = ast_for_expr(c, CHILD(n, 1));
2447 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002450 globals = ast_for_expr(c, CHILD(n, 3));
2451 if (!globals)
2452 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 }
2454 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002455 locals = ast_for_expr(c, CHILD(n, 5));
2456 if (!locals)
2457 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 }
2459
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002460 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2461 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462}
2463
2464static stmt_ty
2465ast_for_assert_stmt(struct compiling *c, const node *n)
2466{
2467 /* assert_stmt: 'assert' test [',' test] */
2468 REQ(n, assert_stmt);
2469 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002470 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2471 if (!expression)
2472 return NULL;
2473 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2474 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 }
2476 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002477 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002479 expr1 = ast_for_expr(c, CHILD(n, 1));
2480 if (!expr1)
2481 return NULL;
2482 expr2 = ast_for_expr(c, CHILD(n, 3));
2483 if (!expr2)
2484 return NULL;
2485
2486 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 }
Neal Norwitz79792652005-11-14 04:25:03 +00002488 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002489 "improper number of parts to 'assert' statement: %d",
2490 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 return NULL;
2492}
2493
2494static asdl_seq *
2495ast_for_suite(struct compiling *c, const node *n)
2496{
2497 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002498 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 stmt_ty s;
2500 int i, total, num, end, pos = 0;
2501 node *ch;
2502
2503 REQ(n, suite);
2504
2505 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002506 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002510 n = CHILD(n, 0);
2511 /* simple_stmt always ends with a NEWLINE,
2512 and may have a trailing SEMI
2513 */
2514 end = NCH(n) - 1;
2515 if (TYPE(CHILD(n, end - 1)) == SEMI)
2516 end--;
2517 /* loop by 2 to skip semi-colons */
2518 for (i = 0; i < end; i += 2) {
2519 ch = CHILD(n, i);
2520 s = ast_for_stmt(c, ch);
2521 if (!s)
2522 return NULL;
2523 asdl_seq_SET(seq, pos++, s);
2524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 }
2526 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002527 for (i = 2; i < (NCH(n) - 1); i++) {
2528 ch = CHILD(n, i);
2529 REQ(ch, stmt);
2530 num = num_stmts(ch);
2531 if (num == 1) {
2532 /* small_stmt or compound_stmt with only one child */
2533 s = ast_for_stmt(c, ch);
2534 if (!s)
2535 return NULL;
2536 asdl_seq_SET(seq, pos++, s);
2537 }
2538 else {
2539 int j;
2540 ch = CHILD(ch, 0);
2541 REQ(ch, simple_stmt);
2542 for (j = 0; j < NCH(ch); j += 2) {
2543 /* statement terminates with a semi-colon ';' */
2544 if (NCH(CHILD(ch, j)) == 0) {
2545 assert((j + 1) == NCH(ch));
2546 break;
2547 }
2548 s = ast_for_stmt(c, CHILD(ch, j));
2549 if (!s)
2550 return NULL;
2551 asdl_seq_SET(seq, pos++, s);
2552 }
2553 }
2554 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 }
2556 assert(pos == seq->size);
2557 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558}
2559
2560static stmt_ty
2561ast_for_if_stmt(struct compiling *c, const node *n)
2562{
2563 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2564 ['else' ':' suite]
2565 */
2566 char *s;
2567
2568 REQ(n, if_stmt);
2569
2570 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002571 expr_ty expression;
2572 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002574 expression = ast_for_expr(c, CHILD(n, 1));
2575 if (!expression)
2576 return NULL;
2577 suite_seq = ast_for_suite(c, CHILD(n, 3));
2578 if (!suite_seq)
2579 return NULL;
2580
2581 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2582 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002584
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 s = STR(CHILD(n, 4));
2586 /* s[2], the third character in the string, will be
2587 's' for el_s_e, or
2588 'i' for el_i_f
2589 */
2590 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002591 expr_ty expression;
2592 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002594 expression = ast_for_expr(c, CHILD(n, 1));
2595 if (!expression)
2596 return NULL;
2597 seq1 = ast_for_suite(c, CHILD(n, 3));
2598 if (!seq1)
2599 return NULL;
2600 seq2 = ast_for_suite(c, CHILD(n, 6));
2601 if (!seq2)
2602 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002604 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2605 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 }
2607 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002608 int i, n_elif, has_else = 0;
2609 expr_ty expression;
2610 asdl_seq *suite_seq;
2611 asdl_seq *orelse = NULL;
2612 n_elif = NCH(n) - 4;
2613 /* must reference the child n_elif+1 since 'else' token is third,
2614 not fourth, child from the end. */
2615 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2616 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2617 has_else = 1;
2618 n_elif -= 3;
2619 }
2620 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002622 if (has_else) {
2623 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002625 orelse = asdl_seq_new(1, c->c_arena);
2626 if (!orelse)
2627 return NULL;
2628 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2629 if (!expression)
2630 return NULL;
2631 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2632 if (!suite_seq)
2633 return NULL;
2634 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2635 if (!suite_seq2)
2636 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002638 asdl_seq_SET(orelse, 0,
2639 If(expression, suite_seq, suite_seq2,
2640 LINENO(CHILD(n, NCH(n) - 6)),
2641 CHILD(n, NCH(n) - 6)->n_col_offset,
2642 c->c_arena));
2643 /* the just-created orelse handled the last elif */
2644 n_elif--;
2645 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002647 for (i = 0; i < n_elif; i++) {
2648 int off = 5 + (n_elif - i - 1) * 4;
2649 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2650 if (!newobj)
2651 return NULL;
2652 expression = ast_for_expr(c, CHILD(n, off));
2653 if (!expression)
2654 return NULL;
2655 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2656 if (!suite_seq)
2657 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002659 asdl_seq_SET(newobj, 0,
2660 If(expression, suite_seq, orelse,
2661 LINENO(CHILD(n, off)),
2662 CHILD(n, off)->n_col_offset, c->c_arena));
2663 orelse = newobj;
2664 }
2665 expression = ast_for_expr(c, CHILD(n, 1));
2666 if (!expression)
2667 return NULL;
2668 suite_seq = ast_for_suite(c, CHILD(n, 3));
2669 if (!suite_seq)
2670 return NULL;
2671 return If(expression, suite_seq, orelse,
2672 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002674
2675 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002676 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002677 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678}
2679
2680static stmt_ty
2681ast_for_while_stmt(struct compiling *c, const node *n)
2682{
2683 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2684 REQ(n, while_stmt);
2685
2686 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002687 expr_ty expression;
2688 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002690 expression = ast_for_expr(c, CHILD(n, 1));
2691 if (!expression)
2692 return NULL;
2693 suite_seq = ast_for_suite(c, CHILD(n, 3));
2694 if (!suite_seq)
2695 return NULL;
2696 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2697 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 }
2699 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002700 expr_ty expression;
2701 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002703 expression = ast_for_expr(c, CHILD(n, 1));
2704 if (!expression)
2705 return NULL;
2706 seq1 = ast_for_suite(c, CHILD(n, 3));
2707 if (!seq1)
2708 return NULL;
2709 seq2 = ast_for_suite(c, CHILD(n, 6));
2710 if (!seq2)
2711 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002713 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2714 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002716
2717 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002718 "wrong number of tokens for 'while' statement: %d",
2719 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002720 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721}
2722
2723static stmt_ty
2724ast_for_for_stmt(struct compiling *c, const node *n)
2725{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002726 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727 expr_ty expression;
2728 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002729 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2731 REQ(n, for_stmt);
2732
2733 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002734 seq = ast_for_suite(c, CHILD(n, 8));
2735 if (!seq)
2736 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 }
2738
Neal Norwitzedef2be2006-07-12 05:26:17 +00002739 node_target = CHILD(n, 1);
2740 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002741 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002742 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002743 /* Check the # of children rather than the length of _target, since
2744 for x, in ... has 1 element in _target, but still requires a Tuple. */
2745 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002746 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002748 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002750 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002751 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002752 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002754 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002757 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002758 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759}
2760
2761static excepthandler_ty
2762ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2763{
2764 /* except_clause: 'except' [test [',' test]] */
2765 REQ(exc, except_clause);
2766 REQ(body, suite);
2767
2768 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002769 asdl_seq *suite_seq = ast_for_suite(c, body);
2770 if (!suite_seq)
2771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002773 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2774 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 }
2776 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002777 expr_ty expression;
2778 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002780 expression = ast_for_expr(c, CHILD(exc, 1));
2781 if (!expression)
2782 return NULL;
2783 suite_seq = ast_for_suite(c, body);
2784 if (!suite_seq)
2785 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002787 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2788 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 }
2790 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002791 asdl_seq *suite_seq;
2792 expr_ty expression;
2793 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2794 if (!e)
2795 return NULL;
2796 if (!set_context(e, Store, CHILD(exc, 3)))
2797 return NULL;
2798 expression = ast_for_expr(c, CHILD(exc, 1));
2799 if (!expression)
2800 return NULL;
2801 suite_seq = ast_for_suite(c, body);
2802 if (!suite_seq)
2803 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002805 return excepthandler(expression, e, suite_seq, LINENO(exc),
2806 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002808
2809 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002810 "wrong number of children for 'except' clause: %d",
2811 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002812 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813}
2814
2815static stmt_ty
2816ast_for_try_stmt(struct compiling *c, const node *n)
2817{
Neal Norwitzf599f422005-12-17 21:33:47 +00002818 const int nch = NCH(n);
2819 int n_except = (nch - 3)/3;
2820 asdl_seq *body, *orelse = NULL, *finally = NULL;
2821
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 REQ(n, try_stmt);
2823
Neal Norwitzf599f422005-12-17 21:33:47 +00002824 body = ast_for_suite(c, CHILD(n, 2));
2825 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
Neal Norwitzf599f422005-12-17 21:33:47 +00002828 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002829 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2830 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2831 /* we can assume it's an "else",
2832 because nch >= 9 for try-else-finally and
2833 it would otherwise have a type of except_clause */
2834 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2835 if (orelse == NULL)
2836 return NULL;
2837 n_except--;
2838 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002840 finally = ast_for_suite(c, CHILD(n, nch - 1));
2841 if (finally == NULL)
2842 return NULL;
2843 n_except--;
2844 }
2845 else {
2846 /* we can assume it's an "else",
2847 otherwise it would have a type of except_clause */
2848 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2849 if (orelse == NULL)
2850 return NULL;
2851 n_except--;
2852 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002854 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002855 ast_error(n, "malformed 'try' statement");
2856 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002858
2859 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002860 int i;
2861 stmt_ty except_st;
2862 /* process except statements to create a try ... except */
2863 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2864 if (handlers == NULL)
2865 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002866
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002867 for (i = 0; i < n_except; i++) {
2868 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2869 CHILD(n, 5 + i * 3));
2870 if (!e)
2871 return NULL;
2872 asdl_seq_SET(handlers, i, e);
2873 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002874
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002875 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2876 n->n_col_offset, c->c_arena);
2877 if (!finally)
2878 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002879
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002880 /* if a 'finally' is present too, we nest the TryExcept within a
2881 TryFinally to emulate try ... except ... finally */
2882 body = asdl_seq_new(1, c->c_arena);
2883 if (body == NULL)
2884 return NULL;
2885 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002886 }
2887
2888 /* must be a try ... finally (except clauses are in body, if any exist) */
2889 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002890 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891}
2892
Guido van Rossumc2e20742006-02-27 22:32:47 +00002893static expr_ty
2894ast_for_with_var(struct compiling *c, const node *n)
2895{
2896 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002897 return ast_for_expr(c, CHILD(n, 1));
2898}
2899
2900/* with_stmt: 'with' test [ with_var ] ':' suite */
2901static stmt_ty
2902ast_for_with_stmt(struct compiling *c, const node *n)
2903{
2904 expr_ty context_expr, optional_vars = NULL;
2905 int suite_index = 3; /* skip 'with', test, and ':' */
2906 asdl_seq *suite_seq;
2907
2908 assert(TYPE(n) == with_stmt);
2909 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002910 if (!context_expr)
2911 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002912 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002913 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002914
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002915 if (!optional_vars) {
2916 return NULL;
2917 }
2918 if (!set_context(optional_vars, Store, n)) {
2919 return NULL;
2920 }
2921 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002922 }
2923
2924 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2925 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002926 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002927 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002928 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002929 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002930}
2931
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002932static stmt_ty
2933ast_for_classdef(struct compiling *c, const node *n)
2934{
2935 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 asdl_seq *bases, *s;
2937
2938 REQ(n, classdef);
2939
2940 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002941 ast_error(n, "assignment to None");
2942 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 }
2944
2945 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002946 s = ast_for_suite(c, CHILD(n, 3));
2947 if (!s)
2948 return NULL;
2949 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2950 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 }
2952 /* check for empty base list */
2953 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002954 s = ast_for_suite(c, CHILD(n,5));
2955 if (!s)
2956 return NULL;
2957 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2958 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959 }
2960
2961 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002962 bases = ast_for_class_bases(c, CHILD(n, 3));
2963 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002964 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965
2966 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002967 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002968 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002969 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002970 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971}
2972
2973static stmt_ty
2974ast_for_stmt(struct compiling *c, const node *n)
2975{
2976 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002977 assert(NCH(n) == 1);
2978 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 }
2980 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002981 assert(num_stmts(n) == 1);
2982 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983 }
2984 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002985 REQ(n, small_stmt);
2986 n = CHILD(n, 0);
2987 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2988 | flow_stmt | import_stmt | global_stmt | exec_stmt
2989 | assert_stmt
2990 */
2991 switch (TYPE(n)) {
2992 case expr_stmt:
2993 return ast_for_expr_stmt(c, n);
2994 case print_stmt:
2995 return ast_for_print_stmt(c, n);
2996 case del_stmt:
2997 return ast_for_del_stmt(c, n);
2998 case pass_stmt:
2999 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3000 case flow_stmt:
3001 return ast_for_flow_stmt(c, n);
3002 case import_stmt:
3003 return ast_for_import_stmt(c, n);
3004 case global_stmt:
3005 return ast_for_global_stmt(c, n);
3006 case exec_stmt:
3007 return ast_for_exec_stmt(c, n);
3008 case assert_stmt:
3009 return ast_for_assert_stmt(c, n);
3010 default:
3011 PyErr_Format(PyExc_SystemError,
3012 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3013 TYPE(n), NCH(n));
3014 return NULL;
3015 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016 }
3017 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003018 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3019 | funcdef | classdef
3020 */
3021 node *ch = CHILD(n, 0);
3022 REQ(n, compound_stmt);
3023 switch (TYPE(ch)) {
3024 case if_stmt:
3025 return ast_for_if_stmt(c, ch);
3026 case while_stmt:
3027 return ast_for_while_stmt(c, ch);
3028 case for_stmt:
3029 return ast_for_for_stmt(c, ch);
3030 case try_stmt:
3031 return ast_for_try_stmt(c, ch);
3032 case with_stmt:
3033 return ast_for_with_stmt(c, ch);
3034 case funcdef:
3035 return ast_for_funcdef(c, ch);
3036 case classdef:
3037 return ast_for_classdef(c, ch);
3038 default:
3039 PyErr_Format(PyExc_SystemError,
3040 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3041 TYPE(n), NCH(n));
3042 return NULL;
3043 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044 }
3045}
3046
3047static PyObject *
3048parsenumber(const char *s)
3049{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003050 const char *end;
3051 long x;
3052 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003054 Py_complex c;
3055 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056#endif
3057
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003058 errno = 0;
3059 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003061 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003063 if (*end == 'l' || *end == 'L')
3064 return PyLong_FromString((char *)s, (char **)0, 0);
3065 if (s[0] == '0') {
3066 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3067 if (x < 0 && errno == 0) {
3068 return PyLong_FromString((char *)s,
3069 (char **)0,
3070 0);
3071 }
3072 }
3073 else
3074 x = PyOS_strtol((char *)s, (char **)&end, 0);
3075 if (*end == '\0') {
3076 if (errno != 0)
3077 return PyLong_FromString((char *)s, (char **)0, 0);
3078 return PyInt_FromLong(x);
3079 }
3080 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003082 if (imflag) {
3083 c.real = 0.;
3084 PyFPE_START_PROTECT("atof", return 0)
3085 c.imag = PyOS_ascii_atof(s);
3086 PyFPE_END_PROTECT(c)
3087 return PyComplex_FromCComplex(c);
3088 }
3089 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003091 {
3092 PyFPE_START_PROTECT("atof", return 0)
3093 dx = PyOS_ascii_atof(s);
3094 PyFPE_END_PROTECT(dx)
3095 return PyFloat_FromDouble(dx);
3096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097}
3098
3099static PyObject *
3100decode_utf8(const char **sPtr, const char *end, char* encoding)
3101{
3102#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003103 Py_FatalError("decode_utf8 should not be called in this build.");
3104 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003105#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003106 PyObject *u, *v;
3107 char *s, *t;
3108 t = s = (char *)*sPtr;
3109 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3110 while (s < end && (*s & 0x80)) s++;
3111 *sPtr = s;
3112 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3113 if (u == NULL)
3114 return NULL;
3115 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3116 Py_DECREF(u);
3117 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118#endif
3119}
3120
3121static PyObject *
3122decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3123{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003124 PyObject *v, *u;
3125 char *buf;
3126 char *p;
3127 const char *end;
3128 if (encoding == NULL) {
3129 buf = (char *)s;
3130 u = NULL;
3131 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3132 buf = (char *)s;
3133 u = NULL;
3134 } else {
3135 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3136 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3137 if (u == NULL)
3138 return NULL;
3139 p = buf = PyString_AsString(u);
3140 end = s + len;
3141 while (s < end) {
3142 if (*s == '\\') {
3143 *p++ = *s++;
3144 if (*s & 0x80) {
3145 strcpy(p, "u005c");
3146 p += 5;
3147 }
3148 }
3149 if (*s & 0x80) { /* XXX inefficient */
3150 PyObject *w;
3151 char *r;
3152 Py_ssize_t rn, i;
3153 w = decode_utf8(&s, end, "utf-16-be");
3154 if (w == NULL) {
3155 Py_DECREF(u);
3156 return NULL;
3157 }
3158 r = PyString_AsString(w);
3159 rn = PyString_Size(w);
3160 assert(rn % 2 == 0);
3161 for (i = 0; i < rn; i += 2) {
3162 sprintf(p, "\\u%02x%02x",
3163 r[i + 0] & 0xFF,
3164 r[i + 1] & 0xFF);
3165 p += 6;
3166 }
3167 Py_DECREF(w);
3168 } else {
3169 *p++ = *s++;
3170 }
3171 }
3172 len = p - buf;
3173 s = buf;
3174 }
3175 if (rawmode)
3176 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3177 else
3178 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3179 Py_XDECREF(u);
3180 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181}
3182
3183/* s is a Python string literal, including the bracketing quote characters,
3184 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3185 * parsestr parses it, and returns the decoded Python string object.
3186 */
3187static PyObject *
3188parsestr(const char *s, const char *encoding)
3189{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003190 size_t len;
3191 int quote = Py_CHARMASK(*s);
3192 int rawmode = 0;
3193 int need_encoding;
3194 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003196 if (isalpha(quote) || quote == '_') {
3197 if (quote == 'u' || quote == 'U') {
3198 quote = *++s;
3199 unicode = 1;
3200 }
3201 if (quote == 'r' || quote == 'R') {
3202 quote = *++s;
3203 rawmode = 1;
3204 }
3205 }
3206 if (quote != '\'' && quote != '\"') {
3207 PyErr_BadInternalCall();
3208 return NULL;
3209 }
3210 s++;
3211 len = strlen(s);
3212 if (len > INT_MAX) {
3213 PyErr_SetString(PyExc_OverflowError,
3214 "string to parse is too long");
3215 return NULL;
3216 }
3217 if (s[--len] != quote) {
3218 PyErr_BadInternalCall();
3219 return NULL;
3220 }
3221 if (len >= 4 && s[0] == quote && s[1] == quote) {
3222 s += 2;
3223 len -= 2;
3224 if (s[--len] != quote || s[--len] != quote) {
3225 PyErr_BadInternalCall();
3226 return NULL;
3227 }
3228 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003230 if (unicode || Py_UnicodeFlag) {
3231 return decode_unicode(s, len, rawmode, encoding);
3232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003234 need_encoding = (encoding != NULL &&
3235 strcmp(encoding, "utf-8") != 0 &&
3236 strcmp(encoding, "iso-8859-1") != 0);
3237 if (rawmode || strchr(s, '\\') == NULL) {
3238 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003239#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003240 /* This should not happen - we never see any other
3241 encoding. */
3242 Py_FatalError(
3243 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003245 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3246 if (u == NULL)
3247 return NULL;
3248 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3249 Py_DECREF(u);
3250 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003252 } else {
3253 return PyString_FromStringAndSize(s, len);
3254 }
3255 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003257 return PyString_DecodeEscape(s, len, NULL, unicode,
3258 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259}
3260
3261/* Build a Python string object out of a STRING atom. This takes care of
3262 * compile-time literal catenation, calling parsestr() on each piece, and
3263 * pasting the intermediate results together.
3264 */
3265static PyObject *
3266parsestrplus(struct compiling *c, const node *n)
3267{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003268 PyObject *v;
3269 int i;
3270 REQ(CHILD(n, 0), STRING);
3271 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3272 /* String literal concatenation */
3273 for (i = 1; i < NCH(n); i++) {
3274 PyObject *s;
3275 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3276 if (s == NULL)
3277 goto onError;
3278 if (PyString_Check(v) && PyString_Check(s)) {
3279 PyString_ConcatAndDel(&v, s);
3280 if (v == NULL)
3281 goto onError;
3282 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003284 else {
3285 PyObject *temp = PyUnicode_Concat(v, s);
3286 Py_DECREF(s);
3287 Py_DECREF(v);
3288 v = temp;
3289 if (v == NULL)
3290 goto onError;
3291 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003293 }
3294 }
3295 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296
3297 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003298 Py_XDECREF(v);
3299 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300}