blob: 6fd1ebe2c9994f8d275e0d22e83688df7673525e [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/* XXX TO DO
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
21 - syntax errors
22*/
23
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024/* Data structure used internally */
25struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000026 char *c_encoding; /* source encoding */
27 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028};
29
30static asdl_seq *seq_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_expr(struct compiling *, const node *);
32static stmt_ty ast_for_stmt(struct compiling *, const node *);
33static asdl_seq *ast_for_suite(struct compiling *, const node *);
Martin v. Löwis28457502006-04-11 09:17:27 +000034static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Georg Brandl7784f122006-05-26 20:04:44 +0000110 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Neal Norwitze98ccf62006-03-23 05:39:47 +0000194 if (TYPE(n) == encoding_decl) {
195 ast_error(n, "encoding declaration in Unicode string");
196 goto error;
197 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 } else if (TYPE(n) == encoding_decl) {
199 c.c_encoding = STR(n);
200 n = CHILD(n, 0);
201 } else {
202 c.c_encoding = NULL;
203 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000204 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Jeremy Hyltona8293132006-02-28 17:58:27 +0000206 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 switch (TYPE(n)) {
208 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000209 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 if (!stmts)
211 return NULL;
212 for (i = 0; i < NCH(n) - 1; i++) {
213 ch = CHILD(n, i);
214 if (TYPE(ch) == NEWLINE)
215 continue;
216 REQ(ch, stmt);
217 num = num_stmts(ch);
218 if (num == 1) {
219 s = ast_for_stmt(&c, ch);
220 if (!s)
221 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000222 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223 }
224 else {
225 ch = CHILD(ch, 0);
226 REQ(ch, simple_stmt);
227 for (j = 0; j < num; j++) {
228 s = ast_for_stmt(&c, CHILD(ch, j * 2));
229 if (!s)
230 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000231 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 }
233 }
234 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000235 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 case eval_input: {
237 expr_ty testlist_ast;
238
239 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000240 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241 if (!testlist_ast)
242 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 }
245 case single_input:
246 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 if (!stmts)
249 goto error;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
251 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 }
254 else {
255 n = CHILD(n, 0);
256 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000257 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 if (!stmts)
259 goto error;
260 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000261 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262 if (!s)
263 goto error;
264 asdl_seq_SET(stmts, 0, s);
265 }
266 else {
267 /* Only a simple_stmt can contain multiple statements. */
268 REQ(n, simple_stmt);
269 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 if (TYPE(CHILD(n, i)) == NEWLINE)
271 break;
272 s = ast_for_stmt(&c, CHILD(n, i));
273 if (!s)
274 goto error;
275 asdl_seq_SET(stmts, i / 2, s);
276 }
277 }
278
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000279 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 }
281 default:
282 goto error;
283 }
284 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 ast_error_finish(filename);
286 return NULL;
287}
288
289/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
290*/
291
292static operator_ty
293get_operator(const node *n)
294{
295 switch (TYPE(n)) {
296 case VBAR:
297 return BitOr;
298 case CIRCUMFLEX:
299 return BitXor;
300 case AMPER:
301 return BitAnd;
302 case LEFTSHIFT:
303 return LShift;
304 case RIGHTSHIFT:
305 return RShift;
306 case PLUS:
307 return Add;
308 case MINUS:
309 return Sub;
310 case STAR:
311 return Mult;
312 case SLASH:
313 return Div;
314 case DOUBLESLASH:
315 return FloorDiv;
316 case PERCENT:
317 return Mod;
318 default:
Martin v. Löwis28457502006-04-11 09:17:27 +0000319 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 }
321}
322
Jeremy Hyltona8293132006-02-28 17:58:27 +0000323/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324
325 Only sets context for expr kinds that "can appear in assignment context"
326 (according to ../Parser/Python.asdl). For other expr kinds, it sets
327 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328*/
329
330static int
331set_context(expr_ty e, expr_context_ty ctx, const node *n)
332{
333 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334 /* If a particular expression type can't be used for assign / delete,
335 set expr_name to its name and an error message will be generated.
336 */
337 const char* expr_name = NULL;
338
339 /* The ast defines augmented store and load contexts, but the
340 implementation here doesn't actually use them. The code may be
341 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000342 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000343 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000344 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345 */
346 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
348 switch (e->kind) {
349 case Attribute_kind:
350 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000351 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 return ast_error(n, "assignment to None");
353 }
354 e->v.Attribute.ctx = ctx;
355 break;
356 case Subscript_kind:
357 e->v.Subscript.ctx = ctx;
358 break;
359 case Name_kind:
360 if (ctx == Store &&
361 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
362 return ast_error(n, "assignment to None");
363 }
364 e->v.Name.ctx = ctx;
365 break;
366 case List_kind:
367 e->v.List.ctx = ctx;
368 s = e->v.List.elts;
369 break;
370 case Tuple_kind:
371 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
372 return ast_error(n, "can't assign to ()");
373 e->v.Tuple.ctx = ctx;
374 s = e->v.Tuple.elts;
375 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 case Lambda_kind:
377 expr_name = "lambda";
378 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000380 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000384 case UnaryOp_kind:
385 expr_name = "operator";
386 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000388 expr_name = "generator expression";
389 break;
390 case ListComp_kind:
391 expr_name = "list comprehension";
392 break;
393 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394 case Num_kind:
395 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000396 expr_name = "literal";
397 break;
398 case Compare_kind:
399 expr_name = "comparison";
400 break;
401 case Repr_kind:
402 expr_name = "repr";
403 break;
Neal Norwitz373f0a72006-05-15 07:04:36 +0000404 case IfExp_kind:
405 expr_name = "conditional expression";
406 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000407 default:
408 PyErr_Format(PyExc_SystemError,
409 "unexpected expression in assignment %d (line %d)",
410 e->kind, e->lineno);
411 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000413 /* Check for error string set by switch */
414 if (expr_name) {
415 char buf[300];
416 PyOS_snprintf(buf, sizeof(buf),
417 "can't %s %s",
418 ctx == Store ? "assign to" : "delete",
419 expr_name);
420 return ast_error(n, buf);
421 }
422
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000424 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425 */
426 if (s) {
427 int i;
428
429 for (i = 0; i < asdl_seq_LEN(s); i++) {
Martin v. Löwis28457502006-04-11 09:17:27 +0000430 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431 return 0;
432 }
433 }
434 return 1;
435}
436
437static operator_ty
438ast_for_augassign(const node *n)
439{
440 REQ(n, augassign);
441 n = CHILD(n, 0);
442 switch (STR(n)[0]) {
443 case '+':
444 return Add;
445 case '-':
446 return Sub;
447 case '/':
448 if (STR(n)[1] == '/')
449 return FloorDiv;
450 else
451 return Div;
452 case '%':
453 return Mod;
454 case '<':
455 return LShift;
456 case '>':
457 return RShift;
458 case '&':
459 return BitAnd;
460 case '^':
461 return BitXor;
462 case '|':
463 return BitOr;
464 case '*':
465 if (STR(n)[1] == '*')
466 return Pow;
467 else
468 return Mult;
469 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000470 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000471 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472 }
473}
474
475static cmpop_ty
476ast_for_comp_op(const node *n)
477{
478 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
479 |'is' 'not'
480 */
481 REQ(n, comp_op);
482 if (NCH(n) == 1) {
483 n = CHILD(n, 0);
484 switch (TYPE(n)) {
485 case LESS:
486 return Lt;
487 case GREATER:
488 return Gt;
489 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 return Eq;
491 case LESSEQUAL:
492 return LtE;
493 case GREATEREQUAL:
494 return GtE;
495 case NOTEQUAL:
496 return NotEq;
497 case NAME:
498 if (strcmp(STR(n), "in") == 0)
499 return In;
500 if (strcmp(STR(n), "is") == 0)
501 return Is;
502 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000503 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000505 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 }
507 }
508 else if (NCH(n) == 2) {
509 /* handle "not in" and "is not" */
510 switch (TYPE(CHILD(n, 0))) {
511 case NAME:
512 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
513 return NotIn;
514 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
515 return IsNot;
516 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000517 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Martin v. Löwis28457502006-04-11 09:17:27 +0000519 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 }
521 }
Neal Norwitz79792652005-11-14 04:25:03 +0000522 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000524 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525}
526
527static asdl_seq *
528seq_for_testlist(struct compiling *c, const node *n)
529{
530 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000531 asdl_seq *seq;
532 expr_ty expression;
533 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534 assert(TYPE(n) == testlist
535 || TYPE(n) == listmaker
536 || TYPE(n) == testlist_gexp
537 || TYPE(n) == testlist_safe
538 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000540 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 if (!seq)
542 return NULL;
543
544 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000545 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546
547 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000548 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
551 assert(i / 2 < seq->size);
552 asdl_seq_SET(seq, i / 2, expression);
553 }
554 return seq;
555}
556
557static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000558compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559{
560 int i, len = (NCH(n) + 1) / 2;
561 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000562 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 if (!args)
564 return NULL;
565
566 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 for (i = 0; i < len; i++) {
568 const node *child = CHILD(CHILD(n, 2*i), 0);
569 expr_ty arg;
570 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000571 if (!strcmp(STR(child), "None")) {
572 ast_error(child, "assignment to None");
573 return NULL;
574 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000575 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
576 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000577 }
578 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000579 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 asdl_seq_SET(args, i, arg);
582 }
583
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000584 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000585 if (!set_context(result, Store, n))
586 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587 return result;
588}
589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Jeremy Hyltona8293132006-02-28 17:58:27 +0000591/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592
593static arguments_ty
594ast_for_arguments(struct compiling *c, const node *n)
595{
596 /* parameters: '(' [varargslist] ')'
597 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
598 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
599 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000600 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 asdl_seq *args, *defaults;
602 identifier vararg = NULL, kwarg = NULL;
603 node *ch;
604
605 if (TYPE(n) == parameters) {
606 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000607 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 n = CHILD(n, 1);
609 }
610 REQ(n, varargslist);
611
612 /* first count the number of normal args & defaults */
613 for (i = 0; i < NCH(n); i++) {
614 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000615 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617 if (TYPE(ch) == EQUAL)
618 n_defaults++;
619 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000620 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 if (!args && n_args)
622 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000623 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 if (!defaults && n_defaults)
625 goto error;
626
627 /* fpdef: NAME | '(' fplist ')'
628 fplist: fpdef (',' fpdef)* [',']
629 */
630 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000631 j = 0; /* index for defaults */
632 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 while (i < NCH(n)) {
634 ch = CHILD(n, i);
635 switch (TYPE(ch)) {
636 case fpdef:
637 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
638 anything other than EQUAL or a comma? */
639 /* XXX Should NCH(n) check be made a separate check? */
640 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000641 asdl_seq_SET(defaults, j++,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 ast_for_expr(c, CHILD(n, i + 2)));
643 i += 2;
644 found_default = 1;
645 }
646 else if (found_default) {
647 ast_error(n,
648 "non-default argument follows default argument");
649 goto error;
650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000652 ch = CHILD(ch, 1);
653 /* def foo((x)): is not complex, special case. */
654 if (NCH(ch) != 1) {
655 /* We have complex arguments, setup for unpacking. */
656 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
657 } else {
658 /* def foo((x)): setup for checking NAME below. */
659 ch = CHILD(ch, 0);
660 }
661 }
662 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000663 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
665 ast_error(CHILD(ch, 0), "assignment to None");
666 goto error;
667 }
Armin Rigo31441302005-10-21 12:57:31 +0000668 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000669 Param, LINENO(ch), ch->n_col_offset,
670 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 if (!name)
672 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000673 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674
675 }
676 i += 2; /* the name and the comma */
677 break;
678 case STAR:
679 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
680 ast_error(CHILD(n, i+1), "assignment to None");
681 goto error;
682 }
683 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
684 i += 3;
685 break;
686 case DOUBLESTAR:
687 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
688 ast_error(CHILD(n, i+1), "assignment to None");
689 goto error;
690 }
691 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
692 i += 3;
693 break;
694 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000695 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 "unexpected node in varargslist: %d @ %d",
697 TYPE(ch), i);
698 goto error;
699 }
700 }
701
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000702 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
704 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000705 Py_XDECREF(vararg);
706 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 return NULL;
708}
709
710static expr_ty
711ast_for_dotted_name(struct compiling *c, const node *n)
712{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000713 expr_ty e;
714 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000715 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716 int i;
717
718 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000719
720 lineno = LINENO(n);
721 col_offset = n->n_col_offset;
722
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 id = NEW_IDENTIFIER(CHILD(n, 0));
724 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000725 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000726 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000728 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729
730 for (i = 2; i < NCH(n); i+=2) {
731 id = NEW_IDENTIFIER(CHILD(n, i));
732 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000733 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000734 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000735 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000736 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 }
738
739 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740}
741
742static expr_ty
743ast_for_decorator(struct compiling *c, const node *n)
744{
745 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
746 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000747 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748
749 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000750 REQ(CHILD(n, 0), AT);
751 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752
753 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
754 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756
757 if (NCH(n) == 3) { /* No arguments */
758 d = name_expr;
759 name_expr = NULL;
760 }
761 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000762 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
763 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 name_expr = NULL;
767 }
768 else {
769 d = ast_for_call(c, CHILD(n, 3), name_expr);
770 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 name_expr = NULL;
773 }
774
775 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776}
777
778static asdl_seq*
779ast_for_decorators(struct compiling *c, const node *n)
780{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000781 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000782 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 int i;
784
785 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000786 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 if (!decorator_seq)
788 return NULL;
789
790 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000791 d = ast_for_decorator(c, CHILD(n, i));
792 if (!d)
793 return NULL;
794 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 }
796 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797}
798
799static stmt_ty
800ast_for_funcdef(struct compiling *c, const node *n)
801{
802 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000803 identifier name;
804 arguments_ty args;
805 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 asdl_seq *decorator_seq = NULL;
807 int name_i;
808
809 REQ(n, funcdef);
810
811 if (NCH(n) == 6) { /* decorators are present */
812 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
813 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 name_i = 2;
816 }
817 else {
818 name_i = 1;
819 }
820
821 name = NEW_IDENTIFIER(CHILD(n, name_i));
822 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000825 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 }
828 args = ast_for_arguments(c, CHILD(n, name_i + 1));
829 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 body = ast_for_suite(c, CHILD(n, name_i + 3));
832 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000835 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
836 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837}
838
839static expr_ty
840ast_for_lambdef(struct compiling *c, const node *n)
841{
842 /* lambdef: 'lambda' [varargslist] ':' test */
843 arguments_ty args;
844 expr_ty expression;
845
846 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000847 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 if (!args)
849 return NULL;
850 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000851 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 }
854 else {
855 args = ast_for_arguments(c, CHILD(n, 1));
856 if (!args)
857 return NULL;
858 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000859 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 }
862
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000863 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864}
865
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000866static expr_ty
867ast_for_ifexpr(struct compiling *c, const node *n)
868{
869 /* test: or_test 'if' or_test 'else' test */
870 expr_ty expression, body, orelse;
871
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000872 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000873 body = ast_for_expr(c, CHILD(n, 0));
874 if (!body)
875 return NULL;
876 expression = ast_for_expr(c, CHILD(n, 2));
877 if (!expression)
878 return NULL;
879 orelse = ast_for_expr(c, CHILD(n, 4));
880 if (!orelse)
881 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000882 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
883 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000884}
885
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886/* Count the number of 'for' loop in a list comprehension.
887
888 Helper for ast_for_listcomp().
889*/
890
891static int
892count_list_fors(const node *n)
893{
894 int n_fors = 0;
895 node *ch = CHILD(n, 1);
896
897 count_list_for:
898 n_fors++;
899 REQ(ch, list_for);
900 if (NCH(ch) == 5)
901 ch = CHILD(ch, 4);
902 else
903 return n_fors;
904 count_list_iter:
905 REQ(ch, list_iter);
906 ch = CHILD(ch, 0);
907 if (TYPE(ch) == list_for)
908 goto count_list_for;
909 else if (TYPE(ch) == list_if) {
910 if (NCH(ch) == 3) {
911 ch = CHILD(ch, 2);
912 goto count_list_iter;
913 }
914 else
915 return n_fors;
916 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000917
918 /* Should never be reached */
919 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
920 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921}
922
923/* Count the number of 'if' statements in a list comprehension.
924
925 Helper for ast_for_listcomp().
926*/
927
928static int
929count_list_ifs(const node *n)
930{
931 int n_ifs = 0;
932
933 count_list_iter:
934 REQ(n, list_iter);
935 if (TYPE(CHILD(n, 0)) == list_for)
936 return n_ifs;
937 n = CHILD(n, 0);
938 REQ(n, list_if);
939 n_ifs++;
940 if (NCH(n) == 2)
941 return n_ifs;
942 n = CHILD(n, 2);
943 goto count_list_iter;
944}
945
946static expr_ty
947ast_for_listcomp(struct compiling *c, const node *n)
948{
949 /* listmaker: test ( list_for | (',' test)* [','] )
950 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
951 list_iter: list_for | list_if
952 list_if: 'if' test [list_iter]
953 testlist_safe: test [(',' test)+ [',']]
954 */
955 expr_ty elt;
956 asdl_seq *listcomps;
957 int i, n_fors;
958 node *ch;
959
960 REQ(n, listmaker);
961 assert(NCH(n) > 1);
962
963 elt = ast_for_expr(c, CHILD(n, 0));
964 if (!elt)
965 return NULL;
966
967 n_fors = count_list_fors(n);
968 if (n_fors == -1)
969 return NULL;
970
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000971 listcomps = asdl_seq_new(n_fors, c->c_arena);
972 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000974
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 ch = CHILD(n, 1);
976 for (i = 0; i < n_fors; i++) {
977 comprehension_ty lc;
978 asdl_seq *t;
979 expr_ty expression;
980
981 REQ(ch, list_for);
982
983 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000984 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000986 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000987 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000990 if (asdl_seq_LEN(t) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +0000991 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000992 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000994 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
995 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000996 expression, NULL, c->c_arena);
997 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999
1000 if (NCH(ch) == 5) {
1001 int j, n_ifs;
1002 asdl_seq *ifs;
1003
1004 ch = CHILD(ch, 4);
1005 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001006 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001009 ifs = asdl_seq_new(n_ifs, c->c_arena);
1010 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012
1013 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001014 REQ(ch, list_iter);
1015 ch = CHILD(ch, 0);
1016 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017
Jeremy Hyltona8293132006-02-28 17:58:27 +00001018 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1019 if (NCH(ch) == 3)
1020 ch = CHILD(ch, 2);
1021 }
1022 /* on exit, must guarantee that ch is a list_for */
1023 if (TYPE(ch) == list_iter)
1024 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001026 }
1027 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 }
1029
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001030 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031}
1032
1033/*
1034 Count the number of 'for' loops in a generator expression.
1035
1036 Helper for ast_for_genexp().
1037*/
1038
1039static int
1040count_gen_fors(const node *n)
1041{
1042 int n_fors = 0;
1043 node *ch = CHILD(n, 1);
1044
1045 count_gen_for:
1046 n_fors++;
1047 REQ(ch, gen_for);
1048 if (NCH(ch) == 5)
1049 ch = CHILD(ch, 4);
1050 else
1051 return n_fors;
1052 count_gen_iter:
1053 REQ(ch, gen_iter);
1054 ch = CHILD(ch, 0);
1055 if (TYPE(ch) == gen_for)
1056 goto count_gen_for;
1057 else if (TYPE(ch) == gen_if) {
1058 if (NCH(ch) == 3) {
1059 ch = CHILD(ch, 2);
1060 goto count_gen_iter;
1061 }
1062 else
1063 return n_fors;
1064 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001065
1066 /* Should never be reached */
1067 PyErr_SetString(PyExc_SystemError,
1068 "logic error in count_gen_fors");
1069 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070}
1071
1072/* Count the number of 'if' statements in a generator expression.
1073
1074 Helper for ast_for_genexp().
1075*/
1076
1077static int
1078count_gen_ifs(const node *n)
1079{
1080 int n_ifs = 0;
1081
1082 while (1) {
1083 REQ(n, gen_iter);
1084 if (TYPE(CHILD(n, 0)) == gen_for)
1085 return n_ifs;
1086 n = CHILD(n, 0);
1087 REQ(n, gen_if);
1088 n_ifs++;
1089 if (NCH(n) == 2)
1090 return n_ifs;
1091 n = CHILD(n, 2);
1092 }
1093}
1094
Jeremy Hyltona8293132006-02-28 17:58:27 +00001095/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096static expr_ty
1097ast_for_genexp(struct compiling *c, const node *n)
1098{
1099 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1100 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1101 expr_ty elt;
1102 asdl_seq *genexps;
1103 int i, n_fors;
1104 node *ch;
1105
1106 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1107 assert(NCH(n) > 1);
1108
1109 elt = ast_for_expr(c, CHILD(n, 0));
1110 if (!elt)
1111 return NULL;
1112
1113 n_fors = count_gen_fors(n);
1114 if (n_fors == -1)
1115 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001116
1117 genexps = asdl_seq_new(n_fors, c->c_arena);
1118 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 ch = CHILD(n, 1);
1122 for (i = 0; i < n_fors; i++) {
1123 comprehension_ty ge;
1124 asdl_seq *t;
1125 expr_ty expression;
1126
1127 REQ(ch, gen_for);
1128
1129 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001130 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001132 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001133 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001135
1136 if (asdl_seq_LEN(t) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001137 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001138 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001140 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1141 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001142 expression, NULL, c->c_arena);
1143
1144 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 if (NCH(ch) == 5) {
1148 int j, n_ifs;
1149 asdl_seq *ifs;
1150
1151 ch = CHILD(ch, 4);
1152 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001153 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001155
1156 ifs = asdl_seq_new(n_ifs, c->c_arena);
1157 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 for (j = 0; j < n_ifs; j++) {
1161 REQ(ch, gen_iter);
1162 ch = CHILD(ch, 0);
1163 REQ(ch, gen_if);
1164
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001165 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001166 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001167 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001168 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 if (NCH(ch) == 3)
1170 ch = CHILD(ch, 2);
1171 }
1172 /* on exit, must guarantee that ch is a gen_for */
1173 if (TYPE(ch) == gen_iter)
1174 ch = CHILD(ch, 0);
1175 ge->ifs = ifs;
1176 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001177 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 }
1179
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001180 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181}
1182
1183static expr_ty
1184ast_for_atom(struct compiling *c, const node *n)
1185{
1186 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1187 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1188 */
1189 node *ch = CHILD(n, 0);
1190
1191 switch (TYPE(ch)) {
1192 case NAME:
1193 /* All names start in Load context, but may later be
1194 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001195 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 case STRING: {
1197 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 if (!str)
1199 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200
1201 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001202 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 }
1204 case NUMBER: {
1205 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 if (!pynum)
1207 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001208
1209 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001210 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 }
1212 case LPAR: /* some parenthesized expressions */
1213 ch = CHILD(n, 1);
1214
1215 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001216 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217
1218 if (TYPE(ch) == yield_expr)
1219 return ast_for_expr(c, ch);
1220
1221 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1222 return ast_for_genexp(c, ch);
1223
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001224 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 case LSQB: /* list (or list comprehension) */
1226 ch = CHILD(n, 1);
1227
1228 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001229 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230
1231 REQ(ch, listmaker);
1232 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1233 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 if (!elts)
1235 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001236
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001237 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 }
1239 else
1240 return ast_for_listcomp(c, ch);
1241 case LBRACE: {
1242 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1243 int i, size;
1244 asdl_seq *keys, *values;
1245
1246 ch = CHILD(n, 1);
1247 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001248 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 if (!keys)
1250 return NULL;
1251
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252 values = asdl_seq_new(size, c->c_arena);
1253 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255
1256 for (i = 0; i < NCH(ch); i += 4) {
1257 expr_ty expression;
1258
1259 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001260 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001262
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001264
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001266 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001268
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 asdl_seq_SET(values, i / 4, expression);
1270 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001271 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 }
1273 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001274 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 if (!expression)
1276 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001277
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001278 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 }
1280 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001281 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 return NULL;
1283 }
1284}
1285
1286static slice_ty
1287ast_for_slice(struct compiling *c, const node *n)
1288{
1289 node *ch;
1290 expr_ty lower = NULL, upper = NULL, step = NULL;
1291
1292 REQ(n, subscript);
1293
1294 /*
1295 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1296 sliceop: ':' [test]
1297 */
1298 ch = CHILD(n, 0);
1299 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001300 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301
1302 if (NCH(n) == 1 && TYPE(ch) == test) {
1303 /* 'step' variable hold no significance in terms of being used over
1304 other vars */
1305 step = ast_for_expr(c, ch);
1306 if (!step)
1307 return NULL;
1308
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001309 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 }
1311
1312 if (TYPE(ch) == test) {
1313 lower = ast_for_expr(c, ch);
1314 if (!lower)
1315 return NULL;
1316 }
1317
1318 /* If there's an upper bound it's in the second or third position. */
1319 if (TYPE(ch) == COLON) {
1320 if (NCH(n) > 1) {
1321 node *n2 = CHILD(n, 1);
1322
1323 if (TYPE(n2) == test) {
1324 upper = ast_for_expr(c, n2);
1325 if (!upper)
1326 return NULL;
1327 }
1328 }
1329 } else if (NCH(n) > 2) {
1330 node *n2 = CHILD(n, 2);
1331
1332 if (TYPE(n2) == test) {
1333 upper = ast_for_expr(c, n2);
1334 if (!upper)
1335 return NULL;
1336 }
1337 }
1338
1339 ch = CHILD(n, NCH(n) - 1);
1340 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001341 if (NCH(ch) == 1) {
1342 /* No expression, so step is None */
1343 ch = CHILD(ch, 0);
1344 step = Name(new_identifier("None", c->c_arena), Load,
1345 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 if (!step)
1347 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001348 } else {
1349 ch = CHILD(ch, 1);
1350 if (TYPE(ch) == test) {
1351 step = ast_for_expr(c, ch);
1352 if (!step)
1353 return NULL;
1354 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 }
1356 }
1357
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001358 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359}
1360
1361static expr_ty
1362ast_for_binop(struct compiling *c, const node *n)
1363{
1364 /* Must account for a sequence of expressions.
1365 How should A op B op C by represented?
1366 BinOp(BinOp(A, op, B), op, C).
1367 */
1368
1369 int i, nops;
1370 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001371 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372
1373 expr1 = ast_for_expr(c, CHILD(n, 0));
1374 if (!expr1)
1375 return NULL;
1376
1377 expr2 = ast_for_expr(c, CHILD(n, 2));
1378 if (!expr2)
1379 return NULL;
1380
Anthony Baxtera863d332006-04-11 07:43:46 +00001381 newoperator = get_operator(CHILD(n, 1));
1382 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 return NULL;
1384
Anthony Baxtera863d332006-04-11 07:43:46 +00001385 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001386 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 if (!result)
1388 return NULL;
1389
1390 nops = (NCH(n) - 1) / 2;
1391 for (i = 1; i < nops; i++) {
1392 expr_ty tmp_result, tmp;
1393 const node* next_oper = CHILD(n, i * 2 + 1);
1394
Anthony Baxtera863d332006-04-11 07:43:46 +00001395 newoperator = get_operator(next_oper);
1396 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 return NULL;
1398
1399 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1400 if (!tmp)
1401 return NULL;
1402
Anthony Baxtera863d332006-04-11 07:43:46 +00001403 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001404 LINENO(next_oper), next_oper->n_col_offset,
1405 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 if (!tmp)
1407 return NULL;
1408 result = tmp_result;
1409 }
1410 return result;
1411}
1412
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001413static expr_ty
1414ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1415{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001416 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1417 subscriptlist: subscript (',' subscript)* [',']
1418 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1419 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001420 REQ(n, trailer);
1421 if (TYPE(CHILD(n, 0)) == LPAR) {
1422 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001423 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1424 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001425 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001426 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001427 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001428 else if (TYPE(CHILD(n, 0)) == DOT ) {
1429 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001430 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001431 }
1432 else {
1433 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001434 REQ(CHILD(n, 2), RSQB);
1435 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001436 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001437 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1438 if (!slc)
1439 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001440 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1441 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001442 }
1443 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001444 /* The grammar is ambiguous here. The ambiguity is resolved
1445 by treating the sequence as a tuple literal if there are
1446 no slice features.
1447 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001448 int j;
1449 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001450 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001451 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001452 asdl_seq *slices, *elts;
1453 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001454 if (!slices)
1455 return NULL;
1456 for (j = 0; j < NCH(n); j += 2) {
1457 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001458 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001459 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001460 if (slc->kind != Index_kind)
1461 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001462 asdl_seq_SET(slices, j / 2, slc);
1463 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001464 if (!simple) {
1465 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001466 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001467 }
1468 /* extract Index values and put them in a Tuple */
1469 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001470 if (!elts)
1471 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001472 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1473 slc = (slice_ty)asdl_seq_GET(slices, j);
1474 assert(slc->kind == Index_kind && slc->v.Index.value);
1475 asdl_seq_SET(elts, j, slc->v.Index.value);
1476 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001477 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001478 if (!e)
1479 return NULL;
1480 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001481 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001482 }
1483 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001484}
1485
1486static expr_ty
1487ast_for_power(struct compiling *c, const node *n)
1488{
1489 /* power: atom trailer* ('**' factor)*
1490 */
1491 int i;
1492 expr_ty e, tmp;
1493 REQ(n, power);
1494 e = ast_for_atom(c, CHILD(n, 0));
1495 if (!e)
1496 return NULL;
1497 if (NCH(n) == 1)
1498 return e;
1499 for (i = 1; i < NCH(n); i++) {
1500 node *ch = CHILD(n, i);
1501 if (TYPE(ch) != trailer)
1502 break;
1503 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001504 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001505 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001506 tmp->lineno = e->lineno;
1507 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001508 e = tmp;
1509 }
1510 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1511 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001512 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001513 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001514 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001515 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001516 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001517 e = tmp;
1518 }
1519 return e;
1520}
1521
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522/* Do not name a variable 'expr'! Will cause a compile error.
1523*/
1524
1525static expr_ty
1526ast_for_expr(struct compiling *c, const node *n)
1527{
1528 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001529 test: or_test ['if' or_test 'else' test] | lambdef
1530 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 and_test: not_test ('and' not_test)*
1532 not_test: 'not' not_test | comparison
1533 comparison: expr (comp_op expr)*
1534 expr: xor_expr ('|' xor_expr)*
1535 xor_expr: and_expr ('^' and_expr)*
1536 and_expr: shift_expr ('&' shift_expr)*
1537 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1538 arith_expr: term (('+'|'-') term)*
1539 term: factor (('*'|'/'|'%'|'//') factor)*
1540 factor: ('+'|'-'|'~') factor | power
1541 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001542
1543 As well as modified versions that exist for backward compatibility,
1544 to explicitly allow:
1545 [ x for x in lambda: 0, lambda: 1 ]
1546 (which would be ambiguous without these extra rules)
1547
1548 old_test: or_test | old_lambdef
1549 old_lambdef: 'lambda' [vararglist] ':' old_test
1550
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 */
1552
1553 asdl_seq *seq;
1554 int i;
1555
1556 loop:
1557 switch (TYPE(n)) {
1558 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001559 case old_test:
1560 if (TYPE(CHILD(n, 0)) == lambdef ||
1561 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001563 else if (NCH(n) > 1)
1564 return ast_for_ifexpr(c, n);
1565 /* Fallthrough */
1566 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 case and_test:
1568 if (NCH(n) == 1) {
1569 n = CHILD(n, 0);
1570 goto loop;
1571 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001572 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573 if (!seq)
1574 return NULL;
1575 for (i = 0; i < NCH(n); i += 2) {
1576 expr_ty e = ast_for_expr(c, CHILD(n, i));
1577 if (!e)
1578 return NULL;
1579 asdl_seq_SET(seq, i / 2, e);
1580 }
1581 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001582 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1583 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001584 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001585 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 case not_test:
1587 if (NCH(n) == 1) {
1588 n = CHILD(n, 0);
1589 goto loop;
1590 }
1591 else {
1592 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1593 if (!expression)
1594 return NULL;
1595
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001596 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1597 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 }
1599 case comparison:
1600 if (NCH(n) == 1) {
1601 n = CHILD(n, 0);
1602 goto loop;
1603 }
1604 else {
1605 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001606 asdl_int_seq *ops;
1607 asdl_seq *cmps;
1608 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 if (!ops)
1610 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001611 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613 return NULL;
1614 }
1615 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001616 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617
Anthony Baxtera863d332006-04-11 07:43:46 +00001618 newoperator = ast_for_comp_op(CHILD(n, i));
1619 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622
1623 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001624 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001626 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001628 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 asdl_seq_SET(cmps, i / 2, expression);
1630 }
1631 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001632 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001636 return Compare(expression, ops, cmps, LINENO(n),
1637 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638 }
1639 break;
1640
1641 /* The next five cases all handle BinOps. The main body of code
1642 is the same in each case, but the switch turned inside out to
1643 reuse the code for each type of operator.
1644 */
1645 case expr:
1646 case xor_expr:
1647 case and_expr:
1648 case shift_expr:
1649 case arith_expr:
1650 case term:
1651 if (NCH(n) == 1) {
1652 n = CHILD(n, 0);
1653 goto loop;
1654 }
1655 return ast_for_binop(c, n);
1656 case yield_expr: {
1657 expr_ty exp = NULL;
1658 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001659 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 if (!exp)
1661 return NULL;
1662 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001663 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664 }
1665 case factor: {
1666 expr_ty expression;
1667
1668 if (NCH(n) == 1) {
1669 n = CHILD(n, 0);
1670 goto loop;
1671 }
1672
1673 expression = ast_for_expr(c, CHILD(n, 1));
1674 if (!expression)
1675 return NULL;
1676
1677 switch (TYPE(CHILD(n, 0))) {
1678 case PLUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001679 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 case MINUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001681 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 case TILDE:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001683 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001685 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1686 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 break;
1688 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001689 case power:
1690 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001692 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 return NULL;
1694 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001695 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 return NULL;
1697}
1698
1699static expr_ty
1700ast_for_call(struct compiling *c, const node *n, expr_ty func)
1701{
1702 /*
1703 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1704 | '**' test)
1705 argument: [test '='] test [gen_for] # Really [keyword '='] test
1706 */
1707
1708 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001709 asdl_seq *args;
1710 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 expr_ty vararg = NULL, kwarg = NULL;
1712
1713 REQ(n, arglist);
1714
1715 nargs = 0;
1716 nkeywords = 0;
1717 ngens = 0;
1718 for (i = 0; i < NCH(n); i++) {
1719 node *ch = CHILD(n, i);
1720 if (TYPE(ch) == argument) {
1721 if (NCH(ch) == 1)
1722 nargs++;
1723 else if (TYPE(CHILD(ch, 1)) == gen_for)
1724 ngens++;
1725 else
1726 nkeywords++;
1727 }
1728 }
1729 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001730 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 "if not sole argument");
1732 return NULL;
1733 }
1734
1735 if (nargs + nkeywords + ngens > 255) {
1736 ast_error(n, "more than 255 arguments");
1737 return NULL;
1738 }
1739
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001740 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001742 return NULL;
1743 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001745 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 nargs = 0;
1747 nkeywords = 0;
1748 for (i = 0; i < NCH(n); i++) {
1749 node *ch = CHILD(n, i);
1750 if (TYPE(ch) == argument) {
1751 expr_ty e;
1752 if (NCH(ch) == 1) {
Neal Norwitz5ef92242006-05-19 06:43:50 +00001753 if (nkeywords) {
1754 ast_error(CHILD(ch, 0),
1755 "non-keyword arg after keyword arg");
1756 return NULL;
1757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758 e = ast_for_expr(c, CHILD(ch, 0));
1759 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001760 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 asdl_seq_SET(args, nargs++, e);
1762 }
1763 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1764 e = ast_for_genexp(c, ch);
1765 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001766 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 asdl_seq_SET(args, nargs++, e);
1768 }
1769 else {
1770 keyword_ty kw;
1771 identifier key;
1772
1773 /* CHILD(ch, 0) is test, but must be an identifier? */
1774 e = ast_for_expr(c, CHILD(ch, 0));
1775 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001776 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 /* f(lambda x: x[0] = 3) ends up getting parsed with
1778 * LHS test = lambda x: x[0], and RHS test = 3.
1779 * SF bug 132313 points out that complaining about a keyword
1780 * then is very confusing.
1781 */
1782 if (e->kind == Lambda_kind) {
1783 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 } else if (e->kind != Name_kind) {
1786 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001787 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788 }
1789 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 e = ast_for_expr(c, CHILD(ch, 2));
1791 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001792 return NULL;
1793 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001795 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 asdl_seq_SET(keywords, nkeywords++, kw);
1797 }
1798 }
1799 else if (TYPE(ch) == STAR) {
1800 vararg = ast_for_expr(c, CHILD(n, i+1));
1801 i++;
1802 }
1803 else if (TYPE(ch) == DOUBLESTAR) {
1804 kwarg = ast_for_expr(c, CHILD(n, i+1));
1805 i++;
1806 }
1807 }
1808
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001809 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810}
1811
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001813ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001815 /* testlist_gexp: test (',' test)* [','] */
1816 /* testlist: test (',' test)* [','] */
1817 /* testlist_safe: test (',' test)+ [','] */
1818 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001820 if (TYPE(n) == testlist_gexp) {
1821 if (NCH(n) > 1)
1822 assert(TYPE(CHILD(n, 1)) != gen_for);
1823 }
1824 else {
1825 assert(TYPE(n) == testlist ||
1826 TYPE(n) == testlist_safe ||
1827 TYPE(n) == testlist1);
1828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 if (NCH(n) == 1)
1830 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 else {
1832 asdl_seq *tmp = seq_for_testlist(c, n);
1833 if (!tmp)
1834 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001835 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001837}
1838
1839static expr_ty
1840ast_for_testlist_gexp(struct compiling *c, const node* n)
1841{
1842 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1843 /* argument: test [ gen_for ] */
1844 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001845 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001846 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001847 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001848}
1849
1850/* like ast_for_testlist() but returns a sequence */
1851static asdl_seq*
1852ast_for_class_bases(struct compiling *c, const node* n)
1853{
1854 /* testlist: test (',' test)* [','] */
1855 assert(NCH(n) > 0);
1856 REQ(n, testlist);
1857 if (NCH(n) == 1) {
1858 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001859 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001860 if (!bases)
1861 return NULL;
1862 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001863 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001864 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001865 asdl_seq_SET(bases, 0, base);
1866 return bases;
1867 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001868
1869 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870}
1871
1872static stmt_ty
1873ast_for_expr_stmt(struct compiling *c, const node *n)
1874{
1875 REQ(n, expr_stmt);
1876 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1877 | ('=' (yield_expr|testlist))*)
1878 testlist: test (',' test)* [',']
1879 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1880 | '<<=' | '>>=' | '**=' | '//='
1881 test: ... here starts the operator precendence dance
1882 */
1883
1884 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001885 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 if (!e)
1887 return NULL;
1888
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001889 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 }
1891 else if (TYPE(CHILD(n, 1)) == augassign) {
1892 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00001893 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 node *ch = CHILD(n, 0);
1895
1896 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001897 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001899 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001900 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901
1902 if (!expr1)
1903 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001904 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001905 switch (expr1->kind) {
1906 case GeneratorExp_kind:
1907 ast_error(ch, "augmented assignment to generator "
1908 "expression not possible");
1909 return NULL;
1910 case Name_kind: {
1911 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1912 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1913 ast_error(ch, "assignment to None");
1914 return NULL;
1915 }
1916 break;
1917 }
1918 case Attribute_kind:
1919 case Subscript_kind:
1920 break;
1921 default:
1922 ast_error(ch, "illegal expression for augmented "
1923 "assignment");
1924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 }
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001926 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927
1928 ch = CHILD(n, 2);
1929 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001930 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001932 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), ch->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001933 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 return NULL;
1935
Anthony Baxtera863d332006-04-11 07:43:46 +00001936 newoperator = ast_for_augassign(CHILD(n, 1));
1937 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938 return NULL;
1939
Anthony Baxtera863d332006-04-11 07:43:46 +00001940 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941 }
1942 else {
1943 int i;
1944 asdl_seq *targets;
1945 node *value;
1946 expr_ty expression;
1947
1948 /* a normal assignment */
1949 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001950 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 if (!targets)
1952 return NULL;
1953 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001954 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955 node *ch = CHILD(n, i);
1956 if (TYPE(ch) == yield_expr) {
1957 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001958 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001960 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961
1962 /* set context to assign */
1963 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001964 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965
Neal Norwitz84456bd2005-12-18 03:16:20 +00001966 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001967 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968
1969 asdl_seq_SET(targets, i / 2, e);
1970 }
1971 value = CHILD(n, NCH(n) - 1);
1972 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001973 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 else
1975 expression = ast_for_expr(c, value);
1976 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001977 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001978 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980}
1981
1982static stmt_ty
1983ast_for_print_stmt(struct compiling *c, const node *n)
1984{
1985 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1986 | '>>' test [ (',' test)+ [','] ] )
1987 */
1988 expr_ty dest = NULL, expression;
1989 asdl_seq *seq;
1990 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001991 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992
1993 REQ(n, print_stmt);
1994 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1995 dest = ast_for_expr(c, CHILD(n, 2));
1996 if (!dest)
1997 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001998 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002000 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002002 return NULL;
2003 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002005 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002007 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 }
2009 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002010 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011}
2012
2013static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002014ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015{
2016 asdl_seq *seq;
2017 int i;
2018 expr_ty e;
2019
2020 REQ(n, exprlist);
2021
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002022 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 if (!seq)
2024 return NULL;
2025 for (i = 0; i < NCH(n); i += 2) {
2026 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002027 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002028 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002029 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002030 if (context && !set_context(e, context, CHILD(n, i)))
2031 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 }
2033 return seq;
2034}
2035
2036static stmt_ty
2037ast_for_del_stmt(struct compiling *c, const node *n)
2038{
2039 asdl_seq *expr_list;
2040
2041 /* del_stmt: 'del' exprlist */
2042 REQ(n, del_stmt);
2043
2044 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2045 if (!expr_list)
2046 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002047 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048}
2049
2050static stmt_ty
2051ast_for_flow_stmt(struct compiling *c, const node *n)
2052{
2053 /*
2054 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2055 | yield_stmt
2056 break_stmt: 'break'
2057 continue_stmt: 'continue'
2058 return_stmt: 'return' [testlist]
2059 yield_stmt: yield_expr
2060 yield_expr: 'yield' testlist
2061 raise_stmt: 'raise' [test [',' test [',' test]]]
2062 */
2063 node *ch;
2064
2065 REQ(n, flow_stmt);
2066 ch = CHILD(n, 0);
2067 switch (TYPE(ch)) {
2068 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002069 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002071 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 case yield_stmt: { /* will reduce to yield_expr */
2073 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2074 if (!exp)
2075 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002076 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 }
2078 case return_stmt:
2079 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002080 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002082 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 if (!expression)
2084 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002085 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 }
2087 case raise_stmt:
2088 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002089 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 else if (NCH(ch) == 2) {
2091 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2092 if (!expression)
2093 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002094 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 }
2096 else if (NCH(ch) == 4) {
2097 expr_ty expr1, expr2;
2098
2099 expr1 = ast_for_expr(c, CHILD(ch, 1));
2100 if (!expr1)
2101 return NULL;
2102 expr2 = ast_for_expr(c, CHILD(ch, 3));
2103 if (!expr2)
2104 return NULL;
2105
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002106 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 }
2108 else if (NCH(ch) == 6) {
2109 expr_ty expr1, expr2, expr3;
2110
2111 expr1 = ast_for_expr(c, CHILD(ch, 1));
2112 if (!expr1)
2113 return NULL;
2114 expr2 = ast_for_expr(c, CHILD(ch, 3));
2115 if (!expr2)
2116 return NULL;
2117 expr3 = ast_for_expr(c, CHILD(ch, 5));
2118 if (!expr3)
2119 return NULL;
2120
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002121 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 }
2123 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002124 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 "unexpected flow_stmt: %d", TYPE(ch));
2126 return NULL;
2127 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002128
2129 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131}
2132
2133static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002134alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135{
2136 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002137 import_as_name: NAME ['as' NAME]
2138 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 dotted_name: NAME ('.' NAME)*
2140 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002141 PyObject *str;
2142
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 loop:
2144 switch (TYPE(n)) {
2145 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002146 str = NULL;
2147 if (NCH(n) == 3) {
2148 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2149 ast_error(n, "must use 'as' in import");
2150 return NULL;
2151 }
2152 str = NEW_IDENTIFIER(CHILD(n, 2));
2153 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002154 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 case dotted_as_name:
2156 if (NCH(n) == 1) {
2157 n = CHILD(n, 0);
2158 goto loop;
2159 }
2160 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002161 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002162 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2163 ast_error(n, "must use 'as' in import");
2164 return NULL;
2165 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 assert(!a->asname);
2167 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2168 return a;
2169 }
2170 break;
2171 case dotted_name:
2172 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002173 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 else {
2175 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002176 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002177 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 char *s;
2179
2180 len = 0;
2181 for (i = 0; i < NCH(n); i += 2)
2182 /* length of string plus one for the dot */
2183 len += strlen(STR(CHILD(n, i))) + 1;
2184 len--; /* the last name doesn't have a dot */
2185 str = PyString_FromStringAndSize(NULL, len);
2186 if (!str)
2187 return NULL;
2188 s = PyString_AS_STRING(str);
2189 if (!s)
2190 return NULL;
2191 for (i = 0; i < NCH(n); i += 2) {
2192 char *sch = STR(CHILD(n, i));
2193 strcpy(s, STR(CHILD(n, i)));
2194 s += strlen(sch);
2195 *s++ = '.';
2196 }
2197 --s;
2198 *s = '\0';
2199 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002200 PyArena_AddPyObject(c->c_arena, str);
2201 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 }
2203 break;
2204 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002205 str = PyString_InternFromString("*");
2206 PyArena_AddPyObject(c->c_arena, str);
2207 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002209 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 "unexpected import name: %d", TYPE(n));
2211 return NULL;
2212 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002213
2214 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215 return NULL;
2216}
2217
2218static stmt_ty
2219ast_for_import_stmt(struct compiling *c, const node *n)
2220{
2221 /*
2222 import_stmt: import_name | import_from
2223 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002224 import_from: 'from' ('.'* dotted_name | '.') 'import'
2225 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002227 int lineno;
2228 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 int i;
2230 asdl_seq *aliases;
2231
2232 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002233 lineno = LINENO(n);
2234 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002236 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002238 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002239 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 if (!aliases)
2241 return NULL;
2242 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002243 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002244 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 asdl_seq_SET(aliases, i / 2, import_alias);
2247 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002248 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002250 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002252 int idx, ndots = 0;
2253 alias_ty mod = NULL;
2254 identifier modname;
2255
2256 /* Count the number of dots (for relative imports) and check for the
2257 optional module name */
2258 for (idx = 1; idx < NCH(n); idx++) {
2259 if (TYPE(CHILD(n, idx)) == dotted_name) {
2260 mod = alias_for_import_name(c, CHILD(n, idx));
2261 idx++;
2262 break;
2263 } else if (TYPE(CHILD(n, idx)) != DOT) {
2264 break;
2265 }
2266 ndots++;
2267 }
2268 idx++; /* skip over the 'import' keyword */
2269 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002270 case STAR:
2271 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002272 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002273 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002274 if (ndots) {
2275 ast_error(n, "'import *' not allowed with 'from .'");
2276 return NULL;
2277 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002278 break;
2279 case LPAR:
2280 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002281 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002282 n_children = NCH(n);
2283 break;
2284 case import_as_names:
2285 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002286 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002287 n_children = NCH(n);
2288 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 ast_error(n, "trailing comma not allowed without"
2290 " surrounding parentheses");
2291 return NULL;
2292 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002293 break;
2294 default:
2295 ast_error(n, "Unexpected node-type in from-import");
2296 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002297 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002299 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002300 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302
2303 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002304 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002305 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002306 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002308 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002310 else {
2311 for (i = 0; i < NCH(n); i += 2) {
2312 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2313 if (!import_alias)
2314 return NULL;
2315 asdl_seq_SET(aliases, i / 2, import_alias);
2316 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002318 if (mod != NULL)
2319 modname = mod->name;
2320 else
2321 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002322 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002323 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 }
Neal Norwitz79792652005-11-14 04:25:03 +00002325 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326 "unknown import statement: starts with command '%s'",
2327 STR(CHILD(n, 0)));
2328 return NULL;
2329}
2330
2331static stmt_ty
2332ast_for_global_stmt(struct compiling *c, const node *n)
2333{
2334 /* global_stmt: 'global' NAME (',' NAME)* */
2335 identifier name;
2336 asdl_seq *s;
2337 int i;
2338
2339 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002340 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 if (!s)
2342 return NULL;
2343 for (i = 1; i < NCH(n); i += 2) {
2344 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002345 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 asdl_seq_SET(s, i / 2, name);
2348 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002349 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350}
2351
2352static stmt_ty
2353ast_for_exec_stmt(struct compiling *c, const node *n)
2354{
2355 expr_ty expr1, globals = NULL, locals = NULL;
2356 int n_children = NCH(n);
2357 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002358 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 "poorly formed 'exec' statement: %d parts to statement",
2360 n_children);
2361 return NULL;
2362 }
2363
2364 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2365 REQ(n, exec_stmt);
2366 expr1 = ast_for_expr(c, CHILD(n, 1));
2367 if (!expr1)
2368 return NULL;
2369 if (n_children >= 4) {
2370 globals = ast_for_expr(c, CHILD(n, 3));
2371 if (!globals)
2372 return NULL;
2373 }
2374 if (n_children == 6) {
2375 locals = ast_for_expr(c, CHILD(n, 5));
2376 if (!locals)
2377 return NULL;
2378 }
2379
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002380 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381}
2382
2383static stmt_ty
2384ast_for_assert_stmt(struct compiling *c, const node *n)
2385{
2386 /* assert_stmt: 'assert' test [',' test] */
2387 REQ(n, assert_stmt);
2388 if (NCH(n) == 2) {
2389 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2390 if (!expression)
2391 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002392 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 }
2394 else if (NCH(n) == 4) {
2395 expr_ty expr1, expr2;
2396
2397 expr1 = ast_for_expr(c, CHILD(n, 1));
2398 if (!expr1)
2399 return NULL;
2400 expr2 = ast_for_expr(c, CHILD(n, 3));
2401 if (!expr2)
2402 return NULL;
2403
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002404 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 }
Neal Norwitz79792652005-11-14 04:25:03 +00002406 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 "improper number of parts to 'assert' statement: %d",
2408 NCH(n));
2409 return NULL;
2410}
2411
2412static asdl_seq *
2413ast_for_suite(struct compiling *c, const node *n)
2414{
2415 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002416 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002417 stmt_ty s;
2418 int i, total, num, end, pos = 0;
2419 node *ch;
2420
2421 REQ(n, suite);
2422
2423 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002424 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425 if (!seq)
2426 return NULL;
2427 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2428 n = CHILD(n, 0);
2429 /* simple_stmt always ends with a NEWLINE,
2430 and may have a trailing SEMI
2431 */
2432 end = NCH(n) - 1;
2433 if (TYPE(CHILD(n, end - 1)) == SEMI)
2434 end--;
2435 /* loop by 2 to skip semi-colons */
2436 for (i = 0; i < end; i += 2) {
2437 ch = CHILD(n, i);
2438 s = ast_for_stmt(c, ch);
2439 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441 asdl_seq_SET(seq, pos++, s);
2442 }
2443 }
2444 else {
2445 for (i = 2; i < (NCH(n) - 1); i++) {
2446 ch = CHILD(n, i);
2447 REQ(ch, stmt);
2448 num = num_stmts(ch);
2449 if (num == 1) {
2450 /* small_stmt or compound_stmt with only one child */
2451 s = ast_for_stmt(c, ch);
2452 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002453 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 asdl_seq_SET(seq, pos++, s);
2455 }
2456 else {
2457 int j;
2458 ch = CHILD(ch, 0);
2459 REQ(ch, simple_stmt);
2460 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002461 /* statement terminates with a semi-colon ';' */
2462 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002463 assert((j + 1) == NCH(ch));
2464 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 s = ast_for_stmt(c, CHILD(ch, j));
2467 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002468 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 asdl_seq_SET(seq, pos++, s);
2470 }
2471 }
2472 }
2473 }
2474 assert(pos == seq->size);
2475 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476}
2477
2478static stmt_ty
2479ast_for_if_stmt(struct compiling *c, const node *n)
2480{
2481 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2482 ['else' ':' suite]
2483 */
2484 char *s;
2485
2486 REQ(n, if_stmt);
2487
2488 if (NCH(n) == 4) {
2489 expr_ty expression;
2490 asdl_seq *suite_seq;
2491
2492 expression = ast_for_expr(c, CHILD(n, 1));
2493 if (!expression)
2494 return NULL;
2495 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002496 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 return NULL;
2498
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002499 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502 s = STR(CHILD(n, 4));
2503 /* s[2], the third character in the string, will be
2504 's' for el_s_e, or
2505 'i' for el_i_f
2506 */
2507 if (s[2] == 's') {
2508 expr_ty expression;
2509 asdl_seq *seq1, *seq2;
2510
2511 expression = ast_for_expr(c, CHILD(n, 1));
2512 if (!expression)
2513 return NULL;
2514 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002515 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 return NULL;
2517 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002518 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 return NULL;
2520
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002521 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 }
2523 else if (s[2] == 'i') {
2524 int i, n_elif, has_else = 0;
2525 asdl_seq *orelse = NULL;
2526 n_elif = NCH(n) - 4;
2527 /* must reference the child n_elif+1 since 'else' token is third,
2528 not fourth, child from the end. */
2529 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2530 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2531 has_else = 1;
2532 n_elif -= 3;
2533 }
2534 n_elif /= 4;
2535
2536 if (has_else) {
2537 expr_ty expression;
2538 asdl_seq *seq1, *seq2;
2539
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002540 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 if (!orelse)
2542 return NULL;
2543 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002544 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002547 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002550 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552
2553 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002554 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002555 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 /* the just-created orelse handled the last elif */
2557 n_elif--;
2558 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559
2560 for (i = 0; i < n_elif; i++) {
2561 int off = 5 + (n_elif - i - 1) * 4;
2562 expr_ty expression;
2563 asdl_seq *suite_seq;
Anthony Baxtera863d332006-04-11 07:43:46 +00002564 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2565 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 return NULL;
2567 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002568 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002571 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573
Anthony Baxtera863d332006-04-11 07:43:46 +00002574 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002576 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Anthony Baxtera863d332006-04-11 07:43:46 +00002577 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 }
2579 return If(ast_for_expr(c, CHILD(n, 1)),
2580 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002581 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002583
2584 PyErr_Format(PyExc_SystemError,
2585 "unexpected token in 'if' statement: %s", s);
2586 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587}
2588
2589static stmt_ty
2590ast_for_while_stmt(struct compiling *c, const node *n)
2591{
2592 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2593 REQ(n, while_stmt);
2594
2595 if (NCH(n) == 4) {
2596 expr_ty expression;
2597 asdl_seq *suite_seq;
2598
2599 expression = ast_for_expr(c, CHILD(n, 1));
2600 if (!expression)
2601 return NULL;
2602 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002603 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002605 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 }
2607 else if (NCH(n) == 7) {
2608 expr_ty expression;
2609 asdl_seq *seq1, *seq2;
2610
2611 expression = ast_for_expr(c, CHILD(n, 1));
2612 if (!expression)
2613 return NULL;
2614 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002615 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 return NULL;
2617 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002618 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 return NULL;
2620
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002621 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002623
2624 PyErr_Format(PyExc_SystemError,
2625 "wrong number of tokens for 'while' statement: %d",
2626 NCH(n));
2627 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628}
2629
2630static stmt_ty
2631ast_for_for_stmt(struct compiling *c, const node *n)
2632{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002633 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 expr_ty expression;
2635 expr_ty target;
2636 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2637 REQ(n, for_stmt);
2638
2639 if (NCH(n) == 9) {
2640 seq = ast_for_suite(c, CHILD(n, 8));
2641 if (!seq)
2642 return NULL;
2643 }
2644
2645 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002646 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002648 if (asdl_seq_LEN(_target) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00002649 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002651 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002653 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002654 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 return NULL;
2656 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002657 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 return NULL;
2659
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002660 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2661 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662}
2663
2664static excepthandler_ty
2665ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2666{
2667 /* except_clause: 'except' [test [',' test]] */
2668 REQ(exc, except_clause);
2669 REQ(body, suite);
2670
2671 if (NCH(exc) == 1) {
2672 asdl_seq *suite_seq = ast_for_suite(c, body);
2673 if (!suite_seq)
2674 return NULL;
2675
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002676 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2677 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678 }
2679 else if (NCH(exc) == 2) {
2680 expr_ty expression;
2681 asdl_seq *suite_seq;
2682
2683 expression = ast_for_expr(c, CHILD(exc, 1));
2684 if (!expression)
2685 return NULL;
2686 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002687 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 return NULL;
2689
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002690 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2691 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 }
2693 else if (NCH(exc) == 4) {
2694 asdl_seq *suite_seq;
2695 expr_ty expression;
2696 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2697 if (!e)
2698 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002699 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 return NULL;
2701 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002702 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 return NULL;
2704 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002705 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 return NULL;
2707
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002708 return excepthandler(expression, e, suite_seq, LINENO(exc),
2709 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002711
2712 PyErr_Format(PyExc_SystemError,
2713 "wrong number of children for 'except' clause: %d",
2714 NCH(exc));
2715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716}
2717
2718static stmt_ty
2719ast_for_try_stmt(struct compiling *c, const node *n)
2720{
Neal Norwitzf599f422005-12-17 21:33:47 +00002721 const int nch = NCH(n);
2722 int n_except = (nch - 3)/3;
2723 asdl_seq *body, *orelse = NULL, *finally = NULL;
2724
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 REQ(n, try_stmt);
2726
Neal Norwitzf599f422005-12-17 21:33:47 +00002727 body = ast_for_suite(c, CHILD(n, 2));
2728 if (body == NULL)
2729 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730
Neal Norwitzf599f422005-12-17 21:33:47 +00002731 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2732 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2733 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2734 /* we can assume it's an "else",
2735 because nch >= 9 for try-else-finally and
2736 it would otherwise have a type of except_clause */
2737 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2738 if (orelse == NULL)
2739 return NULL;
2740 n_except--;
2741 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742
Neal Norwitzf599f422005-12-17 21:33:47 +00002743 finally = ast_for_suite(c, CHILD(n, nch - 1));
2744 if (finally == NULL)
2745 return NULL;
2746 n_except--;
2747 }
2748 else {
2749 /* we can assume it's an "else",
2750 otherwise it would have a type of except_clause */
2751 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2752 if (orelse == NULL)
2753 return NULL;
2754 n_except--;
2755 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002757 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002758 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 return NULL;
2760 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002761
2762 if (n_except > 0) {
2763 int i;
2764 stmt_ty except_st;
2765 /* process except statements to create a try ... except */
2766 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2767 if (handlers == NULL)
2768 return NULL;
2769
2770 for (i = 0; i < n_except; i++) {
2771 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2772 CHILD(n, 5 + i * 3));
2773 if (!e)
2774 return NULL;
2775 asdl_seq_SET(handlers, i, e);
2776 }
2777
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002778 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2779 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002780 if (!finally)
2781 return except_st;
2782
2783 /* if a 'finally' is present too, we nest the TryExcept within a
2784 TryFinally to emulate try ... except ... finally */
2785 body = asdl_seq_new(1, c->c_arena);
2786 if (body == NULL)
2787 return NULL;
2788 asdl_seq_SET(body, 0, except_st);
2789 }
2790
2791 /* must be a try ... finally (except clauses are in body, if any exist) */
2792 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002793 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794}
2795
Guido van Rossumc2e20742006-02-27 22:32:47 +00002796static expr_ty
2797ast_for_with_var(struct compiling *c, const node *n)
2798{
2799 REQ(n, with_var);
2800 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2801 ast_error(n, "expected \"with [expr] as [var]\"");
2802 return NULL;
2803 }
2804 return ast_for_expr(c, CHILD(n, 1));
2805}
2806
2807/* with_stmt: 'with' test [ with_var ] ':' suite */
2808static stmt_ty
2809ast_for_with_stmt(struct compiling *c, const node *n)
2810{
2811 expr_ty context_expr, optional_vars = NULL;
2812 int suite_index = 3; /* skip 'with', test, and ':' */
2813 asdl_seq *suite_seq;
2814
2815 assert(TYPE(n) == with_stmt);
2816 context_expr = ast_for_expr(c, CHILD(n, 1));
2817 if (TYPE(CHILD(n, 2)) == with_var) {
2818 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2819
2820 if (!optional_vars) {
2821 return NULL;
2822 }
2823 if (!set_context(optional_vars, Store, n)) {
2824 return NULL;
2825 }
2826 suite_index = 4;
2827 }
2828
2829 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2830 if (!suite_seq) {
2831 return NULL;
2832 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002833 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2834 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002835}
2836
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837static stmt_ty
2838ast_for_classdef(struct compiling *c, const node *n)
2839{
2840 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 asdl_seq *bases, *s;
2842
2843 REQ(n, classdef);
2844
2845 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2846 ast_error(n, "assignment to None");
2847 return NULL;
2848 }
2849
2850 if (NCH(n) == 4) {
2851 s = ast_for_suite(c, CHILD(n, 3));
2852 if (!s)
2853 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002854 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2855 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 }
2857 /* check for empty base list */
2858 if (TYPE(CHILD(n,3)) == RPAR) {
2859 s = ast_for_suite(c, CHILD(n,5));
2860 if (!s)
2861 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002862 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2863 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 }
2865
2866 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002867 bases = ast_for_class_bases(c, CHILD(n, 3));
2868 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870
2871 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002872 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002874 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2875 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876}
2877
2878static stmt_ty
2879ast_for_stmt(struct compiling *c, const node *n)
2880{
2881 if (TYPE(n) == stmt) {
2882 assert(NCH(n) == 1);
2883 n = CHILD(n, 0);
2884 }
2885 if (TYPE(n) == simple_stmt) {
2886 assert(num_stmts(n) == 1);
2887 n = CHILD(n, 0);
2888 }
2889 if (TYPE(n) == small_stmt) {
2890 REQ(n, small_stmt);
2891 n = CHILD(n, 0);
2892 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2893 | flow_stmt | import_stmt | global_stmt | exec_stmt
2894 | assert_stmt
2895 */
2896 switch (TYPE(n)) {
2897 case expr_stmt:
2898 return ast_for_expr_stmt(c, n);
2899 case print_stmt:
2900 return ast_for_print_stmt(c, n);
2901 case del_stmt:
2902 return ast_for_del_stmt(c, n);
2903 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002904 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 case flow_stmt:
2906 return ast_for_flow_stmt(c, n);
2907 case import_stmt:
2908 return ast_for_import_stmt(c, n);
2909 case global_stmt:
2910 return ast_for_global_stmt(c, n);
2911 case exec_stmt:
2912 return ast_for_exec_stmt(c, n);
2913 case assert_stmt:
2914 return ast_for_assert_stmt(c, n);
2915 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002916 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2918 TYPE(n), NCH(n));
2919 return NULL;
2920 }
2921 }
2922 else {
2923 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2924 | funcdef | classdef
2925 */
2926 node *ch = CHILD(n, 0);
2927 REQ(n, compound_stmt);
2928 switch (TYPE(ch)) {
2929 case if_stmt:
2930 return ast_for_if_stmt(c, ch);
2931 case while_stmt:
2932 return ast_for_while_stmt(c, ch);
2933 case for_stmt:
2934 return ast_for_for_stmt(c, ch);
2935 case try_stmt:
2936 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002937 case with_stmt:
2938 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 case funcdef:
2940 return ast_for_funcdef(c, ch);
2941 case classdef:
2942 return ast_for_classdef(c, ch);
2943 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002944 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2946 TYPE(n), NCH(n));
2947 return NULL;
2948 }
2949 }
2950}
2951
2952static PyObject *
2953parsenumber(const char *s)
2954{
2955 const char *end;
2956 long x;
2957 double dx;
2958#ifndef WITHOUT_COMPLEX
2959 Py_complex c;
2960 int imflag;
2961#endif
2962
2963 errno = 0;
2964 end = s + strlen(s) - 1;
2965#ifndef WITHOUT_COMPLEX
2966 imflag = *end == 'j' || *end == 'J';
2967#endif
2968 if (*end == 'l' || *end == 'L')
2969 return PyLong_FromString((char *)s, (char **)0, 0);
2970 if (s[0] == '0') {
2971 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2972 if (x < 0 && errno == 0) {
2973 return PyLong_FromString((char *)s,
2974 (char **)0,
2975 0);
2976 }
2977 }
2978 else
2979 x = PyOS_strtol((char *)s, (char **)&end, 0);
2980 if (*end == '\0') {
2981 if (errno != 0)
2982 return PyLong_FromString((char *)s, (char **)0, 0);
2983 return PyInt_FromLong(x);
2984 }
2985 /* XXX Huge floats may silently fail */
2986#ifndef WITHOUT_COMPLEX
2987 if (imflag) {
2988 c.real = 0.;
2989 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002990 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991 PyFPE_END_PROTECT(c)
2992 return PyComplex_FromCComplex(c);
2993 }
2994 else
2995#endif
2996 {
2997 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002998 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999 PyFPE_END_PROTECT(dx)
3000 return PyFloat_FromDouble(dx);
3001 }
3002}
3003
3004static PyObject *
3005decode_utf8(const char **sPtr, const char *end, char* encoding)
3006{
3007#ifndef Py_USING_UNICODE
3008 Py_FatalError("decode_utf8 should not be called in this build.");
3009 return NULL;
3010#else
3011 PyObject *u, *v;
3012 char *s, *t;
3013 t = s = (char *)*sPtr;
3014 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3015 while (s < end && (*s & 0x80)) s++;
3016 *sPtr = s;
3017 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3018 if (u == NULL)
3019 return NULL;
3020 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3021 Py_DECREF(u);
3022 return v;
3023#endif
3024}
3025
3026static PyObject *
3027decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3028{
3029 PyObject *v, *u;
3030 char *buf;
3031 char *p;
3032 const char *end;
3033 if (encoding == NULL) {
3034 buf = (char *)s;
3035 u = NULL;
3036 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3037 buf = (char *)s;
3038 u = NULL;
3039 } else {
3040 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3041 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3042 if (u == NULL)
3043 return NULL;
3044 p = buf = PyString_AsString(u);
3045 end = s + len;
3046 while (s < end) {
3047 if (*s == '\\') {
3048 *p++ = *s++;
3049 if (*s & 0x80) {
3050 strcpy(p, "u005c");
3051 p += 5;
3052 }
3053 }
3054 if (*s & 0x80) { /* XXX inefficient */
3055 PyObject *w;
3056 char *r;
Martin v. Löwis66851282006-04-22 11:40:03 +00003057 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058 w = decode_utf8(&s, end, "utf-16-be");
3059 if (w == NULL) {
3060 Py_DECREF(u);
3061 return NULL;
3062 }
3063 r = PyString_AsString(w);
3064 rn = PyString_Size(w);
3065 assert(rn % 2 == 0);
3066 for (i = 0; i < rn; i += 2) {
3067 sprintf(p, "\\u%02x%02x",
3068 r[i + 0] & 0xFF,
3069 r[i + 1] & 0xFF);
3070 p += 6;
3071 }
3072 Py_DECREF(w);
3073 } else {
3074 *p++ = *s++;
3075 }
3076 }
3077 len = p - buf;
3078 s = buf;
3079 }
3080 if (rawmode)
3081 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3082 else
3083 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3084 Py_XDECREF(u);
3085 return v;
3086}
3087
3088/* s is a Python string literal, including the bracketing quote characters,
3089 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3090 * parsestr parses it, and returns the decoded Python string object.
3091 */
3092static PyObject *
3093parsestr(const char *s, const char *encoding)
3094{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003096 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097 int rawmode = 0;
3098 int need_encoding;
3099 int unicode = 0;
3100
3101 if (isalpha(quote) || quote == '_') {
3102 if (quote == 'u' || quote == 'U') {
3103 quote = *++s;
3104 unicode = 1;
3105 }
3106 if (quote == 'r' || quote == 'R') {
3107 quote = *++s;
3108 rawmode = 1;
3109 }
3110 }
3111 if (quote != '\'' && quote != '\"') {
3112 PyErr_BadInternalCall();
3113 return NULL;
3114 }
3115 s++;
3116 len = strlen(s);
3117 if (len > INT_MAX) {
3118 PyErr_SetString(PyExc_OverflowError,
3119 "string to parse is too long");
3120 return NULL;
3121 }
3122 if (s[--len] != quote) {
3123 PyErr_BadInternalCall();
3124 return NULL;
3125 }
3126 if (len >= 4 && s[0] == quote && s[1] == quote) {
3127 s += 2;
3128 len -= 2;
3129 if (s[--len] != quote || s[--len] != quote) {
3130 PyErr_BadInternalCall();
3131 return NULL;
3132 }
3133 }
3134#ifdef Py_USING_UNICODE
3135 if (unicode || Py_UnicodeFlag) {
3136 return decode_unicode(s, len, rawmode, encoding);
3137 }
3138#endif
3139 need_encoding = (encoding != NULL &&
3140 strcmp(encoding, "utf-8") != 0 &&
3141 strcmp(encoding, "iso-8859-1") != 0);
3142 if (rawmode || strchr(s, '\\') == NULL) {
3143 if (need_encoding) {
3144#ifndef Py_USING_UNICODE
3145 /* This should not happen - we never see any other
3146 encoding. */
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003147 Py_FatalError(
3148 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003150 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 if (u == NULL)
3152 return NULL;
3153 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3154 Py_DECREF(u);
3155 return v;
3156#endif
3157 } else {
3158 return PyString_FromStringAndSize(s, len);
3159 }
3160 }
3161
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003162 return PyString_DecodeEscape(s, len, NULL, unicode,
3163 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164}
3165
3166/* Build a Python string object out of a STRING atom. This takes care of
3167 * compile-time literal catenation, calling parsestr() on each piece, and
3168 * pasting the intermediate results together.
3169 */
3170static PyObject *
3171parsestrplus(struct compiling *c, const node *n)
3172{
3173 PyObject *v;
3174 int i;
3175 REQ(CHILD(n, 0), STRING);
3176 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3177 /* String literal concatenation */
3178 for (i = 1; i < NCH(n); i++) {
3179 PyObject *s;
3180 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3181 if (s == NULL)
3182 goto onError;
3183 if (PyString_Check(v) && PyString_Check(s)) {
3184 PyString_ConcatAndDel(&v, s);
3185 if (v == NULL)
3186 goto onError;
3187 }
3188#ifdef Py_USING_UNICODE
3189 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003190 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 Py_DECREF(v);
3193 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003194 if (v == NULL)
3195 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 }
3197#endif
3198 }
3199 }
3200 return v;
3201
3202 onError:
3203 Py_XDECREF(v);
3204 return NULL;
3205}