blob: ea8c10366eaa0ef7fd9a837364918d39cdbe8283 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* XXX TO DO
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
21 - syntax errors
22*/
23
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024/* Data structure used internally */
25struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000026 char *c_encoding; /* source encoding */
27 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028};
29
30static asdl_seq *seq_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_expr(struct compiling *, const node *);
32static stmt_ty ast_for_stmt(struct compiling *, const node *);
33static asdl_seq *ast_for_suite(struct compiling *, const node *);
34static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 value = Py_BuildValue("(OO)", errstr, tmp);
111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Neal Norwitze98ccf62006-03-23 05:39:47 +0000194 if (TYPE(n) == encoding_decl) {
195 ast_error(n, "encoding declaration in Unicode string");
196 goto error;
197 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 } else if (TYPE(n) == encoding_decl) {
199 c.c_encoding = STR(n);
200 n = CHILD(n, 0);
201 } else {
202 c.c_encoding = NULL;
203 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000204 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Jeremy Hyltona8293132006-02-28 17:58:27 +0000206 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 switch (TYPE(n)) {
208 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000209 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 if (!stmts)
211 return NULL;
212 for (i = 0; i < NCH(n) - 1; i++) {
213 ch = CHILD(n, i);
214 if (TYPE(ch) == NEWLINE)
215 continue;
216 REQ(ch, stmt);
217 num = num_stmts(ch);
218 if (num == 1) {
219 s = ast_for_stmt(&c, ch);
220 if (!s)
221 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000222 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223 }
224 else {
225 ch = CHILD(ch, 0);
226 REQ(ch, simple_stmt);
227 for (j = 0; j < num; j++) {
228 s = ast_for_stmt(&c, CHILD(ch, j * 2));
229 if (!s)
230 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000231 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 }
233 }
234 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000235 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 case eval_input: {
237 expr_ty testlist_ast;
238
239 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000240 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241 if (!testlist_ast)
242 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 }
245 case single_input:
246 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 if (!stmts)
249 goto error;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
251 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 }
254 else {
255 n = CHILD(n, 0);
256 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000257 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 if (!stmts)
259 goto error;
260 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000261 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262 if (!s)
263 goto error;
264 asdl_seq_SET(stmts, 0, s);
265 }
266 else {
267 /* Only a simple_stmt can contain multiple statements. */
268 REQ(n, simple_stmt);
269 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 if (TYPE(CHILD(n, i)) == NEWLINE)
271 break;
272 s = ast_for_stmt(&c, CHILD(n, i));
273 if (!s)
274 goto error;
275 asdl_seq_SET(stmts, i / 2, s);
276 }
277 }
278
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000279 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 }
281 default:
282 goto error;
283 }
284 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 ast_error_finish(filename);
286 return NULL;
287}
288
289/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
290*/
291
292static operator_ty
293get_operator(const node *n)
294{
295 switch (TYPE(n)) {
296 case VBAR:
297 return BitOr;
298 case CIRCUMFLEX:
299 return BitXor;
300 case AMPER:
301 return BitAnd;
302 case LEFTSHIFT:
303 return LShift;
304 case RIGHTSHIFT:
305 return RShift;
306 case PLUS:
307 return Add;
308 case MINUS:
309 return Sub;
310 case STAR:
311 return Mult;
312 case SLASH:
313 return Div;
314 case DOUBLESLASH:
315 return FloorDiv;
316 case PERCENT:
317 return Mod;
318 default:
319 return 0;
320 }
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
342 that expressions in an augmented assignment have no context.
343 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000344 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345 */
346 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
348 switch (e->kind) {
349 case Attribute_kind:
350 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000351 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 return ast_error(n, "assignment to None");
353 }
354 e->v.Attribute.ctx = ctx;
355 break;
356 case Subscript_kind:
357 e->v.Subscript.ctx = ctx;
358 break;
359 case Name_kind:
360 if (ctx == Store &&
361 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
362 return ast_error(n, "assignment to None");
363 }
364 e->v.Name.ctx = ctx;
365 break;
366 case List_kind:
367 e->v.List.ctx = ctx;
368 s = e->v.List.elts;
369 break;
370 case Tuple_kind:
371 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
372 return ast_error(n, "can't assign to ()");
373 e->v.Tuple.ctx = ctx;
374 s = e->v.Tuple.elts;
375 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 case Lambda_kind:
377 expr_name = "lambda";
378 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000380 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000384 case UnaryOp_kind:
385 expr_name = "operator";
386 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000388 expr_name = "generator expression";
389 break;
390 case ListComp_kind:
391 expr_name = "list comprehension";
392 break;
393 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394 case Num_kind:
395 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000396 expr_name = "literal";
397 break;
398 case Compare_kind:
399 expr_name = "comparison";
400 break;
401 case Repr_kind:
402 expr_name = "repr";
403 break;
404 default:
405 PyErr_Format(PyExc_SystemError,
406 "unexpected expression in assignment %d (line %d)",
407 e->kind, e->lineno);
408 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000410 /* Check for error string set by switch */
411 if (expr_name) {
412 char buf[300];
413 PyOS_snprintf(buf, sizeof(buf),
414 "can't %s %s",
415 ctx == Store ? "assign to" : "delete",
416 expr_name);
417 return ast_error(n, buf);
418 }
419
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000421 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422 */
423 if (s) {
424 int i;
425
426 for (i = 0; i < asdl_seq_LEN(s); i++) {
427 if (!set_context(asdl_seq_GET(s, i), ctx, n))
428 return 0;
429 }
430 }
431 return 1;
432}
433
434static operator_ty
435ast_for_augassign(const node *n)
436{
437 REQ(n, augassign);
438 n = CHILD(n, 0);
439 switch (STR(n)[0]) {
440 case '+':
441 return Add;
442 case '-':
443 return Sub;
444 case '/':
445 if (STR(n)[1] == '/')
446 return FloorDiv;
447 else
448 return Div;
449 case '%':
450 return Mod;
451 case '<':
452 return LShift;
453 case '>':
454 return RShift;
455 case '&':
456 return BitAnd;
457 case '^':
458 return BitXor;
459 case '|':
460 return BitOr;
461 case '*':
462 if (STR(n)[1] == '*')
463 return Pow;
464 else
465 return Mult;
466 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000467 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468 return 0;
469 }
470}
471
472static cmpop_ty
473ast_for_comp_op(const node *n)
474{
475 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
476 |'is' 'not'
477 */
478 REQ(n, comp_op);
479 if (NCH(n) == 1) {
480 n = CHILD(n, 0);
481 switch (TYPE(n)) {
482 case LESS:
483 return Lt;
484 case GREATER:
485 return Gt;
486 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 return Eq;
488 case LESSEQUAL:
489 return LtE;
490 case GREATEREQUAL:
491 return GtE;
492 case NOTEQUAL:
493 return NotEq;
494 case NAME:
495 if (strcmp(STR(n), "in") == 0)
496 return In;
497 if (strcmp(STR(n), "is") == 0)
498 return Is;
499 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000500 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 STR(n));
502 return 0;
503 }
504 }
505 else if (NCH(n) == 2) {
506 /* handle "not in" and "is not" */
507 switch (TYPE(CHILD(n, 0))) {
508 case NAME:
509 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
510 return NotIn;
511 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
512 return IsNot;
513 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000514 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
516 return 0;
517 }
518 }
Neal Norwitz79792652005-11-14 04:25:03 +0000519 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 NCH(n));
521 return 0;
522}
523
524static asdl_seq *
525seq_for_testlist(struct compiling *c, const node *n)
526{
527 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000528 asdl_seq *seq;
529 expr_ty expression;
530 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 assert(TYPE(n) == testlist
532 || TYPE(n) == listmaker
533 || TYPE(n) == testlist_gexp
534 || TYPE(n) == testlist_safe
535 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000537 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 if (!seq)
539 return NULL;
540
541 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000542 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
544 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000545 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
548 assert(i / 2 < seq->size);
549 asdl_seq_SET(seq, i / 2, expression);
550 }
551 return seq;
552}
553
554static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000555compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556{
557 int i, len = (NCH(n) + 1) / 2;
558 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000559 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560 if (!args)
561 return NULL;
562
563 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 for (i = 0; i < len; i++) {
565 const node *child = CHILD(CHILD(n, 2*i), 0);
566 expr_ty arg;
567 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000568 if (!strcmp(STR(child), "None")) {
569 ast_error(child, "assignment to None");
570 return NULL;
571 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000572 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
573 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000574 }
575 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000576 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000577 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 asdl_seq_SET(args, i, arg);
579 }
580
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000581 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000582 if (!set_context(result, Store, n))
583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584 return result;
585}
586
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Jeremy Hyltona8293132006-02-28 17:58:27 +0000588/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
590static arguments_ty
591ast_for_arguments(struct compiling *c, const node *n)
592{
593 /* parameters: '(' [varargslist] ')'
594 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
595 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
596 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000597 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 asdl_seq *args, *defaults;
599 identifier vararg = NULL, kwarg = NULL;
600 node *ch;
601
602 if (TYPE(n) == parameters) {
603 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000604 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 n = CHILD(n, 1);
606 }
607 REQ(n, varargslist);
608
609 /* first count the number of normal args & defaults */
610 for (i = 0; i < NCH(n); i++) {
611 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000612 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614 if (TYPE(ch) == EQUAL)
615 n_defaults++;
616 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000617 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 if (!args && n_args)
619 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000620 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 if (!defaults && n_defaults)
622 goto error;
623
624 /* fpdef: NAME | '(' fplist ')'
625 fplist: fpdef (',' fpdef)* [',']
626 */
627 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000628 j = 0; /* index for defaults */
629 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630 while (i < NCH(n)) {
631 ch = CHILD(n, i);
632 switch (TYPE(ch)) {
633 case fpdef:
634 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
635 anything other than EQUAL or a comma? */
636 /* XXX Should NCH(n) check be made a separate check? */
637 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000638 asdl_seq_SET(defaults, j++,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 ast_for_expr(c, CHILD(n, i + 2)));
640 i += 2;
641 found_default = 1;
642 }
643 else if (found_default) {
644 ast_error(n,
645 "non-default argument follows default argument");
646 goto error;
647 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000649 ch = CHILD(ch, 1);
650 /* def foo((x)): is not complex, special case. */
651 if (NCH(ch) != 1) {
652 /* We have complex arguments, setup for unpacking. */
653 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
654 } else {
655 /* def foo((x)): setup for checking NAME below. */
656 ch = CHILD(ch, 0);
657 }
658 }
659 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000660 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
662 ast_error(CHILD(ch, 0), "assignment to None");
663 goto error;
664 }
Armin Rigo31441302005-10-21 12:57:31 +0000665 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000666 Param, LINENO(ch), ch->n_col_offset,
667 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 if (!name)
669 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000670 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671
672 }
673 i += 2; /* the name and the comma */
674 break;
675 case STAR:
676 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
677 ast_error(CHILD(n, i+1), "assignment to None");
678 goto error;
679 }
680 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
681 i += 3;
682 break;
683 case DOUBLESTAR:
684 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
685 ast_error(CHILD(n, i+1), "assignment to None");
686 goto error;
687 }
688 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
689 i += 3;
690 break;
691 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000692 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 "unexpected node in varargslist: %d @ %d",
694 TYPE(ch), i);
695 goto error;
696 }
697 }
698
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000699 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700
701 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000702 Py_XDECREF(vararg);
703 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 return NULL;
705}
706
707static expr_ty
708ast_for_dotted_name(struct compiling *c, const node *n)
709{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000710 expr_ty e;
711 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000712 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713 int i;
714
715 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000716
717 lineno = LINENO(n);
718 col_offset = n->n_col_offset;
719
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 id = NEW_IDENTIFIER(CHILD(n, 0));
721 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000722 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000723 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000725 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726
727 for (i = 2; i < NCH(n); i+=2) {
728 id = NEW_IDENTIFIER(CHILD(n, i));
729 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000730 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000731 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000732 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000733 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 }
735
736 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737}
738
739static expr_ty
740ast_for_decorator(struct compiling *c, const node *n)
741{
742 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
743 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000744 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745
746 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000747 REQ(CHILD(n, 0), AT);
748 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749
750 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
751 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000752 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753
754 if (NCH(n) == 3) { /* No arguments */
755 d = name_expr;
756 name_expr = NULL;
757 }
758 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000759 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
760 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000762 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763 name_expr = NULL;
764 }
765 else {
766 d = ast_for_call(c, CHILD(n, 3), name_expr);
767 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
772 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773}
774
775static asdl_seq*
776ast_for_decorators(struct compiling *c, const node *n)
777{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000778 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000779 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 int i;
781
782 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000783 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 if (!decorator_seq)
785 return NULL;
786
787 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000788 d = ast_for_decorator(c, CHILD(n, i));
789 if (!d)
790 return NULL;
791 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 }
793 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794}
795
796static stmt_ty
797ast_for_funcdef(struct compiling *c, const node *n)
798{
799 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000800 identifier name;
801 arguments_ty args;
802 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 asdl_seq *decorator_seq = NULL;
804 int name_i;
805
806 REQ(n, funcdef);
807
808 if (NCH(n) == 6) { /* decorators are present */
809 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
810 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000811 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 name_i = 2;
813 }
814 else {
815 name_i = 1;
816 }
817
818 name = NEW_IDENTIFIER(CHILD(n, name_i));
819 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000822 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 }
825 args = ast_for_arguments(c, CHILD(n, name_i + 1));
826 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 body = ast_for_suite(c, CHILD(n, name_i + 3));
829 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000832 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
833 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834}
835
836static expr_ty
837ast_for_lambdef(struct compiling *c, const node *n)
838{
839 /* lambdef: 'lambda' [varargslist] ':' test */
840 arguments_ty args;
841 expr_ty expression;
842
843 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000844 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 if (!args)
846 return NULL;
847 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000848 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 }
851 else {
852 args = ast_for_arguments(c, CHILD(n, 1));
853 if (!args)
854 return NULL;
855 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000856 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
859
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000860 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861}
862
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000863static expr_ty
864ast_for_ifexpr(struct compiling *c, const node *n)
865{
866 /* test: or_test 'if' or_test 'else' test */
867 expr_ty expression, body, orelse;
868
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000869 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000870 body = ast_for_expr(c, CHILD(n, 0));
871 if (!body)
872 return NULL;
873 expression = ast_for_expr(c, CHILD(n, 2));
874 if (!expression)
875 return NULL;
876 orelse = ast_for_expr(c, CHILD(n, 4));
877 if (!orelse)
878 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000879 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
880 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000881}
882
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883/* Count the number of 'for' loop in a list comprehension.
884
885 Helper for ast_for_listcomp().
886*/
887
888static int
889count_list_fors(const node *n)
890{
891 int n_fors = 0;
892 node *ch = CHILD(n, 1);
893
894 count_list_for:
895 n_fors++;
896 REQ(ch, list_for);
897 if (NCH(ch) == 5)
898 ch = CHILD(ch, 4);
899 else
900 return n_fors;
901 count_list_iter:
902 REQ(ch, list_iter);
903 ch = CHILD(ch, 0);
904 if (TYPE(ch) == list_for)
905 goto count_list_for;
906 else if (TYPE(ch) == list_if) {
907 if (NCH(ch) == 3) {
908 ch = CHILD(ch, 2);
909 goto count_list_iter;
910 }
911 else
912 return n_fors;
913 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000914
915 /* Should never be reached */
916 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
917 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918}
919
920/* Count the number of 'if' statements in a list comprehension.
921
922 Helper for ast_for_listcomp().
923*/
924
925static int
926count_list_ifs(const node *n)
927{
928 int n_ifs = 0;
929
930 count_list_iter:
931 REQ(n, list_iter);
932 if (TYPE(CHILD(n, 0)) == list_for)
933 return n_ifs;
934 n = CHILD(n, 0);
935 REQ(n, list_if);
936 n_ifs++;
937 if (NCH(n) == 2)
938 return n_ifs;
939 n = CHILD(n, 2);
940 goto count_list_iter;
941}
942
943static expr_ty
944ast_for_listcomp(struct compiling *c, const node *n)
945{
946 /* listmaker: test ( list_for | (',' test)* [','] )
947 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
948 list_iter: list_for | list_if
949 list_if: 'if' test [list_iter]
950 testlist_safe: test [(',' test)+ [',']]
951 */
952 expr_ty elt;
953 asdl_seq *listcomps;
954 int i, n_fors;
955 node *ch;
956
957 REQ(n, listmaker);
958 assert(NCH(n) > 1);
959
960 elt = ast_for_expr(c, CHILD(n, 0));
961 if (!elt)
962 return NULL;
963
964 n_fors = count_list_fors(n);
965 if (n_fors == -1)
966 return NULL;
967
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000968 listcomps = asdl_seq_new(n_fors, c->c_arena);
969 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000971
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972 ch = CHILD(n, 1);
973 for (i = 0; i < n_fors; i++) {
974 comprehension_ty lc;
975 asdl_seq *t;
976 expr_ty expression;
977
978 REQ(ch, list_for);
979
980 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000981 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000983 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000984 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000987 if (asdl_seq_LEN(t) == 1)
988 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
989 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000991 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
992 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000993 expression, NULL, c->c_arena);
994 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996
997 if (NCH(ch) == 5) {
998 int j, n_ifs;
999 asdl_seq *ifs;
1000
1001 ch = CHILD(ch, 4);
1002 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001003 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001006 ifs = asdl_seq_new(n_ifs, c->c_arena);
1007 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009
1010 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001011 REQ(ch, list_iter);
1012 ch = CHILD(ch, 0);
1013 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014
Jeremy Hyltona8293132006-02-28 17:58:27 +00001015 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1016 if (NCH(ch) == 3)
1017 ch = CHILD(ch, 2);
1018 }
1019 /* on exit, must guarantee that ch is a list_for */
1020 if (TYPE(ch) == list_iter)
1021 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001023 }
1024 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 }
1026
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001027 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028}
1029
1030/*
1031 Count the number of 'for' loops in a generator expression.
1032
1033 Helper for ast_for_genexp().
1034*/
1035
1036static int
1037count_gen_fors(const node *n)
1038{
1039 int n_fors = 0;
1040 node *ch = CHILD(n, 1);
1041
1042 count_gen_for:
1043 n_fors++;
1044 REQ(ch, gen_for);
1045 if (NCH(ch) == 5)
1046 ch = CHILD(ch, 4);
1047 else
1048 return n_fors;
1049 count_gen_iter:
1050 REQ(ch, gen_iter);
1051 ch = CHILD(ch, 0);
1052 if (TYPE(ch) == gen_for)
1053 goto count_gen_for;
1054 else if (TYPE(ch) == gen_if) {
1055 if (NCH(ch) == 3) {
1056 ch = CHILD(ch, 2);
1057 goto count_gen_iter;
1058 }
1059 else
1060 return n_fors;
1061 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001062
1063 /* Should never be reached */
1064 PyErr_SetString(PyExc_SystemError,
1065 "logic error in count_gen_fors");
1066 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067}
1068
1069/* Count the number of 'if' statements in a generator expression.
1070
1071 Helper for ast_for_genexp().
1072*/
1073
1074static int
1075count_gen_ifs(const node *n)
1076{
1077 int n_ifs = 0;
1078
1079 while (1) {
1080 REQ(n, gen_iter);
1081 if (TYPE(CHILD(n, 0)) == gen_for)
1082 return n_ifs;
1083 n = CHILD(n, 0);
1084 REQ(n, gen_if);
1085 n_ifs++;
1086 if (NCH(n) == 2)
1087 return n_ifs;
1088 n = CHILD(n, 2);
1089 }
1090}
1091
Jeremy Hyltona8293132006-02-28 17:58:27 +00001092/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093static expr_ty
1094ast_for_genexp(struct compiling *c, const node *n)
1095{
1096 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1097 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1098 expr_ty elt;
1099 asdl_seq *genexps;
1100 int i, n_fors;
1101 node *ch;
1102
1103 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1104 assert(NCH(n) > 1);
1105
1106 elt = ast_for_expr(c, CHILD(n, 0));
1107 if (!elt)
1108 return NULL;
1109
1110 n_fors = count_gen_fors(n);
1111 if (n_fors == -1)
1112 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001113
1114 genexps = asdl_seq_new(n_fors, c->c_arena);
1115 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001117
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118 ch = CHILD(n, 1);
1119 for (i = 0; i < n_fors; i++) {
1120 comprehension_ty ge;
1121 asdl_seq *t;
1122 expr_ty expression;
1123
1124 REQ(ch, gen_for);
1125
1126 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001127 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001129 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001130 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001132
1133 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001135 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001137 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1138 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001139 expression, NULL, c->c_arena);
1140
1141 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001143
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 if (NCH(ch) == 5) {
1145 int j, n_ifs;
1146 asdl_seq *ifs;
1147
1148 ch = CHILD(ch, 4);
1149 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001150 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001152
1153 ifs = asdl_seq_new(n_ifs, c->c_arena);
1154 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001156
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 for (j = 0; j < n_ifs; j++) {
1158 REQ(ch, gen_iter);
1159 ch = CHILD(ch, 0);
1160 REQ(ch, gen_if);
1161
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001162 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001163 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001164 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001165 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 if (NCH(ch) == 3)
1167 ch = CHILD(ch, 2);
1168 }
1169 /* on exit, must guarantee that ch is a gen_for */
1170 if (TYPE(ch) == gen_iter)
1171 ch = CHILD(ch, 0);
1172 ge->ifs = ifs;
1173 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001174 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 }
1176
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001177 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178}
1179
1180static expr_ty
1181ast_for_atom(struct compiling *c, const node *n)
1182{
1183 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1184 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1185 */
1186 node *ch = CHILD(n, 0);
1187
1188 switch (TYPE(ch)) {
1189 case NAME:
1190 /* All names start in Load context, but may later be
1191 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001192 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 case STRING: {
1194 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 if (!str)
1196 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001197
1198 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001199 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 }
1201 case NUMBER: {
1202 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 if (!pynum)
1204 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001205
1206 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001207 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 }
1209 case LPAR: /* some parenthesized expressions */
1210 ch = CHILD(n, 1);
1211
1212 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001213 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214
1215 if (TYPE(ch) == yield_expr)
1216 return ast_for_expr(c, ch);
1217
1218 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1219 return ast_for_genexp(c, ch);
1220
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001221 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 case LSQB: /* list (or list comprehension) */
1223 ch = CHILD(n, 1);
1224
1225 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001226 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227
1228 REQ(ch, listmaker);
1229 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1230 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 if (!elts)
1232 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001233
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001234 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 }
1236 else
1237 return ast_for_listcomp(c, ch);
1238 case LBRACE: {
1239 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1240 int i, size;
1241 asdl_seq *keys, *values;
1242
1243 ch = CHILD(n, 1);
1244 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001245 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 if (!keys)
1247 return NULL;
1248
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001249 values = asdl_seq_new(size, c->c_arena);
1250 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252
1253 for (i = 0; i < NCH(ch); i += 4) {
1254 expr_ty expression;
1255
1256 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001257 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001259
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001261
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001263 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001265
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 asdl_seq_SET(values, i / 4, expression);
1267 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001268 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 }
1270 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001271 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 if (!expression)
1273 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001274
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001275 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 }
1277 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001278 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 return NULL;
1280 }
1281}
1282
1283static slice_ty
1284ast_for_slice(struct compiling *c, const node *n)
1285{
1286 node *ch;
1287 expr_ty lower = NULL, upper = NULL, step = NULL;
1288
1289 REQ(n, subscript);
1290
1291 /*
1292 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1293 sliceop: ':' [test]
1294 */
1295 ch = CHILD(n, 0);
1296 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001297 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298
1299 if (NCH(n) == 1 && TYPE(ch) == test) {
1300 /* 'step' variable hold no significance in terms of being used over
1301 other vars */
1302 step = ast_for_expr(c, ch);
1303 if (!step)
1304 return NULL;
1305
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001306 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 }
1308
1309 if (TYPE(ch) == test) {
1310 lower = ast_for_expr(c, ch);
1311 if (!lower)
1312 return NULL;
1313 }
1314
1315 /* If there's an upper bound it's in the second or third position. */
1316 if (TYPE(ch) == COLON) {
1317 if (NCH(n) > 1) {
1318 node *n2 = CHILD(n, 1);
1319
1320 if (TYPE(n2) == test) {
1321 upper = ast_for_expr(c, n2);
1322 if (!upper)
1323 return NULL;
1324 }
1325 }
1326 } else if (NCH(n) > 2) {
1327 node *n2 = CHILD(n, 2);
1328
1329 if (TYPE(n2) == test) {
1330 upper = ast_for_expr(c, n2);
1331 if (!upper)
1332 return NULL;
1333 }
1334 }
1335
1336 ch = CHILD(n, NCH(n) - 1);
1337 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001338 if (NCH(ch) == 1) {
1339 /* No expression, so step is None */
1340 ch = CHILD(ch, 0);
1341 step = Name(new_identifier("None", c->c_arena), Load,
1342 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 if (!step)
1344 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001345 } else {
1346 ch = CHILD(ch, 1);
1347 if (TYPE(ch) == test) {
1348 step = ast_for_expr(c, ch);
1349 if (!step)
1350 return NULL;
1351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 }
1353 }
1354
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001355 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356}
1357
1358static expr_ty
1359ast_for_binop(struct compiling *c, const node *n)
1360{
1361 /* Must account for a sequence of expressions.
1362 How should A op B op C by represented?
1363 BinOp(BinOp(A, op, B), op, C).
1364 */
1365
1366 int i, nops;
1367 expr_ty expr1, expr2, result;
1368 operator_ty operator;
1369
1370 expr1 = ast_for_expr(c, CHILD(n, 0));
1371 if (!expr1)
1372 return NULL;
1373
1374 expr2 = ast_for_expr(c, CHILD(n, 2));
1375 if (!expr2)
1376 return NULL;
1377
1378 operator = get_operator(CHILD(n, 1));
1379 if (!operator)
1380 return NULL;
1381
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001382 result = BinOp(expr1, operator, expr2, LINENO(n), n->n_col_offset,
1383 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384 if (!result)
1385 return NULL;
1386
1387 nops = (NCH(n) - 1) / 2;
1388 for (i = 1; i < nops; i++) {
1389 expr_ty tmp_result, tmp;
1390 const node* next_oper = CHILD(n, i * 2 + 1);
1391
1392 operator = get_operator(next_oper);
1393 if (!operator)
1394 return NULL;
1395
1396 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1397 if (!tmp)
1398 return NULL;
1399
1400 tmp_result = BinOp(result, operator, tmp,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001401 LINENO(next_oper), next_oper->n_col_offset,
1402 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 if (!tmp)
1404 return NULL;
1405 result = tmp_result;
1406 }
1407 return result;
1408}
1409
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001410static expr_ty
1411ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1412{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001413 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1414 subscriptlist: subscript (',' subscript)* [',']
1415 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1416 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001417 REQ(n, trailer);
1418 if (TYPE(CHILD(n, 0)) == LPAR) {
1419 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001420 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1421 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001422 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001423 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001424 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001425 else if (TYPE(CHILD(n, 0)) == DOT ) {
1426 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001427 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001428 }
1429 else {
1430 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001431 REQ(CHILD(n, 2), RSQB);
1432 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001433 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001434 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1435 if (!slc)
1436 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001437 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1438 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001439 }
1440 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001441 /* The grammar is ambiguous here. The ambiguity is resolved
1442 by treating the sequence as a tuple literal if there are
1443 no slice features.
1444 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001445 int j;
1446 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001447 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001448 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001449 asdl_seq *slices, *elts;
1450 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001451 if (!slices)
1452 return NULL;
1453 for (j = 0; j < NCH(n); j += 2) {
1454 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001455 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001456 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001457 if (slc->kind != Index_kind)
1458 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001459 asdl_seq_SET(slices, j / 2, slc);
1460 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001461 if (!simple) {
1462 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001463 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001464 }
1465 /* extract Index values and put them in a Tuple */
1466 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001467 if (!elts)
1468 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001469 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1470 slc = (slice_ty)asdl_seq_GET(slices, j);
1471 assert(slc->kind == Index_kind && slc->v.Index.value);
1472 asdl_seq_SET(elts, j, slc->v.Index.value);
1473 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001474 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001475 if (!e)
1476 return NULL;
1477 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001478 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001479 }
1480 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001481}
1482
1483static expr_ty
1484ast_for_power(struct compiling *c, const node *n)
1485{
1486 /* power: atom trailer* ('**' factor)*
1487 */
1488 int i;
1489 expr_ty e, tmp;
1490 REQ(n, power);
1491 e = ast_for_atom(c, CHILD(n, 0));
1492 if (!e)
1493 return NULL;
1494 if (NCH(n) == 1)
1495 return e;
1496 for (i = 1; i < NCH(n); i++) {
1497 node *ch = CHILD(n, i);
1498 if (TYPE(ch) != trailer)
1499 break;
1500 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001501 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001502 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001503 tmp->lineno = e->lineno;
1504 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001505 e = tmp;
1506 }
1507 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1508 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001509 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001510 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001511 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001512 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001513 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001514 e = tmp;
1515 }
1516 return e;
1517}
1518
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519/* Do not name a variable 'expr'! Will cause a compile error.
1520*/
1521
1522static expr_ty
1523ast_for_expr(struct compiling *c, const node *n)
1524{
1525 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001526 test: or_test ['if' or_test 'else' test] | lambdef
1527 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 and_test: not_test ('and' not_test)*
1529 not_test: 'not' not_test | comparison
1530 comparison: expr (comp_op expr)*
1531 expr: xor_expr ('|' xor_expr)*
1532 xor_expr: and_expr ('^' and_expr)*
1533 and_expr: shift_expr ('&' shift_expr)*
1534 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1535 arith_expr: term (('+'|'-') term)*
1536 term: factor (('*'|'/'|'%'|'//') factor)*
1537 factor: ('+'|'-'|'~') factor | power
1538 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001539
1540 As well as modified versions that exist for backward compatibility,
1541 to explicitly allow:
1542 [ x for x in lambda: 0, lambda: 1 ]
1543 (which would be ambiguous without these extra rules)
1544
1545 old_test: or_test | old_lambdef
1546 old_lambdef: 'lambda' [vararglist] ':' old_test
1547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 */
1549
1550 asdl_seq *seq;
1551 int i;
1552
1553 loop:
1554 switch (TYPE(n)) {
1555 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001556 case old_test:
1557 if (TYPE(CHILD(n, 0)) == lambdef ||
1558 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001560 else if (NCH(n) > 1)
1561 return ast_for_ifexpr(c, n);
1562 /* Fallthrough */
1563 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 case and_test:
1565 if (NCH(n) == 1) {
1566 n = CHILD(n, 0);
1567 goto loop;
1568 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001569 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 if (!seq)
1571 return NULL;
1572 for (i = 0; i < NCH(n); i += 2) {
1573 expr_ty e = ast_for_expr(c, CHILD(n, i));
1574 if (!e)
1575 return NULL;
1576 asdl_seq_SET(seq, i / 2, e);
1577 }
1578 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001579 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1580 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001581 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001582 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 case not_test:
1584 if (NCH(n) == 1) {
1585 n = CHILD(n, 0);
1586 goto loop;
1587 }
1588 else {
1589 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1590 if (!expression)
1591 return NULL;
1592
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001593 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1594 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 }
1596 case comparison:
1597 if (NCH(n) == 1) {
1598 n = CHILD(n, 0);
1599 goto loop;
1600 }
1601 else {
1602 expr_ty expression;
1603 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001604 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 if (!ops)
1606 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001607 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 return NULL;
1610 }
1611 for (i = 1; i < NCH(n); i += 2) {
1612 /* XXX cmpop_ty is just an enum */
1613 cmpop_ty operator;
1614
1615 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001616 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001618 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619
1620 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001621 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001623 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001625 asdl_seq_SET(ops, i / 2, (void *)(Py_uintptr_t)operator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 asdl_seq_SET(cmps, i / 2, expression);
1627 }
1628 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001629 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001631 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001633 return Compare(expression, ops, cmps, LINENO(n),
1634 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635 }
1636 break;
1637
1638 /* The next five cases all handle BinOps. The main body of code
1639 is the same in each case, but the switch turned inside out to
1640 reuse the code for each type of operator.
1641 */
1642 case expr:
1643 case xor_expr:
1644 case and_expr:
1645 case shift_expr:
1646 case arith_expr:
1647 case term:
1648 if (NCH(n) == 1) {
1649 n = CHILD(n, 0);
1650 goto loop;
1651 }
1652 return ast_for_binop(c, n);
1653 case yield_expr: {
1654 expr_ty exp = NULL;
1655 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001656 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 if (!exp)
1658 return NULL;
1659 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001660 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 }
1662 case factor: {
1663 expr_ty expression;
1664
1665 if (NCH(n) == 1) {
1666 n = CHILD(n, 0);
1667 goto loop;
1668 }
1669
1670 expression = ast_for_expr(c, CHILD(n, 1));
1671 if (!expression)
1672 return NULL;
1673
1674 switch (TYPE(CHILD(n, 0))) {
1675 case PLUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001676 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 case MINUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001678 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 case TILDE:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001680 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001682 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1683 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 break;
1685 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001686 case power:
1687 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001689 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 return NULL;
1691 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001692 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 return NULL;
1694}
1695
1696static expr_ty
1697ast_for_call(struct compiling *c, const node *n, expr_ty func)
1698{
1699 /*
1700 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1701 | '**' test)
1702 argument: [test '='] test [gen_for] # Really [keyword '='] test
1703 */
1704
1705 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001706 asdl_seq *args;
1707 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 expr_ty vararg = NULL, kwarg = NULL;
1709
1710 REQ(n, arglist);
1711
1712 nargs = 0;
1713 nkeywords = 0;
1714 ngens = 0;
1715 for (i = 0; i < NCH(n); i++) {
1716 node *ch = CHILD(n, i);
1717 if (TYPE(ch) == argument) {
1718 if (NCH(ch) == 1)
1719 nargs++;
1720 else if (TYPE(CHILD(ch, 1)) == gen_for)
1721 ngens++;
1722 else
1723 nkeywords++;
1724 }
1725 }
1726 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001727 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 "if not sole argument");
1729 return NULL;
1730 }
1731
1732 if (nargs + nkeywords + ngens > 255) {
1733 ast_error(n, "more than 255 arguments");
1734 return NULL;
1735 }
1736
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001737 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001739 return NULL;
1740 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 nargs = 0;
1744 nkeywords = 0;
1745 for (i = 0; i < NCH(n); i++) {
1746 node *ch = CHILD(n, i);
1747 if (TYPE(ch) == argument) {
1748 expr_ty e;
1749 if (NCH(ch) == 1) {
1750 e = ast_for_expr(c, CHILD(ch, 0));
1751 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001752 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753 asdl_seq_SET(args, nargs++, e);
1754 }
1755 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1756 e = ast_for_genexp(c, ch);
1757 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001758 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 asdl_seq_SET(args, nargs++, e);
1760 }
1761 else {
1762 keyword_ty kw;
1763 identifier key;
1764
1765 /* CHILD(ch, 0) is test, but must be an identifier? */
1766 e = ast_for_expr(c, CHILD(ch, 0));
1767 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769 /* f(lambda x: x[0] = 3) ends up getting parsed with
1770 * LHS test = lambda x: x[0], and RHS test = 3.
1771 * SF bug 132313 points out that complaining about a keyword
1772 * then is very confusing.
1773 */
1774 if (e->kind == Lambda_kind) {
1775 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001776 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 } else if (e->kind != Name_kind) {
1778 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001779 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 }
1781 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 e = ast_for_expr(c, CHILD(ch, 2));
1783 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001784 return NULL;
1785 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001787 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788 asdl_seq_SET(keywords, nkeywords++, kw);
1789 }
1790 }
1791 else if (TYPE(ch) == STAR) {
1792 vararg = ast_for_expr(c, CHILD(n, i+1));
1793 i++;
1794 }
1795 else if (TYPE(ch) == DOUBLESTAR) {
1796 kwarg = ast_for_expr(c, CHILD(n, i+1));
1797 i++;
1798 }
1799 }
1800
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001801 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802}
1803
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001805ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001807 /* testlist_gexp: test (',' test)* [','] */
1808 /* testlist: test (',' test)* [','] */
1809 /* testlist_safe: test (',' test)+ [','] */
1810 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001812 if (TYPE(n) == testlist_gexp) {
1813 if (NCH(n) > 1)
1814 assert(TYPE(CHILD(n, 1)) != gen_for);
1815 }
1816 else {
1817 assert(TYPE(n) == testlist ||
1818 TYPE(n) == testlist_safe ||
1819 TYPE(n) == testlist1);
1820 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 if (NCH(n) == 1)
1822 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 else {
1824 asdl_seq *tmp = seq_for_testlist(c, n);
1825 if (!tmp)
1826 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001827 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001829}
1830
1831static expr_ty
1832ast_for_testlist_gexp(struct compiling *c, const node* n)
1833{
1834 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1835 /* argument: test [ gen_for ] */
1836 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001837 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001838 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001839 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001840}
1841
1842/* like ast_for_testlist() but returns a sequence */
1843static asdl_seq*
1844ast_for_class_bases(struct compiling *c, const node* n)
1845{
1846 /* testlist: test (',' test)* [','] */
1847 assert(NCH(n) > 0);
1848 REQ(n, testlist);
1849 if (NCH(n) == 1) {
1850 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001851 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001852 if (!bases)
1853 return NULL;
1854 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001855 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001856 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001857 asdl_seq_SET(bases, 0, base);
1858 return bases;
1859 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001860
1861 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862}
1863
1864static stmt_ty
1865ast_for_expr_stmt(struct compiling *c, const node *n)
1866{
1867 REQ(n, expr_stmt);
1868 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1869 | ('=' (yield_expr|testlist))*)
1870 testlist: test (',' test)* [',']
1871 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1872 | '<<=' | '>>=' | '**=' | '//='
1873 test: ... here starts the operator precendence dance
1874 */
1875
1876 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001877 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 if (!e)
1879 return NULL;
1880
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001881 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 }
1883 else if (TYPE(CHILD(n, 1)) == augassign) {
1884 expr_ty expr1, expr2;
1885 operator_ty operator;
1886 node *ch = CHILD(n, 0);
1887
1888 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001889 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001891 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893
1894 if (!expr1)
1895 return NULL;
Neal Norwitz96e48d42006-02-05 02:07:19 +00001896 /* TODO(jhylton): Figure out why set_context() can't be used here. */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001897 switch (expr1->kind) {
1898 case GeneratorExp_kind:
1899 ast_error(ch, "augmented assignment to generator "
1900 "expression not possible");
1901 return NULL;
1902 case Name_kind: {
1903 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1904 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1905 ast_error(ch, "assignment to None");
1906 return NULL;
1907 }
1908 break;
1909 }
1910 case Attribute_kind:
1911 case Subscript_kind:
1912 break;
1913 default:
1914 ast_error(ch, "illegal expression for augmented "
1915 "assignment");
1916 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918
1919 ch = CHILD(n, 2);
1920 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001921 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001923 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), ch->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001924 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 return NULL;
1926
1927 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001928 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929 return NULL;
1930
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001931 return AugAssign(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932 }
1933 else {
1934 int i;
1935 asdl_seq *targets;
1936 node *value;
1937 expr_ty expression;
1938
1939 /* a normal assignment */
1940 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001941 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 if (!targets)
1943 return NULL;
1944 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001945 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 node *ch = CHILD(n, i);
1947 if (TYPE(ch) == yield_expr) {
1948 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001949 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001951 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952
1953 /* set context to assign */
1954 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001955 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956
Neal Norwitz84456bd2005-12-18 03:16:20 +00001957 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001958 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959
1960 asdl_seq_SET(targets, i / 2, e);
1961 }
1962 value = CHILD(n, NCH(n) - 1);
1963 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001964 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965 else
1966 expression = ast_for_expr(c, value);
1967 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001968 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001969 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971}
1972
1973static stmt_ty
1974ast_for_print_stmt(struct compiling *c, const node *n)
1975{
1976 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1977 | '>>' test [ (',' test)+ [','] ] )
1978 */
1979 expr_ty dest = NULL, expression;
1980 asdl_seq *seq;
1981 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001982 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983
1984 REQ(n, print_stmt);
1985 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1986 dest = ast_for_expr(c, CHILD(n, 2));
1987 if (!dest)
1988 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001989 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001991 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00001993 return NULL;
1994 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001996 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001998 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 }
2000 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002001 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002}
2003
2004static asdl_seq *
2005ast_for_exprlist(struct compiling *c, const node *n, int context)
2006{
2007 asdl_seq *seq;
2008 int i;
2009 expr_ty e;
2010
2011 REQ(n, exprlist);
2012
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002013 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 if (!seq)
2015 return NULL;
2016 for (i = 0; i < NCH(n); i += 2) {
2017 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002018 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002019 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002020 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002021 if (context && !set_context(e, context, CHILD(n, i)))
2022 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 }
2024 return seq;
2025}
2026
2027static stmt_ty
2028ast_for_del_stmt(struct compiling *c, const node *n)
2029{
2030 asdl_seq *expr_list;
2031
2032 /* del_stmt: 'del' exprlist */
2033 REQ(n, del_stmt);
2034
2035 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2036 if (!expr_list)
2037 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002038 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039}
2040
2041static stmt_ty
2042ast_for_flow_stmt(struct compiling *c, const node *n)
2043{
2044 /*
2045 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2046 | yield_stmt
2047 break_stmt: 'break'
2048 continue_stmt: 'continue'
2049 return_stmt: 'return' [testlist]
2050 yield_stmt: yield_expr
2051 yield_expr: 'yield' testlist
2052 raise_stmt: 'raise' [test [',' test [',' test]]]
2053 */
2054 node *ch;
2055
2056 REQ(n, flow_stmt);
2057 ch = CHILD(n, 0);
2058 switch (TYPE(ch)) {
2059 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002060 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002062 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 case yield_stmt: { /* will reduce to yield_expr */
2064 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2065 if (!exp)
2066 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002067 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 }
2069 case return_stmt:
2070 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002071 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002073 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 if (!expression)
2075 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002076 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 }
2078 case raise_stmt:
2079 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002080 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 else if (NCH(ch) == 2) {
2082 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2083 if (!expression)
2084 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002085 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 }
2087 else if (NCH(ch) == 4) {
2088 expr_ty expr1, expr2;
2089
2090 expr1 = ast_for_expr(c, CHILD(ch, 1));
2091 if (!expr1)
2092 return NULL;
2093 expr2 = ast_for_expr(c, CHILD(ch, 3));
2094 if (!expr2)
2095 return NULL;
2096
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002097 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 }
2099 else if (NCH(ch) == 6) {
2100 expr_ty expr1, expr2, expr3;
2101
2102 expr1 = ast_for_expr(c, CHILD(ch, 1));
2103 if (!expr1)
2104 return NULL;
2105 expr2 = ast_for_expr(c, CHILD(ch, 3));
2106 if (!expr2)
2107 return NULL;
2108 expr3 = ast_for_expr(c, CHILD(ch, 5));
2109 if (!expr3)
2110 return NULL;
2111
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002112 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 }
2114 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002115 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 "unexpected flow_stmt: %d", TYPE(ch));
2117 return NULL;
2118 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002119
2120 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2121 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122}
2123
2124static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002125alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126{
2127 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002128 import_as_name: NAME ['as' NAME]
2129 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 dotted_name: NAME ('.' NAME)*
2131 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002132 PyObject *str;
2133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 loop:
2135 switch (TYPE(n)) {
2136 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002137 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2138 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 case dotted_as_name:
2140 if (NCH(n) == 1) {
2141 n = CHILD(n, 0);
2142 goto loop;
2143 }
2144 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002145 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 assert(!a->asname);
2147 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2148 return a;
2149 }
2150 break;
2151 case dotted_name:
2152 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002153 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 else {
2155 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002156 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002157 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 char *s;
2159
2160 len = 0;
2161 for (i = 0; i < NCH(n); i += 2)
2162 /* length of string plus one for the dot */
2163 len += strlen(STR(CHILD(n, i))) + 1;
2164 len--; /* the last name doesn't have a dot */
2165 str = PyString_FromStringAndSize(NULL, len);
2166 if (!str)
2167 return NULL;
2168 s = PyString_AS_STRING(str);
2169 if (!s)
2170 return NULL;
2171 for (i = 0; i < NCH(n); i += 2) {
2172 char *sch = STR(CHILD(n, i));
2173 strcpy(s, STR(CHILD(n, i)));
2174 s += strlen(sch);
2175 *s++ = '.';
2176 }
2177 --s;
2178 *s = '\0';
2179 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002180 PyArena_AddPyObject(c->c_arena, str);
2181 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 }
2183 break;
2184 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002185 str = PyString_InternFromString("*");
2186 PyArena_AddPyObject(c->c_arena, str);
2187 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002189 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 "unexpected import name: %d", TYPE(n));
2191 return NULL;
2192 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002193
2194 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 return NULL;
2196}
2197
2198static stmt_ty
2199ast_for_import_stmt(struct compiling *c, const node *n)
2200{
2201 /*
2202 import_stmt: import_name | import_from
2203 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002204 import_from: 'from' ('.'* dotted_name | '.') 'import'
2205 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002207 int lineno;
2208 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 int i;
2210 asdl_seq *aliases;
2211
2212 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002213 lineno = LINENO(n);
2214 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002216 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002218 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002219 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 if (!aliases)
2221 return NULL;
2222 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002223 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002224 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 asdl_seq_SET(aliases, i / 2, import_alias);
2227 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002228 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002230 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002232 int idx, ndots = 0;
2233 alias_ty mod = NULL;
2234 identifier modname;
2235
2236 /* Count the number of dots (for relative imports) and check for the
2237 optional module name */
2238 for (idx = 1; idx < NCH(n); idx++) {
2239 if (TYPE(CHILD(n, idx)) == dotted_name) {
2240 mod = alias_for_import_name(c, CHILD(n, idx));
2241 idx++;
2242 break;
2243 } else if (TYPE(CHILD(n, idx)) != DOT) {
2244 break;
2245 }
2246 ndots++;
2247 }
2248 idx++; /* skip over the 'import' keyword */
2249 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002250 case STAR:
2251 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002252 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002253 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002254 if (ndots) {
2255 ast_error(n, "'import *' not allowed with 'from .'");
2256 return NULL;
2257 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002258 break;
2259 case LPAR:
2260 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002261 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002262 n_children = NCH(n);
2263 break;
2264 case import_as_names:
2265 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002266 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002267 n_children = NCH(n);
2268 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 ast_error(n, "trailing comma not allowed without"
2270 " surrounding parentheses");
2271 return NULL;
2272 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002273 break;
2274 default:
2275 ast_error(n, "Unexpected node-type in from-import");
2276 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002277 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002279 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002280 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282
2283 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002284 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002285 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002286 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002288 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002290 else {
2291 for (i = 0; i < NCH(n); i += 2) {
2292 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2293 if (!import_alias)
2294 return NULL;
2295 asdl_seq_SET(aliases, i / 2, import_alias);
2296 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002298 if (mod != NULL)
2299 modname = mod->name;
2300 else
2301 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002302 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002303 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 }
Neal Norwitz79792652005-11-14 04:25:03 +00002305 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 "unknown import statement: starts with command '%s'",
2307 STR(CHILD(n, 0)));
2308 return NULL;
2309}
2310
2311static stmt_ty
2312ast_for_global_stmt(struct compiling *c, const node *n)
2313{
2314 /* global_stmt: 'global' NAME (',' NAME)* */
2315 identifier name;
2316 asdl_seq *s;
2317 int i;
2318
2319 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002320 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321 if (!s)
2322 return NULL;
2323 for (i = 1; i < NCH(n); i += 2) {
2324 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002325 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 asdl_seq_SET(s, i / 2, name);
2328 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002329 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330}
2331
2332static stmt_ty
2333ast_for_exec_stmt(struct compiling *c, const node *n)
2334{
2335 expr_ty expr1, globals = NULL, locals = NULL;
2336 int n_children = NCH(n);
2337 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002338 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 "poorly formed 'exec' statement: %d parts to statement",
2340 n_children);
2341 return NULL;
2342 }
2343
2344 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2345 REQ(n, exec_stmt);
2346 expr1 = ast_for_expr(c, CHILD(n, 1));
2347 if (!expr1)
2348 return NULL;
2349 if (n_children >= 4) {
2350 globals = ast_for_expr(c, CHILD(n, 3));
2351 if (!globals)
2352 return NULL;
2353 }
2354 if (n_children == 6) {
2355 locals = ast_for_expr(c, CHILD(n, 5));
2356 if (!locals)
2357 return NULL;
2358 }
2359
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002360 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361}
2362
2363static stmt_ty
2364ast_for_assert_stmt(struct compiling *c, const node *n)
2365{
2366 /* assert_stmt: 'assert' test [',' test] */
2367 REQ(n, assert_stmt);
2368 if (NCH(n) == 2) {
2369 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2370 if (!expression)
2371 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002372 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 }
2374 else if (NCH(n) == 4) {
2375 expr_ty expr1, expr2;
2376
2377 expr1 = ast_for_expr(c, CHILD(n, 1));
2378 if (!expr1)
2379 return NULL;
2380 expr2 = ast_for_expr(c, CHILD(n, 3));
2381 if (!expr2)
2382 return NULL;
2383
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002384 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 }
Neal Norwitz79792652005-11-14 04:25:03 +00002386 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 "improper number of parts to 'assert' statement: %d",
2388 NCH(n));
2389 return NULL;
2390}
2391
2392static asdl_seq *
2393ast_for_suite(struct compiling *c, const node *n)
2394{
2395 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002396 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 stmt_ty s;
2398 int i, total, num, end, pos = 0;
2399 node *ch;
2400
2401 REQ(n, suite);
2402
2403 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002404 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 if (!seq)
2406 return NULL;
2407 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2408 n = CHILD(n, 0);
2409 /* simple_stmt always ends with a NEWLINE,
2410 and may have a trailing SEMI
2411 */
2412 end = NCH(n) - 1;
2413 if (TYPE(CHILD(n, end - 1)) == SEMI)
2414 end--;
2415 /* loop by 2 to skip semi-colons */
2416 for (i = 0; i < end; i += 2) {
2417 ch = CHILD(n, i);
2418 s = ast_for_stmt(c, ch);
2419 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002420 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 asdl_seq_SET(seq, pos++, s);
2422 }
2423 }
2424 else {
2425 for (i = 2; i < (NCH(n) - 1); i++) {
2426 ch = CHILD(n, i);
2427 REQ(ch, stmt);
2428 num = num_stmts(ch);
2429 if (num == 1) {
2430 /* small_stmt or compound_stmt with only one child */
2431 s = ast_for_stmt(c, ch);
2432 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002433 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 asdl_seq_SET(seq, pos++, s);
2435 }
2436 else {
2437 int j;
2438 ch = CHILD(ch, 0);
2439 REQ(ch, simple_stmt);
2440 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002441 /* statement terminates with a semi-colon ';' */
2442 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002443 assert((j + 1) == NCH(ch));
2444 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 s = ast_for_stmt(c, CHILD(ch, j));
2447 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 asdl_seq_SET(seq, pos++, s);
2450 }
2451 }
2452 }
2453 }
2454 assert(pos == seq->size);
2455 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456}
2457
2458static stmt_ty
2459ast_for_if_stmt(struct compiling *c, const node *n)
2460{
2461 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2462 ['else' ':' suite]
2463 */
2464 char *s;
2465
2466 REQ(n, if_stmt);
2467
2468 if (NCH(n) == 4) {
2469 expr_ty expression;
2470 asdl_seq *suite_seq;
2471
2472 expression = ast_for_expr(c, CHILD(n, 1));
2473 if (!expression)
2474 return NULL;
2475 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002476 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 return NULL;
2478
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002479 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002481
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 s = STR(CHILD(n, 4));
2483 /* s[2], the third character in the string, will be
2484 's' for el_s_e, or
2485 'i' for el_i_f
2486 */
2487 if (s[2] == 's') {
2488 expr_ty expression;
2489 asdl_seq *seq1, *seq2;
2490
2491 expression = ast_for_expr(c, CHILD(n, 1));
2492 if (!expression)
2493 return NULL;
2494 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002495 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 return NULL;
2497 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002498 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 return NULL;
2500
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002501 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502 }
2503 else if (s[2] == 'i') {
2504 int i, n_elif, has_else = 0;
2505 asdl_seq *orelse = NULL;
2506 n_elif = NCH(n) - 4;
2507 /* must reference the child n_elif+1 since 'else' token is third,
2508 not fourth, child from the end. */
2509 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2510 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2511 has_else = 1;
2512 n_elif -= 3;
2513 }
2514 n_elif /= 4;
2515
2516 if (has_else) {
2517 expr_ty expression;
2518 asdl_seq *seq1, *seq2;
2519
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002520 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 if (!orelse)
2522 return NULL;
2523 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002524 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002527 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002530 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532
2533 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002534 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002535 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 /* the just-created orelse handled the last elif */
2537 n_elif--;
2538 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539
2540 for (i = 0; i < n_elif; i++) {
2541 int off = 5 + (n_elif - i - 1) * 4;
2542 expr_ty expression;
2543 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002544 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002545 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 return NULL;
2547 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002548 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002551 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553
2554 asdl_seq_SET(new, 0,
2555 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002556 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 orelse = new;
2558 }
2559 return If(ast_for_expr(c, CHILD(n, 1)),
2560 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002561 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002563
2564 PyErr_Format(PyExc_SystemError,
2565 "unexpected token in 'if' statement: %s", s);
2566 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567}
2568
2569static stmt_ty
2570ast_for_while_stmt(struct compiling *c, const node *n)
2571{
2572 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2573 REQ(n, while_stmt);
2574
2575 if (NCH(n) == 4) {
2576 expr_ty expression;
2577 asdl_seq *suite_seq;
2578
2579 expression = ast_for_expr(c, CHILD(n, 1));
2580 if (!expression)
2581 return NULL;
2582 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002583 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002585 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 }
2587 else if (NCH(n) == 7) {
2588 expr_ty expression;
2589 asdl_seq *seq1, *seq2;
2590
2591 expression = ast_for_expr(c, CHILD(n, 1));
2592 if (!expression)
2593 return NULL;
2594 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002595 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 return NULL;
2597 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002598 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 return NULL;
2600
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002601 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002603
2604 PyErr_Format(PyExc_SystemError,
2605 "wrong number of tokens for 'while' statement: %d",
2606 NCH(n));
2607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608}
2609
2610static stmt_ty
2611ast_for_for_stmt(struct compiling *c, const node *n)
2612{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002613 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 expr_ty expression;
2615 expr_ty target;
2616 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2617 REQ(n, for_stmt);
2618
2619 if (NCH(n) == 9) {
2620 seq = ast_for_suite(c, CHILD(n, 8));
2621 if (!seq)
2622 return NULL;
2623 }
2624
2625 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002626 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002628 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002631 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002633 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002634 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 return NULL;
2636 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002637 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 return NULL;
2639
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002640 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2641 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642}
2643
2644static excepthandler_ty
2645ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2646{
2647 /* except_clause: 'except' [test [',' test]] */
2648 REQ(exc, except_clause);
2649 REQ(body, suite);
2650
2651 if (NCH(exc) == 1) {
2652 asdl_seq *suite_seq = ast_for_suite(c, body);
2653 if (!suite_seq)
2654 return NULL;
2655
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002656 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2657 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 }
2659 else if (NCH(exc) == 2) {
2660 expr_ty expression;
2661 asdl_seq *suite_seq;
2662
2663 expression = ast_for_expr(c, CHILD(exc, 1));
2664 if (!expression)
2665 return NULL;
2666 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002667 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 return NULL;
2669
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002670 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2671 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 }
2673 else if (NCH(exc) == 4) {
2674 asdl_seq *suite_seq;
2675 expr_ty expression;
2676 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2677 if (!e)
2678 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002679 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680 return NULL;
2681 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002682 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683 return NULL;
2684 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002685 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 return NULL;
2687
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002688 return excepthandler(expression, e, suite_seq, LINENO(exc),
2689 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002691
2692 PyErr_Format(PyExc_SystemError,
2693 "wrong number of children for 'except' clause: %d",
2694 NCH(exc));
2695 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696}
2697
2698static stmt_ty
2699ast_for_try_stmt(struct compiling *c, const node *n)
2700{
Neal Norwitzf599f422005-12-17 21:33:47 +00002701 const int nch = NCH(n);
2702 int n_except = (nch - 3)/3;
2703 asdl_seq *body, *orelse = NULL, *finally = NULL;
2704
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 REQ(n, try_stmt);
2706
Neal Norwitzf599f422005-12-17 21:33:47 +00002707 body = ast_for_suite(c, CHILD(n, 2));
2708 if (body == NULL)
2709 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710
Neal Norwitzf599f422005-12-17 21:33:47 +00002711 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2712 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2713 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2714 /* we can assume it's an "else",
2715 because nch >= 9 for try-else-finally and
2716 it would otherwise have a type of except_clause */
2717 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2718 if (orelse == NULL)
2719 return NULL;
2720 n_except--;
2721 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722
Neal Norwitzf599f422005-12-17 21:33:47 +00002723 finally = ast_for_suite(c, CHILD(n, nch - 1));
2724 if (finally == NULL)
2725 return NULL;
2726 n_except--;
2727 }
2728 else {
2729 /* we can assume it's an "else",
2730 otherwise it would have a type of except_clause */
2731 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2732 if (orelse == NULL)
2733 return NULL;
2734 n_except--;
2735 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002737 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002738 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 return NULL;
2740 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002741
2742 if (n_except > 0) {
2743 int i;
2744 stmt_ty except_st;
2745 /* process except statements to create a try ... except */
2746 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2747 if (handlers == NULL)
2748 return NULL;
2749
2750 for (i = 0; i < n_except; i++) {
2751 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2752 CHILD(n, 5 + i * 3));
2753 if (!e)
2754 return NULL;
2755 asdl_seq_SET(handlers, i, e);
2756 }
2757
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002758 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2759 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002760 if (!finally)
2761 return except_st;
2762
2763 /* if a 'finally' is present too, we nest the TryExcept within a
2764 TryFinally to emulate try ... except ... finally */
2765 body = asdl_seq_new(1, c->c_arena);
2766 if (body == NULL)
2767 return NULL;
2768 asdl_seq_SET(body, 0, except_st);
2769 }
2770
2771 /* must be a try ... finally (except clauses are in body, if any exist) */
2772 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002773 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774}
2775
Guido van Rossumc2e20742006-02-27 22:32:47 +00002776static expr_ty
2777ast_for_with_var(struct compiling *c, const node *n)
2778{
2779 REQ(n, with_var);
2780 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2781 ast_error(n, "expected \"with [expr] as [var]\"");
2782 return NULL;
2783 }
2784 return ast_for_expr(c, CHILD(n, 1));
2785}
2786
2787/* with_stmt: 'with' test [ with_var ] ':' suite */
2788static stmt_ty
2789ast_for_with_stmt(struct compiling *c, const node *n)
2790{
2791 expr_ty context_expr, optional_vars = NULL;
2792 int suite_index = 3; /* skip 'with', test, and ':' */
2793 asdl_seq *suite_seq;
2794
2795 assert(TYPE(n) == with_stmt);
2796 context_expr = ast_for_expr(c, CHILD(n, 1));
2797 if (TYPE(CHILD(n, 2)) == with_var) {
2798 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2799
2800 if (!optional_vars) {
2801 return NULL;
2802 }
2803 if (!set_context(optional_vars, Store, n)) {
2804 return NULL;
2805 }
2806 suite_index = 4;
2807 }
2808
2809 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2810 if (!suite_seq) {
2811 return NULL;
2812 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002813 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2814 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002815}
2816
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817static stmt_ty
2818ast_for_classdef(struct compiling *c, const node *n)
2819{
2820 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 asdl_seq *bases, *s;
2822
2823 REQ(n, classdef);
2824
2825 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2826 ast_error(n, "assignment to None");
2827 return NULL;
2828 }
2829
2830 if (NCH(n) == 4) {
2831 s = ast_for_suite(c, CHILD(n, 3));
2832 if (!s)
2833 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002834 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2835 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 }
2837 /* check for empty base list */
2838 if (TYPE(CHILD(n,3)) == RPAR) {
2839 s = ast_for_suite(c, CHILD(n,5));
2840 if (!s)
2841 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002842 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2843 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844 }
2845
2846 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002847 bases = ast_for_class_bases(c, CHILD(n, 3));
2848 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850
2851 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002852 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002854 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2855 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856}
2857
2858static stmt_ty
2859ast_for_stmt(struct compiling *c, const node *n)
2860{
2861 if (TYPE(n) == stmt) {
2862 assert(NCH(n) == 1);
2863 n = CHILD(n, 0);
2864 }
2865 if (TYPE(n) == simple_stmt) {
2866 assert(num_stmts(n) == 1);
2867 n = CHILD(n, 0);
2868 }
2869 if (TYPE(n) == small_stmt) {
2870 REQ(n, small_stmt);
2871 n = CHILD(n, 0);
2872 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2873 | flow_stmt | import_stmt | global_stmt | exec_stmt
2874 | assert_stmt
2875 */
2876 switch (TYPE(n)) {
2877 case expr_stmt:
2878 return ast_for_expr_stmt(c, n);
2879 case print_stmt:
2880 return ast_for_print_stmt(c, n);
2881 case del_stmt:
2882 return ast_for_del_stmt(c, n);
2883 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002884 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 case flow_stmt:
2886 return ast_for_flow_stmt(c, n);
2887 case import_stmt:
2888 return ast_for_import_stmt(c, n);
2889 case global_stmt:
2890 return ast_for_global_stmt(c, n);
2891 case exec_stmt:
2892 return ast_for_exec_stmt(c, n);
2893 case assert_stmt:
2894 return ast_for_assert_stmt(c, n);
2895 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002896 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2898 TYPE(n), NCH(n));
2899 return NULL;
2900 }
2901 }
2902 else {
2903 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2904 | funcdef | classdef
2905 */
2906 node *ch = CHILD(n, 0);
2907 REQ(n, compound_stmt);
2908 switch (TYPE(ch)) {
2909 case if_stmt:
2910 return ast_for_if_stmt(c, ch);
2911 case while_stmt:
2912 return ast_for_while_stmt(c, ch);
2913 case for_stmt:
2914 return ast_for_for_stmt(c, ch);
2915 case try_stmt:
2916 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002917 case with_stmt:
2918 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 case funcdef:
2920 return ast_for_funcdef(c, ch);
2921 case classdef:
2922 return ast_for_classdef(c, ch);
2923 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002924 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2926 TYPE(n), NCH(n));
2927 return NULL;
2928 }
2929 }
2930}
2931
2932static PyObject *
2933parsenumber(const char *s)
2934{
2935 const char *end;
2936 long x;
2937 double dx;
2938#ifndef WITHOUT_COMPLEX
2939 Py_complex c;
2940 int imflag;
2941#endif
2942
2943 errno = 0;
2944 end = s + strlen(s) - 1;
2945#ifndef WITHOUT_COMPLEX
2946 imflag = *end == 'j' || *end == 'J';
2947#endif
2948 if (*end == 'l' || *end == 'L')
2949 return PyLong_FromString((char *)s, (char **)0, 0);
2950 if (s[0] == '0') {
2951 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2952 if (x < 0 && errno == 0) {
2953 return PyLong_FromString((char *)s,
2954 (char **)0,
2955 0);
2956 }
2957 }
2958 else
2959 x = PyOS_strtol((char *)s, (char **)&end, 0);
2960 if (*end == '\0') {
2961 if (errno != 0)
2962 return PyLong_FromString((char *)s, (char **)0, 0);
2963 return PyInt_FromLong(x);
2964 }
2965 /* XXX Huge floats may silently fail */
2966#ifndef WITHOUT_COMPLEX
2967 if (imflag) {
2968 c.real = 0.;
2969 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002970 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 PyFPE_END_PROTECT(c)
2972 return PyComplex_FromCComplex(c);
2973 }
2974 else
2975#endif
2976 {
2977 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002978 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 PyFPE_END_PROTECT(dx)
2980 return PyFloat_FromDouble(dx);
2981 }
2982}
2983
2984static PyObject *
2985decode_utf8(const char **sPtr, const char *end, char* encoding)
2986{
2987#ifndef Py_USING_UNICODE
2988 Py_FatalError("decode_utf8 should not be called in this build.");
2989 return NULL;
2990#else
2991 PyObject *u, *v;
2992 char *s, *t;
2993 t = s = (char *)*sPtr;
2994 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2995 while (s < end && (*s & 0x80)) s++;
2996 *sPtr = s;
2997 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2998 if (u == NULL)
2999 return NULL;
3000 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3001 Py_DECREF(u);
3002 return v;
3003#endif
3004}
3005
3006static PyObject *
3007decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3008{
3009 PyObject *v, *u;
3010 char *buf;
3011 char *p;
3012 const char *end;
3013 if (encoding == NULL) {
3014 buf = (char *)s;
3015 u = NULL;
3016 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3017 buf = (char *)s;
3018 u = NULL;
3019 } else {
3020 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3021 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3022 if (u == NULL)
3023 return NULL;
3024 p = buf = PyString_AsString(u);
3025 end = s + len;
3026 while (s < end) {
3027 if (*s == '\\') {
3028 *p++ = *s++;
3029 if (*s & 0x80) {
3030 strcpy(p, "u005c");
3031 p += 5;
3032 }
3033 }
3034 if (*s & 0x80) { /* XXX inefficient */
3035 PyObject *w;
3036 char *r;
3037 int rn, i;
3038 w = decode_utf8(&s, end, "utf-16-be");
3039 if (w == NULL) {
3040 Py_DECREF(u);
3041 return NULL;
3042 }
3043 r = PyString_AsString(w);
3044 rn = PyString_Size(w);
3045 assert(rn % 2 == 0);
3046 for (i = 0; i < rn; i += 2) {
3047 sprintf(p, "\\u%02x%02x",
3048 r[i + 0] & 0xFF,
3049 r[i + 1] & 0xFF);
3050 p += 6;
3051 }
3052 Py_DECREF(w);
3053 } else {
3054 *p++ = *s++;
3055 }
3056 }
3057 len = p - buf;
3058 s = buf;
3059 }
3060 if (rawmode)
3061 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3062 else
3063 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3064 Py_XDECREF(u);
3065 return v;
3066}
3067
3068/* s is a Python string literal, including the bracketing quote characters,
3069 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3070 * parsestr parses it, and returns the decoded Python string object.
3071 */
3072static PyObject *
3073parsestr(const char *s, const char *encoding)
3074{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003076 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 int rawmode = 0;
3078 int need_encoding;
3079 int unicode = 0;
3080
3081 if (isalpha(quote) || quote == '_') {
3082 if (quote == 'u' || quote == 'U') {
3083 quote = *++s;
3084 unicode = 1;
3085 }
3086 if (quote == 'r' || quote == 'R') {
3087 quote = *++s;
3088 rawmode = 1;
3089 }
3090 }
3091 if (quote != '\'' && quote != '\"') {
3092 PyErr_BadInternalCall();
3093 return NULL;
3094 }
3095 s++;
3096 len = strlen(s);
3097 if (len > INT_MAX) {
3098 PyErr_SetString(PyExc_OverflowError,
3099 "string to parse is too long");
3100 return NULL;
3101 }
3102 if (s[--len] != quote) {
3103 PyErr_BadInternalCall();
3104 return NULL;
3105 }
3106 if (len >= 4 && s[0] == quote && s[1] == quote) {
3107 s += 2;
3108 len -= 2;
3109 if (s[--len] != quote || s[--len] != quote) {
3110 PyErr_BadInternalCall();
3111 return NULL;
3112 }
3113 }
3114#ifdef Py_USING_UNICODE
3115 if (unicode || Py_UnicodeFlag) {
3116 return decode_unicode(s, len, rawmode, encoding);
3117 }
3118#endif
3119 need_encoding = (encoding != NULL &&
3120 strcmp(encoding, "utf-8") != 0 &&
3121 strcmp(encoding, "iso-8859-1") != 0);
3122 if (rawmode || strchr(s, '\\') == NULL) {
3123 if (need_encoding) {
3124#ifndef Py_USING_UNICODE
3125 /* This should not happen - we never see any other
3126 encoding. */
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003127 Py_FatalError(
3128 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003130 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 if (u == NULL)
3132 return NULL;
3133 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3134 Py_DECREF(u);
3135 return v;
3136#endif
3137 } else {
3138 return PyString_FromStringAndSize(s, len);
3139 }
3140 }
3141
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003142 return PyString_DecodeEscape(s, len, NULL, unicode,
3143 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144}
3145
3146/* Build a Python string object out of a STRING atom. This takes care of
3147 * compile-time literal catenation, calling parsestr() on each piece, and
3148 * pasting the intermediate results together.
3149 */
3150static PyObject *
3151parsestrplus(struct compiling *c, const node *n)
3152{
3153 PyObject *v;
3154 int i;
3155 REQ(CHILD(n, 0), STRING);
3156 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3157 /* String literal concatenation */
3158 for (i = 1; i < NCH(n); i++) {
3159 PyObject *s;
3160 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3161 if (s == NULL)
3162 goto onError;
3163 if (PyString_Check(v) && PyString_Check(s)) {
3164 PyString_ConcatAndDel(&v, s);
3165 if (v == NULL)
3166 goto onError;
3167 }
3168#ifdef Py_USING_UNICODE
3169 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003170 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 Py_DECREF(v);
3173 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003174 if (v == NULL)
3175 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176 }
3177#endif
3178 }
3179 }
3180 return v;
3181
3182 onError:
3183 Py_XDECREF(v);
3184 return NULL;
3185}