blob: 956ee205e93ecc9aec84c63278cce5be0be55845 [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);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001246 if (!str) {
1247 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1248 PyObject *type, *value, *tback, *errstr;
1249 PyErr_Fetch(&type, &value, &tback);
1250 errstr = ((PyUnicodeErrorObject *)value)->reason;
1251 if (errstr) {
1252 char *s = "";
1253 char buf[128];
1254 s = PyString_AsString(errstr);
1255 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1256 ast_error(n, buf);
1257 } else {
1258 ast_error(n, "(unicode error) unknown error");
1259 }
1260 Py_DECREF(type);
1261 Py_DECREF(value);
1262 Py_XDECREF(tback);
1263 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001264 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001265 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001266 PyArena_AddPyObject(c->c_arena, str);
1267 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 }
1269 case NUMBER: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001270 PyObject *pynum = parsenumber(STR(ch));
1271 if (!pynum)
1272 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001273
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001274 PyArena_AddPyObject(c->c_arena, pynum);
1275 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 }
1277 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001278 ch = CHILD(n, 1);
1279
1280 if (TYPE(ch) == RPAR)
1281 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1282
1283 if (TYPE(ch) == yield_expr)
1284 return ast_for_expr(c, ch);
1285
1286 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1287 return ast_for_genexp(c, ch);
1288
1289 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001291 ch = CHILD(n, 1);
1292
1293 if (TYPE(ch) == RSQB)
1294 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1295
1296 REQ(ch, listmaker);
1297 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1298 asdl_seq *elts = seq_for_testlist(c, ch);
1299 if (!elts)
1300 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001301
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001302 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1303 }
1304 else
1305 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001307 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1308 int i, size;
1309 asdl_seq *keys, *values;
1310
1311 ch = CHILD(n, 1);
1312 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1313 keys = asdl_seq_new(size, c->c_arena);
1314 if (!keys)
1315 return NULL;
1316
1317 values = asdl_seq_new(size, c->c_arena);
1318 if (!values)
1319 return NULL;
1320
1321 for (i = 0; i < NCH(ch); i += 4) {
1322 expr_ty expression;
1323
1324 expression = ast_for_expr(c, CHILD(ch, i));
1325 if (!expression)
1326 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001327
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001328 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001329
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001330 expression = ast_for_expr(c, CHILD(ch, i + 2));
1331 if (!expression)
1332 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001333
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001334 asdl_seq_SET(values, i / 4, expression);
1335 }
1336 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 }
1338 case BACKQUOTE: { /* repr */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001339 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
1340 if (!expression)
1341 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001342
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001343 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 }
1345 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001346 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1347 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 }
1349}
1350
1351static slice_ty
1352ast_for_slice(struct compiling *c, const node *n)
1353{
1354 node *ch;
1355 expr_ty lower = NULL, upper = NULL, step = NULL;
1356
1357 REQ(n, subscript);
1358
1359 /*
1360 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1361 sliceop: ':' [test]
1362 */
1363 ch = CHILD(n, 0);
1364 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001365 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366
1367 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001368 /* 'step' variable hold no significance in terms of being used over
1369 other vars */
1370 step = ast_for_expr(c, ch);
1371 if (!step)
1372 return NULL;
1373
1374 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 }
1376
1377 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001378 lower = ast_for_expr(c, ch);
1379 if (!lower)
1380 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 }
1382
1383 /* If there's an upper bound it's in the second or third position. */
1384 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001385 if (NCH(n) > 1) {
1386 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001388 if (TYPE(n2) == test) {
1389 upper = ast_for_expr(c, n2);
1390 if (!upper)
1391 return NULL;
1392 }
1393 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001395 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001397 if (TYPE(n2) == test) {
1398 upper = ast_for_expr(c, n2);
1399 if (!upper)
1400 return NULL;
1401 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 }
1403
1404 ch = CHILD(n, NCH(n) - 1);
1405 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001406 if (NCH(ch) == 1) {
1407 /* No expression, so step is None */
1408 ch = CHILD(ch, 0);
1409 step = Name(new_identifier("None", c->c_arena), Load,
1410 LINENO(ch), ch->n_col_offset, c->c_arena);
1411 if (!step)
1412 return NULL;
1413 } else {
1414 ch = CHILD(ch, 1);
1415 if (TYPE(ch) == test) {
1416 step = ast_for_expr(c, ch);
1417 if (!step)
1418 return NULL;
1419 }
1420 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421 }
1422
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001423 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424}
1425
1426static expr_ty
1427ast_for_binop(struct compiling *c, const node *n)
1428{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001429 /* Must account for a sequence of expressions.
1430 How should A op B op C by represented?
1431 BinOp(BinOp(A, op, B), op, C).
1432 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001434 int i, nops;
1435 expr_ty expr1, expr2, result;
1436 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001438 expr1 = ast_for_expr(c, CHILD(n, 0));
1439 if (!expr1)
1440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001442 expr2 = ast_for_expr(c, CHILD(n, 2));
1443 if (!expr2)
1444 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001446 newoperator = get_operator(CHILD(n, 1));
1447 if (!newoperator)
1448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001450 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1451 c->c_arena);
1452 if (!result)
1453 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001455 nops = (NCH(n) - 1) / 2;
1456 for (i = 1; i < nops; i++) {
1457 expr_ty tmp_result, tmp;
1458 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001460 newoperator = get_operator(next_oper);
1461 if (!newoperator)
1462 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001464 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1465 if (!tmp)
1466 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001468 tmp_result = BinOp(result, newoperator, tmp,
1469 LINENO(next_oper), next_oper->n_col_offset,
1470 c->c_arena);
1471 if (!tmp)
1472 return NULL;
1473 result = tmp_result;
1474 }
1475 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476}
1477
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001478static expr_ty
1479ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1480{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001481 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1482 subscriptlist: subscript (',' subscript)* [',']
1483 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1484 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001485 REQ(n, trailer);
1486 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001487 if (NCH(n) == 2)
1488 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1489 n->n_col_offset, c->c_arena);
1490 else
1491 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001492 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001493 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001494 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1495 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001496 }
1497 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001498 REQ(CHILD(n, 0), LSQB);
1499 REQ(CHILD(n, 2), RSQB);
1500 n = CHILD(n, 1);
1501 if (NCH(n) == 1) {
1502 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1503 if (!slc)
1504 return NULL;
1505 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1506 c->c_arena);
1507 }
1508 else {
1509 /* The grammar is ambiguous here. The ambiguity is resolved
1510 by treating the sequence as a tuple literal if there are
1511 no slice features.
1512 */
1513 int j;
1514 slice_ty slc;
1515 expr_ty e;
1516 bool simple = true;
1517 asdl_seq *slices, *elts;
1518 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1519 if (!slices)
1520 return NULL;
1521 for (j = 0; j < NCH(n); j += 2) {
1522 slc = ast_for_slice(c, CHILD(n, j));
1523 if (!slc)
1524 return NULL;
1525 if (slc->kind != Index_kind)
1526 simple = false;
1527 asdl_seq_SET(slices, j / 2, slc);
1528 }
1529 if (!simple) {
1530 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1531 Load, LINENO(n), n->n_col_offset, c->c_arena);
1532 }
1533 /* extract Index values and put them in a Tuple */
1534 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1535 if (!elts)
1536 return NULL;
1537 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1538 slc = (slice_ty)asdl_seq_GET(slices, j);
1539 assert(slc->kind == Index_kind && slc->v.Index.value);
1540 asdl_seq_SET(elts, j, slc->v.Index.value);
1541 }
1542 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1543 if (!e)
1544 return NULL;
1545 return Subscript(left_expr, Index(e, c->c_arena),
1546 Load, LINENO(n), n->n_col_offset, c->c_arena);
1547 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001548 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001549}
1550
1551static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001552ast_for_factor(struct compiling *c, const node *n)
1553{
1554 node *pfactor, *ppower, *patom, *pnum;
1555 expr_ty expression;
1556
1557 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001558 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001559 constant. The peephole optimizer already does something like
1560 this but it doesn't handle the case where the constant is
1561 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1562 PyLongObject.
1563 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001564 if (TYPE(CHILD(n, 0)) == MINUS &&
1565 NCH(n) == 2 &&
1566 TYPE((pfactor = CHILD(n, 1))) == factor &&
1567 NCH(pfactor) == 1 &&
1568 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1569 NCH(ppower) == 1 &&
1570 TYPE((patom = CHILD(ppower, 0))) == atom &&
1571 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1572 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1573 if (s == NULL)
1574 return NULL;
1575 s[0] = '-';
1576 strcpy(s + 1, STR(pnum));
1577 PyObject_FREE(STR(pnum));
1578 STR(pnum) = s;
1579 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001580 }
1581
1582 expression = ast_for_expr(c, CHILD(n, 1));
1583 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001584 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001585
1586 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001587 case PLUS:
1588 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1589 c->c_arena);
1590 case MINUS:
1591 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1592 c->c_arena);
1593 case TILDE:
1594 return UnaryOp(Invert, expression, LINENO(n),
1595 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001596 }
1597 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001598 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001599 return NULL;
1600}
1601
1602static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001603ast_for_power(struct compiling *c, const node *n)
1604{
1605 /* power: atom trailer* ('**' factor)*
1606 */
1607 int i;
1608 expr_ty e, tmp;
1609 REQ(n, power);
1610 e = ast_for_atom(c, CHILD(n, 0));
1611 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001612 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001613 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001614 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001615 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001616 node *ch = CHILD(n, i);
1617 if (TYPE(ch) != trailer)
1618 break;
1619 tmp = ast_for_trailer(c, ch, e);
1620 if (!tmp)
1621 return NULL;
1622 tmp->lineno = e->lineno;
1623 tmp->col_offset = e->col_offset;
1624 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001625 }
1626 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001627 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1628 if (!f)
1629 return NULL;
1630 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1631 if (!tmp)
1632 return NULL;
1633 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001634 }
1635 return e;
1636}
1637
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638/* Do not name a variable 'expr'! Will cause a compile error.
1639*/
1640
1641static expr_ty
1642ast_for_expr(struct compiling *c, const node *n)
1643{
1644 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001645 test: or_test ['if' or_test 'else' test] | lambdef
1646 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 and_test: not_test ('and' not_test)*
1648 not_test: 'not' not_test | comparison
1649 comparison: expr (comp_op expr)*
1650 expr: xor_expr ('|' xor_expr)*
1651 xor_expr: and_expr ('^' and_expr)*
1652 and_expr: shift_expr ('&' shift_expr)*
1653 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1654 arith_expr: term (('+'|'-') term)*
1655 term: factor (('*'|'/'|'%'|'//') factor)*
1656 factor: ('+'|'-'|'~') factor | power
1657 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001658
1659 As well as modified versions that exist for backward compatibility,
1660 to explicitly allow:
1661 [ x for x in lambda: 0, lambda: 1 ]
1662 (which would be ambiguous without these extra rules)
1663
1664 old_test: or_test | old_lambdef
1665 old_lambdef: 'lambda' [vararglist] ':' old_test
1666
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 */
1668
1669 asdl_seq *seq;
1670 int i;
1671
1672 loop:
1673 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001674 case test:
1675 case old_test:
1676 if (TYPE(CHILD(n, 0)) == lambdef ||
1677 TYPE(CHILD(n, 0)) == old_lambdef)
1678 return ast_for_lambdef(c, CHILD(n, 0));
1679 else if (NCH(n) > 1)
1680 return ast_for_ifexpr(c, n);
1681 /* Fallthrough */
1682 case or_test:
1683 case and_test:
1684 if (NCH(n) == 1) {
1685 n = CHILD(n, 0);
1686 goto loop;
1687 }
1688 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1689 if (!seq)
1690 return NULL;
1691 for (i = 0; i < NCH(n); i += 2) {
1692 expr_ty e = ast_for_expr(c, CHILD(n, i));
1693 if (!e)
1694 return NULL;
1695 asdl_seq_SET(seq, i / 2, e);
1696 }
1697 if (!strcmp(STR(CHILD(n, 1)), "and"))
1698 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1699 c->c_arena);
1700 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1701 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1702 case not_test:
1703 if (NCH(n) == 1) {
1704 n = CHILD(n, 0);
1705 goto loop;
1706 }
1707 else {
1708 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1709 if (!expression)
1710 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001712 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1713 c->c_arena);
1714 }
1715 case comparison:
1716 if (NCH(n) == 1) {
1717 n = CHILD(n, 0);
1718 goto loop;
1719 }
1720 else {
1721 expr_ty expression;
1722 asdl_int_seq *ops;
1723 asdl_seq *cmps;
1724 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1725 if (!ops)
1726 return NULL;
1727 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1728 if (!cmps) {
1729 return NULL;
1730 }
1731 for (i = 1; i < NCH(n); i += 2) {
1732 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001734 newoperator = ast_for_comp_op(CHILD(n, i));
1735 if (!newoperator) {
1736 return NULL;
1737 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001739 expression = ast_for_expr(c, CHILD(n, i + 1));
1740 if (!expression) {
1741 return NULL;
1742 }
1743
1744 asdl_seq_SET(ops, i / 2, newoperator);
1745 asdl_seq_SET(cmps, i / 2, expression);
1746 }
1747 expression = ast_for_expr(c, CHILD(n, 0));
1748 if (!expression) {
1749 return NULL;
1750 }
1751
1752 return Compare(expression, ops, cmps, LINENO(n),
1753 n->n_col_offset, c->c_arena);
1754 }
1755 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001757 /* The next five cases all handle BinOps. The main body of code
1758 is the same in each case, but the switch turned inside out to
1759 reuse the code for each type of operator.
1760 */
1761 case expr:
1762 case xor_expr:
1763 case and_expr:
1764 case shift_expr:
1765 case arith_expr:
1766 case term:
1767 if (NCH(n) == 1) {
1768 n = CHILD(n, 0);
1769 goto loop;
1770 }
1771 return ast_for_binop(c, n);
1772 case yield_expr: {
1773 expr_ty exp = NULL;
1774 if (NCH(n) == 2) {
1775 exp = ast_for_testlist(c, CHILD(n, 1));
1776 if (!exp)
1777 return NULL;
1778 }
1779 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1780 }
1781 case factor:
1782 if (NCH(n) == 1) {
1783 n = CHILD(n, 0);
1784 goto loop;
1785 }
1786 return ast_for_factor(c, n);
1787 case power:
1788 return ast_for_power(c, n);
1789 default:
1790 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1791 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001793 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 return NULL;
1795}
1796
1797static expr_ty
1798ast_for_call(struct compiling *c, const node *n, expr_ty func)
1799{
1800 /*
1801 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001802 | '**' test)
1803 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804 */
1805
1806 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001807 asdl_seq *args;
1808 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 expr_ty vararg = NULL, kwarg = NULL;
1810
1811 REQ(n, arglist);
1812
1813 nargs = 0;
1814 nkeywords = 0;
1815 ngens = 0;
1816 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001817 node *ch = CHILD(n, i);
1818 if (TYPE(ch) == argument) {
1819 if (NCH(ch) == 1)
1820 nargs++;
1821 else if (TYPE(CHILD(ch, 1)) == gen_for)
1822 ngens++;
1823 else
1824 nkeywords++;
1825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 }
1827 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001828 ast_error(n, "Generator expression must be parenthesized "
1829 "if not sole argument");
1830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 }
1832
1833 if (nargs + nkeywords + ngens > 255) {
1834 ast_error(n, "more than 255 arguments");
1835 return NULL;
1836 }
1837
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001838 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001840 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001841 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 nargs = 0;
1845 nkeywords = 0;
1846 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001847 node *ch = CHILD(n, i);
1848 if (TYPE(ch) == argument) {
1849 expr_ty e;
1850 if (NCH(ch) == 1) {
1851 if (nkeywords) {
1852 ast_error(CHILD(ch, 0),
1853 "non-keyword arg after keyword arg");
1854 return NULL;
1855 }
1856 e = ast_for_expr(c, CHILD(ch, 0));
1857 if (!e)
1858 return NULL;
1859 asdl_seq_SET(args, nargs++, e);
1860 }
1861 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1862 e = ast_for_genexp(c, ch);
1863 if (!e)
1864 return NULL;
1865 asdl_seq_SET(args, nargs++, e);
1866 }
1867 else {
1868 keyword_ty kw;
1869 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001871 /* CHILD(ch, 0) is test, but must be an identifier? */
1872 e = ast_for_expr(c, CHILD(ch, 0));
1873 if (!e)
1874 return NULL;
1875 /* f(lambda x: x[0] = 3) ends up getting parsed with
1876 * LHS test = lambda x: x[0], and RHS test = 3.
1877 * SF bug 132313 points out that complaining about a keyword
1878 * then is very confusing.
1879 */
1880 if (e->kind == Lambda_kind) {
1881 ast_error(CHILD(ch, 0),
1882 "lambda cannot contain assignment");
1883 return NULL;
1884 } else if (e->kind != Name_kind) {
1885 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1886 return NULL;
1887 }
1888 key = e->v.Name.id;
Georg Brandle06cf452007-06-07 13:23:24 +00001889 if (!strcmp(PyString_AS_STRING(key), "None")) {
1890 ast_error(CHILD(ch, 0), "assignment to None");
1891 return NULL;
1892 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001893 e = ast_for_expr(c, CHILD(ch, 2));
1894 if (!e)
1895 return NULL;
1896 kw = keyword(key, e, c->c_arena);
1897 if (!kw)
1898 return NULL;
1899 asdl_seq_SET(keywords, nkeywords++, kw);
1900 }
1901 }
1902 else if (TYPE(ch) == STAR) {
1903 vararg = ast_for_expr(c, CHILD(n, i+1));
1904 i++;
1905 }
1906 else if (TYPE(ch) == DOUBLESTAR) {
1907 kwarg = ast_for_expr(c, CHILD(n, i+1));
1908 i++;
1909 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 }
1911
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001912 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1913 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914}
1915
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001917ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001919 /* testlist_gexp: test (',' test)* [','] */
1920 /* testlist: test (',' test)* [','] */
1921 /* testlist_safe: test (',' test)+ [','] */
1922 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001924 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001925 if (NCH(n) > 1)
1926 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001927 }
1928 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001929 assert(TYPE(n) == testlist ||
1930 TYPE(n) == testlist_safe ||
1931 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001932 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001934 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001936 asdl_seq *tmp = seq_for_testlist(c, n);
1937 if (!tmp)
1938 return NULL;
1939 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001941}
1942
1943static expr_ty
1944ast_for_testlist_gexp(struct compiling *c, const node* n)
1945{
1946 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1947 /* argument: test [ gen_for ] */
1948 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001949 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001950 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001951 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001952}
1953
1954/* like ast_for_testlist() but returns a sequence */
1955static asdl_seq*
1956ast_for_class_bases(struct compiling *c, const node* n)
1957{
1958 /* testlist: test (',' test)* [','] */
1959 assert(NCH(n) > 0);
1960 REQ(n, testlist);
1961 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001962 expr_ty base;
1963 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
1964 if (!bases)
1965 return NULL;
1966 base = ast_for_expr(c, CHILD(n, 0));
1967 if (!base)
1968 return NULL;
1969 asdl_seq_SET(bases, 0, base);
1970 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001971 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001972
1973 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974}
1975
1976static stmt_ty
1977ast_for_expr_stmt(struct compiling *c, const node *n)
1978{
1979 REQ(n, expr_stmt);
1980 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001981 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 testlist: test (',' test)* [',']
1983 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001984 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 test: ... here starts the operator precendence dance
1986 */
1987
1988 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001989 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
1990 if (!e)
1991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001993 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 }
1995 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001996 expr_ty expr1, expr2;
1997 operator_ty newoperator;
1998 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002000 expr1 = ast_for_testlist(c, ch);
2001 if (!expr1)
2002 return NULL;
2003 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2004 switch (expr1->kind) {
2005 case GeneratorExp_kind:
2006 ast_error(ch, "augmented assignment to generator "
2007 "expression not possible");
2008 return NULL;
2009 case Yield_kind:
2010 ast_error(ch, "augmented assignment to yield "
2011 "expression not possible");
2012 return NULL;
2013 case Name_kind: {
2014 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2015 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2016 ast_error(ch, "assignment to None");
2017 return NULL;
2018 }
2019 break;
2020 }
2021 case Attribute_kind:
2022 case Subscript_kind:
2023 break;
2024 default:
2025 ast_error(ch, "illegal expression for augmented "
2026 "assignment");
2027 return NULL;
2028 }
2029 if(!set_context(expr1, Store, ch))
2030 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002032 ch = CHILD(n, 2);
2033 if (TYPE(ch) == testlist)
2034 expr2 = ast_for_testlist(c, ch);
2035 else
2036 expr2 = ast_for_expr(c, ch);
2037 if (!expr2)
2038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002040 newoperator = ast_for_augassign(CHILD(n, 1));
2041 if (!newoperator)
2042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002044 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2045 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 }
2047 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002048 int i;
2049 asdl_seq *targets;
2050 node *value;
2051 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002053 /* a normal assignment */
2054 REQ(CHILD(n, 1), EQUAL);
2055 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2056 if (!targets)
2057 return NULL;
2058 for (i = 0; i < NCH(n) - 2; i += 2) {
2059 expr_ty e;
2060 node *ch = CHILD(n, i);
2061 if (TYPE(ch) == yield_expr) {
2062 ast_error(ch, "assignment to yield expression not possible");
2063 return NULL;
2064 }
2065 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002067 /* set context to assign */
2068 if (!e)
2069 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002071 if (!set_context(e, Store, CHILD(n, i)))
2072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002074 asdl_seq_SET(targets, i / 2, e);
2075 }
2076 value = CHILD(n, NCH(n) - 1);
2077 if (TYPE(value) == testlist)
2078 expression = ast_for_testlist(c, value);
2079 else
2080 expression = ast_for_expr(c, value);
2081 if (!expression)
2082 return NULL;
2083 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2084 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086}
2087
2088static stmt_ty
2089ast_for_print_stmt(struct compiling *c, const node *n)
2090{
2091 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002092 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 */
2094 expr_ty dest = NULL, expression;
2095 asdl_seq *seq;
2096 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002097 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098
2099 REQ(n, print_stmt);
2100 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002101 dest = ast_for_expr(c, CHILD(n, 2));
2102 if (!dest)
2103 return NULL;
2104 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002106 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002108 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002109 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002110 expression = ast_for_expr(c, CHILD(n, i));
2111 if (!expression)
2112 return NULL;
2113 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 }
2115 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002116 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117}
2118
2119static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002120ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121{
2122 asdl_seq *seq;
2123 int i;
2124 expr_ty e;
2125
2126 REQ(n, exprlist);
2127
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002128 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002132 e = ast_for_expr(c, CHILD(n, i));
2133 if (!e)
2134 return NULL;
2135 asdl_seq_SET(seq, i / 2, e);
2136 if (context && !set_context(e, context, CHILD(n, i)))
2137 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 }
2139 return seq;
2140}
2141
2142static stmt_ty
2143ast_for_del_stmt(struct compiling *c, const node *n)
2144{
2145 asdl_seq *expr_list;
2146
2147 /* del_stmt: 'del' exprlist */
2148 REQ(n, del_stmt);
2149
2150 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2151 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002152 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002153 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154}
2155
2156static stmt_ty
2157ast_for_flow_stmt(struct compiling *c, const node *n)
2158{
2159 /*
2160 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002161 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 break_stmt: 'break'
2163 continue_stmt: 'continue'
2164 return_stmt: 'return' [testlist]
2165 yield_stmt: yield_expr
2166 yield_expr: 'yield' testlist
2167 raise_stmt: 'raise' [test [',' test [',' test]]]
2168 */
2169 node *ch;
2170
2171 REQ(n, flow_stmt);
2172 ch = CHILD(n, 0);
2173 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002174 case break_stmt:
2175 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2176 case continue_stmt:
2177 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2178 case yield_stmt: { /* will reduce to yield_expr */
2179 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2180 if (!exp)
2181 return NULL;
2182 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2183 }
2184 case return_stmt:
2185 if (NCH(ch) == 1)
2186 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2187 else {
2188 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2189 if (!expression)
2190 return NULL;
2191 return Return(expression, LINENO(n), n->n_col_offset,
2192 c->c_arena);
2193 }
2194 case raise_stmt:
2195 if (NCH(ch) == 1)
2196 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2197 c->c_arena);
2198 else if (NCH(ch) == 2) {
2199 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2200 if (!expression)
2201 return NULL;
2202 return Raise(expression, NULL, NULL, LINENO(n),
2203 n->n_col_offset, c->c_arena);
2204 }
2205 else if (NCH(ch) == 4) {
2206 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002208 expr1 = ast_for_expr(c, CHILD(ch, 1));
2209 if (!expr1)
2210 return NULL;
2211 expr2 = ast_for_expr(c, CHILD(ch, 3));
2212 if (!expr2)
2213 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002215 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2216 c->c_arena);
2217 }
2218 else if (NCH(ch) == 6) {
2219 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002221 expr1 = ast_for_expr(c, CHILD(ch, 1));
2222 if (!expr1)
2223 return NULL;
2224 expr2 = ast_for_expr(c, CHILD(ch, 3));
2225 if (!expr2)
2226 return NULL;
2227 expr3 = ast_for_expr(c, CHILD(ch, 5));
2228 if (!expr3)
2229 return NULL;
2230
2231 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2232 c->c_arena);
2233 }
2234 default:
2235 PyErr_Format(PyExc_SystemError,
2236 "unexpected flow_stmt: %d", TYPE(ch));
2237 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002239
2240 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2241 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242}
2243
2244static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002245alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246{
2247 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002248 import_as_name: NAME ['as' NAME]
2249 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 dotted_name: NAME ('.' NAME)*
2251 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002252 PyObject *str;
2253
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 loop:
2255 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002256 case import_as_name:
2257 str = NULL;
2258 if (NCH(n) == 3) {
2259 str = NEW_IDENTIFIER(CHILD(n, 2));
2260 }
2261 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2262 case dotted_as_name:
2263 if (NCH(n) == 1) {
2264 n = CHILD(n, 0);
2265 goto loop;
2266 }
2267 else {
2268 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2269 if (!a)
2270 return NULL;
2271 assert(!a->asname);
2272 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2273 return a;
2274 }
2275 break;
2276 case dotted_name:
2277 if (NCH(n) == 1)
2278 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2279 else {
2280 /* Create a string of the form "a.b.c" */
2281 int i;
2282 size_t len;
2283 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002285 len = 0;
2286 for (i = 0; i < NCH(n); i += 2)
2287 /* length of string plus one for the dot */
2288 len += strlen(STR(CHILD(n, i))) + 1;
2289 len--; /* the last name doesn't have a dot */
2290 str = PyString_FromStringAndSize(NULL, len);
2291 if (!str)
2292 return NULL;
2293 s = PyString_AS_STRING(str);
2294 if (!s)
2295 return NULL;
2296 for (i = 0; i < NCH(n); i += 2) {
2297 char *sch = STR(CHILD(n, i));
2298 strcpy(s, STR(CHILD(n, i)));
2299 s += strlen(sch);
2300 *s++ = '.';
2301 }
2302 --s;
2303 *s = '\0';
2304 PyString_InternInPlace(&str);
2305 PyArena_AddPyObject(c->c_arena, str);
2306 return alias(str, NULL, c->c_arena);
2307 }
2308 break;
2309 case STAR:
2310 str = PyString_InternFromString("*");
2311 PyArena_AddPyObject(c->c_arena, str);
2312 return alias(str, NULL, c->c_arena);
2313 default:
2314 PyErr_Format(PyExc_SystemError,
2315 "unexpected import name: %d", TYPE(n));
2316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002318
2319 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 return NULL;
2321}
2322
2323static stmt_ty
2324ast_for_import_stmt(struct compiling *c, const node *n)
2325{
2326 /*
2327 import_stmt: import_name | import_from
2328 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002329 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002330 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002332 int lineno;
2333 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 int i;
2335 asdl_seq *aliases;
2336
2337 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002338 lineno = LINENO(n);
2339 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002341 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002342 n = CHILD(n, 1);
2343 REQ(n, dotted_as_names);
2344 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2345 if (!aliases)
2346 return NULL;
2347 for (i = 0; i < NCH(n); i += 2) {
2348 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2349 if (!import_alias)
2350 return NULL;
2351 asdl_seq_SET(aliases, i / 2, import_alias);
2352 }
2353 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002355 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002356 int n_children;
2357 int idx, ndots = 0;
2358 alias_ty mod = NULL;
2359 identifier modname;
2360
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002361 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002362 optional module name */
2363 for (idx = 1; idx < NCH(n); idx++) {
2364 if (TYPE(CHILD(n, idx)) == dotted_name) {
2365 mod = alias_for_import_name(c, CHILD(n, idx));
2366 idx++;
2367 break;
2368 } else if (TYPE(CHILD(n, idx)) != DOT) {
2369 break;
2370 }
2371 ndots++;
2372 }
2373 idx++; /* skip over the 'import' keyword */
2374 switch (TYPE(CHILD(n, idx))) {
2375 case STAR:
2376 /* from ... import * */
2377 n = CHILD(n, idx);
2378 n_children = 1;
2379 if (ndots) {
2380 ast_error(n, "'import *' not allowed with 'from .'");
2381 return NULL;
2382 }
2383 break;
2384 case LPAR:
2385 /* from ... import (x, y, z) */
2386 n = CHILD(n, idx + 1);
2387 n_children = NCH(n);
2388 break;
2389 case import_as_names:
2390 /* from ... import x, y, z */
2391 n = CHILD(n, idx);
2392 n_children = NCH(n);
2393 if (n_children % 2 == 0) {
2394 ast_error(n, "trailing comma not allowed without"
2395 " surrounding parentheses");
2396 return NULL;
2397 }
2398 break;
2399 default:
2400 ast_error(n, "Unexpected node-type in from-import");
2401 return NULL;
2402 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002404 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2405 if (!aliases)
2406 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002408 /* handle "from ... import *" special b/c there's no children */
2409 if (TYPE(n) == STAR) {
2410 alias_ty import_alias = alias_for_import_name(c, n);
2411 if (!import_alias)
2412 return NULL;
2413 asdl_seq_SET(aliases, 0, import_alias);
2414 }
2415 else {
2416 for (i = 0; i < NCH(n); i += 2) {
2417 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2418 if (!import_alias)
2419 return NULL;
2420 asdl_seq_SET(aliases, i / 2, import_alias);
2421 }
2422 }
2423 if (mod != NULL)
2424 modname = mod->name;
2425 else
2426 modname = new_identifier("", c->c_arena);
2427 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2428 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429 }
Neal Norwitz79792652005-11-14 04:25:03 +00002430 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002431 "unknown import statement: starts with command '%s'",
2432 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433 return NULL;
2434}
2435
2436static stmt_ty
2437ast_for_global_stmt(struct compiling *c, const node *n)
2438{
2439 /* global_stmt: 'global' NAME (',' NAME)* */
2440 identifier name;
2441 asdl_seq *s;
2442 int i;
2443
2444 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002445 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002447 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002449 name = NEW_IDENTIFIER(CHILD(n, i));
2450 if (!name)
2451 return NULL;
2452 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002454 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455}
2456
2457static stmt_ty
2458ast_for_exec_stmt(struct compiling *c, const node *n)
2459{
2460 expr_ty expr1, globals = NULL, locals = NULL;
2461 int n_children = NCH(n);
2462 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002463 PyErr_Format(PyExc_SystemError,
2464 "poorly formed 'exec' statement: %d parts to statement",
2465 n_children);
2466 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 }
2468
2469 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2470 REQ(n, exec_stmt);
2471 expr1 = ast_for_expr(c, CHILD(n, 1));
2472 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002473 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002475 globals = ast_for_expr(c, CHILD(n, 3));
2476 if (!globals)
2477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 }
2479 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002480 locals = ast_for_expr(c, CHILD(n, 5));
2481 if (!locals)
2482 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 }
2484
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002485 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2486 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487}
2488
2489static stmt_ty
2490ast_for_assert_stmt(struct compiling *c, const node *n)
2491{
2492 /* assert_stmt: 'assert' test [',' test] */
2493 REQ(n, assert_stmt);
2494 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002495 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2496 if (!expression)
2497 return NULL;
2498 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2499 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 }
2501 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002502 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002504 expr1 = ast_for_expr(c, CHILD(n, 1));
2505 if (!expr1)
2506 return NULL;
2507 expr2 = ast_for_expr(c, CHILD(n, 3));
2508 if (!expr2)
2509 return NULL;
2510
2511 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 }
Neal Norwitz79792652005-11-14 04:25:03 +00002513 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002514 "improper number of parts to 'assert' statement: %d",
2515 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 return NULL;
2517}
2518
2519static asdl_seq *
2520ast_for_suite(struct compiling *c, const node *n)
2521{
2522 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002523 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 stmt_ty s;
2525 int i, total, num, end, pos = 0;
2526 node *ch;
2527
2528 REQ(n, suite);
2529
2530 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002531 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002533 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002535 n = CHILD(n, 0);
2536 /* simple_stmt always ends with a NEWLINE,
2537 and may have a trailing SEMI
2538 */
2539 end = NCH(n) - 1;
2540 if (TYPE(CHILD(n, end - 1)) == SEMI)
2541 end--;
2542 /* loop by 2 to skip semi-colons */
2543 for (i = 0; i < end; i += 2) {
2544 ch = CHILD(n, i);
2545 s = ast_for_stmt(c, ch);
2546 if (!s)
2547 return NULL;
2548 asdl_seq_SET(seq, pos++, s);
2549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 }
2551 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002552 for (i = 2; i < (NCH(n) - 1); i++) {
2553 ch = CHILD(n, i);
2554 REQ(ch, stmt);
2555 num = num_stmts(ch);
2556 if (num == 1) {
2557 /* small_stmt or compound_stmt with only one child */
2558 s = ast_for_stmt(c, ch);
2559 if (!s)
2560 return NULL;
2561 asdl_seq_SET(seq, pos++, s);
2562 }
2563 else {
2564 int j;
2565 ch = CHILD(ch, 0);
2566 REQ(ch, simple_stmt);
2567 for (j = 0; j < NCH(ch); j += 2) {
2568 /* statement terminates with a semi-colon ';' */
2569 if (NCH(CHILD(ch, j)) == 0) {
2570 assert((j + 1) == NCH(ch));
2571 break;
2572 }
2573 s = ast_for_stmt(c, CHILD(ch, j));
2574 if (!s)
2575 return NULL;
2576 asdl_seq_SET(seq, pos++, s);
2577 }
2578 }
2579 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 }
2581 assert(pos == seq->size);
2582 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583}
2584
2585static stmt_ty
2586ast_for_if_stmt(struct compiling *c, const node *n)
2587{
2588 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2589 ['else' ':' suite]
2590 */
2591 char *s;
2592
2593 REQ(n, if_stmt);
2594
2595 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002596 expr_ty expression;
2597 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002599 expression = ast_for_expr(c, CHILD(n, 1));
2600 if (!expression)
2601 return NULL;
2602 suite_seq = ast_for_suite(c, CHILD(n, 3));
2603 if (!suite_seq)
2604 return NULL;
2605
2606 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2607 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002609
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 s = STR(CHILD(n, 4));
2611 /* s[2], the third character in the string, will be
2612 's' for el_s_e, or
2613 'i' for el_i_f
2614 */
2615 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002616 expr_ty expression;
2617 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002619 expression = ast_for_expr(c, CHILD(n, 1));
2620 if (!expression)
2621 return NULL;
2622 seq1 = ast_for_suite(c, CHILD(n, 3));
2623 if (!seq1)
2624 return NULL;
2625 seq2 = ast_for_suite(c, CHILD(n, 6));
2626 if (!seq2)
2627 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002629 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2630 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 }
2632 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002633 int i, n_elif, has_else = 0;
2634 expr_ty expression;
2635 asdl_seq *suite_seq;
2636 asdl_seq *orelse = NULL;
2637 n_elif = NCH(n) - 4;
2638 /* must reference the child n_elif+1 since 'else' token is third,
2639 not fourth, child from the end. */
2640 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2641 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2642 has_else = 1;
2643 n_elif -= 3;
2644 }
2645 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002647 if (has_else) {
2648 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002650 orelse = asdl_seq_new(1, c->c_arena);
2651 if (!orelse)
2652 return NULL;
2653 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2654 if (!expression)
2655 return NULL;
2656 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2657 if (!suite_seq)
2658 return NULL;
2659 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2660 if (!suite_seq2)
2661 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002663 asdl_seq_SET(orelse, 0,
2664 If(expression, suite_seq, suite_seq2,
2665 LINENO(CHILD(n, NCH(n) - 6)),
2666 CHILD(n, NCH(n) - 6)->n_col_offset,
2667 c->c_arena));
2668 /* the just-created orelse handled the last elif */
2669 n_elif--;
2670 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002672 for (i = 0; i < n_elif; i++) {
2673 int off = 5 + (n_elif - i - 1) * 4;
2674 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2675 if (!newobj)
2676 return NULL;
2677 expression = ast_for_expr(c, CHILD(n, off));
2678 if (!expression)
2679 return NULL;
2680 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2681 if (!suite_seq)
2682 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002684 asdl_seq_SET(newobj, 0,
2685 If(expression, suite_seq, orelse,
2686 LINENO(CHILD(n, off)),
2687 CHILD(n, off)->n_col_offset, c->c_arena));
2688 orelse = newobj;
2689 }
2690 expression = ast_for_expr(c, CHILD(n, 1));
2691 if (!expression)
2692 return NULL;
2693 suite_seq = ast_for_suite(c, CHILD(n, 3));
2694 if (!suite_seq)
2695 return NULL;
2696 return If(expression, suite_seq, orelse,
2697 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002699
2700 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002701 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002702 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703}
2704
2705static stmt_ty
2706ast_for_while_stmt(struct compiling *c, const node *n)
2707{
2708 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2709 REQ(n, while_stmt);
2710
2711 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002712 expr_ty expression;
2713 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002715 expression = ast_for_expr(c, CHILD(n, 1));
2716 if (!expression)
2717 return NULL;
2718 suite_seq = ast_for_suite(c, CHILD(n, 3));
2719 if (!suite_seq)
2720 return NULL;
2721 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2722 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 }
2724 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002725 expr_ty expression;
2726 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002728 expression = ast_for_expr(c, CHILD(n, 1));
2729 if (!expression)
2730 return NULL;
2731 seq1 = ast_for_suite(c, CHILD(n, 3));
2732 if (!seq1)
2733 return NULL;
2734 seq2 = ast_for_suite(c, CHILD(n, 6));
2735 if (!seq2)
2736 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002738 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2739 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002741
2742 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002743 "wrong number of tokens for 'while' statement: %d",
2744 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002745 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746}
2747
2748static stmt_ty
2749ast_for_for_stmt(struct compiling *c, const node *n)
2750{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002751 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 expr_ty expression;
2753 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002754 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2756 REQ(n, for_stmt);
2757
2758 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002759 seq = ast_for_suite(c, CHILD(n, 8));
2760 if (!seq)
2761 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762 }
2763
Neal Norwitzedef2be2006-07-12 05:26:17 +00002764 node_target = CHILD(n, 1);
2765 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002766 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002767 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002768 /* Check the # of children rather than the length of _target, since
2769 for x, in ... has 1 element in _target, but still requires a Tuple. */
2770 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002771 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002773 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002775 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002776 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002779 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002782 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002783 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784}
2785
2786static excepthandler_ty
2787ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2788{
Collin Winter62903052007-05-18 23:11:24 +00002789 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 REQ(exc, except_clause);
2791 REQ(body, suite);
2792
2793 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002794 asdl_seq *suite_seq = ast_for_suite(c, body);
2795 if (!suite_seq)
2796 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002798 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2799 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 }
2801 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002802 expr_ty expression;
2803 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002805 expression = ast_for_expr(c, CHILD(exc, 1));
2806 if (!expression)
2807 return NULL;
2808 suite_seq = ast_for_suite(c, body);
2809 if (!suite_seq)
2810 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002812 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2813 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 }
2815 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002816 asdl_seq *suite_seq;
2817 expr_ty expression;
2818 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2819 if (!e)
2820 return NULL;
2821 if (!set_context(e, Store, CHILD(exc, 3)))
2822 return NULL;
2823 expression = ast_for_expr(c, CHILD(exc, 1));
2824 if (!expression)
2825 return NULL;
2826 suite_seq = ast_for_suite(c, body);
2827 if (!suite_seq)
2828 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002830 return excepthandler(expression, e, suite_seq, LINENO(exc),
2831 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002833
2834 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002835 "wrong number of children for 'except' clause: %d",
2836 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002837 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838}
2839
2840static stmt_ty
2841ast_for_try_stmt(struct compiling *c, const node *n)
2842{
Neal Norwitzf599f422005-12-17 21:33:47 +00002843 const int nch = NCH(n);
2844 int n_except = (nch - 3)/3;
2845 asdl_seq *body, *orelse = NULL, *finally = NULL;
2846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 REQ(n, try_stmt);
2848
Neal Norwitzf599f422005-12-17 21:33:47 +00002849 body = ast_for_suite(c, CHILD(n, 2));
2850 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002851 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852
Neal Norwitzf599f422005-12-17 21:33:47 +00002853 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002854 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2855 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2856 /* we can assume it's an "else",
2857 because nch >= 9 for try-else-finally and
2858 it would otherwise have a type of except_clause */
2859 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2860 if (orelse == NULL)
2861 return NULL;
2862 n_except--;
2863 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002865 finally = ast_for_suite(c, CHILD(n, nch - 1));
2866 if (finally == NULL)
2867 return NULL;
2868 n_except--;
2869 }
2870 else {
2871 /* we can assume it's an "else",
2872 otherwise it would have a type of except_clause */
2873 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2874 if (orelse == NULL)
2875 return NULL;
2876 n_except--;
2877 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002879 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002880 ast_error(n, "malformed 'try' statement");
2881 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002883
2884 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002885 int i;
2886 stmt_ty except_st;
2887 /* process except statements to create a try ... except */
2888 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2889 if (handlers == NULL)
2890 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002891
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002892 for (i = 0; i < n_except; i++) {
2893 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2894 CHILD(n, 5 + i * 3));
2895 if (!e)
2896 return NULL;
2897 asdl_seq_SET(handlers, i, e);
2898 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002899
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002900 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2901 n->n_col_offset, c->c_arena);
2902 if (!finally)
2903 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002904
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002905 /* if a 'finally' is present too, we nest the TryExcept within a
2906 TryFinally to emulate try ... except ... finally */
2907 body = asdl_seq_new(1, c->c_arena);
2908 if (body == NULL)
2909 return NULL;
2910 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002911 }
2912
2913 /* must be a try ... finally (except clauses are in body, if any exist) */
2914 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002915 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916}
2917
Guido van Rossumc2e20742006-02-27 22:32:47 +00002918static expr_ty
2919ast_for_with_var(struct compiling *c, const node *n)
2920{
2921 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002922 return ast_for_expr(c, CHILD(n, 1));
2923}
2924
2925/* with_stmt: 'with' test [ with_var ] ':' suite */
2926static stmt_ty
2927ast_for_with_stmt(struct compiling *c, const node *n)
2928{
2929 expr_ty context_expr, optional_vars = NULL;
2930 int suite_index = 3; /* skip 'with', test, and ':' */
2931 asdl_seq *suite_seq;
2932
2933 assert(TYPE(n) == with_stmt);
2934 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002935 if (!context_expr)
2936 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002937 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002938 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002939
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002940 if (!optional_vars) {
2941 return NULL;
2942 }
2943 if (!set_context(optional_vars, Store, n)) {
2944 return NULL;
2945 }
2946 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002947 }
2948
2949 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2950 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002951 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002952 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002953 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002954 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002955}
2956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957static stmt_ty
2958ast_for_classdef(struct compiling *c, const node *n)
2959{
2960 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961 asdl_seq *bases, *s;
2962
2963 REQ(n, classdef);
2964
2965 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002966 ast_error(n, "assignment to None");
2967 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 }
2969
2970 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002971 s = ast_for_suite(c, CHILD(n, 3));
2972 if (!s)
2973 return NULL;
2974 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2975 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 }
2977 /* check for empty base list */
2978 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002979 s = ast_for_suite(c, CHILD(n,5));
2980 if (!s)
2981 return NULL;
2982 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2983 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 }
2985
2986 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002987 bases = ast_for_class_bases(c, CHILD(n, 3));
2988 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990
2991 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002992 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002993 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002994 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002995 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996}
2997
2998static stmt_ty
2999ast_for_stmt(struct compiling *c, const node *n)
3000{
3001 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003002 assert(NCH(n) == 1);
3003 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004 }
3005 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003006 assert(num_stmts(n) == 1);
3007 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008 }
3009 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003010 REQ(n, small_stmt);
3011 n = CHILD(n, 0);
3012 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3013 | flow_stmt | import_stmt | global_stmt | exec_stmt
3014 | assert_stmt
3015 */
3016 switch (TYPE(n)) {
3017 case expr_stmt:
3018 return ast_for_expr_stmt(c, n);
3019 case print_stmt:
3020 return ast_for_print_stmt(c, n);
3021 case del_stmt:
3022 return ast_for_del_stmt(c, n);
3023 case pass_stmt:
3024 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3025 case flow_stmt:
3026 return ast_for_flow_stmt(c, n);
3027 case import_stmt:
3028 return ast_for_import_stmt(c, n);
3029 case global_stmt:
3030 return ast_for_global_stmt(c, n);
3031 case exec_stmt:
3032 return ast_for_exec_stmt(c, n);
3033 case assert_stmt:
3034 return ast_for_assert_stmt(c, n);
3035 default:
3036 PyErr_Format(PyExc_SystemError,
3037 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3038 TYPE(n), NCH(n));
3039 return NULL;
3040 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 }
3042 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003043 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3044 | funcdef | classdef
3045 */
3046 node *ch = CHILD(n, 0);
3047 REQ(n, compound_stmt);
3048 switch (TYPE(ch)) {
3049 case if_stmt:
3050 return ast_for_if_stmt(c, ch);
3051 case while_stmt:
3052 return ast_for_while_stmt(c, ch);
3053 case for_stmt:
3054 return ast_for_for_stmt(c, ch);
3055 case try_stmt:
3056 return ast_for_try_stmt(c, ch);
3057 case with_stmt:
3058 return ast_for_with_stmt(c, ch);
3059 case funcdef:
3060 return ast_for_funcdef(c, ch);
3061 case classdef:
3062 return ast_for_classdef(c, ch);
3063 default:
3064 PyErr_Format(PyExc_SystemError,
3065 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3066 TYPE(n), NCH(n));
3067 return NULL;
3068 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003069 }
3070}
3071
3072static PyObject *
3073parsenumber(const char *s)
3074{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003075 const char *end;
3076 long x;
3077 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003079 Py_complex c;
3080 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081#endif
3082
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003083 errno = 0;
3084 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003086 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003088 if (*end == 'l' || *end == 'L')
3089 return PyLong_FromString((char *)s, (char **)0, 0);
3090 if (s[0] == '0') {
3091 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3092 if (x < 0 && errno == 0) {
3093 return PyLong_FromString((char *)s,
3094 (char **)0,
3095 0);
3096 }
3097 }
3098 else
3099 x = PyOS_strtol((char *)s, (char **)&end, 0);
3100 if (*end == '\0') {
3101 if (errno != 0)
3102 return PyLong_FromString((char *)s, (char **)0, 0);
3103 return PyInt_FromLong(x);
3104 }
3105 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003107 if (imflag) {
3108 c.real = 0.;
3109 PyFPE_START_PROTECT("atof", return 0)
3110 c.imag = PyOS_ascii_atof(s);
3111 PyFPE_END_PROTECT(c)
3112 return PyComplex_FromCComplex(c);
3113 }
3114 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003116 {
3117 PyFPE_START_PROTECT("atof", return 0)
3118 dx = PyOS_ascii_atof(s);
3119 PyFPE_END_PROTECT(dx)
3120 return PyFloat_FromDouble(dx);
3121 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122}
3123
3124static PyObject *
3125decode_utf8(const char **sPtr, const char *end, char* encoding)
3126{
3127#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003128 Py_FatalError("decode_utf8 should not be called in this build.");
3129 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003131 PyObject *u, *v;
3132 char *s, *t;
3133 t = s = (char *)*sPtr;
3134 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3135 while (s < end && (*s & 0x80)) s++;
3136 *sPtr = s;
3137 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3138 if (u == NULL)
3139 return NULL;
3140 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3141 Py_DECREF(u);
3142 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143#endif
3144}
3145
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003146#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147static PyObject *
3148decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3149{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003150 PyObject *v, *u;
3151 char *buf;
3152 char *p;
3153 const char *end;
3154 if (encoding == NULL) {
3155 buf = (char *)s;
3156 u = NULL;
3157 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3158 buf = (char *)s;
3159 u = NULL;
3160 } else {
3161 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3162 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3163 if (u == NULL)
3164 return NULL;
3165 p = buf = PyString_AsString(u);
3166 end = s + len;
3167 while (s < end) {
3168 if (*s == '\\') {
3169 *p++ = *s++;
3170 if (*s & 0x80) {
3171 strcpy(p, "u005c");
3172 p += 5;
3173 }
3174 }
3175 if (*s & 0x80) { /* XXX inefficient */
3176 PyObject *w;
3177 char *r;
3178 Py_ssize_t rn, i;
3179 w = decode_utf8(&s, end, "utf-16-be");
3180 if (w == NULL) {
3181 Py_DECREF(u);
3182 return NULL;
3183 }
3184 r = PyString_AsString(w);
3185 rn = PyString_Size(w);
3186 assert(rn % 2 == 0);
3187 for (i = 0; i < rn; i += 2) {
3188 sprintf(p, "\\u%02x%02x",
3189 r[i + 0] & 0xFF,
3190 r[i + 1] & 0xFF);
3191 p += 6;
3192 }
3193 Py_DECREF(w);
3194 } else {
3195 *p++ = *s++;
3196 }
3197 }
3198 len = p - buf;
3199 s = buf;
3200 }
3201 if (rawmode)
3202 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3203 else
3204 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3205 Py_XDECREF(u);
3206 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003208#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209
3210/* s is a Python string literal, including the bracketing quote characters,
3211 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3212 * parsestr parses it, and returns the decoded Python string object.
3213 */
3214static PyObject *
3215parsestr(const char *s, const char *encoding)
3216{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003217 size_t len;
3218 int quote = Py_CHARMASK(*s);
3219 int rawmode = 0;
3220 int need_encoding;
3221 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003223 if (isalpha(quote) || quote == '_') {
3224 if (quote == 'u' || quote == 'U') {
3225 quote = *++s;
3226 unicode = 1;
3227 }
3228 if (quote == 'r' || quote == 'R') {
3229 quote = *++s;
3230 rawmode = 1;
3231 }
3232 }
3233 if (quote != '\'' && quote != '\"') {
3234 PyErr_BadInternalCall();
3235 return NULL;
3236 }
3237 s++;
3238 len = strlen(s);
3239 if (len > INT_MAX) {
3240 PyErr_SetString(PyExc_OverflowError,
3241 "string to parse is too long");
3242 return NULL;
3243 }
3244 if (s[--len] != quote) {
3245 PyErr_BadInternalCall();
3246 return NULL;
3247 }
3248 if (len >= 4 && s[0] == quote && s[1] == quote) {
3249 s += 2;
3250 len -= 2;
3251 if (s[--len] != quote || s[--len] != quote) {
3252 PyErr_BadInternalCall();
3253 return NULL;
3254 }
3255 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003257 if (unicode || Py_UnicodeFlag) {
3258 return decode_unicode(s, len, rawmode, encoding);
3259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003261 need_encoding = (encoding != NULL &&
3262 strcmp(encoding, "utf-8") != 0 &&
3263 strcmp(encoding, "iso-8859-1") != 0);
3264 if (rawmode || strchr(s, '\\') == NULL) {
3265 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003267 /* This should not happen - we never see any other
3268 encoding. */
3269 Py_FatalError(
3270 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003272 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3273 if (u == NULL)
3274 return NULL;
3275 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3276 Py_DECREF(u);
3277 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003279 } else {
3280 return PyString_FromStringAndSize(s, len);
3281 }
3282 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003284 return PyString_DecodeEscape(s, len, NULL, unicode,
3285 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286}
3287
3288/* Build a Python string object out of a STRING atom. This takes care of
3289 * compile-time literal catenation, calling parsestr() on each piece, and
3290 * pasting the intermediate results together.
3291 */
3292static PyObject *
3293parsestrplus(struct compiling *c, const node *n)
3294{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003295 PyObject *v;
3296 int i;
3297 REQ(CHILD(n, 0), STRING);
3298 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3299 /* String literal concatenation */
3300 for (i = 1; i < NCH(n); i++) {
3301 PyObject *s;
3302 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3303 if (s == NULL)
3304 goto onError;
3305 if (PyString_Check(v) && PyString_Check(s)) {
3306 PyString_ConcatAndDel(&v, s);
3307 if (v == NULL)
3308 goto onError;
3309 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003311 else {
3312 PyObject *temp = PyUnicode_Concat(v, s);
3313 Py_DECREF(s);
3314 Py_DECREF(v);
3315 v = temp;
3316 if (v == NULL)
3317 goto onError;
3318 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003320 }
3321 }
3322 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323
3324 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003325 Py_XDECREF(v);
3326 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327}