blob: 36f706ec62cd3f9696665dd041abb948a047f186 [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;
402 case Compare_kind:
403 expr_name = "comparison";
404 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000405 case IfExp_kind:
406 expr_name = "conditional expression";
407 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000408 default:
409 PyErr_Format(PyExc_SystemError,
410 "unexpected expression in assignment %d (line %d)",
411 e->kind, e->lineno);
412 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000413 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000414 /* Check for error string set by switch */
415 if (expr_name) {
416 char buf[300];
417 PyOS_snprintf(buf, sizeof(buf),
418 "can't %s %s",
419 ctx == Store ? "assign to" : "delete",
420 expr_name);
421 return ast_error(n, buf);
422 }
423
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000424 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000425 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426 */
427 if (s) {
428 int i;
429
430 for (i = 0; i < asdl_seq_LEN(s); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 return 0;
433 }
434 }
435 return 1;
436}
437
438static operator_ty
439ast_for_augassign(const node *n)
440{
441 REQ(n, augassign);
442 n = CHILD(n, 0);
443 switch (STR(n)[0]) {
444 case '+':
445 return Add;
446 case '-':
447 return Sub;
448 case '/':
449 if (STR(n)[1] == '/')
450 return FloorDiv;
451 else
452 return Div;
453 case '%':
454 return Mod;
455 case '<':
456 return LShift;
457 case '>':
458 return RShift;
459 case '&':
460 return BitAnd;
461 case '^':
462 return BitXor;
463 case '|':
464 return BitOr;
465 case '*':
466 if (STR(n)[1] == '*')
467 return Pow;
468 else
469 return Mult;
470 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000471 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000472 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473 }
474}
475
476static cmpop_ty
477ast_for_comp_op(const node *n)
478{
Guido van Rossumb053cd82006-08-24 03:53:23 +0000479 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480 |'is' 'not'
481 */
482 REQ(n, comp_op);
483 if (NCH(n) == 1) {
484 n = CHILD(n, 0);
485 switch (TYPE(n)) {
486 case LESS:
487 return Lt;
488 case GREATER:
489 return Gt;
490 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491 return Eq;
492 case LESSEQUAL:
493 return LtE;
494 case GREATEREQUAL:
495 return GtE;
496 case NOTEQUAL:
497 return NotEq;
498 case NAME:
499 if (strcmp(STR(n), "in") == 0)
500 return In;
501 if (strcmp(STR(n), "is") == 0)
502 return Is;
503 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000504 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000506 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507 }
508 }
509 else if (NCH(n) == 2) {
510 /* handle "not in" and "is not" */
511 switch (TYPE(CHILD(n, 0))) {
512 case NAME:
513 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
514 return NotIn;
515 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
516 return IsNot;
517 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000518 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 }
522 }
Neal Norwitz79792652005-11-14 04:25:03 +0000523 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000525 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526}
527
528static asdl_seq *
529seq_for_testlist(struct compiling *c, const node *n)
530{
531 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000532 asdl_seq *seq;
533 expr_ty expression;
534 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535 assert(TYPE(n) == testlist
536 || TYPE(n) == listmaker
537 || TYPE(n) == testlist_gexp
538 || TYPE(n) == testlist_safe
539 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000541 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542 if (!seq)
543 return NULL;
544
545 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000546 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
548 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000549 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551
552 assert(i / 2 < seq->size);
553 asdl_seq_SET(seq, i / 2, expression);
554 }
555 return seq;
556}
557
558static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000559compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560{
561 int i, len = (NCH(n) + 1) / 2;
562 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000563 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 if (!args)
565 return NULL;
566
567 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 for (i = 0; i < len; i++) {
569 const node *child = CHILD(CHILD(n, 2*i), 0);
570 expr_ty arg;
571 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000572 if (!strcmp(STR(child), "None")) {
573 ast_error(child, "assignment to None");
574 return NULL;
575 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000576 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
577 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000578 }
579 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000580 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000581 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582 asdl_seq_SET(args, i, arg);
583 }
584
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000585 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000586 if (!set_context(result, Store, n))
587 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588 return result;
589}
590
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
Jeremy Hyltona8293132006-02-28 17:58:27 +0000592/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593
594static arguments_ty
595ast_for_arguments(struct compiling *c, const node *n)
596{
597 /* parameters: '(' [varargslist] ')'
598 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
599 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
600 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000601 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 asdl_seq *args, *defaults;
603 identifier vararg = NULL, kwarg = NULL;
604 node *ch;
605
606 if (TYPE(n) == parameters) {
607 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000608 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 n = CHILD(n, 1);
610 }
611 REQ(n, varargslist);
612
613 /* first count the number of normal args & defaults */
614 for (i = 0; i < NCH(n); i++) {
615 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000616 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 if (TYPE(ch) == EQUAL)
619 n_defaults++;
620 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000621 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 if (!args && n_args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000623 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000624 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 if (!defaults && n_defaults)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627
628 /* fpdef: NAME | '(' fplist ')'
629 fplist: fpdef (',' fpdef)* [',']
630 */
631 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000632 j = 0; /* index for defaults */
633 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 while (i < NCH(n)) {
635 ch = CHILD(n, i);
636 switch (TYPE(ch)) {
637 case fpdef:
638 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
639 anything other than EQUAL or a comma? */
640 /* XXX Should NCH(n) check be made a separate check? */
641 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
643 if (!expression)
644 goto error;
645 assert(defaults != NULL);
646 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 i += 2;
648 found_default = 1;
649 }
650 else if (found_default) {
651 ast_error(n,
652 "non-default argument follows default argument");
653 goto error;
654 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 if (NCH(ch) == 3) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000656 ch = CHILD(ch, 1);
657 /* def foo((x)): is not complex, special case. */
658 if (NCH(ch) != 1) {
659 /* We have complex arguments, setup for unpacking. */
660 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
661 } else {
662 /* def foo((x)): setup for checking NAME below. */
663 ch = CHILD(ch, 0);
664 }
665 }
666 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000667 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
669 ast_error(CHILD(ch, 0), "assignment to None");
670 goto error;
671 }
Armin Rigo31441302005-10-21 12:57:31 +0000672 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000673 Param, LINENO(ch), ch->n_col_offset,
674 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 if (!name)
676 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000677 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678
679 }
680 i += 2; /* the name and the comma */
681 break;
682 case STAR:
683 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
684 ast_error(CHILD(n, i+1), "assignment to None");
685 goto error;
686 }
687 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
688 i += 3;
689 break;
690 case DOUBLESTAR:
691 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
692 ast_error(CHILD(n, i+1), "assignment to None");
693 goto error;
694 }
695 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
696 i += 3;
697 break;
698 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000699 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 "unexpected node in varargslist: %d @ %d",
701 TYPE(ch), i);
702 goto error;
703 }
704 }
705
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000706 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707
708 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000709 Py_XDECREF(vararg);
710 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 return NULL;
712}
713
714static expr_ty
715ast_for_dotted_name(struct compiling *c, const node *n)
716{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000717 expr_ty e;
718 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000719 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 int i;
721
722 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000723
724 lineno = LINENO(n);
725 col_offset = n->n_col_offset;
726
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 id = NEW_IDENTIFIER(CHILD(n, 0));
728 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000729 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000730 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000732 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733
734 for (i = 2; i < NCH(n); i+=2) {
735 id = NEW_IDENTIFIER(CHILD(n, i));
736 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000737 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000738 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000739 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000740 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741 }
742
743 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744}
745
746static expr_ty
747ast_for_decorator(struct compiling *c, const node *n)
748{
749 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
750 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000751 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752
753 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000754 REQ(CHILD(n, 0), AT);
755 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756
757 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
758 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000759 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760
761 if (NCH(n) == 3) { /* No arguments */
762 d = name_expr;
763 name_expr = NULL;
764 }
765 else if (NCH(n) == 5) { /* Call with no arguments */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
767 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000769 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 name_expr = NULL;
771 }
772 else {
773 d = ast_for_call(c, CHILD(n, 3), name_expr);
774 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000775 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 name_expr = NULL;
777 }
778
779 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780}
781
782static asdl_seq*
783ast_for_decorators(struct compiling *c, const node *n)
784{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000785 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000786 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 int i;
788
789 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000790 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 if (!decorator_seq)
792 return NULL;
793
794 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000795 d = ast_for_decorator(c, CHILD(n, i));
796 if (!d)
797 return NULL;
798 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 }
800 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801}
802
803static stmt_ty
804ast_for_funcdef(struct compiling *c, const node *n)
805{
806 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000807 identifier name;
808 arguments_ty args;
809 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 asdl_seq *decorator_seq = NULL;
811 int name_i;
812
813 REQ(n, funcdef);
814
815 if (NCH(n) == 6) { /* decorators are present */
816 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
817 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000818 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 name_i = 2;
820 }
821 else {
822 name_i = 1;
823 }
824
825 name = NEW_IDENTIFIER(CHILD(n, name_i));
826 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000829 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 }
832 args = ast_for_arguments(c, CHILD(n, name_i + 1));
833 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 body = ast_for_suite(c, CHILD(n, name_i + 3));
836 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000837 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
840 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841}
842
843static expr_ty
844ast_for_lambdef(struct compiling *c, const node *n)
845{
846 /* lambdef: 'lambda' [varargslist] ':' test */
847 arguments_ty args;
848 expr_ty expression;
849
850 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000851 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 if (!args)
853 return NULL;
854 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000855 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 }
858 else {
859 args = ast_for_arguments(c, CHILD(n, 1));
860 if (!args)
861 return NULL;
862 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000863 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 }
866
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000867 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868}
869
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000870static expr_ty
871ast_for_ifexpr(struct compiling *c, const node *n)
872{
873 /* test: or_test 'if' or_test 'else' test */
874 expr_ty expression, body, orelse;
875
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000876 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000877 body = ast_for_expr(c, CHILD(n, 0));
878 if (!body)
879 return NULL;
880 expression = ast_for_expr(c, CHILD(n, 2));
881 if (!expression)
882 return NULL;
883 orelse = ast_for_expr(c, CHILD(n, 4));
884 if (!orelse)
885 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000886 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
887 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000888}
889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890/* Count the number of 'for' loop in a list comprehension.
891
892 Helper for ast_for_listcomp().
893*/
894
895static int
896count_list_fors(const node *n)
897{
898 int n_fors = 0;
899 node *ch = CHILD(n, 1);
900
901 count_list_for:
902 n_fors++;
903 REQ(ch, list_for);
904 if (NCH(ch) == 5)
905 ch = CHILD(ch, 4);
906 else
907 return n_fors;
908 count_list_iter:
909 REQ(ch, list_iter);
910 ch = CHILD(ch, 0);
911 if (TYPE(ch) == list_for)
912 goto count_list_for;
913 else if (TYPE(ch) == list_if) {
914 if (NCH(ch) == 3) {
915 ch = CHILD(ch, 2);
916 goto count_list_iter;
917 }
918 else
919 return n_fors;
920 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000921
922 /* Should never be reached */
923 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
924 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925}
926
927/* Count the number of 'if' statements in a list comprehension.
928
929 Helper for ast_for_listcomp().
930*/
931
932static int
933count_list_ifs(const node *n)
934{
935 int n_ifs = 0;
936
937 count_list_iter:
938 REQ(n, list_iter);
939 if (TYPE(CHILD(n, 0)) == list_for)
940 return n_ifs;
941 n = CHILD(n, 0);
942 REQ(n, list_if);
943 n_ifs++;
944 if (NCH(n) == 2)
945 return n_ifs;
946 n = CHILD(n, 2);
947 goto count_list_iter;
948}
949
950static expr_ty
951ast_for_listcomp(struct compiling *c, const node *n)
952{
953 /* listmaker: test ( list_for | (',' test)* [','] )
954 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
955 list_iter: list_for | list_if
956 list_if: 'if' test [list_iter]
957 testlist_safe: test [(',' test)+ [',']]
958 */
959 expr_ty elt;
960 asdl_seq *listcomps;
961 int i, n_fors;
962 node *ch;
963
964 REQ(n, listmaker);
965 assert(NCH(n) > 1);
966
967 elt = ast_for_expr(c, CHILD(n, 0));
968 if (!elt)
969 return NULL;
970
971 n_fors = count_list_fors(n);
972 if (n_fors == -1)
973 return NULL;
974
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000975 listcomps = asdl_seq_new(n_fors, c->c_arena);
976 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000978
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 ch = CHILD(n, 1);
980 for (i = 0; i < n_fors; i++) {
981 comprehension_ty lc;
982 asdl_seq *t;
983 expr_ty expression;
984
985 REQ(ch, list_for);
986
987 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000988 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000990 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000991 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000994 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000995 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000996 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
999 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001000 expression, NULL, c->c_arena);
1001 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003
1004 if (NCH(ch) == 5) {
1005 int j, n_ifs;
1006 asdl_seq *ifs;
1007
1008 ch = CHILD(ch, 4);
1009 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001010 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001013 ifs = asdl_seq_new(n_ifs, c->c_arena);
1014 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016
1017 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001018 REQ(ch, list_iter);
1019 ch = CHILD(ch, 0);
1020 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021
Jeremy Hyltona8293132006-02-28 17:58:27 +00001022 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1023 if (NCH(ch) == 3)
1024 ch = CHILD(ch, 2);
1025 }
1026 /* on exit, must guarantee that ch is a list_for */
1027 if (TYPE(ch) == list_iter)
1028 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001030 }
1031 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 }
1033
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001034 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035}
1036
1037/*
1038 Count the number of 'for' loops in a generator expression.
1039
1040 Helper for ast_for_genexp().
1041*/
1042
1043static int
1044count_gen_fors(const node *n)
1045{
1046 int n_fors = 0;
1047 node *ch = CHILD(n, 1);
1048
1049 count_gen_for:
1050 n_fors++;
1051 REQ(ch, gen_for);
1052 if (NCH(ch) == 5)
1053 ch = CHILD(ch, 4);
1054 else
1055 return n_fors;
1056 count_gen_iter:
1057 REQ(ch, gen_iter);
1058 ch = CHILD(ch, 0);
1059 if (TYPE(ch) == gen_for)
1060 goto count_gen_for;
1061 else if (TYPE(ch) == gen_if) {
1062 if (NCH(ch) == 3) {
1063 ch = CHILD(ch, 2);
1064 goto count_gen_iter;
1065 }
1066 else
1067 return n_fors;
1068 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001069
1070 /* Should never be reached */
1071 PyErr_SetString(PyExc_SystemError,
1072 "logic error in count_gen_fors");
1073 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074}
1075
1076/* Count the number of 'if' statements in a generator expression.
1077
1078 Helper for ast_for_genexp().
1079*/
1080
1081static int
1082count_gen_ifs(const node *n)
1083{
1084 int n_ifs = 0;
1085
1086 while (1) {
1087 REQ(n, gen_iter);
1088 if (TYPE(CHILD(n, 0)) == gen_for)
1089 return n_ifs;
1090 n = CHILD(n, 0);
1091 REQ(n, gen_if);
1092 n_ifs++;
1093 if (NCH(n) == 2)
1094 return n_ifs;
1095 n = CHILD(n, 2);
1096 }
1097}
1098
Jeremy Hyltona8293132006-02-28 17:58:27 +00001099/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100static expr_ty
1101ast_for_genexp(struct compiling *c, const node *n)
1102{
1103 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1104 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1105 expr_ty elt;
1106 asdl_seq *genexps;
1107 int i, n_fors;
1108 node *ch;
1109
1110 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1111 assert(NCH(n) > 1);
1112
1113 elt = ast_for_expr(c, CHILD(n, 0));
1114 if (!elt)
1115 return NULL;
1116
1117 n_fors = count_gen_fors(n);
1118 if (n_fors == -1)
1119 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001120
1121 genexps = asdl_seq_new(n_fors, c->c_arena);
1122 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 ch = CHILD(n, 1);
1126 for (i = 0; i < n_fors; i++) {
1127 comprehension_ty ge;
1128 asdl_seq *t;
1129 expr_ty expression;
1130
1131 REQ(ch, gen_for);
1132
1133 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001134 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001136 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001137 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001139
1140 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001141 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001142 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1145 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001146 expression, NULL, c->c_arena);
1147
1148 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001150
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 if (NCH(ch) == 5) {
1152 int j, n_ifs;
1153 asdl_seq *ifs;
1154
1155 ch = CHILD(ch, 4);
1156 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001157 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159
1160 ifs = asdl_seq_new(n_ifs, c->c_arena);
1161 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 for (j = 0; j < n_ifs; j++) {
1165 REQ(ch, gen_iter);
1166 ch = CHILD(ch, 0);
1167 REQ(ch, gen_if);
1168
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001169 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001170 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001171 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001172 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 if (NCH(ch) == 3)
1174 ch = CHILD(ch, 2);
1175 }
1176 /* on exit, must guarantee that ch is a gen_for */
1177 if (TYPE(ch) == gen_iter)
1178 ch = CHILD(ch, 0);
1179 ge->ifs = ifs;
1180 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001181 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 }
1183
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001184 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185}
1186
1187static expr_ty
1188ast_for_atom(struct compiling *c, const node *n)
1189{
1190 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
Guido van Rossum86e58e22006-08-28 15:27:34 +00001191 | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING+
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 */
1193 node *ch = CHILD(n, 0);
1194
1195 switch (TYPE(ch)) {
1196 case NAME:
1197 /* All names start in Load context, but may later be
1198 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001199 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 case STRING: {
1201 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 if (!str)
1203 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204
1205 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001206 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 }
1208 case NUMBER: {
1209 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 if (!pynum)
1211 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001212
1213 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001214 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 }
1216 case LPAR: /* some parenthesized expressions */
1217 ch = CHILD(n, 1);
1218
1219 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001220 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221
1222 if (TYPE(ch) == yield_expr)
1223 return ast_for_expr(c, ch);
1224
1225 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1226 return ast_for_genexp(c, ch);
1227
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001228 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 case LSQB: /* list (or list comprehension) */
1230 ch = CHILD(n, 1);
1231
1232 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001233 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234
1235 REQ(ch, listmaker);
1236 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1237 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 if (!elts)
1239 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001240
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001241 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 }
1243 else
1244 return ast_for_listcomp(c, ch);
1245 case LBRACE: {
Guido van Rossum86e58e22006-08-28 15:27:34 +00001246 /* dictsetmaker: test ':' test (',' test ':' test)* [','] |
1247 * test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 int i, size;
1249 asdl_seq *keys, *values;
1250
1251 ch = CHILD(n, 1);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001252 if (NCH(ch) == 1 || (NCH(ch) > 0 && STR(CHILD(ch, 1))[0] == ',')) {
1253 /* it's a set */
1254 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
1255 keys = asdl_seq_new(size, c->c_arena);
1256 if (!keys)
1257 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001258
Guido van Rossum86e58e22006-08-28 15:27:34 +00001259 for (i = 0; i < NCH(ch); i += 2) {
1260 expr_ty expression;
1261 expression = ast_for_expr(c, CHILD(ch, i));
1262 if (!expression)
1263 return NULL;
1264 asdl_seq_SET(keys, i / 2, expression);
1265 }
1266 return Set(keys, LINENO(n), n->n_col_offset, c->c_arena);
1267 } else {
1268 /* it's a dict */
1269 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1270 keys = asdl_seq_new(size, c->c_arena);
1271 if (!keys)
1272 return NULL;
1273
1274 values = asdl_seq_new(size, c->c_arena);
1275 if (!values)
1276 return NULL;
1277
1278 for (i = 0; i < NCH(ch); i += 4) {
1279 expr_ty expression;
1280
1281 expression = ast_for_expr(c, CHILD(ch, i));
1282 if (!expression)
1283 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001284
Guido van Rossum86e58e22006-08-28 15:27:34 +00001285 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitze76adcd2005-11-15 05:04:31 +00001286
Guido van Rossum86e58e22006-08-28 15:27:34 +00001287 expression = ast_for_expr(c, CHILD(ch, i + 2));
1288 if (!expression)
1289 return NULL;
1290
1291 asdl_seq_SET(values, i / 4, expression);
1292 }
1293 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1294 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001297 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 return NULL;
1299 }
1300}
1301
1302static slice_ty
1303ast_for_slice(struct compiling *c, const node *n)
1304{
1305 node *ch;
1306 expr_ty lower = NULL, upper = NULL, step = NULL;
1307
1308 REQ(n, subscript);
1309
1310 /*
1311 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1312 sliceop: ':' [test]
1313 */
1314 ch = CHILD(n, 0);
1315 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001316 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317
1318 if (NCH(n) == 1 && TYPE(ch) == test) {
1319 /* 'step' variable hold no significance in terms of being used over
1320 other vars */
1321 step = ast_for_expr(c, ch);
1322 if (!step)
1323 return NULL;
1324
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001325 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 }
1327
1328 if (TYPE(ch) == test) {
1329 lower = ast_for_expr(c, ch);
1330 if (!lower)
1331 return NULL;
1332 }
1333
1334 /* If there's an upper bound it's in the second or third position. */
1335 if (TYPE(ch) == COLON) {
1336 if (NCH(n) > 1) {
1337 node *n2 = CHILD(n, 1);
1338
1339 if (TYPE(n2) == test) {
1340 upper = ast_for_expr(c, n2);
1341 if (!upper)
1342 return NULL;
1343 }
1344 }
1345 } else if (NCH(n) > 2) {
1346 node *n2 = CHILD(n, 2);
1347
1348 if (TYPE(n2) == test) {
1349 upper = ast_for_expr(c, n2);
1350 if (!upper)
1351 return NULL;
1352 }
1353 }
1354
1355 ch = CHILD(n, NCH(n) - 1);
1356 if (TYPE(ch) == sliceop) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001357 if (NCH(ch) == 1) {
1358 /* No expression, so step is None */
1359 ch = CHILD(ch, 0);
1360 step = Name(new_identifier("None", c->c_arena), Load,
1361 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 if (!step)
1363 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001364 } else {
1365 ch = CHILD(ch, 1);
1366 if (TYPE(ch) == test) {
1367 step = ast_for_expr(c, ch);
1368 if (!step)
1369 return NULL;
1370 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 }
1372 }
1373
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001374 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375}
1376
1377static expr_ty
1378ast_for_binop(struct compiling *c, const node *n)
1379{
1380 /* Must account for a sequence of expressions.
1381 How should A op B op C by represented?
1382 BinOp(BinOp(A, op, B), op, C).
1383 */
1384
1385 int i, nops;
1386 expr_ty expr1, expr2, result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001387 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388
1389 expr1 = ast_for_expr(c, CHILD(n, 0));
1390 if (!expr1)
1391 return NULL;
1392
1393 expr2 = ast_for_expr(c, CHILD(n, 2));
1394 if (!expr2)
1395 return NULL;
1396
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001397 newoperator = get_operator(CHILD(n, 1));
1398 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 return NULL;
1400
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001401 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1402 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 if (!result)
1404 return NULL;
1405
1406 nops = (NCH(n) - 1) / 2;
1407 for (i = 1; i < nops; i++) {
1408 expr_ty tmp_result, tmp;
1409 const node* next_oper = CHILD(n, i * 2 + 1);
1410
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411 newoperator = get_operator(next_oper);
1412 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 return NULL;
1414
1415 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1416 if (!tmp)
1417 return NULL;
1418
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001419 tmp_result = BinOp(result, newoperator, tmp,
1420 LINENO(next_oper), next_oper->n_col_offset,
1421 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 if (!tmp)
1423 return NULL;
1424 result = tmp_result;
1425 }
1426 return result;
1427}
1428
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001429static expr_ty
1430ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1431{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001432 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1433 subscriptlist: subscript (',' subscript)* [',']
1434 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1435 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001436 REQ(n, trailer);
1437 if (TYPE(CHILD(n, 0)) == LPAR) {
1438 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001439 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1440 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001441 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001442 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001443 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001444 else if (TYPE(CHILD(n, 0)) == DOT ) {
1445 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001446 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001447 }
1448 else {
1449 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001450 REQ(CHILD(n, 2), RSQB);
1451 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001452 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001453 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1454 if (!slc)
1455 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001456 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1457 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001458 }
1459 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001460 /* The grammar is ambiguous here. The ambiguity is resolved
1461 by treating the sequence as a tuple literal if there are
1462 no slice features.
1463 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001464 int j;
1465 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001466 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001467 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001468 asdl_seq *slices, *elts;
1469 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001470 if (!slices)
1471 return NULL;
1472 for (j = 0; j < NCH(n); j += 2) {
1473 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001474 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001475 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001476 if (slc->kind != Index_kind)
1477 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001478 asdl_seq_SET(slices, j / 2, slc);
1479 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001480 if (!simple) {
1481 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001482 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001483 }
1484 /* extract Index values and put them in a Tuple */
1485 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001486 if (!elts)
1487 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001488 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1489 slc = (slice_ty)asdl_seq_GET(slices, j);
1490 assert(slc->kind == Index_kind && slc->v.Index.value);
1491 asdl_seq_SET(elts, j, slc->v.Index.value);
1492 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001493 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001494 if (!e)
1495 return NULL;
1496 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001497 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001498 }
1499 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001500}
1501
1502static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001503ast_for_factor(struct compiling *c, const node *n)
1504{
1505 node *pfactor, *ppower, *patom, *pnum;
1506 expr_ty expression;
1507
1508 /* If the unary - operator is applied to a constant, don't generate
1509 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1510 constant. The peephole optimizer already does something like
1511 this but it doesn't handle the case where the constant is
1512 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1513 PyLongObject.
1514 */
1515 if (TYPE(CHILD(n, 0)) == MINUS
1516 && NCH(n) == 2
1517 && TYPE((pfactor = CHILD(n, 1))) == factor
1518 && NCH(pfactor) == 1
1519 && TYPE((ppower = CHILD(pfactor, 0))) == power
1520 && NCH(ppower) == 1
1521 && TYPE((patom = CHILD(ppower, 0))) == atom
1522 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1523 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1524 if (s == NULL)
1525 return NULL;
1526 s[0] = '-';
1527 strcpy(s + 1, STR(pnum));
1528 PyObject_FREE(STR(pnum));
1529 STR(pnum) = s;
1530 return ast_for_atom(c, patom);
1531 }
1532
1533 expression = ast_for_expr(c, CHILD(n, 1));
1534 if (!expression)
1535 return NULL;
1536
1537 switch (TYPE(CHILD(n, 0))) {
1538 case PLUS:
1539 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1540 c->c_arena);
1541 case MINUS:
1542 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1543 c->c_arena);
1544 case TILDE:
1545 return UnaryOp(Invert, expression, LINENO(n),
1546 n->n_col_offset, c->c_arena);
1547 }
1548 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1549 TYPE(CHILD(n, 0)));
1550 return NULL;
1551}
1552
1553static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001554ast_for_power(struct compiling *c, const node *n)
1555{
1556 /* power: atom trailer* ('**' factor)*
1557 */
1558 int i;
1559 expr_ty e, tmp;
1560 REQ(n, power);
1561 e = ast_for_atom(c, CHILD(n, 0));
1562 if (!e)
1563 return NULL;
1564 if (NCH(n) == 1)
1565 return e;
1566 for (i = 1; i < NCH(n); i++) {
1567 node *ch = CHILD(n, i);
1568 if (TYPE(ch) != trailer)
1569 break;
1570 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001571 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001572 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001573 tmp->lineno = e->lineno;
1574 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001575 e = tmp;
1576 }
1577 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1578 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001579 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001580 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001581 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001582 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001583 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001584 e = tmp;
1585 }
1586 return e;
1587}
1588
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589/* Do not name a variable 'expr'! Will cause a compile error.
1590*/
1591
1592static expr_ty
1593ast_for_expr(struct compiling *c, const node *n)
1594{
1595 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001596 test: or_test ['if' or_test 'else' test] | lambdef
1597 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 and_test: not_test ('and' not_test)*
1599 not_test: 'not' not_test | comparison
1600 comparison: expr (comp_op expr)*
1601 expr: xor_expr ('|' xor_expr)*
1602 xor_expr: and_expr ('^' and_expr)*
1603 and_expr: shift_expr ('&' shift_expr)*
1604 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1605 arith_expr: term (('+'|'-') term)*
1606 term: factor (('*'|'/'|'%'|'//') factor)*
1607 factor: ('+'|'-'|'~') factor | power
1608 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001609
1610 As well as modified versions that exist for backward compatibility,
1611 to explicitly allow:
1612 [ x for x in lambda: 0, lambda: 1 ]
1613 (which would be ambiguous without these extra rules)
1614
1615 old_test: or_test | old_lambdef
1616 old_lambdef: 'lambda' [vararglist] ':' old_test
1617
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618 */
1619
1620 asdl_seq *seq;
1621 int i;
1622
1623 loop:
1624 switch (TYPE(n)) {
1625 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001626 case old_test:
1627 if (TYPE(CHILD(n, 0)) == lambdef ||
1628 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001630 else if (NCH(n) > 1)
1631 return ast_for_ifexpr(c, n);
1632 /* Fallthrough */
1633 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634 case and_test:
1635 if (NCH(n) == 1) {
1636 n = CHILD(n, 0);
1637 goto loop;
1638 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001639 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 if (!seq)
1641 return NULL;
1642 for (i = 0; i < NCH(n); i += 2) {
1643 expr_ty e = ast_for_expr(c, CHILD(n, i));
1644 if (!e)
1645 return NULL;
1646 asdl_seq_SET(seq, i / 2, e);
1647 }
1648 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001649 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1650 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001651 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001652 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 case not_test:
1654 if (NCH(n) == 1) {
1655 n = CHILD(n, 0);
1656 goto loop;
1657 }
1658 else {
1659 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1660 if (!expression)
1661 return NULL;
1662
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001663 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1664 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 }
1666 case comparison:
1667 if (NCH(n) == 1) {
1668 n = CHILD(n, 0);
1669 goto loop;
1670 }
1671 else {
1672 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001673 asdl_int_seq *ops;
1674 asdl_seq *cmps;
1675 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 if (!ops)
1677 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001678 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 return NULL;
1681 }
1682 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001683 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001685 newoperator = ast_for_comp_op(CHILD(n, i));
1686 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001688 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689
1690 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001691 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001693 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001695 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 asdl_seq_SET(cmps, i / 2, expression);
1697 }
1698 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001699 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001701 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001703 return Compare(expression, ops, cmps, LINENO(n),
1704 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 }
1706 break;
1707
1708 /* The next five cases all handle BinOps. The main body of code
1709 is the same in each case, but the switch turned inside out to
1710 reuse the code for each type of operator.
1711 */
1712 case expr:
1713 case xor_expr:
1714 case and_expr:
1715 case shift_expr:
1716 case arith_expr:
1717 case term:
1718 if (NCH(n) == 1) {
1719 n = CHILD(n, 0);
1720 goto loop;
1721 }
1722 return ast_for_binop(c, n);
1723 case yield_expr: {
1724 expr_ty exp = NULL;
1725 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001726 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727 if (!exp)
1728 return NULL;
1729 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001730 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001732 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 if (NCH(n) == 1) {
1734 n = CHILD(n, 0);
1735 goto loop;
1736 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001737 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001738 case power:
1739 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001741 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 return NULL;
1743 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001744 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 return NULL;
1746}
1747
1748static expr_ty
1749ast_for_call(struct compiling *c, const node *n, expr_ty func)
1750{
1751 /*
1752 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1753 | '**' test)
1754 argument: [test '='] test [gen_for] # Really [keyword '='] test
1755 */
1756
1757 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001758 asdl_seq *args;
1759 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760 expr_ty vararg = NULL, kwarg = NULL;
1761
1762 REQ(n, arglist);
1763
1764 nargs = 0;
1765 nkeywords = 0;
1766 ngens = 0;
1767 for (i = 0; i < NCH(n); i++) {
1768 node *ch = CHILD(n, i);
1769 if (TYPE(ch) == argument) {
1770 if (NCH(ch) == 1)
1771 nargs++;
1772 else if (TYPE(CHILD(ch, 1)) == gen_for)
1773 ngens++;
1774 else
1775 nkeywords++;
1776 }
1777 }
1778 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001779 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 "if not sole argument");
1781 return NULL;
1782 }
1783
1784 if (nargs + nkeywords + ngens > 255) {
1785 ast_error(n, "more than 255 arguments");
1786 return NULL;
1787 }
1788
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001789 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001791 return NULL;
1792 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 nargs = 0;
1796 nkeywords = 0;
1797 for (i = 0; i < NCH(n); i++) {
1798 node *ch = CHILD(n, i);
1799 if (TYPE(ch) == argument) {
1800 expr_ty e;
1801 if (NCH(ch) == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001802 if (nkeywords) {
1803 ast_error(CHILD(ch, 0),
1804 "non-keyword arg after keyword arg");
1805 return NULL;
1806 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 e = ast_for_expr(c, CHILD(ch, 0));
1808 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 asdl_seq_SET(args, nargs++, e);
1811 }
1812 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1813 e = ast_for_genexp(c, ch);
1814 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816 asdl_seq_SET(args, nargs++, e);
1817 }
1818 else {
1819 keyword_ty kw;
1820 identifier key;
1821
1822 /* CHILD(ch, 0) is test, but must be an identifier? */
1823 e = ast_for_expr(c, CHILD(ch, 0));
1824 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001825 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 /* f(lambda x: x[0] = 3) ends up getting parsed with
1827 * LHS test = lambda x: x[0], and RHS test = 3.
1828 * SF bug 132313 points out that complaining about a keyword
1829 * then is very confusing.
1830 */
1831 if (e->kind == Lambda_kind) {
1832 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 } else if (e->kind != Name_kind) {
1835 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 }
1838 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 e = ast_for_expr(c, CHILD(ch, 2));
1840 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001841 return NULL;
1842 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 asdl_seq_SET(keywords, nkeywords++, kw);
1846 }
1847 }
1848 else if (TYPE(ch) == STAR) {
1849 vararg = ast_for_expr(c, CHILD(n, i+1));
1850 i++;
1851 }
1852 else if (TYPE(ch) == DOUBLESTAR) {
1853 kwarg = ast_for_expr(c, CHILD(n, i+1));
1854 i++;
1855 }
1856 }
1857
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001858 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859}
1860
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001862ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001864 /* testlist_gexp: test (',' test)* [','] */
1865 /* testlist: test (',' test)* [','] */
1866 /* testlist_safe: test (',' test)+ [','] */
1867 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001869 if (TYPE(n) == testlist_gexp) {
1870 if (NCH(n) > 1)
1871 assert(TYPE(CHILD(n, 1)) != gen_for);
1872 }
1873 else {
1874 assert(TYPE(n) == testlist ||
1875 TYPE(n) == testlist_safe ||
1876 TYPE(n) == testlist1);
1877 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 if (NCH(n) == 1)
1879 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 else {
1881 asdl_seq *tmp = seq_for_testlist(c, n);
1882 if (!tmp)
1883 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001884 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001886}
1887
1888static expr_ty
1889ast_for_testlist_gexp(struct compiling *c, const node* n)
1890{
1891 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1892 /* argument: test [ gen_for ] */
1893 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001894 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001895 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001896 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001897}
1898
1899/* like ast_for_testlist() but returns a sequence */
1900static asdl_seq*
1901ast_for_class_bases(struct compiling *c, const node* n)
1902{
1903 /* testlist: test (',' test)* [','] */
1904 assert(NCH(n) > 0);
1905 REQ(n, testlist);
1906 if (NCH(n) == 1) {
1907 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001908 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001909 if (!bases)
1910 return NULL;
1911 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001912 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001913 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001914 asdl_seq_SET(bases, 0, base);
1915 return bases;
1916 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001917
1918 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919}
1920
1921static stmt_ty
1922ast_for_expr_stmt(struct compiling *c, const node *n)
1923{
1924 REQ(n, expr_stmt);
1925 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1926 | ('=' (yield_expr|testlist))*)
1927 testlist: test (',' test)* [',']
1928 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1929 | '<<=' | '>>=' | '**=' | '//='
1930 test: ... here starts the operator precendence dance
1931 */
1932
1933 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001934 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 if (!e)
1936 return NULL;
1937
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001938 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 }
1940 else if (TYPE(CHILD(n, 1)) == augassign) {
1941 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001942 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 node *ch = CHILD(n, 0);
1944
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001945 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 if (!expr1)
1947 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001948 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001949 switch (expr1->kind) {
1950 case GeneratorExp_kind:
1951 ast_error(ch, "augmented assignment to generator "
1952 "expression not possible");
1953 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001954 case Yield_kind:
1955 ast_error(ch, "augmented assignment to yield "
1956 "expression not possible");
1957 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001958 case Name_kind: {
1959 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1960 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1961 ast_error(ch, "assignment to None");
1962 return NULL;
1963 }
1964 break;
1965 }
1966 case Attribute_kind:
1967 case Subscript_kind:
1968 break;
1969 default:
1970 ast_error(ch, "illegal expression for augmented "
1971 "assignment");
1972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001974 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975
1976 ch = CHILD(n, 2);
1977 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001978 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 else
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001980 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001981 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 return NULL;
1983
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001984 newoperator = ast_for_augassign(CHILD(n, 1));
1985 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 return NULL;
1987
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001988 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 }
1990 else {
1991 int i;
1992 asdl_seq *targets;
1993 node *value;
1994 expr_ty expression;
1995
1996 /* a normal assignment */
1997 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001998 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 if (!targets)
2000 return NULL;
2001 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00002002 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 node *ch = CHILD(n, i);
2004 if (TYPE(ch) == yield_expr) {
2005 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002006 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002008 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009
2010 /* set context to assign */
2011 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013
Neal Norwitz84456bd2005-12-18 03:16:20 +00002014 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002015 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016
2017 asdl_seq_SET(targets, i / 2, e);
2018 }
2019 value = CHILD(n, NCH(n) - 1);
2020 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002021 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 else
2023 expression = ast_for_expr(c, value);
2024 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002025 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002026 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028}
2029
2030static stmt_ty
2031ast_for_print_stmt(struct compiling *c, const node *n)
2032{
2033 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2034 | '>>' test [ (',' test)+ [','] ] )
2035 */
2036 expr_ty dest = NULL, expression;
2037 asdl_seq *seq;
2038 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002039 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040
2041 REQ(n, print_stmt);
2042 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2043 dest = ast_for_expr(c, CHILD(n, 2));
2044 if (!dest)
2045 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002046 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002048 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002050 return NULL;
2051 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002053 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002055 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 }
2057 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002058 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059}
2060
2061static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002062ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063{
2064 asdl_seq *seq;
2065 int i;
2066 expr_ty e;
2067
2068 REQ(n, exprlist);
2069
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002070 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 if (!seq)
2072 return NULL;
2073 for (i = 0; i < NCH(n); i += 2) {
2074 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002075 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002076 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002077 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002078 if (context && !set_context(e, context, CHILD(n, i)))
2079 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080 }
2081 return seq;
2082}
2083
2084static stmt_ty
2085ast_for_del_stmt(struct compiling *c, const node *n)
2086{
2087 asdl_seq *expr_list;
2088
2089 /* del_stmt: 'del' exprlist */
2090 REQ(n, del_stmt);
2091
2092 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2093 if (!expr_list)
2094 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002095 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096}
2097
2098static stmt_ty
2099ast_for_flow_stmt(struct compiling *c, const node *n)
2100{
2101 /*
2102 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2103 | yield_stmt
2104 break_stmt: 'break'
2105 continue_stmt: 'continue'
2106 return_stmt: 'return' [testlist]
2107 yield_stmt: yield_expr
2108 yield_expr: 'yield' testlist
2109 raise_stmt: 'raise' [test [',' test [',' test]]]
2110 */
2111 node *ch;
2112
2113 REQ(n, flow_stmt);
2114 ch = CHILD(n, 0);
2115 switch (TYPE(ch)) {
2116 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002117 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002119 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 case yield_stmt: { /* will reduce to yield_expr */
2121 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2122 if (!exp)
2123 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002124 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 }
2126 case return_stmt:
2127 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002128 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002130 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 if (!expression)
2132 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002133 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 }
2135 case raise_stmt:
2136 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002137 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 else if (NCH(ch) == 2) {
2139 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2140 if (!expression)
2141 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002142 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 }
2144 else if (NCH(ch) == 4) {
2145 expr_ty expr1, expr2;
2146
2147 expr1 = ast_for_expr(c, CHILD(ch, 1));
2148 if (!expr1)
2149 return NULL;
2150 expr2 = ast_for_expr(c, CHILD(ch, 3));
2151 if (!expr2)
2152 return NULL;
2153
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002154 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 }
2156 else if (NCH(ch) == 6) {
2157 expr_ty expr1, expr2, expr3;
2158
2159 expr1 = ast_for_expr(c, CHILD(ch, 1));
2160 if (!expr1)
2161 return NULL;
2162 expr2 = ast_for_expr(c, CHILD(ch, 3));
2163 if (!expr2)
2164 return NULL;
2165 expr3 = ast_for_expr(c, CHILD(ch, 5));
2166 if (!expr3)
2167 return NULL;
2168
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002169 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 }
2171 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002172 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 "unexpected flow_stmt: %d", TYPE(ch));
2174 return NULL;
2175 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002176
2177 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2178 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179}
2180
2181static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002182alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183{
2184 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002185 import_as_name: NAME ['as' NAME]
2186 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 dotted_name: NAME ('.' NAME)*
2188 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002189 PyObject *str;
2190
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 loop:
2192 switch (TYPE(n)) {
2193 case import_as_name:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002194 str = NULL;
2195 if (NCH(n) == 3) {
2196 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2197 ast_error(n, "must use 'as' in import");
2198 return NULL;
2199 }
2200 str = NEW_IDENTIFIER(CHILD(n, 2));
2201 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002202 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 case dotted_as_name:
2204 if (NCH(n) == 1) {
2205 n = CHILD(n, 0);
2206 goto loop;
2207 }
2208 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002209 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002210 if (!a)
2211 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002212 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2213 ast_error(n, "must use 'as' in import");
2214 return NULL;
2215 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 assert(!a->asname);
2217 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2218 return a;
2219 }
2220 break;
2221 case dotted_name:
2222 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002223 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 else {
2225 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002226 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002227 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 char *s;
2229
2230 len = 0;
2231 for (i = 0; i < NCH(n); i += 2)
2232 /* length of string plus one for the dot */
2233 len += strlen(STR(CHILD(n, i))) + 1;
2234 len--; /* the last name doesn't have a dot */
2235 str = PyString_FromStringAndSize(NULL, len);
2236 if (!str)
2237 return NULL;
2238 s = PyString_AS_STRING(str);
2239 if (!s)
2240 return NULL;
2241 for (i = 0; i < NCH(n); i += 2) {
2242 char *sch = STR(CHILD(n, i));
2243 strcpy(s, STR(CHILD(n, i)));
2244 s += strlen(sch);
2245 *s++ = '.';
2246 }
2247 --s;
2248 *s = '\0';
2249 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002250 PyArena_AddPyObject(c->c_arena, str);
2251 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 }
2253 break;
2254 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002255 str = PyString_InternFromString("*");
2256 PyArena_AddPyObject(c->c_arena, str);
2257 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002259 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 "unexpected import name: %d", TYPE(n));
2261 return NULL;
2262 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002263
2264 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 return NULL;
2266}
2267
2268static stmt_ty
2269ast_for_import_stmt(struct compiling *c, const node *n)
2270{
2271 /*
2272 import_stmt: import_name | import_from
2273 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002274 import_from: 'from' ('.'* dotted_name | '.') 'import'
2275 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002277 int lineno;
2278 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 int i;
2280 asdl_seq *aliases;
2281
2282 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002283 lineno = LINENO(n);
2284 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002286 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002288 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002289 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 if (!aliases)
2291 return NULL;
2292 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002293 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002294 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 asdl_seq_SET(aliases, i / 2, import_alias);
2297 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002298 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002300 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002302 int idx, ndots = 0;
2303 alias_ty mod = NULL;
2304 identifier modname;
2305
2306 /* Count the number of dots (for relative imports) and check for the
2307 optional module name */
2308 for (idx = 1; idx < NCH(n); idx++) {
2309 if (TYPE(CHILD(n, idx)) == dotted_name) {
2310 mod = alias_for_import_name(c, CHILD(n, idx));
2311 idx++;
2312 break;
2313 } else if (TYPE(CHILD(n, idx)) != DOT) {
2314 break;
2315 }
2316 ndots++;
2317 }
2318 idx++; /* skip over the 'import' keyword */
2319 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002320 case STAR:
2321 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002322 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002323 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002324 if (ndots) {
2325 ast_error(n, "'import *' not allowed with 'from .'");
2326 return NULL;
2327 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002328 break;
2329 case LPAR:
2330 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002331 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002332 n_children = NCH(n);
2333 break;
2334 case import_as_names:
2335 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002336 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002337 n_children = NCH(n);
2338 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 ast_error(n, "trailing comma not allowed without"
2340 " surrounding parentheses");
2341 return NULL;
2342 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002343 break;
2344 default:
2345 ast_error(n, "Unexpected node-type in from-import");
2346 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002347 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002349 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002350 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352
2353 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002354 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002355 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002356 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002358 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002360 else {
2361 for (i = 0; i < NCH(n); i += 2) {
2362 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2363 if (!import_alias)
2364 return NULL;
2365 asdl_seq_SET(aliases, i / 2, import_alias);
2366 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002367 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002368 if (mod != NULL)
2369 modname = mod->name;
2370 else
2371 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002372 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002373 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 }
Neal Norwitz79792652005-11-14 04:25:03 +00002375 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 "unknown import statement: starts with command '%s'",
2377 STR(CHILD(n, 0)));
2378 return NULL;
2379}
2380
2381static stmt_ty
2382ast_for_global_stmt(struct compiling *c, const node *n)
2383{
2384 /* global_stmt: 'global' NAME (',' NAME)* */
2385 identifier name;
2386 asdl_seq *s;
2387 int i;
2388
2389 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002390 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 if (!s)
2392 return NULL;
2393 for (i = 1; i < NCH(n); i += 2) {
2394 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002395 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 asdl_seq_SET(s, i / 2, name);
2398 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002399 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400}
2401
2402static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403ast_for_assert_stmt(struct compiling *c, const node *n)
2404{
2405 /* assert_stmt: 'assert' test [',' test] */
2406 REQ(n, assert_stmt);
2407 if (NCH(n) == 2) {
2408 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2409 if (!expression)
2410 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002411 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 }
2413 else if (NCH(n) == 4) {
2414 expr_ty expr1, expr2;
2415
2416 expr1 = ast_for_expr(c, CHILD(n, 1));
2417 if (!expr1)
2418 return NULL;
2419 expr2 = ast_for_expr(c, CHILD(n, 3));
2420 if (!expr2)
2421 return NULL;
2422
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002423 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 }
Neal Norwitz79792652005-11-14 04:25:03 +00002425 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 "improper number of parts to 'assert' statement: %d",
2427 NCH(n));
2428 return NULL;
2429}
2430
2431static asdl_seq *
2432ast_for_suite(struct compiling *c, const node *n)
2433{
2434 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002435 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 stmt_ty s;
2437 int i, total, num, end, pos = 0;
2438 node *ch;
2439
2440 REQ(n, suite);
2441
2442 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002443 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 if (!seq)
2445 return NULL;
2446 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2447 n = CHILD(n, 0);
2448 /* simple_stmt always ends with a NEWLINE,
2449 and may have a trailing SEMI
2450 */
2451 end = NCH(n) - 1;
2452 if (TYPE(CHILD(n, end - 1)) == SEMI)
2453 end--;
2454 /* loop by 2 to skip semi-colons */
2455 for (i = 0; i < end; i += 2) {
2456 ch = CHILD(n, i);
2457 s = ast_for_stmt(c, ch);
2458 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002459 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460 asdl_seq_SET(seq, pos++, s);
2461 }
2462 }
2463 else {
2464 for (i = 2; i < (NCH(n) - 1); i++) {
2465 ch = CHILD(n, i);
2466 REQ(ch, stmt);
2467 num = num_stmts(ch);
2468 if (num == 1) {
2469 /* small_stmt or compound_stmt with only one child */
2470 s = ast_for_stmt(c, ch);
2471 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002472 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 asdl_seq_SET(seq, pos++, s);
2474 }
2475 else {
2476 int j;
2477 ch = CHILD(ch, 0);
2478 REQ(ch, simple_stmt);
2479 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002480 /* statement terminates with a semi-colon ';' */
2481 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002482 assert((j + 1) == NCH(ch));
2483 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002484 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 s = ast_for_stmt(c, CHILD(ch, j));
2486 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002487 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 asdl_seq_SET(seq, pos++, s);
2489 }
2490 }
2491 }
2492 }
2493 assert(pos == seq->size);
2494 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495}
2496
2497static stmt_ty
2498ast_for_if_stmt(struct compiling *c, const node *n)
2499{
2500 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2501 ['else' ':' suite]
2502 */
2503 char *s;
2504
2505 REQ(n, if_stmt);
2506
2507 if (NCH(n) == 4) {
2508 expr_ty expression;
2509 asdl_seq *suite_seq;
2510
2511 expression = ast_for_expr(c, CHILD(n, 1));
2512 if (!expression)
2513 return NULL;
2514 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002515 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 return NULL;
2517
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002518 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 s = STR(CHILD(n, 4));
2522 /* s[2], the third character in the string, will be
2523 's' for el_s_e, or
2524 'i' for el_i_f
2525 */
2526 if (s[2] == 's') {
2527 expr_ty expression;
2528 asdl_seq *seq1, *seq2;
2529
2530 expression = ast_for_expr(c, CHILD(n, 1));
2531 if (!expression)
2532 return NULL;
2533 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002534 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 return NULL;
2536 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002537 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 return NULL;
2539
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002540 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 }
2542 else if (s[2] == 'i') {
2543 int i, n_elif, has_else = 0;
2544 asdl_seq *orelse = NULL;
2545 n_elif = NCH(n) - 4;
2546 /* must reference the child n_elif+1 since 'else' token is third,
2547 not fourth, child from the end. */
2548 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2549 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2550 has_else = 1;
2551 n_elif -= 3;
2552 }
2553 n_elif /= 4;
2554
2555 if (has_else) {
2556 expr_ty expression;
2557 asdl_seq *seq1, *seq2;
2558
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002559 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 if (!orelse)
2561 return NULL;
2562 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002563 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002566 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002569 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571
2572 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002573 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002574 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 /* the just-created orelse handled the last elif */
2576 n_elif--;
2577 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578
2579 for (i = 0; i < n_elif; i++) {
2580 int off = 5 + (n_elif - i - 1) * 4;
2581 expr_ty expression;
2582 asdl_seq *suite_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002583 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2584 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 return NULL;
2586 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002587 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002590 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002593 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002595 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002596 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 }
2598 return If(ast_for_expr(c, CHILD(n, 1)),
2599 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002600 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002602
2603 PyErr_Format(PyExc_SystemError,
2604 "unexpected token in 'if' statement: %s", s);
2605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606}
2607
2608static stmt_ty
2609ast_for_while_stmt(struct compiling *c, const node *n)
2610{
2611 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2612 REQ(n, while_stmt);
2613
2614 if (NCH(n) == 4) {
2615 expr_ty expression;
2616 asdl_seq *suite_seq;
2617
2618 expression = ast_for_expr(c, CHILD(n, 1));
2619 if (!expression)
2620 return NULL;
2621 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002622 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002624 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 }
2626 else if (NCH(n) == 7) {
2627 expr_ty expression;
2628 asdl_seq *seq1, *seq2;
2629
2630 expression = ast_for_expr(c, CHILD(n, 1));
2631 if (!expression)
2632 return NULL;
2633 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002634 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 return NULL;
2636 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002637 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 return NULL;
2639
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002640 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002642
2643 PyErr_Format(PyExc_SystemError,
2644 "wrong number of tokens for 'while' statement: %d",
2645 NCH(n));
2646 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647}
2648
2649static stmt_ty
2650ast_for_for_stmt(struct compiling *c, const node *n)
2651{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002652 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 expr_ty expression;
2654 expr_ty target;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002655 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2657 REQ(n, for_stmt);
2658
2659 if (NCH(n) == 9) {
2660 seq = ast_for_suite(c, CHILD(n, 8));
2661 if (!seq)
2662 return NULL;
2663 }
2664
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002665 node_target = CHILD(n, 1);
2666 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002667 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002669 /* Check the # of children rather than the length of _target, since
2670 for x, in ... has 1 element in _target, but still requires a Tuple. */
2671 if (NCH(node_target) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002672 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002674 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002676 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002677 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678 return NULL;
2679 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002680 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681 return NULL;
2682
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002683 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2684 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685}
2686
2687static excepthandler_ty
2688ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2689{
2690 /* except_clause: 'except' [test [',' test]] */
2691 REQ(exc, except_clause);
2692 REQ(body, suite);
2693
2694 if (NCH(exc) == 1) {
2695 asdl_seq *suite_seq = ast_for_suite(c, body);
2696 if (!suite_seq)
2697 return NULL;
2698
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002699 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2700 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 }
2702 else if (NCH(exc) == 2) {
2703 expr_ty expression;
2704 asdl_seq *suite_seq;
2705
2706 expression = ast_for_expr(c, CHILD(exc, 1));
2707 if (!expression)
2708 return NULL;
2709 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002710 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 return NULL;
2712
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002713 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2714 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 }
2716 else if (NCH(exc) == 4) {
2717 asdl_seq *suite_seq;
2718 expr_ty expression;
2719 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2720 if (!e)
2721 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002722 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 return NULL;
2724 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002725 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 return NULL;
2727 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002728 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 return NULL;
2730
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002731 return excepthandler(expression, e, suite_seq, LINENO(exc),
2732 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002734
2735 PyErr_Format(PyExc_SystemError,
2736 "wrong number of children for 'except' clause: %d",
2737 NCH(exc));
2738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739}
2740
2741static stmt_ty
2742ast_for_try_stmt(struct compiling *c, const node *n)
2743{
Neal Norwitzf599f422005-12-17 21:33:47 +00002744 const int nch = NCH(n);
2745 int n_except = (nch - 3)/3;
2746 asdl_seq *body, *orelse = NULL, *finally = NULL;
2747
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 REQ(n, try_stmt);
2749
Neal Norwitzf599f422005-12-17 21:33:47 +00002750 body = ast_for_suite(c, CHILD(n, 2));
2751 if (body == NULL)
2752 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753
Neal Norwitzf599f422005-12-17 21:33:47 +00002754 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2755 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2756 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2757 /* we can assume it's an "else",
2758 because nch >= 9 for try-else-finally and
2759 it would otherwise have a type of except_clause */
2760 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2761 if (orelse == NULL)
2762 return NULL;
2763 n_except--;
2764 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765
Neal Norwitzf599f422005-12-17 21:33:47 +00002766 finally = ast_for_suite(c, CHILD(n, nch - 1));
2767 if (finally == NULL)
2768 return NULL;
2769 n_except--;
2770 }
2771 else {
2772 /* we can assume it's an "else",
2773 otherwise it would have a type of except_clause */
2774 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2775 if (orelse == NULL)
2776 return NULL;
2777 n_except--;
2778 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002780 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002781 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 return NULL;
2783 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002784
2785 if (n_except > 0) {
2786 int i;
2787 stmt_ty except_st;
2788 /* process except statements to create a try ... except */
2789 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2790 if (handlers == NULL)
2791 return NULL;
2792
2793 for (i = 0; i < n_except; i++) {
2794 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2795 CHILD(n, 5 + i * 3));
2796 if (!e)
2797 return NULL;
2798 asdl_seq_SET(handlers, i, e);
2799 }
2800
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002801 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2802 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002803 if (!finally)
2804 return except_st;
2805
2806 /* if a 'finally' is present too, we nest the TryExcept within a
2807 TryFinally to emulate try ... except ... finally */
2808 body = asdl_seq_new(1, c->c_arena);
2809 if (body == NULL)
2810 return NULL;
2811 asdl_seq_SET(body, 0, except_st);
2812 }
2813
2814 /* must be a try ... finally (except clauses are in body, if any exist) */
2815 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002816 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817}
2818
Guido van Rossumc2e20742006-02-27 22:32:47 +00002819static expr_ty
2820ast_for_with_var(struct compiling *c, const node *n)
2821{
2822 REQ(n, with_var);
2823 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2824 ast_error(n, "expected \"with [expr] as [var]\"");
2825 return NULL;
2826 }
2827 return ast_for_expr(c, CHILD(n, 1));
2828}
2829
2830/* with_stmt: 'with' test [ with_var ] ':' suite */
2831static stmt_ty
2832ast_for_with_stmt(struct compiling *c, const node *n)
2833{
2834 expr_ty context_expr, optional_vars = NULL;
2835 int suite_index = 3; /* skip 'with', test, and ':' */
2836 asdl_seq *suite_seq;
2837
2838 assert(TYPE(n) == with_stmt);
2839 context_expr = ast_for_expr(c, CHILD(n, 1));
2840 if (TYPE(CHILD(n, 2)) == with_var) {
2841 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2842
2843 if (!optional_vars) {
2844 return NULL;
2845 }
2846 if (!set_context(optional_vars, Store, n)) {
2847 return NULL;
2848 }
2849 suite_index = 4;
2850 }
2851
2852 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2853 if (!suite_seq) {
2854 return NULL;
2855 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002856 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2857 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002858}
2859
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860static stmt_ty
2861ast_for_classdef(struct compiling *c, const node *n)
2862{
2863 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 asdl_seq *bases, *s;
2865
2866 REQ(n, classdef);
2867
2868 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2869 ast_error(n, "assignment to None");
2870 return NULL;
2871 }
2872
2873 if (NCH(n) == 4) {
2874 s = ast_for_suite(c, CHILD(n, 3));
2875 if (!s)
2876 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002877 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2878 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 }
2880 /* check for empty base list */
2881 if (TYPE(CHILD(n,3)) == RPAR) {
2882 s = ast_for_suite(c, CHILD(n,5));
2883 if (!s)
2884 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002885 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2886 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 }
2888
2889 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002890 bases = ast_for_class_bases(c, CHILD(n, 3));
2891 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893
2894 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002895 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002897 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2898 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899}
2900
2901static stmt_ty
2902ast_for_stmt(struct compiling *c, const node *n)
2903{
2904 if (TYPE(n) == stmt) {
2905 assert(NCH(n) == 1);
2906 n = CHILD(n, 0);
2907 }
2908 if (TYPE(n) == simple_stmt) {
2909 assert(num_stmts(n) == 1);
2910 n = CHILD(n, 0);
2911 }
2912 if (TYPE(n) == small_stmt) {
2913 REQ(n, small_stmt);
2914 n = CHILD(n, 0);
2915 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002916 | flow_stmt | import_stmt | global_stmt | assert_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 */
2918 switch (TYPE(n)) {
2919 case expr_stmt:
2920 return ast_for_expr_stmt(c, n);
2921 case print_stmt:
2922 return ast_for_print_stmt(c, n);
2923 case del_stmt:
2924 return ast_for_del_stmt(c, n);
2925 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002926 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927 case flow_stmt:
2928 return ast_for_flow_stmt(c, n);
2929 case import_stmt:
2930 return ast_for_import_stmt(c, n);
2931 case global_stmt:
2932 return ast_for_global_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933 case assert_stmt:
2934 return ast_for_assert_stmt(c, n);
2935 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002936 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2938 TYPE(n), NCH(n));
2939 return NULL;
2940 }
2941 }
2942 else {
2943 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2944 | funcdef | classdef
2945 */
2946 node *ch = CHILD(n, 0);
2947 REQ(n, compound_stmt);
2948 switch (TYPE(ch)) {
2949 case if_stmt:
2950 return ast_for_if_stmt(c, ch);
2951 case while_stmt:
2952 return ast_for_while_stmt(c, ch);
2953 case for_stmt:
2954 return ast_for_for_stmt(c, ch);
2955 case try_stmt:
2956 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002957 case with_stmt:
2958 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959 case funcdef:
2960 return ast_for_funcdef(c, ch);
2961 case classdef:
2962 return ast_for_classdef(c, ch);
2963 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002964 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2966 TYPE(n), NCH(n));
2967 return NULL;
2968 }
2969 }
2970}
2971
2972static PyObject *
2973parsenumber(const char *s)
2974{
2975 const char *end;
2976 long x;
2977 double dx;
2978#ifndef WITHOUT_COMPLEX
2979 Py_complex c;
2980 int imflag;
2981#endif
2982
2983 errno = 0;
2984 end = s + strlen(s) - 1;
2985#ifndef WITHOUT_COMPLEX
2986 imflag = *end == 'j' || *end == 'J';
2987#endif
2988 if (*end == 'l' || *end == 'L')
2989 return PyLong_FromString((char *)s, (char **)0, 0);
2990 if (s[0] == '0') {
2991 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2992 if (x < 0 && errno == 0) {
2993 return PyLong_FromString((char *)s,
2994 (char **)0,
2995 0);
2996 }
2997 }
2998 else
2999 x = PyOS_strtol((char *)s, (char **)&end, 0);
3000 if (*end == '\0') {
3001 if (errno != 0)
3002 return PyLong_FromString((char *)s, (char **)0, 0);
3003 return PyInt_FromLong(x);
3004 }
3005 /* XXX Huge floats may silently fail */
3006#ifndef WITHOUT_COMPLEX
3007 if (imflag) {
3008 c.real = 0.;
3009 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003010 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011 PyFPE_END_PROTECT(c)
3012 return PyComplex_FromCComplex(c);
3013 }
3014 else
3015#endif
3016 {
3017 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003018 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019 PyFPE_END_PROTECT(dx)
3020 return PyFloat_FromDouble(dx);
3021 }
3022}
3023
3024static PyObject *
3025decode_utf8(const char **sPtr, const char *end, char* encoding)
3026{
3027#ifndef Py_USING_UNICODE
3028 Py_FatalError("decode_utf8 should not be called in this build.");
3029 return NULL;
3030#else
3031 PyObject *u, *v;
3032 char *s, *t;
3033 t = s = (char *)*sPtr;
3034 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3035 while (s < end && (*s & 0x80)) s++;
3036 *sPtr = s;
3037 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3038 if (u == NULL)
3039 return NULL;
3040 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3041 Py_DECREF(u);
3042 return v;
3043#endif
3044}
3045
3046static PyObject *
3047decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3048{
3049 PyObject *v, *u;
3050 char *buf;
3051 char *p;
3052 const char *end;
3053 if (encoding == NULL) {
3054 buf = (char *)s;
3055 u = NULL;
3056 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3057 buf = (char *)s;
3058 u = NULL;
3059 } else {
3060 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3061 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3062 if (u == NULL)
3063 return NULL;
3064 p = buf = PyString_AsString(u);
3065 end = s + len;
3066 while (s < end) {
3067 if (*s == '\\') {
3068 *p++ = *s++;
3069 if (*s & 0x80) {
3070 strcpy(p, "u005c");
3071 p += 5;
3072 }
3073 }
3074 if (*s & 0x80) { /* XXX inefficient */
3075 PyObject *w;
3076 char *r;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003077 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078 w = decode_utf8(&s, end, "utf-16-be");
3079 if (w == NULL) {
3080 Py_DECREF(u);
3081 return NULL;
3082 }
3083 r = PyString_AsString(w);
3084 rn = PyString_Size(w);
3085 assert(rn % 2 == 0);
3086 for (i = 0; i < rn; i += 2) {
3087 sprintf(p, "\\u%02x%02x",
3088 r[i + 0] & 0xFF,
3089 r[i + 1] & 0xFF);
3090 p += 6;
3091 }
3092 Py_DECREF(w);
3093 } else {
3094 *p++ = *s++;
3095 }
3096 }
3097 len = p - buf;
3098 s = buf;
3099 }
3100 if (rawmode)
3101 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3102 else
3103 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3104 Py_XDECREF(u);
3105 return v;
3106}
3107
3108/* s is a Python string literal, including the bracketing quote characters,
3109 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3110 * parsestr parses it, and returns the decoded Python string object.
3111 */
3112static PyObject *
3113parsestr(const char *s, const char *encoding)
3114{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003116 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 int rawmode = 0;
3118 int need_encoding;
3119 int unicode = 0;
3120
3121 if (isalpha(quote) || quote == '_') {
3122 if (quote == 'u' || quote == 'U') {
3123 quote = *++s;
3124 unicode = 1;
3125 }
3126 if (quote == 'r' || quote == 'R') {
3127 quote = *++s;
3128 rawmode = 1;
3129 }
3130 }
3131 if (quote != '\'' && quote != '\"') {
3132 PyErr_BadInternalCall();
3133 return NULL;
3134 }
3135 s++;
3136 len = strlen(s);
3137 if (len > INT_MAX) {
3138 PyErr_SetString(PyExc_OverflowError,
3139 "string to parse is too long");
3140 return NULL;
3141 }
3142 if (s[--len] != quote) {
3143 PyErr_BadInternalCall();
3144 return NULL;
3145 }
3146 if (len >= 4 && s[0] == quote && s[1] == quote) {
3147 s += 2;
3148 len -= 2;
3149 if (s[--len] != quote || s[--len] != quote) {
3150 PyErr_BadInternalCall();
3151 return NULL;
3152 }
3153 }
3154#ifdef Py_USING_UNICODE
3155 if (unicode || Py_UnicodeFlag) {
3156 return decode_unicode(s, len, rawmode, encoding);
3157 }
3158#endif
3159 need_encoding = (encoding != NULL &&
3160 strcmp(encoding, "utf-8") != 0 &&
3161 strcmp(encoding, "iso-8859-1") != 0);
3162 if (rawmode || strchr(s, '\\') == NULL) {
3163 if (need_encoding) {
3164#ifndef Py_USING_UNICODE
3165 /* This should not happen - we never see any other
3166 encoding. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003167 Py_FatalError(
3168 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003170 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 if (u == NULL)
3172 return NULL;
3173 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3174 Py_DECREF(u);
3175 return v;
3176#endif
3177 } else {
3178 return PyString_FromStringAndSize(s, len);
3179 }
3180 }
3181
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003182 return PyString_DecodeEscape(s, len, NULL, unicode,
3183 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184}
3185
3186/* Build a Python string object out of a STRING atom. This takes care of
3187 * compile-time literal catenation, calling parsestr() on each piece, and
3188 * pasting the intermediate results together.
3189 */
3190static PyObject *
3191parsestrplus(struct compiling *c, const node *n)
3192{
3193 PyObject *v;
3194 int i;
3195 REQ(CHILD(n, 0), STRING);
3196 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3197 /* String literal concatenation */
3198 for (i = 1; i < NCH(n); i++) {
3199 PyObject *s;
3200 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3201 if (s == NULL)
3202 goto onError;
3203 if (PyString_Check(v) && PyString_Check(s)) {
3204 PyString_ConcatAndDel(&v, s);
3205 if (v == NULL)
3206 goto onError;
3207 }
3208#ifdef Py_USING_UNICODE
3209 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003210 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212 Py_DECREF(v);
3213 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003214 if (v == NULL)
3215 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 }
3217#endif
3218 }
3219 }
3220 return v;
3221
3222 onError:
3223 Py_XDECREF(v);
3224 return NULL;
3225}