blob: 3c339f000ee0fd669a1ced0fd892d0461962a408 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* XXX TO DO
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
21 - syntax errors
22*/
23
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024/* Data structure used internally */
25struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000026 char *c_encoding; /* source encoding */
27 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028};
29
30static asdl_seq *seq_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_expr(struct compiling *, const node *);
32static stmt_ty ast_for_stmt(struct compiling *, const node *);
33static asdl_seq *ast_for_suite(struct compiling *, const node *);
34static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 value = Py_BuildValue("(OO)", errstr, tmp);
111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194 } else if (TYPE(n) == encoding_decl) {
195 c.c_encoding = STR(n);
196 n = CHILD(n, 0);
197 } else {
198 c.c_encoding = NULL;
199 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000200 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
Jeremy Hyltona8293132006-02-28 17:58:27 +0000202 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203 switch (TYPE(n)) {
204 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000205 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206 if (!stmts)
207 return NULL;
208 for (i = 0; i < NCH(n) - 1; i++) {
209 ch = CHILD(n, i);
210 if (TYPE(ch) == NEWLINE)
211 continue;
212 REQ(ch, stmt);
213 num = num_stmts(ch);
214 if (num == 1) {
215 s = ast_for_stmt(&c, ch);
216 if (!s)
217 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000218 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219 }
220 else {
221 ch = CHILD(ch, 0);
222 REQ(ch, simple_stmt);
223 for (j = 0; j < num; j++) {
224 s = ast_for_stmt(&c, CHILD(ch, j * 2));
225 if (!s)
226 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000227 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 }
229 }
230 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000231 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 case eval_input: {
233 expr_ty testlist_ast;
234
235 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000236 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 if (!testlist_ast)
238 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000239 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 }
241 case single_input:
242 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 if (!stmts)
245 goto error;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000246 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 }
249 else {
250 n = CHILD(n, 0);
251 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 if (!stmts)
254 goto error;
255 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000256 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 if (!s)
258 goto error;
259 asdl_seq_SET(stmts, 0, s);
260 }
261 else {
262 /* Only a simple_stmt can contain multiple statements. */
263 REQ(n, simple_stmt);
264 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 if (TYPE(CHILD(n, i)) == NEWLINE)
266 break;
267 s = ast_for_stmt(&c, CHILD(n, i));
268 if (!s)
269 goto error;
270 asdl_seq_SET(stmts, i / 2, s);
271 }
272 }
273
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000274 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 }
276 default:
277 goto error;
278 }
279 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 ast_error_finish(filename);
281 return NULL;
282}
283
284/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
285*/
286
287static operator_ty
288get_operator(const node *n)
289{
290 switch (TYPE(n)) {
291 case VBAR:
292 return BitOr;
293 case CIRCUMFLEX:
294 return BitXor;
295 case AMPER:
296 return BitAnd;
297 case LEFTSHIFT:
298 return LShift;
299 case RIGHTSHIFT:
300 return RShift;
301 case PLUS:
302 return Add;
303 case MINUS:
304 return Sub;
305 case STAR:
306 return Mult;
307 case SLASH:
308 return Div;
309 case DOUBLESLASH:
310 return FloorDiv;
311 case PERCENT:
312 return Mod;
313 default:
314 return 0;
315 }
316}
317
Jeremy Hyltona8293132006-02-28 17:58:27 +0000318/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319
320 Only sets context for expr kinds that "can appear in assignment context"
321 (according to ../Parser/Python.asdl). For other expr kinds, it sets
322 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323*/
324
325static int
326set_context(expr_ty e, expr_context_ty ctx, const node *n)
327{
328 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000329 /* If a particular expression type can't be used for assign / delete,
330 set expr_name to its name and an error message will be generated.
331 */
332 const char* expr_name = NULL;
333
334 /* The ast defines augmented store and load contexts, but the
335 implementation here doesn't actually use them. The code may be
336 a little more complex than necessary as a result. It also means
337 that expressions in an augmented assignment have no context.
338 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000339 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000340 */
341 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342
343 switch (e->kind) {
344 case Attribute_kind:
345 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000346 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347 return ast_error(n, "assignment to None");
348 }
349 e->v.Attribute.ctx = ctx;
350 break;
351 case Subscript_kind:
352 e->v.Subscript.ctx = ctx;
353 break;
354 case Name_kind:
355 if (ctx == Store &&
356 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
357 return ast_error(n, "assignment to None");
358 }
359 e->v.Name.ctx = ctx;
360 break;
361 case List_kind:
362 e->v.List.ctx = ctx;
363 s = e->v.List.elts;
364 break;
365 case Tuple_kind:
366 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
367 return ast_error(n, "can't assign to ()");
368 e->v.Tuple.ctx = ctx;
369 s = e->v.Tuple.elts;
370 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000371 case Lambda_kind:
372 expr_name = "lambda";
373 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000375 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000377 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 case UnaryOp_kind:
380 expr_name = "operator";
381 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000383 expr_name = "generator expression";
384 break;
385 case ListComp_kind:
386 expr_name = "list comprehension";
387 break;
388 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389 case Num_kind:
390 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000391 expr_name = "literal";
392 break;
393 case Compare_kind:
394 expr_name = "comparison";
395 break;
396 case Repr_kind:
397 expr_name = "repr";
398 break;
399 default:
400 PyErr_Format(PyExc_SystemError,
401 "unexpected expression in assignment %d (line %d)",
402 e->kind, e->lineno);
403 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000405 /* Check for error string set by switch */
406 if (expr_name) {
407 char buf[300];
408 PyOS_snprintf(buf, sizeof(buf),
409 "can't %s %s",
410 ctx == Store ? "assign to" : "delete",
411 expr_name);
412 return ast_error(n, buf);
413 }
414
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000416 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417 */
418 if (s) {
419 int i;
420
421 for (i = 0; i < asdl_seq_LEN(s); i++) {
422 if (!set_context(asdl_seq_GET(s, i), ctx, n))
423 return 0;
424 }
425 }
426 return 1;
427}
428
429static operator_ty
430ast_for_augassign(const node *n)
431{
432 REQ(n, augassign);
433 n = CHILD(n, 0);
434 switch (STR(n)[0]) {
435 case '+':
436 return Add;
437 case '-':
438 return Sub;
439 case '/':
440 if (STR(n)[1] == '/')
441 return FloorDiv;
442 else
443 return Div;
444 case '%':
445 return Mod;
446 case '<':
447 return LShift;
448 case '>':
449 return RShift;
450 case '&':
451 return BitAnd;
452 case '^':
453 return BitXor;
454 case '|':
455 return BitOr;
456 case '*':
457 if (STR(n)[1] == '*')
458 return Pow;
459 else
460 return Mult;
461 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000462 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 return 0;
464 }
465}
466
467static cmpop_ty
468ast_for_comp_op(const node *n)
469{
470 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
471 |'is' 'not'
472 */
473 REQ(n, comp_op);
474 if (NCH(n) == 1) {
475 n = CHILD(n, 0);
476 switch (TYPE(n)) {
477 case LESS:
478 return Lt;
479 case GREATER:
480 return Gt;
481 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 return Eq;
483 case LESSEQUAL:
484 return LtE;
485 case GREATEREQUAL:
486 return GtE;
487 case NOTEQUAL:
488 return NotEq;
489 case NAME:
490 if (strcmp(STR(n), "in") == 0)
491 return In;
492 if (strcmp(STR(n), "is") == 0)
493 return Is;
494 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000495 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 STR(n));
497 return 0;
498 }
499 }
500 else if (NCH(n) == 2) {
501 /* handle "not in" and "is not" */
502 switch (TYPE(CHILD(n, 0))) {
503 case NAME:
504 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
505 return NotIn;
506 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
507 return IsNot;
508 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000509 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
511 return 0;
512 }
513 }
Neal Norwitz79792652005-11-14 04:25:03 +0000514 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 NCH(n));
516 return 0;
517}
518
519static asdl_seq *
520seq_for_testlist(struct compiling *c, const node *n)
521{
522 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000523 asdl_seq *seq;
524 expr_ty expression;
525 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 assert(TYPE(n) == testlist
527 || TYPE(n) == listmaker
528 || TYPE(n) == testlist_gexp
529 || TYPE(n) == testlist_safe
530 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000532 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533 if (!seq)
534 return NULL;
535
536 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000537 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
539 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000540 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
543 assert(i / 2 < seq->size);
544 asdl_seq_SET(seq, i / 2, expression);
545 }
546 return seq;
547}
548
549static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000550compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551{
552 int i, len = (NCH(n) + 1) / 2;
553 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000554 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 if (!args)
556 return NULL;
557
558 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559 for (i = 0; i < len; i++) {
560 const node *child = CHILD(CHILD(n, 2*i), 0);
561 expr_ty arg;
562 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000563 if (!strcmp(STR(child), "None")) {
564 ast_error(child, "assignment to None");
565 return NULL;
566 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000567 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child), child->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000568 c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000569 }
570 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000571 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000572 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 asdl_seq_SET(args, i, arg);
574 }
575
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000576 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000577 if (!set_context(result, Store, n))
578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579 return result;
580}
581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582
Jeremy Hyltona8293132006-02-28 17:58:27 +0000583/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
585static arguments_ty
586ast_for_arguments(struct compiling *c, const node *n)
587{
588 /* parameters: '(' [varargslist] ')'
589 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
590 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
591 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000592 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 asdl_seq *args, *defaults;
594 identifier vararg = NULL, kwarg = NULL;
595 node *ch;
596
597 if (TYPE(n) == parameters) {
598 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000599 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 n = CHILD(n, 1);
601 }
602 REQ(n, varargslist);
603
604 /* first count the number of normal args & defaults */
605 for (i = 0; i < NCH(n); i++) {
606 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000607 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 if (TYPE(ch) == EQUAL)
610 n_defaults++;
611 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000612 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 if (!args && n_args)
614 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000615 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 if (!defaults && n_defaults)
617 goto error;
618
619 /* fpdef: NAME | '(' fplist ')'
620 fplist: fpdef (',' fpdef)* [',']
621 */
622 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000623 j = 0; /* index for defaults */
624 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 while (i < NCH(n)) {
626 ch = CHILD(n, i);
627 switch (TYPE(ch)) {
628 case fpdef:
629 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
630 anything other than EQUAL or a comma? */
631 /* XXX Should NCH(n) check be made a separate check? */
632 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000633 asdl_seq_SET(defaults, j++,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 ast_for_expr(c, CHILD(n, i + 2)));
635 i += 2;
636 found_default = 1;
637 }
638 else if (found_default) {
639 ast_error(n,
640 "non-default argument follows default argument");
641 goto error;
642 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 if (NCH(ch) == 3) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000644 asdl_seq_SET(args, k++,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000645 compiler_complex_args(c, CHILD(ch, 1)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 }
647 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000648 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
650 ast_error(CHILD(ch, 0), "assignment to None");
651 goto error;
652 }
Armin Rigo31441302005-10-21 12:57:31 +0000653 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000654 Param, LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 if (!name)
656 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000657 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
659 }
660 i += 2; /* the name and the comma */
661 break;
662 case STAR:
663 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
664 ast_error(CHILD(n, i+1), "assignment to None");
665 goto error;
666 }
667 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
668 i += 3;
669 break;
670 case DOUBLESTAR:
671 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
672 ast_error(CHILD(n, i+1), "assignment to None");
673 goto error;
674 }
675 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
676 i += 3;
677 break;
678 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000679 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 "unexpected node in varargslist: %d @ %d",
681 TYPE(ch), i);
682 goto error;
683 }
684 }
685
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000686 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687
688 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000689 Py_XDECREF(vararg);
690 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 return NULL;
692}
693
694static expr_ty
695ast_for_dotted_name(struct compiling *c, const node *n)
696{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000697 expr_ty e;
698 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000699 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 int i;
701
702 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000703
704 lineno = LINENO(n);
705 col_offset = n->n_col_offset;
706
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 id = NEW_IDENTIFIER(CHILD(n, 0));
708 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000709 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000710 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000712 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713
714 for (i = 2; i < NCH(n); i+=2) {
715 id = NEW_IDENTIFIER(CHILD(n, i));
716 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000717 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000718 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000719 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000720 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 }
722
723 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724}
725
726static expr_ty
727ast_for_decorator(struct compiling *c, const node *n)
728{
729 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
730 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000731 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732
733 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000734 REQ(CHILD(n, 0), AT);
735 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
737 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
738 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740
741 if (NCH(n) == 3) { /* No arguments */
742 d = name_expr;
743 name_expr = NULL;
744 }
745 else if (NCH(n) == 5) { /* Call with no arguments */
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000746 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 name_expr = NULL;
750 }
751 else {
752 d = ast_for_call(c, CHILD(n, 3), name_expr);
753 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000754 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 name_expr = NULL;
756 }
757
758 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759}
760
761static asdl_seq*
762ast_for_decorators(struct compiling *c, const node *n)
763{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000764 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000765 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 int i;
767
768 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000769 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 if (!decorator_seq)
771 return NULL;
772
773 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000774 d = ast_for_decorator(c, CHILD(n, i));
775 if (!d)
776 return NULL;
777 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 }
779 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780}
781
782static stmt_ty
783ast_for_funcdef(struct compiling *c, const node *n)
784{
785 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000786 identifier name;
787 arguments_ty args;
788 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 asdl_seq *decorator_seq = NULL;
790 int name_i;
791
792 REQ(n, funcdef);
793
794 if (NCH(n) == 6) { /* decorators are present */
795 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
796 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000797 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 name_i = 2;
799 }
800 else {
801 name_i = 1;
802 }
803
804 name = NEW_IDENTIFIER(CHILD(n, name_i));
805 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000806 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000808 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811 args = ast_for_arguments(c, CHILD(n, name_i + 1));
812 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 body = ast_for_suite(c, CHILD(n, name_i + 3));
815 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000818 return FunctionDef(name, args, body, decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819}
820
821static expr_ty
822ast_for_lambdef(struct compiling *c, const node *n)
823{
824 /* lambdef: 'lambda' [varargslist] ':' test */
825 arguments_ty args;
826 expr_ty expression;
827
828 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000829 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 if (!args)
831 return NULL;
832 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 }
836 else {
837 args = ast_for_arguments(c, CHILD(n, 1));
838 if (!args)
839 return NULL;
840 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000841 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 }
844
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000845 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846}
847
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000848static expr_ty
849ast_for_ifexpr(struct compiling *c, const node *n)
850{
851 /* test: or_test 'if' or_test 'else' test */
852 expr_ty expression, body, orelse;
853
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000854 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000855 body = ast_for_expr(c, CHILD(n, 0));
856 if (!body)
857 return NULL;
858 expression = ast_for_expr(c, CHILD(n, 2));
859 if (!expression)
860 return NULL;
861 orelse = ast_for_expr(c, CHILD(n, 4));
862 if (!orelse)
863 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000864 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000865}
866
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867/* Count the number of 'for' loop in a list comprehension.
868
869 Helper for ast_for_listcomp().
870*/
871
872static int
873count_list_fors(const node *n)
874{
875 int n_fors = 0;
876 node *ch = CHILD(n, 1);
877
878 count_list_for:
879 n_fors++;
880 REQ(ch, list_for);
881 if (NCH(ch) == 5)
882 ch = CHILD(ch, 4);
883 else
884 return n_fors;
885 count_list_iter:
886 REQ(ch, list_iter);
887 ch = CHILD(ch, 0);
888 if (TYPE(ch) == list_for)
889 goto count_list_for;
890 else if (TYPE(ch) == list_if) {
891 if (NCH(ch) == 3) {
892 ch = CHILD(ch, 2);
893 goto count_list_iter;
894 }
895 else
896 return n_fors;
897 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000898
899 /* Should never be reached */
900 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
901 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902}
903
904/* Count the number of 'if' statements in a list comprehension.
905
906 Helper for ast_for_listcomp().
907*/
908
909static int
910count_list_ifs(const node *n)
911{
912 int n_ifs = 0;
913
914 count_list_iter:
915 REQ(n, list_iter);
916 if (TYPE(CHILD(n, 0)) == list_for)
917 return n_ifs;
918 n = CHILD(n, 0);
919 REQ(n, list_if);
920 n_ifs++;
921 if (NCH(n) == 2)
922 return n_ifs;
923 n = CHILD(n, 2);
924 goto count_list_iter;
925}
926
927static expr_ty
928ast_for_listcomp(struct compiling *c, const node *n)
929{
930 /* listmaker: test ( list_for | (',' test)* [','] )
931 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
932 list_iter: list_for | list_if
933 list_if: 'if' test [list_iter]
934 testlist_safe: test [(',' test)+ [',']]
935 */
936 expr_ty elt;
937 asdl_seq *listcomps;
938 int i, n_fors;
939 node *ch;
940
941 REQ(n, listmaker);
942 assert(NCH(n) > 1);
943
944 elt = ast_for_expr(c, CHILD(n, 0));
945 if (!elt)
946 return NULL;
947
948 n_fors = count_list_fors(n);
949 if (n_fors == -1)
950 return NULL;
951
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000952 listcomps = asdl_seq_new(n_fors, c->c_arena);
953 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 ch = CHILD(n, 1);
957 for (i = 0; i < n_fors; i++) {
958 comprehension_ty lc;
959 asdl_seq *t;
960 expr_ty expression;
961
962 REQ(ch, list_for);
963
964 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000965 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000967 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000968 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000971 if (asdl_seq_LEN(t) == 1)
972 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
973 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000975 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976 expression, NULL, c->c_arena);
977 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979
980 if (NCH(ch) == 5) {
981 int j, n_ifs;
982 asdl_seq *ifs;
983
984 ch = CHILD(ch, 4);
985 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000986 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000989 ifs = asdl_seq_new(n_ifs, c->c_arena);
990 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992
993 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000994 REQ(ch, list_iter);
995 ch = CHILD(ch, 0);
996 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
Jeremy Hyltona8293132006-02-28 17:58:27 +0000998 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
999 if (NCH(ch) == 3)
1000 ch = CHILD(ch, 2);
1001 }
1002 /* on exit, must guarantee that ch is a list_for */
1003 if (TYPE(ch) == list_iter)
1004 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001006 }
1007 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 }
1009
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001010 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011}
1012
1013/*
1014 Count the number of 'for' loops in a generator expression.
1015
1016 Helper for ast_for_genexp().
1017*/
1018
1019static int
1020count_gen_fors(const node *n)
1021{
1022 int n_fors = 0;
1023 node *ch = CHILD(n, 1);
1024
1025 count_gen_for:
1026 n_fors++;
1027 REQ(ch, gen_for);
1028 if (NCH(ch) == 5)
1029 ch = CHILD(ch, 4);
1030 else
1031 return n_fors;
1032 count_gen_iter:
1033 REQ(ch, gen_iter);
1034 ch = CHILD(ch, 0);
1035 if (TYPE(ch) == gen_for)
1036 goto count_gen_for;
1037 else if (TYPE(ch) == gen_if) {
1038 if (NCH(ch) == 3) {
1039 ch = CHILD(ch, 2);
1040 goto count_gen_iter;
1041 }
1042 else
1043 return n_fors;
1044 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001045
1046 /* Should never be reached */
1047 PyErr_SetString(PyExc_SystemError,
1048 "logic error in count_gen_fors");
1049 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050}
1051
1052/* Count the number of 'if' statements in a generator expression.
1053
1054 Helper for ast_for_genexp().
1055*/
1056
1057static int
1058count_gen_ifs(const node *n)
1059{
1060 int n_ifs = 0;
1061
1062 while (1) {
1063 REQ(n, gen_iter);
1064 if (TYPE(CHILD(n, 0)) == gen_for)
1065 return n_ifs;
1066 n = CHILD(n, 0);
1067 REQ(n, gen_if);
1068 n_ifs++;
1069 if (NCH(n) == 2)
1070 return n_ifs;
1071 n = CHILD(n, 2);
1072 }
1073}
1074
Jeremy Hyltona8293132006-02-28 17:58:27 +00001075/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076static expr_ty
1077ast_for_genexp(struct compiling *c, const node *n)
1078{
1079 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1080 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1081 expr_ty elt;
1082 asdl_seq *genexps;
1083 int i, n_fors;
1084 node *ch;
1085
1086 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1087 assert(NCH(n) > 1);
1088
1089 elt = ast_for_expr(c, CHILD(n, 0));
1090 if (!elt)
1091 return NULL;
1092
1093 n_fors = count_gen_fors(n);
1094 if (n_fors == -1)
1095 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001096
1097 genexps = asdl_seq_new(n_fors, c->c_arena);
1098 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001100
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101 ch = CHILD(n, 1);
1102 for (i = 0; i < n_fors; i++) {
1103 comprehension_ty ge;
1104 asdl_seq *t;
1105 expr_ty expression;
1106
1107 REQ(ch, gen_for);
1108
1109 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001110 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001112 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001113 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001115
1116 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001118 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001120 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001121 expression, NULL, c->c_arena);
1122
1123 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 if (NCH(ch) == 5) {
1127 int j, n_ifs;
1128 asdl_seq *ifs;
1129
1130 ch = CHILD(ch, 4);
1131 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001132 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001134
1135 ifs = asdl_seq_new(n_ifs, c->c_arena);
1136 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001138
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 for (j = 0; j < n_ifs; j++) {
1140 REQ(ch, gen_iter);
1141 ch = CHILD(ch, 0);
1142 REQ(ch, gen_if);
1143
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001144 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001145 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001146 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001147 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 if (NCH(ch) == 3)
1149 ch = CHILD(ch, 2);
1150 }
1151 /* on exit, must guarantee that ch is a gen_for */
1152 if (TYPE(ch) == gen_iter)
1153 ch = CHILD(ch, 0);
1154 ge->ifs = ifs;
1155 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001156 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 }
1158
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001159 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160}
1161
1162static expr_ty
1163ast_for_atom(struct compiling *c, const node *n)
1164{
1165 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1166 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1167 */
1168 node *ch = CHILD(n, 0);
1169
1170 switch (TYPE(ch)) {
1171 case NAME:
1172 /* All names start in Load context, but may later be
1173 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001174 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 case STRING: {
1176 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 if (!str)
1178 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001179
1180 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001181 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 }
1183 case NUMBER: {
1184 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 if (!pynum)
1186 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187
1188 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001189 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 }
1191 case LPAR: /* some parenthesized expressions */
1192 ch = CHILD(n, 1);
1193
1194 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001195 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196
1197 if (TYPE(ch) == yield_expr)
1198 return ast_for_expr(c, ch);
1199
1200 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1201 return ast_for_genexp(c, ch);
1202
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001203 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 case LSQB: /* list (or list comprehension) */
1205 ch = CHILD(n, 1);
1206
1207 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001208 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209
1210 REQ(ch, listmaker);
1211 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1212 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 if (!elts)
1214 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001215
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001216 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 }
1218 else
1219 return ast_for_listcomp(c, ch);
1220 case LBRACE: {
1221 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1222 int i, size;
1223 asdl_seq *keys, *values;
1224
1225 ch = CHILD(n, 1);
1226 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001227 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228 if (!keys)
1229 return NULL;
1230
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001231 values = asdl_seq_new(size, c->c_arena);
1232 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234
1235 for (i = 0; i < NCH(ch); i += 4) {
1236 expr_ty expression;
1237
1238 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001243
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001245 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001247
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 asdl_seq_SET(values, i / 4, expression);
1249 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001250 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 }
1252 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001253 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 if (!expression)
1255 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001256
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001257 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 }
1259 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001260 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 return NULL;
1262 }
1263}
1264
1265static slice_ty
1266ast_for_slice(struct compiling *c, const node *n)
1267{
1268 node *ch;
1269 expr_ty lower = NULL, upper = NULL, step = NULL;
1270
1271 REQ(n, subscript);
1272
1273 /*
1274 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1275 sliceop: ':' [test]
1276 */
1277 ch = CHILD(n, 0);
1278 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001279 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280
1281 if (NCH(n) == 1 && TYPE(ch) == test) {
1282 /* 'step' variable hold no significance in terms of being used over
1283 other vars */
1284 step = ast_for_expr(c, ch);
1285 if (!step)
1286 return NULL;
1287
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001288 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 }
1290
1291 if (TYPE(ch) == test) {
1292 lower = ast_for_expr(c, ch);
1293 if (!lower)
1294 return NULL;
1295 }
1296
1297 /* If there's an upper bound it's in the second or third position. */
1298 if (TYPE(ch) == COLON) {
1299 if (NCH(n) > 1) {
1300 node *n2 = CHILD(n, 1);
1301
1302 if (TYPE(n2) == test) {
1303 upper = ast_for_expr(c, n2);
1304 if (!upper)
1305 return NULL;
1306 }
1307 }
1308 } else if (NCH(n) > 2) {
1309 node *n2 = CHILD(n, 2);
1310
1311 if (TYPE(n2) == test) {
1312 upper = ast_for_expr(c, n2);
1313 if (!upper)
1314 return NULL;
1315 }
1316 }
1317
1318 ch = CHILD(n, NCH(n) - 1);
1319 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001320 if (NCH(ch) == 1) {
1321 /* No expression, so step is None */
1322 ch = CHILD(ch, 0);
1323 step = Name(new_identifier("None", c->c_arena), Load,
1324 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 if (!step)
1326 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001327 } else {
1328 ch = CHILD(ch, 1);
1329 if (TYPE(ch) == test) {
1330 step = ast_for_expr(c, ch);
1331 if (!step)
1332 return NULL;
1333 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 }
1335 }
1336
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001337 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338}
1339
1340static expr_ty
1341ast_for_binop(struct compiling *c, const node *n)
1342{
1343 /* Must account for a sequence of expressions.
1344 How should A op B op C by represented?
1345 BinOp(BinOp(A, op, B), op, C).
1346 */
1347
1348 int i, nops;
1349 expr_ty expr1, expr2, result;
1350 operator_ty operator;
1351
1352 expr1 = ast_for_expr(c, CHILD(n, 0));
1353 if (!expr1)
1354 return NULL;
1355
1356 expr2 = ast_for_expr(c, CHILD(n, 2));
1357 if (!expr2)
1358 return NULL;
1359
1360 operator = get_operator(CHILD(n, 1));
1361 if (!operator)
1362 return NULL;
1363
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001364 result = BinOp(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 if (!result)
1366 return NULL;
1367
1368 nops = (NCH(n) - 1) / 2;
1369 for (i = 1; i < nops; i++) {
1370 expr_ty tmp_result, tmp;
1371 const node* next_oper = CHILD(n, i * 2 + 1);
1372
1373 operator = get_operator(next_oper);
1374 if (!operator)
1375 return NULL;
1376
1377 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1378 if (!tmp)
1379 return NULL;
1380
1381 tmp_result = BinOp(result, operator, tmp,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001382 LINENO(next_oper), next_oper->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 if (!tmp)
1384 return NULL;
1385 result = tmp_result;
1386 }
1387 return result;
1388}
1389
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001390static expr_ty
1391ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1392{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001393 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1394 subscriptlist: subscript (',' subscript)* [',']
1395 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1396 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001397 REQ(n, trailer);
1398 if (TYPE(CHILD(n, 0)) == LPAR) {
1399 if (NCH(n) == 2)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001400 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001401 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001402 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001403 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001404 else if (TYPE(CHILD(n, 0)) == DOT ) {
1405 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001406 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001407 }
1408 else {
1409 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001410 REQ(CHILD(n, 2), RSQB);
1411 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001412 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001413 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1414 if (!slc)
1415 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001416 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001417 }
1418 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001419 /* The grammar is ambiguous here. The ambiguity is resolved
1420 by treating the sequence as a tuple literal if there are
1421 no slice features.
1422 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001423 int j;
1424 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001425 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001426 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001427 asdl_seq *slices, *elts;
1428 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001429 if (!slices)
1430 return NULL;
1431 for (j = 0; j < NCH(n); j += 2) {
1432 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001433 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001434 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001435 if (slc->kind != Index_kind)
1436 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001437 asdl_seq_SET(slices, j / 2, slc);
1438 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001439 if (!simple) {
1440 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001441 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001442 }
1443 /* extract Index values and put them in a Tuple */
1444 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001445 if (!elts)
1446 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001447 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1448 slc = (slice_ty)asdl_seq_GET(slices, j);
1449 assert(slc->kind == Index_kind && slc->v.Index.value);
1450 asdl_seq_SET(elts, j, slc->v.Index.value);
1451 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001452 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001453 if (!e)
1454 return NULL;
1455 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001456 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001457 }
1458 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001459}
1460
1461static expr_ty
1462ast_for_power(struct compiling *c, const node *n)
1463{
1464 /* power: atom trailer* ('**' factor)*
1465 */
1466 int i;
1467 expr_ty e, tmp;
1468 REQ(n, power);
1469 e = ast_for_atom(c, CHILD(n, 0));
1470 if (!e)
1471 return NULL;
1472 if (NCH(n) == 1)
1473 return e;
1474 for (i = 1; i < NCH(n); i++) {
1475 node *ch = CHILD(n, i);
1476 if (TYPE(ch) != trailer)
1477 break;
1478 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001479 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001480 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001481 tmp->lineno = e->lineno;
1482 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001483 e = tmp;
1484 }
1485 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1486 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001487 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001488 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001489 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001490 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001491 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001492 e = tmp;
1493 }
1494 return e;
1495}
1496
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497/* Do not name a variable 'expr'! Will cause a compile error.
1498*/
1499
1500static expr_ty
1501ast_for_expr(struct compiling *c, const node *n)
1502{
1503 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001504 test: or_test ['if' or_test 'else' test] | lambdef
1505 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 and_test: not_test ('and' not_test)*
1507 not_test: 'not' not_test | comparison
1508 comparison: expr (comp_op expr)*
1509 expr: xor_expr ('|' xor_expr)*
1510 xor_expr: and_expr ('^' and_expr)*
1511 and_expr: shift_expr ('&' shift_expr)*
1512 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1513 arith_expr: term (('+'|'-') term)*
1514 term: factor (('*'|'/'|'%'|'//') factor)*
1515 factor: ('+'|'-'|'~') factor | power
1516 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001517
1518 As well as modified versions that exist for backward compatibility,
1519 to explicitly allow:
1520 [ x for x in lambda: 0, lambda: 1 ]
1521 (which would be ambiguous without these extra rules)
1522
1523 old_test: or_test | old_lambdef
1524 old_lambdef: 'lambda' [vararglist] ':' old_test
1525
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526 */
1527
1528 asdl_seq *seq;
1529 int i;
1530
1531 loop:
1532 switch (TYPE(n)) {
1533 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001534 case old_test:
1535 if (TYPE(CHILD(n, 0)) == lambdef ||
1536 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001538 else if (NCH(n) > 1)
1539 return ast_for_ifexpr(c, n);
1540 /* Fallthrough */
1541 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542 case and_test:
1543 if (NCH(n) == 1) {
1544 n = CHILD(n, 0);
1545 goto loop;
1546 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001547 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 if (!seq)
1549 return NULL;
1550 for (i = 0; i < NCH(n); i += 2) {
1551 expr_ty e = ast_for_expr(c, CHILD(n, i));
1552 if (!e)
1553 return NULL;
1554 asdl_seq_SET(seq, i / 2, e);
1555 }
1556 if (!strcmp(STR(CHILD(n, 1)), "and"))
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001557 return BoolOp(And, seq, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001558 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001559 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560 case not_test:
1561 if (NCH(n) == 1) {
1562 n = CHILD(n, 0);
1563 goto loop;
1564 }
1565 else {
1566 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1567 if (!expression)
1568 return NULL;
1569
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001570 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 }
1572 case comparison:
1573 if (NCH(n) == 1) {
1574 n = CHILD(n, 0);
1575 goto loop;
1576 }
1577 else {
1578 expr_ty expression;
1579 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001580 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 if (!ops)
1582 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001583 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585 return NULL;
1586 }
1587 for (i = 1; i < NCH(n); i += 2) {
1588 /* XXX cmpop_ty is just an enum */
1589 cmpop_ty operator;
1590
1591 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001592 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001594 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595
1596 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001597 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001599 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001601 asdl_seq_SET(ops, i / 2, (void *)(Py_uintptr_t)operator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602 asdl_seq_SET(cmps, i / 2, expression);
1603 }
1604 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001605 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001607 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001609 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 }
1611 break;
1612
1613 /* The next five cases all handle BinOps. The main body of code
1614 is the same in each case, but the switch turned inside out to
1615 reuse the code for each type of operator.
1616 */
1617 case expr:
1618 case xor_expr:
1619 case and_expr:
1620 case shift_expr:
1621 case arith_expr:
1622 case term:
1623 if (NCH(n) == 1) {
1624 n = CHILD(n, 0);
1625 goto loop;
1626 }
1627 return ast_for_binop(c, n);
1628 case yield_expr: {
1629 expr_ty exp = NULL;
1630 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001631 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632 if (!exp)
1633 return NULL;
1634 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001635 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 }
1637 case factor: {
1638 expr_ty expression;
1639
1640 if (NCH(n) == 1) {
1641 n = CHILD(n, 0);
1642 goto loop;
1643 }
1644
1645 expression = ast_for_expr(c, CHILD(n, 1));
1646 if (!expression)
1647 return NULL;
1648
1649 switch (TYPE(CHILD(n, 0))) {
1650 case PLUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001651 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652 case MINUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001653 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654 case TILDE:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001655 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001657 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1658 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 break;
1660 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001661 case power:
1662 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001664 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 return NULL;
1666 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001667 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 return NULL;
1669}
1670
1671static expr_ty
1672ast_for_call(struct compiling *c, const node *n, expr_ty func)
1673{
1674 /*
1675 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1676 | '**' test)
1677 argument: [test '='] test [gen_for] # Really [keyword '='] test
1678 */
1679
1680 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001681 asdl_seq *args;
1682 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 expr_ty vararg = NULL, kwarg = NULL;
1684
1685 REQ(n, arglist);
1686
1687 nargs = 0;
1688 nkeywords = 0;
1689 ngens = 0;
1690 for (i = 0; i < NCH(n); i++) {
1691 node *ch = CHILD(n, i);
1692 if (TYPE(ch) == argument) {
1693 if (NCH(ch) == 1)
1694 nargs++;
1695 else if (TYPE(CHILD(ch, 1)) == gen_for)
1696 ngens++;
1697 else
1698 nkeywords++;
1699 }
1700 }
1701 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001702 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 "if not sole argument");
1704 return NULL;
1705 }
1706
1707 if (nargs + nkeywords + ngens > 255) {
1708 ast_error(n, "more than 255 arguments");
1709 return NULL;
1710 }
1711
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001712 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001714 return NULL;
1715 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001717 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 nargs = 0;
1719 nkeywords = 0;
1720 for (i = 0; i < NCH(n); i++) {
1721 node *ch = CHILD(n, i);
1722 if (TYPE(ch) == argument) {
1723 expr_ty e;
1724 if (NCH(ch) == 1) {
1725 e = ast_for_expr(c, CHILD(ch, 0));
1726 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001727 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 asdl_seq_SET(args, nargs++, e);
1729 }
1730 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1731 e = ast_for_genexp(c, ch);
1732 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001733 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 asdl_seq_SET(args, nargs++, e);
1735 }
1736 else {
1737 keyword_ty kw;
1738 identifier key;
1739
1740 /* CHILD(ch, 0) is test, but must be an identifier? */
1741 e = ast_for_expr(c, CHILD(ch, 0));
1742 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 /* f(lambda x: x[0] = 3) ends up getting parsed with
1745 * LHS test = lambda x: x[0], and RHS test = 3.
1746 * SF bug 132313 points out that complaining about a keyword
1747 * then is very confusing.
1748 */
1749 if (e->kind == Lambda_kind) {
1750 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001751 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752 } else if (e->kind != Name_kind) {
1753 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001754 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755 }
1756 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 e = ast_for_expr(c, CHILD(ch, 2));
1758 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001759 return NULL;
1760 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001762 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 asdl_seq_SET(keywords, nkeywords++, kw);
1764 }
1765 }
1766 else if (TYPE(ch) == STAR) {
1767 vararg = ast_for_expr(c, CHILD(n, i+1));
1768 i++;
1769 }
1770 else if (TYPE(ch) == DOUBLESTAR) {
1771 kwarg = ast_for_expr(c, CHILD(n, i+1));
1772 i++;
1773 }
1774 }
1775
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001776 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777}
1778
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001780ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001782 /* testlist_gexp: test (',' test)* [','] */
1783 /* testlist: test (',' test)* [','] */
1784 /* testlist_safe: test (',' test)+ [','] */
1785 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001787 if (TYPE(n) == testlist_gexp) {
1788 if (NCH(n) > 1)
1789 assert(TYPE(CHILD(n, 1)) != gen_for);
1790 }
1791 else {
1792 assert(TYPE(n) == testlist ||
1793 TYPE(n) == testlist_safe ||
1794 TYPE(n) == testlist1);
1795 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 if (NCH(n) == 1)
1797 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 else {
1799 asdl_seq *tmp = seq_for_testlist(c, n);
1800 if (!tmp)
1801 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001802 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001804}
1805
1806static expr_ty
1807ast_for_testlist_gexp(struct compiling *c, const node* n)
1808{
1809 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1810 /* argument: test [ gen_for ] */
1811 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001812 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001813 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001814 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001815}
1816
1817/* like ast_for_testlist() but returns a sequence */
1818static asdl_seq*
1819ast_for_class_bases(struct compiling *c, const node* n)
1820{
1821 /* testlist: test (',' test)* [','] */
1822 assert(NCH(n) > 0);
1823 REQ(n, testlist);
1824 if (NCH(n) == 1) {
1825 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001826 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001827 if (!bases)
1828 return NULL;
1829 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001830 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001831 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001832 asdl_seq_SET(bases, 0, base);
1833 return bases;
1834 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001835
1836 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837}
1838
1839static stmt_ty
1840ast_for_expr_stmt(struct compiling *c, const node *n)
1841{
1842 REQ(n, expr_stmt);
1843 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1844 | ('=' (yield_expr|testlist))*)
1845 testlist: test (',' test)* [',']
1846 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1847 | '<<=' | '>>=' | '**=' | '//='
1848 test: ... here starts the operator precendence dance
1849 */
1850
1851 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001852 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 if (!e)
1854 return NULL;
1855
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001856 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 }
1858 else if (TYPE(CHILD(n, 1)) == augassign) {
1859 expr_ty expr1, expr2;
1860 operator_ty operator;
1861 node *ch = CHILD(n, 0);
1862
1863 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001864 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001866 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001867 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868
1869 if (!expr1)
1870 return NULL;
Neal Norwitz96e48d42006-02-05 02:07:19 +00001871 /* TODO(jhylton): Figure out why set_context() can't be used here. */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001872 switch (expr1->kind) {
1873 case GeneratorExp_kind:
1874 ast_error(ch, "augmented assignment to generator "
1875 "expression not possible");
1876 return NULL;
1877 case Name_kind: {
1878 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1879 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1880 ast_error(ch, "assignment to None");
1881 return NULL;
1882 }
1883 break;
1884 }
1885 case Attribute_kind:
1886 case Subscript_kind:
1887 break;
1888 default:
1889 ast_error(ch, "illegal expression for augmented "
1890 "assignment");
1891 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893
1894 ch = CHILD(n, 2);
1895 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001896 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001898 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), ch->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001899 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 return NULL;
1901
1902 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001903 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904 return NULL;
1905
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001906 return AugAssign(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 }
1908 else {
1909 int i;
1910 asdl_seq *targets;
1911 node *value;
1912 expr_ty expression;
1913
1914 /* a normal assignment */
1915 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001916 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 if (!targets)
1918 return NULL;
1919 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001920 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 node *ch = CHILD(n, i);
1922 if (TYPE(ch) == yield_expr) {
1923 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001926 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927
1928 /* set context to assign */
1929 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001930 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931
Neal Norwitz84456bd2005-12-18 03:16:20 +00001932 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001933 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934
1935 asdl_seq_SET(targets, i / 2, e);
1936 }
1937 value = CHILD(n, NCH(n) - 1);
1938 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001939 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 else
1941 expression = ast_for_expr(c, value);
1942 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001943 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001944 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946}
1947
1948static stmt_ty
1949ast_for_print_stmt(struct compiling *c, const node *n)
1950{
1951 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1952 | '>>' test [ (',' test)+ [','] ] )
1953 */
1954 expr_ty dest = NULL, expression;
1955 asdl_seq *seq;
1956 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001957 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958
1959 REQ(n, print_stmt);
1960 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1961 dest = ast_for_expr(c, CHILD(n, 2));
1962 if (!dest)
1963 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001964 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001966 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00001968 return NULL;
1969 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001971 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001973 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 }
1975 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001976 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977}
1978
1979static asdl_seq *
1980ast_for_exprlist(struct compiling *c, const node *n, int context)
1981{
1982 asdl_seq *seq;
1983 int i;
1984 expr_ty e;
1985
1986 REQ(n, exprlist);
1987
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001988 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 if (!seq)
1990 return NULL;
1991 for (i = 0; i < NCH(n); i += 2) {
1992 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001993 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001994 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00001995 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001996 if (context && !set_context(e, context, CHILD(n, i)))
1997 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 }
1999 return seq;
2000}
2001
2002static stmt_ty
2003ast_for_del_stmt(struct compiling *c, const node *n)
2004{
2005 asdl_seq *expr_list;
2006
2007 /* del_stmt: 'del' exprlist */
2008 REQ(n, del_stmt);
2009
2010 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2011 if (!expr_list)
2012 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002013 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014}
2015
2016static stmt_ty
2017ast_for_flow_stmt(struct compiling *c, const node *n)
2018{
2019 /*
2020 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2021 | yield_stmt
2022 break_stmt: 'break'
2023 continue_stmt: 'continue'
2024 return_stmt: 'return' [testlist]
2025 yield_stmt: yield_expr
2026 yield_expr: 'yield' testlist
2027 raise_stmt: 'raise' [test [',' test [',' test]]]
2028 */
2029 node *ch;
2030
2031 REQ(n, flow_stmt);
2032 ch = CHILD(n, 0);
2033 switch (TYPE(ch)) {
2034 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002035 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002037 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 case yield_stmt: { /* will reduce to yield_expr */
2039 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2040 if (!exp)
2041 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002042 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 }
2044 case return_stmt:
2045 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002046 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002048 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 if (!expression)
2050 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002051 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 }
2053 case raise_stmt:
2054 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002055 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 else if (NCH(ch) == 2) {
2057 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2058 if (!expression)
2059 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002060 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 }
2062 else if (NCH(ch) == 4) {
2063 expr_ty expr1, expr2;
2064
2065 expr1 = ast_for_expr(c, CHILD(ch, 1));
2066 if (!expr1)
2067 return NULL;
2068 expr2 = ast_for_expr(c, CHILD(ch, 3));
2069 if (!expr2)
2070 return NULL;
2071
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002072 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 }
2074 else if (NCH(ch) == 6) {
2075 expr_ty expr1, expr2, expr3;
2076
2077 expr1 = ast_for_expr(c, CHILD(ch, 1));
2078 if (!expr1)
2079 return NULL;
2080 expr2 = ast_for_expr(c, CHILD(ch, 3));
2081 if (!expr2)
2082 return NULL;
2083 expr3 = ast_for_expr(c, CHILD(ch, 5));
2084 if (!expr3)
2085 return NULL;
2086
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002087 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 }
2089 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002090 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 "unexpected flow_stmt: %d", TYPE(ch));
2092 return NULL;
2093 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002094
2095 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2096 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097}
2098
2099static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002100alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101{
2102 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002103 import_as_name: NAME ['as' NAME]
2104 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 dotted_name: NAME ('.' NAME)*
2106 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002107 PyObject *str;
2108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 loop:
2110 switch (TYPE(n)) {
2111 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002112 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2113 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 case dotted_as_name:
2115 if (NCH(n) == 1) {
2116 n = CHILD(n, 0);
2117 goto loop;
2118 }
2119 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002120 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 assert(!a->asname);
2122 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2123 return a;
2124 }
2125 break;
2126 case dotted_name:
2127 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002128 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 else {
2130 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002131 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002132 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 char *s;
2134
2135 len = 0;
2136 for (i = 0; i < NCH(n); i += 2)
2137 /* length of string plus one for the dot */
2138 len += strlen(STR(CHILD(n, i))) + 1;
2139 len--; /* the last name doesn't have a dot */
2140 str = PyString_FromStringAndSize(NULL, len);
2141 if (!str)
2142 return NULL;
2143 s = PyString_AS_STRING(str);
2144 if (!s)
2145 return NULL;
2146 for (i = 0; i < NCH(n); i += 2) {
2147 char *sch = STR(CHILD(n, i));
2148 strcpy(s, STR(CHILD(n, i)));
2149 s += strlen(sch);
2150 *s++ = '.';
2151 }
2152 --s;
2153 *s = '\0';
2154 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002155 PyArena_AddPyObject(c->c_arena, str);
2156 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 }
2158 break;
2159 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002160 str = PyString_InternFromString("*");
2161 PyArena_AddPyObject(c->c_arena, str);
2162 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002164 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 "unexpected import name: %d", TYPE(n));
2166 return NULL;
2167 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002168
2169 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 return NULL;
2171}
2172
2173static stmt_ty
2174ast_for_import_stmt(struct compiling *c, const node *n)
2175{
2176 /*
2177 import_stmt: import_name | import_from
2178 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002179 import_from: 'from' ('.'* dotted_name | '.') 'import'
2180 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002182 int lineno;
2183 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 int i;
2185 asdl_seq *aliases;
2186
2187 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002188 lineno = LINENO(n);
2189 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002191 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002193 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002194 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 if (!aliases)
2196 return NULL;
2197 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002198 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002199 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 asdl_seq_SET(aliases, i / 2, import_alias);
2202 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002203 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002205 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002207 int idx, ndots = 0;
2208 alias_ty mod = NULL;
2209 identifier modname;
2210
2211 /* Count the number of dots (for relative imports) and check for the
2212 optional module name */
2213 for (idx = 1; idx < NCH(n); idx++) {
2214 if (TYPE(CHILD(n, idx)) == dotted_name) {
2215 mod = alias_for_import_name(c, CHILD(n, idx));
2216 idx++;
2217 break;
2218 } else if (TYPE(CHILD(n, idx)) != DOT) {
2219 break;
2220 }
2221 ndots++;
2222 }
2223 idx++; /* skip over the 'import' keyword */
2224 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002225 case STAR:
2226 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002227 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002228 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002229 if (ndots) {
2230 ast_error(n, "'import *' not allowed with 'from .'");
2231 return NULL;
2232 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002233 break;
2234 case LPAR:
2235 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002236 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002237 n_children = NCH(n);
2238 break;
2239 case import_as_names:
2240 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002241 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002242 n_children = NCH(n);
2243 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 ast_error(n, "trailing comma not allowed without"
2245 " surrounding parentheses");
2246 return NULL;
2247 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002248 break;
2249 default:
2250 ast_error(n, "Unexpected node-type in from-import");
2251 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002252 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002254 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002255 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257
2258 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002259 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002260 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002261 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002263 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002265 else {
2266 for (i = 0; i < NCH(n); i += 2) {
2267 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2268 if (!import_alias)
2269 return NULL;
2270 asdl_seq_SET(aliases, i / 2, import_alias);
2271 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002273 if (mod != NULL)
2274 modname = mod->name;
2275 else
2276 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002277 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002278 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 }
Neal Norwitz79792652005-11-14 04:25:03 +00002280 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 "unknown import statement: starts with command '%s'",
2282 STR(CHILD(n, 0)));
2283 return NULL;
2284}
2285
2286static stmt_ty
2287ast_for_global_stmt(struct compiling *c, const node *n)
2288{
2289 /* global_stmt: 'global' NAME (',' NAME)* */
2290 identifier name;
2291 asdl_seq *s;
2292 int i;
2293
2294 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002295 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 if (!s)
2297 return NULL;
2298 for (i = 1; i < NCH(n); i += 2) {
2299 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002300 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 asdl_seq_SET(s, i / 2, name);
2303 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002304 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305}
2306
2307static stmt_ty
2308ast_for_exec_stmt(struct compiling *c, const node *n)
2309{
2310 expr_ty expr1, globals = NULL, locals = NULL;
2311 int n_children = NCH(n);
2312 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002313 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314 "poorly formed 'exec' statement: %d parts to statement",
2315 n_children);
2316 return NULL;
2317 }
2318
2319 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2320 REQ(n, exec_stmt);
2321 expr1 = ast_for_expr(c, CHILD(n, 1));
2322 if (!expr1)
2323 return NULL;
2324 if (n_children >= 4) {
2325 globals = ast_for_expr(c, CHILD(n, 3));
2326 if (!globals)
2327 return NULL;
2328 }
2329 if (n_children == 6) {
2330 locals = ast_for_expr(c, CHILD(n, 5));
2331 if (!locals)
2332 return NULL;
2333 }
2334
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002335 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336}
2337
2338static stmt_ty
2339ast_for_assert_stmt(struct compiling *c, const node *n)
2340{
2341 /* assert_stmt: 'assert' test [',' test] */
2342 REQ(n, assert_stmt);
2343 if (NCH(n) == 2) {
2344 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2345 if (!expression)
2346 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002347 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 }
2349 else if (NCH(n) == 4) {
2350 expr_ty expr1, expr2;
2351
2352 expr1 = ast_for_expr(c, CHILD(n, 1));
2353 if (!expr1)
2354 return NULL;
2355 expr2 = ast_for_expr(c, CHILD(n, 3));
2356 if (!expr2)
2357 return NULL;
2358
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002359 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360 }
Neal Norwitz79792652005-11-14 04:25:03 +00002361 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002362 "improper number of parts to 'assert' statement: %d",
2363 NCH(n));
2364 return NULL;
2365}
2366
2367static asdl_seq *
2368ast_for_suite(struct compiling *c, const node *n)
2369{
2370 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002371 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002372 stmt_ty s;
2373 int i, total, num, end, pos = 0;
2374 node *ch;
2375
2376 REQ(n, suite);
2377
2378 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002379 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380 if (!seq)
2381 return NULL;
2382 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2383 n = CHILD(n, 0);
2384 /* simple_stmt always ends with a NEWLINE,
2385 and may have a trailing SEMI
2386 */
2387 end = NCH(n) - 1;
2388 if (TYPE(CHILD(n, end - 1)) == SEMI)
2389 end--;
2390 /* loop by 2 to skip semi-colons */
2391 for (i = 0; i < end; i += 2) {
2392 ch = CHILD(n, i);
2393 s = ast_for_stmt(c, ch);
2394 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002395 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 asdl_seq_SET(seq, pos++, s);
2397 }
2398 }
2399 else {
2400 for (i = 2; i < (NCH(n) - 1); i++) {
2401 ch = CHILD(n, i);
2402 REQ(ch, stmt);
2403 num = num_stmts(ch);
2404 if (num == 1) {
2405 /* small_stmt or compound_stmt with only one child */
2406 s = ast_for_stmt(c, ch);
2407 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002408 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409 asdl_seq_SET(seq, pos++, s);
2410 }
2411 else {
2412 int j;
2413 ch = CHILD(ch, 0);
2414 REQ(ch, simple_stmt);
2415 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002416 /* statement terminates with a semi-colon ';' */
2417 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002418 assert((j + 1) == NCH(ch));
2419 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002420 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 s = ast_for_stmt(c, CHILD(ch, j));
2422 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002423 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 asdl_seq_SET(seq, pos++, s);
2425 }
2426 }
2427 }
2428 }
2429 assert(pos == seq->size);
2430 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431}
2432
2433static stmt_ty
2434ast_for_if_stmt(struct compiling *c, const node *n)
2435{
2436 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2437 ['else' ':' suite]
2438 */
2439 char *s;
2440
2441 REQ(n, if_stmt);
2442
2443 if (NCH(n) == 4) {
2444 expr_ty expression;
2445 asdl_seq *suite_seq;
2446
2447 expression = ast_for_expr(c, CHILD(n, 1));
2448 if (!expression)
2449 return NULL;
2450 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002451 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 return NULL;
2453
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002454 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002456
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 s = STR(CHILD(n, 4));
2458 /* s[2], the third character in the string, will be
2459 's' for el_s_e, or
2460 'i' for el_i_f
2461 */
2462 if (s[2] == 's') {
2463 expr_ty expression;
2464 asdl_seq *seq1, *seq2;
2465
2466 expression = ast_for_expr(c, CHILD(n, 1));
2467 if (!expression)
2468 return NULL;
2469 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002470 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 return NULL;
2472 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002473 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 return NULL;
2475
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002476 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 }
2478 else if (s[2] == 'i') {
2479 int i, n_elif, has_else = 0;
2480 asdl_seq *orelse = NULL;
2481 n_elif = NCH(n) - 4;
2482 /* must reference the child n_elif+1 since 'else' token is third,
2483 not fourth, child from the end. */
2484 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2485 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2486 has_else = 1;
2487 n_elif -= 3;
2488 }
2489 n_elif /= 4;
2490
2491 if (has_else) {
2492 expr_ty expression;
2493 asdl_seq *seq1, *seq2;
2494
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002495 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 if (!orelse)
2497 return NULL;
2498 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002499 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002502 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002505 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507
2508 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002509 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002510 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 /* the just-created orelse handled the last elif */
2512 n_elif--;
2513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514
2515 for (i = 0; i < n_elif; i++) {
2516 int off = 5 + (n_elif - i - 1) * 4;
2517 expr_ty expression;
2518 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002519 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002520 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 return NULL;
2522 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002523 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002526 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528
2529 asdl_seq_SET(new, 0,
2530 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002531 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 orelse = new;
2533 }
2534 return If(ast_for_expr(c, CHILD(n, 1)),
2535 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002536 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002538
2539 PyErr_Format(PyExc_SystemError,
2540 "unexpected token in 'if' statement: %s", s);
2541 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542}
2543
2544static stmt_ty
2545ast_for_while_stmt(struct compiling *c, const node *n)
2546{
2547 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2548 REQ(n, while_stmt);
2549
2550 if (NCH(n) == 4) {
2551 expr_ty expression;
2552 asdl_seq *suite_seq;
2553
2554 expression = ast_for_expr(c, CHILD(n, 1));
2555 if (!expression)
2556 return NULL;
2557 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002558 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002560 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
2562 else if (NCH(n) == 7) {
2563 expr_ty expression;
2564 asdl_seq *seq1, *seq2;
2565
2566 expression = ast_for_expr(c, CHILD(n, 1));
2567 if (!expression)
2568 return NULL;
2569 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002570 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 return NULL;
2572 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002573 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 return NULL;
2575
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002576 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002578
2579 PyErr_Format(PyExc_SystemError,
2580 "wrong number of tokens for 'while' statement: %d",
2581 NCH(n));
2582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583}
2584
2585static stmt_ty
2586ast_for_for_stmt(struct compiling *c, const node *n)
2587{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002588 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 expr_ty expression;
2590 expr_ty target;
2591 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2592 REQ(n, for_stmt);
2593
2594 if (NCH(n) == 9) {
2595 seq = ast_for_suite(c, CHILD(n, 8));
2596 if (!seq)
2597 return NULL;
2598 }
2599
2600 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002601 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002603 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002606 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002608 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002609 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 return NULL;
2611 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002612 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 return NULL;
2614
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002615 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616}
2617
2618static excepthandler_ty
2619ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2620{
2621 /* except_clause: 'except' [test [',' test]] */
2622 REQ(exc, except_clause);
2623 REQ(body, suite);
2624
2625 if (NCH(exc) == 1) {
2626 asdl_seq *suite_seq = ast_for_suite(c, body);
2627 if (!suite_seq)
2628 return NULL;
2629
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002630 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 }
2632 else if (NCH(exc) == 2) {
2633 expr_ty expression;
2634 asdl_seq *suite_seq;
2635
2636 expression = ast_for_expr(c, CHILD(exc, 1));
2637 if (!expression)
2638 return NULL;
2639 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002640 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 return NULL;
2642
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002643 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 }
2645 else if (NCH(exc) == 4) {
2646 asdl_seq *suite_seq;
2647 expr_ty expression;
2648 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2649 if (!e)
2650 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002651 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 return NULL;
2653 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002654 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 return NULL;
2656 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002657 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 return NULL;
2659
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002660 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002662
2663 PyErr_Format(PyExc_SystemError,
2664 "wrong number of children for 'except' clause: %d",
2665 NCH(exc));
2666 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667}
2668
2669static stmt_ty
2670ast_for_try_stmt(struct compiling *c, const node *n)
2671{
Neal Norwitzf599f422005-12-17 21:33:47 +00002672 const int nch = NCH(n);
2673 int n_except = (nch - 3)/3;
2674 asdl_seq *body, *orelse = NULL, *finally = NULL;
2675
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 REQ(n, try_stmt);
2677
Neal Norwitzf599f422005-12-17 21:33:47 +00002678 body = ast_for_suite(c, CHILD(n, 2));
2679 if (body == NULL)
2680 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681
Neal Norwitzf599f422005-12-17 21:33:47 +00002682 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2683 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2684 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2685 /* we can assume it's an "else",
2686 because nch >= 9 for try-else-finally and
2687 it would otherwise have a type of except_clause */
2688 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2689 if (orelse == NULL)
2690 return NULL;
2691 n_except--;
2692 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693
Neal Norwitzf599f422005-12-17 21:33:47 +00002694 finally = ast_for_suite(c, CHILD(n, nch - 1));
2695 if (finally == NULL)
2696 return NULL;
2697 n_except--;
2698 }
2699 else {
2700 /* we can assume it's an "else",
2701 otherwise it would have a type of except_clause */
2702 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2703 if (orelse == NULL)
2704 return NULL;
2705 n_except--;
2706 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002708 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002709 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 return NULL;
2711 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002712
2713 if (n_except > 0) {
2714 int i;
2715 stmt_ty except_st;
2716 /* process except statements to create a try ... except */
2717 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2718 if (handlers == NULL)
2719 return NULL;
2720
2721 for (i = 0; i < n_except; i++) {
2722 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2723 CHILD(n, 5 + i * 3));
2724 if (!e)
2725 return NULL;
2726 asdl_seq_SET(handlers, i, e);
2727 }
2728
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002729 except_st = TryExcept(body, handlers, orelse, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002730 if (!finally)
2731 return except_st;
2732
2733 /* if a 'finally' is present too, we nest the TryExcept within a
2734 TryFinally to emulate try ... except ... finally */
2735 body = asdl_seq_new(1, c->c_arena);
2736 if (body == NULL)
2737 return NULL;
2738 asdl_seq_SET(body, 0, except_st);
2739 }
2740
2741 /* must be a try ... finally (except clauses are in body, if any exist) */
2742 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002743 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744}
2745
Guido van Rossumc2e20742006-02-27 22:32:47 +00002746static expr_ty
2747ast_for_with_var(struct compiling *c, const node *n)
2748{
2749 REQ(n, with_var);
2750 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2751 ast_error(n, "expected \"with [expr] as [var]\"");
2752 return NULL;
2753 }
2754 return ast_for_expr(c, CHILD(n, 1));
2755}
2756
2757/* with_stmt: 'with' test [ with_var ] ':' suite */
2758static stmt_ty
2759ast_for_with_stmt(struct compiling *c, const node *n)
2760{
2761 expr_ty context_expr, optional_vars = NULL;
2762 int suite_index = 3; /* skip 'with', test, and ':' */
2763 asdl_seq *suite_seq;
2764
2765 assert(TYPE(n) == with_stmt);
2766 context_expr = ast_for_expr(c, CHILD(n, 1));
2767 if (TYPE(CHILD(n, 2)) == with_var) {
2768 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2769
2770 if (!optional_vars) {
2771 return NULL;
2772 }
2773 if (!set_context(optional_vars, Store, n)) {
2774 return NULL;
2775 }
2776 suite_index = 4;
2777 }
2778
2779 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2780 if (!suite_seq) {
2781 return NULL;
2782 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002783 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2784 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002785}
2786
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787static stmt_ty
2788ast_for_classdef(struct compiling *c, const node *n)
2789{
2790 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 asdl_seq *bases, *s;
2792
2793 REQ(n, classdef);
2794
2795 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2796 ast_error(n, "assignment to None");
2797 return NULL;
2798 }
2799
2800 if (NCH(n) == 4) {
2801 s = ast_for_suite(c, CHILD(n, 3));
2802 if (!s)
2803 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002804 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002805 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 }
2807 /* check for empty base list */
2808 if (TYPE(CHILD(n,3)) == RPAR) {
2809 s = ast_for_suite(c, CHILD(n,5));
2810 if (!s)
2811 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002812 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002813 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 }
2815
2816 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002817 bases = ast_for_class_bases(c, CHILD(n, 3));
2818 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820
2821 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002822 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002824 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002825 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826}
2827
2828static stmt_ty
2829ast_for_stmt(struct compiling *c, const node *n)
2830{
2831 if (TYPE(n) == stmt) {
2832 assert(NCH(n) == 1);
2833 n = CHILD(n, 0);
2834 }
2835 if (TYPE(n) == simple_stmt) {
2836 assert(num_stmts(n) == 1);
2837 n = CHILD(n, 0);
2838 }
2839 if (TYPE(n) == small_stmt) {
2840 REQ(n, small_stmt);
2841 n = CHILD(n, 0);
2842 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2843 | flow_stmt | import_stmt | global_stmt | exec_stmt
2844 | assert_stmt
2845 */
2846 switch (TYPE(n)) {
2847 case expr_stmt:
2848 return ast_for_expr_stmt(c, n);
2849 case print_stmt:
2850 return ast_for_print_stmt(c, n);
2851 case del_stmt:
2852 return ast_for_del_stmt(c, n);
2853 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002854 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 case flow_stmt:
2856 return ast_for_flow_stmt(c, n);
2857 case import_stmt:
2858 return ast_for_import_stmt(c, n);
2859 case global_stmt:
2860 return ast_for_global_stmt(c, n);
2861 case exec_stmt:
2862 return ast_for_exec_stmt(c, n);
2863 case assert_stmt:
2864 return ast_for_assert_stmt(c, n);
2865 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002866 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2868 TYPE(n), NCH(n));
2869 return NULL;
2870 }
2871 }
2872 else {
2873 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2874 | funcdef | classdef
2875 */
2876 node *ch = CHILD(n, 0);
2877 REQ(n, compound_stmt);
2878 switch (TYPE(ch)) {
2879 case if_stmt:
2880 return ast_for_if_stmt(c, ch);
2881 case while_stmt:
2882 return ast_for_while_stmt(c, ch);
2883 case for_stmt:
2884 return ast_for_for_stmt(c, ch);
2885 case try_stmt:
2886 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002887 case with_stmt:
2888 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 case funcdef:
2890 return ast_for_funcdef(c, ch);
2891 case classdef:
2892 return ast_for_classdef(c, ch);
2893 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002894 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2896 TYPE(n), NCH(n));
2897 return NULL;
2898 }
2899 }
2900}
2901
2902static PyObject *
2903parsenumber(const char *s)
2904{
2905 const char *end;
2906 long x;
2907 double dx;
2908#ifndef WITHOUT_COMPLEX
2909 Py_complex c;
2910 int imflag;
2911#endif
2912
2913 errno = 0;
2914 end = s + strlen(s) - 1;
2915#ifndef WITHOUT_COMPLEX
2916 imflag = *end == 'j' || *end == 'J';
2917#endif
2918 if (*end == 'l' || *end == 'L')
2919 return PyLong_FromString((char *)s, (char **)0, 0);
2920 if (s[0] == '0') {
2921 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2922 if (x < 0 && errno == 0) {
2923 return PyLong_FromString((char *)s,
2924 (char **)0,
2925 0);
2926 }
2927 }
2928 else
2929 x = PyOS_strtol((char *)s, (char **)&end, 0);
2930 if (*end == '\0') {
2931 if (errno != 0)
2932 return PyLong_FromString((char *)s, (char **)0, 0);
2933 return PyInt_FromLong(x);
2934 }
2935 /* XXX Huge floats may silently fail */
2936#ifndef WITHOUT_COMPLEX
2937 if (imflag) {
2938 c.real = 0.;
2939 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002940 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002941 PyFPE_END_PROTECT(c)
2942 return PyComplex_FromCComplex(c);
2943 }
2944 else
2945#endif
2946 {
2947 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002948 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 PyFPE_END_PROTECT(dx)
2950 return PyFloat_FromDouble(dx);
2951 }
2952}
2953
2954static PyObject *
2955decode_utf8(const char **sPtr, const char *end, char* encoding)
2956{
2957#ifndef Py_USING_UNICODE
2958 Py_FatalError("decode_utf8 should not be called in this build.");
2959 return NULL;
2960#else
2961 PyObject *u, *v;
2962 char *s, *t;
2963 t = s = (char *)*sPtr;
2964 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2965 while (s < end && (*s & 0x80)) s++;
2966 *sPtr = s;
2967 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2968 if (u == NULL)
2969 return NULL;
2970 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2971 Py_DECREF(u);
2972 return v;
2973#endif
2974}
2975
2976static PyObject *
2977decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2978{
2979 PyObject *v, *u;
2980 char *buf;
2981 char *p;
2982 const char *end;
2983 if (encoding == NULL) {
2984 buf = (char *)s;
2985 u = NULL;
2986 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2987 buf = (char *)s;
2988 u = NULL;
2989 } else {
2990 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2991 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2992 if (u == NULL)
2993 return NULL;
2994 p = buf = PyString_AsString(u);
2995 end = s + len;
2996 while (s < end) {
2997 if (*s == '\\') {
2998 *p++ = *s++;
2999 if (*s & 0x80) {
3000 strcpy(p, "u005c");
3001 p += 5;
3002 }
3003 }
3004 if (*s & 0x80) { /* XXX inefficient */
3005 PyObject *w;
3006 char *r;
3007 int rn, i;
3008 w = decode_utf8(&s, end, "utf-16-be");
3009 if (w == NULL) {
3010 Py_DECREF(u);
3011 return NULL;
3012 }
3013 r = PyString_AsString(w);
3014 rn = PyString_Size(w);
3015 assert(rn % 2 == 0);
3016 for (i = 0; i < rn; i += 2) {
3017 sprintf(p, "\\u%02x%02x",
3018 r[i + 0] & 0xFF,
3019 r[i + 1] & 0xFF);
3020 p += 6;
3021 }
3022 Py_DECREF(w);
3023 } else {
3024 *p++ = *s++;
3025 }
3026 }
3027 len = p - buf;
3028 s = buf;
3029 }
3030 if (rawmode)
3031 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3032 else
3033 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3034 Py_XDECREF(u);
3035 return v;
3036}
3037
3038/* s is a Python string literal, including the bracketing quote characters,
3039 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3040 * parsestr parses it, and returns the decoded Python string object.
3041 */
3042static PyObject *
3043parsestr(const char *s, const char *encoding)
3044{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003046 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047 int rawmode = 0;
3048 int need_encoding;
3049 int unicode = 0;
3050
3051 if (isalpha(quote) || quote == '_') {
3052 if (quote == 'u' || quote == 'U') {
3053 quote = *++s;
3054 unicode = 1;
3055 }
3056 if (quote == 'r' || quote == 'R') {
3057 quote = *++s;
3058 rawmode = 1;
3059 }
3060 }
3061 if (quote != '\'' && quote != '\"') {
3062 PyErr_BadInternalCall();
3063 return NULL;
3064 }
3065 s++;
3066 len = strlen(s);
3067 if (len > INT_MAX) {
3068 PyErr_SetString(PyExc_OverflowError,
3069 "string to parse is too long");
3070 return NULL;
3071 }
3072 if (s[--len] != quote) {
3073 PyErr_BadInternalCall();
3074 return NULL;
3075 }
3076 if (len >= 4 && s[0] == quote && s[1] == quote) {
3077 s += 2;
3078 len -= 2;
3079 if (s[--len] != quote || s[--len] != quote) {
3080 PyErr_BadInternalCall();
3081 return NULL;
3082 }
3083 }
3084#ifdef Py_USING_UNICODE
3085 if (unicode || Py_UnicodeFlag) {
3086 return decode_unicode(s, len, rawmode, encoding);
3087 }
3088#endif
3089 need_encoding = (encoding != NULL &&
3090 strcmp(encoding, "utf-8") != 0 &&
3091 strcmp(encoding, "iso-8859-1") != 0);
3092 if (rawmode || strchr(s, '\\') == NULL) {
3093 if (need_encoding) {
3094#ifndef Py_USING_UNICODE
3095 /* This should not happen - we never see any other
3096 encoding. */
3097 Py_FatalError("cannot deal with encodings in this build.");
3098#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003099 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100 if (u == NULL)
3101 return NULL;
3102 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3103 Py_DECREF(u);
3104 return v;
3105#endif
3106 } else {
3107 return PyString_FromStringAndSize(s, len);
3108 }
3109 }
3110
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003111 return PyString_DecodeEscape(s, len, NULL, unicode,
3112 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113}
3114
3115/* Build a Python string object out of a STRING atom. This takes care of
3116 * compile-time literal catenation, calling parsestr() on each piece, and
3117 * pasting the intermediate results together.
3118 */
3119static PyObject *
3120parsestrplus(struct compiling *c, const node *n)
3121{
3122 PyObject *v;
3123 int i;
3124 REQ(CHILD(n, 0), STRING);
3125 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3126 /* String literal concatenation */
3127 for (i = 1; i < NCH(n); i++) {
3128 PyObject *s;
3129 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3130 if (s == NULL)
3131 goto onError;
3132 if (PyString_Check(v) && PyString_Check(s)) {
3133 PyString_ConcatAndDel(&v, s);
3134 if (v == NULL)
3135 goto onError;
3136 }
3137#ifdef Py_USING_UNICODE
3138 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003139 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 Py_DECREF(v);
3142 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003143 if (v == NULL)
3144 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 }
3146#endif
3147 }
3148 }
3149 return v;
3150
3151 onError:
3152 Py_XDECREF(v);
3153 return NULL;
3154}