blob: d2a9bd130df51562c63902d5d5b69ab6645358e1 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* Data structure used internally */
19struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000020 char *c_encoding; /* source encoding */
21 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022};
23
24static asdl_seq *seq_for_testlist(struct compiling *, const node *);
25static expr_ty ast_for_expr(struct compiling *, const node *);
26static stmt_ty ast_for_stmt(struct compiling *, const node *);
27static asdl_seq *ast_for_suite(struct compiling *, const node *);
Martin v. Löwis28457502006-04-11 09:17:27 +000028static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000029static expr_ty ast_for_testlist(struct compiling *, const node *);
30static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000031
32/* Note different signature for ast_for_call */
33static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
34
35static PyObject *parsenumber(const char *);
36static PyObject *parsestr(const char *s, const char *encoding);
37static PyObject *parsestrplus(struct compiling *, const node *n);
38
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039#ifndef LINENO
40#define LINENO(n) ((n)->n_lineno)
41#endif
42
Neal Norwitzadb69fc2005-12-17 20:54:49 +000043static identifier
44new_identifier(const char* n, PyArena *arena) {
45 PyObject* id = PyString_InternFromString(n);
46 PyArena_AddPyObject(arena, id);
47 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048}
49
Neal Norwitzadb69fc2005-12-17 20:54:49 +000050#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000051
52/* This routine provides an invalid object for the syntax error.
53 The outermost routine must unpack this error and create the
54 proper object. We do this so that we don't have to pass
55 the filename to everything function.
56
57 XXX Maybe we should just pass the filename...
58*/
59
60static int
61ast_error(const node *n, const char *errstr)
62{
63 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
64 if (!u)
65 return 0;
66 PyErr_SetObject(PyExc_SyntaxError, u);
67 Py_DECREF(u);
68 return 0;
69}
70
71static void
72ast_error_finish(const char *filename)
73{
74 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000075 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000076
77 assert(PyErr_Occurred());
78 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
79 return;
80
81 PyErr_Fetch(&type, &value, &tback);
82 errstr = PyTuple_GetItem(value, 0);
83 if (!errstr)
84 return;
85 Py_INCREF(errstr);
86 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000087 if (lineno == -1) {
88 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000090 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091 Py_DECREF(value);
92
93 loc = PyErr_ProgramText(filename, lineno);
94 if (!loc) {
95 Py_INCREF(Py_None);
96 loc = Py_None;
97 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +000098 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000100 if (!tmp) {
101 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000103 }
Georg Brandl7784f122006-05-26 20:04:44 +0000104 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(errstr);
106 Py_DECREF(tmp);
107 if (!value)
108 return;
109 PyErr_Restore(type, value, tback);
110}
111
112/* num_stmts() returns number of contained statements.
113
114 Use this routine to determine how big a sequence is needed for
115 the statements in a parse tree. Its raison d'etre is this bit of
116 grammar:
117
118 stmt: simple_stmt | compound_stmt
119 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
120
121 A simple_stmt can contain multiple small_stmt elements joined
122 by semicolons. If the arg is a simple_stmt, the number of
123 small_stmt elements is returned.
124*/
125
126static int
127num_stmts(const node *n)
128{
129 int i, l;
130 node *ch;
131
132 switch (TYPE(n)) {
133 case single_input:
134 if (TYPE(CHILD(n, 0)) == NEWLINE)
135 return 0;
136 else
137 return num_stmts(CHILD(n, 0));
138 case file_input:
139 l = 0;
140 for (i = 0; i < NCH(n); i++) {
141 ch = CHILD(n, i);
142 if (TYPE(ch) == stmt)
143 l += num_stmts(ch);
144 }
145 return l;
146 case stmt:
147 return num_stmts(CHILD(n, 0));
148 case compound_stmt:
149 return 1;
150 case simple_stmt:
151 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
152 case suite:
153 if (NCH(n) == 1)
154 return num_stmts(CHILD(n, 0));
155 else {
156 l = 0;
157 for (i = 2; i < (NCH(n) - 1); i++)
158 l += num_stmts(CHILD(n, i));
159 return l;
160 }
161 default: {
162 char buf[128];
163
164 sprintf(buf, "Non-statement found: %d %d\n",
165 TYPE(n), NCH(n));
166 Py_FatalError(buf);
167 }
168 }
169 assert(0);
170 return 0;
171}
172
173/* Transform the CST rooted at node * to the appropriate AST
174*/
175
176mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000177PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
178 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000180 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181 asdl_seq *stmts = NULL;
182 stmt_ty s;
183 node *ch;
184 struct compiling c;
185
186 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000187 c.c_encoding = "utf-8";
Neal Norwitze98ccf62006-03-23 05:39:47 +0000188 if (TYPE(n) == encoding_decl) {
189 ast_error(n, "encoding declaration in Unicode string");
190 goto error;
191 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192 } else if (TYPE(n) == encoding_decl) {
193 c.c_encoding = STR(n);
194 n = CHILD(n, 0);
195 } else {
196 c.c_encoding = NULL;
197 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000198 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
Jeremy Hyltona8293132006-02-28 17:58:27 +0000200 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201 switch (TYPE(n)) {
202 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000203 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204 if (!stmts)
205 return NULL;
206 for (i = 0; i < NCH(n) - 1; i++) {
207 ch = CHILD(n, i);
208 if (TYPE(ch) == NEWLINE)
209 continue;
210 REQ(ch, stmt);
211 num = num_stmts(ch);
212 if (num == 1) {
213 s = ast_for_stmt(&c, ch);
214 if (!s)
215 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000216 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 }
218 else {
219 ch = CHILD(ch, 0);
220 REQ(ch, simple_stmt);
221 for (j = 0; j < num; j++) {
222 s = ast_for_stmt(&c, CHILD(ch, j * 2));
223 if (!s)
224 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000225 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 }
227 }
228 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000229 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230 case eval_input: {
231 expr_ty testlist_ast;
232
233 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000234 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235 if (!testlist_ast)
236 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000237 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 }
239 case single_input:
240 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000241 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 if (!stmts)
243 goto error;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000244 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
245 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000246 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247 }
248 else {
249 n = CHILD(n, 0);
250 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000251 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 if (!stmts)
253 goto error;
254 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000255 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256 if (!s)
257 goto error;
258 asdl_seq_SET(stmts, 0, s);
259 }
260 else {
261 /* Only a simple_stmt can contain multiple statements. */
262 REQ(n, simple_stmt);
263 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 if (TYPE(CHILD(n, i)) == NEWLINE)
265 break;
266 s = ast_for_stmt(&c, CHILD(n, i));
267 if (!s)
268 goto error;
269 asdl_seq_SET(stmts, i / 2, s);
270 }
271 }
272
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000273 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 }
275 default:
276 goto error;
277 }
278 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 ast_error_finish(filename);
280 return NULL;
281}
282
283/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
284*/
285
286static operator_ty
287get_operator(const node *n)
288{
289 switch (TYPE(n)) {
290 case VBAR:
291 return BitOr;
292 case CIRCUMFLEX:
293 return BitXor;
294 case AMPER:
295 return BitAnd;
296 case LEFTSHIFT:
297 return LShift;
298 case RIGHTSHIFT:
299 return RShift;
300 case PLUS:
301 return Add;
302 case MINUS:
303 return Sub;
304 case STAR:
305 return Mult;
306 case SLASH:
307 return Div;
308 case DOUBLESLASH:
309 return FloorDiv;
310 case PERCENT:
311 return Mod;
312 default:
Martin v. Löwis28457502006-04-11 09:17:27 +0000313 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314 }
315}
316
Jeremy Hyltona8293132006-02-28 17:58:27 +0000317/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318
319 Only sets context for expr kinds that "can appear in assignment context"
320 (according to ../Parser/Python.asdl). For other expr kinds, it sets
321 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322*/
323
324static int
325set_context(expr_ty e, expr_context_ty ctx, const node *n)
326{
327 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000328 /* If a particular expression type can't be used for assign / delete,
329 set expr_name to its name and an error message will be generated.
330 */
331 const char* expr_name = NULL;
332
333 /* The ast defines augmented store and load contexts, but the
334 implementation here doesn't actually use them. The code may be
335 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000336 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000337 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000338 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000339 */
340 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341
342 switch (e->kind) {
343 case Attribute_kind:
344 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000345 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346 return ast_error(n, "assignment to None");
347 }
348 e->v.Attribute.ctx = ctx;
349 break;
350 case Subscript_kind:
351 e->v.Subscript.ctx = ctx;
352 break;
353 case Name_kind:
354 if (ctx == Store &&
355 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
356 return ast_error(n, "assignment to None");
357 }
358 e->v.Name.ctx = ctx;
359 break;
360 case List_kind:
361 e->v.List.ctx = ctx;
362 s = e->v.List.elts;
363 break;
364 case Tuple_kind:
365 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
366 return ast_error(n, "can't assign to ()");
367 e->v.Tuple.ctx = ctx;
368 s = e->v.Tuple.elts;
369 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000370 case Lambda_kind:
371 expr_name = "lambda";
372 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000374 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000378 case UnaryOp_kind:
379 expr_name = "operator";
380 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 expr_name = "generator expression";
383 break;
Neal Norwitz0d62a062006-07-30 06:53:31 +0000384 case Yield_kind:
385 expr_name = "yield expression";
386 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000387 case ListComp_kind:
388 expr_name = "list comprehension";
389 break;
390 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000391 case Num_kind:
392 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000393 expr_name = "literal";
394 break;
395 case Compare_kind:
396 expr_name = "comparison";
397 break;
398 case Repr_kind:
399 expr_name = "repr";
400 break;
Neal Norwitz373f0a72006-05-15 07:04:36 +0000401 case IfExp_kind:
402 expr_name = "conditional expression";
403 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000404 default:
405 PyErr_Format(PyExc_SystemError,
406 "unexpected expression in assignment %d (line %d)",
407 e->kind, e->lineno);
408 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000410 /* Check for error string set by switch */
411 if (expr_name) {
412 char buf[300];
413 PyOS_snprintf(buf, sizeof(buf),
414 "can't %s %s",
415 ctx == Store ? "assign to" : "delete",
416 expr_name);
417 return ast_error(n, buf);
418 }
419
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000421 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422 */
423 if (s) {
424 int i;
425
426 for (i = 0; i < asdl_seq_LEN(s); i++) {
Martin v. Löwis28457502006-04-11 09:17:27 +0000427 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 return 0;
429 }
430 }
431 return 1;
432}
433
434static operator_ty
435ast_for_augassign(const node *n)
436{
437 REQ(n, augassign);
438 n = CHILD(n, 0);
439 switch (STR(n)[0]) {
440 case '+':
441 return Add;
442 case '-':
443 return Sub;
444 case '/':
445 if (STR(n)[1] == '/')
446 return FloorDiv;
447 else
448 return Div;
449 case '%':
450 return Mod;
451 case '<':
452 return LShift;
453 case '>':
454 return RShift;
455 case '&':
456 return BitAnd;
457 case '^':
458 return BitXor;
459 case '|':
460 return BitOr;
461 case '*':
462 if (STR(n)[1] == '*')
463 return Pow;
464 else
465 return Mult;
466 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000467 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000468 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469 }
470}
471
472static cmpop_ty
473ast_for_comp_op(const node *n)
474{
475 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
476 |'is' 'not'
477 */
478 REQ(n, comp_op);
479 if (NCH(n) == 1) {
480 n = CHILD(n, 0);
481 switch (TYPE(n)) {
482 case LESS:
483 return Lt;
484 case GREATER:
485 return Gt;
486 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 return Eq;
488 case LESSEQUAL:
489 return LtE;
490 case GREATEREQUAL:
491 return GtE;
492 case NOTEQUAL:
493 return NotEq;
494 case NAME:
495 if (strcmp(STR(n), "in") == 0)
496 return In;
497 if (strcmp(STR(n), "is") == 0)
498 return Is;
499 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000500 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000502 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503 }
504 }
505 else if (NCH(n) == 2) {
506 /* handle "not in" and "is not" */
507 switch (TYPE(CHILD(n, 0))) {
508 case NAME:
509 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
510 return NotIn;
511 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
512 return IsNot;
513 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000514 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Martin v. Löwis28457502006-04-11 09:17:27 +0000516 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 }
518 }
Neal Norwitz79792652005-11-14 04:25:03 +0000519 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000521 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522}
523
524static asdl_seq *
525seq_for_testlist(struct compiling *c, const node *n)
526{
527 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000528 asdl_seq *seq;
529 expr_ty expression;
530 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 assert(TYPE(n) == testlist
532 || TYPE(n) == listmaker
533 || TYPE(n) == testlist_gexp
534 || TYPE(n) == testlist_safe
535 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000537 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 if (!seq)
539 return NULL;
540
541 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000542 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
544 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000545 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
548 assert(i / 2 < seq->size);
549 asdl_seq_SET(seq, i / 2, expression);
550 }
551 return seq;
552}
553
554static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000555compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556{
557 int i, len = (NCH(n) + 1) / 2;
558 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000559 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560 if (!args)
561 return NULL;
562
563 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 for (i = 0; i < len; i++) {
565 const node *child = CHILD(CHILD(n, 2*i), 0);
566 expr_ty arg;
567 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000568 if (!strcmp(STR(child), "None")) {
569 ast_error(child, "assignment to None");
570 return NULL;
571 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000572 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
573 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000574 }
575 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000576 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000577 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 asdl_seq_SET(args, i, arg);
579 }
580
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000581 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000582 if (!set_context(result, Store, n))
583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584 return result;
585}
586
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Jeremy Hyltona8293132006-02-28 17:58:27 +0000588/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
590static arguments_ty
591ast_for_arguments(struct compiling *c, const node *n)
592{
593 /* parameters: '(' [varargslist] ')'
594 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
595 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
596 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000597 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 asdl_seq *args, *defaults;
599 identifier vararg = NULL, kwarg = NULL;
600 node *ch;
601
602 if (TYPE(n) == parameters) {
603 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000604 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 n = CHILD(n, 1);
606 }
607 REQ(n, varargslist);
608
609 /* first count the number of normal args & defaults */
610 for (i = 0; i < NCH(n); i++) {
611 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000612 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614 if (TYPE(ch) == EQUAL)
615 n_defaults++;
616 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000617 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 if (!args && n_args)
Neal Norwitzc173b482006-07-30 19:18:13 +0000619 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000620 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 if (!defaults && n_defaults)
Neal Norwitzc173b482006-07-30 19:18:13 +0000622 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623
624 /* fpdef: NAME | '(' fplist ')'
625 fplist: fpdef (',' fpdef)* [',']
626 */
627 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000628 j = 0; /* index for defaults */
629 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630 while (i < NCH(n)) {
631 ch = CHILD(n, i);
632 switch (TYPE(ch)) {
633 case fpdef:
634 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
635 anything other than EQUAL or a comma? */
636 /* XXX Should NCH(n) check be made a separate check? */
637 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000638 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
639 if (!expression)
640 goto error;
Neal Norwitzc173b482006-07-30 19:18:13 +0000641 assert(defaults != NULL);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000642 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 i += 2;
644 found_default = 1;
645 }
646 else if (found_default) {
647 ast_error(n,
648 "non-default argument follows default argument");
649 goto error;
650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000652 ch = CHILD(ch, 1);
653 /* def foo((x)): is not complex, special case. */
654 if (NCH(ch) != 1) {
655 /* We have complex arguments, setup for unpacking. */
656 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
657 } else {
658 /* def foo((x)): setup for checking NAME below. */
659 ch = CHILD(ch, 0);
660 }
661 }
662 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000663 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
665 ast_error(CHILD(ch, 0), "assignment to None");
666 goto error;
667 }
Armin Rigo31441302005-10-21 12:57:31 +0000668 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000669 Param, LINENO(ch), ch->n_col_offset,
670 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 if (!name)
672 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000673 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674
675 }
676 i += 2; /* the name and the comma */
677 break;
678 case STAR:
679 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
680 ast_error(CHILD(n, i+1), "assignment to None");
681 goto error;
682 }
683 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
684 i += 3;
685 break;
686 case DOUBLESTAR:
687 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
688 ast_error(CHILD(n, i+1), "assignment to None");
689 goto error;
690 }
691 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
692 i += 3;
693 break;
694 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000695 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 "unexpected node in varargslist: %d @ %d",
697 TYPE(ch), i);
698 goto error;
699 }
700 }
701
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000702 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
704 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000705 Py_XDECREF(vararg);
706 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 return NULL;
708}
709
710static expr_ty
711ast_for_dotted_name(struct compiling *c, const node *n)
712{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000713 expr_ty e;
714 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000715 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716 int i;
717
718 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000719
720 lineno = LINENO(n);
721 col_offset = n->n_col_offset;
722
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 id = NEW_IDENTIFIER(CHILD(n, 0));
724 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000725 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000726 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000728 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729
730 for (i = 2; i < NCH(n); i+=2) {
731 id = NEW_IDENTIFIER(CHILD(n, i));
732 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000733 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000734 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000735 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000736 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 }
738
739 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740}
741
742static expr_ty
743ast_for_decorator(struct compiling *c, const node *n)
744{
745 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
746 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000747 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748
749 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000750 REQ(CHILD(n, 0), AT);
751 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752
753 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
754 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756
757 if (NCH(n) == 3) { /* No arguments */
758 d = name_expr;
759 name_expr = NULL;
760 }
761 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000762 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
763 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 name_expr = NULL;
767 }
768 else {
769 d = ast_for_call(c, CHILD(n, 3), name_expr);
770 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 name_expr = NULL;
773 }
774
775 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776}
777
778static asdl_seq*
779ast_for_decorators(struct compiling *c, const node *n)
780{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000781 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000782 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 int i;
784
785 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000786 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 if (!decorator_seq)
788 return NULL;
789
790 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000791 d = ast_for_decorator(c, CHILD(n, i));
792 if (!d)
793 return NULL;
794 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 }
796 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797}
798
799static stmt_ty
800ast_for_funcdef(struct compiling *c, const node *n)
801{
802 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000803 identifier name;
804 arguments_ty args;
805 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 asdl_seq *decorator_seq = NULL;
807 int name_i;
808
809 REQ(n, funcdef);
810
811 if (NCH(n) == 6) { /* decorators are present */
812 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
813 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 name_i = 2;
816 }
817 else {
818 name_i = 1;
819 }
820
821 name = NEW_IDENTIFIER(CHILD(n, name_i));
822 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000825 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 }
828 args = ast_for_arguments(c, CHILD(n, name_i + 1));
829 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 body = ast_for_suite(c, CHILD(n, name_i + 3));
832 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000835 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
836 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837}
838
839static expr_ty
840ast_for_lambdef(struct compiling *c, const node *n)
841{
842 /* lambdef: 'lambda' [varargslist] ':' test */
843 arguments_ty args;
844 expr_ty expression;
845
846 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000847 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 if (!args)
849 return NULL;
850 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000851 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 }
854 else {
855 args = ast_for_arguments(c, CHILD(n, 1));
856 if (!args)
857 return NULL;
858 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000859 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 }
862
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000863 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864}
865
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000866static expr_ty
867ast_for_ifexpr(struct compiling *c, const node *n)
868{
869 /* test: or_test 'if' or_test 'else' test */
870 expr_ty expression, body, orelse;
871
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000872 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000873 body = ast_for_expr(c, CHILD(n, 0));
874 if (!body)
875 return NULL;
876 expression = ast_for_expr(c, CHILD(n, 2));
877 if (!expression)
878 return NULL;
879 orelse = ast_for_expr(c, CHILD(n, 4));
880 if (!orelse)
881 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000882 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
883 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000884}
885
Neal Norwitze4d4f002006-09-05 03:58:26 +0000886/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
887 so there is only a single version. Possibly for loops can also re-use
888 the code.
889*/
890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891/* Count the number of 'for' loop in a list comprehension.
892
893 Helper for ast_for_listcomp().
894*/
895
896static int
897count_list_fors(const node *n)
898{
899 int n_fors = 0;
900 node *ch = CHILD(n, 1);
901
902 count_list_for:
903 n_fors++;
904 REQ(ch, list_for);
905 if (NCH(ch) == 5)
906 ch = CHILD(ch, 4);
907 else
908 return n_fors;
909 count_list_iter:
910 REQ(ch, list_iter);
911 ch = CHILD(ch, 0);
912 if (TYPE(ch) == list_for)
913 goto count_list_for;
914 else if (TYPE(ch) == list_if) {
915 if (NCH(ch) == 3) {
916 ch = CHILD(ch, 2);
917 goto count_list_iter;
918 }
919 else
920 return n_fors;
921 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000922
923 /* Should never be reached */
924 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
925 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926}
927
928/* Count the number of 'if' statements in a list comprehension.
929
930 Helper for ast_for_listcomp().
931*/
932
933static int
934count_list_ifs(const node *n)
935{
936 int n_ifs = 0;
937
938 count_list_iter:
939 REQ(n, list_iter);
940 if (TYPE(CHILD(n, 0)) == list_for)
941 return n_ifs;
942 n = CHILD(n, 0);
943 REQ(n, list_if);
944 n_ifs++;
945 if (NCH(n) == 2)
946 return n_ifs;
947 n = CHILD(n, 2);
948 goto count_list_iter;
949}
950
951static expr_ty
952ast_for_listcomp(struct compiling *c, const node *n)
953{
954 /* listmaker: test ( list_for | (',' test)* [','] )
955 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
956 list_iter: list_for | list_if
957 list_if: 'if' test [list_iter]
958 testlist_safe: test [(',' test)+ [',']]
959 */
960 expr_ty elt;
961 asdl_seq *listcomps;
962 int i, n_fors;
963 node *ch;
964
965 REQ(n, listmaker);
966 assert(NCH(n) > 1);
967
968 elt = ast_for_expr(c, CHILD(n, 0));
969 if (!elt)
970 return NULL;
971
972 n_fors = count_list_fors(n);
973 if (n_fors == -1)
974 return NULL;
975
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976 listcomps = asdl_seq_new(n_fors, c->c_arena);
977 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000979
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 ch = CHILD(n, 1);
981 for (i = 0; i < n_fors; i++) {
982 comprehension_ty lc;
983 asdl_seq *t;
984 expr_ty expression;
Neal Norwitzdac090d2006-09-05 03:53:08 +0000985 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986
987 REQ(ch, list_for);
988
Neal Norwitzdac090d2006-09-05 03:53:08 +0000989 for_ch = CHILD(ch, 1);
990 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000991 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000993 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000994 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996
Neal Norwitzdac090d2006-09-05 03:53:08 +0000997 /* Check the # of children rather than the length of t, since
998 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
999 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001000 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001001 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001003 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1004 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001005 expression, NULL, c->c_arena);
1006 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008
1009 if (NCH(ch) == 5) {
1010 int j, n_ifs;
1011 asdl_seq *ifs;
1012
1013 ch = CHILD(ch, 4);
1014 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001015 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001018 ifs = asdl_seq_new(n_ifs, c->c_arena);
1019 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021
1022 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001023 REQ(ch, list_iter);
1024 ch = CHILD(ch, 0);
1025 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
Jeremy Hyltona8293132006-02-28 17:58:27 +00001027 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1028 if (NCH(ch) == 3)
1029 ch = CHILD(ch, 2);
1030 }
1031 /* on exit, must guarantee that ch is a list_for */
1032 if (TYPE(ch) == list_iter)
1033 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001035 }
1036 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 }
1038
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001039 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040}
1041
1042/*
1043 Count the number of 'for' loops in a generator expression.
1044
1045 Helper for ast_for_genexp().
1046*/
1047
1048static int
1049count_gen_fors(const node *n)
1050{
1051 int n_fors = 0;
1052 node *ch = CHILD(n, 1);
1053
1054 count_gen_for:
1055 n_fors++;
1056 REQ(ch, gen_for);
1057 if (NCH(ch) == 5)
1058 ch = CHILD(ch, 4);
1059 else
1060 return n_fors;
1061 count_gen_iter:
1062 REQ(ch, gen_iter);
1063 ch = CHILD(ch, 0);
1064 if (TYPE(ch) == gen_for)
1065 goto count_gen_for;
1066 else if (TYPE(ch) == gen_if) {
1067 if (NCH(ch) == 3) {
1068 ch = CHILD(ch, 2);
1069 goto count_gen_iter;
1070 }
1071 else
1072 return n_fors;
1073 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001074
1075 /* Should never be reached */
1076 PyErr_SetString(PyExc_SystemError,
1077 "logic error in count_gen_fors");
1078 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079}
1080
1081/* Count the number of 'if' statements in a generator expression.
1082
1083 Helper for ast_for_genexp().
1084*/
1085
1086static int
1087count_gen_ifs(const node *n)
1088{
1089 int n_ifs = 0;
1090
1091 while (1) {
1092 REQ(n, gen_iter);
1093 if (TYPE(CHILD(n, 0)) == gen_for)
1094 return n_ifs;
1095 n = CHILD(n, 0);
1096 REQ(n, gen_if);
1097 n_ifs++;
1098 if (NCH(n) == 2)
1099 return n_ifs;
1100 n = CHILD(n, 2);
1101 }
1102}
1103
Jeremy Hyltona8293132006-02-28 17:58:27 +00001104/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105static expr_ty
1106ast_for_genexp(struct compiling *c, const node *n)
1107{
1108 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1109 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1110 expr_ty elt;
1111 asdl_seq *genexps;
1112 int i, n_fors;
1113 node *ch;
1114
1115 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1116 assert(NCH(n) > 1);
1117
1118 elt = ast_for_expr(c, CHILD(n, 0));
1119 if (!elt)
1120 return NULL;
1121
1122 n_fors = count_gen_fors(n);
1123 if (n_fors == -1)
1124 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001125
1126 genexps = asdl_seq_new(n_fors, c->c_arena);
1127 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001129
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 ch = CHILD(n, 1);
1131 for (i = 0; i < n_fors; i++) {
1132 comprehension_ty ge;
1133 asdl_seq *t;
1134 expr_ty expression;
Neal Norwitzdac090d2006-09-05 03:53:08 +00001135 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136
1137 REQ(ch, gen_for);
1138
Neal Norwitzdac090d2006-09-05 03:53:08 +00001139 for_ch = CHILD(ch, 1);
1140 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001141 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001143 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001144 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001146
Neal Norwitzdac090d2006-09-05 03:53:08 +00001147 /* Check the # of children rather than the length of t, since
1148 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1149 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001150 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001151 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001153 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1154 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001155 expression, NULL, c->c_arena);
1156
1157 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 if (NCH(ch) == 5) {
1161 int j, n_ifs;
1162 asdl_seq *ifs;
1163
1164 ch = CHILD(ch, 4);
1165 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001166 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001168
1169 ifs = asdl_seq_new(n_ifs, c->c_arena);
1170 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 for (j = 0; j < n_ifs; j++) {
1174 REQ(ch, gen_iter);
1175 ch = CHILD(ch, 0);
1176 REQ(ch, gen_if);
1177
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001178 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001179 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001180 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001181 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 if (NCH(ch) == 3)
1183 ch = CHILD(ch, 2);
1184 }
1185 /* on exit, must guarantee that ch is a gen_for */
1186 if (TYPE(ch) == gen_iter)
1187 ch = CHILD(ch, 0);
1188 ge->ifs = ifs;
1189 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001190 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 }
1192
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001193 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194}
1195
1196static expr_ty
1197ast_for_atom(struct compiling *c, const node *n)
1198{
1199 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1200 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1201 */
1202 node *ch = CHILD(n, 0);
1203
1204 switch (TYPE(ch)) {
1205 case NAME:
1206 /* All names start in Load context, but may later be
1207 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001208 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 case STRING: {
1210 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 if (!str)
1212 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001213
1214 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001215 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 }
1217 case NUMBER: {
1218 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 if (!pynum)
1220 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001221
1222 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001223 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 }
1225 case LPAR: /* some parenthesized expressions */
1226 ch = CHILD(n, 1);
1227
1228 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001229 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230
1231 if (TYPE(ch) == yield_expr)
1232 return ast_for_expr(c, ch);
1233
1234 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1235 return ast_for_genexp(c, ch);
1236
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001237 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 case LSQB: /* list (or list comprehension) */
1239 ch = CHILD(n, 1);
1240
1241 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001242 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243
1244 REQ(ch, listmaker);
1245 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1246 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 if (!elts)
1248 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001249
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001250 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 }
1252 else
1253 return ast_for_listcomp(c, ch);
1254 case LBRACE: {
1255 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1256 int i, size;
1257 asdl_seq *keys, *values;
1258
1259 ch = CHILD(n, 1);
1260 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001261 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 if (!keys)
1263 return NULL;
1264
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001265 values = asdl_seq_new(size, c->c_arena);
1266 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268
1269 for (i = 0; i < NCH(ch); i += 4) {
1270 expr_ty expression;
1271
1272 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001273 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001275
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001277
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001279 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001281
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 asdl_seq_SET(values, i / 4, expression);
1283 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001284 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 }
1286 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001287 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 if (!expression)
1289 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001290
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001291 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 }
1293 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001294 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 return NULL;
1296 }
1297}
1298
1299static slice_ty
1300ast_for_slice(struct compiling *c, const node *n)
1301{
1302 node *ch;
1303 expr_ty lower = NULL, upper = NULL, step = NULL;
1304
1305 REQ(n, subscript);
1306
1307 /*
1308 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1309 sliceop: ':' [test]
1310 */
1311 ch = CHILD(n, 0);
1312 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001313 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314
1315 if (NCH(n) == 1 && TYPE(ch) == test) {
1316 /* 'step' variable hold no significance in terms of being used over
1317 other vars */
1318 step = ast_for_expr(c, ch);
1319 if (!step)
1320 return NULL;
1321
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001322 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 }
1324
1325 if (TYPE(ch) == test) {
1326 lower = ast_for_expr(c, ch);
1327 if (!lower)
1328 return NULL;
1329 }
1330
1331 /* If there's an upper bound it's in the second or third position. */
1332 if (TYPE(ch) == COLON) {
1333 if (NCH(n) > 1) {
1334 node *n2 = CHILD(n, 1);
1335
1336 if (TYPE(n2) == test) {
1337 upper = ast_for_expr(c, n2);
1338 if (!upper)
1339 return NULL;
1340 }
1341 }
1342 } else if (NCH(n) > 2) {
1343 node *n2 = CHILD(n, 2);
1344
1345 if (TYPE(n2) == test) {
1346 upper = ast_for_expr(c, n2);
1347 if (!upper)
1348 return NULL;
1349 }
1350 }
1351
1352 ch = CHILD(n, NCH(n) - 1);
1353 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001354 if (NCH(ch) == 1) {
1355 /* No expression, so step is None */
1356 ch = CHILD(ch, 0);
1357 step = Name(new_identifier("None", c->c_arena), Load,
1358 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 if (!step)
1360 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001361 } else {
1362 ch = CHILD(ch, 1);
1363 if (TYPE(ch) == test) {
1364 step = ast_for_expr(c, ch);
1365 if (!step)
1366 return NULL;
1367 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 }
1369 }
1370
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001371 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372}
1373
1374static expr_ty
1375ast_for_binop(struct compiling *c, const node *n)
1376{
1377 /* Must account for a sequence of expressions.
1378 How should A op B op C by represented?
1379 BinOp(BinOp(A, op, B), op, C).
1380 */
1381
1382 int i, nops;
1383 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001384 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385
1386 expr1 = ast_for_expr(c, CHILD(n, 0));
1387 if (!expr1)
1388 return NULL;
1389
1390 expr2 = ast_for_expr(c, CHILD(n, 2));
1391 if (!expr2)
1392 return NULL;
1393
Anthony Baxtera863d332006-04-11 07:43:46 +00001394 newoperator = get_operator(CHILD(n, 1));
1395 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 return NULL;
1397
Anthony Baxtera863d332006-04-11 07:43:46 +00001398 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001399 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 if (!result)
1401 return NULL;
1402
1403 nops = (NCH(n) - 1) / 2;
1404 for (i = 1; i < nops; i++) {
1405 expr_ty tmp_result, tmp;
1406 const node* next_oper = CHILD(n, i * 2 + 1);
1407
Anthony Baxtera863d332006-04-11 07:43:46 +00001408 newoperator = get_operator(next_oper);
1409 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 return NULL;
1411
1412 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1413 if (!tmp)
1414 return NULL;
1415
Anthony Baxtera863d332006-04-11 07:43:46 +00001416 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001417 LINENO(next_oper), next_oper->n_col_offset,
1418 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 if (!tmp)
1420 return NULL;
1421 result = tmp_result;
1422 }
1423 return result;
1424}
1425
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001426static expr_ty
1427ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1428{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001429 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1430 subscriptlist: subscript (',' subscript)* [',']
1431 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1432 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001433 REQ(n, trailer);
1434 if (TYPE(CHILD(n, 0)) == LPAR) {
1435 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001436 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1437 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001438 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001439 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001440 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001441 else if (TYPE(CHILD(n, 0)) == DOT ) {
1442 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001443 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001444 }
1445 else {
1446 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001447 REQ(CHILD(n, 2), RSQB);
1448 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001449 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001450 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1451 if (!slc)
1452 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001453 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1454 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001455 }
1456 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001457 /* The grammar is ambiguous here. The ambiguity is resolved
1458 by treating the sequence as a tuple literal if there are
1459 no slice features.
1460 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001461 int j;
1462 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001463 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001464 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001465 asdl_seq *slices, *elts;
1466 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001467 if (!slices)
1468 return NULL;
1469 for (j = 0; j < NCH(n); j += 2) {
1470 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001471 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001472 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001473 if (slc->kind != Index_kind)
1474 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001475 asdl_seq_SET(slices, j / 2, slc);
1476 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001477 if (!simple) {
1478 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001480 }
1481 /* extract Index values and put them in a Tuple */
1482 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001483 if (!elts)
1484 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001485 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1486 slc = (slice_ty)asdl_seq_GET(slices, j);
1487 assert(slc->kind == Index_kind && slc->v.Index.value);
1488 asdl_seq_SET(elts, j, slc->v.Index.value);
1489 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001490 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001491 if (!e)
1492 return NULL;
1493 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001494 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001495 }
1496 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001497}
1498
1499static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001500ast_for_factor(struct compiling *c, const node *n)
1501{
1502 node *pfactor, *ppower, *patom, *pnum;
1503 expr_ty expression;
1504
1505 /* If the unary - operator is applied to a constant, don't generate
1506 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1507 constant. The peephole optimizer already does something like
1508 this but it doesn't handle the case where the constant is
1509 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1510 PyLongObject.
1511 */
1512 if (TYPE(CHILD(n, 0)) == MINUS
1513 && NCH(n) == 2
1514 && TYPE((pfactor = CHILD(n, 1))) == factor
1515 && NCH(pfactor) == 1
1516 && TYPE((ppower = CHILD(pfactor, 0))) == power
1517 && NCH(ppower) == 1
1518 && TYPE((patom = CHILD(ppower, 0))) == atom
1519 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1520 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1521 if (s == NULL)
1522 return NULL;
1523 s[0] = '-';
1524 strcpy(s + 1, STR(pnum));
1525 PyObject_FREE(STR(pnum));
1526 STR(pnum) = s;
1527 return ast_for_atom(c, patom);
1528 }
1529
1530 expression = ast_for_expr(c, CHILD(n, 1));
1531 if (!expression)
1532 return NULL;
1533
1534 switch (TYPE(CHILD(n, 0))) {
1535 case PLUS:
1536 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1537 c->c_arena);
1538 case MINUS:
1539 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1540 c->c_arena);
1541 case TILDE:
1542 return UnaryOp(Invert, expression, LINENO(n),
1543 n->n_col_offset, c->c_arena);
1544 }
1545 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1546 TYPE(CHILD(n, 0)));
1547 return NULL;
1548}
1549
1550static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001551ast_for_power(struct compiling *c, const node *n)
1552{
1553 /* power: atom trailer* ('**' factor)*
1554 */
1555 int i;
1556 expr_ty e, tmp;
1557 REQ(n, power);
1558 e = ast_for_atom(c, CHILD(n, 0));
1559 if (!e)
1560 return NULL;
1561 if (NCH(n) == 1)
1562 return e;
1563 for (i = 1; i < NCH(n); i++) {
1564 node *ch = CHILD(n, i);
1565 if (TYPE(ch) != trailer)
1566 break;
1567 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001568 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001569 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001570 tmp->lineno = e->lineno;
1571 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001572 e = tmp;
1573 }
1574 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1575 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001576 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001577 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001578 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001579 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001580 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001581 e = tmp;
1582 }
1583 return e;
1584}
1585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586/* Do not name a variable 'expr'! Will cause a compile error.
1587*/
1588
1589static expr_ty
1590ast_for_expr(struct compiling *c, const node *n)
1591{
1592 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001593 test: or_test ['if' or_test 'else' test] | lambdef
1594 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 and_test: not_test ('and' not_test)*
1596 not_test: 'not' not_test | comparison
1597 comparison: expr (comp_op expr)*
1598 expr: xor_expr ('|' xor_expr)*
1599 xor_expr: and_expr ('^' and_expr)*
1600 and_expr: shift_expr ('&' shift_expr)*
1601 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1602 arith_expr: term (('+'|'-') term)*
1603 term: factor (('*'|'/'|'%'|'//') factor)*
1604 factor: ('+'|'-'|'~') factor | power
1605 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001606
1607 As well as modified versions that exist for backward compatibility,
1608 to explicitly allow:
1609 [ x for x in lambda: 0, lambda: 1 ]
1610 (which would be ambiguous without these extra rules)
1611
1612 old_test: or_test | old_lambdef
1613 old_lambdef: 'lambda' [vararglist] ':' old_test
1614
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 */
1616
1617 asdl_seq *seq;
1618 int i;
1619
1620 loop:
1621 switch (TYPE(n)) {
1622 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001623 case old_test:
1624 if (TYPE(CHILD(n, 0)) == lambdef ||
1625 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001627 else if (NCH(n) > 1)
1628 return ast_for_ifexpr(c, n);
1629 /* Fallthrough */
1630 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631 case and_test:
1632 if (NCH(n) == 1) {
1633 n = CHILD(n, 0);
1634 goto loop;
1635 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001636 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637 if (!seq)
1638 return NULL;
1639 for (i = 0; i < NCH(n); i += 2) {
1640 expr_ty e = ast_for_expr(c, CHILD(n, i));
1641 if (!e)
1642 return NULL;
1643 asdl_seq_SET(seq, i / 2, e);
1644 }
1645 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001646 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1647 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001648 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001649 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 case not_test:
1651 if (NCH(n) == 1) {
1652 n = CHILD(n, 0);
1653 goto loop;
1654 }
1655 else {
1656 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1657 if (!expression)
1658 return NULL;
1659
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001660 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1661 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 }
1663 case comparison:
1664 if (NCH(n) == 1) {
1665 n = CHILD(n, 0);
1666 goto loop;
1667 }
1668 else {
1669 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001670 asdl_int_seq *ops;
1671 asdl_seq *cmps;
1672 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 if (!ops)
1674 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001675 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 return NULL;
1678 }
1679 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001680 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681
Anthony Baxtera863d332006-04-11 07:43:46 +00001682 newoperator = ast_for_comp_op(CHILD(n, i));
1683 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686
1687 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001688 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001690 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001692 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 asdl_seq_SET(cmps, i / 2, expression);
1694 }
1695 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001696 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001698 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001700 return Compare(expression, ops, cmps, LINENO(n),
1701 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 }
1703 break;
1704
1705 /* The next five cases all handle BinOps. The main body of code
1706 is the same in each case, but the switch turned inside out to
1707 reuse the code for each type of operator.
1708 */
1709 case expr:
1710 case xor_expr:
1711 case and_expr:
1712 case shift_expr:
1713 case arith_expr:
1714 case term:
1715 if (NCH(n) == 1) {
1716 n = CHILD(n, 0);
1717 goto loop;
1718 }
1719 return ast_for_binop(c, n);
1720 case yield_expr: {
1721 expr_ty exp = NULL;
1722 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001723 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 if (!exp)
1725 return NULL;
1726 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001727 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001729 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 if (NCH(n) == 1) {
1731 n = CHILD(n, 0);
1732 goto loop;
1733 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001734 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001735 case power:
1736 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001738 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739 return NULL;
1740 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001741 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 return NULL;
1743}
1744
1745static expr_ty
1746ast_for_call(struct compiling *c, const node *n, expr_ty func)
1747{
1748 /*
1749 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1750 | '**' test)
1751 argument: [test '='] test [gen_for] # Really [keyword '='] test
1752 */
1753
1754 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001755 asdl_seq *args;
1756 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 expr_ty vararg = NULL, kwarg = NULL;
1758
1759 REQ(n, arglist);
1760
1761 nargs = 0;
1762 nkeywords = 0;
1763 ngens = 0;
1764 for (i = 0; i < NCH(n); i++) {
1765 node *ch = CHILD(n, i);
1766 if (TYPE(ch) == argument) {
1767 if (NCH(ch) == 1)
1768 nargs++;
1769 else if (TYPE(CHILD(ch, 1)) == gen_for)
1770 ngens++;
1771 else
1772 nkeywords++;
1773 }
1774 }
1775 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001776 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 "if not sole argument");
1778 return NULL;
1779 }
1780
1781 if (nargs + nkeywords + ngens > 255) {
1782 ast_error(n, "more than 255 arguments");
1783 return NULL;
1784 }
1785
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001786 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001788 return NULL;
1789 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001791 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 nargs = 0;
1793 nkeywords = 0;
1794 for (i = 0; i < NCH(n); i++) {
1795 node *ch = CHILD(n, i);
1796 if (TYPE(ch) == argument) {
1797 expr_ty e;
1798 if (NCH(ch) == 1) {
Neal Norwitz5ef92242006-05-19 06:43:50 +00001799 if (nkeywords) {
1800 ast_error(CHILD(ch, 0),
1801 "non-keyword arg after keyword arg");
1802 return NULL;
1803 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804 e = ast_for_expr(c, CHILD(ch, 0));
1805 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001806 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 asdl_seq_SET(args, nargs++, e);
1808 }
1809 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1810 e = ast_for_genexp(c, ch);
1811 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001812 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 asdl_seq_SET(args, nargs++, e);
1814 }
1815 else {
1816 keyword_ty kw;
1817 identifier key;
1818
1819 /* CHILD(ch, 0) is test, but must be an identifier? */
1820 e = ast_for_expr(c, CHILD(ch, 0));
1821 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001822 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 /* f(lambda x: x[0] = 3) ends up getting parsed with
1824 * LHS test = lambda x: x[0], and RHS test = 3.
1825 * SF bug 132313 points out that complaining about a keyword
1826 * then is very confusing.
1827 */
1828 if (e->kind == Lambda_kind) {
1829 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 } else if (e->kind != Name_kind) {
1832 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 }
1835 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 e = ast_for_expr(c, CHILD(ch, 2));
1837 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001838 return NULL;
1839 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001841 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 asdl_seq_SET(keywords, nkeywords++, kw);
1843 }
1844 }
1845 else if (TYPE(ch) == STAR) {
1846 vararg = ast_for_expr(c, CHILD(n, i+1));
1847 i++;
1848 }
1849 else if (TYPE(ch) == DOUBLESTAR) {
1850 kwarg = ast_for_expr(c, CHILD(n, i+1));
1851 i++;
1852 }
1853 }
1854
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001855 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856}
1857
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001859ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001861 /* testlist_gexp: test (',' test)* [','] */
1862 /* testlist: test (',' test)* [','] */
1863 /* testlist_safe: test (',' test)+ [','] */
1864 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001866 if (TYPE(n) == testlist_gexp) {
1867 if (NCH(n) > 1)
1868 assert(TYPE(CHILD(n, 1)) != gen_for);
1869 }
1870 else {
1871 assert(TYPE(n) == testlist ||
1872 TYPE(n) == testlist_safe ||
1873 TYPE(n) == testlist1);
1874 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 if (NCH(n) == 1)
1876 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 else {
1878 asdl_seq *tmp = seq_for_testlist(c, n);
1879 if (!tmp)
1880 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001881 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001883}
1884
1885static expr_ty
1886ast_for_testlist_gexp(struct compiling *c, const node* n)
1887{
1888 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1889 /* argument: test [ gen_for ] */
1890 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001891 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001892 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001893 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001894}
1895
1896/* like ast_for_testlist() but returns a sequence */
1897static asdl_seq*
1898ast_for_class_bases(struct compiling *c, const node* n)
1899{
1900 /* testlist: test (',' test)* [','] */
1901 assert(NCH(n) > 0);
1902 REQ(n, testlist);
1903 if (NCH(n) == 1) {
1904 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001905 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001906 if (!bases)
1907 return NULL;
1908 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001909 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001910 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001911 asdl_seq_SET(bases, 0, base);
1912 return bases;
1913 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001914
1915 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916}
1917
1918static stmt_ty
1919ast_for_expr_stmt(struct compiling *c, const node *n)
1920{
1921 REQ(n, expr_stmt);
1922 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1923 | ('=' (yield_expr|testlist))*)
1924 testlist: test (',' test)* [',']
1925 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1926 | '<<=' | '>>=' | '**=' | '//='
1927 test: ... here starts the operator precendence dance
1928 */
1929
1930 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001931 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932 if (!e)
1933 return NULL;
1934
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001935 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 }
1937 else if (TYPE(CHILD(n, 1)) == augassign) {
1938 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00001939 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 node *ch = CHILD(n, 0);
1941
Neal Norwitz0d62a062006-07-30 06:53:31 +00001942 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 if (!expr1)
1944 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001945 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001946 switch (expr1->kind) {
1947 case GeneratorExp_kind:
1948 ast_error(ch, "augmented assignment to generator "
1949 "expression not possible");
1950 return NULL;
Neal Norwitz0d62a062006-07-30 06:53:31 +00001951 case Yield_kind:
1952 ast_error(ch, "augmented assignment to yield "
1953 "expression not possible");
1954 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001955 case Name_kind: {
1956 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1957 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1958 ast_error(ch, "assignment to None");
1959 return NULL;
1960 }
1961 break;
1962 }
1963 case Attribute_kind:
1964 case Subscript_kind:
1965 break;
1966 default:
1967 ast_error(ch, "illegal expression for augmented "
1968 "assignment");
1969 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 }
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001971 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972
1973 ch = CHILD(n, 2);
1974 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001975 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 else
Neal Norwitz0d62a062006-07-30 06:53:31 +00001977 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001978 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 return NULL;
1980
Anthony Baxtera863d332006-04-11 07:43:46 +00001981 newoperator = ast_for_augassign(CHILD(n, 1));
1982 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 return NULL;
1984
Anthony Baxtera863d332006-04-11 07:43:46 +00001985 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 }
1987 else {
1988 int i;
1989 asdl_seq *targets;
1990 node *value;
1991 expr_ty expression;
1992
1993 /* a normal assignment */
1994 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001995 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 if (!targets)
1997 return NULL;
1998 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001999 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 node *ch = CHILD(n, i);
2001 if (TYPE(ch) == yield_expr) {
2002 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002005 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006
2007 /* set context to assign */
2008 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010
Neal Norwitz84456bd2005-12-18 03:16:20 +00002011 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013
2014 asdl_seq_SET(targets, i / 2, e);
2015 }
2016 value = CHILD(n, NCH(n) - 1);
2017 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002018 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 else
2020 expression = ast_for_expr(c, value);
2021 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002022 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002023 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025}
2026
2027static stmt_ty
2028ast_for_print_stmt(struct compiling *c, const node *n)
2029{
2030 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2031 | '>>' test [ (',' test)+ [','] ] )
2032 */
2033 expr_ty dest = NULL, expression;
2034 asdl_seq *seq;
2035 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002036 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037
2038 REQ(n, print_stmt);
2039 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2040 dest = ast_for_expr(c, CHILD(n, 2));
2041 if (!dest)
2042 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002043 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002045 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002047 return NULL;
2048 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002050 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002052 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 }
2054 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002055 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056}
2057
2058static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002059ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060{
2061 asdl_seq *seq;
2062 int i;
2063 expr_ty e;
2064
2065 REQ(n, exprlist);
2066
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002067 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 if (!seq)
2069 return NULL;
2070 for (i = 0; i < NCH(n); i += 2) {
2071 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002072 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002073 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002074 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002075 if (context && !set_context(e, context, CHILD(n, i)))
2076 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 }
2078 return seq;
2079}
2080
2081static stmt_ty
2082ast_for_del_stmt(struct compiling *c, const node *n)
2083{
2084 asdl_seq *expr_list;
2085
2086 /* del_stmt: 'del' exprlist */
2087 REQ(n, del_stmt);
2088
2089 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2090 if (!expr_list)
2091 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002092 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093}
2094
2095static stmt_ty
2096ast_for_flow_stmt(struct compiling *c, const node *n)
2097{
2098 /*
2099 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2100 | yield_stmt
2101 break_stmt: 'break'
2102 continue_stmt: 'continue'
2103 return_stmt: 'return' [testlist]
2104 yield_stmt: yield_expr
2105 yield_expr: 'yield' testlist
2106 raise_stmt: 'raise' [test [',' test [',' test]]]
2107 */
2108 node *ch;
2109
2110 REQ(n, flow_stmt);
2111 ch = CHILD(n, 0);
2112 switch (TYPE(ch)) {
2113 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002114 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002116 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 case yield_stmt: { /* will reduce to yield_expr */
2118 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2119 if (!exp)
2120 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002121 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 }
2123 case return_stmt:
2124 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002125 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002127 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 if (!expression)
2129 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002130 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 }
2132 case raise_stmt:
2133 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002134 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 else if (NCH(ch) == 2) {
2136 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2137 if (!expression)
2138 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002139 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 }
2141 else if (NCH(ch) == 4) {
2142 expr_ty expr1, expr2;
2143
2144 expr1 = ast_for_expr(c, CHILD(ch, 1));
2145 if (!expr1)
2146 return NULL;
2147 expr2 = ast_for_expr(c, CHILD(ch, 3));
2148 if (!expr2)
2149 return NULL;
2150
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002151 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 }
2153 else if (NCH(ch) == 6) {
2154 expr_ty expr1, expr2, expr3;
2155
2156 expr1 = ast_for_expr(c, CHILD(ch, 1));
2157 if (!expr1)
2158 return NULL;
2159 expr2 = ast_for_expr(c, CHILD(ch, 3));
2160 if (!expr2)
2161 return NULL;
2162 expr3 = ast_for_expr(c, CHILD(ch, 5));
2163 if (!expr3)
2164 return NULL;
2165
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002166 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 }
2168 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002169 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 "unexpected flow_stmt: %d", TYPE(ch));
2171 return NULL;
2172 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002173
2174 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2175 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176}
2177
2178static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002179alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180{
2181 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002182 import_as_name: NAME ['as' NAME]
2183 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 dotted_name: NAME ('.' NAME)*
2185 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002186 PyObject *str;
2187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 loop:
2189 switch (TYPE(n)) {
2190 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002191 str = NULL;
2192 if (NCH(n) == 3) {
2193 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2194 ast_error(n, "must use 'as' in import");
2195 return NULL;
2196 }
2197 str = NEW_IDENTIFIER(CHILD(n, 2));
2198 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002199 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 case dotted_as_name:
2201 if (NCH(n) == 1) {
2202 n = CHILD(n, 0);
2203 goto loop;
2204 }
2205 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002206 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00002207 if (!a)
2208 return NULL;
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002209 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2210 ast_error(n, "must use 'as' in import");
2211 return NULL;
2212 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 assert(!a->asname);
2214 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2215 return a;
2216 }
2217 break;
2218 case dotted_name:
2219 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002220 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221 else {
2222 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002223 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002224 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 char *s;
2226
2227 len = 0;
2228 for (i = 0; i < NCH(n); i += 2)
2229 /* length of string plus one for the dot */
2230 len += strlen(STR(CHILD(n, i))) + 1;
2231 len--; /* the last name doesn't have a dot */
2232 str = PyString_FromStringAndSize(NULL, len);
2233 if (!str)
2234 return NULL;
2235 s = PyString_AS_STRING(str);
2236 if (!s)
2237 return NULL;
2238 for (i = 0; i < NCH(n); i += 2) {
2239 char *sch = STR(CHILD(n, i));
2240 strcpy(s, STR(CHILD(n, i)));
2241 s += strlen(sch);
2242 *s++ = '.';
2243 }
2244 --s;
2245 *s = '\0';
2246 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002247 PyArena_AddPyObject(c->c_arena, str);
2248 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 }
2250 break;
2251 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002252 str = PyString_InternFromString("*");
2253 PyArena_AddPyObject(c->c_arena, str);
2254 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002256 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 "unexpected import name: %d", TYPE(n));
2258 return NULL;
2259 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002260
2261 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 return NULL;
2263}
2264
2265static stmt_ty
2266ast_for_import_stmt(struct compiling *c, const node *n)
2267{
2268 /*
2269 import_stmt: import_name | import_from
2270 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002271 import_from: 'from' ('.'* dotted_name | '.') 'import'
2272 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002274 int lineno;
2275 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 int i;
2277 asdl_seq *aliases;
2278
2279 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002280 lineno = LINENO(n);
2281 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002283 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002285 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002286 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 if (!aliases)
2288 return NULL;
2289 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002290 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002291 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 asdl_seq_SET(aliases, i / 2, import_alias);
2294 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002295 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002297 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002299 int idx, ndots = 0;
2300 alias_ty mod = NULL;
2301 identifier modname;
2302
2303 /* Count the number of dots (for relative imports) and check for the
2304 optional module name */
2305 for (idx = 1; idx < NCH(n); idx++) {
2306 if (TYPE(CHILD(n, idx)) == dotted_name) {
2307 mod = alias_for_import_name(c, CHILD(n, idx));
2308 idx++;
2309 break;
2310 } else if (TYPE(CHILD(n, idx)) != DOT) {
2311 break;
2312 }
2313 ndots++;
2314 }
2315 idx++; /* skip over the 'import' keyword */
2316 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002317 case STAR:
2318 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002319 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002320 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002321 if (ndots) {
2322 ast_error(n, "'import *' not allowed with 'from .'");
2323 return NULL;
2324 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002325 break;
2326 case LPAR:
2327 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002328 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002329 n_children = NCH(n);
2330 break;
2331 case import_as_names:
2332 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002333 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002334 n_children = NCH(n);
2335 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 ast_error(n, "trailing comma not allowed without"
2337 " surrounding parentheses");
2338 return NULL;
2339 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002340 break;
2341 default:
2342 ast_error(n, "Unexpected node-type in from-import");
2343 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002344 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002346 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002347 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349
2350 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002351 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002352 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002353 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002355 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002357 else {
2358 for (i = 0; i < NCH(n); i += 2) {
2359 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2360 if (!import_alias)
2361 return NULL;
2362 asdl_seq_SET(aliases, i / 2, import_alias);
2363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002365 if (mod != NULL)
2366 modname = mod->name;
2367 else
2368 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002369 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002370 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371 }
Neal Norwitz79792652005-11-14 04:25:03 +00002372 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 "unknown import statement: starts with command '%s'",
2374 STR(CHILD(n, 0)));
2375 return NULL;
2376}
2377
2378static stmt_ty
2379ast_for_global_stmt(struct compiling *c, const node *n)
2380{
2381 /* global_stmt: 'global' NAME (',' NAME)* */
2382 identifier name;
2383 asdl_seq *s;
2384 int i;
2385
2386 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002387 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 if (!s)
2389 return NULL;
2390 for (i = 1; i < NCH(n); i += 2) {
2391 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002392 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 asdl_seq_SET(s, i / 2, name);
2395 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002396 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397}
2398
2399static stmt_ty
2400ast_for_exec_stmt(struct compiling *c, const node *n)
2401{
2402 expr_ty expr1, globals = NULL, locals = NULL;
2403 int n_children = NCH(n);
2404 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002405 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 "poorly formed 'exec' statement: %d parts to statement",
2407 n_children);
2408 return NULL;
2409 }
2410
2411 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2412 REQ(n, exec_stmt);
2413 expr1 = ast_for_expr(c, CHILD(n, 1));
2414 if (!expr1)
2415 return NULL;
2416 if (n_children >= 4) {
2417 globals = ast_for_expr(c, CHILD(n, 3));
2418 if (!globals)
2419 return NULL;
2420 }
2421 if (n_children == 6) {
2422 locals = ast_for_expr(c, CHILD(n, 5));
2423 if (!locals)
2424 return NULL;
2425 }
2426
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002427 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428}
2429
2430static stmt_ty
2431ast_for_assert_stmt(struct compiling *c, const node *n)
2432{
2433 /* assert_stmt: 'assert' test [',' test] */
2434 REQ(n, assert_stmt);
2435 if (NCH(n) == 2) {
2436 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2437 if (!expression)
2438 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002439 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 }
2441 else if (NCH(n) == 4) {
2442 expr_ty expr1, expr2;
2443
2444 expr1 = ast_for_expr(c, CHILD(n, 1));
2445 if (!expr1)
2446 return NULL;
2447 expr2 = ast_for_expr(c, CHILD(n, 3));
2448 if (!expr2)
2449 return NULL;
2450
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002451 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 }
Neal Norwitz79792652005-11-14 04:25:03 +00002453 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 "improper number of parts to 'assert' statement: %d",
2455 NCH(n));
2456 return NULL;
2457}
2458
2459static asdl_seq *
2460ast_for_suite(struct compiling *c, const node *n)
2461{
2462 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002463 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 stmt_ty s;
2465 int i, total, num, end, pos = 0;
2466 node *ch;
2467
2468 REQ(n, suite);
2469
2470 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002471 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 if (!seq)
2473 return NULL;
2474 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2475 n = CHILD(n, 0);
2476 /* simple_stmt always ends with a NEWLINE,
2477 and may have a trailing SEMI
2478 */
2479 end = NCH(n) - 1;
2480 if (TYPE(CHILD(n, end - 1)) == SEMI)
2481 end--;
2482 /* loop by 2 to skip semi-colons */
2483 for (i = 0; i < end; i += 2) {
2484 ch = CHILD(n, i);
2485 s = ast_for_stmt(c, ch);
2486 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002487 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 asdl_seq_SET(seq, pos++, s);
2489 }
2490 }
2491 else {
2492 for (i = 2; i < (NCH(n) - 1); i++) {
2493 ch = CHILD(n, i);
2494 REQ(ch, stmt);
2495 num = num_stmts(ch);
2496 if (num == 1) {
2497 /* small_stmt or compound_stmt with only one child */
2498 s = ast_for_stmt(c, ch);
2499 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002500 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 asdl_seq_SET(seq, pos++, s);
2502 }
2503 else {
2504 int j;
2505 ch = CHILD(ch, 0);
2506 REQ(ch, simple_stmt);
2507 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002508 /* statement terminates with a semi-colon ';' */
2509 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002510 assert((j + 1) == NCH(ch));
2511 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002512 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 s = ast_for_stmt(c, CHILD(ch, j));
2514 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002515 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 asdl_seq_SET(seq, pos++, s);
2517 }
2518 }
2519 }
2520 }
2521 assert(pos == seq->size);
2522 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523}
2524
2525static stmt_ty
2526ast_for_if_stmt(struct compiling *c, const node *n)
2527{
2528 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2529 ['else' ':' suite]
2530 */
2531 char *s;
2532
2533 REQ(n, if_stmt);
2534
2535 if (NCH(n) == 4) {
2536 expr_ty expression;
2537 asdl_seq *suite_seq;
2538
2539 expression = ast_for_expr(c, CHILD(n, 1));
2540 if (!expression)
2541 return NULL;
2542 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002543 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 return NULL;
2545
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002546 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002548
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 s = STR(CHILD(n, 4));
2550 /* s[2], the third character in the string, will be
2551 's' for el_s_e, or
2552 'i' for el_i_f
2553 */
2554 if (s[2] == 's') {
2555 expr_ty expression;
2556 asdl_seq *seq1, *seq2;
2557
2558 expression = ast_for_expr(c, CHILD(n, 1));
2559 if (!expression)
2560 return NULL;
2561 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002562 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 return NULL;
2564 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002565 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 return NULL;
2567
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002568 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 }
2570 else if (s[2] == 'i') {
2571 int i, n_elif, has_else = 0;
2572 asdl_seq *orelse = NULL;
2573 n_elif = NCH(n) - 4;
2574 /* must reference the child n_elif+1 since 'else' token is third,
2575 not fourth, child from the end. */
2576 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2577 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2578 has_else = 1;
2579 n_elif -= 3;
2580 }
2581 n_elif /= 4;
2582
2583 if (has_else) {
2584 expr_ty expression;
2585 asdl_seq *seq1, *seq2;
2586
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002587 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 if (!orelse)
2589 return NULL;
2590 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002591 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002594 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002597 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599
2600 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002601 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002602 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 /* the just-created orelse handled the last elif */
2604 n_elif--;
2605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606
2607 for (i = 0; i < n_elif; i++) {
2608 int off = 5 + (n_elif - i - 1) * 4;
2609 expr_ty expression;
2610 asdl_seq *suite_seq;
Anthony Baxtera863d332006-04-11 07:43:46 +00002611 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2612 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 return NULL;
2614 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002615 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002618 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620
Anthony Baxtera863d332006-04-11 07:43:46 +00002621 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002623 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Anthony Baxtera863d332006-04-11 07:43:46 +00002624 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 }
2626 return If(ast_for_expr(c, CHILD(n, 1)),
2627 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002628 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002630
2631 PyErr_Format(PyExc_SystemError,
2632 "unexpected token in 'if' statement: %s", s);
2633 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634}
2635
2636static stmt_ty
2637ast_for_while_stmt(struct compiling *c, const node *n)
2638{
2639 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2640 REQ(n, while_stmt);
2641
2642 if (NCH(n) == 4) {
2643 expr_ty expression;
2644 asdl_seq *suite_seq;
2645
2646 expression = ast_for_expr(c, CHILD(n, 1));
2647 if (!expression)
2648 return NULL;
2649 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002650 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002652 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 }
2654 else if (NCH(n) == 7) {
2655 expr_ty expression;
2656 asdl_seq *seq1, *seq2;
2657
2658 expression = ast_for_expr(c, CHILD(n, 1));
2659 if (!expression)
2660 return NULL;
2661 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002662 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 return NULL;
2664 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002665 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 return NULL;
2667
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002668 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002670
2671 PyErr_Format(PyExc_SystemError,
2672 "wrong number of tokens for 'while' statement: %d",
2673 NCH(n));
2674 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675}
2676
2677static stmt_ty
2678ast_for_for_stmt(struct compiling *c, const node *n)
2679{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002680 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681 expr_ty expression;
2682 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002683 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2685 REQ(n, for_stmt);
2686
2687 if (NCH(n) == 9) {
2688 seq = ast_for_suite(c, CHILD(n, 8));
2689 if (!seq)
2690 return NULL;
2691 }
2692
Neal Norwitzedef2be2006-07-12 05:26:17 +00002693 node_target = CHILD(n, 1);
2694 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002695 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002697 /* Check the # of children rather than the length of _target, since
2698 for x, in ... has 1 element in _target, but still requires a Tuple. */
2699 if (NCH(node_target) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00002700 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002702 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002704 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002705 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 return NULL;
2707 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002708 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 return NULL;
2710
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002711 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2712 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713}
2714
2715static excepthandler_ty
2716ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2717{
2718 /* except_clause: 'except' [test [',' test]] */
2719 REQ(exc, except_clause);
2720 REQ(body, suite);
2721
2722 if (NCH(exc) == 1) {
2723 asdl_seq *suite_seq = ast_for_suite(c, body);
2724 if (!suite_seq)
2725 return NULL;
2726
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002727 return excepthandler(NULL, 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) == 2) {
2731 expr_ty expression;
2732 asdl_seq *suite_seq;
2733
2734 expression = ast_for_expr(c, CHILD(exc, 1));
2735 if (!expression)
2736 return NULL;
2737 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002738 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 return NULL;
2740
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002741 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2742 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 }
2744 else if (NCH(exc) == 4) {
2745 asdl_seq *suite_seq;
2746 expr_ty expression;
2747 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2748 if (!e)
2749 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002750 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 return NULL;
2752 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002753 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 return NULL;
2755 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002756 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 return NULL;
2758
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002759 return excepthandler(expression, e, suite_seq, LINENO(exc),
2760 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002762
2763 PyErr_Format(PyExc_SystemError,
2764 "wrong number of children for 'except' clause: %d",
2765 NCH(exc));
2766 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767}
2768
2769static stmt_ty
2770ast_for_try_stmt(struct compiling *c, const node *n)
2771{
Neal Norwitzf599f422005-12-17 21:33:47 +00002772 const int nch = NCH(n);
2773 int n_except = (nch - 3)/3;
2774 asdl_seq *body, *orelse = NULL, *finally = NULL;
2775
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 REQ(n, try_stmt);
2777
Neal Norwitzf599f422005-12-17 21:33:47 +00002778 body = ast_for_suite(c, CHILD(n, 2));
2779 if (body == NULL)
2780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781
Neal Norwitzf599f422005-12-17 21:33:47 +00002782 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2783 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2784 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2785 /* we can assume it's an "else",
2786 because nch >= 9 for try-else-finally and
2787 it would otherwise have a type of except_clause */
2788 orelse = ast_for_suite(c, CHILD(n, nch - 4));
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 finally = ast_for_suite(c, CHILD(n, nch - 1));
2795 if (finally == NULL)
2796 return NULL;
2797 n_except--;
2798 }
2799 else {
2800 /* we can assume it's an "else",
2801 otherwise it would have a type of except_clause */
2802 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2803 if (orelse == NULL)
2804 return NULL;
2805 n_except--;
2806 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002808 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002809 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 return NULL;
2811 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002812
2813 if (n_except > 0) {
2814 int i;
2815 stmt_ty except_st;
2816 /* process except statements to create a try ... except */
2817 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2818 if (handlers == NULL)
2819 return NULL;
2820
2821 for (i = 0; i < n_except; i++) {
2822 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2823 CHILD(n, 5 + i * 3));
2824 if (!e)
2825 return NULL;
2826 asdl_seq_SET(handlers, i, e);
2827 }
2828
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002829 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2830 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002831 if (!finally)
2832 return except_st;
2833
2834 /* if a 'finally' is present too, we nest the TryExcept within a
2835 TryFinally to emulate try ... except ... finally */
2836 body = asdl_seq_new(1, c->c_arena);
2837 if (body == NULL)
2838 return NULL;
2839 asdl_seq_SET(body, 0, except_st);
2840 }
2841
2842 /* must be a try ... finally (except clauses are in body, if any exist) */
2843 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002844 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845}
2846
Guido van Rossumc2e20742006-02-27 22:32:47 +00002847static expr_ty
2848ast_for_with_var(struct compiling *c, const node *n)
2849{
2850 REQ(n, with_var);
2851 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2852 ast_error(n, "expected \"with [expr] as [var]\"");
2853 return NULL;
2854 }
2855 return ast_for_expr(c, CHILD(n, 1));
2856}
2857
2858/* with_stmt: 'with' test [ with_var ] ':' suite */
2859static stmt_ty
2860ast_for_with_stmt(struct compiling *c, const node *n)
2861{
2862 expr_ty context_expr, optional_vars = NULL;
2863 int suite_index = 3; /* skip 'with', test, and ':' */
2864 asdl_seq *suite_seq;
2865
2866 assert(TYPE(n) == with_stmt);
2867 context_expr = ast_for_expr(c, CHILD(n, 1));
2868 if (TYPE(CHILD(n, 2)) == with_var) {
2869 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2870
2871 if (!optional_vars) {
2872 return NULL;
2873 }
2874 if (!set_context(optional_vars, Store, n)) {
2875 return NULL;
2876 }
2877 suite_index = 4;
2878 }
2879
2880 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2881 if (!suite_seq) {
2882 return NULL;
2883 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002884 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2885 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002886}
2887
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888static stmt_ty
2889ast_for_classdef(struct compiling *c, const node *n)
2890{
2891 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 asdl_seq *bases, *s;
2893
2894 REQ(n, classdef);
2895
2896 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2897 ast_error(n, "assignment to None");
2898 return NULL;
2899 }
2900
2901 if (NCH(n) == 4) {
2902 s = ast_for_suite(c, CHILD(n, 3));
2903 if (!s)
2904 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002905 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2906 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 }
2908 /* check for empty base list */
2909 if (TYPE(CHILD(n,3)) == RPAR) {
2910 s = ast_for_suite(c, CHILD(n,5));
2911 if (!s)
2912 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002913 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2914 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 }
2916
2917 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002918 bases = ast_for_class_bases(c, CHILD(n, 3));
2919 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921
2922 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002923 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002925 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2926 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927}
2928
2929static stmt_ty
2930ast_for_stmt(struct compiling *c, const node *n)
2931{
2932 if (TYPE(n) == stmt) {
2933 assert(NCH(n) == 1);
2934 n = CHILD(n, 0);
2935 }
2936 if (TYPE(n) == simple_stmt) {
2937 assert(num_stmts(n) == 1);
2938 n = CHILD(n, 0);
2939 }
2940 if (TYPE(n) == small_stmt) {
2941 REQ(n, small_stmt);
2942 n = CHILD(n, 0);
2943 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2944 | flow_stmt | import_stmt | global_stmt | exec_stmt
2945 | assert_stmt
2946 */
2947 switch (TYPE(n)) {
2948 case expr_stmt:
2949 return ast_for_expr_stmt(c, n);
2950 case print_stmt:
2951 return ast_for_print_stmt(c, n);
2952 case del_stmt:
2953 return ast_for_del_stmt(c, n);
2954 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002955 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956 case flow_stmt:
2957 return ast_for_flow_stmt(c, n);
2958 case import_stmt:
2959 return ast_for_import_stmt(c, n);
2960 case global_stmt:
2961 return ast_for_global_stmt(c, n);
2962 case exec_stmt:
2963 return ast_for_exec_stmt(c, n);
2964 case assert_stmt:
2965 return ast_for_assert_stmt(c, n);
2966 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002967 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2969 TYPE(n), NCH(n));
2970 return NULL;
2971 }
2972 }
2973 else {
2974 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2975 | funcdef | classdef
2976 */
2977 node *ch = CHILD(n, 0);
2978 REQ(n, compound_stmt);
2979 switch (TYPE(ch)) {
2980 case if_stmt:
2981 return ast_for_if_stmt(c, ch);
2982 case while_stmt:
2983 return ast_for_while_stmt(c, ch);
2984 case for_stmt:
2985 return ast_for_for_stmt(c, ch);
2986 case try_stmt:
2987 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002988 case with_stmt:
2989 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 case funcdef:
2991 return ast_for_funcdef(c, ch);
2992 case classdef:
2993 return ast_for_classdef(c, ch);
2994 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002995 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2997 TYPE(n), NCH(n));
2998 return NULL;
2999 }
3000 }
3001}
3002
3003static PyObject *
3004parsenumber(const char *s)
3005{
3006 const char *end;
3007 long x;
3008 double dx;
3009#ifndef WITHOUT_COMPLEX
3010 Py_complex c;
3011 int imflag;
3012#endif
3013
3014 errno = 0;
3015 end = s + strlen(s) - 1;
3016#ifndef WITHOUT_COMPLEX
3017 imflag = *end == 'j' || *end == 'J';
3018#endif
3019 if (*end == 'l' || *end == 'L')
3020 return PyLong_FromString((char *)s, (char **)0, 0);
3021 if (s[0] == '0') {
3022 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3023 if (x < 0 && errno == 0) {
3024 return PyLong_FromString((char *)s,
3025 (char **)0,
3026 0);
3027 }
3028 }
3029 else
3030 x = PyOS_strtol((char *)s, (char **)&end, 0);
3031 if (*end == '\0') {
3032 if (errno != 0)
3033 return PyLong_FromString((char *)s, (char **)0, 0);
3034 return PyInt_FromLong(x);
3035 }
3036 /* XXX Huge floats may silently fail */
3037#ifndef WITHOUT_COMPLEX
3038 if (imflag) {
3039 c.real = 0.;
3040 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003041 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 PyFPE_END_PROTECT(c)
3043 return PyComplex_FromCComplex(c);
3044 }
3045 else
3046#endif
3047 {
3048 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003049 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050 PyFPE_END_PROTECT(dx)
3051 return PyFloat_FromDouble(dx);
3052 }
3053}
3054
3055static PyObject *
3056decode_utf8(const char **sPtr, const char *end, char* encoding)
3057{
3058#ifndef Py_USING_UNICODE
3059 Py_FatalError("decode_utf8 should not be called in this build.");
3060 return NULL;
3061#else
3062 PyObject *u, *v;
3063 char *s, *t;
3064 t = s = (char *)*sPtr;
3065 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3066 while (s < end && (*s & 0x80)) s++;
3067 *sPtr = s;
3068 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3069 if (u == NULL)
3070 return NULL;
3071 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3072 Py_DECREF(u);
3073 return v;
3074#endif
3075}
3076
3077static PyObject *
3078decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3079{
3080 PyObject *v, *u;
3081 char *buf;
3082 char *p;
3083 const char *end;
3084 if (encoding == NULL) {
3085 buf = (char *)s;
3086 u = NULL;
3087 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3088 buf = (char *)s;
3089 u = NULL;
3090 } else {
3091 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3092 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3093 if (u == NULL)
3094 return NULL;
3095 p = buf = PyString_AsString(u);
3096 end = s + len;
3097 while (s < end) {
3098 if (*s == '\\') {
3099 *p++ = *s++;
3100 if (*s & 0x80) {
3101 strcpy(p, "u005c");
3102 p += 5;
3103 }
3104 }
3105 if (*s & 0x80) { /* XXX inefficient */
3106 PyObject *w;
3107 char *r;
Martin v. Löwis66851282006-04-22 11:40:03 +00003108 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 w = decode_utf8(&s, end, "utf-16-be");
3110 if (w == NULL) {
3111 Py_DECREF(u);
3112 return NULL;
3113 }
3114 r = PyString_AsString(w);
3115 rn = PyString_Size(w);
3116 assert(rn % 2 == 0);
3117 for (i = 0; i < rn; i += 2) {
3118 sprintf(p, "\\u%02x%02x",
3119 r[i + 0] & 0xFF,
3120 r[i + 1] & 0xFF);
3121 p += 6;
3122 }
3123 Py_DECREF(w);
3124 } else {
3125 *p++ = *s++;
3126 }
3127 }
3128 len = p - buf;
3129 s = buf;
3130 }
3131 if (rawmode)
3132 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3133 else
3134 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3135 Py_XDECREF(u);
3136 return v;
3137}
3138
3139/* s is a Python string literal, including the bracketing quote characters,
3140 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3141 * parsestr parses it, and returns the decoded Python string object.
3142 */
3143static PyObject *
3144parsestr(const char *s, const char *encoding)
3145{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003147 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 int rawmode = 0;
3149 int need_encoding;
3150 int unicode = 0;
3151
3152 if (isalpha(quote) || quote == '_') {
3153 if (quote == 'u' || quote == 'U') {
3154 quote = *++s;
3155 unicode = 1;
3156 }
3157 if (quote == 'r' || quote == 'R') {
3158 quote = *++s;
3159 rawmode = 1;
3160 }
3161 }
3162 if (quote != '\'' && quote != '\"') {
3163 PyErr_BadInternalCall();
3164 return NULL;
3165 }
3166 s++;
3167 len = strlen(s);
3168 if (len > INT_MAX) {
3169 PyErr_SetString(PyExc_OverflowError,
3170 "string to parse is too long");
3171 return NULL;
3172 }
3173 if (s[--len] != quote) {
3174 PyErr_BadInternalCall();
3175 return NULL;
3176 }
3177 if (len >= 4 && s[0] == quote && s[1] == quote) {
3178 s += 2;
3179 len -= 2;
3180 if (s[--len] != quote || s[--len] != quote) {
3181 PyErr_BadInternalCall();
3182 return NULL;
3183 }
3184 }
3185#ifdef Py_USING_UNICODE
3186 if (unicode || Py_UnicodeFlag) {
3187 return decode_unicode(s, len, rawmode, encoding);
3188 }
3189#endif
3190 need_encoding = (encoding != NULL &&
3191 strcmp(encoding, "utf-8") != 0 &&
3192 strcmp(encoding, "iso-8859-1") != 0);
3193 if (rawmode || strchr(s, '\\') == NULL) {
3194 if (need_encoding) {
3195#ifndef Py_USING_UNICODE
3196 /* This should not happen - we never see any other
3197 encoding. */
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003198 Py_FatalError(
3199 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003201 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 if (u == NULL)
3203 return NULL;
3204 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3205 Py_DECREF(u);
3206 return v;
3207#endif
3208 } else {
3209 return PyString_FromStringAndSize(s, len);
3210 }
3211 }
3212
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003213 return PyString_DecodeEscape(s, len, NULL, unicode,
3214 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215}
3216
3217/* Build a Python string object out of a STRING atom. This takes care of
3218 * compile-time literal catenation, calling parsestr() on each piece, and
3219 * pasting the intermediate results together.
3220 */
3221static PyObject *
3222parsestrplus(struct compiling *c, const node *n)
3223{
3224 PyObject *v;
3225 int i;
3226 REQ(CHILD(n, 0), STRING);
3227 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3228 /* String literal concatenation */
3229 for (i = 1; i < NCH(n); i++) {
3230 PyObject *s;
3231 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3232 if (s == NULL)
3233 goto onError;
3234 if (PyString_Check(v) && PyString_Check(s)) {
3235 PyString_ConcatAndDel(&v, s);
3236 if (v == NULL)
3237 goto onError;
3238 }
3239#ifdef Py_USING_UNICODE
3240 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003241 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003243 Py_DECREF(v);
3244 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003245 if (v == NULL)
3246 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247 }
3248#endif
3249 }
3250 }
3251 return v;
3252
3253 onError:
3254 Py_XDECREF(v);
3255 return NULL;
3256}