blob: 5555cf7ddf8ae3d2a3f34b8fae51938ff4e38bbb [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 *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000028static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
29 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000030static expr_ty ast_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032
33/* Note different signature for ast_for_call */
34static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
35
36static PyObject *parsenumber(const char *);
37static PyObject *parsestr(const char *s, const char *encoding);
38static PyObject *parsestrplus(struct compiling *, const node *n);
39
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000041#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042#endif
43
Neal Norwitzadb69fc2005-12-17 20:54:49 +000044static identifier
45new_identifier(const char* n, PyArena *arena) {
46 PyObject* id = PyString_InternFromString(n);
47 PyArena_AddPyObject(arena, id);
48 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000049}
50
Neal Norwitzadb69fc2005-12-17 20:54:49 +000051#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052
53/* This routine provides an invalid object for the syntax error.
54 The outermost routine must unpack this error and create the
55 proper object. We do this so that we don't have to pass
56 the filename to everything function.
57
58 XXX Maybe we should just pass the filename...
59*/
60
61static int
62ast_error(const node *n, const char *errstr)
63{
64 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
65 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000066 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000067 PyErr_SetObject(PyExc_SyntaxError, u);
68 Py_DECREF(u);
69 return 0;
70}
71
72static void
73ast_error_finish(const char *filename)
74{
75 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000076 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077
78 assert(PyErr_Occurred());
79 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000080 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
82 PyErr_Fetch(&type, &value, &tback);
83 errstr = PyTuple_GetItem(value, 0);
84 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000085 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086 Py_INCREF(errstr);
87 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000088 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000089 Py_DECREF(errstr);
90 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000091 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092 Py_DECREF(value);
93
94 loc = PyErr_ProgramText(filename, lineno);
95 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000096 Py_INCREF(Py_None);
97 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +000099 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000101 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000102 Py_DECREF(errstr);
103 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000104 }
Georg Brandl7784f122006-05-26 20:04:44 +0000105 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106 Py_DECREF(errstr);
107 Py_DECREF(tmp);
108 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000109 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 PyErr_Restore(type, value, tback);
111}
112
113/* num_stmts() returns number of contained statements.
114
115 Use this routine to determine how big a sequence is needed for
116 the statements in a parse tree. Its raison d'etre is this bit of
117 grammar:
118
119 stmt: simple_stmt | compound_stmt
120 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
121
122 A simple_stmt can contain multiple small_stmt elements joined
123 by semicolons. If the arg is a simple_stmt, the number of
124 small_stmt elements is returned.
125*/
126
127static int
128num_stmts(const node *n)
129{
130 int i, l;
131 node *ch;
132
133 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000134 case single_input:
135 if (TYPE(CHILD(n, 0)) == NEWLINE)
136 return 0;
137 else
138 return num_stmts(CHILD(n, 0));
139 case file_input:
140 l = 0;
141 for (i = 0; i < NCH(n); i++) {
142 ch = CHILD(n, i);
143 if (TYPE(ch) == stmt)
144 l += num_stmts(ch);
145 }
146 return l;
147 case stmt:
148 return num_stmts(CHILD(n, 0));
149 case compound_stmt:
150 return 1;
151 case simple_stmt:
152 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
153 case suite:
154 if (NCH(n) == 1)
155 return num_stmts(CHILD(n, 0));
156 else {
157 l = 0;
158 for (i = 2; i < (NCH(n) - 1); i++)
159 l += num_stmts(CHILD(n, i));
160 return l;
161 }
162 default: {
163 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000165 sprintf(buf, "Non-statement found: %d %d\n",
166 TYPE(n), NCH(n));
167 Py_FatalError(buf);
168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169 }
170 assert(0);
171 return 0;
172}
173
174/* Transform the CST rooted at node * to the appropriate AST
175*/
176
177mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000178PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000179 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000181 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182 asdl_seq *stmts = NULL;
183 stmt_ty s;
184 node *ch;
185 struct compiling c;
186
187 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000188 c.c_encoding = "utf-8";
189 if (TYPE(n) == encoding_decl) {
190 ast_error(n, "encoding declaration in Unicode string");
191 goto error;
192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000194 c.c_encoding = STR(n);
195 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000197 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000199 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
Jeremy Hyltona8293132006-02-28 17:58:27 +0000201 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000203 case file_input:
204 stmts = asdl_seq_new(num_stmts(n), arena);
205 if (!stmts)
206 return NULL;
207 for (i = 0; i < NCH(n) - 1; i++) {
208 ch = CHILD(n, i);
209 if (TYPE(ch) == NEWLINE)
210 continue;
211 REQ(ch, stmt);
212 num = num_stmts(ch);
213 if (num == 1) {
214 s = ast_for_stmt(&c, ch);
215 if (!s)
216 goto error;
217 asdl_seq_SET(stmts, k++, s);
218 }
219 else {
220 ch = CHILD(ch, 0);
221 REQ(ch, simple_stmt);
222 for (j = 0; j < num; j++) {
223 s = ast_for_stmt(&c, CHILD(ch, j * 2));
224 if (!s)
225 goto error;
226 asdl_seq_SET(stmts, k++, s);
227 }
228 }
229 }
230 return Module(stmts, arena);
231 case eval_input: {
232 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000234 /* XXX Why not gen_for here? */
235 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
236 if (!testlist_ast)
237 goto error;
238 return Expression(testlist_ast, arena);
239 }
240 case single_input:
241 if (TYPE(CHILD(n, 0)) == NEWLINE) {
242 stmts = asdl_seq_new(1, arena);
243 if (!stmts)
244 goto error;
245 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
246 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000247 if (!asdl_seq_GET(stmts, 0))
248 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000249 return Interactive(stmts, arena);
250 }
251 else {
252 n = CHILD(n, 0);
253 num = num_stmts(n);
254 stmts = asdl_seq_new(num, arena);
255 if (!stmts)
256 goto error;
257 if (num == 1) {
258 s = ast_for_stmt(&c, n);
259 if (!s)
260 goto error;
261 asdl_seq_SET(stmts, 0, s);
262 }
263 else {
264 /* Only a simple_stmt can contain multiple statements. */
265 REQ(n, simple_stmt);
266 for (i = 0; i < NCH(n); i += 2) {
267 if (TYPE(CHILD(n, i)) == NEWLINE)
268 break;
269 s = ast_for_stmt(&c, CHILD(n, i));
270 if (!s)
271 goto error;
272 asdl_seq_SET(stmts, i / 2, s);
273 }
274 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000276 return Interactive(stmts, arena);
277 }
278 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000279 PyErr_Format(PyExc_SystemError,
280 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000281 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282 }
283 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284 ast_error_finish(filename);
285 return NULL;
286}
287
288/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
289*/
290
291static operator_ty
292get_operator(const node *n)
293{
294 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000295 case VBAR:
296 return BitOr;
297 case CIRCUMFLEX:
298 return BitXor;
299 case AMPER:
300 return BitAnd;
301 case LEFTSHIFT:
302 return LShift;
303 case RIGHTSHIFT:
304 return RShift;
305 case PLUS:
306 return Add;
307 case MINUS:
308 return Sub;
309 case STAR:
310 return Mult;
311 case SLASH:
312 return Div;
313 case DOUBLESLASH:
314 return FloorDiv;
315 case PERCENT:
316 return Mod;
317 default:
318 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319 }
320}
321
Jeremy Hyltona8293132006-02-28 17:58:27 +0000322/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323
324 Only sets context for expr kinds that "can appear in assignment context"
325 (according to ../Parser/Python.asdl). For other expr kinds, it sets
326 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327*/
328
329static int
330set_context(expr_ty e, expr_context_ty ctx, const node *n)
331{
332 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000333 /* If a particular expression type can't be used for assign / delete,
334 set expr_name to its name and an error message will be generated.
335 */
336 const char* expr_name = NULL;
337
338 /* The ast defines augmented store and load contexts, but the
339 implementation here doesn't actually use them. The code may be
340 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000341 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000342 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000343 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000344 */
345 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346
347 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000348 case Attribute_kind:
349 if (ctx == Store &&
350 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
351 return ast_error(n, "assignment to None");
352 }
353 e->v.Attribute.ctx = ctx;
354 break;
355 case Subscript_kind:
356 e->v.Subscript.ctx = ctx;
357 break;
358 case Name_kind:
359 if (ctx == Store &&
360 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
361 return ast_error(n, "assignment to None");
362 }
363 e->v.Name.ctx = ctx;
364 break;
365 case List_kind:
366 e->v.List.ctx = ctx;
367 s = e->v.List.elts;
368 break;
369 case Tuple_kind:
370 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
371 return ast_error(n, "can't assign to ()");
372 e->v.Tuple.ctx = ctx;
373 s = e->v.Tuple.elts;
374 break;
375 case Lambda_kind:
376 expr_name = "lambda";
377 break;
378 case Call_kind:
379 expr_name = "function call";
380 break;
381 case BoolOp_kind:
382 case BinOp_kind:
383 case UnaryOp_kind:
384 expr_name = "operator";
385 break;
386 case GeneratorExp_kind:
387 expr_name = "generator expression";
388 break;
389 case Yield_kind:
390 expr_name = "yield expression";
391 break;
392 case ListComp_kind:
393 expr_name = "list comprehension";
394 break;
395 case Dict_kind:
396 case Num_kind:
397 case Str_kind:
398 expr_name = "literal";
399 break;
400 case Compare_kind:
401 expr_name = "comparison";
402 break;
403 case Repr_kind:
404 expr_name = "repr";
405 break;
406 case IfExp_kind:
407 expr_name = "conditional expression";
408 break;
409 default:
410 PyErr_Format(PyExc_SystemError,
411 "unexpected expression in assignment %d (line %d)",
412 e->kind, e->lineno);
413 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000415 /* Check for error string set by switch */
416 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000417 char buf[300];
418 PyOS_snprintf(buf, sizeof(buf),
419 "can't %s %s",
420 ctx == Store ? "assign to" : "delete",
421 expr_name);
422 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000423 }
424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000426 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427 */
428 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000429 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000431 for (i = 0; i < asdl_seq_LEN(s); i++) {
432 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
433 return 0;
434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 }
436 return 1;
437}
438
439static operator_ty
440ast_for_augassign(const node *n)
441{
442 REQ(n, augassign);
443 n = CHILD(n, 0);
444 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000445 case '+':
446 return Add;
447 case '-':
448 return Sub;
449 case '/':
450 if (STR(n)[1] == '/')
451 return FloorDiv;
452 else
453 return Div;
454 case '%':
455 return Mod;
456 case '<':
457 return LShift;
458 case '>':
459 return RShift;
460 case '&':
461 return BitAnd;
462 case '^':
463 return BitXor;
464 case '|':
465 return BitOr;
466 case '*':
467 if (STR(n)[1] == '*')
468 return Pow;
469 else
470 return Mult;
471 default:
472 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
473 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474 }
475}
476
477static cmpop_ty
478ast_for_comp_op(const node *n)
479{
480 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000481 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 */
483 REQ(n, comp_op);
484 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000485 n = CHILD(n, 0);
486 switch (TYPE(n)) {
487 case LESS:
488 return Lt;
489 case GREATER:
490 return Gt;
491 case EQEQUAL: /* == */
492 return Eq;
493 case LESSEQUAL:
494 return LtE;
495 case GREATEREQUAL:
496 return GtE;
497 case NOTEQUAL:
498 return NotEq;
499 case NAME:
500 if (strcmp(STR(n), "in") == 0)
501 return In;
502 if (strcmp(STR(n), "is") == 0)
503 return Is;
504 default:
505 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
506 STR(n));
507 return (cmpop_ty)0;
508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 }
510 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000511 /* handle "not in" and "is not" */
512 switch (TYPE(CHILD(n, 0))) {
513 case NAME:
514 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
515 return NotIn;
516 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
517 return IsNot;
518 default:
519 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
520 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
521 return (cmpop_ty)0;
522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 }
Neal Norwitz79792652005-11-14 04:25:03 +0000524 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000525 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000526 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527}
528
529static asdl_seq *
530seq_for_testlist(struct compiling *c, const node *n)
531{
532 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000533 asdl_seq *seq;
534 expr_ty expression;
535 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000536 assert(TYPE(n) == testlist ||
537 TYPE(n) == listmaker ||
538 TYPE(n) == testlist_gexp ||
539 TYPE(n) == testlist_safe ||
540 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000542 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000544 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
546 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000547 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000549 expression = ast_for_expr(c, CHILD(n, i));
550 if (!expression)
551 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000553 assert(i / 2 < seq->size);
554 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 }
556 return seq;
557}
558
559static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000560compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561{
562 int i, len = (NCH(n) + 1) / 2;
563 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000564 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000566 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567
Neal Norwitz3a230172006-09-22 08:18:10 +0000568 /* fpdef: NAME | '(' fplist ')'
569 fplist: fpdef (',' fpdef)* [',']
570 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000573 const node *fpdef_node = CHILD(n, 2*i);
574 const node *child;
575 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000576set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000577 /* fpdef_node is either a NAME or an fplist */
578 child = CHILD(fpdef_node, 0);
579 if (TYPE(child) == NAME) {
580 if (!strcmp(STR(child), "None")) {
581 ast_error(child, "assignment to None");
582 return NULL;
583 }
584 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
585 child->n_col_offset, c->c_arena);
586 }
587 else {
588 assert(TYPE(fpdef_node) == fpdef);
589 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
590 child = CHILD(fpdef_node, 1);
591 assert(TYPE(child) == fplist);
592 /* NCH == 1 means we have (x), we need to elide the extra parens */
593 if (NCH(child) == 1) {
594 fpdef_node = CHILD(child, 0);
595 assert(TYPE(fpdef_node) == fpdef);
596 goto set_name;
597 }
598 arg = compiler_complex_args(c, child);
599 }
600 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 }
602
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000603 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000604 if (!set_context(result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 return result;
607}
608
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609
Jeremy Hyltona8293132006-02-28 17:58:27 +0000610/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
612static arguments_ty
613ast_for_arguments(struct compiling *c, const node *n)
614{
615 /* parameters: '(' [varargslist] ')'
616 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000617 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000619 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 asdl_seq *args, *defaults;
621 identifier vararg = NULL, kwarg = NULL;
622 node *ch;
623
624 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000625 if (NCH(n) == 2) /* () as argument list */
626 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
627 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628 }
629 REQ(n, varargslist);
630
631 /* first count the number of normal args & defaults */
632 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000633 ch = CHILD(n, i);
634 if (TYPE(ch) == fpdef)
635 n_args++;
636 if (TYPE(ch) == EQUAL)
637 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000639 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000641 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000642 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000644 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645
646 /* fpdef: NAME | '(' fplist ')'
647 fplist: fpdef (',' fpdef)* [',']
648 */
649 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000650 j = 0; /* index for defaults */
651 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000653 ch = CHILD(n, i);
654 switch (TYPE(ch)) {
655 case fpdef:
656 handle_fpdef:
657 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
658 anything other than EQUAL or a comma? */
659 /* XXX Should NCH(n) check be made a separate check? */
660 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
661 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
662 if (!expression)
663 goto error;
664 assert(defaults != NULL);
665 asdl_seq_SET(defaults, j++, expression);
666 i += 2;
667 found_default = 1;
668 }
669 else if (found_default) {
670 ast_error(n,
671 "non-default argument follows default argument");
672 goto error;
673 }
674 if (NCH(ch) == 3) {
675 ch = CHILD(ch, 1);
676 /* def foo((x)): is not complex, special case. */
677 if (NCH(ch) != 1) {
678 /* We have complex arguments, setup for unpacking. */
679 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000680 if (!asdl_seq_GET(args, k-1))
681 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000682 } else {
683 /* def foo((x)): setup for checking NAME below. */
684 /* Loop because there can be many parens and tuple
685 unpacking mixed in. */
686 ch = CHILD(ch, 0);
687 assert(TYPE(ch) == fpdef);
688 goto handle_fpdef;
689 }
690 }
691 if (TYPE(CHILD(ch, 0)) == NAME) {
692 expr_ty name;
693 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
694 ast_error(CHILD(ch, 0), "assignment to None");
695 goto error;
696 }
697 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
698 Param, LINENO(ch), ch->n_col_offset,
699 c->c_arena);
700 if (!name)
701 goto error;
702 asdl_seq_SET(args, k++, name);
703
704 }
705 i += 2; /* the name and the comma */
706 break;
707 case STAR:
708 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
709 ast_error(CHILD(n, i+1), "assignment to None");
710 goto error;
711 }
712 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
713 i += 3;
714 break;
715 case DOUBLESTAR:
716 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
717 ast_error(CHILD(n, i+1), "assignment to None");
718 goto error;
719 }
720 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
721 i += 3;
722 break;
723 default:
724 PyErr_Format(PyExc_SystemError,
725 "unexpected node in varargslist: %d @ %d",
726 TYPE(ch), i);
727 goto error;
728 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 }
730
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000731 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732
733 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000734 Py_XDECREF(vararg);
735 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736 return NULL;
737}
738
739static expr_ty
740ast_for_dotted_name(struct compiling *c, const node *n)
741{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000742 expr_ty e;
743 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000744 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 int i;
746
747 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000748
749 lineno = LINENO(n);
750 col_offset = n->n_col_offset;
751
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 id = NEW_IDENTIFIER(CHILD(n, 0));
753 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000754 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000755 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000757 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
759 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000760 id = NEW_IDENTIFIER(CHILD(n, i));
761 if (!id)
762 return NULL;
763 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
764 if (!e)
765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 }
767
768 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769}
770
771static expr_ty
772ast_for_decorator(struct compiling *c, const node *n)
773{
774 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
775 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000776 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
778 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000779 REQ(CHILD(n, 0), AT);
780 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
782 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
783 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000784 return NULL;
785
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000787 d = name_expr;
788 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 }
790 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000791 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
792 n->n_col_offset, c->c_arena);
793 if (!d)
794 return NULL;
795 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 }
797 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000798 d = ast_for_call(c, CHILD(n, 3), name_expr);
799 if (!d)
800 return NULL;
801 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 }
803
804 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805}
806
807static asdl_seq*
808ast_for_decorators(struct compiling *c, const node *n)
809{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000810 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000811 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 int i;
813
814 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000815 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000817 return NULL;
818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000820 d = ast_for_decorator(c, CHILD(n, i));
821 if (!d)
822 return NULL;
823 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 }
825 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826}
827
828static stmt_ty
829ast_for_funcdef(struct compiling *c, const node *n)
830{
831 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000832 identifier name;
833 arguments_ty args;
834 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 asdl_seq *decorator_seq = NULL;
836 int name_i;
837
838 REQ(n, funcdef);
839
840 if (NCH(n) == 6) { /* decorators are present */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000841 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
842 if (!decorator_seq)
843 return NULL;
844 name_i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 }
846 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000847 name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 }
849
850 name = NEW_IDENTIFIER(CHILD(n, name_i));
851 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000854 ast_error(CHILD(n, name_i), "assignment to None");
855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 }
857 args = ast_for_arguments(c, CHILD(n, name_i + 1));
858 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 body = ast_for_suite(c, CHILD(n, name_i + 3));
861 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000864 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000865 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866}
867
868static expr_ty
869ast_for_lambdef(struct compiling *c, const node *n)
870{
871 /* lambdef: 'lambda' [varargslist] ':' test */
872 arguments_ty args;
873 expr_ty expression;
874
875 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000876 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
877 if (!args)
878 return NULL;
879 expression = ast_for_expr(c, CHILD(n, 2));
880 if (!expression)
881 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 }
883 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000884 args = ast_for_arguments(c, CHILD(n, 1));
885 if (!args)
886 return NULL;
887 expression = ast_for_expr(c, CHILD(n, 3));
888 if (!expression)
889 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890 }
891
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000892 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893}
894
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000895static expr_ty
896ast_for_ifexpr(struct compiling *c, const node *n)
897{
898 /* test: or_test 'if' or_test 'else' test */
899 expr_ty expression, body, orelse;
900
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000901 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000902 body = ast_for_expr(c, CHILD(n, 0));
903 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000904 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000905 expression = ast_for_expr(c, CHILD(n, 2));
906 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000907 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000908 orelse = ast_for_expr(c, CHILD(n, 4));
909 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000910 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000911 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000912 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000913}
914
Neal Norwitze4d4f002006-09-05 03:58:26 +0000915/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
916 so there is only a single version. Possibly for loops can also re-use
917 the code.
918*/
919
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920/* Count the number of 'for' loop in a list comprehension.
921
922 Helper for ast_for_listcomp().
923*/
924
925static int
926count_list_fors(const node *n)
927{
928 int n_fors = 0;
929 node *ch = CHILD(n, 1);
930
931 count_list_for:
932 n_fors++;
933 REQ(ch, list_for);
934 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000935 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000937 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938 count_list_iter:
939 REQ(ch, list_iter);
940 ch = CHILD(ch, 0);
941 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000942 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000944 if (NCH(ch) == 3) {
945 ch = CHILD(ch, 2);
946 goto count_list_iter;
947 }
948 else
949 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000951
952 /* Should never be reached */
953 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
954 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955}
956
957/* Count the number of 'if' statements in a list comprehension.
958
959 Helper for ast_for_listcomp().
960*/
961
962static int
963count_list_ifs(const node *n)
964{
965 int n_ifs = 0;
966
967 count_list_iter:
968 REQ(n, list_iter);
969 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000970 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 n = CHILD(n, 0);
972 REQ(n, list_if);
973 n_ifs++;
974 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000975 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 n = CHILD(n, 2);
977 goto count_list_iter;
978}
979
980static expr_ty
981ast_for_listcomp(struct compiling *c, const node *n)
982{
983 /* listmaker: test ( list_for | (',' test)* [','] )
984 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
985 list_iter: list_for | list_if
986 list_if: 'if' test [list_iter]
987 testlist_safe: test [(',' test)+ [',']]
988 */
989 expr_ty elt;
990 asdl_seq *listcomps;
991 int i, n_fors;
992 node *ch;
993
994 REQ(n, listmaker);
995 assert(NCH(n) > 1);
996
997 elt = ast_for_expr(c, CHILD(n, 0));
998 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000999 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000
1001 n_fors = count_list_fors(n);
1002 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001005 listcomps = asdl_seq_new(n_fors, c->c_arena);
1006 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001007 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001008
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 ch = CHILD(n, 1);
1010 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001011 comprehension_ty lc;
1012 asdl_seq *t;
1013 expr_ty expression;
1014 node *for_ch;
1015
1016 REQ(ch, list_for);
1017
1018 for_ch = CHILD(ch, 1);
1019 t = ast_for_exprlist(c, for_ch, Store);
1020 if (!t)
1021 return NULL;
1022 expression = ast_for_testlist(c, CHILD(ch, 3));
1023 if (!expression)
1024 return NULL;
1025
1026 /* Check the # of children rather than the length of t, since
1027 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1028 */
1029 if (NCH(for_ch) == 1)
1030 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1031 c->c_arena);
1032 else
1033 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1034 c->c_arena),
1035 expression, NULL, c->c_arena);
1036 if (!lc)
1037 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001039 if (NCH(ch) == 5) {
1040 int j, n_ifs;
1041 asdl_seq *ifs;
1042 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001044 ch = CHILD(ch, 4);
1045 n_ifs = count_list_ifs(ch);
1046 if (n_ifs == -1)
1047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001049 ifs = asdl_seq_new(n_ifs, c->c_arena);
1050 if (!ifs)
1051 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001053 for (j = 0; j < n_ifs; j++) {
1054 REQ(ch, list_iter);
1055 ch = CHILD(ch, 0);
1056 REQ(ch, list_if);
1057
1058 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1059 if (!list_for_expr)
1060 return NULL;
1061
1062 asdl_seq_SET(ifs, j, list_for_expr);
1063 if (NCH(ch) == 3)
1064 ch = CHILD(ch, 2);
1065 }
1066 /* on exit, must guarantee that ch is a list_for */
1067 if (TYPE(ch) == list_iter)
1068 ch = CHILD(ch, 0);
1069 lc->ifs = ifs;
1070 }
1071 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 }
1073
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001074 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075}
1076
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001077/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078
1079 Helper for ast_for_genexp().
1080*/
1081
1082static int
1083count_gen_fors(const node *n)
1084{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001085 int n_fors = 0;
1086 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087
1088 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001089 n_fors++;
1090 REQ(ch, gen_for);
1091 if (NCH(ch) == 5)
1092 ch = CHILD(ch, 4);
1093 else
1094 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001096 REQ(ch, gen_iter);
1097 ch = CHILD(ch, 0);
1098 if (TYPE(ch) == gen_for)
1099 goto count_gen_for;
1100 else if (TYPE(ch) == gen_if) {
1101 if (NCH(ch) == 3) {
1102 ch = CHILD(ch, 2);
1103 goto count_gen_iter;
1104 }
1105 else
1106 return n_fors;
1107 }
1108
1109 /* Should never be reached */
1110 PyErr_SetString(PyExc_SystemError,
1111 "logic error in count_gen_fors");
1112 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113}
1114
1115/* Count the number of 'if' statements in a generator expression.
1116
1117 Helper for ast_for_genexp().
1118*/
1119
1120static int
1121count_gen_ifs(const node *n)
1122{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001123 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001125 while (1) {
1126 REQ(n, gen_iter);
1127 if (TYPE(CHILD(n, 0)) == gen_for)
1128 return n_ifs;
1129 n = CHILD(n, 0);
1130 REQ(n, gen_if);
1131 n_ifs++;
1132 if (NCH(n) == 2)
1133 return n_ifs;
1134 n = CHILD(n, 2);
1135 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136}
1137
Jeremy Hyltona8293132006-02-28 17:58:27 +00001138/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139static expr_ty
1140ast_for_genexp(struct compiling *c, const node *n)
1141{
1142 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001143 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 expr_ty elt;
1145 asdl_seq *genexps;
1146 int i, n_fors;
1147 node *ch;
1148
1149 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1150 assert(NCH(n) > 1);
1151
1152 elt = ast_for_expr(c, CHILD(n, 0));
1153 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001154 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155
1156 n_fors = count_gen_fors(n);
1157 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001158 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159
1160 genexps = asdl_seq_new(n_fors, c->c_arena);
1161 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001162 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 ch = CHILD(n, 1);
1165 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001166 comprehension_ty ge;
1167 asdl_seq *t;
1168 expr_ty expression;
1169 node *for_ch;
1170
1171 REQ(ch, gen_for);
1172
1173 for_ch = CHILD(ch, 1);
1174 t = ast_for_exprlist(c, for_ch, Store);
1175 if (!t)
1176 return NULL;
1177 expression = ast_for_expr(c, CHILD(ch, 3));
1178 if (!expression)
1179 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001180
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001181 /* Check the # of children rather than the length of t, since
1182 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1183 if (NCH(for_ch) == 1)
1184 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1185 NULL, c->c_arena);
1186 else
1187 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1188 c->c_arena),
1189 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001190
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001191 if (!ge)
1192 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001193
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001194 if (NCH(ch) == 5) {
1195 int j, n_ifs;
1196 asdl_seq *ifs;
1197
1198 ch = CHILD(ch, 4);
1199 n_ifs = count_gen_ifs(ch);
1200 if (n_ifs == -1)
1201 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001202
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001203 ifs = asdl_seq_new(n_ifs, c->c_arena);
1204 if (!ifs)
1205 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001206
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001207 for (j = 0; j < n_ifs; j++) {
1208 REQ(ch, gen_iter);
1209 ch = CHILD(ch, 0);
1210 REQ(ch, gen_if);
1211
1212 expression = ast_for_expr(c, CHILD(ch, 1));
1213 if (!expression)
1214 return NULL;
1215 asdl_seq_SET(ifs, j, expression);
1216 if (NCH(ch) == 3)
1217 ch = CHILD(ch, 2);
1218 }
1219 /* on exit, must guarantee that ch is a gen_for */
1220 if (TYPE(ch) == gen_iter)
1221 ch = CHILD(ch, 0);
1222 ge->ifs = ifs;
1223 }
1224 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 }
1226
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001227 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228}
1229
1230static expr_ty
1231ast_for_atom(struct compiling *c, const node *n)
1232{
1233 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1234 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1235 */
1236 node *ch = CHILD(n, 0);
1237
1238 switch (TYPE(ch)) {
1239 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001240 /* All names start in Load context, but may later be
1241 changed. */
1242 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1243 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001245 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001246 if (!str) {
1247 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1248 PyObject *type, *value, *tback, *errstr;
1249 PyErr_Fetch(&type, &value, &tback);
1250 errstr = ((PyUnicodeErrorObject *)value)->reason;
1251 if (errstr) {
1252 char *s = "";
1253 char buf[128];
1254 s = PyString_AsString(errstr);
1255 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1256 ast_error(n, buf);
1257 } else {
1258 ast_error(n, "(unicode error) unknown error");
1259 }
1260 Py_DECREF(type);
1261 Py_DECREF(value);
1262 Py_XDECREF(tback);
1263 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001264 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001265 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001266 PyArena_AddPyObject(c->c_arena, str);
1267 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 }
1269 case NUMBER: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001270 PyObject *pynum = parsenumber(STR(ch));
1271 if (!pynum)
1272 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001273
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001274 PyArena_AddPyObject(c->c_arena, pynum);
1275 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 }
1277 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001278 ch = CHILD(n, 1);
1279
1280 if (TYPE(ch) == RPAR)
1281 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1282
1283 if (TYPE(ch) == yield_expr)
1284 return ast_for_expr(c, ch);
1285
1286 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1287 return ast_for_genexp(c, ch);
1288
1289 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001291 ch = CHILD(n, 1);
1292
1293 if (TYPE(ch) == RSQB)
1294 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1295
1296 REQ(ch, listmaker);
1297 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1298 asdl_seq *elts = seq_for_testlist(c, ch);
1299 if (!elts)
1300 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001301
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001302 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1303 }
1304 else
1305 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001307 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1308 int i, size;
1309 asdl_seq *keys, *values;
1310
1311 ch = CHILD(n, 1);
1312 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1313 keys = asdl_seq_new(size, c->c_arena);
1314 if (!keys)
1315 return NULL;
1316
1317 values = asdl_seq_new(size, c->c_arena);
1318 if (!values)
1319 return NULL;
1320
1321 for (i = 0; i < NCH(ch); i += 4) {
1322 expr_ty expression;
1323
1324 expression = ast_for_expr(c, CHILD(ch, i));
1325 if (!expression)
1326 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001327
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001328 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001329
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001330 expression = ast_for_expr(c, CHILD(ch, i + 2));
1331 if (!expression)
1332 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001333
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001334 asdl_seq_SET(values, i / 4, expression);
1335 }
1336 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 }
1338 case BACKQUOTE: { /* repr */
Christian Heimes729ab152007-11-23 09:10:36 +00001339 if (Py_Py3kWarningFlag &&
1340 PyErr_Warn(PyExc_DeprecationWarning,
1341 "backquote not supported in 3.x") < 0)
1342 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001343 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
1344 if (!expression)
1345 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001346
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001347 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 }
1349 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001350 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1351 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 }
1353}
1354
1355static slice_ty
1356ast_for_slice(struct compiling *c, const node *n)
1357{
1358 node *ch;
1359 expr_ty lower = NULL, upper = NULL, step = NULL;
1360
1361 REQ(n, subscript);
1362
1363 /*
1364 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1365 sliceop: ':' [test]
1366 */
1367 ch = CHILD(n, 0);
1368 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001369 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370
1371 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001372 /* 'step' variable hold no significance in terms of being used over
1373 other vars */
1374 step = ast_for_expr(c, ch);
1375 if (!step)
1376 return NULL;
1377
1378 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 }
1380
1381 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001382 lower = ast_for_expr(c, ch);
1383 if (!lower)
1384 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 }
1386
1387 /* If there's an upper bound it's in the second or third position. */
1388 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001389 if (NCH(n) > 1) {
1390 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001392 if (TYPE(n2) == test) {
1393 upper = ast_for_expr(c, n2);
1394 if (!upper)
1395 return NULL;
1396 }
1397 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001399 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001401 if (TYPE(n2) == test) {
1402 upper = ast_for_expr(c, n2);
1403 if (!upper)
1404 return NULL;
1405 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 }
1407
1408 ch = CHILD(n, NCH(n) - 1);
1409 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001410 if (NCH(ch) == 1) {
1411 /* No expression, so step is None */
1412 ch = CHILD(ch, 0);
1413 step = Name(new_identifier("None", c->c_arena), Load,
1414 LINENO(ch), ch->n_col_offset, c->c_arena);
1415 if (!step)
1416 return NULL;
1417 } else {
1418 ch = CHILD(ch, 1);
1419 if (TYPE(ch) == test) {
1420 step = ast_for_expr(c, ch);
1421 if (!step)
1422 return NULL;
1423 }
1424 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 }
1426
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001427 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428}
1429
1430static expr_ty
1431ast_for_binop(struct compiling *c, const node *n)
1432{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001433 /* Must account for a sequence of expressions.
1434 How should A op B op C by represented?
1435 BinOp(BinOp(A, op, B), op, C).
1436 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001438 int i, nops;
1439 expr_ty expr1, expr2, result;
1440 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001442 expr1 = ast_for_expr(c, CHILD(n, 0));
1443 if (!expr1)
1444 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001446 expr2 = ast_for_expr(c, CHILD(n, 2));
1447 if (!expr2)
1448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001450 newoperator = get_operator(CHILD(n, 1));
1451 if (!newoperator)
1452 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001454 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1455 c->c_arena);
1456 if (!result)
1457 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001459 nops = (NCH(n) - 1) / 2;
1460 for (i = 1; i < nops; i++) {
1461 expr_ty tmp_result, tmp;
1462 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001464 newoperator = get_operator(next_oper);
1465 if (!newoperator)
1466 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001468 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1469 if (!tmp)
1470 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001472 tmp_result = BinOp(result, newoperator, tmp,
1473 LINENO(next_oper), next_oper->n_col_offset,
1474 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001475 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001476 return NULL;
1477 result = tmp_result;
1478 }
1479 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480}
1481
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001482static expr_ty
1483ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1484{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001485 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1486 subscriptlist: subscript (',' subscript)* [',']
1487 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1488 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001489 REQ(n, trailer);
1490 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001491 if (NCH(n) == 2)
1492 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1493 n->n_col_offset, c->c_arena);
1494 else
1495 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001496 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001497 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001498 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1499 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001500 }
1501 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001502 REQ(CHILD(n, 0), LSQB);
1503 REQ(CHILD(n, 2), RSQB);
1504 n = CHILD(n, 1);
1505 if (NCH(n) == 1) {
1506 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1507 if (!slc)
1508 return NULL;
1509 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1510 c->c_arena);
1511 }
1512 else {
1513 /* The grammar is ambiguous here. The ambiguity is resolved
1514 by treating the sequence as a tuple literal if there are
1515 no slice features.
1516 */
1517 int j;
1518 slice_ty slc;
1519 expr_ty e;
1520 bool simple = true;
1521 asdl_seq *slices, *elts;
1522 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1523 if (!slices)
1524 return NULL;
1525 for (j = 0; j < NCH(n); j += 2) {
1526 slc = ast_for_slice(c, CHILD(n, j));
1527 if (!slc)
1528 return NULL;
1529 if (slc->kind != Index_kind)
1530 simple = false;
1531 asdl_seq_SET(slices, j / 2, slc);
1532 }
1533 if (!simple) {
1534 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1535 Load, LINENO(n), n->n_col_offset, c->c_arena);
1536 }
1537 /* extract Index values and put them in a Tuple */
1538 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1539 if (!elts)
1540 return NULL;
1541 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1542 slc = (slice_ty)asdl_seq_GET(slices, j);
1543 assert(slc->kind == Index_kind && slc->v.Index.value);
1544 asdl_seq_SET(elts, j, slc->v.Index.value);
1545 }
1546 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1547 if (!e)
1548 return NULL;
1549 return Subscript(left_expr, Index(e, c->c_arena),
1550 Load, LINENO(n), n->n_col_offset, c->c_arena);
1551 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001552 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001553}
1554
1555static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001556ast_for_factor(struct compiling *c, const node *n)
1557{
1558 node *pfactor, *ppower, *patom, *pnum;
1559 expr_ty expression;
1560
1561 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001562 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001563 constant. The peephole optimizer already does something like
1564 this but it doesn't handle the case where the constant is
1565 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1566 PyLongObject.
1567 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001568 if (TYPE(CHILD(n, 0)) == MINUS &&
1569 NCH(n) == 2 &&
1570 TYPE((pfactor = CHILD(n, 1))) == factor &&
1571 NCH(pfactor) == 1 &&
1572 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1573 NCH(ppower) == 1 &&
1574 TYPE((patom = CHILD(ppower, 0))) == atom &&
1575 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1576 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1577 if (s == NULL)
1578 return NULL;
1579 s[0] = '-';
1580 strcpy(s + 1, STR(pnum));
1581 PyObject_FREE(STR(pnum));
1582 STR(pnum) = s;
1583 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001584 }
1585
1586 expression = ast_for_expr(c, CHILD(n, 1));
1587 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001588 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001589
1590 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001591 case PLUS:
1592 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1593 c->c_arena);
1594 case MINUS:
1595 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1596 c->c_arena);
1597 case TILDE:
1598 return UnaryOp(Invert, expression, LINENO(n),
1599 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001600 }
1601 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001602 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001603 return NULL;
1604}
1605
1606static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001607ast_for_power(struct compiling *c, const node *n)
1608{
1609 /* power: atom trailer* ('**' factor)*
1610 */
1611 int i;
1612 expr_ty e, tmp;
1613 REQ(n, power);
1614 e = ast_for_atom(c, CHILD(n, 0));
1615 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001616 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001617 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001618 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001619 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001620 node *ch = CHILD(n, i);
1621 if (TYPE(ch) != trailer)
1622 break;
1623 tmp = ast_for_trailer(c, ch, e);
1624 if (!tmp)
1625 return NULL;
1626 tmp->lineno = e->lineno;
1627 tmp->col_offset = e->col_offset;
1628 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001629 }
1630 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001631 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1632 if (!f)
1633 return NULL;
1634 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1635 if (!tmp)
1636 return NULL;
1637 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001638 }
1639 return e;
1640}
1641
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642/* Do not name a variable 'expr'! Will cause a compile error.
1643*/
1644
1645static expr_ty
1646ast_for_expr(struct compiling *c, const node *n)
1647{
1648 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001649 test: or_test ['if' or_test 'else' test] | lambdef
1650 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651 and_test: not_test ('and' not_test)*
1652 not_test: 'not' not_test | comparison
1653 comparison: expr (comp_op expr)*
1654 expr: xor_expr ('|' xor_expr)*
1655 xor_expr: and_expr ('^' and_expr)*
1656 and_expr: shift_expr ('&' shift_expr)*
1657 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1658 arith_expr: term (('+'|'-') term)*
1659 term: factor (('*'|'/'|'%'|'//') factor)*
1660 factor: ('+'|'-'|'~') factor | power
1661 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001662
1663 As well as modified versions that exist for backward compatibility,
1664 to explicitly allow:
1665 [ x for x in lambda: 0, lambda: 1 ]
1666 (which would be ambiguous without these extra rules)
1667
1668 old_test: or_test | old_lambdef
1669 old_lambdef: 'lambda' [vararglist] ':' old_test
1670
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 */
1672
1673 asdl_seq *seq;
1674 int i;
1675
1676 loop:
1677 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001678 case test:
1679 case old_test:
1680 if (TYPE(CHILD(n, 0)) == lambdef ||
1681 TYPE(CHILD(n, 0)) == old_lambdef)
1682 return ast_for_lambdef(c, CHILD(n, 0));
1683 else if (NCH(n) > 1)
1684 return ast_for_ifexpr(c, n);
1685 /* Fallthrough */
1686 case or_test:
1687 case and_test:
1688 if (NCH(n) == 1) {
1689 n = CHILD(n, 0);
1690 goto loop;
1691 }
1692 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1693 if (!seq)
1694 return NULL;
1695 for (i = 0; i < NCH(n); i += 2) {
1696 expr_ty e = ast_for_expr(c, CHILD(n, i));
1697 if (!e)
1698 return NULL;
1699 asdl_seq_SET(seq, i / 2, e);
1700 }
1701 if (!strcmp(STR(CHILD(n, 1)), "and"))
1702 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1703 c->c_arena);
1704 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1705 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1706 case not_test:
1707 if (NCH(n) == 1) {
1708 n = CHILD(n, 0);
1709 goto loop;
1710 }
1711 else {
1712 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1713 if (!expression)
1714 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001716 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1717 c->c_arena);
1718 }
1719 case comparison:
1720 if (NCH(n) == 1) {
1721 n = CHILD(n, 0);
1722 goto loop;
1723 }
1724 else {
1725 expr_ty expression;
1726 asdl_int_seq *ops;
1727 asdl_seq *cmps;
1728 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1729 if (!ops)
1730 return NULL;
1731 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1732 if (!cmps) {
1733 return NULL;
1734 }
1735 for (i = 1; i < NCH(n); i += 2) {
1736 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001738 newoperator = ast_for_comp_op(CHILD(n, i));
1739 if (!newoperator) {
1740 return NULL;
1741 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001743 expression = ast_for_expr(c, CHILD(n, i + 1));
1744 if (!expression) {
1745 return NULL;
1746 }
1747
1748 asdl_seq_SET(ops, i / 2, newoperator);
1749 asdl_seq_SET(cmps, i / 2, expression);
1750 }
1751 expression = ast_for_expr(c, CHILD(n, 0));
1752 if (!expression) {
1753 return NULL;
1754 }
1755
1756 return Compare(expression, ops, cmps, LINENO(n),
1757 n->n_col_offset, c->c_arena);
1758 }
1759 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001761 /* The next five cases all handle BinOps. The main body of code
1762 is the same in each case, but the switch turned inside out to
1763 reuse the code for each type of operator.
1764 */
1765 case expr:
1766 case xor_expr:
1767 case and_expr:
1768 case shift_expr:
1769 case arith_expr:
1770 case term:
1771 if (NCH(n) == 1) {
1772 n = CHILD(n, 0);
1773 goto loop;
1774 }
1775 return ast_for_binop(c, n);
1776 case yield_expr: {
1777 expr_ty exp = NULL;
1778 if (NCH(n) == 2) {
1779 exp = ast_for_testlist(c, CHILD(n, 1));
1780 if (!exp)
1781 return NULL;
1782 }
1783 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1784 }
1785 case factor:
1786 if (NCH(n) == 1) {
1787 n = CHILD(n, 0);
1788 goto loop;
1789 }
1790 return ast_for_factor(c, n);
1791 case power:
1792 return ast_for_power(c, n);
1793 default:
1794 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1795 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001797 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 return NULL;
1799}
1800
1801static expr_ty
1802ast_for_call(struct compiling *c, const node *n, expr_ty func)
1803{
1804 /*
1805 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001806 | '**' test)
1807 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 */
1809
1810 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001811 asdl_seq *args;
1812 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 expr_ty vararg = NULL, kwarg = NULL;
1814
1815 REQ(n, arglist);
1816
1817 nargs = 0;
1818 nkeywords = 0;
1819 ngens = 0;
1820 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001821 node *ch = CHILD(n, i);
1822 if (TYPE(ch) == argument) {
1823 if (NCH(ch) == 1)
1824 nargs++;
1825 else if (TYPE(CHILD(ch, 1)) == gen_for)
1826 ngens++;
1827 else
1828 nkeywords++;
1829 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 }
1831 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001832 ast_error(n, "Generator expression must be parenthesized "
1833 "if not sole argument");
1834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 }
1836
1837 if (nargs + nkeywords + ngens > 255) {
1838 ast_error(n, "more than 255 arguments");
1839 return NULL;
1840 }
1841
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001842 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001844 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001847 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 nargs = 0;
1849 nkeywords = 0;
1850 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001851 node *ch = CHILD(n, i);
1852 if (TYPE(ch) == argument) {
1853 expr_ty e;
1854 if (NCH(ch) == 1) {
1855 if (nkeywords) {
1856 ast_error(CHILD(ch, 0),
1857 "non-keyword arg after keyword arg");
1858 return NULL;
1859 }
1860 e = ast_for_expr(c, CHILD(ch, 0));
1861 if (!e)
1862 return NULL;
1863 asdl_seq_SET(args, nargs++, e);
1864 }
1865 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1866 e = ast_for_genexp(c, ch);
1867 if (!e)
1868 return NULL;
1869 asdl_seq_SET(args, nargs++, e);
1870 }
1871 else {
1872 keyword_ty kw;
1873 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001875 /* CHILD(ch, 0) is test, but must be an identifier? */
1876 e = ast_for_expr(c, CHILD(ch, 0));
1877 if (!e)
1878 return NULL;
1879 /* f(lambda x: x[0] = 3) ends up getting parsed with
1880 * LHS test = lambda x: x[0], and RHS test = 3.
1881 * SF bug 132313 points out that complaining about a keyword
1882 * then is very confusing.
1883 */
1884 if (e->kind == Lambda_kind) {
1885 ast_error(CHILD(ch, 0),
1886 "lambda cannot contain assignment");
1887 return NULL;
1888 } else if (e->kind != Name_kind) {
1889 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1890 return NULL;
1891 }
1892 key = e->v.Name.id;
Georg Brandle06cf452007-06-07 13:23:24 +00001893 if (!strcmp(PyString_AS_STRING(key), "None")) {
1894 ast_error(CHILD(ch, 0), "assignment to None");
1895 return NULL;
1896 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001897 e = ast_for_expr(c, CHILD(ch, 2));
1898 if (!e)
1899 return NULL;
1900 kw = keyword(key, e, c->c_arena);
1901 if (!kw)
1902 return NULL;
1903 asdl_seq_SET(keywords, nkeywords++, kw);
1904 }
1905 }
1906 else if (TYPE(ch) == STAR) {
1907 vararg = ast_for_expr(c, CHILD(n, i+1));
1908 i++;
1909 }
1910 else if (TYPE(ch) == DOUBLESTAR) {
1911 kwarg = ast_for_expr(c, CHILD(n, i+1));
1912 i++;
1913 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 }
1915
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001916 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1917 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918}
1919
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001921ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001923 /* testlist_gexp: test (',' test)* [','] */
1924 /* testlist: test (',' test)* [','] */
1925 /* testlist_safe: test (',' test)+ [','] */
1926 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001928 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001929 if (NCH(n) > 1)
1930 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001931 }
1932 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001933 assert(TYPE(n) == testlist ||
1934 TYPE(n) == testlist_safe ||
1935 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001936 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001938 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001940 asdl_seq *tmp = seq_for_testlist(c, n);
1941 if (!tmp)
1942 return NULL;
1943 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001945}
1946
1947static expr_ty
1948ast_for_testlist_gexp(struct compiling *c, const node* n)
1949{
1950 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1951 /* argument: test [ gen_for ] */
1952 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001953 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001954 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001955 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001956}
1957
1958/* like ast_for_testlist() but returns a sequence */
1959static asdl_seq*
1960ast_for_class_bases(struct compiling *c, const node* n)
1961{
1962 /* testlist: test (',' test)* [','] */
1963 assert(NCH(n) > 0);
1964 REQ(n, testlist);
1965 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001966 expr_ty base;
1967 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
1968 if (!bases)
1969 return NULL;
1970 base = ast_for_expr(c, CHILD(n, 0));
1971 if (!base)
1972 return NULL;
1973 asdl_seq_SET(bases, 0, base);
1974 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001975 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001976
1977 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978}
1979
1980static stmt_ty
1981ast_for_expr_stmt(struct compiling *c, const node *n)
1982{
1983 REQ(n, expr_stmt);
1984 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001985 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 testlist: test (',' test)* [',']
1987 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001988 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 test: ... here starts the operator precendence dance
1990 */
1991
1992 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001993 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
1994 if (!e)
1995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001997 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 }
1999 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002000 expr_ty expr1, expr2;
2001 operator_ty newoperator;
2002 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002004 expr1 = ast_for_testlist(c, ch);
2005 if (!expr1)
2006 return NULL;
2007 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2008 switch (expr1->kind) {
2009 case GeneratorExp_kind:
2010 ast_error(ch, "augmented assignment to generator "
2011 "expression not possible");
2012 return NULL;
2013 case Yield_kind:
2014 ast_error(ch, "augmented assignment to yield "
2015 "expression not possible");
2016 return NULL;
2017 case Name_kind: {
2018 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2019 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2020 ast_error(ch, "assignment to None");
2021 return NULL;
2022 }
2023 break;
2024 }
2025 case Attribute_kind:
2026 case Subscript_kind:
2027 break;
2028 default:
2029 ast_error(ch, "illegal expression for augmented "
2030 "assignment");
2031 return NULL;
2032 }
2033 if(!set_context(expr1, Store, ch))
2034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002036 ch = CHILD(n, 2);
2037 if (TYPE(ch) == testlist)
2038 expr2 = ast_for_testlist(c, ch);
2039 else
2040 expr2 = ast_for_expr(c, ch);
2041 if (!expr2)
2042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002044 newoperator = ast_for_augassign(CHILD(n, 1));
2045 if (!newoperator)
2046 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002048 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2049 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 }
2051 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002052 int i;
2053 asdl_seq *targets;
2054 node *value;
2055 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002057 /* a normal assignment */
2058 REQ(CHILD(n, 1), EQUAL);
2059 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2060 if (!targets)
2061 return NULL;
2062 for (i = 0; i < NCH(n) - 2; i += 2) {
2063 expr_ty e;
2064 node *ch = CHILD(n, i);
2065 if (TYPE(ch) == yield_expr) {
2066 ast_error(ch, "assignment to yield expression not possible");
2067 return NULL;
2068 }
2069 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002071 /* set context to assign */
2072 if (!e)
2073 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002075 if (!set_context(e, Store, CHILD(n, i)))
2076 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002078 asdl_seq_SET(targets, i / 2, e);
2079 }
2080 value = CHILD(n, NCH(n) - 1);
2081 if (TYPE(value) == testlist)
2082 expression = ast_for_testlist(c, value);
2083 else
2084 expression = ast_for_expr(c, value);
2085 if (!expression)
2086 return NULL;
2087 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2088 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090}
2091
2092static stmt_ty
2093ast_for_print_stmt(struct compiling *c, const node *n)
2094{
2095 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002096 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 */
2098 expr_ty dest = NULL, expression;
2099 asdl_seq *seq;
2100 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002101 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102
2103 REQ(n, print_stmt);
2104 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002105 dest = ast_for_expr(c, CHILD(n, 2));
2106 if (!dest)
2107 return NULL;
2108 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002110 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002112 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002113 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002114 expression = ast_for_expr(c, CHILD(n, i));
2115 if (!expression)
2116 return NULL;
2117 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 }
2119 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002120 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121}
2122
2123static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002124ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125{
2126 asdl_seq *seq;
2127 int i;
2128 expr_ty e;
2129
2130 REQ(n, exprlist);
2131
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002132 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002134 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002136 e = ast_for_expr(c, CHILD(n, i));
2137 if (!e)
2138 return NULL;
2139 asdl_seq_SET(seq, i / 2, e);
2140 if (context && !set_context(e, context, CHILD(n, i)))
2141 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 }
2143 return seq;
2144}
2145
2146static stmt_ty
2147ast_for_del_stmt(struct compiling *c, const node *n)
2148{
2149 asdl_seq *expr_list;
2150
2151 /* del_stmt: 'del' exprlist */
2152 REQ(n, del_stmt);
2153
2154 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2155 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002156 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002157 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158}
2159
2160static stmt_ty
2161ast_for_flow_stmt(struct compiling *c, const node *n)
2162{
2163 /*
2164 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002165 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 break_stmt: 'break'
2167 continue_stmt: 'continue'
2168 return_stmt: 'return' [testlist]
2169 yield_stmt: yield_expr
2170 yield_expr: 'yield' testlist
2171 raise_stmt: 'raise' [test [',' test [',' test]]]
2172 */
2173 node *ch;
2174
2175 REQ(n, flow_stmt);
2176 ch = CHILD(n, 0);
2177 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002178 case break_stmt:
2179 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2180 case continue_stmt:
2181 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2182 case yield_stmt: { /* will reduce to yield_expr */
2183 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2184 if (!exp)
2185 return NULL;
2186 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2187 }
2188 case return_stmt:
2189 if (NCH(ch) == 1)
2190 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2191 else {
2192 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2193 if (!expression)
2194 return NULL;
2195 return Return(expression, LINENO(n), n->n_col_offset,
2196 c->c_arena);
2197 }
2198 case raise_stmt:
2199 if (NCH(ch) == 1)
2200 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2201 c->c_arena);
2202 else if (NCH(ch) == 2) {
2203 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2204 if (!expression)
2205 return NULL;
2206 return Raise(expression, NULL, NULL, LINENO(n),
2207 n->n_col_offset, c->c_arena);
2208 }
2209 else if (NCH(ch) == 4) {
2210 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002212 expr1 = ast_for_expr(c, CHILD(ch, 1));
2213 if (!expr1)
2214 return NULL;
2215 expr2 = ast_for_expr(c, CHILD(ch, 3));
2216 if (!expr2)
2217 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002219 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2220 c->c_arena);
2221 }
2222 else if (NCH(ch) == 6) {
2223 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002225 expr1 = ast_for_expr(c, CHILD(ch, 1));
2226 if (!expr1)
2227 return NULL;
2228 expr2 = ast_for_expr(c, CHILD(ch, 3));
2229 if (!expr2)
2230 return NULL;
2231 expr3 = ast_for_expr(c, CHILD(ch, 5));
2232 if (!expr3)
2233 return NULL;
2234
2235 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2236 c->c_arena);
2237 }
2238 default:
2239 PyErr_Format(PyExc_SystemError,
2240 "unexpected flow_stmt: %d", TYPE(ch));
2241 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002243
2244 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2245 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246}
2247
2248static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002249alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250{
2251 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002252 import_as_name: NAME ['as' NAME]
2253 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 dotted_name: NAME ('.' NAME)*
2255 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002256 PyObject *str;
2257
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 loop:
2259 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002260 case import_as_name:
2261 str = NULL;
2262 if (NCH(n) == 3) {
2263 str = NEW_IDENTIFIER(CHILD(n, 2));
2264 }
2265 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2266 case dotted_as_name:
2267 if (NCH(n) == 1) {
2268 n = CHILD(n, 0);
2269 goto loop;
2270 }
2271 else {
2272 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2273 if (!a)
2274 return NULL;
2275 assert(!a->asname);
2276 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2277 return a;
2278 }
2279 break;
2280 case dotted_name:
2281 if (NCH(n) == 1)
2282 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2283 else {
2284 /* Create a string of the form "a.b.c" */
2285 int i;
2286 size_t len;
2287 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002289 len = 0;
2290 for (i = 0; i < NCH(n); i += 2)
2291 /* length of string plus one for the dot */
2292 len += strlen(STR(CHILD(n, i))) + 1;
2293 len--; /* the last name doesn't have a dot */
2294 str = PyString_FromStringAndSize(NULL, len);
2295 if (!str)
2296 return NULL;
2297 s = PyString_AS_STRING(str);
2298 if (!s)
2299 return NULL;
2300 for (i = 0; i < NCH(n); i += 2) {
2301 char *sch = STR(CHILD(n, i));
2302 strcpy(s, STR(CHILD(n, i)));
2303 s += strlen(sch);
2304 *s++ = '.';
2305 }
2306 --s;
2307 *s = '\0';
2308 PyString_InternInPlace(&str);
2309 PyArena_AddPyObject(c->c_arena, str);
2310 return alias(str, NULL, c->c_arena);
2311 }
2312 break;
2313 case STAR:
2314 str = PyString_InternFromString("*");
2315 PyArena_AddPyObject(c->c_arena, str);
2316 return alias(str, NULL, c->c_arena);
2317 default:
2318 PyErr_Format(PyExc_SystemError,
2319 "unexpected import name: %d", TYPE(n));
2320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002322
2323 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 return NULL;
2325}
2326
2327static stmt_ty
2328ast_for_import_stmt(struct compiling *c, const node *n)
2329{
2330 /*
2331 import_stmt: import_name | import_from
2332 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002333 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002334 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002336 int lineno;
2337 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338 int i;
2339 asdl_seq *aliases;
2340
2341 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002342 lineno = LINENO(n);
2343 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002344 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002345 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002346 n = CHILD(n, 1);
2347 REQ(n, dotted_as_names);
2348 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2349 if (!aliases)
2350 return NULL;
2351 for (i = 0; i < NCH(n); i += 2) {
2352 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2353 if (!import_alias)
2354 return NULL;
2355 asdl_seq_SET(aliases, i / 2, import_alias);
2356 }
2357 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002358 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002359 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002360 int n_children;
2361 int idx, ndots = 0;
2362 alias_ty mod = NULL;
2363 identifier modname;
2364
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002365 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002366 optional module name */
2367 for (idx = 1; idx < NCH(n); idx++) {
2368 if (TYPE(CHILD(n, idx)) == dotted_name) {
2369 mod = alias_for_import_name(c, CHILD(n, idx));
2370 idx++;
2371 break;
2372 } else if (TYPE(CHILD(n, idx)) != DOT) {
2373 break;
2374 }
2375 ndots++;
2376 }
2377 idx++; /* skip over the 'import' keyword */
2378 switch (TYPE(CHILD(n, idx))) {
2379 case STAR:
2380 /* from ... import * */
2381 n = CHILD(n, idx);
2382 n_children = 1;
2383 if (ndots) {
2384 ast_error(n, "'import *' not allowed with 'from .'");
2385 return NULL;
2386 }
2387 break;
2388 case LPAR:
2389 /* from ... import (x, y, z) */
2390 n = CHILD(n, idx + 1);
2391 n_children = NCH(n);
2392 break;
2393 case import_as_names:
2394 /* from ... import x, y, z */
2395 n = CHILD(n, idx);
2396 n_children = NCH(n);
2397 if (n_children % 2 == 0) {
2398 ast_error(n, "trailing comma not allowed without"
2399 " surrounding parentheses");
2400 return NULL;
2401 }
2402 break;
2403 default:
2404 ast_error(n, "Unexpected node-type in from-import");
2405 return NULL;
2406 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002408 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2409 if (!aliases)
2410 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002412 /* handle "from ... import *" special b/c there's no children */
2413 if (TYPE(n) == STAR) {
2414 alias_ty import_alias = alias_for_import_name(c, n);
2415 if (!import_alias)
2416 return NULL;
2417 asdl_seq_SET(aliases, 0, import_alias);
2418 }
2419 else {
2420 for (i = 0; i < NCH(n); i += 2) {
2421 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2422 if (!import_alias)
2423 return NULL;
2424 asdl_seq_SET(aliases, i / 2, import_alias);
2425 }
2426 }
2427 if (mod != NULL)
2428 modname = mod->name;
2429 else
2430 modname = new_identifier("", c->c_arena);
2431 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2432 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433 }
Neal Norwitz79792652005-11-14 04:25:03 +00002434 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002435 "unknown import statement: starts with command '%s'",
2436 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 return NULL;
2438}
2439
2440static stmt_ty
2441ast_for_global_stmt(struct compiling *c, const node *n)
2442{
2443 /* global_stmt: 'global' NAME (',' NAME)* */
2444 identifier name;
2445 asdl_seq *s;
2446 int i;
2447
2448 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002449 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002451 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002453 name = NEW_IDENTIFIER(CHILD(n, i));
2454 if (!name)
2455 return NULL;
2456 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002458 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459}
2460
2461static stmt_ty
2462ast_for_exec_stmt(struct compiling *c, const node *n)
2463{
2464 expr_ty expr1, globals = NULL, locals = NULL;
2465 int n_children = NCH(n);
2466 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002467 PyErr_Format(PyExc_SystemError,
2468 "poorly formed 'exec' statement: %d parts to statement",
2469 n_children);
2470 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 }
2472
2473 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2474 REQ(n, exec_stmt);
2475 expr1 = ast_for_expr(c, CHILD(n, 1));
2476 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002479 globals = ast_for_expr(c, CHILD(n, 3));
2480 if (!globals)
2481 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 }
2483 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002484 locals = ast_for_expr(c, CHILD(n, 5));
2485 if (!locals)
2486 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 }
2488
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002489 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2490 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491}
2492
2493static stmt_ty
2494ast_for_assert_stmt(struct compiling *c, const node *n)
2495{
2496 /* assert_stmt: 'assert' test [',' test] */
2497 REQ(n, assert_stmt);
2498 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002499 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2500 if (!expression)
2501 return NULL;
2502 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2503 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 }
2505 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002506 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002508 expr1 = ast_for_expr(c, CHILD(n, 1));
2509 if (!expr1)
2510 return NULL;
2511 expr2 = ast_for_expr(c, CHILD(n, 3));
2512 if (!expr2)
2513 return NULL;
2514
2515 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 }
Neal Norwitz79792652005-11-14 04:25:03 +00002517 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002518 "improper number of parts to 'assert' statement: %d",
2519 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 return NULL;
2521}
2522
2523static asdl_seq *
2524ast_for_suite(struct compiling *c, const node *n)
2525{
2526 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002527 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 stmt_ty s;
2529 int i, total, num, end, pos = 0;
2530 node *ch;
2531
2532 REQ(n, suite);
2533
2534 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002535 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002537 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002539 n = CHILD(n, 0);
2540 /* simple_stmt always ends with a NEWLINE,
2541 and may have a trailing SEMI
2542 */
2543 end = NCH(n) - 1;
2544 if (TYPE(CHILD(n, end - 1)) == SEMI)
2545 end--;
2546 /* loop by 2 to skip semi-colons */
2547 for (i = 0; i < end; i += 2) {
2548 ch = CHILD(n, i);
2549 s = ast_for_stmt(c, ch);
2550 if (!s)
2551 return NULL;
2552 asdl_seq_SET(seq, pos++, s);
2553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 }
2555 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002556 for (i = 2; i < (NCH(n) - 1); i++) {
2557 ch = CHILD(n, i);
2558 REQ(ch, stmt);
2559 num = num_stmts(ch);
2560 if (num == 1) {
2561 /* small_stmt or compound_stmt with only one child */
2562 s = ast_for_stmt(c, ch);
2563 if (!s)
2564 return NULL;
2565 asdl_seq_SET(seq, pos++, s);
2566 }
2567 else {
2568 int j;
2569 ch = CHILD(ch, 0);
2570 REQ(ch, simple_stmt);
2571 for (j = 0; j < NCH(ch); j += 2) {
2572 /* statement terminates with a semi-colon ';' */
2573 if (NCH(CHILD(ch, j)) == 0) {
2574 assert((j + 1) == NCH(ch));
2575 break;
2576 }
2577 s = ast_for_stmt(c, CHILD(ch, j));
2578 if (!s)
2579 return NULL;
2580 asdl_seq_SET(seq, pos++, s);
2581 }
2582 }
2583 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 }
2585 assert(pos == seq->size);
2586 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587}
2588
2589static stmt_ty
2590ast_for_if_stmt(struct compiling *c, const node *n)
2591{
2592 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2593 ['else' ':' suite]
2594 */
2595 char *s;
2596
2597 REQ(n, if_stmt);
2598
2599 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002600 expr_ty expression;
2601 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002603 expression = ast_for_expr(c, CHILD(n, 1));
2604 if (!expression)
2605 return NULL;
2606 suite_seq = ast_for_suite(c, CHILD(n, 3));
2607 if (!suite_seq)
2608 return NULL;
2609
2610 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2611 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 s = STR(CHILD(n, 4));
2615 /* s[2], the third character in the string, will be
2616 's' for el_s_e, or
2617 'i' for el_i_f
2618 */
2619 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002620 expr_ty expression;
2621 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002623 expression = ast_for_expr(c, CHILD(n, 1));
2624 if (!expression)
2625 return NULL;
2626 seq1 = ast_for_suite(c, CHILD(n, 3));
2627 if (!seq1)
2628 return NULL;
2629 seq2 = ast_for_suite(c, CHILD(n, 6));
2630 if (!seq2)
2631 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002633 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2634 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 }
2636 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002637 int i, n_elif, has_else = 0;
2638 expr_ty expression;
2639 asdl_seq *suite_seq;
2640 asdl_seq *orelse = NULL;
2641 n_elif = NCH(n) - 4;
2642 /* must reference the child n_elif+1 since 'else' token is third,
2643 not fourth, child from the end. */
2644 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2645 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2646 has_else = 1;
2647 n_elif -= 3;
2648 }
2649 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002651 if (has_else) {
2652 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002654 orelse = asdl_seq_new(1, c->c_arena);
2655 if (!orelse)
2656 return NULL;
2657 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2658 if (!expression)
2659 return NULL;
2660 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2661 if (!suite_seq)
2662 return NULL;
2663 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2664 if (!suite_seq2)
2665 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002667 asdl_seq_SET(orelse, 0,
2668 If(expression, suite_seq, suite_seq2,
2669 LINENO(CHILD(n, NCH(n) - 6)),
2670 CHILD(n, NCH(n) - 6)->n_col_offset,
2671 c->c_arena));
2672 /* the just-created orelse handled the last elif */
2673 n_elif--;
2674 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002676 for (i = 0; i < n_elif; i++) {
2677 int off = 5 + (n_elif - i - 1) * 4;
2678 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2679 if (!newobj)
2680 return NULL;
2681 expression = ast_for_expr(c, CHILD(n, off));
2682 if (!expression)
2683 return NULL;
2684 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2685 if (!suite_seq)
2686 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002688 asdl_seq_SET(newobj, 0,
2689 If(expression, suite_seq, orelse,
2690 LINENO(CHILD(n, off)),
2691 CHILD(n, off)->n_col_offset, c->c_arena));
2692 orelse = newobj;
2693 }
2694 expression = ast_for_expr(c, CHILD(n, 1));
2695 if (!expression)
2696 return NULL;
2697 suite_seq = ast_for_suite(c, CHILD(n, 3));
2698 if (!suite_seq)
2699 return NULL;
2700 return If(expression, suite_seq, orelse,
2701 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002703
2704 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002705 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002706 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707}
2708
2709static stmt_ty
2710ast_for_while_stmt(struct compiling *c, const node *n)
2711{
2712 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2713 REQ(n, while_stmt);
2714
2715 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002716 expr_ty expression;
2717 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002719 expression = ast_for_expr(c, CHILD(n, 1));
2720 if (!expression)
2721 return NULL;
2722 suite_seq = ast_for_suite(c, CHILD(n, 3));
2723 if (!suite_seq)
2724 return NULL;
2725 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2726 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727 }
2728 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002729 expr_ty expression;
2730 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002732 expression = ast_for_expr(c, CHILD(n, 1));
2733 if (!expression)
2734 return NULL;
2735 seq1 = ast_for_suite(c, CHILD(n, 3));
2736 if (!seq1)
2737 return NULL;
2738 seq2 = ast_for_suite(c, CHILD(n, 6));
2739 if (!seq2)
2740 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002742 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2743 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002745
2746 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002747 "wrong number of tokens for 'while' statement: %d",
2748 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002749 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750}
2751
2752static stmt_ty
2753ast_for_for_stmt(struct compiling *c, const node *n)
2754{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002755 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 expr_ty expression;
2757 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002758 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2760 REQ(n, for_stmt);
2761
2762 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002763 seq = ast_for_suite(c, CHILD(n, 8));
2764 if (!seq)
2765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 }
2767
Neal Norwitzedef2be2006-07-12 05:26:17 +00002768 node_target = CHILD(n, 1);
2769 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002770 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002771 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002772 /* Check the # of children rather than the length of _target, since
2773 for x, in ... has 1 element in _target, but still requires a Tuple. */
2774 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002775 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002777 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002779 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002780 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002781 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002783 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002786 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002787 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788}
2789
2790static excepthandler_ty
2791ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2792{
Collin Winter62903052007-05-18 23:11:24 +00002793 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794 REQ(exc, except_clause);
2795 REQ(body, suite);
2796
2797 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002798 asdl_seq *suite_seq = ast_for_suite(c, body);
2799 if (!suite_seq)
2800 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002802 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2803 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 }
2805 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002806 expr_ty expression;
2807 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002809 expression = ast_for_expr(c, CHILD(exc, 1));
2810 if (!expression)
2811 return NULL;
2812 suite_seq = ast_for_suite(c, body);
2813 if (!suite_seq)
2814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002816 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2817 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 }
2819 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002820 asdl_seq *suite_seq;
2821 expr_ty expression;
2822 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2823 if (!e)
2824 return NULL;
2825 if (!set_context(e, Store, CHILD(exc, 3)))
2826 return NULL;
2827 expression = ast_for_expr(c, CHILD(exc, 1));
2828 if (!expression)
2829 return NULL;
2830 suite_seq = ast_for_suite(c, body);
2831 if (!suite_seq)
2832 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002834 return excepthandler(expression, e, suite_seq, LINENO(exc),
2835 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002837
2838 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002839 "wrong number of children for 'except' clause: %d",
2840 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002841 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842}
2843
2844static stmt_ty
2845ast_for_try_stmt(struct compiling *c, const node *n)
2846{
Neal Norwitzf599f422005-12-17 21:33:47 +00002847 const int nch = NCH(n);
2848 int n_except = (nch - 3)/3;
2849 asdl_seq *body, *orelse = NULL, *finally = NULL;
2850
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851 REQ(n, try_stmt);
2852
Neal Norwitzf599f422005-12-17 21:33:47 +00002853 body = ast_for_suite(c, CHILD(n, 2));
2854 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856
Neal Norwitzf599f422005-12-17 21:33:47 +00002857 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002858 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2859 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2860 /* we can assume it's an "else",
2861 because nch >= 9 for try-else-finally and
2862 it would otherwise have a type of except_clause */
2863 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2864 if (orelse == NULL)
2865 return NULL;
2866 n_except--;
2867 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002869 finally = ast_for_suite(c, CHILD(n, nch - 1));
2870 if (finally == NULL)
2871 return NULL;
2872 n_except--;
2873 }
2874 else {
2875 /* we can assume it's an "else",
2876 otherwise it would have a type of except_clause */
2877 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2878 if (orelse == NULL)
2879 return NULL;
2880 n_except--;
2881 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002883 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002884 ast_error(n, "malformed 'try' statement");
2885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002887
2888 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002889 int i;
2890 stmt_ty except_st;
2891 /* process except statements to create a try ... except */
2892 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2893 if (handlers == NULL)
2894 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002895
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002896 for (i = 0; i < n_except; i++) {
2897 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2898 CHILD(n, 5 + i * 3));
2899 if (!e)
2900 return NULL;
2901 asdl_seq_SET(handlers, i, e);
2902 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002903
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002904 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2905 n->n_col_offset, c->c_arena);
2906 if (!finally)
2907 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002908
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002909 /* if a 'finally' is present too, we nest the TryExcept within a
2910 TryFinally to emulate try ... except ... finally */
2911 body = asdl_seq_new(1, c->c_arena);
2912 if (body == NULL)
2913 return NULL;
2914 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002915 }
2916
2917 /* must be a try ... finally (except clauses are in body, if any exist) */
2918 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002919 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920}
2921
Guido van Rossumc2e20742006-02-27 22:32:47 +00002922static expr_ty
2923ast_for_with_var(struct compiling *c, const node *n)
2924{
2925 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002926 return ast_for_expr(c, CHILD(n, 1));
2927}
2928
2929/* with_stmt: 'with' test [ with_var ] ':' suite */
2930static stmt_ty
2931ast_for_with_stmt(struct compiling *c, const node *n)
2932{
2933 expr_ty context_expr, optional_vars = NULL;
2934 int suite_index = 3; /* skip 'with', test, and ':' */
2935 asdl_seq *suite_seq;
2936
2937 assert(TYPE(n) == with_stmt);
2938 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002939 if (!context_expr)
2940 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002941 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002942 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002943
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002944 if (!optional_vars) {
2945 return NULL;
2946 }
2947 if (!set_context(optional_vars, Store, n)) {
2948 return NULL;
2949 }
2950 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002951 }
2952
2953 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2954 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002955 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002956 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002957 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002958 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002959}
2960
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961static stmt_ty
2962ast_for_classdef(struct compiling *c, const node *n)
2963{
2964 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 asdl_seq *bases, *s;
2966
2967 REQ(n, classdef);
2968
2969 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002970 ast_error(n, "assignment to None");
2971 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972 }
2973
2974 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002975 s = ast_for_suite(c, CHILD(n, 3));
2976 if (!s)
2977 return NULL;
2978 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2979 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 }
2981 /* check for empty base list */
2982 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002983 s = ast_for_suite(c, CHILD(n,5));
2984 if (!s)
2985 return NULL;
2986 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2987 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 }
2989
2990 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002991 bases = ast_for_class_bases(c, CHILD(n, 3));
2992 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994
2995 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002996 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002997 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002998 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002999 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000}
3001
3002static stmt_ty
3003ast_for_stmt(struct compiling *c, const node *n)
3004{
3005 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003006 assert(NCH(n) == 1);
3007 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008 }
3009 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003010 assert(num_stmts(n) == 1);
3011 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012 }
3013 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003014 REQ(n, small_stmt);
3015 n = CHILD(n, 0);
3016 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3017 | flow_stmt | import_stmt | global_stmt | exec_stmt
3018 | assert_stmt
3019 */
3020 switch (TYPE(n)) {
3021 case expr_stmt:
3022 return ast_for_expr_stmt(c, n);
3023 case print_stmt:
3024 return ast_for_print_stmt(c, n);
3025 case del_stmt:
3026 return ast_for_del_stmt(c, n);
3027 case pass_stmt:
3028 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3029 case flow_stmt:
3030 return ast_for_flow_stmt(c, n);
3031 case import_stmt:
3032 return ast_for_import_stmt(c, n);
3033 case global_stmt:
3034 return ast_for_global_stmt(c, n);
3035 case exec_stmt:
3036 return ast_for_exec_stmt(c, n);
3037 case assert_stmt:
3038 return ast_for_assert_stmt(c, n);
3039 default:
3040 PyErr_Format(PyExc_SystemError,
3041 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3042 TYPE(n), NCH(n));
3043 return NULL;
3044 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 }
3046 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003047 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3048 | funcdef | classdef
3049 */
3050 node *ch = CHILD(n, 0);
3051 REQ(n, compound_stmt);
3052 switch (TYPE(ch)) {
3053 case if_stmt:
3054 return ast_for_if_stmt(c, ch);
3055 case while_stmt:
3056 return ast_for_while_stmt(c, ch);
3057 case for_stmt:
3058 return ast_for_for_stmt(c, ch);
3059 case try_stmt:
3060 return ast_for_try_stmt(c, ch);
3061 case with_stmt:
3062 return ast_for_with_stmt(c, ch);
3063 case funcdef:
3064 return ast_for_funcdef(c, ch);
3065 case classdef:
3066 return ast_for_classdef(c, ch);
3067 default:
3068 PyErr_Format(PyExc_SystemError,
3069 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3070 TYPE(n), NCH(n));
3071 return NULL;
3072 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 }
3074}
3075
3076static PyObject *
3077parsenumber(const char *s)
3078{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003079 const char *end;
3080 long x;
3081 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003083 Py_complex c;
3084 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085#endif
3086
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003087 errno = 0;
3088 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003090 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003092 if (*end == 'l' || *end == 'L')
3093 return PyLong_FromString((char *)s, (char **)0, 0);
3094 if (s[0] == '0') {
3095 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3096 if (x < 0 && errno == 0) {
3097 return PyLong_FromString((char *)s,
3098 (char **)0,
3099 0);
3100 }
3101 }
3102 else
3103 x = PyOS_strtol((char *)s, (char **)&end, 0);
3104 if (*end == '\0') {
3105 if (errno != 0)
3106 return PyLong_FromString((char *)s, (char **)0, 0);
3107 return PyInt_FromLong(x);
3108 }
3109 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003111 if (imflag) {
3112 c.real = 0.;
3113 PyFPE_START_PROTECT("atof", return 0)
3114 c.imag = PyOS_ascii_atof(s);
3115 PyFPE_END_PROTECT(c)
3116 return PyComplex_FromCComplex(c);
3117 }
3118 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003120 {
3121 PyFPE_START_PROTECT("atof", return 0)
3122 dx = PyOS_ascii_atof(s);
3123 PyFPE_END_PROTECT(dx)
3124 return PyFloat_FromDouble(dx);
3125 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126}
3127
3128static PyObject *
3129decode_utf8(const char **sPtr, const char *end, char* encoding)
3130{
3131#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003132 Py_FatalError("decode_utf8 should not be called in this build.");
3133 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003135 PyObject *u, *v;
3136 char *s, *t;
3137 t = s = (char *)*sPtr;
3138 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3139 while (s < end && (*s & 0x80)) s++;
3140 *sPtr = s;
3141 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3142 if (u == NULL)
3143 return NULL;
3144 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3145 Py_DECREF(u);
3146 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147#endif
3148}
3149
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003150#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151static PyObject *
3152decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3153{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003154 PyObject *v, *u;
3155 char *buf;
3156 char *p;
3157 const char *end;
3158 if (encoding == NULL) {
3159 buf = (char *)s;
3160 u = NULL;
3161 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3162 buf = (char *)s;
3163 u = NULL;
3164 } else {
3165 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3166 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3167 if (u == NULL)
3168 return NULL;
3169 p = buf = PyString_AsString(u);
3170 end = s + len;
3171 while (s < end) {
3172 if (*s == '\\') {
3173 *p++ = *s++;
3174 if (*s & 0x80) {
3175 strcpy(p, "u005c");
3176 p += 5;
3177 }
3178 }
3179 if (*s & 0x80) { /* XXX inefficient */
3180 PyObject *w;
3181 char *r;
3182 Py_ssize_t rn, i;
3183 w = decode_utf8(&s, end, "utf-16-be");
3184 if (w == NULL) {
3185 Py_DECREF(u);
3186 return NULL;
3187 }
3188 r = PyString_AsString(w);
3189 rn = PyString_Size(w);
3190 assert(rn % 2 == 0);
3191 for (i = 0; i < rn; i += 2) {
3192 sprintf(p, "\\u%02x%02x",
3193 r[i + 0] & 0xFF,
3194 r[i + 1] & 0xFF);
3195 p += 6;
3196 }
3197 Py_DECREF(w);
3198 } else {
3199 *p++ = *s++;
3200 }
3201 }
3202 len = p - buf;
3203 s = buf;
3204 }
3205 if (rawmode)
3206 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3207 else
3208 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3209 Py_XDECREF(u);
3210 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003212#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213
3214/* s is a Python string literal, including the bracketing quote characters,
3215 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3216 * parsestr parses it, and returns the decoded Python string object.
3217 */
3218static PyObject *
3219parsestr(const char *s, const char *encoding)
3220{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003221 size_t len;
3222 int quote = Py_CHARMASK(*s);
3223 int rawmode = 0;
3224 int need_encoding;
3225 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003227 if (isalpha(quote) || quote == '_') {
3228 if (quote == 'u' || quote == 'U') {
3229 quote = *++s;
3230 unicode = 1;
3231 }
3232 if (quote == 'r' || quote == 'R') {
3233 quote = *++s;
3234 rawmode = 1;
3235 }
3236 }
3237 if (quote != '\'' && quote != '\"') {
3238 PyErr_BadInternalCall();
3239 return NULL;
3240 }
3241 s++;
3242 len = strlen(s);
3243 if (len > INT_MAX) {
3244 PyErr_SetString(PyExc_OverflowError,
3245 "string to parse is too long");
3246 return NULL;
3247 }
3248 if (s[--len] != quote) {
3249 PyErr_BadInternalCall();
3250 return NULL;
3251 }
3252 if (len >= 4 && s[0] == quote && s[1] == quote) {
3253 s += 2;
3254 len -= 2;
3255 if (s[--len] != quote || s[--len] != quote) {
3256 PyErr_BadInternalCall();
3257 return NULL;
3258 }
3259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003261 if (unicode || Py_UnicodeFlag) {
3262 return decode_unicode(s, len, rawmode, encoding);
3263 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003265 need_encoding = (encoding != NULL &&
3266 strcmp(encoding, "utf-8") != 0 &&
3267 strcmp(encoding, "iso-8859-1") != 0);
3268 if (rawmode || strchr(s, '\\') == NULL) {
3269 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003271 /* This should not happen - we never see any other
3272 encoding. */
3273 Py_FatalError(
3274 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003276 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3277 if (u == NULL)
3278 return NULL;
3279 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3280 Py_DECREF(u);
3281 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003283 } else {
3284 return PyString_FromStringAndSize(s, len);
3285 }
3286 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003288 return PyString_DecodeEscape(s, len, NULL, unicode,
3289 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290}
3291
3292/* Build a Python string object out of a STRING atom. This takes care of
3293 * compile-time literal catenation, calling parsestr() on each piece, and
3294 * pasting the intermediate results together.
3295 */
3296static PyObject *
3297parsestrplus(struct compiling *c, const node *n)
3298{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003299 PyObject *v;
3300 int i;
3301 REQ(CHILD(n, 0), STRING);
3302 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3303 /* String literal concatenation */
3304 for (i = 1; i < NCH(n); i++) {
3305 PyObject *s;
3306 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3307 if (s == NULL)
3308 goto onError;
3309 if (PyString_Check(v) && PyString_Check(s)) {
3310 PyString_ConcatAndDel(&v, s);
3311 if (v == NULL)
3312 goto onError;
3313 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003315 else {
3316 PyObject *temp = PyUnicode_Concat(v, s);
3317 Py_DECREF(s);
3318 Py_DECREF(v);
3319 v = temp;
3320 if (v == NULL)
3321 goto onError;
3322 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003324 }
3325 }
3326 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327
3328 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003329 Py_XDECREF(v);
3330 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331}