blob: b566ba3e390844f69c19341bb9f8356c49985038 [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 *);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 }
Thomas Wouters477c8d52006-05-27 19:21:47 +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";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters0e3f5912006-08-11 14:57:12 +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;
Thomas Wouters0e3f5912006-08-11 14:57:12 +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;
Thomas Wouters477c8d52006-05-27 19:21:47 +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++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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{
Guido van Rossumb053cd82006-08-24 03:53:23 +0000481 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 |'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));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000625 return NULL; /* Don't need to goto error; no objects 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)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000628 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629
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) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000644 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
645 if (!expression)
646 goto error;
647 assert(defaults != NULL);
648 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 i += 2;
650 found_default = 1;
651 }
652 else if (found_default) {
653 ast_error(n,
654 "non-default argument follows default argument");
655 goto error;
656 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657 if (NCH(ch) == 3) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658 ch = CHILD(ch, 1);
659 /* def foo((x)): is not complex, special case. */
660 if (NCH(ch) != 1) {
661 /* We have complex arguments, setup for unpacking. */
662 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
663 } else {
664 /* def foo((x)): setup for checking NAME below. */
665 ch = CHILD(ch, 0);
666 }
667 }
668 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000669 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
671 ast_error(CHILD(ch, 0), "assignment to None");
672 goto error;
673 }
Armin Rigo31441302005-10-21 12:57:31 +0000674 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 Param, LINENO(ch), ch->n_col_offset,
676 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 if (!name)
678 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000679 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680
681 }
682 i += 2; /* the name and the comma */
683 break;
684 case STAR:
685 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
686 ast_error(CHILD(n, i+1), "assignment to None");
687 goto error;
688 }
689 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
690 i += 3;
691 break;
692 case DOUBLESTAR:
693 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
694 ast_error(CHILD(n, i+1), "assignment to None");
695 goto error;
696 }
697 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
698 i += 3;
699 break;
700 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000701 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 "unexpected node in varargslist: %d @ %d",
703 TYPE(ch), i);
704 goto error;
705 }
706 }
707
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000708 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709
710 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000711 Py_XDECREF(vararg);
712 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713 return NULL;
714}
715
716static expr_ty
717ast_for_dotted_name(struct compiling *c, const node *n)
718{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000719 expr_ty e;
720 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000721 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 int i;
723
724 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000725
726 lineno = LINENO(n);
727 col_offset = n->n_col_offset;
728
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 id = NEW_IDENTIFIER(CHILD(n, 0));
730 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000731 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000732 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735
736 for (i = 2; i < NCH(n); i+=2) {
737 id = NEW_IDENTIFIER(CHILD(n, i));
738 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000739 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000740 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000741 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 }
744
745 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746}
747
748static expr_ty
749ast_for_decorator(struct compiling *c, const node *n)
750{
751 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
752 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000753 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
755 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000756 REQ(CHILD(n, 0), AT);
757 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
759 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
760 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000761 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762
763 if (NCH(n) == 3) { /* No arguments */
764 d = name_expr;
765 name_expr = NULL;
766 }
767 else if (NCH(n) == 5) { /* Call with no arguments */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
769 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 name_expr = NULL;
773 }
774 else {
775 d = ast_for_call(c, CHILD(n, 3), name_expr);
776 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 name_expr = NULL;
779 }
780
781 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782}
783
784static asdl_seq*
785ast_for_decorators(struct compiling *c, const node *n)
786{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000787 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000788 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 int i;
790
791 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000792 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 if (!decorator_seq)
794 return NULL;
795
796 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000797 d = ast_for_decorator(c, CHILD(n, i));
798 if (!d)
799 return NULL;
800 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 }
802 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803}
804
805static stmt_ty
806ast_for_funcdef(struct compiling *c, const node *n)
807{
808 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000809 identifier name;
810 arguments_ty args;
811 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 asdl_seq *decorator_seq = NULL;
813 int name_i;
814
815 REQ(n, funcdef);
816
817 if (NCH(n) == 6) { /* decorators are present */
818 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
819 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 name_i = 2;
822 }
823 else {
824 name_i = 1;
825 }
826
827 name = NEW_IDENTIFIER(CHILD(n, name_i));
828 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000829 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000831 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000832 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 }
834 args = ast_for_arguments(c, CHILD(n, name_i + 1));
835 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 body = ast_for_suite(c, CHILD(n, name_i + 3));
838 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000839 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000841 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
842 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843}
844
845static expr_ty
846ast_for_lambdef(struct compiling *c, const node *n)
847{
848 /* lambdef: 'lambda' [varargslist] ':' test */
849 arguments_ty args;
850 expr_ty expression;
851
852 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000853 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 if (!args)
855 return NULL;
856 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000857 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 }
860 else {
861 args = ast_for_arguments(c, CHILD(n, 1));
862 if (!args)
863 return NULL;
864 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000865 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 }
868
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000869 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870}
871
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000872static expr_ty
873ast_for_ifexpr(struct compiling *c, const node *n)
874{
875 /* test: or_test 'if' or_test 'else' test */
876 expr_ty expression, body, orelse;
877
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000878 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000879 body = ast_for_expr(c, CHILD(n, 0));
880 if (!body)
881 return NULL;
882 expression = ast_for_expr(c, CHILD(n, 2));
883 if (!expression)
884 return NULL;
885 orelse = ast_for_expr(c, CHILD(n, 4));
886 if (!orelse)
887 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000888 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
889 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000890}
891
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892/* Count the number of 'for' loop in a list comprehension.
893
894 Helper for ast_for_listcomp().
895*/
896
897static int
898count_list_fors(const node *n)
899{
900 int n_fors = 0;
901 node *ch = CHILD(n, 1);
902
903 count_list_for:
904 n_fors++;
905 REQ(ch, list_for);
906 if (NCH(ch) == 5)
907 ch = CHILD(ch, 4);
908 else
909 return n_fors;
910 count_list_iter:
911 REQ(ch, list_iter);
912 ch = CHILD(ch, 0);
913 if (TYPE(ch) == list_for)
914 goto count_list_for;
915 else if (TYPE(ch) == list_if) {
916 if (NCH(ch) == 3) {
917 ch = CHILD(ch, 2);
918 goto count_list_iter;
919 }
920 else
921 return n_fors;
922 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000923
924 /* Should never be reached */
925 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
926 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927}
928
929/* Count the number of 'if' statements in a list comprehension.
930
931 Helper for ast_for_listcomp().
932*/
933
934static int
935count_list_ifs(const node *n)
936{
937 int n_ifs = 0;
938
939 count_list_iter:
940 REQ(n, list_iter);
941 if (TYPE(CHILD(n, 0)) == list_for)
942 return n_ifs;
943 n = CHILD(n, 0);
944 REQ(n, list_if);
945 n_ifs++;
946 if (NCH(n) == 2)
947 return n_ifs;
948 n = CHILD(n, 2);
949 goto count_list_iter;
950}
951
952static expr_ty
953ast_for_listcomp(struct compiling *c, const node *n)
954{
955 /* listmaker: test ( list_for | (',' test)* [','] )
956 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
957 list_iter: list_for | list_if
958 list_if: 'if' test [list_iter]
959 testlist_safe: test [(',' test)+ [',']]
960 */
961 expr_ty elt;
962 asdl_seq *listcomps;
963 int i, n_fors;
964 node *ch;
965
966 REQ(n, listmaker);
967 assert(NCH(n) > 1);
968
969 elt = ast_for_expr(c, CHILD(n, 0));
970 if (!elt)
971 return NULL;
972
973 n_fors = count_list_fors(n);
974 if (n_fors == -1)
975 return NULL;
976
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000977 listcomps = asdl_seq_new(n_fors, c->c_arena);
978 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 ch = CHILD(n, 1);
982 for (i = 0; i < n_fors; i++) {
983 comprehension_ty lc;
984 asdl_seq *t;
985 expr_ty expression;
986
987 REQ(ch, list_for);
988
989 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000990 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000992 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000993 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000996 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000997 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000998 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001000 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1001 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001002 expression, NULL, c->c_arena);
1003 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005
1006 if (NCH(ch) == 5) {
1007 int j, n_ifs;
1008 asdl_seq *ifs;
1009
1010 ch = CHILD(ch, 4);
1011 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001012 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001015 ifs = asdl_seq_new(n_ifs, c->c_arena);
1016 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018
1019 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001020 REQ(ch, list_iter);
1021 ch = CHILD(ch, 0);
1022 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023
Jeremy Hyltona8293132006-02-28 17:58:27 +00001024 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1025 if (NCH(ch) == 3)
1026 ch = CHILD(ch, 2);
1027 }
1028 /* on exit, must guarantee that ch is a list_for */
1029 if (TYPE(ch) == list_iter)
1030 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001032 }
1033 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 }
1035
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001036 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037}
1038
1039/*
1040 Count the number of 'for' loops in a generator expression.
1041
1042 Helper for ast_for_genexp().
1043*/
1044
1045static int
1046count_gen_fors(const node *n)
1047{
1048 int n_fors = 0;
1049 node *ch = CHILD(n, 1);
1050
1051 count_gen_for:
1052 n_fors++;
1053 REQ(ch, gen_for);
1054 if (NCH(ch) == 5)
1055 ch = CHILD(ch, 4);
1056 else
1057 return n_fors;
1058 count_gen_iter:
1059 REQ(ch, gen_iter);
1060 ch = CHILD(ch, 0);
1061 if (TYPE(ch) == gen_for)
1062 goto count_gen_for;
1063 else if (TYPE(ch) == gen_if) {
1064 if (NCH(ch) == 3) {
1065 ch = CHILD(ch, 2);
1066 goto count_gen_iter;
1067 }
1068 else
1069 return n_fors;
1070 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001071
1072 /* Should never be reached */
1073 PyErr_SetString(PyExc_SystemError,
1074 "logic error in count_gen_fors");
1075 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076}
1077
1078/* Count the number of 'if' statements in a generator expression.
1079
1080 Helper for ast_for_genexp().
1081*/
1082
1083static int
1084count_gen_ifs(const node *n)
1085{
1086 int n_ifs = 0;
1087
1088 while (1) {
1089 REQ(n, gen_iter);
1090 if (TYPE(CHILD(n, 0)) == gen_for)
1091 return n_ifs;
1092 n = CHILD(n, 0);
1093 REQ(n, gen_if);
1094 n_ifs++;
1095 if (NCH(n) == 2)
1096 return n_ifs;
1097 n = CHILD(n, 2);
1098 }
1099}
1100
Jeremy Hyltona8293132006-02-28 17:58:27 +00001101/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102static expr_ty
1103ast_for_genexp(struct compiling *c, const node *n)
1104{
1105 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1106 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1107 expr_ty elt;
1108 asdl_seq *genexps;
1109 int i, n_fors;
1110 node *ch;
1111
1112 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1113 assert(NCH(n) > 1);
1114
1115 elt = ast_for_expr(c, CHILD(n, 0));
1116 if (!elt)
1117 return NULL;
1118
1119 n_fors = count_gen_fors(n);
1120 if (n_fors == -1)
1121 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001122
1123 genexps = asdl_seq_new(n_fors, c->c_arena);
1124 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 ch = CHILD(n, 1);
1128 for (i = 0; i < n_fors; i++) {
1129 comprehension_ty ge;
1130 asdl_seq *t;
1131 expr_ty expression;
1132
1133 REQ(ch, gen_for);
1134
1135 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001136 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001138 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001139 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001141
1142 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001144 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1147 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001148 expression, NULL, c->c_arena);
1149
1150 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 if (NCH(ch) == 5) {
1154 int j, n_ifs;
1155 asdl_seq *ifs;
1156
1157 ch = CHILD(ch, 4);
1158 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001161
1162 ifs = asdl_seq_new(n_ifs, c->c_arena);
1163 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001165
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 for (j = 0; j < n_ifs; j++) {
1167 REQ(ch, gen_iter);
1168 ch = CHILD(ch, 0);
1169 REQ(ch, gen_if);
1170
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001171 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001172 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001173 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001174 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 if (NCH(ch) == 3)
1176 ch = CHILD(ch, 2);
1177 }
1178 /* on exit, must guarantee that ch is a gen_for */
1179 if (TYPE(ch) == gen_iter)
1180 ch = CHILD(ch, 0);
1181 ge->ifs = ifs;
1182 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001183 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 }
1185
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001186 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187}
1188
1189static expr_ty
1190ast_for_atom(struct compiling *c, const node *n)
1191{
1192 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
Brett Cannoncf588f62006-08-25 04:28:18 +00001193 | '{' [dictmaker] '}' | NAME | NUMBER | STRING+
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 */
1195 node *ch = CHILD(n, 0);
1196
1197 switch (TYPE(ch)) {
1198 case NAME:
1199 /* All names start in Load context, but may later be
1200 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001201 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 case STRING: {
1203 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 if (!str)
1205 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001206
1207 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001208 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 }
1210 case NUMBER: {
1211 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 if (!pynum)
1213 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001214
1215 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001216 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 }
1218 case LPAR: /* some parenthesized expressions */
1219 ch = CHILD(n, 1);
1220
1221 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001222 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223
1224 if (TYPE(ch) == yield_expr)
1225 return ast_for_expr(c, ch);
1226
1227 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1228 return ast_for_genexp(c, ch);
1229
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001230 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 case LSQB: /* list (or list comprehension) */
1232 ch = CHILD(n, 1);
1233
1234 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001235 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236
1237 REQ(ch, listmaker);
1238 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1239 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 if (!elts)
1241 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001242
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001243 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 }
1245 else
1246 return ast_for_listcomp(c, ch);
1247 case LBRACE: {
1248 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1249 int i, size;
1250 asdl_seq *keys, *values;
1251
1252 ch = CHILD(n, 1);
1253 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001254 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 if (!keys)
1256 return NULL;
1257
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001258 values = asdl_seq_new(size, c->c_arena);
1259 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261
1262 for (i = 0; i < NCH(ch); i += 4) {
1263 expr_ty expression;
1264
1265 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001266 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001268
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001270
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001272 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001274
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 asdl_seq_SET(values, i / 4, expression);
1276 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001277 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001280 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 return NULL;
1282 }
1283}
1284
1285static slice_ty
1286ast_for_slice(struct compiling *c, const node *n)
1287{
1288 node *ch;
1289 expr_ty lower = NULL, upper = NULL, step = NULL;
1290
1291 REQ(n, subscript);
1292
1293 /*
1294 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1295 sliceop: ':' [test]
1296 */
1297 ch = CHILD(n, 0);
1298 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001299 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300
1301 if (NCH(n) == 1 && TYPE(ch) == test) {
1302 /* 'step' variable hold no significance in terms of being used over
1303 other vars */
1304 step = ast_for_expr(c, ch);
1305 if (!step)
1306 return NULL;
1307
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001308 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 }
1310
1311 if (TYPE(ch) == test) {
1312 lower = ast_for_expr(c, ch);
1313 if (!lower)
1314 return NULL;
1315 }
1316
1317 /* If there's an upper bound it's in the second or third position. */
1318 if (TYPE(ch) == COLON) {
1319 if (NCH(n) > 1) {
1320 node *n2 = CHILD(n, 1);
1321
1322 if (TYPE(n2) == test) {
1323 upper = ast_for_expr(c, n2);
1324 if (!upper)
1325 return NULL;
1326 }
1327 }
1328 } else if (NCH(n) > 2) {
1329 node *n2 = CHILD(n, 2);
1330
1331 if (TYPE(n2) == test) {
1332 upper = ast_for_expr(c, n2);
1333 if (!upper)
1334 return NULL;
1335 }
1336 }
1337
1338 ch = CHILD(n, NCH(n) - 1);
1339 if (TYPE(ch) == sliceop) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001340 if (NCH(ch) == 1) {
1341 /* No expression, so step is None */
1342 ch = CHILD(ch, 0);
1343 step = Name(new_identifier("None", c->c_arena), Load,
1344 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 if (!step)
1346 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001347 } else {
1348 ch = CHILD(ch, 1);
1349 if (TYPE(ch) == test) {
1350 step = ast_for_expr(c, ch);
1351 if (!step)
1352 return NULL;
1353 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 }
1355 }
1356
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001357 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358}
1359
1360static expr_ty
1361ast_for_binop(struct compiling *c, const node *n)
1362{
1363 /* Must account for a sequence of expressions.
1364 How should A op B op C by represented?
1365 BinOp(BinOp(A, op, B), op, C).
1366 */
1367
1368 int i, nops;
1369 expr_ty expr1, expr2, result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371
1372 expr1 = ast_for_expr(c, CHILD(n, 0));
1373 if (!expr1)
1374 return NULL;
1375
1376 expr2 = ast_for_expr(c, CHILD(n, 2));
1377 if (!expr2)
1378 return NULL;
1379
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001380 newoperator = get_operator(CHILD(n, 1));
1381 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 return NULL;
1383
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001384 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1385 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 if (!result)
1387 return NULL;
1388
1389 nops = (NCH(n) - 1) / 2;
1390 for (i = 1; i < nops; i++) {
1391 expr_ty tmp_result, tmp;
1392 const node* next_oper = CHILD(n, i * 2 + 1);
1393
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001394 newoperator = get_operator(next_oper);
1395 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 return NULL;
1397
1398 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1399 if (!tmp)
1400 return NULL;
1401
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001402 tmp_result = BinOp(result, newoperator, tmp,
1403 LINENO(next_oper), next_oper->n_col_offset,
1404 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 if (!tmp)
1406 return NULL;
1407 result = tmp_result;
1408 }
1409 return result;
1410}
1411
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001412static expr_ty
1413ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1414{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001415 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1416 subscriptlist: subscript (',' subscript)* [',']
1417 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1418 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001419 REQ(n, trailer);
1420 if (TYPE(CHILD(n, 0)) == LPAR) {
1421 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001422 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1423 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001424 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001425 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001426 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001427 else if (TYPE(CHILD(n, 0)) == DOT ) {
1428 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001429 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001430 }
1431 else {
1432 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001433 REQ(CHILD(n, 2), RSQB);
1434 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001435 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001436 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1437 if (!slc)
1438 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001439 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1440 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001441 }
1442 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001443 /* The grammar is ambiguous here. The ambiguity is resolved
1444 by treating the sequence as a tuple literal if there are
1445 no slice features.
1446 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001447 int j;
1448 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001449 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001450 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001451 asdl_seq *slices, *elts;
1452 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001453 if (!slices)
1454 return NULL;
1455 for (j = 0; j < NCH(n); j += 2) {
1456 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001457 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001458 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001459 if (slc->kind != Index_kind)
1460 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001461 asdl_seq_SET(slices, j / 2, slc);
1462 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001463 if (!simple) {
1464 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001465 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001466 }
1467 /* extract Index values and put them in a Tuple */
1468 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001469 if (!elts)
1470 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001471 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1472 slc = (slice_ty)asdl_seq_GET(slices, j);
1473 assert(slc->kind == Index_kind && slc->v.Index.value);
1474 asdl_seq_SET(elts, j, slc->v.Index.value);
1475 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001476 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001477 if (!e)
1478 return NULL;
1479 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001480 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001481 }
1482 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001483}
1484
1485static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001486ast_for_factor(struct compiling *c, const node *n)
1487{
1488 node *pfactor, *ppower, *patom, *pnum;
1489 expr_ty expression;
1490
1491 /* If the unary - operator is applied to a constant, don't generate
1492 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1493 constant. The peephole optimizer already does something like
1494 this but it doesn't handle the case where the constant is
1495 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1496 PyLongObject.
1497 */
1498 if (TYPE(CHILD(n, 0)) == MINUS
1499 && NCH(n) == 2
1500 && TYPE((pfactor = CHILD(n, 1))) == factor
1501 && NCH(pfactor) == 1
1502 && TYPE((ppower = CHILD(pfactor, 0))) == power
1503 && NCH(ppower) == 1
1504 && TYPE((patom = CHILD(ppower, 0))) == atom
1505 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1506 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1507 if (s == NULL)
1508 return NULL;
1509 s[0] = '-';
1510 strcpy(s + 1, STR(pnum));
1511 PyObject_FREE(STR(pnum));
1512 STR(pnum) = s;
1513 return ast_for_atom(c, patom);
1514 }
1515
1516 expression = ast_for_expr(c, CHILD(n, 1));
1517 if (!expression)
1518 return NULL;
1519
1520 switch (TYPE(CHILD(n, 0))) {
1521 case PLUS:
1522 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1523 c->c_arena);
1524 case MINUS:
1525 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1526 c->c_arena);
1527 case TILDE:
1528 return UnaryOp(Invert, expression, LINENO(n),
1529 n->n_col_offset, c->c_arena);
1530 }
1531 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1532 TYPE(CHILD(n, 0)));
1533 return NULL;
1534}
1535
1536static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001537ast_for_power(struct compiling *c, const node *n)
1538{
1539 /* power: atom trailer* ('**' factor)*
1540 */
1541 int i;
1542 expr_ty e, tmp;
1543 REQ(n, power);
1544 e = ast_for_atom(c, CHILD(n, 0));
1545 if (!e)
1546 return NULL;
1547 if (NCH(n) == 1)
1548 return e;
1549 for (i = 1; i < NCH(n); i++) {
1550 node *ch = CHILD(n, i);
1551 if (TYPE(ch) != trailer)
1552 break;
1553 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001554 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001555 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001556 tmp->lineno = e->lineno;
1557 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001558 e = tmp;
1559 }
1560 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1561 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001562 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001563 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001564 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001565 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001566 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001567 e = tmp;
1568 }
1569 return e;
1570}
1571
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572/* Do not name a variable 'expr'! Will cause a compile error.
1573*/
1574
1575static expr_ty
1576ast_for_expr(struct compiling *c, const node *n)
1577{
1578 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001579 test: or_test ['if' or_test 'else' test] | lambdef
1580 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 and_test: not_test ('and' not_test)*
1582 not_test: 'not' not_test | comparison
1583 comparison: expr (comp_op expr)*
1584 expr: xor_expr ('|' xor_expr)*
1585 xor_expr: and_expr ('^' and_expr)*
1586 and_expr: shift_expr ('&' shift_expr)*
1587 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1588 arith_expr: term (('+'|'-') term)*
1589 term: factor (('*'|'/'|'%'|'//') factor)*
1590 factor: ('+'|'-'|'~') factor | power
1591 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001592
1593 As well as modified versions that exist for backward compatibility,
1594 to explicitly allow:
1595 [ x for x in lambda: 0, lambda: 1 ]
1596 (which would be ambiguous without these extra rules)
1597
1598 old_test: or_test | old_lambdef
1599 old_lambdef: 'lambda' [vararglist] ':' old_test
1600
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601 */
1602
1603 asdl_seq *seq;
1604 int i;
1605
1606 loop:
1607 switch (TYPE(n)) {
1608 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001609 case old_test:
1610 if (TYPE(CHILD(n, 0)) == lambdef ||
1611 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001613 else if (NCH(n) > 1)
1614 return ast_for_ifexpr(c, n);
1615 /* Fallthrough */
1616 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 case and_test:
1618 if (NCH(n) == 1) {
1619 n = CHILD(n, 0);
1620 goto loop;
1621 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001622 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 if (!seq)
1624 return NULL;
1625 for (i = 0; i < NCH(n); i += 2) {
1626 expr_ty e = ast_for_expr(c, CHILD(n, i));
1627 if (!e)
1628 return NULL;
1629 asdl_seq_SET(seq, i / 2, e);
1630 }
1631 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001632 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1633 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001634 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001635 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 case not_test:
1637 if (NCH(n) == 1) {
1638 n = CHILD(n, 0);
1639 goto loop;
1640 }
1641 else {
1642 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1643 if (!expression)
1644 return NULL;
1645
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001646 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1647 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 }
1649 case comparison:
1650 if (NCH(n) == 1) {
1651 n = CHILD(n, 0);
1652 goto loop;
1653 }
1654 else {
1655 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001656 asdl_int_seq *ops;
1657 asdl_seq *cmps;
1658 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 if (!ops)
1660 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001661 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663 return NULL;
1664 }
1665 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001666 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001668 newoperator = ast_for_comp_op(CHILD(n, i));
1669 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001671 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672
1673 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001674 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001676 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001678 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 asdl_seq_SET(cmps, i / 2, expression);
1680 }
1681 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001682 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001684 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001686 return Compare(expression, ops, cmps, LINENO(n),
1687 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 }
1689 break;
1690
1691 /* The next five cases all handle BinOps. The main body of code
1692 is the same in each case, but the switch turned inside out to
1693 reuse the code for each type of operator.
1694 */
1695 case expr:
1696 case xor_expr:
1697 case and_expr:
1698 case shift_expr:
1699 case arith_expr:
1700 case term:
1701 if (NCH(n) == 1) {
1702 n = CHILD(n, 0);
1703 goto loop;
1704 }
1705 return ast_for_binop(c, n);
1706 case yield_expr: {
1707 expr_ty exp = NULL;
1708 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001709 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 if (!exp)
1711 return NULL;
1712 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001713 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001715 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 if (NCH(n) == 1) {
1717 n = CHILD(n, 0);
1718 goto loop;
1719 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001720 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001721 case power:
1722 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001724 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 return NULL;
1726 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001727 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 return NULL;
1729}
1730
1731static expr_ty
1732ast_for_call(struct compiling *c, const node *n, expr_ty func)
1733{
1734 /*
1735 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1736 | '**' test)
1737 argument: [test '='] test [gen_for] # Really [keyword '='] test
1738 */
1739
1740 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001741 asdl_seq *args;
1742 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 expr_ty vararg = NULL, kwarg = NULL;
1744
1745 REQ(n, arglist);
1746
1747 nargs = 0;
1748 nkeywords = 0;
1749 ngens = 0;
1750 for (i = 0; i < NCH(n); i++) {
1751 node *ch = CHILD(n, i);
1752 if (TYPE(ch) == argument) {
1753 if (NCH(ch) == 1)
1754 nargs++;
1755 else if (TYPE(CHILD(ch, 1)) == gen_for)
1756 ngens++;
1757 else
1758 nkeywords++;
1759 }
1760 }
1761 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001762 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 "if not sole argument");
1764 return NULL;
1765 }
1766
1767 if (nargs + nkeywords + ngens > 255) {
1768 ast_error(n, "more than 255 arguments");
1769 return NULL;
1770 }
1771
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001772 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001774 return NULL;
1775 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778 nargs = 0;
1779 nkeywords = 0;
1780 for (i = 0; i < NCH(n); i++) {
1781 node *ch = CHILD(n, i);
1782 if (TYPE(ch) == argument) {
1783 expr_ty e;
1784 if (NCH(ch) == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001785 if (nkeywords) {
1786 ast_error(CHILD(ch, 0),
1787 "non-keyword arg after keyword arg");
1788 return NULL;
1789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 e = ast_for_expr(c, CHILD(ch, 0));
1791 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001792 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 asdl_seq_SET(args, nargs++, e);
1794 }
1795 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1796 e = ast_for_genexp(c, ch);
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 {
1802 keyword_ty kw;
1803 identifier key;
1804
1805 /* CHILD(ch, 0) is test, but must be an identifier? */
1806 e = ast_for_expr(c, CHILD(ch, 0));
1807 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001808 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 /* f(lambda x: x[0] = 3) ends up getting parsed with
1810 * LHS test = lambda x: x[0], and RHS test = 3.
1811 * SF bug 132313 points out that complaining about a keyword
1812 * then is very confusing.
1813 */
1814 if (e->kind == Lambda_kind) {
1815 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 } else if (e->kind != Name_kind) {
1818 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001819 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 }
1821 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 e = ast_for_expr(c, CHILD(ch, 2));
1823 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001824 return NULL;
1825 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 asdl_seq_SET(keywords, nkeywords++, kw);
1829 }
1830 }
1831 else if (TYPE(ch) == STAR) {
1832 vararg = ast_for_expr(c, CHILD(n, i+1));
1833 i++;
1834 }
1835 else if (TYPE(ch) == DOUBLESTAR) {
1836 kwarg = ast_for_expr(c, CHILD(n, i+1));
1837 i++;
1838 }
1839 }
1840
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001841 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842}
1843
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001845ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001847 /* testlist_gexp: test (',' test)* [','] */
1848 /* testlist: test (',' test)* [','] */
1849 /* testlist_safe: test (',' test)+ [','] */
1850 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001852 if (TYPE(n) == testlist_gexp) {
1853 if (NCH(n) > 1)
1854 assert(TYPE(CHILD(n, 1)) != gen_for);
1855 }
1856 else {
1857 assert(TYPE(n) == testlist ||
1858 TYPE(n) == testlist_safe ||
1859 TYPE(n) == testlist1);
1860 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 if (NCH(n) == 1)
1862 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 else {
1864 asdl_seq *tmp = seq_for_testlist(c, n);
1865 if (!tmp)
1866 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001867 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001869}
1870
1871static expr_ty
1872ast_for_testlist_gexp(struct compiling *c, const node* n)
1873{
1874 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1875 /* argument: test [ gen_for ] */
1876 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001877 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001878 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001879 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001880}
1881
1882/* like ast_for_testlist() but returns a sequence */
1883static asdl_seq*
1884ast_for_class_bases(struct compiling *c, const node* n)
1885{
1886 /* testlist: test (',' test)* [','] */
1887 assert(NCH(n) > 0);
1888 REQ(n, testlist);
1889 if (NCH(n) == 1) {
1890 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001892 if (!bases)
1893 return NULL;
1894 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001895 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001896 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001897 asdl_seq_SET(bases, 0, base);
1898 return bases;
1899 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001900
1901 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902}
1903
1904static stmt_ty
1905ast_for_expr_stmt(struct compiling *c, const node *n)
1906{
1907 REQ(n, expr_stmt);
1908 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1909 | ('=' (yield_expr|testlist))*)
1910 testlist: test (',' test)* [',']
1911 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1912 | '<<=' | '>>=' | '**=' | '//='
1913 test: ... here starts the operator precendence dance
1914 */
1915
1916 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001917 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 if (!e)
1919 return NULL;
1920
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001921 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922 }
1923 else if (TYPE(CHILD(n, 1)) == augassign) {
1924 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001925 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926 node *ch = CHILD(n, 0);
1927
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001928 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929 if (!expr1)
1930 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001931 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001932 switch (expr1->kind) {
1933 case GeneratorExp_kind:
1934 ast_error(ch, "augmented assignment to generator "
1935 "expression not possible");
1936 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001937 case Yield_kind:
1938 ast_error(ch, "augmented assignment to yield "
1939 "expression not possible");
1940 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001941 case Name_kind: {
1942 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1943 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1944 ast_error(ch, "assignment to None");
1945 return NULL;
1946 }
1947 break;
1948 }
1949 case Attribute_kind:
1950 case Subscript_kind:
1951 break;
1952 default:
1953 ast_error(ch, "illegal expression for augmented "
1954 "assignment");
1955 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001957 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958
1959 ch = CHILD(n, 2);
1960 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001961 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 else
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001963 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001964 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965 return NULL;
1966
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001967 newoperator = ast_for_augassign(CHILD(n, 1));
1968 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 return NULL;
1970
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001971 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 }
1973 else {
1974 int i;
1975 asdl_seq *targets;
1976 node *value;
1977 expr_ty expression;
1978
1979 /* a normal assignment */
1980 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001981 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 if (!targets)
1983 return NULL;
1984 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001985 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 node *ch = CHILD(n, i);
1987 if (TYPE(ch) == yield_expr) {
1988 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001991 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992
1993 /* set context to assign */
1994 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996
Neal Norwitz84456bd2005-12-18 03:16:20 +00001997 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999
2000 asdl_seq_SET(targets, i / 2, e);
2001 }
2002 value = CHILD(n, NCH(n) - 1);
2003 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002004 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 else
2006 expression = ast_for_expr(c, value);
2007 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002008 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002009 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011}
2012
2013static stmt_ty
2014ast_for_print_stmt(struct compiling *c, const node *n)
2015{
2016 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2017 | '>>' test [ (',' test)+ [','] ] )
2018 */
2019 expr_ty dest = NULL, expression;
2020 asdl_seq *seq;
2021 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002022 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023
2024 REQ(n, print_stmt);
2025 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2026 dest = ast_for_expr(c, CHILD(n, 2));
2027 if (!dest)
2028 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002029 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002031 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002033 return NULL;
2034 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002036 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002038 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 }
2040 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002041 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042}
2043
2044static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002045ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046{
2047 asdl_seq *seq;
2048 int i;
2049 expr_ty e;
2050
2051 REQ(n, exprlist);
2052
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002053 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 if (!seq)
2055 return NULL;
2056 for (i = 0; i < NCH(n); i += 2) {
2057 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002058 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002059 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002060 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002061 if (context && !set_context(e, context, CHILD(n, i)))
2062 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 }
2064 return seq;
2065}
2066
2067static stmt_ty
2068ast_for_del_stmt(struct compiling *c, const node *n)
2069{
2070 asdl_seq *expr_list;
2071
2072 /* del_stmt: 'del' exprlist */
2073 REQ(n, del_stmt);
2074
2075 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2076 if (!expr_list)
2077 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002078 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079}
2080
2081static stmt_ty
2082ast_for_flow_stmt(struct compiling *c, const node *n)
2083{
2084 /*
2085 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2086 | yield_stmt
2087 break_stmt: 'break'
2088 continue_stmt: 'continue'
2089 return_stmt: 'return' [testlist]
2090 yield_stmt: yield_expr
2091 yield_expr: 'yield' testlist
2092 raise_stmt: 'raise' [test [',' test [',' test]]]
2093 */
2094 node *ch;
2095
2096 REQ(n, flow_stmt);
2097 ch = CHILD(n, 0);
2098 switch (TYPE(ch)) {
2099 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002100 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002102 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 case yield_stmt: { /* will reduce to yield_expr */
2104 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2105 if (!exp)
2106 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002107 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 }
2109 case return_stmt:
2110 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002111 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002113 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 if (!expression)
2115 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002116 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 }
2118 case raise_stmt:
2119 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002120 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 else if (NCH(ch) == 2) {
2122 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2123 if (!expression)
2124 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002125 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 }
2127 else if (NCH(ch) == 4) {
2128 expr_ty expr1, expr2;
2129
2130 expr1 = ast_for_expr(c, CHILD(ch, 1));
2131 if (!expr1)
2132 return NULL;
2133 expr2 = ast_for_expr(c, CHILD(ch, 3));
2134 if (!expr2)
2135 return NULL;
2136
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002137 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 }
2139 else if (NCH(ch) == 6) {
2140 expr_ty expr1, expr2, expr3;
2141
2142 expr1 = ast_for_expr(c, CHILD(ch, 1));
2143 if (!expr1)
2144 return NULL;
2145 expr2 = ast_for_expr(c, CHILD(ch, 3));
2146 if (!expr2)
2147 return NULL;
2148 expr3 = ast_for_expr(c, CHILD(ch, 5));
2149 if (!expr3)
2150 return NULL;
2151
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002152 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 }
2154 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002155 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156 "unexpected flow_stmt: %d", TYPE(ch));
2157 return NULL;
2158 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002159
2160 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2161 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162}
2163
2164static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002165alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166{
2167 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002168 import_as_name: NAME ['as' NAME]
2169 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 dotted_name: NAME ('.' NAME)*
2171 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002172 PyObject *str;
2173
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 loop:
2175 switch (TYPE(n)) {
2176 case import_as_name:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002177 str = NULL;
2178 if (NCH(n) == 3) {
2179 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2180 ast_error(n, "must use 'as' in import");
2181 return NULL;
2182 }
2183 str = NEW_IDENTIFIER(CHILD(n, 2));
2184 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002185 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 case dotted_as_name:
2187 if (NCH(n) == 1) {
2188 n = CHILD(n, 0);
2189 goto loop;
2190 }
2191 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002192 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002193 if (!a)
2194 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002195 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2196 ast_error(n, "must use 'as' in import");
2197 return NULL;
2198 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 assert(!a->asname);
2200 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2201 return a;
2202 }
2203 break;
2204 case dotted_name:
2205 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002206 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 else {
2208 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002209 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002210 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 char *s;
2212
2213 len = 0;
2214 for (i = 0; i < NCH(n); i += 2)
2215 /* length of string plus one for the dot */
2216 len += strlen(STR(CHILD(n, i))) + 1;
2217 len--; /* the last name doesn't have a dot */
2218 str = PyString_FromStringAndSize(NULL, len);
2219 if (!str)
2220 return NULL;
2221 s = PyString_AS_STRING(str);
2222 if (!s)
2223 return NULL;
2224 for (i = 0; i < NCH(n); i += 2) {
2225 char *sch = STR(CHILD(n, i));
2226 strcpy(s, STR(CHILD(n, i)));
2227 s += strlen(sch);
2228 *s++ = '.';
2229 }
2230 --s;
2231 *s = '\0';
2232 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002233 PyArena_AddPyObject(c->c_arena, str);
2234 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 }
2236 break;
2237 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002238 str = PyString_InternFromString("*");
2239 PyArena_AddPyObject(c->c_arena, str);
2240 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002242 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 "unexpected import name: %d", TYPE(n));
2244 return NULL;
2245 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002246
2247 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 return NULL;
2249}
2250
2251static stmt_ty
2252ast_for_import_stmt(struct compiling *c, const node *n)
2253{
2254 /*
2255 import_stmt: import_name | import_from
2256 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002257 import_from: 'from' ('.'* dotted_name | '.') 'import'
2258 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002260 int lineno;
2261 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 int i;
2263 asdl_seq *aliases;
2264
2265 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002266 lineno = LINENO(n);
2267 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002269 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002271 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002272 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 if (!aliases)
2274 return NULL;
2275 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002276 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002277 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 asdl_seq_SET(aliases, i / 2, import_alias);
2280 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002281 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002283 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002285 int idx, ndots = 0;
2286 alias_ty mod = NULL;
2287 identifier modname;
2288
2289 /* Count the number of dots (for relative imports) and check for the
2290 optional module name */
2291 for (idx = 1; idx < NCH(n); idx++) {
2292 if (TYPE(CHILD(n, idx)) == dotted_name) {
2293 mod = alias_for_import_name(c, CHILD(n, idx));
2294 idx++;
2295 break;
2296 } else if (TYPE(CHILD(n, idx)) != DOT) {
2297 break;
2298 }
2299 ndots++;
2300 }
2301 idx++; /* skip over the 'import' keyword */
2302 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002303 case STAR:
2304 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002305 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002306 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002307 if (ndots) {
2308 ast_error(n, "'import *' not allowed with 'from .'");
2309 return NULL;
2310 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002311 break;
2312 case LPAR:
2313 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002314 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002315 n_children = NCH(n);
2316 break;
2317 case import_as_names:
2318 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002319 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002320 n_children = NCH(n);
2321 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 ast_error(n, "trailing comma not allowed without"
2323 " surrounding parentheses");
2324 return NULL;
2325 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002326 break;
2327 default:
2328 ast_error(n, "Unexpected node-type in from-import");
2329 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002330 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002332 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002333 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335
2336 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002337 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002338 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002339 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002341 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002343 else {
2344 for (i = 0; i < NCH(n); i += 2) {
2345 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2346 if (!import_alias)
2347 return NULL;
2348 asdl_seq_SET(aliases, i / 2, import_alias);
2349 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002351 if (mod != NULL)
2352 modname = mod->name;
2353 else
2354 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002355 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002356 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357 }
Neal Norwitz79792652005-11-14 04:25:03 +00002358 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 "unknown import statement: starts with command '%s'",
2360 STR(CHILD(n, 0)));
2361 return NULL;
2362}
2363
2364static stmt_ty
2365ast_for_global_stmt(struct compiling *c, const node *n)
2366{
2367 /* global_stmt: 'global' NAME (',' NAME)* */
2368 identifier name;
2369 asdl_seq *s;
2370 int i;
2371
2372 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002373 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 if (!s)
2375 return NULL;
2376 for (i = 1; i < NCH(n); i += 2) {
2377 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002378 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380 asdl_seq_SET(s, i / 2, name);
2381 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002382 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383}
2384
2385static stmt_ty
2386ast_for_exec_stmt(struct compiling *c, const node *n)
2387{
2388 expr_ty expr1, globals = NULL, locals = NULL;
2389 int n_children = NCH(n);
2390 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002391 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 "poorly formed 'exec' statement: %d parts to statement",
2393 n_children);
2394 return NULL;
2395 }
2396
2397 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2398 REQ(n, exec_stmt);
2399 expr1 = ast_for_expr(c, CHILD(n, 1));
2400 if (!expr1)
2401 return NULL;
2402 if (n_children >= 4) {
2403 globals = ast_for_expr(c, CHILD(n, 3));
2404 if (!globals)
2405 return NULL;
2406 }
2407 if (n_children == 6) {
2408 locals = ast_for_expr(c, CHILD(n, 5));
2409 if (!locals)
2410 return NULL;
2411 }
2412
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002413 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414}
2415
2416static stmt_ty
2417ast_for_assert_stmt(struct compiling *c, const node *n)
2418{
2419 /* assert_stmt: 'assert' test [',' test] */
2420 REQ(n, assert_stmt);
2421 if (NCH(n) == 2) {
2422 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2423 if (!expression)
2424 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002425 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 }
2427 else if (NCH(n) == 4) {
2428 expr_ty expr1, expr2;
2429
2430 expr1 = ast_for_expr(c, CHILD(n, 1));
2431 if (!expr1)
2432 return NULL;
2433 expr2 = ast_for_expr(c, CHILD(n, 3));
2434 if (!expr2)
2435 return NULL;
2436
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002437 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 }
Neal Norwitz79792652005-11-14 04:25:03 +00002439 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 "improper number of parts to 'assert' statement: %d",
2441 NCH(n));
2442 return NULL;
2443}
2444
2445static asdl_seq *
2446ast_for_suite(struct compiling *c, const node *n)
2447{
2448 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002449 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 stmt_ty s;
2451 int i, total, num, end, pos = 0;
2452 node *ch;
2453
2454 REQ(n, suite);
2455
2456 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002457 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 if (!seq)
2459 return NULL;
2460 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2461 n = CHILD(n, 0);
2462 /* simple_stmt always ends with a NEWLINE,
2463 and may have a trailing SEMI
2464 */
2465 end = NCH(n) - 1;
2466 if (TYPE(CHILD(n, end - 1)) == SEMI)
2467 end--;
2468 /* loop by 2 to skip semi-colons */
2469 for (i = 0; i < end; i += 2) {
2470 ch = CHILD(n, i);
2471 s = ast_for_stmt(c, ch);
2472 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002473 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 asdl_seq_SET(seq, pos++, s);
2475 }
2476 }
2477 else {
2478 for (i = 2; i < (NCH(n) - 1); i++) {
2479 ch = CHILD(n, i);
2480 REQ(ch, stmt);
2481 num = num_stmts(ch);
2482 if (num == 1) {
2483 /* small_stmt or compound_stmt with only one child */
2484 s = ast_for_stmt(c, ch);
2485 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002486 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 asdl_seq_SET(seq, pos++, s);
2488 }
2489 else {
2490 int j;
2491 ch = CHILD(ch, 0);
2492 REQ(ch, simple_stmt);
2493 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002494 /* statement terminates with a semi-colon ';' */
2495 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002496 assert((j + 1) == NCH(ch));
2497 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002498 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 s = ast_for_stmt(c, CHILD(ch, j));
2500 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502 asdl_seq_SET(seq, pos++, s);
2503 }
2504 }
2505 }
2506 }
2507 assert(pos == seq->size);
2508 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509}
2510
2511static stmt_ty
2512ast_for_if_stmt(struct compiling *c, const node *n)
2513{
2514 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2515 ['else' ':' suite]
2516 */
2517 char *s;
2518
2519 REQ(n, if_stmt);
2520
2521 if (NCH(n) == 4) {
2522 expr_ty expression;
2523 asdl_seq *suite_seq;
2524
2525 expression = ast_for_expr(c, CHILD(n, 1));
2526 if (!expression)
2527 return NULL;
2528 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002529 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 return NULL;
2531
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002532 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002534
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 s = STR(CHILD(n, 4));
2536 /* s[2], the third character in the string, will be
2537 's' for el_s_e, or
2538 'i' for el_i_f
2539 */
2540 if (s[2] == 's') {
2541 expr_ty expression;
2542 asdl_seq *seq1, *seq2;
2543
2544 expression = ast_for_expr(c, CHILD(n, 1));
2545 if (!expression)
2546 return NULL;
2547 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002548 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 return NULL;
2550 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002551 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 return NULL;
2553
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002554 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 }
2556 else if (s[2] == 'i') {
2557 int i, n_elif, has_else = 0;
2558 asdl_seq *orelse = NULL;
2559 n_elif = NCH(n) - 4;
2560 /* must reference the child n_elif+1 since 'else' token is third,
2561 not fourth, child from the end. */
2562 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2563 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2564 has_else = 1;
2565 n_elif -= 3;
2566 }
2567 n_elif /= 4;
2568
2569 if (has_else) {
2570 expr_ty expression;
2571 asdl_seq *seq1, *seq2;
2572
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002573 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 if (!orelse)
2575 return NULL;
2576 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002577 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002580 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002583 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585
2586 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002587 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002588 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 /* the just-created orelse handled the last elif */
2590 n_elif--;
2591 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592
2593 for (i = 0; i < n_elif; i++) {
2594 int off = 5 + (n_elif - i - 1) * 4;
2595 expr_ty expression;
2596 asdl_seq *suite_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002597 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2598 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 return NULL;
2600 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002601 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002604 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002607 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002609 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002610 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 }
2612 return If(ast_for_expr(c, CHILD(n, 1)),
2613 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002614 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002616
2617 PyErr_Format(PyExc_SystemError,
2618 "unexpected token in 'if' statement: %s", s);
2619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620}
2621
2622static stmt_ty
2623ast_for_while_stmt(struct compiling *c, const node *n)
2624{
2625 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2626 REQ(n, while_stmt);
2627
2628 if (NCH(n) == 4) {
2629 expr_ty expression;
2630 asdl_seq *suite_seq;
2631
2632 expression = ast_for_expr(c, CHILD(n, 1));
2633 if (!expression)
2634 return NULL;
2635 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002636 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002638 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 }
2640 else if (NCH(n) == 7) {
2641 expr_ty expression;
2642 asdl_seq *seq1, *seq2;
2643
2644 expression = ast_for_expr(c, CHILD(n, 1));
2645 if (!expression)
2646 return NULL;
2647 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002648 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 return NULL;
2650 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002651 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 return NULL;
2653
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002654 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002656
2657 PyErr_Format(PyExc_SystemError,
2658 "wrong number of tokens for 'while' statement: %d",
2659 NCH(n));
2660 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661}
2662
2663static stmt_ty
2664ast_for_for_stmt(struct compiling *c, const node *n)
2665{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002666 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 expr_ty expression;
2668 expr_ty target;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002669 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2671 REQ(n, for_stmt);
2672
2673 if (NCH(n) == 9) {
2674 seq = ast_for_suite(c, CHILD(n, 8));
2675 if (!seq)
2676 return NULL;
2677 }
2678
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002679 node_target = CHILD(n, 1);
2680 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002681 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002683 /* Check the # of children rather than the length of _target, since
2684 for x, in ... has 1 element in _target, but still requires a Tuple. */
2685 if (NCH(node_target) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002686 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002688 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002690 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002691 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 return NULL;
2693 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002694 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 return NULL;
2696
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002697 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2698 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699}
2700
2701static excepthandler_ty
2702ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2703{
2704 /* except_clause: 'except' [test [',' test]] */
2705 REQ(exc, except_clause);
2706 REQ(body, suite);
2707
2708 if (NCH(exc) == 1) {
2709 asdl_seq *suite_seq = ast_for_suite(c, body);
2710 if (!suite_seq)
2711 return NULL;
2712
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002713 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2714 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 }
2716 else if (NCH(exc) == 2) {
2717 expr_ty expression;
2718 asdl_seq *suite_seq;
2719
2720 expression = ast_for_expr(c, CHILD(exc, 1));
2721 if (!expression)
2722 return NULL;
2723 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002724 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 return NULL;
2726
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002727 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2728 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 }
2730 else if (NCH(exc) == 4) {
2731 asdl_seq *suite_seq;
2732 expr_ty expression;
2733 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2734 if (!e)
2735 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002736 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 return NULL;
2738 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002739 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 return NULL;
2741 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002742 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 return NULL;
2744
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002745 return excepthandler(expression, e, suite_seq, LINENO(exc),
2746 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002748
2749 PyErr_Format(PyExc_SystemError,
2750 "wrong number of children for 'except' clause: %d",
2751 NCH(exc));
2752 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753}
2754
2755static stmt_ty
2756ast_for_try_stmt(struct compiling *c, const node *n)
2757{
Neal Norwitzf599f422005-12-17 21:33:47 +00002758 const int nch = NCH(n);
2759 int n_except = (nch - 3)/3;
2760 asdl_seq *body, *orelse = NULL, *finally = NULL;
2761
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762 REQ(n, try_stmt);
2763
Neal Norwitzf599f422005-12-17 21:33:47 +00002764 body = ast_for_suite(c, CHILD(n, 2));
2765 if (body == NULL)
2766 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767
Neal Norwitzf599f422005-12-17 21:33:47 +00002768 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2769 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2770 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2771 /* we can assume it's an "else",
2772 because nch >= 9 for try-else-finally and
2773 it would otherwise have a type of except_clause */
2774 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2775 if (orelse == NULL)
2776 return NULL;
2777 n_except--;
2778 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779
Neal Norwitzf599f422005-12-17 21:33:47 +00002780 finally = ast_for_suite(c, CHILD(n, nch - 1));
2781 if (finally == NULL)
2782 return NULL;
2783 n_except--;
2784 }
2785 else {
2786 /* we can assume it's an "else",
2787 otherwise it would have a type of except_clause */
2788 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2789 if (orelse == NULL)
2790 return NULL;
2791 n_except--;
2792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002794 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002795 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 return NULL;
2797 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002798
2799 if (n_except > 0) {
2800 int i;
2801 stmt_ty except_st;
2802 /* process except statements to create a try ... except */
2803 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2804 if (handlers == NULL)
2805 return NULL;
2806
2807 for (i = 0; i < n_except; i++) {
2808 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2809 CHILD(n, 5 + i * 3));
2810 if (!e)
2811 return NULL;
2812 asdl_seq_SET(handlers, i, e);
2813 }
2814
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002815 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2816 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002817 if (!finally)
2818 return except_st;
2819
2820 /* if a 'finally' is present too, we nest the TryExcept within a
2821 TryFinally to emulate try ... except ... finally */
2822 body = asdl_seq_new(1, c->c_arena);
2823 if (body == NULL)
2824 return NULL;
2825 asdl_seq_SET(body, 0, except_st);
2826 }
2827
2828 /* must be a try ... finally (except clauses are in body, if any exist) */
2829 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002830 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831}
2832
Guido van Rossumc2e20742006-02-27 22:32:47 +00002833static expr_ty
2834ast_for_with_var(struct compiling *c, const node *n)
2835{
2836 REQ(n, with_var);
2837 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2838 ast_error(n, "expected \"with [expr] as [var]\"");
2839 return NULL;
2840 }
2841 return ast_for_expr(c, CHILD(n, 1));
2842}
2843
2844/* with_stmt: 'with' test [ with_var ] ':' suite */
2845static stmt_ty
2846ast_for_with_stmt(struct compiling *c, const node *n)
2847{
2848 expr_ty context_expr, optional_vars = NULL;
2849 int suite_index = 3; /* skip 'with', test, and ':' */
2850 asdl_seq *suite_seq;
2851
2852 assert(TYPE(n) == with_stmt);
2853 context_expr = ast_for_expr(c, CHILD(n, 1));
2854 if (TYPE(CHILD(n, 2)) == with_var) {
2855 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2856
2857 if (!optional_vars) {
2858 return NULL;
2859 }
2860 if (!set_context(optional_vars, Store, n)) {
2861 return NULL;
2862 }
2863 suite_index = 4;
2864 }
2865
2866 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2867 if (!suite_seq) {
2868 return NULL;
2869 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002870 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2871 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002872}
2873
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874static stmt_ty
2875ast_for_classdef(struct compiling *c, const node *n)
2876{
2877 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 asdl_seq *bases, *s;
2879
2880 REQ(n, classdef);
2881
2882 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2883 ast_error(n, "assignment to None");
2884 return NULL;
2885 }
2886
2887 if (NCH(n) == 4) {
2888 s = ast_for_suite(c, CHILD(n, 3));
2889 if (!s)
2890 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002891 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2892 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 }
2894 /* check for empty base list */
2895 if (TYPE(CHILD(n,3)) == RPAR) {
2896 s = ast_for_suite(c, CHILD(n,5));
2897 if (!s)
2898 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002899 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2900 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 }
2902
2903 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002904 bases = ast_for_class_bases(c, CHILD(n, 3));
2905 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907
2908 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002909 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002911 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2912 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913}
2914
2915static stmt_ty
2916ast_for_stmt(struct compiling *c, const node *n)
2917{
2918 if (TYPE(n) == stmt) {
2919 assert(NCH(n) == 1);
2920 n = CHILD(n, 0);
2921 }
2922 if (TYPE(n) == simple_stmt) {
2923 assert(num_stmts(n) == 1);
2924 n = CHILD(n, 0);
2925 }
2926 if (TYPE(n) == small_stmt) {
2927 REQ(n, small_stmt);
2928 n = CHILD(n, 0);
2929 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2930 | flow_stmt | import_stmt | global_stmt | exec_stmt
2931 | assert_stmt
2932 */
2933 switch (TYPE(n)) {
2934 case expr_stmt:
2935 return ast_for_expr_stmt(c, n);
2936 case print_stmt:
2937 return ast_for_print_stmt(c, n);
2938 case del_stmt:
2939 return ast_for_del_stmt(c, n);
2940 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002941 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002942 case flow_stmt:
2943 return ast_for_flow_stmt(c, n);
2944 case import_stmt:
2945 return ast_for_import_stmt(c, n);
2946 case global_stmt:
2947 return ast_for_global_stmt(c, n);
2948 case exec_stmt:
2949 return ast_for_exec_stmt(c, n);
2950 case assert_stmt:
2951 return ast_for_assert_stmt(c, n);
2952 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002953 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2955 TYPE(n), NCH(n));
2956 return NULL;
2957 }
2958 }
2959 else {
2960 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2961 | funcdef | classdef
2962 */
2963 node *ch = CHILD(n, 0);
2964 REQ(n, compound_stmt);
2965 switch (TYPE(ch)) {
2966 case if_stmt:
2967 return ast_for_if_stmt(c, ch);
2968 case while_stmt:
2969 return ast_for_while_stmt(c, ch);
2970 case for_stmt:
2971 return ast_for_for_stmt(c, ch);
2972 case try_stmt:
2973 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002974 case with_stmt:
2975 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 case funcdef:
2977 return ast_for_funcdef(c, ch);
2978 case classdef:
2979 return ast_for_classdef(c, ch);
2980 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002981 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2983 TYPE(n), NCH(n));
2984 return NULL;
2985 }
2986 }
2987}
2988
2989static PyObject *
2990parsenumber(const char *s)
2991{
2992 const char *end;
2993 long x;
2994 double dx;
2995#ifndef WITHOUT_COMPLEX
2996 Py_complex c;
2997 int imflag;
2998#endif
2999
3000 errno = 0;
3001 end = s + strlen(s) - 1;
3002#ifndef WITHOUT_COMPLEX
3003 imflag = *end == 'j' || *end == 'J';
3004#endif
3005 if (*end == 'l' || *end == 'L')
3006 return PyLong_FromString((char *)s, (char **)0, 0);
3007 if (s[0] == '0') {
3008 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3009 if (x < 0 && errno == 0) {
3010 return PyLong_FromString((char *)s,
3011 (char **)0,
3012 0);
3013 }
3014 }
3015 else
3016 x = PyOS_strtol((char *)s, (char **)&end, 0);
3017 if (*end == '\0') {
3018 if (errno != 0)
3019 return PyLong_FromString((char *)s, (char **)0, 0);
3020 return PyInt_FromLong(x);
3021 }
3022 /* XXX Huge floats may silently fail */
3023#ifndef WITHOUT_COMPLEX
3024 if (imflag) {
3025 c.real = 0.;
3026 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003027 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 PyFPE_END_PROTECT(c)
3029 return PyComplex_FromCComplex(c);
3030 }
3031 else
3032#endif
3033 {
3034 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003035 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 PyFPE_END_PROTECT(dx)
3037 return PyFloat_FromDouble(dx);
3038 }
3039}
3040
3041static PyObject *
3042decode_utf8(const char **sPtr, const char *end, char* encoding)
3043{
3044#ifndef Py_USING_UNICODE
3045 Py_FatalError("decode_utf8 should not be called in this build.");
3046 return NULL;
3047#else
3048 PyObject *u, *v;
3049 char *s, *t;
3050 t = s = (char *)*sPtr;
3051 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3052 while (s < end && (*s & 0x80)) s++;
3053 *sPtr = s;
3054 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3055 if (u == NULL)
3056 return NULL;
3057 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3058 Py_DECREF(u);
3059 return v;
3060#endif
3061}
3062
3063static PyObject *
3064decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3065{
3066 PyObject *v, *u;
3067 char *buf;
3068 char *p;
3069 const char *end;
3070 if (encoding == NULL) {
3071 buf = (char *)s;
3072 u = NULL;
3073 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3074 buf = (char *)s;
3075 u = NULL;
3076 } else {
3077 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3078 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3079 if (u == NULL)
3080 return NULL;
3081 p = buf = PyString_AsString(u);
3082 end = s + len;
3083 while (s < end) {
3084 if (*s == '\\') {
3085 *p++ = *s++;
3086 if (*s & 0x80) {
3087 strcpy(p, "u005c");
3088 p += 5;
3089 }
3090 }
3091 if (*s & 0x80) { /* XXX inefficient */
3092 PyObject *w;
3093 char *r;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003094 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095 w = decode_utf8(&s, end, "utf-16-be");
3096 if (w == NULL) {
3097 Py_DECREF(u);
3098 return NULL;
3099 }
3100 r = PyString_AsString(w);
3101 rn = PyString_Size(w);
3102 assert(rn % 2 == 0);
3103 for (i = 0; i < rn; i += 2) {
3104 sprintf(p, "\\u%02x%02x",
3105 r[i + 0] & 0xFF,
3106 r[i + 1] & 0xFF);
3107 p += 6;
3108 }
3109 Py_DECREF(w);
3110 } else {
3111 *p++ = *s++;
3112 }
3113 }
3114 len = p - buf;
3115 s = buf;
3116 }
3117 if (rawmode)
3118 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3119 else
3120 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3121 Py_XDECREF(u);
3122 return v;
3123}
3124
3125/* s is a Python string literal, including the bracketing quote characters,
3126 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3127 * parsestr parses it, and returns the decoded Python string object.
3128 */
3129static PyObject *
3130parsestr(const char *s, const char *encoding)
3131{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003133 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 int rawmode = 0;
3135 int need_encoding;
3136 int unicode = 0;
3137
3138 if (isalpha(quote) || quote == '_') {
3139 if (quote == 'u' || quote == 'U') {
3140 quote = *++s;
3141 unicode = 1;
3142 }
3143 if (quote == 'r' || quote == 'R') {
3144 quote = *++s;
3145 rawmode = 1;
3146 }
3147 }
3148 if (quote != '\'' && quote != '\"') {
3149 PyErr_BadInternalCall();
3150 return NULL;
3151 }
3152 s++;
3153 len = strlen(s);
3154 if (len > INT_MAX) {
3155 PyErr_SetString(PyExc_OverflowError,
3156 "string to parse is too long");
3157 return NULL;
3158 }
3159 if (s[--len] != quote) {
3160 PyErr_BadInternalCall();
3161 return NULL;
3162 }
3163 if (len >= 4 && s[0] == quote && s[1] == quote) {
3164 s += 2;
3165 len -= 2;
3166 if (s[--len] != quote || s[--len] != quote) {
3167 PyErr_BadInternalCall();
3168 return NULL;
3169 }
3170 }
3171#ifdef Py_USING_UNICODE
3172 if (unicode || Py_UnicodeFlag) {
3173 return decode_unicode(s, len, rawmode, encoding);
3174 }
3175#endif
3176 need_encoding = (encoding != NULL &&
3177 strcmp(encoding, "utf-8") != 0 &&
3178 strcmp(encoding, "iso-8859-1") != 0);
3179 if (rawmode || strchr(s, '\\') == NULL) {
3180 if (need_encoding) {
3181#ifndef Py_USING_UNICODE
3182 /* This should not happen - we never see any other
3183 encoding. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003184 Py_FatalError(
3185 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003187 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188 if (u == NULL)
3189 return NULL;
3190 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3191 Py_DECREF(u);
3192 return v;
3193#endif
3194 } else {
3195 return PyString_FromStringAndSize(s, len);
3196 }
3197 }
3198
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003199 return PyString_DecodeEscape(s, len, NULL, unicode,
3200 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201}
3202
3203/* Build a Python string object out of a STRING atom. This takes care of
3204 * compile-time literal catenation, calling parsestr() on each piece, and
3205 * pasting the intermediate results together.
3206 */
3207static PyObject *
3208parsestrplus(struct compiling *c, const node *n)
3209{
3210 PyObject *v;
3211 int i;
3212 REQ(CHILD(n, 0), STRING);
3213 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3214 /* String literal concatenation */
3215 for (i = 1; i < NCH(n); i++) {
3216 PyObject *s;
3217 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3218 if (s == NULL)
3219 goto onError;
3220 if (PyString_Check(v) && PyString_Check(s)) {
3221 PyString_ConcatAndDel(&v, s);
3222 if (v == NULL)
3223 goto onError;
3224 }
3225#ifdef Py_USING_UNICODE
3226 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003227 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229 Py_DECREF(v);
3230 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003231 if (v == NULL)
3232 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 }
3234#endif
3235 }
3236 }
3237 return v;
3238
3239 onError:
3240 Py_XDECREF(v);
3241 return NULL;
3242}