blob: 6551063a7b8734fbf3f22e9645f251c85d4b388d [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* XXX TO DO
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
21 - syntax errors
22*/
23
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024/* Data structure used internally */
25struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000026 char *c_encoding; /* source encoding */
27 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028};
29
30static asdl_seq *seq_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_expr(struct compiling *, const node *);
32static stmt_ty ast_for_stmt(struct compiling *, const node *);
33static asdl_seq *ast_for_suite(struct compiling *, const node *);
Martin v. Löwis28457502006-04-11 09:17:27 +000034static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Georg Brandl7784f122006-05-26 20:04:44 +0000110 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Neal Norwitze98ccf62006-03-23 05:39:47 +0000194 if (TYPE(n) == encoding_decl) {
195 ast_error(n, "encoding declaration in Unicode string");
196 goto error;
197 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 } else if (TYPE(n) == encoding_decl) {
199 c.c_encoding = STR(n);
200 n = CHILD(n, 0);
201 } else {
202 c.c_encoding = NULL;
203 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000204 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Jeremy Hyltona8293132006-02-28 17:58:27 +0000206 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 switch (TYPE(n)) {
208 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000209 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 if (!stmts)
211 return NULL;
212 for (i = 0; i < NCH(n) - 1; i++) {
213 ch = CHILD(n, i);
214 if (TYPE(ch) == NEWLINE)
215 continue;
216 REQ(ch, stmt);
217 num = num_stmts(ch);
218 if (num == 1) {
219 s = ast_for_stmt(&c, ch);
220 if (!s)
221 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000222 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223 }
224 else {
225 ch = CHILD(ch, 0);
226 REQ(ch, simple_stmt);
227 for (j = 0; j < num; j++) {
228 s = ast_for_stmt(&c, CHILD(ch, j * 2));
229 if (!s)
230 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000231 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 }
233 }
234 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000235 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 case eval_input: {
237 expr_ty testlist_ast;
238
239 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000240 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241 if (!testlist_ast)
242 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 }
245 case single_input:
246 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 if (!stmts)
249 goto error;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
251 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 }
254 else {
255 n = CHILD(n, 0);
256 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000257 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 if (!stmts)
259 goto error;
260 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000261 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262 if (!s)
263 goto error;
264 asdl_seq_SET(stmts, 0, s);
265 }
266 else {
267 /* Only a simple_stmt can contain multiple statements. */
268 REQ(n, simple_stmt);
269 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 if (TYPE(CHILD(n, i)) == NEWLINE)
271 break;
272 s = ast_for_stmt(&c, CHILD(n, i));
273 if (!s)
274 goto error;
275 asdl_seq_SET(stmts, i / 2, s);
276 }
277 }
278
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000279 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 }
281 default:
282 goto error;
283 }
284 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 ast_error_finish(filename);
286 return NULL;
287}
288
289/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
290*/
291
292static operator_ty
293get_operator(const node *n)
294{
295 switch (TYPE(n)) {
296 case VBAR:
297 return BitOr;
298 case CIRCUMFLEX:
299 return BitXor;
300 case AMPER:
301 return BitAnd;
302 case LEFTSHIFT:
303 return LShift;
304 case RIGHTSHIFT:
305 return RShift;
306 case PLUS:
307 return Add;
308 case MINUS:
309 return Sub;
310 case STAR:
311 return Mult;
312 case SLASH:
313 return Div;
314 case DOUBLESLASH:
315 return FloorDiv;
316 case PERCENT:
317 return Mod;
318 default:
Martin v. Löwis28457502006-04-11 09:17:27 +0000319 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 }
321}
322
Jeremy Hyltona8293132006-02-28 17:58:27 +0000323/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324
325 Only sets context for expr kinds that "can appear in assignment context"
326 (according to ../Parser/Python.asdl). For other expr kinds, it sets
327 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328*/
329
330static int
331set_context(expr_ty e, expr_context_ty ctx, const node *n)
332{
333 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334 /* If a particular expression type can't be used for assign / delete,
335 set expr_name to its name and an error message will be generated.
336 */
337 const char* expr_name = NULL;
338
339 /* The ast defines augmented store and load contexts, but the
340 implementation here doesn't actually use them. The code may be
341 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000342 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000343 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000344 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345 */
346 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
348 switch (e->kind) {
349 case Attribute_kind:
350 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000351 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 return ast_error(n, "assignment to None");
353 }
354 e->v.Attribute.ctx = ctx;
355 break;
356 case Subscript_kind:
357 e->v.Subscript.ctx = ctx;
358 break;
359 case Name_kind:
360 if (ctx == Store &&
361 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
362 return ast_error(n, "assignment to None");
363 }
364 e->v.Name.ctx = ctx;
365 break;
366 case List_kind:
367 e->v.List.ctx = ctx;
368 s = e->v.List.elts;
369 break;
370 case Tuple_kind:
371 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
372 return ast_error(n, "can't assign to ()");
373 e->v.Tuple.ctx = ctx;
374 s = e->v.Tuple.elts;
375 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 case Lambda_kind:
377 expr_name = "lambda";
378 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000380 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000384 case UnaryOp_kind:
385 expr_name = "operator";
386 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000388 expr_name = "generator expression";
389 break;
Neal Norwitz0d62a062006-07-30 06:53:31 +0000390 case Yield_kind:
391 expr_name = "yield expression";
392 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000393 case ListComp_kind:
394 expr_name = "list comprehension";
395 break;
396 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 case Num_kind:
398 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000399 expr_name = "literal";
400 break;
401 case Compare_kind:
402 expr_name = "comparison";
403 break;
404 case Repr_kind:
405 expr_name = "repr";
406 break;
Neal Norwitz373f0a72006-05-15 07:04:36 +0000407 case IfExp_kind:
408 expr_name = "conditional expression";
409 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000410 default:
411 PyErr_Format(PyExc_SystemError,
412 "unexpected expression in assignment %d (line %d)",
413 e->kind, e->lineno);
414 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000416 /* Check for error string set by switch */
417 if (expr_name) {
418 char buf[300];
419 PyOS_snprintf(buf, sizeof(buf),
420 "can't %s %s",
421 ctx == Store ? "assign to" : "delete",
422 expr_name);
423 return ast_error(n, buf);
424 }
425
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000427 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 */
429 if (s) {
430 int i;
431
432 for (i = 0; i < asdl_seq_LEN(s); i++) {
Martin v. Löwis28457502006-04-11 09:17:27 +0000433 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434 return 0;
435 }
436 }
437 return 1;
438}
439
440static operator_ty
441ast_for_augassign(const node *n)
442{
443 REQ(n, augassign);
444 n = CHILD(n, 0);
445 switch (STR(n)[0]) {
446 case '+':
447 return Add;
448 case '-':
449 return Sub;
450 case '/':
451 if (STR(n)[1] == '/')
452 return FloorDiv;
453 else
454 return Div;
455 case '%':
456 return Mod;
457 case '<':
458 return LShift;
459 case '>':
460 return RShift;
461 case '&':
462 return BitAnd;
463 case '^':
464 return BitXor;
465 case '|':
466 return BitOr;
467 case '*':
468 if (STR(n)[1] == '*')
469 return Pow;
470 else
471 return Mult;
472 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000473 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000474 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 }
476}
477
478static cmpop_ty
479ast_for_comp_op(const node *n)
480{
481 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
482 |'is' 'not'
483 */
484 REQ(n, comp_op);
485 if (NCH(n) == 1) {
486 n = CHILD(n, 0);
487 switch (TYPE(n)) {
488 case LESS:
489 return Lt;
490 case GREATER:
491 return Gt;
492 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 return Eq;
494 case LESSEQUAL:
495 return LtE;
496 case GREATEREQUAL:
497 return GtE;
498 case NOTEQUAL:
499 return NotEq;
500 case NAME:
501 if (strcmp(STR(n), "in") == 0)
502 return In;
503 if (strcmp(STR(n), "is") == 0)
504 return Is;
505 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000506 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507 STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000508 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 }
510 }
511 else if (NCH(n) == 2) {
512 /* handle "not in" and "is not" */
513 switch (TYPE(CHILD(n, 0))) {
514 case NAME:
515 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
516 return NotIn;
517 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
518 return IsNot;
519 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000520 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Martin v. Löwis28457502006-04-11 09:17:27 +0000522 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 }
524 }
Neal Norwitz79792652005-11-14 04:25:03 +0000525 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000527 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528}
529
530static asdl_seq *
531seq_for_testlist(struct compiling *c, const node *n)
532{
533 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000534 asdl_seq *seq;
535 expr_ty expression;
536 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 assert(TYPE(n) == testlist
538 || TYPE(n) == listmaker
539 || TYPE(n) == testlist_gexp
540 || TYPE(n) == testlist_safe
Neal Norwitza3ce6aa2006-11-04 19:32:54 +0000541 || TYPE(n) == testlist1
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000544 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 if (!seq)
546 return NULL;
547
548 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000549 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
551 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000552 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554
555 assert(i / 2 < seq->size);
556 asdl_seq_SET(seq, i / 2, expression);
557 }
558 return seq;
559}
560
561static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000562compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563{
564 int i, len = (NCH(n) + 1) / 2;
565 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000566 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 if (!args)
568 return NULL;
569
Georg Brandlc57221e2006-09-25 07:04:10 +0000570 /* fpdef: NAME | '(' fplist ')'
571 fplist: fpdef (',' fpdef)* [',']
572 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 for (i = 0; i < len; i++) {
Georg Brandlc57221e2006-09-25 07:04:10 +0000575 const node *fpdef_node = CHILD(n, 2*i);
576 const node *child;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 expr_ty arg;
Georg Brandlc57221e2006-09-25 07:04:10 +0000578set_name:
579 /* fpdef_node is either a NAME or an fplist */
580 child = CHILD(fpdef_node, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000582 if (!strcmp(STR(child), "None")) {
583 ast_error(child, "assignment to None");
584 return NULL;
585 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000586 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
587 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000588 }
589 else {
Georg Brandlc57221e2006-09-25 07:04:10 +0000590 assert(TYPE(fpdef_node) == fpdef);
591 /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */
592 child = CHILD(fpdef_node, 1);
593 assert(TYPE(child) == fplist);
594 /* NCH == 1 means we have (x), we need to elide the extra parens */
595 if (NCH(child) == 1) {
596 fpdef_node = CHILD(child, 0);
597 assert(TYPE(fpdef_node) == fpdef);
598 goto set_name;
599 }
600 arg = compiler_complex_args(c, child);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000601 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 asdl_seq_SET(args, i, arg);
603 }
604
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000605 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000606 if (!set_context(result, Store, n))
607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 return result;
609}
610
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
Jeremy Hyltona8293132006-02-28 17:58:27 +0000612/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
614static arguments_ty
615ast_for_arguments(struct compiling *c, const node *n)
616{
617 /* parameters: '(' [varargslist] ')'
618 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
619 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
620 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000621 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 asdl_seq *args, *defaults;
623 identifier vararg = NULL, kwarg = NULL;
624 node *ch;
625
626 if (TYPE(n) == parameters) {
627 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000628 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629 n = CHILD(n, 1);
630 }
631 REQ(n, varargslist);
632
633 /* first count the number of normal args & defaults */
634 for (i = 0; i < NCH(n); i++) {
635 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000636 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 if (TYPE(ch) == EQUAL)
639 n_defaults++;
640 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000641 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 if (!args && n_args)
Neal Norwitzc173b482006-07-30 19:18:13 +0000643 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000644 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!defaults && n_defaults)
Neal Norwitzc173b482006-07-30 19:18:13 +0000646 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647
648 /* fpdef: NAME | '(' fplist ')'
649 fplist: fpdef (',' fpdef)* [',']
650 */
651 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000652 j = 0; /* index for defaults */
653 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 while (i < NCH(n)) {
655 ch = CHILD(n, i);
656 switch (TYPE(ch)) {
657 case fpdef:
Georg Brandlc57221e2006-09-25 07:04:10 +0000658 handle_fpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
660 anything other than EQUAL or a comma? */
661 /* XXX Should NCH(n) check be made a separate check? */
662 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000663 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
664 if (!expression)
665 goto error;
Neal Norwitzc173b482006-07-30 19:18:13 +0000666 assert(defaults != NULL);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000667 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 i += 2;
669 found_default = 1;
670 }
671 else if (found_default) {
672 ast_error(n,
673 "non-default argument follows default argument");
674 goto error;
675 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000677 ch = CHILD(ch, 1);
678 /* def foo((x)): is not complex, special case. */
679 if (NCH(ch) != 1) {
680 /* We have complex arguments, setup for unpacking. */
681 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
682 } else {
683 /* def foo((x)): setup for checking NAME below. */
Georg Brandlc57221e2006-09-25 07:04:10 +0000684 /* Loop because there can be many parens and tuple
685 upacking mixed in. */
Neal Norwitz33b730e2006-03-27 08:58:23 +0000686 ch = CHILD(ch, 0);
Georg Brandlc57221e2006-09-25 07:04:10 +0000687 assert(TYPE(ch) == fpdef);
688 goto handle_fpdef;
Neal Norwitz33b730e2006-03-27 08:58:23 +0000689 }
690 }
691 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000692 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
694 ast_error(CHILD(ch, 0), "assignment to None");
695 goto error;
696 }
Armin Rigo31441302005-10-21 12:57:31 +0000697 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000698 Param, LINENO(ch), ch->n_col_offset,
699 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 if (!name)
701 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000702 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
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:
Neal Norwitz79792652005-11-14 04:25:03 +0000724 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725 "unexpected node in varargslist: %d @ %d",
726 TYPE(ch), i);
727 goto error;
728 }
729 }
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)
Neal Norwitzadb69fc2005-12-17 20:54:49 +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)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000757 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
759 for (i = 2; i < NCH(n); i+=2) {
760 id = NEW_IDENTIFIER(CHILD(n, i));
761 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000762 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000763 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000764 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000765 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)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
786 if (NCH(n) == 3) { /* No arguments */
787 d = name_expr;
788 name_expr = NULL;
789 }
790 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000791 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
792 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 name_expr = NULL;
796 }
797 else {
798 d = ast_for_call(c, CHILD(n, 3), name_expr);
799 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000800 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 name_expr = NULL;
802 }
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)
817 return NULL;
818
819 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +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 */
841 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
842 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 name_i = 2;
845 }
846 else {
847 name_i = 1;
848 }
849
850 name = NEW_IDENTIFIER(CHILD(n, name_i));
851 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000854 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 }
857 args = ast_for_arguments(c, CHILD(n, name_i + 1));
858 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 body = ast_for_suite(c, CHILD(n, name_i + 3));
861 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +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),
865 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) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000876 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 if (!args)
878 return NULL;
879 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000880 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 }
883 else {
884 args = ast_for_arguments(c, CHILD(n, 1));
885 if (!args)
886 return NULL;
887 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000888 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889 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)
904 return NULL;
905 expression = ast_for_expr(c, CHILD(n, 2));
906 if (!expression)
907 return NULL;
908 orelse = ast_for_expr(c, CHILD(n, 4));
909 if (!orelse)
910 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000911 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
912 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000913}
914
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915/* Count the number of 'for' loop in a list comprehension.
916
917 Helper for ast_for_listcomp().
918*/
919
920static int
921count_list_fors(const node *n)
922{
923 int n_fors = 0;
924 node *ch = CHILD(n, 1);
925
926 count_list_for:
927 n_fors++;
928 REQ(ch, list_for);
929 if (NCH(ch) == 5)
930 ch = CHILD(ch, 4);
931 else
932 return n_fors;
933 count_list_iter:
934 REQ(ch, list_iter);
935 ch = CHILD(ch, 0);
936 if (TYPE(ch) == list_for)
937 goto count_list_for;
938 else if (TYPE(ch) == list_if) {
939 if (NCH(ch) == 3) {
940 ch = CHILD(ch, 2);
941 goto count_list_iter;
942 }
943 else
944 return n_fors;
945 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000946
947 /* Should never be reached */
948 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
949 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950}
951
952/* Count the number of 'if' statements in a list comprehension.
953
954 Helper for ast_for_listcomp().
955*/
956
957static int
958count_list_ifs(const node *n)
959{
960 int n_ifs = 0;
961
962 count_list_iter:
963 REQ(n, list_iter);
964 if (TYPE(CHILD(n, 0)) == list_for)
965 return n_ifs;
966 n = CHILD(n, 0);
967 REQ(n, list_if);
968 n_ifs++;
969 if (NCH(n) == 2)
970 return n_ifs;
971 n = CHILD(n, 2);
972 goto count_list_iter;
973}
974
975static expr_ty
976ast_for_listcomp(struct compiling *c, const node *n)
977{
978 /* listmaker: test ( list_for | (',' test)* [','] )
979 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
980 list_iter: list_for | list_if
981 list_if: 'if' test [list_iter]
982 testlist_safe: test [(',' test)+ [',']]
983 */
984 expr_ty elt;
985 asdl_seq *listcomps;
986 int i, n_fors;
987 node *ch;
988
989 REQ(n, listmaker);
990 assert(NCH(n) > 1);
991
992 elt = ast_for_expr(c, CHILD(n, 0));
993 if (!elt)
994 return NULL;
995
996 n_fors = count_list_fors(n);
997 if (n_fors == -1)
998 return NULL;
999
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001000 listcomps = asdl_seq_new(n_fors, c->c_arena);
1001 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001003
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 ch = CHILD(n, 1);
1005 for (i = 0; i < n_fors; i++) {
1006 comprehension_ty lc;
1007 asdl_seq *t;
1008 expr_ty expression;
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001009 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010
1011 REQ(ch, list_for);
1012
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001013 for_ch = CHILD(ch, 1);
1014 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001015 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001017 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001018 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001021 /* Check the # of children rather than the length of t, since
1022 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
1023 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001024 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001025 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001027 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1028 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001029 expression, NULL, c->c_arena);
1030 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032
1033 if (NCH(ch) == 5) {
1034 int j, n_ifs;
1035 asdl_seq *ifs;
1036
1037 ch = CHILD(ch, 4);
1038 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001039 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001042 ifs = asdl_seq_new(n_ifs, c->c_arena);
1043 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045
1046 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001047 REQ(ch, list_iter);
1048 ch = CHILD(ch, 0);
1049 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050
Jeremy Hyltona8293132006-02-28 17:58:27 +00001051 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1052 if (NCH(ch) == 3)
1053 ch = CHILD(ch, 2);
1054 }
1055 /* on exit, must guarantee that ch is a list_for */
1056 if (TYPE(ch) == list_iter)
1057 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001059 }
1060 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 }
1062
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001063 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064}
1065
1066/*
1067 Count the number of 'for' loops in a generator expression.
1068
1069 Helper for ast_for_genexp().
1070*/
1071
1072static int
1073count_gen_fors(const node *n)
1074{
1075 int n_fors = 0;
1076 node *ch = CHILD(n, 1);
1077
1078 count_gen_for:
1079 n_fors++;
1080 REQ(ch, gen_for);
1081 if (NCH(ch) == 5)
1082 ch = CHILD(ch, 4);
1083 else
1084 return n_fors;
1085 count_gen_iter:
1086 REQ(ch, gen_iter);
1087 ch = CHILD(ch, 0);
1088 if (TYPE(ch) == gen_for)
1089 goto count_gen_for;
1090 else if (TYPE(ch) == gen_if) {
1091 if (NCH(ch) == 3) {
1092 ch = CHILD(ch, 2);
1093 goto count_gen_iter;
1094 }
1095 else
1096 return n_fors;
1097 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001098
1099 /* Should never be reached */
1100 PyErr_SetString(PyExc_SystemError,
1101 "logic error in count_gen_fors");
1102 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103}
1104
1105/* Count the number of 'if' statements in a generator expression.
1106
1107 Helper for ast_for_genexp().
1108*/
1109
1110static int
1111count_gen_ifs(const node *n)
1112{
1113 int n_ifs = 0;
1114
1115 while (1) {
1116 REQ(n, gen_iter);
1117 if (TYPE(CHILD(n, 0)) == gen_for)
1118 return n_ifs;
1119 n = CHILD(n, 0);
1120 REQ(n, gen_if);
1121 n_ifs++;
1122 if (NCH(n) == 2)
1123 return n_ifs;
1124 n = CHILD(n, 2);
1125 }
1126}
1127
Jeremy Hyltona8293132006-02-28 17:58:27 +00001128/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129static expr_ty
1130ast_for_genexp(struct compiling *c, const node *n)
1131{
1132 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1133 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1134 expr_ty elt;
1135 asdl_seq *genexps;
1136 int i, n_fors;
1137 node *ch;
1138
1139 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1140 assert(NCH(n) > 1);
1141
1142 elt = ast_for_expr(c, CHILD(n, 0));
1143 if (!elt)
1144 return NULL;
1145
1146 n_fors = count_gen_fors(n);
1147 if (n_fors == -1)
1148 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001149
1150 genexps = asdl_seq_new(n_fors, c->c_arena);
1151 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 ch = CHILD(n, 1);
1155 for (i = 0; i < n_fors; i++) {
1156 comprehension_ty ge;
1157 asdl_seq *t;
1158 expr_ty expression;
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001159 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160
1161 REQ(ch, gen_for);
1162
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001163 for_ch = CHILD(ch, 1);
1164 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001165 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001167 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001168 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001170
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001171 /* Check the # of children rather than the length of t, since
1172 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1173 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001174 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001175 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001177 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1178 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001179 expression, NULL, c->c_arena);
1180
1181 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001183
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 if (NCH(ch) == 5) {
1185 int j, n_ifs;
1186 asdl_seq *ifs;
1187
1188 ch = CHILD(ch, 4);
1189 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001190 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001192
1193 ifs = asdl_seq_new(n_ifs, c->c_arena);
1194 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001196
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 for (j = 0; j < n_ifs; j++) {
1198 REQ(ch, gen_iter);
1199 ch = CHILD(ch, 0);
1200 REQ(ch, gen_if);
1201
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001202 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001203 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001204 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001205 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 if (NCH(ch) == 3)
1207 ch = CHILD(ch, 2);
1208 }
1209 /* on exit, must guarantee that ch is a gen_for */
1210 if (TYPE(ch) == gen_iter)
1211 ch = CHILD(ch, 0);
1212 ge->ifs = ifs;
1213 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001214 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 }
1216
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001217 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218}
1219
1220static expr_ty
1221ast_for_atom(struct compiling *c, const node *n)
1222{
1223 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1224 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1225 */
1226 node *ch = CHILD(n, 0);
1227
1228 switch (TYPE(ch)) {
1229 case NAME:
1230 /* All names start in Load context, but may later be
1231 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001232 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 case STRING: {
1234 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 if (!str)
1236 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001237
1238 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001239 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 }
1241 case NUMBER: {
1242 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 if (!pynum)
1244 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001245
1246 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001247 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 }
1249 case LPAR: /* some parenthesized expressions */
1250 ch = CHILD(n, 1);
1251
1252 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001253 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254
1255 if (TYPE(ch) == yield_expr)
1256 return ast_for_expr(c, ch);
1257
1258 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1259 return ast_for_genexp(c, ch);
1260
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001261 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 case LSQB: /* list (or list comprehension) */
1263 ch = CHILD(n, 1);
1264
1265 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001266 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267
1268 REQ(ch, listmaker);
1269 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1270 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 if (!elts)
1272 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001273
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001274 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 }
1276 else
1277 return ast_for_listcomp(c, ch);
1278 case LBRACE: {
1279 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1280 int i, size;
1281 asdl_seq *keys, *values;
1282
1283 ch = CHILD(n, 1);
1284 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001285 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 if (!keys)
1287 return NULL;
1288
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001289 values = asdl_seq_new(size, c->c_arena);
1290 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292
1293 for (i = 0; i < NCH(ch); i += 4) {
1294 expr_ty expression;
1295
1296 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001297 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001299
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001301
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001303 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001305
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 asdl_seq_SET(values, i / 4, expression);
1307 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001308 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 }
1310 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001311 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 if (!expression)
1313 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001314
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001315 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 }
1317 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001318 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319 return NULL;
1320 }
1321}
1322
1323static slice_ty
1324ast_for_slice(struct compiling *c, const node *n)
1325{
1326 node *ch;
1327 expr_ty lower = NULL, upper = NULL, step = NULL;
1328
1329 REQ(n, subscript);
1330
1331 /*
1332 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1333 sliceop: ':' [test]
1334 */
1335 ch = CHILD(n, 0);
1336 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001337 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338
1339 if (NCH(n) == 1 && TYPE(ch) == test) {
1340 /* 'step' variable hold no significance in terms of being used over
1341 other vars */
1342 step = ast_for_expr(c, ch);
1343 if (!step)
1344 return NULL;
1345
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001346 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 }
1348
1349 if (TYPE(ch) == test) {
1350 lower = ast_for_expr(c, ch);
1351 if (!lower)
1352 return NULL;
1353 }
1354
1355 /* If there's an upper bound it's in the second or third position. */
1356 if (TYPE(ch) == COLON) {
1357 if (NCH(n) > 1) {
1358 node *n2 = CHILD(n, 1);
1359
1360 if (TYPE(n2) == test) {
1361 upper = ast_for_expr(c, n2);
1362 if (!upper)
1363 return NULL;
1364 }
1365 }
1366 } else if (NCH(n) > 2) {
1367 node *n2 = CHILD(n, 2);
1368
1369 if (TYPE(n2) == test) {
1370 upper = ast_for_expr(c, n2);
1371 if (!upper)
1372 return NULL;
1373 }
1374 }
1375
1376 ch = CHILD(n, NCH(n) - 1);
1377 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001378 if (NCH(ch) == 1) {
1379 /* No expression, so step is None */
1380 ch = CHILD(ch, 0);
1381 step = Name(new_identifier("None", c->c_arena), Load,
1382 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 if (!step)
1384 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001385 } else {
1386 ch = CHILD(ch, 1);
1387 if (TYPE(ch) == test) {
1388 step = ast_for_expr(c, ch);
1389 if (!step)
1390 return NULL;
1391 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 }
1393 }
1394
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001395 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396}
1397
1398static expr_ty
1399ast_for_binop(struct compiling *c, const node *n)
1400{
1401 /* Must account for a sequence of expressions.
1402 How should A op B op C by represented?
1403 BinOp(BinOp(A, op, B), op, C).
1404 */
1405
1406 int i, nops;
1407 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001408 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409
1410 expr1 = ast_for_expr(c, CHILD(n, 0));
1411 if (!expr1)
1412 return NULL;
1413
1414 expr2 = ast_for_expr(c, CHILD(n, 2));
1415 if (!expr2)
1416 return NULL;
1417
Anthony Baxtera863d332006-04-11 07:43:46 +00001418 newoperator = get_operator(CHILD(n, 1));
1419 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 return NULL;
1421
Anthony Baxtera863d332006-04-11 07:43:46 +00001422 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001423 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 if (!result)
1425 return NULL;
1426
1427 nops = (NCH(n) - 1) / 2;
1428 for (i = 1; i < nops; i++) {
1429 expr_ty tmp_result, tmp;
1430 const node* next_oper = CHILD(n, i * 2 + 1);
1431
Anthony Baxtera863d332006-04-11 07:43:46 +00001432 newoperator = get_operator(next_oper);
1433 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 return NULL;
1435
1436 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1437 if (!tmp)
1438 return NULL;
1439
Anthony Baxtera863d332006-04-11 07:43:46 +00001440 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001441 LINENO(next_oper), next_oper->n_col_offset,
1442 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 if (!tmp)
1444 return NULL;
1445 result = tmp_result;
1446 }
1447 return result;
1448}
1449
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001450static expr_ty
1451ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1452{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001453 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1454 subscriptlist: subscript (',' subscript)* [',']
1455 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1456 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001457 REQ(n, trailer);
1458 if (TYPE(CHILD(n, 0)) == LPAR) {
1459 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001460 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1461 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001462 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001463 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001464 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001465 else if (TYPE(CHILD(n, 0)) == DOT ) {
1466 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001467 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001468 }
1469 else {
1470 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001471 REQ(CHILD(n, 2), RSQB);
1472 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001473 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001474 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1475 if (!slc)
1476 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001477 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1478 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001479 }
1480 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001481 /* The grammar is ambiguous here. The ambiguity is resolved
1482 by treating the sequence as a tuple literal if there are
1483 no slice features.
1484 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001485 int j;
1486 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001487 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001488 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001489 asdl_seq *slices, *elts;
1490 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001491 if (!slices)
1492 return NULL;
1493 for (j = 0; j < NCH(n); j += 2) {
1494 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001495 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001496 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001497 if (slc->kind != Index_kind)
1498 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001499 asdl_seq_SET(slices, j / 2, slc);
1500 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001501 if (!simple) {
1502 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001503 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001504 }
1505 /* extract Index values and put them in a Tuple */
1506 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001507 if (!elts)
1508 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001509 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1510 slc = (slice_ty)asdl_seq_GET(slices, j);
1511 assert(slc->kind == Index_kind && slc->v.Index.value);
1512 asdl_seq_SET(elts, j, slc->v.Index.value);
1513 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001514 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001515 if (!e)
1516 return NULL;
1517 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001518 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001519 }
1520 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001521}
1522
1523static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001524ast_for_factor(struct compiling *c, const node *n)
1525{
1526 node *pfactor, *ppower, *patom, *pnum;
1527 expr_ty expression;
1528
1529 /* If the unary - operator is applied to a constant, don't generate
1530 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1531 constant. The peephole optimizer already does something like
1532 this but it doesn't handle the case where the constant is
1533 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1534 PyLongObject.
1535 */
1536 if (TYPE(CHILD(n, 0)) == MINUS
1537 && NCH(n) == 2
1538 && TYPE((pfactor = CHILD(n, 1))) == factor
1539 && NCH(pfactor) == 1
1540 && TYPE((ppower = CHILD(pfactor, 0))) == power
1541 && NCH(ppower) == 1
1542 && TYPE((patom = CHILD(ppower, 0))) == atom
1543 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1544 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1545 if (s == NULL)
1546 return NULL;
1547 s[0] = '-';
1548 strcpy(s + 1, STR(pnum));
1549 PyObject_FREE(STR(pnum));
1550 STR(pnum) = s;
1551 return ast_for_atom(c, patom);
1552 }
1553
1554 expression = ast_for_expr(c, CHILD(n, 1));
1555 if (!expression)
1556 return NULL;
1557
1558 switch (TYPE(CHILD(n, 0))) {
1559 case PLUS:
1560 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1561 c->c_arena);
1562 case MINUS:
1563 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1564 c->c_arena);
1565 case TILDE:
1566 return UnaryOp(Invert, expression, LINENO(n),
1567 n->n_col_offset, c->c_arena);
1568 }
1569 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1570 TYPE(CHILD(n, 0)));
1571 return NULL;
1572}
1573
1574static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001575ast_for_power(struct compiling *c, const node *n)
1576{
1577 /* power: atom trailer* ('**' factor)*
1578 */
1579 int i;
1580 expr_ty e, tmp;
1581 REQ(n, power);
1582 e = ast_for_atom(c, CHILD(n, 0));
1583 if (!e)
1584 return NULL;
1585 if (NCH(n) == 1)
1586 return e;
1587 for (i = 1; i < NCH(n); i++) {
1588 node *ch = CHILD(n, i);
1589 if (TYPE(ch) != trailer)
1590 break;
1591 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001592 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001593 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001594 tmp->lineno = e->lineno;
1595 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001596 e = tmp;
1597 }
1598 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1599 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001600 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001601 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001602 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001603 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001604 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001605 e = tmp;
1606 }
1607 return e;
1608}
1609
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610/* Do not name a variable 'expr'! Will cause a compile error.
1611*/
1612
1613static expr_ty
1614ast_for_expr(struct compiling *c, const node *n)
1615{
1616 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001617 test: or_test ['if' or_test 'else' test] | lambdef
1618 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619 and_test: not_test ('and' not_test)*
1620 not_test: 'not' not_test | comparison
1621 comparison: expr (comp_op expr)*
1622 expr: xor_expr ('|' xor_expr)*
1623 xor_expr: and_expr ('^' and_expr)*
1624 and_expr: shift_expr ('&' shift_expr)*
1625 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1626 arith_expr: term (('+'|'-') term)*
1627 term: factor (('*'|'/'|'%'|'//') factor)*
1628 factor: ('+'|'-'|'~') factor | power
1629 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001630
1631 As well as modified versions that exist for backward compatibility,
1632 to explicitly allow:
1633 [ x for x in lambda: 0, lambda: 1 ]
1634 (which would be ambiguous without these extra rules)
1635
1636 old_test: or_test | old_lambdef
1637 old_lambdef: 'lambda' [vararglist] ':' old_test
1638
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639 */
1640
1641 asdl_seq *seq;
1642 int i;
1643
1644 loop:
1645 switch (TYPE(n)) {
1646 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001647 case old_test:
1648 if (TYPE(CHILD(n, 0)) == lambdef ||
1649 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001651 else if (NCH(n) > 1)
1652 return ast_for_ifexpr(c, n);
1653 /* Fallthrough */
1654 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 case and_test:
1656 if (NCH(n) == 1) {
1657 n = CHILD(n, 0);
1658 goto loop;
1659 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001660 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 if (!seq)
1662 return NULL;
1663 for (i = 0; i < NCH(n); i += 2) {
1664 expr_ty e = ast_for_expr(c, CHILD(n, i));
1665 if (!e)
1666 return NULL;
1667 asdl_seq_SET(seq, i / 2, e);
1668 }
1669 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001670 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1671 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001672 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001673 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 case not_test:
1675 if (NCH(n) == 1) {
1676 n = CHILD(n, 0);
1677 goto loop;
1678 }
1679 else {
1680 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1681 if (!expression)
1682 return NULL;
1683
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001684 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1685 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 }
1687 case comparison:
1688 if (NCH(n) == 1) {
1689 n = CHILD(n, 0);
1690 goto loop;
1691 }
1692 else {
1693 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001694 asdl_int_seq *ops;
1695 asdl_seq *cmps;
1696 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 if (!ops)
1698 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001699 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 return NULL;
1702 }
1703 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001704 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705
Anthony Baxtera863d332006-04-11 07:43:46 +00001706 newoperator = ast_for_comp_op(CHILD(n, i));
1707 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001709 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710
1711 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001712 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001714 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001716 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 asdl_seq_SET(cmps, i / 2, expression);
1718 }
1719 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001720 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001722 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001724 return Compare(expression, ops, cmps, LINENO(n),
1725 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 }
1727 break;
1728
1729 /* The next five cases all handle BinOps. The main body of code
1730 is the same in each case, but the switch turned inside out to
1731 reuse the code for each type of operator.
1732 */
1733 case expr:
1734 case xor_expr:
1735 case and_expr:
1736 case shift_expr:
1737 case arith_expr:
1738 case term:
1739 if (NCH(n) == 1) {
1740 n = CHILD(n, 0);
1741 goto loop;
1742 }
1743 return ast_for_binop(c, n);
1744 case yield_expr: {
1745 expr_ty exp = NULL;
1746 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001747 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 if (!exp)
1749 return NULL;
1750 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001751 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001753 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 if (NCH(n) == 1) {
1755 n = CHILD(n, 0);
1756 goto loop;
1757 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001758 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001759 case power:
1760 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001762 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 return NULL;
1764 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001765 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 return NULL;
1767}
1768
1769static expr_ty
1770ast_for_call(struct compiling *c, const node *n, expr_ty func)
1771{
1772 /*
1773 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1774 | '**' test)
1775 argument: [test '='] test [gen_for] # Really [keyword '='] test
1776 */
1777
1778 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001779 asdl_seq *args;
1780 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781 expr_ty vararg = NULL, kwarg = NULL;
1782
1783 REQ(n, arglist);
1784
1785 nargs = 0;
1786 nkeywords = 0;
1787 ngens = 0;
1788 for (i = 0; i < NCH(n); i++) {
1789 node *ch = CHILD(n, i);
1790 if (TYPE(ch) == argument) {
1791 if (NCH(ch) == 1)
1792 nargs++;
1793 else if (TYPE(CHILD(ch, 1)) == gen_for)
1794 ngens++;
1795 else
1796 nkeywords++;
1797 }
1798 }
1799 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001800 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801 "if not sole argument");
1802 return NULL;
1803 }
1804
1805 if (nargs + nkeywords + ngens > 255) {
1806 ast_error(n, "more than 255 arguments");
1807 return NULL;
1808 }
1809
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001810 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001812 return NULL;
1813 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816 nargs = 0;
1817 nkeywords = 0;
1818 for (i = 0; i < NCH(n); i++) {
1819 node *ch = CHILD(n, i);
1820 if (TYPE(ch) == argument) {
1821 expr_ty e;
1822 if (NCH(ch) == 1) {
Neal Norwitz5ef92242006-05-19 06:43:50 +00001823 if (nkeywords) {
1824 ast_error(CHILD(ch, 0),
1825 "non-keyword arg after keyword arg");
1826 return NULL;
1827 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 e = ast_for_expr(c, CHILD(ch, 0));
1829 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 asdl_seq_SET(args, nargs++, e);
1832 }
1833 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1834 e = ast_for_genexp(c, ch);
1835 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 asdl_seq_SET(args, nargs++, e);
1838 }
1839 else {
1840 keyword_ty kw;
1841 identifier key;
1842
1843 /* CHILD(ch, 0) is test, but must be an identifier? */
1844 e = ast_for_expr(c, CHILD(ch, 0));
1845 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001846 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 /* f(lambda x: x[0] = 3) ends up getting parsed with
1848 * LHS test = lambda x: x[0], and RHS test = 3.
1849 * SF bug 132313 points out that complaining about a keyword
1850 * then is very confusing.
1851 */
1852 if (e->kind == Lambda_kind) {
1853 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001854 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855 } else if (e->kind != Name_kind) {
1856 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001857 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 }
1859 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 e = ast_for_expr(c, CHILD(ch, 2));
1861 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001862 return NULL;
1863 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001865 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 asdl_seq_SET(keywords, nkeywords++, kw);
1867 }
1868 }
1869 else if (TYPE(ch) == STAR) {
1870 vararg = ast_for_expr(c, CHILD(n, i+1));
1871 i++;
1872 }
1873 else if (TYPE(ch) == DOUBLESTAR) {
1874 kwarg = ast_for_expr(c, CHILD(n, i+1));
1875 i++;
1876 }
1877 }
1878
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001879 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880}
1881
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001883ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001885 /* testlist_gexp: test (',' test)* [','] */
1886 /* testlist: test (',' test)* [','] */
1887 /* testlist_safe: test (',' test)+ [','] */
1888 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001890 if (TYPE(n) == testlist_gexp) {
1891 if (NCH(n) > 1)
1892 assert(TYPE(CHILD(n, 1)) != gen_for);
1893 }
1894 else {
1895 assert(TYPE(n) == testlist ||
1896 TYPE(n) == testlist_safe ||
1897 TYPE(n) == testlist1);
1898 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 if (NCH(n) == 1)
1900 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 else {
1902 asdl_seq *tmp = seq_for_testlist(c, n);
1903 if (!tmp)
1904 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001905 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001907}
1908
1909static expr_ty
1910ast_for_testlist_gexp(struct compiling *c, const node* n)
1911{
1912 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1913 /* argument: test [ gen_for ] */
1914 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001915 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001916 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001917 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001918}
1919
1920/* like ast_for_testlist() but returns a sequence */
1921static asdl_seq*
1922ast_for_class_bases(struct compiling *c, const node* n)
1923{
1924 /* testlist: test (',' test)* [','] */
1925 assert(NCH(n) > 0);
1926 REQ(n, testlist);
1927 if (NCH(n) == 1) {
1928 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001929 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001930 if (!bases)
1931 return NULL;
1932 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001933 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001934 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001935 asdl_seq_SET(bases, 0, base);
1936 return bases;
1937 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001938
1939 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940}
1941
1942static stmt_ty
1943ast_for_expr_stmt(struct compiling *c, const node *n)
1944{
1945 REQ(n, expr_stmt);
1946 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1947 | ('=' (yield_expr|testlist))*)
1948 testlist: test (',' test)* [',']
1949 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1950 | '<<=' | '>>=' | '**=' | '//='
1951 test: ... here starts the operator precendence dance
1952 */
1953
1954 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001955 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956 if (!e)
1957 return NULL;
1958
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001959 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 }
1961 else if (TYPE(CHILD(n, 1)) == augassign) {
1962 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00001963 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964 node *ch = CHILD(n, 0);
1965
Neal Norwitz0d62a062006-07-30 06:53:31 +00001966 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 if (!expr1)
1968 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001969 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001970 switch (expr1->kind) {
1971 case GeneratorExp_kind:
1972 ast_error(ch, "augmented assignment to generator "
1973 "expression not possible");
1974 return NULL;
Neal Norwitz0d62a062006-07-30 06:53:31 +00001975 case Yield_kind:
1976 ast_error(ch, "augmented assignment to yield "
1977 "expression not possible");
1978 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001979 case Name_kind: {
1980 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1981 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1982 ast_error(ch, "assignment to None");
1983 return NULL;
1984 }
1985 break;
1986 }
1987 case Attribute_kind:
1988 case Subscript_kind:
1989 break;
1990 default:
1991 ast_error(ch, "illegal expression for augmented "
1992 "assignment");
1993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 }
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001995 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996
1997 ch = CHILD(n, 2);
1998 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001999 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 else
Neal Norwitz0d62a062006-07-30 06:53:31 +00002001 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002002 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 return NULL;
2004
Anthony Baxtera863d332006-04-11 07:43:46 +00002005 newoperator = ast_for_augassign(CHILD(n, 1));
2006 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007 return NULL;
2008
Anthony Baxtera863d332006-04-11 07:43:46 +00002009 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 }
2011 else {
2012 int i;
2013 asdl_seq *targets;
2014 node *value;
2015 expr_ty expression;
2016
2017 /* a normal assignment */
2018 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002019 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 if (!targets)
2021 return NULL;
2022 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00002023 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 node *ch = CHILD(n, i);
2025 if (TYPE(ch) == yield_expr) {
2026 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002027 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002029 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030
2031 /* set context to assign */
2032 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002033 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034
Neal Norwitz84456bd2005-12-18 03:16:20 +00002035 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037
2038 asdl_seq_SET(targets, i / 2, e);
2039 }
2040 value = CHILD(n, NCH(n) - 1);
2041 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002042 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 else
2044 expression = ast_for_expr(c, value);
2045 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002046 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002047 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049}
2050
2051static stmt_ty
2052ast_for_print_stmt(struct compiling *c, const node *n)
2053{
2054 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2055 | '>>' test [ (',' test)+ [','] ] )
2056 */
2057 expr_ty dest = NULL, expression;
2058 asdl_seq *seq;
2059 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002060 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061
2062 REQ(n, print_stmt);
2063 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2064 dest = ast_for_expr(c, CHILD(n, 2));
2065 if (!dest)
2066 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002067 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002069 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002071 return NULL;
2072 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002074 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002076 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 }
2078 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002079 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080}
2081
2082static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002083ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084{
2085 asdl_seq *seq;
2086 int i;
2087 expr_ty e;
2088
2089 REQ(n, exprlist);
2090
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002091 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 if (!seq)
2093 return NULL;
2094 for (i = 0; i < NCH(n); i += 2) {
2095 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002096 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002097 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002098 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002099 if (context && !set_context(e, context, CHILD(n, i)))
2100 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 }
2102 return seq;
2103}
2104
2105static stmt_ty
2106ast_for_del_stmt(struct compiling *c, const node *n)
2107{
2108 asdl_seq *expr_list;
2109
2110 /* del_stmt: 'del' exprlist */
2111 REQ(n, del_stmt);
2112
2113 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2114 if (!expr_list)
2115 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002116 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117}
2118
2119static stmt_ty
2120ast_for_flow_stmt(struct compiling *c, const node *n)
2121{
2122 /*
2123 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2124 | yield_stmt
2125 break_stmt: 'break'
2126 continue_stmt: 'continue'
2127 return_stmt: 'return' [testlist]
2128 yield_stmt: yield_expr
2129 yield_expr: 'yield' testlist
2130 raise_stmt: 'raise' [test [',' test [',' test]]]
2131 */
2132 node *ch;
2133
2134 REQ(n, flow_stmt);
2135 ch = CHILD(n, 0);
2136 switch (TYPE(ch)) {
2137 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002138 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002140 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 case yield_stmt: { /* will reduce to yield_expr */
2142 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2143 if (!exp)
2144 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002145 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 }
2147 case return_stmt:
2148 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002149 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002151 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 if (!expression)
2153 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002154 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 }
2156 case raise_stmt:
2157 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002158 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 else if (NCH(ch) == 2) {
2160 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2161 if (!expression)
2162 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002163 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 }
2165 else if (NCH(ch) == 4) {
2166 expr_ty expr1, expr2;
2167
2168 expr1 = ast_for_expr(c, CHILD(ch, 1));
2169 if (!expr1)
2170 return NULL;
2171 expr2 = ast_for_expr(c, CHILD(ch, 3));
2172 if (!expr2)
2173 return NULL;
2174
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002175 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 }
2177 else if (NCH(ch) == 6) {
2178 expr_ty expr1, expr2, expr3;
2179
2180 expr1 = ast_for_expr(c, CHILD(ch, 1));
2181 if (!expr1)
2182 return NULL;
2183 expr2 = ast_for_expr(c, CHILD(ch, 3));
2184 if (!expr2)
2185 return NULL;
2186 expr3 = ast_for_expr(c, CHILD(ch, 5));
2187 if (!expr3)
2188 return NULL;
2189
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002190 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 }
2192 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002193 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 "unexpected flow_stmt: %d", TYPE(ch));
2195 return NULL;
2196 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002197
2198 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200}
2201
2202static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002203alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204{
2205 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002206 import_as_name: NAME ['as' NAME]
2207 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208 dotted_name: NAME ('.' NAME)*
2209 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002210 PyObject *str;
2211
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 loop:
2213 switch (TYPE(n)) {
2214 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002215 str = NULL;
2216 if (NCH(n) == 3) {
2217 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2218 ast_error(n, "must use 'as' in import");
2219 return NULL;
2220 }
2221 str = NEW_IDENTIFIER(CHILD(n, 2));
2222 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002223 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 case dotted_as_name:
2225 if (NCH(n) == 1) {
2226 n = CHILD(n, 0);
2227 goto loop;
2228 }
2229 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002230 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00002231 if (!a)
2232 return NULL;
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002233 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2234 ast_error(n, "must use 'as' in import");
2235 return NULL;
2236 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 assert(!a->asname);
2238 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2239 return a;
2240 }
2241 break;
2242 case dotted_name:
2243 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002244 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 else {
2246 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002247 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002248 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 char *s;
2250
2251 len = 0;
2252 for (i = 0; i < NCH(n); i += 2)
2253 /* length of string plus one for the dot */
2254 len += strlen(STR(CHILD(n, i))) + 1;
2255 len--; /* the last name doesn't have a dot */
2256 str = PyString_FromStringAndSize(NULL, len);
2257 if (!str)
2258 return NULL;
2259 s = PyString_AS_STRING(str);
2260 if (!s)
2261 return NULL;
2262 for (i = 0; i < NCH(n); i += 2) {
2263 char *sch = STR(CHILD(n, i));
2264 strcpy(s, STR(CHILD(n, i)));
2265 s += strlen(sch);
2266 *s++ = '.';
2267 }
2268 --s;
2269 *s = '\0';
2270 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002271 PyArena_AddPyObject(c->c_arena, str);
2272 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 }
2274 break;
2275 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002276 str = PyString_InternFromString("*");
2277 PyArena_AddPyObject(c->c_arena, str);
2278 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002280 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 "unexpected import name: %d", TYPE(n));
2282 return NULL;
2283 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002284
2285 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 return NULL;
2287}
2288
2289static stmt_ty
2290ast_for_import_stmt(struct compiling *c, const node *n)
2291{
2292 /*
2293 import_stmt: import_name | import_from
2294 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002295 import_from: 'from' ('.'* dotted_name | '.') 'import'
2296 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002298 int lineno;
2299 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 int i;
2301 asdl_seq *aliases;
2302
2303 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002304 lineno = LINENO(n);
2305 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002307 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002309 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002310 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 if (!aliases)
2312 return NULL;
2313 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002314 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002315 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 asdl_seq_SET(aliases, i / 2, import_alias);
2318 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002319 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002321 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002323 int idx, ndots = 0;
2324 alias_ty mod = NULL;
2325 identifier modname;
2326
2327 /* Count the number of dots (for relative imports) and check for the
2328 optional module name */
2329 for (idx = 1; idx < NCH(n); idx++) {
2330 if (TYPE(CHILD(n, idx)) == dotted_name) {
2331 mod = alias_for_import_name(c, CHILD(n, idx));
2332 idx++;
2333 break;
2334 } else if (TYPE(CHILD(n, idx)) != DOT) {
2335 break;
2336 }
2337 ndots++;
2338 }
2339 idx++; /* skip over the 'import' keyword */
2340 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002341 case STAR:
2342 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002343 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002344 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002345 if (ndots) {
2346 ast_error(n, "'import *' not allowed with 'from .'");
2347 return NULL;
2348 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002349 break;
2350 case LPAR:
2351 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002352 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002353 n_children = NCH(n);
2354 break;
2355 case import_as_names:
2356 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002357 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002358 n_children = NCH(n);
2359 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360 ast_error(n, "trailing comma not allowed without"
2361 " surrounding parentheses");
2362 return NULL;
2363 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002364 break;
2365 default:
2366 ast_error(n, "Unexpected node-type in from-import");
2367 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002368 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002369
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002370 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002371 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002372 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373
2374 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002375 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002376 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002377 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002379 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002381 else {
2382 for (i = 0; i < NCH(n); i += 2) {
2383 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2384 if (!import_alias)
2385 return NULL;
2386 asdl_seq_SET(aliases, i / 2, import_alias);
2387 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002389 if (mod != NULL)
2390 modname = mod->name;
2391 else
2392 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002393 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002394 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 }
Neal Norwitz79792652005-11-14 04:25:03 +00002396 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 "unknown import statement: starts with command '%s'",
2398 STR(CHILD(n, 0)));
2399 return NULL;
2400}
2401
2402static stmt_ty
2403ast_for_global_stmt(struct compiling *c, const node *n)
2404{
2405 /* global_stmt: 'global' NAME (',' NAME)* */
2406 identifier name;
2407 asdl_seq *s;
2408 int i;
2409
2410 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002411 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 if (!s)
2413 return NULL;
2414 for (i = 1; i < NCH(n); i += 2) {
2415 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002416 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002417 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418 asdl_seq_SET(s, i / 2, name);
2419 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002420 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421}
2422
2423static stmt_ty
2424ast_for_exec_stmt(struct compiling *c, const node *n)
2425{
2426 expr_ty expr1, globals = NULL, locals = NULL;
2427 int n_children = NCH(n);
2428 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002429 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 "poorly formed 'exec' statement: %d parts to statement",
2431 n_children);
2432 return NULL;
2433 }
2434
2435 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2436 REQ(n, exec_stmt);
2437 expr1 = ast_for_expr(c, CHILD(n, 1));
2438 if (!expr1)
2439 return NULL;
2440 if (n_children >= 4) {
2441 globals = ast_for_expr(c, CHILD(n, 3));
2442 if (!globals)
2443 return NULL;
2444 }
2445 if (n_children == 6) {
2446 locals = ast_for_expr(c, CHILD(n, 5));
2447 if (!locals)
2448 return NULL;
2449 }
2450
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002451 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452}
2453
2454static stmt_ty
2455ast_for_assert_stmt(struct compiling *c, const node *n)
2456{
2457 /* assert_stmt: 'assert' test [',' test] */
2458 REQ(n, assert_stmt);
2459 if (NCH(n) == 2) {
2460 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2461 if (!expression)
2462 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002463 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 }
2465 else if (NCH(n) == 4) {
2466 expr_ty expr1, expr2;
2467
2468 expr1 = ast_for_expr(c, CHILD(n, 1));
2469 if (!expr1)
2470 return NULL;
2471 expr2 = ast_for_expr(c, CHILD(n, 3));
2472 if (!expr2)
2473 return NULL;
2474
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002475 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 }
Neal Norwitz79792652005-11-14 04:25:03 +00002477 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 "improper number of parts to 'assert' statement: %d",
2479 NCH(n));
2480 return NULL;
2481}
2482
2483static asdl_seq *
2484ast_for_suite(struct compiling *c, const node *n)
2485{
2486 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002487 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 stmt_ty s;
2489 int i, total, num, end, pos = 0;
2490 node *ch;
2491
2492 REQ(n, suite);
2493
2494 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002495 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 if (!seq)
2497 return NULL;
2498 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2499 n = CHILD(n, 0);
2500 /* simple_stmt always ends with a NEWLINE,
2501 and may have a trailing SEMI
2502 */
2503 end = NCH(n) - 1;
2504 if (TYPE(CHILD(n, end - 1)) == SEMI)
2505 end--;
2506 /* loop by 2 to skip semi-colons */
2507 for (i = 0; i < end; i += 2) {
2508 ch = CHILD(n, i);
2509 s = ast_for_stmt(c, ch);
2510 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002511 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 asdl_seq_SET(seq, pos++, s);
2513 }
2514 }
2515 else {
2516 for (i = 2; i < (NCH(n) - 1); i++) {
2517 ch = CHILD(n, i);
2518 REQ(ch, stmt);
2519 num = num_stmts(ch);
2520 if (num == 1) {
2521 /* small_stmt or compound_stmt with only one child */
2522 s = ast_for_stmt(c, ch);
2523 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002524 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 asdl_seq_SET(seq, pos++, s);
2526 }
2527 else {
2528 int j;
2529 ch = CHILD(ch, 0);
2530 REQ(ch, simple_stmt);
2531 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002532 /* statement terminates with a semi-colon ';' */
2533 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002534 assert((j + 1) == NCH(ch));
2535 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002536 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 s = ast_for_stmt(c, CHILD(ch, j));
2538 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002539 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 asdl_seq_SET(seq, pos++, s);
2541 }
2542 }
2543 }
2544 }
2545 assert(pos == seq->size);
2546 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547}
2548
2549static stmt_ty
2550ast_for_if_stmt(struct compiling *c, const node *n)
2551{
2552 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2553 ['else' ':' suite]
2554 */
2555 char *s;
2556
2557 REQ(n, if_stmt);
2558
2559 if (NCH(n) == 4) {
2560 expr_ty expression;
2561 asdl_seq *suite_seq;
2562
2563 expression = ast_for_expr(c, CHILD(n, 1));
2564 if (!expression)
2565 return NULL;
2566 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002567 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 return NULL;
2569
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002570 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002572
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 s = STR(CHILD(n, 4));
2574 /* s[2], the third character in the string, will be
2575 's' for el_s_e, or
2576 'i' for el_i_f
2577 */
2578 if (s[2] == 's') {
2579 expr_ty expression;
2580 asdl_seq *seq1, *seq2;
2581
2582 expression = ast_for_expr(c, CHILD(n, 1));
2583 if (!expression)
2584 return NULL;
2585 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002586 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 return NULL;
2588 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002589 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 return NULL;
2591
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002592 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 }
2594 else if (s[2] == 'i') {
2595 int i, n_elif, has_else = 0;
2596 asdl_seq *orelse = NULL;
2597 n_elif = NCH(n) - 4;
2598 /* must reference the child n_elif+1 since 'else' token is third,
2599 not fourth, child from the end. */
2600 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2601 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2602 has_else = 1;
2603 n_elif -= 3;
2604 }
2605 n_elif /= 4;
2606
2607 if (has_else) {
2608 expr_ty expression;
2609 asdl_seq *seq1, *seq2;
2610
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002611 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 if (!orelse)
2613 return NULL;
2614 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002615 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002618 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002621 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623
2624 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002625 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002626 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 /* the just-created orelse handled the last elif */
2628 n_elif--;
2629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630
2631 for (i = 0; i < n_elif; i++) {
2632 int off = 5 + (n_elif - i - 1) * 4;
2633 expr_ty expression;
2634 asdl_seq *suite_seq;
Anthony Baxtera863d332006-04-11 07:43:46 +00002635 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2636 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 return NULL;
2638 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002639 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002642 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644
Anthony Baxtera863d332006-04-11 07:43:46 +00002645 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002647 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Anthony Baxtera863d332006-04-11 07:43:46 +00002648 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 }
2650 return If(ast_for_expr(c, CHILD(n, 1)),
2651 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002652 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002654
2655 PyErr_Format(PyExc_SystemError,
2656 "unexpected token in 'if' statement: %s", s);
2657 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658}
2659
2660static stmt_ty
2661ast_for_while_stmt(struct compiling *c, const node *n)
2662{
2663 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2664 REQ(n, while_stmt);
2665
2666 if (NCH(n) == 4) {
2667 expr_ty expression;
2668 asdl_seq *suite_seq;
2669
2670 expression = ast_for_expr(c, CHILD(n, 1));
2671 if (!expression)
2672 return NULL;
2673 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002674 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002676 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 }
2678 else if (NCH(n) == 7) {
2679 expr_ty expression;
2680 asdl_seq *seq1, *seq2;
2681
2682 expression = ast_for_expr(c, CHILD(n, 1));
2683 if (!expression)
2684 return NULL;
2685 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002686 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687 return NULL;
2688 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002689 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690 return NULL;
2691
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002692 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002694
2695 PyErr_Format(PyExc_SystemError,
2696 "wrong number of tokens for 'while' statement: %d",
2697 NCH(n));
2698 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699}
2700
2701static stmt_ty
2702ast_for_for_stmt(struct compiling *c, const node *n)
2703{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002704 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 expr_ty expression;
2706 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002707 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2709 REQ(n, for_stmt);
2710
2711 if (NCH(n) == 9) {
2712 seq = ast_for_suite(c, CHILD(n, 8));
2713 if (!seq)
2714 return NULL;
2715 }
2716
Neal Norwitzedef2be2006-07-12 05:26:17 +00002717 node_target = CHILD(n, 1);
2718 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002719 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002721 /* Check the # of children rather than the length of _target, since
2722 for x, in ... has 1 element in _target, but still requires a Tuple. */
2723 if (NCH(node_target) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00002724 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002726 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002728 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002729 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 return NULL;
2731 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002732 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 return NULL;
2734
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002735 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2736 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737}
2738
2739static excepthandler_ty
2740ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2741{
2742 /* except_clause: 'except' [test [',' test]] */
2743 REQ(exc, except_clause);
2744 REQ(body, suite);
2745
2746 if (NCH(exc) == 1) {
2747 asdl_seq *suite_seq = ast_for_suite(c, body);
2748 if (!suite_seq)
2749 return NULL;
2750
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002751 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2752 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 }
2754 else if (NCH(exc) == 2) {
2755 expr_ty expression;
2756 asdl_seq *suite_seq;
2757
2758 expression = ast_for_expr(c, CHILD(exc, 1));
2759 if (!expression)
2760 return NULL;
2761 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002762 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 return NULL;
2764
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002765 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2766 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 }
2768 else if (NCH(exc) == 4) {
2769 asdl_seq *suite_seq;
2770 expr_ty expression;
2771 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2772 if (!e)
2773 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002774 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 return NULL;
2776 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002777 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 return NULL;
2779 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002780 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 return NULL;
2782
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002783 return excepthandler(expression, e, suite_seq, LINENO(exc),
2784 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002786
2787 PyErr_Format(PyExc_SystemError,
2788 "wrong number of children for 'except' clause: %d",
2789 NCH(exc));
2790 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791}
2792
2793static stmt_ty
2794ast_for_try_stmt(struct compiling *c, const node *n)
2795{
Neal Norwitzf599f422005-12-17 21:33:47 +00002796 const int nch = NCH(n);
2797 int n_except = (nch - 3)/3;
2798 asdl_seq *body, *orelse = NULL, *finally = NULL;
2799
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 REQ(n, try_stmt);
2801
Neal Norwitzf599f422005-12-17 21:33:47 +00002802 body = ast_for_suite(c, CHILD(n, 2));
2803 if (body == NULL)
2804 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805
Neal Norwitzf599f422005-12-17 21:33:47 +00002806 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2807 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2808 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2809 /* we can assume it's an "else",
2810 because nch >= 9 for try-else-finally and
2811 it would otherwise have a type of except_clause */
2812 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2813 if (orelse == NULL)
2814 return NULL;
2815 n_except--;
2816 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817
Neal Norwitzf599f422005-12-17 21:33:47 +00002818 finally = ast_for_suite(c, CHILD(n, nch - 1));
2819 if (finally == NULL)
2820 return NULL;
2821 n_except--;
2822 }
2823 else {
2824 /* we can assume it's an "else",
2825 otherwise it would have a type of except_clause */
2826 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2827 if (orelse == NULL)
2828 return NULL;
2829 n_except--;
2830 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002832 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002833 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 return NULL;
2835 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002836
2837 if (n_except > 0) {
2838 int i;
2839 stmt_ty except_st;
2840 /* process except statements to create a try ... except */
2841 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2842 if (handlers == NULL)
2843 return NULL;
2844
2845 for (i = 0; i < n_except; i++) {
2846 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2847 CHILD(n, 5 + i * 3));
2848 if (!e)
2849 return NULL;
2850 asdl_seq_SET(handlers, i, e);
2851 }
2852
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002853 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2854 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002855 if (!finally)
2856 return except_st;
2857
2858 /* if a 'finally' is present too, we nest the TryExcept within a
2859 TryFinally to emulate try ... except ... finally */
2860 body = asdl_seq_new(1, c->c_arena);
2861 if (body == NULL)
2862 return NULL;
2863 asdl_seq_SET(body, 0, except_st);
2864 }
2865
2866 /* must be a try ... finally (except clauses are in body, if any exist) */
2867 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002868 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869}
2870
Guido van Rossumc2e20742006-02-27 22:32:47 +00002871static expr_ty
2872ast_for_with_var(struct compiling *c, const node *n)
2873{
2874 REQ(n, with_var);
2875 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2876 ast_error(n, "expected \"with [expr] as [var]\"");
2877 return NULL;
2878 }
2879 return ast_for_expr(c, CHILD(n, 1));
2880}
2881
2882/* with_stmt: 'with' test [ with_var ] ':' suite */
2883static stmt_ty
2884ast_for_with_stmt(struct compiling *c, const node *n)
2885{
2886 expr_ty context_expr, optional_vars = NULL;
2887 int suite_index = 3; /* skip 'with', test, and ':' */
2888 asdl_seq *suite_seq;
2889
2890 assert(TYPE(n) == with_stmt);
2891 context_expr = ast_for_expr(c, CHILD(n, 1));
2892 if (TYPE(CHILD(n, 2)) == with_var) {
2893 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2894
2895 if (!optional_vars) {
2896 return NULL;
2897 }
2898 if (!set_context(optional_vars, Store, n)) {
2899 return NULL;
2900 }
2901 suite_index = 4;
2902 }
2903
2904 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2905 if (!suite_seq) {
2906 return NULL;
2907 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002908 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2909 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002910}
2911
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912static stmt_ty
2913ast_for_classdef(struct compiling *c, const node *n)
2914{
2915 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 asdl_seq *bases, *s;
2917
2918 REQ(n, classdef);
2919
2920 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2921 ast_error(n, "assignment to None");
2922 return NULL;
2923 }
2924
2925 if (NCH(n) == 4) {
2926 s = ast_for_suite(c, CHILD(n, 3));
2927 if (!s)
2928 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002929 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2930 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931 }
2932 /* check for empty base list */
2933 if (TYPE(CHILD(n,3)) == RPAR) {
2934 s = ast_for_suite(c, CHILD(n,5));
2935 if (!s)
2936 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002937 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2938 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 }
2940
2941 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002942 bases = ast_for_class_bases(c, CHILD(n, 3));
2943 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945
2946 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002947 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002949 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2950 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951}
2952
2953static stmt_ty
2954ast_for_stmt(struct compiling *c, const node *n)
2955{
2956 if (TYPE(n) == stmt) {
2957 assert(NCH(n) == 1);
2958 n = CHILD(n, 0);
2959 }
2960 if (TYPE(n) == simple_stmt) {
2961 assert(num_stmts(n) == 1);
2962 n = CHILD(n, 0);
2963 }
2964 if (TYPE(n) == small_stmt) {
2965 REQ(n, small_stmt);
2966 n = CHILD(n, 0);
2967 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2968 | flow_stmt | import_stmt | global_stmt | exec_stmt
2969 | assert_stmt
2970 */
2971 switch (TYPE(n)) {
2972 case expr_stmt:
2973 return ast_for_expr_stmt(c, n);
2974 case print_stmt:
2975 return ast_for_print_stmt(c, n);
2976 case del_stmt:
2977 return ast_for_del_stmt(c, n);
2978 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002979 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 case flow_stmt:
2981 return ast_for_flow_stmt(c, n);
2982 case import_stmt:
2983 return ast_for_import_stmt(c, n);
2984 case global_stmt:
2985 return ast_for_global_stmt(c, n);
2986 case exec_stmt:
2987 return ast_for_exec_stmt(c, n);
2988 case assert_stmt:
2989 return ast_for_assert_stmt(c, n);
2990 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002991 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2993 TYPE(n), NCH(n));
2994 return NULL;
2995 }
2996 }
2997 else {
2998 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2999 | funcdef | classdef
3000 */
3001 node *ch = CHILD(n, 0);
3002 REQ(n, compound_stmt);
3003 switch (TYPE(ch)) {
3004 case if_stmt:
3005 return ast_for_if_stmt(c, ch);
3006 case while_stmt:
3007 return ast_for_while_stmt(c, ch);
3008 case for_stmt:
3009 return ast_for_for_stmt(c, ch);
3010 case try_stmt:
3011 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003012 case with_stmt:
3013 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 case funcdef:
3015 return ast_for_funcdef(c, ch);
3016 case classdef:
3017 return ast_for_classdef(c, ch);
3018 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003019 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3021 TYPE(n), NCH(n));
3022 return NULL;
3023 }
3024 }
3025}
3026
3027static PyObject *
3028parsenumber(const char *s)
3029{
3030 const char *end;
3031 long x;
3032 double dx;
3033#ifndef WITHOUT_COMPLEX
3034 Py_complex c;
3035 int imflag;
3036#endif
3037
3038 errno = 0;
3039 end = s + strlen(s) - 1;
3040#ifndef WITHOUT_COMPLEX
3041 imflag = *end == 'j' || *end == 'J';
3042#endif
3043 if (*end == 'l' || *end == 'L')
3044 return PyLong_FromString((char *)s, (char **)0, 0);
3045 if (s[0] == '0') {
3046 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3047 if (x < 0 && errno == 0) {
3048 return PyLong_FromString((char *)s,
3049 (char **)0,
3050 0);
3051 }
3052 }
3053 else
3054 x = PyOS_strtol((char *)s, (char **)&end, 0);
3055 if (*end == '\0') {
3056 if (errno != 0)
3057 return PyLong_FromString((char *)s, (char **)0, 0);
3058 return PyInt_FromLong(x);
3059 }
3060 /* XXX Huge floats may silently fail */
3061#ifndef WITHOUT_COMPLEX
3062 if (imflag) {
3063 c.real = 0.;
3064 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003065 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066 PyFPE_END_PROTECT(c)
3067 return PyComplex_FromCComplex(c);
3068 }
3069 else
3070#endif
3071 {
3072 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003073 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 PyFPE_END_PROTECT(dx)
3075 return PyFloat_FromDouble(dx);
3076 }
3077}
3078
3079static PyObject *
3080decode_utf8(const char **sPtr, const char *end, char* encoding)
3081{
3082#ifndef Py_USING_UNICODE
3083 Py_FatalError("decode_utf8 should not be called in this build.");
3084 return NULL;
3085#else
3086 PyObject *u, *v;
3087 char *s, *t;
3088 t = s = (char *)*sPtr;
3089 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3090 while (s < end && (*s & 0x80)) s++;
3091 *sPtr = s;
3092 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3093 if (u == NULL)
3094 return NULL;
3095 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3096 Py_DECREF(u);
3097 return v;
3098#endif
3099}
3100
3101static PyObject *
3102decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3103{
3104 PyObject *v, *u;
3105 char *buf;
3106 char *p;
3107 const char *end;
3108 if (encoding == NULL) {
3109 buf = (char *)s;
3110 u = NULL;
3111 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3112 buf = (char *)s;
3113 u = NULL;
3114 } else {
3115 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3116 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3117 if (u == NULL)
3118 return NULL;
3119 p = buf = PyString_AsString(u);
3120 end = s + len;
3121 while (s < end) {
3122 if (*s == '\\') {
3123 *p++ = *s++;
3124 if (*s & 0x80) {
3125 strcpy(p, "u005c");
3126 p += 5;
3127 }
3128 }
3129 if (*s & 0x80) { /* XXX inefficient */
3130 PyObject *w;
3131 char *r;
Martin v. Löwis66851282006-04-22 11:40:03 +00003132 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 w = decode_utf8(&s, end, "utf-16-be");
3134 if (w == NULL) {
3135 Py_DECREF(u);
3136 return NULL;
3137 }
3138 r = PyString_AsString(w);
3139 rn = PyString_Size(w);
3140 assert(rn % 2 == 0);
3141 for (i = 0; i < rn; i += 2) {
3142 sprintf(p, "\\u%02x%02x",
3143 r[i + 0] & 0xFF,
3144 r[i + 1] & 0xFF);
3145 p += 6;
3146 }
3147 Py_DECREF(w);
3148 } else {
3149 *p++ = *s++;
3150 }
3151 }
3152 len = p - buf;
3153 s = buf;
3154 }
3155 if (rawmode)
3156 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3157 else
3158 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3159 Py_XDECREF(u);
3160 return v;
3161}
3162
3163/* s is a Python string literal, including the bracketing quote characters,
3164 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3165 * parsestr parses it, and returns the decoded Python string object.
3166 */
3167static PyObject *
3168parsestr(const char *s, const char *encoding)
3169{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003171 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 int rawmode = 0;
3173 int need_encoding;
3174 int unicode = 0;
3175
3176 if (isalpha(quote) || quote == '_') {
3177 if (quote == 'u' || quote == 'U') {
3178 quote = *++s;
3179 unicode = 1;
3180 }
3181 if (quote == 'r' || quote == 'R') {
3182 quote = *++s;
3183 rawmode = 1;
3184 }
3185 }
3186 if (quote != '\'' && quote != '\"') {
3187 PyErr_BadInternalCall();
3188 return NULL;
3189 }
3190 s++;
3191 len = strlen(s);
3192 if (len > INT_MAX) {
3193 PyErr_SetString(PyExc_OverflowError,
3194 "string to parse is too long");
3195 return NULL;
3196 }
3197 if (s[--len] != quote) {
3198 PyErr_BadInternalCall();
3199 return NULL;
3200 }
3201 if (len >= 4 && s[0] == quote && s[1] == quote) {
3202 s += 2;
3203 len -= 2;
3204 if (s[--len] != quote || s[--len] != quote) {
3205 PyErr_BadInternalCall();
3206 return NULL;
3207 }
3208 }
3209#ifdef Py_USING_UNICODE
3210 if (unicode || Py_UnicodeFlag) {
3211 return decode_unicode(s, len, rawmode, encoding);
3212 }
3213#endif
3214 need_encoding = (encoding != NULL &&
3215 strcmp(encoding, "utf-8") != 0 &&
3216 strcmp(encoding, "iso-8859-1") != 0);
3217 if (rawmode || strchr(s, '\\') == NULL) {
3218 if (need_encoding) {
3219#ifndef Py_USING_UNICODE
3220 /* This should not happen - we never see any other
3221 encoding. */
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003222 Py_FatalError(
3223 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003225 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226 if (u == NULL)
3227 return NULL;
3228 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3229 Py_DECREF(u);
3230 return v;
3231#endif
3232 } else {
3233 return PyString_FromStringAndSize(s, len);
3234 }
3235 }
3236
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003237 return PyString_DecodeEscape(s, len, NULL, unicode,
3238 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003239}
3240
3241/* Build a Python string object out of a STRING atom. This takes care of
3242 * compile-time literal catenation, calling parsestr() on each piece, and
3243 * pasting the intermediate results together.
3244 */
3245static PyObject *
3246parsestrplus(struct compiling *c, const node *n)
3247{
3248 PyObject *v;
3249 int i;
3250 REQ(CHILD(n, 0), STRING);
3251 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3252 /* String literal concatenation */
3253 for (i = 1; i < NCH(n); i++) {
3254 PyObject *s;
3255 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3256 if (s == NULL)
3257 goto onError;
3258 if (PyString_Check(v) && PyString_Check(s)) {
3259 PyString_ConcatAndDel(&v, s);
3260 if (v == NULL)
3261 goto onError;
3262 }
3263#ifdef Py_USING_UNICODE
3264 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003265 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267 Py_DECREF(v);
3268 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003269 if (v == NULL)
3270 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271 }
3272#endif
3273 }
3274 }
3275 return v;
3276
3277 onError:
3278 Py_XDECREF(v);
3279 return NULL;
3280}