blob: 9e8d911a87a1e78fcbcdaa46246a8740e0567c06 [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;
Neal Norwitz0d62a062006-07-30 06:53:31 +0000390 case Yield_kind:
391 expr_name = "yield expression";
392 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000393 case ListComp_kind:
394 expr_name = "list comprehension";
395 break;
396 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 case Num_kind:
398 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000399 expr_name = "literal";
400 break;
401 case Compare_kind:
402 expr_name = "comparison";
403 break;
404 case Repr_kind:
405 expr_name = "repr";
406 break;
Neal Norwitz373f0a72006-05-15 07:04:36 +0000407 case IfExp_kind:
408 expr_name = "conditional expression";
409 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000410 default:
411 PyErr_Format(PyExc_SystemError,
412 "unexpected expression in assignment %d (line %d)",
413 e->kind, e->lineno);
414 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000416 /* Check for error string set by switch */
417 if (expr_name) {
418 char buf[300];
419 PyOS_snprintf(buf, sizeof(buf),
420 "can't %s %s",
421 ctx == Store ? "assign to" : "delete",
422 expr_name);
423 return ast_error(n, buf);
424 }
425
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000427 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 */
429 if (s) {
430 int i;
431
432 for (i = 0; i < asdl_seq_LEN(s); i++) {
Martin v. Löwis28457502006-04-11 09:17:27 +0000433 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434 return 0;
435 }
436 }
437 return 1;
438}
439
440static operator_ty
441ast_for_augassign(const node *n)
442{
443 REQ(n, augassign);
444 n = CHILD(n, 0);
445 switch (STR(n)[0]) {
446 case '+':
447 return Add;
448 case '-':
449 return Sub;
450 case '/':
451 if (STR(n)[1] == '/')
452 return FloorDiv;
453 else
454 return Div;
455 case '%':
456 return Mod;
457 case '<':
458 return LShift;
459 case '>':
460 return RShift;
461 case '&':
462 return BitAnd;
463 case '^':
464 return BitXor;
465 case '|':
466 return BitOr;
467 case '*':
468 if (STR(n)[1] == '*')
469 return Pow;
470 else
471 return Mult;
472 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000473 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000474 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 }
476}
477
478static cmpop_ty
479ast_for_comp_op(const node *n)
480{
481 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
482 |'is' 'not'
483 */
484 REQ(n, comp_op);
485 if (NCH(n) == 1) {
486 n = CHILD(n, 0);
487 switch (TYPE(n)) {
488 case LESS:
489 return Lt;
490 case GREATER:
491 return Gt;
492 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 return Eq;
494 case LESSEQUAL:
495 return LtE;
496 case GREATEREQUAL:
497 return GtE;
498 case NOTEQUAL:
499 return NotEq;
500 case NAME:
501 if (strcmp(STR(n), "in") == 0)
502 return In;
503 if (strcmp(STR(n), "is") == 0)
504 return Is;
505 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000506 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507 STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000508 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 }
510 }
511 else if (NCH(n) == 2) {
512 /* handle "not in" and "is not" */
513 switch (TYPE(CHILD(n, 0))) {
514 case NAME:
515 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
516 return NotIn;
517 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
518 return IsNot;
519 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000520 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Martin v. Löwis28457502006-04-11 09:17:27 +0000522 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 }
524 }
Neal Norwitz79792652005-11-14 04:25:03 +0000525 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000527 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528}
529
530static asdl_seq *
531seq_for_testlist(struct compiling *c, const node *n)
532{
533 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000534 asdl_seq *seq;
535 expr_ty expression;
536 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 assert(TYPE(n) == testlist
538 || TYPE(n) == listmaker
539 || TYPE(n) == testlist_gexp
540 || TYPE(n) == testlist_safe
541 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000543 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544 if (!seq)
545 return NULL;
546
547 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000548 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549
550 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000551 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553
554 assert(i / 2 < seq->size);
555 asdl_seq_SET(seq, i / 2, expression);
556 }
557 return seq;
558}
559
560static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000561compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562{
563 int i, len = (NCH(n) + 1) / 2;
564 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000565 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566 if (!args)
567 return NULL;
568
569 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 for (i = 0; i < len; i++) {
571 const node *child = CHILD(CHILD(n, 2*i), 0);
572 expr_ty arg;
573 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000574 if (!strcmp(STR(child), "None")) {
575 ast_error(child, "assignment to None");
576 return NULL;
577 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000578 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
579 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000580 }
581 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000582 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000583 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584 asdl_seq_SET(args, i, arg);
585 }
586
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000587 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000588 if (!set_context(result, Store, n))
589 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 return result;
591}
592
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593
Jeremy Hyltona8293132006-02-28 17:58:27 +0000594/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
596static arguments_ty
597ast_for_arguments(struct compiling *c, const node *n)
598{
599 /* parameters: '(' [varargslist] ')'
600 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
601 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
602 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000603 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 asdl_seq *args, *defaults;
605 identifier vararg = NULL, kwarg = NULL;
606 node *ch;
607
608 if (TYPE(n) == parameters) {
609 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000610 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611 n = CHILD(n, 1);
612 }
613 REQ(n, varargslist);
614
615 /* first count the number of normal args & defaults */
616 for (i = 0; i < NCH(n); i++) {
617 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000618 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 if (TYPE(ch) == EQUAL)
621 n_defaults++;
622 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000623 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 if (!args && n_args)
625 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000626 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627 if (!defaults && n_defaults)
628 goto error;
629
630 /* fpdef: NAME | '(' fplist ')'
631 fplist: fpdef (',' fpdef)* [',']
632 */
633 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000634 j = 0; /* index for defaults */
635 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 while (i < NCH(n)) {
637 ch = CHILD(n, i);
638 switch (TYPE(ch)) {
639 case fpdef:
640 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
641 anything other than EQUAL or a comma? */
642 /* XXX Should NCH(n) check be made a separate check? */
643 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000644 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
645 if (!expression)
646 goto error;
647 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 i += 2;
649 found_default = 1;
650 }
651 else if (found_default) {
652 ast_error(n,
653 "non-default argument follows default argument");
654 goto error;
655 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000657 ch = CHILD(ch, 1);
658 /* def foo((x)): is not complex, special case. */
659 if (NCH(ch) != 1) {
660 /* We have complex arguments, setup for unpacking. */
661 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
662 } else {
663 /* def foo((x)): setup for checking NAME below. */
664 ch = CHILD(ch, 0);
665 }
666 }
667 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000668 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
670 ast_error(CHILD(ch, 0), "assignment to None");
671 goto error;
672 }
Armin Rigo31441302005-10-21 12:57:31 +0000673 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000674 Param, LINENO(ch), ch->n_col_offset,
675 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 if (!name)
677 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000678 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679
680 }
681 i += 2; /* the name and the comma */
682 break;
683 case STAR:
684 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
685 ast_error(CHILD(n, i+1), "assignment to None");
686 goto error;
687 }
688 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
689 i += 3;
690 break;
691 case DOUBLESTAR:
692 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
693 ast_error(CHILD(n, i+1), "assignment to None");
694 goto error;
695 }
696 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
697 i += 3;
698 break;
699 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000700 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 "unexpected node in varargslist: %d @ %d",
702 TYPE(ch), i);
703 goto error;
704 }
705 }
706
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000707 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708
709 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000710 Py_XDECREF(vararg);
711 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 return NULL;
713}
714
715static expr_ty
716ast_for_dotted_name(struct compiling *c, const node *n)
717{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000718 expr_ty e;
719 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000720 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 int i;
722
723 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000724
725 lineno = LINENO(n);
726 col_offset = n->n_col_offset;
727
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728 id = NEW_IDENTIFIER(CHILD(n, 0));
729 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000730 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000731 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000733 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734
735 for (i = 2; i < NCH(n); i+=2) {
736 id = NEW_IDENTIFIER(CHILD(n, i));
737 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000738 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000739 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000740 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742 }
743
744 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745}
746
747static expr_ty
748ast_for_decorator(struct compiling *c, const node *n)
749{
750 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
751 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000752 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753
754 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000755 REQ(CHILD(n, 0), AT);
756 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757
758 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
759 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000760 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761
762 if (NCH(n) == 3) { /* No arguments */
763 d = name_expr;
764 name_expr = NULL;
765 }
766 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000767 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
768 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000770 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 name_expr = NULL;
772 }
773 else {
774 d = ast_for_call(c, CHILD(n, 3), name_expr);
775 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000776 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 name_expr = NULL;
778 }
779
780 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781}
782
783static asdl_seq*
784ast_for_decorators(struct compiling *c, const node *n)
785{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000786 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000787 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 int i;
789
790 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000791 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 if (!decorator_seq)
793 return NULL;
794
795 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000796 d = ast_for_decorator(c, CHILD(n, i));
797 if (!d)
798 return NULL;
799 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 }
801 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802}
803
804static stmt_ty
805ast_for_funcdef(struct compiling *c, const node *n)
806{
807 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000808 identifier name;
809 arguments_ty args;
810 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 asdl_seq *decorator_seq = NULL;
812 int name_i;
813
814 REQ(n, funcdef);
815
816 if (NCH(n) == 6) { /* decorators are present */
817 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
818 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000819 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 name_i = 2;
821 }
822 else {
823 name_i = 1;
824 }
825
826 name = NEW_IDENTIFIER(CHILD(n, name_i));
827 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000828 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000830 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000831 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 }
833 args = ast_for_arguments(c, CHILD(n, name_i + 1));
834 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000835 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 body = ast_for_suite(c, CHILD(n, name_i + 3));
837 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000838 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000840 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
841 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842}
843
844static expr_ty
845ast_for_lambdef(struct compiling *c, const node *n)
846{
847 /* lambdef: 'lambda' [varargslist] ':' test */
848 arguments_ty args;
849 expr_ty expression;
850
851 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000852 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 if (!args)
854 return NULL;
855 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000856 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
859 else {
860 args = ast_for_arguments(c, CHILD(n, 1));
861 if (!args)
862 return NULL;
863 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000864 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 }
867
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000868 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869}
870
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000871static expr_ty
872ast_for_ifexpr(struct compiling *c, const node *n)
873{
874 /* test: or_test 'if' or_test 'else' test */
875 expr_ty expression, body, orelse;
876
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000877 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000878 body = ast_for_expr(c, CHILD(n, 0));
879 if (!body)
880 return NULL;
881 expression = ast_for_expr(c, CHILD(n, 2));
882 if (!expression)
883 return NULL;
884 orelse = ast_for_expr(c, CHILD(n, 4));
885 if (!orelse)
886 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000887 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
888 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000889}
890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891/* Count the number of 'for' loop in a list comprehension.
892
893 Helper for ast_for_listcomp().
894*/
895
896static int
897count_list_fors(const node *n)
898{
899 int n_fors = 0;
900 node *ch = CHILD(n, 1);
901
902 count_list_for:
903 n_fors++;
904 REQ(ch, list_for);
905 if (NCH(ch) == 5)
906 ch = CHILD(ch, 4);
907 else
908 return n_fors;
909 count_list_iter:
910 REQ(ch, list_iter);
911 ch = CHILD(ch, 0);
912 if (TYPE(ch) == list_for)
913 goto count_list_for;
914 else if (TYPE(ch) == list_if) {
915 if (NCH(ch) == 3) {
916 ch = CHILD(ch, 2);
917 goto count_list_iter;
918 }
919 else
920 return n_fors;
921 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000922
923 /* Should never be reached */
924 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
925 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926}
927
928/* Count the number of 'if' statements in a list comprehension.
929
930 Helper for ast_for_listcomp().
931*/
932
933static int
934count_list_ifs(const node *n)
935{
936 int n_ifs = 0;
937
938 count_list_iter:
939 REQ(n, list_iter);
940 if (TYPE(CHILD(n, 0)) == list_for)
941 return n_ifs;
942 n = CHILD(n, 0);
943 REQ(n, list_if);
944 n_ifs++;
945 if (NCH(n) == 2)
946 return n_ifs;
947 n = CHILD(n, 2);
948 goto count_list_iter;
949}
950
951static expr_ty
952ast_for_listcomp(struct compiling *c, const node *n)
953{
954 /* listmaker: test ( list_for | (',' test)* [','] )
955 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
956 list_iter: list_for | list_if
957 list_if: 'if' test [list_iter]
958 testlist_safe: test [(',' test)+ [',']]
959 */
960 expr_ty elt;
961 asdl_seq *listcomps;
962 int i, n_fors;
963 node *ch;
964
965 REQ(n, listmaker);
966 assert(NCH(n) > 1);
967
968 elt = ast_for_expr(c, CHILD(n, 0));
969 if (!elt)
970 return NULL;
971
972 n_fors = count_list_fors(n);
973 if (n_fors == -1)
974 return NULL;
975
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976 listcomps = asdl_seq_new(n_fors, c->c_arena);
977 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000979
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 ch = CHILD(n, 1);
981 for (i = 0; i < n_fors; i++) {
982 comprehension_ty lc;
983 asdl_seq *t;
984 expr_ty expression;
985
986 REQ(ch, list_for);
987
988 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000989 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000991 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000992 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000995 if (asdl_seq_LEN(t) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +0000996 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000997 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000999 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1000 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001001 expression, NULL, c->c_arena);
1002 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004
1005 if (NCH(ch) == 5) {
1006 int j, n_ifs;
1007 asdl_seq *ifs;
1008
1009 ch = CHILD(ch, 4);
1010 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001011 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001014 ifs = asdl_seq_new(n_ifs, c->c_arena);
1015 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017
1018 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001019 REQ(ch, list_iter);
1020 ch = CHILD(ch, 0);
1021 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022
Jeremy Hyltona8293132006-02-28 17:58:27 +00001023 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1024 if (NCH(ch) == 3)
1025 ch = CHILD(ch, 2);
1026 }
1027 /* on exit, must guarantee that ch is a list_for */
1028 if (TYPE(ch) == list_iter)
1029 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001031 }
1032 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 }
1034
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001035 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036}
1037
1038/*
1039 Count the number of 'for' loops in a generator expression.
1040
1041 Helper for ast_for_genexp().
1042*/
1043
1044static int
1045count_gen_fors(const node *n)
1046{
1047 int n_fors = 0;
1048 node *ch = CHILD(n, 1);
1049
1050 count_gen_for:
1051 n_fors++;
1052 REQ(ch, gen_for);
1053 if (NCH(ch) == 5)
1054 ch = CHILD(ch, 4);
1055 else
1056 return n_fors;
1057 count_gen_iter:
1058 REQ(ch, gen_iter);
1059 ch = CHILD(ch, 0);
1060 if (TYPE(ch) == gen_for)
1061 goto count_gen_for;
1062 else if (TYPE(ch) == gen_if) {
1063 if (NCH(ch) == 3) {
1064 ch = CHILD(ch, 2);
1065 goto count_gen_iter;
1066 }
1067 else
1068 return n_fors;
1069 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001070
1071 /* Should never be reached */
1072 PyErr_SetString(PyExc_SystemError,
1073 "logic error in count_gen_fors");
1074 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075}
1076
1077/* Count the number of 'if' statements in a generator expression.
1078
1079 Helper for ast_for_genexp().
1080*/
1081
1082static int
1083count_gen_ifs(const node *n)
1084{
1085 int n_ifs = 0;
1086
1087 while (1) {
1088 REQ(n, gen_iter);
1089 if (TYPE(CHILD(n, 0)) == gen_for)
1090 return n_ifs;
1091 n = CHILD(n, 0);
1092 REQ(n, gen_if);
1093 n_ifs++;
1094 if (NCH(n) == 2)
1095 return n_ifs;
1096 n = CHILD(n, 2);
1097 }
1098}
1099
Jeremy Hyltona8293132006-02-28 17:58:27 +00001100/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101static expr_ty
1102ast_for_genexp(struct compiling *c, const node *n)
1103{
1104 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1105 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1106 expr_ty elt;
1107 asdl_seq *genexps;
1108 int i, n_fors;
1109 node *ch;
1110
1111 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1112 assert(NCH(n) > 1);
1113
1114 elt = ast_for_expr(c, CHILD(n, 0));
1115 if (!elt)
1116 return NULL;
1117
1118 n_fors = count_gen_fors(n);
1119 if (n_fors == -1)
1120 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001121
1122 genexps = asdl_seq_new(n_fors, c->c_arena);
1123 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 ch = CHILD(n, 1);
1127 for (i = 0; i < n_fors; i++) {
1128 comprehension_ty ge;
1129 asdl_seq *t;
1130 expr_ty expression;
1131
1132 REQ(ch, gen_for);
1133
1134 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001135 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001137 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001138 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001140
1141 if (asdl_seq_LEN(t) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001142 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001143 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001145 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1146 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001147 expression, NULL, c->c_arena);
1148
1149 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 if (NCH(ch) == 5) {
1153 int j, n_ifs;
1154 asdl_seq *ifs;
1155
1156 ch = CHILD(ch, 4);
1157 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001158 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001160
1161 ifs = asdl_seq_new(n_ifs, c->c_arena);
1162 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001164
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 for (j = 0; j < n_ifs; j++) {
1166 REQ(ch, gen_iter);
1167 ch = CHILD(ch, 0);
1168 REQ(ch, gen_if);
1169
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001170 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001171 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001172 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001173 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 if (NCH(ch) == 3)
1175 ch = CHILD(ch, 2);
1176 }
1177 /* on exit, must guarantee that ch is a gen_for */
1178 if (TYPE(ch) == gen_iter)
1179 ch = CHILD(ch, 0);
1180 ge->ifs = ifs;
1181 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001182 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 }
1184
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001185 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186}
1187
1188static expr_ty
1189ast_for_atom(struct compiling *c, const node *n)
1190{
1191 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1192 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1193 */
1194 node *ch = CHILD(n, 0);
1195
1196 switch (TYPE(ch)) {
1197 case NAME:
1198 /* All names start in Load context, but may later be
1199 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001200 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 case STRING: {
1202 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 if (!str)
1204 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001205
1206 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001207 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 }
1209 case NUMBER: {
1210 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 if (!pynum)
1212 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001213
1214 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001215 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 }
1217 case LPAR: /* some parenthesized expressions */
1218 ch = CHILD(n, 1);
1219
1220 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001221 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222
1223 if (TYPE(ch) == yield_expr)
1224 return ast_for_expr(c, ch);
1225
1226 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1227 return ast_for_genexp(c, ch);
1228
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001229 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 case LSQB: /* list (or list comprehension) */
1231 ch = CHILD(n, 1);
1232
1233 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001234 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235
1236 REQ(ch, listmaker);
1237 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1238 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 if (!elts)
1240 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001242 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 }
1244 else
1245 return ast_for_listcomp(c, ch);
1246 case LBRACE: {
1247 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1248 int i, size;
1249 asdl_seq *keys, *values;
1250
1251 ch = CHILD(n, 1);
1252 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001253 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 if (!keys)
1255 return NULL;
1256
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001257 values = asdl_seq_new(size, c->c_arena);
1258 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260
1261 for (i = 0; i < NCH(ch); i += 4) {
1262 expr_ty expression;
1263
1264 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001265 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001267
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001269
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001271 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001273
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 asdl_seq_SET(values, i / 4, expression);
1275 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001276 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 }
1278 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001279 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 if (!expression)
1281 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001282
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001283 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 }
1285 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001286 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 return NULL;
1288 }
1289}
1290
1291static slice_ty
1292ast_for_slice(struct compiling *c, const node *n)
1293{
1294 node *ch;
1295 expr_ty lower = NULL, upper = NULL, step = NULL;
1296
1297 REQ(n, subscript);
1298
1299 /*
1300 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1301 sliceop: ':' [test]
1302 */
1303 ch = CHILD(n, 0);
1304 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001305 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306
1307 if (NCH(n) == 1 && TYPE(ch) == test) {
1308 /* 'step' variable hold no significance in terms of being used over
1309 other vars */
1310 step = ast_for_expr(c, ch);
1311 if (!step)
1312 return NULL;
1313
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001314 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315 }
1316
1317 if (TYPE(ch) == test) {
1318 lower = ast_for_expr(c, ch);
1319 if (!lower)
1320 return NULL;
1321 }
1322
1323 /* If there's an upper bound it's in the second or third position. */
1324 if (TYPE(ch) == COLON) {
1325 if (NCH(n) > 1) {
1326 node *n2 = CHILD(n, 1);
1327
1328 if (TYPE(n2) == test) {
1329 upper = ast_for_expr(c, n2);
1330 if (!upper)
1331 return NULL;
1332 }
1333 }
1334 } else if (NCH(n) > 2) {
1335 node *n2 = CHILD(n, 2);
1336
1337 if (TYPE(n2) == test) {
1338 upper = ast_for_expr(c, n2);
1339 if (!upper)
1340 return NULL;
1341 }
1342 }
1343
1344 ch = CHILD(n, NCH(n) - 1);
1345 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001346 if (NCH(ch) == 1) {
1347 /* No expression, so step is None */
1348 ch = CHILD(ch, 0);
1349 step = Name(new_identifier("None", c->c_arena), Load,
1350 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 if (!step)
1352 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001353 } else {
1354 ch = CHILD(ch, 1);
1355 if (TYPE(ch) == test) {
1356 step = ast_for_expr(c, ch);
1357 if (!step)
1358 return NULL;
1359 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 }
1361 }
1362
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001363 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364}
1365
1366static expr_ty
1367ast_for_binop(struct compiling *c, const node *n)
1368{
1369 /* Must account for a sequence of expressions.
1370 How should A op B op C by represented?
1371 BinOp(BinOp(A, op, B), op, C).
1372 */
1373
1374 int i, nops;
1375 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001376 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377
1378 expr1 = ast_for_expr(c, CHILD(n, 0));
1379 if (!expr1)
1380 return NULL;
1381
1382 expr2 = ast_for_expr(c, CHILD(n, 2));
1383 if (!expr2)
1384 return NULL;
1385
Anthony Baxtera863d332006-04-11 07:43:46 +00001386 newoperator = get_operator(CHILD(n, 1));
1387 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 return NULL;
1389
Anthony Baxtera863d332006-04-11 07:43:46 +00001390 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001391 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 if (!result)
1393 return NULL;
1394
1395 nops = (NCH(n) - 1) / 2;
1396 for (i = 1; i < nops; i++) {
1397 expr_ty tmp_result, tmp;
1398 const node* next_oper = CHILD(n, i * 2 + 1);
1399
Anthony Baxtera863d332006-04-11 07:43:46 +00001400 newoperator = get_operator(next_oper);
1401 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 return NULL;
1403
1404 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1405 if (!tmp)
1406 return NULL;
1407
Anthony Baxtera863d332006-04-11 07:43:46 +00001408 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001409 LINENO(next_oper), next_oper->n_col_offset,
1410 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 if (!tmp)
1412 return NULL;
1413 result = tmp_result;
1414 }
1415 return result;
1416}
1417
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001418static expr_ty
1419ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1420{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001421 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1422 subscriptlist: subscript (',' subscript)* [',']
1423 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1424 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001425 REQ(n, trailer);
1426 if (TYPE(CHILD(n, 0)) == LPAR) {
1427 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001428 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1429 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001430 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001431 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001432 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001433 else if (TYPE(CHILD(n, 0)) == DOT ) {
1434 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001435 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001436 }
1437 else {
1438 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001439 REQ(CHILD(n, 2), RSQB);
1440 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001441 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001442 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1443 if (!slc)
1444 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001445 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1446 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001447 }
1448 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001449 /* The grammar is ambiguous here. The ambiguity is resolved
1450 by treating the sequence as a tuple literal if there are
1451 no slice features.
1452 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001453 int j;
1454 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001455 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001456 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001457 asdl_seq *slices, *elts;
1458 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001459 if (!slices)
1460 return NULL;
1461 for (j = 0; j < NCH(n); j += 2) {
1462 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001463 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001464 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001465 if (slc->kind != Index_kind)
1466 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001467 asdl_seq_SET(slices, j / 2, slc);
1468 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001469 if (!simple) {
1470 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001471 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001472 }
1473 /* extract Index values and put them in a Tuple */
1474 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001475 if (!elts)
1476 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001477 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1478 slc = (slice_ty)asdl_seq_GET(slices, j);
1479 assert(slc->kind == Index_kind && slc->v.Index.value);
1480 asdl_seq_SET(elts, j, slc->v.Index.value);
1481 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001482 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001483 if (!e)
1484 return NULL;
1485 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001486 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001487 }
1488 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001489}
1490
1491static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001492ast_for_factor(struct compiling *c, const node *n)
1493{
1494 node *pfactor, *ppower, *patom, *pnum;
1495 expr_ty expression;
1496
1497 /* If the unary - operator is applied to a constant, don't generate
1498 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1499 constant. The peephole optimizer already does something like
1500 this but it doesn't handle the case where the constant is
1501 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1502 PyLongObject.
1503 */
1504 if (TYPE(CHILD(n, 0)) == MINUS
1505 && NCH(n) == 2
1506 && TYPE((pfactor = CHILD(n, 1))) == factor
1507 && NCH(pfactor) == 1
1508 && TYPE((ppower = CHILD(pfactor, 0))) == power
1509 && NCH(ppower) == 1
1510 && TYPE((patom = CHILD(ppower, 0))) == atom
1511 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1512 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1513 if (s == NULL)
1514 return NULL;
1515 s[0] = '-';
1516 strcpy(s + 1, STR(pnum));
1517 PyObject_FREE(STR(pnum));
1518 STR(pnum) = s;
1519 return ast_for_atom(c, patom);
1520 }
1521
1522 expression = ast_for_expr(c, CHILD(n, 1));
1523 if (!expression)
1524 return NULL;
1525
1526 switch (TYPE(CHILD(n, 0))) {
1527 case PLUS:
1528 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1529 c->c_arena);
1530 case MINUS:
1531 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1532 c->c_arena);
1533 case TILDE:
1534 return UnaryOp(Invert, expression, LINENO(n),
1535 n->n_col_offset, c->c_arena);
1536 }
1537 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1538 TYPE(CHILD(n, 0)));
1539 return NULL;
1540}
1541
1542static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001543ast_for_power(struct compiling *c, const node *n)
1544{
1545 /* power: atom trailer* ('**' factor)*
1546 */
1547 int i;
1548 expr_ty e, tmp;
1549 REQ(n, power);
1550 e = ast_for_atom(c, CHILD(n, 0));
1551 if (!e)
1552 return NULL;
1553 if (NCH(n) == 1)
1554 return e;
1555 for (i = 1; i < NCH(n); i++) {
1556 node *ch = CHILD(n, i);
1557 if (TYPE(ch) != trailer)
1558 break;
1559 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001560 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001561 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001562 tmp->lineno = e->lineno;
1563 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001564 e = tmp;
1565 }
1566 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1567 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001568 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001569 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001570 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001571 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001572 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001573 e = tmp;
1574 }
1575 return e;
1576}
1577
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578/* Do not name a variable 'expr'! Will cause a compile error.
1579*/
1580
1581static expr_ty
1582ast_for_expr(struct compiling *c, const node *n)
1583{
1584 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001585 test: or_test ['if' or_test 'else' test] | lambdef
1586 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587 and_test: not_test ('and' not_test)*
1588 not_test: 'not' not_test | comparison
1589 comparison: expr (comp_op expr)*
1590 expr: xor_expr ('|' xor_expr)*
1591 xor_expr: and_expr ('^' and_expr)*
1592 and_expr: shift_expr ('&' shift_expr)*
1593 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1594 arith_expr: term (('+'|'-') term)*
1595 term: factor (('*'|'/'|'%'|'//') factor)*
1596 factor: ('+'|'-'|'~') factor | power
1597 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001598
1599 As well as modified versions that exist for backward compatibility,
1600 to explicitly allow:
1601 [ x for x in lambda: 0, lambda: 1 ]
1602 (which would be ambiguous without these extra rules)
1603
1604 old_test: or_test | old_lambdef
1605 old_lambdef: 'lambda' [vararglist] ':' old_test
1606
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607 */
1608
1609 asdl_seq *seq;
1610 int i;
1611
1612 loop:
1613 switch (TYPE(n)) {
1614 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001615 case old_test:
1616 if (TYPE(CHILD(n, 0)) == lambdef ||
1617 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001619 else if (NCH(n) > 1)
1620 return ast_for_ifexpr(c, n);
1621 /* Fallthrough */
1622 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 case and_test:
1624 if (NCH(n) == 1) {
1625 n = CHILD(n, 0);
1626 goto loop;
1627 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001628 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 if (!seq)
1630 return NULL;
1631 for (i = 0; i < NCH(n); i += 2) {
1632 expr_ty e = ast_for_expr(c, CHILD(n, i));
1633 if (!e)
1634 return NULL;
1635 asdl_seq_SET(seq, i / 2, e);
1636 }
1637 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001638 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1639 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001640 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001641 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642 case not_test:
1643 if (NCH(n) == 1) {
1644 n = CHILD(n, 0);
1645 goto loop;
1646 }
1647 else {
1648 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1649 if (!expression)
1650 return NULL;
1651
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001652 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1653 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654 }
1655 case comparison:
1656 if (NCH(n) == 1) {
1657 n = CHILD(n, 0);
1658 goto loop;
1659 }
1660 else {
1661 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001662 asdl_int_seq *ops;
1663 asdl_seq *cmps;
1664 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 if (!ops)
1666 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001667 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669 return NULL;
1670 }
1671 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001672 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673
Anthony Baxtera863d332006-04-11 07:43:46 +00001674 newoperator = ast_for_comp_op(CHILD(n, i));
1675 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001677 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678
1679 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001680 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001682 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001684 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 asdl_seq_SET(cmps, i / 2, expression);
1686 }
1687 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001688 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001690 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001692 return Compare(expression, ops, cmps, LINENO(n),
1693 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694 }
1695 break;
1696
1697 /* The next five cases all handle BinOps. The main body of code
1698 is the same in each case, but the switch turned inside out to
1699 reuse the code for each type of operator.
1700 */
1701 case expr:
1702 case xor_expr:
1703 case and_expr:
1704 case shift_expr:
1705 case arith_expr:
1706 case term:
1707 if (NCH(n) == 1) {
1708 n = CHILD(n, 0);
1709 goto loop;
1710 }
1711 return ast_for_binop(c, n);
1712 case yield_expr: {
1713 expr_ty exp = NULL;
1714 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001715 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 if (!exp)
1717 return NULL;
1718 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001719 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001721 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 if (NCH(n) == 1) {
1723 n = CHILD(n, 0);
1724 goto loop;
1725 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001726 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001727 case power:
1728 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001730 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 return NULL;
1732 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001733 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 return NULL;
1735}
1736
1737static expr_ty
1738ast_for_call(struct compiling *c, const node *n, expr_ty func)
1739{
1740 /*
1741 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1742 | '**' test)
1743 argument: [test '='] test [gen_for] # Really [keyword '='] test
1744 */
1745
1746 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001747 asdl_seq *args;
1748 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 expr_ty vararg = NULL, kwarg = NULL;
1750
1751 REQ(n, arglist);
1752
1753 nargs = 0;
1754 nkeywords = 0;
1755 ngens = 0;
1756 for (i = 0; i < NCH(n); i++) {
1757 node *ch = CHILD(n, i);
1758 if (TYPE(ch) == argument) {
1759 if (NCH(ch) == 1)
1760 nargs++;
1761 else if (TYPE(CHILD(ch, 1)) == gen_for)
1762 ngens++;
1763 else
1764 nkeywords++;
1765 }
1766 }
1767 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001768 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769 "if not sole argument");
1770 return NULL;
1771 }
1772
1773 if (nargs + nkeywords + ngens > 255) {
1774 ast_error(n, "more than 255 arguments");
1775 return NULL;
1776 }
1777
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001778 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001780 return NULL;
1781 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001783 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784 nargs = 0;
1785 nkeywords = 0;
1786 for (i = 0; i < NCH(n); i++) {
1787 node *ch = CHILD(n, i);
1788 if (TYPE(ch) == argument) {
1789 expr_ty e;
1790 if (NCH(ch) == 1) {
Neal Norwitz5ef92242006-05-19 06:43:50 +00001791 if (nkeywords) {
1792 ast_error(CHILD(ch, 0),
1793 "non-keyword arg after keyword arg");
1794 return NULL;
1795 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 e = ast_for_expr(c, CHILD(ch, 0));
1797 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 asdl_seq_SET(args, nargs++, e);
1800 }
1801 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1802 e = ast_for_genexp(c, ch);
1803 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001804 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 asdl_seq_SET(args, nargs++, e);
1806 }
1807 else {
1808 keyword_ty kw;
1809 identifier key;
1810
1811 /* CHILD(ch, 0) is test, but must be an identifier? */
1812 e = ast_for_expr(c, CHILD(ch, 0));
1813 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 /* f(lambda x: x[0] = 3) ends up getting parsed with
1816 * LHS test = lambda x: x[0], and RHS test = 3.
1817 * SF bug 132313 points out that complaining about a keyword
1818 * then is very confusing.
1819 */
1820 if (e->kind == Lambda_kind) {
1821 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001822 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 } else if (e->kind != Name_kind) {
1824 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001825 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 }
1827 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 e = ast_for_expr(c, CHILD(ch, 2));
1829 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001830 return NULL;
1831 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 asdl_seq_SET(keywords, nkeywords++, kw);
1835 }
1836 }
1837 else if (TYPE(ch) == STAR) {
1838 vararg = ast_for_expr(c, CHILD(n, i+1));
1839 i++;
1840 }
1841 else if (TYPE(ch) == DOUBLESTAR) {
1842 kwarg = ast_for_expr(c, CHILD(n, i+1));
1843 i++;
1844 }
1845 }
1846
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001847 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848}
1849
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001851ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001853 /* testlist_gexp: test (',' test)* [','] */
1854 /* testlist: test (',' test)* [','] */
1855 /* testlist_safe: test (',' test)+ [','] */
1856 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001858 if (TYPE(n) == testlist_gexp) {
1859 if (NCH(n) > 1)
1860 assert(TYPE(CHILD(n, 1)) != gen_for);
1861 }
1862 else {
1863 assert(TYPE(n) == testlist ||
1864 TYPE(n) == testlist_safe ||
1865 TYPE(n) == testlist1);
1866 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 if (NCH(n) == 1)
1868 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 else {
1870 asdl_seq *tmp = seq_for_testlist(c, n);
1871 if (!tmp)
1872 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001873 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001875}
1876
1877static expr_ty
1878ast_for_testlist_gexp(struct compiling *c, const node* n)
1879{
1880 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1881 /* argument: test [ gen_for ] */
1882 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001883 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001884 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001885 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001886}
1887
1888/* like ast_for_testlist() but returns a sequence */
1889static asdl_seq*
1890ast_for_class_bases(struct compiling *c, const node* n)
1891{
1892 /* testlist: test (',' test)* [','] */
1893 assert(NCH(n) > 0);
1894 REQ(n, testlist);
1895 if (NCH(n) == 1) {
1896 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001897 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001898 if (!bases)
1899 return NULL;
1900 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001901 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001902 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001903 asdl_seq_SET(bases, 0, base);
1904 return bases;
1905 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001906
1907 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908}
1909
1910static stmt_ty
1911ast_for_expr_stmt(struct compiling *c, const node *n)
1912{
1913 REQ(n, expr_stmt);
1914 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1915 | ('=' (yield_expr|testlist))*)
1916 testlist: test (',' test)* [',']
1917 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1918 | '<<=' | '>>=' | '**=' | '//='
1919 test: ... here starts the operator precendence dance
1920 */
1921
1922 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001923 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924 if (!e)
1925 return NULL;
1926
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001927 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 }
1929 else if (TYPE(CHILD(n, 1)) == augassign) {
1930 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00001931 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932 node *ch = CHILD(n, 0);
1933
Neal Norwitz0d62a062006-07-30 06:53:31 +00001934 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 if (!expr1)
1936 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001937 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001938 switch (expr1->kind) {
1939 case GeneratorExp_kind:
1940 ast_error(ch, "augmented assignment to generator "
1941 "expression not possible");
1942 return NULL;
Neal Norwitz0d62a062006-07-30 06:53:31 +00001943 case Yield_kind:
1944 ast_error(ch, "augmented assignment to yield "
1945 "expression not possible");
1946 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001947 case Name_kind: {
1948 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1949 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1950 ast_error(ch, "assignment to None");
1951 return NULL;
1952 }
1953 break;
1954 }
1955 case Attribute_kind:
1956 case Subscript_kind:
1957 break;
1958 default:
1959 ast_error(ch, "illegal expression for augmented "
1960 "assignment");
1961 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 }
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001963 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964
1965 ch = CHILD(n, 2);
1966 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001967 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968 else
Neal Norwitz0d62a062006-07-30 06:53:31 +00001969 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001970 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 return NULL;
1972
Anthony Baxtera863d332006-04-11 07:43:46 +00001973 newoperator = ast_for_augassign(CHILD(n, 1));
1974 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975 return NULL;
1976
Anthony Baxtera863d332006-04-11 07:43:46 +00001977 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978 }
1979 else {
1980 int i;
1981 asdl_seq *targets;
1982 node *value;
1983 expr_ty expression;
1984
1985 /* a normal assignment */
1986 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001987 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988 if (!targets)
1989 return NULL;
1990 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001991 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 node *ch = CHILD(n, i);
1993 if (TYPE(ch) == yield_expr) {
1994 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001997 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998
1999 /* set context to assign */
2000 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002
Neal Norwitz84456bd2005-12-18 03:16:20 +00002003 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002004 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005
2006 asdl_seq_SET(targets, i / 2, e);
2007 }
2008 value = CHILD(n, NCH(n) - 1);
2009 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002010 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 else
2012 expression = ast_for_expr(c, value);
2013 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002014 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002015 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017}
2018
2019static stmt_ty
2020ast_for_print_stmt(struct compiling *c, const node *n)
2021{
2022 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2023 | '>>' test [ (',' test)+ [','] ] )
2024 */
2025 expr_ty dest = NULL, expression;
2026 asdl_seq *seq;
2027 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002028 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029
2030 REQ(n, print_stmt);
2031 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2032 dest = ast_for_expr(c, CHILD(n, 2));
2033 if (!dest)
2034 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002035 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002037 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002039 return NULL;
2040 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002042 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002044 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 }
2046 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002047 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048}
2049
2050static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002051ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052{
2053 asdl_seq *seq;
2054 int i;
2055 expr_ty e;
2056
2057 REQ(n, exprlist);
2058
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002059 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 if (!seq)
2061 return NULL;
2062 for (i = 0; i < NCH(n); i += 2) {
2063 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002064 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002065 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002066 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002067 if (context && !set_context(e, context, CHILD(n, i)))
2068 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 }
2070 return seq;
2071}
2072
2073static stmt_ty
2074ast_for_del_stmt(struct compiling *c, const node *n)
2075{
2076 asdl_seq *expr_list;
2077
2078 /* del_stmt: 'del' exprlist */
2079 REQ(n, del_stmt);
2080
2081 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2082 if (!expr_list)
2083 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002084 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085}
2086
2087static stmt_ty
2088ast_for_flow_stmt(struct compiling *c, const node *n)
2089{
2090 /*
2091 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2092 | yield_stmt
2093 break_stmt: 'break'
2094 continue_stmt: 'continue'
2095 return_stmt: 'return' [testlist]
2096 yield_stmt: yield_expr
2097 yield_expr: 'yield' testlist
2098 raise_stmt: 'raise' [test [',' test [',' test]]]
2099 */
2100 node *ch;
2101
2102 REQ(n, flow_stmt);
2103 ch = CHILD(n, 0);
2104 switch (TYPE(ch)) {
2105 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002106 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002108 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 case yield_stmt: { /* will reduce to yield_expr */
2110 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2111 if (!exp)
2112 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002113 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 }
2115 case return_stmt:
2116 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002117 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002119 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 if (!expression)
2121 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002122 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 }
2124 case raise_stmt:
2125 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002126 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 else if (NCH(ch) == 2) {
2128 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2129 if (!expression)
2130 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002131 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 }
2133 else if (NCH(ch) == 4) {
2134 expr_ty expr1, expr2;
2135
2136 expr1 = ast_for_expr(c, CHILD(ch, 1));
2137 if (!expr1)
2138 return NULL;
2139 expr2 = ast_for_expr(c, CHILD(ch, 3));
2140 if (!expr2)
2141 return NULL;
2142
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002143 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 }
2145 else if (NCH(ch) == 6) {
2146 expr_ty expr1, expr2, expr3;
2147
2148 expr1 = ast_for_expr(c, CHILD(ch, 1));
2149 if (!expr1)
2150 return NULL;
2151 expr2 = ast_for_expr(c, CHILD(ch, 3));
2152 if (!expr2)
2153 return NULL;
2154 expr3 = ast_for_expr(c, CHILD(ch, 5));
2155 if (!expr3)
2156 return NULL;
2157
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002158 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 }
2160 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002161 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 "unexpected flow_stmt: %d", TYPE(ch));
2163 return NULL;
2164 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002165
2166 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168}
2169
2170static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002171alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172{
2173 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002174 import_as_name: NAME ['as' NAME]
2175 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 dotted_name: NAME ('.' NAME)*
2177 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002178 PyObject *str;
2179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 loop:
2181 switch (TYPE(n)) {
2182 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002183 str = NULL;
2184 if (NCH(n) == 3) {
2185 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2186 ast_error(n, "must use 'as' in import");
2187 return NULL;
2188 }
2189 str = NEW_IDENTIFIER(CHILD(n, 2));
2190 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002191 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 case dotted_as_name:
2193 if (NCH(n) == 1) {
2194 n = CHILD(n, 0);
2195 goto loop;
2196 }
2197 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002198 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002199 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2200 ast_error(n, "must use 'as' in import");
2201 return NULL;
2202 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 assert(!a->asname);
2204 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2205 return a;
2206 }
2207 break;
2208 case dotted_name:
2209 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002210 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 else {
2212 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002213 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002214 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215 char *s;
2216
2217 len = 0;
2218 for (i = 0; i < NCH(n); i += 2)
2219 /* length of string plus one for the dot */
2220 len += strlen(STR(CHILD(n, i))) + 1;
2221 len--; /* the last name doesn't have a dot */
2222 str = PyString_FromStringAndSize(NULL, len);
2223 if (!str)
2224 return NULL;
2225 s = PyString_AS_STRING(str);
2226 if (!s)
2227 return NULL;
2228 for (i = 0; i < NCH(n); i += 2) {
2229 char *sch = STR(CHILD(n, i));
2230 strcpy(s, STR(CHILD(n, i)));
2231 s += strlen(sch);
2232 *s++ = '.';
2233 }
2234 --s;
2235 *s = '\0';
2236 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002237 PyArena_AddPyObject(c->c_arena, str);
2238 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 }
2240 break;
2241 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002242 str = PyString_InternFromString("*");
2243 PyArena_AddPyObject(c->c_arena, str);
2244 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002246 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 "unexpected import name: %d", TYPE(n));
2248 return NULL;
2249 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002250
2251 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 return NULL;
2253}
2254
2255static stmt_ty
2256ast_for_import_stmt(struct compiling *c, const node *n)
2257{
2258 /*
2259 import_stmt: import_name | import_from
2260 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002261 import_from: 'from' ('.'* dotted_name | '.') 'import'
2262 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002264 int lineno;
2265 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 int i;
2267 asdl_seq *aliases;
2268
2269 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002270 lineno = LINENO(n);
2271 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002273 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002275 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002276 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 if (!aliases)
2278 return NULL;
2279 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002280 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002281 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 asdl_seq_SET(aliases, i / 2, import_alias);
2284 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002285 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002287 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002289 int idx, ndots = 0;
2290 alias_ty mod = NULL;
2291 identifier modname;
2292
2293 /* Count the number of dots (for relative imports) and check for the
2294 optional module name */
2295 for (idx = 1; idx < NCH(n); idx++) {
2296 if (TYPE(CHILD(n, idx)) == dotted_name) {
2297 mod = alias_for_import_name(c, CHILD(n, idx));
2298 idx++;
2299 break;
2300 } else if (TYPE(CHILD(n, idx)) != DOT) {
2301 break;
2302 }
2303 ndots++;
2304 }
2305 idx++; /* skip over the 'import' keyword */
2306 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002307 case STAR:
2308 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002309 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002310 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002311 if (ndots) {
2312 ast_error(n, "'import *' not allowed with 'from .'");
2313 return NULL;
2314 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002315 break;
2316 case LPAR:
2317 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002318 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002319 n_children = NCH(n);
2320 break;
2321 case import_as_names:
2322 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002323 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002324 n_children = NCH(n);
2325 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326 ast_error(n, "trailing comma not allowed without"
2327 " surrounding parentheses");
2328 return NULL;
2329 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002330 break;
2331 default:
2332 ast_error(n, "Unexpected node-type in from-import");
2333 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002334 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002336 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002337 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339
2340 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002341 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002342 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002343 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002344 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002345 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002347 else {
2348 for (i = 0; i < NCH(n); i += 2) {
2349 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2350 if (!import_alias)
2351 return NULL;
2352 asdl_seq_SET(aliases, i / 2, import_alias);
2353 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002355 if (mod != NULL)
2356 modname = mod->name;
2357 else
2358 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002359 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002360 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361 }
Neal Norwitz79792652005-11-14 04:25:03 +00002362 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002363 "unknown import statement: starts with command '%s'",
2364 STR(CHILD(n, 0)));
2365 return NULL;
2366}
2367
2368static stmt_ty
2369ast_for_global_stmt(struct compiling *c, const node *n)
2370{
2371 /* global_stmt: 'global' NAME (',' NAME)* */
2372 identifier name;
2373 asdl_seq *s;
2374 int i;
2375
2376 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002377 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 if (!s)
2379 return NULL;
2380 for (i = 1; i < NCH(n); i += 2) {
2381 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002382 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384 asdl_seq_SET(s, i / 2, name);
2385 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002386 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387}
2388
2389static stmt_ty
2390ast_for_exec_stmt(struct compiling *c, const node *n)
2391{
2392 expr_ty expr1, globals = NULL, locals = NULL;
2393 int n_children = NCH(n);
2394 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002395 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 "poorly formed 'exec' statement: %d parts to statement",
2397 n_children);
2398 return NULL;
2399 }
2400
2401 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2402 REQ(n, exec_stmt);
2403 expr1 = ast_for_expr(c, CHILD(n, 1));
2404 if (!expr1)
2405 return NULL;
2406 if (n_children >= 4) {
2407 globals = ast_for_expr(c, CHILD(n, 3));
2408 if (!globals)
2409 return NULL;
2410 }
2411 if (n_children == 6) {
2412 locals = ast_for_expr(c, CHILD(n, 5));
2413 if (!locals)
2414 return NULL;
2415 }
2416
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002417 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418}
2419
2420static stmt_ty
2421ast_for_assert_stmt(struct compiling *c, const node *n)
2422{
2423 /* assert_stmt: 'assert' test [',' test] */
2424 REQ(n, assert_stmt);
2425 if (NCH(n) == 2) {
2426 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2427 if (!expression)
2428 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002429 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 }
2431 else if (NCH(n) == 4) {
2432 expr_ty expr1, expr2;
2433
2434 expr1 = ast_for_expr(c, CHILD(n, 1));
2435 if (!expr1)
2436 return NULL;
2437 expr2 = ast_for_expr(c, CHILD(n, 3));
2438 if (!expr2)
2439 return NULL;
2440
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002441 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 }
Neal Norwitz79792652005-11-14 04:25:03 +00002443 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 "improper number of parts to 'assert' statement: %d",
2445 NCH(n));
2446 return NULL;
2447}
2448
2449static asdl_seq *
2450ast_for_suite(struct compiling *c, const node *n)
2451{
2452 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002453 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 stmt_ty s;
2455 int i, total, num, end, pos = 0;
2456 node *ch;
2457
2458 REQ(n, suite);
2459
2460 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002461 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462 if (!seq)
2463 return NULL;
2464 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2465 n = CHILD(n, 0);
2466 /* simple_stmt always ends with a NEWLINE,
2467 and may have a trailing SEMI
2468 */
2469 end = NCH(n) - 1;
2470 if (TYPE(CHILD(n, end - 1)) == SEMI)
2471 end--;
2472 /* loop by 2 to skip semi-colons */
2473 for (i = 0; i < end; i += 2) {
2474 ch = CHILD(n, i);
2475 s = ast_for_stmt(c, ch);
2476 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 asdl_seq_SET(seq, pos++, s);
2479 }
2480 }
2481 else {
2482 for (i = 2; i < (NCH(n) - 1); i++) {
2483 ch = CHILD(n, i);
2484 REQ(ch, stmt);
2485 num = num_stmts(ch);
2486 if (num == 1) {
2487 /* small_stmt or compound_stmt with only one child */
2488 s = ast_for_stmt(c, ch);
2489 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 asdl_seq_SET(seq, pos++, s);
2492 }
2493 else {
2494 int j;
2495 ch = CHILD(ch, 0);
2496 REQ(ch, simple_stmt);
2497 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002498 /* statement terminates with a semi-colon ';' */
2499 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002500 assert((j + 1) == NCH(ch));
2501 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002502 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 s = ast_for_stmt(c, CHILD(ch, j));
2504 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 asdl_seq_SET(seq, pos++, s);
2507 }
2508 }
2509 }
2510 }
2511 assert(pos == seq->size);
2512 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513}
2514
2515static stmt_ty
2516ast_for_if_stmt(struct compiling *c, const node *n)
2517{
2518 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2519 ['else' ':' suite]
2520 */
2521 char *s;
2522
2523 REQ(n, if_stmt);
2524
2525 if (NCH(n) == 4) {
2526 expr_ty expression;
2527 asdl_seq *suite_seq;
2528
2529 expression = ast_for_expr(c, CHILD(n, 1));
2530 if (!expression)
2531 return NULL;
2532 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002533 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 return NULL;
2535
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002536 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 s = STR(CHILD(n, 4));
2540 /* s[2], the third character in the string, will be
2541 's' for el_s_e, or
2542 'i' for el_i_f
2543 */
2544 if (s[2] == 's') {
2545 expr_ty expression;
2546 asdl_seq *seq1, *seq2;
2547
2548 expression = ast_for_expr(c, CHILD(n, 1));
2549 if (!expression)
2550 return NULL;
2551 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002552 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 return NULL;
2554 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002555 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 return NULL;
2557
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002558 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 }
2560 else if (s[2] == 'i') {
2561 int i, n_elif, has_else = 0;
2562 asdl_seq *orelse = NULL;
2563 n_elif = NCH(n) - 4;
2564 /* must reference the child n_elif+1 since 'else' token is third,
2565 not fourth, child from the end. */
2566 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2567 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2568 has_else = 1;
2569 n_elif -= 3;
2570 }
2571 n_elif /= 4;
2572
2573 if (has_else) {
2574 expr_ty expression;
2575 asdl_seq *seq1, *seq2;
2576
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002577 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 if (!orelse)
2579 return NULL;
2580 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002581 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002584 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002587 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589
2590 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002591 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002592 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 /* the just-created orelse handled the last elif */
2594 n_elif--;
2595 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596
2597 for (i = 0; i < n_elif; i++) {
2598 int off = 5 + (n_elif - i - 1) * 4;
2599 expr_ty expression;
2600 asdl_seq *suite_seq;
Anthony Baxtera863d332006-04-11 07:43:46 +00002601 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2602 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 return NULL;
2604 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002605 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002608 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610
Anthony Baxtera863d332006-04-11 07:43:46 +00002611 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002613 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Anthony Baxtera863d332006-04-11 07:43:46 +00002614 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 }
2616 return If(ast_for_expr(c, CHILD(n, 1)),
2617 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002618 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002620
2621 PyErr_Format(PyExc_SystemError,
2622 "unexpected token in 'if' statement: %s", s);
2623 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624}
2625
2626static stmt_ty
2627ast_for_while_stmt(struct compiling *c, const node *n)
2628{
2629 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2630 REQ(n, while_stmt);
2631
2632 if (NCH(n) == 4) {
2633 expr_ty expression;
2634 asdl_seq *suite_seq;
2635
2636 expression = ast_for_expr(c, CHILD(n, 1));
2637 if (!expression)
2638 return NULL;
2639 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002640 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002642 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 }
2644 else if (NCH(n) == 7) {
2645 expr_ty expression;
2646 asdl_seq *seq1, *seq2;
2647
2648 expression = ast_for_expr(c, CHILD(n, 1));
2649 if (!expression)
2650 return NULL;
2651 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002652 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 return NULL;
2654 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002655 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 return NULL;
2657
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002658 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002660
2661 PyErr_Format(PyExc_SystemError,
2662 "wrong number of tokens for 'while' statement: %d",
2663 NCH(n));
2664 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665}
2666
2667static stmt_ty
2668ast_for_for_stmt(struct compiling *c, const node *n)
2669{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002670 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 expr_ty expression;
2672 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002673 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2675 REQ(n, for_stmt);
2676
2677 if (NCH(n) == 9) {
2678 seq = ast_for_suite(c, CHILD(n, 8));
2679 if (!seq)
2680 return NULL;
2681 }
2682
Neal Norwitzedef2be2006-07-12 05:26:17 +00002683 node_target = CHILD(n, 1);
2684 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002685 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002687 /* Check the # of children rather than the length of _target, since
2688 for x, in ... has 1 element in _target, but still requires a Tuple. */
2689 if (NCH(node_target) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00002690 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002692 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002694 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002695 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 return NULL;
2697 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002698 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 return NULL;
2700
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002701 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2702 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703}
2704
2705static excepthandler_ty
2706ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2707{
2708 /* except_clause: 'except' [test [',' test]] */
2709 REQ(exc, except_clause);
2710 REQ(body, suite);
2711
2712 if (NCH(exc) == 1) {
2713 asdl_seq *suite_seq = ast_for_suite(c, body);
2714 if (!suite_seq)
2715 return NULL;
2716
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002717 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2718 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 }
2720 else if (NCH(exc) == 2) {
2721 expr_ty expression;
2722 asdl_seq *suite_seq;
2723
2724 expression = ast_for_expr(c, CHILD(exc, 1));
2725 if (!expression)
2726 return NULL;
2727 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002728 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 return NULL;
2730
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002731 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2732 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 }
2734 else if (NCH(exc) == 4) {
2735 asdl_seq *suite_seq;
2736 expr_ty expression;
2737 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2738 if (!e)
2739 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002740 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 return NULL;
2742 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002743 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744 return NULL;
2745 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002746 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 return NULL;
2748
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002749 return excepthandler(expression, e, suite_seq, LINENO(exc),
2750 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002752
2753 PyErr_Format(PyExc_SystemError,
2754 "wrong number of children for 'except' clause: %d",
2755 NCH(exc));
2756 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757}
2758
2759static stmt_ty
2760ast_for_try_stmt(struct compiling *c, const node *n)
2761{
Neal Norwitzf599f422005-12-17 21:33:47 +00002762 const int nch = NCH(n);
2763 int n_except = (nch - 3)/3;
2764 asdl_seq *body, *orelse = NULL, *finally = NULL;
2765
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 REQ(n, try_stmt);
2767
Neal Norwitzf599f422005-12-17 21:33:47 +00002768 body = ast_for_suite(c, CHILD(n, 2));
2769 if (body == NULL)
2770 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771
Neal Norwitzf599f422005-12-17 21:33:47 +00002772 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2773 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2774 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2775 /* we can assume it's an "else",
2776 because nch >= 9 for try-else-finally and
2777 it would otherwise have a type of except_clause */
2778 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2779 if (orelse == NULL)
2780 return NULL;
2781 n_except--;
2782 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783
Neal Norwitzf599f422005-12-17 21:33:47 +00002784 finally = ast_for_suite(c, CHILD(n, nch - 1));
2785 if (finally == NULL)
2786 return NULL;
2787 n_except--;
2788 }
2789 else {
2790 /* we can assume it's an "else",
2791 otherwise it would have a type of except_clause */
2792 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2793 if (orelse == NULL)
2794 return NULL;
2795 n_except--;
2796 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002798 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002799 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 return NULL;
2801 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002802
2803 if (n_except > 0) {
2804 int i;
2805 stmt_ty except_st;
2806 /* process except statements to create a try ... except */
2807 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2808 if (handlers == NULL)
2809 return NULL;
2810
2811 for (i = 0; i < n_except; i++) {
2812 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2813 CHILD(n, 5 + i * 3));
2814 if (!e)
2815 return NULL;
2816 asdl_seq_SET(handlers, i, e);
2817 }
2818
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002819 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2820 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002821 if (!finally)
2822 return except_st;
2823
2824 /* if a 'finally' is present too, we nest the TryExcept within a
2825 TryFinally to emulate try ... except ... finally */
2826 body = asdl_seq_new(1, c->c_arena);
2827 if (body == NULL)
2828 return NULL;
2829 asdl_seq_SET(body, 0, except_st);
2830 }
2831
2832 /* must be a try ... finally (except clauses are in body, if any exist) */
2833 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002834 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835}
2836
Guido van Rossumc2e20742006-02-27 22:32:47 +00002837static expr_ty
2838ast_for_with_var(struct compiling *c, const node *n)
2839{
2840 REQ(n, with_var);
2841 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2842 ast_error(n, "expected \"with [expr] as [var]\"");
2843 return NULL;
2844 }
2845 return ast_for_expr(c, CHILD(n, 1));
2846}
2847
2848/* with_stmt: 'with' test [ with_var ] ':' suite */
2849static stmt_ty
2850ast_for_with_stmt(struct compiling *c, const node *n)
2851{
2852 expr_ty context_expr, optional_vars = NULL;
2853 int suite_index = 3; /* skip 'with', test, and ':' */
2854 asdl_seq *suite_seq;
2855
2856 assert(TYPE(n) == with_stmt);
2857 context_expr = ast_for_expr(c, CHILD(n, 1));
2858 if (TYPE(CHILD(n, 2)) == with_var) {
2859 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2860
2861 if (!optional_vars) {
2862 return NULL;
2863 }
2864 if (!set_context(optional_vars, Store, n)) {
2865 return NULL;
2866 }
2867 suite_index = 4;
2868 }
2869
2870 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2871 if (!suite_seq) {
2872 return NULL;
2873 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002874 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2875 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002876}
2877
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878static stmt_ty
2879ast_for_classdef(struct compiling *c, const node *n)
2880{
2881 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 asdl_seq *bases, *s;
2883
2884 REQ(n, classdef);
2885
2886 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2887 ast_error(n, "assignment to None");
2888 return NULL;
2889 }
2890
2891 if (NCH(n) == 4) {
2892 s = ast_for_suite(c, CHILD(n, 3));
2893 if (!s)
2894 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002895 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2896 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 }
2898 /* check for empty base list */
2899 if (TYPE(CHILD(n,3)) == RPAR) {
2900 s = ast_for_suite(c, CHILD(n,5));
2901 if (!s)
2902 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002903 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2904 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 }
2906
2907 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002908 bases = ast_for_class_bases(c, CHILD(n, 3));
2909 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911
2912 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002913 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002915 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2916 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917}
2918
2919static stmt_ty
2920ast_for_stmt(struct compiling *c, const node *n)
2921{
2922 if (TYPE(n) == stmt) {
2923 assert(NCH(n) == 1);
2924 n = CHILD(n, 0);
2925 }
2926 if (TYPE(n) == simple_stmt) {
2927 assert(num_stmts(n) == 1);
2928 n = CHILD(n, 0);
2929 }
2930 if (TYPE(n) == small_stmt) {
2931 REQ(n, small_stmt);
2932 n = CHILD(n, 0);
2933 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2934 | flow_stmt | import_stmt | global_stmt | exec_stmt
2935 | assert_stmt
2936 */
2937 switch (TYPE(n)) {
2938 case expr_stmt:
2939 return ast_for_expr_stmt(c, n);
2940 case print_stmt:
2941 return ast_for_print_stmt(c, n);
2942 case del_stmt:
2943 return ast_for_del_stmt(c, n);
2944 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002945 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 case flow_stmt:
2947 return ast_for_flow_stmt(c, n);
2948 case import_stmt:
2949 return ast_for_import_stmt(c, n);
2950 case global_stmt:
2951 return ast_for_global_stmt(c, n);
2952 case exec_stmt:
2953 return ast_for_exec_stmt(c, n);
2954 case assert_stmt:
2955 return ast_for_assert_stmt(c, n);
2956 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002957 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2959 TYPE(n), NCH(n));
2960 return NULL;
2961 }
2962 }
2963 else {
2964 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2965 | funcdef | classdef
2966 */
2967 node *ch = CHILD(n, 0);
2968 REQ(n, compound_stmt);
2969 switch (TYPE(ch)) {
2970 case if_stmt:
2971 return ast_for_if_stmt(c, ch);
2972 case while_stmt:
2973 return ast_for_while_stmt(c, ch);
2974 case for_stmt:
2975 return ast_for_for_stmt(c, ch);
2976 case try_stmt:
2977 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002978 case with_stmt:
2979 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 case funcdef:
2981 return ast_for_funcdef(c, ch);
2982 case classdef:
2983 return ast_for_classdef(c, ch);
2984 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002985 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2987 TYPE(n), NCH(n));
2988 return NULL;
2989 }
2990 }
2991}
2992
2993static PyObject *
2994parsenumber(const char *s)
2995{
2996 const char *end;
2997 long x;
2998 double dx;
2999#ifndef WITHOUT_COMPLEX
3000 Py_complex c;
3001 int imflag;
3002#endif
3003
3004 errno = 0;
3005 end = s + strlen(s) - 1;
3006#ifndef WITHOUT_COMPLEX
3007 imflag = *end == 'j' || *end == 'J';
3008#endif
3009 if (*end == 'l' || *end == 'L')
3010 return PyLong_FromString((char *)s, (char **)0, 0);
3011 if (s[0] == '0') {
3012 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3013 if (x < 0 && errno == 0) {
3014 return PyLong_FromString((char *)s,
3015 (char **)0,
3016 0);
3017 }
3018 }
3019 else
3020 x = PyOS_strtol((char *)s, (char **)&end, 0);
3021 if (*end == '\0') {
3022 if (errno != 0)
3023 return PyLong_FromString((char *)s, (char **)0, 0);
3024 return PyInt_FromLong(x);
3025 }
3026 /* XXX Huge floats may silently fail */
3027#ifndef WITHOUT_COMPLEX
3028 if (imflag) {
3029 c.real = 0.;
3030 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003031 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 PyFPE_END_PROTECT(c)
3033 return PyComplex_FromCComplex(c);
3034 }
3035 else
3036#endif
3037 {
3038 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003039 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 PyFPE_END_PROTECT(dx)
3041 return PyFloat_FromDouble(dx);
3042 }
3043}
3044
3045static PyObject *
3046decode_utf8(const char **sPtr, const char *end, char* encoding)
3047{
3048#ifndef Py_USING_UNICODE
3049 Py_FatalError("decode_utf8 should not be called in this build.");
3050 return NULL;
3051#else
3052 PyObject *u, *v;
3053 char *s, *t;
3054 t = s = (char *)*sPtr;
3055 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3056 while (s < end && (*s & 0x80)) s++;
3057 *sPtr = s;
3058 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3059 if (u == NULL)
3060 return NULL;
3061 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3062 Py_DECREF(u);
3063 return v;
3064#endif
3065}
3066
3067static PyObject *
3068decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3069{
3070 PyObject *v, *u;
3071 char *buf;
3072 char *p;
3073 const char *end;
3074 if (encoding == NULL) {
3075 buf = (char *)s;
3076 u = NULL;
3077 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3078 buf = (char *)s;
3079 u = NULL;
3080 } else {
3081 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3082 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3083 if (u == NULL)
3084 return NULL;
3085 p = buf = PyString_AsString(u);
3086 end = s + len;
3087 while (s < end) {
3088 if (*s == '\\') {
3089 *p++ = *s++;
3090 if (*s & 0x80) {
3091 strcpy(p, "u005c");
3092 p += 5;
3093 }
3094 }
3095 if (*s & 0x80) { /* XXX inefficient */
3096 PyObject *w;
3097 char *r;
Martin v. Löwis66851282006-04-22 11:40:03 +00003098 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003099 w = decode_utf8(&s, end, "utf-16-be");
3100 if (w == NULL) {
3101 Py_DECREF(u);
3102 return NULL;
3103 }
3104 r = PyString_AsString(w);
3105 rn = PyString_Size(w);
3106 assert(rn % 2 == 0);
3107 for (i = 0; i < rn; i += 2) {
3108 sprintf(p, "\\u%02x%02x",
3109 r[i + 0] & 0xFF,
3110 r[i + 1] & 0xFF);
3111 p += 6;
3112 }
3113 Py_DECREF(w);
3114 } else {
3115 *p++ = *s++;
3116 }
3117 }
3118 len = p - buf;
3119 s = buf;
3120 }
3121 if (rawmode)
3122 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3123 else
3124 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3125 Py_XDECREF(u);
3126 return v;
3127}
3128
3129/* s is a Python string literal, including the bracketing quote characters,
3130 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3131 * parsestr parses it, and returns the decoded Python string object.
3132 */
3133static PyObject *
3134parsestr(const char *s, const char *encoding)
3135{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003137 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 int rawmode = 0;
3139 int need_encoding;
3140 int unicode = 0;
3141
3142 if (isalpha(quote) || quote == '_') {
3143 if (quote == 'u' || quote == 'U') {
3144 quote = *++s;
3145 unicode = 1;
3146 }
3147 if (quote == 'r' || quote == 'R') {
3148 quote = *++s;
3149 rawmode = 1;
3150 }
3151 }
3152 if (quote != '\'' && quote != '\"') {
3153 PyErr_BadInternalCall();
3154 return NULL;
3155 }
3156 s++;
3157 len = strlen(s);
3158 if (len > INT_MAX) {
3159 PyErr_SetString(PyExc_OverflowError,
3160 "string to parse is too long");
3161 return NULL;
3162 }
3163 if (s[--len] != quote) {
3164 PyErr_BadInternalCall();
3165 return NULL;
3166 }
3167 if (len >= 4 && s[0] == quote && s[1] == quote) {
3168 s += 2;
3169 len -= 2;
3170 if (s[--len] != quote || s[--len] != quote) {
3171 PyErr_BadInternalCall();
3172 return NULL;
3173 }
3174 }
3175#ifdef Py_USING_UNICODE
3176 if (unicode || Py_UnicodeFlag) {
3177 return decode_unicode(s, len, rawmode, encoding);
3178 }
3179#endif
3180 need_encoding = (encoding != NULL &&
3181 strcmp(encoding, "utf-8") != 0 &&
3182 strcmp(encoding, "iso-8859-1") != 0);
3183 if (rawmode || strchr(s, '\\') == NULL) {
3184 if (need_encoding) {
3185#ifndef Py_USING_UNICODE
3186 /* This should not happen - we never see any other
3187 encoding. */
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003188 Py_FatalError(
3189 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003191 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 if (u == NULL)
3193 return NULL;
3194 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3195 Py_DECREF(u);
3196 return v;
3197#endif
3198 } else {
3199 return PyString_FromStringAndSize(s, len);
3200 }
3201 }
3202
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003203 return PyString_DecodeEscape(s, len, NULL, unicode,
3204 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205}
3206
3207/* Build a Python string object out of a STRING atom. This takes care of
3208 * compile-time literal catenation, calling parsestr() on each piece, and
3209 * pasting the intermediate results together.
3210 */
3211static PyObject *
3212parsestrplus(struct compiling *c, const node *n)
3213{
3214 PyObject *v;
3215 int i;
3216 REQ(CHILD(n, 0), STRING);
3217 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3218 /* String literal concatenation */
3219 for (i = 1; i < NCH(n); i++) {
3220 PyObject *s;
3221 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3222 if (s == NULL)
3223 goto onError;
3224 if (PyString_Check(v) && PyString_Check(s)) {
3225 PyString_ConcatAndDel(&v, s);
3226 if (v == NULL)
3227 goto onError;
3228 }
3229#ifdef Py_USING_UNICODE
3230 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003231 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 Py_DECREF(v);
3234 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003235 if (v == NULL)
3236 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 }
3238#endif
3239 }
3240 }
3241 return v;
3242
3243 onError:
3244 Py_XDECREF(v);
3245 return NULL;
3246}