blob: eb4c68b885ed17c16b4b980a42d85633df12e086 [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/* Data structure used internally */
19struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000020 char *c_encoding; /* source encoding */
21 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022};
23
24static asdl_seq *seq_for_testlist(struct compiling *, const node *);
25static expr_ty ast_for_expr(struct compiling *, const node *);
26static stmt_ty ast_for_stmt(struct compiling *, const node *);
27static asdl_seq *ast_for_suite(struct compiling *, const node *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000028static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
29 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000030static expr_ty ast_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032
33/* Note different signature for ast_for_call */
34static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
35
36static PyObject *parsenumber(const char *);
37static PyObject *parsestr(const char *s, const char *encoding);
38static PyObject *parsestrplus(struct compiling *, const node *n);
39
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000041#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042#endif
43
Neal Norwitzadb69fc2005-12-17 20:54:49 +000044static identifier
45new_identifier(const char* n, PyArena *arena) {
46 PyObject* id = PyString_InternFromString(n);
47 PyArena_AddPyObject(arena, id);
48 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000049}
50
Neal Norwitzadb69fc2005-12-17 20:54:49 +000051#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052
53/* This routine provides an invalid object for the syntax error.
54 The outermost routine must unpack this error and create the
55 proper object. We do this so that we don't have to pass
56 the filename to everything function.
57
58 XXX Maybe we should just pass the filename...
59*/
60
61static int
62ast_error(const node *n, const char *errstr)
63{
64 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
65 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000066 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000067 PyErr_SetObject(PyExc_SyntaxError, u);
68 Py_DECREF(u);
69 return 0;
70}
71
72static void
73ast_error_finish(const char *filename)
74{
75 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000076 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077
78 assert(PyErr_Occurred());
79 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000080 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
82 PyErr_Fetch(&type, &value, &tback);
83 errstr = PyTuple_GetItem(value, 0);
84 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000085 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086 Py_INCREF(errstr);
87 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000088 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000089 Py_DECREF(errstr);
90 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000091 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092 Py_DECREF(value);
93
94 loc = PyErr_ProgramText(filename, lineno);
95 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000096 Py_INCREF(Py_None);
97 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +000099 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000101 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000102 Py_DECREF(errstr);
103 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000104 }
Georg Brandl7784f122006-05-26 20:04:44 +0000105 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106 Py_DECREF(errstr);
107 Py_DECREF(tmp);
108 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000109 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 PyErr_Restore(type, value, tback);
111}
112
113/* num_stmts() returns number of contained statements.
114
115 Use this routine to determine how big a sequence is needed for
116 the statements in a parse tree. Its raison d'etre is this bit of
117 grammar:
118
119 stmt: simple_stmt | compound_stmt
120 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
121
122 A simple_stmt can contain multiple small_stmt elements joined
123 by semicolons. If the arg is a simple_stmt, the number of
124 small_stmt elements is returned.
125*/
126
127static int
128num_stmts(const node *n)
129{
130 int i, l;
131 node *ch;
132
133 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000134 case single_input:
135 if (TYPE(CHILD(n, 0)) == NEWLINE)
136 return 0;
137 else
138 return num_stmts(CHILD(n, 0));
139 case file_input:
140 l = 0;
141 for (i = 0; i < NCH(n); i++) {
142 ch = CHILD(n, i);
143 if (TYPE(ch) == stmt)
144 l += num_stmts(ch);
145 }
146 return l;
147 case stmt:
148 return num_stmts(CHILD(n, 0));
149 case compound_stmt:
150 return 1;
151 case simple_stmt:
152 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
153 case suite:
154 if (NCH(n) == 1)
155 return num_stmts(CHILD(n, 0));
156 else {
157 l = 0;
158 for (i = 2; i < (NCH(n) - 1); i++)
159 l += num_stmts(CHILD(n, i));
160 return l;
161 }
162 default: {
163 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000165 sprintf(buf, "Non-statement found: %d %d\n",
166 TYPE(n), NCH(n));
167 Py_FatalError(buf);
168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169 }
170 assert(0);
171 return 0;
172}
173
174/* Transform the CST rooted at node * to the appropriate AST
175*/
176
177mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000178PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000179 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000181 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182 asdl_seq *stmts = NULL;
183 stmt_ty s;
184 node *ch;
185 struct compiling c;
186
187 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000188 c.c_encoding = "utf-8";
189 if (TYPE(n) == encoding_decl) {
190 ast_error(n, "encoding declaration in Unicode string");
191 goto error;
192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000194 c.c_encoding = STR(n);
195 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000197 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000199 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
Jeremy Hyltona8293132006-02-28 17:58:27 +0000201 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000203 case file_input:
204 stmts = asdl_seq_new(num_stmts(n), arena);
205 if (!stmts)
206 return NULL;
207 for (i = 0; i < NCH(n) - 1; i++) {
208 ch = CHILD(n, i);
209 if (TYPE(ch) == NEWLINE)
210 continue;
211 REQ(ch, stmt);
212 num = num_stmts(ch);
213 if (num == 1) {
214 s = ast_for_stmt(&c, ch);
215 if (!s)
216 goto error;
217 asdl_seq_SET(stmts, k++, s);
218 }
219 else {
220 ch = CHILD(ch, 0);
221 REQ(ch, simple_stmt);
222 for (j = 0; j < num; j++) {
223 s = ast_for_stmt(&c, CHILD(ch, j * 2));
224 if (!s)
225 goto error;
226 asdl_seq_SET(stmts, k++, s);
227 }
228 }
229 }
230 return Module(stmts, arena);
231 case eval_input: {
232 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000234 /* XXX Why not gen_for here? */
235 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
236 if (!testlist_ast)
237 goto error;
238 return Expression(testlist_ast, arena);
239 }
240 case single_input:
241 if (TYPE(CHILD(n, 0)) == NEWLINE) {
242 stmts = asdl_seq_new(1, arena);
243 if (!stmts)
244 goto error;
245 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
246 arena));
247 return Interactive(stmts, arena);
248 }
249 else {
250 n = CHILD(n, 0);
251 num = num_stmts(n);
252 stmts = asdl_seq_new(num, arena);
253 if (!stmts)
254 goto error;
255 if (num == 1) {
256 s = ast_for_stmt(&c, n);
257 if (!s)
258 goto error;
259 asdl_seq_SET(stmts, 0, s);
260 }
261 else {
262 /* Only a simple_stmt can contain multiple statements. */
263 REQ(n, simple_stmt);
264 for (i = 0; i < NCH(n); i += 2) {
265 if (TYPE(CHILD(n, i)) == NEWLINE)
266 break;
267 s = ast_for_stmt(&c, CHILD(n, i));
268 if (!s)
269 goto error;
270 asdl_seq_SET(stmts, i / 2, s);
271 }
272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000274 return Interactive(stmts, arena);
275 }
276 default:
277 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278 }
279 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 ast_error_finish(filename);
281 return NULL;
282}
283
284/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
285*/
286
287static operator_ty
288get_operator(const node *n)
289{
290 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000291 case VBAR:
292 return BitOr;
293 case CIRCUMFLEX:
294 return BitXor;
295 case AMPER:
296 return BitAnd;
297 case LEFTSHIFT:
298 return LShift;
299 case RIGHTSHIFT:
300 return RShift;
301 case PLUS:
302 return Add;
303 case MINUS:
304 return Sub;
305 case STAR:
306 return Mult;
307 case SLASH:
308 return Div;
309 case DOUBLESLASH:
310 return FloorDiv;
311 case PERCENT:
312 return Mod;
313 default:
314 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315 }
316}
317
Jeremy Hyltona8293132006-02-28 17:58:27 +0000318/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319
320 Only sets context for expr kinds that "can appear in assignment context"
321 (according to ../Parser/Python.asdl). For other expr kinds, it sets
322 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323*/
324
325static int
326set_context(expr_ty e, expr_context_ty ctx, const node *n)
327{
328 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000329 /* If a particular expression type can't be used for assign / delete,
330 set expr_name to its name and an error message will be generated.
331 */
332 const char* expr_name = NULL;
333
334 /* The ast defines augmented store and load contexts, but the
335 implementation here doesn't actually use them. The code may be
336 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000337 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000338 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000339 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000340 */
341 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342
343 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000344 case Attribute_kind:
345 if (ctx == Store &&
346 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
347 return ast_error(n, "assignment to None");
348 }
349 e->v.Attribute.ctx = ctx;
350 break;
351 case Subscript_kind:
352 e->v.Subscript.ctx = ctx;
353 break;
354 case Name_kind:
355 if (ctx == Store &&
356 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
357 return ast_error(n, "assignment to None");
358 }
359 e->v.Name.ctx = ctx;
360 break;
361 case List_kind:
362 e->v.List.ctx = ctx;
363 s = e->v.List.elts;
364 break;
365 case Tuple_kind:
366 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
367 return ast_error(n, "can't assign to ()");
368 e->v.Tuple.ctx = ctx;
369 s = e->v.Tuple.elts;
370 break;
371 case Lambda_kind:
372 expr_name = "lambda";
373 break;
374 case Call_kind:
375 expr_name = "function call";
376 break;
377 case BoolOp_kind:
378 case BinOp_kind:
379 case UnaryOp_kind:
380 expr_name = "operator";
381 break;
382 case GeneratorExp_kind:
383 expr_name = "generator expression";
384 break;
385 case Yield_kind:
386 expr_name = "yield expression";
387 break;
388 case ListComp_kind:
389 expr_name = "list comprehension";
390 break;
391 case Dict_kind:
392 case Num_kind:
393 case Str_kind:
394 expr_name = "literal";
395 break;
396 case Compare_kind:
397 expr_name = "comparison";
398 break;
399 case Repr_kind:
400 expr_name = "repr";
401 break;
402 case IfExp_kind:
403 expr_name = "conditional expression";
404 break;
405 default:
406 PyErr_Format(PyExc_SystemError,
407 "unexpected expression in assignment %d (line %d)",
408 e->kind, e->lineno);
409 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000411 /* Check for error string set by switch */
412 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000413 char buf[300];
414 PyOS_snprintf(buf, sizeof(buf),
415 "can't %s %s",
416 ctx == Store ? "assign to" : "delete",
417 expr_name);
418 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000419 }
420
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000422 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423 */
424 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000425 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000427 for (i = 0; i < asdl_seq_LEN(s); i++) {
428 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
429 return 0;
430 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431 }
432 return 1;
433}
434
435static operator_ty
436ast_for_augassign(const node *n)
437{
438 REQ(n, augassign);
439 n = CHILD(n, 0);
440 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000441 case '+':
442 return Add;
443 case '-':
444 return Sub;
445 case '/':
446 if (STR(n)[1] == '/')
447 return FloorDiv;
448 else
449 return Div;
450 case '%':
451 return Mod;
452 case '<':
453 return LShift;
454 case '>':
455 return RShift;
456 case '&':
457 return BitAnd;
458 case '^':
459 return BitXor;
460 case '|':
461 return BitOr;
462 case '*':
463 if (STR(n)[1] == '*')
464 return Pow;
465 else
466 return Mult;
467 default:
468 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
469 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470 }
471}
472
473static cmpop_ty
474ast_for_comp_op(const node *n)
475{
476 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000477 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478 */
479 REQ(n, comp_op);
480 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000481 n = CHILD(n, 0);
482 switch (TYPE(n)) {
483 case LESS:
484 return Lt;
485 case GREATER:
486 return Gt;
487 case EQEQUAL: /* == */
488 return Eq;
489 case LESSEQUAL:
490 return LtE;
491 case GREATEREQUAL:
492 return GtE;
493 case NOTEQUAL:
494 return NotEq;
495 case NAME:
496 if (strcmp(STR(n), "in") == 0)
497 return In;
498 if (strcmp(STR(n), "is") == 0)
499 return Is;
500 default:
501 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
502 STR(n));
503 return (cmpop_ty)0;
504 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505 }
506 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000507 /* handle "not in" and "is not" */
508 switch (TYPE(CHILD(n, 0))) {
509 case NAME:
510 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
511 return NotIn;
512 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
513 return IsNot;
514 default:
515 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
516 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
517 return (cmpop_ty)0;
518 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 }
Neal Norwitz79792652005-11-14 04:25:03 +0000520 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000521 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000522 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523}
524
525static asdl_seq *
526seq_for_testlist(struct compiling *c, const node *n)
527{
528 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000529 asdl_seq *seq;
530 expr_ty expression;
531 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000532 assert(TYPE(n) == testlist ||
533 TYPE(n) == listmaker ||
534 TYPE(n) == testlist_gexp ||
535 TYPE(n) == testlist_safe ||
536 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000538 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000540 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
542 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000543 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000545 expression = ast_for_expr(c, CHILD(n, i));
546 if (!expression)
547 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000549 assert(i / 2 < seq->size);
550 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551 }
552 return seq;
553}
554
555static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000556compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557{
558 int i, len = (NCH(n) + 1) / 2;
559 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000560 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000562 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563
Neal Norwitz3a230172006-09-22 08:18:10 +0000564 /* fpdef: NAME | '(' fplist ')'
565 fplist: fpdef (',' fpdef)* [',']
566 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000569 const node *fpdef_node = CHILD(n, 2*i);
570 const node *child;
571 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000572set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000573 /* fpdef_node is either a NAME or an fplist */
574 child = CHILD(fpdef_node, 0);
575 if (TYPE(child) == NAME) {
576 if (!strcmp(STR(child), "None")) {
577 ast_error(child, "assignment to None");
578 return NULL;
579 }
580 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
581 child->n_col_offset, c->c_arena);
582 }
583 else {
584 assert(TYPE(fpdef_node) == fpdef);
585 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
586 child = CHILD(fpdef_node, 1);
587 assert(TYPE(child) == fplist);
588 /* NCH == 1 means we have (x), we need to elide the extra parens */
589 if (NCH(child) == 1) {
590 fpdef_node = CHILD(child, 0);
591 assert(TYPE(fpdef_node) == fpdef);
592 goto set_name;
593 }
594 arg = compiler_complex_args(c, child);
595 }
596 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 }
598
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000599 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000600 if (!set_context(result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000601 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 return result;
603}
604
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
Jeremy Hyltona8293132006-02-28 17:58:27 +0000606/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607
608static arguments_ty
609ast_for_arguments(struct compiling *c, const node *n)
610{
611 /* parameters: '(' [varargslist] ')'
612 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000613 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000615 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 asdl_seq *args, *defaults;
617 identifier vararg = NULL, kwarg = NULL;
618 node *ch;
619
620 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000621 if (NCH(n) == 2) /* () as argument list */
622 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
623 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 }
625 REQ(n, varargslist);
626
627 /* first count the number of normal args & defaults */
628 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000629 ch = CHILD(n, i);
630 if (TYPE(ch) == fpdef)
631 n_args++;
632 if (TYPE(ch) == EQUAL)
633 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000635 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000637 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000638 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000640 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641
642 /* fpdef: NAME | '(' fplist ')'
643 fplist: fpdef (',' fpdef)* [',']
644 */
645 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000646 j = 0; /* index for defaults */
647 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000649 ch = CHILD(n, i);
650 switch (TYPE(ch)) {
651 case fpdef:
652 handle_fpdef:
653 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
654 anything other than EQUAL or a comma? */
655 /* XXX Should NCH(n) check be made a separate check? */
656 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
657 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
658 if (!expression)
659 goto error;
660 assert(defaults != NULL);
661 asdl_seq_SET(defaults, j++, expression);
662 i += 2;
663 found_default = 1;
664 }
665 else if (found_default) {
666 ast_error(n,
667 "non-default argument follows default argument");
668 goto error;
669 }
670 if (NCH(ch) == 3) {
671 ch = CHILD(ch, 1);
672 /* def foo((x)): is not complex, special case. */
673 if (NCH(ch) != 1) {
674 /* We have complex arguments, setup for unpacking. */
675 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
676 } else {
677 /* def foo((x)): setup for checking NAME below. */
678 /* Loop because there can be many parens and tuple
679 unpacking mixed in. */
680 ch = CHILD(ch, 0);
681 assert(TYPE(ch) == fpdef);
682 goto handle_fpdef;
683 }
684 }
685 if (TYPE(CHILD(ch, 0)) == NAME) {
686 expr_ty name;
687 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
688 ast_error(CHILD(ch, 0), "assignment to None");
689 goto error;
690 }
691 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
692 Param, LINENO(ch), ch->n_col_offset,
693 c->c_arena);
694 if (!name)
695 goto error;
696 asdl_seq_SET(args, k++, name);
697
698 }
699 i += 2; /* the name and the comma */
700 break;
701 case STAR:
702 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
703 ast_error(CHILD(n, i+1), "assignment to None");
704 goto error;
705 }
706 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
707 i += 3;
708 break;
709 case DOUBLESTAR:
710 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
711 ast_error(CHILD(n, i+1), "assignment to None");
712 goto error;
713 }
714 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
715 i += 3;
716 break;
717 default:
718 PyErr_Format(PyExc_SystemError,
719 "unexpected node in varargslist: %d @ %d",
720 TYPE(ch), i);
721 goto error;
722 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 }
724
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000725 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726
727 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000728 Py_XDECREF(vararg);
729 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730 return NULL;
731}
732
733static expr_ty
734ast_for_dotted_name(struct compiling *c, const node *n)
735{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000736 expr_ty e;
737 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000738 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 int i;
740
741 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000742
743 lineno = LINENO(n);
744 col_offset = n->n_col_offset;
745
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 id = NEW_IDENTIFIER(CHILD(n, 0));
747 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000748 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000749 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000751 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752
753 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000754 id = NEW_IDENTIFIER(CHILD(n, i));
755 if (!id)
756 return NULL;
757 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
758 if (!e)
759 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 }
761
762 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763}
764
765static expr_ty
766ast_for_decorator(struct compiling *c, const node *n)
767{
768 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
769 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000770 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771
772 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000773 REQ(CHILD(n, 0), AT);
774 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775
776 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
777 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000778 return NULL;
779
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000781 d = name_expr;
782 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 }
784 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000785 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
786 n->n_col_offset, c->c_arena);
787 if (!d)
788 return NULL;
789 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 }
791 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000792 d = ast_for_call(c, CHILD(n, 3), name_expr);
793 if (!d)
794 return NULL;
795 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 }
797
798 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799}
800
801static asdl_seq*
802ast_for_decorators(struct compiling *c, const node *n)
803{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000804 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000805 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 int i;
807
808 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000809 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000811 return NULL;
812
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000814 d = ast_for_decorator(c, CHILD(n, i));
815 if (!d)
816 return NULL;
817 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 }
819 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820}
821
822static stmt_ty
823ast_for_funcdef(struct compiling *c, const node *n)
824{
825 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000826 identifier name;
827 arguments_ty args;
828 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 asdl_seq *decorator_seq = NULL;
830 int name_i;
831
832 REQ(n, funcdef);
833
834 if (NCH(n) == 6) { /* decorators are present */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000835 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
836 if (!decorator_seq)
837 return NULL;
838 name_i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 }
840 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000841 name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 }
843
844 name = NEW_IDENTIFIER(CHILD(n, name_i));
845 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000846 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000848 ast_error(CHILD(n, name_i), "assignment to None");
849 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 }
851 args = ast_for_arguments(c, CHILD(n, name_i + 1));
852 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000853 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 body = ast_for_suite(c, CHILD(n, name_i + 3));
855 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000856 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000858 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000859 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860}
861
862static expr_ty
863ast_for_lambdef(struct compiling *c, const node *n)
864{
865 /* lambdef: 'lambda' [varargslist] ':' test */
866 arguments_ty args;
867 expr_ty expression;
868
869 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000870 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
871 if (!args)
872 return NULL;
873 expression = ast_for_expr(c, CHILD(n, 2));
874 if (!expression)
875 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 }
877 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000878 args = ast_for_arguments(c, CHILD(n, 1));
879 if (!args)
880 return NULL;
881 expression = ast_for_expr(c, CHILD(n, 3));
882 if (!expression)
883 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 }
885
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000886 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887}
888
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000889static expr_ty
890ast_for_ifexpr(struct compiling *c, const node *n)
891{
892 /* test: or_test 'if' or_test 'else' test */
893 expr_ty expression, body, orelse;
894
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000895 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000896 body = ast_for_expr(c, CHILD(n, 0));
897 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000898 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000899 expression = ast_for_expr(c, CHILD(n, 2));
900 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000901 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000902 orelse = ast_for_expr(c, CHILD(n, 4));
903 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000904 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000905 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000906 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000907}
908
Neal Norwitze4d4f002006-09-05 03:58:26 +0000909/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
910 so there is only a single version. Possibly for loops can also re-use
911 the code.
912*/
913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914/* Count the number of 'for' loop in a list comprehension.
915
916 Helper for ast_for_listcomp().
917*/
918
919static int
920count_list_fors(const node *n)
921{
922 int n_fors = 0;
923 node *ch = CHILD(n, 1);
924
925 count_list_for:
926 n_fors++;
927 REQ(ch, list_for);
928 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000929 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000931 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 count_list_iter:
933 REQ(ch, list_iter);
934 ch = CHILD(ch, 0);
935 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000936 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000938 if (NCH(ch) == 3) {
939 ch = CHILD(ch, 2);
940 goto count_list_iter;
941 }
942 else
943 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000945
946 /* Should never be reached */
947 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
948 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949}
950
951/* Count the number of 'if' statements in a list comprehension.
952
953 Helper for ast_for_listcomp().
954*/
955
956static int
957count_list_ifs(const node *n)
958{
959 int n_ifs = 0;
960
961 count_list_iter:
962 REQ(n, list_iter);
963 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000964 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 n = CHILD(n, 0);
966 REQ(n, list_if);
967 n_ifs++;
968 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000969 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 n = CHILD(n, 2);
971 goto count_list_iter;
972}
973
974static expr_ty
975ast_for_listcomp(struct compiling *c, const node *n)
976{
977 /* listmaker: test ( list_for | (',' test)* [','] )
978 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
979 list_iter: list_for | list_if
980 list_if: 'if' test [list_iter]
981 testlist_safe: test [(',' test)+ [',']]
982 */
983 expr_ty elt;
984 asdl_seq *listcomps;
985 int i, n_fors;
986 node *ch;
987
988 REQ(n, listmaker);
989 assert(NCH(n) > 1);
990
991 elt = ast_for_expr(c, CHILD(n, 0));
992 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994
995 n_fors = count_list_fors(n);
996 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000997 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000999 listcomps = asdl_seq_new(n_fors, c->c_arena);
1000 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001001 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001002
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 ch = CHILD(n, 1);
1004 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001005 comprehension_ty lc;
1006 asdl_seq *t;
1007 expr_ty expression;
1008 node *for_ch;
1009
1010 REQ(ch, list_for);
1011
1012 for_ch = CHILD(ch, 1);
1013 t = ast_for_exprlist(c, for_ch, Store);
1014 if (!t)
1015 return NULL;
1016 expression = ast_for_testlist(c, CHILD(ch, 3));
1017 if (!expression)
1018 return NULL;
1019
1020 /* Check the # of children rather than the length of t, since
1021 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1022 */
1023 if (NCH(for_ch) == 1)
1024 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1025 c->c_arena);
1026 else
1027 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1028 c->c_arena),
1029 expression, NULL, c->c_arena);
1030 if (!lc)
1031 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001033 if (NCH(ch) == 5) {
1034 int j, n_ifs;
1035 asdl_seq *ifs;
1036 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001038 ch = CHILD(ch, 4);
1039 n_ifs = count_list_ifs(ch);
1040 if (n_ifs == -1)
1041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001043 ifs = asdl_seq_new(n_ifs, c->c_arena);
1044 if (!ifs)
1045 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001047 for (j = 0; j < n_ifs; j++) {
1048 REQ(ch, list_iter);
1049 ch = CHILD(ch, 0);
1050 REQ(ch, list_if);
1051
1052 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1053 if (!list_for_expr)
1054 return NULL;
1055
1056 asdl_seq_SET(ifs, j, list_for_expr);
1057 if (NCH(ch) == 3)
1058 ch = CHILD(ch, 2);
1059 }
1060 /* on exit, must guarantee that ch is a list_for */
1061 if (TYPE(ch) == list_iter)
1062 ch = CHILD(ch, 0);
1063 lc->ifs = ifs;
1064 }
1065 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066 }
1067
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001068 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069}
1070
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001071/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072
1073 Helper for ast_for_genexp().
1074*/
1075
1076static int
1077count_gen_fors(const node *n)
1078{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001079 int n_fors = 0;
1080 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081
1082 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001083 n_fors++;
1084 REQ(ch, gen_for);
1085 if (NCH(ch) == 5)
1086 ch = CHILD(ch, 4);
1087 else
1088 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001090 REQ(ch, gen_iter);
1091 ch = CHILD(ch, 0);
1092 if (TYPE(ch) == gen_for)
1093 goto count_gen_for;
1094 else if (TYPE(ch) == gen_if) {
1095 if (NCH(ch) == 3) {
1096 ch = CHILD(ch, 2);
1097 goto count_gen_iter;
1098 }
1099 else
1100 return n_fors;
1101 }
1102
1103 /* Should never be reached */
1104 PyErr_SetString(PyExc_SystemError,
1105 "logic error in count_gen_fors");
1106 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107}
1108
1109/* Count the number of 'if' statements in a generator expression.
1110
1111 Helper for ast_for_genexp().
1112*/
1113
1114static int
1115count_gen_ifs(const node *n)
1116{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001117 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001119 while (1) {
1120 REQ(n, gen_iter);
1121 if (TYPE(CHILD(n, 0)) == gen_for)
1122 return n_ifs;
1123 n = CHILD(n, 0);
1124 REQ(n, gen_if);
1125 n_ifs++;
1126 if (NCH(n) == 2)
1127 return n_ifs;
1128 n = CHILD(n, 2);
1129 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130}
1131
Jeremy Hyltona8293132006-02-28 17:58:27 +00001132/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133static expr_ty
1134ast_for_genexp(struct compiling *c, const node *n)
1135{
1136 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001137 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 expr_ty elt;
1139 asdl_seq *genexps;
1140 int i, n_fors;
1141 node *ch;
1142
1143 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1144 assert(NCH(n) > 1);
1145
1146 elt = ast_for_expr(c, CHILD(n, 0));
1147 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001148 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149
1150 n_fors = count_gen_fors(n);
1151 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001152 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001153
1154 genexps = asdl_seq_new(n_fors, c->c_arena);
1155 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001156 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001157
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 ch = CHILD(n, 1);
1159 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001160 comprehension_ty ge;
1161 asdl_seq *t;
1162 expr_ty expression;
1163 node *for_ch;
1164
1165 REQ(ch, gen_for);
1166
1167 for_ch = CHILD(ch, 1);
1168 t = ast_for_exprlist(c, for_ch, Store);
1169 if (!t)
1170 return NULL;
1171 expression = ast_for_expr(c, CHILD(ch, 3));
1172 if (!expression)
1173 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001174
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001175 /* Check the # of children rather than the length of t, since
1176 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1177 if (NCH(for_ch) == 1)
1178 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1179 NULL, c->c_arena);
1180 else
1181 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1182 c->c_arena),
1183 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001184
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001185 if (!ge)
1186 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001188 if (NCH(ch) == 5) {
1189 int j, n_ifs;
1190 asdl_seq *ifs;
1191
1192 ch = CHILD(ch, 4);
1193 n_ifs = count_gen_ifs(ch);
1194 if (n_ifs == -1)
1195 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001196
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001197 ifs = asdl_seq_new(n_ifs, c->c_arena);
1198 if (!ifs)
1199 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001201 for (j = 0; j < n_ifs; j++) {
1202 REQ(ch, gen_iter);
1203 ch = CHILD(ch, 0);
1204 REQ(ch, gen_if);
1205
1206 expression = ast_for_expr(c, CHILD(ch, 1));
1207 if (!expression)
1208 return NULL;
1209 asdl_seq_SET(ifs, j, expression);
1210 if (NCH(ch) == 3)
1211 ch = CHILD(ch, 2);
1212 }
1213 /* on exit, must guarantee that ch is a gen_for */
1214 if (TYPE(ch) == gen_iter)
1215 ch = CHILD(ch, 0);
1216 ge->ifs = ifs;
1217 }
1218 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 }
1220
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001221 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222}
1223
1224static expr_ty
1225ast_for_atom(struct compiling *c, const node *n)
1226{
1227 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1228 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1229 */
1230 node *ch = CHILD(n, 0);
1231
1232 switch (TYPE(ch)) {
1233 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001234 /* All names start in Load context, but may later be
1235 changed. */
1236 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1237 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001239 PyObject *str = parsestrplus(c, n);
1240 if (!str)
1241 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001242
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001243 PyArena_AddPyObject(c->c_arena, str);
1244 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 }
1246 case NUMBER: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001247 PyObject *pynum = parsenumber(STR(ch));
1248 if (!pynum)
1249 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001250
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001251 PyArena_AddPyObject(c->c_arena, pynum);
1252 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 }
1254 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001255 ch = CHILD(n, 1);
1256
1257 if (TYPE(ch) == RPAR)
1258 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1259
1260 if (TYPE(ch) == yield_expr)
1261 return ast_for_expr(c, ch);
1262
1263 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1264 return ast_for_genexp(c, ch);
1265
1266 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001268 ch = CHILD(n, 1);
1269
1270 if (TYPE(ch) == RSQB)
1271 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1272
1273 REQ(ch, listmaker);
1274 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1275 asdl_seq *elts = seq_for_testlist(c, ch);
1276 if (!elts)
1277 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001278
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001279 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1280 }
1281 else
1282 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001284 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1285 int i, size;
1286 asdl_seq *keys, *values;
1287
1288 ch = CHILD(n, 1);
1289 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1290 keys = asdl_seq_new(size, c->c_arena);
1291 if (!keys)
1292 return NULL;
1293
1294 values = asdl_seq_new(size, c->c_arena);
1295 if (!values)
1296 return NULL;
1297
1298 for (i = 0; i < NCH(ch); i += 4) {
1299 expr_ty expression;
1300
1301 expression = ast_for_expr(c, CHILD(ch, i));
1302 if (!expression)
1303 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001304
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001305 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001306
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001307 expression = ast_for_expr(c, CHILD(ch, i + 2));
1308 if (!expression)
1309 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001310
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001311 asdl_seq_SET(values, i / 4, expression);
1312 }
1313 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 }
1315 case BACKQUOTE: { /* repr */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001316 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
1317 if (!expression)
1318 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001319
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001320 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 }
1322 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001323 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 }
1326}
1327
1328static slice_ty
1329ast_for_slice(struct compiling *c, const node *n)
1330{
1331 node *ch;
1332 expr_ty lower = NULL, upper = NULL, step = NULL;
1333
1334 REQ(n, subscript);
1335
1336 /*
1337 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1338 sliceop: ':' [test]
1339 */
1340 ch = CHILD(n, 0);
1341 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001342 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343
1344 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001345 /* 'step' variable hold no significance in terms of being used over
1346 other vars */
1347 step = ast_for_expr(c, ch);
1348 if (!step)
1349 return NULL;
1350
1351 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 }
1353
1354 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001355 lower = ast_for_expr(c, ch);
1356 if (!lower)
1357 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 }
1359
1360 /* If there's an upper bound it's in the second or third position. */
1361 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001362 if (NCH(n) > 1) {
1363 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001365 if (TYPE(n2) == test) {
1366 upper = ast_for_expr(c, n2);
1367 if (!upper)
1368 return NULL;
1369 }
1370 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001372 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001374 if (TYPE(n2) == test) {
1375 upper = ast_for_expr(c, n2);
1376 if (!upper)
1377 return NULL;
1378 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 }
1380
1381 ch = CHILD(n, NCH(n) - 1);
1382 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001383 if (NCH(ch) == 1) {
1384 /* No expression, so step is None */
1385 ch = CHILD(ch, 0);
1386 step = Name(new_identifier("None", c->c_arena), Load,
1387 LINENO(ch), ch->n_col_offset, c->c_arena);
1388 if (!step)
1389 return NULL;
1390 } else {
1391 ch = CHILD(ch, 1);
1392 if (TYPE(ch) == test) {
1393 step = ast_for_expr(c, ch);
1394 if (!step)
1395 return NULL;
1396 }
1397 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 }
1399
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001400 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401}
1402
1403static expr_ty
1404ast_for_binop(struct compiling *c, const node *n)
1405{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001406 /* Must account for a sequence of expressions.
1407 How should A op B op C by represented?
1408 BinOp(BinOp(A, op, B), op, C).
1409 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001411 int i, nops;
1412 expr_ty expr1, expr2, result;
1413 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001415 expr1 = ast_for_expr(c, CHILD(n, 0));
1416 if (!expr1)
1417 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001419 expr2 = ast_for_expr(c, CHILD(n, 2));
1420 if (!expr2)
1421 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001423 newoperator = get_operator(CHILD(n, 1));
1424 if (!newoperator)
1425 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001427 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1428 c->c_arena);
1429 if (!result)
1430 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001432 nops = (NCH(n) - 1) / 2;
1433 for (i = 1; i < nops; i++) {
1434 expr_ty tmp_result, tmp;
1435 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001437 newoperator = get_operator(next_oper);
1438 if (!newoperator)
1439 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001441 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1442 if (!tmp)
1443 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001445 tmp_result = BinOp(result, newoperator, tmp,
1446 LINENO(next_oper), next_oper->n_col_offset,
1447 c->c_arena);
1448 if (!tmp)
1449 return NULL;
1450 result = tmp_result;
1451 }
1452 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453}
1454
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001455static expr_ty
1456ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1457{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001458 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1459 subscriptlist: subscript (',' subscript)* [',']
1460 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1461 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001462 REQ(n, trailer);
1463 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001464 if (NCH(n) == 2)
1465 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1466 n->n_col_offset, c->c_arena);
1467 else
1468 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001469 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001470 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001471 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1472 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001473 }
1474 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001475 REQ(CHILD(n, 0), LSQB);
1476 REQ(CHILD(n, 2), RSQB);
1477 n = CHILD(n, 1);
1478 if (NCH(n) == 1) {
1479 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1480 if (!slc)
1481 return NULL;
1482 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1483 c->c_arena);
1484 }
1485 else {
1486 /* The grammar is ambiguous here. The ambiguity is resolved
1487 by treating the sequence as a tuple literal if there are
1488 no slice features.
1489 */
1490 int j;
1491 slice_ty slc;
1492 expr_ty e;
1493 bool simple = true;
1494 asdl_seq *slices, *elts;
1495 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1496 if (!slices)
1497 return NULL;
1498 for (j = 0; j < NCH(n); j += 2) {
1499 slc = ast_for_slice(c, CHILD(n, j));
1500 if (!slc)
1501 return NULL;
1502 if (slc->kind != Index_kind)
1503 simple = false;
1504 asdl_seq_SET(slices, j / 2, slc);
1505 }
1506 if (!simple) {
1507 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1508 Load, LINENO(n), n->n_col_offset, c->c_arena);
1509 }
1510 /* extract Index values and put them in a Tuple */
1511 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1512 if (!elts)
1513 return NULL;
1514 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1515 slc = (slice_ty)asdl_seq_GET(slices, j);
1516 assert(slc->kind == Index_kind && slc->v.Index.value);
1517 asdl_seq_SET(elts, j, slc->v.Index.value);
1518 }
1519 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1520 if (!e)
1521 return NULL;
1522 return Subscript(left_expr, Index(e, c->c_arena),
1523 Load, LINENO(n), n->n_col_offset, c->c_arena);
1524 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001525 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001526}
1527
1528static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001529ast_for_factor(struct compiling *c, const node *n)
1530{
1531 node *pfactor, *ppower, *patom, *pnum;
1532 expr_ty expression;
1533
1534 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001535 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001536 constant. The peephole optimizer already does something like
1537 this but it doesn't handle the case where the constant is
1538 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1539 PyLongObject.
1540 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001541 if (TYPE(CHILD(n, 0)) == MINUS &&
1542 NCH(n) == 2 &&
1543 TYPE((pfactor = CHILD(n, 1))) == factor &&
1544 NCH(pfactor) == 1 &&
1545 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1546 NCH(ppower) == 1 &&
1547 TYPE((patom = CHILD(ppower, 0))) == atom &&
1548 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1549 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1550 if (s == NULL)
1551 return NULL;
1552 s[0] = '-';
1553 strcpy(s + 1, STR(pnum));
1554 PyObject_FREE(STR(pnum));
1555 STR(pnum) = s;
1556 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001557 }
1558
1559 expression = ast_for_expr(c, CHILD(n, 1));
1560 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001561 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001562
1563 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001564 case PLUS:
1565 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1566 c->c_arena);
1567 case MINUS:
1568 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1569 c->c_arena);
1570 case TILDE:
1571 return UnaryOp(Invert, expression, LINENO(n),
1572 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001573 }
1574 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001575 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001576 return NULL;
1577}
1578
1579static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001580ast_for_power(struct compiling *c, const node *n)
1581{
1582 /* power: atom trailer* ('**' factor)*
1583 */
1584 int i;
1585 expr_ty e, tmp;
1586 REQ(n, power);
1587 e = ast_for_atom(c, CHILD(n, 0));
1588 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001589 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001590 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001591 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001592 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001593 node *ch = CHILD(n, i);
1594 if (TYPE(ch) != trailer)
1595 break;
1596 tmp = ast_for_trailer(c, ch, e);
1597 if (!tmp)
1598 return NULL;
1599 tmp->lineno = e->lineno;
1600 tmp->col_offset = e->col_offset;
1601 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001602 }
1603 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001604 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1605 if (!f)
1606 return NULL;
1607 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1608 if (!tmp)
1609 return NULL;
1610 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001611 }
1612 return e;
1613}
1614
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615/* Do not name a variable 'expr'! Will cause a compile error.
1616*/
1617
1618static expr_ty
1619ast_for_expr(struct compiling *c, const node *n)
1620{
1621 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001622 test: or_test ['if' or_test 'else' test] | lambdef
1623 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624 and_test: not_test ('and' not_test)*
1625 not_test: 'not' not_test | comparison
1626 comparison: expr (comp_op expr)*
1627 expr: xor_expr ('|' xor_expr)*
1628 xor_expr: and_expr ('^' and_expr)*
1629 and_expr: shift_expr ('&' shift_expr)*
1630 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1631 arith_expr: term (('+'|'-') term)*
1632 term: factor (('*'|'/'|'%'|'//') factor)*
1633 factor: ('+'|'-'|'~') factor | power
1634 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001635
1636 As well as modified versions that exist for backward compatibility,
1637 to explicitly allow:
1638 [ x for x in lambda: 0, lambda: 1 ]
1639 (which would be ambiguous without these extra rules)
1640
1641 old_test: or_test | old_lambdef
1642 old_lambdef: 'lambda' [vararglist] ':' old_test
1643
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644 */
1645
1646 asdl_seq *seq;
1647 int i;
1648
1649 loop:
1650 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001651 case test:
1652 case old_test:
1653 if (TYPE(CHILD(n, 0)) == lambdef ||
1654 TYPE(CHILD(n, 0)) == old_lambdef)
1655 return ast_for_lambdef(c, CHILD(n, 0));
1656 else if (NCH(n) > 1)
1657 return ast_for_ifexpr(c, n);
1658 /* Fallthrough */
1659 case or_test:
1660 case and_test:
1661 if (NCH(n) == 1) {
1662 n = CHILD(n, 0);
1663 goto loop;
1664 }
1665 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1666 if (!seq)
1667 return NULL;
1668 for (i = 0; i < NCH(n); i += 2) {
1669 expr_ty e = ast_for_expr(c, CHILD(n, i));
1670 if (!e)
1671 return NULL;
1672 asdl_seq_SET(seq, i / 2, e);
1673 }
1674 if (!strcmp(STR(CHILD(n, 1)), "and"))
1675 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1676 c->c_arena);
1677 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1678 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1679 case not_test:
1680 if (NCH(n) == 1) {
1681 n = CHILD(n, 0);
1682 goto loop;
1683 }
1684 else {
1685 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1686 if (!expression)
1687 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001689 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1690 c->c_arena);
1691 }
1692 case comparison:
1693 if (NCH(n) == 1) {
1694 n = CHILD(n, 0);
1695 goto loop;
1696 }
1697 else {
1698 expr_ty expression;
1699 asdl_int_seq *ops;
1700 asdl_seq *cmps;
1701 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1702 if (!ops)
1703 return NULL;
1704 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1705 if (!cmps) {
1706 return NULL;
1707 }
1708 for (i = 1; i < NCH(n); i += 2) {
1709 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001711 newoperator = ast_for_comp_op(CHILD(n, i));
1712 if (!newoperator) {
1713 return NULL;
1714 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001716 expression = ast_for_expr(c, CHILD(n, i + 1));
1717 if (!expression) {
1718 return NULL;
1719 }
1720
1721 asdl_seq_SET(ops, i / 2, newoperator);
1722 asdl_seq_SET(cmps, i / 2, expression);
1723 }
1724 expression = ast_for_expr(c, CHILD(n, 0));
1725 if (!expression) {
1726 return NULL;
1727 }
1728
1729 return Compare(expression, ops, cmps, LINENO(n),
1730 n->n_col_offset, c->c_arena);
1731 }
1732 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001734 /* The next five cases all handle BinOps. The main body of code
1735 is the same in each case, but the switch turned inside out to
1736 reuse the code for each type of operator.
1737 */
1738 case expr:
1739 case xor_expr:
1740 case and_expr:
1741 case shift_expr:
1742 case arith_expr:
1743 case term:
1744 if (NCH(n) == 1) {
1745 n = CHILD(n, 0);
1746 goto loop;
1747 }
1748 return ast_for_binop(c, n);
1749 case yield_expr: {
1750 expr_ty exp = NULL;
1751 if (NCH(n) == 2) {
1752 exp = ast_for_testlist(c, CHILD(n, 1));
1753 if (!exp)
1754 return NULL;
1755 }
1756 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1757 }
1758 case factor:
1759 if (NCH(n) == 1) {
1760 n = CHILD(n, 0);
1761 goto loop;
1762 }
1763 return ast_for_factor(c, n);
1764 case power:
1765 return ast_for_power(c, n);
1766 default:
1767 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001770 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 return NULL;
1772}
1773
1774static expr_ty
1775ast_for_call(struct compiling *c, const node *n, expr_ty func)
1776{
1777 /*
1778 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001779 | '**' test)
1780 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781 */
1782
1783 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001784 asdl_seq *args;
1785 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786 expr_ty vararg = NULL, kwarg = NULL;
1787
1788 REQ(n, arglist);
1789
1790 nargs = 0;
1791 nkeywords = 0;
1792 ngens = 0;
1793 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001794 node *ch = CHILD(n, i);
1795 if (TYPE(ch) == argument) {
1796 if (NCH(ch) == 1)
1797 nargs++;
1798 else if (TYPE(CHILD(ch, 1)) == gen_for)
1799 ngens++;
1800 else
1801 nkeywords++;
1802 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803 }
1804 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001805 ast_error(n, "Generator expression must be parenthesized "
1806 "if not sole argument");
1807 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 }
1809
1810 if (nargs + nkeywords + ngens > 255) {
1811 ast_error(n, "more than 255 arguments");
1812 return NULL;
1813 }
1814
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001815 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001817 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001818 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 nargs = 0;
1822 nkeywords = 0;
1823 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001824 node *ch = CHILD(n, i);
1825 if (TYPE(ch) == argument) {
1826 expr_ty e;
1827 if (NCH(ch) == 1) {
1828 if (nkeywords) {
1829 ast_error(CHILD(ch, 0),
1830 "non-keyword arg after keyword arg");
1831 return NULL;
1832 }
1833 e = ast_for_expr(c, CHILD(ch, 0));
1834 if (!e)
1835 return NULL;
1836 asdl_seq_SET(args, nargs++, e);
1837 }
1838 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1839 e = ast_for_genexp(c, ch);
1840 if (!e)
1841 return NULL;
1842 asdl_seq_SET(args, nargs++, e);
1843 }
1844 else {
1845 keyword_ty kw;
1846 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001848 /* CHILD(ch, 0) is test, but must be an identifier? */
1849 e = ast_for_expr(c, CHILD(ch, 0));
1850 if (!e)
1851 return NULL;
1852 /* f(lambda x: x[0] = 3) ends up getting parsed with
1853 * LHS test = lambda x: x[0], and RHS test = 3.
1854 * SF bug 132313 points out that complaining about a keyword
1855 * then is very confusing.
1856 */
1857 if (e->kind == Lambda_kind) {
1858 ast_error(CHILD(ch, 0),
1859 "lambda cannot contain assignment");
1860 return NULL;
1861 } else if (e->kind != Name_kind) {
1862 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1863 return NULL;
1864 }
1865 key = e->v.Name.id;
1866 e = ast_for_expr(c, CHILD(ch, 2));
1867 if (!e)
1868 return NULL;
1869 kw = keyword(key, e, c->c_arena);
1870 if (!kw)
1871 return NULL;
1872 asdl_seq_SET(keywords, nkeywords++, kw);
1873 }
1874 }
1875 else if (TYPE(ch) == STAR) {
1876 vararg = ast_for_expr(c, CHILD(n, i+1));
1877 i++;
1878 }
1879 else if (TYPE(ch) == DOUBLESTAR) {
1880 kwarg = ast_for_expr(c, CHILD(n, i+1));
1881 i++;
1882 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 }
1884
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001885 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1886 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887}
1888
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001890ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001892 /* testlist_gexp: test (',' test)* [','] */
1893 /* testlist: test (',' test)* [','] */
1894 /* testlist_safe: test (',' test)+ [','] */
1895 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001897 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001898 if (NCH(n) > 1)
1899 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001900 }
1901 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001902 assert(TYPE(n) == testlist ||
1903 TYPE(n) == testlist_safe ||
1904 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001905 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001907 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001909 asdl_seq *tmp = seq_for_testlist(c, n);
1910 if (!tmp)
1911 return NULL;
1912 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001914}
1915
1916static expr_ty
1917ast_for_testlist_gexp(struct compiling *c, const node* n)
1918{
1919 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1920 /* argument: test [ gen_for ] */
1921 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001922 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001923 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001924 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001925}
1926
1927/* like ast_for_testlist() but returns a sequence */
1928static asdl_seq*
1929ast_for_class_bases(struct compiling *c, const node* n)
1930{
1931 /* testlist: test (',' test)* [','] */
1932 assert(NCH(n) > 0);
1933 REQ(n, testlist);
1934 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001935 expr_ty base;
1936 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
1937 if (!bases)
1938 return NULL;
1939 base = ast_for_expr(c, CHILD(n, 0));
1940 if (!base)
1941 return NULL;
1942 asdl_seq_SET(bases, 0, base);
1943 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001944 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001945
1946 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947}
1948
1949static stmt_ty
1950ast_for_expr_stmt(struct compiling *c, const node *n)
1951{
1952 REQ(n, expr_stmt);
1953 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001954 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955 testlist: test (',' test)* [',']
1956 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001957 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 test: ... here starts the operator precendence dance
1959 */
1960
1961 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001962 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
1963 if (!e)
1964 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001966 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 }
1968 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001969 expr_ty expr1, expr2;
1970 operator_ty newoperator;
1971 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001973 expr1 = ast_for_testlist(c, ch);
1974 if (!expr1)
1975 return NULL;
1976 /* TODO(nas): Remove duplicated error checks (set_context does it) */
1977 switch (expr1->kind) {
1978 case GeneratorExp_kind:
1979 ast_error(ch, "augmented assignment to generator "
1980 "expression not possible");
1981 return NULL;
1982 case Yield_kind:
1983 ast_error(ch, "augmented assignment to yield "
1984 "expression not possible");
1985 return NULL;
1986 case Name_kind: {
1987 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1988 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1989 ast_error(ch, "assignment to None");
1990 return NULL;
1991 }
1992 break;
1993 }
1994 case Attribute_kind:
1995 case Subscript_kind:
1996 break;
1997 default:
1998 ast_error(ch, "illegal expression for augmented "
1999 "assignment");
2000 return NULL;
2001 }
2002 if(!set_context(expr1, Store, ch))
2003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002005 ch = CHILD(n, 2);
2006 if (TYPE(ch) == testlist)
2007 expr2 = ast_for_testlist(c, ch);
2008 else
2009 expr2 = ast_for_expr(c, ch);
2010 if (!expr2)
2011 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002013 newoperator = ast_for_augassign(CHILD(n, 1));
2014 if (!newoperator)
2015 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002017 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2018 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 }
2020 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002021 int i;
2022 asdl_seq *targets;
2023 node *value;
2024 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002026 /* a normal assignment */
2027 REQ(CHILD(n, 1), EQUAL);
2028 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2029 if (!targets)
2030 return NULL;
2031 for (i = 0; i < NCH(n) - 2; i += 2) {
2032 expr_ty e;
2033 node *ch = CHILD(n, i);
2034 if (TYPE(ch) == yield_expr) {
2035 ast_error(ch, "assignment to yield expression not possible");
2036 return NULL;
2037 }
2038 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002040 /* set context to assign */
2041 if (!e)
2042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002044 if (!set_context(e, Store, CHILD(n, i)))
2045 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002047 asdl_seq_SET(targets, i / 2, e);
2048 }
2049 value = CHILD(n, NCH(n) - 1);
2050 if (TYPE(value) == testlist)
2051 expression = ast_for_testlist(c, value);
2052 else
2053 expression = ast_for_expr(c, value);
2054 if (!expression)
2055 return NULL;
2056 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2057 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059}
2060
2061static stmt_ty
2062ast_for_print_stmt(struct compiling *c, const node *n)
2063{
2064 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002065 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 */
2067 expr_ty dest = NULL, expression;
2068 asdl_seq *seq;
2069 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002070 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071
2072 REQ(n, print_stmt);
2073 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002074 dest = ast_for_expr(c, CHILD(n, 2));
2075 if (!dest)
2076 return NULL;
2077 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002079 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002081 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002082 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002083 expression = ast_for_expr(c, CHILD(n, i));
2084 if (!expression)
2085 return NULL;
2086 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 }
2088 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002089 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090}
2091
2092static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002093ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094{
2095 asdl_seq *seq;
2096 int i;
2097 expr_ty e;
2098
2099 REQ(n, exprlist);
2100
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002101 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002103 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002105 e = ast_for_expr(c, CHILD(n, i));
2106 if (!e)
2107 return NULL;
2108 asdl_seq_SET(seq, i / 2, e);
2109 if (context && !set_context(e, context, CHILD(n, i)))
2110 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 }
2112 return seq;
2113}
2114
2115static stmt_ty
2116ast_for_del_stmt(struct compiling *c, const node *n)
2117{
2118 asdl_seq *expr_list;
2119
2120 /* del_stmt: 'del' exprlist */
2121 REQ(n, del_stmt);
2122
2123 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2124 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002125 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002126 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127}
2128
2129static stmt_ty
2130ast_for_flow_stmt(struct compiling *c, const node *n)
2131{
2132 /*
2133 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002134 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 break_stmt: 'break'
2136 continue_stmt: 'continue'
2137 return_stmt: 'return' [testlist]
2138 yield_stmt: yield_expr
2139 yield_expr: 'yield' testlist
2140 raise_stmt: 'raise' [test [',' test [',' test]]]
2141 */
2142 node *ch;
2143
2144 REQ(n, flow_stmt);
2145 ch = CHILD(n, 0);
2146 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002147 case break_stmt:
2148 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2149 case continue_stmt:
2150 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2151 case yield_stmt: { /* will reduce to yield_expr */
2152 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2153 if (!exp)
2154 return NULL;
2155 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2156 }
2157 case return_stmt:
2158 if (NCH(ch) == 1)
2159 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2160 else {
2161 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2162 if (!expression)
2163 return NULL;
2164 return Return(expression, LINENO(n), n->n_col_offset,
2165 c->c_arena);
2166 }
2167 case raise_stmt:
2168 if (NCH(ch) == 1)
2169 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2170 c->c_arena);
2171 else if (NCH(ch) == 2) {
2172 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2173 if (!expression)
2174 return NULL;
2175 return Raise(expression, NULL, NULL, LINENO(n),
2176 n->n_col_offset, c->c_arena);
2177 }
2178 else if (NCH(ch) == 4) {
2179 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002181 expr1 = ast_for_expr(c, CHILD(ch, 1));
2182 if (!expr1)
2183 return NULL;
2184 expr2 = ast_for_expr(c, CHILD(ch, 3));
2185 if (!expr2)
2186 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002188 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2189 c->c_arena);
2190 }
2191 else if (NCH(ch) == 6) {
2192 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002194 expr1 = ast_for_expr(c, CHILD(ch, 1));
2195 if (!expr1)
2196 return NULL;
2197 expr2 = ast_for_expr(c, CHILD(ch, 3));
2198 if (!expr2)
2199 return NULL;
2200 expr3 = ast_for_expr(c, CHILD(ch, 5));
2201 if (!expr3)
2202 return NULL;
2203
2204 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2205 c->c_arena);
2206 }
2207 default:
2208 PyErr_Format(PyExc_SystemError,
2209 "unexpected flow_stmt: %d", TYPE(ch));
2210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002212
2213 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2214 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215}
2216
2217static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002218alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219{
2220 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002221 import_as_name: NAME ['as' NAME]
2222 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 dotted_name: NAME ('.' NAME)*
2224 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002225 PyObject *str;
2226
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 loop:
2228 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002229 case import_as_name:
2230 str = NULL;
2231 if (NCH(n) == 3) {
2232 str = NEW_IDENTIFIER(CHILD(n, 2));
2233 }
2234 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2235 case dotted_as_name:
2236 if (NCH(n) == 1) {
2237 n = CHILD(n, 0);
2238 goto loop;
2239 }
2240 else {
2241 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2242 if (!a)
2243 return NULL;
2244 assert(!a->asname);
2245 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2246 return a;
2247 }
2248 break;
2249 case dotted_name:
2250 if (NCH(n) == 1)
2251 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2252 else {
2253 /* Create a string of the form "a.b.c" */
2254 int i;
2255 size_t len;
2256 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002258 len = 0;
2259 for (i = 0; i < NCH(n); i += 2)
2260 /* length of string plus one for the dot */
2261 len += strlen(STR(CHILD(n, i))) + 1;
2262 len--; /* the last name doesn't have a dot */
2263 str = PyString_FromStringAndSize(NULL, len);
2264 if (!str)
2265 return NULL;
2266 s = PyString_AS_STRING(str);
2267 if (!s)
2268 return NULL;
2269 for (i = 0; i < NCH(n); i += 2) {
2270 char *sch = STR(CHILD(n, i));
2271 strcpy(s, STR(CHILD(n, i)));
2272 s += strlen(sch);
2273 *s++ = '.';
2274 }
2275 --s;
2276 *s = '\0';
2277 PyString_InternInPlace(&str);
2278 PyArena_AddPyObject(c->c_arena, str);
2279 return alias(str, NULL, c->c_arena);
2280 }
2281 break;
2282 case STAR:
2283 str = PyString_InternFromString("*");
2284 PyArena_AddPyObject(c->c_arena, str);
2285 return alias(str, NULL, c->c_arena);
2286 default:
2287 PyErr_Format(PyExc_SystemError,
2288 "unexpected import name: %d", TYPE(n));
2289 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002291
2292 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 return NULL;
2294}
2295
2296static stmt_ty
2297ast_for_import_stmt(struct compiling *c, const node *n)
2298{
2299 /*
2300 import_stmt: import_name | import_from
2301 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002302 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002303 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002305 int lineno;
2306 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 int i;
2308 asdl_seq *aliases;
2309
2310 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002311 lineno = LINENO(n);
2312 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002314 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002315 n = CHILD(n, 1);
2316 REQ(n, dotted_as_names);
2317 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2318 if (!aliases)
2319 return NULL;
2320 for (i = 0; i < NCH(n); i += 2) {
2321 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2322 if (!import_alias)
2323 return NULL;
2324 asdl_seq_SET(aliases, i / 2, import_alias);
2325 }
2326 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002328 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002329 int n_children;
2330 int idx, ndots = 0;
2331 alias_ty mod = NULL;
2332 identifier modname;
2333
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002334 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002335 optional module name */
2336 for (idx = 1; idx < NCH(n); idx++) {
2337 if (TYPE(CHILD(n, idx)) == dotted_name) {
2338 mod = alias_for_import_name(c, CHILD(n, idx));
2339 idx++;
2340 break;
2341 } else if (TYPE(CHILD(n, idx)) != DOT) {
2342 break;
2343 }
2344 ndots++;
2345 }
2346 idx++; /* skip over the 'import' keyword */
2347 switch (TYPE(CHILD(n, idx))) {
2348 case STAR:
2349 /* from ... import * */
2350 n = CHILD(n, idx);
2351 n_children = 1;
2352 if (ndots) {
2353 ast_error(n, "'import *' not allowed with 'from .'");
2354 return NULL;
2355 }
2356 break;
2357 case LPAR:
2358 /* from ... import (x, y, z) */
2359 n = CHILD(n, idx + 1);
2360 n_children = NCH(n);
2361 break;
2362 case import_as_names:
2363 /* from ... import x, y, z */
2364 n = CHILD(n, idx);
2365 n_children = NCH(n);
2366 if (n_children % 2 == 0) {
2367 ast_error(n, "trailing comma not allowed without"
2368 " surrounding parentheses");
2369 return NULL;
2370 }
2371 break;
2372 default:
2373 ast_error(n, "Unexpected node-type in from-import");
2374 return NULL;
2375 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002377 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2378 if (!aliases)
2379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002381 /* handle "from ... import *" special b/c there's no children */
2382 if (TYPE(n) == STAR) {
2383 alias_ty import_alias = alias_for_import_name(c, n);
2384 if (!import_alias)
2385 return NULL;
2386 asdl_seq_SET(aliases, 0, import_alias);
2387 }
2388 else {
2389 for (i = 0; i < NCH(n); i += 2) {
2390 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2391 if (!import_alias)
2392 return NULL;
2393 asdl_seq_SET(aliases, i / 2, import_alias);
2394 }
2395 }
2396 if (mod != NULL)
2397 modname = mod->name;
2398 else
2399 modname = new_identifier("", c->c_arena);
2400 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2401 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 }
Neal Norwitz79792652005-11-14 04:25:03 +00002403 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002404 "unknown import statement: starts with command '%s'",
2405 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 return NULL;
2407}
2408
2409static stmt_ty
2410ast_for_global_stmt(struct compiling *c, const node *n)
2411{
2412 /* global_stmt: 'global' NAME (',' NAME)* */
2413 identifier name;
2414 asdl_seq *s;
2415 int i;
2416
2417 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002418 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002419 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002420 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002422 name = NEW_IDENTIFIER(CHILD(n, i));
2423 if (!name)
2424 return NULL;
2425 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002427 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428}
2429
2430static stmt_ty
2431ast_for_exec_stmt(struct compiling *c, const node *n)
2432{
2433 expr_ty expr1, globals = NULL, locals = NULL;
2434 int n_children = NCH(n);
2435 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002436 PyErr_Format(PyExc_SystemError,
2437 "poorly formed 'exec' statement: %d parts to statement",
2438 n_children);
2439 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 }
2441
2442 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2443 REQ(n, exec_stmt);
2444 expr1 = ast_for_expr(c, CHILD(n, 1));
2445 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002446 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002448 globals = ast_for_expr(c, CHILD(n, 3));
2449 if (!globals)
2450 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 }
2452 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002453 locals = ast_for_expr(c, CHILD(n, 5));
2454 if (!locals)
2455 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 }
2457
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002458 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2459 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460}
2461
2462static stmt_ty
2463ast_for_assert_stmt(struct compiling *c, const node *n)
2464{
2465 /* assert_stmt: 'assert' test [',' test] */
2466 REQ(n, assert_stmt);
2467 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002468 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2469 if (!expression)
2470 return NULL;
2471 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2472 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 }
2474 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002475 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002477 expr1 = ast_for_expr(c, CHILD(n, 1));
2478 if (!expr1)
2479 return NULL;
2480 expr2 = ast_for_expr(c, CHILD(n, 3));
2481 if (!expr2)
2482 return NULL;
2483
2484 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 }
Neal Norwitz79792652005-11-14 04:25:03 +00002486 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002487 "improper number of parts to 'assert' statement: %d",
2488 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 return NULL;
2490}
2491
2492static asdl_seq *
2493ast_for_suite(struct compiling *c, const node *n)
2494{
2495 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002496 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 stmt_ty s;
2498 int i, total, num, end, pos = 0;
2499 node *ch;
2500
2501 REQ(n, suite);
2502
2503 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002504 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002506 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002508 n = CHILD(n, 0);
2509 /* simple_stmt always ends with a NEWLINE,
2510 and may have a trailing SEMI
2511 */
2512 end = NCH(n) - 1;
2513 if (TYPE(CHILD(n, end - 1)) == SEMI)
2514 end--;
2515 /* loop by 2 to skip semi-colons */
2516 for (i = 0; i < end; i += 2) {
2517 ch = CHILD(n, i);
2518 s = ast_for_stmt(c, ch);
2519 if (!s)
2520 return NULL;
2521 asdl_seq_SET(seq, pos++, s);
2522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 }
2524 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002525 for (i = 2; i < (NCH(n) - 1); i++) {
2526 ch = CHILD(n, i);
2527 REQ(ch, stmt);
2528 num = num_stmts(ch);
2529 if (num == 1) {
2530 /* small_stmt or compound_stmt with only one child */
2531 s = ast_for_stmt(c, ch);
2532 if (!s)
2533 return NULL;
2534 asdl_seq_SET(seq, pos++, s);
2535 }
2536 else {
2537 int j;
2538 ch = CHILD(ch, 0);
2539 REQ(ch, simple_stmt);
2540 for (j = 0; j < NCH(ch); j += 2) {
2541 /* statement terminates with a semi-colon ';' */
2542 if (NCH(CHILD(ch, j)) == 0) {
2543 assert((j + 1) == NCH(ch));
2544 break;
2545 }
2546 s = ast_for_stmt(c, CHILD(ch, j));
2547 if (!s)
2548 return NULL;
2549 asdl_seq_SET(seq, pos++, s);
2550 }
2551 }
2552 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 }
2554 assert(pos == seq->size);
2555 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556}
2557
2558static stmt_ty
2559ast_for_if_stmt(struct compiling *c, const node *n)
2560{
2561 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2562 ['else' ':' suite]
2563 */
2564 char *s;
2565
2566 REQ(n, if_stmt);
2567
2568 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002569 expr_ty expression;
2570 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002572 expression = ast_for_expr(c, CHILD(n, 1));
2573 if (!expression)
2574 return NULL;
2575 suite_seq = ast_for_suite(c, CHILD(n, 3));
2576 if (!suite_seq)
2577 return NULL;
2578
2579 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2580 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 s = STR(CHILD(n, 4));
2584 /* s[2], the third character in the string, will be
2585 's' for el_s_e, or
2586 'i' for el_i_f
2587 */
2588 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002589 expr_ty expression;
2590 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002592 expression = ast_for_expr(c, CHILD(n, 1));
2593 if (!expression)
2594 return NULL;
2595 seq1 = ast_for_suite(c, CHILD(n, 3));
2596 if (!seq1)
2597 return NULL;
2598 seq2 = ast_for_suite(c, CHILD(n, 6));
2599 if (!seq2)
2600 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002602 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2603 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 }
2605 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002606 int i, n_elif, has_else = 0;
2607 expr_ty expression;
2608 asdl_seq *suite_seq;
2609 asdl_seq *orelse = NULL;
2610 n_elif = NCH(n) - 4;
2611 /* must reference the child n_elif+1 since 'else' token is third,
2612 not fourth, child from the end. */
2613 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2614 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2615 has_else = 1;
2616 n_elif -= 3;
2617 }
2618 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002620 if (has_else) {
2621 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002623 orelse = asdl_seq_new(1, c->c_arena);
2624 if (!orelse)
2625 return NULL;
2626 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2627 if (!expression)
2628 return NULL;
2629 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2630 if (!suite_seq)
2631 return NULL;
2632 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2633 if (!suite_seq2)
2634 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002636 asdl_seq_SET(orelse, 0,
2637 If(expression, suite_seq, suite_seq2,
2638 LINENO(CHILD(n, NCH(n) - 6)),
2639 CHILD(n, NCH(n) - 6)->n_col_offset,
2640 c->c_arena));
2641 /* the just-created orelse handled the last elif */
2642 n_elif--;
2643 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002645 for (i = 0; i < n_elif; i++) {
2646 int off = 5 + (n_elif - i - 1) * 4;
2647 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2648 if (!newobj)
2649 return NULL;
2650 expression = ast_for_expr(c, CHILD(n, off));
2651 if (!expression)
2652 return NULL;
2653 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2654 if (!suite_seq)
2655 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002657 asdl_seq_SET(newobj, 0,
2658 If(expression, suite_seq, orelse,
2659 LINENO(CHILD(n, off)),
2660 CHILD(n, off)->n_col_offset, c->c_arena));
2661 orelse = newobj;
2662 }
2663 expression = ast_for_expr(c, CHILD(n, 1));
2664 if (!expression)
2665 return NULL;
2666 suite_seq = ast_for_suite(c, CHILD(n, 3));
2667 if (!suite_seq)
2668 return NULL;
2669 return If(expression, suite_seq, orelse,
2670 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002672
2673 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002674 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002675 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676}
2677
2678static stmt_ty
2679ast_for_while_stmt(struct compiling *c, const node *n)
2680{
2681 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2682 REQ(n, while_stmt);
2683
2684 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002685 expr_ty expression;
2686 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002688 expression = ast_for_expr(c, CHILD(n, 1));
2689 if (!expression)
2690 return NULL;
2691 suite_seq = ast_for_suite(c, CHILD(n, 3));
2692 if (!suite_seq)
2693 return NULL;
2694 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2695 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 }
2697 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002698 expr_ty expression;
2699 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002701 expression = ast_for_expr(c, CHILD(n, 1));
2702 if (!expression)
2703 return NULL;
2704 seq1 = ast_for_suite(c, CHILD(n, 3));
2705 if (!seq1)
2706 return NULL;
2707 seq2 = ast_for_suite(c, CHILD(n, 6));
2708 if (!seq2)
2709 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002711 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2712 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002714
2715 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002716 "wrong number of tokens for 'while' statement: %d",
2717 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002718 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719}
2720
2721static stmt_ty
2722ast_for_for_stmt(struct compiling *c, const node *n)
2723{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002724 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 expr_ty expression;
2726 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002727 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2729 REQ(n, for_stmt);
2730
2731 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002732 seq = ast_for_suite(c, CHILD(n, 8));
2733 if (!seq)
2734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 }
2736
Neal Norwitzedef2be2006-07-12 05:26:17 +00002737 node_target = CHILD(n, 1);
2738 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002739 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002740 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002741 /* Check the # of children rather than the length of _target, since
2742 for x, in ... has 1 element in _target, but still requires a Tuple. */
2743 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002744 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002746 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002748 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002749 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002750 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002752 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002753 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002755 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002756 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757}
2758
2759static excepthandler_ty
2760ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2761{
2762 /* except_clause: 'except' [test [',' test]] */
2763 REQ(exc, except_clause);
2764 REQ(body, suite);
2765
2766 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002767 asdl_seq *suite_seq = ast_for_suite(c, body);
2768 if (!suite_seq)
2769 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002771 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2772 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 }
2774 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002775 expr_ty expression;
2776 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002778 expression = ast_for_expr(c, CHILD(exc, 1));
2779 if (!expression)
2780 return NULL;
2781 suite_seq = ast_for_suite(c, body);
2782 if (!suite_seq)
2783 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002785 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2786 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787 }
2788 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002789 asdl_seq *suite_seq;
2790 expr_ty expression;
2791 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2792 if (!e)
2793 return NULL;
2794 if (!set_context(e, Store, CHILD(exc, 3)))
2795 return NULL;
2796 expression = ast_for_expr(c, CHILD(exc, 1));
2797 if (!expression)
2798 return NULL;
2799 suite_seq = ast_for_suite(c, body);
2800 if (!suite_seq)
2801 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002803 return excepthandler(expression, e, suite_seq, LINENO(exc),
2804 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002806
2807 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002808 "wrong number of children for 'except' clause: %d",
2809 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002810 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811}
2812
2813static stmt_ty
2814ast_for_try_stmt(struct compiling *c, const node *n)
2815{
Neal Norwitzf599f422005-12-17 21:33:47 +00002816 const int nch = NCH(n);
2817 int n_except = (nch - 3)/3;
2818 asdl_seq *body, *orelse = NULL, *finally = NULL;
2819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 REQ(n, try_stmt);
2821
Neal Norwitzf599f422005-12-17 21:33:47 +00002822 body = ast_for_suite(c, CHILD(n, 2));
2823 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002824 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825
Neal Norwitzf599f422005-12-17 21:33:47 +00002826 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002827 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2828 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2829 /* we can assume it's an "else",
2830 because nch >= 9 for try-else-finally and
2831 it would otherwise have a type of except_clause */
2832 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2833 if (orelse == NULL)
2834 return NULL;
2835 n_except--;
2836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002838 finally = ast_for_suite(c, CHILD(n, nch - 1));
2839 if (finally == NULL)
2840 return NULL;
2841 n_except--;
2842 }
2843 else {
2844 /* we can assume it's an "else",
2845 otherwise it would have a type of except_clause */
2846 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2847 if (orelse == NULL)
2848 return NULL;
2849 n_except--;
2850 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002852 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002853 ast_error(n, "malformed 'try' statement");
2854 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002856
2857 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002858 int i;
2859 stmt_ty except_st;
2860 /* process except statements to create a try ... except */
2861 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2862 if (handlers == NULL)
2863 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002864
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002865 for (i = 0; i < n_except; i++) {
2866 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2867 CHILD(n, 5 + i * 3));
2868 if (!e)
2869 return NULL;
2870 asdl_seq_SET(handlers, i, e);
2871 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002872
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002873 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2874 n->n_col_offset, c->c_arena);
2875 if (!finally)
2876 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002877
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002878 /* if a 'finally' is present too, we nest the TryExcept within a
2879 TryFinally to emulate try ... except ... finally */
2880 body = asdl_seq_new(1, c->c_arena);
2881 if (body == NULL)
2882 return NULL;
2883 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002884 }
2885
2886 /* must be a try ... finally (except clauses are in body, if any exist) */
2887 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002888 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889}
2890
Guido van Rossumc2e20742006-02-27 22:32:47 +00002891static expr_ty
2892ast_for_with_var(struct compiling *c, const node *n)
2893{
2894 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002895 return ast_for_expr(c, CHILD(n, 1));
2896}
2897
2898/* with_stmt: 'with' test [ with_var ] ':' suite */
2899static stmt_ty
2900ast_for_with_stmt(struct compiling *c, const node *n)
2901{
2902 expr_ty context_expr, optional_vars = NULL;
2903 int suite_index = 3; /* skip 'with', test, and ':' */
2904 asdl_seq *suite_seq;
2905
2906 assert(TYPE(n) == with_stmt);
2907 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002908 if (!context_expr)
2909 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002910 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002911 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002912
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002913 if (!optional_vars) {
2914 return NULL;
2915 }
2916 if (!set_context(optional_vars, Store, n)) {
2917 return NULL;
2918 }
2919 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002920 }
2921
2922 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2923 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002924 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002925 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002926 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002927 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002928}
2929
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930static stmt_ty
2931ast_for_classdef(struct compiling *c, const node *n)
2932{
2933 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934 asdl_seq *bases, *s;
2935
2936 REQ(n, classdef);
2937
2938 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002939 ast_error(n, "assignment to None");
2940 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002941 }
2942
2943 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002944 s = ast_for_suite(c, CHILD(n, 3));
2945 if (!s)
2946 return NULL;
2947 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2948 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 }
2950 /* check for empty base list */
2951 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002952 s = ast_for_suite(c, CHILD(n,5));
2953 if (!s)
2954 return NULL;
2955 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2956 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 }
2958
2959 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002960 bases = ast_for_class_bases(c, CHILD(n, 3));
2961 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002962 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963
2964 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002965 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002966 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002967 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002968 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969}
2970
2971static stmt_ty
2972ast_for_stmt(struct compiling *c, const node *n)
2973{
2974 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002975 assert(NCH(n) == 1);
2976 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 }
2978 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002979 assert(num_stmts(n) == 1);
2980 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981 }
2982 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002983 REQ(n, small_stmt);
2984 n = CHILD(n, 0);
2985 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2986 | flow_stmt | import_stmt | global_stmt | exec_stmt
2987 | assert_stmt
2988 */
2989 switch (TYPE(n)) {
2990 case expr_stmt:
2991 return ast_for_expr_stmt(c, n);
2992 case print_stmt:
2993 return ast_for_print_stmt(c, n);
2994 case del_stmt:
2995 return ast_for_del_stmt(c, n);
2996 case pass_stmt:
2997 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
2998 case flow_stmt:
2999 return ast_for_flow_stmt(c, n);
3000 case import_stmt:
3001 return ast_for_import_stmt(c, n);
3002 case global_stmt:
3003 return ast_for_global_stmt(c, n);
3004 case exec_stmt:
3005 return ast_for_exec_stmt(c, n);
3006 case assert_stmt:
3007 return ast_for_assert_stmt(c, n);
3008 default:
3009 PyErr_Format(PyExc_SystemError,
3010 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3011 TYPE(n), NCH(n));
3012 return NULL;
3013 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 }
3015 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003016 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3017 | funcdef | classdef
3018 */
3019 node *ch = CHILD(n, 0);
3020 REQ(n, compound_stmt);
3021 switch (TYPE(ch)) {
3022 case if_stmt:
3023 return ast_for_if_stmt(c, ch);
3024 case while_stmt:
3025 return ast_for_while_stmt(c, ch);
3026 case for_stmt:
3027 return ast_for_for_stmt(c, ch);
3028 case try_stmt:
3029 return ast_for_try_stmt(c, ch);
3030 case with_stmt:
3031 return ast_for_with_stmt(c, ch);
3032 case funcdef:
3033 return ast_for_funcdef(c, ch);
3034 case classdef:
3035 return ast_for_classdef(c, ch);
3036 default:
3037 PyErr_Format(PyExc_SystemError,
3038 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3039 TYPE(n), NCH(n));
3040 return NULL;
3041 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 }
3043}
3044
3045static PyObject *
3046parsenumber(const char *s)
3047{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003048 const char *end;
3049 long x;
3050 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003052 Py_complex c;
3053 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054#endif
3055
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003056 errno = 0;
3057 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003059 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003061 if (*end == 'l' || *end == 'L')
3062 return PyLong_FromString((char *)s, (char **)0, 0);
3063 if (s[0] == '0') {
3064 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3065 if (x < 0 && errno == 0) {
3066 return PyLong_FromString((char *)s,
3067 (char **)0,
3068 0);
3069 }
3070 }
3071 else
3072 x = PyOS_strtol((char *)s, (char **)&end, 0);
3073 if (*end == '\0') {
3074 if (errno != 0)
3075 return PyLong_FromString((char *)s, (char **)0, 0);
3076 return PyInt_FromLong(x);
3077 }
3078 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003080 if (imflag) {
3081 c.real = 0.;
3082 PyFPE_START_PROTECT("atof", return 0)
3083 c.imag = PyOS_ascii_atof(s);
3084 PyFPE_END_PROTECT(c)
3085 return PyComplex_FromCComplex(c);
3086 }
3087 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003089 {
3090 PyFPE_START_PROTECT("atof", return 0)
3091 dx = PyOS_ascii_atof(s);
3092 PyFPE_END_PROTECT(dx)
3093 return PyFloat_FromDouble(dx);
3094 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095}
3096
3097static PyObject *
3098decode_utf8(const char **sPtr, const char *end, char* encoding)
3099{
3100#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003101 Py_FatalError("decode_utf8 should not be called in this build.");
3102 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003104 PyObject *u, *v;
3105 char *s, *t;
3106 t = s = (char *)*sPtr;
3107 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3108 while (s < end && (*s & 0x80)) s++;
3109 *sPtr = s;
3110 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3111 if (u == NULL)
3112 return NULL;
3113 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3114 Py_DECREF(u);
3115 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116#endif
3117}
3118
3119static PyObject *
3120decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3121{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003122 PyObject *v, *u;
3123 char *buf;
3124 char *p;
3125 const char *end;
3126 if (encoding == NULL) {
3127 buf = (char *)s;
3128 u = NULL;
3129 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3130 buf = (char *)s;
3131 u = NULL;
3132 } else {
3133 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3134 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3135 if (u == NULL)
3136 return NULL;
3137 p = buf = PyString_AsString(u);
3138 end = s + len;
3139 while (s < end) {
3140 if (*s == '\\') {
3141 *p++ = *s++;
3142 if (*s & 0x80) {
3143 strcpy(p, "u005c");
3144 p += 5;
3145 }
3146 }
3147 if (*s & 0x80) { /* XXX inefficient */
3148 PyObject *w;
3149 char *r;
3150 Py_ssize_t rn, i;
3151 w = decode_utf8(&s, end, "utf-16-be");
3152 if (w == NULL) {
3153 Py_DECREF(u);
3154 return NULL;
3155 }
3156 r = PyString_AsString(w);
3157 rn = PyString_Size(w);
3158 assert(rn % 2 == 0);
3159 for (i = 0; i < rn; i += 2) {
3160 sprintf(p, "\\u%02x%02x",
3161 r[i + 0] & 0xFF,
3162 r[i + 1] & 0xFF);
3163 p += 6;
3164 }
3165 Py_DECREF(w);
3166 } else {
3167 *p++ = *s++;
3168 }
3169 }
3170 len = p - buf;
3171 s = buf;
3172 }
3173 if (rawmode)
3174 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3175 else
3176 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3177 Py_XDECREF(u);
3178 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179}
3180
3181/* s is a Python string literal, including the bracketing quote characters,
3182 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3183 * parsestr parses it, and returns the decoded Python string object.
3184 */
3185static PyObject *
3186parsestr(const char *s, const char *encoding)
3187{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003188 size_t len;
3189 int quote = Py_CHARMASK(*s);
3190 int rawmode = 0;
3191 int need_encoding;
3192 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003194 if (isalpha(quote) || quote == '_') {
3195 if (quote == 'u' || quote == 'U') {
3196 quote = *++s;
3197 unicode = 1;
3198 }
3199 if (quote == 'r' || quote == 'R') {
3200 quote = *++s;
3201 rawmode = 1;
3202 }
3203 }
3204 if (quote != '\'' && quote != '\"') {
3205 PyErr_BadInternalCall();
3206 return NULL;
3207 }
3208 s++;
3209 len = strlen(s);
3210 if (len > INT_MAX) {
3211 PyErr_SetString(PyExc_OverflowError,
3212 "string to parse is too long");
3213 return NULL;
3214 }
3215 if (s[--len] != quote) {
3216 PyErr_BadInternalCall();
3217 return NULL;
3218 }
3219 if (len >= 4 && s[0] == quote && s[1] == quote) {
3220 s += 2;
3221 len -= 2;
3222 if (s[--len] != quote || s[--len] != quote) {
3223 PyErr_BadInternalCall();
3224 return NULL;
3225 }
3226 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003228 if (unicode || Py_UnicodeFlag) {
3229 return decode_unicode(s, len, rawmode, encoding);
3230 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003232 need_encoding = (encoding != NULL &&
3233 strcmp(encoding, "utf-8") != 0 &&
3234 strcmp(encoding, "iso-8859-1") != 0);
3235 if (rawmode || strchr(s, '\\') == NULL) {
3236 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003238 /* This should not happen - we never see any other
3239 encoding. */
3240 Py_FatalError(
3241 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003243 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3244 if (u == NULL)
3245 return NULL;
3246 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3247 Py_DECREF(u);
3248 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003250 } else {
3251 return PyString_FromStringAndSize(s, len);
3252 }
3253 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003255 return PyString_DecodeEscape(s, len, NULL, unicode,
3256 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257}
3258
3259/* Build a Python string object out of a STRING atom. This takes care of
3260 * compile-time literal catenation, calling parsestr() on each piece, and
3261 * pasting the intermediate results together.
3262 */
3263static PyObject *
3264parsestrplus(struct compiling *c, const node *n)
3265{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003266 PyObject *v;
3267 int i;
3268 REQ(CHILD(n, 0), STRING);
3269 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3270 /* String literal concatenation */
3271 for (i = 1; i < NCH(n); i++) {
3272 PyObject *s;
3273 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3274 if (s == NULL)
3275 goto onError;
3276 if (PyString_Check(v) && PyString_Check(s)) {
3277 PyString_ConcatAndDel(&v, s);
3278 if (v == NULL)
3279 goto onError;
3280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003282 else {
3283 PyObject *temp = PyUnicode_Concat(v, s);
3284 Py_DECREF(s);
3285 Py_DECREF(v);
3286 v = temp;
3287 if (v == NULL)
3288 goto onError;
3289 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003291 }
3292 }
3293 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294
3295 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003296 Py_XDECREF(v);
3297 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298}