blob: b084e01a575e56bf0052e486337b38eae712a49f [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 */
Christian Heimesffcd1e12007-11-24 01:36:02 +000022 const char *c_filename; /* filename */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000023};
24
25static asdl_seq *seq_for_testlist(struct compiling *, const node *);
26static expr_ty ast_for_expr(struct compiling *, const node *);
27static stmt_ty ast_for_stmt(struct compiling *, const node *);
28static asdl_seq *ast_for_suite(struct compiling *, const node *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000029static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
30 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000031static expr_ty ast_for_testlist(struct compiling *, const node *);
32static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033
34/* Note different signature for ast_for_call */
35static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
36
37static PyObject *parsenumber(const char *);
38static PyObject *parsestr(const char *s, const char *encoding);
39static PyObject *parsestrplus(struct compiling *, const node *n);
40
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000042#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043#endif
44
Neal Norwitzadb69fc2005-12-17 20:54:49 +000045static identifier
46new_identifier(const char* n, PyArena *arena) {
47 PyObject* id = PyString_InternFromString(n);
48 PyArena_AddPyObject(arena, id);
49 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000050}
51
Neal Norwitzadb69fc2005-12-17 20:54:49 +000052#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053
54/* This routine provides an invalid object for the syntax error.
55 The outermost routine must unpack this error and create the
56 proper object. We do this so that we don't have to pass
57 the filename to everything function.
58
59 XXX Maybe we should just pass the filename...
60*/
61
62static int
63ast_error(const node *n, const char *errstr)
64{
65 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
66 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000067 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000068 PyErr_SetObject(PyExc_SyntaxError, u);
69 Py_DECREF(u);
70 return 0;
71}
72
73static void
74ast_error_finish(const char *filename)
75{
76 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000077 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078
79 assert(PyErr_Occurred());
80 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000081 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 PyErr_Fetch(&type, &value, &tback);
84 errstr = PyTuple_GetItem(value, 0);
85 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000086 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087 Py_INCREF(errstr);
88 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000089 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000090 Py_DECREF(errstr);
91 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000092 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000093 Py_DECREF(value);
94
95 loc = PyErr_ProgramText(filename, lineno);
96 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000097 Py_INCREF(Py_None);
98 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000100 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000101 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000102 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000103 Py_DECREF(errstr);
104 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000105 }
Georg Brandl7784f122006-05-26 20:04:44 +0000106 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107 Py_DECREF(errstr);
108 Py_DECREF(tmp);
109 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000110 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111 PyErr_Restore(type, value, tback);
112}
113
114/* num_stmts() returns number of contained statements.
115
116 Use this routine to determine how big a sequence is needed for
117 the statements in a parse tree. Its raison d'etre is this bit of
118 grammar:
119
120 stmt: simple_stmt | compound_stmt
121 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
122
123 A simple_stmt can contain multiple small_stmt elements joined
124 by semicolons. If the arg is a simple_stmt, the number of
125 small_stmt elements is returned.
126*/
127
128static int
129num_stmts(const node *n)
130{
131 int i, l;
132 node *ch;
133
134 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000135 case single_input:
136 if (TYPE(CHILD(n, 0)) == NEWLINE)
137 return 0;
138 else
139 return num_stmts(CHILD(n, 0));
140 case file_input:
141 l = 0;
142 for (i = 0; i < NCH(n); i++) {
143 ch = CHILD(n, i);
144 if (TYPE(ch) == stmt)
145 l += num_stmts(ch);
146 }
147 return l;
148 case stmt:
149 return num_stmts(CHILD(n, 0));
150 case compound_stmt:
151 return 1;
152 case simple_stmt:
153 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
154 case suite:
155 if (NCH(n) == 1)
156 return num_stmts(CHILD(n, 0));
157 else {
158 l = 0;
159 for (i = 2; i < (NCH(n) - 1); i++)
160 l += num_stmts(CHILD(n, i));
161 return l;
162 }
163 default: {
164 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000166 sprintf(buf, "Non-statement found: %d %d\n",
167 TYPE(n), NCH(n));
168 Py_FatalError(buf);
169 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000170 }
171 assert(0);
172 return 0;
173}
174
175/* Transform the CST rooted at node * to the appropriate AST
176*/
177
178mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000179PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000180 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000182 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183 asdl_seq *stmts = NULL;
184 stmt_ty s;
185 node *ch;
186 struct compiling c;
187
188 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000189 c.c_encoding = "utf-8";
190 if (TYPE(n) == encoding_decl) {
191 ast_error(n, "encoding declaration in Unicode string");
192 goto error;
193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000195 c.c_encoding = STR(n);
196 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000198 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000200 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000201 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202
Jeremy Hyltona8293132006-02-28 17:58:27 +0000203 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000205 case file_input:
206 stmts = asdl_seq_new(num_stmts(n), arena);
207 if (!stmts)
208 return NULL;
209 for (i = 0; i < NCH(n) - 1; i++) {
210 ch = CHILD(n, i);
211 if (TYPE(ch) == NEWLINE)
212 continue;
213 REQ(ch, stmt);
214 num = num_stmts(ch);
215 if (num == 1) {
216 s = ast_for_stmt(&c, ch);
217 if (!s)
218 goto error;
219 asdl_seq_SET(stmts, k++, s);
220 }
221 else {
222 ch = CHILD(ch, 0);
223 REQ(ch, simple_stmt);
224 for (j = 0; j < num; j++) {
225 s = ast_for_stmt(&c, CHILD(ch, j * 2));
226 if (!s)
227 goto error;
228 asdl_seq_SET(stmts, k++, s);
229 }
230 }
231 }
232 return Module(stmts, arena);
233 case eval_input: {
234 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000236 /* XXX Why not gen_for here? */
237 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
238 if (!testlist_ast)
239 goto error;
240 return Expression(testlist_ast, arena);
241 }
242 case single_input:
243 if (TYPE(CHILD(n, 0)) == NEWLINE) {
244 stmts = asdl_seq_new(1, arena);
245 if (!stmts)
246 goto error;
247 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
248 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000249 if (!asdl_seq_GET(stmts, 0))
250 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000251 return Interactive(stmts, arena);
252 }
253 else {
254 n = CHILD(n, 0);
255 num = num_stmts(n);
256 stmts = asdl_seq_new(num, arena);
257 if (!stmts)
258 goto error;
259 if (num == 1) {
260 s = ast_for_stmt(&c, n);
261 if (!s)
262 goto error;
263 asdl_seq_SET(stmts, 0, s);
264 }
265 else {
266 /* Only a simple_stmt can contain multiple statements. */
267 REQ(n, simple_stmt);
268 for (i = 0; i < NCH(n); i += 2) {
269 if (TYPE(CHILD(n, i)) == NEWLINE)
270 break;
271 s = ast_for_stmt(&c, CHILD(n, i));
272 if (!s)
273 goto error;
274 asdl_seq_SET(stmts, i / 2, s);
275 }
276 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000278 return Interactive(stmts, arena);
279 }
280 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000281 PyErr_Format(PyExc_SystemError,
282 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000283 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284 }
285 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286 ast_error_finish(filename);
287 return NULL;
288}
289
290/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
291*/
292
293static operator_ty
294get_operator(const node *n)
295{
296 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000297 case VBAR:
298 return BitOr;
299 case CIRCUMFLEX:
300 return BitXor;
301 case AMPER:
302 return BitAnd;
303 case LEFTSHIFT:
304 return LShift;
305 case RIGHTSHIFT:
306 return RShift;
307 case PLUS:
308 return Add;
309 case MINUS:
310 return Sub;
311 case STAR:
312 return Mult;
313 case SLASH:
314 return Div;
315 case DOUBLESLASH:
316 return FloorDiv;
317 case PERCENT:
318 return Mod;
319 default:
320 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 }
322}
323
Jeremy Hyltona8293132006-02-28 17:58:27 +0000324/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325
326 Only sets context for expr kinds that "can appear in assignment context"
327 (according to ../Parser/Python.asdl). For other expr kinds, it sets
328 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329*/
330
331static int
332set_context(expr_ty e, expr_context_ty ctx, const node *n)
333{
334 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000335 /* If a particular expression type can't be used for assign / delete,
336 set expr_name to its name and an error message will be generated.
337 */
338 const char* expr_name = NULL;
339
340 /* The ast defines augmented store and load contexts, but the
341 implementation here doesn't actually use them. The code may be
342 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000343 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000344 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000345 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000346 */
347 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348
349 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000350 case Attribute_kind:
351 if (ctx == Store &&
352 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
353 return ast_error(n, "assignment to None");
354 }
355 e->v.Attribute.ctx = ctx;
356 break;
357 case Subscript_kind:
358 e->v.Subscript.ctx = ctx;
359 break;
360 case Name_kind:
361 if (ctx == Store &&
362 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
363 return ast_error(n, "assignment to None");
364 }
365 e->v.Name.ctx = ctx;
366 break;
367 case List_kind:
368 e->v.List.ctx = ctx;
369 s = e->v.List.elts;
370 break;
371 case Tuple_kind:
372 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
373 return ast_error(n, "can't assign to ()");
374 e->v.Tuple.ctx = ctx;
375 s = e->v.Tuple.elts;
376 break;
377 case Lambda_kind:
378 expr_name = "lambda";
379 break;
380 case Call_kind:
381 expr_name = "function call";
382 break;
383 case BoolOp_kind:
384 case BinOp_kind:
385 case UnaryOp_kind:
386 expr_name = "operator";
387 break;
388 case GeneratorExp_kind:
389 expr_name = "generator expression";
390 break;
391 case Yield_kind:
392 expr_name = "yield expression";
393 break;
394 case ListComp_kind:
395 expr_name = "list comprehension";
396 break;
397 case Dict_kind:
398 case Num_kind:
399 case Str_kind:
400 expr_name = "literal";
401 break;
402 case Compare_kind:
403 expr_name = "comparison";
404 break;
405 case Repr_kind:
406 expr_name = "repr";
407 break;
408 case IfExp_kind:
409 expr_name = "conditional expression";
410 break;
411 default:
412 PyErr_Format(PyExc_SystemError,
413 "unexpected expression in assignment %d (line %d)",
414 e->kind, e->lineno);
415 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000417 /* Check for error string set by switch */
418 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000419 char buf[300];
420 PyOS_snprintf(buf, sizeof(buf),
421 "can't %s %s",
422 ctx == Store ? "assign to" : "delete",
423 expr_name);
424 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000425 }
426
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000428 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429 */
430 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000431 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000433 for (i = 0; i < asdl_seq_LEN(s); i++) {
434 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
435 return 0;
436 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437 }
438 return 1;
439}
440
441static operator_ty
442ast_for_augassign(const node *n)
443{
444 REQ(n, augassign);
445 n = CHILD(n, 0);
446 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000447 case '+':
448 return Add;
449 case '-':
450 return Sub;
451 case '/':
452 if (STR(n)[1] == '/')
453 return FloorDiv;
454 else
455 return Div;
456 case '%':
457 return Mod;
458 case '<':
459 return LShift;
460 case '>':
461 return RShift;
462 case '&':
463 return BitAnd;
464 case '^':
465 return BitXor;
466 case '|':
467 return BitOr;
468 case '*':
469 if (STR(n)[1] == '*')
470 return Pow;
471 else
472 return Mult;
473 default:
474 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
475 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476 }
477}
478
479static cmpop_ty
480ast_for_comp_op(const node *n)
481{
482 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000483 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484 */
485 REQ(n, comp_op);
486 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000487 n = CHILD(n, 0);
488 switch (TYPE(n)) {
489 case LESS:
490 return Lt;
491 case GREATER:
492 return Gt;
493 case EQEQUAL: /* == */
494 return Eq;
495 case LESSEQUAL:
496 return LtE;
497 case GREATEREQUAL:
498 return GtE;
499 case NOTEQUAL:
500 return NotEq;
501 case NAME:
502 if (strcmp(STR(n), "in") == 0)
503 return In;
504 if (strcmp(STR(n), "is") == 0)
505 return Is;
506 default:
507 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
508 STR(n));
509 return (cmpop_ty)0;
510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511 }
512 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000513 /* handle "not in" and "is not" */
514 switch (TYPE(CHILD(n, 0))) {
515 case NAME:
516 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
517 return NotIn;
518 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
519 return IsNot;
520 default:
521 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
522 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
523 return (cmpop_ty)0;
524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 }
Neal Norwitz79792652005-11-14 04:25:03 +0000526 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000527 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000528 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529}
530
531static asdl_seq *
532seq_for_testlist(struct compiling *c, const node *n)
533{
534 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000535 asdl_seq *seq;
536 expr_ty expression;
537 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000538 assert(TYPE(n) == testlist ||
539 TYPE(n) == listmaker ||
540 TYPE(n) == testlist_gexp ||
541 TYPE(n) == testlist_safe ||
542 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000544 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000546 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
548 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000549 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000551 expression = ast_for_expr(c, CHILD(n, i));
552 if (!expression)
553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000555 assert(i / 2 < seq->size);
556 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557 }
558 return seq;
559}
560
561static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000562compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563{
564 int i, len = (NCH(n) + 1) / 2;
565 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000566 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000568 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569
Neal Norwitz3a230172006-09-22 08:18:10 +0000570 /* fpdef: NAME | '(' fplist ')'
571 fplist: fpdef (',' fpdef)* [',']
572 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000575 const node *fpdef_node = CHILD(n, 2*i);
576 const node *child;
577 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000578set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000579 /* fpdef_node is either a NAME or an fplist */
580 child = CHILD(fpdef_node, 0);
581 if (TYPE(child) == NAME) {
582 if (!strcmp(STR(child), "None")) {
583 ast_error(child, "assignment to None");
584 return NULL;
585 }
586 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
587 child->n_col_offset, c->c_arena);
588 }
589 else {
590 assert(TYPE(fpdef_node) == fpdef);
591 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
592 child = CHILD(fpdef_node, 1);
593 assert(TYPE(child) == fplist);
594 /* NCH == 1 means we have (x), we need to elide the extra parens */
595 if (NCH(child) == 1) {
596 fpdef_node = CHILD(child, 0);
597 assert(TYPE(fpdef_node) == fpdef);
598 goto set_name;
599 }
600 arg = compiler_complex_args(c, child);
601 }
602 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 }
604
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000605 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000606 if (!set_context(result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 return result;
609}
610
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
Jeremy Hyltona8293132006-02-28 17:58:27 +0000612/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
614static arguments_ty
615ast_for_arguments(struct compiling *c, const node *n)
616{
617 /* parameters: '(' [varargslist] ')'
618 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000619 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000621 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 asdl_seq *args, *defaults;
623 identifier vararg = NULL, kwarg = NULL;
624 node *ch;
625
626 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000627 if (NCH(n) == 2) /* () as argument list */
628 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
629 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630 }
631 REQ(n, varargslist);
632
633 /* first count the number of normal args & defaults */
634 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000635 ch = CHILD(n, i);
636 if (TYPE(ch) == fpdef)
637 n_args++;
638 if (TYPE(ch) == EQUAL)
639 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000641 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000643 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000644 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000646 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647
648 /* fpdef: NAME | '(' fplist ')'
649 fplist: fpdef (',' fpdef)* [',']
650 */
651 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000652 j = 0; /* index for defaults */
653 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000655 ch = CHILD(n, i);
656 switch (TYPE(ch)) {
657 case fpdef:
658 handle_fpdef:
659 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
660 anything other than EQUAL or a comma? */
661 /* XXX Should NCH(n) check be made a separate check? */
662 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
663 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
664 if (!expression)
665 goto error;
666 assert(defaults != NULL);
667 asdl_seq_SET(defaults, j++, expression);
668 i += 2;
669 found_default = 1;
670 }
671 else if (found_default) {
672 ast_error(n,
673 "non-default argument follows default argument");
674 goto error;
675 }
676 if (NCH(ch) == 3) {
677 ch = CHILD(ch, 1);
678 /* def foo((x)): is not complex, special case. */
679 if (NCH(ch) != 1) {
680 /* We have complex arguments, setup for unpacking. */
681 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000682 if (!asdl_seq_GET(args, k-1))
683 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000684 } else {
685 /* def foo((x)): setup for checking NAME below. */
686 /* Loop because there can be many parens and tuple
687 unpacking mixed in. */
688 ch = CHILD(ch, 0);
689 assert(TYPE(ch) == fpdef);
690 goto handle_fpdef;
691 }
692 }
693 if (TYPE(CHILD(ch, 0)) == NAME) {
694 expr_ty name;
695 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
696 ast_error(CHILD(ch, 0), "assignment to None");
697 goto error;
698 }
699 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
700 Param, LINENO(ch), ch->n_col_offset,
701 c->c_arena);
702 if (!name)
703 goto error;
704 asdl_seq_SET(args, k++, name);
705
706 }
707 i += 2; /* the name and the comma */
708 break;
709 case STAR:
710 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
711 ast_error(CHILD(n, i+1), "assignment to None");
712 goto error;
713 }
714 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
715 i += 3;
716 break;
717 case DOUBLESTAR:
718 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
719 ast_error(CHILD(n, i+1), "assignment to None");
720 goto error;
721 }
722 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
723 i += 3;
724 break;
725 default:
726 PyErr_Format(PyExc_SystemError,
727 "unexpected node in varargslist: %d @ %d",
728 TYPE(ch), i);
729 goto error;
730 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731 }
732
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000733 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734
735 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000736 Py_XDECREF(vararg);
737 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 return NULL;
739}
740
741static expr_ty
742ast_for_dotted_name(struct compiling *c, const node *n)
743{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000744 expr_ty e;
745 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000746 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 int i;
748
749 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000750
751 lineno = LINENO(n);
752 col_offset = n->n_col_offset;
753
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 id = NEW_IDENTIFIER(CHILD(n, 0));
755 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000756 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000757 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000759 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760
761 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000762 id = NEW_IDENTIFIER(CHILD(n, i));
763 if (!id)
764 return NULL;
765 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
766 if (!e)
767 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 }
769
770 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771}
772
773static expr_ty
774ast_for_decorator(struct compiling *c, const node *n)
775{
776 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
777 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000778 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
780 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000781 REQ(CHILD(n, 0), AT);
782 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783
784 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
785 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000786 return NULL;
787
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000789 d = name_expr;
790 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 }
792 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000793 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
794 n->n_col_offset, c->c_arena);
795 if (!d)
796 return NULL;
797 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 }
799 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000800 d = ast_for_call(c, CHILD(n, 3), name_expr);
801 if (!d)
802 return NULL;
803 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 }
805
806 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807}
808
809static asdl_seq*
810ast_for_decorators(struct compiling *c, const node *n)
811{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000812 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000813 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 int i;
815
816 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000817 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000819 return NULL;
820
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000822 d = ast_for_decorator(c, CHILD(n, i));
823 if (!d)
824 return NULL;
825 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 }
827 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828}
829
830static stmt_ty
831ast_for_funcdef(struct compiling *c, const node *n)
832{
833 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000834 identifier name;
835 arguments_ty args;
836 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 asdl_seq *decorator_seq = NULL;
838 int name_i;
839
840 REQ(n, funcdef);
841
842 if (NCH(n) == 6) { /* decorators are present */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000843 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
844 if (!decorator_seq)
845 return NULL;
846 name_i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 }
848 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000849 name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 }
851
852 name = NEW_IDENTIFIER(CHILD(n, name_i));
853 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000854 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000856 ast_error(CHILD(n, name_i), "assignment to None");
857 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
859 args = ast_for_arguments(c, CHILD(n, name_i + 1));
860 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000861 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 body = ast_for_suite(c, CHILD(n, name_i + 3));
863 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000864 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000866 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000867 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868}
869
870static expr_ty
871ast_for_lambdef(struct compiling *c, const node *n)
872{
873 /* lambdef: 'lambda' [varargslist] ':' test */
874 arguments_ty args;
875 expr_ty expression;
876
877 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000878 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
879 if (!args)
880 return NULL;
881 expression = ast_for_expr(c, CHILD(n, 2));
882 if (!expression)
883 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 }
885 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000886 args = ast_for_arguments(c, CHILD(n, 1));
887 if (!args)
888 return NULL;
889 expression = ast_for_expr(c, CHILD(n, 3));
890 if (!expression)
891 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 }
893
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000894 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895}
896
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000897static expr_ty
898ast_for_ifexpr(struct compiling *c, const node *n)
899{
900 /* test: or_test 'if' or_test 'else' test */
901 expr_ty expression, body, orelse;
902
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000903 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000904 body = ast_for_expr(c, CHILD(n, 0));
905 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000906 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000907 expression = ast_for_expr(c, CHILD(n, 2));
908 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000909 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000910 orelse = ast_for_expr(c, CHILD(n, 4));
911 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000912 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000913 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000914 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000915}
916
Neal Norwitze4d4f002006-09-05 03:58:26 +0000917/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
918 so there is only a single version. Possibly for loops can also re-use
919 the code.
920*/
921
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922/* Count the number of 'for' loop in a list comprehension.
923
924 Helper for ast_for_listcomp().
925*/
926
927static int
928count_list_fors(const node *n)
929{
930 int n_fors = 0;
931 node *ch = CHILD(n, 1);
932
933 count_list_for:
934 n_fors++;
935 REQ(ch, list_for);
936 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000937 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000939 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 count_list_iter:
941 REQ(ch, list_iter);
942 ch = CHILD(ch, 0);
943 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000944 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000946 if (NCH(ch) == 3) {
947 ch = CHILD(ch, 2);
948 goto count_list_iter;
949 }
950 else
951 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000953
954 /* Should never be reached */
955 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
956 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957}
958
959/* Count the number of 'if' statements in a list comprehension.
960
961 Helper for ast_for_listcomp().
962*/
963
964static int
965count_list_ifs(const node *n)
966{
967 int n_ifs = 0;
968
969 count_list_iter:
970 REQ(n, list_iter);
971 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000972 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973 n = CHILD(n, 0);
974 REQ(n, list_if);
975 n_ifs++;
976 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000977 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 n = CHILD(n, 2);
979 goto count_list_iter;
980}
981
982static expr_ty
983ast_for_listcomp(struct compiling *c, const node *n)
984{
985 /* listmaker: test ( list_for | (',' test)* [','] )
986 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
987 list_iter: list_for | list_if
988 list_if: 'if' test [list_iter]
989 testlist_safe: test [(',' test)+ [',']]
990 */
991 expr_ty elt;
992 asdl_seq *listcomps;
993 int i, n_fors;
994 node *ch;
995
996 REQ(n, listmaker);
997 assert(NCH(n) > 1);
998
999 elt = ast_for_expr(c, CHILD(n, 0));
1000 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002
1003 n_fors = count_list_fors(n);
1004 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001005 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001007 listcomps = asdl_seq_new(n_fors, c->c_arena);
1008 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001009 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001010
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011 ch = CHILD(n, 1);
1012 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001013 comprehension_ty lc;
1014 asdl_seq *t;
1015 expr_ty expression;
1016 node *for_ch;
1017
1018 REQ(ch, list_for);
1019
1020 for_ch = CHILD(ch, 1);
1021 t = ast_for_exprlist(c, for_ch, Store);
1022 if (!t)
1023 return NULL;
1024 expression = ast_for_testlist(c, CHILD(ch, 3));
1025 if (!expression)
1026 return NULL;
1027
1028 /* Check the # of children rather than the length of t, since
1029 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1030 */
1031 if (NCH(for_ch) == 1)
1032 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1033 c->c_arena);
1034 else
1035 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1036 c->c_arena),
1037 expression, NULL, c->c_arena);
1038 if (!lc)
1039 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001041 if (NCH(ch) == 5) {
1042 int j, n_ifs;
1043 asdl_seq *ifs;
1044 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001046 ch = CHILD(ch, 4);
1047 n_ifs = count_list_ifs(ch);
1048 if (n_ifs == -1)
1049 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001051 ifs = asdl_seq_new(n_ifs, c->c_arena);
1052 if (!ifs)
1053 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001055 for (j = 0; j < n_ifs; j++) {
1056 REQ(ch, list_iter);
1057 ch = CHILD(ch, 0);
1058 REQ(ch, list_if);
1059
1060 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1061 if (!list_for_expr)
1062 return NULL;
1063
1064 asdl_seq_SET(ifs, j, list_for_expr);
1065 if (NCH(ch) == 3)
1066 ch = CHILD(ch, 2);
1067 }
1068 /* on exit, must guarantee that ch is a list_for */
1069 if (TYPE(ch) == list_iter)
1070 ch = CHILD(ch, 0);
1071 lc->ifs = ifs;
1072 }
1073 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 }
1075
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001076 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
1078
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001079/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080
1081 Helper for ast_for_genexp().
1082*/
1083
1084static int
1085count_gen_fors(const node *n)
1086{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001087 int n_fors = 0;
1088 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089
1090 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001091 n_fors++;
1092 REQ(ch, gen_for);
1093 if (NCH(ch) == 5)
1094 ch = CHILD(ch, 4);
1095 else
1096 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001098 REQ(ch, gen_iter);
1099 ch = CHILD(ch, 0);
1100 if (TYPE(ch) == gen_for)
1101 goto count_gen_for;
1102 else if (TYPE(ch) == gen_if) {
1103 if (NCH(ch) == 3) {
1104 ch = CHILD(ch, 2);
1105 goto count_gen_iter;
1106 }
1107 else
1108 return n_fors;
1109 }
1110
1111 /* Should never be reached */
1112 PyErr_SetString(PyExc_SystemError,
1113 "logic error in count_gen_fors");
1114 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115}
1116
1117/* Count the number of 'if' statements in a generator expression.
1118
1119 Helper for ast_for_genexp().
1120*/
1121
1122static int
1123count_gen_ifs(const node *n)
1124{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001125 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001127 while (1) {
1128 REQ(n, gen_iter);
1129 if (TYPE(CHILD(n, 0)) == gen_for)
1130 return n_ifs;
1131 n = CHILD(n, 0);
1132 REQ(n, gen_if);
1133 n_ifs++;
1134 if (NCH(n) == 2)
1135 return n_ifs;
1136 n = CHILD(n, 2);
1137 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138}
1139
Jeremy Hyltona8293132006-02-28 17:58:27 +00001140/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141static expr_ty
1142ast_for_genexp(struct compiling *c, const node *n)
1143{
1144 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001145 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 expr_ty elt;
1147 asdl_seq *genexps;
1148 int i, n_fors;
1149 node *ch;
1150
1151 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1152 assert(NCH(n) > 1);
1153
1154 elt = ast_for_expr(c, CHILD(n, 0));
1155 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001156 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157
1158 n_fors = count_gen_fors(n);
1159 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001160 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001161
1162 genexps = asdl_seq_new(n_fors, c->c_arena);
1163 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001164 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001165
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 ch = CHILD(n, 1);
1167 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001168 comprehension_ty ge;
1169 asdl_seq *t;
1170 expr_ty expression;
1171 node *for_ch;
1172
1173 REQ(ch, gen_for);
1174
1175 for_ch = CHILD(ch, 1);
1176 t = ast_for_exprlist(c, for_ch, Store);
1177 if (!t)
1178 return NULL;
1179 expression = ast_for_expr(c, CHILD(ch, 3));
1180 if (!expression)
1181 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001182
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001183 /* Check the # of children rather than the length of t, since
1184 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1185 if (NCH(for_ch) == 1)
1186 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1187 NULL, c->c_arena);
1188 else
1189 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1190 c->c_arena),
1191 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001192
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001193 if (!ge)
1194 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001195
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001196 if (NCH(ch) == 5) {
1197 int j, n_ifs;
1198 asdl_seq *ifs;
1199
1200 ch = CHILD(ch, 4);
1201 n_ifs = count_gen_ifs(ch);
1202 if (n_ifs == -1)
1203 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001205 ifs = asdl_seq_new(n_ifs, c->c_arena);
1206 if (!ifs)
1207 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001208
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001209 for (j = 0; j < n_ifs; j++) {
1210 REQ(ch, gen_iter);
1211 ch = CHILD(ch, 0);
1212 REQ(ch, gen_if);
1213
1214 expression = ast_for_expr(c, CHILD(ch, 1));
1215 if (!expression)
1216 return NULL;
1217 asdl_seq_SET(ifs, j, expression);
1218 if (NCH(ch) == 3)
1219 ch = CHILD(ch, 2);
1220 }
1221 /* on exit, must guarantee that ch is a gen_for */
1222 if (TYPE(ch) == gen_iter)
1223 ch = CHILD(ch, 0);
1224 ge->ifs = ifs;
1225 }
1226 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 }
1228
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001229 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230}
1231
1232static expr_ty
1233ast_for_atom(struct compiling *c, const node *n)
1234{
1235 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1236 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1237 */
1238 node *ch = CHILD(n, 0);
1239
1240 switch (TYPE(ch)) {
1241 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001242 /* All names start in Load context, but may later be
1243 changed. */
1244 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1245 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001247 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001248 if (!str) {
1249 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1250 PyObject *type, *value, *tback, *errstr;
1251 PyErr_Fetch(&type, &value, &tback);
1252 errstr = ((PyUnicodeErrorObject *)value)->reason;
1253 if (errstr) {
1254 char *s = "";
1255 char buf[128];
1256 s = PyString_AsString(errstr);
1257 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1258 ast_error(n, buf);
1259 } else {
1260 ast_error(n, "(unicode error) unknown error");
1261 }
1262 Py_DECREF(type);
1263 Py_DECREF(value);
1264 Py_XDECREF(tback);
1265 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001266 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001267 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001268 PyArena_AddPyObject(c->c_arena, str);
1269 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 }
1271 case NUMBER: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001272 PyObject *pynum = parsenumber(STR(ch));
1273 if (!pynum)
1274 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001275
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001276 PyArena_AddPyObject(c->c_arena, pynum);
1277 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 }
1279 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001280 ch = CHILD(n, 1);
1281
1282 if (TYPE(ch) == RPAR)
1283 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1284
1285 if (TYPE(ch) == yield_expr)
1286 return ast_for_expr(c, ch);
1287
1288 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1289 return ast_for_genexp(c, ch);
1290
1291 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001293 ch = CHILD(n, 1);
1294
1295 if (TYPE(ch) == RSQB)
1296 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1297
1298 REQ(ch, listmaker);
1299 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1300 asdl_seq *elts = seq_for_testlist(c, ch);
1301 if (!elts)
1302 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001303
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001304 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1305 }
1306 else
1307 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001309 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1310 int i, size;
1311 asdl_seq *keys, *values;
1312
1313 ch = CHILD(n, 1);
1314 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1315 keys = asdl_seq_new(size, c->c_arena);
1316 if (!keys)
1317 return NULL;
1318
1319 values = asdl_seq_new(size, c->c_arena);
1320 if (!values)
1321 return NULL;
1322
1323 for (i = 0; i < NCH(ch); i += 4) {
1324 expr_ty expression;
1325
1326 expression = ast_for_expr(c, CHILD(ch, i));
1327 if (!expression)
1328 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001329
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001330 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001331
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001332 expression = ast_for_expr(c, CHILD(ch, i + 2));
1333 if (!expression)
1334 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001335
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001336 asdl_seq_SET(values, i / 4, expression);
1337 }
1338 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 }
1340 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001341 expr_ty expression;
Christian Heimes02c9ab52007-11-23 12:12:02 +00001342 if (Py_Py3kWarningFlag) {
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001343 if (PyErr_WarnExplicit(PyExc_DeprecationWarning,
1344 "backquote not supported in 3.x",
Christian Heimesffcd1e12007-11-24 01:36:02 +00001345 c->c_filename, LINENO(n),
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001346 NULL, NULL)) {
1347 return NULL;
1348 }
1349 }
1350 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001351 if (!expression)
1352 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001353
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001354 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 }
1356 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001357 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1358 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 }
1360}
1361
1362static slice_ty
1363ast_for_slice(struct compiling *c, const node *n)
1364{
1365 node *ch;
1366 expr_ty lower = NULL, upper = NULL, step = NULL;
1367
1368 REQ(n, subscript);
1369
1370 /*
1371 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1372 sliceop: ':' [test]
1373 */
1374 ch = CHILD(n, 0);
1375 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001376 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377
1378 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001379 /* 'step' variable hold no significance in terms of being used over
1380 other vars */
1381 step = ast_for_expr(c, ch);
1382 if (!step)
1383 return NULL;
1384
1385 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 }
1387
1388 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001389 lower = ast_for_expr(c, ch);
1390 if (!lower)
1391 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 }
1393
1394 /* If there's an upper bound it's in the second or third position. */
1395 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001396 if (NCH(n) > 1) {
1397 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001399 if (TYPE(n2) == test) {
1400 upper = ast_for_expr(c, n2);
1401 if (!upper)
1402 return NULL;
1403 }
1404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001406 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001408 if (TYPE(n2) == test) {
1409 upper = ast_for_expr(c, n2);
1410 if (!upper)
1411 return NULL;
1412 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 }
1414
1415 ch = CHILD(n, NCH(n) - 1);
1416 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001417 if (NCH(ch) == 1) {
1418 /* No expression, so step is None */
1419 ch = CHILD(ch, 0);
1420 step = Name(new_identifier("None", c->c_arena), Load,
1421 LINENO(ch), ch->n_col_offset, c->c_arena);
1422 if (!step)
1423 return NULL;
1424 } else {
1425 ch = CHILD(ch, 1);
1426 if (TYPE(ch) == test) {
1427 step = ast_for_expr(c, ch);
1428 if (!step)
1429 return NULL;
1430 }
1431 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 }
1433
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001434 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435}
1436
1437static expr_ty
1438ast_for_binop(struct compiling *c, const node *n)
1439{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001440 /* Must account for a sequence of expressions.
1441 How should A op B op C by represented?
1442 BinOp(BinOp(A, op, B), op, C).
1443 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001445 int i, nops;
1446 expr_ty expr1, expr2, result;
1447 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001449 expr1 = ast_for_expr(c, CHILD(n, 0));
1450 if (!expr1)
1451 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001453 expr2 = ast_for_expr(c, CHILD(n, 2));
1454 if (!expr2)
1455 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001457 newoperator = get_operator(CHILD(n, 1));
1458 if (!newoperator)
1459 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001461 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1462 c->c_arena);
1463 if (!result)
1464 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001466 nops = (NCH(n) - 1) / 2;
1467 for (i = 1; i < nops; i++) {
1468 expr_ty tmp_result, tmp;
1469 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001471 newoperator = get_operator(next_oper);
1472 if (!newoperator)
1473 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001475 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1476 if (!tmp)
1477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001479 tmp_result = BinOp(result, newoperator, tmp,
1480 LINENO(next_oper), next_oper->n_col_offset,
1481 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001482 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001483 return NULL;
1484 result = tmp_result;
1485 }
1486 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487}
1488
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001489static expr_ty
1490ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1491{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001492 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1493 subscriptlist: subscript (',' subscript)* [',']
1494 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1495 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001496 REQ(n, trailer);
1497 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001498 if (NCH(n) == 2)
1499 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1500 n->n_col_offset, c->c_arena);
1501 else
1502 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001503 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001504 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001505 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1506 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001507 }
1508 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001509 REQ(CHILD(n, 0), LSQB);
1510 REQ(CHILD(n, 2), RSQB);
1511 n = CHILD(n, 1);
1512 if (NCH(n) == 1) {
1513 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1514 if (!slc)
1515 return NULL;
1516 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1517 c->c_arena);
1518 }
1519 else {
1520 /* The grammar is ambiguous here. The ambiguity is resolved
1521 by treating the sequence as a tuple literal if there are
1522 no slice features.
1523 */
1524 int j;
1525 slice_ty slc;
1526 expr_ty e;
1527 bool simple = true;
1528 asdl_seq *slices, *elts;
1529 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1530 if (!slices)
1531 return NULL;
1532 for (j = 0; j < NCH(n); j += 2) {
1533 slc = ast_for_slice(c, CHILD(n, j));
1534 if (!slc)
1535 return NULL;
1536 if (slc->kind != Index_kind)
1537 simple = false;
1538 asdl_seq_SET(slices, j / 2, slc);
1539 }
1540 if (!simple) {
1541 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1542 Load, LINENO(n), n->n_col_offset, c->c_arena);
1543 }
1544 /* extract Index values and put them in a Tuple */
1545 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1546 if (!elts)
1547 return NULL;
1548 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1549 slc = (slice_ty)asdl_seq_GET(slices, j);
1550 assert(slc->kind == Index_kind && slc->v.Index.value);
1551 asdl_seq_SET(elts, j, slc->v.Index.value);
1552 }
1553 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1554 if (!e)
1555 return NULL;
1556 return Subscript(left_expr, Index(e, c->c_arena),
1557 Load, LINENO(n), n->n_col_offset, c->c_arena);
1558 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001559 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001560}
1561
1562static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001563ast_for_factor(struct compiling *c, const node *n)
1564{
1565 node *pfactor, *ppower, *patom, *pnum;
1566 expr_ty expression;
1567
1568 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001569 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001570 constant. The peephole optimizer already does something like
1571 this but it doesn't handle the case where the constant is
1572 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1573 PyLongObject.
1574 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001575 if (TYPE(CHILD(n, 0)) == MINUS &&
1576 NCH(n) == 2 &&
1577 TYPE((pfactor = CHILD(n, 1))) == factor &&
1578 NCH(pfactor) == 1 &&
1579 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1580 NCH(ppower) == 1 &&
1581 TYPE((patom = CHILD(ppower, 0))) == atom &&
1582 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1583 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1584 if (s == NULL)
1585 return NULL;
1586 s[0] = '-';
1587 strcpy(s + 1, STR(pnum));
1588 PyObject_FREE(STR(pnum));
1589 STR(pnum) = s;
1590 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001591 }
1592
1593 expression = ast_for_expr(c, CHILD(n, 1));
1594 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001595 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001596
1597 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001598 case PLUS:
1599 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1600 c->c_arena);
1601 case MINUS:
1602 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1603 c->c_arena);
1604 case TILDE:
1605 return UnaryOp(Invert, expression, LINENO(n),
1606 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001607 }
1608 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001609 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001610 return NULL;
1611}
1612
1613static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001614ast_for_power(struct compiling *c, const node *n)
1615{
1616 /* power: atom trailer* ('**' factor)*
1617 */
1618 int i;
1619 expr_ty e, tmp;
1620 REQ(n, power);
1621 e = ast_for_atom(c, CHILD(n, 0));
1622 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001623 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001624 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001625 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001626 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001627 node *ch = CHILD(n, i);
1628 if (TYPE(ch) != trailer)
1629 break;
1630 tmp = ast_for_trailer(c, ch, e);
1631 if (!tmp)
1632 return NULL;
1633 tmp->lineno = e->lineno;
1634 tmp->col_offset = e->col_offset;
1635 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001636 }
1637 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001638 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1639 if (!f)
1640 return NULL;
1641 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1642 if (!tmp)
1643 return NULL;
1644 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001645 }
1646 return e;
1647}
1648
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649/* Do not name a variable 'expr'! Will cause a compile error.
1650*/
1651
1652static expr_ty
1653ast_for_expr(struct compiling *c, const node *n)
1654{
1655 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001656 test: or_test ['if' or_test 'else' test] | lambdef
1657 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658 and_test: not_test ('and' not_test)*
1659 not_test: 'not' not_test | comparison
1660 comparison: expr (comp_op expr)*
1661 expr: xor_expr ('|' xor_expr)*
1662 xor_expr: and_expr ('^' and_expr)*
1663 and_expr: shift_expr ('&' shift_expr)*
1664 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1665 arith_expr: term (('+'|'-') term)*
1666 term: factor (('*'|'/'|'%'|'//') factor)*
1667 factor: ('+'|'-'|'~') factor | power
1668 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001669
1670 As well as modified versions that exist for backward compatibility,
1671 to explicitly allow:
1672 [ x for x in lambda: 0, lambda: 1 ]
1673 (which would be ambiguous without these extra rules)
1674
1675 old_test: or_test | old_lambdef
1676 old_lambdef: 'lambda' [vararglist] ':' old_test
1677
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 */
1679
1680 asdl_seq *seq;
1681 int i;
1682
1683 loop:
1684 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001685 case test:
1686 case old_test:
1687 if (TYPE(CHILD(n, 0)) == lambdef ||
1688 TYPE(CHILD(n, 0)) == old_lambdef)
1689 return ast_for_lambdef(c, CHILD(n, 0));
1690 else if (NCH(n) > 1)
1691 return ast_for_ifexpr(c, n);
1692 /* Fallthrough */
1693 case or_test:
1694 case and_test:
1695 if (NCH(n) == 1) {
1696 n = CHILD(n, 0);
1697 goto loop;
1698 }
1699 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1700 if (!seq)
1701 return NULL;
1702 for (i = 0; i < NCH(n); i += 2) {
1703 expr_ty e = ast_for_expr(c, CHILD(n, i));
1704 if (!e)
1705 return NULL;
1706 asdl_seq_SET(seq, i / 2, e);
1707 }
1708 if (!strcmp(STR(CHILD(n, 1)), "and"))
1709 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1710 c->c_arena);
1711 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1712 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1713 case not_test:
1714 if (NCH(n) == 1) {
1715 n = CHILD(n, 0);
1716 goto loop;
1717 }
1718 else {
1719 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1720 if (!expression)
1721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001723 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1724 c->c_arena);
1725 }
1726 case comparison:
1727 if (NCH(n) == 1) {
1728 n = CHILD(n, 0);
1729 goto loop;
1730 }
1731 else {
1732 expr_ty expression;
1733 asdl_int_seq *ops;
1734 asdl_seq *cmps;
1735 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1736 if (!ops)
1737 return NULL;
1738 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1739 if (!cmps) {
1740 return NULL;
1741 }
1742 for (i = 1; i < NCH(n); i += 2) {
1743 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001745 newoperator = ast_for_comp_op(CHILD(n, i));
1746 if (!newoperator) {
1747 return NULL;
1748 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001750 expression = ast_for_expr(c, CHILD(n, i + 1));
1751 if (!expression) {
1752 return NULL;
1753 }
1754
1755 asdl_seq_SET(ops, i / 2, newoperator);
1756 asdl_seq_SET(cmps, i / 2, expression);
1757 }
1758 expression = ast_for_expr(c, CHILD(n, 0));
1759 if (!expression) {
1760 return NULL;
1761 }
1762
1763 return Compare(expression, ops, cmps, LINENO(n),
1764 n->n_col_offset, c->c_arena);
1765 }
1766 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001768 /* The next five cases all handle BinOps. The main body of code
1769 is the same in each case, but the switch turned inside out to
1770 reuse the code for each type of operator.
1771 */
1772 case expr:
1773 case xor_expr:
1774 case and_expr:
1775 case shift_expr:
1776 case arith_expr:
1777 case term:
1778 if (NCH(n) == 1) {
1779 n = CHILD(n, 0);
1780 goto loop;
1781 }
1782 return ast_for_binop(c, n);
1783 case yield_expr: {
1784 expr_ty exp = NULL;
1785 if (NCH(n) == 2) {
1786 exp = ast_for_testlist(c, CHILD(n, 1));
1787 if (!exp)
1788 return NULL;
1789 }
1790 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1791 }
1792 case factor:
1793 if (NCH(n) == 1) {
1794 n = CHILD(n, 0);
1795 goto loop;
1796 }
1797 return ast_for_factor(c, n);
1798 case power:
1799 return ast_for_power(c, n);
1800 default:
1801 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1802 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001804 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 return NULL;
1806}
1807
1808static expr_ty
1809ast_for_call(struct compiling *c, const node *n, expr_ty func)
1810{
1811 /*
1812 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001813 | '**' test)
1814 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 */
1816
1817 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001818 asdl_seq *args;
1819 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 expr_ty vararg = NULL, kwarg = NULL;
1821
1822 REQ(n, arglist);
1823
1824 nargs = 0;
1825 nkeywords = 0;
1826 ngens = 0;
1827 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001828 node *ch = CHILD(n, i);
1829 if (TYPE(ch) == argument) {
1830 if (NCH(ch) == 1)
1831 nargs++;
1832 else if (TYPE(CHILD(ch, 1)) == gen_for)
1833 ngens++;
1834 else
1835 nkeywords++;
1836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 }
1838 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001839 ast_error(n, "Generator expression must be parenthesized "
1840 "if not sole argument");
1841 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 }
1843
1844 if (nargs + nkeywords + ngens > 255) {
1845 ast_error(n, "more than 255 arguments");
1846 return NULL;
1847 }
1848
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001851 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001852 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001854 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855 nargs = 0;
1856 nkeywords = 0;
1857 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001858 node *ch = CHILD(n, i);
1859 if (TYPE(ch) == argument) {
1860 expr_ty e;
1861 if (NCH(ch) == 1) {
1862 if (nkeywords) {
1863 ast_error(CHILD(ch, 0),
1864 "non-keyword arg after keyword arg");
1865 return NULL;
1866 }
1867 e = ast_for_expr(c, CHILD(ch, 0));
1868 if (!e)
1869 return NULL;
1870 asdl_seq_SET(args, nargs++, e);
1871 }
1872 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1873 e = ast_for_genexp(c, ch);
1874 if (!e)
1875 return NULL;
1876 asdl_seq_SET(args, nargs++, e);
1877 }
1878 else {
1879 keyword_ty kw;
1880 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001882 /* CHILD(ch, 0) is test, but must be an identifier? */
1883 e = ast_for_expr(c, CHILD(ch, 0));
1884 if (!e)
1885 return NULL;
1886 /* f(lambda x: x[0] = 3) ends up getting parsed with
1887 * LHS test = lambda x: x[0], and RHS test = 3.
1888 * SF bug 132313 points out that complaining about a keyword
1889 * then is very confusing.
1890 */
1891 if (e->kind == Lambda_kind) {
1892 ast_error(CHILD(ch, 0),
1893 "lambda cannot contain assignment");
1894 return NULL;
1895 } else if (e->kind != Name_kind) {
1896 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1897 return NULL;
1898 }
1899 key = e->v.Name.id;
Georg Brandle06cf452007-06-07 13:23:24 +00001900 if (!strcmp(PyString_AS_STRING(key), "None")) {
1901 ast_error(CHILD(ch, 0), "assignment to None");
1902 return NULL;
1903 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001904 e = ast_for_expr(c, CHILD(ch, 2));
1905 if (!e)
1906 return NULL;
1907 kw = keyword(key, e, c->c_arena);
1908 if (!kw)
1909 return NULL;
1910 asdl_seq_SET(keywords, nkeywords++, kw);
1911 }
1912 }
1913 else if (TYPE(ch) == STAR) {
1914 vararg = ast_for_expr(c, CHILD(n, i+1));
1915 i++;
1916 }
1917 else if (TYPE(ch) == DOUBLESTAR) {
1918 kwarg = ast_for_expr(c, CHILD(n, i+1));
1919 i++;
1920 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 }
1922
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001923 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1924 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925}
1926
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001928ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001930 /* testlist_gexp: test (',' test)* [','] */
1931 /* testlist: test (',' test)* [','] */
1932 /* testlist_safe: test (',' test)+ [','] */
1933 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001935 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001936 if (NCH(n) > 1)
1937 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001938 }
1939 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001940 assert(TYPE(n) == testlist ||
1941 TYPE(n) == testlist_safe ||
1942 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001943 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001945 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001947 asdl_seq *tmp = seq_for_testlist(c, n);
1948 if (!tmp)
1949 return NULL;
1950 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001952}
1953
1954static expr_ty
1955ast_for_testlist_gexp(struct compiling *c, const node* n)
1956{
1957 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1958 /* argument: test [ gen_for ] */
1959 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001960 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001961 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001962 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001963}
1964
1965/* like ast_for_testlist() but returns a sequence */
1966static asdl_seq*
1967ast_for_class_bases(struct compiling *c, const node* n)
1968{
1969 /* testlist: test (',' test)* [','] */
1970 assert(NCH(n) > 0);
1971 REQ(n, testlist);
1972 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001973 expr_ty base;
1974 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
1975 if (!bases)
1976 return NULL;
1977 base = ast_for_expr(c, CHILD(n, 0));
1978 if (!base)
1979 return NULL;
1980 asdl_seq_SET(bases, 0, base);
1981 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001982 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001983
1984 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985}
1986
1987static stmt_ty
1988ast_for_expr_stmt(struct compiling *c, const node *n)
1989{
1990 REQ(n, expr_stmt);
1991 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001992 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 testlist: test (',' test)* [',']
1994 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001995 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 test: ... here starts the operator precendence dance
1997 */
1998
1999 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002000 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2001 if (!e)
2002 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002004 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 }
2006 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002007 expr_ty expr1, expr2;
2008 operator_ty newoperator;
2009 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002011 expr1 = ast_for_testlist(c, ch);
2012 if (!expr1)
2013 return NULL;
2014 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2015 switch (expr1->kind) {
2016 case GeneratorExp_kind:
2017 ast_error(ch, "augmented assignment to generator "
2018 "expression not possible");
2019 return NULL;
2020 case Yield_kind:
2021 ast_error(ch, "augmented assignment to yield "
2022 "expression not possible");
2023 return NULL;
2024 case Name_kind: {
2025 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2026 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2027 ast_error(ch, "assignment to None");
2028 return NULL;
2029 }
2030 break;
2031 }
2032 case Attribute_kind:
2033 case Subscript_kind:
2034 break;
2035 default:
2036 ast_error(ch, "illegal expression for augmented "
2037 "assignment");
2038 return NULL;
2039 }
2040 if(!set_context(expr1, Store, ch))
2041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002043 ch = CHILD(n, 2);
2044 if (TYPE(ch) == testlist)
2045 expr2 = ast_for_testlist(c, ch);
2046 else
2047 expr2 = ast_for_expr(c, ch);
2048 if (!expr2)
2049 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002051 newoperator = ast_for_augassign(CHILD(n, 1));
2052 if (!newoperator)
2053 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002055 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2056 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 }
2058 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002059 int i;
2060 asdl_seq *targets;
2061 node *value;
2062 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002064 /* a normal assignment */
2065 REQ(CHILD(n, 1), EQUAL);
2066 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2067 if (!targets)
2068 return NULL;
2069 for (i = 0; i < NCH(n) - 2; i += 2) {
2070 expr_ty e;
2071 node *ch = CHILD(n, i);
2072 if (TYPE(ch) == yield_expr) {
2073 ast_error(ch, "assignment to yield expression not possible");
2074 return NULL;
2075 }
2076 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002078 /* set context to assign */
2079 if (!e)
2080 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002082 if (!set_context(e, Store, CHILD(n, i)))
2083 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002085 asdl_seq_SET(targets, i / 2, e);
2086 }
2087 value = CHILD(n, NCH(n) - 1);
2088 if (TYPE(value) == testlist)
2089 expression = ast_for_testlist(c, value);
2090 else
2091 expression = ast_for_expr(c, value);
2092 if (!expression)
2093 return NULL;
2094 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2095 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097}
2098
2099static stmt_ty
2100ast_for_print_stmt(struct compiling *c, const node *n)
2101{
2102 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002103 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 */
2105 expr_ty dest = NULL, expression;
2106 asdl_seq *seq;
2107 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002108 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109
2110 REQ(n, print_stmt);
2111 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002112 dest = ast_for_expr(c, CHILD(n, 2));
2113 if (!dest)
2114 return NULL;
2115 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002117 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002119 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002120 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002121 expression = ast_for_expr(c, CHILD(n, i));
2122 if (!expression)
2123 return NULL;
2124 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 }
2126 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002127 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128}
2129
2130static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002131ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132{
2133 asdl_seq *seq;
2134 int i;
2135 expr_ty e;
2136
2137 REQ(n, exprlist);
2138
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002139 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002141 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002143 e = ast_for_expr(c, CHILD(n, i));
2144 if (!e)
2145 return NULL;
2146 asdl_seq_SET(seq, i / 2, e);
2147 if (context && !set_context(e, context, CHILD(n, i)))
2148 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 }
2150 return seq;
2151}
2152
2153static stmt_ty
2154ast_for_del_stmt(struct compiling *c, const node *n)
2155{
2156 asdl_seq *expr_list;
2157
2158 /* del_stmt: 'del' exprlist */
2159 REQ(n, del_stmt);
2160
2161 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2162 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002163 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002164 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165}
2166
2167static stmt_ty
2168ast_for_flow_stmt(struct compiling *c, const node *n)
2169{
2170 /*
2171 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002172 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 break_stmt: 'break'
2174 continue_stmt: 'continue'
2175 return_stmt: 'return' [testlist]
2176 yield_stmt: yield_expr
2177 yield_expr: 'yield' testlist
2178 raise_stmt: 'raise' [test [',' test [',' test]]]
2179 */
2180 node *ch;
2181
2182 REQ(n, flow_stmt);
2183 ch = CHILD(n, 0);
2184 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002185 case break_stmt:
2186 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2187 case continue_stmt:
2188 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2189 case yield_stmt: { /* will reduce to yield_expr */
2190 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2191 if (!exp)
2192 return NULL;
2193 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2194 }
2195 case return_stmt:
2196 if (NCH(ch) == 1)
2197 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2198 else {
2199 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2200 if (!expression)
2201 return NULL;
2202 return Return(expression, LINENO(n), n->n_col_offset,
2203 c->c_arena);
2204 }
2205 case raise_stmt:
2206 if (NCH(ch) == 1)
2207 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2208 c->c_arena);
2209 else if (NCH(ch) == 2) {
2210 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2211 if (!expression)
2212 return NULL;
2213 return Raise(expression, NULL, NULL, LINENO(n),
2214 n->n_col_offset, c->c_arena);
2215 }
2216 else if (NCH(ch) == 4) {
2217 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002219 expr1 = ast_for_expr(c, CHILD(ch, 1));
2220 if (!expr1)
2221 return NULL;
2222 expr2 = ast_for_expr(c, CHILD(ch, 3));
2223 if (!expr2)
2224 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002226 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2227 c->c_arena);
2228 }
2229 else if (NCH(ch) == 6) {
2230 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002232 expr1 = ast_for_expr(c, CHILD(ch, 1));
2233 if (!expr1)
2234 return NULL;
2235 expr2 = ast_for_expr(c, CHILD(ch, 3));
2236 if (!expr2)
2237 return NULL;
2238 expr3 = ast_for_expr(c, CHILD(ch, 5));
2239 if (!expr3)
2240 return NULL;
2241
2242 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2243 c->c_arena);
2244 }
2245 default:
2246 PyErr_Format(PyExc_SystemError,
2247 "unexpected flow_stmt: %d", TYPE(ch));
2248 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002250
2251 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2252 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253}
2254
2255static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002256alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257{
2258 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002259 import_as_name: NAME ['as' NAME]
2260 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 dotted_name: NAME ('.' NAME)*
2262 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002263 PyObject *str;
2264
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 loop:
2266 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002267 case import_as_name:
2268 str = NULL;
2269 if (NCH(n) == 3) {
2270 str = NEW_IDENTIFIER(CHILD(n, 2));
2271 }
2272 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2273 case dotted_as_name:
2274 if (NCH(n) == 1) {
2275 n = CHILD(n, 0);
2276 goto loop;
2277 }
2278 else {
2279 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2280 if (!a)
2281 return NULL;
2282 assert(!a->asname);
2283 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2284 return a;
2285 }
2286 break;
2287 case dotted_name:
2288 if (NCH(n) == 1)
2289 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2290 else {
2291 /* Create a string of the form "a.b.c" */
2292 int i;
2293 size_t len;
2294 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002296 len = 0;
2297 for (i = 0; i < NCH(n); i += 2)
2298 /* length of string plus one for the dot */
2299 len += strlen(STR(CHILD(n, i))) + 1;
2300 len--; /* the last name doesn't have a dot */
2301 str = PyString_FromStringAndSize(NULL, len);
2302 if (!str)
2303 return NULL;
2304 s = PyString_AS_STRING(str);
2305 if (!s)
2306 return NULL;
2307 for (i = 0; i < NCH(n); i += 2) {
2308 char *sch = STR(CHILD(n, i));
2309 strcpy(s, STR(CHILD(n, i)));
2310 s += strlen(sch);
2311 *s++ = '.';
2312 }
2313 --s;
2314 *s = '\0';
2315 PyString_InternInPlace(&str);
2316 PyArena_AddPyObject(c->c_arena, str);
2317 return alias(str, NULL, c->c_arena);
2318 }
2319 break;
2320 case STAR:
2321 str = PyString_InternFromString("*");
2322 PyArena_AddPyObject(c->c_arena, str);
2323 return alias(str, NULL, c->c_arena);
2324 default:
2325 PyErr_Format(PyExc_SystemError,
2326 "unexpected import name: %d", TYPE(n));
2327 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002329
2330 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 return NULL;
2332}
2333
2334static stmt_ty
2335ast_for_import_stmt(struct compiling *c, const node *n)
2336{
2337 /*
2338 import_stmt: import_name | import_from
2339 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002340 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002341 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002343 int lineno;
2344 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 int i;
2346 asdl_seq *aliases;
2347
2348 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002349 lineno = LINENO(n);
2350 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002352 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002353 n = CHILD(n, 1);
2354 REQ(n, dotted_as_names);
2355 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2356 if (!aliases)
2357 return NULL;
2358 for (i = 0; i < NCH(n); i += 2) {
2359 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2360 if (!import_alias)
2361 return NULL;
2362 asdl_seq_SET(aliases, i / 2, import_alias);
2363 }
2364 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002366 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002367 int n_children;
2368 int idx, ndots = 0;
2369 alias_ty mod = NULL;
2370 identifier modname;
2371
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002372 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002373 optional module name */
2374 for (idx = 1; idx < NCH(n); idx++) {
2375 if (TYPE(CHILD(n, idx)) == dotted_name) {
2376 mod = alias_for_import_name(c, CHILD(n, idx));
2377 idx++;
2378 break;
2379 } else if (TYPE(CHILD(n, idx)) != DOT) {
2380 break;
2381 }
2382 ndots++;
2383 }
2384 idx++; /* skip over the 'import' keyword */
2385 switch (TYPE(CHILD(n, idx))) {
2386 case STAR:
2387 /* from ... import * */
2388 n = CHILD(n, idx);
2389 n_children = 1;
2390 if (ndots) {
2391 ast_error(n, "'import *' not allowed with 'from .'");
2392 return NULL;
2393 }
2394 break;
2395 case LPAR:
2396 /* from ... import (x, y, z) */
2397 n = CHILD(n, idx + 1);
2398 n_children = NCH(n);
2399 break;
2400 case import_as_names:
2401 /* from ... import x, y, z */
2402 n = CHILD(n, idx);
2403 n_children = NCH(n);
2404 if (n_children % 2 == 0) {
2405 ast_error(n, "trailing comma not allowed without"
2406 " surrounding parentheses");
2407 return NULL;
2408 }
2409 break;
2410 default:
2411 ast_error(n, "Unexpected node-type in from-import");
2412 return NULL;
2413 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002415 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2416 if (!aliases)
2417 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002419 /* handle "from ... import *" special b/c there's no children */
2420 if (TYPE(n) == STAR) {
2421 alias_ty import_alias = alias_for_import_name(c, n);
2422 if (!import_alias)
2423 return NULL;
2424 asdl_seq_SET(aliases, 0, import_alias);
2425 }
2426 else {
2427 for (i = 0; i < NCH(n); i += 2) {
2428 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2429 if (!import_alias)
2430 return NULL;
2431 asdl_seq_SET(aliases, i / 2, import_alias);
2432 }
2433 }
2434 if (mod != NULL)
2435 modname = mod->name;
2436 else
2437 modname = new_identifier("", c->c_arena);
2438 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2439 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 }
Neal Norwitz79792652005-11-14 04:25:03 +00002441 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002442 "unknown import statement: starts with command '%s'",
2443 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 return NULL;
2445}
2446
2447static stmt_ty
2448ast_for_global_stmt(struct compiling *c, const node *n)
2449{
2450 /* global_stmt: 'global' NAME (',' NAME)* */
2451 identifier name;
2452 asdl_seq *s;
2453 int i;
2454
2455 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002456 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002458 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002460 name = NEW_IDENTIFIER(CHILD(n, i));
2461 if (!name)
2462 return NULL;
2463 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002465 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466}
2467
2468static stmt_ty
2469ast_for_exec_stmt(struct compiling *c, const node *n)
2470{
2471 expr_ty expr1, globals = NULL, locals = NULL;
2472 int n_children = NCH(n);
2473 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002474 PyErr_Format(PyExc_SystemError,
2475 "poorly formed 'exec' statement: %d parts to statement",
2476 n_children);
2477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 }
2479
2480 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2481 REQ(n, exec_stmt);
2482 expr1 = ast_for_expr(c, CHILD(n, 1));
2483 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002484 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002486 globals = ast_for_expr(c, CHILD(n, 3));
2487 if (!globals)
2488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 }
2490 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002491 locals = ast_for_expr(c, CHILD(n, 5));
2492 if (!locals)
2493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 }
2495
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002496 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2497 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498}
2499
2500static stmt_ty
2501ast_for_assert_stmt(struct compiling *c, const node *n)
2502{
2503 /* assert_stmt: 'assert' test [',' test] */
2504 REQ(n, assert_stmt);
2505 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002506 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2507 if (!expression)
2508 return NULL;
2509 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2510 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 }
2512 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002513 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002515 expr1 = ast_for_expr(c, CHILD(n, 1));
2516 if (!expr1)
2517 return NULL;
2518 expr2 = ast_for_expr(c, CHILD(n, 3));
2519 if (!expr2)
2520 return NULL;
2521
2522 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 }
Neal Norwitz79792652005-11-14 04:25:03 +00002524 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002525 "improper number of parts to 'assert' statement: %d",
2526 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 return NULL;
2528}
2529
2530static asdl_seq *
2531ast_for_suite(struct compiling *c, const node *n)
2532{
2533 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002534 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 stmt_ty s;
2536 int i, total, num, end, pos = 0;
2537 node *ch;
2538
2539 REQ(n, suite);
2540
2541 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002542 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002544 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002546 n = CHILD(n, 0);
2547 /* simple_stmt always ends with a NEWLINE,
2548 and may have a trailing SEMI
2549 */
2550 end = NCH(n) - 1;
2551 if (TYPE(CHILD(n, end - 1)) == SEMI)
2552 end--;
2553 /* loop by 2 to skip semi-colons */
2554 for (i = 0; i < end; i += 2) {
2555 ch = CHILD(n, i);
2556 s = ast_for_stmt(c, ch);
2557 if (!s)
2558 return NULL;
2559 asdl_seq_SET(seq, pos++, s);
2560 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
2562 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002563 for (i = 2; i < (NCH(n) - 1); i++) {
2564 ch = CHILD(n, i);
2565 REQ(ch, stmt);
2566 num = num_stmts(ch);
2567 if (num == 1) {
2568 /* small_stmt or compound_stmt with only one child */
2569 s = ast_for_stmt(c, ch);
2570 if (!s)
2571 return NULL;
2572 asdl_seq_SET(seq, pos++, s);
2573 }
2574 else {
2575 int j;
2576 ch = CHILD(ch, 0);
2577 REQ(ch, simple_stmt);
2578 for (j = 0; j < NCH(ch); j += 2) {
2579 /* statement terminates with a semi-colon ';' */
2580 if (NCH(CHILD(ch, j)) == 0) {
2581 assert((j + 1) == NCH(ch));
2582 break;
2583 }
2584 s = ast_for_stmt(c, CHILD(ch, j));
2585 if (!s)
2586 return NULL;
2587 asdl_seq_SET(seq, pos++, s);
2588 }
2589 }
2590 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 }
2592 assert(pos == seq->size);
2593 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594}
2595
2596static stmt_ty
2597ast_for_if_stmt(struct compiling *c, const node *n)
2598{
2599 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2600 ['else' ':' suite]
2601 */
2602 char *s;
2603
2604 REQ(n, if_stmt);
2605
2606 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002607 expr_ty expression;
2608 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002610 expression = ast_for_expr(c, CHILD(n, 1));
2611 if (!expression)
2612 return NULL;
2613 suite_seq = ast_for_suite(c, CHILD(n, 3));
2614 if (!suite_seq)
2615 return NULL;
2616
2617 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2618 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002620
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 s = STR(CHILD(n, 4));
2622 /* s[2], the third character in the string, will be
2623 's' for el_s_e, or
2624 'i' for el_i_f
2625 */
2626 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002627 expr_ty expression;
2628 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002630 expression = ast_for_expr(c, CHILD(n, 1));
2631 if (!expression)
2632 return NULL;
2633 seq1 = ast_for_suite(c, CHILD(n, 3));
2634 if (!seq1)
2635 return NULL;
2636 seq2 = ast_for_suite(c, CHILD(n, 6));
2637 if (!seq2)
2638 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002640 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2641 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 }
2643 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002644 int i, n_elif, has_else = 0;
2645 expr_ty expression;
2646 asdl_seq *suite_seq;
2647 asdl_seq *orelse = NULL;
2648 n_elif = NCH(n) - 4;
2649 /* must reference the child n_elif+1 since 'else' token is third,
2650 not fourth, child from the end. */
2651 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2652 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2653 has_else = 1;
2654 n_elif -= 3;
2655 }
2656 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002658 if (has_else) {
2659 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002661 orelse = asdl_seq_new(1, c->c_arena);
2662 if (!orelse)
2663 return NULL;
2664 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2665 if (!expression)
2666 return NULL;
2667 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2668 if (!suite_seq)
2669 return NULL;
2670 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2671 if (!suite_seq2)
2672 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002674 asdl_seq_SET(orelse, 0,
2675 If(expression, suite_seq, suite_seq2,
2676 LINENO(CHILD(n, NCH(n) - 6)),
2677 CHILD(n, NCH(n) - 6)->n_col_offset,
2678 c->c_arena));
2679 /* the just-created orelse handled the last elif */
2680 n_elif--;
2681 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002683 for (i = 0; i < n_elif; i++) {
2684 int off = 5 + (n_elif - i - 1) * 4;
2685 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2686 if (!newobj)
2687 return NULL;
2688 expression = ast_for_expr(c, CHILD(n, off));
2689 if (!expression)
2690 return NULL;
2691 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2692 if (!suite_seq)
2693 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002695 asdl_seq_SET(newobj, 0,
2696 If(expression, suite_seq, orelse,
2697 LINENO(CHILD(n, off)),
2698 CHILD(n, off)->n_col_offset, c->c_arena));
2699 orelse = newobj;
2700 }
2701 expression = ast_for_expr(c, CHILD(n, 1));
2702 if (!expression)
2703 return NULL;
2704 suite_seq = ast_for_suite(c, CHILD(n, 3));
2705 if (!suite_seq)
2706 return NULL;
2707 return If(expression, suite_seq, orelse,
2708 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002710
2711 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002712 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002713 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714}
2715
2716static stmt_ty
2717ast_for_while_stmt(struct compiling *c, const node *n)
2718{
2719 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2720 REQ(n, while_stmt);
2721
2722 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002723 expr_ty expression;
2724 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002726 expression = ast_for_expr(c, CHILD(n, 1));
2727 if (!expression)
2728 return NULL;
2729 suite_seq = ast_for_suite(c, CHILD(n, 3));
2730 if (!suite_seq)
2731 return NULL;
2732 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2733 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 }
2735 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002736 expr_ty expression;
2737 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002739 expression = ast_for_expr(c, CHILD(n, 1));
2740 if (!expression)
2741 return NULL;
2742 seq1 = ast_for_suite(c, CHILD(n, 3));
2743 if (!seq1)
2744 return NULL;
2745 seq2 = ast_for_suite(c, CHILD(n, 6));
2746 if (!seq2)
2747 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002749 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2750 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002752
2753 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002754 "wrong number of tokens for 'while' statement: %d",
2755 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002756 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757}
2758
2759static stmt_ty
2760ast_for_for_stmt(struct compiling *c, const node *n)
2761{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002762 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 expr_ty expression;
2764 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002765 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2767 REQ(n, for_stmt);
2768
2769 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002770 seq = ast_for_suite(c, CHILD(n, 8));
2771 if (!seq)
2772 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 }
2774
Neal Norwitzedef2be2006-07-12 05:26:17 +00002775 node_target = CHILD(n, 1);
2776 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002777 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002778 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002779 /* Check the # of children rather than the length of _target, since
2780 for x, in ... has 1 element in _target, but still requires a Tuple. */
2781 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002782 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002784 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002786 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002787 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002788 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002790 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002791 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002793 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002794 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795}
2796
2797static excepthandler_ty
2798ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2799{
Collin Winter62903052007-05-18 23:11:24 +00002800 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801 REQ(exc, except_clause);
2802 REQ(body, suite);
2803
2804 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002805 asdl_seq *suite_seq = ast_for_suite(c, body);
2806 if (!suite_seq)
2807 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002809 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2810 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 }
2812 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002813 expr_ty expression;
2814 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002816 expression = ast_for_expr(c, CHILD(exc, 1));
2817 if (!expression)
2818 return NULL;
2819 suite_seq = ast_for_suite(c, body);
2820 if (!suite_seq)
2821 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002823 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2824 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 }
2826 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002827 asdl_seq *suite_seq;
2828 expr_ty expression;
2829 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2830 if (!e)
2831 return NULL;
2832 if (!set_context(e, Store, CHILD(exc, 3)))
2833 return NULL;
2834 expression = ast_for_expr(c, CHILD(exc, 1));
2835 if (!expression)
2836 return NULL;
2837 suite_seq = ast_for_suite(c, body);
2838 if (!suite_seq)
2839 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002841 return excepthandler(expression, e, suite_seq, LINENO(exc),
2842 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002844
2845 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002846 "wrong number of children for 'except' clause: %d",
2847 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002848 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849}
2850
2851static stmt_ty
2852ast_for_try_stmt(struct compiling *c, const node *n)
2853{
Neal Norwitzf599f422005-12-17 21:33:47 +00002854 const int nch = NCH(n);
2855 int n_except = (nch - 3)/3;
2856 asdl_seq *body, *orelse = NULL, *finally = NULL;
2857
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 REQ(n, try_stmt);
2859
Neal Norwitzf599f422005-12-17 21:33:47 +00002860 body = ast_for_suite(c, CHILD(n, 2));
2861 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863
Neal Norwitzf599f422005-12-17 21:33:47 +00002864 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002865 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2866 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2867 /* we can assume it's an "else",
2868 because nch >= 9 for try-else-finally and
2869 it would otherwise have a type of except_clause */
2870 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2871 if (orelse == NULL)
2872 return NULL;
2873 n_except--;
2874 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002876 finally = ast_for_suite(c, CHILD(n, nch - 1));
2877 if (finally == NULL)
2878 return NULL;
2879 n_except--;
2880 }
2881 else {
2882 /* we can assume it's an "else",
2883 otherwise it would have a type of except_clause */
2884 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2885 if (orelse == NULL)
2886 return NULL;
2887 n_except--;
2888 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002890 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002891 ast_error(n, "malformed 'try' statement");
2892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002894
2895 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002896 int i;
2897 stmt_ty except_st;
2898 /* process except statements to create a try ... except */
2899 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2900 if (handlers == NULL)
2901 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002902
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002903 for (i = 0; i < n_except; i++) {
2904 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2905 CHILD(n, 5 + i * 3));
2906 if (!e)
2907 return NULL;
2908 asdl_seq_SET(handlers, i, e);
2909 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002910
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002911 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2912 n->n_col_offset, c->c_arena);
2913 if (!finally)
2914 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002915
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002916 /* if a 'finally' is present too, we nest the TryExcept within a
2917 TryFinally to emulate try ... except ... finally */
2918 body = asdl_seq_new(1, c->c_arena);
2919 if (body == NULL)
2920 return NULL;
2921 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002922 }
2923
2924 /* must be a try ... finally (except clauses are in body, if any exist) */
2925 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002926 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927}
2928
Guido van Rossumc2e20742006-02-27 22:32:47 +00002929static expr_ty
2930ast_for_with_var(struct compiling *c, const node *n)
2931{
2932 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002933 return ast_for_expr(c, CHILD(n, 1));
2934}
2935
2936/* with_stmt: 'with' test [ with_var ] ':' suite */
2937static stmt_ty
2938ast_for_with_stmt(struct compiling *c, const node *n)
2939{
2940 expr_ty context_expr, optional_vars = NULL;
2941 int suite_index = 3; /* skip 'with', test, and ':' */
2942 asdl_seq *suite_seq;
2943
2944 assert(TYPE(n) == with_stmt);
2945 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002946 if (!context_expr)
2947 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002948 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002949 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002950
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002951 if (!optional_vars) {
2952 return NULL;
2953 }
2954 if (!set_context(optional_vars, Store, n)) {
2955 return NULL;
2956 }
2957 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002958 }
2959
2960 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2961 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002962 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002963 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002964 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002965 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002966}
2967
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968static stmt_ty
2969ast_for_classdef(struct compiling *c, const node *n)
2970{
2971 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972 asdl_seq *bases, *s;
2973
2974 REQ(n, classdef);
2975
2976 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002977 ast_error(n, "assignment to None");
2978 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 }
2980
2981 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002982 s = ast_for_suite(c, CHILD(n, 3));
2983 if (!s)
2984 return NULL;
2985 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2986 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 }
2988 /* check for empty base list */
2989 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002990 s = ast_for_suite(c, CHILD(n,5));
2991 if (!s)
2992 return NULL;
2993 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2994 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 }
2996
2997 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002998 bases = ast_for_class_bases(c, CHILD(n, 3));
2999 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001
3002 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003003 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003004 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003005 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003006 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007}
3008
3009static stmt_ty
3010ast_for_stmt(struct compiling *c, const node *n)
3011{
3012 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003013 assert(NCH(n) == 1);
3014 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 }
3016 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003017 assert(num_stmts(n) == 1);
3018 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019 }
3020 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003021 REQ(n, small_stmt);
3022 n = CHILD(n, 0);
3023 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3024 | flow_stmt | import_stmt | global_stmt | exec_stmt
3025 | assert_stmt
3026 */
3027 switch (TYPE(n)) {
3028 case expr_stmt:
3029 return ast_for_expr_stmt(c, n);
3030 case print_stmt:
3031 return ast_for_print_stmt(c, n);
3032 case del_stmt:
3033 return ast_for_del_stmt(c, n);
3034 case pass_stmt:
3035 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3036 case flow_stmt:
3037 return ast_for_flow_stmt(c, n);
3038 case import_stmt:
3039 return ast_for_import_stmt(c, n);
3040 case global_stmt:
3041 return ast_for_global_stmt(c, n);
3042 case exec_stmt:
3043 return ast_for_exec_stmt(c, n);
3044 case assert_stmt:
3045 return ast_for_assert_stmt(c, n);
3046 default:
3047 PyErr_Format(PyExc_SystemError,
3048 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3049 TYPE(n), NCH(n));
3050 return NULL;
3051 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052 }
3053 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003054 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3055 | funcdef | classdef
3056 */
3057 node *ch = CHILD(n, 0);
3058 REQ(n, compound_stmt);
3059 switch (TYPE(ch)) {
3060 case if_stmt:
3061 return ast_for_if_stmt(c, ch);
3062 case while_stmt:
3063 return ast_for_while_stmt(c, ch);
3064 case for_stmt:
3065 return ast_for_for_stmt(c, ch);
3066 case try_stmt:
3067 return ast_for_try_stmt(c, ch);
3068 case with_stmt:
3069 return ast_for_with_stmt(c, ch);
3070 case funcdef:
3071 return ast_for_funcdef(c, ch);
3072 case classdef:
3073 return ast_for_classdef(c, ch);
3074 default:
3075 PyErr_Format(PyExc_SystemError,
3076 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3077 TYPE(n), NCH(n));
3078 return NULL;
3079 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 }
3081}
3082
3083static PyObject *
3084parsenumber(const char *s)
3085{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003086 const char *end;
3087 long x;
3088 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003090 Py_complex c;
3091 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092#endif
3093
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003094 errno = 0;
3095 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003097 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003098#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003099 if (*end == 'l' || *end == 'L')
3100 return PyLong_FromString((char *)s, (char **)0, 0);
3101 if (s[0] == '0') {
3102 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3103 if (x < 0 && errno == 0) {
3104 return PyLong_FromString((char *)s,
3105 (char **)0,
3106 0);
3107 }
3108 }
3109 else
3110 x = PyOS_strtol((char *)s, (char **)&end, 0);
3111 if (*end == '\0') {
3112 if (errno != 0)
3113 return PyLong_FromString((char *)s, (char **)0, 0);
3114 return PyInt_FromLong(x);
3115 }
3116 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003118 if (imflag) {
3119 c.real = 0.;
3120 PyFPE_START_PROTECT("atof", return 0)
3121 c.imag = PyOS_ascii_atof(s);
3122 PyFPE_END_PROTECT(c)
3123 return PyComplex_FromCComplex(c);
3124 }
3125 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003127 {
3128 PyFPE_START_PROTECT("atof", return 0)
3129 dx = PyOS_ascii_atof(s);
3130 PyFPE_END_PROTECT(dx)
3131 return PyFloat_FromDouble(dx);
3132 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133}
3134
3135static PyObject *
3136decode_utf8(const char **sPtr, const char *end, char* encoding)
3137{
3138#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003139 Py_FatalError("decode_utf8 should not be called in this build.");
3140 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003142 PyObject *u, *v;
3143 char *s, *t;
3144 t = s = (char *)*sPtr;
3145 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3146 while (s < end && (*s & 0x80)) s++;
3147 *sPtr = s;
3148 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3149 if (u == NULL)
3150 return NULL;
3151 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3152 Py_DECREF(u);
3153 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154#endif
3155}
3156
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003157#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158static PyObject *
3159decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3160{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003161 PyObject *v, *u;
3162 char *buf;
3163 char *p;
3164 const char *end;
3165 if (encoding == NULL) {
3166 buf = (char *)s;
3167 u = NULL;
3168 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3169 buf = (char *)s;
3170 u = NULL;
3171 } else {
3172 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3173 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3174 if (u == NULL)
3175 return NULL;
3176 p = buf = PyString_AsString(u);
3177 end = s + len;
3178 while (s < end) {
3179 if (*s == '\\') {
3180 *p++ = *s++;
3181 if (*s & 0x80) {
3182 strcpy(p, "u005c");
3183 p += 5;
3184 }
3185 }
3186 if (*s & 0x80) { /* XXX inefficient */
3187 PyObject *w;
3188 char *r;
3189 Py_ssize_t rn, i;
3190 w = decode_utf8(&s, end, "utf-16-be");
3191 if (w == NULL) {
3192 Py_DECREF(u);
3193 return NULL;
3194 }
3195 r = PyString_AsString(w);
3196 rn = PyString_Size(w);
3197 assert(rn % 2 == 0);
3198 for (i = 0; i < rn; i += 2) {
3199 sprintf(p, "\\u%02x%02x",
3200 r[i + 0] & 0xFF,
3201 r[i + 1] & 0xFF);
3202 p += 6;
3203 }
3204 Py_DECREF(w);
3205 } else {
3206 *p++ = *s++;
3207 }
3208 }
3209 len = p - buf;
3210 s = buf;
3211 }
3212 if (rawmode)
3213 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3214 else
3215 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3216 Py_XDECREF(u);
3217 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003219#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220
3221/* s is a Python string literal, including the bracketing quote characters,
3222 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3223 * parsestr parses it, and returns the decoded Python string object.
3224 */
3225static PyObject *
3226parsestr(const char *s, const char *encoding)
3227{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003228 size_t len;
3229 int quote = Py_CHARMASK(*s);
3230 int rawmode = 0;
3231 int need_encoding;
3232 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003234 if (isalpha(quote) || quote == '_') {
3235 if (quote == 'u' || quote == 'U') {
3236 quote = *++s;
3237 unicode = 1;
3238 }
3239 if (quote == 'r' || quote == 'R') {
3240 quote = *++s;
3241 rawmode = 1;
3242 }
3243 }
3244 if (quote != '\'' && quote != '\"') {
3245 PyErr_BadInternalCall();
3246 return NULL;
3247 }
3248 s++;
3249 len = strlen(s);
3250 if (len > INT_MAX) {
3251 PyErr_SetString(PyExc_OverflowError,
3252 "string to parse is too long");
3253 return NULL;
3254 }
3255 if (s[--len] != quote) {
3256 PyErr_BadInternalCall();
3257 return NULL;
3258 }
3259 if (len >= 4 && s[0] == quote && s[1] == quote) {
3260 s += 2;
3261 len -= 2;
3262 if (s[--len] != quote || s[--len] != quote) {
3263 PyErr_BadInternalCall();
3264 return NULL;
3265 }
3266 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003268 if (unicode || Py_UnicodeFlag) {
3269 return decode_unicode(s, len, rawmode, encoding);
3270 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003272 need_encoding = (encoding != NULL &&
3273 strcmp(encoding, "utf-8") != 0 &&
3274 strcmp(encoding, "iso-8859-1") != 0);
3275 if (rawmode || strchr(s, '\\') == NULL) {
3276 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003278 /* This should not happen - we never see any other
3279 encoding. */
3280 Py_FatalError(
3281 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003283 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3284 if (u == NULL)
3285 return NULL;
3286 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3287 Py_DECREF(u);
3288 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003290 } else {
3291 return PyString_FromStringAndSize(s, len);
3292 }
3293 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003295 return PyString_DecodeEscape(s, len, NULL, unicode,
3296 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297}
3298
3299/* Build a Python string object out of a STRING atom. This takes care of
3300 * compile-time literal catenation, calling parsestr() on each piece, and
3301 * pasting the intermediate results together.
3302 */
3303static PyObject *
3304parsestrplus(struct compiling *c, const node *n)
3305{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003306 PyObject *v;
3307 int i;
3308 REQ(CHILD(n, 0), STRING);
3309 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3310 /* String literal concatenation */
3311 for (i = 1; i < NCH(n); i++) {
3312 PyObject *s;
3313 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3314 if (s == NULL)
3315 goto onError;
3316 if (PyString_Check(v) && PyString_Check(s)) {
3317 PyString_ConcatAndDel(&v, s);
3318 if (v == NULL)
3319 goto onError;
3320 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003322 else {
3323 PyObject *temp = PyUnicode_Concat(v, s);
3324 Py_DECREF(s);
3325 Py_DECREF(v);
3326 v = temp;
3327 if (v == NULL)
3328 goto onError;
3329 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003331 }
3332 }
3333 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334
3335 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003336 Py_XDECREF(v);
3337 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338}