blob: 9180fd0d693fe176e0894fa8e37a6d4b40f1acdc [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 *);
Martin v. Löwis28457502006-04-11 09:17:27 +000034static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Georg Brandl7784f122006-05-26 20:04:44 +0000110 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Neal Norwitze98ccf62006-03-23 05:39:47 +0000194 if (TYPE(n) == encoding_decl) {
195 ast_error(n, "encoding declaration in Unicode string");
196 goto error;
197 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 } else if (TYPE(n) == encoding_decl) {
199 c.c_encoding = STR(n);
200 n = CHILD(n, 0);
201 } else {
202 c.c_encoding = NULL;
203 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000204 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Jeremy Hyltona8293132006-02-28 17:58:27 +0000206 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 switch (TYPE(n)) {
208 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000209 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 if (!stmts)
211 return NULL;
212 for (i = 0; i < NCH(n) - 1; i++) {
213 ch = CHILD(n, i);
214 if (TYPE(ch) == NEWLINE)
215 continue;
216 REQ(ch, stmt);
217 num = num_stmts(ch);
218 if (num == 1) {
219 s = ast_for_stmt(&c, ch);
220 if (!s)
221 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000222 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223 }
224 else {
225 ch = CHILD(ch, 0);
226 REQ(ch, simple_stmt);
227 for (j = 0; j < num; j++) {
228 s = ast_for_stmt(&c, CHILD(ch, j * 2));
229 if (!s)
230 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000231 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 }
233 }
234 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000235 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 case eval_input: {
237 expr_ty testlist_ast;
238
239 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000240 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241 if (!testlist_ast)
242 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 }
245 case single_input:
246 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 if (!stmts)
249 goto error;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
251 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 }
254 else {
255 n = CHILD(n, 0);
256 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000257 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 if (!stmts)
259 goto error;
260 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000261 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262 if (!s)
263 goto error;
264 asdl_seq_SET(stmts, 0, s);
265 }
266 else {
267 /* Only a simple_stmt can contain multiple statements. */
268 REQ(n, simple_stmt);
269 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 if (TYPE(CHILD(n, i)) == NEWLINE)
271 break;
272 s = ast_for_stmt(&c, CHILD(n, i));
273 if (!s)
274 goto error;
275 asdl_seq_SET(stmts, i / 2, s);
276 }
277 }
278
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000279 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 }
281 default:
282 goto error;
283 }
284 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 ast_error_finish(filename);
286 return NULL;
287}
288
289/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
290*/
291
292static operator_ty
293get_operator(const node *n)
294{
295 switch (TYPE(n)) {
296 case VBAR:
297 return BitOr;
298 case CIRCUMFLEX:
299 return BitXor;
300 case AMPER:
301 return BitAnd;
302 case LEFTSHIFT:
303 return LShift;
304 case RIGHTSHIFT:
305 return RShift;
306 case PLUS:
307 return Add;
308 case MINUS:
309 return Sub;
310 case STAR:
311 return Mult;
312 case SLASH:
313 return Div;
314 case DOUBLESLASH:
315 return FloorDiv;
316 case PERCENT:
317 return Mod;
318 default:
Martin v. Löwis28457502006-04-11 09:17:27 +0000319 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 }
321}
322
Jeremy Hyltona8293132006-02-28 17:58:27 +0000323/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324
325 Only sets context for expr kinds that "can appear in assignment context"
326 (according to ../Parser/Python.asdl). For other expr kinds, it sets
327 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328*/
329
330static int
331set_context(expr_ty e, expr_context_ty ctx, const node *n)
332{
333 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334 /* If a particular expression type can't be used for assign / delete,
335 set expr_name to its name and an error message will be generated.
336 */
337 const char* expr_name = NULL;
338
339 /* The ast defines augmented store and load contexts, but the
340 implementation here doesn't actually use them. The code may be
341 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000342 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000343 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000344 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345 */
346 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
348 switch (e->kind) {
349 case Attribute_kind:
350 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000351 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 return ast_error(n, "assignment to None");
353 }
354 e->v.Attribute.ctx = ctx;
355 break;
356 case Subscript_kind:
357 e->v.Subscript.ctx = ctx;
358 break;
359 case Name_kind:
360 if (ctx == Store &&
361 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
362 return ast_error(n, "assignment to None");
363 }
364 e->v.Name.ctx = ctx;
365 break;
366 case List_kind:
367 e->v.List.ctx = ctx;
368 s = e->v.List.elts;
369 break;
370 case Tuple_kind:
371 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
372 return ast_error(n, "can't assign to ()");
373 e->v.Tuple.ctx = ctx;
374 s = e->v.Tuple.elts;
375 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 case Lambda_kind:
377 expr_name = "lambda";
378 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000380 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000384 case UnaryOp_kind:
385 expr_name = "operator";
386 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000388 expr_name = "generator expression";
389 break;
390 case ListComp_kind:
391 expr_name = "list comprehension";
392 break;
393 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394 case Num_kind:
395 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000396 expr_name = "literal";
397 break;
398 case Compare_kind:
399 expr_name = "comparison";
400 break;
401 case Repr_kind:
402 expr_name = "repr";
403 break;
Neal Norwitz373f0a72006-05-15 07:04:36 +0000404 case IfExp_kind:
405 expr_name = "conditional expression";
406 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000407 default:
408 PyErr_Format(PyExc_SystemError,
409 "unexpected expression in assignment %d (line %d)",
410 e->kind, e->lineno);
411 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000413 /* Check for error string set by switch */
414 if (expr_name) {
415 char buf[300];
416 PyOS_snprintf(buf, sizeof(buf),
417 "can't %s %s",
418 ctx == Store ? "assign to" : "delete",
419 expr_name);
420 return ast_error(n, buf);
421 }
422
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000424 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425 */
426 if (s) {
427 int i;
428
429 for (i = 0; i < asdl_seq_LEN(s); i++) {
Martin v. Löwis28457502006-04-11 09:17:27 +0000430 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431 return 0;
432 }
433 }
434 return 1;
435}
436
437static operator_ty
438ast_for_augassign(const node *n)
439{
440 REQ(n, augassign);
441 n = CHILD(n, 0);
442 switch (STR(n)[0]) {
443 case '+':
444 return Add;
445 case '-':
446 return Sub;
447 case '/':
448 if (STR(n)[1] == '/')
449 return FloorDiv;
450 else
451 return Div;
452 case '%':
453 return Mod;
454 case '<':
455 return LShift;
456 case '>':
457 return RShift;
458 case '&':
459 return BitAnd;
460 case '^':
461 return BitXor;
462 case '|':
463 return BitOr;
464 case '*':
465 if (STR(n)[1] == '*')
466 return Pow;
467 else
468 return Mult;
469 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000470 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000471 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472 }
473}
474
475static cmpop_ty
476ast_for_comp_op(const node *n)
477{
478 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
479 |'is' 'not'
480 */
481 REQ(n, comp_op);
482 if (NCH(n) == 1) {
483 n = CHILD(n, 0);
484 switch (TYPE(n)) {
485 case LESS:
486 return Lt;
487 case GREATER:
488 return Gt;
489 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 return Eq;
491 case LESSEQUAL:
492 return LtE;
493 case GREATEREQUAL:
494 return GtE;
495 case NOTEQUAL:
496 return NotEq;
497 case NAME:
498 if (strcmp(STR(n), "in") == 0)
499 return In;
500 if (strcmp(STR(n), "is") == 0)
501 return Is;
502 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000503 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000505 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 }
507 }
508 else if (NCH(n) == 2) {
509 /* handle "not in" and "is not" */
510 switch (TYPE(CHILD(n, 0))) {
511 case NAME:
512 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
513 return NotIn;
514 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
515 return IsNot;
516 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000517 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Martin v. Löwis28457502006-04-11 09:17:27 +0000519 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 }
521 }
Neal Norwitz79792652005-11-14 04:25:03 +0000522 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000524 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525}
526
527static asdl_seq *
528seq_for_testlist(struct compiling *c, const node *n)
529{
530 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000531 asdl_seq *seq;
532 expr_ty expression;
533 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534 assert(TYPE(n) == testlist
535 || TYPE(n) == listmaker
536 || TYPE(n) == testlist_gexp
537 || TYPE(n) == testlist_safe
538 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000540 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 if (!seq)
542 return NULL;
543
544 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000545 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546
547 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000548 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
551 assert(i / 2 < seq->size);
552 asdl_seq_SET(seq, i / 2, expression);
553 }
554 return seq;
555}
556
557static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000558compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559{
560 int i, len = (NCH(n) + 1) / 2;
561 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000562 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 if (!args)
564 return NULL;
565
566 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 for (i = 0; i < len; i++) {
568 const node *child = CHILD(CHILD(n, 2*i), 0);
569 expr_ty arg;
570 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000571 if (!strcmp(STR(child), "None")) {
572 ast_error(child, "assignment to None");
573 return NULL;
574 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000575 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
576 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000577 }
578 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000579 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 asdl_seq_SET(args, i, arg);
582 }
583
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000584 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000585 if (!set_context(result, Store, n))
586 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587 return result;
588}
589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Jeremy Hyltona8293132006-02-28 17:58:27 +0000591/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592
593static arguments_ty
594ast_for_arguments(struct compiling *c, const node *n)
595{
596 /* parameters: '(' [varargslist] ')'
597 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
598 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
599 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000600 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 asdl_seq *args, *defaults;
602 identifier vararg = NULL, kwarg = NULL;
603 node *ch;
604
605 if (TYPE(n) == parameters) {
606 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000607 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 n = CHILD(n, 1);
609 }
610 REQ(n, varargslist);
611
612 /* first count the number of normal args & defaults */
613 for (i = 0; i < NCH(n); i++) {
614 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000615 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617 if (TYPE(ch) == EQUAL)
618 n_defaults++;
619 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000620 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 if (!args && n_args)
622 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000623 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 if (!defaults && n_defaults)
625 goto error;
626
627 /* fpdef: NAME | '(' fplist ')'
628 fplist: fpdef (',' fpdef)* [',']
629 */
630 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000631 j = 0; /* index for defaults */
632 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 while (i < NCH(n)) {
634 ch = CHILD(n, i);
635 switch (TYPE(ch)) {
636 case fpdef:
637 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
638 anything other than EQUAL or a comma? */
639 /* XXX Should NCH(n) check be made a separate check? */
640 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000641 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
642 if (!expression)
643 goto error;
644 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 i += 2;
646 found_default = 1;
647 }
648 else if (found_default) {
649 ast_error(n,
650 "non-default argument follows default argument");
651 goto error;
652 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000654 ch = CHILD(ch, 1);
655 /* def foo((x)): is not complex, special case. */
656 if (NCH(ch) != 1) {
657 /* We have complex arguments, setup for unpacking. */
658 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
659 } else {
660 /* def foo((x)): setup for checking NAME below. */
661 ch = CHILD(ch, 0);
662 }
663 }
664 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000665 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
667 ast_error(CHILD(ch, 0), "assignment to None");
668 goto error;
669 }
Armin Rigo31441302005-10-21 12:57:31 +0000670 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000671 Param, LINENO(ch), ch->n_col_offset,
672 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 if (!name)
674 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000675 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676
677 }
678 i += 2; /* the name and the comma */
679 break;
680 case STAR:
681 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
682 ast_error(CHILD(n, i+1), "assignment to None");
683 goto error;
684 }
685 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
686 i += 3;
687 break;
688 case DOUBLESTAR:
689 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
690 ast_error(CHILD(n, i+1), "assignment to None");
691 goto error;
692 }
693 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
694 i += 3;
695 break;
696 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000697 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698 "unexpected node in varargslist: %d @ %d",
699 TYPE(ch), i);
700 goto error;
701 }
702 }
703
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000704 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705
706 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000707 Py_XDECREF(vararg);
708 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709 return NULL;
710}
711
712static expr_ty
713ast_for_dotted_name(struct compiling *c, const node *n)
714{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000715 expr_ty e;
716 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000717 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 int i;
719
720 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000721
722 lineno = LINENO(n);
723 col_offset = n->n_col_offset;
724
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725 id = NEW_IDENTIFIER(CHILD(n, 0));
726 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000727 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000728 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000730 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731
732 for (i = 2; i < NCH(n); i+=2) {
733 id = NEW_IDENTIFIER(CHILD(n, i));
734 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000735 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000736 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000737 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 }
740
741 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742}
743
744static expr_ty
745ast_for_decorator(struct compiling *c, const node *n)
746{
747 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
748 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000749 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750
751 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000752 REQ(CHILD(n, 0), AT);
753 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
755 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
756 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000757 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
759 if (NCH(n) == 3) { /* No arguments */
760 d = name_expr;
761 name_expr = NULL;
762 }
763 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000764 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
765 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000767 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 name_expr = NULL;
769 }
770 else {
771 d = ast_for_call(c, CHILD(n, 3), name_expr);
772 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000773 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 name_expr = NULL;
775 }
776
777 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778}
779
780static asdl_seq*
781ast_for_decorators(struct compiling *c, const node *n)
782{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000783 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000784 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 int i;
786
787 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000788 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 if (!decorator_seq)
790 return NULL;
791
792 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000793 d = ast_for_decorator(c, CHILD(n, i));
794 if (!d)
795 return NULL;
796 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 }
798 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799}
800
801static stmt_ty
802ast_for_funcdef(struct compiling *c, const node *n)
803{
804 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000805 identifier name;
806 arguments_ty args;
807 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 asdl_seq *decorator_seq = NULL;
809 int name_i;
810
811 REQ(n, funcdef);
812
813 if (NCH(n) == 6) { /* decorators are present */
814 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
815 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 name_i = 2;
818 }
819 else {
820 name_i = 1;
821 }
822
823 name = NEW_IDENTIFIER(CHILD(n, name_i));
824 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000825 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000827 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000828 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 }
830 args = ast_for_arguments(c, CHILD(n, name_i + 1));
831 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000832 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 body = ast_for_suite(c, CHILD(n, name_i + 3));
834 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000835 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000837 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
838 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839}
840
841static expr_ty
842ast_for_lambdef(struct compiling *c, const node *n)
843{
844 /* lambdef: 'lambda' [varargslist] ':' test */
845 arguments_ty args;
846 expr_ty expression;
847
848 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000849 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 if (!args)
851 return NULL;
852 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000853 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 }
856 else {
857 args = ast_for_arguments(c, CHILD(n, 1));
858 if (!args)
859 return NULL;
860 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000861 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 }
864
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000865 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866}
867
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000868static expr_ty
869ast_for_ifexpr(struct compiling *c, const node *n)
870{
871 /* test: or_test 'if' or_test 'else' test */
872 expr_ty expression, body, orelse;
873
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000874 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000875 body = ast_for_expr(c, CHILD(n, 0));
876 if (!body)
877 return NULL;
878 expression = ast_for_expr(c, CHILD(n, 2));
879 if (!expression)
880 return NULL;
881 orelse = ast_for_expr(c, CHILD(n, 4));
882 if (!orelse)
883 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000884 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
885 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000886}
887
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888/* Count the number of 'for' loop in a list comprehension.
889
890 Helper for ast_for_listcomp().
891*/
892
893static int
894count_list_fors(const node *n)
895{
896 int n_fors = 0;
897 node *ch = CHILD(n, 1);
898
899 count_list_for:
900 n_fors++;
901 REQ(ch, list_for);
902 if (NCH(ch) == 5)
903 ch = CHILD(ch, 4);
904 else
905 return n_fors;
906 count_list_iter:
907 REQ(ch, list_iter);
908 ch = CHILD(ch, 0);
909 if (TYPE(ch) == list_for)
910 goto count_list_for;
911 else if (TYPE(ch) == list_if) {
912 if (NCH(ch) == 3) {
913 ch = CHILD(ch, 2);
914 goto count_list_iter;
915 }
916 else
917 return n_fors;
918 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000919
920 /* Should never be reached */
921 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
922 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923}
924
925/* Count the number of 'if' statements in a list comprehension.
926
927 Helper for ast_for_listcomp().
928*/
929
930static int
931count_list_ifs(const node *n)
932{
933 int n_ifs = 0;
934
935 count_list_iter:
936 REQ(n, list_iter);
937 if (TYPE(CHILD(n, 0)) == list_for)
938 return n_ifs;
939 n = CHILD(n, 0);
940 REQ(n, list_if);
941 n_ifs++;
942 if (NCH(n) == 2)
943 return n_ifs;
944 n = CHILD(n, 2);
945 goto count_list_iter;
946}
947
948static expr_ty
949ast_for_listcomp(struct compiling *c, const node *n)
950{
951 /* listmaker: test ( list_for | (',' test)* [','] )
952 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
953 list_iter: list_for | list_if
954 list_if: 'if' test [list_iter]
955 testlist_safe: test [(',' test)+ [',']]
956 */
957 expr_ty elt;
958 asdl_seq *listcomps;
959 int i, n_fors;
960 node *ch;
961
962 REQ(n, listmaker);
963 assert(NCH(n) > 1);
964
965 elt = ast_for_expr(c, CHILD(n, 0));
966 if (!elt)
967 return NULL;
968
969 n_fors = count_list_fors(n);
970 if (n_fors == -1)
971 return NULL;
972
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000973 listcomps = asdl_seq_new(n_fors, c->c_arena);
974 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 ch = CHILD(n, 1);
978 for (i = 0; i < n_fors; i++) {
979 comprehension_ty lc;
980 asdl_seq *t;
981 expr_ty expression;
982
983 REQ(ch, list_for);
984
985 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000986 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000988 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000989 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000992 if (asdl_seq_LEN(t) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +0000993 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000994 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000996 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
997 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000998 expression, NULL, c->c_arena);
999 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001
1002 if (NCH(ch) == 5) {
1003 int j, n_ifs;
1004 asdl_seq *ifs;
1005
1006 ch = CHILD(ch, 4);
1007 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001008 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001011 ifs = asdl_seq_new(n_ifs, c->c_arena);
1012 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014
1015 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001016 REQ(ch, list_iter);
1017 ch = CHILD(ch, 0);
1018 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019
Jeremy Hyltona8293132006-02-28 17:58:27 +00001020 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1021 if (NCH(ch) == 3)
1022 ch = CHILD(ch, 2);
1023 }
1024 /* on exit, must guarantee that ch is a list_for */
1025 if (TYPE(ch) == list_iter)
1026 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001028 }
1029 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 }
1031
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001032 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033}
1034
1035/*
1036 Count the number of 'for' loops in a generator expression.
1037
1038 Helper for ast_for_genexp().
1039*/
1040
1041static int
1042count_gen_fors(const node *n)
1043{
1044 int n_fors = 0;
1045 node *ch = CHILD(n, 1);
1046
1047 count_gen_for:
1048 n_fors++;
1049 REQ(ch, gen_for);
1050 if (NCH(ch) == 5)
1051 ch = CHILD(ch, 4);
1052 else
1053 return n_fors;
1054 count_gen_iter:
1055 REQ(ch, gen_iter);
1056 ch = CHILD(ch, 0);
1057 if (TYPE(ch) == gen_for)
1058 goto count_gen_for;
1059 else if (TYPE(ch) == gen_if) {
1060 if (NCH(ch) == 3) {
1061 ch = CHILD(ch, 2);
1062 goto count_gen_iter;
1063 }
1064 else
1065 return n_fors;
1066 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001067
1068 /* Should never be reached */
1069 PyErr_SetString(PyExc_SystemError,
1070 "logic error in count_gen_fors");
1071 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072}
1073
1074/* Count the number of 'if' statements in a generator expression.
1075
1076 Helper for ast_for_genexp().
1077*/
1078
1079static int
1080count_gen_ifs(const node *n)
1081{
1082 int n_ifs = 0;
1083
1084 while (1) {
1085 REQ(n, gen_iter);
1086 if (TYPE(CHILD(n, 0)) == gen_for)
1087 return n_ifs;
1088 n = CHILD(n, 0);
1089 REQ(n, gen_if);
1090 n_ifs++;
1091 if (NCH(n) == 2)
1092 return n_ifs;
1093 n = CHILD(n, 2);
1094 }
1095}
1096
Jeremy Hyltona8293132006-02-28 17:58:27 +00001097/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098static expr_ty
1099ast_for_genexp(struct compiling *c, const node *n)
1100{
1101 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1102 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1103 expr_ty elt;
1104 asdl_seq *genexps;
1105 int i, n_fors;
1106 node *ch;
1107
1108 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1109 assert(NCH(n) > 1);
1110
1111 elt = ast_for_expr(c, CHILD(n, 0));
1112 if (!elt)
1113 return NULL;
1114
1115 n_fors = count_gen_fors(n);
1116 if (n_fors == -1)
1117 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001118
1119 genexps = asdl_seq_new(n_fors, c->c_arena);
1120 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 ch = CHILD(n, 1);
1124 for (i = 0; i < n_fors; i++) {
1125 comprehension_ty ge;
1126 asdl_seq *t;
1127 expr_ty expression;
1128
1129 REQ(ch, gen_for);
1130
1131 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001132 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001134 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001135 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001137
1138 if (asdl_seq_LEN(t) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001139 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001140 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001142 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1143 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001144 expression, NULL, c->c_arena);
1145
1146 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 if (NCH(ch) == 5) {
1150 int j, n_ifs;
1151 asdl_seq *ifs;
1152
1153 ch = CHILD(ch, 4);
1154 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001155 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001157
1158 ifs = asdl_seq_new(n_ifs, c->c_arena);
1159 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001161
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 for (j = 0; j < n_ifs; j++) {
1163 REQ(ch, gen_iter);
1164 ch = CHILD(ch, 0);
1165 REQ(ch, gen_if);
1166
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001167 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001168 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001169 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001170 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 if (NCH(ch) == 3)
1172 ch = CHILD(ch, 2);
1173 }
1174 /* on exit, must guarantee that ch is a gen_for */
1175 if (TYPE(ch) == gen_iter)
1176 ch = CHILD(ch, 0);
1177 ge->ifs = ifs;
1178 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001179 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 }
1181
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001182 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183}
1184
1185static expr_ty
1186ast_for_atom(struct compiling *c, const node *n)
1187{
1188 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1189 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1190 */
1191 node *ch = CHILD(n, 0);
1192
1193 switch (TYPE(ch)) {
1194 case NAME:
1195 /* All names start in Load context, but may later be
1196 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001197 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 case STRING: {
1199 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 if (!str)
1201 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001202
1203 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001204 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 }
1206 case NUMBER: {
1207 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 if (!pynum)
1209 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001210
1211 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001212 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 }
1214 case LPAR: /* some parenthesized expressions */
1215 ch = CHILD(n, 1);
1216
1217 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001218 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219
1220 if (TYPE(ch) == yield_expr)
1221 return ast_for_expr(c, ch);
1222
1223 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1224 return ast_for_genexp(c, ch);
1225
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001226 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 case LSQB: /* list (or list comprehension) */
1228 ch = CHILD(n, 1);
1229
1230 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001231 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232
1233 REQ(ch, listmaker);
1234 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1235 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 if (!elts)
1237 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001238
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001239 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 }
1241 else
1242 return ast_for_listcomp(c, ch);
1243 case LBRACE: {
1244 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1245 int i, size;
1246 asdl_seq *keys, *values;
1247
1248 ch = CHILD(n, 1);
1249 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001250 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 if (!keys)
1252 return NULL;
1253
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001254 values = asdl_seq_new(size, c->c_arena);
1255 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257
1258 for (i = 0; i < NCH(ch); i += 4) {
1259 expr_ty expression;
1260
1261 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001262 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001264
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001266
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001268 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001270
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 asdl_seq_SET(values, i / 4, expression);
1272 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001273 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 }
1275 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001276 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 if (!expression)
1278 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001279
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001280 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 }
1282 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001283 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 return NULL;
1285 }
1286}
1287
1288static slice_ty
1289ast_for_slice(struct compiling *c, const node *n)
1290{
1291 node *ch;
1292 expr_ty lower = NULL, upper = NULL, step = NULL;
1293
1294 REQ(n, subscript);
1295
1296 /*
1297 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1298 sliceop: ':' [test]
1299 */
1300 ch = CHILD(n, 0);
1301 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001302 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303
1304 if (NCH(n) == 1 && TYPE(ch) == test) {
1305 /* 'step' variable hold no significance in terms of being used over
1306 other vars */
1307 step = ast_for_expr(c, ch);
1308 if (!step)
1309 return NULL;
1310
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001311 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 }
1313
1314 if (TYPE(ch) == test) {
1315 lower = ast_for_expr(c, ch);
1316 if (!lower)
1317 return NULL;
1318 }
1319
1320 /* If there's an upper bound it's in the second or third position. */
1321 if (TYPE(ch) == COLON) {
1322 if (NCH(n) > 1) {
1323 node *n2 = CHILD(n, 1);
1324
1325 if (TYPE(n2) == test) {
1326 upper = ast_for_expr(c, n2);
1327 if (!upper)
1328 return NULL;
1329 }
1330 }
1331 } else if (NCH(n) > 2) {
1332 node *n2 = CHILD(n, 2);
1333
1334 if (TYPE(n2) == test) {
1335 upper = ast_for_expr(c, n2);
1336 if (!upper)
1337 return NULL;
1338 }
1339 }
1340
1341 ch = CHILD(n, NCH(n) - 1);
1342 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001343 if (NCH(ch) == 1) {
1344 /* No expression, so step is None */
1345 ch = CHILD(ch, 0);
1346 step = Name(new_identifier("None", c->c_arena), Load,
1347 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 if (!step)
1349 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001350 } else {
1351 ch = CHILD(ch, 1);
1352 if (TYPE(ch) == test) {
1353 step = ast_for_expr(c, ch);
1354 if (!step)
1355 return NULL;
1356 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 }
1358 }
1359
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001360 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361}
1362
1363static expr_ty
1364ast_for_binop(struct compiling *c, const node *n)
1365{
1366 /* Must account for a sequence of expressions.
1367 How should A op B op C by represented?
1368 BinOp(BinOp(A, op, B), op, C).
1369 */
1370
1371 int i, nops;
1372 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001373 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374
1375 expr1 = ast_for_expr(c, CHILD(n, 0));
1376 if (!expr1)
1377 return NULL;
1378
1379 expr2 = ast_for_expr(c, CHILD(n, 2));
1380 if (!expr2)
1381 return NULL;
1382
Anthony Baxtera863d332006-04-11 07:43:46 +00001383 newoperator = get_operator(CHILD(n, 1));
1384 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 return NULL;
1386
Anthony Baxtera863d332006-04-11 07:43:46 +00001387 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001388 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389 if (!result)
1390 return NULL;
1391
1392 nops = (NCH(n) - 1) / 2;
1393 for (i = 1; i < nops; i++) {
1394 expr_ty tmp_result, tmp;
1395 const node* next_oper = CHILD(n, i * 2 + 1);
1396
Anthony Baxtera863d332006-04-11 07:43:46 +00001397 newoperator = get_operator(next_oper);
1398 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 return NULL;
1400
1401 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1402 if (!tmp)
1403 return NULL;
1404
Anthony Baxtera863d332006-04-11 07:43:46 +00001405 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001406 LINENO(next_oper), next_oper->n_col_offset,
1407 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 if (!tmp)
1409 return NULL;
1410 result = tmp_result;
1411 }
1412 return result;
1413}
1414
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001415static expr_ty
1416ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1417{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001418 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1419 subscriptlist: subscript (',' subscript)* [',']
1420 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1421 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001422 REQ(n, trailer);
1423 if (TYPE(CHILD(n, 0)) == LPAR) {
1424 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001425 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1426 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001427 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001428 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001429 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001430 else if (TYPE(CHILD(n, 0)) == DOT ) {
1431 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001432 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001433 }
1434 else {
1435 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001436 REQ(CHILD(n, 2), RSQB);
1437 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001438 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001439 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1440 if (!slc)
1441 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001442 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1443 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001444 }
1445 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001446 /* The grammar is ambiguous here. The ambiguity is resolved
1447 by treating the sequence as a tuple literal if there are
1448 no slice features.
1449 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001450 int j;
1451 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001452 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001453 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001454 asdl_seq *slices, *elts;
1455 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001456 if (!slices)
1457 return NULL;
1458 for (j = 0; j < NCH(n); j += 2) {
1459 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001460 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001461 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001462 if (slc->kind != Index_kind)
1463 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001464 asdl_seq_SET(slices, j / 2, slc);
1465 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001466 if (!simple) {
1467 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001468 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001469 }
1470 /* extract Index values and put them in a Tuple */
1471 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001472 if (!elts)
1473 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001474 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1475 slc = (slice_ty)asdl_seq_GET(slices, j);
1476 assert(slc->kind == Index_kind && slc->v.Index.value);
1477 asdl_seq_SET(elts, j, slc->v.Index.value);
1478 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001480 if (!e)
1481 return NULL;
1482 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001483 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001484 }
1485 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001486}
1487
1488static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001489ast_for_factor(struct compiling *c, const node *n)
1490{
1491 node *pfactor, *ppower, *patom, *pnum;
1492 expr_ty expression;
1493
1494 /* If the unary - operator is applied to a constant, don't generate
1495 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1496 constant. The peephole optimizer already does something like
1497 this but it doesn't handle the case where the constant is
1498 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1499 PyLongObject.
1500 */
1501 if (TYPE(CHILD(n, 0)) == MINUS
1502 && NCH(n) == 2
1503 && TYPE((pfactor = CHILD(n, 1))) == factor
1504 && NCH(pfactor) == 1
1505 && TYPE((ppower = CHILD(pfactor, 0))) == power
1506 && NCH(ppower) == 1
1507 && TYPE((patom = CHILD(ppower, 0))) == atom
1508 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1509 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1510 if (s == NULL)
1511 return NULL;
1512 s[0] = '-';
1513 strcpy(s + 1, STR(pnum));
1514 PyObject_FREE(STR(pnum));
1515 STR(pnum) = s;
1516 return ast_for_atom(c, patom);
1517 }
1518
1519 expression = ast_for_expr(c, CHILD(n, 1));
1520 if (!expression)
1521 return NULL;
1522
1523 switch (TYPE(CHILD(n, 0))) {
1524 case PLUS:
1525 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1526 c->c_arena);
1527 case MINUS:
1528 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1529 c->c_arena);
1530 case TILDE:
1531 return UnaryOp(Invert, expression, LINENO(n),
1532 n->n_col_offset, c->c_arena);
1533 }
1534 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1535 TYPE(CHILD(n, 0)));
1536 return NULL;
1537}
1538
1539static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001540ast_for_power(struct compiling *c, const node *n)
1541{
1542 /* power: atom trailer* ('**' factor)*
1543 */
1544 int i;
1545 expr_ty e, tmp;
1546 REQ(n, power);
1547 e = ast_for_atom(c, CHILD(n, 0));
1548 if (!e)
1549 return NULL;
1550 if (NCH(n) == 1)
1551 return e;
1552 for (i = 1; i < NCH(n); i++) {
1553 node *ch = CHILD(n, i);
1554 if (TYPE(ch) != trailer)
1555 break;
1556 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001557 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001558 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001559 tmp->lineno = e->lineno;
1560 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001561 e = tmp;
1562 }
1563 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1564 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001565 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001566 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001567 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001568 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001569 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001570 e = tmp;
1571 }
1572 return e;
1573}
1574
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575/* Do not name a variable 'expr'! Will cause a compile error.
1576*/
1577
1578static expr_ty
1579ast_for_expr(struct compiling *c, const node *n)
1580{
1581 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001582 test: or_test ['if' or_test 'else' test] | lambdef
1583 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 and_test: not_test ('and' not_test)*
1585 not_test: 'not' not_test | comparison
1586 comparison: expr (comp_op expr)*
1587 expr: xor_expr ('|' xor_expr)*
1588 xor_expr: and_expr ('^' and_expr)*
1589 and_expr: shift_expr ('&' shift_expr)*
1590 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1591 arith_expr: term (('+'|'-') term)*
1592 term: factor (('*'|'/'|'%'|'//') factor)*
1593 factor: ('+'|'-'|'~') factor | power
1594 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001595
1596 As well as modified versions that exist for backward compatibility,
1597 to explicitly allow:
1598 [ x for x in lambda: 0, lambda: 1 ]
1599 (which would be ambiguous without these extra rules)
1600
1601 old_test: or_test | old_lambdef
1602 old_lambdef: 'lambda' [vararglist] ':' old_test
1603
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604 */
1605
1606 asdl_seq *seq;
1607 int i;
1608
1609 loop:
1610 switch (TYPE(n)) {
1611 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001612 case old_test:
1613 if (TYPE(CHILD(n, 0)) == lambdef ||
1614 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001616 else if (NCH(n) > 1)
1617 return ast_for_ifexpr(c, n);
1618 /* Fallthrough */
1619 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 case and_test:
1621 if (NCH(n) == 1) {
1622 n = CHILD(n, 0);
1623 goto loop;
1624 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001625 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 if (!seq)
1627 return NULL;
1628 for (i = 0; i < NCH(n); i += 2) {
1629 expr_ty e = ast_for_expr(c, CHILD(n, i));
1630 if (!e)
1631 return NULL;
1632 asdl_seq_SET(seq, i / 2, e);
1633 }
1634 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001635 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1636 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001637 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001638 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639 case not_test:
1640 if (NCH(n) == 1) {
1641 n = CHILD(n, 0);
1642 goto loop;
1643 }
1644 else {
1645 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1646 if (!expression)
1647 return NULL;
1648
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001649 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1650 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651 }
1652 case comparison:
1653 if (NCH(n) == 1) {
1654 n = CHILD(n, 0);
1655 goto loop;
1656 }
1657 else {
1658 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001659 asdl_int_seq *ops;
1660 asdl_seq *cmps;
1661 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 if (!ops)
1663 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001664 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666 return NULL;
1667 }
1668 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001669 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670
Anthony Baxtera863d332006-04-11 07:43:46 +00001671 newoperator = ast_for_comp_op(CHILD(n, i));
1672 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001674 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675
1676 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001677 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001679 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001681 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 asdl_seq_SET(cmps, i / 2, expression);
1683 }
1684 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001685 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001687 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001689 return Compare(expression, ops, cmps, LINENO(n),
1690 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 }
1692 break;
1693
1694 /* The next five cases all handle BinOps. The main body of code
1695 is the same in each case, but the switch turned inside out to
1696 reuse the code for each type of operator.
1697 */
1698 case expr:
1699 case xor_expr:
1700 case and_expr:
1701 case shift_expr:
1702 case arith_expr:
1703 case term:
1704 if (NCH(n) == 1) {
1705 n = CHILD(n, 0);
1706 goto loop;
1707 }
1708 return ast_for_binop(c, n);
1709 case yield_expr: {
1710 expr_ty exp = NULL;
1711 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001712 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 if (!exp)
1714 return NULL;
1715 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001716 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001718 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 if (NCH(n) == 1) {
1720 n = CHILD(n, 0);
1721 goto loop;
1722 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001723 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001724 case power:
1725 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001727 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 return NULL;
1729 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001730 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 return NULL;
1732}
1733
1734static expr_ty
1735ast_for_call(struct compiling *c, const node *n, expr_ty func)
1736{
1737 /*
1738 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1739 | '**' test)
1740 argument: [test '='] test [gen_for] # Really [keyword '='] test
1741 */
1742
1743 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001744 asdl_seq *args;
1745 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 expr_ty vararg = NULL, kwarg = NULL;
1747
1748 REQ(n, arglist);
1749
1750 nargs = 0;
1751 nkeywords = 0;
1752 ngens = 0;
1753 for (i = 0; i < NCH(n); i++) {
1754 node *ch = CHILD(n, i);
1755 if (TYPE(ch) == argument) {
1756 if (NCH(ch) == 1)
1757 nargs++;
1758 else if (TYPE(CHILD(ch, 1)) == gen_for)
1759 ngens++;
1760 else
1761 nkeywords++;
1762 }
1763 }
1764 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001765 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 "if not sole argument");
1767 return NULL;
1768 }
1769
1770 if (nargs + nkeywords + ngens > 255) {
1771 ast_error(n, "more than 255 arguments");
1772 return NULL;
1773 }
1774
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001775 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001777 return NULL;
1778 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781 nargs = 0;
1782 nkeywords = 0;
1783 for (i = 0; i < NCH(n); i++) {
1784 node *ch = CHILD(n, i);
1785 if (TYPE(ch) == argument) {
1786 expr_ty e;
1787 if (NCH(ch) == 1) {
Neal Norwitz5ef92242006-05-19 06:43:50 +00001788 if (nkeywords) {
1789 ast_error(CHILD(ch, 0),
1790 "non-keyword arg after keyword arg");
1791 return NULL;
1792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 e = ast_for_expr(c, CHILD(ch, 0));
1794 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001795 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 asdl_seq_SET(args, nargs++, e);
1797 }
1798 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1799 e = ast_for_genexp(c, ch);
1800 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001801 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 asdl_seq_SET(args, nargs++, e);
1803 }
1804 else {
1805 keyword_ty kw;
1806 identifier key;
1807
1808 /* CHILD(ch, 0) is test, but must be an identifier? */
1809 e = ast_for_expr(c, CHILD(ch, 0));
1810 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001811 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812 /* f(lambda x: x[0] = 3) ends up getting parsed with
1813 * LHS test = lambda x: x[0], and RHS test = 3.
1814 * SF bug 132313 points out that complaining about a keyword
1815 * then is very confusing.
1816 */
1817 if (e->kind == Lambda_kind) {
1818 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001819 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 } else if (e->kind != Name_kind) {
1821 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001822 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 }
1824 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 e = ast_for_expr(c, CHILD(ch, 2));
1826 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001827 return NULL;
1828 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 asdl_seq_SET(keywords, nkeywords++, kw);
1832 }
1833 }
1834 else if (TYPE(ch) == STAR) {
1835 vararg = ast_for_expr(c, CHILD(n, i+1));
1836 i++;
1837 }
1838 else if (TYPE(ch) == DOUBLESTAR) {
1839 kwarg = ast_for_expr(c, CHILD(n, i+1));
1840 i++;
1841 }
1842 }
1843
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001844 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845}
1846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001848ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001850 /* testlist_gexp: test (',' test)* [','] */
1851 /* testlist: test (',' test)* [','] */
1852 /* testlist_safe: test (',' test)+ [','] */
1853 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001855 if (TYPE(n) == testlist_gexp) {
1856 if (NCH(n) > 1)
1857 assert(TYPE(CHILD(n, 1)) != gen_for);
1858 }
1859 else {
1860 assert(TYPE(n) == testlist ||
1861 TYPE(n) == testlist_safe ||
1862 TYPE(n) == testlist1);
1863 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 if (NCH(n) == 1)
1865 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 else {
1867 asdl_seq *tmp = seq_for_testlist(c, n);
1868 if (!tmp)
1869 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001870 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001872}
1873
1874static expr_ty
1875ast_for_testlist_gexp(struct compiling *c, const node* n)
1876{
1877 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1878 /* argument: test [ gen_for ] */
1879 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001880 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001881 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001882 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001883}
1884
1885/* like ast_for_testlist() but returns a sequence */
1886static asdl_seq*
1887ast_for_class_bases(struct compiling *c, const node* n)
1888{
1889 /* testlist: test (',' test)* [','] */
1890 assert(NCH(n) > 0);
1891 REQ(n, testlist);
1892 if (NCH(n) == 1) {
1893 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001895 if (!bases)
1896 return NULL;
1897 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001898 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001899 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001900 asdl_seq_SET(bases, 0, base);
1901 return bases;
1902 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001903
1904 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905}
1906
1907static stmt_ty
1908ast_for_expr_stmt(struct compiling *c, const node *n)
1909{
1910 REQ(n, expr_stmt);
1911 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1912 | ('=' (yield_expr|testlist))*)
1913 testlist: test (',' test)* [',']
1914 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1915 | '<<=' | '>>=' | '**=' | '//='
1916 test: ... here starts the operator precendence dance
1917 */
1918
1919 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001920 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 if (!e)
1922 return NULL;
1923
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001924 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 }
1926 else if (TYPE(CHILD(n, 1)) == augassign) {
1927 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00001928 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929 node *ch = CHILD(n, 0);
1930
1931 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001932 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001934 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001935 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936
1937 if (!expr1)
1938 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001939 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001940 switch (expr1->kind) {
1941 case GeneratorExp_kind:
1942 ast_error(ch, "augmented assignment to generator "
1943 "expression not possible");
1944 return NULL;
1945 case Name_kind: {
1946 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1947 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1948 ast_error(ch, "assignment to None");
1949 return NULL;
1950 }
1951 break;
1952 }
1953 case Attribute_kind:
1954 case Subscript_kind:
1955 break;
1956 default:
1957 ast_error(ch, "illegal expression for augmented "
1958 "assignment");
1959 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 }
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001961 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962
1963 ch = CHILD(n, 2);
1964 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001965 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001967 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), ch->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001968 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 return NULL;
1970
Anthony Baxtera863d332006-04-11 07:43:46 +00001971 newoperator = ast_for_augassign(CHILD(n, 1));
1972 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 return NULL;
1974
Anthony Baxtera863d332006-04-11 07:43:46 +00001975 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 }
1977 else {
1978 int i;
1979 asdl_seq *targets;
1980 node *value;
1981 expr_ty expression;
1982
1983 /* a normal assignment */
1984 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001985 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 if (!targets)
1987 return NULL;
1988 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001989 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 node *ch = CHILD(n, i);
1991 if (TYPE(ch) == yield_expr) {
1992 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001995 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996
1997 /* set context to assign */
1998 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001999 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000
Neal Norwitz84456bd2005-12-18 03:16:20 +00002001 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002002 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003
2004 asdl_seq_SET(targets, i / 2, e);
2005 }
2006 value = CHILD(n, NCH(n) - 1);
2007 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002008 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 else
2010 expression = ast_for_expr(c, value);
2011 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002012 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002013 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015}
2016
2017static stmt_ty
2018ast_for_print_stmt(struct compiling *c, const node *n)
2019{
2020 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2021 | '>>' test [ (',' test)+ [','] ] )
2022 */
2023 expr_ty dest = NULL, expression;
2024 asdl_seq *seq;
2025 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002026 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027
2028 REQ(n, print_stmt);
2029 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2030 dest = ast_for_expr(c, CHILD(n, 2));
2031 if (!dest)
2032 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002033 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002035 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002037 return NULL;
2038 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002040 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002042 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 }
2044 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002045 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046}
2047
2048static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002049ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050{
2051 asdl_seq *seq;
2052 int i;
2053 expr_ty e;
2054
2055 REQ(n, exprlist);
2056
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002057 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 if (!seq)
2059 return NULL;
2060 for (i = 0; i < NCH(n); i += 2) {
2061 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002062 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002063 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002064 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002065 if (context && !set_context(e, context, CHILD(n, i)))
2066 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 }
2068 return seq;
2069}
2070
2071static stmt_ty
2072ast_for_del_stmt(struct compiling *c, const node *n)
2073{
2074 asdl_seq *expr_list;
2075
2076 /* del_stmt: 'del' exprlist */
2077 REQ(n, del_stmt);
2078
2079 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2080 if (!expr_list)
2081 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002082 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083}
2084
2085static stmt_ty
2086ast_for_flow_stmt(struct compiling *c, const node *n)
2087{
2088 /*
2089 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2090 | yield_stmt
2091 break_stmt: 'break'
2092 continue_stmt: 'continue'
2093 return_stmt: 'return' [testlist]
2094 yield_stmt: yield_expr
2095 yield_expr: 'yield' testlist
2096 raise_stmt: 'raise' [test [',' test [',' test]]]
2097 */
2098 node *ch;
2099
2100 REQ(n, flow_stmt);
2101 ch = CHILD(n, 0);
2102 switch (TYPE(ch)) {
2103 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002104 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002106 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 case yield_stmt: { /* will reduce to yield_expr */
2108 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2109 if (!exp)
2110 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002111 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 }
2113 case return_stmt:
2114 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002115 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002117 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 if (!expression)
2119 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002120 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 }
2122 case raise_stmt:
2123 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002124 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 else if (NCH(ch) == 2) {
2126 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2127 if (!expression)
2128 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002129 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 }
2131 else if (NCH(ch) == 4) {
2132 expr_ty expr1, expr2;
2133
2134 expr1 = ast_for_expr(c, CHILD(ch, 1));
2135 if (!expr1)
2136 return NULL;
2137 expr2 = ast_for_expr(c, CHILD(ch, 3));
2138 if (!expr2)
2139 return NULL;
2140
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002141 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 }
2143 else if (NCH(ch) == 6) {
2144 expr_ty expr1, expr2, expr3;
2145
2146 expr1 = ast_for_expr(c, CHILD(ch, 1));
2147 if (!expr1)
2148 return NULL;
2149 expr2 = ast_for_expr(c, CHILD(ch, 3));
2150 if (!expr2)
2151 return NULL;
2152 expr3 = ast_for_expr(c, CHILD(ch, 5));
2153 if (!expr3)
2154 return NULL;
2155
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002156 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 }
2158 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002159 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 "unexpected flow_stmt: %d", TYPE(ch));
2161 return NULL;
2162 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002163
2164 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2165 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166}
2167
2168static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002169alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170{
2171 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002172 import_as_name: NAME ['as' NAME]
2173 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 dotted_name: NAME ('.' NAME)*
2175 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002176 PyObject *str;
2177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 loop:
2179 switch (TYPE(n)) {
2180 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002181 str = NULL;
2182 if (NCH(n) == 3) {
2183 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2184 ast_error(n, "must use 'as' in import");
2185 return NULL;
2186 }
2187 str = NEW_IDENTIFIER(CHILD(n, 2));
2188 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002189 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 case dotted_as_name:
2191 if (NCH(n) == 1) {
2192 n = CHILD(n, 0);
2193 goto loop;
2194 }
2195 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002196 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002197 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2198 ast_error(n, "must use 'as' in import");
2199 return NULL;
2200 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 assert(!a->asname);
2202 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2203 return a;
2204 }
2205 break;
2206 case dotted_name:
2207 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002208 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 else {
2210 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002211 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002212 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 char *s;
2214
2215 len = 0;
2216 for (i = 0; i < NCH(n); i += 2)
2217 /* length of string plus one for the dot */
2218 len += strlen(STR(CHILD(n, i))) + 1;
2219 len--; /* the last name doesn't have a dot */
2220 str = PyString_FromStringAndSize(NULL, len);
2221 if (!str)
2222 return NULL;
2223 s = PyString_AS_STRING(str);
2224 if (!s)
2225 return NULL;
2226 for (i = 0; i < NCH(n); i += 2) {
2227 char *sch = STR(CHILD(n, i));
2228 strcpy(s, STR(CHILD(n, i)));
2229 s += strlen(sch);
2230 *s++ = '.';
2231 }
2232 --s;
2233 *s = '\0';
2234 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002235 PyArena_AddPyObject(c->c_arena, str);
2236 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 }
2238 break;
2239 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002240 str = PyString_InternFromString("*");
2241 PyArena_AddPyObject(c->c_arena, str);
2242 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002244 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 "unexpected import name: %d", TYPE(n));
2246 return NULL;
2247 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002248
2249 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 return NULL;
2251}
2252
2253static stmt_ty
2254ast_for_import_stmt(struct compiling *c, const node *n)
2255{
2256 /*
2257 import_stmt: import_name | import_from
2258 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002259 import_from: 'from' ('.'* dotted_name | '.') 'import'
2260 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002262 int lineno;
2263 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 int i;
2265 asdl_seq *aliases;
2266
2267 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002268 lineno = LINENO(n);
2269 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002271 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002273 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002274 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 if (!aliases)
2276 return NULL;
2277 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002278 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002279 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 asdl_seq_SET(aliases, i / 2, import_alias);
2282 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002283 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002285 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002287 int idx, ndots = 0;
2288 alias_ty mod = NULL;
2289 identifier modname;
2290
2291 /* Count the number of dots (for relative imports) and check for the
2292 optional module name */
2293 for (idx = 1; idx < NCH(n); idx++) {
2294 if (TYPE(CHILD(n, idx)) == dotted_name) {
2295 mod = alias_for_import_name(c, CHILD(n, idx));
2296 idx++;
2297 break;
2298 } else if (TYPE(CHILD(n, idx)) != DOT) {
2299 break;
2300 }
2301 ndots++;
2302 }
2303 idx++; /* skip over the 'import' keyword */
2304 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002305 case STAR:
2306 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002307 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002308 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002309 if (ndots) {
2310 ast_error(n, "'import *' not allowed with 'from .'");
2311 return NULL;
2312 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002313 break;
2314 case LPAR:
2315 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002316 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002317 n_children = NCH(n);
2318 break;
2319 case import_as_names:
2320 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002321 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002322 n_children = NCH(n);
2323 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 ast_error(n, "trailing comma not allowed without"
2325 " surrounding parentheses");
2326 return NULL;
2327 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002328 break;
2329 default:
2330 ast_error(n, "Unexpected node-type in from-import");
2331 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002332 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002334 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002335 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337
2338 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002339 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002340 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002341 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002343 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002344 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002345 else {
2346 for (i = 0; i < NCH(n); i += 2) {
2347 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2348 if (!import_alias)
2349 return NULL;
2350 asdl_seq_SET(aliases, i / 2, import_alias);
2351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002353 if (mod != NULL)
2354 modname = mod->name;
2355 else
2356 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002357 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002358 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 }
Neal Norwitz79792652005-11-14 04:25:03 +00002360 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361 "unknown import statement: starts with command '%s'",
2362 STR(CHILD(n, 0)));
2363 return NULL;
2364}
2365
2366static stmt_ty
2367ast_for_global_stmt(struct compiling *c, const node *n)
2368{
2369 /* global_stmt: 'global' NAME (',' NAME)* */
2370 identifier name;
2371 asdl_seq *s;
2372 int i;
2373
2374 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002375 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 if (!s)
2377 return NULL;
2378 for (i = 1; i < NCH(n); i += 2) {
2379 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002380 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382 asdl_seq_SET(s, i / 2, name);
2383 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002384 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385}
2386
2387static stmt_ty
2388ast_for_exec_stmt(struct compiling *c, const node *n)
2389{
2390 expr_ty expr1, globals = NULL, locals = NULL;
2391 int n_children = NCH(n);
2392 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002393 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 "poorly formed 'exec' statement: %d parts to statement",
2395 n_children);
2396 return NULL;
2397 }
2398
2399 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2400 REQ(n, exec_stmt);
2401 expr1 = ast_for_expr(c, CHILD(n, 1));
2402 if (!expr1)
2403 return NULL;
2404 if (n_children >= 4) {
2405 globals = ast_for_expr(c, CHILD(n, 3));
2406 if (!globals)
2407 return NULL;
2408 }
2409 if (n_children == 6) {
2410 locals = ast_for_expr(c, CHILD(n, 5));
2411 if (!locals)
2412 return NULL;
2413 }
2414
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002415 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416}
2417
2418static stmt_ty
2419ast_for_assert_stmt(struct compiling *c, const node *n)
2420{
2421 /* assert_stmt: 'assert' test [',' test] */
2422 REQ(n, assert_stmt);
2423 if (NCH(n) == 2) {
2424 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2425 if (!expression)
2426 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002427 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 }
2429 else if (NCH(n) == 4) {
2430 expr_ty expr1, expr2;
2431
2432 expr1 = ast_for_expr(c, CHILD(n, 1));
2433 if (!expr1)
2434 return NULL;
2435 expr2 = ast_for_expr(c, CHILD(n, 3));
2436 if (!expr2)
2437 return NULL;
2438
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002439 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 }
Neal Norwitz79792652005-11-14 04:25:03 +00002441 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 "improper number of parts to 'assert' statement: %d",
2443 NCH(n));
2444 return NULL;
2445}
2446
2447static asdl_seq *
2448ast_for_suite(struct compiling *c, const node *n)
2449{
2450 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002451 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 stmt_ty s;
2453 int i, total, num, end, pos = 0;
2454 node *ch;
2455
2456 REQ(n, suite);
2457
2458 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002459 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460 if (!seq)
2461 return NULL;
2462 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2463 n = CHILD(n, 0);
2464 /* simple_stmt always ends with a NEWLINE,
2465 and may have a trailing SEMI
2466 */
2467 end = NCH(n) - 1;
2468 if (TYPE(CHILD(n, end - 1)) == SEMI)
2469 end--;
2470 /* loop by 2 to skip semi-colons */
2471 for (i = 0; i < end; i += 2) {
2472 ch = CHILD(n, i);
2473 s = ast_for_stmt(c, ch);
2474 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002475 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 asdl_seq_SET(seq, pos++, s);
2477 }
2478 }
2479 else {
2480 for (i = 2; i < (NCH(n) - 1); i++) {
2481 ch = CHILD(n, i);
2482 REQ(ch, stmt);
2483 num = num_stmts(ch);
2484 if (num == 1) {
2485 /* small_stmt or compound_stmt with only one child */
2486 s = ast_for_stmt(c, ch);
2487 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 asdl_seq_SET(seq, pos++, s);
2490 }
2491 else {
2492 int j;
2493 ch = CHILD(ch, 0);
2494 REQ(ch, simple_stmt);
2495 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002496 /* statement terminates with a semi-colon ';' */
2497 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002498 assert((j + 1) == NCH(ch));
2499 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002500 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 s = ast_for_stmt(c, CHILD(ch, j));
2502 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002503 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 asdl_seq_SET(seq, pos++, s);
2505 }
2506 }
2507 }
2508 }
2509 assert(pos == seq->size);
2510 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511}
2512
2513static stmt_ty
2514ast_for_if_stmt(struct compiling *c, const node *n)
2515{
2516 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2517 ['else' ':' suite]
2518 */
2519 char *s;
2520
2521 REQ(n, if_stmt);
2522
2523 if (NCH(n) == 4) {
2524 expr_ty expression;
2525 asdl_seq *suite_seq;
2526
2527 expression = ast_for_expr(c, CHILD(n, 1));
2528 if (!expression)
2529 return NULL;
2530 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002531 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 return NULL;
2533
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002534 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002536
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 s = STR(CHILD(n, 4));
2538 /* s[2], the third character in the string, will be
2539 's' for el_s_e, or
2540 'i' for el_i_f
2541 */
2542 if (s[2] == 's') {
2543 expr_ty expression;
2544 asdl_seq *seq1, *seq2;
2545
2546 expression = ast_for_expr(c, CHILD(n, 1));
2547 if (!expression)
2548 return NULL;
2549 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002550 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 return NULL;
2552 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002553 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 return NULL;
2555
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002556 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 }
2558 else if (s[2] == 'i') {
2559 int i, n_elif, has_else = 0;
2560 asdl_seq *orelse = NULL;
2561 n_elif = NCH(n) - 4;
2562 /* must reference the child n_elif+1 since 'else' token is third,
2563 not fourth, child from the end. */
2564 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2565 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2566 has_else = 1;
2567 n_elif -= 3;
2568 }
2569 n_elif /= 4;
2570
2571 if (has_else) {
2572 expr_ty expression;
2573 asdl_seq *seq1, *seq2;
2574
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002575 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 if (!orelse)
2577 return NULL;
2578 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002579 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002582 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002585 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587
2588 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002589 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002590 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 /* the just-created orelse handled the last elif */
2592 n_elif--;
2593 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594
2595 for (i = 0; i < n_elif; i++) {
2596 int off = 5 + (n_elif - i - 1) * 4;
2597 expr_ty expression;
2598 asdl_seq *suite_seq;
Anthony Baxtera863d332006-04-11 07:43:46 +00002599 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2600 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 return NULL;
2602 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002603 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002606 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608
Anthony Baxtera863d332006-04-11 07:43:46 +00002609 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002611 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Anthony Baxtera863d332006-04-11 07:43:46 +00002612 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 }
2614 return If(ast_for_expr(c, CHILD(n, 1)),
2615 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002616 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002618
2619 PyErr_Format(PyExc_SystemError,
2620 "unexpected token in 'if' statement: %s", s);
2621 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622}
2623
2624static stmt_ty
2625ast_for_while_stmt(struct compiling *c, const node *n)
2626{
2627 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2628 REQ(n, while_stmt);
2629
2630 if (NCH(n) == 4) {
2631 expr_ty expression;
2632 asdl_seq *suite_seq;
2633
2634 expression = ast_for_expr(c, CHILD(n, 1));
2635 if (!expression)
2636 return NULL;
2637 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002638 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002640 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 }
2642 else if (NCH(n) == 7) {
2643 expr_ty expression;
2644 asdl_seq *seq1, *seq2;
2645
2646 expression = ast_for_expr(c, CHILD(n, 1));
2647 if (!expression)
2648 return NULL;
2649 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002650 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 return NULL;
2652 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002653 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 return NULL;
2655
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002656 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002658
2659 PyErr_Format(PyExc_SystemError,
2660 "wrong number of tokens for 'while' statement: %d",
2661 NCH(n));
2662 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663}
2664
2665static stmt_ty
2666ast_for_for_stmt(struct compiling *c, const node *n)
2667{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002668 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 expr_ty expression;
2670 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002671 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2673 REQ(n, for_stmt);
2674
2675 if (NCH(n) == 9) {
2676 seq = ast_for_suite(c, CHILD(n, 8));
2677 if (!seq)
2678 return NULL;
2679 }
2680
Neal Norwitzedef2be2006-07-12 05:26:17 +00002681 node_target = CHILD(n, 1);
2682 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002683 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002685 /* Check the # of children rather than the length of _target, since
2686 for x, in ... has 1 element in _target, but still requires a Tuple. */
2687 if (NCH(node_target) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00002688 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002690 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002692 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002693 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694 return NULL;
2695 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002696 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 return NULL;
2698
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002699 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2700 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701}
2702
2703static excepthandler_ty
2704ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2705{
2706 /* except_clause: 'except' [test [',' test]] */
2707 REQ(exc, except_clause);
2708 REQ(body, suite);
2709
2710 if (NCH(exc) == 1) {
2711 asdl_seq *suite_seq = ast_for_suite(c, body);
2712 if (!suite_seq)
2713 return NULL;
2714
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002715 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2716 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717 }
2718 else if (NCH(exc) == 2) {
2719 expr_ty expression;
2720 asdl_seq *suite_seq;
2721
2722 expression = ast_for_expr(c, CHILD(exc, 1));
2723 if (!expression)
2724 return NULL;
2725 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002726 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727 return NULL;
2728
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002729 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2730 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 }
2732 else if (NCH(exc) == 4) {
2733 asdl_seq *suite_seq;
2734 expr_ty expression;
2735 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2736 if (!e)
2737 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002738 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 return NULL;
2740 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002741 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 return NULL;
2743 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002744 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 return NULL;
2746
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002747 return excepthandler(expression, e, suite_seq, LINENO(exc),
2748 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002750
2751 PyErr_Format(PyExc_SystemError,
2752 "wrong number of children for 'except' clause: %d",
2753 NCH(exc));
2754 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755}
2756
2757static stmt_ty
2758ast_for_try_stmt(struct compiling *c, const node *n)
2759{
Neal Norwitzf599f422005-12-17 21:33:47 +00002760 const int nch = NCH(n);
2761 int n_except = (nch - 3)/3;
2762 asdl_seq *body, *orelse = NULL, *finally = NULL;
2763
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764 REQ(n, try_stmt);
2765
Neal Norwitzf599f422005-12-17 21:33:47 +00002766 body = ast_for_suite(c, CHILD(n, 2));
2767 if (body == NULL)
2768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769
Neal Norwitzf599f422005-12-17 21:33:47 +00002770 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2771 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2772 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2773 /* we can assume it's an "else",
2774 because nch >= 9 for try-else-finally and
2775 it would otherwise have a type of except_clause */
2776 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2777 if (orelse == NULL)
2778 return NULL;
2779 n_except--;
2780 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781
Neal Norwitzf599f422005-12-17 21:33:47 +00002782 finally = ast_for_suite(c, CHILD(n, nch - 1));
2783 if (finally == NULL)
2784 return NULL;
2785 n_except--;
2786 }
2787 else {
2788 /* we can assume it's an "else",
2789 otherwise it would have a type of except_clause */
2790 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2791 if (orelse == NULL)
2792 return NULL;
2793 n_except--;
2794 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002796 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002797 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 return NULL;
2799 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002800
2801 if (n_except > 0) {
2802 int i;
2803 stmt_ty except_st;
2804 /* process except statements to create a try ... except */
2805 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2806 if (handlers == NULL)
2807 return NULL;
2808
2809 for (i = 0; i < n_except; i++) {
2810 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2811 CHILD(n, 5 + i * 3));
2812 if (!e)
2813 return NULL;
2814 asdl_seq_SET(handlers, i, e);
2815 }
2816
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002817 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2818 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002819 if (!finally)
2820 return except_st;
2821
2822 /* if a 'finally' is present too, we nest the TryExcept within a
2823 TryFinally to emulate try ... except ... finally */
2824 body = asdl_seq_new(1, c->c_arena);
2825 if (body == NULL)
2826 return NULL;
2827 asdl_seq_SET(body, 0, except_st);
2828 }
2829
2830 /* must be a try ... finally (except clauses are in body, if any exist) */
2831 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002832 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833}
2834
Guido van Rossumc2e20742006-02-27 22:32:47 +00002835static expr_ty
2836ast_for_with_var(struct compiling *c, const node *n)
2837{
2838 REQ(n, with_var);
2839 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2840 ast_error(n, "expected \"with [expr] as [var]\"");
2841 return NULL;
2842 }
2843 return ast_for_expr(c, CHILD(n, 1));
2844}
2845
2846/* with_stmt: 'with' test [ with_var ] ':' suite */
2847static stmt_ty
2848ast_for_with_stmt(struct compiling *c, const node *n)
2849{
2850 expr_ty context_expr, optional_vars = NULL;
2851 int suite_index = 3; /* skip 'with', test, and ':' */
2852 asdl_seq *suite_seq;
2853
2854 assert(TYPE(n) == with_stmt);
2855 context_expr = ast_for_expr(c, CHILD(n, 1));
2856 if (TYPE(CHILD(n, 2)) == with_var) {
2857 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2858
2859 if (!optional_vars) {
2860 return NULL;
2861 }
2862 if (!set_context(optional_vars, Store, n)) {
2863 return NULL;
2864 }
2865 suite_index = 4;
2866 }
2867
2868 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2869 if (!suite_seq) {
2870 return NULL;
2871 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002872 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2873 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002874}
2875
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876static stmt_ty
2877ast_for_classdef(struct compiling *c, const node *n)
2878{
2879 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 asdl_seq *bases, *s;
2881
2882 REQ(n, classdef);
2883
2884 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2885 ast_error(n, "assignment to None");
2886 return NULL;
2887 }
2888
2889 if (NCH(n) == 4) {
2890 s = ast_for_suite(c, CHILD(n, 3));
2891 if (!s)
2892 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002893 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2894 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 }
2896 /* check for empty base list */
2897 if (TYPE(CHILD(n,3)) == RPAR) {
2898 s = ast_for_suite(c, CHILD(n,5));
2899 if (!s)
2900 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002901 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2902 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 }
2904
2905 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002906 bases = ast_for_class_bases(c, CHILD(n, 3));
2907 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909
2910 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002911 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002913 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2914 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915}
2916
2917static stmt_ty
2918ast_for_stmt(struct compiling *c, const node *n)
2919{
2920 if (TYPE(n) == stmt) {
2921 assert(NCH(n) == 1);
2922 n = CHILD(n, 0);
2923 }
2924 if (TYPE(n) == simple_stmt) {
2925 assert(num_stmts(n) == 1);
2926 n = CHILD(n, 0);
2927 }
2928 if (TYPE(n) == small_stmt) {
2929 REQ(n, small_stmt);
2930 n = CHILD(n, 0);
2931 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2932 | flow_stmt | import_stmt | global_stmt | exec_stmt
2933 | assert_stmt
2934 */
2935 switch (TYPE(n)) {
2936 case expr_stmt:
2937 return ast_for_expr_stmt(c, n);
2938 case print_stmt:
2939 return ast_for_print_stmt(c, n);
2940 case del_stmt:
2941 return ast_for_del_stmt(c, n);
2942 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002943 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 case flow_stmt:
2945 return ast_for_flow_stmt(c, n);
2946 case import_stmt:
2947 return ast_for_import_stmt(c, n);
2948 case global_stmt:
2949 return ast_for_global_stmt(c, n);
2950 case exec_stmt:
2951 return ast_for_exec_stmt(c, n);
2952 case assert_stmt:
2953 return ast_for_assert_stmt(c, n);
2954 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002955 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2957 TYPE(n), NCH(n));
2958 return NULL;
2959 }
2960 }
2961 else {
2962 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2963 | funcdef | classdef
2964 */
2965 node *ch = CHILD(n, 0);
2966 REQ(n, compound_stmt);
2967 switch (TYPE(ch)) {
2968 case if_stmt:
2969 return ast_for_if_stmt(c, ch);
2970 case while_stmt:
2971 return ast_for_while_stmt(c, ch);
2972 case for_stmt:
2973 return ast_for_for_stmt(c, ch);
2974 case try_stmt:
2975 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002976 case with_stmt:
2977 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978 case funcdef:
2979 return ast_for_funcdef(c, ch);
2980 case classdef:
2981 return ast_for_classdef(c, ch);
2982 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002983 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2985 TYPE(n), NCH(n));
2986 return NULL;
2987 }
2988 }
2989}
2990
2991static PyObject *
2992parsenumber(const char *s)
2993{
2994 const char *end;
2995 long x;
2996 double dx;
2997#ifndef WITHOUT_COMPLEX
2998 Py_complex c;
2999 int imflag;
3000#endif
3001
3002 errno = 0;
3003 end = s + strlen(s) - 1;
3004#ifndef WITHOUT_COMPLEX
3005 imflag = *end == 'j' || *end == 'J';
3006#endif
3007 if (*end == 'l' || *end == 'L')
3008 return PyLong_FromString((char *)s, (char **)0, 0);
3009 if (s[0] == '0') {
3010 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3011 if (x < 0 && errno == 0) {
3012 return PyLong_FromString((char *)s,
3013 (char **)0,
3014 0);
3015 }
3016 }
3017 else
3018 x = PyOS_strtol((char *)s, (char **)&end, 0);
3019 if (*end == '\0') {
3020 if (errno != 0)
3021 return PyLong_FromString((char *)s, (char **)0, 0);
3022 return PyInt_FromLong(x);
3023 }
3024 /* XXX Huge floats may silently fail */
3025#ifndef WITHOUT_COMPLEX
3026 if (imflag) {
3027 c.real = 0.;
3028 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003029 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030 PyFPE_END_PROTECT(c)
3031 return PyComplex_FromCComplex(c);
3032 }
3033 else
3034#endif
3035 {
3036 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003037 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038 PyFPE_END_PROTECT(dx)
3039 return PyFloat_FromDouble(dx);
3040 }
3041}
3042
3043static PyObject *
3044decode_utf8(const char **sPtr, const char *end, char* encoding)
3045{
3046#ifndef Py_USING_UNICODE
3047 Py_FatalError("decode_utf8 should not be called in this build.");
3048 return NULL;
3049#else
3050 PyObject *u, *v;
3051 char *s, *t;
3052 t = s = (char *)*sPtr;
3053 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3054 while (s < end && (*s & 0x80)) s++;
3055 *sPtr = s;
3056 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3057 if (u == NULL)
3058 return NULL;
3059 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3060 Py_DECREF(u);
3061 return v;
3062#endif
3063}
3064
3065static PyObject *
3066decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3067{
3068 PyObject *v, *u;
3069 char *buf;
3070 char *p;
3071 const char *end;
3072 if (encoding == NULL) {
3073 buf = (char *)s;
3074 u = NULL;
3075 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3076 buf = (char *)s;
3077 u = NULL;
3078 } else {
3079 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3080 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3081 if (u == NULL)
3082 return NULL;
3083 p = buf = PyString_AsString(u);
3084 end = s + len;
3085 while (s < end) {
3086 if (*s == '\\') {
3087 *p++ = *s++;
3088 if (*s & 0x80) {
3089 strcpy(p, "u005c");
3090 p += 5;
3091 }
3092 }
3093 if (*s & 0x80) { /* XXX inefficient */
3094 PyObject *w;
3095 char *r;
Martin v. Löwis66851282006-04-22 11:40:03 +00003096 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097 w = decode_utf8(&s, end, "utf-16-be");
3098 if (w == NULL) {
3099 Py_DECREF(u);
3100 return NULL;
3101 }
3102 r = PyString_AsString(w);
3103 rn = PyString_Size(w);
3104 assert(rn % 2 == 0);
3105 for (i = 0; i < rn; i += 2) {
3106 sprintf(p, "\\u%02x%02x",
3107 r[i + 0] & 0xFF,
3108 r[i + 1] & 0xFF);
3109 p += 6;
3110 }
3111 Py_DECREF(w);
3112 } else {
3113 *p++ = *s++;
3114 }
3115 }
3116 len = p - buf;
3117 s = buf;
3118 }
3119 if (rawmode)
3120 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3121 else
3122 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3123 Py_XDECREF(u);
3124 return v;
3125}
3126
3127/* s is a Python string literal, including the bracketing quote characters,
3128 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3129 * parsestr parses it, and returns the decoded Python string object.
3130 */
3131static PyObject *
3132parsestr(const char *s, const char *encoding)
3133{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003135 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 int rawmode = 0;
3137 int need_encoding;
3138 int unicode = 0;
3139
3140 if (isalpha(quote) || quote == '_') {
3141 if (quote == 'u' || quote == 'U') {
3142 quote = *++s;
3143 unicode = 1;
3144 }
3145 if (quote == 'r' || quote == 'R') {
3146 quote = *++s;
3147 rawmode = 1;
3148 }
3149 }
3150 if (quote != '\'' && quote != '\"') {
3151 PyErr_BadInternalCall();
3152 return NULL;
3153 }
3154 s++;
3155 len = strlen(s);
3156 if (len > INT_MAX) {
3157 PyErr_SetString(PyExc_OverflowError,
3158 "string to parse is too long");
3159 return NULL;
3160 }
3161 if (s[--len] != quote) {
3162 PyErr_BadInternalCall();
3163 return NULL;
3164 }
3165 if (len >= 4 && s[0] == quote && s[1] == quote) {
3166 s += 2;
3167 len -= 2;
3168 if (s[--len] != quote || s[--len] != quote) {
3169 PyErr_BadInternalCall();
3170 return NULL;
3171 }
3172 }
3173#ifdef Py_USING_UNICODE
3174 if (unicode || Py_UnicodeFlag) {
3175 return decode_unicode(s, len, rawmode, encoding);
3176 }
3177#endif
3178 need_encoding = (encoding != NULL &&
3179 strcmp(encoding, "utf-8") != 0 &&
3180 strcmp(encoding, "iso-8859-1") != 0);
3181 if (rawmode || strchr(s, '\\') == NULL) {
3182 if (need_encoding) {
3183#ifndef Py_USING_UNICODE
3184 /* This should not happen - we never see any other
3185 encoding. */
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003186 Py_FatalError(
3187 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003189 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 if (u == NULL)
3191 return NULL;
3192 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3193 Py_DECREF(u);
3194 return v;
3195#endif
3196 } else {
3197 return PyString_FromStringAndSize(s, len);
3198 }
3199 }
3200
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003201 return PyString_DecodeEscape(s, len, NULL, unicode,
3202 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203}
3204
3205/* Build a Python string object out of a STRING atom. This takes care of
3206 * compile-time literal catenation, calling parsestr() on each piece, and
3207 * pasting the intermediate results together.
3208 */
3209static PyObject *
3210parsestrplus(struct compiling *c, const node *n)
3211{
3212 PyObject *v;
3213 int i;
3214 REQ(CHILD(n, 0), STRING);
3215 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3216 /* String literal concatenation */
3217 for (i = 1; i < NCH(n); i++) {
3218 PyObject *s;
3219 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3220 if (s == NULL)
3221 goto onError;
3222 if (PyString_Check(v) && PyString_Check(s)) {
3223 PyString_ConcatAndDel(&v, s);
3224 if (v == NULL)
3225 goto onError;
3226 }
3227#ifdef Py_USING_UNICODE
3228 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003229 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 Py_DECREF(v);
3232 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003233 if (v == NULL)
3234 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235 }
3236#endif
3237 }
3238 }
3239 return v;
3240
3241 onError:
3242 Py_XDECREF(v);
3243 return NULL;
3244}