blob: 1a438789434ad8ba96c315af933ec7a2ab215814 [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 *);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 }
Thomas Wouters477c8d52006-05-27 19:21:47 +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";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters0e3f5912006-08-11 14:57:12 +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;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000390 case Yield_kind:
391 expr_name = "yield expression";
392 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000393 case ListComp_kind:
394 expr_name = "list comprehension";
395 break;
396 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 case Num_kind:
398 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000399 expr_name = "literal";
400 break;
401 case Compare_kind:
402 expr_name = "comparison";
403 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +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++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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{
Guido van Rossumb053cd82006-08-24 03:53:23 +0000478 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479 |'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));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000622 return NULL; /* Don't need to goto error; no objects 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)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000625 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626
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) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
642 if (!expression)
643 goto error;
644 assert(defaults != NULL);
645 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 i += 2;
647 found_default = 1;
648 }
649 else if (found_default) {
650 ast_error(n,
651 "non-default argument follows default argument");
652 goto error;
653 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 if (NCH(ch) == 3) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655 ch = CHILD(ch, 1);
656 /* def foo((x)): is not complex, special case. */
657 if (NCH(ch) != 1) {
658 /* We have complex arguments, setup for unpacking. */
659 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
660 } else {
661 /* def foo((x)): setup for checking NAME below. */
662 ch = CHILD(ch, 0);
663 }
664 }
665 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000666 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
668 ast_error(CHILD(ch, 0), "assignment to None");
669 goto error;
670 }
Armin Rigo31441302005-10-21 12:57:31 +0000671 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672 Param, LINENO(ch), ch->n_col_offset,
673 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674 if (!name)
675 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000676 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677
678 }
679 i += 2; /* the name and the comma */
680 break;
681 case STAR:
682 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
683 ast_error(CHILD(n, i+1), "assignment to None");
684 goto error;
685 }
686 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
687 i += 3;
688 break;
689 case DOUBLESTAR:
690 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
691 ast_error(CHILD(n, i+1), "assignment to None");
692 goto error;
693 }
694 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
695 i += 3;
696 break;
697 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000698 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 "unexpected node in varargslist: %d @ %d",
700 TYPE(ch), i);
701 goto error;
702 }
703 }
704
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000705 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706
707 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000708 Py_XDECREF(vararg);
709 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710 return NULL;
711}
712
713static expr_ty
714ast_for_dotted_name(struct compiling *c, const node *n)
715{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000716 expr_ty e;
717 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000718 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719 int i;
720
721 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000722
723 lineno = LINENO(n);
724 col_offset = n->n_col_offset;
725
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 id = NEW_IDENTIFIER(CHILD(n, 0));
727 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000728 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000729 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000731 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732
733 for (i = 2; i < NCH(n); i+=2) {
734 id = NEW_IDENTIFIER(CHILD(n, i));
735 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000736 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000737 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000738 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740 }
741
742 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743}
744
745static expr_ty
746ast_for_decorator(struct compiling *c, const node *n)
747{
748 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
749 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000750 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751
752 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000753 REQ(CHILD(n, 0), AT);
754 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755
756 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
757 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000758 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759
760 if (NCH(n) == 3) { /* No arguments */
761 d = name_expr;
762 name_expr = NULL;
763 }
764 else if (NCH(n) == 5) { /* Call with no arguments */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000765 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
766 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 name_expr = NULL;
770 }
771 else {
772 d = ast_for_call(c, CHILD(n, 3), name_expr);
773 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000774 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 name_expr = NULL;
776 }
777
778 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779}
780
781static asdl_seq*
782ast_for_decorators(struct compiling *c, const node *n)
783{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000784 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000785 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 int i;
787
788 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000789 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 if (!decorator_seq)
791 return NULL;
792
793 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000794 d = ast_for_decorator(c, CHILD(n, i));
795 if (!d)
796 return NULL;
797 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 }
799 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800}
801
802static stmt_ty
803ast_for_funcdef(struct compiling *c, const node *n)
804{
805 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000806 identifier name;
807 arguments_ty args;
808 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 asdl_seq *decorator_seq = NULL;
810 int name_i;
811
812 REQ(n, funcdef);
813
814 if (NCH(n) == 6) { /* decorators are present */
815 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
816 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000817 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 name_i = 2;
819 }
820 else {
821 name_i = 1;
822 }
823
824 name = NEW_IDENTIFIER(CHILD(n, name_i));
825 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000828 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000829 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 }
831 args = ast_for_arguments(c, CHILD(n, name_i + 1));
832 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 body = ast_for_suite(c, CHILD(n, name_i + 3));
835 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
839 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840}
841
842static expr_ty
843ast_for_lambdef(struct compiling *c, const node *n)
844{
845 /* lambdef: 'lambda' [varargslist] ':' test */
846 arguments_ty args;
847 expr_ty expression;
848
849 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000850 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 if (!args)
852 return NULL;
853 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000854 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 }
857 else {
858 args = ast_for_arguments(c, CHILD(n, 1));
859 if (!args)
860 return NULL;
861 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000862 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 }
865
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000866 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867}
868
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000869static expr_ty
870ast_for_ifexpr(struct compiling *c, const node *n)
871{
872 /* test: or_test 'if' or_test 'else' test */
873 expr_ty expression, body, orelse;
874
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000875 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000876 body = ast_for_expr(c, CHILD(n, 0));
877 if (!body)
878 return NULL;
879 expression = ast_for_expr(c, CHILD(n, 2));
880 if (!expression)
881 return NULL;
882 orelse = ast_for_expr(c, CHILD(n, 4));
883 if (!orelse)
884 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000885 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
886 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000887}
888
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889/* Count the number of 'for' loop in a list comprehension.
890
891 Helper for ast_for_listcomp().
892*/
893
894static int
895count_list_fors(const node *n)
896{
897 int n_fors = 0;
898 node *ch = CHILD(n, 1);
899
900 count_list_for:
901 n_fors++;
902 REQ(ch, list_for);
903 if (NCH(ch) == 5)
904 ch = CHILD(ch, 4);
905 else
906 return n_fors;
907 count_list_iter:
908 REQ(ch, list_iter);
909 ch = CHILD(ch, 0);
910 if (TYPE(ch) == list_for)
911 goto count_list_for;
912 else if (TYPE(ch) == list_if) {
913 if (NCH(ch) == 3) {
914 ch = CHILD(ch, 2);
915 goto count_list_iter;
916 }
917 else
918 return n_fors;
919 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000920
921 /* Should never be reached */
922 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
923 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924}
925
926/* Count the number of 'if' statements in a list comprehension.
927
928 Helper for ast_for_listcomp().
929*/
930
931static int
932count_list_ifs(const node *n)
933{
934 int n_ifs = 0;
935
936 count_list_iter:
937 REQ(n, list_iter);
938 if (TYPE(CHILD(n, 0)) == list_for)
939 return n_ifs;
940 n = CHILD(n, 0);
941 REQ(n, list_if);
942 n_ifs++;
943 if (NCH(n) == 2)
944 return n_ifs;
945 n = CHILD(n, 2);
946 goto count_list_iter;
947}
948
949static expr_ty
950ast_for_listcomp(struct compiling *c, const node *n)
951{
952 /* listmaker: test ( list_for | (',' test)* [','] )
953 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
954 list_iter: list_for | list_if
955 list_if: 'if' test [list_iter]
956 testlist_safe: test [(',' test)+ [',']]
957 */
958 expr_ty elt;
959 asdl_seq *listcomps;
960 int i, n_fors;
961 node *ch;
962
963 REQ(n, listmaker);
964 assert(NCH(n) > 1);
965
966 elt = ast_for_expr(c, CHILD(n, 0));
967 if (!elt)
968 return NULL;
969
970 n_fors = count_list_fors(n);
971 if (n_fors == -1)
972 return NULL;
973
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000974 listcomps = asdl_seq_new(n_fors, c->c_arena);
975 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 ch = CHILD(n, 1);
979 for (i = 0; i < n_fors; i++) {
980 comprehension_ty lc;
981 asdl_seq *t;
982 expr_ty expression;
983
984 REQ(ch, list_for);
985
986 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000987 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000989 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000990 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000993 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000994 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000995 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000997 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
998 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000999 expression, NULL, c->c_arena);
1000 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002
1003 if (NCH(ch) == 5) {
1004 int j, n_ifs;
1005 asdl_seq *ifs;
1006
1007 ch = CHILD(ch, 4);
1008 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001009 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001012 ifs = asdl_seq_new(n_ifs, c->c_arena);
1013 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015
1016 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001017 REQ(ch, list_iter);
1018 ch = CHILD(ch, 0);
1019 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020
Jeremy Hyltona8293132006-02-28 17:58:27 +00001021 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1022 if (NCH(ch) == 3)
1023 ch = CHILD(ch, 2);
1024 }
1025 /* on exit, must guarantee that ch is a list_for */
1026 if (TYPE(ch) == list_iter)
1027 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001029 }
1030 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 }
1032
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001033 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034}
1035
1036/*
1037 Count the number of 'for' loops in a generator expression.
1038
1039 Helper for ast_for_genexp().
1040*/
1041
1042static int
1043count_gen_fors(const node *n)
1044{
1045 int n_fors = 0;
1046 node *ch = CHILD(n, 1);
1047
1048 count_gen_for:
1049 n_fors++;
1050 REQ(ch, gen_for);
1051 if (NCH(ch) == 5)
1052 ch = CHILD(ch, 4);
1053 else
1054 return n_fors;
1055 count_gen_iter:
1056 REQ(ch, gen_iter);
1057 ch = CHILD(ch, 0);
1058 if (TYPE(ch) == gen_for)
1059 goto count_gen_for;
1060 else if (TYPE(ch) == gen_if) {
1061 if (NCH(ch) == 3) {
1062 ch = CHILD(ch, 2);
1063 goto count_gen_iter;
1064 }
1065 else
1066 return n_fors;
1067 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001068
1069 /* Should never be reached */
1070 PyErr_SetString(PyExc_SystemError,
1071 "logic error in count_gen_fors");
1072 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073}
1074
1075/* Count the number of 'if' statements in a generator expression.
1076
1077 Helper for ast_for_genexp().
1078*/
1079
1080static int
1081count_gen_ifs(const node *n)
1082{
1083 int n_ifs = 0;
1084
1085 while (1) {
1086 REQ(n, gen_iter);
1087 if (TYPE(CHILD(n, 0)) == gen_for)
1088 return n_ifs;
1089 n = CHILD(n, 0);
1090 REQ(n, gen_if);
1091 n_ifs++;
1092 if (NCH(n) == 2)
1093 return n_ifs;
1094 n = CHILD(n, 2);
1095 }
1096}
1097
Jeremy Hyltona8293132006-02-28 17:58:27 +00001098/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099static expr_ty
1100ast_for_genexp(struct compiling *c, const node *n)
1101{
1102 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1103 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1104 expr_ty elt;
1105 asdl_seq *genexps;
1106 int i, n_fors;
1107 node *ch;
1108
1109 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1110 assert(NCH(n) > 1);
1111
1112 elt = ast_for_expr(c, CHILD(n, 0));
1113 if (!elt)
1114 return NULL;
1115
1116 n_fors = count_gen_fors(n);
1117 if (n_fors == -1)
1118 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001119
1120 genexps = asdl_seq_new(n_fors, c->c_arena);
1121 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 ch = CHILD(n, 1);
1125 for (i = 0; i < n_fors; i++) {
1126 comprehension_ty ge;
1127 asdl_seq *t;
1128 expr_ty expression;
1129
1130 REQ(ch, gen_for);
1131
1132 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001133 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001135 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001136 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001138
1139 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001140 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001141 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1144 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001145 expression, NULL, c->c_arena);
1146
1147 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001149
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 if (NCH(ch) == 5) {
1151 int j, n_ifs;
1152 asdl_seq *ifs;
1153
1154 ch = CHILD(ch, 4);
1155 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001156 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001158
1159 ifs = asdl_seq_new(n_ifs, c->c_arena);
1160 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001162
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 for (j = 0; j < n_ifs; j++) {
1164 REQ(ch, gen_iter);
1165 ch = CHILD(ch, 0);
1166 REQ(ch, gen_if);
1167
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001168 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001169 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001170 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001171 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 if (NCH(ch) == 3)
1173 ch = CHILD(ch, 2);
1174 }
1175 /* on exit, must guarantee that ch is a gen_for */
1176 if (TYPE(ch) == gen_iter)
1177 ch = CHILD(ch, 0);
1178 ge->ifs = ifs;
1179 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001180 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 }
1182
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001183 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184}
1185
1186static expr_ty
1187ast_for_atom(struct compiling *c, const node *n)
1188{
1189 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
Brett Cannoncf588f62006-08-25 04:28:18 +00001190 | '{' [dictmaker] '}' | NAME | NUMBER | STRING+
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 */
1192 node *ch = CHILD(n, 0);
1193
1194 switch (TYPE(ch)) {
1195 case NAME:
1196 /* All names start in Load context, but may later be
1197 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001198 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 case STRING: {
1200 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 if (!str)
1202 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001203
1204 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001205 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 }
1207 case NUMBER: {
1208 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 if (!pynum)
1210 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001211
1212 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001213 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 }
1215 case LPAR: /* some parenthesized expressions */
1216 ch = CHILD(n, 1);
1217
1218 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001219 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220
1221 if (TYPE(ch) == yield_expr)
1222 return ast_for_expr(c, ch);
1223
1224 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1225 return ast_for_genexp(c, ch);
1226
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001227 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228 case LSQB: /* list (or list comprehension) */
1229 ch = CHILD(n, 1);
1230
1231 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001232 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233
1234 REQ(ch, listmaker);
1235 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1236 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 if (!elts)
1238 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001240 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 }
1242 else
1243 return ast_for_listcomp(c, ch);
1244 case LBRACE: {
1245 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1246 int i, size;
1247 asdl_seq *keys, *values;
1248
1249 ch = CHILD(n, 1);
1250 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001251 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 if (!keys)
1253 return NULL;
1254
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001255 values = asdl_seq_new(size, c->c_arena);
1256 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258
1259 for (i = 0; i < NCH(ch); i += 4) {
1260 expr_ty expression;
1261
1262 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001263 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001265
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001267
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001269 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001271
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 asdl_seq_SET(values, i / 4, expression);
1273 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001274 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001277 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 return NULL;
1279 }
1280}
1281
1282static slice_ty
1283ast_for_slice(struct compiling *c, const node *n)
1284{
1285 node *ch;
1286 expr_ty lower = NULL, upper = NULL, step = NULL;
1287
1288 REQ(n, subscript);
1289
1290 /*
1291 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1292 sliceop: ':' [test]
1293 */
1294 ch = CHILD(n, 0);
1295 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001296 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297
1298 if (NCH(n) == 1 && TYPE(ch) == test) {
1299 /* 'step' variable hold no significance in terms of being used over
1300 other vars */
1301 step = ast_for_expr(c, ch);
1302 if (!step)
1303 return NULL;
1304
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001305 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 }
1307
1308 if (TYPE(ch) == test) {
1309 lower = ast_for_expr(c, ch);
1310 if (!lower)
1311 return NULL;
1312 }
1313
1314 /* If there's an upper bound it's in the second or third position. */
1315 if (TYPE(ch) == COLON) {
1316 if (NCH(n) > 1) {
1317 node *n2 = CHILD(n, 1);
1318
1319 if (TYPE(n2) == test) {
1320 upper = ast_for_expr(c, n2);
1321 if (!upper)
1322 return NULL;
1323 }
1324 }
1325 } else if (NCH(n) > 2) {
1326 node *n2 = CHILD(n, 2);
1327
1328 if (TYPE(n2) == test) {
1329 upper = ast_for_expr(c, n2);
1330 if (!upper)
1331 return NULL;
1332 }
1333 }
1334
1335 ch = CHILD(n, NCH(n) - 1);
1336 if (TYPE(ch) == sliceop) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001337 if (NCH(ch) == 1) {
1338 /* No expression, so step is None */
1339 ch = CHILD(ch, 0);
1340 step = Name(new_identifier("None", c->c_arena), Load,
1341 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 if (!step)
1343 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001344 } else {
1345 ch = CHILD(ch, 1);
1346 if (TYPE(ch) == test) {
1347 step = ast_for_expr(c, ch);
1348 if (!step)
1349 return NULL;
1350 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 }
1352 }
1353
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001354 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355}
1356
1357static expr_ty
1358ast_for_binop(struct compiling *c, const node *n)
1359{
1360 /* Must account for a sequence of expressions.
1361 How should A op B op C by represented?
1362 BinOp(BinOp(A, op, B), op, C).
1363 */
1364
1365 int i, nops;
1366 expr_ty expr1, expr2, result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001367 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368
1369 expr1 = ast_for_expr(c, CHILD(n, 0));
1370 if (!expr1)
1371 return NULL;
1372
1373 expr2 = ast_for_expr(c, CHILD(n, 2));
1374 if (!expr2)
1375 return NULL;
1376
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001377 newoperator = get_operator(CHILD(n, 1));
1378 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 return NULL;
1380
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001381 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1382 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 if (!result)
1384 return NULL;
1385
1386 nops = (NCH(n) - 1) / 2;
1387 for (i = 1; i < nops; i++) {
1388 expr_ty tmp_result, tmp;
1389 const node* next_oper = CHILD(n, i * 2 + 1);
1390
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001391 newoperator = get_operator(next_oper);
1392 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 return NULL;
1394
1395 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1396 if (!tmp)
1397 return NULL;
1398
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001399 tmp_result = BinOp(result, newoperator, tmp,
1400 LINENO(next_oper), next_oper->n_col_offset,
1401 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 if (!tmp)
1403 return NULL;
1404 result = tmp_result;
1405 }
1406 return result;
1407}
1408
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001409static expr_ty
1410ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1411{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001412 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1413 subscriptlist: subscript (',' subscript)* [',']
1414 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1415 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001416 REQ(n, trailer);
1417 if (TYPE(CHILD(n, 0)) == LPAR) {
1418 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001419 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1420 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001421 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001422 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001423 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001424 else if (TYPE(CHILD(n, 0)) == DOT ) {
1425 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001426 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001427 }
1428 else {
1429 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001430 REQ(CHILD(n, 2), RSQB);
1431 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001432 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001433 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1434 if (!slc)
1435 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001436 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1437 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001438 }
1439 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001440 /* The grammar is ambiguous here. The ambiguity is resolved
1441 by treating the sequence as a tuple literal if there are
1442 no slice features.
1443 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001444 int j;
1445 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001446 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001447 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001448 asdl_seq *slices, *elts;
1449 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001450 if (!slices)
1451 return NULL;
1452 for (j = 0; j < NCH(n); j += 2) {
1453 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001454 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001455 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001456 if (slc->kind != Index_kind)
1457 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001458 asdl_seq_SET(slices, j / 2, slc);
1459 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001460 if (!simple) {
1461 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001462 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001463 }
1464 /* extract Index values and put them in a Tuple */
1465 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001466 if (!elts)
1467 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001468 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1469 slc = (slice_ty)asdl_seq_GET(slices, j);
1470 assert(slc->kind == Index_kind && slc->v.Index.value);
1471 asdl_seq_SET(elts, j, slc->v.Index.value);
1472 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001473 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001474 if (!e)
1475 return NULL;
1476 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001477 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001478 }
1479 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001480}
1481
1482static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001483ast_for_factor(struct compiling *c, const node *n)
1484{
1485 node *pfactor, *ppower, *patom, *pnum;
1486 expr_ty expression;
1487
1488 /* If the unary - operator is applied to a constant, don't generate
1489 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1490 constant. The peephole optimizer already does something like
1491 this but it doesn't handle the case where the constant is
1492 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1493 PyLongObject.
1494 */
1495 if (TYPE(CHILD(n, 0)) == MINUS
1496 && NCH(n) == 2
1497 && TYPE((pfactor = CHILD(n, 1))) == factor
1498 && NCH(pfactor) == 1
1499 && TYPE((ppower = CHILD(pfactor, 0))) == power
1500 && NCH(ppower) == 1
1501 && TYPE((patom = CHILD(ppower, 0))) == atom
1502 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1503 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1504 if (s == NULL)
1505 return NULL;
1506 s[0] = '-';
1507 strcpy(s + 1, STR(pnum));
1508 PyObject_FREE(STR(pnum));
1509 STR(pnum) = s;
1510 return ast_for_atom(c, patom);
1511 }
1512
1513 expression = ast_for_expr(c, CHILD(n, 1));
1514 if (!expression)
1515 return NULL;
1516
1517 switch (TYPE(CHILD(n, 0))) {
1518 case PLUS:
1519 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1520 c->c_arena);
1521 case MINUS:
1522 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1523 c->c_arena);
1524 case TILDE:
1525 return UnaryOp(Invert, expression, LINENO(n),
1526 n->n_col_offset, c->c_arena);
1527 }
1528 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1529 TYPE(CHILD(n, 0)));
1530 return NULL;
1531}
1532
1533static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001534ast_for_power(struct compiling *c, const node *n)
1535{
1536 /* power: atom trailer* ('**' factor)*
1537 */
1538 int i;
1539 expr_ty e, tmp;
1540 REQ(n, power);
1541 e = ast_for_atom(c, CHILD(n, 0));
1542 if (!e)
1543 return NULL;
1544 if (NCH(n) == 1)
1545 return e;
1546 for (i = 1; i < NCH(n); i++) {
1547 node *ch = CHILD(n, i);
1548 if (TYPE(ch) != trailer)
1549 break;
1550 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001551 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001552 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001553 tmp->lineno = e->lineno;
1554 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001555 e = tmp;
1556 }
1557 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1558 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001559 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001560 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001561 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001562 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001563 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001564 e = tmp;
1565 }
1566 return e;
1567}
1568
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569/* Do not name a variable 'expr'! Will cause a compile error.
1570*/
1571
1572static expr_ty
1573ast_for_expr(struct compiling *c, const node *n)
1574{
1575 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001576 test: or_test ['if' or_test 'else' test] | lambdef
1577 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 and_test: not_test ('and' not_test)*
1579 not_test: 'not' not_test | comparison
1580 comparison: expr (comp_op expr)*
1581 expr: xor_expr ('|' xor_expr)*
1582 xor_expr: and_expr ('^' and_expr)*
1583 and_expr: shift_expr ('&' shift_expr)*
1584 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1585 arith_expr: term (('+'|'-') term)*
1586 term: factor (('*'|'/'|'%'|'//') factor)*
1587 factor: ('+'|'-'|'~') factor | power
1588 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001589
1590 As well as modified versions that exist for backward compatibility,
1591 to explicitly allow:
1592 [ x for x in lambda: 0, lambda: 1 ]
1593 (which would be ambiguous without these extra rules)
1594
1595 old_test: or_test | old_lambdef
1596 old_lambdef: 'lambda' [vararglist] ':' old_test
1597
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 */
1599
1600 asdl_seq *seq;
1601 int i;
1602
1603 loop:
1604 switch (TYPE(n)) {
1605 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001606 case old_test:
1607 if (TYPE(CHILD(n, 0)) == lambdef ||
1608 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001610 else if (NCH(n) > 1)
1611 return ast_for_ifexpr(c, n);
1612 /* Fallthrough */
1613 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614 case and_test:
1615 if (NCH(n) == 1) {
1616 n = CHILD(n, 0);
1617 goto loop;
1618 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001619 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 if (!seq)
1621 return NULL;
1622 for (i = 0; i < NCH(n); i += 2) {
1623 expr_ty e = ast_for_expr(c, CHILD(n, i));
1624 if (!e)
1625 return NULL;
1626 asdl_seq_SET(seq, i / 2, e);
1627 }
1628 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001629 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1630 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001631 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001632 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633 case not_test:
1634 if (NCH(n) == 1) {
1635 n = CHILD(n, 0);
1636 goto loop;
1637 }
1638 else {
1639 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1640 if (!expression)
1641 return NULL;
1642
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001643 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1644 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 }
1646 case comparison:
1647 if (NCH(n) == 1) {
1648 n = CHILD(n, 0);
1649 goto loop;
1650 }
1651 else {
1652 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001653 asdl_int_seq *ops;
1654 asdl_seq *cmps;
1655 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 if (!ops)
1657 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001658 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 return NULL;
1661 }
1662 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001663 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001665 newoperator = ast_for_comp_op(CHILD(n, i));
1666 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001668 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669
1670 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001671 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001673 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001675 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 asdl_seq_SET(cmps, i / 2, expression);
1677 }
1678 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001679 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001681 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001683 return Compare(expression, ops, cmps, LINENO(n),
1684 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 }
1686 break;
1687
1688 /* The next five cases all handle BinOps. The main body of code
1689 is the same in each case, but the switch turned inside out to
1690 reuse the code for each type of operator.
1691 */
1692 case expr:
1693 case xor_expr:
1694 case and_expr:
1695 case shift_expr:
1696 case arith_expr:
1697 case term:
1698 if (NCH(n) == 1) {
1699 n = CHILD(n, 0);
1700 goto loop;
1701 }
1702 return ast_for_binop(c, n);
1703 case yield_expr: {
1704 expr_ty exp = NULL;
1705 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001706 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 if (!exp)
1708 return NULL;
1709 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001710 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001712 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 if (NCH(n) == 1) {
1714 n = CHILD(n, 0);
1715 goto loop;
1716 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001717 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001718 case power:
1719 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001721 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 return NULL;
1723 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001724 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 return NULL;
1726}
1727
1728static expr_ty
1729ast_for_call(struct compiling *c, const node *n, expr_ty func)
1730{
1731 /*
1732 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1733 | '**' test)
1734 argument: [test '='] test [gen_for] # Really [keyword '='] test
1735 */
1736
1737 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001738 asdl_seq *args;
1739 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 expr_ty vararg = NULL, kwarg = NULL;
1741
1742 REQ(n, arglist);
1743
1744 nargs = 0;
1745 nkeywords = 0;
1746 ngens = 0;
1747 for (i = 0; i < NCH(n); i++) {
1748 node *ch = CHILD(n, i);
1749 if (TYPE(ch) == argument) {
1750 if (NCH(ch) == 1)
1751 nargs++;
1752 else if (TYPE(CHILD(ch, 1)) == gen_for)
1753 ngens++;
1754 else
1755 nkeywords++;
1756 }
1757 }
1758 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001759 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760 "if not sole argument");
1761 return NULL;
1762 }
1763
1764 if (nargs + nkeywords + ngens > 255) {
1765 ast_error(n, "more than 255 arguments");
1766 return NULL;
1767 }
1768
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001769 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001771 return NULL;
1772 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001774 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 nargs = 0;
1776 nkeywords = 0;
1777 for (i = 0; i < NCH(n); i++) {
1778 node *ch = CHILD(n, i);
1779 if (TYPE(ch) == argument) {
1780 expr_ty e;
1781 if (NCH(ch) == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001782 if (nkeywords) {
1783 ast_error(CHILD(ch, 0),
1784 "non-keyword arg after keyword arg");
1785 return NULL;
1786 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787 e = ast_for_expr(c, CHILD(ch, 0));
1788 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001789 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 asdl_seq_SET(args, nargs++, e);
1791 }
1792 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1793 e = ast_for_genexp(c, ch);
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 {
1799 keyword_ty kw;
1800 identifier key;
1801
1802 /* CHILD(ch, 0) is test, but must be an identifier? */
1803 e = ast_for_expr(c, CHILD(ch, 0));
1804 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001805 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 /* f(lambda x: x[0] = 3) ends up getting parsed with
1807 * LHS test = lambda x: x[0], and RHS test = 3.
1808 * SF bug 132313 points out that complaining about a keyword
1809 * then is very confusing.
1810 */
1811 if (e->kind == Lambda_kind) {
1812 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 } else if (e->kind != Name_kind) {
1815 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 }
1818 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 e = ast_for_expr(c, CHILD(ch, 2));
1820 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001821 return NULL;
1822 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001824 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 asdl_seq_SET(keywords, nkeywords++, kw);
1826 }
1827 }
1828 else if (TYPE(ch) == STAR) {
1829 vararg = ast_for_expr(c, CHILD(n, i+1));
1830 i++;
1831 }
1832 else if (TYPE(ch) == DOUBLESTAR) {
1833 kwarg = ast_for_expr(c, CHILD(n, i+1));
1834 i++;
1835 }
1836 }
1837
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001838 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839}
1840
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001842ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001844 /* testlist_gexp: test (',' test)* [','] */
1845 /* testlist: test (',' test)* [','] */
1846 /* testlist_safe: test (',' test)+ [','] */
1847 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001849 if (TYPE(n) == testlist_gexp) {
1850 if (NCH(n) > 1)
1851 assert(TYPE(CHILD(n, 1)) != gen_for);
1852 }
1853 else {
1854 assert(TYPE(n) == testlist ||
1855 TYPE(n) == testlist_safe ||
1856 TYPE(n) == testlist1);
1857 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 if (NCH(n) == 1)
1859 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 else {
1861 asdl_seq *tmp = seq_for_testlist(c, n);
1862 if (!tmp)
1863 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001864 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001866}
1867
1868static expr_ty
1869ast_for_testlist_gexp(struct compiling *c, const node* n)
1870{
1871 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1872 /* argument: test [ gen_for ] */
1873 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001874 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001875 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001876 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001877}
1878
1879/* like ast_for_testlist() but returns a sequence */
1880static asdl_seq*
1881ast_for_class_bases(struct compiling *c, const node* n)
1882{
1883 /* testlist: test (',' test)* [','] */
1884 assert(NCH(n) > 0);
1885 REQ(n, testlist);
1886 if (NCH(n) == 1) {
1887 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001888 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001889 if (!bases)
1890 return NULL;
1891 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001892 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001893 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001894 asdl_seq_SET(bases, 0, base);
1895 return bases;
1896 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001897
1898 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899}
1900
1901static stmt_ty
1902ast_for_expr_stmt(struct compiling *c, const node *n)
1903{
1904 REQ(n, expr_stmt);
1905 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1906 | ('=' (yield_expr|testlist))*)
1907 testlist: test (',' test)* [',']
1908 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1909 | '<<=' | '>>=' | '**=' | '//='
1910 test: ... here starts the operator precendence dance
1911 */
1912
1913 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001914 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 if (!e)
1916 return NULL;
1917
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001918 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 }
1920 else if (TYPE(CHILD(n, 1)) == augassign) {
1921 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001922 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923 node *ch = CHILD(n, 0);
1924
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001925 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926 if (!expr1)
1927 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001928 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001929 switch (expr1->kind) {
1930 case GeneratorExp_kind:
1931 ast_error(ch, "augmented assignment to generator "
1932 "expression not possible");
1933 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001934 case Yield_kind:
1935 ast_error(ch, "augmented assignment to yield "
1936 "expression not possible");
1937 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001938 case Name_kind: {
1939 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1940 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1941 ast_error(ch, "assignment to None");
1942 return NULL;
1943 }
1944 break;
1945 }
1946 case Attribute_kind:
1947 case Subscript_kind:
1948 break;
1949 default:
1950 ast_error(ch, "illegal expression for augmented "
1951 "assignment");
1952 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001954 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955
1956 ch = CHILD(n, 2);
1957 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001958 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959 else
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001960 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001961 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 return NULL;
1963
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001964 newoperator = ast_for_augassign(CHILD(n, 1));
1965 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966 return NULL;
1967
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001968 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 }
1970 else {
1971 int i;
1972 asdl_seq *targets;
1973 node *value;
1974 expr_ty expression;
1975
1976 /* a normal assignment */
1977 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001978 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 if (!targets)
1980 return NULL;
1981 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001982 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 node *ch = CHILD(n, i);
1984 if (TYPE(ch) == yield_expr) {
1985 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001986 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001988 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989
1990 /* set context to assign */
1991 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001992 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993
Neal Norwitz84456bd2005-12-18 03:16:20 +00001994 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996
1997 asdl_seq_SET(targets, i / 2, e);
1998 }
1999 value = CHILD(n, NCH(n) - 1);
2000 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002001 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 else
2003 expression = ast_for_expr(c, value);
2004 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002005 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002006 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008}
2009
2010static stmt_ty
2011ast_for_print_stmt(struct compiling *c, const node *n)
2012{
2013 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2014 | '>>' test [ (',' test)+ [','] ] )
2015 */
2016 expr_ty dest = NULL, expression;
2017 asdl_seq *seq;
2018 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002019 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020
2021 REQ(n, print_stmt);
2022 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2023 dest = ast_for_expr(c, CHILD(n, 2));
2024 if (!dest)
2025 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002026 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002028 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002030 return NULL;
2031 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002033 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002035 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 }
2037 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002038 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039}
2040
2041static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002042ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043{
2044 asdl_seq *seq;
2045 int i;
2046 expr_ty e;
2047
2048 REQ(n, exprlist);
2049
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002050 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 if (!seq)
2052 return NULL;
2053 for (i = 0; i < NCH(n); i += 2) {
2054 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002055 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002056 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002057 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002058 if (context && !set_context(e, context, CHILD(n, i)))
2059 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 }
2061 return seq;
2062}
2063
2064static stmt_ty
2065ast_for_del_stmt(struct compiling *c, const node *n)
2066{
2067 asdl_seq *expr_list;
2068
2069 /* del_stmt: 'del' exprlist */
2070 REQ(n, del_stmt);
2071
2072 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2073 if (!expr_list)
2074 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002075 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076}
2077
2078static stmt_ty
2079ast_for_flow_stmt(struct compiling *c, const node *n)
2080{
2081 /*
2082 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2083 | yield_stmt
2084 break_stmt: 'break'
2085 continue_stmt: 'continue'
2086 return_stmt: 'return' [testlist]
2087 yield_stmt: yield_expr
2088 yield_expr: 'yield' testlist
2089 raise_stmt: 'raise' [test [',' test [',' test]]]
2090 */
2091 node *ch;
2092
2093 REQ(n, flow_stmt);
2094 ch = CHILD(n, 0);
2095 switch (TYPE(ch)) {
2096 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002097 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002099 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 case yield_stmt: { /* will reduce to yield_expr */
2101 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2102 if (!exp)
2103 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002104 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 }
2106 case return_stmt:
2107 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002108 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002110 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 if (!expression)
2112 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002113 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 }
2115 case raise_stmt:
2116 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002117 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 else if (NCH(ch) == 2) {
2119 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2120 if (!expression)
2121 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002122 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 }
2124 else if (NCH(ch) == 4) {
2125 expr_ty expr1, expr2;
2126
2127 expr1 = ast_for_expr(c, CHILD(ch, 1));
2128 if (!expr1)
2129 return NULL;
2130 expr2 = ast_for_expr(c, CHILD(ch, 3));
2131 if (!expr2)
2132 return NULL;
2133
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002134 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 }
2136 else if (NCH(ch) == 6) {
2137 expr_ty expr1, expr2, expr3;
2138
2139 expr1 = ast_for_expr(c, CHILD(ch, 1));
2140 if (!expr1)
2141 return NULL;
2142 expr2 = ast_for_expr(c, CHILD(ch, 3));
2143 if (!expr2)
2144 return NULL;
2145 expr3 = ast_for_expr(c, CHILD(ch, 5));
2146 if (!expr3)
2147 return NULL;
2148
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002149 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 }
2151 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002152 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 "unexpected flow_stmt: %d", TYPE(ch));
2154 return NULL;
2155 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002156
2157 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2158 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159}
2160
2161static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002162alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163{
2164 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002165 import_as_name: NAME ['as' NAME]
2166 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 dotted_name: NAME ('.' NAME)*
2168 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002169 PyObject *str;
2170
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 loop:
2172 switch (TYPE(n)) {
2173 case import_as_name:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002174 str = NULL;
2175 if (NCH(n) == 3) {
2176 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2177 ast_error(n, "must use 'as' in import");
2178 return NULL;
2179 }
2180 str = NEW_IDENTIFIER(CHILD(n, 2));
2181 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002182 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183 case dotted_as_name:
2184 if (NCH(n) == 1) {
2185 n = CHILD(n, 0);
2186 goto loop;
2187 }
2188 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002189 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002190 if (!a)
2191 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002192 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2193 ast_error(n, "must use 'as' in import");
2194 return NULL;
2195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 assert(!a->asname);
2197 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2198 return a;
2199 }
2200 break;
2201 case dotted_name:
2202 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002203 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 else {
2205 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002206 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002207 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208 char *s;
2209
2210 len = 0;
2211 for (i = 0; i < NCH(n); i += 2)
2212 /* length of string plus one for the dot */
2213 len += strlen(STR(CHILD(n, i))) + 1;
2214 len--; /* the last name doesn't have a dot */
2215 str = PyString_FromStringAndSize(NULL, len);
2216 if (!str)
2217 return NULL;
2218 s = PyString_AS_STRING(str);
2219 if (!s)
2220 return NULL;
2221 for (i = 0; i < NCH(n); i += 2) {
2222 char *sch = STR(CHILD(n, i));
2223 strcpy(s, STR(CHILD(n, i)));
2224 s += strlen(sch);
2225 *s++ = '.';
2226 }
2227 --s;
2228 *s = '\0';
2229 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002230 PyArena_AddPyObject(c->c_arena, str);
2231 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 }
2233 break;
2234 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002235 str = PyString_InternFromString("*");
2236 PyArena_AddPyObject(c->c_arena, str);
2237 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002239 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 "unexpected import name: %d", TYPE(n));
2241 return NULL;
2242 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002243
2244 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 return NULL;
2246}
2247
2248static stmt_ty
2249ast_for_import_stmt(struct compiling *c, const node *n)
2250{
2251 /*
2252 import_stmt: import_name | import_from
2253 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002254 import_from: 'from' ('.'* dotted_name | '.') 'import'
2255 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002257 int lineno;
2258 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 int i;
2260 asdl_seq *aliases;
2261
2262 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002263 lineno = LINENO(n);
2264 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002266 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002268 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002269 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270 if (!aliases)
2271 return NULL;
2272 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002273 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002274 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 asdl_seq_SET(aliases, i / 2, import_alias);
2277 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002278 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002280 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002282 int idx, ndots = 0;
2283 alias_ty mod = NULL;
2284 identifier modname;
2285
2286 /* Count the number of dots (for relative imports) and check for the
2287 optional module name */
2288 for (idx = 1; idx < NCH(n); idx++) {
2289 if (TYPE(CHILD(n, idx)) == dotted_name) {
2290 mod = alias_for_import_name(c, CHILD(n, idx));
2291 idx++;
2292 break;
2293 } else if (TYPE(CHILD(n, idx)) != DOT) {
2294 break;
2295 }
2296 ndots++;
2297 }
2298 idx++; /* skip over the 'import' keyword */
2299 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002300 case STAR:
2301 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002302 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002303 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002304 if (ndots) {
2305 ast_error(n, "'import *' not allowed with 'from .'");
2306 return NULL;
2307 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002308 break;
2309 case LPAR:
2310 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002311 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002312 n_children = NCH(n);
2313 break;
2314 case import_as_names:
2315 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002316 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002317 n_children = NCH(n);
2318 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319 ast_error(n, "trailing comma not allowed without"
2320 " surrounding parentheses");
2321 return NULL;
2322 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002323 break;
2324 default:
2325 ast_error(n, "Unexpected node-type in from-import");
2326 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002327 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002329 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002330 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332
2333 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002334 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002335 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002336 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002338 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002340 else {
2341 for (i = 0; i < NCH(n); i += 2) {
2342 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2343 if (!import_alias)
2344 return NULL;
2345 asdl_seq_SET(aliases, i / 2, import_alias);
2346 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002348 if (mod != NULL)
2349 modname = mod->name;
2350 else
2351 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002352 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002353 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 }
Neal Norwitz79792652005-11-14 04:25:03 +00002355 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 "unknown import statement: starts with command '%s'",
2357 STR(CHILD(n, 0)));
2358 return NULL;
2359}
2360
2361static stmt_ty
2362ast_for_global_stmt(struct compiling *c, const node *n)
2363{
2364 /* global_stmt: 'global' NAME (',' NAME)* */
2365 identifier name;
2366 asdl_seq *s;
2367 int i;
2368
2369 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002370 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371 if (!s)
2372 return NULL;
2373 for (i = 1; i < NCH(n); i += 2) {
2374 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002375 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 asdl_seq_SET(s, i / 2, name);
2378 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002379 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380}
2381
2382static stmt_ty
2383ast_for_exec_stmt(struct compiling *c, const node *n)
2384{
2385 expr_ty expr1, globals = NULL, locals = NULL;
2386 int n_children = NCH(n);
2387 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002388 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 "poorly formed 'exec' statement: %d parts to statement",
2390 n_children);
2391 return NULL;
2392 }
2393
2394 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2395 REQ(n, exec_stmt);
2396 expr1 = ast_for_expr(c, CHILD(n, 1));
2397 if (!expr1)
2398 return NULL;
2399 if (n_children >= 4) {
2400 globals = ast_for_expr(c, CHILD(n, 3));
2401 if (!globals)
2402 return NULL;
2403 }
2404 if (n_children == 6) {
2405 locals = ast_for_expr(c, CHILD(n, 5));
2406 if (!locals)
2407 return NULL;
2408 }
2409
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002410 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411}
2412
2413static stmt_ty
2414ast_for_assert_stmt(struct compiling *c, const node *n)
2415{
2416 /* assert_stmt: 'assert' test [',' test] */
2417 REQ(n, assert_stmt);
2418 if (NCH(n) == 2) {
2419 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2420 if (!expression)
2421 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002422 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 }
2424 else if (NCH(n) == 4) {
2425 expr_ty expr1, expr2;
2426
2427 expr1 = ast_for_expr(c, CHILD(n, 1));
2428 if (!expr1)
2429 return NULL;
2430 expr2 = ast_for_expr(c, CHILD(n, 3));
2431 if (!expr2)
2432 return NULL;
2433
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002434 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435 }
Neal Norwitz79792652005-11-14 04:25:03 +00002436 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 "improper number of parts to 'assert' statement: %d",
2438 NCH(n));
2439 return NULL;
2440}
2441
2442static asdl_seq *
2443ast_for_suite(struct compiling *c, const node *n)
2444{
2445 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002446 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 stmt_ty s;
2448 int i, total, num, end, pos = 0;
2449 node *ch;
2450
2451 REQ(n, suite);
2452
2453 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002454 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 if (!seq)
2456 return NULL;
2457 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2458 n = CHILD(n, 0);
2459 /* simple_stmt always ends with a NEWLINE,
2460 and may have a trailing SEMI
2461 */
2462 end = NCH(n) - 1;
2463 if (TYPE(CHILD(n, end - 1)) == SEMI)
2464 end--;
2465 /* loop by 2 to skip semi-colons */
2466 for (i = 0; i < end; i += 2) {
2467 ch = CHILD(n, i);
2468 s = ast_for_stmt(c, ch);
2469 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002470 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 asdl_seq_SET(seq, pos++, s);
2472 }
2473 }
2474 else {
2475 for (i = 2; i < (NCH(n) - 1); i++) {
2476 ch = CHILD(n, i);
2477 REQ(ch, stmt);
2478 num = num_stmts(ch);
2479 if (num == 1) {
2480 /* small_stmt or compound_stmt with only one child */
2481 s = ast_for_stmt(c, ch);
2482 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002483 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 asdl_seq_SET(seq, pos++, s);
2485 }
2486 else {
2487 int j;
2488 ch = CHILD(ch, 0);
2489 REQ(ch, simple_stmt);
2490 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002491 /* statement terminates with a semi-colon ';' */
2492 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002493 assert((j + 1) == NCH(ch));
2494 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002495 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 s = ast_for_stmt(c, CHILD(ch, j));
2497 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002498 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 asdl_seq_SET(seq, pos++, s);
2500 }
2501 }
2502 }
2503 }
2504 assert(pos == seq->size);
2505 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506}
2507
2508static stmt_ty
2509ast_for_if_stmt(struct compiling *c, const node *n)
2510{
2511 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2512 ['else' ':' suite]
2513 */
2514 char *s;
2515
2516 REQ(n, if_stmt);
2517
2518 if (NCH(n) == 4) {
2519 expr_ty expression;
2520 asdl_seq *suite_seq;
2521
2522 expression = ast_for_expr(c, CHILD(n, 1));
2523 if (!expression)
2524 return NULL;
2525 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002526 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 return NULL;
2528
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002529 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 s = STR(CHILD(n, 4));
2533 /* s[2], the third character in the string, will be
2534 's' for el_s_e, or
2535 'i' for el_i_f
2536 */
2537 if (s[2] == 's') {
2538 expr_ty expression;
2539 asdl_seq *seq1, *seq2;
2540
2541 expression = ast_for_expr(c, CHILD(n, 1));
2542 if (!expression)
2543 return NULL;
2544 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002545 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 return NULL;
2547 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002548 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 return NULL;
2550
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002551 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 }
2553 else if (s[2] == 'i') {
2554 int i, n_elif, has_else = 0;
2555 asdl_seq *orelse = NULL;
2556 n_elif = NCH(n) - 4;
2557 /* must reference the child n_elif+1 since 'else' token is third,
2558 not fourth, child from the end. */
2559 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2560 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2561 has_else = 1;
2562 n_elif -= 3;
2563 }
2564 n_elif /= 4;
2565
2566 if (has_else) {
2567 expr_ty expression;
2568 asdl_seq *seq1, *seq2;
2569
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002570 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 if (!orelse)
2572 return NULL;
2573 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002574 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002577 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002580 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582
2583 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002584 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002585 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 /* the just-created orelse handled the last elif */
2587 n_elif--;
2588 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589
2590 for (i = 0; i < n_elif; i++) {
2591 int off = 5 + (n_elif - i - 1) * 4;
2592 expr_ty expression;
2593 asdl_seq *suite_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002594 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2595 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 return NULL;
2597 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002598 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002601 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002604 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002606 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002607 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 }
2609 return If(ast_for_expr(c, CHILD(n, 1)),
2610 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002611 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002613
2614 PyErr_Format(PyExc_SystemError,
2615 "unexpected token in 'if' statement: %s", s);
2616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617}
2618
2619static stmt_ty
2620ast_for_while_stmt(struct compiling *c, const node *n)
2621{
2622 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2623 REQ(n, while_stmt);
2624
2625 if (NCH(n) == 4) {
2626 expr_ty expression;
2627 asdl_seq *suite_seq;
2628
2629 expression = ast_for_expr(c, CHILD(n, 1));
2630 if (!expression)
2631 return NULL;
2632 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002633 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002635 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 }
2637 else if (NCH(n) == 7) {
2638 expr_ty expression;
2639 asdl_seq *seq1, *seq2;
2640
2641 expression = ast_for_expr(c, CHILD(n, 1));
2642 if (!expression)
2643 return NULL;
2644 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002645 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 return NULL;
2647 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002648 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 return NULL;
2650
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002651 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002653
2654 PyErr_Format(PyExc_SystemError,
2655 "wrong number of tokens for 'while' statement: %d",
2656 NCH(n));
2657 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658}
2659
2660static stmt_ty
2661ast_for_for_stmt(struct compiling *c, const node *n)
2662{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002663 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664 expr_ty expression;
2665 expr_ty target;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002666 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2668 REQ(n, for_stmt);
2669
2670 if (NCH(n) == 9) {
2671 seq = ast_for_suite(c, CHILD(n, 8));
2672 if (!seq)
2673 return NULL;
2674 }
2675
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002676 node_target = CHILD(n, 1);
2677 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002678 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002680 /* Check the # of children rather than the length of _target, since
2681 for x, in ... has 1 element in _target, but still requires a Tuple. */
2682 if (NCH(node_target) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002683 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002685 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002687 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002688 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689 return NULL;
2690 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002691 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 return NULL;
2693
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002694 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2695 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696}
2697
2698static excepthandler_ty
2699ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2700{
2701 /* except_clause: 'except' [test [',' test]] */
2702 REQ(exc, except_clause);
2703 REQ(body, suite);
2704
2705 if (NCH(exc) == 1) {
2706 asdl_seq *suite_seq = ast_for_suite(c, body);
2707 if (!suite_seq)
2708 return NULL;
2709
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002710 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2711 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712 }
2713 else if (NCH(exc) == 2) {
2714 expr_ty expression;
2715 asdl_seq *suite_seq;
2716
2717 expression = ast_for_expr(c, CHILD(exc, 1));
2718 if (!expression)
2719 return NULL;
2720 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002721 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 return NULL;
2723
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002724 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2725 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 }
2727 else if (NCH(exc) == 4) {
2728 asdl_seq *suite_seq;
2729 expr_ty expression;
2730 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2731 if (!e)
2732 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002733 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 return NULL;
2735 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002736 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 return NULL;
2738 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002739 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 return NULL;
2741
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002742 return excepthandler(expression, e, suite_seq, LINENO(exc),
2743 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002745
2746 PyErr_Format(PyExc_SystemError,
2747 "wrong number of children for 'except' clause: %d",
2748 NCH(exc));
2749 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750}
2751
2752static stmt_ty
2753ast_for_try_stmt(struct compiling *c, const node *n)
2754{
Neal Norwitzf599f422005-12-17 21:33:47 +00002755 const int nch = NCH(n);
2756 int n_except = (nch - 3)/3;
2757 asdl_seq *body, *orelse = NULL, *finally = NULL;
2758
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 REQ(n, try_stmt);
2760
Neal Norwitzf599f422005-12-17 21:33:47 +00002761 body = ast_for_suite(c, CHILD(n, 2));
2762 if (body == NULL)
2763 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764
Neal Norwitzf599f422005-12-17 21:33:47 +00002765 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2766 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2767 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2768 /* we can assume it's an "else",
2769 because nch >= 9 for try-else-finally and
2770 it would otherwise have a type of except_clause */
2771 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2772 if (orelse == NULL)
2773 return NULL;
2774 n_except--;
2775 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776
Neal Norwitzf599f422005-12-17 21:33:47 +00002777 finally = ast_for_suite(c, CHILD(n, nch - 1));
2778 if (finally == NULL)
2779 return NULL;
2780 n_except--;
2781 }
2782 else {
2783 /* we can assume it's an "else",
2784 otherwise it would have a type of except_clause */
2785 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2786 if (orelse == NULL)
2787 return NULL;
2788 n_except--;
2789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002791 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002792 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 return NULL;
2794 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002795
2796 if (n_except > 0) {
2797 int i;
2798 stmt_ty except_st;
2799 /* process except statements to create a try ... except */
2800 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2801 if (handlers == NULL)
2802 return NULL;
2803
2804 for (i = 0; i < n_except; i++) {
2805 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2806 CHILD(n, 5 + i * 3));
2807 if (!e)
2808 return NULL;
2809 asdl_seq_SET(handlers, i, e);
2810 }
2811
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002812 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2813 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002814 if (!finally)
2815 return except_st;
2816
2817 /* if a 'finally' is present too, we nest the TryExcept within a
2818 TryFinally to emulate try ... except ... finally */
2819 body = asdl_seq_new(1, c->c_arena);
2820 if (body == NULL)
2821 return NULL;
2822 asdl_seq_SET(body, 0, except_st);
2823 }
2824
2825 /* must be a try ... finally (except clauses are in body, if any exist) */
2826 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002827 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828}
2829
Guido van Rossumc2e20742006-02-27 22:32:47 +00002830static expr_ty
2831ast_for_with_var(struct compiling *c, const node *n)
2832{
2833 REQ(n, with_var);
2834 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2835 ast_error(n, "expected \"with [expr] as [var]\"");
2836 return NULL;
2837 }
2838 return ast_for_expr(c, CHILD(n, 1));
2839}
2840
2841/* with_stmt: 'with' test [ with_var ] ':' suite */
2842static stmt_ty
2843ast_for_with_stmt(struct compiling *c, const node *n)
2844{
2845 expr_ty context_expr, optional_vars = NULL;
2846 int suite_index = 3; /* skip 'with', test, and ':' */
2847 asdl_seq *suite_seq;
2848
2849 assert(TYPE(n) == with_stmt);
2850 context_expr = ast_for_expr(c, CHILD(n, 1));
2851 if (TYPE(CHILD(n, 2)) == with_var) {
2852 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2853
2854 if (!optional_vars) {
2855 return NULL;
2856 }
2857 if (!set_context(optional_vars, Store, n)) {
2858 return NULL;
2859 }
2860 suite_index = 4;
2861 }
2862
2863 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2864 if (!suite_seq) {
2865 return NULL;
2866 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002867 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2868 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002869}
2870
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871static stmt_ty
2872ast_for_classdef(struct compiling *c, const node *n)
2873{
2874 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 asdl_seq *bases, *s;
2876
2877 REQ(n, classdef);
2878
2879 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2880 ast_error(n, "assignment to None");
2881 return NULL;
2882 }
2883
2884 if (NCH(n) == 4) {
2885 s = ast_for_suite(c, CHILD(n, 3));
2886 if (!s)
2887 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002888 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2889 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 }
2891 /* check for empty base list */
2892 if (TYPE(CHILD(n,3)) == RPAR) {
2893 s = ast_for_suite(c, CHILD(n,5));
2894 if (!s)
2895 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002896 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2897 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898 }
2899
2900 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002901 bases = ast_for_class_bases(c, CHILD(n, 3));
2902 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904
2905 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002906 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002908 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2909 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910}
2911
2912static stmt_ty
2913ast_for_stmt(struct compiling *c, const node *n)
2914{
2915 if (TYPE(n) == stmt) {
2916 assert(NCH(n) == 1);
2917 n = CHILD(n, 0);
2918 }
2919 if (TYPE(n) == simple_stmt) {
2920 assert(num_stmts(n) == 1);
2921 n = CHILD(n, 0);
2922 }
2923 if (TYPE(n) == small_stmt) {
2924 REQ(n, small_stmt);
2925 n = CHILD(n, 0);
2926 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2927 | flow_stmt | import_stmt | global_stmt | exec_stmt
2928 | assert_stmt
2929 */
2930 switch (TYPE(n)) {
2931 case expr_stmt:
2932 return ast_for_expr_stmt(c, n);
2933 case print_stmt:
2934 return ast_for_print_stmt(c, n);
2935 case del_stmt:
2936 return ast_for_del_stmt(c, n);
2937 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002938 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 case flow_stmt:
2940 return ast_for_flow_stmt(c, n);
2941 case import_stmt:
2942 return ast_for_import_stmt(c, n);
2943 case global_stmt:
2944 return ast_for_global_stmt(c, n);
2945 case exec_stmt:
2946 return ast_for_exec_stmt(c, n);
2947 case assert_stmt:
2948 return ast_for_assert_stmt(c, n);
2949 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002950 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2952 TYPE(n), NCH(n));
2953 return NULL;
2954 }
2955 }
2956 else {
2957 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2958 | funcdef | classdef
2959 */
2960 node *ch = CHILD(n, 0);
2961 REQ(n, compound_stmt);
2962 switch (TYPE(ch)) {
2963 case if_stmt:
2964 return ast_for_if_stmt(c, ch);
2965 case while_stmt:
2966 return ast_for_while_stmt(c, ch);
2967 case for_stmt:
2968 return ast_for_for_stmt(c, ch);
2969 case try_stmt:
2970 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002971 case with_stmt:
2972 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 case funcdef:
2974 return ast_for_funcdef(c, ch);
2975 case classdef:
2976 return ast_for_classdef(c, ch);
2977 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002978 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2980 TYPE(n), NCH(n));
2981 return NULL;
2982 }
2983 }
2984}
2985
2986static PyObject *
2987parsenumber(const char *s)
2988{
2989 const char *end;
2990 long x;
2991 double dx;
2992#ifndef WITHOUT_COMPLEX
2993 Py_complex c;
2994 int imflag;
2995#endif
2996
2997 errno = 0;
2998 end = s + strlen(s) - 1;
2999#ifndef WITHOUT_COMPLEX
3000 imflag = *end == 'j' || *end == 'J';
3001#endif
3002 if (*end == 'l' || *end == 'L')
3003 return PyLong_FromString((char *)s, (char **)0, 0);
3004 if (s[0] == '0') {
3005 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3006 if (x < 0 && errno == 0) {
3007 return PyLong_FromString((char *)s,
3008 (char **)0,
3009 0);
3010 }
3011 }
3012 else
3013 x = PyOS_strtol((char *)s, (char **)&end, 0);
3014 if (*end == '\0') {
3015 if (errno != 0)
3016 return PyLong_FromString((char *)s, (char **)0, 0);
3017 return PyInt_FromLong(x);
3018 }
3019 /* XXX Huge floats may silently fail */
3020#ifndef WITHOUT_COMPLEX
3021 if (imflag) {
3022 c.real = 0.;
3023 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003024 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 PyFPE_END_PROTECT(c)
3026 return PyComplex_FromCComplex(c);
3027 }
3028 else
3029#endif
3030 {
3031 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003032 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033 PyFPE_END_PROTECT(dx)
3034 return PyFloat_FromDouble(dx);
3035 }
3036}
3037
3038static PyObject *
3039decode_utf8(const char **sPtr, const char *end, char* encoding)
3040{
3041#ifndef Py_USING_UNICODE
3042 Py_FatalError("decode_utf8 should not be called in this build.");
3043 return NULL;
3044#else
3045 PyObject *u, *v;
3046 char *s, *t;
3047 t = s = (char *)*sPtr;
3048 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3049 while (s < end && (*s & 0x80)) s++;
3050 *sPtr = s;
3051 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3052 if (u == NULL)
3053 return NULL;
3054 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3055 Py_DECREF(u);
3056 return v;
3057#endif
3058}
3059
3060static PyObject *
3061decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3062{
3063 PyObject *v, *u;
3064 char *buf;
3065 char *p;
3066 const char *end;
3067 if (encoding == NULL) {
3068 buf = (char *)s;
3069 u = NULL;
3070 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3071 buf = (char *)s;
3072 u = NULL;
3073 } else {
3074 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3075 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3076 if (u == NULL)
3077 return NULL;
3078 p = buf = PyString_AsString(u);
3079 end = s + len;
3080 while (s < end) {
3081 if (*s == '\\') {
3082 *p++ = *s++;
3083 if (*s & 0x80) {
3084 strcpy(p, "u005c");
3085 p += 5;
3086 }
3087 }
3088 if (*s & 0x80) { /* XXX inefficient */
3089 PyObject *w;
3090 char *r;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003091 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092 w = decode_utf8(&s, end, "utf-16-be");
3093 if (w == NULL) {
3094 Py_DECREF(u);
3095 return NULL;
3096 }
3097 r = PyString_AsString(w);
3098 rn = PyString_Size(w);
3099 assert(rn % 2 == 0);
3100 for (i = 0; i < rn; i += 2) {
3101 sprintf(p, "\\u%02x%02x",
3102 r[i + 0] & 0xFF,
3103 r[i + 1] & 0xFF);
3104 p += 6;
3105 }
3106 Py_DECREF(w);
3107 } else {
3108 *p++ = *s++;
3109 }
3110 }
3111 len = p - buf;
3112 s = buf;
3113 }
3114 if (rawmode)
3115 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3116 else
3117 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3118 Py_XDECREF(u);
3119 return v;
3120}
3121
3122/* s is a Python string literal, including the bracketing quote characters,
3123 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3124 * parsestr parses it, and returns the decoded Python string object.
3125 */
3126static PyObject *
3127parsestr(const char *s, const char *encoding)
3128{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003130 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 int rawmode = 0;
3132 int need_encoding;
3133 int unicode = 0;
3134
3135 if (isalpha(quote) || quote == '_') {
3136 if (quote == 'u' || quote == 'U') {
3137 quote = *++s;
3138 unicode = 1;
3139 }
3140 if (quote == 'r' || quote == 'R') {
3141 quote = *++s;
3142 rawmode = 1;
3143 }
3144 }
3145 if (quote != '\'' && quote != '\"') {
3146 PyErr_BadInternalCall();
3147 return NULL;
3148 }
3149 s++;
3150 len = strlen(s);
3151 if (len > INT_MAX) {
3152 PyErr_SetString(PyExc_OverflowError,
3153 "string to parse is too long");
3154 return NULL;
3155 }
3156 if (s[--len] != quote) {
3157 PyErr_BadInternalCall();
3158 return NULL;
3159 }
3160 if (len >= 4 && s[0] == quote && s[1] == quote) {
3161 s += 2;
3162 len -= 2;
3163 if (s[--len] != quote || s[--len] != quote) {
3164 PyErr_BadInternalCall();
3165 return NULL;
3166 }
3167 }
3168#ifdef Py_USING_UNICODE
3169 if (unicode || Py_UnicodeFlag) {
3170 return decode_unicode(s, len, rawmode, encoding);
3171 }
3172#endif
3173 need_encoding = (encoding != NULL &&
3174 strcmp(encoding, "utf-8") != 0 &&
3175 strcmp(encoding, "iso-8859-1") != 0);
3176 if (rawmode || strchr(s, '\\') == NULL) {
3177 if (need_encoding) {
3178#ifndef Py_USING_UNICODE
3179 /* This should not happen - we never see any other
3180 encoding. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003181 Py_FatalError(
3182 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003184 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 if (u == NULL)
3186 return NULL;
3187 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3188 Py_DECREF(u);
3189 return v;
3190#endif
3191 } else {
3192 return PyString_FromStringAndSize(s, len);
3193 }
3194 }
3195
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003196 return PyString_DecodeEscape(s, len, NULL, unicode,
3197 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198}
3199
3200/* Build a Python string object out of a STRING atom. This takes care of
3201 * compile-time literal catenation, calling parsestr() on each piece, and
3202 * pasting the intermediate results together.
3203 */
3204static PyObject *
3205parsestrplus(struct compiling *c, const node *n)
3206{
3207 PyObject *v;
3208 int i;
3209 REQ(CHILD(n, 0), STRING);
3210 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3211 /* String literal concatenation */
3212 for (i = 1; i < NCH(n); i++) {
3213 PyObject *s;
3214 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3215 if (s == NULL)
3216 goto onError;
3217 if (PyString_Check(v) && PyString_Check(s)) {
3218 PyString_ConcatAndDel(&v, s);
3219 if (v == NULL)
3220 goto onError;
3221 }
3222#ifdef Py_USING_UNICODE
3223 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003224 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226 Py_DECREF(v);
3227 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003228 if (v == NULL)
3229 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 }
3231#endif
3232 }
3233 }
3234 return v;
3235
3236 onError:
3237 Py_XDECREF(v);
3238 return NULL;
3239}