blob: bb2f3a391f72d1c6aa3c5a47c6dac7e23fd61852 [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 *);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
251 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 }
254 else {
255 n = CHILD(n, 0);
256 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000257 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 if (!stmts)
259 goto error;
260 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000261 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262 if (!s)
263 goto error;
264 asdl_seq_SET(stmts, 0, s);
265 }
266 else {
267 /* Only a simple_stmt can contain multiple statements. */
268 REQ(n, simple_stmt);
269 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 if (TYPE(CHILD(n, i)) == NEWLINE)
271 break;
272 s = ast_for_stmt(&c, CHILD(n, i));
273 if (!s)
274 goto error;
275 asdl_seq_SET(stmts, i / 2, s);
276 }
277 }
278
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000279 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 }
281 default:
282 goto error;
283 }
284 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 ast_error_finish(filename);
286 return NULL;
287}
288
289/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
290*/
291
292static operator_ty
293get_operator(const node *n)
294{
295 switch (TYPE(n)) {
296 case VBAR:
297 return BitOr;
298 case CIRCUMFLEX:
299 return BitXor;
300 case AMPER:
301 return BitAnd;
302 case LEFTSHIFT:
303 return LShift;
304 case RIGHTSHIFT:
305 return RShift;
306 case PLUS:
307 return Add;
308 case MINUS:
309 return Sub;
310 case STAR:
311 return Mult;
312 case SLASH:
313 return Div;
314 case DOUBLESLASH:
315 return FloorDiv;
316 case PERCENT:
317 return Mod;
318 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 }
321}
322
Jeremy Hyltona8293132006-02-28 17:58:27 +0000323/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324
325 Only sets context for expr kinds that "can appear in assignment context"
326 (according to ../Parser/Python.asdl). For other expr kinds, it sets
327 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328*/
329
330static int
331set_context(expr_ty e, expr_context_ty ctx, const node *n)
332{
333 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334 /* If a particular expression type can't be used for assign / delete,
335 set expr_name to its name and an error message will be generated.
336 */
337 const char* expr_name = NULL;
338
339 /* The ast defines augmented store and load contexts, but the
340 implementation here doesn't actually use them. The code may be
341 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000342 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000343 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000344 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345 */
346 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
348 switch (e->kind) {
349 case Attribute_kind:
350 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000351 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 return ast_error(n, "assignment to None");
353 }
354 e->v.Attribute.ctx = ctx;
355 break;
356 case Subscript_kind:
357 e->v.Subscript.ctx = ctx;
358 break;
359 case Name_kind:
360 if (ctx == Store &&
361 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
362 return ast_error(n, "assignment to None");
363 }
364 e->v.Name.ctx = ctx;
365 break;
366 case List_kind:
367 e->v.List.ctx = ctx;
368 s = e->v.List.elts;
369 break;
370 case Tuple_kind:
371 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
372 return ast_error(n, "can't assign to ()");
373 e->v.Tuple.ctx = ctx;
374 s = e->v.Tuple.elts;
375 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 case Lambda_kind:
377 expr_name = "lambda";
378 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000380 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000384 case UnaryOp_kind:
385 expr_name = "operator";
386 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000388 expr_name = "generator expression";
389 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000390 case Yield_kind:
391 expr_name = "yield expression";
392 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000393 case ListComp_kind:
394 expr_name = "list comprehension";
395 break;
396 case Dict_kind:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000397 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398 case Num_kind:
399 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000400 expr_name = "literal";
401 break;
Georg Brandl52318d62006-09-06 07:06:08 +0000402 case Ellipsis_kind:
403 expr_name = "Ellipsis";
404 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000405 case Compare_kind:
406 expr_name = "comparison";
407 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000408 case IfExp_kind:
409 expr_name = "conditional expression";
410 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000411 default:
412 PyErr_Format(PyExc_SystemError,
413 "unexpected expression in assignment %d (line %d)",
414 e->kind, e->lineno);
415 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000417 /* Check for error string set by switch */
418 if (expr_name) {
419 char buf[300];
420 PyOS_snprintf(buf, sizeof(buf),
421 "can't %s %s",
422 ctx == Store ? "assign to" : "delete",
423 expr_name);
424 return ast_error(n, buf);
425 }
426
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000428 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429 */
430 if (s) {
431 int i;
432
433 for (i = 0; i < asdl_seq_LEN(s); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000434 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 return 0;
436 }
437 }
438 return 1;
439}
440
441static operator_ty
442ast_for_augassign(const node *n)
443{
444 REQ(n, augassign);
445 n = CHILD(n, 0);
446 switch (STR(n)[0]) {
447 case '+':
448 return Add;
449 case '-':
450 return Sub;
451 case '/':
452 if (STR(n)[1] == '/')
453 return FloorDiv;
454 else
455 return Div;
456 case '%':
457 return Mod;
458 case '<':
459 return LShift;
460 case '>':
461 return RShift;
462 case '&':
463 return BitAnd;
464 case '^':
465 return BitXor;
466 case '|':
467 return BitOr;
468 case '*':
469 if (STR(n)[1] == '*')
470 return Pow;
471 else
472 return Mult;
473 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000474 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476 }
477}
478
479static cmpop_ty
480ast_for_comp_op(const node *n)
481{
Guido van Rossumb053cd82006-08-24 03:53:23 +0000482 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483 |'is' 'not'
484 */
485 REQ(n, comp_op);
486 if (NCH(n) == 1) {
487 n = CHILD(n, 0);
488 switch (TYPE(n)) {
489 case LESS:
490 return Lt;
491 case GREATER:
492 return Gt;
493 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494 return Eq;
495 case LESSEQUAL:
496 return LtE;
497 case GREATEREQUAL:
498 return GtE;
499 case NOTEQUAL:
500 return NotEq;
501 case NAME:
502 if (strcmp(STR(n), "in") == 0)
503 return In;
504 if (strcmp(STR(n), "is") == 0)
505 return Is;
506 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000507 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 }
511 }
512 else if (NCH(n) == 2) {
513 /* handle "not in" and "is not" */
514 switch (TYPE(CHILD(n, 0))) {
515 case NAME:
516 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
517 return NotIn;
518 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
519 return IsNot;
520 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000521 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000523 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 }
525 }
Neal Norwitz79792652005-11-14 04:25:03 +0000526 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000528 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529}
530
531static asdl_seq *
532seq_for_testlist(struct compiling *c, const node *n)
533{
534 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000535 asdl_seq *seq;
536 expr_ty expression;
537 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 assert(TYPE(n) == testlist
539 || TYPE(n) == listmaker
540 || TYPE(n) == testlist_gexp
541 || TYPE(n) == testlist_safe
542 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000544 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 if (!seq)
546 return NULL;
547
548 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000549 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
551 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000552 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554
555 assert(i / 2 < seq->size);
556 asdl_seq_SET(seq, i / 2, expression);
557 }
558 return seq;
559}
560
561static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000562compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563{
564 int i, len = (NCH(n) + 1) / 2;
565 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000566 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 if (!args)
568 return NULL;
569
570 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 for (i = 0; i < len; i++) {
572 const node *child = CHILD(CHILD(n, 2*i), 0);
573 expr_ty arg;
574 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000575 if (!strcmp(STR(child), "None")) {
576 ast_error(child, "assignment to None");
577 return NULL;
578 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
580 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000581 }
582 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000583 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000584 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585 asdl_seq_SET(args, i, arg);
586 }
587
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000588 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000589 if (!set_context(result, Store, n))
590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 return result;
592}
593
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594
Jeremy Hyltona8293132006-02-28 17:58:27 +0000595/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596
597static arguments_ty
598ast_for_arguments(struct compiling *c, const node *n)
599{
600 /* parameters: '(' [varargslist] ')'
601 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
602 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
603 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000604 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 asdl_seq *args, *defaults;
606 identifier vararg = NULL, kwarg = NULL;
607 node *ch;
608
609 if (TYPE(n) == parameters) {
610 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000611 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 n = CHILD(n, 1);
613 }
614 REQ(n, varargslist);
615
616 /* first count the number of normal args & defaults */
617 for (i = 0; i < NCH(n); i++) {
618 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000619 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 if (TYPE(ch) == EQUAL)
622 n_defaults++;
623 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000624 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 if (!args && n_args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000627 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628 if (!defaults && n_defaults)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000629 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630
631 /* fpdef: NAME | '(' fplist ')'
632 fplist: fpdef (',' fpdef)* [',']
633 */
634 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000635 j = 0; /* index for defaults */
636 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 while (i < NCH(n)) {
638 ch = CHILD(n, i);
639 switch (TYPE(ch)) {
640 case fpdef:
641 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
642 anything other than EQUAL or a comma? */
643 /* XXX Should NCH(n) check be made a separate check? */
644 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
646 if (!expression)
647 goto error;
648 assert(defaults != NULL);
649 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 i += 2;
651 found_default = 1;
652 }
653 else if (found_default) {
654 ast_error(n,
655 "non-default argument follows default argument");
656 goto error;
657 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 if (NCH(ch) == 3) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000659 ch = CHILD(ch, 1);
660 /* def foo((x)): is not complex, special case. */
661 if (NCH(ch) != 1) {
662 /* We have complex arguments, setup for unpacking. */
663 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
664 } else {
665 /* def foo((x)): setup for checking NAME below. */
666 ch = CHILD(ch, 0);
667 }
668 }
669 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000670 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
672 ast_error(CHILD(ch, 0), "assignment to None");
673 goto error;
674 }
Armin Rigo31441302005-10-21 12:57:31 +0000675 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000676 Param, LINENO(ch), ch->n_col_offset,
677 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 if (!name)
679 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000680 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681
682 }
683 i += 2; /* the name and the comma */
684 break;
685 case STAR:
686 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
687 ast_error(CHILD(n, i+1), "assignment to None");
688 goto error;
689 }
690 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
691 i += 3;
692 break;
693 case DOUBLESTAR:
694 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
695 ast_error(CHILD(n, i+1), "assignment to None");
696 goto error;
697 }
698 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
699 i += 3;
700 break;
701 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000702 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 "unexpected node in varargslist: %d @ %d",
704 TYPE(ch), i);
705 goto error;
706 }
707 }
708
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000709 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710
711 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000712 Py_XDECREF(vararg);
713 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714 return NULL;
715}
716
717static expr_ty
718ast_for_dotted_name(struct compiling *c, const node *n)
719{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000720 expr_ty e;
721 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000722 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 int i;
724
725 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000726
727 lineno = LINENO(n);
728 col_offset = n->n_col_offset;
729
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730 id = NEW_IDENTIFIER(CHILD(n, 0));
731 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000732 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000733 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000735 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
737 for (i = 2; i < NCH(n); i+=2) {
738 id = NEW_IDENTIFIER(CHILD(n, i));
739 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000740 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000741 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000742 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 }
745
746 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747}
748
749static expr_ty
750ast_for_decorator(struct compiling *c, const node *n)
751{
752 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
753 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000754 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755
756 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000757 REQ(CHILD(n, 0), AT);
758 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759
760 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
761 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000762 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
764 if (NCH(n) == 3) { /* No arguments */
765 d = name_expr;
766 name_expr = NULL;
767 }
768 else if (NCH(n) == 5) { /* Call with no arguments */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000769 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
770 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000772 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 name_expr = NULL;
774 }
775 else {
776 d = ast_for_call(c, CHILD(n, 3), name_expr);
777 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000778 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 name_expr = NULL;
780 }
781
782 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783}
784
785static asdl_seq*
786ast_for_decorators(struct compiling *c, const node *n)
787{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000788 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000789 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 int i;
791
792 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000793 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 if (!decorator_seq)
795 return NULL;
796
797 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000798 d = ast_for_decorator(c, CHILD(n, i));
799 if (!d)
800 return NULL;
801 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 }
803 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804}
805
806static stmt_ty
807ast_for_funcdef(struct compiling *c, const node *n)
808{
809 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000810 identifier name;
811 arguments_ty args;
812 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 asdl_seq *decorator_seq = NULL;
814 int name_i;
815
816 REQ(n, funcdef);
817
818 if (NCH(n) == 6) { /* decorators are present */
819 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
820 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000821 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 name_i = 2;
823 }
824 else {
825 name_i = 1;
826 }
827
828 name = NEW_IDENTIFIER(CHILD(n, name_i));
829 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000832 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 }
835 args = ast_for_arguments(c, CHILD(n, name_i + 1));
836 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000837 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 body = ast_for_suite(c, CHILD(n, name_i + 3));
839 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000840 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000842 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
843 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844}
845
846static expr_ty
847ast_for_lambdef(struct compiling *c, const node *n)
848{
849 /* lambdef: 'lambda' [varargslist] ':' test */
850 arguments_ty args;
851 expr_ty expression;
852
853 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000854 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 if (!args)
856 return NULL;
857 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000858 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 }
861 else {
862 args = ast_for_arguments(c, CHILD(n, 1));
863 if (!args)
864 return NULL;
865 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000866 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 }
869
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000870 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871}
872
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000873static expr_ty
874ast_for_ifexpr(struct compiling *c, const node *n)
875{
876 /* test: or_test 'if' or_test 'else' test */
877 expr_ty expression, body, orelse;
878
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000879 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000880 body = ast_for_expr(c, CHILD(n, 0));
881 if (!body)
882 return NULL;
883 expression = ast_for_expr(c, CHILD(n, 2));
884 if (!expression)
885 return NULL;
886 orelse = ast_for_expr(c, CHILD(n, 4));
887 if (!orelse)
888 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000889 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
890 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000891}
892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893/* Count the number of 'for' loop in a list comprehension.
894
895 Helper for ast_for_listcomp().
896*/
897
898static int
899count_list_fors(const node *n)
900{
901 int n_fors = 0;
902 node *ch = CHILD(n, 1);
903
904 count_list_for:
905 n_fors++;
906 REQ(ch, list_for);
907 if (NCH(ch) == 5)
908 ch = CHILD(ch, 4);
909 else
910 return n_fors;
911 count_list_iter:
912 REQ(ch, list_iter);
913 ch = CHILD(ch, 0);
914 if (TYPE(ch) == list_for)
915 goto count_list_for;
916 else if (TYPE(ch) == list_if) {
917 if (NCH(ch) == 3) {
918 ch = CHILD(ch, 2);
919 goto count_list_iter;
920 }
921 else
922 return n_fors;
923 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000924
925 /* Should never be reached */
926 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
927 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928}
929
930/* Count the number of 'if' statements in a list comprehension.
931
932 Helper for ast_for_listcomp().
933*/
934
935static int
936count_list_ifs(const node *n)
937{
938 int n_ifs = 0;
939
940 count_list_iter:
941 REQ(n, list_iter);
942 if (TYPE(CHILD(n, 0)) == list_for)
943 return n_ifs;
944 n = CHILD(n, 0);
945 REQ(n, list_if);
946 n_ifs++;
947 if (NCH(n) == 2)
948 return n_ifs;
949 n = CHILD(n, 2);
950 goto count_list_iter;
951}
952
953static expr_ty
954ast_for_listcomp(struct compiling *c, const node *n)
955{
956 /* listmaker: test ( list_for | (',' test)* [','] )
957 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
958 list_iter: list_for | list_if
959 list_if: 'if' test [list_iter]
960 testlist_safe: test [(',' test)+ [',']]
961 */
962 expr_ty elt;
963 asdl_seq *listcomps;
964 int i, n_fors;
965 node *ch;
966
967 REQ(n, listmaker);
968 assert(NCH(n) > 1);
969
970 elt = ast_for_expr(c, CHILD(n, 0));
971 if (!elt)
972 return NULL;
973
974 n_fors = count_list_fors(n);
975 if (n_fors == -1)
976 return NULL;
977
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000978 listcomps = asdl_seq_new(n_fors, c->c_arena);
979 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000981
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 ch = CHILD(n, 1);
983 for (i = 0; i < n_fors; i++) {
984 comprehension_ty lc;
985 asdl_seq *t;
986 expr_ty expression;
987
988 REQ(ch, list_for);
989
990 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000991 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000993 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000994 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000997 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000999 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001001 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1002 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001003 expression, NULL, c->c_arena);
1004 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006
1007 if (NCH(ch) == 5) {
1008 int j, n_ifs;
1009 asdl_seq *ifs;
1010
1011 ch = CHILD(ch, 4);
1012 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001013 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001016 ifs = asdl_seq_new(n_ifs, c->c_arena);
1017 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019
1020 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001021 REQ(ch, list_iter);
1022 ch = CHILD(ch, 0);
1023 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024
Jeremy Hyltona8293132006-02-28 17:58:27 +00001025 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1026 if (NCH(ch) == 3)
1027 ch = CHILD(ch, 2);
1028 }
1029 /* on exit, must guarantee that ch is a list_for */
1030 if (TYPE(ch) == list_iter)
1031 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001033 }
1034 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 }
1036
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001037 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038}
1039
1040/*
1041 Count the number of 'for' loops in a generator expression.
1042
1043 Helper for ast_for_genexp().
1044*/
1045
1046static int
1047count_gen_fors(const node *n)
1048{
1049 int n_fors = 0;
1050 node *ch = CHILD(n, 1);
1051
1052 count_gen_for:
1053 n_fors++;
1054 REQ(ch, gen_for);
1055 if (NCH(ch) == 5)
1056 ch = CHILD(ch, 4);
1057 else
1058 return n_fors;
1059 count_gen_iter:
1060 REQ(ch, gen_iter);
1061 ch = CHILD(ch, 0);
1062 if (TYPE(ch) == gen_for)
1063 goto count_gen_for;
1064 else if (TYPE(ch) == gen_if) {
1065 if (NCH(ch) == 3) {
1066 ch = CHILD(ch, 2);
1067 goto count_gen_iter;
1068 }
1069 else
1070 return n_fors;
1071 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001072
1073 /* Should never be reached */
1074 PyErr_SetString(PyExc_SystemError,
1075 "logic error in count_gen_fors");
1076 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
1078
1079/* Count the number of 'if' statements in a generator expression.
1080
1081 Helper for ast_for_genexp().
1082*/
1083
1084static int
1085count_gen_ifs(const node *n)
1086{
1087 int n_ifs = 0;
1088
1089 while (1) {
1090 REQ(n, gen_iter);
1091 if (TYPE(CHILD(n, 0)) == gen_for)
1092 return n_ifs;
1093 n = CHILD(n, 0);
1094 REQ(n, gen_if);
1095 n_ifs++;
1096 if (NCH(n) == 2)
1097 return n_ifs;
1098 n = CHILD(n, 2);
1099 }
1100}
1101
Jeremy Hyltona8293132006-02-28 17:58:27 +00001102/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103static expr_ty
1104ast_for_genexp(struct compiling *c, const node *n)
1105{
1106 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1107 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1108 expr_ty elt;
1109 asdl_seq *genexps;
1110 int i, n_fors;
1111 node *ch;
1112
1113 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1114 assert(NCH(n) > 1);
1115
1116 elt = ast_for_expr(c, CHILD(n, 0));
1117 if (!elt)
1118 return NULL;
1119
1120 n_fors = count_gen_fors(n);
1121 if (n_fors == -1)
1122 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001123
1124 genexps = asdl_seq_new(n_fors, c->c_arena);
1125 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001127
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 ch = CHILD(n, 1);
1129 for (i = 0; i < n_fors; i++) {
1130 comprehension_ty ge;
1131 asdl_seq *t;
1132 expr_ty expression;
1133
1134 REQ(ch, gen_for);
1135
1136 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001137 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001139 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001140 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001142
1143 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001145 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1148 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001149 expression, NULL, c->c_arena);
1150
1151 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 if (NCH(ch) == 5) {
1155 int j, n_ifs;
1156 asdl_seq *ifs;
1157
1158 ch = CHILD(ch, 4);
1159 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001160 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001162
1163 ifs = asdl_seq_new(n_ifs, c->c_arena);
1164 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001166
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 for (j = 0; j < n_ifs; j++) {
1168 REQ(ch, gen_iter);
1169 ch = CHILD(ch, 0);
1170 REQ(ch, gen_if);
1171
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001172 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001173 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001174 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001175 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 if (NCH(ch) == 3)
1177 ch = CHILD(ch, 2);
1178 }
1179 /* on exit, must guarantee that ch is a gen_for */
1180 if (TYPE(ch) == gen_iter)
1181 ch = CHILD(ch, 0);
1182 ge->ifs = ifs;
1183 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001184 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 }
1186
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001187 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188}
1189
1190static expr_ty
1191ast_for_atom(struct compiling *c, const node *n)
1192{
1193 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
Guido van Rossum86e58e22006-08-28 15:27:34 +00001194 | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING+
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 */
1196 node *ch = CHILD(n, 0);
1197
1198 switch (TYPE(ch)) {
1199 case NAME:
1200 /* All names start in Load context, but may later be
1201 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001202 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 case STRING: {
1204 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 if (!str)
1206 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001207
1208 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001209 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 }
1211 case NUMBER: {
1212 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 if (!pynum)
1214 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001215
1216 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001217 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 }
Georg Brandl52318d62006-09-06 07:06:08 +00001219 case DOT:
1220 /* Ellipsis */
1221 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 case LPAR: /* some parenthesized expressions */
1223 ch = CHILD(n, 1);
1224
1225 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001226 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227
1228 if (TYPE(ch) == yield_expr)
1229 return ast_for_expr(c, ch);
1230
1231 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1232 return ast_for_genexp(c, ch);
1233
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001234 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 case LSQB: /* list (or list comprehension) */
1236 ch = CHILD(n, 1);
1237
1238 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001239 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240
1241 REQ(ch, listmaker);
1242 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1243 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 if (!elts)
1245 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001246
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001247 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 }
1249 else
1250 return ast_for_listcomp(c, ch);
1251 case LBRACE: {
Guido van Rossum86e58e22006-08-28 15:27:34 +00001252 /* dictsetmaker: test ':' test (',' test ':' test)* [','] |
1253 * test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 int i, size;
1255 asdl_seq *keys, *values;
1256
1257 ch = CHILD(n, 1);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001258 if (NCH(ch) == 1 || (NCH(ch) > 0 && STR(CHILD(ch, 1))[0] == ',')) {
1259 /* it's a set */
1260 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
1261 keys = asdl_seq_new(size, c->c_arena);
1262 if (!keys)
1263 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001264
Guido van Rossum86e58e22006-08-28 15:27:34 +00001265 for (i = 0; i < NCH(ch); i += 2) {
1266 expr_ty expression;
1267 expression = ast_for_expr(c, CHILD(ch, i));
1268 if (!expression)
1269 return NULL;
1270 asdl_seq_SET(keys, i / 2, expression);
1271 }
1272 return Set(keys, LINENO(n), n->n_col_offset, c->c_arena);
1273 } else {
1274 /* it's a dict */
1275 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1276 keys = asdl_seq_new(size, c->c_arena);
1277 if (!keys)
1278 return NULL;
1279
1280 values = asdl_seq_new(size, c->c_arena);
1281 if (!values)
1282 return NULL;
1283
1284 for (i = 0; i < NCH(ch); i += 4) {
1285 expr_ty expression;
1286
1287 expression = ast_for_expr(c, CHILD(ch, i));
1288 if (!expression)
1289 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001290
Guido van Rossum86e58e22006-08-28 15:27:34 +00001291 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitze76adcd2005-11-15 05:04:31 +00001292
Guido van Rossum86e58e22006-08-28 15:27:34 +00001293 expression = ast_for_expr(c, CHILD(ch, i + 2));
1294 if (!expression)
1295 return NULL;
1296
1297 asdl_seq_SET(values, i / 4, expression);
1298 }
1299 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001303 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 return NULL;
1305 }
1306}
1307
1308static slice_ty
1309ast_for_slice(struct compiling *c, const node *n)
1310{
1311 node *ch;
1312 expr_ty lower = NULL, upper = NULL, step = NULL;
1313
1314 REQ(n, subscript);
1315
1316 /*
Georg Brandl52318d62006-09-06 07:06:08 +00001317 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 sliceop: ':' [test]
1319 */
1320 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 if (NCH(n) == 1 && TYPE(ch) == test) {
1322 /* 'step' variable hold no significance in terms of being used over
1323 other vars */
1324 step = ast_for_expr(c, ch);
1325 if (!step)
1326 return NULL;
1327
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001328 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 }
1330
1331 if (TYPE(ch) == test) {
1332 lower = ast_for_expr(c, ch);
1333 if (!lower)
1334 return NULL;
1335 }
1336
1337 /* If there's an upper bound it's in the second or third position. */
1338 if (TYPE(ch) == COLON) {
1339 if (NCH(n) > 1) {
1340 node *n2 = CHILD(n, 1);
1341
1342 if (TYPE(n2) == test) {
1343 upper = ast_for_expr(c, n2);
1344 if (!upper)
1345 return NULL;
1346 }
1347 }
1348 } else if (NCH(n) > 2) {
1349 node *n2 = CHILD(n, 2);
1350
1351 if (TYPE(n2) == test) {
1352 upper = ast_for_expr(c, n2);
1353 if (!upper)
1354 return NULL;
1355 }
1356 }
1357
1358 ch = CHILD(n, NCH(n) - 1);
1359 if (TYPE(ch) == sliceop) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001360 if (NCH(ch) == 1) {
1361 /* No expression, so step is None */
1362 ch = CHILD(ch, 0);
1363 step = Name(new_identifier("None", c->c_arena), Load,
1364 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 if (!step)
1366 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001367 } else {
1368 ch = CHILD(ch, 1);
1369 if (TYPE(ch) == test) {
1370 step = ast_for_expr(c, ch);
1371 if (!step)
1372 return NULL;
1373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 }
1375 }
1376
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001377 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378}
1379
1380static expr_ty
1381ast_for_binop(struct compiling *c, const node *n)
1382{
1383 /* Must account for a sequence of expressions.
1384 How should A op B op C by represented?
1385 BinOp(BinOp(A, op, B), op, C).
1386 */
1387
1388 int i, nops;
1389 expr_ty expr1, expr2, result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001390 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391
1392 expr1 = ast_for_expr(c, CHILD(n, 0));
1393 if (!expr1)
1394 return NULL;
1395
1396 expr2 = ast_for_expr(c, CHILD(n, 2));
1397 if (!expr2)
1398 return NULL;
1399
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001400 newoperator = get_operator(CHILD(n, 1));
1401 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 return NULL;
1403
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001404 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1405 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 if (!result)
1407 return NULL;
1408
1409 nops = (NCH(n) - 1) / 2;
1410 for (i = 1; i < nops; i++) {
1411 expr_ty tmp_result, tmp;
1412 const node* next_oper = CHILD(n, i * 2 + 1);
1413
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001414 newoperator = get_operator(next_oper);
1415 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 return NULL;
1417
1418 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1419 if (!tmp)
1420 return NULL;
1421
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001422 tmp_result = BinOp(result, newoperator, tmp,
1423 LINENO(next_oper), next_oper->n_col_offset,
1424 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 if (!tmp)
1426 return NULL;
1427 result = tmp_result;
1428 }
1429 return result;
1430}
1431
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001432static expr_ty
1433ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1434{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001435 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1436 subscriptlist: subscript (',' subscript)* [',']
1437 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1438 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001439 REQ(n, trailer);
1440 if (TYPE(CHILD(n, 0)) == LPAR) {
1441 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001442 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1443 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001444 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001445 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001446 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001447 else if (TYPE(CHILD(n, 0)) == DOT ) {
1448 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001449 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001450 }
1451 else {
1452 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001453 REQ(CHILD(n, 2), RSQB);
1454 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001455 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001456 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1457 if (!slc)
1458 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001459 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1460 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001461 }
1462 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001463 /* The grammar is ambiguous here. The ambiguity is resolved
1464 by treating the sequence as a tuple literal if there are
1465 no slice features.
1466 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001467 int j;
1468 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001469 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001470 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001471 asdl_seq *slices, *elts;
1472 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001473 if (!slices)
1474 return NULL;
1475 for (j = 0; j < NCH(n); j += 2) {
1476 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001477 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001478 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001479 if (slc->kind != Index_kind)
1480 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001481 asdl_seq_SET(slices, j / 2, slc);
1482 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001483 if (!simple) {
1484 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001485 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001486 }
1487 /* extract Index values and put them in a Tuple */
1488 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001489 if (!elts)
1490 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001491 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1492 slc = (slice_ty)asdl_seq_GET(slices, j);
1493 assert(slc->kind == Index_kind && slc->v.Index.value);
1494 asdl_seq_SET(elts, j, slc->v.Index.value);
1495 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001496 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001497 if (!e)
1498 return NULL;
1499 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001500 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001501 }
1502 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001503}
1504
1505static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001506ast_for_factor(struct compiling *c, const node *n)
1507{
1508 node *pfactor, *ppower, *patom, *pnum;
1509 expr_ty expression;
1510
1511 /* If the unary - operator is applied to a constant, don't generate
1512 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1513 constant. The peephole optimizer already does something like
1514 this but it doesn't handle the case where the constant is
1515 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1516 PyLongObject.
1517 */
1518 if (TYPE(CHILD(n, 0)) == MINUS
1519 && NCH(n) == 2
1520 && TYPE((pfactor = CHILD(n, 1))) == factor
1521 && NCH(pfactor) == 1
1522 && TYPE((ppower = CHILD(pfactor, 0))) == power
1523 && NCH(ppower) == 1
1524 && TYPE((patom = CHILD(ppower, 0))) == atom
1525 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1526 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1527 if (s == NULL)
1528 return NULL;
1529 s[0] = '-';
1530 strcpy(s + 1, STR(pnum));
1531 PyObject_FREE(STR(pnum));
1532 STR(pnum) = s;
1533 return ast_for_atom(c, patom);
1534 }
1535
1536 expression = ast_for_expr(c, CHILD(n, 1));
1537 if (!expression)
1538 return NULL;
1539
1540 switch (TYPE(CHILD(n, 0))) {
1541 case PLUS:
1542 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1543 c->c_arena);
1544 case MINUS:
1545 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1546 c->c_arena);
1547 case TILDE:
1548 return UnaryOp(Invert, expression, LINENO(n),
1549 n->n_col_offset, c->c_arena);
1550 }
1551 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1552 TYPE(CHILD(n, 0)));
1553 return NULL;
1554}
1555
1556static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001557ast_for_power(struct compiling *c, const node *n)
1558{
1559 /* power: atom trailer* ('**' factor)*
1560 */
1561 int i;
1562 expr_ty e, tmp;
1563 REQ(n, power);
1564 e = ast_for_atom(c, CHILD(n, 0));
1565 if (!e)
1566 return NULL;
1567 if (NCH(n) == 1)
1568 return e;
1569 for (i = 1; i < NCH(n); i++) {
1570 node *ch = CHILD(n, i);
1571 if (TYPE(ch) != trailer)
1572 break;
1573 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001574 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001575 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001576 tmp->lineno = e->lineno;
1577 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001578 e = tmp;
1579 }
1580 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1581 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001582 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001583 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001584 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001585 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001586 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001587 e = tmp;
1588 }
1589 return e;
1590}
1591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592/* Do not name a variable 'expr'! Will cause a compile error.
1593*/
1594
1595static expr_ty
1596ast_for_expr(struct compiling *c, const node *n)
1597{
1598 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001599 test: or_test ['if' or_test 'else' test] | lambdef
1600 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601 and_test: not_test ('and' not_test)*
1602 not_test: 'not' not_test | comparison
1603 comparison: expr (comp_op expr)*
1604 expr: xor_expr ('|' xor_expr)*
1605 xor_expr: and_expr ('^' and_expr)*
1606 and_expr: shift_expr ('&' shift_expr)*
1607 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1608 arith_expr: term (('+'|'-') term)*
1609 term: factor (('*'|'/'|'%'|'//') factor)*
1610 factor: ('+'|'-'|'~') factor | power
1611 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001612
1613 As well as modified versions that exist for backward compatibility,
1614 to explicitly allow:
1615 [ x for x in lambda: 0, lambda: 1 ]
1616 (which would be ambiguous without these extra rules)
1617
1618 old_test: or_test | old_lambdef
1619 old_lambdef: 'lambda' [vararglist] ':' old_test
1620
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621 */
1622
1623 asdl_seq *seq;
1624 int i;
1625
1626 loop:
1627 switch (TYPE(n)) {
1628 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001629 case old_test:
1630 if (TYPE(CHILD(n, 0)) == lambdef ||
1631 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001633 else if (NCH(n) > 1)
1634 return ast_for_ifexpr(c, n);
1635 /* Fallthrough */
1636 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637 case and_test:
1638 if (NCH(n) == 1) {
1639 n = CHILD(n, 0);
1640 goto loop;
1641 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001642 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 if (!seq)
1644 return NULL;
1645 for (i = 0; i < NCH(n); i += 2) {
1646 expr_ty e = ast_for_expr(c, CHILD(n, i));
1647 if (!e)
1648 return NULL;
1649 asdl_seq_SET(seq, i / 2, e);
1650 }
1651 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001652 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1653 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001654 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001655 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 case not_test:
1657 if (NCH(n) == 1) {
1658 n = CHILD(n, 0);
1659 goto loop;
1660 }
1661 else {
1662 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1663 if (!expression)
1664 return NULL;
1665
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001666 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1667 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 }
1669 case comparison:
1670 if (NCH(n) == 1) {
1671 n = CHILD(n, 0);
1672 goto loop;
1673 }
1674 else {
1675 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001676 asdl_int_seq *ops;
1677 asdl_seq *cmps;
1678 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 if (!ops)
1680 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001681 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 return NULL;
1684 }
1685 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001686 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001688 newoperator = ast_for_comp_op(CHILD(n, i));
1689 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001691 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692
1693 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001694 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001696 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001698 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 asdl_seq_SET(cmps, i / 2, expression);
1700 }
1701 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001702 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001704 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001706 return Compare(expression, ops, cmps, LINENO(n),
1707 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 }
1709 break;
1710
1711 /* The next five cases all handle BinOps. The main body of code
1712 is the same in each case, but the switch turned inside out to
1713 reuse the code for each type of operator.
1714 */
1715 case expr:
1716 case xor_expr:
1717 case and_expr:
1718 case shift_expr:
1719 case arith_expr:
1720 case term:
1721 if (NCH(n) == 1) {
1722 n = CHILD(n, 0);
1723 goto loop;
1724 }
1725 return ast_for_binop(c, n);
1726 case yield_expr: {
1727 expr_ty exp = NULL;
1728 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001729 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 if (!exp)
1731 return NULL;
1732 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001733 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001735 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736 if (NCH(n) == 1) {
1737 n = CHILD(n, 0);
1738 goto loop;
1739 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001740 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001741 case power:
1742 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001744 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 return NULL;
1746 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001747 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 return NULL;
1749}
1750
1751static expr_ty
1752ast_for_call(struct compiling *c, const node *n, expr_ty func)
1753{
1754 /*
1755 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1756 | '**' test)
1757 argument: [test '='] test [gen_for] # Really [keyword '='] test
1758 */
1759
1760 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001761 asdl_seq *args;
1762 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 expr_ty vararg = NULL, kwarg = NULL;
1764
1765 REQ(n, arglist);
1766
1767 nargs = 0;
1768 nkeywords = 0;
1769 ngens = 0;
1770 for (i = 0; i < NCH(n); i++) {
1771 node *ch = CHILD(n, i);
1772 if (TYPE(ch) == argument) {
1773 if (NCH(ch) == 1)
1774 nargs++;
1775 else if (TYPE(CHILD(ch, 1)) == gen_for)
1776 ngens++;
1777 else
1778 nkeywords++;
1779 }
1780 }
1781 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001782 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783 "if not sole argument");
1784 return NULL;
1785 }
1786
1787 if (nargs + nkeywords + ngens > 255) {
1788 ast_error(n, "more than 255 arguments");
1789 return NULL;
1790 }
1791
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001792 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001794 return NULL;
1795 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001797 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 nargs = 0;
1799 nkeywords = 0;
1800 for (i = 0; i < NCH(n); i++) {
1801 node *ch = CHILD(n, i);
1802 if (TYPE(ch) == argument) {
1803 expr_ty e;
1804 if (NCH(ch) == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001805 if (nkeywords) {
1806 ast_error(CHILD(ch, 0),
1807 "non-keyword arg after keyword arg");
1808 return NULL;
1809 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 e = ast_for_expr(c, CHILD(ch, 0));
1811 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001812 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 asdl_seq_SET(args, nargs++, e);
1814 }
1815 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1816 e = ast_for_genexp(c, ch);
1817 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001818 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 asdl_seq_SET(args, nargs++, e);
1820 }
1821 else {
1822 keyword_ty kw;
1823 identifier key;
1824
1825 /* CHILD(ch, 0) is test, but must be an identifier? */
1826 e = ast_for_expr(c, CHILD(ch, 0));
1827 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001828 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 /* f(lambda x: x[0] = 3) ends up getting parsed with
1830 * LHS test = lambda x: x[0], and RHS test = 3.
1831 * SF bug 132313 points out that complaining about a keyword
1832 * then is very confusing.
1833 */
1834 if (e->kind == Lambda_kind) {
1835 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 } else if (e->kind != Name_kind) {
1838 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001839 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 }
1841 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 e = ast_for_expr(c, CHILD(ch, 2));
1843 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844 return NULL;
1845 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001847 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 asdl_seq_SET(keywords, nkeywords++, kw);
1849 }
1850 }
1851 else if (TYPE(ch) == STAR) {
1852 vararg = ast_for_expr(c, CHILD(n, i+1));
1853 i++;
1854 }
1855 else if (TYPE(ch) == DOUBLESTAR) {
1856 kwarg = ast_for_expr(c, CHILD(n, i+1));
1857 i++;
1858 }
1859 }
1860
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001861 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862}
1863
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001865ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001867 /* testlist_gexp: test (',' test)* [','] */
1868 /* testlist: test (',' test)* [','] */
1869 /* testlist_safe: test (',' test)+ [','] */
1870 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001872 if (TYPE(n) == testlist_gexp) {
1873 if (NCH(n) > 1)
1874 assert(TYPE(CHILD(n, 1)) != gen_for);
1875 }
1876 else {
1877 assert(TYPE(n) == testlist ||
1878 TYPE(n) == testlist_safe ||
1879 TYPE(n) == testlist1);
1880 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 if (NCH(n) == 1)
1882 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 else {
1884 asdl_seq *tmp = seq_for_testlist(c, n);
1885 if (!tmp)
1886 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001887 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001889}
1890
1891static expr_ty
1892ast_for_testlist_gexp(struct compiling *c, const node* n)
1893{
1894 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1895 /* argument: test [ gen_for ] */
1896 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001897 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001898 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001899 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001900}
1901
1902/* like ast_for_testlist() but returns a sequence */
1903static asdl_seq*
1904ast_for_class_bases(struct compiling *c, const node* n)
1905{
1906 /* testlist: test (',' test)* [','] */
1907 assert(NCH(n) > 0);
1908 REQ(n, testlist);
1909 if (NCH(n) == 1) {
1910 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001911 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001912 if (!bases)
1913 return NULL;
1914 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001915 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001916 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001917 asdl_seq_SET(bases, 0, base);
1918 return bases;
1919 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001920
1921 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922}
1923
1924static stmt_ty
1925ast_for_expr_stmt(struct compiling *c, const node *n)
1926{
1927 REQ(n, expr_stmt);
1928 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1929 | ('=' (yield_expr|testlist))*)
1930 testlist: test (',' test)* [',']
1931 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1932 | '<<=' | '>>=' | '**=' | '//='
1933 test: ... here starts the operator precendence dance
1934 */
1935
1936 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001937 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938 if (!e)
1939 return NULL;
1940
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001941 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 }
1943 else if (TYPE(CHILD(n, 1)) == augassign) {
1944 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001945 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 node *ch = CHILD(n, 0);
1947
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001948 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 if (!expr1)
1950 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001951 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001952 switch (expr1->kind) {
1953 case GeneratorExp_kind:
1954 ast_error(ch, "augmented assignment to generator "
1955 "expression not possible");
1956 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001957 case Yield_kind:
1958 ast_error(ch, "augmented assignment to yield "
1959 "expression not possible");
1960 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001961 case Name_kind: {
1962 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1963 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1964 ast_error(ch, "assignment to None");
1965 return NULL;
1966 }
1967 break;
1968 }
1969 case Attribute_kind:
1970 case Subscript_kind:
1971 break;
1972 default:
1973 ast_error(ch, "illegal expression for augmented "
1974 "assignment");
1975 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001977 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978
1979 ch = CHILD(n, 2);
1980 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001981 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 else
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001983 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001984 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 return NULL;
1986
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001987 newoperator = ast_for_augassign(CHILD(n, 1));
1988 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 return NULL;
1990
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001991 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 }
1993 else {
1994 int i;
1995 asdl_seq *targets;
1996 node *value;
1997 expr_ty expression;
1998
1999 /* a normal assignment */
2000 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002001 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 if (!targets)
2003 return NULL;
2004 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00002005 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006 node *ch = CHILD(n, i);
2007 if (TYPE(ch) == yield_expr) {
2008 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002011 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012
2013 /* set context to assign */
2014 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002015 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016
Neal Norwitz84456bd2005-12-18 03:16:20 +00002017 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019
2020 asdl_seq_SET(targets, i / 2, e);
2021 }
2022 value = CHILD(n, NCH(n) - 1);
2023 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002024 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 else
2026 expression = ast_for_expr(c, value);
2027 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002028 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002029 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031}
2032
2033static stmt_ty
2034ast_for_print_stmt(struct compiling *c, const node *n)
2035{
2036 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2037 | '>>' test [ (',' test)+ [','] ] )
2038 */
2039 expr_ty dest = NULL, expression;
2040 asdl_seq *seq;
2041 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002042 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043
2044 REQ(n, print_stmt);
2045 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2046 dest = ast_for_expr(c, CHILD(n, 2));
2047 if (!dest)
2048 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002049 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002051 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002053 return NULL;
2054 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002056 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002058 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 }
2060 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002061 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062}
2063
2064static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002065ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066{
2067 asdl_seq *seq;
2068 int i;
2069 expr_ty e;
2070
2071 REQ(n, exprlist);
2072
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002073 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 if (!seq)
2075 return NULL;
2076 for (i = 0; i < NCH(n); i += 2) {
2077 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002078 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002079 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002080 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002081 if (context && !set_context(e, context, CHILD(n, i)))
2082 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 }
2084 return seq;
2085}
2086
2087static stmt_ty
2088ast_for_del_stmt(struct compiling *c, const node *n)
2089{
2090 asdl_seq *expr_list;
2091
2092 /* del_stmt: 'del' exprlist */
2093 REQ(n, del_stmt);
2094
2095 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2096 if (!expr_list)
2097 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002098 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099}
2100
2101static stmt_ty
2102ast_for_flow_stmt(struct compiling *c, const node *n)
2103{
2104 /*
2105 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2106 | yield_stmt
2107 break_stmt: 'break'
2108 continue_stmt: 'continue'
2109 return_stmt: 'return' [testlist]
2110 yield_stmt: yield_expr
2111 yield_expr: 'yield' testlist
2112 raise_stmt: 'raise' [test [',' test [',' test]]]
2113 */
2114 node *ch;
2115
2116 REQ(n, flow_stmt);
2117 ch = CHILD(n, 0);
2118 switch (TYPE(ch)) {
2119 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002120 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002122 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 case yield_stmt: { /* will reduce to yield_expr */
2124 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2125 if (!exp)
2126 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002127 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 }
2129 case return_stmt:
2130 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002131 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002133 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 if (!expression)
2135 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002136 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 }
2138 case raise_stmt:
2139 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002140 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 else if (NCH(ch) == 2) {
2142 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2143 if (!expression)
2144 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002145 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 }
2147 else if (NCH(ch) == 4) {
2148 expr_ty expr1, expr2;
2149
2150 expr1 = ast_for_expr(c, CHILD(ch, 1));
2151 if (!expr1)
2152 return NULL;
2153 expr2 = ast_for_expr(c, CHILD(ch, 3));
2154 if (!expr2)
2155 return NULL;
2156
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002157 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 }
2159 else if (NCH(ch) == 6) {
2160 expr_ty expr1, expr2, expr3;
2161
2162 expr1 = ast_for_expr(c, CHILD(ch, 1));
2163 if (!expr1)
2164 return NULL;
2165 expr2 = ast_for_expr(c, CHILD(ch, 3));
2166 if (!expr2)
2167 return NULL;
2168 expr3 = ast_for_expr(c, CHILD(ch, 5));
2169 if (!expr3)
2170 return NULL;
2171
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002172 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 }
2174 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002175 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 "unexpected flow_stmt: %d", TYPE(ch));
2177 return NULL;
2178 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002179
2180 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2181 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182}
2183
2184static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002185alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186{
2187 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002188 import_as_name: NAME ['as' NAME]
2189 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 dotted_name: NAME ('.' NAME)*
2191 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002192 PyObject *str;
2193
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 loop:
2195 switch (TYPE(n)) {
2196 case import_as_name:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002197 str = NULL;
2198 if (NCH(n) == 3) {
2199 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2200 ast_error(n, "must use 'as' in import");
2201 return NULL;
2202 }
2203 str = NEW_IDENTIFIER(CHILD(n, 2));
2204 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002205 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 case dotted_as_name:
2207 if (NCH(n) == 1) {
2208 n = CHILD(n, 0);
2209 goto loop;
2210 }
2211 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002212 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002213 if (!a)
2214 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002215 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2216 ast_error(n, "must use 'as' in import");
2217 return NULL;
2218 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219 assert(!a->asname);
2220 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2221 return a;
2222 }
2223 break;
2224 case dotted_name:
2225 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002226 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 else {
2228 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002229 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002230 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231 char *s;
2232
2233 len = 0;
2234 for (i = 0; i < NCH(n); i += 2)
2235 /* length of string plus one for the dot */
2236 len += strlen(STR(CHILD(n, i))) + 1;
2237 len--; /* the last name doesn't have a dot */
2238 str = PyString_FromStringAndSize(NULL, len);
2239 if (!str)
2240 return NULL;
2241 s = PyString_AS_STRING(str);
2242 if (!s)
2243 return NULL;
2244 for (i = 0; i < NCH(n); i += 2) {
2245 char *sch = STR(CHILD(n, i));
2246 strcpy(s, STR(CHILD(n, i)));
2247 s += strlen(sch);
2248 *s++ = '.';
2249 }
2250 --s;
2251 *s = '\0';
2252 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002253 PyArena_AddPyObject(c->c_arena, str);
2254 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 }
2256 break;
2257 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002258 str = PyString_InternFromString("*");
2259 PyArena_AddPyObject(c->c_arena, str);
2260 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002262 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 "unexpected import name: %d", TYPE(n));
2264 return NULL;
2265 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002266
2267 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 return NULL;
2269}
2270
2271static stmt_ty
2272ast_for_import_stmt(struct compiling *c, const node *n)
2273{
2274 /*
2275 import_stmt: import_name | import_from
2276 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002277 import_from: 'from' ('.'* dotted_name | '.') 'import'
2278 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002280 int lineno;
2281 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 int i;
2283 asdl_seq *aliases;
2284
2285 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002286 lineno = LINENO(n);
2287 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002289 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002291 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002292 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 if (!aliases)
2294 return NULL;
2295 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002296 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002297 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 asdl_seq_SET(aliases, i / 2, import_alias);
2300 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002301 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002303 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002305 int idx, ndots = 0;
2306 alias_ty mod = NULL;
2307 identifier modname;
2308
2309 /* Count the number of dots (for relative imports) and check for the
2310 optional module name */
2311 for (idx = 1; idx < NCH(n); idx++) {
2312 if (TYPE(CHILD(n, idx)) == dotted_name) {
2313 mod = alias_for_import_name(c, CHILD(n, idx));
2314 idx++;
2315 break;
2316 } else if (TYPE(CHILD(n, idx)) != DOT) {
2317 break;
2318 }
2319 ndots++;
2320 }
2321 idx++; /* skip over the 'import' keyword */
2322 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002323 case STAR:
2324 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002325 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002326 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002327 if (ndots) {
2328 ast_error(n, "'import *' not allowed with 'from .'");
2329 return NULL;
2330 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002331 break;
2332 case LPAR:
2333 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002334 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002335 n_children = NCH(n);
2336 break;
2337 case import_as_names:
2338 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002339 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002340 n_children = NCH(n);
2341 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 ast_error(n, "trailing comma not allowed without"
2343 " surrounding parentheses");
2344 return NULL;
2345 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002346 break;
2347 default:
2348 ast_error(n, "Unexpected node-type in from-import");
2349 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002350 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002352 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002353 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355
2356 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002357 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002358 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002359 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002361 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002362 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002363 else {
2364 for (i = 0; i < NCH(n); i += 2) {
2365 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2366 if (!import_alias)
2367 return NULL;
2368 asdl_seq_SET(aliases, i / 2, import_alias);
2369 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002371 if (mod != NULL)
2372 modname = mod->name;
2373 else
2374 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002375 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002376 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 }
Neal Norwitz79792652005-11-14 04:25:03 +00002378 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 "unknown import statement: starts with command '%s'",
2380 STR(CHILD(n, 0)));
2381 return NULL;
2382}
2383
2384static stmt_ty
2385ast_for_global_stmt(struct compiling *c, const node *n)
2386{
2387 /* global_stmt: 'global' NAME (',' NAME)* */
2388 identifier name;
2389 asdl_seq *s;
2390 int i;
2391
2392 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002393 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 if (!s)
2395 return NULL;
2396 for (i = 1; i < NCH(n); i += 2) {
2397 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002398 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400 asdl_seq_SET(s, i / 2, name);
2401 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002402 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403}
2404
2405static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406ast_for_assert_stmt(struct compiling *c, const node *n)
2407{
2408 /* assert_stmt: 'assert' test [',' test] */
2409 REQ(n, assert_stmt);
2410 if (NCH(n) == 2) {
2411 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2412 if (!expression)
2413 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002414 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 }
2416 else if (NCH(n) == 4) {
2417 expr_ty expr1, expr2;
2418
2419 expr1 = ast_for_expr(c, CHILD(n, 1));
2420 if (!expr1)
2421 return NULL;
2422 expr2 = ast_for_expr(c, CHILD(n, 3));
2423 if (!expr2)
2424 return NULL;
2425
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002426 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 }
Neal Norwitz79792652005-11-14 04:25:03 +00002428 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429 "improper number of parts to 'assert' statement: %d",
2430 NCH(n));
2431 return NULL;
2432}
2433
2434static asdl_seq *
2435ast_for_suite(struct compiling *c, const node *n)
2436{
2437 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002438 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439 stmt_ty s;
2440 int i, total, num, end, pos = 0;
2441 node *ch;
2442
2443 REQ(n, suite);
2444
2445 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002446 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 if (!seq)
2448 return NULL;
2449 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2450 n = CHILD(n, 0);
2451 /* simple_stmt always ends with a NEWLINE,
2452 and may have a trailing SEMI
2453 */
2454 end = NCH(n) - 1;
2455 if (TYPE(CHILD(n, end - 1)) == SEMI)
2456 end--;
2457 /* loop by 2 to skip semi-colons */
2458 for (i = 0; i < end; i += 2) {
2459 ch = CHILD(n, i);
2460 s = ast_for_stmt(c, ch);
2461 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002462 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463 asdl_seq_SET(seq, pos++, s);
2464 }
2465 }
2466 else {
2467 for (i = 2; i < (NCH(n) - 1); i++) {
2468 ch = CHILD(n, i);
2469 REQ(ch, stmt);
2470 num = num_stmts(ch);
2471 if (num == 1) {
2472 /* small_stmt or compound_stmt with only one child */
2473 s = ast_for_stmt(c, ch);
2474 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002475 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 asdl_seq_SET(seq, pos++, s);
2477 }
2478 else {
2479 int j;
2480 ch = CHILD(ch, 0);
2481 REQ(ch, simple_stmt);
2482 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002483 /* statement terminates with a semi-colon ';' */
2484 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002485 assert((j + 1) == NCH(ch));
2486 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 s = ast_for_stmt(c, CHILD(ch, j));
2489 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 asdl_seq_SET(seq, pos++, s);
2492 }
2493 }
2494 }
2495 }
2496 assert(pos == seq->size);
2497 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498}
2499
2500static stmt_ty
2501ast_for_if_stmt(struct compiling *c, const node *n)
2502{
2503 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2504 ['else' ':' suite]
2505 */
2506 char *s;
2507
2508 REQ(n, if_stmt);
2509
2510 if (NCH(n) == 4) {
2511 expr_ty expression;
2512 asdl_seq *suite_seq;
2513
2514 expression = ast_for_expr(c, CHILD(n, 1));
2515 if (!expression)
2516 return NULL;
2517 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002518 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 return NULL;
2520
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002521 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 s = STR(CHILD(n, 4));
2525 /* s[2], the third character in the string, will be
2526 's' for el_s_e, or
2527 'i' for el_i_f
2528 */
2529 if (s[2] == 's') {
2530 expr_ty expression;
2531 asdl_seq *seq1, *seq2;
2532
2533 expression = ast_for_expr(c, CHILD(n, 1));
2534 if (!expression)
2535 return NULL;
2536 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002537 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 return NULL;
2539 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002540 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 return NULL;
2542
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002543 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 }
2545 else if (s[2] == 'i') {
2546 int i, n_elif, has_else = 0;
2547 asdl_seq *orelse = NULL;
2548 n_elif = NCH(n) - 4;
2549 /* must reference the child n_elif+1 since 'else' token is third,
2550 not fourth, child from the end. */
2551 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2552 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2553 has_else = 1;
2554 n_elif -= 3;
2555 }
2556 n_elif /= 4;
2557
2558 if (has_else) {
2559 expr_ty expression;
2560 asdl_seq *seq1, *seq2;
2561
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002562 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 if (!orelse)
2564 return NULL;
2565 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002566 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002569 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002572 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574
2575 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002576 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002577 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 /* the just-created orelse handled the last elif */
2579 n_elif--;
2580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581
2582 for (i = 0; i < n_elif; i++) {
2583 int off = 5 + (n_elif - i - 1) * 4;
2584 expr_ty expression;
2585 asdl_seq *suite_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002586 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2587 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 return NULL;
2589 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002590 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002593 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002596 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002598 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002599 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 }
2601 return If(ast_for_expr(c, CHILD(n, 1)),
2602 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002603 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002605
2606 PyErr_Format(PyExc_SystemError,
2607 "unexpected token in 'if' statement: %s", s);
2608 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609}
2610
2611static stmt_ty
2612ast_for_while_stmt(struct compiling *c, const node *n)
2613{
2614 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2615 REQ(n, while_stmt);
2616
2617 if (NCH(n) == 4) {
2618 expr_ty expression;
2619 asdl_seq *suite_seq;
2620
2621 expression = ast_for_expr(c, CHILD(n, 1));
2622 if (!expression)
2623 return NULL;
2624 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002625 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002627 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 }
2629 else if (NCH(n) == 7) {
2630 expr_ty expression;
2631 asdl_seq *seq1, *seq2;
2632
2633 expression = ast_for_expr(c, CHILD(n, 1));
2634 if (!expression)
2635 return NULL;
2636 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002637 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 return NULL;
2639 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002640 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 return NULL;
2642
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002643 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002645
2646 PyErr_Format(PyExc_SystemError,
2647 "wrong number of tokens for 'while' statement: %d",
2648 NCH(n));
2649 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650}
2651
2652static stmt_ty
2653ast_for_for_stmt(struct compiling *c, const node *n)
2654{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002655 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 expr_ty expression;
2657 expr_ty target;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002658 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2660 REQ(n, for_stmt);
2661
2662 if (NCH(n) == 9) {
2663 seq = ast_for_suite(c, CHILD(n, 8));
2664 if (!seq)
2665 return NULL;
2666 }
2667
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002668 node_target = CHILD(n, 1);
2669 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002670 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002672 /* Check the # of children rather than the length of _target, since
2673 for x, in ... has 1 element in _target, but still requires a Tuple. */
2674 if (NCH(node_target) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002675 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002677 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002679 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002680 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681 return NULL;
2682 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002683 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684 return NULL;
2685
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002686 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2687 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688}
2689
2690static excepthandler_ty
2691ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2692{
2693 /* except_clause: 'except' [test [',' test]] */
2694 REQ(exc, except_clause);
2695 REQ(body, suite);
2696
2697 if (NCH(exc) == 1) {
2698 asdl_seq *suite_seq = ast_for_suite(c, body);
2699 if (!suite_seq)
2700 return NULL;
2701
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002702 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2703 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 }
2705 else if (NCH(exc) == 2) {
2706 expr_ty expression;
2707 asdl_seq *suite_seq;
2708
2709 expression = ast_for_expr(c, CHILD(exc, 1));
2710 if (!expression)
2711 return NULL;
2712 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002713 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 return NULL;
2715
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002716 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2717 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 }
2719 else if (NCH(exc) == 4) {
2720 asdl_seq *suite_seq;
2721 expr_ty expression;
2722 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2723 if (!e)
2724 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002725 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 return NULL;
2727 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002728 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 return NULL;
2730 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002731 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732 return NULL;
2733
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002734 return excepthandler(expression, e, suite_seq, LINENO(exc),
2735 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002737
2738 PyErr_Format(PyExc_SystemError,
2739 "wrong number of children for 'except' clause: %d",
2740 NCH(exc));
2741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742}
2743
2744static stmt_ty
2745ast_for_try_stmt(struct compiling *c, const node *n)
2746{
Neal Norwitzf599f422005-12-17 21:33:47 +00002747 const int nch = NCH(n);
2748 int n_except = (nch - 3)/3;
2749 asdl_seq *body, *orelse = NULL, *finally = NULL;
2750
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 REQ(n, try_stmt);
2752
Neal Norwitzf599f422005-12-17 21:33:47 +00002753 body = ast_for_suite(c, CHILD(n, 2));
2754 if (body == NULL)
2755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756
Neal Norwitzf599f422005-12-17 21:33:47 +00002757 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2758 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2759 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2760 /* we can assume it's an "else",
2761 because nch >= 9 for try-else-finally and
2762 it would otherwise have a type of except_clause */
2763 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2764 if (orelse == NULL)
2765 return NULL;
2766 n_except--;
2767 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768
Neal Norwitzf599f422005-12-17 21:33:47 +00002769 finally = ast_for_suite(c, CHILD(n, nch - 1));
2770 if (finally == NULL)
2771 return NULL;
2772 n_except--;
2773 }
2774 else {
2775 /* we can assume it's an "else",
2776 otherwise it would have a type of except_clause */
2777 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2778 if (orelse == NULL)
2779 return NULL;
2780 n_except--;
2781 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002783 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002784 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 return NULL;
2786 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002787
2788 if (n_except > 0) {
2789 int i;
2790 stmt_ty except_st;
2791 /* process except statements to create a try ... except */
2792 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2793 if (handlers == NULL)
2794 return NULL;
2795
2796 for (i = 0; i < n_except; i++) {
2797 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2798 CHILD(n, 5 + i * 3));
2799 if (!e)
2800 return NULL;
2801 asdl_seq_SET(handlers, i, e);
2802 }
2803
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002804 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2805 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002806 if (!finally)
2807 return except_st;
2808
2809 /* if a 'finally' is present too, we nest the TryExcept within a
2810 TryFinally to emulate try ... except ... finally */
2811 body = asdl_seq_new(1, c->c_arena);
2812 if (body == NULL)
2813 return NULL;
2814 asdl_seq_SET(body, 0, except_st);
2815 }
2816
2817 /* must be a try ... finally (except clauses are in body, if any exist) */
2818 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002819 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820}
2821
Guido van Rossumc2e20742006-02-27 22:32:47 +00002822static expr_ty
2823ast_for_with_var(struct compiling *c, const node *n)
2824{
2825 REQ(n, with_var);
2826 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2827 ast_error(n, "expected \"with [expr] as [var]\"");
2828 return NULL;
2829 }
2830 return ast_for_expr(c, CHILD(n, 1));
2831}
2832
2833/* with_stmt: 'with' test [ with_var ] ':' suite */
2834static stmt_ty
2835ast_for_with_stmt(struct compiling *c, const node *n)
2836{
2837 expr_ty context_expr, optional_vars = NULL;
2838 int suite_index = 3; /* skip 'with', test, and ':' */
2839 asdl_seq *suite_seq;
2840
2841 assert(TYPE(n) == with_stmt);
2842 context_expr = ast_for_expr(c, CHILD(n, 1));
2843 if (TYPE(CHILD(n, 2)) == with_var) {
2844 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2845
2846 if (!optional_vars) {
2847 return NULL;
2848 }
2849 if (!set_context(optional_vars, Store, n)) {
2850 return NULL;
2851 }
2852 suite_index = 4;
2853 }
2854
2855 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2856 if (!suite_seq) {
2857 return NULL;
2858 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002859 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2860 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002861}
2862
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863static stmt_ty
2864ast_for_classdef(struct compiling *c, const node *n)
2865{
2866 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 asdl_seq *bases, *s;
2868
2869 REQ(n, classdef);
2870
2871 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2872 ast_error(n, "assignment to None");
2873 return NULL;
2874 }
2875
2876 if (NCH(n) == 4) {
2877 s = ast_for_suite(c, CHILD(n, 3));
2878 if (!s)
2879 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002880 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2881 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 }
2883 /* check for empty base list */
2884 if (TYPE(CHILD(n,3)) == RPAR) {
2885 s = ast_for_suite(c, CHILD(n,5));
2886 if (!s)
2887 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002888 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2889 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 }
2891
2892 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002893 bases = ast_for_class_bases(c, CHILD(n, 3));
2894 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896
2897 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002898 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002900 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2901 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902}
2903
2904static stmt_ty
2905ast_for_stmt(struct compiling *c, const node *n)
2906{
2907 if (TYPE(n) == stmt) {
2908 assert(NCH(n) == 1);
2909 n = CHILD(n, 0);
2910 }
2911 if (TYPE(n) == simple_stmt) {
2912 assert(num_stmts(n) == 1);
2913 n = CHILD(n, 0);
2914 }
2915 if (TYPE(n) == small_stmt) {
2916 REQ(n, small_stmt);
2917 n = CHILD(n, 0);
2918 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002919 | flow_stmt | import_stmt | global_stmt | assert_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 */
2921 switch (TYPE(n)) {
2922 case expr_stmt:
2923 return ast_for_expr_stmt(c, n);
2924 case print_stmt:
2925 return ast_for_print_stmt(c, n);
2926 case del_stmt:
2927 return ast_for_del_stmt(c, n);
2928 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002929 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930 case flow_stmt:
2931 return ast_for_flow_stmt(c, n);
2932 case import_stmt:
2933 return ast_for_import_stmt(c, n);
2934 case global_stmt:
2935 return ast_for_global_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 case assert_stmt:
2937 return ast_for_assert_stmt(c, n);
2938 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002939 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2941 TYPE(n), NCH(n));
2942 return NULL;
2943 }
2944 }
2945 else {
2946 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2947 | funcdef | classdef
2948 */
2949 node *ch = CHILD(n, 0);
2950 REQ(n, compound_stmt);
2951 switch (TYPE(ch)) {
2952 case if_stmt:
2953 return ast_for_if_stmt(c, ch);
2954 case while_stmt:
2955 return ast_for_while_stmt(c, ch);
2956 case for_stmt:
2957 return ast_for_for_stmt(c, ch);
2958 case try_stmt:
2959 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002960 case with_stmt:
2961 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 case funcdef:
2963 return ast_for_funcdef(c, ch);
2964 case classdef:
2965 return ast_for_classdef(c, ch);
2966 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002967 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2969 TYPE(n), NCH(n));
2970 return NULL;
2971 }
2972 }
2973}
2974
2975static PyObject *
2976parsenumber(const char *s)
2977{
2978 const char *end;
2979 long x;
2980 double dx;
2981#ifndef WITHOUT_COMPLEX
2982 Py_complex c;
2983 int imflag;
2984#endif
2985
2986 errno = 0;
2987 end = s + strlen(s) - 1;
2988#ifndef WITHOUT_COMPLEX
2989 imflag = *end == 'j' || *end == 'J';
2990#endif
2991 if (*end == 'l' || *end == 'L')
2992 return PyLong_FromString((char *)s, (char **)0, 0);
2993 if (s[0] == '0') {
2994 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2995 if (x < 0 && errno == 0) {
2996 return PyLong_FromString((char *)s,
2997 (char **)0,
2998 0);
2999 }
3000 }
3001 else
3002 x = PyOS_strtol((char *)s, (char **)&end, 0);
3003 if (*end == '\0') {
3004 if (errno != 0)
3005 return PyLong_FromString((char *)s, (char **)0, 0);
3006 return PyInt_FromLong(x);
3007 }
3008 /* XXX Huge floats may silently fail */
3009#ifndef WITHOUT_COMPLEX
3010 if (imflag) {
3011 c.real = 0.;
3012 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003013 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 PyFPE_END_PROTECT(c)
3015 return PyComplex_FromCComplex(c);
3016 }
3017 else
3018#endif
3019 {
3020 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003021 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 PyFPE_END_PROTECT(dx)
3023 return PyFloat_FromDouble(dx);
3024 }
3025}
3026
3027static PyObject *
3028decode_utf8(const char **sPtr, const char *end, char* encoding)
3029{
3030#ifndef Py_USING_UNICODE
3031 Py_FatalError("decode_utf8 should not be called in this build.");
3032 return NULL;
3033#else
3034 PyObject *u, *v;
3035 char *s, *t;
3036 t = s = (char *)*sPtr;
3037 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3038 while (s < end && (*s & 0x80)) s++;
3039 *sPtr = s;
3040 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3041 if (u == NULL)
3042 return NULL;
3043 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3044 Py_DECREF(u);
3045 return v;
3046#endif
3047}
3048
3049static PyObject *
3050decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3051{
3052 PyObject *v, *u;
3053 char *buf;
3054 char *p;
3055 const char *end;
3056 if (encoding == NULL) {
3057 buf = (char *)s;
3058 u = NULL;
3059 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3060 buf = (char *)s;
3061 u = NULL;
3062 } else {
3063 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3064 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3065 if (u == NULL)
3066 return NULL;
3067 p = buf = PyString_AsString(u);
3068 end = s + len;
3069 while (s < end) {
3070 if (*s == '\\') {
3071 *p++ = *s++;
3072 if (*s & 0x80) {
3073 strcpy(p, "u005c");
3074 p += 5;
3075 }
3076 }
3077 if (*s & 0x80) { /* XXX inefficient */
3078 PyObject *w;
3079 char *r;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003080 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081 w = decode_utf8(&s, end, "utf-16-be");
3082 if (w == NULL) {
3083 Py_DECREF(u);
3084 return NULL;
3085 }
3086 r = PyString_AsString(w);
3087 rn = PyString_Size(w);
3088 assert(rn % 2 == 0);
3089 for (i = 0; i < rn; i += 2) {
3090 sprintf(p, "\\u%02x%02x",
3091 r[i + 0] & 0xFF,
3092 r[i + 1] & 0xFF);
3093 p += 6;
3094 }
3095 Py_DECREF(w);
3096 } else {
3097 *p++ = *s++;
3098 }
3099 }
3100 len = p - buf;
3101 s = buf;
3102 }
3103 if (rawmode)
3104 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3105 else
3106 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3107 Py_XDECREF(u);
3108 return v;
3109}
3110
3111/* s is a Python string literal, including the bracketing quote characters,
3112 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3113 * parsestr parses it, and returns the decoded Python string object.
3114 */
3115static PyObject *
3116parsestr(const char *s, const char *encoding)
3117{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003119 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 int rawmode = 0;
3121 int need_encoding;
3122 int unicode = 0;
3123
3124 if (isalpha(quote) || quote == '_') {
3125 if (quote == 'u' || quote == 'U') {
3126 quote = *++s;
3127 unicode = 1;
3128 }
3129 if (quote == 'r' || quote == 'R') {
3130 quote = *++s;
3131 rawmode = 1;
3132 }
3133 }
3134 if (quote != '\'' && quote != '\"') {
3135 PyErr_BadInternalCall();
3136 return NULL;
3137 }
3138 s++;
3139 len = strlen(s);
3140 if (len > INT_MAX) {
3141 PyErr_SetString(PyExc_OverflowError,
3142 "string to parse is too long");
3143 return NULL;
3144 }
3145 if (s[--len] != quote) {
3146 PyErr_BadInternalCall();
3147 return NULL;
3148 }
3149 if (len >= 4 && s[0] == quote && s[1] == quote) {
3150 s += 2;
3151 len -= 2;
3152 if (s[--len] != quote || s[--len] != quote) {
3153 PyErr_BadInternalCall();
3154 return NULL;
3155 }
3156 }
3157#ifdef Py_USING_UNICODE
3158 if (unicode || Py_UnicodeFlag) {
3159 return decode_unicode(s, len, rawmode, encoding);
3160 }
3161#endif
3162 need_encoding = (encoding != NULL &&
3163 strcmp(encoding, "utf-8") != 0 &&
3164 strcmp(encoding, "iso-8859-1") != 0);
3165 if (rawmode || strchr(s, '\\') == NULL) {
3166 if (need_encoding) {
3167#ifndef Py_USING_UNICODE
3168 /* This should not happen - we never see any other
3169 encoding. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003170 Py_FatalError(
3171 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003173 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174 if (u == NULL)
3175 return NULL;
3176 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3177 Py_DECREF(u);
3178 return v;
3179#endif
3180 } else {
3181 return PyString_FromStringAndSize(s, len);
3182 }
3183 }
3184
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003185 return PyString_DecodeEscape(s, len, NULL, unicode,
3186 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187}
3188
3189/* Build a Python string object out of a STRING atom. This takes care of
3190 * compile-time literal catenation, calling parsestr() on each piece, and
3191 * pasting the intermediate results together.
3192 */
3193static PyObject *
3194parsestrplus(struct compiling *c, const node *n)
3195{
3196 PyObject *v;
3197 int i;
3198 REQ(CHILD(n, 0), STRING);
3199 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3200 /* String literal concatenation */
3201 for (i = 1; i < NCH(n); i++) {
3202 PyObject *s;
3203 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3204 if (s == NULL)
3205 goto onError;
3206 if (PyString_Check(v) && PyString_Check(s)) {
3207 PyString_ConcatAndDel(&v, s);
3208 if (v == NULL)
3209 goto onError;
3210 }
3211#ifdef Py_USING_UNICODE
3212 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003213 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215 Py_DECREF(v);
3216 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003217 if (v == NULL)
3218 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219 }
3220#endif
3221 }
3222 }
3223 return v;
3224
3225 onError:
3226 Py_XDECREF(v);
3227 return NULL;
3228}