blob: 7edd345fefc89dba15cd853c9e8f950c1e5e427a [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* XXX TO DO
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
21 - syntax errors
22*/
23
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024/* Data structure used internally */
25struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000026 char *c_encoding; /* source encoding */
27 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028};
29
30static asdl_seq *seq_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_expr(struct compiling *, const node *);
32static stmt_ty ast_for_stmt(struct compiling *, const node *);
33static asdl_seq *ast_for_suite(struct compiling *, const node *);
34static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 value = Py_BuildValue("(OO)", errstr, tmp);
111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194 } else if (TYPE(n) == encoding_decl) {
195 c.c_encoding = STR(n);
196 n = CHILD(n, 0);
197 } else {
198 c.c_encoding = NULL;
199 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000200 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
Jeremy Hyltona8293132006-02-28 17:58:27 +0000202 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203 switch (TYPE(n)) {
204 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000205 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206 if (!stmts)
207 return NULL;
208 for (i = 0; i < NCH(n) - 1; i++) {
209 ch = CHILD(n, i);
210 if (TYPE(ch) == NEWLINE)
211 continue;
212 REQ(ch, stmt);
213 num = num_stmts(ch);
214 if (num == 1) {
215 s = ast_for_stmt(&c, ch);
216 if (!s)
217 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000218 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219 }
220 else {
221 ch = CHILD(ch, 0);
222 REQ(ch, simple_stmt);
223 for (j = 0; j < num; j++) {
224 s = ast_for_stmt(&c, CHILD(ch, j * 2));
225 if (!s)
226 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000227 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 }
229 }
230 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000231 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 case eval_input: {
233 expr_ty testlist_ast;
234
235 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000236 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 if (!testlist_ast)
238 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000239 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 }
241 case single_input:
242 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 if (!stmts)
245 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000246 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, arena));
247 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 }
249 else {
250 n = CHILD(n, 0);
251 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 if (!stmts)
254 goto error;
255 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000256 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 if (!s)
258 goto error;
259 asdl_seq_SET(stmts, 0, s);
260 }
261 else {
262 /* Only a simple_stmt can contain multiple statements. */
263 REQ(n, simple_stmt);
264 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 if (TYPE(CHILD(n, i)) == NEWLINE)
266 break;
267 s = ast_for_stmt(&c, CHILD(n, i));
268 if (!s)
269 goto error;
270 asdl_seq_SET(stmts, i / 2, s);
271 }
272 }
273
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000274 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 }
276 default:
277 goto error;
278 }
279 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 ast_error_finish(filename);
281 return NULL;
282}
283
284/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
285*/
286
287static operator_ty
288get_operator(const node *n)
289{
290 switch (TYPE(n)) {
291 case VBAR:
292 return BitOr;
293 case CIRCUMFLEX:
294 return BitXor;
295 case AMPER:
296 return BitAnd;
297 case LEFTSHIFT:
298 return LShift;
299 case RIGHTSHIFT:
300 return RShift;
301 case PLUS:
302 return Add;
303 case MINUS:
304 return Sub;
305 case STAR:
306 return Mult;
307 case SLASH:
308 return Div;
309 case DOUBLESLASH:
310 return FloorDiv;
311 case PERCENT:
312 return Mod;
313 default:
314 return 0;
315 }
316}
317
Jeremy Hyltona8293132006-02-28 17:58:27 +0000318/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319
320 Only sets context for expr kinds that "can appear in assignment context"
321 (according to ../Parser/Python.asdl). For other expr kinds, it sets
322 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323*/
324
325static int
326set_context(expr_ty e, expr_context_ty ctx, const node *n)
327{
328 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000329 /* If a particular expression type can't be used for assign / delete,
330 set expr_name to its name and an error message will be generated.
331 */
332 const char* expr_name = NULL;
333
334 /* The ast defines augmented store and load contexts, but the
335 implementation here doesn't actually use them. The code may be
336 a little more complex than necessary as a result. It also means
337 that expressions in an augmented assignment have no context.
338 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000339 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000340 */
341 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342
343 switch (e->kind) {
344 case Attribute_kind:
345 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000346 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347 return ast_error(n, "assignment to None");
348 }
349 e->v.Attribute.ctx = ctx;
350 break;
351 case Subscript_kind:
352 e->v.Subscript.ctx = ctx;
353 break;
354 case Name_kind:
355 if (ctx == Store &&
356 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
357 return ast_error(n, "assignment to None");
358 }
359 e->v.Name.ctx = ctx;
360 break;
361 case List_kind:
362 e->v.List.ctx = ctx;
363 s = e->v.List.elts;
364 break;
365 case Tuple_kind:
366 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
367 return ast_error(n, "can't assign to ()");
368 e->v.Tuple.ctx = ctx;
369 s = e->v.Tuple.elts;
370 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000371 case Lambda_kind:
372 expr_name = "lambda";
373 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000375 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000377 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 case UnaryOp_kind:
380 expr_name = "operator";
381 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000383 expr_name = "generator expression";
384 break;
385 case ListComp_kind:
386 expr_name = "list comprehension";
387 break;
388 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389 case Num_kind:
390 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000391 expr_name = "literal";
392 break;
393 case Compare_kind:
394 expr_name = "comparison";
395 break;
396 case Repr_kind:
397 expr_name = "repr";
398 break;
399 default:
400 PyErr_Format(PyExc_SystemError,
401 "unexpected expression in assignment %d (line %d)",
402 e->kind, e->lineno);
403 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000405 /* Check for error string set by switch */
406 if (expr_name) {
407 char buf[300];
408 PyOS_snprintf(buf, sizeof(buf),
409 "can't %s %s",
410 ctx == Store ? "assign to" : "delete",
411 expr_name);
412 return ast_error(n, buf);
413 }
414
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000416 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417 */
418 if (s) {
419 int i;
420
421 for (i = 0; i < asdl_seq_LEN(s); i++) {
422 if (!set_context(asdl_seq_GET(s, i), ctx, n))
423 return 0;
424 }
425 }
426 return 1;
427}
428
429static operator_ty
430ast_for_augassign(const node *n)
431{
432 REQ(n, augassign);
433 n = CHILD(n, 0);
434 switch (STR(n)[0]) {
435 case '+':
436 return Add;
437 case '-':
438 return Sub;
439 case '/':
440 if (STR(n)[1] == '/')
441 return FloorDiv;
442 else
443 return Div;
444 case '%':
445 return Mod;
446 case '<':
447 return LShift;
448 case '>':
449 return RShift;
450 case '&':
451 return BitAnd;
452 case '^':
453 return BitXor;
454 case '|':
455 return BitOr;
456 case '*':
457 if (STR(n)[1] == '*')
458 return Pow;
459 else
460 return Mult;
461 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000462 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 return 0;
464 }
465}
466
467static cmpop_ty
468ast_for_comp_op(const node *n)
469{
470 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
471 |'is' 'not'
472 */
473 REQ(n, comp_op);
474 if (NCH(n) == 1) {
475 n = CHILD(n, 0);
476 switch (TYPE(n)) {
477 case LESS:
478 return Lt;
479 case GREATER:
480 return Gt;
481 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 return Eq;
483 case LESSEQUAL:
484 return LtE;
485 case GREATEREQUAL:
486 return GtE;
487 case NOTEQUAL:
488 return NotEq;
489 case NAME:
490 if (strcmp(STR(n), "in") == 0)
491 return In;
492 if (strcmp(STR(n), "is") == 0)
493 return Is;
494 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000495 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 STR(n));
497 return 0;
498 }
499 }
500 else if (NCH(n) == 2) {
501 /* handle "not in" and "is not" */
502 switch (TYPE(CHILD(n, 0))) {
503 case NAME:
504 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
505 return NotIn;
506 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
507 return IsNot;
508 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000509 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
511 return 0;
512 }
513 }
Neal Norwitz79792652005-11-14 04:25:03 +0000514 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 NCH(n));
516 return 0;
517}
518
519static asdl_seq *
520seq_for_testlist(struct compiling *c, const node *n)
521{
522 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000523 asdl_seq *seq;
524 expr_ty expression;
525 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 assert(TYPE(n) == testlist
527 || TYPE(n) == listmaker
528 || TYPE(n) == testlist_gexp
529 || TYPE(n) == testlist_safe
530 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000532 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533 if (!seq)
534 return NULL;
535
536 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000537 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
539 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000540 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
543 assert(i / 2 < seq->size);
544 asdl_seq_SET(seq, i / 2, expression);
545 }
546 return seq;
547}
548
549static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000550compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551{
552 int i, len = (NCH(n) + 1) / 2;
553 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000554 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 if (!args)
556 return NULL;
557
558 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559 for (i = 0; i < len; i++) {
560 const node *child = CHILD(CHILD(n, 2*i), 0);
561 expr_ty arg;
562 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000563 if (!strcmp(STR(child), "None")) {
564 ast_error(child, "assignment to None");
565 return NULL;
566 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000567 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
568 c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000569 }
570 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000571 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000572 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 asdl_seq_SET(args, i, arg);
574 }
575
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000576 result = Tuple(args, Store, LINENO(n), c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000577 if (!set_context(result, Store, n))
578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579 return result;
580}
581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582
Jeremy Hyltona8293132006-02-28 17:58:27 +0000583/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
585static arguments_ty
586ast_for_arguments(struct compiling *c, const node *n)
587{
588 /* parameters: '(' [varargslist] ')'
589 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
590 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
591 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000592 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 asdl_seq *args, *defaults;
594 identifier vararg = NULL, kwarg = NULL;
595 node *ch;
596
597 if (TYPE(n) == parameters) {
598 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000599 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 n = CHILD(n, 1);
601 }
602 REQ(n, varargslist);
603
604 /* first count the number of normal args & defaults */
605 for (i = 0; i < NCH(n); i++) {
606 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000607 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 if (TYPE(ch) == EQUAL)
610 n_defaults++;
611 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000612 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 if (!args && n_args)
614 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000615 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 if (!defaults && n_defaults)
617 goto error;
618
619 /* fpdef: NAME | '(' fplist ')'
620 fplist: fpdef (',' fpdef)* [',']
621 */
622 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000623 j = 0; /* index for defaults */
624 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 while (i < NCH(n)) {
626 ch = CHILD(n, i);
627 switch (TYPE(ch)) {
628 case fpdef:
629 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
630 anything other than EQUAL or a comma? */
631 /* XXX Should NCH(n) check be made a separate check? */
632 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000633 asdl_seq_SET(defaults, j++,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 ast_for_expr(c, CHILD(n, i + 2)));
635 i += 2;
636 found_default = 1;
637 }
638 else if (found_default) {
639 ast_error(n,
640 "non-default argument follows default argument");
641 goto error;
642 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 if (NCH(ch) == 3) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000644 asdl_seq_SET(args, k++,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000645 compiler_complex_args(c, CHILD(ch, 1)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 }
647 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000648 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
650 ast_error(CHILD(ch, 0), "assignment to None");
651 goto error;
652 }
Armin Rigo31441302005-10-21 12:57:31 +0000653 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000654 Param, LINENO(ch), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 if (!name)
656 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000657 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
659 }
660 i += 2; /* the name and the comma */
661 break;
662 case STAR:
663 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
664 ast_error(CHILD(n, i+1), "assignment to None");
665 goto error;
666 }
667 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
668 i += 3;
669 break;
670 case DOUBLESTAR:
671 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
672 ast_error(CHILD(n, i+1), "assignment to None");
673 goto error;
674 }
675 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
676 i += 3;
677 break;
678 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000679 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 "unexpected node in varargslist: %d @ %d",
681 TYPE(ch), i);
682 goto error;
683 }
684 }
685
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000686 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687
688 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000689 Py_XDECREF(vararg);
690 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 return NULL;
692}
693
694static expr_ty
695ast_for_dotted_name(struct compiling *c, const node *n)
696{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000697 expr_ty e;
698 identifier id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 int i;
700
701 REQ(n, dotted_name);
702
703 id = NEW_IDENTIFIER(CHILD(n, 0));
704 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000705 return NULL;
706 e = Name(id, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000708 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709
710 for (i = 2; i < NCH(n); i+=2) {
711 id = NEW_IDENTIFIER(CHILD(n, i));
712 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000713 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000714 e = Attribute(e, id, Load, LINENO(CHILD(n, i)), c->c_arena);
715 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000716 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717 }
718
719 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720}
721
722static expr_ty
723ast_for_decorator(struct compiling *c, const node *n)
724{
725 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
726 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000727 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728
729 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000730 REQ(CHILD(n, 0), AT);
731 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732
733 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
734 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000735 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
737 if (NCH(n) == 3) { /* No arguments */
738 d = name_expr;
739 name_expr = NULL;
740 }
741 else if (NCH(n) == 5) { /* Call with no arguments */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000742 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000744 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 name_expr = NULL;
746 }
747 else {
748 d = ast_for_call(c, CHILD(n, 3), name_expr);
749 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000750 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 name_expr = NULL;
752 }
753
754 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755}
756
757static asdl_seq*
758ast_for_decorators(struct compiling *c, const node *n)
759{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000760 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000761 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762 int i;
763
764 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000765 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 if (!decorator_seq)
767 return NULL;
768
769 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000770 d = ast_for_decorator(c, CHILD(n, i));
771 if (!d)
772 return NULL;
773 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 }
775 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776}
777
778static stmt_ty
779ast_for_funcdef(struct compiling *c, const node *n)
780{
781 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000782 identifier name;
783 arguments_ty args;
784 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 asdl_seq *decorator_seq = NULL;
786 int name_i;
787
788 REQ(n, funcdef);
789
790 if (NCH(n) == 6) { /* decorators are present */
791 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
792 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000793 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 name_i = 2;
795 }
796 else {
797 name_i = 1;
798 }
799
800 name = NEW_IDENTIFIER(CHILD(n, name_i));
801 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000802 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000804 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000805 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
807 args = ast_for_arguments(c, CHILD(n, name_i + 1));
808 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 body = ast_for_suite(c, CHILD(n, name_i + 3));
811 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000812 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000814 return FunctionDef(name, args, body, decorator_seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815}
816
817static expr_ty
818ast_for_lambdef(struct compiling *c, const node *n)
819{
820 /* lambdef: 'lambda' [varargslist] ':' test */
821 arguments_ty args;
822 expr_ty expression;
823
824 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000825 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 if (!args)
827 return NULL;
828 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000829 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 }
832 else {
833 args = ast_for_arguments(c, CHILD(n, 1));
834 if (!args)
835 return NULL;
836 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000837 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 }
840
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000841 return Lambda(args, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842}
843
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000844static expr_ty
845ast_for_ifexpr(struct compiling *c, const node *n)
846{
847 /* test: or_test 'if' or_test 'else' test */
848 expr_ty expression, body, orelse;
849
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000850 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000851 body = ast_for_expr(c, CHILD(n, 0));
852 if (!body)
853 return NULL;
854 expression = ast_for_expr(c, CHILD(n, 2));
855 if (!expression)
856 return NULL;
857 orelse = ast_for_expr(c, CHILD(n, 4));
858 if (!orelse)
859 return NULL;
860 return IfExp(expression, body, orelse, LINENO(n), c->c_arena);
861}
862
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863/* Count the number of 'for' loop in a list comprehension.
864
865 Helper for ast_for_listcomp().
866*/
867
868static int
869count_list_fors(const node *n)
870{
871 int n_fors = 0;
872 node *ch = CHILD(n, 1);
873
874 count_list_for:
875 n_fors++;
876 REQ(ch, list_for);
877 if (NCH(ch) == 5)
878 ch = CHILD(ch, 4);
879 else
880 return n_fors;
881 count_list_iter:
882 REQ(ch, list_iter);
883 ch = CHILD(ch, 0);
884 if (TYPE(ch) == list_for)
885 goto count_list_for;
886 else if (TYPE(ch) == list_if) {
887 if (NCH(ch) == 3) {
888 ch = CHILD(ch, 2);
889 goto count_list_iter;
890 }
891 else
892 return n_fors;
893 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000894
895 /* Should never be reached */
896 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
897 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898}
899
900/* Count the number of 'if' statements in a list comprehension.
901
902 Helper for ast_for_listcomp().
903*/
904
905static int
906count_list_ifs(const node *n)
907{
908 int n_ifs = 0;
909
910 count_list_iter:
911 REQ(n, list_iter);
912 if (TYPE(CHILD(n, 0)) == list_for)
913 return n_ifs;
914 n = CHILD(n, 0);
915 REQ(n, list_if);
916 n_ifs++;
917 if (NCH(n) == 2)
918 return n_ifs;
919 n = CHILD(n, 2);
920 goto count_list_iter;
921}
922
923static expr_ty
924ast_for_listcomp(struct compiling *c, const node *n)
925{
926 /* listmaker: test ( list_for | (',' test)* [','] )
927 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
928 list_iter: list_for | list_if
929 list_if: 'if' test [list_iter]
930 testlist_safe: test [(',' test)+ [',']]
931 */
932 expr_ty elt;
933 asdl_seq *listcomps;
934 int i, n_fors;
935 node *ch;
936
937 REQ(n, listmaker);
938 assert(NCH(n) > 1);
939
940 elt = ast_for_expr(c, CHILD(n, 0));
941 if (!elt)
942 return NULL;
943
944 n_fors = count_list_fors(n);
945 if (n_fors == -1)
946 return NULL;
947
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000948 listcomps = asdl_seq_new(n_fors, c->c_arena);
949 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000951
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 ch = CHILD(n, 1);
953 for (i = 0; i < n_fors; i++) {
954 comprehension_ty lc;
955 asdl_seq *t;
956 expr_ty expression;
957
958 REQ(ch, list_for);
959
960 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000961 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000963 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000964 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000967 if (asdl_seq_LEN(t) == 1)
968 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
969 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000971 lc = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
972 expression, NULL, c->c_arena);
973 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975
976 if (NCH(ch) == 5) {
977 int j, n_ifs;
978 asdl_seq *ifs;
979
980 ch = CHILD(ch, 4);
981 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000982 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000985 ifs = asdl_seq_new(n_ifs, c->c_arena);
986 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988
989 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000990 REQ(ch, list_iter);
991 ch = CHILD(ch, 0);
992 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993
Jeremy Hyltona8293132006-02-28 17:58:27 +0000994 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
995 if (NCH(ch) == 3)
996 ch = CHILD(ch, 2);
997 }
998 /* on exit, must guarantee that ch is a list_for */
999 if (TYPE(ch) == list_iter)
1000 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001002 }
1003 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 }
1005
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001006 return ListComp(elt, listcomps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007}
1008
1009/*
1010 Count the number of 'for' loops in a generator expression.
1011
1012 Helper for ast_for_genexp().
1013*/
1014
1015static int
1016count_gen_fors(const node *n)
1017{
1018 int n_fors = 0;
1019 node *ch = CHILD(n, 1);
1020
1021 count_gen_for:
1022 n_fors++;
1023 REQ(ch, gen_for);
1024 if (NCH(ch) == 5)
1025 ch = CHILD(ch, 4);
1026 else
1027 return n_fors;
1028 count_gen_iter:
1029 REQ(ch, gen_iter);
1030 ch = CHILD(ch, 0);
1031 if (TYPE(ch) == gen_for)
1032 goto count_gen_for;
1033 else if (TYPE(ch) == gen_if) {
1034 if (NCH(ch) == 3) {
1035 ch = CHILD(ch, 2);
1036 goto count_gen_iter;
1037 }
1038 else
1039 return n_fors;
1040 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001041
1042 /* Should never be reached */
1043 PyErr_SetString(PyExc_SystemError,
1044 "logic error in count_gen_fors");
1045 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046}
1047
1048/* Count the number of 'if' statements in a generator expression.
1049
1050 Helper for ast_for_genexp().
1051*/
1052
1053static int
1054count_gen_ifs(const node *n)
1055{
1056 int n_ifs = 0;
1057
1058 while (1) {
1059 REQ(n, gen_iter);
1060 if (TYPE(CHILD(n, 0)) == gen_for)
1061 return n_ifs;
1062 n = CHILD(n, 0);
1063 REQ(n, gen_if);
1064 n_ifs++;
1065 if (NCH(n) == 2)
1066 return n_ifs;
1067 n = CHILD(n, 2);
1068 }
1069}
1070
Jeremy Hyltona8293132006-02-28 17:58:27 +00001071/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072static expr_ty
1073ast_for_genexp(struct compiling *c, const node *n)
1074{
1075 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1076 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1077 expr_ty elt;
1078 asdl_seq *genexps;
1079 int i, n_fors;
1080 node *ch;
1081
1082 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1083 assert(NCH(n) > 1);
1084
1085 elt = ast_for_expr(c, CHILD(n, 0));
1086 if (!elt)
1087 return NULL;
1088
1089 n_fors = count_gen_fors(n);
1090 if (n_fors == -1)
1091 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001092
1093 genexps = asdl_seq_new(n_fors, c->c_arena);
1094 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 ch = CHILD(n, 1);
1098 for (i = 0; i < n_fors; i++) {
1099 comprehension_ty ge;
1100 asdl_seq *t;
1101 expr_ty expression;
1102
1103 REQ(ch, gen_for);
1104
1105 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001106 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001108 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001109 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001111
1112 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001114 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001116 ge = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
1117 expression, NULL, c->c_arena);
1118
1119 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 if (NCH(ch) == 5) {
1123 int j, n_ifs;
1124 asdl_seq *ifs;
1125
1126 ch = CHILD(ch, 4);
1127 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001128 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001130
1131 ifs = asdl_seq_new(n_ifs, c->c_arena);
1132 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001134
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 for (j = 0; j < n_ifs; j++) {
1136 REQ(ch, gen_iter);
1137 ch = CHILD(ch, 0);
1138 REQ(ch, gen_if);
1139
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001140 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001141 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001142 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001143 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 if (NCH(ch) == 3)
1145 ch = CHILD(ch, 2);
1146 }
1147 /* on exit, must guarantee that ch is a gen_for */
1148 if (TYPE(ch) == gen_iter)
1149 ch = CHILD(ch, 0);
1150 ge->ifs = ifs;
1151 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001152 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 }
1154
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001155 return GeneratorExp(elt, genexps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156}
1157
1158static expr_ty
1159ast_for_atom(struct compiling *c, const node *n)
1160{
1161 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1162 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1163 */
1164 node *ch = CHILD(n, 0);
1165
1166 switch (TYPE(ch)) {
1167 case NAME:
1168 /* All names start in Load context, but may later be
1169 changed. */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001170 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 case STRING: {
1172 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 if (!str)
1174 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001175
1176 PyArena_AddPyObject(c->c_arena, str);
1177 return Str(str, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 }
1179 case NUMBER: {
1180 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 if (!pynum)
1182 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001183
1184 PyArena_AddPyObject(c->c_arena, pynum);
1185 return Num(pynum, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 }
1187 case LPAR: /* some parenthesized expressions */
1188 ch = CHILD(n, 1);
1189
1190 if (TYPE(ch) == RPAR)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001191 return Tuple(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192
1193 if (TYPE(ch) == yield_expr)
1194 return ast_for_expr(c, ch);
1195
1196 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1197 return ast_for_genexp(c, ch);
1198
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001199 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 case LSQB: /* list (or list comprehension) */
1201 ch = CHILD(n, 1);
1202
1203 if (TYPE(ch) == RSQB)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204 return List(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205
1206 REQ(ch, listmaker);
1207 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1208 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 if (!elts)
1210 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001211
1212 return List(elts, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 }
1214 else
1215 return ast_for_listcomp(c, ch);
1216 case LBRACE: {
1217 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1218 int i, size;
1219 asdl_seq *keys, *values;
1220
1221 ch = CHILD(n, 1);
1222 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001223 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 if (!keys)
1225 return NULL;
1226
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001227 values = asdl_seq_new(size, c->c_arena);
1228 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230
1231 for (i = 0; i < NCH(ch); i += 4) {
1232 expr_ty expression;
1233
1234 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001235 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001237
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001243
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 asdl_seq_SET(values, i / 4, expression);
1245 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001246 return Dict(keys, values, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 }
1248 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001249 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 if (!expression)
1251 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252
1253 return Repr(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 }
1255 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001256 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 return NULL;
1258 }
1259}
1260
1261static slice_ty
1262ast_for_slice(struct compiling *c, const node *n)
1263{
1264 node *ch;
1265 expr_ty lower = NULL, upper = NULL, step = NULL;
1266
1267 REQ(n, subscript);
1268
1269 /*
1270 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1271 sliceop: ':' [test]
1272 */
1273 ch = CHILD(n, 0);
1274 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001275 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276
1277 if (NCH(n) == 1 && TYPE(ch) == test) {
1278 /* 'step' variable hold no significance in terms of being used over
1279 other vars */
1280 step = ast_for_expr(c, ch);
1281 if (!step)
1282 return NULL;
1283
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001284 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 }
1286
1287 if (TYPE(ch) == test) {
1288 lower = ast_for_expr(c, ch);
1289 if (!lower)
1290 return NULL;
1291 }
1292
1293 /* If there's an upper bound it's in the second or third position. */
1294 if (TYPE(ch) == COLON) {
1295 if (NCH(n) > 1) {
1296 node *n2 = CHILD(n, 1);
1297
1298 if (TYPE(n2) == test) {
1299 upper = ast_for_expr(c, n2);
1300 if (!upper)
1301 return NULL;
1302 }
1303 }
1304 } else if (NCH(n) > 2) {
1305 node *n2 = CHILD(n, 2);
1306
1307 if (TYPE(n2) == test) {
1308 upper = ast_for_expr(c, n2);
1309 if (!upper)
1310 return NULL;
1311 }
1312 }
1313
1314 ch = CHILD(n, NCH(n) - 1);
1315 if (TYPE(ch) == sliceop) {
1316 if (NCH(ch) == 1)
1317 /* XXX: If only 1 child, then should just be a colon. Should we
1318 just skip assigning and just get to the return? */
1319 ch = CHILD(ch, 0);
1320 else
1321 ch = CHILD(ch, 1);
1322 if (TYPE(ch) == test) {
1323 step = ast_for_expr(c, ch);
1324 if (!step)
1325 return NULL;
1326 }
1327 }
1328
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001329 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330}
1331
1332static expr_ty
1333ast_for_binop(struct compiling *c, const node *n)
1334{
1335 /* Must account for a sequence of expressions.
1336 How should A op B op C by represented?
1337 BinOp(BinOp(A, op, B), op, C).
1338 */
1339
1340 int i, nops;
1341 expr_ty expr1, expr2, result;
1342 operator_ty operator;
1343
1344 expr1 = ast_for_expr(c, CHILD(n, 0));
1345 if (!expr1)
1346 return NULL;
1347
1348 expr2 = ast_for_expr(c, CHILD(n, 2));
1349 if (!expr2)
1350 return NULL;
1351
1352 operator = get_operator(CHILD(n, 1));
1353 if (!operator)
1354 return NULL;
1355
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001356 result = BinOp(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 if (!result)
1358 return NULL;
1359
1360 nops = (NCH(n) - 1) / 2;
1361 for (i = 1; i < nops; i++) {
1362 expr_ty tmp_result, tmp;
1363 const node* next_oper = CHILD(n, i * 2 + 1);
1364
1365 operator = get_operator(next_oper);
1366 if (!operator)
1367 return NULL;
1368
1369 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1370 if (!tmp)
1371 return NULL;
1372
1373 tmp_result = BinOp(result, operator, tmp,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001374 LINENO(next_oper), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 if (!tmp)
1376 return NULL;
1377 result = tmp_result;
1378 }
1379 return result;
1380}
1381
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001382static expr_ty
1383ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1384{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001385 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1386 subscriptlist: subscript (',' subscript)* [',']
1387 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1388 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001389 REQ(n, trailer);
1390 if (TYPE(CHILD(n, 0)) == LPAR) {
1391 if (NCH(n) == 2)
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001392 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001393 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001394 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001395 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001396 else if (TYPE(CHILD(n, 0)) == DOT ) {
1397 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1398 LINENO(n), c->c_arena);
1399 }
1400 else {
1401 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001402 REQ(CHILD(n, 2), RSQB);
1403 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001404 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001405 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1406 if (!slc)
1407 return NULL;
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001408 return Subscript(left_expr, slc, Load, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001409 }
1410 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001411 /* The grammar is ambiguous here. The ambiguity is resolved
1412 by treating the sequence as a tuple literal if there are
1413 no slice features.
1414 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001415 int j;
1416 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001417 expr_ty e;
1418 bool simple;
1419 asdl_seq *slices, *elts;
1420 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001421 if (!slices)
1422 return NULL;
1423 for (j = 0; j < NCH(n); j += 2) {
1424 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001425 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001426 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001427 if (slc->kind != Index_kind)
1428 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001429 asdl_seq_SET(slices, j / 2, slc);
1430 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001431 if (!simple) {
1432 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1433 Load, LINENO(n), c->c_arena);
1434 }
1435 /* extract Index values and put them in a Tuple */
1436 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1437 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1438 slc = (slice_ty)asdl_seq_GET(slices, j);
1439 assert(slc->kind == Index_kind && slc->v.Index.value);
1440 asdl_seq_SET(elts, j, slc->v.Index.value);
1441 }
1442 e = Tuple(elts, Load, LINENO(n), c->c_arena);
1443 if (!e)
1444 return NULL;
1445 return Subscript(left_expr, Index(e, c->c_arena),
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001446 Load, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001447 }
1448 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001449}
1450
1451static expr_ty
1452ast_for_power(struct compiling *c, const node *n)
1453{
1454 /* power: atom trailer* ('**' factor)*
1455 */
1456 int i;
1457 expr_ty e, tmp;
1458 REQ(n, power);
1459 e = ast_for_atom(c, CHILD(n, 0));
1460 if (!e)
1461 return NULL;
1462 if (NCH(n) == 1)
1463 return e;
1464 for (i = 1; i < NCH(n); i++) {
1465 node *ch = CHILD(n, i);
1466 if (TYPE(ch) != trailer)
1467 break;
1468 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001469 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001470 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001471 e = tmp;
1472 }
1473 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1474 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001475 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001476 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001477 tmp = BinOp(e, Pow, f, LINENO(n), c->c_arena);
1478 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001479 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001480 e = tmp;
1481 }
1482 return e;
1483}
1484
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485/* Do not name a variable 'expr'! Will cause a compile error.
1486*/
1487
1488static expr_ty
1489ast_for_expr(struct compiling *c, const node *n)
1490{
1491 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001492 test: or_test ['if' or_test 'else' test] | lambdef
1493 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 and_test: not_test ('and' not_test)*
1495 not_test: 'not' not_test | comparison
1496 comparison: expr (comp_op expr)*
1497 expr: xor_expr ('|' xor_expr)*
1498 xor_expr: and_expr ('^' and_expr)*
1499 and_expr: shift_expr ('&' shift_expr)*
1500 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1501 arith_expr: term (('+'|'-') term)*
1502 term: factor (('*'|'/'|'%'|'//') factor)*
1503 factor: ('+'|'-'|'~') factor | power
1504 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001505
1506 As well as modified versions that exist for backward compatibility,
1507 to explicitly allow:
1508 [ x for x in lambda: 0, lambda: 1 ]
1509 (which would be ambiguous without these extra rules)
1510
1511 old_test: or_test | old_lambdef
1512 old_lambdef: 'lambda' [vararglist] ':' old_test
1513
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 */
1515
1516 asdl_seq *seq;
1517 int i;
1518
1519 loop:
1520 switch (TYPE(n)) {
1521 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001522 case old_test:
1523 if (TYPE(CHILD(n, 0)) == lambdef ||
1524 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001526 else if (NCH(n) > 1)
1527 return ast_for_ifexpr(c, n);
1528 /* Fallthrough */
1529 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530 case and_test:
1531 if (NCH(n) == 1) {
1532 n = CHILD(n, 0);
1533 goto loop;
1534 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001535 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 if (!seq)
1537 return NULL;
1538 for (i = 0; i < NCH(n); i += 2) {
1539 expr_ty e = ast_for_expr(c, CHILD(n, i));
1540 if (!e)
1541 return NULL;
1542 asdl_seq_SET(seq, i / 2, e);
1543 }
1544 if (!strcmp(STR(CHILD(n, 1)), "and"))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001545 return BoolOp(And, seq, LINENO(n), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001546 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1547 return BoolOp(Or, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 case not_test:
1549 if (NCH(n) == 1) {
1550 n = CHILD(n, 0);
1551 goto loop;
1552 }
1553 else {
1554 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1555 if (!expression)
1556 return NULL;
1557
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001558 return UnaryOp(Not, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 }
1560 case comparison:
1561 if (NCH(n) == 1) {
1562 n = CHILD(n, 0);
1563 goto loop;
1564 }
1565 else {
1566 expr_ty expression;
1567 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001568 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 if (!ops)
1570 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001571 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573 return NULL;
1574 }
1575 for (i = 1; i < NCH(n); i += 2) {
1576 /* XXX cmpop_ty is just an enum */
1577 cmpop_ty operator;
1578
1579 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001580 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001582 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583
1584 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001585 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001587 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001589 asdl_seq_SET(ops, i / 2, (void *)(Py_uintptr_t)operator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590 asdl_seq_SET(cmps, i / 2, expression);
1591 }
1592 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001593 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001595 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001597 return Compare(expression, ops, cmps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 }
1599 break;
1600
1601 /* The next five cases all handle BinOps. The main body of code
1602 is the same in each case, but the switch turned inside out to
1603 reuse the code for each type of operator.
1604 */
1605 case expr:
1606 case xor_expr:
1607 case and_expr:
1608 case shift_expr:
1609 case arith_expr:
1610 case term:
1611 if (NCH(n) == 1) {
1612 n = CHILD(n, 0);
1613 goto loop;
1614 }
1615 return ast_for_binop(c, n);
1616 case yield_expr: {
1617 expr_ty exp = NULL;
1618 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001619 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 if (!exp)
1621 return NULL;
1622 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001623 return Yield(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624 }
1625 case factor: {
1626 expr_ty expression;
1627
1628 if (NCH(n) == 1) {
1629 n = CHILD(n, 0);
1630 goto loop;
1631 }
1632
1633 expression = ast_for_expr(c, CHILD(n, 1));
1634 if (!expression)
1635 return NULL;
1636
1637 switch (TYPE(CHILD(n, 0))) {
1638 case PLUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001639 return UnaryOp(UAdd, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 case MINUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001641 return UnaryOp(USub, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642 case TILDE:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001643 return UnaryOp(Invert, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001645 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1646 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 break;
1648 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001649 case power:
1650 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001652 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 return NULL;
1654 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001655 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 return NULL;
1657}
1658
1659static expr_ty
1660ast_for_call(struct compiling *c, const node *n, expr_ty func)
1661{
1662 /*
1663 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1664 | '**' test)
1665 argument: [test '='] test [gen_for] # Really [keyword '='] test
1666 */
1667
1668 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001669 asdl_seq *args;
1670 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 expr_ty vararg = NULL, kwarg = NULL;
1672
1673 REQ(n, arglist);
1674
1675 nargs = 0;
1676 nkeywords = 0;
1677 ngens = 0;
1678 for (i = 0; i < NCH(n); i++) {
1679 node *ch = CHILD(n, i);
1680 if (TYPE(ch) == argument) {
1681 if (NCH(ch) == 1)
1682 nargs++;
1683 else if (TYPE(CHILD(ch, 1)) == gen_for)
1684 ngens++;
1685 else
1686 nkeywords++;
1687 }
1688 }
1689 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001690 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 "if not sole argument");
1692 return NULL;
1693 }
1694
1695 if (nargs + nkeywords + ngens > 255) {
1696 ast_error(n, "more than 255 arguments");
1697 return NULL;
1698 }
1699
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001700 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001702 return NULL;
1703 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001705 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 nargs = 0;
1707 nkeywords = 0;
1708 for (i = 0; i < NCH(n); i++) {
1709 node *ch = CHILD(n, i);
1710 if (TYPE(ch) == argument) {
1711 expr_ty e;
1712 if (NCH(ch) == 1) {
1713 e = ast_for_expr(c, CHILD(ch, 0));
1714 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 asdl_seq_SET(args, nargs++, e);
1717 }
1718 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1719 e = ast_for_genexp(c, ch);
1720 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 asdl_seq_SET(args, nargs++, e);
1723 }
1724 else {
1725 keyword_ty kw;
1726 identifier key;
1727
1728 /* CHILD(ch, 0) is test, but must be an identifier? */
1729 e = ast_for_expr(c, CHILD(ch, 0));
1730 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001731 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 /* f(lambda x: x[0] = 3) ends up getting parsed with
1733 * LHS test = lambda x: x[0], and RHS test = 3.
1734 * SF bug 132313 points out that complaining about a keyword
1735 * then is very confusing.
1736 */
1737 if (e->kind == Lambda_kind) {
1738 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 } else if (e->kind != Name_kind) {
1741 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 }
1744 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 e = ast_for_expr(c, CHILD(ch, 2));
1746 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001747 return NULL;
1748 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001750 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751 asdl_seq_SET(keywords, nkeywords++, kw);
1752 }
1753 }
1754 else if (TYPE(ch) == STAR) {
1755 vararg = ast_for_expr(c, CHILD(n, i+1));
1756 i++;
1757 }
1758 else if (TYPE(ch) == DOUBLESTAR) {
1759 kwarg = ast_for_expr(c, CHILD(n, i+1));
1760 i++;
1761 }
1762 }
1763
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001764 return Call(func, args, keywords, vararg, kwarg, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765}
1766
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001768ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001770 /* testlist_gexp: test (',' test)* [','] */
1771 /* testlist: test (',' test)* [','] */
1772 /* testlist_safe: test (',' test)+ [','] */
1773 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001775 if (TYPE(n) == testlist_gexp) {
1776 if (NCH(n) > 1)
1777 assert(TYPE(CHILD(n, 1)) != gen_for);
1778 }
1779 else {
1780 assert(TYPE(n) == testlist ||
1781 TYPE(n) == testlist_safe ||
1782 TYPE(n) == testlist1);
1783 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784 if (NCH(n) == 1)
1785 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786 else {
1787 asdl_seq *tmp = seq_for_testlist(c, n);
1788 if (!tmp)
1789 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001790 return Tuple(tmp, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001792}
1793
1794static expr_ty
1795ast_for_testlist_gexp(struct compiling *c, const node* n)
1796{
1797 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1798 /* argument: test [ gen_for ] */
1799 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001800 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001801 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001802 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001803}
1804
1805/* like ast_for_testlist() but returns a sequence */
1806static asdl_seq*
1807ast_for_class_bases(struct compiling *c, const node* n)
1808{
1809 /* testlist: test (',' test)* [','] */
1810 assert(NCH(n) > 0);
1811 REQ(n, testlist);
1812 if (NCH(n) == 1) {
1813 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001814 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001815 if (!bases)
1816 return NULL;
1817 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001818 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001819 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001820 asdl_seq_SET(bases, 0, base);
1821 return bases;
1822 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001823
1824 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825}
1826
1827static stmt_ty
1828ast_for_expr_stmt(struct compiling *c, const node *n)
1829{
1830 REQ(n, expr_stmt);
1831 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1832 | ('=' (yield_expr|testlist))*)
1833 testlist: test (',' test)* [',']
1834 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1835 | '<<=' | '>>=' | '**=' | '//='
1836 test: ... here starts the operator precendence dance
1837 */
1838
1839 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001840 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 if (!e)
1842 return NULL;
1843
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844 return Expr(e, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 }
1846 else if (TYPE(CHILD(n, 1)) == augassign) {
1847 expr_ty expr1, expr2;
1848 operator_ty operator;
1849 node *ch = CHILD(n, 0);
1850
1851 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001852 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001854 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch),
1855 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856
1857 if (!expr1)
1858 return NULL;
Neal Norwitz96e48d42006-02-05 02:07:19 +00001859 /* TODO(jhylton): Figure out why set_context() can't be used here. */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001860 switch (expr1->kind) {
1861 case GeneratorExp_kind:
1862 ast_error(ch, "augmented assignment to generator "
1863 "expression not possible");
1864 return NULL;
1865 case Name_kind: {
1866 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1867 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1868 ast_error(ch, "assignment to None");
1869 return NULL;
1870 }
1871 break;
1872 }
1873 case Attribute_kind:
1874 case Subscript_kind:
1875 break;
1876 default:
1877 ast_error(ch, "illegal expression for augmented "
1878 "assignment");
1879 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881
1882 ch = CHILD(n, 2);
1883 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001884 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001887 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 return NULL;
1889
1890 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001891 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 return NULL;
1893
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894 return AugAssign(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 }
1896 else {
1897 int i;
1898 asdl_seq *targets;
1899 node *value;
1900 expr_ty expression;
1901
1902 /* a normal assignment */
1903 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001904 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 if (!targets)
1906 return NULL;
1907 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001908 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 node *ch = CHILD(n, i);
1910 if (TYPE(ch) == yield_expr) {
1911 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001912 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001914 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915
1916 /* set context to assign */
1917 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001918 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919
Neal Norwitz84456bd2005-12-18 03:16:20 +00001920 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001921 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922
1923 asdl_seq_SET(targets, i / 2, e);
1924 }
1925 value = CHILD(n, NCH(n) - 1);
1926 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001927 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 else
1929 expression = ast_for_expr(c, value);
1930 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001931 return NULL;
1932 return Assign(targets, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934}
1935
1936static stmt_ty
1937ast_for_print_stmt(struct compiling *c, const node *n)
1938{
1939 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1940 | '>>' test [ (',' test)+ [','] ] )
1941 */
1942 expr_ty dest = NULL, expression;
1943 asdl_seq *seq;
1944 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001945 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946
1947 REQ(n, print_stmt);
1948 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1949 dest = ast_for_expr(c, CHILD(n, 2));
1950 if (!dest)
1951 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001952 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001954 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00001956 return NULL;
1957 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001959 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001961 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 }
1963 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001964 return Print(dest, seq, nl, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965}
1966
1967static asdl_seq *
1968ast_for_exprlist(struct compiling *c, const node *n, int context)
1969{
1970 asdl_seq *seq;
1971 int i;
1972 expr_ty e;
1973
1974 REQ(n, exprlist);
1975
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001976 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 if (!seq)
1978 return NULL;
1979 for (i = 0; i < NCH(n); i += 2) {
1980 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001981 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00001983 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001984 if (context && !set_context(e, context, CHILD(n, i)))
1985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 }
1987 return seq;
1988}
1989
1990static stmt_ty
1991ast_for_del_stmt(struct compiling *c, const node *n)
1992{
1993 asdl_seq *expr_list;
1994
1995 /* del_stmt: 'del' exprlist */
1996 REQ(n, del_stmt);
1997
1998 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
1999 if (!expr_list)
2000 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002001 return Delete(expr_list, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002}
2003
2004static stmt_ty
2005ast_for_flow_stmt(struct compiling *c, const node *n)
2006{
2007 /*
2008 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2009 | yield_stmt
2010 break_stmt: 'break'
2011 continue_stmt: 'continue'
2012 return_stmt: 'return' [testlist]
2013 yield_stmt: yield_expr
2014 yield_expr: 'yield' testlist
2015 raise_stmt: 'raise' [test [',' test [',' test]]]
2016 */
2017 node *ch;
2018
2019 REQ(n, flow_stmt);
2020 ch = CHILD(n, 0);
2021 switch (TYPE(ch)) {
2022 case break_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002023 return Break(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 case continue_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002025 return Continue(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026 case yield_stmt: { /* will reduce to yield_expr */
2027 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2028 if (!exp)
2029 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002030 return Expr(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 }
2032 case return_stmt:
2033 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002034 return Return(NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002036 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 if (!expression)
2038 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002039 return Return(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 }
2041 case raise_stmt:
2042 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002043 return Raise(NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044 else if (NCH(ch) == 2) {
2045 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2046 if (!expression)
2047 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002048 return Raise(expression, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 }
2050 else if (NCH(ch) == 4) {
2051 expr_ty expr1, expr2;
2052
2053 expr1 = ast_for_expr(c, CHILD(ch, 1));
2054 if (!expr1)
2055 return NULL;
2056 expr2 = ast_for_expr(c, CHILD(ch, 3));
2057 if (!expr2)
2058 return NULL;
2059
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002060 return Raise(expr1, expr2, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 }
2062 else if (NCH(ch) == 6) {
2063 expr_ty expr1, expr2, expr3;
2064
2065 expr1 = ast_for_expr(c, CHILD(ch, 1));
2066 if (!expr1)
2067 return NULL;
2068 expr2 = ast_for_expr(c, CHILD(ch, 3));
2069 if (!expr2)
2070 return NULL;
2071 expr3 = ast_for_expr(c, CHILD(ch, 5));
2072 if (!expr3)
2073 return NULL;
2074
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002075 return Raise(expr1, expr2, expr3, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 }
2077 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002078 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 "unexpected flow_stmt: %d", TYPE(ch));
2080 return NULL;
2081 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002082
2083 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2084 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085}
2086
2087static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002088alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089{
2090 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002091 import_as_name: NAME ['as' NAME]
2092 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 dotted_name: NAME ('.' NAME)*
2094 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002095 PyObject *str;
2096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 loop:
2098 switch (TYPE(n)) {
2099 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002100 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2101 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 case dotted_as_name:
2103 if (NCH(n) == 1) {
2104 n = CHILD(n, 0);
2105 goto loop;
2106 }
2107 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002108 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 assert(!a->asname);
2110 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2111 return a;
2112 }
2113 break;
2114 case dotted_name:
2115 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002116 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 else {
2118 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002119 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002120 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 char *s;
2122
2123 len = 0;
2124 for (i = 0; i < NCH(n); i += 2)
2125 /* length of string plus one for the dot */
2126 len += strlen(STR(CHILD(n, i))) + 1;
2127 len--; /* the last name doesn't have a dot */
2128 str = PyString_FromStringAndSize(NULL, len);
2129 if (!str)
2130 return NULL;
2131 s = PyString_AS_STRING(str);
2132 if (!s)
2133 return NULL;
2134 for (i = 0; i < NCH(n); i += 2) {
2135 char *sch = STR(CHILD(n, i));
2136 strcpy(s, STR(CHILD(n, i)));
2137 s += strlen(sch);
2138 *s++ = '.';
2139 }
2140 --s;
2141 *s = '\0';
2142 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002143 PyArena_AddPyObject(c->c_arena, str);
2144 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 }
2146 break;
2147 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002148 str = PyString_InternFromString("*");
2149 PyArena_AddPyObject(c->c_arena, str);
2150 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002152 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 "unexpected import name: %d", TYPE(n));
2154 return NULL;
2155 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002156
2157 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 return NULL;
2159}
2160
2161static stmt_ty
2162ast_for_import_stmt(struct compiling *c, const node *n)
2163{
2164 /*
2165 import_stmt: import_name | import_from
2166 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002167 import_from: 'from' ('.'* dotted_name | '.') 'import'
2168 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 */
2170 int i;
2171 asdl_seq *aliases;
2172
2173 REQ(n, import_stmt);
2174 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002175 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002177 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002178 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 if (!aliases)
2180 return NULL;
2181 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002182 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002183 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 asdl_seq_SET(aliases, i / 2, import_alias);
2186 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002187 return Import(aliases, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002189 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 int n_children;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 int lineno = LINENO(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002192 int idx, ndots = 0;
2193 alias_ty mod = NULL;
2194 identifier modname;
2195
2196 /* Count the number of dots (for relative imports) and check for the
2197 optional module name */
2198 for (idx = 1; idx < NCH(n); idx++) {
2199 if (TYPE(CHILD(n, idx)) == dotted_name) {
2200 mod = alias_for_import_name(c, CHILD(n, idx));
2201 idx++;
2202 break;
2203 } else if (TYPE(CHILD(n, idx)) != DOT) {
2204 break;
2205 }
2206 ndots++;
2207 }
2208 idx++; /* skip over the 'import' keyword */
2209 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002210 case STAR:
2211 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002212 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002213 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002214 if (ndots) {
2215 ast_error(n, "'import *' not allowed with 'from .'");
2216 return NULL;
2217 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002218 break;
2219 case LPAR:
2220 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002221 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002222 n_children = NCH(n);
2223 break;
2224 case import_as_names:
2225 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002226 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002227 n_children = NCH(n);
2228 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 ast_error(n, "trailing comma not allowed without"
2230 " surrounding parentheses");
2231 return NULL;
2232 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002233 break;
2234 default:
2235 ast_error(n, "Unexpected node-type in from-import");
2236 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002237 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002239 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002240 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242
2243 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002244 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002245 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002246 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002248 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002250 else {
2251 for (i = 0; i < NCH(n); i += 2) {
2252 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2253 if (!import_alias)
2254 return NULL;
2255 asdl_seq_SET(aliases, i / 2, import_alias);
2256 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002258 if (mod != NULL)
2259 modname = mod->name;
2260 else
2261 modname = new_identifier("", c->c_arena);
2262 return ImportFrom(modname, aliases, ndots, lineno,
2263 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 }
Neal Norwitz79792652005-11-14 04:25:03 +00002265 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 "unknown import statement: starts with command '%s'",
2267 STR(CHILD(n, 0)));
2268 return NULL;
2269}
2270
2271static stmt_ty
2272ast_for_global_stmt(struct compiling *c, const node *n)
2273{
2274 /* global_stmt: 'global' NAME (',' NAME)* */
2275 identifier name;
2276 asdl_seq *s;
2277 int i;
2278
2279 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002280 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 if (!s)
2282 return NULL;
2283 for (i = 1; i < NCH(n); i += 2) {
2284 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002285 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 asdl_seq_SET(s, i / 2, name);
2288 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002289 return Global(s, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290}
2291
2292static stmt_ty
2293ast_for_exec_stmt(struct compiling *c, const node *n)
2294{
2295 expr_ty expr1, globals = NULL, locals = NULL;
2296 int n_children = NCH(n);
2297 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002298 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 "poorly formed 'exec' statement: %d parts to statement",
2300 n_children);
2301 return NULL;
2302 }
2303
2304 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2305 REQ(n, exec_stmt);
2306 expr1 = ast_for_expr(c, CHILD(n, 1));
2307 if (!expr1)
2308 return NULL;
2309 if (n_children >= 4) {
2310 globals = ast_for_expr(c, CHILD(n, 3));
2311 if (!globals)
2312 return NULL;
2313 }
2314 if (n_children == 6) {
2315 locals = ast_for_expr(c, CHILD(n, 5));
2316 if (!locals)
2317 return NULL;
2318 }
2319
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002320 return Exec(expr1, globals, locals, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321}
2322
2323static stmt_ty
2324ast_for_assert_stmt(struct compiling *c, const node *n)
2325{
2326 /* assert_stmt: 'assert' test [',' test] */
2327 REQ(n, assert_stmt);
2328 if (NCH(n) == 2) {
2329 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2330 if (!expression)
2331 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002332 return Assert(expression, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 }
2334 else if (NCH(n) == 4) {
2335 expr_ty expr1, expr2;
2336
2337 expr1 = ast_for_expr(c, CHILD(n, 1));
2338 if (!expr1)
2339 return NULL;
2340 expr2 = ast_for_expr(c, CHILD(n, 3));
2341 if (!expr2)
2342 return NULL;
2343
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002344 return Assert(expr1, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 }
Neal Norwitz79792652005-11-14 04:25:03 +00002346 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 "improper number of parts to 'assert' statement: %d",
2348 NCH(n));
2349 return NULL;
2350}
2351
2352static asdl_seq *
2353ast_for_suite(struct compiling *c, const node *n)
2354{
2355 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002356 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357 stmt_ty s;
2358 int i, total, num, end, pos = 0;
2359 node *ch;
2360
2361 REQ(n, suite);
2362
2363 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002364 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365 if (!seq)
2366 return NULL;
2367 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2368 n = CHILD(n, 0);
2369 /* simple_stmt always ends with a NEWLINE,
2370 and may have a trailing SEMI
2371 */
2372 end = NCH(n) - 1;
2373 if (TYPE(CHILD(n, end - 1)) == SEMI)
2374 end--;
2375 /* loop by 2 to skip semi-colons */
2376 for (i = 0; i < end; i += 2) {
2377 ch = CHILD(n, i);
2378 s = ast_for_stmt(c, ch);
2379 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002380 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381 asdl_seq_SET(seq, pos++, s);
2382 }
2383 }
2384 else {
2385 for (i = 2; i < (NCH(n) - 1); i++) {
2386 ch = CHILD(n, i);
2387 REQ(ch, stmt);
2388 num = num_stmts(ch);
2389 if (num == 1) {
2390 /* small_stmt or compound_stmt with only one child */
2391 s = ast_for_stmt(c, ch);
2392 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002393 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 asdl_seq_SET(seq, pos++, s);
2395 }
2396 else {
2397 int j;
2398 ch = CHILD(ch, 0);
2399 REQ(ch, simple_stmt);
2400 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002401 /* statement terminates with a semi-colon ';' */
2402 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002403 assert((j + 1) == NCH(ch));
2404 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002405 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 s = ast_for_stmt(c, CHILD(ch, j));
2407 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002408 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409 asdl_seq_SET(seq, pos++, s);
2410 }
2411 }
2412 }
2413 }
2414 assert(pos == seq->size);
2415 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416}
2417
2418static stmt_ty
2419ast_for_if_stmt(struct compiling *c, const node *n)
2420{
2421 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2422 ['else' ':' suite]
2423 */
2424 char *s;
2425
2426 REQ(n, if_stmt);
2427
2428 if (NCH(n) == 4) {
2429 expr_ty expression;
2430 asdl_seq *suite_seq;
2431
2432 expression = ast_for_expr(c, CHILD(n, 1));
2433 if (!expression)
2434 return NULL;
2435 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002436 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 return NULL;
2438
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002439 return If(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002441
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 s = STR(CHILD(n, 4));
2443 /* s[2], the third character in the string, will be
2444 's' for el_s_e, or
2445 'i' for el_i_f
2446 */
2447 if (s[2] == 's') {
2448 expr_ty expression;
2449 asdl_seq *seq1, *seq2;
2450
2451 expression = ast_for_expr(c, CHILD(n, 1));
2452 if (!expression)
2453 return NULL;
2454 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002455 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 return NULL;
2457 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002458 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 return NULL;
2460
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002461 return If(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462 }
2463 else if (s[2] == 'i') {
2464 int i, n_elif, has_else = 0;
2465 asdl_seq *orelse = NULL;
2466 n_elif = NCH(n) - 4;
2467 /* must reference the child n_elif+1 since 'else' token is third,
2468 not fourth, child from the end. */
2469 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2470 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2471 has_else = 1;
2472 n_elif -= 3;
2473 }
2474 n_elif /= 4;
2475
2476 if (has_else) {
2477 expr_ty expression;
2478 asdl_seq *seq1, *seq2;
2479
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002480 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 if (!orelse)
2482 return NULL;
2483 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002484 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002487 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002490 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492
2493 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002494 LINENO(CHILD(n, NCH(n) - 6)),
2495 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 /* the just-created orelse handled the last elif */
2497 n_elif--;
2498 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499
2500 for (i = 0; i < n_elif; i++) {
2501 int off = 5 + (n_elif - i - 1) * 4;
2502 expr_ty expression;
2503 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002504 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002505 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 return NULL;
2507 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002508 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002511 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513
2514 asdl_seq_SET(new, 0,
2515 If(expression, suite_seq, orelse,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002516 LINENO(CHILD(n, off)), c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 orelse = new;
2518 }
2519 return If(ast_for_expr(c, CHILD(n, 1)),
2520 ast_for_suite(c, CHILD(n, 3)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002521 orelse, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002523
2524 PyErr_Format(PyExc_SystemError,
2525 "unexpected token in 'if' statement: %s", s);
2526 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527}
2528
2529static stmt_ty
2530ast_for_while_stmt(struct compiling *c, const node *n)
2531{
2532 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2533 REQ(n, while_stmt);
2534
2535 if (NCH(n) == 4) {
2536 expr_ty expression;
2537 asdl_seq *suite_seq;
2538
2539 expression = ast_for_expr(c, CHILD(n, 1));
2540 if (!expression)
2541 return NULL;
2542 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002543 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002545 return While(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 }
2547 else if (NCH(n) == 7) {
2548 expr_ty expression;
2549 asdl_seq *seq1, *seq2;
2550
2551 expression = ast_for_expr(c, CHILD(n, 1));
2552 if (!expression)
2553 return NULL;
2554 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002555 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 return NULL;
2557 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002558 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 return NULL;
2560
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002561 return While(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002563
2564 PyErr_Format(PyExc_SystemError,
2565 "wrong number of tokens for 'while' statement: %d",
2566 NCH(n));
2567 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568}
2569
2570static stmt_ty
2571ast_for_for_stmt(struct compiling *c, const node *n)
2572{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002573 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 expr_ty expression;
2575 expr_ty target;
2576 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2577 REQ(n, for_stmt);
2578
2579 if (NCH(n) == 9) {
2580 seq = ast_for_suite(c, CHILD(n, 8));
2581 if (!seq)
2582 return NULL;
2583 }
2584
2585 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002586 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002588 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002591 target = Tuple(_target, Store, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002593 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002594 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595 return NULL;
2596 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002597 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 return NULL;
2599
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002600 return For(target, expression, suite_seq, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601}
2602
2603static excepthandler_ty
2604ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2605{
2606 /* except_clause: 'except' [test [',' test]] */
2607 REQ(exc, except_clause);
2608 REQ(body, suite);
2609
2610 if (NCH(exc) == 1) {
2611 asdl_seq *suite_seq = ast_for_suite(c, body);
2612 if (!suite_seq)
2613 return NULL;
2614
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002615 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 }
2617 else if (NCH(exc) == 2) {
2618 expr_ty expression;
2619 asdl_seq *suite_seq;
2620
2621 expression = ast_for_expr(c, CHILD(exc, 1));
2622 if (!expression)
2623 return NULL;
2624 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002625 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 return NULL;
2627
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002628 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 }
2630 else if (NCH(exc) == 4) {
2631 asdl_seq *suite_seq;
2632 expr_ty expression;
2633 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2634 if (!e)
2635 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002636 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 return NULL;
2638 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002639 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 return NULL;
2641 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002642 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 return NULL;
2644
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002645 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002647
2648 PyErr_Format(PyExc_SystemError,
2649 "wrong number of children for 'except' clause: %d",
2650 NCH(exc));
2651 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652}
2653
2654static stmt_ty
2655ast_for_try_stmt(struct compiling *c, const node *n)
2656{
Neal Norwitzf599f422005-12-17 21:33:47 +00002657 const int nch = NCH(n);
2658 int n_except = (nch - 3)/3;
2659 asdl_seq *body, *orelse = NULL, *finally = NULL;
2660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 REQ(n, try_stmt);
2662
Neal Norwitzf599f422005-12-17 21:33:47 +00002663 body = ast_for_suite(c, CHILD(n, 2));
2664 if (body == NULL)
2665 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666
Neal Norwitzf599f422005-12-17 21:33:47 +00002667 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2668 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2669 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2670 /* we can assume it's an "else",
2671 because nch >= 9 for try-else-finally and
2672 it would otherwise have a type of except_clause */
2673 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2674 if (orelse == NULL)
2675 return NULL;
2676 n_except--;
2677 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678
Neal Norwitzf599f422005-12-17 21:33:47 +00002679 finally = ast_for_suite(c, CHILD(n, nch - 1));
2680 if (finally == NULL)
2681 return NULL;
2682 n_except--;
2683 }
2684 else {
2685 /* we can assume it's an "else",
2686 otherwise it would have a type of except_clause */
2687 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2688 if (orelse == NULL)
2689 return NULL;
2690 n_except--;
2691 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002693 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002694 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 return NULL;
2696 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002697
2698 if (n_except > 0) {
2699 int i;
2700 stmt_ty except_st;
2701 /* process except statements to create a try ... except */
2702 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2703 if (handlers == NULL)
2704 return NULL;
2705
2706 for (i = 0; i < n_except; i++) {
2707 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2708 CHILD(n, 5 + i * 3));
2709 if (!e)
2710 return NULL;
2711 asdl_seq_SET(handlers, i, e);
2712 }
2713
2714 except_st = TryExcept(body, handlers, orelse, LINENO(n), c->c_arena);
2715 if (!finally)
2716 return except_st;
2717
2718 /* if a 'finally' is present too, we nest the TryExcept within a
2719 TryFinally to emulate try ... except ... finally */
2720 body = asdl_seq_new(1, c->c_arena);
2721 if (body == NULL)
2722 return NULL;
2723 asdl_seq_SET(body, 0, except_st);
2724 }
2725
2726 /* must be a try ... finally (except clauses are in body, if any exist) */
2727 assert(finally != NULL);
2728 return TryFinally(body, finally, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729}
2730
Guido van Rossumc2e20742006-02-27 22:32:47 +00002731static expr_ty
2732ast_for_with_var(struct compiling *c, const node *n)
2733{
2734 REQ(n, with_var);
2735 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2736 ast_error(n, "expected \"with [expr] as [var]\"");
2737 return NULL;
2738 }
2739 return ast_for_expr(c, CHILD(n, 1));
2740}
2741
2742/* with_stmt: 'with' test [ with_var ] ':' suite */
2743static stmt_ty
2744ast_for_with_stmt(struct compiling *c, const node *n)
2745{
2746 expr_ty context_expr, optional_vars = NULL;
2747 int suite_index = 3; /* skip 'with', test, and ':' */
2748 asdl_seq *suite_seq;
2749
2750 assert(TYPE(n) == with_stmt);
2751 context_expr = ast_for_expr(c, CHILD(n, 1));
2752 if (TYPE(CHILD(n, 2)) == with_var) {
2753 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2754
2755 if (!optional_vars) {
2756 return NULL;
2757 }
2758 if (!set_context(optional_vars, Store, n)) {
2759 return NULL;
2760 }
2761 suite_index = 4;
2762 }
2763
2764 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2765 if (!suite_seq) {
2766 return NULL;
2767 }
2768 return With(context_expr, optional_vars, suite_seq, LINENO(n), c->c_arena);
2769}
2770
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771static stmt_ty
2772ast_for_classdef(struct compiling *c, const node *n)
2773{
2774 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 asdl_seq *bases, *s;
2776
2777 REQ(n, classdef);
2778
2779 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2780 ast_error(n, "assignment to None");
2781 return NULL;
2782 }
2783
2784 if (NCH(n) == 4) {
2785 s = ast_for_suite(c, CHILD(n, 3));
2786 if (!s)
2787 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002788 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2789 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 }
2791 /* check for empty base list */
2792 if (TYPE(CHILD(n,3)) == RPAR) {
2793 s = ast_for_suite(c, CHILD(n,5));
2794 if (!s)
2795 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002796 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2797 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 }
2799
2800 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002801 bases = ast_for_class_bases(c, CHILD(n, 3));
2802 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804
2805 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002806 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002808 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2809 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810}
2811
2812static stmt_ty
2813ast_for_stmt(struct compiling *c, const node *n)
2814{
2815 if (TYPE(n) == stmt) {
2816 assert(NCH(n) == 1);
2817 n = CHILD(n, 0);
2818 }
2819 if (TYPE(n) == simple_stmt) {
2820 assert(num_stmts(n) == 1);
2821 n = CHILD(n, 0);
2822 }
2823 if (TYPE(n) == small_stmt) {
2824 REQ(n, small_stmt);
2825 n = CHILD(n, 0);
2826 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2827 | flow_stmt | import_stmt | global_stmt | exec_stmt
2828 | assert_stmt
2829 */
2830 switch (TYPE(n)) {
2831 case expr_stmt:
2832 return ast_for_expr_stmt(c, n);
2833 case print_stmt:
2834 return ast_for_print_stmt(c, n);
2835 case del_stmt:
2836 return ast_for_del_stmt(c, n);
2837 case pass_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002838 return Pass(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 case flow_stmt:
2840 return ast_for_flow_stmt(c, n);
2841 case import_stmt:
2842 return ast_for_import_stmt(c, n);
2843 case global_stmt:
2844 return ast_for_global_stmt(c, n);
2845 case exec_stmt:
2846 return ast_for_exec_stmt(c, n);
2847 case assert_stmt:
2848 return ast_for_assert_stmt(c, n);
2849 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002850 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2852 TYPE(n), NCH(n));
2853 return NULL;
2854 }
2855 }
2856 else {
2857 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2858 | funcdef | classdef
2859 */
2860 node *ch = CHILD(n, 0);
2861 REQ(n, compound_stmt);
2862 switch (TYPE(ch)) {
2863 case if_stmt:
2864 return ast_for_if_stmt(c, ch);
2865 case while_stmt:
2866 return ast_for_while_stmt(c, ch);
2867 case for_stmt:
2868 return ast_for_for_stmt(c, ch);
2869 case try_stmt:
2870 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002871 case with_stmt:
2872 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 case funcdef:
2874 return ast_for_funcdef(c, ch);
2875 case classdef:
2876 return ast_for_classdef(c, ch);
2877 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002878 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2880 TYPE(n), NCH(n));
2881 return NULL;
2882 }
2883 }
2884}
2885
2886static PyObject *
2887parsenumber(const char *s)
2888{
2889 const char *end;
2890 long x;
2891 double dx;
2892#ifndef WITHOUT_COMPLEX
2893 Py_complex c;
2894 int imflag;
2895#endif
2896
2897 errno = 0;
2898 end = s + strlen(s) - 1;
2899#ifndef WITHOUT_COMPLEX
2900 imflag = *end == 'j' || *end == 'J';
2901#endif
2902 if (*end == 'l' || *end == 'L')
2903 return PyLong_FromString((char *)s, (char **)0, 0);
2904 if (s[0] == '0') {
2905 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2906 if (x < 0 && errno == 0) {
2907 return PyLong_FromString((char *)s,
2908 (char **)0,
2909 0);
2910 }
2911 }
2912 else
2913 x = PyOS_strtol((char *)s, (char **)&end, 0);
2914 if (*end == '\0') {
2915 if (errno != 0)
2916 return PyLong_FromString((char *)s, (char **)0, 0);
2917 return PyInt_FromLong(x);
2918 }
2919 /* XXX Huge floats may silently fail */
2920#ifndef WITHOUT_COMPLEX
2921 if (imflag) {
2922 c.real = 0.;
2923 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002924 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 PyFPE_END_PROTECT(c)
2926 return PyComplex_FromCComplex(c);
2927 }
2928 else
2929#endif
2930 {
2931 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002932 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933 PyFPE_END_PROTECT(dx)
2934 return PyFloat_FromDouble(dx);
2935 }
2936}
2937
2938static PyObject *
2939decode_utf8(const char **sPtr, const char *end, char* encoding)
2940{
2941#ifndef Py_USING_UNICODE
2942 Py_FatalError("decode_utf8 should not be called in this build.");
2943 return NULL;
2944#else
2945 PyObject *u, *v;
2946 char *s, *t;
2947 t = s = (char *)*sPtr;
2948 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2949 while (s < end && (*s & 0x80)) s++;
2950 *sPtr = s;
2951 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2952 if (u == NULL)
2953 return NULL;
2954 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2955 Py_DECREF(u);
2956 return v;
2957#endif
2958}
2959
2960static PyObject *
2961decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2962{
2963 PyObject *v, *u;
2964 char *buf;
2965 char *p;
2966 const char *end;
2967 if (encoding == NULL) {
2968 buf = (char *)s;
2969 u = NULL;
2970 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2971 buf = (char *)s;
2972 u = NULL;
2973 } else {
2974 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2975 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2976 if (u == NULL)
2977 return NULL;
2978 p = buf = PyString_AsString(u);
2979 end = s + len;
2980 while (s < end) {
2981 if (*s == '\\') {
2982 *p++ = *s++;
2983 if (*s & 0x80) {
2984 strcpy(p, "u005c");
2985 p += 5;
2986 }
2987 }
2988 if (*s & 0x80) { /* XXX inefficient */
2989 PyObject *w;
2990 char *r;
2991 int rn, i;
2992 w = decode_utf8(&s, end, "utf-16-be");
2993 if (w == NULL) {
2994 Py_DECREF(u);
2995 return NULL;
2996 }
2997 r = PyString_AsString(w);
2998 rn = PyString_Size(w);
2999 assert(rn % 2 == 0);
3000 for (i = 0; i < rn; i += 2) {
3001 sprintf(p, "\\u%02x%02x",
3002 r[i + 0] & 0xFF,
3003 r[i + 1] & 0xFF);
3004 p += 6;
3005 }
3006 Py_DECREF(w);
3007 } else {
3008 *p++ = *s++;
3009 }
3010 }
3011 len = p - buf;
3012 s = buf;
3013 }
3014 if (rawmode)
3015 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3016 else
3017 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3018 Py_XDECREF(u);
3019 return v;
3020}
3021
3022/* s is a Python string literal, including the bracketing quote characters,
3023 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3024 * parsestr parses it, and returns the decoded Python string object.
3025 */
3026static PyObject *
3027parsestr(const char *s, const char *encoding)
3028{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003030 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 int rawmode = 0;
3032 int need_encoding;
3033 int unicode = 0;
3034
3035 if (isalpha(quote) || quote == '_') {
3036 if (quote == 'u' || quote == 'U') {
3037 quote = *++s;
3038 unicode = 1;
3039 }
3040 if (quote == 'r' || quote == 'R') {
3041 quote = *++s;
3042 rawmode = 1;
3043 }
3044 }
3045 if (quote != '\'' && quote != '\"') {
3046 PyErr_BadInternalCall();
3047 return NULL;
3048 }
3049 s++;
3050 len = strlen(s);
3051 if (len > INT_MAX) {
3052 PyErr_SetString(PyExc_OverflowError,
3053 "string to parse is too long");
3054 return NULL;
3055 }
3056 if (s[--len] != quote) {
3057 PyErr_BadInternalCall();
3058 return NULL;
3059 }
3060 if (len >= 4 && s[0] == quote && s[1] == quote) {
3061 s += 2;
3062 len -= 2;
3063 if (s[--len] != quote || s[--len] != quote) {
3064 PyErr_BadInternalCall();
3065 return NULL;
3066 }
3067 }
3068#ifdef Py_USING_UNICODE
3069 if (unicode || Py_UnicodeFlag) {
3070 return decode_unicode(s, len, rawmode, encoding);
3071 }
3072#endif
3073 need_encoding = (encoding != NULL &&
3074 strcmp(encoding, "utf-8") != 0 &&
3075 strcmp(encoding, "iso-8859-1") != 0);
3076 if (rawmode || strchr(s, '\\') == NULL) {
3077 if (need_encoding) {
3078#ifndef Py_USING_UNICODE
3079 /* This should not happen - we never see any other
3080 encoding. */
3081 Py_FatalError("cannot deal with encodings in this build.");
3082#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003083 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 if (u == NULL)
3085 return NULL;
3086 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3087 Py_DECREF(u);
3088 return v;
3089#endif
3090 } else {
3091 return PyString_FromStringAndSize(s, len);
3092 }
3093 }
3094
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003095 return PyString_DecodeEscape(s, len, NULL, unicode,
3096 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097}
3098
3099/* Build a Python string object out of a STRING atom. This takes care of
3100 * compile-time literal catenation, calling parsestr() on each piece, and
3101 * pasting the intermediate results together.
3102 */
3103static PyObject *
3104parsestrplus(struct compiling *c, const node *n)
3105{
3106 PyObject *v;
3107 int i;
3108 REQ(CHILD(n, 0), STRING);
3109 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3110 /* String literal concatenation */
3111 for (i = 1; i < NCH(n); i++) {
3112 PyObject *s;
3113 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3114 if (s == NULL)
3115 goto onError;
3116 if (PyString_Check(v) && PyString_Check(s)) {
3117 PyString_ConcatAndDel(&v, s);
3118 if (v == NULL)
3119 goto onError;
3120 }
3121#ifdef Py_USING_UNICODE
3122 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003123 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 Py_DECREF(v);
3126 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003127 if (v == NULL)
3128 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 }
3130#endif
3131 }
3132 }
3133 return v;
3134
3135 onError:
3136 Py_XDECREF(v);
3137 return NULL;
3138}