blob: 6131ca52f98b27c1af3949364f00f6a6e1af5464 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* Data structure used internally */
19struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000020 char *c_encoding; /* source encoding */
21 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022};
23
24static asdl_seq *seq_for_testlist(struct compiling *, const node *);
25static expr_ty ast_for_expr(struct compiling *, const node *);
26static stmt_ty ast_for_stmt(struct compiling *, const node *);
27static asdl_seq *ast_for_suite(struct compiling *, const node *);
Martin v. Löwis28457502006-04-11 09:17:27 +000028static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000029static expr_ty ast_for_testlist(struct compiling *, const node *);
30static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000031
32/* Note different signature for ast_for_call */
33static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
34
35static PyObject *parsenumber(const char *);
36static PyObject *parsestr(const char *s, const char *encoding);
37static PyObject *parsestrplus(struct compiling *, const node *n);
38
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039#ifndef LINENO
Jeremy Hylton819de6c2007-02-27 16:13:23 +000040#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041#endif
42
Neal Norwitzadb69fc2005-12-17 20:54:49 +000043static identifier
44new_identifier(const char* n, PyArena *arena) {
45 PyObject* id = PyString_InternFromString(n);
46 PyArena_AddPyObject(arena, id);
47 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048}
49
Neal Norwitzadb69fc2005-12-17 20:54:49 +000050#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000051
52/* This routine provides an invalid object for the syntax error.
53 The outermost routine must unpack this error and create the
54 proper object. We do this so that we don't have to pass
55 the filename to everything function.
56
57 XXX Maybe we should just pass the filename...
58*/
59
60static int
61ast_error(const node *n, const char *errstr)
62{
63 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
64 if (!u)
Jeremy Hylton819de6c2007-02-27 16:13:23 +000065 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000066 PyErr_SetObject(PyExc_SyntaxError, u);
67 Py_DECREF(u);
68 return 0;
69}
70
71static void
72ast_error_finish(const char *filename)
73{
74 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000075 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000076
77 assert(PyErr_Occurred());
78 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hylton819de6c2007-02-27 16:13:23 +000079 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080
81 PyErr_Fetch(&type, &value, &tback);
82 errstr = PyTuple_GetItem(value, 0);
83 if (!errstr)
Jeremy Hylton819de6c2007-02-27 16:13:23 +000084 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085 Py_INCREF(errstr);
86 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000087 if (lineno == -1) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +000088 Py_DECREF(errstr);
89 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000090 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091 Py_DECREF(value);
92
93 loc = PyErr_ProgramText(filename, lineno);
94 if (!loc) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +000095 Py_INCREF(Py_None);
96 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +000098 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000100 if (!tmp) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000101 Py_DECREF(errstr);
102 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000103 }
Georg Brandl7784f122006-05-26 20:04:44 +0000104 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(errstr);
106 Py_DECREF(tmp);
107 if (!value)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000108 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109 PyErr_Restore(type, value, tback);
110}
111
112/* num_stmts() returns number of contained statements.
113
114 Use this routine to determine how big a sequence is needed for
115 the statements in a parse tree. Its raison d'etre is this bit of
116 grammar:
117
118 stmt: simple_stmt | compound_stmt
119 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
120
121 A simple_stmt can contain multiple small_stmt elements joined
122 by semicolons. If the arg is a simple_stmt, the number of
123 small_stmt elements is returned.
124*/
125
126static int
127num_stmts(const node *n)
128{
129 int i, l;
130 node *ch;
131
132 switch (TYPE(n)) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000133 case single_input:
134 if (TYPE(CHILD(n, 0)) == NEWLINE)
135 return 0;
136 else
137 return num_stmts(CHILD(n, 0));
138 case file_input:
139 l = 0;
140 for (i = 0; i < NCH(n); i++) {
141 ch = CHILD(n, i);
142 if (TYPE(ch) == stmt)
143 l += num_stmts(ch);
144 }
145 return l;
146 case stmt:
147 return num_stmts(CHILD(n, 0));
148 case compound_stmt:
149 return 1;
150 case simple_stmt:
151 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
152 case suite:
153 if (NCH(n) == 1)
154 return num_stmts(CHILD(n, 0));
155 else {
156 l = 0;
157 for (i = 2; i < (NCH(n) - 1); i++)
158 l += num_stmts(CHILD(n, i));
159 return l;
160 }
161 default: {
162 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000164 sprintf(buf, "Non-statement found: %d %d\n",
165 TYPE(n), NCH(n));
166 Py_FatalError(buf);
167 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168 }
169 assert(0);
170 return 0;
171}
172
173/* Transform the CST rooted at node * to the appropriate AST
174*/
175
176mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000177PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000178 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000180 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181 asdl_seq *stmts = NULL;
182 stmt_ty s;
183 node *ch;
184 struct compiling c;
185
186 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000187 c.c_encoding = "utf-8";
188 if (TYPE(n) == encoding_decl) {
189 ast_error(n, "encoding declaration in Unicode string");
190 goto error;
191 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192 } else if (TYPE(n) == encoding_decl) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000193 c.c_encoding = STR(n);
194 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195 } else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000196 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000198 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
Jeremy Hyltona8293132006-02-28 17:58:27 +0000200 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201 switch (TYPE(n)) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000202 case file_input:
203 stmts = asdl_seq_new(num_stmts(n), arena);
204 if (!stmts)
205 return NULL;
206 for (i = 0; i < NCH(n) - 1; i++) {
207 ch = CHILD(n, i);
208 if (TYPE(ch) == NEWLINE)
209 continue;
210 REQ(ch, stmt);
211 num = num_stmts(ch);
212 if (num == 1) {
213 s = ast_for_stmt(&c, ch);
214 if (!s)
215 goto error;
216 asdl_seq_SET(stmts, k++, s);
217 }
218 else {
219 ch = CHILD(ch, 0);
220 REQ(ch, simple_stmt);
221 for (j = 0; j < num; j++) {
222 s = ast_for_stmt(&c, CHILD(ch, j * 2));
223 if (!s)
224 goto error;
225 asdl_seq_SET(stmts, k++, s);
226 }
227 }
228 }
229 return Module(stmts, arena);
230 case eval_input: {
231 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000233 /* XXX Why not gen_for here? */
234 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
235 if (!testlist_ast)
236 goto error;
237 return Expression(testlist_ast, arena);
238 }
239 case single_input:
240 if (TYPE(CHILD(n, 0)) == NEWLINE) {
241 stmts = asdl_seq_new(1, arena);
242 if (!stmts)
243 goto error;
244 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
245 arena));
246 return Interactive(stmts, arena);
247 }
248 else {
249 n = CHILD(n, 0);
250 num = num_stmts(n);
251 stmts = asdl_seq_new(num, arena);
252 if (!stmts)
253 goto error;
254 if (num == 1) {
255 s = ast_for_stmt(&c, n);
256 if (!s)
257 goto error;
258 asdl_seq_SET(stmts, 0, s);
259 }
260 else {
261 /* Only a simple_stmt can contain multiple statements. */
262 REQ(n, simple_stmt);
263 for (i = 0; i < NCH(n); i += 2) {
264 if (TYPE(CHILD(n, i)) == NEWLINE)
265 break;
266 s = ast_for_stmt(&c, CHILD(n, i));
267 if (!s)
268 goto error;
269 asdl_seq_SET(stmts, i / 2, s);
270 }
271 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000273 return Interactive(stmts, arena);
274 }
275 default:
276 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277 }
278 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 ast_error_finish(filename);
280 return NULL;
281}
282
283/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
284*/
285
286static operator_ty
287get_operator(const node *n)
288{
289 switch (TYPE(n)) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000290 case VBAR:
291 return BitOr;
292 case CIRCUMFLEX:
293 return BitXor;
294 case AMPER:
295 return BitAnd;
296 case LEFTSHIFT:
297 return LShift;
298 case RIGHTSHIFT:
299 return RShift;
300 case PLUS:
301 return Add;
302 case MINUS:
303 return Sub;
304 case STAR:
305 return Mult;
306 case SLASH:
307 return Div;
308 case DOUBLESLASH:
309 return FloorDiv;
310 case PERCENT:
311 return Mod;
312 default:
313 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314 }
315}
316
Jeremy Hyltona8293132006-02-28 17:58:27 +0000317/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318
319 Only sets context for expr kinds that "can appear in assignment context"
320 (according to ../Parser/Python.asdl). For other expr kinds, it sets
321 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322*/
323
324static int
325set_context(expr_ty e, expr_context_ty ctx, const node *n)
326{
327 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000328 /* If a particular expression type can't be used for assign / delete,
329 set expr_name to its name and an error message will be generated.
330 */
331 const char* expr_name = NULL;
332
333 /* The ast defines augmented store and load contexts, but the
334 implementation here doesn't actually use them. The code may be
335 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000336 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000337 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000338 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000339 */
340 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341
342 switch (e->kind) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000343 case Attribute_kind:
344 if (ctx == Store &&
345 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
346 return ast_error(n, "assignment to None");
347 }
348 e->v.Attribute.ctx = ctx;
349 break;
350 case Subscript_kind:
351 e->v.Subscript.ctx = ctx;
352 break;
353 case Name_kind:
354 if (ctx == Store &&
355 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
356 return ast_error(n, "assignment to None");
357 }
358 e->v.Name.ctx = ctx;
359 break;
360 case List_kind:
361 e->v.List.ctx = ctx;
362 s = e->v.List.elts;
363 break;
364 case Tuple_kind:
365 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
366 return ast_error(n, "can't assign to ()");
367 e->v.Tuple.ctx = ctx;
368 s = e->v.Tuple.elts;
369 break;
370 case Lambda_kind:
371 expr_name = "lambda";
372 break;
373 case Call_kind:
374 expr_name = "function call";
375 break;
376 case BoolOp_kind:
377 case BinOp_kind:
378 case UnaryOp_kind:
379 expr_name = "operator";
380 break;
381 case GeneratorExp_kind:
382 expr_name = "generator expression";
383 break;
384 case Yield_kind:
385 expr_name = "yield expression";
386 break;
387 case ListComp_kind:
388 expr_name = "list comprehension";
389 break;
390 case Dict_kind:
391 case Num_kind:
392 case Str_kind:
393 expr_name = "literal";
394 break;
395 case Compare_kind:
396 expr_name = "comparison";
397 break;
398 case Repr_kind:
399 expr_name = "repr";
400 break;
401 case IfExp_kind:
402 expr_name = "conditional expression";
403 break;
404 default:
405 PyErr_Format(PyExc_SystemError,
406 "unexpected expression in assignment %d (line %d)",
407 e->kind, e->lineno);
408 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000410 /* Check for error string set by switch */
411 if (expr_name) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000412 char buf[300];
413 PyOS_snprintf(buf, sizeof(buf),
414 "can't %s %s",
415 ctx == Store ? "assign to" : "delete",
416 expr_name);
417 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000418 }
419
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000421 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422 */
423 if (s) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000424 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000426 for (i = 0; i < asdl_seq_LEN(s); i++) {
427 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
428 return 0;
429 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430 }
431 return 1;
432}
433
434static operator_ty
435ast_for_augassign(const node *n)
436{
437 REQ(n, augassign);
438 n = CHILD(n, 0);
439 switch (STR(n)[0]) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000440 case '+':
441 return Add;
442 case '-':
443 return Sub;
444 case '/':
445 if (STR(n)[1] == '/')
446 return FloorDiv;
447 else
448 return Div;
449 case '%':
450 return Mod;
451 case '<':
452 return LShift;
453 case '>':
454 return RShift;
455 case '&':
456 return BitAnd;
457 case '^':
458 return BitXor;
459 case '|':
460 return BitOr;
461 case '*':
462 if (STR(n)[1] == '*')
463 return Pow;
464 else
465 return Mult;
466 default:
467 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
468 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469 }
470}
471
472static cmpop_ty
473ast_for_comp_op(const node *n)
474{
475 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000476 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 */
478 REQ(n, comp_op);
479 if (NCH(n) == 1) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000480 n = CHILD(n, 0);
481 switch (TYPE(n)) {
482 case LESS:
483 return Lt;
484 case GREATER:
485 return Gt;
486 case EQEQUAL: /* == */
487 return Eq;
488 case LESSEQUAL:
489 return LtE;
490 case GREATEREQUAL:
491 return GtE;
492 case NOTEQUAL:
493 return NotEq;
494 case NAME:
495 if (strcmp(STR(n), "in") == 0)
496 return In;
497 if (strcmp(STR(n), "is") == 0)
498 return Is;
499 default:
500 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
501 STR(n));
502 return (cmpop_ty)0;
503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 }
505 else if (NCH(n) == 2) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000506 /* handle "not in" and "is not" */
507 switch (TYPE(CHILD(n, 0))) {
508 case NAME:
509 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
510 return NotIn;
511 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
512 return IsNot;
513 default:
514 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
515 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
516 return (cmpop_ty)0;
517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 }
Neal Norwitz79792652005-11-14 04:25:03 +0000519 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000520 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000521 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522}
523
524static asdl_seq *
525seq_for_testlist(struct compiling *c, const node *n)
526{
527 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000528 asdl_seq *seq;
529 expr_ty expression;
530 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 assert(TYPE(n) == testlist
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000532 || TYPE(n) == listmaker
533 || TYPE(n) == testlist_gexp
534 || TYPE(n) == testlist_safe
535 || TYPE(n) == testlist1
536 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000538 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539 if (!seq)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000540 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
542 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000543 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000545 expression = ast_for_expr(c, CHILD(n, i));
546 if (!expression)
547 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000549 assert(i / 2 < seq->size);
550 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551 }
552 return seq;
553}
554
555static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000556compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557{
558 int i, len = (NCH(n) + 1) / 2;
559 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000560 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 if (!args)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000562 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563
Neal Norwitz3a230172006-09-22 08:18:10 +0000564 /* fpdef: NAME | '(' fplist ')'
565 fplist: fpdef (',' fpdef)* [',']
566 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 for (i = 0; i < len; i++) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000569 const node *fpdef_node = CHILD(n, 2*i);
570 const node *child;
571 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000572set_name:
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000573 /* fpdef_node is either a NAME or an fplist */
574 child = CHILD(fpdef_node, 0);
575 if (TYPE(child) == NAME) {
576 if (!strcmp(STR(child), "None")) {
577 ast_error(child, "assignment to None");
578 return NULL;
579 }
580 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
581 child->n_col_offset, c->c_arena);
582 }
583 else {
584 assert(TYPE(fpdef_node) == fpdef);
585 /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */
586 child = CHILD(fpdef_node, 1);
587 assert(TYPE(child) == fplist);
588 /* NCH == 1 means we have (x), we need to elide the extra parens */
589 if (NCH(child) == 1) {
590 fpdef_node = CHILD(child, 0);
591 assert(TYPE(fpdef_node) == fpdef);
592 goto set_name;
593 }
594 arg = compiler_complex_args(c, child);
595 }
596 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 }
598
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000599 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000600 if (!set_context(result, Store, n))
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000601 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 return result;
603}
604
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
Jeremy Hyltona8293132006-02-28 17:58:27 +0000606/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607
608static arguments_ty
609ast_for_arguments(struct compiling *c, const node *n)
610{
611 /* parameters: '(' [varargslist] ')'
612 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000613 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000615 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 asdl_seq *args, *defaults;
617 identifier vararg = NULL, kwarg = NULL;
618 node *ch;
619
620 if (TYPE(n) == parameters) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000621 if (NCH(n) == 2) /* () as argument list */
622 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
623 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 }
625 REQ(n, varargslist);
626
627 /* first count the number of normal args & defaults */
628 for (i = 0; i < NCH(n); i++) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000629 ch = CHILD(n, i);
630 if (TYPE(ch) == fpdef)
631 n_args++;
632 if (TYPE(ch) == EQUAL)
633 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000635 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 if (!args && n_args)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000637 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000638 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 if (!defaults && n_defaults)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000640 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641
642 /* fpdef: NAME | '(' fplist ')'
643 fplist: fpdef (',' fpdef)* [',']
644 */
645 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000646 j = 0; /* index for defaults */
647 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 while (i < NCH(n)) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000649 ch = CHILD(n, i);
650 switch (TYPE(ch)) {
651 case fpdef:
652 handle_fpdef:
653 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
654 anything other than EQUAL or a comma? */
655 /* XXX Should NCH(n) check be made a separate check? */
656 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
657 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
658 if (!expression)
659 goto error;
660 assert(defaults != NULL);
661 asdl_seq_SET(defaults, j++, expression);
662 i += 2;
663 found_default = 1;
664 }
665 else if (found_default) {
666 ast_error(n,
667 "non-default argument follows default argument");
668 goto error;
669 }
670 if (NCH(ch) == 3) {
671 ch = CHILD(ch, 1);
672 /* def foo((x)): is not complex, special case. */
673 if (NCH(ch) != 1) {
674 /* We have complex arguments, setup for unpacking. */
675 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
676 } else {
677 /* def foo((x)): setup for checking NAME below. */
678 /* Loop because there can be many parens and tuple
679 unpacking mixed in. */
680 ch = CHILD(ch, 0);
681 assert(TYPE(ch) == fpdef);
682 goto handle_fpdef;
683 }
684 }
685 if (TYPE(CHILD(ch, 0)) == NAME) {
686 expr_ty name;
687 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
688 ast_error(CHILD(ch, 0), "assignment to None");
689 goto error;
690 }
691 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
692 Param, LINENO(ch), ch->n_col_offset,
693 c->c_arena);
694 if (!name)
695 goto error;
696 asdl_seq_SET(args, k++, name);
697
698 }
699 i += 2; /* the name and the comma */
700 break;
701 case STAR:
702 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
703 ast_error(CHILD(n, i+1), "assignment to None");
704 goto error;
705 }
706 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
707 i += 3;
708 break;
709 case DOUBLESTAR:
710 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
711 ast_error(CHILD(n, i+1), "assignment to None");
712 goto error;
713 }
714 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
715 i += 3;
716 break;
717 default:
718 PyErr_Format(PyExc_SystemError,
719 "unexpected node in varargslist: %d @ %d",
720 TYPE(ch), i);
721 goto error;
722 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 }
724
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000725 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726
727 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000728 Py_XDECREF(vararg);
729 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730 return NULL;
731}
732
733static expr_ty
734ast_for_dotted_name(struct compiling *c, const node *n)
735{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000736 expr_ty e;
737 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000738 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 int i;
740
741 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000742
743 lineno = LINENO(n);
744 col_offset = n->n_col_offset;
745
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 id = NEW_IDENTIFIER(CHILD(n, 0));
747 if (!id)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000748 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000749 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 if (!e)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000751 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752
753 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000754 id = NEW_IDENTIFIER(CHILD(n, i));
755 if (!id)
756 return NULL;
757 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
758 if (!e)
759 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 }
761
762 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763}
764
765static expr_ty
766ast_for_decorator(struct compiling *c, const node *n)
767{
768 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
769 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000770 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771
772 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000773 REQ(CHILD(n, 0), AT);
774 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775
776 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
777 if (!name_expr)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000778 return NULL;
779
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 if (NCH(n) == 3) { /* No arguments */
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000781 d = name_expr;
782 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 }
784 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000785 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
786 n->n_col_offset, c->c_arena);
787 if (!d)
788 return NULL;
789 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 }
791 else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000792 d = ast_for_call(c, CHILD(n, 3), name_expr);
793 if (!d)
794 return NULL;
795 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 }
797
798 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799}
800
801static asdl_seq*
802ast_for_decorators(struct compiling *c, const node *n)
803{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000804 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000805 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 int i;
807
808 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000809 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 if (!decorator_seq)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000811 return NULL;
812
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 for (i = 0; i < NCH(n); i++) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000814 d = ast_for_decorator(c, CHILD(n, i));
815 if (!d)
816 return NULL;
817 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 }
819 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820}
821
822static stmt_ty
823ast_for_funcdef(struct compiling *c, const node *n)
824{
825 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000826 identifier name;
827 arguments_ty args;
828 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 asdl_seq *decorator_seq = NULL;
830 int name_i;
831
832 REQ(n, funcdef);
833
834 if (NCH(n) == 6) { /* decorators are present */
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000835 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
836 if (!decorator_seq)
837 return NULL;
838 name_i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 }
840 else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000841 name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 }
843
844 name = NEW_IDENTIFIER(CHILD(n, name_i));
845 if (!name)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000846 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000848 ast_error(CHILD(n, name_i), "assignment to None");
849 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 }
851 args = ast_for_arguments(c, CHILD(n, name_i + 1));
852 if (!args)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000853 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 body = ast_for_suite(c, CHILD(n, name_i + 3));
855 if (!body)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000856 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000858 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000859 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860}
861
862static expr_ty
863ast_for_lambdef(struct compiling *c, const node *n)
864{
865 /* lambdef: 'lambda' [varargslist] ':' test */
866 arguments_ty args;
867 expr_ty expression;
868
869 if (NCH(n) == 3) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000870 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
871 if (!args)
872 return NULL;
873 expression = ast_for_expr(c, CHILD(n, 2));
874 if (!expression)
875 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 }
877 else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000878 args = ast_for_arguments(c, CHILD(n, 1));
879 if (!args)
880 return NULL;
881 expression = ast_for_expr(c, CHILD(n, 3));
882 if (!expression)
883 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 }
885
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000886 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887}
888
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000889static expr_ty
890ast_for_ifexpr(struct compiling *c, const node *n)
891{
892 /* test: or_test 'if' or_test 'else' test */
893 expr_ty expression, body, orelse;
894
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000895 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000896 body = ast_for_expr(c, CHILD(n, 0));
897 if (!body)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000898 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000899 expression = ast_for_expr(c, CHILD(n, 2));
900 if (!expression)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000901 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000902 orelse = ast_for_expr(c, CHILD(n, 4));
903 if (!orelse)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000904 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000905 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000906 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000907}
908
Neal Norwitze4d4f002006-09-05 03:58:26 +0000909/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
910 so there is only a single version. Possibly for loops can also re-use
911 the code.
912*/
913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914/* Count the number of 'for' loop in a list comprehension.
915
916 Helper for ast_for_listcomp().
917*/
918
919static int
920count_list_fors(const node *n)
921{
922 int n_fors = 0;
923 node *ch = CHILD(n, 1);
924
925 count_list_for:
926 n_fors++;
927 REQ(ch, list_for);
928 if (NCH(ch) == 5)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000929 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 else
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000931 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 count_list_iter:
933 REQ(ch, list_iter);
934 ch = CHILD(ch, 0);
935 if (TYPE(ch) == list_for)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000936 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 else if (TYPE(ch) == list_if) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000938 if (NCH(ch) == 3) {
939 ch = CHILD(ch, 2);
940 goto count_list_iter;
941 }
942 else
943 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000945
946 /* Should never be reached */
947 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
948 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949}
950
951/* Count the number of 'if' statements in a list comprehension.
952
953 Helper for ast_for_listcomp().
954*/
955
956static int
957count_list_ifs(const node *n)
958{
959 int n_ifs = 0;
960
961 count_list_iter:
962 REQ(n, list_iter);
963 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000964 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 n = CHILD(n, 0);
966 REQ(n, list_if);
967 n_ifs++;
968 if (NCH(n) == 2)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000969 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 n = CHILD(n, 2);
971 goto count_list_iter;
972}
973
974static expr_ty
975ast_for_listcomp(struct compiling *c, const node *n)
976{
977 /* listmaker: test ( list_for | (',' test)* [','] )
978 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
979 list_iter: list_for | list_if
980 list_if: 'if' test [list_iter]
981 testlist_safe: test [(',' test)+ [',']]
982 */
983 expr_ty elt;
984 asdl_seq *listcomps;
985 int i, n_fors;
986 node *ch;
987
988 REQ(n, listmaker);
989 assert(NCH(n) > 1);
990
991 elt = ast_for_expr(c, CHILD(n, 0));
992 if (!elt)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994
995 n_fors = count_list_fors(n);
996 if (n_fors == -1)
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000997 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000999 listcomps = asdl_seq_new(n_fors, c->c_arena);
1000 if (!listcomps)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001001 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001002
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 ch = CHILD(n, 1);
1004 for (i = 0; i < n_fors; i++) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001005 comprehension_ty lc;
1006 asdl_seq *t;
1007 expr_ty expression;
1008 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001010 REQ(ch, list_for);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001012 for_ch = CHILD(ch, 1);
1013 t = ast_for_exprlist(c, for_ch, Store);
1014 if (!t)
1015 return NULL;
1016 expression = ast_for_testlist(c, CHILD(ch, 3));
1017 if (!expression)
1018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001020 /* Check the # of children rather than the length of t, since
1021 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
1022 if (NCH(for_ch) == 1)
1023 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1024 c->c_arena);
1025 else
1026 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1027 c->c_arena),
1028 expression, NULL, c->c_arena);
1029 if (!lc)
1030 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001032 if (NCH(ch) == 5) {
1033 int j, n_ifs;
1034 asdl_seq *ifs;
Collin Winter77c67bd2007-03-16 04:11:30 +00001035 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001037 ch = CHILD(ch, 4);
1038 n_ifs = count_list_ifs(ch);
1039 if (n_ifs == -1)
1040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001042 ifs = asdl_seq_new(n_ifs, c->c_arena);
1043 if (!ifs)
1044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001046 for (j = 0; j < n_ifs; j++) {
1047 REQ(ch, list_iter);
1048 ch = CHILD(ch, 0);
1049 REQ(ch, list_if);
Collin Winter77c67bd2007-03-16 04:11:30 +00001050
1051 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1052 if (!list_for_expr)
1053 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054
Collin Winter77c67bd2007-03-16 04:11:30 +00001055 asdl_seq_SET(ifs, j, list_for_expr);
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001056 if (NCH(ch) == 3)
1057 ch = CHILD(ch, 2);
1058 }
1059 /* on exit, must guarantee that ch is a list_for */
1060 if (TYPE(ch) == list_iter)
1061 ch = CHILD(ch, 0);
1062 lc->ifs = ifs;
1063 }
1064 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 }
1066
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001067 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068}
1069
1070/*
1071 Count the number of 'for' loops in a generator expression.
1072
1073 Helper for ast_for_genexp().
1074*/
1075
1076static int
1077count_gen_fors(const node *n)
1078{
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001079 int n_fors = 0;
1080 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081
1082 count_gen_for:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001083 n_fors++;
1084 REQ(ch, gen_for);
1085 if (NCH(ch) == 5)
1086 ch = CHILD(ch, 4);
1087 else
1088 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089 count_gen_iter:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001090 REQ(ch, gen_iter);
1091 ch = CHILD(ch, 0);
1092 if (TYPE(ch) == gen_for)
1093 goto count_gen_for;
1094 else if (TYPE(ch) == gen_if) {
1095 if (NCH(ch) == 3) {
1096 ch = CHILD(ch, 2);
1097 goto count_gen_iter;
1098 }
1099 else
1100 return n_fors;
1101 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001102
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001103 /* Should never be reached */
1104 PyErr_SetString(PyExc_SystemError,
1105 "logic error in count_gen_fors");
1106 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107}
1108
1109/* Count the number of 'if' statements in a generator expression.
1110
1111 Helper for ast_for_genexp().
1112*/
1113
1114static int
1115count_gen_ifs(const node *n)
1116{
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001117 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001119 while (1) {
1120 REQ(n, gen_iter);
1121 if (TYPE(CHILD(n, 0)) == gen_for)
1122 return n_ifs;
1123 n = CHILD(n, 0);
1124 REQ(n, gen_if);
1125 n_ifs++;
1126 if (NCH(n) == 2)
1127 return n_ifs;
1128 n = CHILD(n, 2);
1129 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130}
1131
Jeremy Hyltona8293132006-02-28 17:58:27 +00001132/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133static expr_ty
1134ast_for_genexp(struct compiling *c, const node *n)
1135{
1136 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001137 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 expr_ty elt;
1139 asdl_seq *genexps;
1140 int i, n_fors;
1141 node *ch;
1142
1143 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1144 assert(NCH(n) > 1);
1145
1146 elt = ast_for_expr(c, CHILD(n, 0));
1147 if (!elt)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001148 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149
1150 n_fors = count_gen_fors(n);
1151 if (n_fors == -1)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001152 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001153
1154 genexps = asdl_seq_new(n_fors, c->c_arena);
1155 if (!genexps)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001156 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001157
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 ch = CHILD(n, 1);
1159 for (i = 0; i < n_fors; i++) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001160 comprehension_ty ge;
1161 asdl_seq *t;
1162 expr_ty expression;
1163 node *for_ch;
1164
1165 REQ(ch, gen_for);
1166
1167 for_ch = CHILD(ch, 1);
1168 t = ast_for_exprlist(c, for_ch, Store);
1169 if (!t)
1170 return NULL;
1171 expression = ast_for_expr(c, CHILD(ch, 3));
1172 if (!expression)
1173 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001174
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001175 /* Check the # of children rather than the length of t, since
1176 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1177 if (NCH(for_ch) == 1)
1178 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1179 NULL, c->c_arena);
1180 else
1181 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1182 c->c_arena),
1183 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001184
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001185 if (!ge)
1186 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001188 if (NCH(ch) == 5) {
1189 int j, n_ifs;
1190 asdl_seq *ifs;
1191
1192 ch = CHILD(ch, 4);
1193 n_ifs = count_gen_ifs(ch);
1194 if (n_ifs == -1)
1195 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001196
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001197 ifs = asdl_seq_new(n_ifs, c->c_arena);
1198 if (!ifs)
1199 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001201 for (j = 0; j < n_ifs; j++) {
1202 REQ(ch, gen_iter);
1203 ch = CHILD(ch, 0);
1204 REQ(ch, gen_if);
1205
1206 expression = ast_for_expr(c, CHILD(ch, 1));
1207 if (!expression)
1208 return NULL;
1209 asdl_seq_SET(ifs, j, expression);
1210 if (NCH(ch) == 3)
1211 ch = CHILD(ch, 2);
1212 }
1213 /* on exit, must guarantee that ch is a gen_for */
1214 if (TYPE(ch) == gen_iter)
1215 ch = CHILD(ch, 0);
1216 ge->ifs = ifs;
1217 }
1218 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 }
1220
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001221 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222}
1223
1224static expr_ty
1225ast_for_atom(struct compiling *c, const node *n)
1226{
1227 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1228 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1229 */
1230 node *ch = CHILD(n, 0);
1231
1232 switch (TYPE(ch)) {
1233 case NAME:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001234 /* All names start in Load context, but may later be
1235 changed. */
1236 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 case STRING: {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001238 PyObject *str = parsestrplus(c, n);
1239 if (!str)
1240 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001242 PyArena_AddPyObject(c->c_arena, str);
1243 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 }
1245 case NUMBER: {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001246 PyObject *pynum = parsenumber(STR(ch));
1247 if (!pynum)
1248 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001249
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001250 PyArena_AddPyObject(c->c_arena, pynum);
1251 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 }
1253 case LPAR: /* some parenthesized expressions */
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001254 ch = CHILD(n, 1);
1255
1256 if (TYPE(ch) == RPAR)
1257 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1258
1259 if (TYPE(ch) == yield_expr)
1260 return ast_for_expr(c, ch);
1261
1262 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1263 return ast_for_genexp(c, ch);
1264
1265 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 case LSQB: /* list (or list comprehension) */
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001267 ch = CHILD(n, 1);
1268
1269 if (TYPE(ch) == RSQB)
1270 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1271
1272 REQ(ch, listmaker);
1273 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1274 asdl_seq *elts = seq_for_testlist(c, ch);
1275 if (!elts)
1276 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001277
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001278 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1279 }
1280 else
1281 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 case LBRACE: {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001283 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1284 int i, size;
1285 asdl_seq *keys, *values;
1286
1287 ch = CHILD(n, 1);
1288 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1289 keys = asdl_seq_new(size, c->c_arena);
1290 if (!keys)
1291 return NULL;
1292
1293 values = asdl_seq_new(size, c->c_arena);
1294 if (!values)
1295 return NULL;
1296
1297 for (i = 0; i < NCH(ch); i += 4) {
1298 expr_ty expression;
1299
1300 expression = ast_for_expr(c, CHILD(ch, i));
1301 if (!expression)
1302 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001303
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001304 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001305
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001306 expression = ast_for_expr(c, CHILD(ch, i + 2));
1307 if (!expression)
1308 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001309
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001310 asdl_seq_SET(values, i / 4, expression);
1311 }
1312 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 }
1314 case BACKQUOTE: { /* repr */
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001315 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
1316 if (!expression)
1317 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001318
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001319 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 }
1321 default:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001322 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1323 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 }
1325}
1326
1327static slice_ty
1328ast_for_slice(struct compiling *c, const node *n)
1329{
1330 node *ch;
1331 expr_ty lower = NULL, upper = NULL, step = NULL;
1332
1333 REQ(n, subscript);
1334
1335 /*
1336 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1337 sliceop: ':' [test]
1338 */
1339 ch = CHILD(n, 0);
1340 if (TYPE(ch) == DOT)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001341 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342
1343 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001344 /* 'step' variable hold no significance in terms of being used over
1345 other vars */
1346 step = ast_for_expr(c, ch);
1347 if (!step)
1348 return NULL;
1349
1350 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 }
1352
1353 if (TYPE(ch) == test) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001354 lower = ast_for_expr(c, ch);
1355 if (!lower)
1356 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 }
1358
1359 /* If there's an upper bound it's in the second or third position. */
1360 if (TYPE(ch) == COLON) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001361 if (NCH(n) > 1) {
1362 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001364 if (TYPE(n2) == test) {
1365 upper = ast_for_expr(c, n2);
1366 if (!upper)
1367 return NULL;
1368 }
1369 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 } else if (NCH(n) > 2) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001371 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001373 if (TYPE(n2) == test) {
1374 upper = ast_for_expr(c, n2);
1375 if (!upper)
1376 return NULL;
1377 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 }
1379
1380 ch = CHILD(n, NCH(n) - 1);
1381 if (TYPE(ch) == sliceop) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001382 if (NCH(ch) == 1) {
1383 /* No expression, so step is None */
1384 ch = CHILD(ch, 0);
1385 step = Name(new_identifier("None", c->c_arena), Load,
1386 LINENO(ch), ch->n_col_offset, c->c_arena);
1387 if (!step)
1388 return NULL;
1389 } else {
1390 ch = CHILD(ch, 1);
1391 if (TYPE(ch) == test) {
1392 step = ast_for_expr(c, ch);
1393 if (!step)
1394 return NULL;
1395 }
1396 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 }
1398
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001399 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400}
1401
1402static expr_ty
1403ast_for_binop(struct compiling *c, const node *n)
1404{
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001405 /* Must account for a sequence of expressions.
1406 How should A op B op C by represented?
1407 BinOp(BinOp(A, op, B), op, C).
1408 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001410 int i, nops;
1411 expr_ty expr1, expr2, result;
1412 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001414 expr1 = ast_for_expr(c, CHILD(n, 0));
1415 if (!expr1)
1416 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001418 expr2 = ast_for_expr(c, CHILD(n, 2));
1419 if (!expr2)
1420 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001422 newoperator = get_operator(CHILD(n, 1));
1423 if (!newoperator)
1424 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001426 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1427 c->c_arena);
1428 if (!result)
1429 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001431 nops = (NCH(n) - 1) / 2;
1432 for (i = 1; i < nops; i++) {
1433 expr_ty tmp_result, tmp;
1434 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001436 newoperator = get_operator(next_oper);
1437 if (!newoperator)
1438 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001440 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1441 if (!tmp)
1442 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001444 tmp_result = BinOp(result, newoperator, tmp,
1445 LINENO(next_oper), next_oper->n_col_offset,
1446 c->c_arena);
1447 if (!tmp)
1448 return NULL;
1449 result = tmp_result;
1450 }
1451 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452}
1453
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001454static expr_ty
1455ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1456{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001457 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1458 subscriptlist: subscript (',' subscript)* [',']
1459 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1460 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001461 REQ(n, trailer);
1462 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001463 if (NCH(n) == 2)
1464 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1465 n->n_col_offset, c->c_arena);
1466 else
1467 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001468 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001469 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001470 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1471 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001472 }
1473 else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001474 REQ(CHILD(n, 0), LSQB);
1475 REQ(CHILD(n, 2), RSQB);
1476 n = CHILD(n, 1);
1477 if (NCH(n) == 1) {
1478 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1479 if (!slc)
1480 return NULL;
1481 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1482 c->c_arena);
1483 }
1484 else {
1485 /* The grammar is ambiguous here. The ambiguity is resolved
1486 by treating the sequence as a tuple literal if there are
1487 no slice features.
1488 */
1489 int j;
1490 slice_ty slc;
1491 expr_ty e;
1492 bool simple = true;
1493 asdl_seq *slices, *elts;
1494 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1495 if (!slices)
1496 return NULL;
1497 for (j = 0; j < NCH(n); j += 2) {
1498 slc = ast_for_slice(c, CHILD(n, j));
1499 if (!slc)
1500 return NULL;
1501 if (slc->kind != Index_kind)
1502 simple = false;
1503 asdl_seq_SET(slices, j / 2, slc);
1504 }
1505 if (!simple) {
1506 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1507 Load, LINENO(n), n->n_col_offset, c->c_arena);
1508 }
1509 /* extract Index values and put them in a Tuple */
1510 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1511 if (!elts)
1512 return NULL;
1513 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1514 slc = (slice_ty)asdl_seq_GET(slices, j);
1515 assert(slc->kind == Index_kind && slc->v.Index.value);
1516 asdl_seq_SET(elts, j, slc->v.Index.value);
1517 }
1518 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1519 if (!e)
1520 return NULL;
1521 return Subscript(left_expr, Index(e, c->c_arena),
1522 Load, LINENO(n), n->n_col_offset, c->c_arena);
1523 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001524 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001525}
1526
1527static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001528ast_for_factor(struct compiling *c, const node *n)
1529{
1530 node *pfactor, *ppower, *patom, *pnum;
1531 expr_ty expression;
1532
1533 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001534 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001535 constant. The peephole optimizer already does something like
1536 this but it doesn't handle the case where the constant is
1537 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1538 PyLongObject.
1539 */
1540 if (TYPE(CHILD(n, 0)) == MINUS
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001541 && NCH(n) == 2
1542 && TYPE((pfactor = CHILD(n, 1))) == factor
1543 && NCH(pfactor) == 1
1544 && TYPE((ppower = CHILD(pfactor, 0))) == power
1545 && NCH(ppower) == 1
1546 && TYPE((patom = CHILD(ppower, 0))) == atom
1547 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1548 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1549 if (s == NULL)
1550 return NULL;
1551 s[0] = '-';
1552 strcpy(s + 1, STR(pnum));
1553 PyObject_FREE(STR(pnum));
1554 STR(pnum) = s;
1555 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001556 }
1557
1558 expression = ast_for_expr(c, CHILD(n, 1));
1559 if (!expression)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001560 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001561
1562 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001563 case PLUS:
1564 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1565 c->c_arena);
1566 case MINUS:
1567 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1568 c->c_arena);
1569 case TILDE:
1570 return UnaryOp(Invert, expression, LINENO(n),
1571 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001572 }
1573 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001574 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001575 return NULL;
1576}
1577
1578static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001579ast_for_power(struct compiling *c, const node *n)
1580{
1581 /* power: atom trailer* ('**' factor)*
1582 */
1583 int i;
1584 expr_ty e, tmp;
1585 REQ(n, power);
1586 e = ast_for_atom(c, CHILD(n, 0));
1587 if (!e)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001588 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001589 if (NCH(n) == 1)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001590 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001591 for (i = 1; i < NCH(n); i++) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001592 node *ch = CHILD(n, i);
1593 if (TYPE(ch) != trailer)
1594 break;
1595 tmp = ast_for_trailer(c, ch, e);
1596 if (!tmp)
1597 return NULL;
1598 tmp->lineno = e->lineno;
1599 tmp->col_offset = e->col_offset;
1600 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001601 }
1602 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001603 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1604 if (!f)
1605 return NULL;
1606 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1607 if (!tmp)
1608 return NULL;
1609 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001610 }
1611 return e;
1612}
1613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614/* Do not name a variable 'expr'! Will cause a compile error.
1615*/
1616
1617static expr_ty
1618ast_for_expr(struct compiling *c, const node *n)
1619{
1620 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001621 test: or_test ['if' or_test 'else' test] | lambdef
1622 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 and_test: not_test ('and' not_test)*
1624 not_test: 'not' not_test | comparison
1625 comparison: expr (comp_op expr)*
1626 expr: xor_expr ('|' xor_expr)*
1627 xor_expr: and_expr ('^' and_expr)*
1628 and_expr: shift_expr ('&' shift_expr)*
1629 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1630 arith_expr: term (('+'|'-') term)*
1631 term: factor (('*'|'/'|'%'|'//') factor)*
1632 factor: ('+'|'-'|'~') factor | power
1633 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001634
1635 As well as modified versions that exist for backward compatibility,
1636 to explicitly allow:
1637 [ x for x in lambda: 0, lambda: 1 ]
1638 (which would be ambiguous without these extra rules)
1639
1640 old_test: or_test | old_lambdef
1641 old_lambdef: 'lambda' [vararglist] ':' old_test
1642
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 */
1644
1645 asdl_seq *seq;
1646 int i;
1647
1648 loop:
1649 switch (TYPE(n)) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001650 case test:
1651 case old_test:
1652 if (TYPE(CHILD(n, 0)) == lambdef ||
1653 TYPE(CHILD(n, 0)) == old_lambdef)
1654 return ast_for_lambdef(c, CHILD(n, 0));
1655 else if (NCH(n) > 1)
1656 return ast_for_ifexpr(c, n);
1657 /* Fallthrough */
1658 case or_test:
1659 case and_test:
1660 if (NCH(n) == 1) {
1661 n = CHILD(n, 0);
1662 goto loop;
1663 }
1664 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1665 if (!seq)
1666 return NULL;
1667 for (i = 0; i < NCH(n); i += 2) {
1668 expr_ty e = ast_for_expr(c, CHILD(n, i));
1669 if (!e)
1670 return NULL;
1671 asdl_seq_SET(seq, i / 2, e);
1672 }
1673 if (!strcmp(STR(CHILD(n, 1)), "and"))
1674 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1675 c->c_arena);
1676 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1677 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1678 case not_test:
1679 if (NCH(n) == 1) {
1680 n = CHILD(n, 0);
1681 goto loop;
1682 }
1683 else {
1684 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1685 if (!expression)
1686 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001688 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1689 c->c_arena);
1690 }
1691 case comparison:
1692 if (NCH(n) == 1) {
1693 n = CHILD(n, 0);
1694 goto loop;
1695 }
1696 else {
1697 expr_ty expression;
1698 asdl_int_seq *ops;
1699 asdl_seq *cmps;
1700 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1701 if (!ops)
1702 return NULL;
1703 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1704 if (!cmps) {
1705 return NULL;
1706 }
1707 for (i = 1; i < NCH(n); i += 2) {
1708 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001710 newoperator = ast_for_comp_op(CHILD(n, i));
1711 if (!newoperator) {
1712 return NULL;
1713 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001715 expression = ast_for_expr(c, CHILD(n, i + 1));
1716 if (!expression) {
1717 return NULL;
1718 }
1719
1720 asdl_seq_SET(ops, i / 2, newoperator);
1721 asdl_seq_SET(cmps, i / 2, expression);
1722 }
1723 expression = ast_for_expr(c, CHILD(n, 0));
1724 if (!expression) {
1725 return NULL;
1726 }
1727
1728 return Compare(expression, ops, cmps, LINENO(n),
1729 n->n_col_offset, c->c_arena);
1730 }
1731 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001733 /* The next five cases all handle BinOps. The main body of code
1734 is the same in each case, but the switch turned inside out to
1735 reuse the code for each type of operator.
1736 */
1737 case expr:
1738 case xor_expr:
1739 case and_expr:
1740 case shift_expr:
1741 case arith_expr:
1742 case term:
1743 if (NCH(n) == 1) {
1744 n = CHILD(n, 0);
1745 goto loop;
1746 }
1747 return ast_for_binop(c, n);
1748 case yield_expr: {
1749 expr_ty exp = NULL;
1750 if (NCH(n) == 2) {
1751 exp = ast_for_testlist(c, CHILD(n, 1));
1752 if (!exp)
1753 return NULL;
1754 }
1755 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1756 }
1757 case factor:
1758 if (NCH(n) == 1) {
1759 n = CHILD(n, 0);
1760 goto loop;
1761 }
1762 return ast_for_factor(c, n);
1763 case power:
1764 return ast_for_power(c, n);
1765 default:
1766 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1767 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001769 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 return NULL;
1771}
1772
1773static expr_ty
1774ast_for_call(struct compiling *c, const node *n, expr_ty func)
1775{
1776 /*
1777 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001778 | '**' test)
1779 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 */
1781
1782 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001783 asdl_seq *args;
1784 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 expr_ty vararg = NULL, kwarg = NULL;
1786
1787 REQ(n, arglist);
1788
1789 nargs = 0;
1790 nkeywords = 0;
1791 ngens = 0;
1792 for (i = 0; i < NCH(n); i++) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001793 node *ch = CHILD(n, i);
1794 if (TYPE(ch) == argument) {
1795 if (NCH(ch) == 1)
1796 nargs++;
1797 else if (TYPE(CHILD(ch, 1)) == gen_for)
1798 ngens++;
1799 else
1800 nkeywords++;
1801 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 }
1803 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001804 ast_error(n, "Generator expression must be parenthesized "
1805 "if not sole argument");
1806 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 }
1808
1809 if (nargs + nkeywords + ngens > 255) {
1810 ast_error(n, "more than 255 arguments");
1811 return NULL;
1812 }
1813
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001814 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 if (!args)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001816 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 if (!keywords)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001819 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 nargs = 0;
1821 nkeywords = 0;
1822 for (i = 0; i < NCH(n); i++) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001823 node *ch = CHILD(n, i);
1824 if (TYPE(ch) == argument) {
1825 expr_ty e;
1826 if (NCH(ch) == 1) {
1827 if (nkeywords) {
1828 ast_error(CHILD(ch, 0),
1829 "non-keyword arg after keyword arg");
1830 return NULL;
1831 }
1832 e = ast_for_expr(c, CHILD(ch, 0));
1833 if (!e)
1834 return NULL;
1835 asdl_seq_SET(args, nargs++, e);
1836 }
1837 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1838 e = ast_for_genexp(c, ch);
1839 if (!e)
1840 return NULL;
1841 asdl_seq_SET(args, nargs++, e);
1842 }
1843 else {
1844 keyword_ty kw;
1845 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001847 /* CHILD(ch, 0) is test, but must be an identifier? */
1848 e = ast_for_expr(c, CHILD(ch, 0));
1849 if (!e)
1850 return NULL;
1851 /* f(lambda x: x[0] = 3) ends up getting parsed with
1852 * LHS test = lambda x: x[0], and RHS test = 3.
1853 * SF bug 132313 points out that complaining about a keyword
1854 * then is very confusing.
1855 */
1856 if (e->kind == Lambda_kind) {
1857 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
1858 return NULL;
1859 } else if (e->kind != Name_kind) {
1860 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1861 return NULL;
1862 }
1863 key = e->v.Name.id;
1864 e = ast_for_expr(c, CHILD(ch, 2));
1865 if (!e)
1866 return NULL;
1867 kw = keyword(key, e, c->c_arena);
1868 if (!kw)
1869 return NULL;
1870 asdl_seq_SET(keywords, nkeywords++, kw);
1871 }
1872 }
1873 else if (TYPE(ch) == STAR) {
1874 vararg = ast_for_expr(c, CHILD(n, i+1));
1875 i++;
1876 }
1877 else if (TYPE(ch) == DOUBLESTAR) {
1878 kwarg = ast_for_expr(c, CHILD(n, i+1));
1879 i++;
1880 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 }
1882
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001883 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884}
1885
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001887ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001889 /* testlist_gexp: test (',' test)* [','] */
1890 /* testlist: test (',' test)* [','] */
1891 /* testlist_safe: test (',' test)+ [','] */
1892 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001894 if (TYPE(n) == testlist_gexp) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001895 if (NCH(n) > 1)
1896 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001897 }
1898 else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001899 assert(TYPE(n) == testlist ||
1900 TYPE(n) == testlist_safe ||
1901 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001902 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903 if (NCH(n) == 1)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001904 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001906 asdl_seq *tmp = seq_for_testlist(c, n);
1907 if (!tmp)
1908 return NULL;
1909 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001911}
1912
1913static expr_ty
1914ast_for_testlist_gexp(struct compiling *c, const node* n)
1915{
1916 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1917 /* argument: test [ gen_for ] */
1918 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001919 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001920 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001921 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001922}
1923
1924/* like ast_for_testlist() but returns a sequence */
1925static asdl_seq*
1926ast_for_class_bases(struct compiling *c, const node* n)
1927{
1928 /* testlist: test (',' test)* [','] */
1929 assert(NCH(n) > 0);
1930 REQ(n, testlist);
1931 if (NCH(n) == 1) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001932 expr_ty base;
1933 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
1934 if (!bases)
1935 return NULL;
1936 base = ast_for_expr(c, CHILD(n, 0));
1937 if (!base)
1938 return NULL;
1939 asdl_seq_SET(bases, 0, base);
1940 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001941 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001942
1943 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944}
1945
1946static stmt_ty
1947ast_for_expr_stmt(struct compiling *c, const node *n)
1948{
1949 REQ(n, expr_stmt);
1950 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001951 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952 testlist: test (',' test)* [',']
1953 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001954 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955 test: ... here starts the operator precendence dance
1956 */
1957
1958 if (NCH(n) == 1) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001959 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
1960 if (!e)
1961 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001963 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964 }
1965 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001966 expr_ty expr1, expr2;
1967 operator_ty newoperator;
1968 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001970 expr1 = ast_for_testlist(c, ch);
1971 if (!expr1)
1972 return NULL;
1973 /* TODO(nas): Remove duplicated error checks (set_context does it) */
1974 switch (expr1->kind) {
1975 case GeneratorExp_kind:
1976 ast_error(ch, "augmented assignment to generator "
1977 "expression not possible");
1978 return NULL;
1979 case Yield_kind:
1980 ast_error(ch, "augmented assignment to yield "
1981 "expression not possible");
1982 return NULL;
1983 case Name_kind: {
1984 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1985 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1986 ast_error(ch, "assignment to None");
1987 return NULL;
1988 }
1989 break;
1990 }
1991 case Attribute_kind:
1992 case Subscript_kind:
1993 break;
1994 default:
1995 ast_error(ch, "illegal expression for augmented "
1996 "assignment");
1997 return NULL;
1998 }
Collin Winter77c67bd2007-03-16 04:11:30 +00001999 if(!set_context(expr1, Store, ch))
2000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002002 ch = CHILD(n, 2);
2003 if (TYPE(ch) == testlist)
2004 expr2 = ast_for_testlist(c, ch);
2005 else
2006 expr2 = ast_for_expr(c, ch);
2007 if (!expr2)
2008 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002010 newoperator = ast_for_augassign(CHILD(n, 1));
2011 if (!newoperator)
2012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002014 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 }
2016 else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002017 int i;
2018 asdl_seq *targets;
2019 node *value;
2020 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002022 /* a normal assignment */
2023 REQ(CHILD(n, 1), EQUAL);
2024 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2025 if (!targets)
2026 return NULL;
2027 for (i = 0; i < NCH(n) - 2; i += 2) {
2028 expr_ty e;
2029 node *ch = CHILD(n, i);
2030 if (TYPE(ch) == yield_expr) {
2031 ast_error(ch, "assignment to yield expression not possible");
2032 return NULL;
2033 }
2034 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002036 /* set context to assign */
2037 if (!e)
2038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002040 if (!set_context(e, Store, CHILD(n, i)))
2041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002043 asdl_seq_SET(targets, i / 2, e);
2044 }
2045 value = CHILD(n, NCH(n) - 1);
2046 if (TYPE(value) == testlist)
2047 expression = ast_for_testlist(c, value);
2048 else
2049 expression = ast_for_expr(c, value);
2050 if (!expression)
2051 return NULL;
2052 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054}
2055
2056static stmt_ty
2057ast_for_print_stmt(struct compiling *c, const node *n)
2058{
2059 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002060 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 */
2062 expr_ty dest = NULL, expression;
2063 asdl_seq *seq;
2064 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002065 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066
2067 REQ(n, print_stmt);
2068 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002069 dest = ast_for_expr(c, CHILD(n, 2));
2070 if (!dest)
2071 return NULL;
2072 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002074 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 if (!seq)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002076 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002077 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002078 expression = ast_for_expr(c, CHILD(n, i));
2079 if (!expression)
2080 return NULL;
2081 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 }
2083 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002084 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085}
2086
2087static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002088ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089{
2090 asdl_seq *seq;
2091 int i;
2092 expr_ty e;
2093
2094 REQ(n, exprlist);
2095
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002096 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 if (!seq)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002098 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002100 e = ast_for_expr(c, CHILD(n, i));
2101 if (!e)
2102 return NULL;
2103 asdl_seq_SET(seq, i / 2, e);
2104 if (context && !set_context(e, context, CHILD(n, i)))
2105 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 }
2107 return seq;
2108}
2109
2110static stmt_ty
2111ast_for_del_stmt(struct compiling *c, const node *n)
2112{
2113 asdl_seq *expr_list;
2114
2115 /* del_stmt: 'del' exprlist */
2116 REQ(n, del_stmt);
2117
2118 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2119 if (!expr_list)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002120 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002121 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122}
2123
2124static stmt_ty
2125ast_for_flow_stmt(struct compiling *c, const node *n)
2126{
2127 /*
2128 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002129 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 break_stmt: 'break'
2131 continue_stmt: 'continue'
2132 return_stmt: 'return' [testlist]
2133 yield_stmt: yield_expr
2134 yield_expr: 'yield' testlist
2135 raise_stmt: 'raise' [test [',' test [',' test]]]
2136 */
2137 node *ch;
2138
2139 REQ(n, flow_stmt);
2140 ch = CHILD(n, 0);
2141 switch (TYPE(ch)) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002142 case break_stmt:
2143 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2144 case continue_stmt:
2145 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2146 case yield_stmt: { /* will reduce to yield_expr */
2147 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2148 if (!exp)
2149 return NULL;
2150 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2151 }
2152 case return_stmt:
2153 if (NCH(ch) == 1)
2154 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2155 else {
2156 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2157 if (!expression)
2158 return NULL;
2159 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
2160 }
2161 case raise_stmt:
2162 if (NCH(ch) == 1)
2163 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2164 else if (NCH(ch) == 2) {
2165 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2166 if (!expression)
2167 return NULL;
2168 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2169 }
2170 else if (NCH(ch) == 4) {
2171 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002173 expr1 = ast_for_expr(c, CHILD(ch, 1));
2174 if (!expr1)
2175 return NULL;
2176 expr2 = ast_for_expr(c, CHILD(ch, 3));
2177 if (!expr2)
2178 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002180 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2181 }
2182 else if (NCH(ch) == 6) {
2183 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002185 expr1 = ast_for_expr(c, CHILD(ch, 1));
2186 if (!expr1)
2187 return NULL;
2188 expr2 = ast_for_expr(c, CHILD(ch, 3));
2189 if (!expr2)
2190 return NULL;
2191 expr3 = ast_for_expr(c, CHILD(ch, 5));
2192 if (!expr3)
2193 return NULL;
2194
2195 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
2196 }
2197 default:
2198 PyErr_Format(PyExc_SystemError,
2199 "unexpected flow_stmt: %d", TYPE(ch));
2200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002202
2203 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205}
2206
2207static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002208alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209{
2210 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002211 import_as_name: NAME ['as' NAME]
2212 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 dotted_name: NAME ('.' NAME)*
2214 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002215 PyObject *str;
2216
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217 loop:
2218 switch (TYPE(n)) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002219 case import_as_name:
2220 str = NULL;
2221 if (NCH(n) == 3) {
2222 str = NEW_IDENTIFIER(CHILD(n, 2));
2223 }
2224 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2225 case dotted_as_name:
2226 if (NCH(n) == 1) {
2227 n = CHILD(n, 0);
2228 goto loop;
2229 }
2230 else {
2231 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2232 if (!a)
2233 return NULL;
2234 assert(!a->asname);
2235 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2236 return a;
2237 }
2238 break;
2239 case dotted_name:
2240 if (NCH(n) == 1)
2241 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2242 else {
2243 /* Create a string of the form "a.b.c" */
2244 int i;
2245 size_t len;
2246 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002248 len = 0;
2249 for (i = 0; i < NCH(n); i += 2)
2250 /* length of string plus one for the dot */
2251 len += strlen(STR(CHILD(n, i))) + 1;
2252 len--; /* the last name doesn't have a dot */
2253 str = PyString_FromStringAndSize(NULL, len);
2254 if (!str)
2255 return NULL;
2256 s = PyString_AS_STRING(str);
2257 if (!s)
2258 return NULL;
2259 for (i = 0; i < NCH(n); i += 2) {
2260 char *sch = STR(CHILD(n, i));
2261 strcpy(s, STR(CHILD(n, i)));
2262 s += strlen(sch);
2263 *s++ = '.';
2264 }
2265 --s;
2266 *s = '\0';
2267 PyString_InternInPlace(&str);
2268 PyArena_AddPyObject(c->c_arena, str);
2269 return alias(str, NULL, c->c_arena);
2270 }
2271 break;
2272 case STAR:
2273 str = PyString_InternFromString("*");
2274 PyArena_AddPyObject(c->c_arena, str);
2275 return alias(str, NULL, c->c_arena);
2276 default:
2277 PyErr_Format(PyExc_SystemError,
2278 "unexpected import name: %d", TYPE(n));
2279 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002281
2282 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 return NULL;
2284}
2285
2286static stmt_ty
2287ast_for_import_stmt(struct compiling *c, const node *n)
2288{
2289 /*
2290 import_stmt: import_name | import_from
2291 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002292 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002293 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002295 int lineno;
2296 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 int i;
2298 asdl_seq *aliases;
2299
2300 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002301 lineno = LINENO(n);
2302 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002304 if (TYPE(n) == import_name) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002305 n = CHILD(n, 1);
2306 REQ(n, dotted_as_names);
2307 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2308 if (!aliases)
2309 return NULL;
2310 for (i = 0; i < NCH(n); i += 2) {
2311 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2312 if (!import_alias)
2313 return NULL;
2314 asdl_seq_SET(aliases, i / 2, import_alias);
2315 }
2316 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002318 else if (TYPE(n) == import_from) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002319 int n_children;
2320 int idx, ndots = 0;
2321 alias_ty mod = NULL;
2322 identifier modname;
2323
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002324 /* Count the number of dots (for relative imports) and check for the
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002325 optional module name */
2326 for (idx = 1; idx < NCH(n); idx++) {
2327 if (TYPE(CHILD(n, idx)) == dotted_name) {
2328 mod = alias_for_import_name(c, CHILD(n, idx));
2329 idx++;
2330 break;
2331 } else if (TYPE(CHILD(n, idx)) != DOT) {
2332 break;
2333 }
2334 ndots++;
2335 }
2336 idx++; /* skip over the 'import' keyword */
2337 switch (TYPE(CHILD(n, idx))) {
2338 case STAR:
2339 /* from ... import * */
2340 n = CHILD(n, idx);
2341 n_children = 1;
2342 if (ndots) {
2343 ast_error(n, "'import *' not allowed with 'from .'");
2344 return NULL;
2345 }
2346 break;
2347 case LPAR:
2348 /* from ... import (x, y, z) */
2349 n = CHILD(n, idx + 1);
2350 n_children = NCH(n);
2351 break;
2352 case import_as_names:
2353 /* from ... import x, y, z */
2354 n = CHILD(n, idx);
2355 n_children = NCH(n);
2356 if (n_children % 2 == 0) {
2357 ast_error(n, "trailing comma not allowed without"
2358 " surrounding parentheses");
2359 return NULL;
2360 }
2361 break;
2362 default:
2363 ast_error(n, "Unexpected node-type in from-import");
2364 return NULL;
2365 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002367 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2368 if (!aliases)
2369 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002371 /* handle "from ... import *" special b/c there's no children */
2372 if (TYPE(n) == STAR) {
2373 alias_ty import_alias = alias_for_import_name(c, n);
2374 if (!import_alias)
2375 return NULL;
2376 asdl_seq_SET(aliases, 0, import_alias);
2377 }
2378 else {
2379 for (i = 0; i < NCH(n); i += 2) {
2380 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2381 if (!import_alias)
2382 return NULL;
2383 asdl_seq_SET(aliases, i / 2, import_alias);
2384 }
2385 }
2386 if (mod != NULL)
2387 modname = mod->name;
2388 else
2389 modname = new_identifier("", c->c_arena);
2390 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2391 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 }
Neal Norwitz79792652005-11-14 04:25:03 +00002393 PyErr_Format(PyExc_SystemError,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002394 "unknown import statement: starts with command '%s'",
2395 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 return NULL;
2397}
2398
2399static stmt_ty
2400ast_for_global_stmt(struct compiling *c, const node *n)
2401{
2402 /* global_stmt: 'global' NAME (',' NAME)* */
2403 identifier name;
2404 asdl_seq *s;
2405 int i;
2406
2407 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002408 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409 if (!s)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002410 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002412 name = NEW_IDENTIFIER(CHILD(n, i));
2413 if (!name)
2414 return NULL;
2415 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002417 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418}
2419
2420static stmt_ty
2421ast_for_exec_stmt(struct compiling *c, const node *n)
2422{
2423 expr_ty expr1, globals = NULL, locals = NULL;
2424 int n_children = NCH(n);
2425 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002426 PyErr_Format(PyExc_SystemError,
2427 "poorly formed 'exec' statement: %d parts to statement",
2428 n_children);
2429 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 }
2431
2432 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2433 REQ(n, exec_stmt);
2434 expr1 = ast_for_expr(c, CHILD(n, 1));
2435 if (!expr1)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002436 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 if (n_children >= 4) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002438 globals = ast_for_expr(c, CHILD(n, 3));
2439 if (!globals)
2440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441 }
2442 if (n_children == 6) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002443 locals = ast_for_expr(c, CHILD(n, 5));
2444 if (!locals)
2445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 }
2447
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002448 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449}
2450
2451static stmt_ty
2452ast_for_assert_stmt(struct compiling *c, const node *n)
2453{
2454 /* assert_stmt: 'assert' test [',' test] */
2455 REQ(n, assert_stmt);
2456 if (NCH(n) == 2) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002457 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2458 if (!expression)
2459 return NULL;
2460 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 }
2462 else if (NCH(n) == 4) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002463 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002465 expr1 = ast_for_expr(c, CHILD(n, 1));
2466 if (!expr1)
2467 return NULL;
2468 expr2 = ast_for_expr(c, CHILD(n, 3));
2469 if (!expr2)
2470 return NULL;
2471
2472 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 }
Neal Norwitz79792652005-11-14 04:25:03 +00002474 PyErr_Format(PyExc_SystemError,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002475 "improper number of parts to 'assert' statement: %d",
2476 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 return NULL;
2478}
2479
2480static asdl_seq *
2481ast_for_suite(struct compiling *c, const node *n)
2482{
2483 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002484 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 stmt_ty s;
2486 int i, total, num, end, pos = 0;
2487 node *ch;
2488
2489 REQ(n, suite);
2490
2491 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002492 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 if (!seq)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002494 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002496 n = CHILD(n, 0);
2497 /* simple_stmt always ends with a NEWLINE,
2498 and may have a trailing SEMI
2499 */
2500 end = NCH(n) - 1;
2501 if (TYPE(CHILD(n, end - 1)) == SEMI)
2502 end--;
2503 /* loop by 2 to skip semi-colons */
2504 for (i = 0; i < end; i += 2) {
2505 ch = CHILD(n, i);
2506 s = ast_for_stmt(c, ch);
2507 if (!s)
2508 return NULL;
2509 asdl_seq_SET(seq, pos++, s);
2510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 }
2512 else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002513 for (i = 2; i < (NCH(n) - 1); i++) {
2514 ch = CHILD(n, i);
2515 REQ(ch, stmt);
2516 num = num_stmts(ch);
2517 if (num == 1) {
2518 /* small_stmt or compound_stmt with only one child */
2519 s = ast_for_stmt(c, ch);
2520 if (!s)
2521 return NULL;
2522 asdl_seq_SET(seq, pos++, s);
2523 }
2524 else {
2525 int j;
2526 ch = CHILD(ch, 0);
2527 REQ(ch, simple_stmt);
2528 for (j = 0; j < NCH(ch); j += 2) {
2529 /* statement terminates with a semi-colon ';' */
2530 if (NCH(CHILD(ch, j)) == 0) {
2531 assert((j + 1) == NCH(ch));
2532 break;
2533 }
2534 s = ast_for_stmt(c, CHILD(ch, j));
2535 if (!s)
2536 return NULL;
2537 asdl_seq_SET(seq, pos++, s);
2538 }
2539 }
2540 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 }
2542 assert(pos == seq->size);
2543 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544}
2545
2546static stmt_ty
2547ast_for_if_stmt(struct compiling *c, const node *n)
2548{
2549 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2550 ['else' ':' suite]
2551 */
2552 char *s;
2553
2554 REQ(n, if_stmt);
2555
2556 if (NCH(n) == 4) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002557 expr_ty expression;
2558 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002560 expression = ast_for_expr(c, CHILD(n, 1));
2561 if (!expression)
2562 return NULL;
2563 suite_seq = ast_for_suite(c, CHILD(n, 3));
2564 if (!suite_seq)
2565 return NULL;
2566
2567 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002569
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 s = STR(CHILD(n, 4));
2571 /* s[2], the third character in the string, will be
2572 's' for el_s_e, or
2573 'i' for el_i_f
2574 */
2575 if (s[2] == 's') {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002576 expr_ty expression;
2577 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002579 expression = ast_for_expr(c, CHILD(n, 1));
2580 if (!expression)
2581 return NULL;
2582 seq1 = ast_for_suite(c, CHILD(n, 3));
2583 if (!seq1)
2584 return NULL;
2585 seq2 = ast_for_suite(c, CHILD(n, 6));
2586 if (!seq2)
2587 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002589 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 }
2591 else if (s[2] == 'i') {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002592 int i, n_elif, has_else = 0;
Collin Winter77c67bd2007-03-16 04:11:30 +00002593 expr_ty expression;
2594 asdl_seq *suite_seq;
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002595 asdl_seq *orelse = NULL;
2596 n_elif = NCH(n) - 4;
2597 /* must reference the child n_elif+1 since 'else' token is third,
2598 not fourth, child from the end. */
2599 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2600 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2601 has_else = 1;
2602 n_elif -= 3;
2603 }
2604 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002606 if (has_else) {
Collin Winter77c67bd2007-03-16 04:11:30 +00002607 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002609 orelse = asdl_seq_new(1, c->c_arena);
2610 if (!orelse)
2611 return NULL;
2612 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2613 if (!expression)
2614 return NULL;
Collin Winter77c67bd2007-03-16 04:11:30 +00002615 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2616 if (!suite_seq)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002617 return NULL;
Collin Winter77c67bd2007-03-16 04:11:30 +00002618 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2619 if (!suite_seq2)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002620 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621
Collin Winter77c67bd2007-03-16 04:11:30 +00002622 asdl_seq_SET(orelse, 0, If(expression, suite_seq, suite_seq2,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002623 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
2624 c->c_arena));
2625 /* the just-created orelse handled the last elif */
2626 n_elif--;
2627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002629 for (i = 0; i < n_elif; i++) {
2630 int off = 5 + (n_elif - i - 1) * 4;
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002631 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2632 if (!newobj)
2633 return NULL;
2634 expression = ast_for_expr(c, CHILD(n, off));
2635 if (!expression)
2636 return NULL;
2637 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2638 if (!suite_seq)
2639 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002641 asdl_seq_SET(newobj, 0,
2642 If(expression, suite_seq, orelse,
2643 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
2644 orelse = newobj;
2645 }
Collin Winter77c67bd2007-03-16 04:11:30 +00002646 expression = ast_for_expr(c, CHILD(n, 1));
2647 if (!expression)
2648 return NULL;
2649 suite_seq = ast_for_suite(c, CHILD(n, 3));
2650 if (!suite_seq)
2651 return NULL;
2652 return If(expression, suite_seq, orelse,
2653 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002655
2656 PyErr_Format(PyExc_SystemError,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002657 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002658 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659}
2660
2661static stmt_ty
2662ast_for_while_stmt(struct compiling *c, const node *n)
2663{
2664 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2665 REQ(n, while_stmt);
2666
2667 if (NCH(n) == 4) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002668 expr_ty expression;
2669 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002671 expression = ast_for_expr(c, CHILD(n, 1));
2672 if (!expression)
2673 return NULL;
2674 suite_seq = ast_for_suite(c, CHILD(n, 3));
2675 if (!suite_seq)
2676 return NULL;
2677 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678 }
2679 else if (NCH(n) == 7) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002680 expr_ty expression;
2681 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002683 expression = ast_for_expr(c, CHILD(n, 1));
2684 if (!expression)
2685 return NULL;
2686 seq1 = ast_for_suite(c, CHILD(n, 3));
2687 if (!seq1)
2688 return NULL;
2689 seq2 = ast_for_suite(c, CHILD(n, 6));
2690 if (!seq2)
2691 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002693 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002695
2696 PyErr_Format(PyExc_SystemError,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002697 "wrong number of tokens for 'while' statement: %d",
2698 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002699 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700}
2701
2702static stmt_ty
2703ast_for_for_stmt(struct compiling *c, const node *n)
2704{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002705 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 expr_ty expression;
2707 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002708 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2710 REQ(n, for_stmt);
2711
2712 if (NCH(n) == 9) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002713 seq = ast_for_suite(c, CHILD(n, 8));
2714 if (!seq)
2715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 }
2717
Neal Norwitzedef2be2006-07-12 05:26:17 +00002718 node_target = CHILD(n, 1);
2719 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002720 if (!_target)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002721 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002722 /* Check the # of children rather than the length of _target, since
2723 for x, in ... has 1 element in _target, but still requires a Tuple. */
2724 if (NCH(node_target) == 1)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002725 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 else
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002727 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002729 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002730 if (!expression)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002731 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002733 if (!suite_seq)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002736 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002737 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738}
2739
2740static excepthandler_ty
2741ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2742{
2743 /* except_clause: 'except' [test [',' test]] */
2744 REQ(exc, except_clause);
2745 REQ(body, suite);
2746
2747 if (NCH(exc) == 1) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002748 asdl_seq *suite_seq = ast_for_suite(c, body);
2749 if (!suite_seq)
2750 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002752 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2753 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 }
2755 else if (NCH(exc) == 2) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002756 expr_ty expression;
2757 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002759 expression = ast_for_expr(c, CHILD(exc, 1));
2760 if (!expression)
2761 return NULL;
2762 suite_seq = ast_for_suite(c, body);
2763 if (!suite_seq)
2764 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002766 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2767 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 }
2769 else if (NCH(exc) == 4) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002770 asdl_seq *suite_seq;
2771 expr_ty expression;
2772 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2773 if (!e)
2774 return NULL;
2775 if (!set_context(e, Store, CHILD(exc, 3)))
2776 return NULL;
2777 expression = ast_for_expr(c, CHILD(exc, 1));
2778 if (!expression)
2779 return NULL;
2780 suite_seq = ast_for_suite(c, body);
2781 if (!suite_seq)
2782 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002784 return excepthandler(expression, e, suite_seq, LINENO(exc),
2785 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002787
2788 PyErr_Format(PyExc_SystemError,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002789 "wrong number of children for 'except' clause: %d",
2790 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002791 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792}
2793
2794static stmt_ty
2795ast_for_try_stmt(struct compiling *c, const node *n)
2796{
Neal Norwitzf599f422005-12-17 21:33:47 +00002797 const int nch = NCH(n);
2798 int n_except = (nch - 3)/3;
2799 asdl_seq *body, *orelse = NULL, *finally = NULL;
2800
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801 REQ(n, try_stmt);
2802
Neal Norwitzf599f422005-12-17 21:33:47 +00002803 body = ast_for_suite(c, CHILD(n, 2));
2804 if (body == NULL)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002805 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806
Neal Norwitzf599f422005-12-17 21:33:47 +00002807 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002808 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2809 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2810 /* we can assume it's an "else",
2811 because nch >= 9 for try-else-finally and
2812 it would otherwise have a type of except_clause */
2813 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2814 if (orelse == NULL)
2815 return NULL;
2816 n_except--;
2817 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002819 finally = ast_for_suite(c, CHILD(n, nch - 1));
2820 if (finally == NULL)
2821 return NULL;
2822 n_except--;
2823 }
2824 else {
2825 /* we can assume it's an "else",
2826 otherwise it would have a type of except_clause */
2827 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2828 if (orelse == NULL)
2829 return NULL;
2830 n_except--;
2831 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002833 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002834 ast_error(n, "malformed 'try' statement");
2835 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002837
2838 if (n_except > 0) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002839 int i;
2840 stmt_ty except_st;
2841 /* process except statements to create a try ... except */
2842 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2843 if (handlers == NULL)
2844 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002845
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002846 for (i = 0; i < n_except; i++) {
2847 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2848 CHILD(n, 5 + i * 3));
2849 if (!e)
2850 return NULL;
2851 asdl_seq_SET(handlers, i, e);
2852 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002853
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002854 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2855 n->n_col_offset, c->c_arena);
2856 if (!finally)
2857 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002858
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002859 /* if a 'finally' is present too, we nest the TryExcept within a
2860 TryFinally to emulate try ... except ... finally */
2861 body = asdl_seq_new(1, c->c_arena);
2862 if (body == NULL)
2863 return NULL;
2864 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002865 }
2866
2867 /* must be a try ... finally (except clauses are in body, if any exist) */
2868 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002869 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870}
2871
Guido van Rossumc2e20742006-02-27 22:32:47 +00002872static expr_ty
2873ast_for_with_var(struct compiling *c, const node *n)
2874{
2875 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002876 return ast_for_expr(c, CHILD(n, 1));
2877}
2878
2879/* with_stmt: 'with' test [ with_var ] ':' suite */
2880static stmt_ty
2881ast_for_with_stmt(struct compiling *c, const node *n)
2882{
2883 expr_ty context_expr, optional_vars = NULL;
2884 int suite_index = 3; /* skip 'with', test, and ':' */
2885 asdl_seq *suite_seq;
2886
2887 assert(TYPE(n) == with_stmt);
2888 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002889 if (!context_expr)
2890 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002891 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002892 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002893
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002894 if (!optional_vars) {
2895 return NULL;
2896 }
2897 if (!set_context(optional_vars, Store, n)) {
2898 return NULL;
2899 }
2900 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002901 }
2902
2903 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2904 if (!suite_seq) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002905 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002906 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002907 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002908 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002909}
2910
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911static stmt_ty
2912ast_for_classdef(struct compiling *c, const node *n)
2913{
2914 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 asdl_seq *bases, *s;
2916
2917 REQ(n, classdef);
2918
2919 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002920 ast_error(n, "assignment to None");
2921 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922 }
2923
2924 if (NCH(n) == 4) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002925 s = ast_for_suite(c, CHILD(n, 3));
2926 if (!s)
2927 return NULL;
2928 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2929 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930 }
2931 /* check for empty base list */
2932 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002933 s = ast_for_suite(c, CHILD(n,5));
2934 if (!s)
2935 return NULL;
2936 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2937 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 }
2939
2940 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002941 bases = ast_for_class_bases(c, CHILD(n, 3));
2942 if (!bases)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002943 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944
2945 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002946 if (!s)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002947 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002948 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002949 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950}
2951
2952static stmt_ty
2953ast_for_stmt(struct compiling *c, const node *n)
2954{
2955 if (TYPE(n) == stmt) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002956 assert(NCH(n) == 1);
2957 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 }
2959 if (TYPE(n) == simple_stmt) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002960 assert(num_stmts(n) == 1);
2961 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 }
2963 if (TYPE(n) == small_stmt) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002964 REQ(n, small_stmt);
2965 n = CHILD(n, 0);
2966 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2967 | flow_stmt | import_stmt | global_stmt | exec_stmt
2968 | assert_stmt
2969 */
2970 switch (TYPE(n)) {
2971 case expr_stmt:
2972 return ast_for_expr_stmt(c, n);
2973 case print_stmt:
2974 return ast_for_print_stmt(c, n);
2975 case del_stmt:
2976 return ast_for_del_stmt(c, n);
2977 case pass_stmt:
2978 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
2979 case flow_stmt:
2980 return ast_for_flow_stmt(c, n);
2981 case import_stmt:
2982 return ast_for_import_stmt(c, n);
2983 case global_stmt:
2984 return ast_for_global_stmt(c, n);
2985 case exec_stmt:
2986 return ast_for_exec_stmt(c, n);
2987 case assert_stmt:
2988 return ast_for_assert_stmt(c, n);
2989 default:
2990 PyErr_Format(PyExc_SystemError,
2991 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2992 TYPE(n), NCH(n));
2993 return NULL;
2994 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 }
2996 else {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002997 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2998 | funcdef | classdef
2999 */
3000 node *ch = CHILD(n, 0);
3001 REQ(n, compound_stmt);
3002 switch (TYPE(ch)) {
3003 case if_stmt:
3004 return ast_for_if_stmt(c, ch);
3005 case while_stmt:
3006 return ast_for_while_stmt(c, ch);
3007 case for_stmt:
3008 return ast_for_for_stmt(c, ch);
3009 case try_stmt:
3010 return ast_for_try_stmt(c, ch);
3011 case with_stmt:
3012 return ast_for_with_stmt(c, ch);
3013 case funcdef:
3014 return ast_for_funcdef(c, ch);
3015 case classdef:
3016 return ast_for_classdef(c, ch);
3017 default:
3018 PyErr_Format(PyExc_SystemError,
3019 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3020 TYPE(n), NCH(n));
3021 return NULL;
3022 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 }
3024}
3025
3026static PyObject *
3027parsenumber(const char *s)
3028{
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003029 const char *end;
3030 long x;
3031 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032#ifndef WITHOUT_COMPLEX
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003033 Py_complex c;
3034 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035#endif
3036
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003037 errno = 0;
3038 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039#ifndef WITHOUT_COMPLEX
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003040 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041#endif
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003042 if (*end == 'l' || *end == 'L')
3043 return PyLong_FromString((char *)s, (char **)0, 0);
3044 if (s[0] == '0') {
3045 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3046 if (x < 0 && errno == 0) {
3047 return PyLong_FromString((char *)s,
3048 (char **)0,
3049 0);
3050 }
3051 }
3052 else
3053 x = PyOS_strtol((char *)s, (char **)&end, 0);
3054 if (*end == '\0') {
3055 if (errno != 0)
3056 return PyLong_FromString((char *)s, (char **)0, 0);
3057 return PyInt_FromLong(x);
3058 }
3059 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060#ifndef WITHOUT_COMPLEX
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003061 if (imflag) {
3062 c.real = 0.;
3063 PyFPE_START_PROTECT("atof", return 0)
3064 c.imag = PyOS_ascii_atof(s);
3065 PyFPE_END_PROTECT(c)
3066 return PyComplex_FromCComplex(c);
3067 }
3068 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003069#endif
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003070 {
3071 PyFPE_START_PROTECT("atof", return 0)
3072 dx = PyOS_ascii_atof(s);
3073 PyFPE_END_PROTECT(dx)
3074 return PyFloat_FromDouble(dx);
3075 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076}
3077
3078static PyObject *
3079decode_utf8(const char **sPtr, const char *end, char* encoding)
3080{
3081#ifndef Py_USING_UNICODE
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003082 Py_FatalError("decode_utf8 should not be called in this build.");
3083 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084#else
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003085 PyObject *u, *v;
3086 char *s, *t;
3087 t = s = (char *)*sPtr;
3088 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3089 while (s < end && (*s & 0x80)) s++;
3090 *sPtr = s;
3091 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3092 if (u == NULL)
3093 return NULL;
3094 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3095 Py_DECREF(u);
3096 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097#endif
3098}
3099
3100static PyObject *
3101decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3102{
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003103 PyObject *v, *u;
3104 char *buf;
3105 char *p;
3106 const char *end;
3107 if (encoding == NULL) {
3108 buf = (char *)s;
3109 u = NULL;
3110 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3111 buf = (char *)s;
3112 u = NULL;
3113 } else {
3114 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3115 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3116 if (u == NULL)
3117 return NULL;
3118 p = buf = PyString_AsString(u);
3119 end = s + len;
3120 while (s < end) {
3121 if (*s == '\\') {
3122 *p++ = *s++;
3123 if (*s & 0x80) {
3124 strcpy(p, "u005c");
3125 p += 5;
3126 }
3127 }
3128 if (*s & 0x80) { /* XXX inefficient */
3129 PyObject *w;
3130 char *r;
3131 Py_ssize_t rn, i;
3132 w = decode_utf8(&s, end, "utf-16-be");
3133 if (w == NULL) {
3134 Py_DECREF(u);
3135 return NULL;
3136 }
3137 r = PyString_AsString(w);
3138 rn = PyString_Size(w);
3139 assert(rn % 2 == 0);
3140 for (i = 0; i < rn; i += 2) {
3141 sprintf(p, "\\u%02x%02x",
3142 r[i + 0] & 0xFF,
3143 r[i + 1] & 0xFF);
3144 p += 6;
3145 }
3146 Py_DECREF(w);
3147 } else {
3148 *p++ = *s++;
3149 }
3150 }
3151 len = p - buf;
3152 s = buf;
3153 }
3154 if (rawmode)
3155 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3156 else
3157 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3158 Py_XDECREF(u);
3159 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160}
3161
3162/* s is a Python string literal, including the bracketing quote characters,
3163 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3164 * parsestr parses it, and returns the decoded Python string object.
3165 */
3166static PyObject *
3167parsestr(const char *s, const char *encoding)
3168{
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003169 size_t len;
3170 int quote = Py_CHARMASK(*s);
3171 int rawmode = 0;
3172 int need_encoding;
3173 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003175 if (isalpha(quote) || quote == '_') {
3176 if (quote == 'u' || quote == 'U') {
3177 quote = *++s;
3178 unicode = 1;
3179 }
3180 if (quote == 'r' || quote == 'R') {
3181 quote = *++s;
3182 rawmode = 1;
3183 }
3184 }
3185 if (quote != '\'' && quote != '\"') {
3186 PyErr_BadInternalCall();
3187 return NULL;
3188 }
3189 s++;
3190 len = strlen(s);
3191 if (len > INT_MAX) {
3192 PyErr_SetString(PyExc_OverflowError,
3193 "string to parse is too long");
3194 return NULL;
3195 }
3196 if (s[--len] != quote) {
3197 PyErr_BadInternalCall();
3198 return NULL;
3199 }
3200 if (len >= 4 && s[0] == quote && s[1] == quote) {
3201 s += 2;
3202 len -= 2;
3203 if (s[--len] != quote || s[--len] != quote) {
3204 PyErr_BadInternalCall();
3205 return NULL;
3206 }
3207 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208#ifdef Py_USING_UNICODE
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003209 if (unicode || Py_UnicodeFlag) {
3210 return decode_unicode(s, len, rawmode, encoding);
3211 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212#endif
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003213 need_encoding = (encoding != NULL &&
3214 strcmp(encoding, "utf-8") != 0 &&
3215 strcmp(encoding, "iso-8859-1") != 0);
3216 if (rawmode || strchr(s, '\\') == NULL) {
3217 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218#ifndef Py_USING_UNICODE
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003219 /* This should not happen - we never see any other
3220 encoding. */
3221 Py_FatalError(
3222 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223#else
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003224 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3225 if (u == NULL)
3226 return NULL;
3227 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3228 Py_DECREF(u);
3229 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230#endif
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003231 } else {
3232 return PyString_FromStringAndSize(s, len);
3233 }
3234 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003236 return PyString_DecodeEscape(s, len, NULL, unicode,
3237 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238}
3239
3240/* Build a Python string object out of a STRING atom. This takes care of
3241 * compile-time literal catenation, calling parsestr() on each piece, and
3242 * pasting the intermediate results together.
3243 */
3244static PyObject *
3245parsestrplus(struct compiling *c, const node *n)
3246{
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003247 PyObject *v;
3248 int i;
3249 REQ(CHILD(n, 0), STRING);
3250 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3251 /* String literal concatenation */
3252 for (i = 1; i < NCH(n); i++) {
3253 PyObject *s;
3254 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3255 if (s == NULL)
3256 goto onError;
3257 if (PyString_Check(v) && PyString_Check(s)) {
3258 PyString_ConcatAndDel(&v, s);
3259 if (v == NULL)
3260 goto onError;
3261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262#ifdef Py_USING_UNICODE
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003263 else {
3264 PyObject *temp = PyUnicode_Concat(v, s);
3265 Py_DECREF(s);
3266 Py_DECREF(v);
3267 v = temp;
3268 if (v == NULL)
3269 goto onError;
3270 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271#endif
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003272 }
3273 }
3274 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275
3276 onError:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003277 Py_XDECREF(v);
3278 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279}