blob: 30275a6d403d476e8c5b541b26e26fb85ab0d3b9 [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;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000251 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 }
253 else {
254 n = CHILD(n, 0);
255 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000256 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 if (!stmts)
258 goto error;
259 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000260 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 if (!s)
262 goto error;
263 asdl_seq_SET(stmts, 0, s);
264 }
265 else {
266 /* Only a simple_stmt can contain multiple statements. */
267 REQ(n, simple_stmt);
268 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 if (TYPE(CHILD(n, i)) == NEWLINE)
270 break;
271 s = ast_for_stmt(&c, CHILD(n, i));
272 if (!s)
273 goto error;
274 asdl_seq_SET(stmts, i / 2, s);
275 }
276 }
277
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000278 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 }
280 default:
281 goto error;
282 }
283 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284 ast_error_finish(filename);
285 return NULL;
286}
287
288/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
289*/
290
291static operator_ty
292get_operator(const node *n)
293{
294 switch (TYPE(n)) {
295 case VBAR:
296 return BitOr;
297 case CIRCUMFLEX:
298 return BitXor;
299 case AMPER:
300 return BitAnd;
301 case LEFTSHIFT:
302 return LShift;
303 case RIGHTSHIFT:
304 return RShift;
305 case PLUS:
306 return Add;
307 case MINUS:
308 return Sub;
309 case STAR:
310 return Mult;
311 case SLASH:
312 return Div;
313 case DOUBLESLASH:
314 return FloorDiv;
315 case PERCENT:
316 return Mod;
317 default:
318 return 0;
319 }
320}
321
Jeremy Hyltona8293132006-02-28 17:58:27 +0000322/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323
324 Only sets context for expr kinds that "can appear in assignment context"
325 (according to ../Parser/Python.asdl). For other expr kinds, it sets
326 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327*/
328
329static int
330set_context(expr_ty e, expr_context_ty ctx, const node *n)
331{
332 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000333 /* If a particular expression type can't be used for assign / delete,
334 set expr_name to its name and an error message will be generated.
335 */
336 const char* expr_name = NULL;
337
338 /* The ast defines augmented store and load contexts, but the
339 implementation here doesn't actually use them. The code may be
340 a little more complex than necessary as a result. It also means
341 that expressions in an augmented assignment have no context.
342 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000343 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000344 */
345 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346
347 switch (e->kind) {
348 case Attribute_kind:
349 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000350 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351 return ast_error(n, "assignment to None");
352 }
353 e->v.Attribute.ctx = ctx;
354 break;
355 case Subscript_kind:
356 e->v.Subscript.ctx = ctx;
357 break;
358 case Name_kind:
359 if (ctx == Store &&
360 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
361 return ast_error(n, "assignment to None");
362 }
363 e->v.Name.ctx = ctx;
364 break;
365 case List_kind:
366 e->v.List.ctx = ctx;
367 s = e->v.List.elts;
368 break;
369 case Tuple_kind:
370 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
371 return ast_error(n, "can't assign to ()");
372 e->v.Tuple.ctx = ctx;
373 s = e->v.Tuple.elts;
374 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000375 case Lambda_kind:
376 expr_name = "lambda";
377 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000381 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000383 case UnaryOp_kind:
384 expr_name = "operator";
385 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000387 expr_name = "generator expression";
388 break;
389 case ListComp_kind:
390 expr_name = "list comprehension";
391 break;
392 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393 case Num_kind:
394 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000395 expr_name = "literal";
396 break;
397 case Compare_kind:
398 expr_name = "comparison";
399 break;
400 case Repr_kind:
401 expr_name = "repr";
402 break;
403 default:
404 PyErr_Format(PyExc_SystemError,
405 "unexpected expression in assignment %d (line %d)",
406 e->kind, e->lineno);
407 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000409 /* Check for error string set by switch */
410 if (expr_name) {
411 char buf[300];
412 PyOS_snprintf(buf, sizeof(buf),
413 "can't %s %s",
414 ctx == Store ? "assign to" : "delete",
415 expr_name);
416 return ast_error(n, buf);
417 }
418
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000419 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000420 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421 */
422 if (s) {
423 int i;
424
425 for (i = 0; i < asdl_seq_LEN(s); i++) {
426 if (!set_context(asdl_seq_GET(s, i), ctx, n))
427 return 0;
428 }
429 }
430 return 1;
431}
432
433static operator_ty
434ast_for_augassign(const node *n)
435{
436 REQ(n, augassign);
437 n = CHILD(n, 0);
438 switch (STR(n)[0]) {
439 case '+':
440 return Add;
441 case '-':
442 return Sub;
443 case '/':
444 if (STR(n)[1] == '/')
445 return FloorDiv;
446 else
447 return Div;
448 case '%':
449 return Mod;
450 case '<':
451 return LShift;
452 case '>':
453 return RShift;
454 case '&':
455 return BitAnd;
456 case '^':
457 return BitXor;
458 case '|':
459 return BitOr;
460 case '*':
461 if (STR(n)[1] == '*')
462 return Pow;
463 else
464 return Mult;
465 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000466 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 return 0;
468 }
469}
470
471static cmpop_ty
472ast_for_comp_op(const node *n)
473{
474 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
475 |'is' 'not'
476 */
477 REQ(n, comp_op);
478 if (NCH(n) == 1) {
479 n = CHILD(n, 0);
480 switch (TYPE(n)) {
481 case LESS:
482 return Lt;
483 case GREATER:
484 return Gt;
485 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486 return Eq;
487 case LESSEQUAL:
488 return LtE;
489 case GREATEREQUAL:
490 return GtE;
491 case NOTEQUAL:
492 return NotEq;
493 case NAME:
494 if (strcmp(STR(n), "in") == 0)
495 return In;
496 if (strcmp(STR(n), "is") == 0)
497 return Is;
498 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000499 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 STR(n));
501 return 0;
502 }
503 }
504 else if (NCH(n) == 2) {
505 /* handle "not in" and "is not" */
506 switch (TYPE(CHILD(n, 0))) {
507 case NAME:
508 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
509 return NotIn;
510 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
511 return IsNot;
512 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000513 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
515 return 0;
516 }
517 }
Neal Norwitz79792652005-11-14 04:25:03 +0000518 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 NCH(n));
520 return 0;
521}
522
523static asdl_seq *
524seq_for_testlist(struct compiling *c, const node *n)
525{
526 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000527 asdl_seq *seq;
528 expr_ty expression;
529 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530 assert(TYPE(n) == testlist
531 || TYPE(n) == listmaker
532 || TYPE(n) == testlist_gexp
533 || TYPE(n) == testlist_safe
534 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000536 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 if (!seq)
538 return NULL;
539
540 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000541 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
543 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000544 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546
547 assert(i / 2 < seq->size);
548 asdl_seq_SET(seq, i / 2, expression);
549 }
550 return seq;
551}
552
553static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000554compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555{
556 int i, len = (NCH(n) + 1) / 2;
557 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000558 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559 if (!args)
560 return NULL;
561
562 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 for (i = 0; i < len; i++) {
564 const node *child = CHILD(CHILD(n, 2*i), 0);
565 expr_ty arg;
566 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000567 if (!strcmp(STR(child), "None")) {
568 ast_error(child, "assignment to None");
569 return NULL;
570 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000571 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child), child->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000572 c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000573 }
574 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000575 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000576 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 asdl_seq_SET(args, i, arg);
578 }
579
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000580 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000581 if (!set_context(result, Store, n))
582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 return result;
584}
585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Jeremy Hyltona8293132006-02-28 17:58:27 +0000587/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
589static arguments_ty
590ast_for_arguments(struct compiling *c, const node *n)
591{
592 /* parameters: '(' [varargslist] ')'
593 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
594 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
595 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000596 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 asdl_seq *args, *defaults;
598 identifier vararg = NULL, kwarg = NULL;
599 node *ch;
600
601 if (TYPE(n) == parameters) {
602 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000603 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 n = CHILD(n, 1);
605 }
606 REQ(n, varargslist);
607
608 /* first count the number of normal args & defaults */
609 for (i = 0; i < NCH(n); i++) {
610 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000611 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 if (TYPE(ch) == EQUAL)
614 n_defaults++;
615 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000616 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617 if (!args && n_args)
618 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000619 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 if (!defaults && n_defaults)
621 goto error;
622
623 /* fpdef: NAME | '(' fplist ')'
624 fplist: fpdef (',' fpdef)* [',']
625 */
626 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000627 j = 0; /* index for defaults */
628 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629 while (i < NCH(n)) {
630 ch = CHILD(n, i);
631 switch (TYPE(ch)) {
632 case fpdef:
633 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
634 anything other than EQUAL or a comma? */
635 /* XXX Should NCH(n) check be made a separate check? */
636 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000637 asdl_seq_SET(defaults, j++,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 ast_for_expr(c, CHILD(n, i + 2)));
639 i += 2;
640 found_default = 1;
641 }
642 else if (found_default) {
643 ast_error(n,
644 "non-default argument follows default argument");
645 goto error;
646 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 if (NCH(ch) == 3) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000648 asdl_seq_SET(args, k++,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000649 compiler_complex_args(c, CHILD(ch, 1)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 }
651 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000652 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
654 ast_error(CHILD(ch, 0), "assignment to None");
655 goto error;
656 }
Armin Rigo31441302005-10-21 12:57:31 +0000657 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000658 Param, LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659 if (!name)
660 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000661 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662
663 }
664 i += 2; /* the name and the comma */
665 break;
666 case STAR:
667 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
668 ast_error(CHILD(n, i+1), "assignment to None");
669 goto error;
670 }
671 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
672 i += 3;
673 break;
674 case DOUBLESTAR:
675 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
676 ast_error(CHILD(n, i+1), "assignment to None");
677 goto error;
678 }
679 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
680 i += 3;
681 break;
682 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000683 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684 "unexpected node in varargslist: %d @ %d",
685 TYPE(ch), i);
686 goto error;
687 }
688 }
689
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000690 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691
692 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000693 Py_XDECREF(vararg);
694 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 return NULL;
696}
697
698static expr_ty
699ast_for_dotted_name(struct compiling *c, const node *n)
700{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000701 expr_ty e;
702 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000703 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 int i;
705
706 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000707
708 lineno = LINENO(n);
709 col_offset = n->n_col_offset;
710
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 id = NEW_IDENTIFIER(CHILD(n, 0));
712 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000713 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000714 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000716 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717
718 for (i = 2; i < NCH(n); i+=2) {
719 id = NEW_IDENTIFIER(CHILD(n, i));
720 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000721 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000722 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000723 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000724 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725 }
726
727 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728}
729
730static expr_ty
731ast_for_decorator(struct compiling *c, const node *n)
732{
733 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
734 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000735 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
737 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000738 REQ(CHILD(n, 0), AT);
739 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740
741 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
742 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744
745 if (NCH(n) == 3) { /* No arguments */
746 d = name_expr;
747 name_expr = NULL;
748 }
749 else if (NCH(n) == 5) { /* Call with no arguments */
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000750 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000752 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 name_expr = NULL;
754 }
755 else {
756 d = ast_for_call(c, CHILD(n, 3), name_expr);
757 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000758 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 name_expr = NULL;
760 }
761
762 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763}
764
765static asdl_seq*
766ast_for_decorators(struct compiling *c, const node *n)
767{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000768 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000769 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 int i;
771
772 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000773 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 if (!decorator_seq)
775 return NULL;
776
777 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000778 d = ast_for_decorator(c, CHILD(n, i));
779 if (!d)
780 return NULL;
781 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 }
783 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784}
785
786static stmt_ty
787ast_for_funcdef(struct compiling *c, const node *n)
788{
789 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000790 identifier name;
791 arguments_ty args;
792 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 asdl_seq *decorator_seq = NULL;
794 int name_i;
795
796 REQ(n, funcdef);
797
798 if (NCH(n) == 6) { /* decorators are present */
799 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
800 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000801 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 name_i = 2;
803 }
804 else {
805 name_i = 1;
806 }
807
808 name = NEW_IDENTIFIER(CHILD(n, name_i));
809 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000810 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000812 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 }
815 args = ast_for_arguments(c, CHILD(n, name_i + 1));
816 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000817 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 body = ast_for_suite(c, CHILD(n, name_i + 3));
819 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000822 return FunctionDef(name, args, body, decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823}
824
825static expr_ty
826ast_for_lambdef(struct compiling *c, const node *n)
827{
828 /* lambdef: 'lambda' [varargslist] ':' test */
829 arguments_ty args;
830 expr_ty expression;
831
832 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 if (!args)
835 return NULL;
836 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000837 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 }
840 else {
841 args = ast_for_arguments(c, CHILD(n, 1));
842 if (!args)
843 return NULL;
844 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000845 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 }
848
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000849 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850}
851
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000852static expr_ty
853ast_for_ifexpr(struct compiling *c, const node *n)
854{
855 /* test: or_test 'if' or_test 'else' test */
856 expr_ty expression, body, orelse;
857
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000858 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000859 body = ast_for_expr(c, CHILD(n, 0));
860 if (!body)
861 return NULL;
862 expression = ast_for_expr(c, CHILD(n, 2));
863 if (!expression)
864 return NULL;
865 orelse = ast_for_expr(c, CHILD(n, 4));
866 if (!orelse)
867 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000868 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000869}
870
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871/* Count the number of 'for' loop in a list comprehension.
872
873 Helper for ast_for_listcomp().
874*/
875
876static int
877count_list_fors(const node *n)
878{
879 int n_fors = 0;
880 node *ch = CHILD(n, 1);
881
882 count_list_for:
883 n_fors++;
884 REQ(ch, list_for);
885 if (NCH(ch) == 5)
886 ch = CHILD(ch, 4);
887 else
888 return n_fors;
889 count_list_iter:
890 REQ(ch, list_iter);
891 ch = CHILD(ch, 0);
892 if (TYPE(ch) == list_for)
893 goto count_list_for;
894 else if (TYPE(ch) == list_if) {
895 if (NCH(ch) == 3) {
896 ch = CHILD(ch, 2);
897 goto count_list_iter;
898 }
899 else
900 return n_fors;
901 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000902
903 /* Should never be reached */
904 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
905 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906}
907
908/* Count the number of 'if' statements in a list comprehension.
909
910 Helper for ast_for_listcomp().
911*/
912
913static int
914count_list_ifs(const node *n)
915{
916 int n_ifs = 0;
917
918 count_list_iter:
919 REQ(n, list_iter);
920 if (TYPE(CHILD(n, 0)) == list_for)
921 return n_ifs;
922 n = CHILD(n, 0);
923 REQ(n, list_if);
924 n_ifs++;
925 if (NCH(n) == 2)
926 return n_ifs;
927 n = CHILD(n, 2);
928 goto count_list_iter;
929}
930
931static expr_ty
932ast_for_listcomp(struct compiling *c, const node *n)
933{
934 /* listmaker: test ( list_for | (',' test)* [','] )
935 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
936 list_iter: list_for | list_if
937 list_if: 'if' test [list_iter]
938 testlist_safe: test [(',' test)+ [',']]
939 */
940 expr_ty elt;
941 asdl_seq *listcomps;
942 int i, n_fors;
943 node *ch;
944
945 REQ(n, listmaker);
946 assert(NCH(n) > 1);
947
948 elt = ast_for_expr(c, CHILD(n, 0));
949 if (!elt)
950 return NULL;
951
952 n_fors = count_list_fors(n);
953 if (n_fors == -1)
954 return NULL;
955
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000956 listcomps = asdl_seq_new(n_fors, c->c_arena);
957 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000959
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 ch = CHILD(n, 1);
961 for (i = 0; i < n_fors; i++) {
962 comprehension_ty lc;
963 asdl_seq *t;
964 expr_ty expression;
965
966 REQ(ch, list_for);
967
968 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000969 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000971 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000972 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000975 if (asdl_seq_LEN(t) == 1)
976 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
977 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000979 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000980 expression, NULL, c->c_arena);
981 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983
984 if (NCH(ch) == 5) {
985 int j, n_ifs;
986 asdl_seq *ifs;
987
988 ch = CHILD(ch, 4);
989 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000990 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000993 ifs = asdl_seq_new(n_ifs, c->c_arena);
994 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996
997 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000998 REQ(ch, list_iter);
999 ch = CHILD(ch, 0);
1000 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001
Jeremy Hyltona8293132006-02-28 17:58:27 +00001002 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1003 if (NCH(ch) == 3)
1004 ch = CHILD(ch, 2);
1005 }
1006 /* on exit, must guarantee that ch is a list_for */
1007 if (TYPE(ch) == list_iter)
1008 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001010 }
1011 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 }
1013
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001014 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015}
1016
1017/*
1018 Count the number of 'for' loops in a generator expression.
1019
1020 Helper for ast_for_genexp().
1021*/
1022
1023static int
1024count_gen_fors(const node *n)
1025{
1026 int n_fors = 0;
1027 node *ch = CHILD(n, 1);
1028
1029 count_gen_for:
1030 n_fors++;
1031 REQ(ch, gen_for);
1032 if (NCH(ch) == 5)
1033 ch = CHILD(ch, 4);
1034 else
1035 return n_fors;
1036 count_gen_iter:
1037 REQ(ch, gen_iter);
1038 ch = CHILD(ch, 0);
1039 if (TYPE(ch) == gen_for)
1040 goto count_gen_for;
1041 else if (TYPE(ch) == gen_if) {
1042 if (NCH(ch) == 3) {
1043 ch = CHILD(ch, 2);
1044 goto count_gen_iter;
1045 }
1046 else
1047 return n_fors;
1048 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001049
1050 /* Should never be reached */
1051 PyErr_SetString(PyExc_SystemError,
1052 "logic error in count_gen_fors");
1053 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054}
1055
1056/* Count the number of 'if' statements in a generator expression.
1057
1058 Helper for ast_for_genexp().
1059*/
1060
1061static int
1062count_gen_ifs(const node *n)
1063{
1064 int n_ifs = 0;
1065
1066 while (1) {
1067 REQ(n, gen_iter);
1068 if (TYPE(CHILD(n, 0)) == gen_for)
1069 return n_ifs;
1070 n = CHILD(n, 0);
1071 REQ(n, gen_if);
1072 n_ifs++;
1073 if (NCH(n) == 2)
1074 return n_ifs;
1075 n = CHILD(n, 2);
1076 }
1077}
1078
Jeremy Hyltona8293132006-02-28 17:58:27 +00001079/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080static expr_ty
1081ast_for_genexp(struct compiling *c, const node *n)
1082{
1083 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1084 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1085 expr_ty elt;
1086 asdl_seq *genexps;
1087 int i, n_fors;
1088 node *ch;
1089
1090 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1091 assert(NCH(n) > 1);
1092
1093 elt = ast_for_expr(c, CHILD(n, 0));
1094 if (!elt)
1095 return NULL;
1096
1097 n_fors = count_gen_fors(n);
1098 if (n_fors == -1)
1099 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001100
1101 genexps = asdl_seq_new(n_fors, c->c_arena);
1102 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105 ch = CHILD(n, 1);
1106 for (i = 0; i < n_fors; i++) {
1107 comprehension_ty ge;
1108 asdl_seq *t;
1109 expr_ty expression;
1110
1111 REQ(ch, gen_for);
1112
1113 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001114 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001116 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001117 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001119
1120 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001122 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001124 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001125 expression, NULL, c->c_arena);
1126
1127 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001129
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 if (NCH(ch) == 5) {
1131 int j, n_ifs;
1132 asdl_seq *ifs;
1133
1134 ch = CHILD(ch, 4);
1135 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001136 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001138
1139 ifs = asdl_seq_new(n_ifs, c->c_arena);
1140 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001142
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 for (j = 0; j < n_ifs; j++) {
1144 REQ(ch, gen_iter);
1145 ch = CHILD(ch, 0);
1146 REQ(ch, gen_if);
1147
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001148 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001149 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001150 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001151 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 if (NCH(ch) == 3)
1153 ch = CHILD(ch, 2);
1154 }
1155 /* on exit, must guarantee that ch is a gen_for */
1156 if (TYPE(ch) == gen_iter)
1157 ch = CHILD(ch, 0);
1158 ge->ifs = ifs;
1159 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001160 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 }
1162
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001163 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164}
1165
1166static expr_ty
1167ast_for_atom(struct compiling *c, const node *n)
1168{
1169 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1170 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1171 */
1172 node *ch = CHILD(n, 0);
1173
1174 switch (TYPE(ch)) {
1175 case NAME:
1176 /* All names start in Load context, but may later be
1177 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001178 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 case STRING: {
1180 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 if (!str)
1182 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001183
1184 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001185 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 }
1187 case NUMBER: {
1188 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 if (!pynum)
1190 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001191
1192 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001193 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 }
1195 case LPAR: /* some parenthesized expressions */
1196 ch = CHILD(n, 1);
1197
1198 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001199 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200
1201 if (TYPE(ch) == yield_expr)
1202 return ast_for_expr(c, ch);
1203
1204 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1205 return ast_for_genexp(c, ch);
1206
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001207 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 case LSQB: /* list (or list comprehension) */
1209 ch = CHILD(n, 1);
1210
1211 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001212 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213
1214 REQ(ch, listmaker);
1215 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1216 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 if (!elts)
1218 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001219
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001220 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 }
1222 else
1223 return ast_for_listcomp(c, ch);
1224 case LBRACE: {
1225 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1226 int i, size;
1227 asdl_seq *keys, *values;
1228
1229 ch = CHILD(n, 1);
1230 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001231 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 if (!keys)
1233 return NULL;
1234
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001235 values = asdl_seq_new(size, c->c_arena);
1236 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238
1239 for (i = 0; i < NCH(ch); i += 4) {
1240 expr_ty expression;
1241
1242 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001243 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001245
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001247
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001249 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001251
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 asdl_seq_SET(values, i / 4, expression);
1253 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001254 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 }
1256 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001257 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 if (!expression)
1259 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001260
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001261 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 }
1263 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001264 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 return NULL;
1266 }
1267}
1268
1269static slice_ty
1270ast_for_slice(struct compiling *c, const node *n)
1271{
1272 node *ch;
1273 expr_ty lower = NULL, upper = NULL, step = NULL;
1274
1275 REQ(n, subscript);
1276
1277 /*
1278 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1279 sliceop: ':' [test]
1280 */
1281 ch = CHILD(n, 0);
1282 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001283 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284
1285 if (NCH(n) == 1 && TYPE(ch) == test) {
1286 /* 'step' variable hold no significance in terms of being used over
1287 other vars */
1288 step = ast_for_expr(c, ch);
1289 if (!step)
1290 return NULL;
1291
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001292 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293 }
1294
1295 if (TYPE(ch) == test) {
1296 lower = ast_for_expr(c, ch);
1297 if (!lower)
1298 return NULL;
1299 }
1300
1301 /* If there's an upper bound it's in the second or third position. */
1302 if (TYPE(ch) == COLON) {
1303 if (NCH(n) > 1) {
1304 node *n2 = CHILD(n, 1);
1305
1306 if (TYPE(n2) == test) {
1307 upper = ast_for_expr(c, n2);
1308 if (!upper)
1309 return NULL;
1310 }
1311 }
1312 } else if (NCH(n) > 2) {
1313 node *n2 = CHILD(n, 2);
1314
1315 if (TYPE(n2) == test) {
1316 upper = ast_for_expr(c, n2);
1317 if (!upper)
1318 return NULL;
1319 }
1320 }
1321
1322 ch = CHILD(n, NCH(n) - 1);
1323 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001324 if (NCH(ch) == 1) {
1325 /* No expression, so step is None */
1326 ch = CHILD(ch, 0);
1327 step = Name(new_identifier("None", c->c_arena), Load,
1328 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 if (!step)
1330 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001331 } else {
1332 ch = CHILD(ch, 1);
1333 if (TYPE(ch) == test) {
1334 step = ast_for_expr(c, ch);
1335 if (!step)
1336 return NULL;
1337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338 }
1339 }
1340
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001341 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342}
1343
1344static expr_ty
1345ast_for_binop(struct compiling *c, const node *n)
1346{
1347 /* Must account for a sequence of expressions.
1348 How should A op B op C by represented?
1349 BinOp(BinOp(A, op, B), op, C).
1350 */
1351
1352 int i, nops;
1353 expr_ty expr1, expr2, result;
1354 operator_ty operator;
1355
1356 expr1 = ast_for_expr(c, CHILD(n, 0));
1357 if (!expr1)
1358 return NULL;
1359
1360 expr2 = ast_for_expr(c, CHILD(n, 2));
1361 if (!expr2)
1362 return NULL;
1363
1364 operator = get_operator(CHILD(n, 1));
1365 if (!operator)
1366 return NULL;
1367
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001368 result = BinOp(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 if (!result)
1370 return NULL;
1371
1372 nops = (NCH(n) - 1) / 2;
1373 for (i = 1; i < nops; i++) {
1374 expr_ty tmp_result, tmp;
1375 const node* next_oper = CHILD(n, i * 2 + 1);
1376
1377 operator = get_operator(next_oper);
1378 if (!operator)
1379 return NULL;
1380
1381 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1382 if (!tmp)
1383 return NULL;
1384
1385 tmp_result = BinOp(result, operator, tmp,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001386 LINENO(next_oper), next_oper->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 if (!tmp)
1388 return NULL;
1389 result = tmp_result;
1390 }
1391 return result;
1392}
1393
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001394static expr_ty
1395ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1396{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001397 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1398 subscriptlist: subscript (',' subscript)* [',']
1399 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1400 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001401 REQ(n, trailer);
1402 if (TYPE(CHILD(n, 0)) == LPAR) {
1403 if (NCH(n) == 2)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001404 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001405 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001406 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001407 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001408 else if (TYPE(CHILD(n, 0)) == DOT ) {
1409 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001410 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001411 }
1412 else {
1413 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001414 REQ(CHILD(n, 2), RSQB);
1415 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001416 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001417 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1418 if (!slc)
1419 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001420 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001421 }
1422 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001423 /* The grammar is ambiguous here. The ambiguity is resolved
1424 by treating the sequence as a tuple literal if there are
1425 no slice features.
1426 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001427 int j;
1428 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001429 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001430 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001431 asdl_seq *slices, *elts;
1432 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001433 if (!slices)
1434 return NULL;
1435 for (j = 0; j < NCH(n); j += 2) {
1436 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001437 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001438 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001439 if (slc->kind != Index_kind)
1440 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001441 asdl_seq_SET(slices, j / 2, slc);
1442 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001443 if (!simple) {
1444 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001445 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001446 }
1447 /* extract Index values and put them in a Tuple */
1448 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001449 if (!elts)
1450 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001451 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1452 slc = (slice_ty)asdl_seq_GET(slices, j);
1453 assert(slc->kind == Index_kind && slc->v.Index.value);
1454 asdl_seq_SET(elts, j, slc->v.Index.value);
1455 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001456 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001457 if (!e)
1458 return NULL;
1459 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001460 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001461 }
1462 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001463}
1464
1465static expr_ty
1466ast_for_power(struct compiling *c, const node *n)
1467{
1468 /* power: atom trailer* ('**' factor)*
1469 */
1470 int i;
1471 expr_ty e, tmp;
1472 REQ(n, power);
1473 e = ast_for_atom(c, CHILD(n, 0));
1474 if (!e)
1475 return NULL;
1476 if (NCH(n) == 1)
1477 return e;
1478 for (i = 1; i < NCH(n); i++) {
1479 node *ch = CHILD(n, i);
1480 if (TYPE(ch) != trailer)
1481 break;
1482 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001483 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001484 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001485 tmp->lineno = e->lineno;
1486 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001487 e = tmp;
1488 }
1489 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1490 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001491 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001492 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001493 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001494 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001495 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001496 e = tmp;
1497 }
1498 return e;
1499}
1500
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501/* Do not name a variable 'expr'! Will cause a compile error.
1502*/
1503
1504static expr_ty
1505ast_for_expr(struct compiling *c, const node *n)
1506{
1507 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001508 test: or_test ['if' or_test 'else' test] | lambdef
1509 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 and_test: not_test ('and' not_test)*
1511 not_test: 'not' not_test | comparison
1512 comparison: expr (comp_op expr)*
1513 expr: xor_expr ('|' xor_expr)*
1514 xor_expr: and_expr ('^' and_expr)*
1515 and_expr: shift_expr ('&' shift_expr)*
1516 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1517 arith_expr: term (('+'|'-') term)*
1518 term: factor (('*'|'/'|'%'|'//') factor)*
1519 factor: ('+'|'-'|'~') factor | power
1520 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001521
1522 As well as modified versions that exist for backward compatibility,
1523 to explicitly allow:
1524 [ x for x in lambda: 0, lambda: 1 ]
1525 (which would be ambiguous without these extra rules)
1526
1527 old_test: or_test | old_lambdef
1528 old_lambdef: 'lambda' [vararglist] ':' old_test
1529
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530 */
1531
1532 asdl_seq *seq;
1533 int i;
1534
1535 loop:
1536 switch (TYPE(n)) {
1537 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001538 case old_test:
1539 if (TYPE(CHILD(n, 0)) == lambdef ||
1540 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001542 else if (NCH(n) > 1)
1543 return ast_for_ifexpr(c, n);
1544 /* Fallthrough */
1545 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 case and_test:
1547 if (NCH(n) == 1) {
1548 n = CHILD(n, 0);
1549 goto loop;
1550 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001551 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552 if (!seq)
1553 return NULL;
1554 for (i = 0; i < NCH(n); i += 2) {
1555 expr_ty e = ast_for_expr(c, CHILD(n, i));
1556 if (!e)
1557 return NULL;
1558 asdl_seq_SET(seq, i / 2, e);
1559 }
1560 if (!strcmp(STR(CHILD(n, 1)), "and"))
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001561 return BoolOp(And, seq, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001562 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001563 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 case not_test:
1565 if (NCH(n) == 1) {
1566 n = CHILD(n, 0);
1567 goto loop;
1568 }
1569 else {
1570 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1571 if (!expression)
1572 return NULL;
1573
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001574 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 }
1576 case comparison:
1577 if (NCH(n) == 1) {
1578 n = CHILD(n, 0);
1579 goto loop;
1580 }
1581 else {
1582 expr_ty expression;
1583 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001584 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585 if (!ops)
1586 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001587 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589 return NULL;
1590 }
1591 for (i = 1; i < NCH(n); i += 2) {
1592 /* XXX cmpop_ty is just an enum */
1593 cmpop_ty operator;
1594
1595 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001596 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001598 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599
1600 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001601 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001603 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001605 asdl_seq_SET(ops, i / 2, (void *)(Py_uintptr_t)operator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 asdl_seq_SET(cmps, i / 2, expression);
1607 }
1608 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001609 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001611 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001613 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614 }
1615 break;
1616
1617 /* The next five cases all handle BinOps. The main body of code
1618 is the same in each case, but the switch turned inside out to
1619 reuse the code for each type of operator.
1620 */
1621 case expr:
1622 case xor_expr:
1623 case and_expr:
1624 case shift_expr:
1625 case arith_expr:
1626 case term:
1627 if (NCH(n) == 1) {
1628 n = CHILD(n, 0);
1629 goto loop;
1630 }
1631 return ast_for_binop(c, n);
1632 case yield_expr: {
1633 expr_ty exp = NULL;
1634 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001635 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 if (!exp)
1637 return NULL;
1638 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001639 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 }
1641 case factor: {
1642 expr_ty expression;
1643
1644 if (NCH(n) == 1) {
1645 n = CHILD(n, 0);
1646 goto loop;
1647 }
1648
1649 expression = ast_for_expr(c, CHILD(n, 1));
1650 if (!expression)
1651 return NULL;
1652
1653 switch (TYPE(CHILD(n, 0))) {
1654 case PLUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001655 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 case MINUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001657 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658 case TILDE:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001659 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001661 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1662 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663 break;
1664 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001665 case power:
1666 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001668 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669 return NULL;
1670 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001671 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672 return NULL;
1673}
1674
1675static expr_ty
1676ast_for_call(struct compiling *c, const node *n, expr_ty func)
1677{
1678 /*
1679 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1680 | '**' test)
1681 argument: [test '='] test [gen_for] # Really [keyword '='] test
1682 */
1683
1684 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001685 asdl_seq *args;
1686 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 expr_ty vararg = NULL, kwarg = NULL;
1688
1689 REQ(n, arglist);
1690
1691 nargs = 0;
1692 nkeywords = 0;
1693 ngens = 0;
1694 for (i = 0; i < NCH(n); i++) {
1695 node *ch = CHILD(n, i);
1696 if (TYPE(ch) == argument) {
1697 if (NCH(ch) == 1)
1698 nargs++;
1699 else if (TYPE(CHILD(ch, 1)) == gen_for)
1700 ngens++;
1701 else
1702 nkeywords++;
1703 }
1704 }
1705 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001706 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 "if not sole argument");
1708 return NULL;
1709 }
1710
1711 if (nargs + nkeywords + ngens > 255) {
1712 ast_error(n, "more than 255 arguments");
1713 return NULL;
1714 }
1715
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001716 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001718 return NULL;
1719 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 nargs = 0;
1723 nkeywords = 0;
1724 for (i = 0; i < NCH(n); i++) {
1725 node *ch = CHILD(n, i);
1726 if (TYPE(ch) == argument) {
1727 expr_ty e;
1728 if (NCH(ch) == 1) {
1729 e = ast_for_expr(c, CHILD(ch, 0));
1730 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001731 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 asdl_seq_SET(args, nargs++, e);
1733 }
1734 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1735 e = ast_for_genexp(c, ch);
1736 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 asdl_seq_SET(args, nargs++, e);
1739 }
1740 else {
1741 keyword_ty kw;
1742 identifier key;
1743
1744 /* CHILD(ch, 0) is test, but must be an identifier? */
1745 e = ast_for_expr(c, CHILD(ch, 0));
1746 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001747 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 /* f(lambda x: x[0] = 3) ends up getting parsed with
1749 * LHS test = lambda x: x[0], and RHS test = 3.
1750 * SF bug 132313 points out that complaining about a keyword
1751 * then is very confusing.
1752 */
1753 if (e->kind == Lambda_kind) {
1754 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 } else if (e->kind != Name_kind) {
1757 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001758 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 }
1760 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 e = ast_for_expr(c, CHILD(ch, 2));
1762 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001763 return NULL;
1764 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001766 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 asdl_seq_SET(keywords, nkeywords++, kw);
1768 }
1769 }
1770 else if (TYPE(ch) == STAR) {
1771 vararg = ast_for_expr(c, CHILD(n, i+1));
1772 i++;
1773 }
1774 else if (TYPE(ch) == DOUBLESTAR) {
1775 kwarg = ast_for_expr(c, CHILD(n, i+1));
1776 i++;
1777 }
1778 }
1779
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001780 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781}
1782
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001784ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001786 /* testlist_gexp: test (',' test)* [','] */
1787 /* testlist: test (',' test)* [','] */
1788 /* testlist_safe: test (',' test)+ [','] */
1789 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001791 if (TYPE(n) == testlist_gexp) {
1792 if (NCH(n) > 1)
1793 assert(TYPE(CHILD(n, 1)) != gen_for);
1794 }
1795 else {
1796 assert(TYPE(n) == testlist ||
1797 TYPE(n) == testlist_safe ||
1798 TYPE(n) == testlist1);
1799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800 if (NCH(n) == 1)
1801 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 else {
1803 asdl_seq *tmp = seq_for_testlist(c, n);
1804 if (!tmp)
1805 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001806 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001808}
1809
1810static expr_ty
1811ast_for_testlist_gexp(struct compiling *c, const node* n)
1812{
1813 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1814 /* argument: test [ gen_for ] */
1815 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001816 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001817 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001818 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001819}
1820
1821/* like ast_for_testlist() but returns a sequence */
1822static asdl_seq*
1823ast_for_class_bases(struct compiling *c, const node* n)
1824{
1825 /* testlist: test (',' test)* [','] */
1826 assert(NCH(n) > 0);
1827 REQ(n, testlist);
1828 if (NCH(n) == 1) {
1829 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001830 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001831 if (!bases)
1832 return NULL;
1833 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001834 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001835 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001836 asdl_seq_SET(bases, 0, base);
1837 return bases;
1838 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001839
1840 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841}
1842
1843static stmt_ty
1844ast_for_expr_stmt(struct compiling *c, const node *n)
1845{
1846 REQ(n, expr_stmt);
1847 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1848 | ('=' (yield_expr|testlist))*)
1849 testlist: test (',' test)* [',']
1850 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1851 | '<<=' | '>>=' | '**=' | '//='
1852 test: ... here starts the operator precendence dance
1853 */
1854
1855 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001856 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 if (!e)
1858 return NULL;
1859
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001860 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 }
1862 else if (TYPE(CHILD(n, 1)) == augassign) {
1863 expr_ty expr1, expr2;
1864 operator_ty operator;
1865 node *ch = CHILD(n, 0);
1866
1867 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001868 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001870 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872
1873 if (!expr1)
1874 return NULL;
Neal Norwitz96e48d42006-02-05 02:07:19 +00001875 /* TODO(jhylton): Figure out why set_context() can't be used here. */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001876 switch (expr1->kind) {
1877 case GeneratorExp_kind:
1878 ast_error(ch, "augmented assignment to generator "
1879 "expression not possible");
1880 return NULL;
1881 case Name_kind: {
1882 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1883 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1884 ast_error(ch, "assignment to None");
1885 return NULL;
1886 }
1887 break;
1888 }
1889 case Attribute_kind:
1890 case Subscript_kind:
1891 break;
1892 default:
1893 ast_error(ch, "illegal expression for augmented "
1894 "assignment");
1895 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897
1898 ch = CHILD(n, 2);
1899 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001900 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001902 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), ch->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001903 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904 return NULL;
1905
1906 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001907 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 return NULL;
1909
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001910 return AugAssign(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 }
1912 else {
1913 int i;
1914 asdl_seq *targets;
1915 node *value;
1916 expr_ty expression;
1917
1918 /* a normal assignment */
1919 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001920 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 if (!targets)
1922 return NULL;
1923 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001924 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 node *ch = CHILD(n, i);
1926 if (TYPE(ch) == yield_expr) {
1927 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001928 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001930 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931
1932 /* set context to assign */
1933 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001934 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935
Neal Norwitz84456bd2005-12-18 03:16:20 +00001936 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001937 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938
1939 asdl_seq_SET(targets, i / 2, e);
1940 }
1941 value = CHILD(n, NCH(n) - 1);
1942 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001943 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 else
1945 expression = ast_for_expr(c, value);
1946 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001947 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001948 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950}
1951
1952static stmt_ty
1953ast_for_print_stmt(struct compiling *c, const node *n)
1954{
1955 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1956 | '>>' test [ (',' test)+ [','] ] )
1957 */
1958 expr_ty dest = NULL, expression;
1959 asdl_seq *seq;
1960 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001961 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962
1963 REQ(n, print_stmt);
1964 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1965 dest = ast_for_expr(c, CHILD(n, 2));
1966 if (!dest)
1967 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001968 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001970 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00001972 return NULL;
1973 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001975 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001977 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978 }
1979 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001980 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981}
1982
1983static asdl_seq *
1984ast_for_exprlist(struct compiling *c, const node *n, int context)
1985{
1986 asdl_seq *seq;
1987 int i;
1988 expr_ty e;
1989
1990 REQ(n, exprlist);
1991
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001992 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 if (!seq)
1994 return NULL;
1995 for (i = 0; i < NCH(n); i += 2) {
1996 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001997 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001998 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00001999 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002000 if (context && !set_context(e, context, CHILD(n, i)))
2001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 }
2003 return seq;
2004}
2005
2006static stmt_ty
2007ast_for_del_stmt(struct compiling *c, const node *n)
2008{
2009 asdl_seq *expr_list;
2010
2011 /* del_stmt: 'del' exprlist */
2012 REQ(n, del_stmt);
2013
2014 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2015 if (!expr_list)
2016 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002017 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018}
2019
2020static stmt_ty
2021ast_for_flow_stmt(struct compiling *c, const node *n)
2022{
2023 /*
2024 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2025 | yield_stmt
2026 break_stmt: 'break'
2027 continue_stmt: 'continue'
2028 return_stmt: 'return' [testlist]
2029 yield_stmt: yield_expr
2030 yield_expr: 'yield' testlist
2031 raise_stmt: 'raise' [test [',' test [',' test]]]
2032 */
2033 node *ch;
2034
2035 REQ(n, flow_stmt);
2036 ch = CHILD(n, 0);
2037 switch (TYPE(ch)) {
2038 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002039 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002041 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 case yield_stmt: { /* will reduce to yield_expr */
2043 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2044 if (!exp)
2045 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002046 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 }
2048 case return_stmt:
2049 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002050 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002052 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 if (!expression)
2054 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002055 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 }
2057 case raise_stmt:
2058 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002059 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 else if (NCH(ch) == 2) {
2061 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2062 if (!expression)
2063 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002064 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 }
2066 else if (NCH(ch) == 4) {
2067 expr_ty expr1, expr2;
2068
2069 expr1 = ast_for_expr(c, CHILD(ch, 1));
2070 if (!expr1)
2071 return NULL;
2072 expr2 = ast_for_expr(c, CHILD(ch, 3));
2073 if (!expr2)
2074 return NULL;
2075
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002076 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 }
2078 else if (NCH(ch) == 6) {
2079 expr_ty expr1, expr2, expr3;
2080
2081 expr1 = ast_for_expr(c, CHILD(ch, 1));
2082 if (!expr1)
2083 return NULL;
2084 expr2 = ast_for_expr(c, CHILD(ch, 3));
2085 if (!expr2)
2086 return NULL;
2087 expr3 = ast_for_expr(c, CHILD(ch, 5));
2088 if (!expr3)
2089 return NULL;
2090
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002091 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 }
2093 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002094 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 "unexpected flow_stmt: %d", TYPE(ch));
2096 return NULL;
2097 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002098
2099 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2100 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101}
2102
2103static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002104alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105{
2106 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002107 import_as_name: NAME ['as' NAME]
2108 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 dotted_name: NAME ('.' NAME)*
2110 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002111 PyObject *str;
2112
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 loop:
2114 switch (TYPE(n)) {
2115 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002116 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2117 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 case dotted_as_name:
2119 if (NCH(n) == 1) {
2120 n = CHILD(n, 0);
2121 goto loop;
2122 }
2123 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002124 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 assert(!a->asname);
2126 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2127 return a;
2128 }
2129 break;
2130 case dotted_name:
2131 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002132 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 else {
2134 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002135 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002136 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 char *s;
2138
2139 len = 0;
2140 for (i = 0; i < NCH(n); i += 2)
2141 /* length of string plus one for the dot */
2142 len += strlen(STR(CHILD(n, i))) + 1;
2143 len--; /* the last name doesn't have a dot */
2144 str = PyString_FromStringAndSize(NULL, len);
2145 if (!str)
2146 return NULL;
2147 s = PyString_AS_STRING(str);
2148 if (!s)
2149 return NULL;
2150 for (i = 0; i < NCH(n); i += 2) {
2151 char *sch = STR(CHILD(n, i));
2152 strcpy(s, STR(CHILD(n, i)));
2153 s += strlen(sch);
2154 *s++ = '.';
2155 }
2156 --s;
2157 *s = '\0';
2158 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002159 PyArena_AddPyObject(c->c_arena, str);
2160 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 }
2162 break;
2163 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002164 str = PyString_InternFromString("*");
2165 PyArena_AddPyObject(c->c_arena, str);
2166 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002168 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 "unexpected import name: %d", TYPE(n));
2170 return NULL;
2171 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002172
2173 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 return NULL;
2175}
2176
2177static stmt_ty
2178ast_for_import_stmt(struct compiling *c, const node *n)
2179{
2180 /*
2181 import_stmt: import_name | import_from
2182 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002183 import_from: 'from' ('.'* dotted_name | '.') 'import'
2184 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002186 int lineno;
2187 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 int i;
2189 asdl_seq *aliases;
2190
2191 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002192 lineno = LINENO(n);
2193 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002195 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002197 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002198 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 if (!aliases)
2200 return NULL;
2201 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002202 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002203 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205 asdl_seq_SET(aliases, i / 2, import_alias);
2206 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002207 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002209 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002211 int idx, ndots = 0;
2212 alias_ty mod = NULL;
2213 identifier modname;
2214
2215 /* Count the number of dots (for relative imports) and check for the
2216 optional module name */
2217 for (idx = 1; idx < NCH(n); idx++) {
2218 if (TYPE(CHILD(n, idx)) == dotted_name) {
2219 mod = alias_for_import_name(c, CHILD(n, idx));
2220 idx++;
2221 break;
2222 } else if (TYPE(CHILD(n, idx)) != DOT) {
2223 break;
2224 }
2225 ndots++;
2226 }
2227 idx++; /* skip over the 'import' keyword */
2228 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002229 case STAR:
2230 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002231 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002232 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002233 if (ndots) {
2234 ast_error(n, "'import *' not allowed with 'from .'");
2235 return NULL;
2236 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002237 break;
2238 case LPAR:
2239 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002240 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002241 n_children = NCH(n);
2242 break;
2243 case import_as_names:
2244 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002245 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002246 n_children = NCH(n);
2247 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 ast_error(n, "trailing comma not allowed without"
2249 " surrounding parentheses");
2250 return NULL;
2251 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002252 break;
2253 default:
2254 ast_error(n, "Unexpected node-type in from-import");
2255 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002256 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002258 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002259 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261
2262 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002263 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002264 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002265 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002267 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002269 else {
2270 for (i = 0; i < NCH(n); i += 2) {
2271 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2272 if (!import_alias)
2273 return NULL;
2274 asdl_seq_SET(aliases, i / 2, import_alias);
2275 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002277 if (mod != NULL)
2278 modname = mod->name;
2279 else
2280 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002281 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002282 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 }
Neal Norwitz79792652005-11-14 04:25:03 +00002284 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 "unknown import statement: starts with command '%s'",
2286 STR(CHILD(n, 0)));
2287 return NULL;
2288}
2289
2290static stmt_ty
2291ast_for_global_stmt(struct compiling *c, const node *n)
2292{
2293 /* global_stmt: 'global' NAME (',' NAME)* */
2294 identifier name;
2295 asdl_seq *s;
2296 int i;
2297
2298 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002299 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 if (!s)
2301 return NULL;
2302 for (i = 1; i < NCH(n); i += 2) {
2303 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002304 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 asdl_seq_SET(s, i / 2, name);
2307 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002308 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309}
2310
2311static stmt_ty
2312ast_for_exec_stmt(struct compiling *c, const node *n)
2313{
2314 expr_ty expr1, globals = NULL, locals = NULL;
2315 int n_children = NCH(n);
2316 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002317 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318 "poorly formed 'exec' statement: %d parts to statement",
2319 n_children);
2320 return NULL;
2321 }
2322
2323 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2324 REQ(n, exec_stmt);
2325 expr1 = ast_for_expr(c, CHILD(n, 1));
2326 if (!expr1)
2327 return NULL;
2328 if (n_children >= 4) {
2329 globals = ast_for_expr(c, CHILD(n, 3));
2330 if (!globals)
2331 return NULL;
2332 }
2333 if (n_children == 6) {
2334 locals = ast_for_expr(c, CHILD(n, 5));
2335 if (!locals)
2336 return NULL;
2337 }
2338
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002339 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340}
2341
2342static stmt_ty
2343ast_for_assert_stmt(struct compiling *c, const node *n)
2344{
2345 /* assert_stmt: 'assert' test [',' test] */
2346 REQ(n, assert_stmt);
2347 if (NCH(n) == 2) {
2348 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2349 if (!expression)
2350 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002351 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352 }
2353 else if (NCH(n) == 4) {
2354 expr_ty expr1, expr2;
2355
2356 expr1 = ast_for_expr(c, CHILD(n, 1));
2357 if (!expr1)
2358 return NULL;
2359 expr2 = ast_for_expr(c, CHILD(n, 3));
2360 if (!expr2)
2361 return NULL;
2362
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002363 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 }
Neal Norwitz79792652005-11-14 04:25:03 +00002365 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366 "improper number of parts to 'assert' statement: %d",
2367 NCH(n));
2368 return NULL;
2369}
2370
2371static asdl_seq *
2372ast_for_suite(struct compiling *c, const node *n)
2373{
2374 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002375 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 stmt_ty s;
2377 int i, total, num, end, pos = 0;
2378 node *ch;
2379
2380 REQ(n, suite);
2381
2382 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002383 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384 if (!seq)
2385 return NULL;
2386 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2387 n = CHILD(n, 0);
2388 /* simple_stmt always ends with a NEWLINE,
2389 and may have a trailing SEMI
2390 */
2391 end = NCH(n) - 1;
2392 if (TYPE(CHILD(n, end - 1)) == SEMI)
2393 end--;
2394 /* loop by 2 to skip semi-colons */
2395 for (i = 0; i < end; i += 2) {
2396 ch = CHILD(n, i);
2397 s = ast_for_stmt(c, ch);
2398 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002399 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400 asdl_seq_SET(seq, pos++, s);
2401 }
2402 }
2403 else {
2404 for (i = 2; i < (NCH(n) - 1); i++) {
2405 ch = CHILD(n, i);
2406 REQ(ch, stmt);
2407 num = num_stmts(ch);
2408 if (num == 1) {
2409 /* small_stmt or compound_stmt with only one child */
2410 s = ast_for_stmt(c, ch);
2411 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002412 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413 asdl_seq_SET(seq, pos++, s);
2414 }
2415 else {
2416 int j;
2417 ch = CHILD(ch, 0);
2418 REQ(ch, simple_stmt);
2419 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002420 /* statement terminates with a semi-colon ';' */
2421 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002422 assert((j + 1) == NCH(ch));
2423 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002424 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425 s = ast_for_stmt(c, CHILD(ch, j));
2426 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002427 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 asdl_seq_SET(seq, pos++, s);
2429 }
2430 }
2431 }
2432 }
2433 assert(pos == seq->size);
2434 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435}
2436
2437static stmt_ty
2438ast_for_if_stmt(struct compiling *c, const node *n)
2439{
2440 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2441 ['else' ':' suite]
2442 */
2443 char *s;
2444
2445 REQ(n, if_stmt);
2446
2447 if (NCH(n) == 4) {
2448 expr_ty expression;
2449 asdl_seq *suite_seq;
2450
2451 expression = ast_for_expr(c, CHILD(n, 1));
2452 if (!expression)
2453 return NULL;
2454 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002455 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 return NULL;
2457
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002458 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002460
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 s = STR(CHILD(n, 4));
2462 /* s[2], the third character in the string, will be
2463 's' for el_s_e, or
2464 'i' for el_i_f
2465 */
2466 if (s[2] == 's') {
2467 expr_ty expression;
2468 asdl_seq *seq1, *seq2;
2469
2470 expression = ast_for_expr(c, CHILD(n, 1));
2471 if (!expression)
2472 return NULL;
2473 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002474 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 return NULL;
2476 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002477 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 return NULL;
2479
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002480 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 }
2482 else if (s[2] == 'i') {
2483 int i, n_elif, has_else = 0;
2484 asdl_seq *orelse = NULL;
2485 n_elif = NCH(n) - 4;
2486 /* must reference the child n_elif+1 since 'else' token is third,
2487 not fourth, child from the end. */
2488 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2489 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2490 has_else = 1;
2491 n_elif -= 3;
2492 }
2493 n_elif /= 4;
2494
2495 if (has_else) {
2496 expr_ty expression;
2497 asdl_seq *seq1, *seq2;
2498
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002499 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 if (!orelse)
2501 return NULL;
2502 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002503 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002506 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002509 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511
2512 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002513 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002514 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 /* the just-created orelse handled the last elif */
2516 n_elif--;
2517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518
2519 for (i = 0; i < n_elif; i++) {
2520 int off = 5 + (n_elif - i - 1) * 4;
2521 expr_ty expression;
2522 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002523 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002524 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 return NULL;
2526 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002527 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002530 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532
2533 asdl_seq_SET(new, 0,
2534 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002535 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 orelse = new;
2537 }
2538 return If(ast_for_expr(c, CHILD(n, 1)),
2539 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002540 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002542
2543 PyErr_Format(PyExc_SystemError,
2544 "unexpected token in 'if' statement: %s", s);
2545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546}
2547
2548static stmt_ty
2549ast_for_while_stmt(struct compiling *c, const node *n)
2550{
2551 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2552 REQ(n, while_stmt);
2553
2554 if (NCH(n) == 4) {
2555 expr_ty expression;
2556 asdl_seq *suite_seq;
2557
2558 expression = ast_for_expr(c, CHILD(n, 1));
2559 if (!expression)
2560 return NULL;
2561 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002562 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002564 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 }
2566 else if (NCH(n) == 7) {
2567 expr_ty expression;
2568 asdl_seq *seq1, *seq2;
2569
2570 expression = ast_for_expr(c, CHILD(n, 1));
2571 if (!expression)
2572 return NULL;
2573 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002574 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 return NULL;
2576 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002577 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 return NULL;
2579
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002580 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002582
2583 PyErr_Format(PyExc_SystemError,
2584 "wrong number of tokens for 'while' statement: %d",
2585 NCH(n));
2586 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587}
2588
2589static stmt_ty
2590ast_for_for_stmt(struct compiling *c, const node *n)
2591{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002592 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 expr_ty expression;
2594 expr_ty target;
2595 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2596 REQ(n, for_stmt);
2597
2598 if (NCH(n) == 9) {
2599 seq = ast_for_suite(c, CHILD(n, 8));
2600 if (!seq)
2601 return NULL;
2602 }
2603
2604 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002605 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002607 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002610 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002612 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002613 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 return NULL;
2615 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002616 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 return NULL;
2618
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002619 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620}
2621
2622static excepthandler_ty
2623ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2624{
2625 /* except_clause: 'except' [test [',' test]] */
2626 REQ(exc, except_clause);
2627 REQ(body, suite);
2628
2629 if (NCH(exc) == 1) {
2630 asdl_seq *suite_seq = ast_for_suite(c, body);
2631 if (!suite_seq)
2632 return NULL;
2633
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002634 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 }
2636 else if (NCH(exc) == 2) {
2637 expr_ty expression;
2638 asdl_seq *suite_seq;
2639
2640 expression = ast_for_expr(c, CHILD(exc, 1));
2641 if (!expression)
2642 return NULL;
2643 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002644 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 return NULL;
2646
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002647 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 }
2649 else if (NCH(exc) == 4) {
2650 asdl_seq *suite_seq;
2651 expr_ty expression;
2652 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2653 if (!e)
2654 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002655 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 return NULL;
2657 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002658 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 return NULL;
2660 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002661 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 return NULL;
2663
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002664 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002666
2667 PyErr_Format(PyExc_SystemError,
2668 "wrong number of children for 'except' clause: %d",
2669 NCH(exc));
2670 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671}
2672
2673static stmt_ty
2674ast_for_try_stmt(struct compiling *c, const node *n)
2675{
Neal Norwitzf599f422005-12-17 21:33:47 +00002676 const int nch = NCH(n);
2677 int n_except = (nch - 3)/3;
2678 asdl_seq *body, *orelse = NULL, *finally = NULL;
2679
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680 REQ(n, try_stmt);
2681
Neal Norwitzf599f422005-12-17 21:33:47 +00002682 body = ast_for_suite(c, CHILD(n, 2));
2683 if (body == NULL)
2684 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685
Neal Norwitzf599f422005-12-17 21:33:47 +00002686 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2687 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2688 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2689 /* we can assume it's an "else",
2690 because nch >= 9 for try-else-finally and
2691 it would otherwise have a type of except_clause */
2692 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2693 if (orelse == NULL)
2694 return NULL;
2695 n_except--;
2696 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697
Neal Norwitzf599f422005-12-17 21:33:47 +00002698 finally = ast_for_suite(c, CHILD(n, nch - 1));
2699 if (finally == NULL)
2700 return NULL;
2701 n_except--;
2702 }
2703 else {
2704 /* we can assume it's an "else",
2705 otherwise it would have a type of except_clause */
2706 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2707 if (orelse == NULL)
2708 return NULL;
2709 n_except--;
2710 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002712 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002713 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 return NULL;
2715 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002716
2717 if (n_except > 0) {
2718 int i;
2719 stmt_ty except_st;
2720 /* process except statements to create a try ... except */
2721 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2722 if (handlers == NULL)
2723 return NULL;
2724
2725 for (i = 0; i < n_except; i++) {
2726 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2727 CHILD(n, 5 + i * 3));
2728 if (!e)
2729 return NULL;
2730 asdl_seq_SET(handlers, i, e);
2731 }
2732
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002733 except_st = TryExcept(body, handlers, orelse, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002734 if (!finally)
2735 return except_st;
2736
2737 /* if a 'finally' is present too, we nest the TryExcept within a
2738 TryFinally to emulate try ... except ... finally */
2739 body = asdl_seq_new(1, c->c_arena);
2740 if (body == NULL)
2741 return NULL;
2742 asdl_seq_SET(body, 0, except_st);
2743 }
2744
2745 /* must be a try ... finally (except clauses are in body, if any exist) */
2746 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002747 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748}
2749
Guido van Rossumc2e20742006-02-27 22:32:47 +00002750static expr_ty
2751ast_for_with_var(struct compiling *c, const node *n)
2752{
2753 REQ(n, with_var);
2754 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2755 ast_error(n, "expected \"with [expr] as [var]\"");
2756 return NULL;
2757 }
2758 return ast_for_expr(c, CHILD(n, 1));
2759}
2760
2761/* with_stmt: 'with' test [ with_var ] ':' suite */
2762static stmt_ty
2763ast_for_with_stmt(struct compiling *c, const node *n)
2764{
2765 expr_ty context_expr, optional_vars = NULL;
2766 int suite_index = 3; /* skip 'with', test, and ':' */
2767 asdl_seq *suite_seq;
2768
2769 assert(TYPE(n) == with_stmt);
2770 context_expr = ast_for_expr(c, CHILD(n, 1));
2771 if (TYPE(CHILD(n, 2)) == with_var) {
2772 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2773
2774 if (!optional_vars) {
2775 return NULL;
2776 }
2777 if (!set_context(optional_vars, Store, n)) {
2778 return NULL;
2779 }
2780 suite_index = 4;
2781 }
2782
2783 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2784 if (!suite_seq) {
2785 return NULL;
2786 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002787 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2788 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002789}
2790
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791static stmt_ty
2792ast_for_classdef(struct compiling *c, const node *n)
2793{
2794 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 asdl_seq *bases, *s;
2796
2797 REQ(n, classdef);
2798
2799 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2800 ast_error(n, "assignment to None");
2801 return NULL;
2802 }
2803
2804 if (NCH(n) == 4) {
2805 s = ast_for_suite(c, CHILD(n, 3));
2806 if (!s)
2807 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002808 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002809 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 }
2811 /* check for empty base list */
2812 if (TYPE(CHILD(n,3)) == RPAR) {
2813 s = ast_for_suite(c, CHILD(n,5));
2814 if (!s)
2815 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002816 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002817 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 }
2819
2820 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002821 bases = ast_for_class_bases(c, CHILD(n, 3));
2822 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824
2825 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002826 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002828 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002829 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830}
2831
2832static stmt_ty
2833ast_for_stmt(struct compiling *c, const node *n)
2834{
2835 if (TYPE(n) == stmt) {
2836 assert(NCH(n) == 1);
2837 n = CHILD(n, 0);
2838 }
2839 if (TYPE(n) == simple_stmt) {
2840 assert(num_stmts(n) == 1);
2841 n = CHILD(n, 0);
2842 }
2843 if (TYPE(n) == small_stmt) {
2844 REQ(n, small_stmt);
2845 n = CHILD(n, 0);
2846 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2847 | flow_stmt | import_stmt | global_stmt | exec_stmt
2848 | assert_stmt
2849 */
2850 switch (TYPE(n)) {
2851 case expr_stmt:
2852 return ast_for_expr_stmt(c, n);
2853 case print_stmt:
2854 return ast_for_print_stmt(c, n);
2855 case del_stmt:
2856 return ast_for_del_stmt(c, n);
2857 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002858 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 case flow_stmt:
2860 return ast_for_flow_stmt(c, n);
2861 case import_stmt:
2862 return ast_for_import_stmt(c, n);
2863 case global_stmt:
2864 return ast_for_global_stmt(c, n);
2865 case exec_stmt:
2866 return ast_for_exec_stmt(c, n);
2867 case assert_stmt:
2868 return ast_for_assert_stmt(c, n);
2869 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002870 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2872 TYPE(n), NCH(n));
2873 return NULL;
2874 }
2875 }
2876 else {
2877 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2878 | funcdef | classdef
2879 */
2880 node *ch = CHILD(n, 0);
2881 REQ(n, compound_stmt);
2882 switch (TYPE(ch)) {
2883 case if_stmt:
2884 return ast_for_if_stmt(c, ch);
2885 case while_stmt:
2886 return ast_for_while_stmt(c, ch);
2887 case for_stmt:
2888 return ast_for_for_stmt(c, ch);
2889 case try_stmt:
2890 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002891 case with_stmt:
2892 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 case funcdef:
2894 return ast_for_funcdef(c, ch);
2895 case classdef:
2896 return ast_for_classdef(c, ch);
2897 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002898 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2900 TYPE(n), NCH(n));
2901 return NULL;
2902 }
2903 }
2904}
2905
2906static PyObject *
2907parsenumber(const char *s)
2908{
2909 const char *end;
2910 long x;
2911 double dx;
2912#ifndef WITHOUT_COMPLEX
2913 Py_complex c;
2914 int imflag;
2915#endif
2916
2917 errno = 0;
2918 end = s + strlen(s) - 1;
2919#ifndef WITHOUT_COMPLEX
2920 imflag = *end == 'j' || *end == 'J';
2921#endif
2922 if (*end == 'l' || *end == 'L')
2923 return PyLong_FromString((char *)s, (char **)0, 0);
2924 if (s[0] == '0') {
2925 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2926 if (x < 0 && errno == 0) {
2927 return PyLong_FromString((char *)s,
2928 (char **)0,
2929 0);
2930 }
2931 }
2932 else
2933 x = PyOS_strtol((char *)s, (char **)&end, 0);
2934 if (*end == '\0') {
2935 if (errno != 0)
2936 return PyLong_FromString((char *)s, (char **)0, 0);
2937 return PyInt_FromLong(x);
2938 }
2939 /* XXX Huge floats may silently fail */
2940#ifndef WITHOUT_COMPLEX
2941 if (imflag) {
2942 c.real = 0.;
2943 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002944 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 PyFPE_END_PROTECT(c)
2946 return PyComplex_FromCComplex(c);
2947 }
2948 else
2949#endif
2950 {
2951 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002952 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 PyFPE_END_PROTECT(dx)
2954 return PyFloat_FromDouble(dx);
2955 }
2956}
2957
2958static PyObject *
2959decode_utf8(const char **sPtr, const char *end, char* encoding)
2960{
2961#ifndef Py_USING_UNICODE
2962 Py_FatalError("decode_utf8 should not be called in this build.");
2963 return NULL;
2964#else
2965 PyObject *u, *v;
2966 char *s, *t;
2967 t = s = (char *)*sPtr;
2968 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2969 while (s < end && (*s & 0x80)) s++;
2970 *sPtr = s;
2971 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2972 if (u == NULL)
2973 return NULL;
2974 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2975 Py_DECREF(u);
2976 return v;
2977#endif
2978}
2979
2980static PyObject *
2981decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2982{
2983 PyObject *v, *u;
2984 char *buf;
2985 char *p;
2986 const char *end;
2987 if (encoding == NULL) {
2988 buf = (char *)s;
2989 u = NULL;
2990 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2991 buf = (char *)s;
2992 u = NULL;
2993 } else {
2994 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2995 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2996 if (u == NULL)
2997 return NULL;
2998 p = buf = PyString_AsString(u);
2999 end = s + len;
3000 while (s < end) {
3001 if (*s == '\\') {
3002 *p++ = *s++;
3003 if (*s & 0x80) {
3004 strcpy(p, "u005c");
3005 p += 5;
3006 }
3007 }
3008 if (*s & 0x80) { /* XXX inefficient */
3009 PyObject *w;
3010 char *r;
3011 int rn, i;
3012 w = decode_utf8(&s, end, "utf-16-be");
3013 if (w == NULL) {
3014 Py_DECREF(u);
3015 return NULL;
3016 }
3017 r = PyString_AsString(w);
3018 rn = PyString_Size(w);
3019 assert(rn % 2 == 0);
3020 for (i = 0; i < rn; i += 2) {
3021 sprintf(p, "\\u%02x%02x",
3022 r[i + 0] & 0xFF,
3023 r[i + 1] & 0xFF);
3024 p += 6;
3025 }
3026 Py_DECREF(w);
3027 } else {
3028 *p++ = *s++;
3029 }
3030 }
3031 len = p - buf;
3032 s = buf;
3033 }
3034 if (rawmode)
3035 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3036 else
3037 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3038 Py_XDECREF(u);
3039 return v;
3040}
3041
3042/* s is a Python string literal, including the bracketing quote characters,
3043 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3044 * parsestr parses it, and returns the decoded Python string object.
3045 */
3046static PyObject *
3047parsestr(const char *s, const char *encoding)
3048{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003050 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 int rawmode = 0;
3052 int need_encoding;
3053 int unicode = 0;
3054
3055 if (isalpha(quote) || quote == '_') {
3056 if (quote == 'u' || quote == 'U') {
3057 quote = *++s;
3058 unicode = 1;
3059 }
3060 if (quote == 'r' || quote == 'R') {
3061 quote = *++s;
3062 rawmode = 1;
3063 }
3064 }
3065 if (quote != '\'' && quote != '\"') {
3066 PyErr_BadInternalCall();
3067 return NULL;
3068 }
3069 s++;
3070 len = strlen(s);
3071 if (len > INT_MAX) {
3072 PyErr_SetString(PyExc_OverflowError,
3073 "string to parse is too long");
3074 return NULL;
3075 }
3076 if (s[--len] != quote) {
3077 PyErr_BadInternalCall();
3078 return NULL;
3079 }
3080 if (len >= 4 && s[0] == quote && s[1] == quote) {
3081 s += 2;
3082 len -= 2;
3083 if (s[--len] != quote || s[--len] != quote) {
3084 PyErr_BadInternalCall();
3085 return NULL;
3086 }
3087 }
3088#ifdef Py_USING_UNICODE
3089 if (unicode || Py_UnicodeFlag) {
3090 return decode_unicode(s, len, rawmode, encoding);
3091 }
3092#endif
3093 need_encoding = (encoding != NULL &&
3094 strcmp(encoding, "utf-8") != 0 &&
3095 strcmp(encoding, "iso-8859-1") != 0);
3096 if (rawmode || strchr(s, '\\') == NULL) {
3097 if (need_encoding) {
3098#ifndef Py_USING_UNICODE
3099 /* This should not happen - we never see any other
3100 encoding. */
3101 Py_FatalError("cannot deal with encodings in this build.");
3102#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003103 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 if (u == NULL)
3105 return NULL;
3106 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3107 Py_DECREF(u);
3108 return v;
3109#endif
3110 } else {
3111 return PyString_FromStringAndSize(s, len);
3112 }
3113 }
3114
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003115 return PyString_DecodeEscape(s, len, NULL, unicode,
3116 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117}
3118
3119/* Build a Python string object out of a STRING atom. This takes care of
3120 * compile-time literal catenation, calling parsestr() on each piece, and
3121 * pasting the intermediate results together.
3122 */
3123static PyObject *
3124parsestrplus(struct compiling *c, const node *n)
3125{
3126 PyObject *v;
3127 int i;
3128 REQ(CHILD(n, 0), STRING);
3129 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3130 /* String literal concatenation */
3131 for (i = 1; i < NCH(n); i++) {
3132 PyObject *s;
3133 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3134 if (s == NULL)
3135 goto onError;
3136 if (PyString_Check(v) && PyString_Check(s)) {
3137 PyString_ConcatAndDel(&v, s);
3138 if (v == NULL)
3139 goto onError;
3140 }
3141#ifdef Py_USING_UNICODE
3142 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003143 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 Py_DECREF(v);
3146 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003147 if (v == NULL)
3148 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 }
3150#endif
3151 }
3152 }
3153 return v;
3154
3155 onError:
3156 Py_XDECREF(v);
3157 return NULL;
3158}