blob: 86f3d3c7c69b8a0c70fb14b28e5e12ac8c3dd629 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* XXX TO DO
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
21 - syntax errors
22*/
23
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024/* Data structure used internally */
25struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000026 char *c_encoding; /* source encoding */
27 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028};
29
30static asdl_seq *seq_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_expr(struct compiling *, const node *);
32static stmt_ty ast_for_stmt(struct compiling *, const node *);
33static asdl_seq *ast_for_suite(struct compiling *, const node *);
34static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 value = Py_BuildValue("(OO)", errstr, tmp);
111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Neal Norwitze98ccf62006-03-23 05:39:47 +0000194 if (TYPE(n) == encoding_decl) {
195 ast_error(n, "encoding declaration in Unicode string");
196 goto error;
197 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 } else if (TYPE(n) == encoding_decl) {
199 c.c_encoding = STR(n);
200 n = CHILD(n, 0);
201 } else {
202 c.c_encoding = NULL;
203 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000204 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Jeremy Hyltona8293132006-02-28 17:58:27 +0000206 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 switch (TYPE(n)) {
208 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000209 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 if (!stmts)
211 return NULL;
212 for (i = 0; i < NCH(n) - 1; i++) {
213 ch = CHILD(n, i);
214 if (TYPE(ch) == NEWLINE)
215 continue;
216 REQ(ch, stmt);
217 num = num_stmts(ch);
218 if (num == 1) {
219 s = ast_for_stmt(&c, ch);
220 if (!s)
221 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000222 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223 }
224 else {
225 ch = CHILD(ch, 0);
226 REQ(ch, simple_stmt);
227 for (j = 0; j < num; j++) {
228 s = ast_for_stmt(&c, CHILD(ch, j * 2));
229 if (!s)
230 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000231 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 }
233 }
234 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000235 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 case eval_input: {
237 expr_ty testlist_ast;
238
239 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000240 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241 if (!testlist_ast)
242 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 }
245 case single_input:
246 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 if (!stmts)
249 goto error;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000251 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 }
253 else {
254 n = CHILD(n, 0);
255 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000256 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 if (!stmts)
258 goto error;
259 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000260 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 if (!s)
262 goto error;
263 asdl_seq_SET(stmts, 0, s);
264 }
265 else {
266 /* Only a simple_stmt can contain multiple statements. */
267 REQ(n, simple_stmt);
268 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 if (TYPE(CHILD(n, i)) == NEWLINE)
270 break;
271 s = ast_for_stmt(&c, CHILD(n, i));
272 if (!s)
273 goto error;
274 asdl_seq_SET(stmts, i / 2, s);
275 }
276 }
277
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000278 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 }
280 default:
281 goto error;
282 }
283 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284 ast_error_finish(filename);
285 return NULL;
286}
287
288/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
289*/
290
291static operator_ty
292get_operator(const node *n)
293{
294 switch (TYPE(n)) {
295 case VBAR:
296 return BitOr;
297 case CIRCUMFLEX:
298 return BitXor;
299 case AMPER:
300 return BitAnd;
301 case LEFTSHIFT:
302 return LShift;
303 case RIGHTSHIFT:
304 return RShift;
305 case PLUS:
306 return Add;
307 case MINUS:
308 return Sub;
309 case STAR:
310 return Mult;
311 case SLASH:
312 return Div;
313 case DOUBLESLASH:
314 return FloorDiv;
315 case PERCENT:
316 return Mod;
317 default:
318 return 0;
319 }
320}
321
Jeremy Hyltona8293132006-02-28 17:58:27 +0000322/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323
324 Only sets context for expr kinds that "can appear in assignment context"
325 (according to ../Parser/Python.asdl). For other expr kinds, it sets
326 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327*/
328
329static int
330set_context(expr_ty e, expr_context_ty ctx, const node *n)
331{
332 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000333 /* If a particular expression type can't be used for assign / delete,
334 set expr_name to its name and an error message will be generated.
335 */
336 const char* expr_name = NULL;
337
338 /* The ast defines augmented store and load contexts, but the
339 implementation here doesn't actually use them. The code may be
340 a little more complex than necessary as a result. It also means
341 that expressions in an augmented assignment have no context.
342 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000343 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000344 */
345 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346
347 switch (e->kind) {
348 case Attribute_kind:
349 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000350 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351 return ast_error(n, "assignment to None");
352 }
353 e->v.Attribute.ctx = ctx;
354 break;
355 case Subscript_kind:
356 e->v.Subscript.ctx = ctx;
357 break;
358 case Name_kind:
359 if (ctx == Store &&
360 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
361 return ast_error(n, "assignment to None");
362 }
363 e->v.Name.ctx = ctx;
364 break;
365 case List_kind:
366 e->v.List.ctx = ctx;
367 s = e->v.List.elts;
368 break;
369 case Tuple_kind:
370 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
371 return ast_error(n, "can't assign to ()");
372 e->v.Tuple.ctx = ctx;
373 s = e->v.Tuple.elts;
374 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000375 case Lambda_kind:
376 expr_name = "lambda";
377 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000381 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000383 case UnaryOp_kind:
384 expr_name = "operator";
385 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000387 expr_name = "generator expression";
388 break;
389 case ListComp_kind:
390 expr_name = "list comprehension";
391 break;
392 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393 case Num_kind:
394 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000395 expr_name = "literal";
396 break;
397 case Compare_kind:
398 expr_name = "comparison";
399 break;
400 case Repr_kind:
401 expr_name = "repr";
402 break;
403 default:
404 PyErr_Format(PyExc_SystemError,
405 "unexpected expression in assignment %d (line %d)",
406 e->kind, e->lineno);
407 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000409 /* Check for error string set by switch */
410 if (expr_name) {
411 char buf[300];
412 PyOS_snprintf(buf, sizeof(buf),
413 "can't %s %s",
414 ctx == Store ? "assign to" : "delete",
415 expr_name);
416 return ast_error(n, buf);
417 }
418
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000419 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000420 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421 */
422 if (s) {
423 int i;
424
425 for (i = 0; i < asdl_seq_LEN(s); i++) {
426 if (!set_context(asdl_seq_GET(s, i), ctx, n))
427 return 0;
428 }
429 }
430 return 1;
431}
432
433static operator_ty
434ast_for_augassign(const node *n)
435{
436 REQ(n, augassign);
437 n = CHILD(n, 0);
438 switch (STR(n)[0]) {
439 case '+':
440 return Add;
441 case '-':
442 return Sub;
443 case '/':
444 if (STR(n)[1] == '/')
445 return FloorDiv;
446 else
447 return Div;
448 case '%':
449 return Mod;
450 case '<':
451 return LShift;
452 case '>':
453 return RShift;
454 case '&':
455 return BitAnd;
456 case '^':
457 return BitXor;
458 case '|':
459 return BitOr;
460 case '*':
461 if (STR(n)[1] == '*')
462 return Pow;
463 else
464 return Mult;
465 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000466 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 return 0;
468 }
469}
470
471static cmpop_ty
472ast_for_comp_op(const node *n)
473{
474 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
475 |'is' 'not'
476 */
477 REQ(n, comp_op);
478 if (NCH(n) == 1) {
479 n = CHILD(n, 0);
480 switch (TYPE(n)) {
481 case LESS:
482 return Lt;
483 case GREATER:
484 return Gt;
485 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486 return Eq;
487 case LESSEQUAL:
488 return LtE;
489 case GREATEREQUAL:
490 return GtE;
491 case NOTEQUAL:
492 return NotEq;
493 case NAME:
494 if (strcmp(STR(n), "in") == 0)
495 return In;
496 if (strcmp(STR(n), "is") == 0)
497 return Is;
498 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000499 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 STR(n));
501 return 0;
502 }
503 }
504 else if (NCH(n) == 2) {
505 /* handle "not in" and "is not" */
506 switch (TYPE(CHILD(n, 0))) {
507 case NAME:
508 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
509 return NotIn;
510 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
511 return IsNot;
512 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000513 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
515 return 0;
516 }
517 }
Neal Norwitz79792652005-11-14 04:25:03 +0000518 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 NCH(n));
520 return 0;
521}
522
523static asdl_seq *
524seq_for_testlist(struct compiling *c, const node *n)
525{
526 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000527 asdl_seq *seq;
528 expr_ty expression;
529 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530 assert(TYPE(n) == testlist
531 || TYPE(n) == listmaker
532 || TYPE(n) == testlist_gexp
533 || TYPE(n) == testlist_safe
534 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000536 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 if (!seq)
538 return NULL;
539
540 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000541 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
543 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000544 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546
547 assert(i / 2 < seq->size);
548 asdl_seq_SET(seq, i / 2, expression);
549 }
550 return seq;
551}
552
553static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000554compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555{
556 int i, len = (NCH(n) + 1) / 2;
557 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000558 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559 if (!args)
560 return NULL;
561
562 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 for (i = 0; i < len; i++) {
564 const node *child = CHILD(CHILD(n, 2*i), 0);
565 expr_ty arg;
566 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000567 if (!strcmp(STR(child), "None")) {
568 ast_error(child, "assignment to None");
569 return NULL;
570 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000571 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child), child->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000572 c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000573 }
574 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000575 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000576 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 asdl_seq_SET(args, i, arg);
578 }
579
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000580 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000581 if (!set_context(result, Store, n))
582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 return result;
584}
585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Jeremy Hyltona8293132006-02-28 17:58:27 +0000587/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
589static arguments_ty
590ast_for_arguments(struct compiling *c, const node *n)
591{
592 /* parameters: '(' [varargslist] ')'
593 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
594 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
595 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000596 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 asdl_seq *args, *defaults;
598 identifier vararg = NULL, kwarg = NULL;
599 node *ch;
600
601 if (TYPE(n) == parameters) {
602 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000603 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 n = CHILD(n, 1);
605 }
606 REQ(n, varargslist);
607
608 /* first count the number of normal args & defaults */
609 for (i = 0; i < NCH(n); i++) {
610 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000611 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 if (TYPE(ch) == EQUAL)
614 n_defaults++;
615 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000616 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617 if (!args && n_args)
618 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000619 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 if (!defaults && n_defaults)
621 goto error;
622
623 /* fpdef: NAME | '(' fplist ')'
624 fplist: fpdef (',' fpdef)* [',']
625 */
626 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000627 j = 0; /* index for defaults */
628 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629 while (i < NCH(n)) {
630 ch = CHILD(n, i);
631 switch (TYPE(ch)) {
632 case fpdef:
633 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
634 anything other than EQUAL or a comma? */
635 /* XXX Should NCH(n) check be made a separate check? */
636 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000637 asdl_seq_SET(defaults, j++,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 ast_for_expr(c, CHILD(n, i + 2)));
639 i += 2;
640 found_default = 1;
641 }
642 else if (found_default) {
643 ast_error(n,
644 "non-default argument follows default argument");
645 goto error;
646 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000648 ch = CHILD(ch, 1);
649 /* def foo((x)): is not complex, special case. */
650 if (NCH(ch) != 1) {
651 /* We have complex arguments, setup for unpacking. */
652 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
653 } else {
654 /* def foo((x)): setup for checking NAME below. */
655 ch = CHILD(ch, 0);
656 }
657 }
658 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000659 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
661 ast_error(CHILD(ch, 0), "assignment to None");
662 goto error;
663 }
Armin Rigo31441302005-10-21 12:57:31 +0000664 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000665 Param, LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 if (!name)
667 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000668 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669
670 }
671 i += 2; /* the name and the comma */
672 break;
673 case STAR:
674 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
675 ast_error(CHILD(n, i+1), "assignment to None");
676 goto error;
677 }
678 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
679 i += 3;
680 break;
681 case DOUBLESTAR:
682 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
683 ast_error(CHILD(n, i+1), "assignment to None");
684 goto error;
685 }
686 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
687 i += 3;
688 break;
689 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000690 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 "unexpected node in varargslist: %d @ %d",
692 TYPE(ch), i);
693 goto error;
694 }
695 }
696
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000697 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698
699 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000700 Py_XDECREF(vararg);
701 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 return NULL;
703}
704
705static expr_ty
706ast_for_dotted_name(struct compiling *c, const node *n)
707{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000708 expr_ty e;
709 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000710 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 int i;
712
713 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000714
715 lineno = LINENO(n);
716 col_offset = n->n_col_offset;
717
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 id = NEW_IDENTIFIER(CHILD(n, 0));
719 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000720 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000721 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000723 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724
725 for (i = 2; i < NCH(n); i+=2) {
726 id = NEW_IDENTIFIER(CHILD(n, i));
727 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000728 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000729 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000730 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000731 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 }
733
734 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735}
736
737static expr_ty
738ast_for_decorator(struct compiling *c, const node *n)
739{
740 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
741 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000742 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743
744 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000745 REQ(CHILD(n, 0), AT);
746 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747
748 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
749 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000750 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751
752 if (NCH(n) == 3) { /* No arguments */
753 d = name_expr;
754 name_expr = NULL;
755 }
756 else if (NCH(n) == 5) { /* Call with no arguments */
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000757 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000759 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 name_expr = NULL;
761 }
762 else {
763 d = ast_for_call(c, CHILD(n, 3), name_expr);
764 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 name_expr = NULL;
767 }
768
769 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770}
771
772static asdl_seq*
773ast_for_decorators(struct compiling *c, const node *n)
774{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000775 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000776 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 int i;
778
779 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000780 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 if (!decorator_seq)
782 return NULL;
783
784 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000785 d = ast_for_decorator(c, CHILD(n, i));
786 if (!d)
787 return NULL;
788 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 }
790 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791}
792
793static stmt_ty
794ast_for_funcdef(struct compiling *c, const node *n)
795{
796 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000797 identifier name;
798 arguments_ty args;
799 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 asdl_seq *decorator_seq = NULL;
801 int name_i;
802
803 REQ(n, funcdef);
804
805 if (NCH(n) == 6) { /* decorators are present */
806 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
807 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000808 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 name_i = 2;
810 }
811 else {
812 name_i = 1;
813 }
814
815 name = NEW_IDENTIFIER(CHILD(n, name_i));
816 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000817 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000819 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 }
822 args = ast_for_arguments(c, CHILD(n, name_i + 1));
823 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000824 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 body = ast_for_suite(c, CHILD(n, name_i + 3));
826 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000829 return FunctionDef(name, args, body, decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830}
831
832static expr_ty
833ast_for_lambdef(struct compiling *c, const node *n)
834{
835 /* lambdef: 'lambda' [varargslist] ':' test */
836 arguments_ty args;
837 expr_ty expression;
838
839 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000840 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 if (!args)
842 return NULL;
843 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000844 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 }
847 else {
848 args = ast_for_arguments(c, CHILD(n, 1));
849 if (!args)
850 return NULL;
851 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000852 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 }
855
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000856 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857}
858
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000859static expr_ty
860ast_for_ifexpr(struct compiling *c, const node *n)
861{
862 /* test: or_test 'if' or_test 'else' test */
863 expr_ty expression, body, orelse;
864
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000865 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000866 body = ast_for_expr(c, CHILD(n, 0));
867 if (!body)
868 return NULL;
869 expression = ast_for_expr(c, CHILD(n, 2));
870 if (!expression)
871 return NULL;
872 orelse = ast_for_expr(c, CHILD(n, 4));
873 if (!orelse)
874 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000875 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000876}
877
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878/* Count the number of 'for' loop in a list comprehension.
879
880 Helper for ast_for_listcomp().
881*/
882
883static int
884count_list_fors(const node *n)
885{
886 int n_fors = 0;
887 node *ch = CHILD(n, 1);
888
889 count_list_for:
890 n_fors++;
891 REQ(ch, list_for);
892 if (NCH(ch) == 5)
893 ch = CHILD(ch, 4);
894 else
895 return n_fors;
896 count_list_iter:
897 REQ(ch, list_iter);
898 ch = CHILD(ch, 0);
899 if (TYPE(ch) == list_for)
900 goto count_list_for;
901 else if (TYPE(ch) == list_if) {
902 if (NCH(ch) == 3) {
903 ch = CHILD(ch, 2);
904 goto count_list_iter;
905 }
906 else
907 return n_fors;
908 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000909
910 /* Should never be reached */
911 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
912 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913}
914
915/* Count the number of 'if' statements in a list comprehension.
916
917 Helper for ast_for_listcomp().
918*/
919
920static int
921count_list_ifs(const node *n)
922{
923 int n_ifs = 0;
924
925 count_list_iter:
926 REQ(n, list_iter);
927 if (TYPE(CHILD(n, 0)) == list_for)
928 return n_ifs;
929 n = CHILD(n, 0);
930 REQ(n, list_if);
931 n_ifs++;
932 if (NCH(n) == 2)
933 return n_ifs;
934 n = CHILD(n, 2);
935 goto count_list_iter;
936}
937
938static expr_ty
939ast_for_listcomp(struct compiling *c, const node *n)
940{
941 /* listmaker: test ( list_for | (',' test)* [','] )
942 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
943 list_iter: list_for | list_if
944 list_if: 'if' test [list_iter]
945 testlist_safe: test [(',' test)+ [',']]
946 */
947 expr_ty elt;
948 asdl_seq *listcomps;
949 int i, n_fors;
950 node *ch;
951
952 REQ(n, listmaker);
953 assert(NCH(n) > 1);
954
955 elt = ast_for_expr(c, CHILD(n, 0));
956 if (!elt)
957 return NULL;
958
959 n_fors = count_list_fors(n);
960 if (n_fors == -1)
961 return NULL;
962
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000963 listcomps = asdl_seq_new(n_fors, c->c_arena);
964 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000966
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 ch = CHILD(n, 1);
968 for (i = 0; i < n_fors; i++) {
969 comprehension_ty lc;
970 asdl_seq *t;
971 expr_ty expression;
972
973 REQ(ch, list_for);
974
975 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000978 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000979 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000982 if (asdl_seq_LEN(t) == 1)
983 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
984 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000986 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000987 expression, NULL, c->c_arena);
988 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990
991 if (NCH(ch) == 5) {
992 int j, n_ifs;
993 asdl_seq *ifs;
994
995 ch = CHILD(ch, 4);
996 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000997 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001000 ifs = asdl_seq_new(n_ifs, c->c_arena);
1001 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003
1004 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001005 REQ(ch, list_iter);
1006 ch = CHILD(ch, 0);
1007 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008
Jeremy Hyltona8293132006-02-28 17:58:27 +00001009 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1010 if (NCH(ch) == 3)
1011 ch = CHILD(ch, 2);
1012 }
1013 /* on exit, must guarantee that ch is a list_for */
1014 if (TYPE(ch) == list_iter)
1015 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001017 }
1018 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 }
1020
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001021 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022}
1023
1024/*
1025 Count the number of 'for' loops in a generator expression.
1026
1027 Helper for ast_for_genexp().
1028*/
1029
1030static int
1031count_gen_fors(const node *n)
1032{
1033 int n_fors = 0;
1034 node *ch = CHILD(n, 1);
1035
1036 count_gen_for:
1037 n_fors++;
1038 REQ(ch, gen_for);
1039 if (NCH(ch) == 5)
1040 ch = CHILD(ch, 4);
1041 else
1042 return n_fors;
1043 count_gen_iter:
1044 REQ(ch, gen_iter);
1045 ch = CHILD(ch, 0);
1046 if (TYPE(ch) == gen_for)
1047 goto count_gen_for;
1048 else if (TYPE(ch) == gen_if) {
1049 if (NCH(ch) == 3) {
1050 ch = CHILD(ch, 2);
1051 goto count_gen_iter;
1052 }
1053 else
1054 return n_fors;
1055 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001056
1057 /* Should never be reached */
1058 PyErr_SetString(PyExc_SystemError,
1059 "logic error in count_gen_fors");
1060 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061}
1062
1063/* Count the number of 'if' statements in a generator expression.
1064
1065 Helper for ast_for_genexp().
1066*/
1067
1068static int
1069count_gen_ifs(const node *n)
1070{
1071 int n_ifs = 0;
1072
1073 while (1) {
1074 REQ(n, gen_iter);
1075 if (TYPE(CHILD(n, 0)) == gen_for)
1076 return n_ifs;
1077 n = CHILD(n, 0);
1078 REQ(n, gen_if);
1079 n_ifs++;
1080 if (NCH(n) == 2)
1081 return n_ifs;
1082 n = CHILD(n, 2);
1083 }
1084}
1085
Jeremy Hyltona8293132006-02-28 17:58:27 +00001086/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087static expr_ty
1088ast_for_genexp(struct compiling *c, const node *n)
1089{
1090 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1091 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1092 expr_ty elt;
1093 asdl_seq *genexps;
1094 int i, n_fors;
1095 node *ch;
1096
1097 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1098 assert(NCH(n) > 1);
1099
1100 elt = ast_for_expr(c, CHILD(n, 0));
1101 if (!elt)
1102 return NULL;
1103
1104 n_fors = count_gen_fors(n);
1105 if (n_fors == -1)
1106 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001107
1108 genexps = asdl_seq_new(n_fors, c->c_arena);
1109 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 ch = CHILD(n, 1);
1113 for (i = 0; i < n_fors; i++) {
1114 comprehension_ty ge;
1115 asdl_seq *t;
1116 expr_ty expression;
1117
1118 REQ(ch, gen_for);
1119
1120 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001121 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001123 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001124 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001126
1127 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001129 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001131 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001132 expression, NULL, c->c_arena);
1133
1134 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001136
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 if (NCH(ch) == 5) {
1138 int j, n_ifs;
1139 asdl_seq *ifs;
1140
1141 ch = CHILD(ch, 4);
1142 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001143 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001145
1146 ifs = asdl_seq_new(n_ifs, c->c_arena);
1147 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001149
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 for (j = 0; j < n_ifs; j++) {
1151 REQ(ch, gen_iter);
1152 ch = CHILD(ch, 0);
1153 REQ(ch, gen_if);
1154
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001155 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001156 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001157 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001158 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 if (NCH(ch) == 3)
1160 ch = CHILD(ch, 2);
1161 }
1162 /* on exit, must guarantee that ch is a gen_for */
1163 if (TYPE(ch) == gen_iter)
1164 ch = CHILD(ch, 0);
1165 ge->ifs = ifs;
1166 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001167 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 }
1169
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001170 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171}
1172
1173static expr_ty
1174ast_for_atom(struct compiling *c, const node *n)
1175{
1176 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1177 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1178 */
1179 node *ch = CHILD(n, 0);
1180
1181 switch (TYPE(ch)) {
1182 case NAME:
1183 /* All names start in Load context, but may later be
1184 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001185 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 case STRING: {
1187 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 if (!str)
1189 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001190
1191 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001192 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 }
1194 case NUMBER: {
1195 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 if (!pynum)
1197 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001198
1199 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001200 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 }
1202 case LPAR: /* some parenthesized expressions */
1203 ch = CHILD(n, 1);
1204
1205 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001206 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207
1208 if (TYPE(ch) == yield_expr)
1209 return ast_for_expr(c, ch);
1210
1211 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1212 return ast_for_genexp(c, ch);
1213
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001214 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 case LSQB: /* list (or list comprehension) */
1216 ch = CHILD(n, 1);
1217
1218 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001219 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220
1221 REQ(ch, listmaker);
1222 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1223 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 if (!elts)
1225 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001226
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001227 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228 }
1229 else
1230 return ast_for_listcomp(c, ch);
1231 case LBRACE: {
1232 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1233 int i, size;
1234 asdl_seq *keys, *values;
1235
1236 ch = CHILD(n, 1);
1237 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001238 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 if (!keys)
1240 return NULL;
1241
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001242 values = asdl_seq_new(size, c->c_arena);
1243 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245
1246 for (i = 0; i < NCH(ch); i += 4) {
1247 expr_ty expression;
1248
1249 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001250 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001254
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001256 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001258
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 asdl_seq_SET(values, i / 4, expression);
1260 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001261 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 }
1263 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001264 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 if (!expression)
1266 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001267
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001268 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 }
1270 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001271 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 return NULL;
1273 }
1274}
1275
1276static slice_ty
1277ast_for_slice(struct compiling *c, const node *n)
1278{
1279 node *ch;
1280 expr_ty lower = NULL, upper = NULL, step = NULL;
1281
1282 REQ(n, subscript);
1283
1284 /*
1285 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1286 sliceop: ':' [test]
1287 */
1288 ch = CHILD(n, 0);
1289 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001290 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291
1292 if (NCH(n) == 1 && TYPE(ch) == test) {
1293 /* 'step' variable hold no significance in terms of being used over
1294 other vars */
1295 step = ast_for_expr(c, ch);
1296 if (!step)
1297 return NULL;
1298
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001299 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 }
1301
1302 if (TYPE(ch) == test) {
1303 lower = ast_for_expr(c, ch);
1304 if (!lower)
1305 return NULL;
1306 }
1307
1308 /* If there's an upper bound it's in the second or third position. */
1309 if (TYPE(ch) == COLON) {
1310 if (NCH(n) > 1) {
1311 node *n2 = CHILD(n, 1);
1312
1313 if (TYPE(n2) == test) {
1314 upper = ast_for_expr(c, n2);
1315 if (!upper)
1316 return NULL;
1317 }
1318 }
1319 } else if (NCH(n) > 2) {
1320 node *n2 = CHILD(n, 2);
1321
1322 if (TYPE(n2) == test) {
1323 upper = ast_for_expr(c, n2);
1324 if (!upper)
1325 return NULL;
1326 }
1327 }
1328
1329 ch = CHILD(n, NCH(n) - 1);
1330 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001331 if (NCH(ch) == 1) {
1332 /* No expression, so step is None */
1333 ch = CHILD(ch, 0);
1334 step = Name(new_identifier("None", c->c_arena), Load,
1335 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336 if (!step)
1337 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001338 } else {
1339 ch = CHILD(ch, 1);
1340 if (TYPE(ch) == test) {
1341 step = ast_for_expr(c, ch);
1342 if (!step)
1343 return NULL;
1344 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 }
1346 }
1347
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001348 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349}
1350
1351static expr_ty
1352ast_for_binop(struct compiling *c, const node *n)
1353{
1354 /* Must account for a sequence of expressions.
1355 How should A op B op C by represented?
1356 BinOp(BinOp(A, op, B), op, C).
1357 */
1358
1359 int i, nops;
1360 expr_ty expr1, expr2, result;
1361 operator_ty operator;
1362
1363 expr1 = ast_for_expr(c, CHILD(n, 0));
1364 if (!expr1)
1365 return NULL;
1366
1367 expr2 = ast_for_expr(c, CHILD(n, 2));
1368 if (!expr2)
1369 return NULL;
1370
1371 operator = get_operator(CHILD(n, 1));
1372 if (!operator)
1373 return NULL;
1374
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001375 result = BinOp(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 if (!result)
1377 return NULL;
1378
1379 nops = (NCH(n) - 1) / 2;
1380 for (i = 1; i < nops; i++) {
1381 expr_ty tmp_result, tmp;
1382 const node* next_oper = CHILD(n, i * 2 + 1);
1383
1384 operator = get_operator(next_oper);
1385 if (!operator)
1386 return NULL;
1387
1388 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1389 if (!tmp)
1390 return NULL;
1391
1392 tmp_result = BinOp(result, operator, tmp,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001393 LINENO(next_oper), next_oper->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 if (!tmp)
1395 return NULL;
1396 result = tmp_result;
1397 }
1398 return result;
1399}
1400
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001401static expr_ty
1402ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1403{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001404 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1405 subscriptlist: subscript (',' subscript)* [',']
1406 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1407 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001408 REQ(n, trailer);
1409 if (TYPE(CHILD(n, 0)) == LPAR) {
1410 if (NCH(n) == 2)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001411 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001412 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001413 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001414 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001415 else if (TYPE(CHILD(n, 0)) == DOT ) {
1416 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001417 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001418 }
1419 else {
1420 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001421 REQ(CHILD(n, 2), RSQB);
1422 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001423 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001424 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1425 if (!slc)
1426 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001427 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001428 }
1429 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001430 /* The grammar is ambiguous here. The ambiguity is resolved
1431 by treating the sequence as a tuple literal if there are
1432 no slice features.
1433 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001434 int j;
1435 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001436 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001437 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001438 asdl_seq *slices, *elts;
1439 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001440 if (!slices)
1441 return NULL;
1442 for (j = 0; j < NCH(n); j += 2) {
1443 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001444 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001445 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001446 if (slc->kind != Index_kind)
1447 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001448 asdl_seq_SET(slices, j / 2, slc);
1449 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001450 if (!simple) {
1451 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001452 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001453 }
1454 /* extract Index values and put them in a Tuple */
1455 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001456 if (!elts)
1457 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001458 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1459 slc = (slice_ty)asdl_seq_GET(slices, j);
1460 assert(slc->kind == Index_kind && slc->v.Index.value);
1461 asdl_seq_SET(elts, j, slc->v.Index.value);
1462 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001463 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001464 if (!e)
1465 return NULL;
1466 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001467 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001468 }
1469 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001470}
1471
1472static expr_ty
1473ast_for_power(struct compiling *c, const node *n)
1474{
1475 /* power: atom trailer* ('**' factor)*
1476 */
1477 int i;
1478 expr_ty e, tmp;
1479 REQ(n, power);
1480 e = ast_for_atom(c, CHILD(n, 0));
1481 if (!e)
1482 return NULL;
1483 if (NCH(n) == 1)
1484 return e;
1485 for (i = 1; i < NCH(n); i++) {
1486 node *ch = CHILD(n, i);
1487 if (TYPE(ch) != trailer)
1488 break;
1489 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001490 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001491 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001492 tmp->lineno = e->lineno;
1493 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001494 e = tmp;
1495 }
1496 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1497 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001498 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001499 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001500 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001501 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001502 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001503 e = tmp;
1504 }
1505 return e;
1506}
1507
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508/* Do not name a variable 'expr'! Will cause a compile error.
1509*/
1510
1511static expr_ty
1512ast_for_expr(struct compiling *c, const node *n)
1513{
1514 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001515 test: or_test ['if' or_test 'else' test] | lambdef
1516 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 and_test: not_test ('and' not_test)*
1518 not_test: 'not' not_test | comparison
1519 comparison: expr (comp_op expr)*
1520 expr: xor_expr ('|' xor_expr)*
1521 xor_expr: and_expr ('^' and_expr)*
1522 and_expr: shift_expr ('&' shift_expr)*
1523 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1524 arith_expr: term (('+'|'-') term)*
1525 term: factor (('*'|'/'|'%'|'//') factor)*
1526 factor: ('+'|'-'|'~') factor | power
1527 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001528
1529 As well as modified versions that exist for backward compatibility,
1530 to explicitly allow:
1531 [ x for x in lambda: 0, lambda: 1 ]
1532 (which would be ambiguous without these extra rules)
1533
1534 old_test: or_test | old_lambdef
1535 old_lambdef: 'lambda' [vararglist] ':' old_test
1536
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 */
1538
1539 asdl_seq *seq;
1540 int i;
1541
1542 loop:
1543 switch (TYPE(n)) {
1544 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001545 case old_test:
1546 if (TYPE(CHILD(n, 0)) == lambdef ||
1547 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001549 else if (NCH(n) > 1)
1550 return ast_for_ifexpr(c, n);
1551 /* Fallthrough */
1552 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 case and_test:
1554 if (NCH(n) == 1) {
1555 n = CHILD(n, 0);
1556 goto loop;
1557 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001558 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 if (!seq)
1560 return NULL;
1561 for (i = 0; i < NCH(n); i += 2) {
1562 expr_ty e = ast_for_expr(c, CHILD(n, i));
1563 if (!e)
1564 return NULL;
1565 asdl_seq_SET(seq, i / 2, e);
1566 }
1567 if (!strcmp(STR(CHILD(n, 1)), "and"))
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001568 return BoolOp(And, seq, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001569 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001570 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 case not_test:
1572 if (NCH(n) == 1) {
1573 n = CHILD(n, 0);
1574 goto loop;
1575 }
1576 else {
1577 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1578 if (!expression)
1579 return NULL;
1580
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001581 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582 }
1583 case comparison:
1584 if (NCH(n) == 1) {
1585 n = CHILD(n, 0);
1586 goto loop;
1587 }
1588 else {
1589 expr_ty expression;
1590 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001591 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592 if (!ops)
1593 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001594 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 return NULL;
1597 }
1598 for (i = 1; i < NCH(n); i += 2) {
1599 /* XXX cmpop_ty is just an enum */
1600 cmpop_ty operator;
1601
1602 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001603 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606
1607 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001608 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001610 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001612 asdl_seq_SET(ops, i / 2, (void *)(Py_uintptr_t)operator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613 asdl_seq_SET(cmps, i / 2, expression);
1614 }
1615 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001616 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001618 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001620 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621 }
1622 break;
1623
1624 /* The next five cases all handle BinOps. The main body of code
1625 is the same in each case, but the switch turned inside out to
1626 reuse the code for each type of operator.
1627 */
1628 case expr:
1629 case xor_expr:
1630 case and_expr:
1631 case shift_expr:
1632 case arith_expr:
1633 case term:
1634 if (NCH(n) == 1) {
1635 n = CHILD(n, 0);
1636 goto loop;
1637 }
1638 return ast_for_binop(c, n);
1639 case yield_expr: {
1640 expr_ty exp = NULL;
1641 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001642 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 if (!exp)
1644 return NULL;
1645 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001646 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 }
1648 case factor: {
1649 expr_ty expression;
1650
1651 if (NCH(n) == 1) {
1652 n = CHILD(n, 0);
1653 goto loop;
1654 }
1655
1656 expression = ast_for_expr(c, CHILD(n, 1));
1657 if (!expression)
1658 return NULL;
1659
1660 switch (TYPE(CHILD(n, 0))) {
1661 case PLUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001662 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663 case MINUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001664 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 case TILDE:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001666 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001668 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1669 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 break;
1671 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001672 case power:
1673 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001675 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 return NULL;
1677 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001678 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 return NULL;
1680}
1681
1682static expr_ty
1683ast_for_call(struct compiling *c, const node *n, expr_ty func)
1684{
1685 /*
1686 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1687 | '**' test)
1688 argument: [test '='] test [gen_for] # Really [keyword '='] test
1689 */
1690
1691 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001692 asdl_seq *args;
1693 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694 expr_ty vararg = NULL, kwarg = NULL;
1695
1696 REQ(n, arglist);
1697
1698 nargs = 0;
1699 nkeywords = 0;
1700 ngens = 0;
1701 for (i = 0; i < NCH(n); i++) {
1702 node *ch = CHILD(n, i);
1703 if (TYPE(ch) == argument) {
1704 if (NCH(ch) == 1)
1705 nargs++;
1706 else if (TYPE(CHILD(ch, 1)) == gen_for)
1707 ngens++;
1708 else
1709 nkeywords++;
1710 }
1711 }
1712 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001713 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 "if not sole argument");
1715 return NULL;
1716 }
1717
1718 if (nargs + nkeywords + ngens > 255) {
1719 ast_error(n, "more than 255 arguments");
1720 return NULL;
1721 }
1722
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001723 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001725 return NULL;
1726 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001728 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 nargs = 0;
1730 nkeywords = 0;
1731 for (i = 0; i < NCH(n); i++) {
1732 node *ch = CHILD(n, i);
1733 if (TYPE(ch) == argument) {
1734 expr_ty e;
1735 if (NCH(ch) == 1) {
1736 e = ast_for_expr(c, CHILD(ch, 0));
1737 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739 asdl_seq_SET(args, nargs++, e);
1740 }
1741 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1742 e = ast_for_genexp(c, ch);
1743 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001744 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 asdl_seq_SET(args, nargs++, e);
1746 }
1747 else {
1748 keyword_ty kw;
1749 identifier key;
1750
1751 /* CHILD(ch, 0) is test, but must be an identifier? */
1752 e = ast_for_expr(c, CHILD(ch, 0));
1753 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001754 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755 /* f(lambda x: x[0] = 3) ends up getting parsed with
1756 * LHS test = lambda x: x[0], and RHS test = 3.
1757 * SF bug 132313 points out that complaining about a keyword
1758 * then is very confusing.
1759 */
1760 if (e->kind == Lambda_kind) {
1761 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001762 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 } else if (e->kind != Name_kind) {
1764 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 }
1767 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 e = ast_for_expr(c, CHILD(ch, 2));
1769 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001770 return NULL;
1771 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001773 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 asdl_seq_SET(keywords, nkeywords++, kw);
1775 }
1776 }
1777 else if (TYPE(ch) == STAR) {
1778 vararg = ast_for_expr(c, CHILD(n, i+1));
1779 i++;
1780 }
1781 else if (TYPE(ch) == DOUBLESTAR) {
1782 kwarg = ast_for_expr(c, CHILD(n, i+1));
1783 i++;
1784 }
1785 }
1786
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001787 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788}
1789
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001791ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001793 /* testlist_gexp: test (',' test)* [','] */
1794 /* testlist: test (',' test)* [','] */
1795 /* testlist_safe: test (',' test)+ [','] */
1796 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001798 if (TYPE(n) == testlist_gexp) {
1799 if (NCH(n) > 1)
1800 assert(TYPE(CHILD(n, 1)) != gen_for);
1801 }
1802 else {
1803 assert(TYPE(n) == testlist ||
1804 TYPE(n) == testlist_safe ||
1805 TYPE(n) == testlist1);
1806 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 if (NCH(n) == 1)
1808 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 else {
1810 asdl_seq *tmp = seq_for_testlist(c, n);
1811 if (!tmp)
1812 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001813 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001815}
1816
1817static expr_ty
1818ast_for_testlist_gexp(struct compiling *c, const node* n)
1819{
1820 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1821 /* argument: test [ gen_for ] */
1822 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001823 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001824 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001825 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001826}
1827
1828/* like ast_for_testlist() but returns a sequence */
1829static asdl_seq*
1830ast_for_class_bases(struct compiling *c, const node* n)
1831{
1832 /* testlist: test (',' test)* [','] */
1833 assert(NCH(n) > 0);
1834 REQ(n, testlist);
1835 if (NCH(n) == 1) {
1836 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001837 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001838 if (!bases)
1839 return NULL;
1840 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001841 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001842 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001843 asdl_seq_SET(bases, 0, base);
1844 return bases;
1845 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001846
1847 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848}
1849
1850static stmt_ty
1851ast_for_expr_stmt(struct compiling *c, const node *n)
1852{
1853 REQ(n, expr_stmt);
1854 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1855 | ('=' (yield_expr|testlist))*)
1856 testlist: test (',' test)* [',']
1857 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1858 | '<<=' | '>>=' | '**=' | '//='
1859 test: ... here starts the operator precendence dance
1860 */
1861
1862 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001863 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 if (!e)
1865 return NULL;
1866
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001867 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 }
1869 else if (TYPE(CHILD(n, 1)) == augassign) {
1870 expr_ty expr1, expr2;
1871 operator_ty operator;
1872 node *ch = CHILD(n, 0);
1873
1874 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001875 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001877 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001878 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879
1880 if (!expr1)
1881 return NULL;
Neal Norwitz96e48d42006-02-05 02:07:19 +00001882 /* TODO(jhylton): Figure out why set_context() can't be used here. */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001883 switch (expr1->kind) {
1884 case GeneratorExp_kind:
1885 ast_error(ch, "augmented assignment to generator "
1886 "expression not possible");
1887 return NULL;
1888 case Name_kind: {
1889 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1890 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1891 ast_error(ch, "assignment to None");
1892 return NULL;
1893 }
1894 break;
1895 }
1896 case Attribute_kind:
1897 case Subscript_kind:
1898 break;
1899 default:
1900 ast_error(ch, "illegal expression for augmented "
1901 "assignment");
1902 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904
1905 ch = CHILD(n, 2);
1906 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001907 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001909 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), ch->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001910 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 return NULL;
1912
1913 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001914 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 return NULL;
1916
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001917 return AugAssign(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 }
1919 else {
1920 int i;
1921 asdl_seq *targets;
1922 node *value;
1923 expr_ty expression;
1924
1925 /* a normal assignment */
1926 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001927 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 if (!targets)
1929 return NULL;
1930 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001931 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932 node *ch = CHILD(n, i);
1933 if (TYPE(ch) == yield_expr) {
1934 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001935 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001937 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938
1939 /* set context to assign */
1940 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001941 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942
Neal Norwitz84456bd2005-12-18 03:16:20 +00001943 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001944 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945
1946 asdl_seq_SET(targets, i / 2, e);
1947 }
1948 value = CHILD(n, NCH(n) - 1);
1949 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001950 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 else
1952 expression = ast_for_expr(c, value);
1953 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001954 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001955 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957}
1958
1959static stmt_ty
1960ast_for_print_stmt(struct compiling *c, const node *n)
1961{
1962 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1963 | '>>' test [ (',' test)+ [','] ] )
1964 */
1965 expr_ty dest = NULL, expression;
1966 asdl_seq *seq;
1967 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001968 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969
1970 REQ(n, print_stmt);
1971 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1972 dest = ast_for_expr(c, CHILD(n, 2));
1973 if (!dest)
1974 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001975 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001977 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00001979 return NULL;
1980 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001982 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001984 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 }
1986 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001987 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988}
1989
1990static asdl_seq *
1991ast_for_exprlist(struct compiling *c, const node *n, int context)
1992{
1993 asdl_seq *seq;
1994 int i;
1995 expr_ty e;
1996
1997 REQ(n, exprlist);
1998
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001999 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 if (!seq)
2001 return NULL;
2002 for (i = 0; i < NCH(n); i += 2) {
2003 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002004 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002005 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002006 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002007 if (context && !set_context(e, context, CHILD(n, i)))
2008 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 }
2010 return seq;
2011}
2012
2013static stmt_ty
2014ast_for_del_stmt(struct compiling *c, const node *n)
2015{
2016 asdl_seq *expr_list;
2017
2018 /* del_stmt: 'del' exprlist */
2019 REQ(n, del_stmt);
2020
2021 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2022 if (!expr_list)
2023 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002024 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025}
2026
2027static stmt_ty
2028ast_for_flow_stmt(struct compiling *c, const node *n)
2029{
2030 /*
2031 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2032 | yield_stmt
2033 break_stmt: 'break'
2034 continue_stmt: 'continue'
2035 return_stmt: 'return' [testlist]
2036 yield_stmt: yield_expr
2037 yield_expr: 'yield' testlist
2038 raise_stmt: 'raise' [test [',' test [',' test]]]
2039 */
2040 node *ch;
2041
2042 REQ(n, flow_stmt);
2043 ch = CHILD(n, 0);
2044 switch (TYPE(ch)) {
2045 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002046 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002048 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 case yield_stmt: { /* will reduce to yield_expr */
2050 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2051 if (!exp)
2052 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002053 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 }
2055 case return_stmt:
2056 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002057 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002059 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 if (!expression)
2061 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002062 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 }
2064 case raise_stmt:
2065 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002066 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 else if (NCH(ch) == 2) {
2068 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2069 if (!expression)
2070 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002071 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 }
2073 else if (NCH(ch) == 4) {
2074 expr_ty expr1, expr2;
2075
2076 expr1 = ast_for_expr(c, CHILD(ch, 1));
2077 if (!expr1)
2078 return NULL;
2079 expr2 = ast_for_expr(c, CHILD(ch, 3));
2080 if (!expr2)
2081 return NULL;
2082
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002083 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 }
2085 else if (NCH(ch) == 6) {
2086 expr_ty expr1, expr2, expr3;
2087
2088 expr1 = ast_for_expr(c, CHILD(ch, 1));
2089 if (!expr1)
2090 return NULL;
2091 expr2 = ast_for_expr(c, CHILD(ch, 3));
2092 if (!expr2)
2093 return NULL;
2094 expr3 = ast_for_expr(c, CHILD(ch, 5));
2095 if (!expr3)
2096 return NULL;
2097
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002098 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 }
2100 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002101 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 "unexpected flow_stmt: %d", TYPE(ch));
2103 return NULL;
2104 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002105
2106 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2107 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108}
2109
2110static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002111alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112{
2113 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002114 import_as_name: NAME ['as' NAME]
2115 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 dotted_name: NAME ('.' NAME)*
2117 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002118 PyObject *str;
2119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 loop:
2121 switch (TYPE(n)) {
2122 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002123 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2124 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 case dotted_as_name:
2126 if (NCH(n) == 1) {
2127 n = CHILD(n, 0);
2128 goto loop;
2129 }
2130 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002131 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 assert(!a->asname);
2133 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2134 return a;
2135 }
2136 break;
2137 case dotted_name:
2138 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002139 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 else {
2141 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002142 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002143 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 char *s;
2145
2146 len = 0;
2147 for (i = 0; i < NCH(n); i += 2)
2148 /* length of string plus one for the dot */
2149 len += strlen(STR(CHILD(n, i))) + 1;
2150 len--; /* the last name doesn't have a dot */
2151 str = PyString_FromStringAndSize(NULL, len);
2152 if (!str)
2153 return NULL;
2154 s = PyString_AS_STRING(str);
2155 if (!s)
2156 return NULL;
2157 for (i = 0; i < NCH(n); i += 2) {
2158 char *sch = STR(CHILD(n, i));
2159 strcpy(s, STR(CHILD(n, i)));
2160 s += strlen(sch);
2161 *s++ = '.';
2162 }
2163 --s;
2164 *s = '\0';
2165 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002166 PyArena_AddPyObject(c->c_arena, str);
2167 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 }
2169 break;
2170 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002171 str = PyString_InternFromString("*");
2172 PyArena_AddPyObject(c->c_arena, str);
2173 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002175 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 "unexpected import name: %d", TYPE(n));
2177 return NULL;
2178 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002179
2180 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 return NULL;
2182}
2183
2184static stmt_ty
2185ast_for_import_stmt(struct compiling *c, const node *n)
2186{
2187 /*
2188 import_stmt: import_name | import_from
2189 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002190 import_from: 'from' ('.'* dotted_name | '.') 'import'
2191 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002193 int lineno;
2194 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 int i;
2196 asdl_seq *aliases;
2197
2198 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002199 lineno = LINENO(n);
2200 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002202 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002204 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002205 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 if (!aliases)
2207 return NULL;
2208 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002209 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002210 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 asdl_seq_SET(aliases, i / 2, import_alias);
2213 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002214 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002216 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002218 int idx, ndots = 0;
2219 alias_ty mod = NULL;
2220 identifier modname;
2221
2222 /* Count the number of dots (for relative imports) and check for the
2223 optional module name */
2224 for (idx = 1; idx < NCH(n); idx++) {
2225 if (TYPE(CHILD(n, idx)) == dotted_name) {
2226 mod = alias_for_import_name(c, CHILD(n, idx));
2227 idx++;
2228 break;
2229 } else if (TYPE(CHILD(n, idx)) != DOT) {
2230 break;
2231 }
2232 ndots++;
2233 }
2234 idx++; /* skip over the 'import' keyword */
2235 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002236 case STAR:
2237 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002238 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002239 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002240 if (ndots) {
2241 ast_error(n, "'import *' not allowed with 'from .'");
2242 return NULL;
2243 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002244 break;
2245 case LPAR:
2246 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002247 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002248 n_children = NCH(n);
2249 break;
2250 case import_as_names:
2251 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002252 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002253 n_children = NCH(n);
2254 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 ast_error(n, "trailing comma not allowed without"
2256 " surrounding parentheses");
2257 return NULL;
2258 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002259 break;
2260 default:
2261 ast_error(n, "Unexpected node-type in from-import");
2262 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002263 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002265 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002266 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268
2269 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002270 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002271 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002272 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002274 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002276 else {
2277 for (i = 0; i < NCH(n); i += 2) {
2278 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2279 if (!import_alias)
2280 return NULL;
2281 asdl_seq_SET(aliases, i / 2, import_alias);
2282 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002284 if (mod != NULL)
2285 modname = mod->name;
2286 else
2287 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002288 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002289 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 }
Neal Norwitz79792652005-11-14 04:25:03 +00002291 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 "unknown import statement: starts with command '%s'",
2293 STR(CHILD(n, 0)));
2294 return NULL;
2295}
2296
2297static stmt_ty
2298ast_for_global_stmt(struct compiling *c, const node *n)
2299{
2300 /* global_stmt: 'global' NAME (',' NAME)* */
2301 identifier name;
2302 asdl_seq *s;
2303 int i;
2304
2305 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002306 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 if (!s)
2308 return NULL;
2309 for (i = 1; i < NCH(n); i += 2) {
2310 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002311 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 asdl_seq_SET(s, i / 2, name);
2314 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002315 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316}
2317
2318static stmt_ty
2319ast_for_exec_stmt(struct compiling *c, const node *n)
2320{
2321 expr_ty expr1, globals = NULL, locals = NULL;
2322 int n_children = NCH(n);
2323 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002324 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 "poorly formed 'exec' statement: %d parts to statement",
2326 n_children);
2327 return NULL;
2328 }
2329
2330 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2331 REQ(n, exec_stmt);
2332 expr1 = ast_for_expr(c, CHILD(n, 1));
2333 if (!expr1)
2334 return NULL;
2335 if (n_children >= 4) {
2336 globals = ast_for_expr(c, CHILD(n, 3));
2337 if (!globals)
2338 return NULL;
2339 }
2340 if (n_children == 6) {
2341 locals = ast_for_expr(c, CHILD(n, 5));
2342 if (!locals)
2343 return NULL;
2344 }
2345
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002346 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347}
2348
2349static stmt_ty
2350ast_for_assert_stmt(struct compiling *c, const node *n)
2351{
2352 /* assert_stmt: 'assert' test [',' test] */
2353 REQ(n, assert_stmt);
2354 if (NCH(n) == 2) {
2355 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2356 if (!expression)
2357 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002358 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 }
2360 else if (NCH(n) == 4) {
2361 expr_ty expr1, expr2;
2362
2363 expr1 = ast_for_expr(c, CHILD(n, 1));
2364 if (!expr1)
2365 return NULL;
2366 expr2 = ast_for_expr(c, CHILD(n, 3));
2367 if (!expr2)
2368 return NULL;
2369
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002370 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371 }
Neal Norwitz79792652005-11-14 04:25:03 +00002372 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 "improper number of parts to 'assert' statement: %d",
2374 NCH(n));
2375 return NULL;
2376}
2377
2378static asdl_seq *
2379ast_for_suite(struct compiling *c, const node *n)
2380{
2381 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002382 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 stmt_ty s;
2384 int i, total, num, end, pos = 0;
2385 node *ch;
2386
2387 REQ(n, suite);
2388
2389 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002390 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 if (!seq)
2392 return NULL;
2393 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2394 n = CHILD(n, 0);
2395 /* simple_stmt always ends with a NEWLINE,
2396 and may have a trailing SEMI
2397 */
2398 end = NCH(n) - 1;
2399 if (TYPE(CHILD(n, end - 1)) == SEMI)
2400 end--;
2401 /* loop by 2 to skip semi-colons */
2402 for (i = 0; i < end; i += 2) {
2403 ch = CHILD(n, i);
2404 s = ast_for_stmt(c, ch);
2405 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002406 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 asdl_seq_SET(seq, pos++, s);
2408 }
2409 }
2410 else {
2411 for (i = 2; i < (NCH(n) - 1); i++) {
2412 ch = CHILD(n, i);
2413 REQ(ch, stmt);
2414 num = num_stmts(ch);
2415 if (num == 1) {
2416 /* small_stmt or compound_stmt with only one child */
2417 s = ast_for_stmt(c, ch);
2418 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002419 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 asdl_seq_SET(seq, pos++, s);
2421 }
2422 else {
2423 int j;
2424 ch = CHILD(ch, 0);
2425 REQ(ch, simple_stmt);
2426 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002427 /* statement terminates with a semi-colon ';' */
2428 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002429 assert((j + 1) == NCH(ch));
2430 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002431 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432 s = ast_for_stmt(c, CHILD(ch, j));
2433 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002434 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435 asdl_seq_SET(seq, pos++, s);
2436 }
2437 }
2438 }
2439 }
2440 assert(pos == seq->size);
2441 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442}
2443
2444static stmt_ty
2445ast_for_if_stmt(struct compiling *c, const node *n)
2446{
2447 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2448 ['else' ':' suite]
2449 */
2450 char *s;
2451
2452 REQ(n, if_stmt);
2453
2454 if (NCH(n) == 4) {
2455 expr_ty expression;
2456 asdl_seq *suite_seq;
2457
2458 expression = ast_for_expr(c, CHILD(n, 1));
2459 if (!expression)
2460 return NULL;
2461 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002462 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463 return NULL;
2464
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002465 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002467
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 s = STR(CHILD(n, 4));
2469 /* s[2], the third character in the string, will be
2470 's' for el_s_e, or
2471 'i' for el_i_f
2472 */
2473 if (s[2] == 's') {
2474 expr_ty expression;
2475 asdl_seq *seq1, *seq2;
2476
2477 expression = ast_for_expr(c, CHILD(n, 1));
2478 if (!expression)
2479 return NULL;
2480 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002481 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 return NULL;
2483 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002484 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 return NULL;
2486
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002487 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 }
2489 else if (s[2] == 'i') {
2490 int i, n_elif, has_else = 0;
2491 asdl_seq *orelse = NULL;
2492 n_elif = NCH(n) - 4;
2493 /* must reference the child n_elif+1 since 'else' token is third,
2494 not fourth, child from the end. */
2495 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2496 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2497 has_else = 1;
2498 n_elif -= 3;
2499 }
2500 n_elif /= 4;
2501
2502 if (has_else) {
2503 expr_ty expression;
2504 asdl_seq *seq1, *seq2;
2505
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002506 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 if (!orelse)
2508 return NULL;
2509 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002510 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002513 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002516 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518
2519 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002520 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002521 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 /* the just-created orelse handled the last elif */
2523 n_elif--;
2524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525
2526 for (i = 0; i < n_elif; i++) {
2527 int off = 5 + (n_elif - i - 1) * 4;
2528 expr_ty expression;
2529 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002530 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002531 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 return NULL;
2533 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002534 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002537 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539
2540 asdl_seq_SET(new, 0,
2541 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002542 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 orelse = new;
2544 }
2545 return If(ast_for_expr(c, CHILD(n, 1)),
2546 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002547 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002549
2550 PyErr_Format(PyExc_SystemError,
2551 "unexpected token in 'if' statement: %s", s);
2552 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553}
2554
2555static stmt_ty
2556ast_for_while_stmt(struct compiling *c, const node *n)
2557{
2558 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2559 REQ(n, while_stmt);
2560
2561 if (NCH(n) == 4) {
2562 expr_ty expression;
2563 asdl_seq *suite_seq;
2564
2565 expression = ast_for_expr(c, CHILD(n, 1));
2566 if (!expression)
2567 return NULL;
2568 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002569 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002571 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 }
2573 else if (NCH(n) == 7) {
2574 expr_ty expression;
2575 asdl_seq *seq1, *seq2;
2576
2577 expression = ast_for_expr(c, CHILD(n, 1));
2578 if (!expression)
2579 return NULL;
2580 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002581 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 return NULL;
2583 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002584 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 return NULL;
2586
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002587 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002589
2590 PyErr_Format(PyExc_SystemError,
2591 "wrong number of tokens for 'while' statement: %d",
2592 NCH(n));
2593 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594}
2595
2596static stmt_ty
2597ast_for_for_stmt(struct compiling *c, const node *n)
2598{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002599 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 expr_ty expression;
2601 expr_ty target;
2602 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2603 REQ(n, for_stmt);
2604
2605 if (NCH(n) == 9) {
2606 seq = ast_for_suite(c, CHILD(n, 8));
2607 if (!seq)
2608 return NULL;
2609 }
2610
2611 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002612 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002614 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002617 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002619 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002620 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 return NULL;
2622 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002623 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 return NULL;
2625
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002626 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627}
2628
2629static excepthandler_ty
2630ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2631{
2632 /* except_clause: 'except' [test [',' test]] */
2633 REQ(exc, except_clause);
2634 REQ(body, suite);
2635
2636 if (NCH(exc) == 1) {
2637 asdl_seq *suite_seq = ast_for_suite(c, body);
2638 if (!suite_seq)
2639 return NULL;
2640
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002641 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 }
2643 else if (NCH(exc) == 2) {
2644 expr_ty expression;
2645 asdl_seq *suite_seq;
2646
2647 expression = ast_for_expr(c, CHILD(exc, 1));
2648 if (!expression)
2649 return NULL;
2650 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002651 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 return NULL;
2653
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002654 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 }
2656 else if (NCH(exc) == 4) {
2657 asdl_seq *suite_seq;
2658 expr_ty expression;
2659 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2660 if (!e)
2661 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002662 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 return NULL;
2664 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002665 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 return NULL;
2667 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002668 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 return NULL;
2670
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002671 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002673
2674 PyErr_Format(PyExc_SystemError,
2675 "wrong number of children for 'except' clause: %d",
2676 NCH(exc));
2677 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678}
2679
2680static stmt_ty
2681ast_for_try_stmt(struct compiling *c, const node *n)
2682{
Neal Norwitzf599f422005-12-17 21:33:47 +00002683 const int nch = NCH(n);
2684 int n_except = (nch - 3)/3;
2685 asdl_seq *body, *orelse = NULL, *finally = NULL;
2686
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687 REQ(n, try_stmt);
2688
Neal Norwitzf599f422005-12-17 21:33:47 +00002689 body = ast_for_suite(c, CHILD(n, 2));
2690 if (body == NULL)
2691 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692
Neal Norwitzf599f422005-12-17 21:33:47 +00002693 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2694 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2695 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2696 /* we can assume it's an "else",
2697 because nch >= 9 for try-else-finally and
2698 it would otherwise have a type of except_clause */
2699 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2700 if (orelse == NULL)
2701 return NULL;
2702 n_except--;
2703 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704
Neal Norwitzf599f422005-12-17 21:33:47 +00002705 finally = ast_for_suite(c, CHILD(n, nch - 1));
2706 if (finally == NULL)
2707 return NULL;
2708 n_except--;
2709 }
2710 else {
2711 /* we can assume it's an "else",
2712 otherwise it would have a type of except_clause */
2713 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2714 if (orelse == NULL)
2715 return NULL;
2716 n_except--;
2717 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002719 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002720 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721 return NULL;
2722 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002723
2724 if (n_except > 0) {
2725 int i;
2726 stmt_ty except_st;
2727 /* process except statements to create a try ... except */
2728 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2729 if (handlers == NULL)
2730 return NULL;
2731
2732 for (i = 0; i < n_except; i++) {
2733 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2734 CHILD(n, 5 + i * 3));
2735 if (!e)
2736 return NULL;
2737 asdl_seq_SET(handlers, i, e);
2738 }
2739
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002740 except_st = TryExcept(body, handlers, orelse, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002741 if (!finally)
2742 return except_st;
2743
2744 /* if a 'finally' is present too, we nest the TryExcept within a
2745 TryFinally to emulate try ... except ... finally */
2746 body = asdl_seq_new(1, c->c_arena);
2747 if (body == NULL)
2748 return NULL;
2749 asdl_seq_SET(body, 0, except_st);
2750 }
2751
2752 /* must be a try ... finally (except clauses are in body, if any exist) */
2753 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002754 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755}
2756
Guido van Rossumc2e20742006-02-27 22:32:47 +00002757static expr_ty
2758ast_for_with_var(struct compiling *c, const node *n)
2759{
2760 REQ(n, with_var);
2761 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2762 ast_error(n, "expected \"with [expr] as [var]\"");
2763 return NULL;
2764 }
2765 return ast_for_expr(c, CHILD(n, 1));
2766}
2767
2768/* with_stmt: 'with' test [ with_var ] ':' suite */
2769static stmt_ty
2770ast_for_with_stmt(struct compiling *c, const node *n)
2771{
2772 expr_ty context_expr, optional_vars = NULL;
2773 int suite_index = 3; /* skip 'with', test, and ':' */
2774 asdl_seq *suite_seq;
2775
2776 assert(TYPE(n) == with_stmt);
2777 context_expr = ast_for_expr(c, CHILD(n, 1));
2778 if (TYPE(CHILD(n, 2)) == with_var) {
2779 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2780
2781 if (!optional_vars) {
2782 return NULL;
2783 }
2784 if (!set_context(optional_vars, Store, n)) {
2785 return NULL;
2786 }
2787 suite_index = 4;
2788 }
2789
2790 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2791 if (!suite_seq) {
2792 return NULL;
2793 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002794 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2795 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002796}
2797
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798static stmt_ty
2799ast_for_classdef(struct compiling *c, const node *n)
2800{
2801 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 asdl_seq *bases, *s;
2803
2804 REQ(n, classdef);
2805
2806 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2807 ast_error(n, "assignment to None");
2808 return NULL;
2809 }
2810
2811 if (NCH(n) == 4) {
2812 s = ast_for_suite(c, CHILD(n, 3));
2813 if (!s)
2814 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002815 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002816 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 }
2818 /* check for empty base list */
2819 if (TYPE(CHILD(n,3)) == RPAR) {
2820 s = ast_for_suite(c, CHILD(n,5));
2821 if (!s)
2822 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002823 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002824 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 }
2826
2827 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002828 bases = ast_for_class_bases(c, CHILD(n, 3));
2829 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
2832 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002833 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002835 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002836 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837}
2838
2839static stmt_ty
2840ast_for_stmt(struct compiling *c, const node *n)
2841{
2842 if (TYPE(n) == stmt) {
2843 assert(NCH(n) == 1);
2844 n = CHILD(n, 0);
2845 }
2846 if (TYPE(n) == simple_stmt) {
2847 assert(num_stmts(n) == 1);
2848 n = CHILD(n, 0);
2849 }
2850 if (TYPE(n) == small_stmt) {
2851 REQ(n, small_stmt);
2852 n = CHILD(n, 0);
2853 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2854 | flow_stmt | import_stmt | global_stmt | exec_stmt
2855 | assert_stmt
2856 */
2857 switch (TYPE(n)) {
2858 case expr_stmt:
2859 return ast_for_expr_stmt(c, n);
2860 case print_stmt:
2861 return ast_for_print_stmt(c, n);
2862 case del_stmt:
2863 return ast_for_del_stmt(c, n);
2864 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002865 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 case flow_stmt:
2867 return ast_for_flow_stmt(c, n);
2868 case import_stmt:
2869 return ast_for_import_stmt(c, n);
2870 case global_stmt:
2871 return ast_for_global_stmt(c, n);
2872 case exec_stmt:
2873 return ast_for_exec_stmt(c, n);
2874 case assert_stmt:
2875 return ast_for_assert_stmt(c, n);
2876 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002877 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2879 TYPE(n), NCH(n));
2880 return NULL;
2881 }
2882 }
2883 else {
2884 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2885 | funcdef | classdef
2886 */
2887 node *ch = CHILD(n, 0);
2888 REQ(n, compound_stmt);
2889 switch (TYPE(ch)) {
2890 case if_stmt:
2891 return ast_for_if_stmt(c, ch);
2892 case while_stmt:
2893 return ast_for_while_stmt(c, ch);
2894 case for_stmt:
2895 return ast_for_for_stmt(c, ch);
2896 case try_stmt:
2897 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002898 case with_stmt:
2899 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 case funcdef:
2901 return ast_for_funcdef(c, ch);
2902 case classdef:
2903 return ast_for_classdef(c, ch);
2904 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002905 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2907 TYPE(n), NCH(n));
2908 return NULL;
2909 }
2910 }
2911}
2912
2913static PyObject *
2914parsenumber(const char *s)
2915{
2916 const char *end;
2917 long x;
2918 double dx;
2919#ifndef WITHOUT_COMPLEX
2920 Py_complex c;
2921 int imflag;
2922#endif
2923
2924 errno = 0;
2925 end = s + strlen(s) - 1;
2926#ifndef WITHOUT_COMPLEX
2927 imflag = *end == 'j' || *end == 'J';
2928#endif
2929 if (*end == 'l' || *end == 'L')
2930 return PyLong_FromString((char *)s, (char **)0, 0);
2931 if (s[0] == '0') {
2932 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2933 if (x < 0 && errno == 0) {
2934 return PyLong_FromString((char *)s,
2935 (char **)0,
2936 0);
2937 }
2938 }
2939 else
2940 x = PyOS_strtol((char *)s, (char **)&end, 0);
2941 if (*end == '\0') {
2942 if (errno != 0)
2943 return PyLong_FromString((char *)s, (char **)0, 0);
2944 return PyInt_FromLong(x);
2945 }
2946 /* XXX Huge floats may silently fail */
2947#ifndef WITHOUT_COMPLEX
2948 if (imflag) {
2949 c.real = 0.;
2950 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002951 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 PyFPE_END_PROTECT(c)
2953 return PyComplex_FromCComplex(c);
2954 }
2955 else
2956#endif
2957 {
2958 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002959 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 PyFPE_END_PROTECT(dx)
2961 return PyFloat_FromDouble(dx);
2962 }
2963}
2964
2965static PyObject *
2966decode_utf8(const char **sPtr, const char *end, char* encoding)
2967{
2968#ifndef Py_USING_UNICODE
2969 Py_FatalError("decode_utf8 should not be called in this build.");
2970 return NULL;
2971#else
2972 PyObject *u, *v;
2973 char *s, *t;
2974 t = s = (char *)*sPtr;
2975 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2976 while (s < end && (*s & 0x80)) s++;
2977 *sPtr = s;
2978 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2979 if (u == NULL)
2980 return NULL;
2981 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2982 Py_DECREF(u);
2983 return v;
2984#endif
2985}
2986
2987static PyObject *
2988decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2989{
2990 PyObject *v, *u;
2991 char *buf;
2992 char *p;
2993 const char *end;
2994 if (encoding == NULL) {
2995 buf = (char *)s;
2996 u = NULL;
2997 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2998 buf = (char *)s;
2999 u = NULL;
3000 } else {
3001 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3002 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3003 if (u == NULL)
3004 return NULL;
3005 p = buf = PyString_AsString(u);
3006 end = s + len;
3007 while (s < end) {
3008 if (*s == '\\') {
3009 *p++ = *s++;
3010 if (*s & 0x80) {
3011 strcpy(p, "u005c");
3012 p += 5;
3013 }
3014 }
3015 if (*s & 0x80) { /* XXX inefficient */
3016 PyObject *w;
3017 char *r;
3018 int rn, i;
3019 w = decode_utf8(&s, end, "utf-16-be");
3020 if (w == NULL) {
3021 Py_DECREF(u);
3022 return NULL;
3023 }
3024 r = PyString_AsString(w);
3025 rn = PyString_Size(w);
3026 assert(rn % 2 == 0);
3027 for (i = 0; i < rn; i += 2) {
3028 sprintf(p, "\\u%02x%02x",
3029 r[i + 0] & 0xFF,
3030 r[i + 1] & 0xFF);
3031 p += 6;
3032 }
3033 Py_DECREF(w);
3034 } else {
3035 *p++ = *s++;
3036 }
3037 }
3038 len = p - buf;
3039 s = buf;
3040 }
3041 if (rawmode)
3042 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3043 else
3044 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3045 Py_XDECREF(u);
3046 return v;
3047}
3048
3049/* s is a Python string literal, including the bracketing quote characters,
3050 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3051 * parsestr parses it, and returns the decoded Python string object.
3052 */
3053static PyObject *
3054parsestr(const char *s, const char *encoding)
3055{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003057 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058 int rawmode = 0;
3059 int need_encoding;
3060 int unicode = 0;
3061
3062 if (isalpha(quote) || quote == '_') {
3063 if (quote == 'u' || quote == 'U') {
3064 quote = *++s;
3065 unicode = 1;
3066 }
3067 if (quote == 'r' || quote == 'R') {
3068 quote = *++s;
3069 rawmode = 1;
3070 }
3071 }
3072 if (quote != '\'' && quote != '\"') {
3073 PyErr_BadInternalCall();
3074 return NULL;
3075 }
3076 s++;
3077 len = strlen(s);
3078 if (len > INT_MAX) {
3079 PyErr_SetString(PyExc_OverflowError,
3080 "string to parse is too long");
3081 return NULL;
3082 }
3083 if (s[--len] != quote) {
3084 PyErr_BadInternalCall();
3085 return NULL;
3086 }
3087 if (len >= 4 && s[0] == quote && s[1] == quote) {
3088 s += 2;
3089 len -= 2;
3090 if (s[--len] != quote || s[--len] != quote) {
3091 PyErr_BadInternalCall();
3092 return NULL;
3093 }
3094 }
3095#ifdef Py_USING_UNICODE
3096 if (unicode || Py_UnicodeFlag) {
3097 return decode_unicode(s, len, rawmode, encoding);
3098 }
3099#endif
3100 need_encoding = (encoding != NULL &&
3101 strcmp(encoding, "utf-8") != 0 &&
3102 strcmp(encoding, "iso-8859-1") != 0);
3103 if (rawmode || strchr(s, '\\') == NULL) {
3104 if (need_encoding) {
3105#ifndef Py_USING_UNICODE
3106 /* This should not happen - we never see any other
3107 encoding. */
3108 Py_FatalError("cannot deal with encodings in this build.");
3109#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003110 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 if (u == NULL)
3112 return NULL;
3113 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3114 Py_DECREF(u);
3115 return v;
3116#endif
3117 } else {
3118 return PyString_FromStringAndSize(s, len);
3119 }
3120 }
3121
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003122 return PyString_DecodeEscape(s, len, NULL, unicode,
3123 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124}
3125
3126/* Build a Python string object out of a STRING atom. This takes care of
3127 * compile-time literal catenation, calling parsestr() on each piece, and
3128 * pasting the intermediate results together.
3129 */
3130static PyObject *
3131parsestrplus(struct compiling *c, const node *n)
3132{
3133 PyObject *v;
3134 int i;
3135 REQ(CHILD(n, 0), STRING);
3136 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3137 /* String literal concatenation */
3138 for (i = 1; i < NCH(n); i++) {
3139 PyObject *s;
3140 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3141 if (s == NULL)
3142 goto onError;
3143 if (PyString_Check(v) && PyString_Check(s)) {
3144 PyString_ConcatAndDel(&v, s);
3145 if (v == NULL)
3146 goto onError;
3147 }
3148#ifdef Py_USING_UNICODE
3149 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003150 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 Py_DECREF(v);
3153 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003154 if (v == NULL)
3155 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 }
3157#endif
3158 }
3159 }
3160 return v;
3161
3162 onError:
3163 Py_XDECREF(v);
3164 return NULL;
3165}