blob: a3fdd8998d1b7273264390483c8c4e4ae2d1abe0 [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 */
Christian Heimes3c608332008-03-26 22:01:37 +000021 int c_future_unicode; /* __future__ unicode literals flag */
Neal Norwitzadb69fc2005-12-17 20:54:49 +000022 PyArena *c_arena; /* arena for allocating memeory */
Christian Heimesffcd1e12007-11-24 01:36:02 +000023 const char *c_filename; /* filename */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024};
25
26static asdl_seq *seq_for_testlist(struct compiling *, const node *);
27static expr_ty ast_for_expr(struct compiling *, const node *);
28static stmt_ty ast_for_stmt(struct compiling *, const node *);
29static asdl_seq *ast_for_suite(struct compiling *, const node *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000030static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
31 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000032static expr_ty ast_for_testlist(struct compiling *, const node *);
Christian Heimes5224d282008-02-23 15:01:05 +000033static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000034static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035
36/* Note different signature for ast_for_call */
37static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
38
Benjamin Peterson2b30ea02008-05-03 15:56:42 +000039static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes3c608332008-03-26 22:01:37 +000040static PyObject *parsestr(struct compiling *, const char *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041static PyObject *parsestrplus(struct compiling *, const node *n);
42
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000044#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#endif
46
Neal Norwitzadb69fc2005-12-17 20:54:49 +000047static identifier
48new_identifier(const char* n, PyArena *arena) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000049 PyObject* id = PyString_InternFromString(n);
Benjamin Peterson5cb67632008-11-25 04:00:37 +000050 if (id != NULL)
51 PyArena_AddPyObject(arena, id);
Neal Norwitzadb69fc2005-12-17 20:54:49 +000052 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053}
54
Neal Norwitzadb69fc2005-12-17 20:54:49 +000055#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056
57/* This routine provides an invalid object for the syntax error.
58 The outermost routine must unpack this error and create the
59 proper object. We do this so that we don't have to pass
60 the filename to everything function.
61
62 XXX Maybe we should just pass the filename...
63*/
64
65static int
66ast_error(const node *n, const char *errstr)
67{
68 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
69 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000070 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000071 PyErr_SetObject(PyExc_SyntaxError, u);
72 Py_DECREF(u);
73 return 0;
74}
75
76static void
77ast_error_finish(const char *filename)
78{
79 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000080 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
82 assert(PyErr_Occurred());
83 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000084 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085
86 PyErr_Fetch(&type, &value, &tback);
87 errstr = PyTuple_GetItem(value, 0);
88 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000089 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000090 Py_INCREF(errstr);
91 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000092 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000093 Py_DECREF(errstr);
94 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000095 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096 Py_DECREF(value);
97
98 loc = PyErr_ProgramText(filename, lineno);
99 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000100 Py_INCREF(Py_None);
101 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000103 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000105 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000106 Py_DECREF(errstr);
107 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000108 }
Georg Brandl7784f122006-05-26 20:04:44 +0000109 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 Py_DECREF(errstr);
111 Py_DECREF(tmp);
112 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000113 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114 PyErr_Restore(type, value, tback);
115}
116
Benjamin Petersoncbd78132008-06-08 15:45:23 +0000117static int
118ast_warn(struct compiling *c, const node *n, char *msg)
119{
120 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),
121 NULL, NULL) < 0) {
122 /* if -Werr, change it to a SyntaxError */
123 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))
124 ast_error(n, msg);
125 return 0;
126 }
127 return 1;
128}
129
Benjamin Petersond5efd202008-06-08 22:52:37 +0000130static int
131forbidden_check(struct compiling *c, const node *n, const char *x)
132{
133 if (!strcmp(x, "None"))
134 return ast_error(n, "assignment to None");
135 if (Py_Py3kWarningFlag && !(strcmp(x, "True") && strcmp(x, "False")) &&
136 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
137 return 0;
138 return 1;
139}
140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141/* num_stmts() returns number of contained statements.
142
143 Use this routine to determine how big a sequence is needed for
144 the statements in a parse tree. Its raison d'etre is this bit of
145 grammar:
146
147 stmt: simple_stmt | compound_stmt
148 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
149
150 A simple_stmt can contain multiple small_stmt elements joined
151 by semicolons. If the arg is a simple_stmt, the number of
152 small_stmt elements is returned.
153*/
154
155static int
156num_stmts(const node *n)
157{
158 int i, l;
159 node *ch;
160
161 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000162 case single_input:
163 if (TYPE(CHILD(n, 0)) == NEWLINE)
164 return 0;
165 else
166 return num_stmts(CHILD(n, 0));
167 case file_input:
168 l = 0;
169 for (i = 0; i < NCH(n); i++) {
170 ch = CHILD(n, i);
171 if (TYPE(ch) == stmt)
172 l += num_stmts(ch);
173 }
174 return l;
175 case stmt:
176 return num_stmts(CHILD(n, 0));
177 case compound_stmt:
178 return 1;
179 case simple_stmt:
180 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
181 case suite:
182 if (NCH(n) == 1)
183 return num_stmts(CHILD(n, 0));
184 else {
185 l = 0;
186 for (i = 2; i < (NCH(n) - 1); i++)
187 l += num_stmts(CHILD(n, i));
188 return l;
189 }
190 default: {
191 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000193 sprintf(buf, "Non-statement found: %d %d\n",
194 TYPE(n), NCH(n));
195 Py_FatalError(buf);
196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 }
198 assert(0);
199 return 0;
200}
201
202/* Transform the CST rooted at node * to the appropriate AST
203*/
204
205mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000206PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000207 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000209 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 asdl_seq *stmts = NULL;
211 stmt_ty s;
212 node *ch;
213 struct compiling c;
214
215 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000216 c.c_encoding = "utf-8";
217 if (TYPE(n) == encoding_decl) {
218 ast_error(n, "encoding declaration in Unicode string");
219 goto error;
220 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000222 c.c_encoding = STR(n);
223 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000225 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 }
Christian Heimes3c608332008-03-26 22:01:37 +0000227 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000228 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000229 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230
Jeremy Hyltona8293132006-02-28 17:58:27 +0000231 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000233 case file_input:
234 stmts = asdl_seq_new(num_stmts(n), arena);
235 if (!stmts)
236 return NULL;
237 for (i = 0; i < NCH(n) - 1; i++) {
238 ch = CHILD(n, i);
239 if (TYPE(ch) == NEWLINE)
240 continue;
241 REQ(ch, stmt);
242 num = num_stmts(ch);
243 if (num == 1) {
244 s = ast_for_stmt(&c, ch);
245 if (!s)
246 goto error;
247 asdl_seq_SET(stmts, k++, s);
248 }
249 else {
250 ch = CHILD(ch, 0);
251 REQ(ch, simple_stmt);
252 for (j = 0; j < num; j++) {
253 s = ast_for_stmt(&c, CHILD(ch, j * 2));
254 if (!s)
255 goto error;
256 asdl_seq_SET(stmts, k++, s);
257 }
258 }
259 }
260 return Module(stmts, arena);
261 case eval_input: {
262 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000264 /* XXX Why not gen_for here? */
265 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
266 if (!testlist_ast)
267 goto error;
268 return Expression(testlist_ast, arena);
269 }
270 case single_input:
271 if (TYPE(CHILD(n, 0)) == NEWLINE) {
272 stmts = asdl_seq_new(1, arena);
273 if (!stmts)
274 goto error;
275 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
276 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000277 if (!asdl_seq_GET(stmts, 0))
278 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000279 return Interactive(stmts, arena);
280 }
281 else {
282 n = CHILD(n, 0);
283 num = num_stmts(n);
284 stmts = asdl_seq_new(num, arena);
285 if (!stmts)
286 goto error;
287 if (num == 1) {
288 s = ast_for_stmt(&c, n);
289 if (!s)
290 goto error;
291 asdl_seq_SET(stmts, 0, s);
292 }
293 else {
294 /* Only a simple_stmt can contain multiple statements. */
295 REQ(n, simple_stmt);
296 for (i = 0; i < NCH(n); i += 2) {
297 if (TYPE(CHILD(n, i)) == NEWLINE)
298 break;
299 s = ast_for_stmt(&c, CHILD(n, i));
300 if (!s)
301 goto error;
302 asdl_seq_SET(stmts, i / 2, s);
303 }
304 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000306 return Interactive(stmts, arena);
307 }
308 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000309 PyErr_Format(PyExc_SystemError,
310 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000311 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312 }
313 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314 ast_error_finish(filename);
315 return NULL;
316}
317
318/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
319*/
320
321static operator_ty
322get_operator(const node *n)
323{
324 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000325 case VBAR:
326 return BitOr;
327 case CIRCUMFLEX:
328 return BitXor;
329 case AMPER:
330 return BitAnd;
331 case LEFTSHIFT:
332 return LShift;
333 case RIGHTSHIFT:
334 return RShift;
335 case PLUS:
336 return Add;
337 case MINUS:
338 return Sub;
339 case STAR:
340 return Mult;
341 case SLASH:
342 return Div;
343 case DOUBLESLASH:
344 return FloorDiv;
345 case PERCENT:
346 return Mod;
347 default:
348 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349 }
350}
351
Jeremy Hyltona8293132006-02-28 17:58:27 +0000352/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353
354 Only sets context for expr kinds that "can appear in assignment context"
355 (according to ../Parser/Python.asdl). For other expr kinds, it sets
356 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357*/
358
359static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000360set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361{
362 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000363 /* If a particular expression type can't be used for assign / delete,
364 set expr_name to its name and an error message will be generated.
365 */
366 const char* expr_name = NULL;
367
368 /* The ast defines augmented store and load contexts, but the
369 implementation here doesn't actually use them. The code may be
370 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000371 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000372 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000373 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000374 */
375 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376
377 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000378 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000379 if (ctx == Store && !forbidden_check(c, n,
380 PyBytes_AS_STRING(e->v.Attribute.attr)))
381 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000382 e->v.Attribute.ctx = ctx;
383 break;
384 case Subscript_kind:
385 e->v.Subscript.ctx = ctx;
386 break;
387 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000388 if (ctx == Store && !forbidden_check(c, n,
389 PyBytes_AS_STRING(e->v.Name.id)))
390 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000391 e->v.Name.ctx = ctx;
392 break;
393 case List_kind:
394 e->v.List.ctx = ctx;
395 s = e->v.List.elts;
396 break;
397 case Tuple_kind:
398 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
399 return ast_error(n, "can't assign to ()");
400 e->v.Tuple.ctx = ctx;
401 s = e->v.Tuple.elts;
402 break;
403 case Lambda_kind:
404 expr_name = "lambda";
405 break;
406 case Call_kind:
407 expr_name = "function call";
408 break;
409 case BoolOp_kind:
410 case BinOp_kind:
411 case UnaryOp_kind:
412 expr_name = "operator";
413 break;
414 case GeneratorExp_kind:
415 expr_name = "generator expression";
416 break;
417 case Yield_kind:
418 expr_name = "yield expression";
419 break;
420 case ListComp_kind:
421 expr_name = "list comprehension";
422 break;
423 case Dict_kind:
424 case Num_kind:
425 case Str_kind:
426 expr_name = "literal";
427 break;
428 case Compare_kind:
429 expr_name = "comparison";
430 break;
431 case Repr_kind:
432 expr_name = "repr";
433 break;
434 case IfExp_kind:
435 expr_name = "conditional expression";
436 break;
437 default:
438 PyErr_Format(PyExc_SystemError,
439 "unexpected expression in assignment %d (line %d)",
440 e->kind, e->lineno);
441 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000443 /* Check for error string set by switch */
444 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000445 char buf[300];
446 PyOS_snprintf(buf, sizeof(buf),
447 "can't %s %s",
448 ctx == Store ? "assign to" : "delete",
449 expr_name);
450 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000451 }
452
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000454 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455 */
456 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000457 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000459 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000460 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000461 return 0;
462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 }
464 return 1;
465}
466
467static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000468ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469{
470 REQ(n, augassign);
471 n = CHILD(n, 0);
472 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000473 case '+':
474 return Add;
475 case '-':
476 return Sub;
477 case '/':
478 if (STR(n)[1] == '/')
479 return FloorDiv;
480 else
481 return Div;
482 case '%':
483 return Mod;
484 case '<':
485 return LShift;
486 case '>':
487 return RShift;
488 case '&':
489 return BitAnd;
490 case '^':
491 return BitXor;
492 case '|':
493 return BitOr;
494 case '*':
495 if (STR(n)[1] == '*')
496 return Pow;
497 else
498 return Mult;
499 default:
500 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
501 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502 }
503}
504
505static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000506ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507{
508 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000509 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 */
511 REQ(n, comp_op);
512 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000513 n = CHILD(n, 0);
514 switch (TYPE(n)) {
515 case LESS:
516 return Lt;
517 case GREATER:
518 return Gt;
519 case EQEQUAL: /* == */
520 return Eq;
521 case LESSEQUAL:
522 return LtE;
523 case GREATEREQUAL:
524 return GtE;
525 case NOTEQUAL:
526 return NotEq;
527 case NAME:
528 if (strcmp(STR(n), "in") == 0)
529 return In;
530 if (strcmp(STR(n), "is") == 0)
531 return Is;
532 default:
533 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
534 STR(n));
535 return (cmpop_ty)0;
536 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 }
538 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000539 /* handle "not in" and "is not" */
540 switch (TYPE(CHILD(n, 0))) {
541 case NAME:
542 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
543 return NotIn;
544 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
545 return IsNot;
546 default:
547 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
548 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
549 return (cmpop_ty)0;
550 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551 }
Neal Norwitz79792652005-11-14 04:25:03 +0000552 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000553 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000554 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555}
556
557static asdl_seq *
558seq_for_testlist(struct compiling *c, const node *n)
559{
560 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000561 asdl_seq *seq;
562 expr_ty expression;
563 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000564 assert(TYPE(n) == testlist ||
565 TYPE(n) == listmaker ||
566 TYPE(n) == testlist_gexp ||
567 TYPE(n) == testlist_safe ||
568 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000570 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000572 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573
574 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000575 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000577 expression = ast_for_expr(c, CHILD(n, i));
578 if (!expression)
579 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000581 assert(i / 2 < seq->size);
582 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 }
584 return seq;
585}
586
587static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000588compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589{
590 int i, len = (NCH(n) + 1) / 2;
591 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000592 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000594 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
Neal Norwitz3a230172006-09-22 08:18:10 +0000596 /* fpdef: NAME | '(' fplist ')'
597 fplist: fpdef (',' fpdef)* [',']
598 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 for (i = 0; i < len; i++) {
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000601 PyObject *arg_id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000602 const node *fpdef_node = CHILD(n, 2*i);
603 const node *child;
604 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000605set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000606 /* fpdef_node is either a NAME or an fplist */
607 child = CHILD(fpdef_node, 0);
608 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000609 if (!forbidden_check(c, n, STR(child)))
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000610 return NULL;
611 arg_id = NEW_IDENTIFIER(child);
612 if (!arg_id)
613 return NULL;
614 arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
615 c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000616 }
617 else {
618 assert(TYPE(fpdef_node) == fpdef);
619 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
620 child = CHILD(fpdef_node, 1);
621 assert(TYPE(child) == fplist);
622 /* NCH == 1 means we have (x), we need to elide the extra parens */
623 if (NCH(child) == 1) {
624 fpdef_node = CHILD(child, 0);
625 assert(TYPE(fpdef_node) == fpdef);
626 goto set_name;
627 }
628 arg = compiler_complex_args(c, child);
629 }
630 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 }
632
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000633 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000634 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000635 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 return result;
637}
638
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639
Jeremy Hyltona8293132006-02-28 17:58:27 +0000640/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641
642static arguments_ty
643ast_for_arguments(struct compiling *c, const node *n)
644{
645 /* parameters: '(' [varargslist] ')'
646 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000647 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000649 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 asdl_seq *args, *defaults;
651 identifier vararg = NULL, kwarg = NULL;
652 node *ch;
653
654 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000655 if (NCH(n) == 2) /* () as argument list */
656 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
657 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 }
659 REQ(n, varargslist);
660
661 /* first count the number of normal args & defaults */
662 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000663 ch = CHILD(n, i);
664 if (TYPE(ch) == fpdef)
665 n_args++;
666 if (TYPE(ch) == EQUAL)
667 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000669 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000671 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000672 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000674 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675
676 /* fpdef: NAME | '(' fplist ')'
677 fplist: fpdef (',' fpdef)* [',']
678 */
679 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000680 j = 0; /* index for defaults */
681 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000683 ch = CHILD(n, i);
684 switch (TYPE(ch)) {
685 case fpdef:
686 handle_fpdef:
687 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
688 anything other than EQUAL or a comma? */
689 /* XXX Should NCH(n) check be made a separate check? */
690 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
691 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
692 if (!expression)
693 goto error;
694 assert(defaults != NULL);
695 asdl_seq_SET(defaults, j++, expression);
696 i += 2;
697 found_default = 1;
698 }
699 else if (found_default) {
700 ast_error(n,
701 "non-default argument follows default argument");
702 goto error;
703 }
704 if (NCH(ch) == 3) {
705 ch = CHILD(ch, 1);
706 /* def foo((x)): is not complex, special case. */
707 if (NCH(ch) != 1) {
708 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000709 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
710 "tuple parameter unpacking has been removed in 3.x"))
711 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000712 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000713 if (!asdl_seq_GET(args, k-1))
714 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000715 } else {
716 /* def foo((x)): setup for checking NAME below. */
717 /* Loop because there can be many parens and tuple
718 unpacking mixed in. */
719 ch = CHILD(ch, 0);
720 assert(TYPE(ch) == fpdef);
721 goto handle_fpdef;
722 }
723 }
724 if (TYPE(CHILD(ch, 0)) == NAME) {
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000725 PyObject *id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000726 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000727 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000728 goto error;
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000729 id = NEW_IDENTIFIER(CHILD(ch, 0));
730 if (!id)
731 goto error;
732 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000733 c->c_arena);
734 if (!name)
735 goto error;
736 asdl_seq_SET(args, k++, name);
737
738 }
739 i += 2; /* the name and the comma */
740 break;
741 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000742 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000743 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000744 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000745 if (!vararg)
746 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000747 i += 3;
748 break;
749 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000750 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000751 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000752 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000753 if (!kwarg)
754 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000755 i += 3;
756 break;
757 default:
758 PyErr_Format(PyExc_SystemError,
759 "unexpected node in varargslist: %d @ %d",
760 TYPE(ch), i);
761 goto error;
762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763 }
764
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000765 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766
767 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000768 Py_XDECREF(vararg);
769 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 return NULL;
771}
772
773static expr_ty
774ast_for_dotted_name(struct compiling *c, const node *n)
775{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000776 expr_ty e;
777 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000778 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 int i;
780
781 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000782
783 lineno = LINENO(n);
784 col_offset = n->n_col_offset;
785
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 id = NEW_IDENTIFIER(CHILD(n, 0));
787 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000788 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000789 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000791 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
793 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000794 id = NEW_IDENTIFIER(CHILD(n, i));
795 if (!id)
796 return NULL;
797 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
798 if (!e)
799 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 }
801
802 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803}
804
805static expr_ty
806ast_for_decorator(struct compiling *c, const node *n)
807{
808 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
809 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000810 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811
812 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000813 REQ(CHILD(n, 0), AT);
814 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815
816 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
817 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000818 return NULL;
819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000821 d = name_expr;
822 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 }
824 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000825 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
826 n->n_col_offset, c->c_arena);
827 if (!d)
828 return NULL;
829 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 }
831 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000832 d = ast_for_call(c, CHILD(n, 3), name_expr);
833 if (!d)
834 return NULL;
835 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 }
837
838 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839}
840
841static asdl_seq*
842ast_for_decorators(struct compiling *c, const node *n)
843{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000844 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000845 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 int i;
847
848 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000849 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000851 return NULL;
852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000854 d = ast_for_decorator(c, CHILD(n, i));
855 if (!d)
856 return NULL;
857 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
859 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860}
861
862static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000863ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864{
Christian Heimes5224d282008-02-23 15:01:05 +0000865 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000866 identifier name;
867 arguments_ty args;
868 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000869 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870
871 REQ(n, funcdef);
872
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 name = NEW_IDENTIFIER(CHILD(n, name_i));
874 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000875 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000876 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000877 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 args = ast_for_arguments(c, CHILD(n, name_i + 1));
879 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000880 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 body = ast_for_suite(c, CHILD(n, name_i + 3));
882 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000883 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000885 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000886 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887}
888
Christian Heimes5224d282008-02-23 15:01:05 +0000889static stmt_ty
890ast_for_decorated(struct compiling *c, const node *n)
891{
892 /* decorated: decorators (classdef | funcdef) */
893 stmt_ty thing = NULL;
894 asdl_seq *decorator_seq = NULL;
895
896 REQ(n, decorated);
897
898 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
899 if (!decorator_seq)
900 return NULL;
901
902 assert(TYPE(CHILD(n, 1)) == funcdef ||
903 TYPE(CHILD(n, 1)) == classdef);
904
905 if (TYPE(CHILD(n, 1)) == funcdef) {
906 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
907 } else if (TYPE(CHILD(n, 1)) == classdef) {
908 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
909 }
910 /* we count the decorators in when talking about the class' or
911 function's line number */
912 if (thing) {
913 thing->lineno = LINENO(n);
914 thing->col_offset = n->n_col_offset;
915 }
916 return thing;
917}
918
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919static expr_ty
920ast_for_lambdef(struct compiling *c, const node *n)
921{
922 /* lambdef: 'lambda' [varargslist] ':' test */
923 arguments_ty args;
924 expr_ty expression;
925
926 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000927 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
928 if (!args)
929 return NULL;
930 expression = ast_for_expr(c, CHILD(n, 2));
931 if (!expression)
932 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933 }
934 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000935 args = ast_for_arguments(c, CHILD(n, 1));
936 if (!args)
937 return NULL;
938 expression = ast_for_expr(c, CHILD(n, 3));
939 if (!expression)
940 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 }
942
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000943 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944}
945
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000946static expr_ty
947ast_for_ifexpr(struct compiling *c, const node *n)
948{
949 /* test: or_test 'if' or_test 'else' test */
950 expr_ty expression, body, orelse;
951
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000952 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000953 body = ast_for_expr(c, CHILD(n, 0));
954 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000955 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000956 expression = ast_for_expr(c, CHILD(n, 2));
957 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000958 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000959 orelse = ast_for_expr(c, CHILD(n, 4));
960 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000961 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000962 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000963 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000964}
965
Neal Norwitze4d4f002006-09-05 03:58:26 +0000966/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
967 so there is only a single version. Possibly for loops can also re-use
968 the code.
969*/
970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971/* Count the number of 'for' loop in a list comprehension.
972
973 Helper for ast_for_listcomp().
974*/
975
976static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000977count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978{
979 int n_fors = 0;
980 node *ch = CHILD(n, 1);
981
982 count_list_for:
983 n_fors++;
984 REQ(ch, list_for);
985 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000986 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000988 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 count_list_iter:
990 REQ(ch, list_iter);
991 ch = CHILD(ch, 0);
992 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000993 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000995 if (NCH(ch) == 3) {
996 ch = CHILD(ch, 2);
997 goto count_list_iter;
998 }
999 else
1000 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001002
1003 /* Should never be reached */
1004 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1005 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006}
1007
1008/* Count the number of 'if' statements in a list comprehension.
1009
1010 Helper for ast_for_listcomp().
1011*/
1012
1013static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001014count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015{
1016 int n_ifs = 0;
1017
1018 count_list_iter:
1019 REQ(n, list_iter);
1020 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001021 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 n = CHILD(n, 0);
1023 REQ(n, list_if);
1024 n_ifs++;
1025 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001026 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 n = CHILD(n, 2);
1028 goto count_list_iter;
1029}
1030
1031static expr_ty
1032ast_for_listcomp(struct compiling *c, const node *n)
1033{
1034 /* listmaker: test ( list_for | (',' test)* [','] )
1035 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1036 list_iter: list_for | list_if
1037 list_if: 'if' test [list_iter]
1038 testlist_safe: test [(',' test)+ [',']]
1039 */
1040 expr_ty elt;
1041 asdl_seq *listcomps;
1042 int i, n_fors;
1043 node *ch;
1044
1045 REQ(n, listmaker);
1046 assert(NCH(n) > 1);
1047
1048 elt = ast_for_expr(c, CHILD(n, 0));
1049 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001050 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001052 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001054 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001056 listcomps = asdl_seq_new(n_fors, c->c_arena);
1057 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001058 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 ch = CHILD(n, 1);
1061 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001062 comprehension_ty lc;
1063 asdl_seq *t;
1064 expr_ty expression;
1065 node *for_ch;
1066
1067 REQ(ch, list_for);
1068
1069 for_ch = CHILD(ch, 1);
1070 t = ast_for_exprlist(c, for_ch, Store);
1071 if (!t)
1072 return NULL;
1073 expression = ast_for_testlist(c, CHILD(ch, 3));
1074 if (!expression)
1075 return NULL;
1076
1077 /* Check the # of children rather than the length of t, since
1078 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1079 */
1080 if (NCH(for_ch) == 1)
1081 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1082 c->c_arena);
1083 else
1084 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1085 c->c_arena),
1086 expression, NULL, c->c_arena);
1087 if (!lc)
1088 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001090 if (NCH(ch) == 5) {
1091 int j, n_ifs;
1092 asdl_seq *ifs;
1093 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001095 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001096 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001097 if (n_ifs == -1)
1098 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001100 ifs = asdl_seq_new(n_ifs, c->c_arena);
1101 if (!ifs)
1102 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001104 for (j = 0; j < n_ifs; j++) {
1105 REQ(ch, list_iter);
1106 ch = CHILD(ch, 0);
1107 REQ(ch, list_if);
1108
1109 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1110 if (!list_for_expr)
1111 return NULL;
1112
1113 asdl_seq_SET(ifs, j, list_for_expr);
1114 if (NCH(ch) == 3)
1115 ch = CHILD(ch, 2);
1116 }
1117 /* on exit, must guarantee that ch is a list_for */
1118 if (TYPE(ch) == list_iter)
1119 ch = CHILD(ch, 0);
1120 lc->ifs = ifs;
1121 }
1122 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 }
1124
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001125 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126}
1127
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001128/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129
1130 Helper for ast_for_genexp().
1131*/
1132
1133static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001134count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001136 int n_fors = 0;
1137 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138
1139 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001140 n_fors++;
1141 REQ(ch, gen_for);
1142 if (NCH(ch) == 5)
1143 ch = CHILD(ch, 4);
1144 else
1145 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001147 REQ(ch, gen_iter);
1148 ch = CHILD(ch, 0);
1149 if (TYPE(ch) == gen_for)
1150 goto count_gen_for;
1151 else if (TYPE(ch) == gen_if) {
1152 if (NCH(ch) == 3) {
1153 ch = CHILD(ch, 2);
1154 goto count_gen_iter;
1155 }
1156 else
1157 return n_fors;
1158 }
1159
1160 /* Should never be reached */
1161 PyErr_SetString(PyExc_SystemError,
1162 "logic error in count_gen_fors");
1163 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164}
1165
1166/* Count the number of 'if' statements in a generator expression.
1167
1168 Helper for ast_for_genexp().
1169*/
1170
1171static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001172count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001174 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001176 while (1) {
1177 REQ(n, gen_iter);
1178 if (TYPE(CHILD(n, 0)) == gen_for)
1179 return n_ifs;
1180 n = CHILD(n, 0);
1181 REQ(n, gen_if);
1182 n_ifs++;
1183 if (NCH(n) == 2)
1184 return n_ifs;
1185 n = CHILD(n, 2);
1186 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187}
1188
Jeremy Hyltona8293132006-02-28 17:58:27 +00001189/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190static expr_ty
1191ast_for_genexp(struct compiling *c, const node *n)
1192{
1193 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001194 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 expr_ty elt;
1196 asdl_seq *genexps;
1197 int i, n_fors;
1198 node *ch;
1199
1200 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1201 assert(NCH(n) > 1);
1202
1203 elt = ast_for_expr(c, CHILD(n, 0));
1204 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001207 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001209 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001210
1211 genexps = asdl_seq_new(n_fors, c->c_arena);
1212 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001213 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001214
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 ch = CHILD(n, 1);
1216 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001217 comprehension_ty ge;
1218 asdl_seq *t;
1219 expr_ty expression;
1220 node *for_ch;
1221
1222 REQ(ch, gen_for);
1223
1224 for_ch = CHILD(ch, 1);
1225 t = ast_for_exprlist(c, for_ch, Store);
1226 if (!t)
1227 return NULL;
1228 expression = ast_for_expr(c, CHILD(ch, 3));
1229 if (!expression)
1230 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001231
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001232 /* Check the # of children rather than the length of t, since
1233 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1234 if (NCH(for_ch) == 1)
1235 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1236 NULL, c->c_arena);
1237 else
1238 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1239 c->c_arena),
1240 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001242 if (!ge)
1243 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001244
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001245 if (NCH(ch) == 5) {
1246 int j, n_ifs;
1247 asdl_seq *ifs;
1248
1249 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001250 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001251 if (n_ifs == -1)
1252 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001253
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001254 ifs = asdl_seq_new(n_ifs, c->c_arena);
1255 if (!ifs)
1256 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001257
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001258 for (j = 0; j < n_ifs; j++) {
1259 REQ(ch, gen_iter);
1260 ch = CHILD(ch, 0);
1261 REQ(ch, gen_if);
1262
1263 expression = ast_for_expr(c, CHILD(ch, 1));
1264 if (!expression)
1265 return NULL;
1266 asdl_seq_SET(ifs, j, expression);
1267 if (NCH(ch) == 3)
1268 ch = CHILD(ch, 2);
1269 }
1270 /* on exit, must guarantee that ch is a gen_for */
1271 if (TYPE(ch) == gen_iter)
1272 ch = CHILD(ch, 0);
1273 ge->ifs = ifs;
1274 }
1275 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 }
1277
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001278 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279}
1280
1281static expr_ty
1282ast_for_atom(struct compiling *c, const node *n)
1283{
1284 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1285 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1286 */
1287 node *ch = CHILD(n, 0);
1288
1289 switch (TYPE(ch)) {
Benjamin Peterson5cb67632008-11-25 04:00:37 +00001290 case NAME: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001291 /* All names start in Load context, but may later be
1292 changed. */
Benjamin Peterson5cb67632008-11-25 04:00:37 +00001293 PyObject *name = NEW_IDENTIFIER(ch);
1294 if (!name)
1295 return NULL;
1296 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1297 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001299 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001300 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001301#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001302 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1303 PyObject *type, *value, *tback, *errstr;
1304 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson71ce9e72008-11-21 22:52:21 +00001305 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001306 if (errstr) {
1307 char *s = "";
1308 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001309 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001310 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1311 ast_error(n, buf);
Benjamin Peterson71ce9e72008-11-21 22:52:21 +00001312 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001313 } else {
1314 ast_error(n, "(unicode error) unknown error");
1315 }
1316 Py_DECREF(type);
1317 Py_DECREF(value);
1318 Py_XDECREF(tback);
1319 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001320#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001321 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001322 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001323 PyArena_AddPyObject(c->c_arena, str);
1324 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 }
1326 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001327 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001328 if (!pynum)
1329 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001330
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001331 PyArena_AddPyObject(c->c_arena, pynum);
1332 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 }
1334 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001335 ch = CHILD(n, 1);
1336
1337 if (TYPE(ch) == RPAR)
1338 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1339
1340 if (TYPE(ch) == yield_expr)
1341 return ast_for_expr(c, ch);
1342
1343 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1344 return ast_for_genexp(c, ch);
1345
1346 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001348 ch = CHILD(n, 1);
1349
1350 if (TYPE(ch) == RSQB)
1351 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1352
1353 REQ(ch, listmaker);
1354 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1355 asdl_seq *elts = seq_for_testlist(c, ch);
1356 if (!elts)
1357 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001358
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001359 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1360 }
1361 else
1362 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001364 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1365 int i, size;
1366 asdl_seq *keys, *values;
1367
1368 ch = CHILD(n, 1);
1369 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1370 keys = asdl_seq_new(size, c->c_arena);
1371 if (!keys)
1372 return NULL;
1373
1374 values = asdl_seq_new(size, c->c_arena);
1375 if (!values)
1376 return NULL;
1377
1378 for (i = 0; i < NCH(ch); i += 4) {
1379 expr_ty expression;
1380
1381 expression = ast_for_expr(c, CHILD(ch, i));
1382 if (!expression)
1383 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001384
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001385 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001386
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001387 expression = ast_for_expr(c, CHILD(ch, i + 2));
1388 if (!expression)
1389 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001390
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001391 asdl_seq_SET(values, i / 4, expression);
1392 }
1393 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 }
1395 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001396 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001397 if (Py_Py3kWarningFlag &&
1398 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001399 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001400 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001401 if (!expression)
1402 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001403
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001404 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 }
1406 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001407 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1408 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 }
1410}
1411
1412static slice_ty
1413ast_for_slice(struct compiling *c, const node *n)
1414{
1415 node *ch;
1416 expr_ty lower = NULL, upper = NULL, step = NULL;
1417
1418 REQ(n, subscript);
1419
1420 /*
1421 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1422 sliceop: ':' [test]
1423 */
1424 ch = CHILD(n, 0);
1425 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001426 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427
1428 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001429 /* 'step' variable hold no significance in terms of being used over
1430 other vars */
1431 step = ast_for_expr(c, ch);
1432 if (!step)
1433 return NULL;
1434
1435 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 }
1437
1438 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001439 lower = ast_for_expr(c, ch);
1440 if (!lower)
1441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 }
1443
1444 /* If there's an upper bound it's in the second or third position. */
1445 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001446 if (NCH(n) > 1) {
1447 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001449 if (TYPE(n2) == test) {
1450 upper = ast_for_expr(c, n2);
1451 if (!upper)
1452 return NULL;
1453 }
1454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001456 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001458 if (TYPE(n2) == test) {
1459 upper = ast_for_expr(c, n2);
1460 if (!upper)
1461 return NULL;
1462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 }
1464
1465 ch = CHILD(n, NCH(n) - 1);
1466 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001467 if (NCH(ch) == 1) {
1468 /* No expression, so step is None */
1469 ch = CHILD(ch, 0);
1470 step = Name(new_identifier("None", c->c_arena), Load,
1471 LINENO(ch), ch->n_col_offset, c->c_arena);
1472 if (!step)
1473 return NULL;
1474 } else {
1475 ch = CHILD(ch, 1);
1476 if (TYPE(ch) == test) {
1477 step = ast_for_expr(c, ch);
1478 if (!step)
1479 return NULL;
1480 }
1481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 }
1483
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001484 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485}
1486
1487static expr_ty
1488ast_for_binop(struct compiling *c, const node *n)
1489{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001490 /* Must account for a sequence of expressions.
1491 How should A op B op C by represented?
1492 BinOp(BinOp(A, op, B), op, C).
1493 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001495 int i, nops;
1496 expr_ty expr1, expr2, result;
1497 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001499 expr1 = ast_for_expr(c, CHILD(n, 0));
1500 if (!expr1)
1501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001503 expr2 = ast_for_expr(c, CHILD(n, 2));
1504 if (!expr2)
1505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001507 newoperator = get_operator(CHILD(n, 1));
1508 if (!newoperator)
1509 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001511 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1512 c->c_arena);
1513 if (!result)
1514 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001516 nops = (NCH(n) - 1) / 2;
1517 for (i = 1; i < nops; i++) {
1518 expr_ty tmp_result, tmp;
1519 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001521 newoperator = get_operator(next_oper);
1522 if (!newoperator)
1523 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001525 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1526 if (!tmp)
1527 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001529 tmp_result = BinOp(result, newoperator, tmp,
1530 LINENO(next_oper), next_oper->n_col_offset,
1531 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001532 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001533 return NULL;
1534 result = tmp_result;
1535 }
1536 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537}
1538
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001539static expr_ty
1540ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1541{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001542 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1543 subscriptlist: subscript (',' subscript)* [',']
1544 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1545 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001546 REQ(n, trailer);
1547 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001548 if (NCH(n) == 2)
1549 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1550 n->n_col_offset, c->c_arena);
1551 else
1552 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001553 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001554 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson5cb67632008-11-25 04:00:37 +00001555 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1556 if (!attr_id)
1557 return NULL;
1558 return Attribute(left_expr, attr_id, Load,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001559 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001560 }
1561 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001562 REQ(CHILD(n, 0), LSQB);
1563 REQ(CHILD(n, 2), RSQB);
1564 n = CHILD(n, 1);
1565 if (NCH(n) == 1) {
1566 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1567 if (!slc)
1568 return NULL;
1569 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1570 c->c_arena);
1571 }
1572 else {
1573 /* The grammar is ambiguous here. The ambiguity is resolved
1574 by treating the sequence as a tuple literal if there are
1575 no slice features.
1576 */
1577 int j;
1578 slice_ty slc;
1579 expr_ty e;
1580 bool simple = true;
1581 asdl_seq *slices, *elts;
1582 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1583 if (!slices)
1584 return NULL;
1585 for (j = 0; j < NCH(n); j += 2) {
1586 slc = ast_for_slice(c, CHILD(n, j));
1587 if (!slc)
1588 return NULL;
1589 if (slc->kind != Index_kind)
1590 simple = false;
1591 asdl_seq_SET(slices, j / 2, slc);
1592 }
1593 if (!simple) {
1594 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1595 Load, LINENO(n), n->n_col_offset, c->c_arena);
1596 }
1597 /* extract Index values and put them in a Tuple */
1598 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1599 if (!elts)
1600 return NULL;
1601 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1602 slc = (slice_ty)asdl_seq_GET(slices, j);
1603 assert(slc->kind == Index_kind && slc->v.Index.value);
1604 asdl_seq_SET(elts, j, slc->v.Index.value);
1605 }
1606 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1607 if (!e)
1608 return NULL;
1609 return Subscript(left_expr, Index(e, c->c_arena),
1610 Load, LINENO(n), n->n_col_offset, c->c_arena);
1611 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001612 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001613}
1614
1615static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001616ast_for_factor(struct compiling *c, const node *n)
1617{
1618 node *pfactor, *ppower, *patom, *pnum;
1619 expr_ty expression;
1620
1621 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001622 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001623 constant. The peephole optimizer already does something like
1624 this but it doesn't handle the case where the constant is
1625 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1626 PyLongObject.
1627 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001628 if (TYPE(CHILD(n, 0)) == MINUS &&
1629 NCH(n) == 2 &&
1630 TYPE((pfactor = CHILD(n, 1))) == factor &&
1631 NCH(pfactor) == 1 &&
1632 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1633 NCH(ppower) == 1 &&
1634 TYPE((patom = CHILD(ppower, 0))) == atom &&
1635 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1636 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1637 if (s == NULL)
1638 return NULL;
1639 s[0] = '-';
1640 strcpy(s + 1, STR(pnum));
1641 PyObject_FREE(STR(pnum));
1642 STR(pnum) = s;
1643 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001644 }
1645
1646 expression = ast_for_expr(c, CHILD(n, 1));
1647 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001648 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001649
1650 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001651 case PLUS:
1652 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1653 c->c_arena);
1654 case MINUS:
1655 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1656 c->c_arena);
1657 case TILDE:
1658 return UnaryOp(Invert, expression, LINENO(n),
1659 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001660 }
1661 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001662 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001663 return NULL;
1664}
1665
1666static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001667ast_for_power(struct compiling *c, const node *n)
1668{
1669 /* power: atom trailer* ('**' factor)*
1670 */
1671 int i;
1672 expr_ty e, tmp;
1673 REQ(n, power);
1674 e = ast_for_atom(c, CHILD(n, 0));
1675 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001676 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001677 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001678 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001679 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001680 node *ch = CHILD(n, i);
1681 if (TYPE(ch) != trailer)
1682 break;
1683 tmp = ast_for_trailer(c, ch, e);
1684 if (!tmp)
1685 return NULL;
1686 tmp->lineno = e->lineno;
1687 tmp->col_offset = e->col_offset;
1688 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001689 }
1690 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001691 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1692 if (!f)
1693 return NULL;
1694 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1695 if (!tmp)
1696 return NULL;
1697 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001698 }
1699 return e;
1700}
1701
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702/* Do not name a variable 'expr'! Will cause a compile error.
1703*/
1704
1705static expr_ty
1706ast_for_expr(struct compiling *c, const node *n)
1707{
1708 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001709 test: or_test ['if' or_test 'else' test] | lambdef
1710 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 and_test: not_test ('and' not_test)*
1712 not_test: 'not' not_test | comparison
1713 comparison: expr (comp_op expr)*
1714 expr: xor_expr ('|' xor_expr)*
1715 xor_expr: and_expr ('^' and_expr)*
1716 and_expr: shift_expr ('&' shift_expr)*
1717 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1718 arith_expr: term (('+'|'-') term)*
1719 term: factor (('*'|'/'|'%'|'//') factor)*
1720 factor: ('+'|'-'|'~') factor | power
1721 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001722
1723 As well as modified versions that exist for backward compatibility,
1724 to explicitly allow:
1725 [ x for x in lambda: 0, lambda: 1 ]
1726 (which would be ambiguous without these extra rules)
1727
1728 old_test: or_test | old_lambdef
1729 old_lambdef: 'lambda' [vararglist] ':' old_test
1730
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 */
1732
1733 asdl_seq *seq;
1734 int i;
1735
1736 loop:
1737 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001738 case test:
1739 case old_test:
1740 if (TYPE(CHILD(n, 0)) == lambdef ||
1741 TYPE(CHILD(n, 0)) == old_lambdef)
1742 return ast_for_lambdef(c, CHILD(n, 0));
1743 else if (NCH(n) > 1)
1744 return ast_for_ifexpr(c, n);
1745 /* Fallthrough */
1746 case or_test:
1747 case and_test:
1748 if (NCH(n) == 1) {
1749 n = CHILD(n, 0);
1750 goto loop;
1751 }
1752 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1753 if (!seq)
1754 return NULL;
1755 for (i = 0; i < NCH(n); i += 2) {
1756 expr_ty e = ast_for_expr(c, CHILD(n, i));
1757 if (!e)
1758 return NULL;
1759 asdl_seq_SET(seq, i / 2, e);
1760 }
1761 if (!strcmp(STR(CHILD(n, 1)), "and"))
1762 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1763 c->c_arena);
1764 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1765 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1766 case not_test:
1767 if (NCH(n) == 1) {
1768 n = CHILD(n, 0);
1769 goto loop;
1770 }
1771 else {
1772 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1773 if (!expression)
1774 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001776 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1777 c->c_arena);
1778 }
1779 case comparison:
1780 if (NCH(n) == 1) {
1781 n = CHILD(n, 0);
1782 goto loop;
1783 }
1784 else {
1785 expr_ty expression;
1786 asdl_int_seq *ops;
1787 asdl_seq *cmps;
1788 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1789 if (!ops)
1790 return NULL;
1791 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1792 if (!cmps) {
1793 return NULL;
1794 }
1795 for (i = 1; i < NCH(n); i += 2) {
1796 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001798 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001799 if (!newoperator) {
1800 return NULL;
1801 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001803 expression = ast_for_expr(c, CHILD(n, i + 1));
1804 if (!expression) {
1805 return NULL;
1806 }
1807
1808 asdl_seq_SET(ops, i / 2, newoperator);
1809 asdl_seq_SET(cmps, i / 2, expression);
1810 }
1811 expression = ast_for_expr(c, CHILD(n, 0));
1812 if (!expression) {
1813 return NULL;
1814 }
1815
1816 return Compare(expression, ops, cmps, LINENO(n),
1817 n->n_col_offset, c->c_arena);
1818 }
1819 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001821 /* The next five cases all handle BinOps. The main body of code
1822 is the same in each case, but the switch turned inside out to
1823 reuse the code for each type of operator.
1824 */
1825 case expr:
1826 case xor_expr:
1827 case and_expr:
1828 case shift_expr:
1829 case arith_expr:
1830 case term:
1831 if (NCH(n) == 1) {
1832 n = CHILD(n, 0);
1833 goto loop;
1834 }
1835 return ast_for_binop(c, n);
1836 case yield_expr: {
1837 expr_ty exp = NULL;
1838 if (NCH(n) == 2) {
1839 exp = ast_for_testlist(c, CHILD(n, 1));
1840 if (!exp)
1841 return NULL;
1842 }
1843 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1844 }
1845 case factor:
1846 if (NCH(n) == 1) {
1847 n = CHILD(n, 0);
1848 goto loop;
1849 }
1850 return ast_for_factor(c, n);
1851 case power:
1852 return ast_for_power(c, n);
1853 default:
1854 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001857 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 return NULL;
1859}
1860
1861static expr_ty
1862ast_for_call(struct compiling *c, const node *n, expr_ty func)
1863{
1864 /*
1865 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001866 | '**' test)
1867 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 */
1869
1870 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001871 asdl_seq *args;
1872 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 expr_ty vararg = NULL, kwarg = NULL;
1874
1875 REQ(n, arglist);
1876
1877 nargs = 0;
1878 nkeywords = 0;
1879 ngens = 0;
1880 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001881 node *ch = CHILD(n, i);
1882 if (TYPE(ch) == argument) {
1883 if (NCH(ch) == 1)
1884 nargs++;
1885 else if (TYPE(CHILD(ch, 1)) == gen_for)
1886 ngens++;
1887 else
1888 nkeywords++;
1889 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 }
1891 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001892 ast_error(n, "Generator expression must be parenthesized "
1893 "if not sole argument");
1894 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 }
1896
1897 if (nargs + nkeywords + ngens > 255) {
1898 ast_error(n, "more than 255 arguments");
1899 return NULL;
1900 }
1901
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001902 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001904 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001905 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001907 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 nargs = 0;
1909 nkeywords = 0;
1910 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001911 node *ch = CHILD(n, i);
1912 if (TYPE(ch) == argument) {
1913 expr_ty e;
1914 if (NCH(ch) == 1) {
1915 if (nkeywords) {
1916 ast_error(CHILD(ch, 0),
1917 "non-keyword arg after keyword arg");
1918 return NULL;
1919 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001920 if (vararg) {
1921 ast_error(CHILD(ch, 0),
1922 "only named arguments may follow *expression");
1923 return NULL;
1924 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001925 e = ast_for_expr(c, CHILD(ch, 0));
1926 if (!e)
1927 return NULL;
1928 asdl_seq_SET(args, nargs++, e);
1929 }
1930 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1931 e = ast_for_genexp(c, ch);
1932 if (!e)
1933 return NULL;
1934 asdl_seq_SET(args, nargs++, e);
1935 }
1936 else {
1937 keyword_ty kw;
1938 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001939 int k;
1940 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001942 /* CHILD(ch, 0) is test, but must be an identifier? */
1943 e = ast_for_expr(c, CHILD(ch, 0));
1944 if (!e)
1945 return NULL;
1946 /* f(lambda x: x[0] = 3) ends up getting parsed with
1947 * LHS test = lambda x: x[0], and RHS test = 3.
1948 * SF bug 132313 points out that complaining about a keyword
1949 * then is very confusing.
1950 */
1951 if (e->kind == Lambda_kind) {
1952 ast_error(CHILD(ch, 0),
1953 "lambda cannot contain assignment");
1954 return NULL;
1955 } else if (e->kind != Name_kind) {
1956 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1957 return NULL;
1958 }
1959 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001960 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001961 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001962 for (k = 0; k < nkeywords; k++) {
1963 tmp = PyString_AS_STRING(
1964 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
1965 if (!strcmp(tmp, PyString_AS_STRING(key))) {
1966 ast_error(CHILD(ch, 0), "keyword argument repeated");
1967 return NULL;
1968 }
1969 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001970 e = ast_for_expr(c, CHILD(ch, 2));
1971 if (!e)
1972 return NULL;
1973 kw = keyword(key, e, c->c_arena);
1974 if (!kw)
1975 return NULL;
1976 asdl_seq_SET(keywords, nkeywords++, kw);
1977 }
1978 }
1979 else if (TYPE(ch) == STAR) {
1980 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001981 if (!vararg)
1982 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001983 i++;
1984 }
1985 else if (TYPE(ch) == DOUBLESTAR) {
1986 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001987 if (!kwarg)
1988 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001989 i++;
1990 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 }
1992
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001993 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1994 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995}
1996
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001998ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002000 /* testlist_gexp: test (',' test)* [','] */
2001 /* testlist: test (',' test)* [','] */
2002 /* testlist_safe: test (',' test)+ [','] */
2003 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002005 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002006 if (NCH(n) > 1)
2007 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002008 }
2009 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002010 assert(TYPE(n) == testlist ||
2011 TYPE(n) == testlist_safe ||
2012 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002013 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002015 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002017 asdl_seq *tmp = seq_for_testlist(c, n);
2018 if (!tmp)
2019 return NULL;
2020 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002022}
2023
2024static expr_ty
2025ast_for_testlist_gexp(struct compiling *c, const node* n)
2026{
2027 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2028 /* argument: test [ gen_for ] */
2029 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002030 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002031 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002032 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002033}
2034
2035/* like ast_for_testlist() but returns a sequence */
2036static asdl_seq*
2037ast_for_class_bases(struct compiling *c, const node* n)
2038{
2039 /* testlist: test (',' test)* [','] */
2040 assert(NCH(n) > 0);
2041 REQ(n, testlist);
2042 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002043 expr_ty base;
2044 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2045 if (!bases)
2046 return NULL;
2047 base = ast_for_expr(c, CHILD(n, 0));
2048 if (!base)
2049 return NULL;
2050 asdl_seq_SET(bases, 0, base);
2051 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002052 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002053
2054 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055}
2056
2057static stmt_ty
2058ast_for_expr_stmt(struct compiling *c, const node *n)
2059{
2060 REQ(n, expr_stmt);
2061 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002062 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 testlist: test (',' test)* [',']
2064 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002065 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 test: ... here starts the operator precendence dance
2067 */
2068
2069 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002070 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2071 if (!e)
2072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002074 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 }
2076 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002077 expr_ty expr1, expr2;
2078 operator_ty newoperator;
2079 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002081 expr1 = ast_for_testlist(c, ch);
2082 if (!expr1)
2083 return NULL;
2084 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2085 switch (expr1->kind) {
2086 case GeneratorExp_kind:
2087 ast_error(ch, "augmented assignment to generator "
2088 "expression not possible");
2089 return NULL;
2090 case Yield_kind:
2091 ast_error(ch, "augmented assignment to yield "
2092 "expression not possible");
2093 return NULL;
2094 case Name_kind: {
Christian Heimes593daf52008-05-26 12:51:38 +00002095 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
Benjamin Petersond5efd202008-06-08 22:52:37 +00002096 if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') &&
2097 !forbidden_check(c, ch, var_name))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002098 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002099 break;
2100 }
2101 case Attribute_kind:
2102 case Subscript_kind:
2103 break;
2104 default:
2105 ast_error(ch, "illegal expression for augmented "
2106 "assignment");
2107 return NULL;
2108 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002109 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002110 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002112 ch = CHILD(n, 2);
2113 if (TYPE(ch) == testlist)
2114 expr2 = ast_for_testlist(c, ch);
2115 else
2116 expr2 = ast_for_expr(c, ch);
2117 if (!expr2)
2118 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002120 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002121 if (!newoperator)
2122 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002124 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2125 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 }
2127 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002128 int i;
2129 asdl_seq *targets;
2130 node *value;
2131 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002133 /* a normal assignment */
2134 REQ(CHILD(n, 1), EQUAL);
2135 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2136 if (!targets)
2137 return NULL;
2138 for (i = 0; i < NCH(n) - 2; i += 2) {
2139 expr_ty e;
2140 node *ch = CHILD(n, i);
2141 if (TYPE(ch) == yield_expr) {
2142 ast_error(ch, "assignment to yield expression not possible");
2143 return NULL;
2144 }
2145 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002147 /* set context to assign */
2148 if (!e)
2149 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002151 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002152 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002154 asdl_seq_SET(targets, i / 2, e);
2155 }
2156 value = CHILD(n, NCH(n) - 1);
2157 if (TYPE(value) == testlist)
2158 expression = ast_for_testlist(c, value);
2159 else
2160 expression = ast_for_expr(c, value);
2161 if (!expression)
2162 return NULL;
2163 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2164 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166}
2167
2168static stmt_ty
2169ast_for_print_stmt(struct compiling *c, const node *n)
2170{
2171 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002172 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 */
2174 expr_ty dest = NULL, expression;
2175 asdl_seq *seq;
2176 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002177 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178
2179 REQ(n, print_stmt);
2180 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002181 dest = ast_for_expr(c, CHILD(n, 2));
2182 if (!dest)
2183 return NULL;
2184 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002186 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002188 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002189 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002190 expression = ast_for_expr(c, CHILD(n, i));
2191 if (!expression)
2192 return NULL;
2193 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 }
2195 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002196 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197}
2198
2199static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002200ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201{
2202 asdl_seq *seq;
2203 int i;
2204 expr_ty e;
2205
2206 REQ(n, exprlist);
2207
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002208 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002212 e = ast_for_expr(c, CHILD(n, i));
2213 if (!e)
2214 return NULL;
2215 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002216 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002217 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 }
2219 return seq;
2220}
2221
2222static stmt_ty
2223ast_for_del_stmt(struct compiling *c, const node *n)
2224{
2225 asdl_seq *expr_list;
2226
2227 /* del_stmt: 'del' exprlist */
2228 REQ(n, del_stmt);
2229
2230 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2231 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002232 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002233 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234}
2235
2236static stmt_ty
2237ast_for_flow_stmt(struct compiling *c, const node *n)
2238{
2239 /*
2240 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002241 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 break_stmt: 'break'
2243 continue_stmt: 'continue'
2244 return_stmt: 'return' [testlist]
2245 yield_stmt: yield_expr
2246 yield_expr: 'yield' testlist
2247 raise_stmt: 'raise' [test [',' test [',' test]]]
2248 */
2249 node *ch;
2250
2251 REQ(n, flow_stmt);
2252 ch = CHILD(n, 0);
2253 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002254 case break_stmt:
2255 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2256 case continue_stmt:
2257 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2258 case yield_stmt: { /* will reduce to yield_expr */
2259 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2260 if (!exp)
2261 return NULL;
2262 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2263 }
2264 case return_stmt:
2265 if (NCH(ch) == 1)
2266 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2267 else {
2268 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2269 if (!expression)
2270 return NULL;
2271 return Return(expression, LINENO(n), n->n_col_offset,
2272 c->c_arena);
2273 }
2274 case raise_stmt:
2275 if (NCH(ch) == 1)
2276 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2277 c->c_arena);
2278 else if (NCH(ch) == 2) {
2279 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2280 if (!expression)
2281 return NULL;
2282 return Raise(expression, NULL, NULL, LINENO(n),
2283 n->n_col_offset, c->c_arena);
2284 }
2285 else if (NCH(ch) == 4) {
2286 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002288 expr1 = ast_for_expr(c, CHILD(ch, 1));
2289 if (!expr1)
2290 return NULL;
2291 expr2 = ast_for_expr(c, CHILD(ch, 3));
2292 if (!expr2)
2293 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002295 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2296 c->c_arena);
2297 }
2298 else if (NCH(ch) == 6) {
2299 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002301 expr1 = ast_for_expr(c, CHILD(ch, 1));
2302 if (!expr1)
2303 return NULL;
2304 expr2 = ast_for_expr(c, CHILD(ch, 3));
2305 if (!expr2)
2306 return NULL;
2307 expr3 = ast_for_expr(c, CHILD(ch, 5));
2308 if (!expr3)
2309 return NULL;
2310
2311 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2312 c->c_arena);
2313 }
2314 default:
2315 PyErr_Format(PyExc_SystemError,
2316 "unexpected flow_stmt: %d", TYPE(ch));
2317 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002319
2320 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2321 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322}
2323
2324static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002325alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326{
2327 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002328 import_as_name: NAME ['as' NAME]
2329 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 dotted_name: NAME ('.' NAME)*
2331 */
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002332 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 loop:
2335 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002336 case import_as_name:
2337 str = NULL;
2338 if (NCH(n) == 3) {
2339 str = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002340 if (!str)
2341 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002342 }
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002343 name = NEW_IDENTIFIER(CHILD(n, 0));
2344 if (!name)
2345 return NULL;
2346 return alias(name, str, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002347 case dotted_as_name:
2348 if (NCH(n) == 1) {
2349 n = CHILD(n, 0);
2350 goto loop;
2351 }
2352 else {
2353 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2354 if (!a)
2355 return NULL;
2356 assert(!a->asname);
2357 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002358 if (!a->asname)
2359 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002360 return a;
2361 }
2362 break;
2363 case dotted_name:
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002364 if (NCH(n) == 1) {
2365 name = NEW_IDENTIFIER(CHILD(n, 0));
2366 if (!name)
2367 return NULL;
2368 return alias(name, NULL, c->c_arena);
2369 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002370 else {
2371 /* Create a string of the form "a.b.c" */
2372 int i;
2373 size_t len;
2374 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002376 len = 0;
2377 for (i = 0; i < NCH(n); i += 2)
2378 /* length of string plus one for the dot */
2379 len += strlen(STR(CHILD(n, i))) + 1;
2380 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002381 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002382 if (!str)
2383 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002384 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002385 if (!s)
2386 return NULL;
2387 for (i = 0; i < NCH(n); i += 2) {
2388 char *sch = STR(CHILD(n, i));
2389 strcpy(s, STR(CHILD(n, i)));
2390 s += strlen(sch);
2391 *s++ = '.';
2392 }
2393 --s;
2394 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002395 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002396 PyArena_AddPyObject(c->c_arena, str);
2397 return alias(str, NULL, c->c_arena);
2398 }
2399 break;
2400 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002401 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002402 PyArena_AddPyObject(c->c_arena, str);
2403 return alias(str, NULL, c->c_arena);
2404 default:
2405 PyErr_Format(PyExc_SystemError,
2406 "unexpected import name: %d", TYPE(n));
2407 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002409
2410 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411 return NULL;
2412}
2413
2414static stmt_ty
2415ast_for_import_stmt(struct compiling *c, const node *n)
2416{
2417 /*
2418 import_stmt: import_name | import_from
2419 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002420 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002421 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002423 int lineno;
2424 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425 int i;
2426 asdl_seq *aliases;
2427
2428 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002429 lineno = LINENO(n);
2430 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002432 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002433 n = CHILD(n, 1);
2434 REQ(n, dotted_as_names);
2435 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2436 if (!aliases)
2437 return NULL;
2438 for (i = 0; i < NCH(n); i += 2) {
2439 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2440 if (!import_alias)
2441 return NULL;
2442 asdl_seq_SET(aliases, i / 2, import_alias);
2443 }
2444 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002446 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002447 int n_children;
2448 int idx, ndots = 0;
2449 alias_ty mod = NULL;
2450 identifier modname;
2451
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002452 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002453 optional module name */
2454 for (idx = 1; idx < NCH(n); idx++) {
2455 if (TYPE(CHILD(n, idx)) == dotted_name) {
2456 mod = alias_for_import_name(c, CHILD(n, idx));
2457 idx++;
2458 break;
2459 } else if (TYPE(CHILD(n, idx)) != DOT) {
2460 break;
2461 }
2462 ndots++;
2463 }
2464 idx++; /* skip over the 'import' keyword */
2465 switch (TYPE(CHILD(n, idx))) {
2466 case STAR:
2467 /* from ... import * */
2468 n = CHILD(n, idx);
2469 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002470 break;
2471 case LPAR:
2472 /* from ... import (x, y, z) */
2473 n = CHILD(n, idx + 1);
2474 n_children = NCH(n);
2475 break;
2476 case import_as_names:
2477 /* from ... import x, y, z */
2478 n = CHILD(n, idx);
2479 n_children = NCH(n);
2480 if (n_children % 2 == 0) {
2481 ast_error(n, "trailing comma not allowed without"
2482 " surrounding parentheses");
2483 return NULL;
2484 }
2485 break;
2486 default:
2487 ast_error(n, "Unexpected node-type in from-import");
2488 return NULL;
2489 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002491 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2492 if (!aliases)
2493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002495 /* handle "from ... import *" special b/c there's no children */
2496 if (TYPE(n) == STAR) {
2497 alias_ty import_alias = alias_for_import_name(c, n);
2498 if (!import_alias)
2499 return NULL;
2500 asdl_seq_SET(aliases, 0, import_alias);
2501 }
2502 else {
2503 for (i = 0; i < NCH(n); i += 2) {
2504 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2505 if (!import_alias)
2506 return NULL;
2507 asdl_seq_SET(aliases, i / 2, import_alias);
2508 }
2509 }
2510 if (mod != NULL)
2511 modname = mod->name;
2512 else
2513 modname = new_identifier("", c->c_arena);
2514 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2515 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 }
Neal Norwitz79792652005-11-14 04:25:03 +00002517 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002518 "unknown import statement: starts with command '%s'",
2519 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 return NULL;
2521}
2522
2523static stmt_ty
2524ast_for_global_stmt(struct compiling *c, const node *n)
2525{
2526 /* global_stmt: 'global' NAME (',' NAME)* */
2527 identifier name;
2528 asdl_seq *s;
2529 int i;
2530
2531 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002532 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002534 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002536 name = NEW_IDENTIFIER(CHILD(n, i));
2537 if (!name)
2538 return NULL;
2539 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002541 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542}
2543
2544static stmt_ty
2545ast_for_exec_stmt(struct compiling *c, const node *n)
2546{
2547 expr_ty expr1, globals = NULL, locals = NULL;
2548 int n_children = NCH(n);
2549 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002550 PyErr_Format(PyExc_SystemError,
2551 "poorly formed 'exec' statement: %d parts to statement",
2552 n_children);
2553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 }
2555
2556 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2557 REQ(n, exec_stmt);
2558 expr1 = ast_for_expr(c, CHILD(n, 1));
2559 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002560 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002562 globals = ast_for_expr(c, CHILD(n, 3));
2563 if (!globals)
2564 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 }
2566 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002567 locals = ast_for_expr(c, CHILD(n, 5));
2568 if (!locals)
2569 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 }
2571
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002572 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2573 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574}
2575
2576static stmt_ty
2577ast_for_assert_stmt(struct compiling *c, const node *n)
2578{
2579 /* assert_stmt: 'assert' test [',' test] */
2580 REQ(n, assert_stmt);
2581 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002582 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2583 if (!expression)
2584 return NULL;
2585 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2586 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 }
2588 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002589 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002591 expr1 = ast_for_expr(c, CHILD(n, 1));
2592 if (!expr1)
2593 return NULL;
2594 expr2 = ast_for_expr(c, CHILD(n, 3));
2595 if (!expr2)
2596 return NULL;
2597
2598 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 }
Neal Norwitz79792652005-11-14 04:25:03 +00002600 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002601 "improper number of parts to 'assert' statement: %d",
2602 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 return NULL;
2604}
2605
2606static asdl_seq *
2607ast_for_suite(struct compiling *c, const node *n)
2608{
2609 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002610 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 stmt_ty s;
2612 int i, total, num, end, pos = 0;
2613 node *ch;
2614
2615 REQ(n, suite);
2616
2617 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002618 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002620 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002622 n = CHILD(n, 0);
2623 /* simple_stmt always ends with a NEWLINE,
2624 and may have a trailing SEMI
2625 */
2626 end = NCH(n) - 1;
2627 if (TYPE(CHILD(n, end - 1)) == SEMI)
2628 end--;
2629 /* loop by 2 to skip semi-colons */
2630 for (i = 0; i < end; i += 2) {
2631 ch = CHILD(n, i);
2632 s = ast_for_stmt(c, ch);
2633 if (!s)
2634 return NULL;
2635 asdl_seq_SET(seq, pos++, s);
2636 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 }
2638 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002639 for (i = 2; i < (NCH(n) - 1); i++) {
2640 ch = CHILD(n, i);
2641 REQ(ch, stmt);
2642 num = num_stmts(ch);
2643 if (num == 1) {
2644 /* small_stmt or compound_stmt with only one child */
2645 s = ast_for_stmt(c, ch);
2646 if (!s)
2647 return NULL;
2648 asdl_seq_SET(seq, pos++, s);
2649 }
2650 else {
2651 int j;
2652 ch = CHILD(ch, 0);
2653 REQ(ch, simple_stmt);
2654 for (j = 0; j < NCH(ch); j += 2) {
2655 /* statement terminates with a semi-colon ';' */
2656 if (NCH(CHILD(ch, j)) == 0) {
2657 assert((j + 1) == NCH(ch));
2658 break;
2659 }
2660 s = ast_for_stmt(c, CHILD(ch, j));
2661 if (!s)
2662 return NULL;
2663 asdl_seq_SET(seq, pos++, s);
2664 }
2665 }
2666 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 }
2668 assert(pos == seq->size);
2669 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670}
2671
2672static stmt_ty
2673ast_for_if_stmt(struct compiling *c, const node *n)
2674{
2675 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2676 ['else' ':' suite]
2677 */
2678 char *s;
2679
2680 REQ(n, if_stmt);
2681
2682 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002683 expr_ty expression;
2684 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002686 expression = ast_for_expr(c, CHILD(n, 1));
2687 if (!expression)
2688 return NULL;
2689 suite_seq = ast_for_suite(c, CHILD(n, 3));
2690 if (!suite_seq)
2691 return NULL;
2692
2693 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2694 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002696
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 s = STR(CHILD(n, 4));
2698 /* s[2], the third character in the string, will be
2699 's' for el_s_e, or
2700 'i' for el_i_f
2701 */
2702 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002703 expr_ty expression;
2704 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002706 expression = ast_for_expr(c, CHILD(n, 1));
2707 if (!expression)
2708 return NULL;
2709 seq1 = ast_for_suite(c, CHILD(n, 3));
2710 if (!seq1)
2711 return NULL;
2712 seq2 = ast_for_suite(c, CHILD(n, 6));
2713 if (!seq2)
2714 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002716 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2717 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 }
2719 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002720 int i, n_elif, has_else = 0;
2721 expr_ty expression;
2722 asdl_seq *suite_seq;
2723 asdl_seq *orelse = NULL;
2724 n_elif = NCH(n) - 4;
2725 /* must reference the child n_elif+1 since 'else' token is third,
2726 not fourth, child from the end. */
2727 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2728 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2729 has_else = 1;
2730 n_elif -= 3;
2731 }
2732 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002734 if (has_else) {
2735 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002737 orelse = asdl_seq_new(1, c->c_arena);
2738 if (!orelse)
2739 return NULL;
2740 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2741 if (!expression)
2742 return NULL;
2743 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2744 if (!suite_seq)
2745 return NULL;
2746 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2747 if (!suite_seq2)
2748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002750 asdl_seq_SET(orelse, 0,
2751 If(expression, suite_seq, suite_seq2,
2752 LINENO(CHILD(n, NCH(n) - 6)),
2753 CHILD(n, NCH(n) - 6)->n_col_offset,
2754 c->c_arena));
2755 /* the just-created orelse handled the last elif */
2756 n_elif--;
2757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002759 for (i = 0; i < n_elif; i++) {
2760 int off = 5 + (n_elif - i - 1) * 4;
2761 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2762 if (!newobj)
2763 return NULL;
2764 expression = ast_for_expr(c, CHILD(n, off));
2765 if (!expression)
2766 return NULL;
2767 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2768 if (!suite_seq)
2769 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002771 asdl_seq_SET(newobj, 0,
2772 If(expression, suite_seq, orelse,
2773 LINENO(CHILD(n, off)),
2774 CHILD(n, off)->n_col_offset, c->c_arena));
2775 orelse = newobj;
2776 }
2777 expression = ast_for_expr(c, CHILD(n, 1));
2778 if (!expression)
2779 return NULL;
2780 suite_seq = ast_for_suite(c, CHILD(n, 3));
2781 if (!suite_seq)
2782 return NULL;
2783 return If(expression, suite_seq, orelse,
2784 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002786
2787 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002788 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002789 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790}
2791
2792static stmt_ty
2793ast_for_while_stmt(struct compiling *c, const node *n)
2794{
2795 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2796 REQ(n, while_stmt);
2797
2798 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002799 expr_ty expression;
2800 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002802 expression = ast_for_expr(c, CHILD(n, 1));
2803 if (!expression)
2804 return NULL;
2805 suite_seq = ast_for_suite(c, CHILD(n, 3));
2806 if (!suite_seq)
2807 return NULL;
2808 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2809 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 }
2811 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002812 expr_ty expression;
2813 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002815 expression = ast_for_expr(c, CHILD(n, 1));
2816 if (!expression)
2817 return NULL;
2818 seq1 = ast_for_suite(c, CHILD(n, 3));
2819 if (!seq1)
2820 return NULL;
2821 seq2 = ast_for_suite(c, CHILD(n, 6));
2822 if (!seq2)
2823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002825 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2826 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002828
2829 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002830 "wrong number of tokens for 'while' statement: %d",
2831 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002832 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833}
2834
2835static stmt_ty
2836ast_for_for_stmt(struct compiling *c, const node *n)
2837{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002838 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 expr_ty expression;
2840 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002841 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2843 REQ(n, for_stmt);
2844
2845 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002846 seq = ast_for_suite(c, CHILD(n, 8));
2847 if (!seq)
2848 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 }
2850
Neal Norwitzedef2be2006-07-12 05:26:17 +00002851 node_target = CHILD(n, 1);
2852 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002853 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002854 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002855 /* Check the # of children rather than the length of _target, since
2856 for x, in ... has 1 element in _target, but still requires a Tuple. */
2857 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002858 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002860 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002862 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002863 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002864 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002866 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002867 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002869 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002870 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871}
2872
2873static excepthandler_ty
2874ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2875{
Collin Winter62903052007-05-18 23:11:24 +00002876 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 REQ(exc, except_clause);
2878 REQ(body, suite);
2879
2880 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002881 asdl_seq *suite_seq = ast_for_suite(c, body);
2882 if (!suite_seq)
2883 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884
Georg Brandla48f3ab2008-03-30 06:40:17 +00002885 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002886 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 }
2888 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002889 expr_ty expression;
2890 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002892 expression = ast_for_expr(c, CHILD(exc, 1));
2893 if (!expression)
2894 return NULL;
2895 suite_seq = ast_for_suite(c, body);
2896 if (!suite_seq)
2897 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898
Georg Brandla48f3ab2008-03-30 06:40:17 +00002899 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002900 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 }
2902 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002903 asdl_seq *suite_seq;
2904 expr_ty expression;
2905 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2906 if (!e)
2907 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002908 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002909 return NULL;
2910 expression = ast_for_expr(c, CHILD(exc, 1));
2911 if (!expression)
2912 return NULL;
2913 suite_seq = ast_for_suite(c, body);
2914 if (!suite_seq)
2915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916
Georg Brandla48f3ab2008-03-30 06:40:17 +00002917 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002918 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002920
2921 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002922 "wrong number of children for 'except' clause: %d",
2923 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925}
2926
2927static stmt_ty
2928ast_for_try_stmt(struct compiling *c, const node *n)
2929{
Neal Norwitzf599f422005-12-17 21:33:47 +00002930 const int nch = NCH(n);
2931 int n_except = (nch - 3)/3;
2932 asdl_seq *body, *orelse = NULL, *finally = NULL;
2933
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934 REQ(n, try_stmt);
2935
Neal Norwitzf599f422005-12-17 21:33:47 +00002936 body = ast_for_suite(c, CHILD(n, 2));
2937 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939
Neal Norwitzf599f422005-12-17 21:33:47 +00002940 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002941 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2942 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2943 /* we can assume it's an "else",
2944 because nch >= 9 for try-else-finally and
2945 it would otherwise have a type of except_clause */
2946 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2947 if (orelse == NULL)
2948 return NULL;
2949 n_except--;
2950 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002952 finally = ast_for_suite(c, CHILD(n, nch - 1));
2953 if (finally == NULL)
2954 return NULL;
2955 n_except--;
2956 }
2957 else {
2958 /* we can assume it's an "else",
2959 otherwise it would have a type of except_clause */
2960 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2961 if (orelse == NULL)
2962 return NULL;
2963 n_except--;
2964 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002966 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002967 ast_error(n, "malformed 'try' statement");
2968 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002970
2971 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002972 int i;
2973 stmt_ty except_st;
2974 /* process except statements to create a try ... except */
2975 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2976 if (handlers == NULL)
2977 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002978
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002979 for (i = 0; i < n_except; i++) {
2980 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2981 CHILD(n, 5 + i * 3));
2982 if (!e)
2983 return NULL;
2984 asdl_seq_SET(handlers, i, e);
2985 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002986
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002987 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2988 n->n_col_offset, c->c_arena);
2989 if (!finally)
2990 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002991
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002992 /* if a 'finally' is present too, we nest the TryExcept within a
2993 TryFinally to emulate try ... except ... finally */
2994 body = asdl_seq_new(1, c->c_arena);
2995 if (body == NULL)
2996 return NULL;
2997 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002998 }
2999
3000 /* must be a try ... finally (except clauses are in body, if any exist) */
3001 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003002 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003}
3004
Guido van Rossumc2e20742006-02-27 22:32:47 +00003005static expr_ty
3006ast_for_with_var(struct compiling *c, const node *n)
3007{
3008 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003009 return ast_for_expr(c, CHILD(n, 1));
3010}
3011
3012/* with_stmt: 'with' test [ with_var ] ':' suite */
3013static stmt_ty
3014ast_for_with_stmt(struct compiling *c, const node *n)
3015{
3016 expr_ty context_expr, optional_vars = NULL;
3017 int suite_index = 3; /* skip 'with', test, and ':' */
3018 asdl_seq *suite_seq;
3019
3020 assert(TYPE(n) == with_stmt);
3021 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00003022 if (!context_expr)
3023 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003024 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003025 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003026
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003027 if (!optional_vars) {
3028 return NULL;
3029 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003030 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003031 return NULL;
3032 }
3033 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003034 }
3035
3036 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
3037 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003038 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003039 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003040 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003041 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003042}
3043
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003045ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046{
3047 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Benjamin Peterson5cb67632008-11-25 04:00:37 +00003048 PyObject *classname;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049 asdl_seq *bases, *s;
3050
3051 REQ(n, classdef);
3052
Benjamin Petersond5efd202008-06-08 22:52:37 +00003053 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003054 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055
3056 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003057 s = ast_for_suite(c, CHILD(n, 3));
3058 if (!s)
3059 return NULL;
Benjamin Peterson5cb67632008-11-25 04:00:37 +00003060 classname = NEW_IDENTIFIER(CHILD(n, 1));
3061 if (!classname)
3062 return NULL;
3063 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3064 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065 }
3066 /* check for empty base list */
3067 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003068 s = ast_for_suite(c, CHILD(n,5));
3069 if (!s)
Benjamin Peterson5cb67632008-11-25 04:00:37 +00003070 return NULL;
3071 classname = NEW_IDENTIFIER(CHILD(n, 1));
3072 if (!classname)
3073 return NULL;
3074 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3075 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 }
3077
3078 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003079 bases = ast_for_class_bases(c, CHILD(n, 3));
3080 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003081 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082
3083 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003084 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003085 return NULL;
Benjamin Peterson5cb67632008-11-25 04:00:37 +00003086 classname = NEW_IDENTIFIER(CHILD(n, 1));
3087 if (!classname)
3088 return NULL;
3089 return ClassDef(classname, bases, s, decorator_seq,
Christian Heimes5224d282008-02-23 15:01:05 +00003090 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091}
3092
3093static stmt_ty
3094ast_for_stmt(struct compiling *c, const node *n)
3095{
3096 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003097 assert(NCH(n) == 1);
3098 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003099 }
3100 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003101 assert(num_stmts(n) == 1);
3102 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103 }
3104 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003105 REQ(n, small_stmt);
3106 n = CHILD(n, 0);
3107 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3108 | flow_stmt | import_stmt | global_stmt | exec_stmt
3109 | assert_stmt
3110 */
3111 switch (TYPE(n)) {
3112 case expr_stmt:
3113 return ast_for_expr_stmt(c, n);
3114 case print_stmt:
3115 return ast_for_print_stmt(c, n);
3116 case del_stmt:
3117 return ast_for_del_stmt(c, n);
3118 case pass_stmt:
3119 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3120 case flow_stmt:
3121 return ast_for_flow_stmt(c, n);
3122 case import_stmt:
3123 return ast_for_import_stmt(c, n);
3124 case global_stmt:
3125 return ast_for_global_stmt(c, n);
3126 case exec_stmt:
3127 return ast_for_exec_stmt(c, n);
3128 case assert_stmt:
3129 return ast_for_assert_stmt(c, n);
3130 default:
3131 PyErr_Format(PyExc_SystemError,
3132 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3133 TYPE(n), NCH(n));
3134 return NULL;
3135 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 }
3137 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003138 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003139 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003140 */
3141 node *ch = CHILD(n, 0);
3142 REQ(n, compound_stmt);
3143 switch (TYPE(ch)) {
3144 case if_stmt:
3145 return ast_for_if_stmt(c, ch);
3146 case while_stmt:
3147 return ast_for_while_stmt(c, ch);
3148 case for_stmt:
3149 return ast_for_for_stmt(c, ch);
3150 case try_stmt:
3151 return ast_for_try_stmt(c, ch);
3152 case with_stmt:
3153 return ast_for_with_stmt(c, ch);
3154 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003155 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003156 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003157 return ast_for_classdef(c, ch, NULL);
3158 case decorated:
3159 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003160 default:
3161 PyErr_Format(PyExc_SystemError,
3162 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3163 TYPE(n), NCH(n));
3164 return NULL;
3165 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 }
3167}
3168
3169static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003170parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003172 const char *end;
3173 long x;
3174 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003176 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003177 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178#endif
3179
Mark Dickinson514624d2008-12-05 18:01:46 +00003180 assert(s != NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003181 errno = 0;
3182 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003184 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003186 if (*end == 'l' || *end == 'L')
3187 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003188 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003189 if (*end == '\0') {
3190 if (errno != 0)
3191 return PyLong_FromString((char *)s, (char **)0, 0);
3192 return PyInt_FromLong(x);
3193 }
3194 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003196 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003197 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003198 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003199 complex.imag = PyOS_ascii_atof(s);
3200 PyFPE_END_PROTECT(complex)
3201 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003202 }
3203 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003205 {
3206 PyFPE_START_PROTECT("atof", return 0)
3207 dx = PyOS_ascii_atof(s);
3208 PyFPE_END_PROTECT(dx)
3209 return PyFloat_FromDouble(dx);
3210 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211}
3212
3213static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003214decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215{
3216#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003217 Py_FatalError("decode_utf8 should not be called in this build.");
3218 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003220 PyObject *u, *v;
3221 char *s, *t;
3222 t = s = (char *)*sPtr;
3223 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3224 while (s < end && (*s & 0x80)) s++;
3225 *sPtr = s;
3226 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3227 if (u == NULL)
3228 return NULL;
3229 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3230 Py_DECREF(u);
3231 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232#endif
3233}
3234
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003235#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003237decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003239 PyObject *v, *u;
3240 char *buf;
3241 char *p;
3242 const char *end;
3243 if (encoding == NULL) {
3244 buf = (char *)s;
3245 u = NULL;
3246 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3247 buf = (char *)s;
3248 u = NULL;
3249 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003250 /* check for integer overflow */
3251 if (len > PY_SIZE_MAX / 4)
3252 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003253 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003254 u = PyString_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003255 if (u == NULL)
3256 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003257 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003258 end = s + len;
3259 while (s < end) {
3260 if (*s == '\\') {
3261 *p++ = *s++;
3262 if (*s & 0x80) {
3263 strcpy(p, "u005c");
3264 p += 5;
3265 }
3266 }
3267 if (*s & 0x80) { /* XXX inefficient */
3268 PyObject *w;
3269 char *r;
3270 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003271 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003272 if (w == NULL) {
3273 Py_DECREF(u);
3274 return NULL;
3275 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003276 r = PyString_AsString(w);
3277 rn = PyString_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003278 assert(rn % 2 == 0);
3279 for (i = 0; i < rn; i += 2) {
3280 sprintf(p, "\\u%02x%02x",
3281 r[i + 0] & 0xFF,
3282 r[i + 1] & 0xFF);
3283 p += 6;
3284 }
3285 Py_DECREF(w);
3286 } else {
3287 *p++ = *s++;
3288 }
3289 }
3290 len = p - buf;
3291 s = buf;
3292 }
3293 if (rawmode)
3294 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3295 else
3296 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3297 Py_XDECREF(u);
3298 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003300#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301
3302/* s is a Python string literal, including the bracketing quote characters,
3303 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3304 * parsestr parses it, and returns the decoded Python string object.
3305 */
3306static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003307parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003309 size_t len;
3310 int quote = Py_CHARMASK(*s);
3311 int rawmode = 0;
3312 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003313 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003315 if (isalpha(quote) || quote == '_') {
3316 if (quote == 'u' || quote == 'U') {
3317 quote = *++s;
3318 unicode = 1;
3319 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003320 if (quote == 'b' || quote == 'B') {
3321 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003322 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003323 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003324 if (quote == 'r' || quote == 'R') {
3325 quote = *++s;
3326 rawmode = 1;
3327 }
3328 }
3329 if (quote != '\'' && quote != '\"') {
3330 PyErr_BadInternalCall();
3331 return NULL;
3332 }
3333 s++;
3334 len = strlen(s);
3335 if (len > INT_MAX) {
3336 PyErr_SetString(PyExc_OverflowError,
3337 "string to parse is too long");
3338 return NULL;
3339 }
3340 if (s[--len] != quote) {
3341 PyErr_BadInternalCall();
3342 return NULL;
3343 }
3344 if (len >= 4 && s[0] == quote && s[1] == quote) {
3345 s += 2;
3346 len -= 2;
3347 if (s[--len] != quote || s[--len] != quote) {
3348 PyErr_BadInternalCall();
3349 return NULL;
3350 }
3351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003353 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003354 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003355 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003357 need_encoding = (c->c_encoding != NULL &&
3358 strcmp(c->c_encoding, "utf-8") != 0 &&
3359 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003360 if (rawmode || strchr(s, '\\') == NULL) {
3361 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003363 /* This should not happen - we never see any other
3364 encoding. */
3365 Py_FatalError(
3366 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003368 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3369 if (u == NULL)
3370 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003371 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003372 Py_DECREF(u);
3373 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003375 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003376 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003377 }
3378 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003380 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003381 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382}
3383
3384/* Build a Python string object out of a STRING atom. This takes care of
3385 * compile-time literal catenation, calling parsestr() on each piece, and
3386 * pasting the intermediate results together.
3387 */
3388static PyObject *
3389parsestrplus(struct compiling *c, const node *n)
3390{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003391 PyObject *v;
3392 int i;
3393 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003394 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003395 /* String literal concatenation */
3396 for (i = 1; i < NCH(n); i++) {
3397 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003398 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003399 if (s == NULL)
3400 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003401 if (PyString_Check(v) && PyString_Check(s)) {
3402 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003403 if (v == NULL)
3404 goto onError;
3405 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003407 else {
3408 PyObject *temp = PyUnicode_Concat(v, s);
3409 Py_DECREF(s);
3410 Py_DECREF(v);
3411 v = temp;
3412 if (v == NULL)
3413 goto onError;
3414 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003416 }
3417 }
3418 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419
3420 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003421 Py_XDECREF(v);
3422 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423}