blob: ace4950402e944c4ed5cbaaedf425b0bc38921fa [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/* Data structure used internally */
19struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000020 char *c_encoding; /* source encoding */
21 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022};
23
24static asdl_seq *seq_for_testlist(struct compiling *, const node *);
25static expr_ty ast_for_expr(struct compiling *, const node *);
26static stmt_ty ast_for_stmt(struct compiling *, const node *);
27static asdl_seq *ast_for_suite(struct compiling *, const node *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000028static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
29 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000030static expr_ty ast_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032
33/* Note different signature for ast_for_call */
34static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
35
36static PyObject *parsenumber(const char *);
37static PyObject *parsestr(const char *s, const char *encoding);
38static PyObject *parsestrplus(struct compiling *, const node *n);
39
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000041#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042#endif
43
Neal Norwitzadb69fc2005-12-17 20:54:49 +000044static identifier
45new_identifier(const char* n, PyArena *arena) {
46 PyObject* id = PyString_InternFromString(n);
47 PyArena_AddPyObject(arena, id);
48 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000049}
50
Neal Norwitzadb69fc2005-12-17 20:54:49 +000051#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052
53/* This routine provides an invalid object for the syntax error.
54 The outermost routine must unpack this error and create the
55 proper object. We do this so that we don't have to pass
56 the filename to everything function.
57
58 XXX Maybe we should just pass the filename...
59*/
60
61static int
62ast_error(const node *n, const char *errstr)
63{
64 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
65 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000066 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000067 PyErr_SetObject(PyExc_SyntaxError, u);
68 Py_DECREF(u);
69 return 0;
70}
71
72static void
73ast_error_finish(const char *filename)
74{
75 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000076 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077
78 assert(PyErr_Occurred());
79 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000080 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
82 PyErr_Fetch(&type, &value, &tback);
83 errstr = PyTuple_GetItem(value, 0);
84 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000085 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086 Py_INCREF(errstr);
87 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000088 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000089 Py_DECREF(errstr);
90 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000091 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092 Py_DECREF(value);
93
94 loc = PyErr_ProgramText(filename, lineno);
95 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000096 Py_INCREF(Py_None);
97 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +000099 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000101 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000102 Py_DECREF(errstr);
103 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000104 }
Georg Brandl7784f122006-05-26 20:04:44 +0000105 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106 Py_DECREF(errstr);
107 Py_DECREF(tmp);
108 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000109 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 PyErr_Restore(type, value, tback);
111}
112
113/* num_stmts() returns number of contained statements.
114
115 Use this routine to determine how big a sequence is needed for
116 the statements in a parse tree. Its raison d'etre is this bit of
117 grammar:
118
119 stmt: simple_stmt | compound_stmt
120 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
121
122 A simple_stmt can contain multiple small_stmt elements joined
123 by semicolons. If the arg is a simple_stmt, the number of
124 small_stmt elements is returned.
125*/
126
127static int
128num_stmts(const node *n)
129{
130 int i, l;
131 node *ch;
132
133 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000134 case single_input:
135 if (TYPE(CHILD(n, 0)) == NEWLINE)
136 return 0;
137 else
138 return num_stmts(CHILD(n, 0));
139 case file_input:
140 l = 0;
141 for (i = 0; i < NCH(n); i++) {
142 ch = CHILD(n, i);
143 if (TYPE(ch) == stmt)
144 l += num_stmts(ch);
145 }
146 return l;
147 case stmt:
148 return num_stmts(CHILD(n, 0));
149 case compound_stmt:
150 return 1;
151 case simple_stmt:
152 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
153 case suite:
154 if (NCH(n) == 1)
155 return num_stmts(CHILD(n, 0));
156 else {
157 l = 0;
158 for (i = 2; i < (NCH(n) - 1); i++)
159 l += num_stmts(CHILD(n, i));
160 return l;
161 }
162 default: {
163 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000165 sprintf(buf, "Non-statement found: %d %d\n",
166 TYPE(n), NCH(n));
167 Py_FatalError(buf);
168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169 }
170 assert(0);
171 return 0;
172}
173
174/* Transform the CST rooted at node * to the appropriate AST
175*/
176
177mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000178PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000179 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000181 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182 asdl_seq *stmts = NULL;
183 stmt_ty s;
184 node *ch;
185 struct compiling c;
186
187 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000188 c.c_encoding = "utf-8";
189 if (TYPE(n) == encoding_decl) {
190 ast_error(n, "encoding declaration in Unicode string");
191 goto error;
192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000194 c.c_encoding = STR(n);
195 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000197 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000199 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
Jeremy Hyltona8293132006-02-28 17:58:27 +0000201 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000203 case file_input:
204 stmts = asdl_seq_new(num_stmts(n), arena);
205 if (!stmts)
206 return NULL;
207 for (i = 0; i < NCH(n) - 1; i++) {
208 ch = CHILD(n, i);
209 if (TYPE(ch) == NEWLINE)
210 continue;
211 REQ(ch, stmt);
212 num = num_stmts(ch);
213 if (num == 1) {
214 s = ast_for_stmt(&c, ch);
215 if (!s)
216 goto error;
217 asdl_seq_SET(stmts, k++, s);
218 }
219 else {
220 ch = CHILD(ch, 0);
221 REQ(ch, simple_stmt);
222 for (j = 0; j < num; j++) {
223 s = ast_for_stmt(&c, CHILD(ch, j * 2));
224 if (!s)
225 goto error;
226 asdl_seq_SET(stmts, k++, s);
227 }
228 }
229 }
230 return Module(stmts, arena);
231 case eval_input: {
232 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000234 /* XXX Why not gen_for here? */
235 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
236 if (!testlist_ast)
237 goto error;
238 return Expression(testlist_ast, arena);
239 }
240 case single_input:
241 if (TYPE(CHILD(n, 0)) == NEWLINE) {
242 stmts = asdl_seq_new(1, arena);
243 if (!stmts)
244 goto error;
245 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
246 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000247 if (!asdl_seq_GET(stmts, 0))
248 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000249 return Interactive(stmts, arena);
250 }
251 else {
252 n = CHILD(n, 0);
253 num = num_stmts(n);
254 stmts = asdl_seq_new(num, arena);
255 if (!stmts)
256 goto error;
257 if (num == 1) {
258 s = ast_for_stmt(&c, n);
259 if (!s)
260 goto error;
261 asdl_seq_SET(stmts, 0, s);
262 }
263 else {
264 /* Only a simple_stmt can contain multiple statements. */
265 REQ(n, simple_stmt);
266 for (i = 0; i < NCH(n); i += 2) {
267 if (TYPE(CHILD(n, i)) == NEWLINE)
268 break;
269 s = ast_for_stmt(&c, CHILD(n, i));
270 if (!s)
271 goto error;
272 asdl_seq_SET(stmts, i / 2, s);
273 }
274 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000276 return Interactive(stmts, arena);
277 }
278 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000279 PyErr_Format(PyExc_SystemError,
280 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000281 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282 }
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)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000295 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 (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319 }
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
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000341 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000342 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) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000348 case Attribute_kind:
349 if (ctx == Store &&
350 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
351 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;
375 case Lambda_kind:
376 expr_name = "lambda";
377 break;
378 case Call_kind:
379 expr_name = "function call";
380 break;
381 case BoolOp_kind:
382 case BinOp_kind:
383 case UnaryOp_kind:
384 expr_name = "operator";
385 break;
386 case GeneratorExp_kind:
387 expr_name = "generator expression";
388 break;
389 case Yield_kind:
390 expr_name = "yield expression";
391 break;
392 case ListComp_kind:
393 expr_name = "list comprehension";
394 break;
395 case Dict_kind:
396 case Num_kind:
397 case Str_kind:
398 expr_name = "literal";
399 break;
400 case Compare_kind:
401 expr_name = "comparison";
402 break;
403 case Repr_kind:
404 expr_name = "repr";
405 break;
406 case IfExp_kind:
407 expr_name = "conditional expression";
408 break;
409 default:
410 PyErr_Format(PyExc_SystemError,
411 "unexpected expression in assignment %d (line %d)",
412 e->kind, e->lineno);
413 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000415 /* Check for error string set by switch */
416 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000417 char buf[300];
418 PyOS_snprintf(buf, sizeof(buf),
419 "can't %s %s",
420 ctx == Store ? "assign to" : "delete",
421 expr_name);
422 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000423 }
424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000426 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427 */
428 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000429 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000431 for (i = 0; i < asdl_seq_LEN(s); i++) {
432 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
433 return 0;
434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 }
436 return 1;
437}
438
439static operator_ty
440ast_for_augassign(const node *n)
441{
442 REQ(n, augassign);
443 n = CHILD(n, 0);
444 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000445 case '+':
446 return Add;
447 case '-':
448 return Sub;
449 case '/':
450 if (STR(n)[1] == '/')
451 return FloorDiv;
452 else
453 return Div;
454 case '%':
455 return Mod;
456 case '<':
457 return LShift;
458 case '>':
459 return RShift;
460 case '&':
461 return BitAnd;
462 case '^':
463 return BitXor;
464 case '|':
465 return BitOr;
466 case '*':
467 if (STR(n)[1] == '*')
468 return Pow;
469 else
470 return Mult;
471 default:
472 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
473 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474 }
475}
476
477static cmpop_ty
478ast_for_comp_op(const node *n)
479{
480 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000481 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 */
483 REQ(n, comp_op);
484 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000485 n = CHILD(n, 0);
486 switch (TYPE(n)) {
487 case LESS:
488 return Lt;
489 case GREATER:
490 return Gt;
491 case EQEQUAL: /* == */
492 return Eq;
493 case LESSEQUAL:
494 return LtE;
495 case GREATEREQUAL:
496 return GtE;
497 case NOTEQUAL:
498 return NotEq;
499 case NAME:
500 if (strcmp(STR(n), "in") == 0)
501 return In;
502 if (strcmp(STR(n), "is") == 0)
503 return Is;
504 default:
505 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
506 STR(n));
507 return (cmpop_ty)0;
508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 }
510 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000511 /* handle "not in" and "is not" */
512 switch (TYPE(CHILD(n, 0))) {
513 case NAME:
514 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
515 return NotIn;
516 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
517 return IsNot;
518 default:
519 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
520 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
521 return (cmpop_ty)0;
522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 }
Neal Norwitz79792652005-11-14 04:25:03 +0000524 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000525 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000526 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527}
528
529static asdl_seq *
530seq_for_testlist(struct compiling *c, const node *n)
531{
532 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000533 asdl_seq *seq;
534 expr_ty expression;
535 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000536 assert(TYPE(n) == testlist ||
537 TYPE(n) == listmaker ||
538 TYPE(n) == testlist_gexp ||
539 TYPE(n) == testlist_safe ||
540 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000542 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000544 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
546 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000547 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000549 expression = ast_for_expr(c, CHILD(n, i));
550 if (!expression)
551 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000553 assert(i / 2 < seq->size);
554 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 }
556 return seq;
557}
558
559static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000560compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561{
562 int i, len = (NCH(n) + 1) / 2;
563 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000564 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000566 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567
Neal Norwitz3a230172006-09-22 08:18:10 +0000568 /* fpdef: NAME | '(' fplist ')'
569 fplist: fpdef (',' fpdef)* [',']
570 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000573 const node *fpdef_node = CHILD(n, 2*i);
574 const node *child;
575 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000576set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000577 /* fpdef_node is either a NAME or an fplist */
578 child = CHILD(fpdef_node, 0);
579 if (TYPE(child) == NAME) {
580 if (!strcmp(STR(child), "None")) {
581 ast_error(child, "assignment to None");
582 return NULL;
583 }
584 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
585 child->n_col_offset, c->c_arena);
586 }
587 else {
588 assert(TYPE(fpdef_node) == fpdef);
589 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
590 child = CHILD(fpdef_node, 1);
591 assert(TYPE(child) == fplist);
592 /* NCH == 1 means we have (x), we need to elide the extra parens */
593 if (NCH(child) == 1) {
594 fpdef_node = CHILD(child, 0);
595 assert(TYPE(fpdef_node) == fpdef);
596 goto set_name;
597 }
598 arg = compiler_complex_args(c, child);
599 }
600 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 }
602
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000603 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000604 if (!set_context(result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 return result;
607}
608
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609
Jeremy Hyltona8293132006-02-28 17:58:27 +0000610/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
612static arguments_ty
613ast_for_arguments(struct compiling *c, const node *n)
614{
615 /* parameters: '(' [varargslist] ')'
616 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000617 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000619 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 asdl_seq *args, *defaults;
621 identifier vararg = NULL, kwarg = NULL;
622 node *ch;
623
624 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000625 if (NCH(n) == 2) /* () as argument list */
626 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
627 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628 }
629 REQ(n, varargslist);
630
631 /* first count the number of normal args & defaults */
632 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000633 ch = CHILD(n, i);
634 if (TYPE(ch) == fpdef)
635 n_args++;
636 if (TYPE(ch) == EQUAL)
637 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000639 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000641 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000642 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000644 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645
646 /* fpdef: NAME | '(' fplist ')'
647 fplist: fpdef (',' fpdef)* [',']
648 */
649 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000650 j = 0; /* index for defaults */
651 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000653 ch = CHILD(n, i);
654 switch (TYPE(ch)) {
655 case fpdef:
656 handle_fpdef:
657 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
658 anything other than EQUAL or a comma? */
659 /* XXX Should NCH(n) check be made a separate check? */
660 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
661 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
662 if (!expression)
663 goto error;
664 assert(defaults != NULL);
665 asdl_seq_SET(defaults, j++, expression);
666 i += 2;
667 found_default = 1;
668 }
669 else if (found_default) {
670 ast_error(n,
671 "non-default argument follows default argument");
672 goto error;
673 }
674 if (NCH(ch) == 3) {
675 ch = CHILD(ch, 1);
676 /* def foo((x)): is not complex, special case. */
677 if (NCH(ch) != 1) {
678 /* We have complex arguments, setup for unpacking. */
679 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000680 if (!asdl_seq_GET(args, k-1))
681 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000682 } else {
683 /* def foo((x)): setup for checking NAME below. */
684 /* Loop because there can be many parens and tuple
685 unpacking mixed in. */
686 ch = CHILD(ch, 0);
687 assert(TYPE(ch) == fpdef);
688 goto handle_fpdef;
689 }
690 }
691 if (TYPE(CHILD(ch, 0)) == NAME) {
692 expr_ty name;
693 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
694 ast_error(CHILD(ch, 0), "assignment to None");
695 goto error;
696 }
697 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
698 Param, LINENO(ch), ch->n_col_offset,
699 c->c_arena);
700 if (!name)
701 goto error;
702 asdl_seq_SET(args, k++, name);
703
704 }
705 i += 2; /* the name and the comma */
706 break;
707 case STAR:
708 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
709 ast_error(CHILD(n, i+1), "assignment to None");
710 goto error;
711 }
712 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
713 i += 3;
714 break;
715 case DOUBLESTAR:
716 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
717 ast_error(CHILD(n, i+1), "assignment to None");
718 goto error;
719 }
720 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
721 i += 3;
722 break;
723 default:
724 PyErr_Format(PyExc_SystemError,
725 "unexpected node in varargslist: %d @ %d",
726 TYPE(ch), i);
727 goto error;
728 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 }
730
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000731 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732
733 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000734 Py_XDECREF(vararg);
735 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736 return NULL;
737}
738
739static expr_ty
740ast_for_dotted_name(struct compiling *c, const node *n)
741{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000742 expr_ty e;
743 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000744 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 int i;
746
747 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000748
749 lineno = LINENO(n);
750 col_offset = n->n_col_offset;
751
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 id = NEW_IDENTIFIER(CHILD(n, 0));
753 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000754 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000755 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000757 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
759 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000760 id = NEW_IDENTIFIER(CHILD(n, i));
761 if (!id)
762 return NULL;
763 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
764 if (!e)
765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 }
767
768 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769}
770
771static expr_ty
772ast_for_decorator(struct compiling *c, const node *n)
773{
774 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
775 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000776 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
778 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000779 REQ(CHILD(n, 0), AT);
780 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
782 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
783 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000784 return NULL;
785
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000787 d = name_expr;
788 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 }
790 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000791 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
792 n->n_col_offset, c->c_arena);
793 if (!d)
794 return NULL;
795 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 }
797 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000798 d = ast_for_call(c, CHILD(n, 3), name_expr);
799 if (!d)
800 return NULL;
801 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 }
803
804 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805}
806
807static asdl_seq*
808ast_for_decorators(struct compiling *c, const node *n)
809{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000810 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000811 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 int i;
813
814 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000815 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000817 return NULL;
818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000820 d = ast_for_decorator(c, CHILD(n, i));
821 if (!d)
822 return NULL;
823 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 }
825 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826}
827
828static stmt_ty
829ast_for_funcdef(struct compiling *c, const node *n)
830{
831 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000832 identifier name;
833 arguments_ty args;
834 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 asdl_seq *decorator_seq = NULL;
836 int name_i;
837
838 REQ(n, funcdef);
839
840 if (NCH(n) == 6) { /* decorators are present */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000841 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
842 if (!decorator_seq)
843 return NULL;
844 name_i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 }
846 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000847 name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 }
849
850 name = NEW_IDENTIFIER(CHILD(n, name_i));
851 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000854 ast_error(CHILD(n, name_i), "assignment to None");
855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 }
857 args = ast_for_arguments(c, CHILD(n, name_i + 1));
858 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 body = ast_for_suite(c, CHILD(n, name_i + 3));
861 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000864 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000865 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866}
867
868static expr_ty
869ast_for_lambdef(struct compiling *c, const node *n)
870{
871 /* lambdef: 'lambda' [varargslist] ':' test */
872 arguments_ty args;
873 expr_ty expression;
874
875 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000876 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
877 if (!args)
878 return NULL;
879 expression = ast_for_expr(c, CHILD(n, 2));
880 if (!expression)
881 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 }
883 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000884 args = ast_for_arguments(c, CHILD(n, 1));
885 if (!args)
886 return NULL;
887 expression = ast_for_expr(c, CHILD(n, 3));
888 if (!expression)
889 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890 }
891
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000892 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893}
894
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000895static expr_ty
896ast_for_ifexpr(struct compiling *c, const node *n)
897{
898 /* test: or_test 'if' or_test 'else' test */
899 expr_ty expression, body, orelse;
900
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000901 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000902 body = ast_for_expr(c, CHILD(n, 0));
903 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000904 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000905 expression = ast_for_expr(c, CHILD(n, 2));
906 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000907 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000908 orelse = ast_for_expr(c, CHILD(n, 4));
909 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000910 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000911 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000912 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000913}
914
Neal Norwitze4d4f002006-09-05 03:58:26 +0000915/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
916 so there is only a single version. Possibly for loops can also re-use
917 the code.
918*/
919
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920/* Count the number of 'for' loop in a list comprehension.
921
922 Helper for ast_for_listcomp().
923*/
924
925static int
926count_list_fors(const node *n)
927{
928 int n_fors = 0;
929 node *ch = CHILD(n, 1);
930
931 count_list_for:
932 n_fors++;
933 REQ(ch, list_for);
934 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000935 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000937 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938 count_list_iter:
939 REQ(ch, list_iter);
940 ch = CHILD(ch, 0);
941 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000942 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000944 if (NCH(ch) == 3) {
945 ch = CHILD(ch, 2);
946 goto count_list_iter;
947 }
948 else
949 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000951
952 /* Should never be reached */
953 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
954 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955}
956
957/* Count the number of 'if' statements in a list comprehension.
958
959 Helper for ast_for_listcomp().
960*/
961
962static int
963count_list_ifs(const node *n)
964{
965 int n_ifs = 0;
966
967 count_list_iter:
968 REQ(n, list_iter);
969 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000970 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 n = CHILD(n, 0);
972 REQ(n, list_if);
973 n_ifs++;
974 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000975 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 n = CHILD(n, 2);
977 goto count_list_iter;
978}
979
980static expr_ty
981ast_for_listcomp(struct compiling *c, const node *n)
982{
983 /* listmaker: test ( list_for | (',' test)* [','] )
984 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
985 list_iter: list_for | list_if
986 list_if: 'if' test [list_iter]
987 testlist_safe: test [(',' test)+ [',']]
988 */
989 expr_ty elt;
990 asdl_seq *listcomps;
991 int i, n_fors;
992 node *ch;
993
994 REQ(n, listmaker);
995 assert(NCH(n) > 1);
996
997 elt = ast_for_expr(c, CHILD(n, 0));
998 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000999 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000
1001 n_fors = count_list_fors(n);
1002 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001005 listcomps = asdl_seq_new(n_fors, c->c_arena);
1006 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001007 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001008
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 ch = CHILD(n, 1);
1010 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001011 comprehension_ty lc;
1012 asdl_seq *t;
1013 expr_ty expression;
1014 node *for_ch;
1015
1016 REQ(ch, list_for);
1017
1018 for_ch = CHILD(ch, 1);
1019 t = ast_for_exprlist(c, for_ch, Store);
1020 if (!t)
1021 return NULL;
1022 expression = ast_for_testlist(c, CHILD(ch, 3));
1023 if (!expression)
1024 return NULL;
1025
1026 /* Check the # of children rather than the length of t, since
1027 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1028 */
1029 if (NCH(for_ch) == 1)
1030 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1031 c->c_arena);
1032 else
1033 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1034 c->c_arena),
1035 expression, NULL, c->c_arena);
1036 if (!lc)
1037 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001039 if (NCH(ch) == 5) {
1040 int j, n_ifs;
1041 asdl_seq *ifs;
1042 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001044 ch = CHILD(ch, 4);
1045 n_ifs = count_list_ifs(ch);
1046 if (n_ifs == -1)
1047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001049 ifs = asdl_seq_new(n_ifs, c->c_arena);
1050 if (!ifs)
1051 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001053 for (j = 0; j < n_ifs; j++) {
1054 REQ(ch, list_iter);
1055 ch = CHILD(ch, 0);
1056 REQ(ch, list_if);
1057
1058 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1059 if (!list_for_expr)
1060 return NULL;
1061
1062 asdl_seq_SET(ifs, j, list_for_expr);
1063 if (NCH(ch) == 3)
1064 ch = CHILD(ch, 2);
1065 }
1066 /* on exit, must guarantee that ch is a list_for */
1067 if (TYPE(ch) == list_iter)
1068 ch = CHILD(ch, 0);
1069 lc->ifs = ifs;
1070 }
1071 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 }
1073
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001074 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075}
1076
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001077/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078
1079 Helper for ast_for_genexp().
1080*/
1081
1082static int
1083count_gen_fors(const node *n)
1084{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001085 int n_fors = 0;
1086 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087
1088 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001089 n_fors++;
1090 REQ(ch, gen_for);
1091 if (NCH(ch) == 5)
1092 ch = CHILD(ch, 4);
1093 else
1094 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001096 REQ(ch, gen_iter);
1097 ch = CHILD(ch, 0);
1098 if (TYPE(ch) == gen_for)
1099 goto count_gen_for;
1100 else if (TYPE(ch) == gen_if) {
1101 if (NCH(ch) == 3) {
1102 ch = CHILD(ch, 2);
1103 goto count_gen_iter;
1104 }
1105 else
1106 return n_fors;
1107 }
1108
1109 /* Should never be reached */
1110 PyErr_SetString(PyExc_SystemError,
1111 "logic error in count_gen_fors");
1112 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113}
1114
1115/* Count the number of 'if' statements in a generator expression.
1116
1117 Helper for ast_for_genexp().
1118*/
1119
1120static int
1121count_gen_ifs(const node *n)
1122{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001123 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001125 while (1) {
1126 REQ(n, gen_iter);
1127 if (TYPE(CHILD(n, 0)) == gen_for)
1128 return n_ifs;
1129 n = CHILD(n, 0);
1130 REQ(n, gen_if);
1131 n_ifs++;
1132 if (NCH(n) == 2)
1133 return n_ifs;
1134 n = CHILD(n, 2);
1135 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136}
1137
Jeremy Hyltona8293132006-02-28 17:58:27 +00001138/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139static expr_ty
1140ast_for_genexp(struct compiling *c, const node *n)
1141{
1142 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001143 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 expr_ty elt;
1145 asdl_seq *genexps;
1146 int i, n_fors;
1147 node *ch;
1148
1149 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1150 assert(NCH(n) > 1);
1151
1152 elt = ast_for_expr(c, CHILD(n, 0));
1153 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001154 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155
1156 n_fors = count_gen_fors(n);
1157 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001158 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159
1160 genexps = asdl_seq_new(n_fors, c->c_arena);
1161 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001162 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 ch = CHILD(n, 1);
1165 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001166 comprehension_ty ge;
1167 asdl_seq *t;
1168 expr_ty expression;
1169 node *for_ch;
1170
1171 REQ(ch, gen_for);
1172
1173 for_ch = CHILD(ch, 1);
1174 t = ast_for_exprlist(c, for_ch, Store);
1175 if (!t)
1176 return NULL;
1177 expression = ast_for_expr(c, CHILD(ch, 3));
1178 if (!expression)
1179 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001180
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001181 /* Check the # of children rather than the length of t, since
1182 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1183 if (NCH(for_ch) == 1)
1184 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1185 NULL, c->c_arena);
1186 else
1187 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1188 c->c_arena),
1189 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001190
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001191 if (!ge)
1192 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001193
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001194 if (NCH(ch) == 5) {
1195 int j, n_ifs;
1196 asdl_seq *ifs;
1197
1198 ch = CHILD(ch, 4);
1199 n_ifs = count_gen_ifs(ch);
1200 if (n_ifs == -1)
1201 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001202
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001203 ifs = asdl_seq_new(n_ifs, c->c_arena);
1204 if (!ifs)
1205 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001206
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001207 for (j = 0; j < n_ifs; j++) {
1208 REQ(ch, gen_iter);
1209 ch = CHILD(ch, 0);
1210 REQ(ch, gen_if);
1211
1212 expression = ast_for_expr(c, CHILD(ch, 1));
1213 if (!expression)
1214 return NULL;
1215 asdl_seq_SET(ifs, j, expression);
1216 if (NCH(ch) == 3)
1217 ch = CHILD(ch, 2);
1218 }
1219 /* on exit, must guarantee that ch is a gen_for */
1220 if (TYPE(ch) == gen_iter)
1221 ch = CHILD(ch, 0);
1222 ge->ifs = ifs;
1223 }
1224 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 }
1226
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001227 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228}
1229
1230static expr_ty
1231ast_for_atom(struct compiling *c, const node *n)
1232{
1233 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1234 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1235 */
1236 node *ch = CHILD(n, 0);
1237
1238 switch (TYPE(ch)) {
1239 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001240 /* All names start in Load context, but may later be
1241 changed. */
1242 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1243 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001245 PyObject *str = parsestrplus(c, n);
1246 if (!str)
1247 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001248
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001249 PyArena_AddPyObject(c->c_arena, str);
1250 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 }
1252 case NUMBER: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001253 PyObject *pynum = parsenumber(STR(ch));
1254 if (!pynum)
1255 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001256
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001257 PyArena_AddPyObject(c->c_arena, pynum);
1258 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 }
1260 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001261 ch = CHILD(n, 1);
1262
1263 if (TYPE(ch) == RPAR)
1264 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1265
1266 if (TYPE(ch) == yield_expr)
1267 return ast_for_expr(c, ch);
1268
1269 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1270 return ast_for_genexp(c, ch);
1271
1272 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001274 ch = CHILD(n, 1);
1275
1276 if (TYPE(ch) == RSQB)
1277 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1278
1279 REQ(ch, listmaker);
1280 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1281 asdl_seq *elts = seq_for_testlist(c, ch);
1282 if (!elts)
1283 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001284
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001285 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1286 }
1287 else
1288 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001290 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1291 int i, size;
1292 asdl_seq *keys, *values;
1293
1294 ch = CHILD(n, 1);
1295 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1296 keys = asdl_seq_new(size, c->c_arena);
1297 if (!keys)
1298 return NULL;
1299
1300 values = asdl_seq_new(size, c->c_arena);
1301 if (!values)
1302 return NULL;
1303
1304 for (i = 0; i < NCH(ch); i += 4) {
1305 expr_ty expression;
1306
1307 expression = ast_for_expr(c, CHILD(ch, i));
1308 if (!expression)
1309 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001310
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001311 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001312
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001313 expression = ast_for_expr(c, CHILD(ch, i + 2));
1314 if (!expression)
1315 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001316
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001317 asdl_seq_SET(values, i / 4, expression);
1318 }
1319 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 }
1321 case BACKQUOTE: { /* repr */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001322 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
1323 if (!expression)
1324 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001325
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001326 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 }
1328 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001329 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1330 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 }
1332}
1333
1334static slice_ty
1335ast_for_slice(struct compiling *c, const node *n)
1336{
1337 node *ch;
1338 expr_ty lower = NULL, upper = NULL, step = NULL;
1339
1340 REQ(n, subscript);
1341
1342 /*
1343 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1344 sliceop: ':' [test]
1345 */
1346 ch = CHILD(n, 0);
1347 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001348 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349
1350 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001351 /* 'step' variable hold no significance in terms of being used over
1352 other vars */
1353 step = ast_for_expr(c, ch);
1354 if (!step)
1355 return NULL;
1356
1357 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 }
1359
1360 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001361 lower = ast_for_expr(c, ch);
1362 if (!lower)
1363 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 }
1365
1366 /* If there's an upper bound it's in the second or third position. */
1367 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001368 if (NCH(n) > 1) {
1369 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001371 if (TYPE(n2) == test) {
1372 upper = ast_for_expr(c, n2);
1373 if (!upper)
1374 return NULL;
1375 }
1376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001378 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001380 if (TYPE(n2) == test) {
1381 upper = ast_for_expr(c, n2);
1382 if (!upper)
1383 return NULL;
1384 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 }
1386
1387 ch = CHILD(n, NCH(n) - 1);
1388 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001389 if (NCH(ch) == 1) {
1390 /* No expression, so step is None */
1391 ch = CHILD(ch, 0);
1392 step = Name(new_identifier("None", c->c_arena), Load,
1393 LINENO(ch), ch->n_col_offset, c->c_arena);
1394 if (!step)
1395 return NULL;
1396 } else {
1397 ch = CHILD(ch, 1);
1398 if (TYPE(ch) == test) {
1399 step = ast_for_expr(c, ch);
1400 if (!step)
1401 return NULL;
1402 }
1403 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 }
1405
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001406 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407}
1408
1409static expr_ty
1410ast_for_binop(struct compiling *c, const node *n)
1411{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001412 /* Must account for a sequence of expressions.
1413 How should A op B op C by represented?
1414 BinOp(BinOp(A, op, B), op, C).
1415 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001417 int i, nops;
1418 expr_ty expr1, expr2, result;
1419 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001421 expr1 = ast_for_expr(c, CHILD(n, 0));
1422 if (!expr1)
1423 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001425 expr2 = ast_for_expr(c, CHILD(n, 2));
1426 if (!expr2)
1427 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001429 newoperator = get_operator(CHILD(n, 1));
1430 if (!newoperator)
1431 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001433 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1434 c->c_arena);
1435 if (!result)
1436 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001438 nops = (NCH(n) - 1) / 2;
1439 for (i = 1; i < nops; i++) {
1440 expr_ty tmp_result, tmp;
1441 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001443 newoperator = get_operator(next_oper);
1444 if (!newoperator)
1445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001447 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1448 if (!tmp)
1449 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001451 tmp_result = BinOp(result, newoperator, tmp,
1452 LINENO(next_oper), next_oper->n_col_offset,
1453 c->c_arena);
1454 if (!tmp)
1455 return NULL;
1456 result = tmp_result;
1457 }
1458 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459}
1460
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001461static expr_ty
1462ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1463{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001464 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1465 subscriptlist: subscript (',' subscript)* [',']
1466 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1467 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001468 REQ(n, trailer);
1469 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001470 if (NCH(n) == 2)
1471 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1472 n->n_col_offset, c->c_arena);
1473 else
1474 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001475 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001476 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001477 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1478 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001479 }
1480 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001481 REQ(CHILD(n, 0), LSQB);
1482 REQ(CHILD(n, 2), RSQB);
1483 n = CHILD(n, 1);
1484 if (NCH(n) == 1) {
1485 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1486 if (!slc)
1487 return NULL;
1488 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1489 c->c_arena);
1490 }
1491 else {
1492 /* The grammar is ambiguous here. The ambiguity is resolved
1493 by treating the sequence as a tuple literal if there are
1494 no slice features.
1495 */
1496 int j;
1497 slice_ty slc;
1498 expr_ty e;
1499 bool simple = true;
1500 asdl_seq *slices, *elts;
1501 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1502 if (!slices)
1503 return NULL;
1504 for (j = 0; j < NCH(n); j += 2) {
1505 slc = ast_for_slice(c, CHILD(n, j));
1506 if (!slc)
1507 return NULL;
1508 if (slc->kind != Index_kind)
1509 simple = false;
1510 asdl_seq_SET(slices, j / 2, slc);
1511 }
1512 if (!simple) {
1513 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1514 Load, LINENO(n), n->n_col_offset, c->c_arena);
1515 }
1516 /* extract Index values and put them in a Tuple */
1517 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1518 if (!elts)
1519 return NULL;
1520 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1521 slc = (slice_ty)asdl_seq_GET(slices, j);
1522 assert(slc->kind == Index_kind && slc->v.Index.value);
1523 asdl_seq_SET(elts, j, slc->v.Index.value);
1524 }
1525 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1526 if (!e)
1527 return NULL;
1528 return Subscript(left_expr, Index(e, c->c_arena),
1529 Load, LINENO(n), n->n_col_offset, c->c_arena);
1530 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001531 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001532}
1533
1534static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001535ast_for_factor(struct compiling *c, const node *n)
1536{
1537 node *pfactor, *ppower, *patom, *pnum;
1538 expr_ty expression;
1539
1540 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001541 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001542 constant. The peephole optimizer already does something like
1543 this but it doesn't handle the case where the constant is
1544 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1545 PyLongObject.
1546 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001547 if (TYPE(CHILD(n, 0)) == MINUS &&
1548 NCH(n) == 2 &&
1549 TYPE((pfactor = CHILD(n, 1))) == factor &&
1550 NCH(pfactor) == 1 &&
1551 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1552 NCH(ppower) == 1 &&
1553 TYPE((patom = CHILD(ppower, 0))) == atom &&
1554 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1555 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1556 if (s == NULL)
1557 return NULL;
1558 s[0] = '-';
1559 strcpy(s + 1, STR(pnum));
1560 PyObject_FREE(STR(pnum));
1561 STR(pnum) = s;
1562 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001563 }
1564
1565 expression = ast_for_expr(c, CHILD(n, 1));
1566 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001567 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001568
1569 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001570 case PLUS:
1571 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1572 c->c_arena);
1573 case MINUS:
1574 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1575 c->c_arena);
1576 case TILDE:
1577 return UnaryOp(Invert, expression, LINENO(n),
1578 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001579 }
1580 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001581 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001582 return NULL;
1583}
1584
1585static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001586ast_for_power(struct compiling *c, const node *n)
1587{
1588 /* power: atom trailer* ('**' factor)*
1589 */
1590 int i;
1591 expr_ty e, tmp;
1592 REQ(n, power);
1593 e = ast_for_atom(c, CHILD(n, 0));
1594 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001595 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001596 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001597 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001598 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001599 node *ch = CHILD(n, i);
1600 if (TYPE(ch) != trailer)
1601 break;
1602 tmp = ast_for_trailer(c, ch, e);
1603 if (!tmp)
1604 return NULL;
1605 tmp->lineno = e->lineno;
1606 tmp->col_offset = e->col_offset;
1607 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001608 }
1609 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001610 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1611 if (!f)
1612 return NULL;
1613 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1614 if (!tmp)
1615 return NULL;
1616 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001617 }
1618 return e;
1619}
1620
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621/* Do not name a variable 'expr'! Will cause a compile error.
1622*/
1623
1624static expr_ty
1625ast_for_expr(struct compiling *c, const node *n)
1626{
1627 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001628 test: or_test ['if' or_test 'else' test] | lambdef
1629 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 and_test: not_test ('and' not_test)*
1631 not_test: 'not' not_test | comparison
1632 comparison: expr (comp_op expr)*
1633 expr: xor_expr ('|' xor_expr)*
1634 xor_expr: and_expr ('^' and_expr)*
1635 and_expr: shift_expr ('&' shift_expr)*
1636 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1637 arith_expr: term (('+'|'-') term)*
1638 term: factor (('*'|'/'|'%'|'//') factor)*
1639 factor: ('+'|'-'|'~') factor | power
1640 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001641
1642 As well as modified versions that exist for backward compatibility,
1643 to explicitly allow:
1644 [ x for x in lambda: 0, lambda: 1 ]
1645 (which would be ambiguous without these extra rules)
1646
1647 old_test: or_test | old_lambdef
1648 old_lambdef: 'lambda' [vararglist] ':' old_test
1649
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 */
1651
1652 asdl_seq *seq;
1653 int i;
1654
1655 loop:
1656 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001657 case test:
1658 case old_test:
1659 if (TYPE(CHILD(n, 0)) == lambdef ||
1660 TYPE(CHILD(n, 0)) == old_lambdef)
1661 return ast_for_lambdef(c, CHILD(n, 0));
1662 else if (NCH(n) > 1)
1663 return ast_for_ifexpr(c, n);
1664 /* Fallthrough */
1665 case or_test:
1666 case and_test:
1667 if (NCH(n) == 1) {
1668 n = CHILD(n, 0);
1669 goto loop;
1670 }
1671 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1672 if (!seq)
1673 return NULL;
1674 for (i = 0; i < NCH(n); i += 2) {
1675 expr_ty e = ast_for_expr(c, CHILD(n, i));
1676 if (!e)
1677 return NULL;
1678 asdl_seq_SET(seq, i / 2, e);
1679 }
1680 if (!strcmp(STR(CHILD(n, 1)), "and"))
1681 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1682 c->c_arena);
1683 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1684 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1685 case not_test:
1686 if (NCH(n) == 1) {
1687 n = CHILD(n, 0);
1688 goto loop;
1689 }
1690 else {
1691 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1692 if (!expression)
1693 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001695 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1696 c->c_arena);
1697 }
1698 case comparison:
1699 if (NCH(n) == 1) {
1700 n = CHILD(n, 0);
1701 goto loop;
1702 }
1703 else {
1704 expr_ty expression;
1705 asdl_int_seq *ops;
1706 asdl_seq *cmps;
1707 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1708 if (!ops)
1709 return NULL;
1710 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1711 if (!cmps) {
1712 return NULL;
1713 }
1714 for (i = 1; i < NCH(n); i += 2) {
1715 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001717 newoperator = ast_for_comp_op(CHILD(n, i));
1718 if (!newoperator) {
1719 return NULL;
1720 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001722 expression = ast_for_expr(c, CHILD(n, i + 1));
1723 if (!expression) {
1724 return NULL;
1725 }
1726
1727 asdl_seq_SET(ops, i / 2, newoperator);
1728 asdl_seq_SET(cmps, i / 2, expression);
1729 }
1730 expression = ast_for_expr(c, CHILD(n, 0));
1731 if (!expression) {
1732 return NULL;
1733 }
1734
1735 return Compare(expression, ops, cmps, LINENO(n),
1736 n->n_col_offset, c->c_arena);
1737 }
1738 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001740 /* The next five cases all handle BinOps. The main body of code
1741 is the same in each case, but the switch turned inside out to
1742 reuse the code for each type of operator.
1743 */
1744 case expr:
1745 case xor_expr:
1746 case and_expr:
1747 case shift_expr:
1748 case arith_expr:
1749 case term:
1750 if (NCH(n) == 1) {
1751 n = CHILD(n, 0);
1752 goto loop;
1753 }
1754 return ast_for_binop(c, n);
1755 case yield_expr: {
1756 expr_ty exp = NULL;
1757 if (NCH(n) == 2) {
1758 exp = ast_for_testlist(c, CHILD(n, 1));
1759 if (!exp)
1760 return NULL;
1761 }
1762 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1763 }
1764 case factor:
1765 if (NCH(n) == 1) {
1766 n = CHILD(n, 0);
1767 goto loop;
1768 }
1769 return ast_for_factor(c, n);
1770 case power:
1771 return ast_for_power(c, n);
1772 default:
1773 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1774 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001776 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 return NULL;
1778}
1779
1780static expr_ty
1781ast_for_call(struct compiling *c, const node *n, expr_ty func)
1782{
1783 /*
1784 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001785 | '**' test)
1786 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787 */
1788
1789 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001790 asdl_seq *args;
1791 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 expr_ty vararg = NULL, kwarg = NULL;
1793
1794 REQ(n, arglist);
1795
1796 nargs = 0;
1797 nkeywords = 0;
1798 ngens = 0;
1799 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001800 node *ch = CHILD(n, i);
1801 if (TYPE(ch) == argument) {
1802 if (NCH(ch) == 1)
1803 nargs++;
1804 else if (TYPE(CHILD(ch, 1)) == gen_for)
1805 ngens++;
1806 else
1807 nkeywords++;
1808 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 }
1810 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001811 ast_error(n, "Generator expression must be parenthesized "
1812 "if not sole argument");
1813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 }
1815
1816 if (nargs + nkeywords + ngens > 255) {
1817 ast_error(n, "more than 255 arguments");
1818 return NULL;
1819 }
1820
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001821 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001823 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001824 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 nargs = 0;
1828 nkeywords = 0;
1829 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001830 node *ch = CHILD(n, i);
1831 if (TYPE(ch) == argument) {
1832 expr_ty e;
1833 if (NCH(ch) == 1) {
1834 if (nkeywords) {
1835 ast_error(CHILD(ch, 0),
1836 "non-keyword arg after keyword arg");
1837 return NULL;
1838 }
1839 e = ast_for_expr(c, CHILD(ch, 0));
1840 if (!e)
1841 return NULL;
1842 asdl_seq_SET(args, nargs++, e);
1843 }
1844 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1845 e = ast_for_genexp(c, ch);
1846 if (!e)
1847 return NULL;
1848 asdl_seq_SET(args, nargs++, e);
1849 }
1850 else {
1851 keyword_ty kw;
1852 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001854 /* CHILD(ch, 0) is test, but must be an identifier? */
1855 e = ast_for_expr(c, CHILD(ch, 0));
1856 if (!e)
1857 return NULL;
1858 /* f(lambda x: x[0] = 3) ends up getting parsed with
1859 * LHS test = lambda x: x[0], and RHS test = 3.
1860 * SF bug 132313 points out that complaining about a keyword
1861 * then is very confusing.
1862 */
1863 if (e->kind == Lambda_kind) {
1864 ast_error(CHILD(ch, 0),
1865 "lambda cannot contain assignment");
1866 return NULL;
1867 } else if (e->kind != Name_kind) {
1868 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1869 return NULL;
1870 }
1871 key = e->v.Name.id;
1872 e = ast_for_expr(c, CHILD(ch, 2));
1873 if (!e)
1874 return NULL;
1875 kw = keyword(key, e, c->c_arena);
1876 if (!kw)
1877 return NULL;
1878 asdl_seq_SET(keywords, nkeywords++, kw);
1879 }
1880 }
1881 else if (TYPE(ch) == STAR) {
1882 vararg = ast_for_expr(c, CHILD(n, i+1));
1883 i++;
1884 }
1885 else if (TYPE(ch) == DOUBLESTAR) {
1886 kwarg = ast_for_expr(c, CHILD(n, i+1));
1887 i++;
1888 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 }
1890
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001891 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1892 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893}
1894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001896ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001898 /* testlist_gexp: test (',' test)* [','] */
1899 /* testlist: test (',' test)* [','] */
1900 /* testlist_safe: test (',' test)+ [','] */
1901 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001903 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001904 if (NCH(n) > 1)
1905 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001906 }
1907 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001908 assert(TYPE(n) == testlist ||
1909 TYPE(n) == testlist_safe ||
1910 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001911 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001913 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001915 asdl_seq *tmp = seq_for_testlist(c, n);
1916 if (!tmp)
1917 return NULL;
1918 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001920}
1921
1922static expr_ty
1923ast_for_testlist_gexp(struct compiling *c, const node* n)
1924{
1925 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1926 /* argument: test [ gen_for ] */
1927 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001928 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001929 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001930 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001931}
1932
1933/* like ast_for_testlist() but returns a sequence */
1934static asdl_seq*
1935ast_for_class_bases(struct compiling *c, const node* n)
1936{
1937 /* testlist: test (',' test)* [','] */
1938 assert(NCH(n) > 0);
1939 REQ(n, testlist);
1940 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001941 expr_ty base;
1942 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
1943 if (!bases)
1944 return NULL;
1945 base = ast_for_expr(c, CHILD(n, 0));
1946 if (!base)
1947 return NULL;
1948 asdl_seq_SET(bases, 0, base);
1949 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001950 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001951
1952 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953}
1954
1955static stmt_ty
1956ast_for_expr_stmt(struct compiling *c, const node *n)
1957{
1958 REQ(n, expr_stmt);
1959 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001960 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961 testlist: test (',' test)* [',']
1962 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001963 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964 test: ... here starts the operator precendence dance
1965 */
1966
1967 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001968 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
1969 if (!e)
1970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001972 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 }
1974 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001975 expr_ty expr1, expr2;
1976 operator_ty newoperator;
1977 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001979 expr1 = ast_for_testlist(c, ch);
1980 if (!expr1)
1981 return NULL;
1982 /* TODO(nas): Remove duplicated error checks (set_context does it) */
1983 switch (expr1->kind) {
1984 case GeneratorExp_kind:
1985 ast_error(ch, "augmented assignment to generator "
1986 "expression not possible");
1987 return NULL;
1988 case Yield_kind:
1989 ast_error(ch, "augmented assignment to yield "
1990 "expression not possible");
1991 return NULL;
1992 case Name_kind: {
1993 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1994 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1995 ast_error(ch, "assignment to None");
1996 return NULL;
1997 }
1998 break;
1999 }
2000 case Attribute_kind:
2001 case Subscript_kind:
2002 break;
2003 default:
2004 ast_error(ch, "illegal expression for augmented "
2005 "assignment");
2006 return NULL;
2007 }
2008 if(!set_context(expr1, Store, ch))
2009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002011 ch = CHILD(n, 2);
2012 if (TYPE(ch) == testlist)
2013 expr2 = ast_for_testlist(c, ch);
2014 else
2015 expr2 = ast_for_expr(c, ch);
2016 if (!expr2)
2017 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002019 newoperator = ast_for_augassign(CHILD(n, 1));
2020 if (!newoperator)
2021 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002023 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2024 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 }
2026 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002027 int i;
2028 asdl_seq *targets;
2029 node *value;
2030 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002032 /* a normal assignment */
2033 REQ(CHILD(n, 1), EQUAL);
2034 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2035 if (!targets)
2036 return NULL;
2037 for (i = 0; i < NCH(n) - 2; i += 2) {
2038 expr_ty e;
2039 node *ch = CHILD(n, i);
2040 if (TYPE(ch) == yield_expr) {
2041 ast_error(ch, "assignment to yield expression not possible");
2042 return NULL;
2043 }
2044 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002046 /* set context to assign */
2047 if (!e)
2048 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002050 if (!set_context(e, Store, CHILD(n, i)))
2051 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002053 asdl_seq_SET(targets, i / 2, e);
2054 }
2055 value = CHILD(n, NCH(n) - 1);
2056 if (TYPE(value) == testlist)
2057 expression = ast_for_testlist(c, value);
2058 else
2059 expression = ast_for_expr(c, value);
2060 if (!expression)
2061 return NULL;
2062 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2063 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065}
2066
2067static stmt_ty
2068ast_for_print_stmt(struct compiling *c, const node *n)
2069{
2070 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002071 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 */
2073 expr_ty dest = NULL, expression;
2074 asdl_seq *seq;
2075 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002076 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077
2078 REQ(n, print_stmt);
2079 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002080 dest = ast_for_expr(c, CHILD(n, 2));
2081 if (!dest)
2082 return NULL;
2083 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002085 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002087 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002088 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002089 expression = ast_for_expr(c, CHILD(n, i));
2090 if (!expression)
2091 return NULL;
2092 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 }
2094 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002095 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096}
2097
2098static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002099ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100{
2101 asdl_seq *seq;
2102 int i;
2103 expr_ty e;
2104
2105 REQ(n, exprlist);
2106
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002107 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002109 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002111 e = ast_for_expr(c, CHILD(n, i));
2112 if (!e)
2113 return NULL;
2114 asdl_seq_SET(seq, i / 2, e);
2115 if (context && !set_context(e, context, CHILD(n, i)))
2116 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 }
2118 return seq;
2119}
2120
2121static stmt_ty
2122ast_for_del_stmt(struct compiling *c, const node *n)
2123{
2124 asdl_seq *expr_list;
2125
2126 /* del_stmt: 'del' exprlist */
2127 REQ(n, del_stmt);
2128
2129 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2130 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002131 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002132 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133}
2134
2135static stmt_ty
2136ast_for_flow_stmt(struct compiling *c, const node *n)
2137{
2138 /*
2139 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002140 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 break_stmt: 'break'
2142 continue_stmt: 'continue'
2143 return_stmt: 'return' [testlist]
2144 yield_stmt: yield_expr
2145 yield_expr: 'yield' testlist
2146 raise_stmt: 'raise' [test [',' test [',' test]]]
2147 */
2148 node *ch;
2149
2150 REQ(n, flow_stmt);
2151 ch = CHILD(n, 0);
2152 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002153 case break_stmt:
2154 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2155 case continue_stmt:
2156 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2157 case yield_stmt: { /* will reduce to yield_expr */
2158 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2159 if (!exp)
2160 return NULL;
2161 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2162 }
2163 case return_stmt:
2164 if (NCH(ch) == 1)
2165 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2166 else {
2167 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2168 if (!expression)
2169 return NULL;
2170 return Return(expression, LINENO(n), n->n_col_offset,
2171 c->c_arena);
2172 }
2173 case raise_stmt:
2174 if (NCH(ch) == 1)
2175 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2176 c->c_arena);
2177 else if (NCH(ch) == 2) {
2178 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2179 if (!expression)
2180 return NULL;
2181 return Raise(expression, NULL, NULL, LINENO(n),
2182 n->n_col_offset, c->c_arena);
2183 }
2184 else if (NCH(ch) == 4) {
2185 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002187 expr1 = ast_for_expr(c, CHILD(ch, 1));
2188 if (!expr1)
2189 return NULL;
2190 expr2 = ast_for_expr(c, CHILD(ch, 3));
2191 if (!expr2)
2192 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002194 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2195 c->c_arena);
2196 }
2197 else if (NCH(ch) == 6) {
2198 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002200 expr1 = ast_for_expr(c, CHILD(ch, 1));
2201 if (!expr1)
2202 return NULL;
2203 expr2 = ast_for_expr(c, CHILD(ch, 3));
2204 if (!expr2)
2205 return NULL;
2206 expr3 = ast_for_expr(c, CHILD(ch, 5));
2207 if (!expr3)
2208 return NULL;
2209
2210 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2211 c->c_arena);
2212 }
2213 default:
2214 PyErr_Format(PyExc_SystemError,
2215 "unexpected flow_stmt: %d", TYPE(ch));
2216 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002218
2219 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221}
2222
2223static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002224alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225{
2226 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002227 import_as_name: NAME ['as' NAME]
2228 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 dotted_name: NAME ('.' NAME)*
2230 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002231 PyObject *str;
2232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002233 loop:
2234 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002235 case import_as_name:
2236 str = NULL;
2237 if (NCH(n) == 3) {
2238 str = NEW_IDENTIFIER(CHILD(n, 2));
2239 }
2240 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2241 case dotted_as_name:
2242 if (NCH(n) == 1) {
2243 n = CHILD(n, 0);
2244 goto loop;
2245 }
2246 else {
2247 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2248 if (!a)
2249 return NULL;
2250 assert(!a->asname);
2251 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2252 return a;
2253 }
2254 break;
2255 case dotted_name:
2256 if (NCH(n) == 1)
2257 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2258 else {
2259 /* Create a string of the form "a.b.c" */
2260 int i;
2261 size_t len;
2262 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002264 len = 0;
2265 for (i = 0; i < NCH(n); i += 2)
2266 /* length of string plus one for the dot */
2267 len += strlen(STR(CHILD(n, i))) + 1;
2268 len--; /* the last name doesn't have a dot */
2269 str = PyString_FromStringAndSize(NULL, len);
2270 if (!str)
2271 return NULL;
2272 s = PyString_AS_STRING(str);
2273 if (!s)
2274 return NULL;
2275 for (i = 0; i < NCH(n); i += 2) {
2276 char *sch = STR(CHILD(n, i));
2277 strcpy(s, STR(CHILD(n, i)));
2278 s += strlen(sch);
2279 *s++ = '.';
2280 }
2281 --s;
2282 *s = '\0';
2283 PyString_InternInPlace(&str);
2284 PyArena_AddPyObject(c->c_arena, str);
2285 return alias(str, NULL, c->c_arena);
2286 }
2287 break;
2288 case STAR:
2289 str = PyString_InternFromString("*");
2290 PyArena_AddPyObject(c->c_arena, str);
2291 return alias(str, NULL, c->c_arena);
2292 default:
2293 PyErr_Format(PyExc_SystemError,
2294 "unexpected import name: %d", TYPE(n));
2295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002297
2298 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 return NULL;
2300}
2301
2302static stmt_ty
2303ast_for_import_stmt(struct compiling *c, const node *n)
2304{
2305 /*
2306 import_stmt: import_name | import_from
2307 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002308 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002309 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002311 int lineno;
2312 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 int i;
2314 asdl_seq *aliases;
2315
2316 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002317 lineno = LINENO(n);
2318 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002320 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002321 n = CHILD(n, 1);
2322 REQ(n, dotted_as_names);
2323 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2324 if (!aliases)
2325 return NULL;
2326 for (i = 0; i < NCH(n); i += 2) {
2327 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2328 if (!import_alias)
2329 return NULL;
2330 asdl_seq_SET(aliases, i / 2, import_alias);
2331 }
2332 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002334 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002335 int n_children;
2336 int idx, ndots = 0;
2337 alias_ty mod = NULL;
2338 identifier modname;
2339
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002340 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002341 optional module name */
2342 for (idx = 1; idx < NCH(n); idx++) {
2343 if (TYPE(CHILD(n, idx)) == dotted_name) {
2344 mod = alias_for_import_name(c, CHILD(n, idx));
2345 idx++;
2346 break;
2347 } else if (TYPE(CHILD(n, idx)) != DOT) {
2348 break;
2349 }
2350 ndots++;
2351 }
2352 idx++; /* skip over the 'import' keyword */
2353 switch (TYPE(CHILD(n, idx))) {
2354 case STAR:
2355 /* from ... import * */
2356 n = CHILD(n, idx);
2357 n_children = 1;
2358 if (ndots) {
2359 ast_error(n, "'import *' not allowed with 'from .'");
2360 return NULL;
2361 }
2362 break;
2363 case LPAR:
2364 /* from ... import (x, y, z) */
2365 n = CHILD(n, idx + 1);
2366 n_children = NCH(n);
2367 break;
2368 case import_as_names:
2369 /* from ... import x, y, z */
2370 n = CHILD(n, idx);
2371 n_children = NCH(n);
2372 if (n_children % 2 == 0) {
2373 ast_error(n, "trailing comma not allowed without"
2374 " surrounding parentheses");
2375 return NULL;
2376 }
2377 break;
2378 default:
2379 ast_error(n, "Unexpected node-type in from-import");
2380 return NULL;
2381 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002383 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2384 if (!aliases)
2385 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002387 /* handle "from ... import *" special b/c there's no children */
2388 if (TYPE(n) == STAR) {
2389 alias_ty import_alias = alias_for_import_name(c, n);
2390 if (!import_alias)
2391 return NULL;
2392 asdl_seq_SET(aliases, 0, import_alias);
2393 }
2394 else {
2395 for (i = 0; i < NCH(n); i += 2) {
2396 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2397 if (!import_alias)
2398 return NULL;
2399 asdl_seq_SET(aliases, i / 2, import_alias);
2400 }
2401 }
2402 if (mod != NULL)
2403 modname = mod->name;
2404 else
2405 modname = new_identifier("", c->c_arena);
2406 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2407 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 }
Neal Norwitz79792652005-11-14 04:25:03 +00002409 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002410 "unknown import statement: starts with command '%s'",
2411 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 return NULL;
2413}
2414
2415static stmt_ty
2416ast_for_global_stmt(struct compiling *c, const node *n)
2417{
2418 /* global_stmt: 'global' NAME (',' NAME)* */
2419 identifier name;
2420 asdl_seq *s;
2421 int i;
2422
2423 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002424 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002426 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002428 name = NEW_IDENTIFIER(CHILD(n, i));
2429 if (!name)
2430 return NULL;
2431 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002433 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434}
2435
2436static stmt_ty
2437ast_for_exec_stmt(struct compiling *c, const node *n)
2438{
2439 expr_ty expr1, globals = NULL, locals = NULL;
2440 int n_children = NCH(n);
2441 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002442 PyErr_Format(PyExc_SystemError,
2443 "poorly formed 'exec' statement: %d parts to statement",
2444 n_children);
2445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 }
2447
2448 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2449 REQ(n, exec_stmt);
2450 expr1 = ast_for_expr(c, CHILD(n, 1));
2451 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002452 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002454 globals = ast_for_expr(c, CHILD(n, 3));
2455 if (!globals)
2456 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 }
2458 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002459 locals = ast_for_expr(c, CHILD(n, 5));
2460 if (!locals)
2461 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462 }
2463
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002464 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2465 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466}
2467
2468static stmt_ty
2469ast_for_assert_stmt(struct compiling *c, const node *n)
2470{
2471 /* assert_stmt: 'assert' test [',' test] */
2472 REQ(n, assert_stmt);
2473 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002474 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2475 if (!expression)
2476 return NULL;
2477 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2478 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 }
2480 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002481 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002483 expr1 = ast_for_expr(c, CHILD(n, 1));
2484 if (!expr1)
2485 return NULL;
2486 expr2 = ast_for_expr(c, CHILD(n, 3));
2487 if (!expr2)
2488 return NULL;
2489
2490 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 }
Neal Norwitz79792652005-11-14 04:25:03 +00002492 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002493 "improper number of parts to 'assert' statement: %d",
2494 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 return NULL;
2496}
2497
2498static asdl_seq *
2499ast_for_suite(struct compiling *c, const node *n)
2500{
2501 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002502 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 stmt_ty s;
2504 int i, total, num, end, pos = 0;
2505 node *ch;
2506
2507 REQ(n, suite);
2508
2509 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002510 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002514 n = CHILD(n, 0);
2515 /* simple_stmt always ends with a NEWLINE,
2516 and may have a trailing SEMI
2517 */
2518 end = NCH(n) - 1;
2519 if (TYPE(CHILD(n, end - 1)) == SEMI)
2520 end--;
2521 /* loop by 2 to skip semi-colons */
2522 for (i = 0; i < end; i += 2) {
2523 ch = CHILD(n, i);
2524 s = ast_for_stmt(c, ch);
2525 if (!s)
2526 return NULL;
2527 asdl_seq_SET(seq, pos++, s);
2528 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 }
2530 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002531 for (i = 2; i < (NCH(n) - 1); i++) {
2532 ch = CHILD(n, i);
2533 REQ(ch, stmt);
2534 num = num_stmts(ch);
2535 if (num == 1) {
2536 /* small_stmt or compound_stmt with only one child */
2537 s = ast_for_stmt(c, ch);
2538 if (!s)
2539 return NULL;
2540 asdl_seq_SET(seq, pos++, s);
2541 }
2542 else {
2543 int j;
2544 ch = CHILD(ch, 0);
2545 REQ(ch, simple_stmt);
2546 for (j = 0; j < NCH(ch); j += 2) {
2547 /* statement terminates with a semi-colon ';' */
2548 if (NCH(CHILD(ch, j)) == 0) {
2549 assert((j + 1) == NCH(ch));
2550 break;
2551 }
2552 s = ast_for_stmt(c, CHILD(ch, j));
2553 if (!s)
2554 return NULL;
2555 asdl_seq_SET(seq, pos++, s);
2556 }
2557 }
2558 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 }
2560 assert(pos == seq->size);
2561 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562}
2563
2564static stmt_ty
2565ast_for_if_stmt(struct compiling *c, const node *n)
2566{
2567 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2568 ['else' ':' suite]
2569 */
2570 char *s;
2571
2572 REQ(n, if_stmt);
2573
2574 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002575 expr_ty expression;
2576 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002578 expression = ast_for_expr(c, CHILD(n, 1));
2579 if (!expression)
2580 return NULL;
2581 suite_seq = ast_for_suite(c, CHILD(n, 3));
2582 if (!suite_seq)
2583 return NULL;
2584
2585 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2586 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002588
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 s = STR(CHILD(n, 4));
2590 /* s[2], the third character in the string, will be
2591 's' for el_s_e, or
2592 'i' for el_i_f
2593 */
2594 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002595 expr_ty expression;
2596 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002598 expression = ast_for_expr(c, CHILD(n, 1));
2599 if (!expression)
2600 return NULL;
2601 seq1 = ast_for_suite(c, CHILD(n, 3));
2602 if (!seq1)
2603 return NULL;
2604 seq2 = ast_for_suite(c, CHILD(n, 6));
2605 if (!seq2)
2606 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002608 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2609 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 }
2611 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002612 int i, n_elif, has_else = 0;
2613 expr_ty expression;
2614 asdl_seq *suite_seq;
2615 asdl_seq *orelse = NULL;
2616 n_elif = NCH(n) - 4;
2617 /* must reference the child n_elif+1 since 'else' token is third,
2618 not fourth, child from the end. */
2619 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2620 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2621 has_else = 1;
2622 n_elif -= 3;
2623 }
2624 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002626 if (has_else) {
2627 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002629 orelse = asdl_seq_new(1, c->c_arena);
2630 if (!orelse)
2631 return NULL;
2632 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2633 if (!expression)
2634 return NULL;
2635 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2636 if (!suite_seq)
2637 return NULL;
2638 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2639 if (!suite_seq2)
2640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002642 asdl_seq_SET(orelse, 0,
2643 If(expression, suite_seq, suite_seq2,
2644 LINENO(CHILD(n, NCH(n) - 6)),
2645 CHILD(n, NCH(n) - 6)->n_col_offset,
2646 c->c_arena));
2647 /* the just-created orelse handled the last elif */
2648 n_elif--;
2649 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002651 for (i = 0; i < n_elif; i++) {
2652 int off = 5 + (n_elif - i - 1) * 4;
2653 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2654 if (!newobj)
2655 return NULL;
2656 expression = ast_for_expr(c, CHILD(n, off));
2657 if (!expression)
2658 return NULL;
2659 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2660 if (!suite_seq)
2661 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002663 asdl_seq_SET(newobj, 0,
2664 If(expression, suite_seq, orelse,
2665 LINENO(CHILD(n, off)),
2666 CHILD(n, off)->n_col_offset, c->c_arena));
2667 orelse = newobj;
2668 }
2669 expression = ast_for_expr(c, CHILD(n, 1));
2670 if (!expression)
2671 return NULL;
2672 suite_seq = ast_for_suite(c, CHILD(n, 3));
2673 if (!suite_seq)
2674 return NULL;
2675 return If(expression, suite_seq, orelse,
2676 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002678
2679 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002680 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002681 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682}
2683
2684static stmt_ty
2685ast_for_while_stmt(struct compiling *c, const node *n)
2686{
2687 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2688 REQ(n, while_stmt);
2689
2690 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002691 expr_ty expression;
2692 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002694 expression = ast_for_expr(c, CHILD(n, 1));
2695 if (!expression)
2696 return NULL;
2697 suite_seq = ast_for_suite(c, CHILD(n, 3));
2698 if (!suite_seq)
2699 return NULL;
2700 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2701 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 }
2703 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002704 expr_ty expression;
2705 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002707 expression = ast_for_expr(c, CHILD(n, 1));
2708 if (!expression)
2709 return NULL;
2710 seq1 = ast_for_suite(c, CHILD(n, 3));
2711 if (!seq1)
2712 return NULL;
2713 seq2 = ast_for_suite(c, CHILD(n, 6));
2714 if (!seq2)
2715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002717 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2718 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002720
2721 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002722 "wrong number of tokens for 'while' statement: %d",
2723 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002724 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725}
2726
2727static stmt_ty
2728ast_for_for_stmt(struct compiling *c, const node *n)
2729{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002730 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 expr_ty expression;
2732 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002733 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2735 REQ(n, for_stmt);
2736
2737 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002738 seq = ast_for_suite(c, CHILD(n, 8));
2739 if (!seq)
2740 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 }
2742
Neal Norwitzedef2be2006-07-12 05:26:17 +00002743 node_target = CHILD(n, 1);
2744 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002745 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002746 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002747 /* Check the # of children rather than the length of _target, since
2748 for x, in ... has 1 element in _target, but still requires a Tuple. */
2749 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002750 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002752 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002754 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002755 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002756 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002758 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002759 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002761 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002762 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763}
2764
2765static excepthandler_ty
2766ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2767{
2768 /* except_clause: 'except' [test [',' test]] */
2769 REQ(exc, except_clause);
2770 REQ(body, suite);
2771
2772 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002773 asdl_seq *suite_seq = ast_for_suite(c, body);
2774 if (!suite_seq)
2775 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002777 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2778 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 }
2780 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002781 expr_ty expression;
2782 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002784 expression = ast_for_expr(c, CHILD(exc, 1));
2785 if (!expression)
2786 return NULL;
2787 suite_seq = ast_for_suite(c, body);
2788 if (!suite_seq)
2789 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002791 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2792 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 }
2794 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002795 asdl_seq *suite_seq;
2796 expr_ty expression;
2797 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2798 if (!e)
2799 return NULL;
2800 if (!set_context(e, Store, CHILD(exc, 3)))
2801 return NULL;
2802 expression = ast_for_expr(c, CHILD(exc, 1));
2803 if (!expression)
2804 return NULL;
2805 suite_seq = ast_for_suite(c, body);
2806 if (!suite_seq)
2807 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002809 return excepthandler(expression, e, suite_seq, LINENO(exc),
2810 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002812
2813 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002814 "wrong number of children for 'except' clause: %d",
2815 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817}
2818
2819static stmt_ty
2820ast_for_try_stmt(struct compiling *c, const node *n)
2821{
Neal Norwitzf599f422005-12-17 21:33:47 +00002822 const int nch = NCH(n);
2823 int n_except = (nch - 3)/3;
2824 asdl_seq *body, *orelse = NULL, *finally = NULL;
2825
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 REQ(n, try_stmt);
2827
Neal Norwitzf599f422005-12-17 21:33:47 +00002828 body = ast_for_suite(c, CHILD(n, 2));
2829 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
Neal Norwitzf599f422005-12-17 21:33:47 +00002832 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002833 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2834 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2835 /* we can assume it's an "else",
2836 because nch >= 9 for try-else-finally and
2837 it would otherwise have a type of except_clause */
2838 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2839 if (orelse == NULL)
2840 return NULL;
2841 n_except--;
2842 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002844 finally = ast_for_suite(c, CHILD(n, nch - 1));
2845 if (finally == NULL)
2846 return NULL;
2847 n_except--;
2848 }
2849 else {
2850 /* we can assume it's an "else",
2851 otherwise it would have a type of except_clause */
2852 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2853 if (orelse == NULL)
2854 return NULL;
2855 n_except--;
2856 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002858 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002859 ast_error(n, "malformed 'try' statement");
2860 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002862
2863 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002864 int i;
2865 stmt_ty except_st;
2866 /* process except statements to create a try ... except */
2867 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2868 if (handlers == NULL)
2869 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002870
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002871 for (i = 0; i < n_except; i++) {
2872 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2873 CHILD(n, 5 + i * 3));
2874 if (!e)
2875 return NULL;
2876 asdl_seq_SET(handlers, i, e);
2877 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002878
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002879 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2880 n->n_col_offset, c->c_arena);
2881 if (!finally)
2882 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002883
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002884 /* if a 'finally' is present too, we nest the TryExcept within a
2885 TryFinally to emulate try ... except ... finally */
2886 body = asdl_seq_new(1, c->c_arena);
2887 if (body == NULL)
2888 return NULL;
2889 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002890 }
2891
2892 /* must be a try ... finally (except clauses are in body, if any exist) */
2893 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002894 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895}
2896
Guido van Rossumc2e20742006-02-27 22:32:47 +00002897static expr_ty
2898ast_for_with_var(struct compiling *c, const node *n)
2899{
2900 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002901 return ast_for_expr(c, CHILD(n, 1));
2902}
2903
2904/* with_stmt: 'with' test [ with_var ] ':' suite */
2905static stmt_ty
2906ast_for_with_stmt(struct compiling *c, const node *n)
2907{
2908 expr_ty context_expr, optional_vars = NULL;
2909 int suite_index = 3; /* skip 'with', test, and ':' */
2910 asdl_seq *suite_seq;
2911
2912 assert(TYPE(n) == with_stmt);
2913 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002914 if (!context_expr)
2915 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002916 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002917 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002918
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002919 if (!optional_vars) {
2920 return NULL;
2921 }
2922 if (!set_context(optional_vars, Store, n)) {
2923 return NULL;
2924 }
2925 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002926 }
2927
2928 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2929 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002930 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002931 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002932 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002933 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002934}
2935
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936static stmt_ty
2937ast_for_classdef(struct compiling *c, const node *n)
2938{
2939 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 asdl_seq *bases, *s;
2941
2942 REQ(n, classdef);
2943
2944 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002945 ast_error(n, "assignment to None");
2946 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 }
2948
2949 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002950 s = ast_for_suite(c, CHILD(n, 3));
2951 if (!s)
2952 return NULL;
2953 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2954 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 }
2956 /* check for empty base list */
2957 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002958 s = ast_for_suite(c, CHILD(n,5));
2959 if (!s)
2960 return NULL;
2961 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2962 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 }
2964
2965 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002966 bases = ast_for_class_bases(c, CHILD(n, 3));
2967 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002968 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969
2970 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002971 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002972 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002973 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002974 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975}
2976
2977static stmt_ty
2978ast_for_stmt(struct compiling *c, const node *n)
2979{
2980 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002981 assert(NCH(n) == 1);
2982 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983 }
2984 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002985 assert(num_stmts(n) == 1);
2986 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 }
2988 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002989 REQ(n, small_stmt);
2990 n = CHILD(n, 0);
2991 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2992 | flow_stmt | import_stmt | global_stmt | exec_stmt
2993 | assert_stmt
2994 */
2995 switch (TYPE(n)) {
2996 case expr_stmt:
2997 return ast_for_expr_stmt(c, n);
2998 case print_stmt:
2999 return ast_for_print_stmt(c, n);
3000 case del_stmt:
3001 return ast_for_del_stmt(c, n);
3002 case pass_stmt:
3003 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3004 case flow_stmt:
3005 return ast_for_flow_stmt(c, n);
3006 case import_stmt:
3007 return ast_for_import_stmt(c, n);
3008 case global_stmt:
3009 return ast_for_global_stmt(c, n);
3010 case exec_stmt:
3011 return ast_for_exec_stmt(c, n);
3012 case assert_stmt:
3013 return ast_for_assert_stmt(c, n);
3014 default:
3015 PyErr_Format(PyExc_SystemError,
3016 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3017 TYPE(n), NCH(n));
3018 return NULL;
3019 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 }
3021 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003022 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3023 | funcdef | classdef
3024 */
3025 node *ch = CHILD(n, 0);
3026 REQ(n, compound_stmt);
3027 switch (TYPE(ch)) {
3028 case if_stmt:
3029 return ast_for_if_stmt(c, ch);
3030 case while_stmt:
3031 return ast_for_while_stmt(c, ch);
3032 case for_stmt:
3033 return ast_for_for_stmt(c, ch);
3034 case try_stmt:
3035 return ast_for_try_stmt(c, ch);
3036 case with_stmt:
3037 return ast_for_with_stmt(c, ch);
3038 case funcdef:
3039 return ast_for_funcdef(c, ch);
3040 case classdef:
3041 return ast_for_classdef(c, ch);
3042 default:
3043 PyErr_Format(PyExc_SystemError,
3044 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3045 TYPE(n), NCH(n));
3046 return NULL;
3047 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048 }
3049}
3050
3051static PyObject *
3052parsenumber(const char *s)
3053{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003054 const char *end;
3055 long x;
3056 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003058 Py_complex c;
3059 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060#endif
3061
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003062 errno = 0;
3063 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003065 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003067 if (*end == 'l' || *end == 'L')
3068 return PyLong_FromString((char *)s, (char **)0, 0);
3069 if (s[0] == '0') {
3070 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3071 if (x < 0 && errno == 0) {
3072 return PyLong_FromString((char *)s,
3073 (char **)0,
3074 0);
3075 }
3076 }
3077 else
3078 x = PyOS_strtol((char *)s, (char **)&end, 0);
3079 if (*end == '\0') {
3080 if (errno != 0)
3081 return PyLong_FromString((char *)s, (char **)0, 0);
3082 return PyInt_FromLong(x);
3083 }
3084 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003086 if (imflag) {
3087 c.real = 0.;
3088 PyFPE_START_PROTECT("atof", return 0)
3089 c.imag = PyOS_ascii_atof(s);
3090 PyFPE_END_PROTECT(c)
3091 return PyComplex_FromCComplex(c);
3092 }
3093 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003094#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003095 {
3096 PyFPE_START_PROTECT("atof", return 0)
3097 dx = PyOS_ascii_atof(s);
3098 PyFPE_END_PROTECT(dx)
3099 return PyFloat_FromDouble(dx);
3100 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003101}
3102
3103static PyObject *
3104decode_utf8(const char **sPtr, const char *end, char* encoding)
3105{
3106#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003107 Py_FatalError("decode_utf8 should not be called in this build.");
3108 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003110 PyObject *u, *v;
3111 char *s, *t;
3112 t = s = (char *)*sPtr;
3113 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3114 while (s < end && (*s & 0x80)) s++;
3115 *sPtr = s;
3116 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3117 if (u == NULL)
3118 return NULL;
3119 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3120 Py_DECREF(u);
3121 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122#endif
3123}
3124
3125static PyObject *
3126decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3127{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003128 PyObject *v, *u;
3129 char *buf;
3130 char *p;
3131 const char *end;
3132 if (encoding == NULL) {
3133 buf = (char *)s;
3134 u = NULL;
3135 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3136 buf = (char *)s;
3137 u = NULL;
3138 } else {
3139 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3140 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3141 if (u == NULL)
3142 return NULL;
3143 p = buf = PyString_AsString(u);
3144 end = s + len;
3145 while (s < end) {
3146 if (*s == '\\') {
3147 *p++ = *s++;
3148 if (*s & 0x80) {
3149 strcpy(p, "u005c");
3150 p += 5;
3151 }
3152 }
3153 if (*s & 0x80) { /* XXX inefficient */
3154 PyObject *w;
3155 char *r;
3156 Py_ssize_t rn, i;
3157 w = decode_utf8(&s, end, "utf-16-be");
3158 if (w == NULL) {
3159 Py_DECREF(u);
3160 return NULL;
3161 }
3162 r = PyString_AsString(w);
3163 rn = PyString_Size(w);
3164 assert(rn % 2 == 0);
3165 for (i = 0; i < rn; i += 2) {
3166 sprintf(p, "\\u%02x%02x",
3167 r[i + 0] & 0xFF,
3168 r[i + 1] & 0xFF);
3169 p += 6;
3170 }
3171 Py_DECREF(w);
3172 } else {
3173 *p++ = *s++;
3174 }
3175 }
3176 len = p - buf;
3177 s = buf;
3178 }
3179 if (rawmode)
3180 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3181 else
3182 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3183 Py_XDECREF(u);
3184 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185}
3186
3187/* s is a Python string literal, including the bracketing quote characters,
3188 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3189 * parsestr parses it, and returns the decoded Python string object.
3190 */
3191static PyObject *
3192parsestr(const char *s, const char *encoding)
3193{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003194 size_t len;
3195 int quote = Py_CHARMASK(*s);
3196 int rawmode = 0;
3197 int need_encoding;
3198 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003200 if (isalpha(quote) || quote == '_') {
3201 if (quote == 'u' || quote == 'U') {
3202 quote = *++s;
3203 unicode = 1;
3204 }
3205 if (quote == 'r' || quote == 'R') {
3206 quote = *++s;
3207 rawmode = 1;
3208 }
3209 }
3210 if (quote != '\'' && quote != '\"') {
3211 PyErr_BadInternalCall();
3212 return NULL;
3213 }
3214 s++;
3215 len = strlen(s);
3216 if (len > INT_MAX) {
3217 PyErr_SetString(PyExc_OverflowError,
3218 "string to parse is too long");
3219 return NULL;
3220 }
3221 if (s[--len] != quote) {
3222 PyErr_BadInternalCall();
3223 return NULL;
3224 }
3225 if (len >= 4 && s[0] == quote && s[1] == quote) {
3226 s += 2;
3227 len -= 2;
3228 if (s[--len] != quote || s[--len] != quote) {
3229 PyErr_BadInternalCall();
3230 return NULL;
3231 }
3232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003234 if (unicode || Py_UnicodeFlag) {
3235 return decode_unicode(s, len, rawmode, encoding);
3236 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003238 need_encoding = (encoding != NULL &&
3239 strcmp(encoding, "utf-8") != 0 &&
3240 strcmp(encoding, "iso-8859-1") != 0);
3241 if (rawmode || strchr(s, '\\') == NULL) {
3242 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003243#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003244 /* This should not happen - we never see any other
3245 encoding. */
3246 Py_FatalError(
3247 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003249 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3250 if (u == NULL)
3251 return NULL;
3252 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3253 Py_DECREF(u);
3254 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003256 } else {
3257 return PyString_FromStringAndSize(s, len);
3258 }
3259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003261 return PyString_DecodeEscape(s, len, NULL, unicode,
3262 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263}
3264
3265/* Build a Python string object out of a STRING atom. This takes care of
3266 * compile-time literal catenation, calling parsestr() on each piece, and
3267 * pasting the intermediate results together.
3268 */
3269static PyObject *
3270parsestrplus(struct compiling *c, const node *n)
3271{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003272 PyObject *v;
3273 int i;
3274 REQ(CHILD(n, 0), STRING);
3275 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3276 /* String literal concatenation */
3277 for (i = 1; i < NCH(n); i++) {
3278 PyObject *s;
3279 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3280 if (s == NULL)
3281 goto onError;
3282 if (PyString_Check(v) && PyString_Check(s)) {
3283 PyString_ConcatAndDel(&v, s);
3284 if (v == NULL)
3285 goto onError;
3286 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003288 else {
3289 PyObject *temp = PyUnicode_Concat(v, s);
3290 Py_DECREF(s);
3291 Py_DECREF(v);
3292 v = temp;
3293 if (v == NULL)
3294 goto onError;
3295 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003297 }
3298 }
3299 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300
3301 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003302 Py_XDECREF(v);
3303 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304}