blob: b3f4fcb6c08748c519f1406b7f02e4e0c5c82e1c [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 Peterson9e6310d2008-11-25 03:43:14 +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"))
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000134 return ast_error(n, "cannot assign to None");
135 if (!strcmp(x, "__debug__"))
136 return ast_error(n, "cannot assign to __debug__");
Benjamin Peterson399b1fe2008-10-25 02:53:28 +0000137 if (Py_Py3kWarningFlag) {
138 if (!(strcmp(x, "True") && strcmp(x, "False")) &&
139 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
140 return 0;
141 if (!strcmp(x, "nonlocal") &&
142 !ast_warn(c, n, "nonlocal is a keyword in 3.x"))
143 return 0;
144 }
Benjamin Petersond5efd202008-06-08 22:52:37 +0000145 return 1;
146}
147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148/* num_stmts() returns number of contained statements.
149
150 Use this routine to determine how big a sequence is needed for
151 the statements in a parse tree. Its raison d'etre is this bit of
152 grammar:
153
154 stmt: simple_stmt | compound_stmt
155 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
156
157 A simple_stmt can contain multiple small_stmt elements joined
158 by semicolons. If the arg is a simple_stmt, the number of
159 small_stmt elements is returned.
160*/
161
162static int
163num_stmts(const node *n)
164{
165 int i, l;
166 node *ch;
167
168 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000169 case single_input:
170 if (TYPE(CHILD(n, 0)) == NEWLINE)
171 return 0;
172 else
173 return num_stmts(CHILD(n, 0));
174 case file_input:
175 l = 0;
176 for (i = 0; i < NCH(n); i++) {
177 ch = CHILD(n, i);
178 if (TYPE(ch) == stmt)
179 l += num_stmts(ch);
180 }
181 return l;
182 case stmt:
183 return num_stmts(CHILD(n, 0));
184 case compound_stmt:
185 return 1;
186 case simple_stmt:
187 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
188 case suite:
189 if (NCH(n) == 1)
190 return num_stmts(CHILD(n, 0));
191 else {
192 l = 0;
193 for (i = 2; i < (NCH(n) - 1); i++)
194 l += num_stmts(CHILD(n, i));
195 return l;
196 }
197 default: {
198 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +0000200 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000201 TYPE(n), NCH(n));
202 Py_FatalError(buf);
203 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204 }
205 assert(0);
206 return 0;
207}
208
209/* Transform the CST rooted at node * to the appropriate AST
210*/
211
212mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000213PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000214 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000216 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 asdl_seq *stmts = NULL;
218 stmt_ty s;
219 node *ch;
220 struct compiling c;
221
222 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000223 c.c_encoding = "utf-8";
224 if (TYPE(n) == encoding_decl) {
225 ast_error(n, "encoding declaration in Unicode string");
226 goto error;
227 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000229 c.c_encoding = STR(n);
230 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000232 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 }
Christian Heimes3c608332008-03-26 22:01:37 +0000234 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000235 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000236 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237
Jeremy Hyltona8293132006-02-28 17:58:27 +0000238 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000240 case file_input:
241 stmts = asdl_seq_new(num_stmts(n), arena);
242 if (!stmts)
243 return NULL;
244 for (i = 0; i < NCH(n) - 1; i++) {
245 ch = CHILD(n, i);
246 if (TYPE(ch) == NEWLINE)
247 continue;
248 REQ(ch, stmt);
249 num = num_stmts(ch);
250 if (num == 1) {
251 s = ast_for_stmt(&c, ch);
252 if (!s)
253 goto error;
254 asdl_seq_SET(stmts, k++, s);
255 }
256 else {
257 ch = CHILD(ch, 0);
258 REQ(ch, simple_stmt);
259 for (j = 0; j < num; j++) {
260 s = ast_for_stmt(&c, CHILD(ch, j * 2));
261 if (!s)
262 goto error;
263 asdl_seq_SET(stmts, k++, s);
264 }
265 }
266 }
267 return Module(stmts, arena);
268 case eval_input: {
269 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000271 /* XXX Why not gen_for here? */
272 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
273 if (!testlist_ast)
274 goto error;
275 return Expression(testlist_ast, arena);
276 }
277 case single_input:
278 if (TYPE(CHILD(n, 0)) == NEWLINE) {
279 stmts = asdl_seq_new(1, arena);
280 if (!stmts)
281 goto error;
282 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
283 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000284 if (!asdl_seq_GET(stmts, 0))
285 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000286 return Interactive(stmts, arena);
287 }
288 else {
289 n = CHILD(n, 0);
290 num = num_stmts(n);
291 stmts = asdl_seq_new(num, arena);
292 if (!stmts)
293 goto error;
294 if (num == 1) {
295 s = ast_for_stmt(&c, n);
296 if (!s)
297 goto error;
298 asdl_seq_SET(stmts, 0, s);
299 }
300 else {
301 /* Only a simple_stmt can contain multiple statements. */
302 REQ(n, simple_stmt);
303 for (i = 0; i < NCH(n); i += 2) {
304 if (TYPE(CHILD(n, i)) == NEWLINE)
305 break;
306 s = ast_for_stmt(&c, CHILD(n, i));
307 if (!s)
308 goto error;
309 asdl_seq_SET(stmts, i / 2, s);
310 }
311 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000313 return Interactive(stmts, arena);
314 }
315 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000316 PyErr_Format(PyExc_SystemError,
317 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000318 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319 }
320 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 ast_error_finish(filename);
322 return NULL;
323}
324
325/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
326*/
327
328static operator_ty
329get_operator(const node *n)
330{
331 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000332 case VBAR:
333 return BitOr;
334 case CIRCUMFLEX:
335 return BitXor;
336 case AMPER:
337 return BitAnd;
338 case LEFTSHIFT:
339 return LShift;
340 case RIGHTSHIFT:
341 return RShift;
342 case PLUS:
343 return Add;
344 case MINUS:
345 return Sub;
346 case STAR:
347 return Mult;
348 case SLASH:
349 return Div;
350 case DOUBLESLASH:
351 return FloorDiv;
352 case PERCENT:
353 return Mod;
354 default:
355 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356 }
357}
358
Jeremy Hyltona8293132006-02-28 17:58:27 +0000359/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360
361 Only sets context for expr kinds that "can appear in assignment context"
362 (according to ../Parser/Python.asdl). For other expr kinds, it sets
363 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364*/
365
366static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000367set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368{
369 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000370 /* If a particular expression type can't be used for assign / delete,
371 set expr_name to its name and an error message will be generated.
372 */
373 const char* expr_name = NULL;
374
375 /* The ast defines augmented store and load contexts, but the
376 implementation here doesn't actually use them. The code may be
377 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000378 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000380 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000381 */
382 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383
384 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000385 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000386 if (ctx == Store && !forbidden_check(c, n,
387 PyBytes_AS_STRING(e->v.Attribute.attr)))
388 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000389 e->v.Attribute.ctx = ctx;
390 break;
391 case Subscript_kind:
392 e->v.Subscript.ctx = ctx;
393 break;
394 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000395 if (ctx == Store && !forbidden_check(c, n,
396 PyBytes_AS_STRING(e->v.Name.id)))
397 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000398 e->v.Name.ctx = ctx;
399 break;
400 case List_kind:
401 e->v.List.ctx = ctx;
402 s = e->v.List.elts;
403 break;
404 case Tuple_kind:
405 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
406 return ast_error(n, "can't assign to ()");
407 e->v.Tuple.ctx = ctx;
408 s = e->v.Tuple.elts;
409 break;
410 case Lambda_kind:
411 expr_name = "lambda";
412 break;
413 case Call_kind:
414 expr_name = "function call";
415 break;
416 case BoolOp_kind:
417 case BinOp_kind:
418 case UnaryOp_kind:
419 expr_name = "operator";
420 break;
421 case GeneratorExp_kind:
422 expr_name = "generator expression";
423 break;
424 case Yield_kind:
425 expr_name = "yield expression";
426 break;
427 case ListComp_kind:
428 expr_name = "list comprehension";
429 break;
430 case Dict_kind:
431 case Num_kind:
432 case Str_kind:
433 expr_name = "literal";
434 break;
435 case Compare_kind:
436 expr_name = "comparison";
437 break;
438 case Repr_kind:
439 expr_name = "repr";
440 break;
441 case IfExp_kind:
442 expr_name = "conditional expression";
443 break;
444 default:
445 PyErr_Format(PyExc_SystemError,
446 "unexpected expression in assignment %d (line %d)",
447 e->kind, e->lineno);
448 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000450 /* Check for error string set by switch */
451 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000452 char buf[300];
453 PyOS_snprintf(buf, sizeof(buf),
454 "can't %s %s",
455 ctx == Store ? "assign to" : "delete",
456 expr_name);
457 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000458 }
459
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000461 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462 */
463 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000464 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000466 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000467 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000468 return 0;
469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470 }
471 return 1;
472}
473
474static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000475ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476{
477 REQ(n, augassign);
478 n = CHILD(n, 0);
479 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000480 case '+':
481 return Add;
482 case '-':
483 return Sub;
484 case '/':
485 if (STR(n)[1] == '/')
486 return FloorDiv;
487 else
488 return Div;
489 case '%':
490 return Mod;
491 case '<':
492 return LShift;
493 case '>':
494 return RShift;
495 case '&':
496 return BitAnd;
497 case '^':
498 return BitXor;
499 case '|':
500 return BitOr;
501 case '*':
502 if (STR(n)[1] == '*')
503 return Pow;
504 else
505 return Mult;
506 default:
507 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
508 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 }
510}
511
512static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000513ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514{
515 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000516 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 */
518 REQ(n, comp_op);
519 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000520 n = CHILD(n, 0);
521 switch (TYPE(n)) {
522 case LESS:
523 return Lt;
524 case GREATER:
525 return Gt;
526 case EQEQUAL: /* == */
527 return Eq;
528 case LESSEQUAL:
529 return LtE;
530 case GREATEREQUAL:
531 return GtE;
532 case NOTEQUAL:
533 return NotEq;
534 case NAME:
535 if (strcmp(STR(n), "in") == 0)
536 return In;
537 if (strcmp(STR(n), "is") == 0)
538 return Is;
539 default:
540 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
541 STR(n));
542 return (cmpop_ty)0;
543 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544 }
545 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000546 /* handle "not in" and "is not" */
547 switch (TYPE(CHILD(n, 0))) {
548 case NAME:
549 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
550 return NotIn;
551 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
552 return IsNot;
553 default:
554 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
555 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
556 return (cmpop_ty)0;
557 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558 }
Neal Norwitz79792652005-11-14 04:25:03 +0000559 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000560 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000561 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562}
563
564static asdl_seq *
565seq_for_testlist(struct compiling *c, const node *n)
566{
567 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000568 asdl_seq *seq;
569 expr_ty expression;
570 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000571 assert(TYPE(n) == testlist ||
572 TYPE(n) == listmaker ||
573 TYPE(n) == testlist_gexp ||
574 TYPE(n) == testlist_safe ||
575 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000577 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000579 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
581 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000582 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000584 expression = ast_for_expr(c, CHILD(n, i));
585 if (!expression)
586 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000588 assert(i / 2 < seq->size);
589 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 }
591 return seq;
592}
593
594static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000595compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596{
597 int i, len = (NCH(n) + 1) / 2;
598 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000599 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000601 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Neal Norwitz3a230172006-09-22 08:18:10 +0000603 /* fpdef: NAME | '(' fplist ')'
604 fplist: fpdef (',' fpdef)* [',']
605 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607 for (i = 0; i < len; i++) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000608 PyObject *arg_id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000609 const node *fpdef_node = CHILD(n, 2*i);
610 const node *child;
611 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000612set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000613 /* fpdef_node is either a NAME or an fplist */
614 child = CHILD(fpdef_node, 0);
615 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000616 if (!forbidden_check(c, n, STR(child)))
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000617 return NULL;
618 arg_id = NEW_IDENTIFIER(child);
619 if (!arg_id)
620 return NULL;
621 arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
622 c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000623 }
624 else {
625 assert(TYPE(fpdef_node) == fpdef);
626 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
627 child = CHILD(fpdef_node, 1);
628 assert(TYPE(child) == fplist);
629 /* NCH == 1 means we have (x), we need to elide the extra parens */
630 if (NCH(child) == 1) {
631 fpdef_node = CHILD(child, 0);
632 assert(TYPE(fpdef_node) == fpdef);
633 goto set_name;
634 }
635 arg = compiler_complex_args(c, child);
636 }
637 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 }
639
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000640 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000641 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000642 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 return result;
644}
645
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646
Jeremy Hyltona8293132006-02-28 17:58:27 +0000647/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648
649static arguments_ty
650ast_for_arguments(struct compiling *c, const node *n)
651{
652 /* parameters: '(' [varargslist] ')'
653 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000654 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000656 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657 asdl_seq *args, *defaults;
658 identifier vararg = NULL, kwarg = NULL;
659 node *ch;
660
661 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000662 if (NCH(n) == 2) /* () as argument list */
663 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
664 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 }
666 REQ(n, varargslist);
667
668 /* first count the number of normal args & defaults */
669 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000670 ch = CHILD(n, i);
671 if (TYPE(ch) == fpdef)
672 n_args++;
673 if (TYPE(ch) == EQUAL)
674 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000676 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000678 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000679 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000681 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
683 /* fpdef: NAME | '(' fplist ')'
684 fplist: fpdef (',' fpdef)* [',']
685 */
686 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000687 j = 0; /* index for defaults */
688 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000690 ch = CHILD(n, i);
691 switch (TYPE(ch)) {
692 case fpdef:
693 handle_fpdef:
694 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
695 anything other than EQUAL or a comma? */
696 /* XXX Should NCH(n) check be made a separate check? */
697 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
698 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
699 if (!expression)
700 goto error;
701 assert(defaults != NULL);
702 asdl_seq_SET(defaults, j++, expression);
703 i += 2;
704 found_default = 1;
705 }
706 else if (found_default) {
707 ast_error(n,
708 "non-default argument follows default argument");
709 goto error;
710 }
711 if (NCH(ch) == 3) {
712 ch = CHILD(ch, 1);
713 /* def foo((x)): is not complex, special case. */
714 if (NCH(ch) != 1) {
715 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000716 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
717 "tuple parameter unpacking has been removed in 3.x"))
718 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000719 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000720 if (!asdl_seq_GET(args, k-1))
721 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000722 } else {
723 /* def foo((x)): setup for checking NAME below. */
724 /* Loop because there can be many parens and tuple
725 unpacking mixed in. */
726 ch = CHILD(ch, 0);
727 assert(TYPE(ch) == fpdef);
728 goto handle_fpdef;
729 }
730 }
731 if (TYPE(CHILD(ch, 0)) == NAME) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000732 PyObject *id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000733 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000734 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000735 goto error;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000736 id = NEW_IDENTIFIER(CHILD(ch, 0));
737 if (!id)
738 goto error;
739 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000740 c->c_arena);
741 if (!name)
742 goto error;
743 asdl_seq_SET(args, k++, name);
744
745 }
746 i += 2; /* the name and the comma */
747 break;
748 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000749 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000750 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000751 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000752 if (!vararg)
753 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000754 i += 3;
755 break;
756 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000757 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000758 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000759 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000760 if (!kwarg)
761 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000762 i += 3;
763 break;
764 default:
765 PyErr_Format(PyExc_SystemError,
766 "unexpected node in varargslist: %d @ %d",
767 TYPE(ch), i);
768 goto error;
769 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 }
771
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000772 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773
774 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000775 Py_XDECREF(vararg);
776 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 return NULL;
778}
779
780static expr_ty
781ast_for_dotted_name(struct compiling *c, const node *n)
782{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000783 expr_ty e;
784 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000785 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 int i;
787
788 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000789
790 lineno = LINENO(n);
791 col_offset = n->n_col_offset;
792
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 id = NEW_IDENTIFIER(CHILD(n, 0));
794 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000795 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000796 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799
800 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000801 id = NEW_IDENTIFIER(CHILD(n, i));
802 if (!id)
803 return NULL;
804 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
805 if (!e)
806 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808
809 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810}
811
812static expr_ty
813ast_for_decorator(struct compiling *c, const node *n)
814{
815 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
816 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000817 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818
819 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000820 REQ(CHILD(n, 0), AT);
821 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822
823 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
824 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000825 return NULL;
826
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000828 d = name_expr;
829 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 }
831 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000832 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
833 n->n_col_offset, c->c_arena);
834 if (!d)
835 return NULL;
836 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 }
838 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000839 d = ast_for_call(c, CHILD(n, 3), name_expr);
840 if (!d)
841 return NULL;
842 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 }
844
845 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846}
847
848static asdl_seq*
849ast_for_decorators(struct compiling *c, const node *n)
850{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000851 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000852 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 int i;
854
855 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000856 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000858 return NULL;
859
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000861 d = ast_for_decorator(c, CHILD(n, i));
862 if (!d)
863 return NULL;
864 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 }
866 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867}
868
869static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000870ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871{
Christian Heimes5224d282008-02-23 15:01:05 +0000872 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000873 identifier name;
874 arguments_ty args;
875 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000876 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877
878 REQ(n, funcdef);
879
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 name = NEW_IDENTIFIER(CHILD(n, name_i));
881 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000882 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000883 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000884 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885 args = ast_for_arguments(c, CHILD(n, name_i + 1));
886 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000887 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 body = ast_for_suite(c, CHILD(n, name_i + 3));
889 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000892 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000893 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894}
895
Christian Heimes5224d282008-02-23 15:01:05 +0000896static stmt_ty
897ast_for_decorated(struct compiling *c, const node *n)
898{
899 /* decorated: decorators (classdef | funcdef) */
900 stmt_ty thing = NULL;
901 asdl_seq *decorator_seq = NULL;
902
903 REQ(n, decorated);
904
905 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
906 if (!decorator_seq)
907 return NULL;
908
909 assert(TYPE(CHILD(n, 1)) == funcdef ||
910 TYPE(CHILD(n, 1)) == classdef);
911
912 if (TYPE(CHILD(n, 1)) == funcdef) {
913 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
914 } else if (TYPE(CHILD(n, 1)) == classdef) {
915 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
916 }
917 /* we count the decorators in when talking about the class' or
918 function's line number */
919 if (thing) {
920 thing->lineno = LINENO(n);
921 thing->col_offset = n->n_col_offset;
922 }
923 return thing;
924}
925
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926static expr_ty
927ast_for_lambdef(struct compiling *c, const node *n)
928{
929 /* lambdef: 'lambda' [varargslist] ':' test */
930 arguments_ty args;
931 expr_ty expression;
932
933 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000934 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
935 if (!args)
936 return NULL;
937 expression = ast_for_expr(c, CHILD(n, 2));
938 if (!expression)
939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 }
941 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000942 args = ast_for_arguments(c, CHILD(n, 1));
943 if (!args)
944 return NULL;
945 expression = ast_for_expr(c, CHILD(n, 3));
946 if (!expression)
947 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 }
949
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000950 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951}
952
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000953static expr_ty
954ast_for_ifexpr(struct compiling *c, const node *n)
955{
956 /* test: or_test 'if' or_test 'else' test */
957 expr_ty expression, body, orelse;
958
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000959 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000960 body = ast_for_expr(c, CHILD(n, 0));
961 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000962 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000963 expression = ast_for_expr(c, CHILD(n, 2));
964 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000965 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000966 orelse = ast_for_expr(c, CHILD(n, 4));
967 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000968 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000969 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000970 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000971}
972
Neal Norwitze4d4f002006-09-05 03:58:26 +0000973/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
974 so there is only a single version. Possibly for loops can also re-use
975 the code.
976*/
977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978/* Count the number of 'for' loop in a list comprehension.
979
980 Helper for ast_for_listcomp().
981*/
982
983static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000984count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985{
986 int n_fors = 0;
987 node *ch = CHILD(n, 1);
988
989 count_list_for:
990 n_fors++;
991 REQ(ch, list_for);
992 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000993 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000995 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 count_list_iter:
997 REQ(ch, list_iter);
998 ch = CHILD(ch, 0);
999 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001000 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001002 if (NCH(ch) == 3) {
1003 ch = CHILD(ch, 2);
1004 goto count_list_iter;
1005 }
1006 else
1007 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001009
1010 /* Should never be reached */
1011 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1012 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013}
1014
1015/* Count the number of 'if' statements in a list comprehension.
1016
1017 Helper for ast_for_listcomp().
1018*/
1019
1020static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001021count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022{
1023 int n_ifs = 0;
1024
1025 count_list_iter:
1026 REQ(n, list_iter);
1027 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001028 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 n = CHILD(n, 0);
1030 REQ(n, list_if);
1031 n_ifs++;
1032 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001033 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 n = CHILD(n, 2);
1035 goto count_list_iter;
1036}
1037
1038static expr_ty
1039ast_for_listcomp(struct compiling *c, const node *n)
1040{
1041 /* listmaker: test ( list_for | (',' test)* [','] )
1042 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1043 list_iter: list_for | list_if
1044 list_if: 'if' test [list_iter]
1045 testlist_safe: test [(',' test)+ [',']]
1046 */
1047 expr_ty elt;
1048 asdl_seq *listcomps;
1049 int i, n_fors;
1050 node *ch;
1051
1052 REQ(n, listmaker);
1053 assert(NCH(n) > 1);
1054
1055 elt = ast_for_expr(c, CHILD(n, 0));
1056 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001057 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001059 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001063 listcomps = asdl_seq_new(n_fors, c->c_arena);
1064 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001065 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001066
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 ch = CHILD(n, 1);
1068 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001069 comprehension_ty lc;
1070 asdl_seq *t;
1071 expr_ty expression;
1072 node *for_ch;
1073
1074 REQ(ch, list_for);
1075
1076 for_ch = CHILD(ch, 1);
1077 t = ast_for_exprlist(c, for_ch, Store);
1078 if (!t)
1079 return NULL;
1080 expression = ast_for_testlist(c, CHILD(ch, 3));
1081 if (!expression)
1082 return NULL;
1083
1084 /* Check the # of children rather than the length of t, since
1085 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1086 */
1087 if (NCH(for_ch) == 1)
1088 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1089 c->c_arena);
1090 else
1091 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1092 c->c_arena),
1093 expression, NULL, c->c_arena);
1094 if (!lc)
1095 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001097 if (NCH(ch) == 5) {
1098 int j, n_ifs;
1099 asdl_seq *ifs;
1100 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001102 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001103 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001104 if (n_ifs == -1)
1105 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001107 ifs = asdl_seq_new(n_ifs, c->c_arena);
1108 if (!ifs)
1109 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001111 for (j = 0; j < n_ifs; j++) {
1112 REQ(ch, list_iter);
1113 ch = CHILD(ch, 0);
1114 REQ(ch, list_if);
1115
1116 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1117 if (!list_for_expr)
1118 return NULL;
1119
1120 asdl_seq_SET(ifs, j, list_for_expr);
1121 if (NCH(ch) == 3)
1122 ch = CHILD(ch, 2);
1123 }
1124 /* on exit, must guarantee that ch is a list_for */
1125 if (TYPE(ch) == list_iter)
1126 ch = CHILD(ch, 0);
1127 lc->ifs = ifs;
1128 }
1129 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 }
1131
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001132 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133}
1134
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001135/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136
1137 Helper for ast_for_genexp().
1138*/
1139
1140static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001141count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001143 int n_fors = 0;
1144 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145
1146 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001147 n_fors++;
1148 REQ(ch, gen_for);
1149 if (NCH(ch) == 5)
1150 ch = CHILD(ch, 4);
1151 else
1152 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001154 REQ(ch, gen_iter);
1155 ch = CHILD(ch, 0);
1156 if (TYPE(ch) == gen_for)
1157 goto count_gen_for;
1158 else if (TYPE(ch) == gen_if) {
1159 if (NCH(ch) == 3) {
1160 ch = CHILD(ch, 2);
1161 goto count_gen_iter;
1162 }
1163 else
1164 return n_fors;
1165 }
1166
1167 /* Should never be reached */
1168 PyErr_SetString(PyExc_SystemError,
1169 "logic error in count_gen_fors");
1170 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171}
1172
1173/* Count the number of 'if' statements in a generator expression.
1174
1175 Helper for ast_for_genexp().
1176*/
1177
1178static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001179count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001181 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001183 while (1) {
1184 REQ(n, gen_iter);
1185 if (TYPE(CHILD(n, 0)) == gen_for)
1186 return n_ifs;
1187 n = CHILD(n, 0);
1188 REQ(n, gen_if);
1189 n_ifs++;
1190 if (NCH(n) == 2)
1191 return n_ifs;
1192 n = CHILD(n, 2);
1193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194}
1195
Jeremy Hyltona8293132006-02-28 17:58:27 +00001196/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197static expr_ty
1198ast_for_genexp(struct compiling *c, const node *n)
1199{
1200 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001201 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 expr_ty elt;
1203 asdl_seq *genexps;
1204 int i, n_fors;
1205 node *ch;
1206
1207 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1208 assert(NCH(n) > 1);
1209
1210 elt = ast_for_expr(c, CHILD(n, 0));
1211 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001214 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001216 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001217
1218 genexps = asdl_seq_new(n_fors, c->c_arena);
1219 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001220 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001221
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 ch = CHILD(n, 1);
1223 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001224 comprehension_ty ge;
1225 asdl_seq *t;
1226 expr_ty expression;
1227 node *for_ch;
1228
1229 REQ(ch, gen_for);
1230
1231 for_ch = CHILD(ch, 1);
1232 t = ast_for_exprlist(c, for_ch, Store);
1233 if (!t)
1234 return NULL;
1235 expression = ast_for_expr(c, CHILD(ch, 3));
1236 if (!expression)
1237 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001238
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001239 /* Check the # of children rather than the length of t, since
1240 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1241 if (NCH(for_ch) == 1)
1242 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1243 NULL, c->c_arena);
1244 else
1245 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1246 c->c_arena),
1247 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001248
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001249 if (!ge)
1250 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001251
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001252 if (NCH(ch) == 5) {
1253 int j, n_ifs;
1254 asdl_seq *ifs;
1255
1256 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001257 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001258 if (n_ifs == -1)
1259 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001260
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001261 ifs = asdl_seq_new(n_ifs, c->c_arena);
1262 if (!ifs)
1263 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001264
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001265 for (j = 0; j < n_ifs; j++) {
1266 REQ(ch, gen_iter);
1267 ch = CHILD(ch, 0);
1268 REQ(ch, gen_if);
1269
1270 expression = ast_for_expr(c, CHILD(ch, 1));
1271 if (!expression)
1272 return NULL;
1273 asdl_seq_SET(ifs, j, expression);
1274 if (NCH(ch) == 3)
1275 ch = CHILD(ch, 2);
1276 }
1277 /* on exit, must guarantee that ch is a gen_for */
1278 if (TYPE(ch) == gen_iter)
1279 ch = CHILD(ch, 0);
1280 ge->ifs = ifs;
1281 }
1282 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 }
1284
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001285 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286}
1287
1288static expr_ty
1289ast_for_atom(struct compiling *c, const node *n)
1290{
1291 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1292 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1293 */
1294 node *ch = CHILD(n, 0);
1295
1296 switch (TYPE(ch)) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001297 case NAME: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001298 /* All names start in Load context, but may later be
1299 changed. */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001300 PyObject *name = NEW_IDENTIFIER(ch);
1301 if (!name)
1302 return NULL;
1303 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1304 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001306 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001307 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001308#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001309 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1310 PyObject *type, *value, *tback, *errstr;
1311 PyErr_Fetch(&type, &value, &tback);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001312 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001313 if (errstr) {
1314 char *s = "";
1315 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001316 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001317 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1318 ast_error(n, buf);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001319 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001320 } else {
1321 ast_error(n, "(unicode error) unknown error");
1322 }
1323 Py_DECREF(type);
1324 Py_DECREF(value);
1325 Py_XDECREF(tback);
1326 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001327#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001328 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001329 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001330 PyArena_AddPyObject(c->c_arena, str);
1331 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 }
1333 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001334 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001335 if (!pynum)
1336 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001337
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001338 PyArena_AddPyObject(c->c_arena, pynum);
1339 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 }
1341 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001342 ch = CHILD(n, 1);
1343
1344 if (TYPE(ch) == RPAR)
1345 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1346
1347 if (TYPE(ch) == yield_expr)
1348 return ast_for_expr(c, ch);
1349
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001350 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001352 ch = CHILD(n, 1);
1353
1354 if (TYPE(ch) == RSQB)
1355 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1356
1357 REQ(ch, listmaker);
1358 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1359 asdl_seq *elts = seq_for_testlist(c, ch);
1360 if (!elts)
1361 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001362
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001363 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1364 }
1365 else
1366 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001368 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1369 int i, size;
1370 asdl_seq *keys, *values;
1371
1372 ch = CHILD(n, 1);
1373 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1374 keys = asdl_seq_new(size, c->c_arena);
1375 if (!keys)
1376 return NULL;
1377
1378 values = asdl_seq_new(size, c->c_arena);
1379 if (!values)
1380 return NULL;
1381
1382 for (i = 0; i < NCH(ch); i += 4) {
1383 expr_ty expression;
1384
1385 expression = ast_for_expr(c, CHILD(ch, i));
1386 if (!expression)
1387 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001388
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001389 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001390
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001391 expression = ast_for_expr(c, CHILD(ch, i + 2));
1392 if (!expression)
1393 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001394
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001395 asdl_seq_SET(values, i / 4, expression);
1396 }
1397 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 }
1399 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001400 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001401 if (Py_Py3kWarningFlag &&
1402 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001403 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001404 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001405 if (!expression)
1406 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001407
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001408 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 }
1410 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001411 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1412 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 }
1414}
1415
1416static slice_ty
1417ast_for_slice(struct compiling *c, const node *n)
1418{
1419 node *ch;
1420 expr_ty lower = NULL, upper = NULL, step = NULL;
1421
1422 REQ(n, subscript);
1423
1424 /*
1425 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1426 sliceop: ':' [test]
1427 */
1428 ch = CHILD(n, 0);
1429 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001430 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431
1432 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001433 /* 'step' variable hold no significance in terms of being used over
1434 other vars */
1435 step = ast_for_expr(c, ch);
1436 if (!step)
1437 return NULL;
1438
1439 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 }
1441
1442 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001443 lower = ast_for_expr(c, ch);
1444 if (!lower)
1445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 }
1447
1448 /* If there's an upper bound it's in the second or third position. */
1449 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001450 if (NCH(n) > 1) {
1451 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001453 if (TYPE(n2) == test) {
1454 upper = ast_for_expr(c, n2);
1455 if (!upper)
1456 return NULL;
1457 }
1458 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001460 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001462 if (TYPE(n2) == test) {
1463 upper = ast_for_expr(c, n2);
1464 if (!upper)
1465 return NULL;
1466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 }
1468
1469 ch = CHILD(n, NCH(n) - 1);
1470 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001471 if (NCH(ch) == 1) {
1472 /* No expression, so step is None */
1473 ch = CHILD(ch, 0);
1474 step = Name(new_identifier("None", c->c_arena), Load,
1475 LINENO(ch), ch->n_col_offset, c->c_arena);
1476 if (!step)
1477 return NULL;
1478 } else {
1479 ch = CHILD(ch, 1);
1480 if (TYPE(ch) == test) {
1481 step = ast_for_expr(c, ch);
1482 if (!step)
1483 return NULL;
1484 }
1485 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486 }
1487
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001488 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489}
1490
1491static expr_ty
1492ast_for_binop(struct compiling *c, const node *n)
1493{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001494 /* Must account for a sequence of expressions.
1495 How should A op B op C by represented?
1496 BinOp(BinOp(A, op, B), op, C).
1497 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001499 int i, nops;
1500 expr_ty expr1, expr2, result;
1501 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001503 expr1 = ast_for_expr(c, CHILD(n, 0));
1504 if (!expr1)
1505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001507 expr2 = ast_for_expr(c, CHILD(n, 2));
1508 if (!expr2)
1509 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001511 newoperator = get_operator(CHILD(n, 1));
1512 if (!newoperator)
1513 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001515 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1516 c->c_arena);
1517 if (!result)
1518 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001520 nops = (NCH(n) - 1) / 2;
1521 for (i = 1; i < nops; i++) {
1522 expr_ty tmp_result, tmp;
1523 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001525 newoperator = get_operator(next_oper);
1526 if (!newoperator)
1527 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001529 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1530 if (!tmp)
1531 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001533 tmp_result = BinOp(result, newoperator, tmp,
1534 LINENO(next_oper), next_oper->n_col_offset,
1535 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001536 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001537 return NULL;
1538 result = tmp_result;
1539 }
1540 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541}
1542
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001543static expr_ty
1544ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1545{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001546 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1547 subscriptlist: subscript (',' subscript)* [',']
1548 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1549 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001550 REQ(n, trailer);
1551 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001552 if (NCH(n) == 2)
1553 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1554 n->n_col_offset, c->c_arena);
1555 else
1556 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001557 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001558 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001559 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1560 if (!attr_id)
1561 return NULL;
1562 return Attribute(left_expr, attr_id, Load,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001563 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001564 }
1565 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001566 REQ(CHILD(n, 0), LSQB);
1567 REQ(CHILD(n, 2), RSQB);
1568 n = CHILD(n, 1);
1569 if (NCH(n) == 1) {
1570 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1571 if (!slc)
1572 return NULL;
1573 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1574 c->c_arena);
1575 }
1576 else {
1577 /* The grammar is ambiguous here. The ambiguity is resolved
1578 by treating the sequence as a tuple literal if there are
1579 no slice features.
1580 */
1581 int j;
1582 slice_ty slc;
1583 expr_ty e;
1584 bool simple = true;
1585 asdl_seq *slices, *elts;
1586 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1587 if (!slices)
1588 return NULL;
1589 for (j = 0; j < NCH(n); j += 2) {
1590 slc = ast_for_slice(c, CHILD(n, j));
1591 if (!slc)
1592 return NULL;
1593 if (slc->kind != Index_kind)
1594 simple = false;
1595 asdl_seq_SET(slices, j / 2, slc);
1596 }
1597 if (!simple) {
1598 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1599 Load, LINENO(n), n->n_col_offset, c->c_arena);
1600 }
1601 /* extract Index values and put them in a Tuple */
1602 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1603 if (!elts)
1604 return NULL;
1605 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1606 slc = (slice_ty)asdl_seq_GET(slices, j);
1607 assert(slc->kind == Index_kind && slc->v.Index.value);
1608 asdl_seq_SET(elts, j, slc->v.Index.value);
1609 }
1610 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1611 if (!e)
1612 return NULL;
1613 return Subscript(left_expr, Index(e, c->c_arena),
1614 Load, LINENO(n), n->n_col_offset, c->c_arena);
1615 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001616 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001617}
1618
1619static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001620ast_for_factor(struct compiling *c, const node *n)
1621{
1622 node *pfactor, *ppower, *patom, *pnum;
1623 expr_ty expression;
1624
1625 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001626 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001627 constant. The peephole optimizer already does something like
1628 this but it doesn't handle the case where the constant is
1629 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1630 PyLongObject.
1631 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001632 if (TYPE(CHILD(n, 0)) == MINUS &&
1633 NCH(n) == 2 &&
1634 TYPE((pfactor = CHILD(n, 1))) == factor &&
1635 NCH(pfactor) == 1 &&
1636 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1637 NCH(ppower) == 1 &&
1638 TYPE((patom = CHILD(ppower, 0))) == atom &&
1639 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1640 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1641 if (s == NULL)
1642 return NULL;
1643 s[0] = '-';
1644 strcpy(s + 1, STR(pnum));
1645 PyObject_FREE(STR(pnum));
1646 STR(pnum) = s;
1647 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001648 }
1649
1650 expression = ast_for_expr(c, CHILD(n, 1));
1651 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001652 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001653
1654 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001655 case PLUS:
1656 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1657 c->c_arena);
1658 case MINUS:
1659 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1660 c->c_arena);
1661 case TILDE:
1662 return UnaryOp(Invert, expression, LINENO(n),
1663 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001664 }
1665 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001666 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001667 return NULL;
1668}
1669
1670static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001671ast_for_power(struct compiling *c, const node *n)
1672{
1673 /* power: atom trailer* ('**' factor)*
1674 */
1675 int i;
1676 expr_ty e, tmp;
1677 REQ(n, power);
1678 e = ast_for_atom(c, CHILD(n, 0));
1679 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001680 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001681 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001682 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001683 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001684 node *ch = CHILD(n, i);
1685 if (TYPE(ch) != trailer)
1686 break;
1687 tmp = ast_for_trailer(c, ch, e);
1688 if (!tmp)
1689 return NULL;
1690 tmp->lineno = e->lineno;
1691 tmp->col_offset = e->col_offset;
1692 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001693 }
1694 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001695 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1696 if (!f)
1697 return NULL;
1698 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1699 if (!tmp)
1700 return NULL;
1701 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001702 }
1703 return e;
1704}
1705
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706/* Do not name a variable 'expr'! Will cause a compile error.
1707*/
1708
1709static expr_ty
1710ast_for_expr(struct compiling *c, const node *n)
1711{
1712 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001713 test: or_test ['if' or_test 'else' test] | lambdef
1714 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 and_test: not_test ('and' not_test)*
1716 not_test: 'not' not_test | comparison
1717 comparison: expr (comp_op expr)*
1718 expr: xor_expr ('|' xor_expr)*
1719 xor_expr: and_expr ('^' and_expr)*
1720 and_expr: shift_expr ('&' shift_expr)*
1721 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1722 arith_expr: term (('+'|'-') term)*
1723 term: factor (('*'|'/'|'%'|'//') factor)*
1724 factor: ('+'|'-'|'~') factor | power
1725 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001726
1727 As well as modified versions that exist for backward compatibility,
1728 to explicitly allow:
1729 [ x for x in lambda: 0, lambda: 1 ]
1730 (which would be ambiguous without these extra rules)
1731
1732 old_test: or_test | old_lambdef
1733 old_lambdef: 'lambda' [vararglist] ':' old_test
1734
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 */
1736
1737 asdl_seq *seq;
1738 int i;
1739
1740 loop:
1741 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001742 case test:
1743 case old_test:
1744 if (TYPE(CHILD(n, 0)) == lambdef ||
1745 TYPE(CHILD(n, 0)) == old_lambdef)
1746 return ast_for_lambdef(c, CHILD(n, 0));
1747 else if (NCH(n) > 1)
1748 return ast_for_ifexpr(c, n);
1749 /* Fallthrough */
1750 case or_test:
1751 case and_test:
1752 if (NCH(n) == 1) {
1753 n = CHILD(n, 0);
1754 goto loop;
1755 }
1756 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1757 if (!seq)
1758 return NULL;
1759 for (i = 0; i < NCH(n); i += 2) {
1760 expr_ty e = ast_for_expr(c, CHILD(n, i));
1761 if (!e)
1762 return NULL;
1763 asdl_seq_SET(seq, i / 2, e);
1764 }
1765 if (!strcmp(STR(CHILD(n, 1)), "and"))
1766 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1767 c->c_arena);
1768 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1769 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1770 case not_test:
1771 if (NCH(n) == 1) {
1772 n = CHILD(n, 0);
1773 goto loop;
1774 }
1775 else {
1776 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1777 if (!expression)
1778 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001780 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1781 c->c_arena);
1782 }
1783 case comparison:
1784 if (NCH(n) == 1) {
1785 n = CHILD(n, 0);
1786 goto loop;
1787 }
1788 else {
1789 expr_ty expression;
1790 asdl_int_seq *ops;
1791 asdl_seq *cmps;
1792 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1793 if (!ops)
1794 return NULL;
1795 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1796 if (!cmps) {
1797 return NULL;
1798 }
1799 for (i = 1; i < NCH(n); i += 2) {
1800 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001802 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001803 if (!newoperator) {
1804 return NULL;
1805 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001807 expression = ast_for_expr(c, CHILD(n, i + 1));
1808 if (!expression) {
1809 return NULL;
1810 }
1811
1812 asdl_seq_SET(ops, i / 2, newoperator);
1813 asdl_seq_SET(cmps, i / 2, expression);
1814 }
1815 expression = ast_for_expr(c, CHILD(n, 0));
1816 if (!expression) {
1817 return NULL;
1818 }
1819
1820 return Compare(expression, ops, cmps, LINENO(n),
1821 n->n_col_offset, c->c_arena);
1822 }
1823 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001825 /* The next five cases all handle BinOps. The main body of code
1826 is the same in each case, but the switch turned inside out to
1827 reuse the code for each type of operator.
1828 */
1829 case expr:
1830 case xor_expr:
1831 case and_expr:
1832 case shift_expr:
1833 case arith_expr:
1834 case term:
1835 if (NCH(n) == 1) {
1836 n = CHILD(n, 0);
1837 goto loop;
1838 }
1839 return ast_for_binop(c, n);
1840 case yield_expr: {
1841 expr_ty exp = NULL;
1842 if (NCH(n) == 2) {
1843 exp = ast_for_testlist(c, CHILD(n, 1));
1844 if (!exp)
1845 return NULL;
1846 }
1847 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1848 }
1849 case factor:
1850 if (NCH(n) == 1) {
1851 n = CHILD(n, 0);
1852 goto loop;
1853 }
1854 return ast_for_factor(c, n);
1855 case power:
1856 return ast_for_power(c, n);
1857 default:
1858 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001861 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 return NULL;
1863}
1864
1865static expr_ty
1866ast_for_call(struct compiling *c, const node *n, expr_ty func)
1867{
1868 /*
1869 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001870 | '**' test)
1871 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 */
1873
1874 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001875 asdl_seq *args;
1876 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 expr_ty vararg = NULL, kwarg = NULL;
1878
1879 REQ(n, arglist);
1880
1881 nargs = 0;
1882 nkeywords = 0;
1883 ngens = 0;
1884 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001885 node *ch = CHILD(n, i);
1886 if (TYPE(ch) == argument) {
1887 if (NCH(ch) == 1)
1888 nargs++;
1889 else if (TYPE(CHILD(ch, 1)) == gen_for)
1890 ngens++;
1891 else
1892 nkeywords++;
1893 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 }
1895 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001896 ast_error(n, "Generator expression must be parenthesized "
1897 "if not sole argument");
1898 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 }
1900
1901 if (nargs + nkeywords + ngens > 255) {
1902 ast_error(n, "more than 255 arguments");
1903 return NULL;
1904 }
1905
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001906 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001908 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001909 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912 nargs = 0;
1913 nkeywords = 0;
1914 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001915 node *ch = CHILD(n, i);
1916 if (TYPE(ch) == argument) {
1917 expr_ty e;
1918 if (NCH(ch) == 1) {
1919 if (nkeywords) {
1920 ast_error(CHILD(ch, 0),
1921 "non-keyword arg after keyword arg");
1922 return NULL;
1923 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001924 if (vararg) {
1925 ast_error(CHILD(ch, 0),
1926 "only named arguments may follow *expression");
1927 return NULL;
1928 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001929 e = ast_for_expr(c, CHILD(ch, 0));
1930 if (!e)
1931 return NULL;
1932 asdl_seq_SET(args, nargs++, e);
1933 }
1934 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1935 e = ast_for_genexp(c, ch);
1936 if (!e)
1937 return NULL;
1938 asdl_seq_SET(args, nargs++, e);
1939 }
1940 else {
1941 keyword_ty kw;
1942 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001943 int k;
1944 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001946 /* CHILD(ch, 0) is test, but must be an identifier? */
1947 e = ast_for_expr(c, CHILD(ch, 0));
1948 if (!e)
1949 return NULL;
1950 /* f(lambda x: x[0] = 3) ends up getting parsed with
1951 * LHS test = lambda x: x[0], and RHS test = 3.
1952 * SF bug 132313 points out that complaining about a keyword
1953 * then is very confusing.
1954 */
1955 if (e->kind == Lambda_kind) {
1956 ast_error(CHILD(ch, 0),
1957 "lambda cannot contain assignment");
1958 return NULL;
1959 } else if (e->kind != Name_kind) {
1960 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1961 return NULL;
1962 }
1963 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001964 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001965 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001966 for (k = 0; k < nkeywords; k++) {
1967 tmp = PyString_AS_STRING(
1968 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
1969 if (!strcmp(tmp, PyString_AS_STRING(key))) {
1970 ast_error(CHILD(ch, 0), "keyword argument repeated");
1971 return NULL;
1972 }
1973 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001974 e = ast_for_expr(c, CHILD(ch, 2));
1975 if (!e)
1976 return NULL;
1977 kw = keyword(key, e, c->c_arena);
1978 if (!kw)
1979 return NULL;
1980 asdl_seq_SET(keywords, nkeywords++, kw);
1981 }
1982 }
1983 else if (TYPE(ch) == STAR) {
1984 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001985 if (!vararg)
1986 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001987 i++;
1988 }
1989 else if (TYPE(ch) == DOUBLESTAR) {
1990 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001991 if (!kwarg)
1992 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001993 i++;
1994 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 }
1996
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001997 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1998 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999}
2000
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002002ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002004 /* testlist_gexp: test (',' test)* [','] */
2005 /* testlist: test (',' test)* [','] */
2006 /* testlist_safe: test (',' test)+ [','] */
2007 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002009 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002010 if (NCH(n) > 1)
2011 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002012 }
2013 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002014 assert(TYPE(n) == testlist ||
2015 TYPE(n) == testlist_safe ||
2016 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002017 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002019 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002021 asdl_seq *tmp = seq_for_testlist(c, n);
2022 if (!tmp)
2023 return NULL;
2024 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002026}
2027
2028static expr_ty
2029ast_for_testlist_gexp(struct compiling *c, const node* n)
2030{
2031 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2032 /* argument: test [ gen_for ] */
2033 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002034 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002035 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002036 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002037}
2038
2039/* like ast_for_testlist() but returns a sequence */
2040static asdl_seq*
2041ast_for_class_bases(struct compiling *c, const node* n)
2042{
2043 /* testlist: test (',' test)* [','] */
2044 assert(NCH(n) > 0);
2045 REQ(n, testlist);
2046 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002047 expr_ty base;
2048 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2049 if (!bases)
2050 return NULL;
2051 base = ast_for_expr(c, CHILD(n, 0));
2052 if (!base)
2053 return NULL;
2054 asdl_seq_SET(bases, 0, base);
2055 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002056 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002057
2058 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059}
2060
2061static stmt_ty
2062ast_for_expr_stmt(struct compiling *c, const node *n)
2063{
2064 REQ(n, expr_stmt);
2065 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002066 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 testlist: test (',' test)* [',']
2068 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002069 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 test: ... here starts the operator precendence dance
2071 */
2072
2073 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002074 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2075 if (!e)
2076 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002078 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 }
2080 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002081 expr_ty expr1, expr2;
2082 operator_ty newoperator;
2083 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002085 expr1 = ast_for_testlist(c, ch);
2086 if (!expr1)
2087 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002088 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002089 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002091 ch = CHILD(n, 2);
2092 if (TYPE(ch) == testlist)
2093 expr2 = ast_for_testlist(c, ch);
2094 else
2095 expr2 = ast_for_expr(c, ch);
2096 if (!expr2)
2097 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002099 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002100 if (!newoperator)
2101 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002103 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2104 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 }
2106 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002107 int i;
2108 asdl_seq *targets;
2109 node *value;
2110 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002112 /* a normal assignment */
2113 REQ(CHILD(n, 1), EQUAL);
2114 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2115 if (!targets)
2116 return NULL;
2117 for (i = 0; i < NCH(n) - 2; i += 2) {
2118 expr_ty e;
2119 node *ch = CHILD(n, i);
Benjamin Petersonb2664812009-06-11 17:49:38 +00002120 if (TYPE(ch) == yield_expr) {
2121 ast_error(ch, "assignment to yield expression not possible");
2122 return NULL;
2123 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002124 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002126 /* set context to assign */
2127 if (!e)
2128 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002130 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002131 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002133 asdl_seq_SET(targets, i / 2, e);
2134 }
2135 value = CHILD(n, NCH(n) - 1);
2136 if (TYPE(value) == testlist)
2137 expression = ast_for_testlist(c, value);
2138 else
2139 expression = ast_for_expr(c, value);
2140 if (!expression)
2141 return NULL;
2142 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2143 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145}
2146
2147static stmt_ty
2148ast_for_print_stmt(struct compiling *c, const node *n)
2149{
2150 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002151 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 */
2153 expr_ty dest = NULL, expression;
2154 asdl_seq *seq;
2155 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002156 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157
2158 REQ(n, print_stmt);
2159 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002160 dest = ast_for_expr(c, CHILD(n, 2));
2161 if (!dest)
2162 return NULL;
2163 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002165 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002167 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002168 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002169 expression = ast_for_expr(c, CHILD(n, i));
2170 if (!expression)
2171 return NULL;
2172 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 }
2174 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002175 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176}
2177
2178static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002179ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180{
2181 asdl_seq *seq;
2182 int i;
2183 expr_ty e;
2184
2185 REQ(n, exprlist);
2186
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002187 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002189 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002191 e = ast_for_expr(c, CHILD(n, i));
2192 if (!e)
2193 return NULL;
2194 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002195 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002196 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 }
2198 return seq;
2199}
2200
2201static stmt_ty
2202ast_for_del_stmt(struct compiling *c, const node *n)
2203{
2204 asdl_seq *expr_list;
2205
2206 /* del_stmt: 'del' exprlist */
2207 REQ(n, del_stmt);
2208
2209 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2210 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002211 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002212 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213}
2214
2215static stmt_ty
2216ast_for_flow_stmt(struct compiling *c, const node *n)
2217{
2218 /*
2219 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002220 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221 break_stmt: 'break'
2222 continue_stmt: 'continue'
2223 return_stmt: 'return' [testlist]
2224 yield_stmt: yield_expr
2225 yield_expr: 'yield' testlist
2226 raise_stmt: 'raise' [test [',' test [',' test]]]
2227 */
2228 node *ch;
2229
2230 REQ(n, flow_stmt);
2231 ch = CHILD(n, 0);
2232 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002233 case break_stmt:
2234 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2235 case continue_stmt:
2236 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2237 case yield_stmt: { /* will reduce to yield_expr */
2238 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2239 if (!exp)
2240 return NULL;
2241 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2242 }
2243 case return_stmt:
2244 if (NCH(ch) == 1)
2245 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2246 else {
2247 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2248 if (!expression)
2249 return NULL;
2250 return Return(expression, LINENO(n), n->n_col_offset,
2251 c->c_arena);
2252 }
2253 case raise_stmt:
2254 if (NCH(ch) == 1)
2255 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2256 c->c_arena);
2257 else if (NCH(ch) == 2) {
2258 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2259 if (!expression)
2260 return NULL;
2261 return Raise(expression, NULL, NULL, LINENO(n),
2262 n->n_col_offset, c->c_arena);
2263 }
2264 else if (NCH(ch) == 4) {
2265 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002267 expr1 = ast_for_expr(c, CHILD(ch, 1));
2268 if (!expr1)
2269 return NULL;
2270 expr2 = ast_for_expr(c, CHILD(ch, 3));
2271 if (!expr2)
2272 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002274 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2275 c->c_arena);
2276 }
2277 else if (NCH(ch) == 6) {
2278 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002280 expr1 = ast_for_expr(c, CHILD(ch, 1));
2281 if (!expr1)
2282 return NULL;
2283 expr2 = ast_for_expr(c, CHILD(ch, 3));
2284 if (!expr2)
2285 return NULL;
2286 expr3 = ast_for_expr(c, CHILD(ch, 5));
2287 if (!expr3)
2288 return NULL;
2289
2290 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2291 c->c_arena);
2292 }
2293 default:
2294 PyErr_Format(PyExc_SystemError,
2295 "unexpected flow_stmt: %d", TYPE(ch));
2296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002298
2299 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2300 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301}
2302
2303static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002304alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305{
2306 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002307 import_as_name: NAME ['as' NAME]
2308 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 dotted_name: NAME ('.' NAME)*
2310 */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002311 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002312
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 loop:
2314 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002315 case import_as_name:
2316 str = NULL;
2317 if (NCH(n) == 3) {
2318 str = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002319 if (!str)
2320 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002321 }
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002322 name = NEW_IDENTIFIER(CHILD(n, 0));
2323 if (!name)
2324 return NULL;
2325 return alias(name, str, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002326 case dotted_as_name:
2327 if (NCH(n) == 1) {
2328 n = CHILD(n, 0);
2329 goto loop;
2330 }
2331 else {
2332 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2333 if (!a)
2334 return NULL;
2335 assert(!a->asname);
2336 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002337 if (!a->asname)
2338 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002339 return a;
2340 }
2341 break;
2342 case dotted_name:
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002343 if (NCH(n) == 1) {
2344 name = NEW_IDENTIFIER(CHILD(n, 0));
2345 if (!name)
2346 return NULL;
2347 return alias(name, NULL, c->c_arena);
2348 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002349 else {
2350 /* Create a string of the form "a.b.c" */
2351 int i;
2352 size_t len;
2353 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002355 len = 0;
2356 for (i = 0; i < NCH(n); i += 2)
2357 /* length of string plus one for the dot */
2358 len += strlen(STR(CHILD(n, i))) + 1;
2359 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002360 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002361 if (!str)
2362 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002363 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002364 if (!s)
2365 return NULL;
2366 for (i = 0; i < NCH(n); i += 2) {
2367 char *sch = STR(CHILD(n, i));
2368 strcpy(s, STR(CHILD(n, i)));
2369 s += strlen(sch);
2370 *s++ = '.';
2371 }
2372 --s;
2373 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002374 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002375 PyArena_AddPyObject(c->c_arena, str);
2376 return alias(str, NULL, c->c_arena);
2377 }
2378 break;
2379 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002380 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002381 PyArena_AddPyObject(c->c_arena, str);
2382 return alias(str, NULL, c->c_arena);
2383 default:
2384 PyErr_Format(PyExc_SystemError,
2385 "unexpected import name: %d", TYPE(n));
2386 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002388
2389 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390 return NULL;
2391}
2392
2393static stmt_ty
2394ast_for_import_stmt(struct compiling *c, const node *n)
2395{
2396 /*
2397 import_stmt: import_name | import_from
2398 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002399 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002400 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002402 int lineno;
2403 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 int i;
2405 asdl_seq *aliases;
2406
2407 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002408 lineno = LINENO(n);
2409 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002411 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002412 n = CHILD(n, 1);
2413 REQ(n, dotted_as_names);
2414 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2415 if (!aliases)
2416 return NULL;
2417 for (i = 0; i < NCH(n); i += 2) {
2418 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2419 if (!import_alias)
2420 return NULL;
2421 asdl_seq_SET(aliases, i / 2, import_alias);
2422 }
2423 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002425 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002426 int n_children;
2427 int idx, ndots = 0;
2428 alias_ty mod = NULL;
2429 identifier modname;
2430
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002431 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002432 optional module name */
2433 for (idx = 1; idx < NCH(n); idx++) {
2434 if (TYPE(CHILD(n, idx)) == dotted_name) {
2435 mod = alias_for_import_name(c, CHILD(n, idx));
2436 idx++;
2437 break;
2438 } else if (TYPE(CHILD(n, idx)) != DOT) {
2439 break;
2440 }
2441 ndots++;
2442 }
2443 idx++; /* skip over the 'import' keyword */
2444 switch (TYPE(CHILD(n, idx))) {
2445 case STAR:
2446 /* from ... import * */
2447 n = CHILD(n, idx);
2448 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002449 break;
2450 case LPAR:
2451 /* from ... import (x, y, z) */
2452 n = CHILD(n, idx + 1);
2453 n_children = NCH(n);
2454 break;
2455 case import_as_names:
2456 /* from ... import x, y, z */
2457 n = CHILD(n, idx);
2458 n_children = NCH(n);
2459 if (n_children % 2 == 0) {
2460 ast_error(n, "trailing comma not allowed without"
2461 " surrounding parentheses");
2462 return NULL;
2463 }
2464 break;
2465 default:
2466 ast_error(n, "Unexpected node-type in from-import");
2467 return NULL;
2468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002470 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2471 if (!aliases)
2472 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002474 /* handle "from ... import *" special b/c there's no children */
2475 if (TYPE(n) == STAR) {
2476 alias_ty import_alias = alias_for_import_name(c, n);
2477 if (!import_alias)
2478 return NULL;
2479 asdl_seq_SET(aliases, 0, import_alias);
2480 }
2481 else {
2482 for (i = 0; i < NCH(n); i += 2) {
2483 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2484 if (!import_alias)
2485 return NULL;
2486 asdl_seq_SET(aliases, i / 2, import_alias);
2487 }
2488 }
2489 if (mod != NULL)
2490 modname = mod->name;
2491 else
2492 modname = new_identifier("", c->c_arena);
2493 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2494 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 }
Neal Norwitz79792652005-11-14 04:25:03 +00002496 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002497 "unknown import statement: starts with command '%s'",
2498 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 return NULL;
2500}
2501
2502static stmt_ty
2503ast_for_global_stmt(struct compiling *c, const node *n)
2504{
2505 /* global_stmt: 'global' NAME (',' NAME)* */
2506 identifier name;
2507 asdl_seq *s;
2508 int i;
2509
2510 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002511 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002513 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002515 name = NEW_IDENTIFIER(CHILD(n, i));
2516 if (!name)
2517 return NULL;
2518 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002520 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521}
2522
2523static stmt_ty
2524ast_for_exec_stmt(struct compiling *c, const node *n)
2525{
2526 expr_ty expr1, globals = NULL, locals = NULL;
2527 int n_children = NCH(n);
2528 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002529 PyErr_Format(PyExc_SystemError,
2530 "poorly formed 'exec' statement: %d parts to statement",
2531 n_children);
2532 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 }
2534
2535 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2536 REQ(n, exec_stmt);
2537 expr1 = ast_for_expr(c, CHILD(n, 1));
2538 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002539 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002541 globals = ast_for_expr(c, CHILD(n, 3));
2542 if (!globals)
2543 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 }
2545 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002546 locals = ast_for_expr(c, CHILD(n, 5));
2547 if (!locals)
2548 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 }
2550
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002551 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2552 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553}
2554
2555static stmt_ty
2556ast_for_assert_stmt(struct compiling *c, const node *n)
2557{
2558 /* assert_stmt: 'assert' test [',' test] */
2559 REQ(n, assert_stmt);
2560 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002561 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2562 if (!expression)
2563 return NULL;
2564 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2565 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 }
2567 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002568 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002570 expr1 = ast_for_expr(c, CHILD(n, 1));
2571 if (!expr1)
2572 return NULL;
2573 expr2 = ast_for_expr(c, CHILD(n, 3));
2574 if (!expr2)
2575 return NULL;
2576
2577 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 }
Neal Norwitz79792652005-11-14 04:25:03 +00002579 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002580 "improper number of parts to 'assert' statement: %d",
2581 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 return NULL;
2583}
2584
2585static asdl_seq *
2586ast_for_suite(struct compiling *c, const node *n)
2587{
2588 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002589 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 stmt_ty s;
2591 int i, total, num, end, pos = 0;
2592 node *ch;
2593
2594 REQ(n, suite);
2595
2596 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002597 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002599 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002601 n = CHILD(n, 0);
2602 /* simple_stmt always ends with a NEWLINE,
2603 and may have a trailing SEMI
2604 */
2605 end = NCH(n) - 1;
2606 if (TYPE(CHILD(n, end - 1)) == SEMI)
2607 end--;
2608 /* loop by 2 to skip semi-colons */
2609 for (i = 0; i < end; i += 2) {
2610 ch = CHILD(n, i);
2611 s = ast_for_stmt(c, ch);
2612 if (!s)
2613 return NULL;
2614 asdl_seq_SET(seq, pos++, s);
2615 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 }
2617 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002618 for (i = 2; i < (NCH(n) - 1); i++) {
2619 ch = CHILD(n, i);
2620 REQ(ch, stmt);
2621 num = num_stmts(ch);
2622 if (num == 1) {
2623 /* small_stmt or compound_stmt with only one child */
2624 s = ast_for_stmt(c, ch);
2625 if (!s)
2626 return NULL;
2627 asdl_seq_SET(seq, pos++, s);
2628 }
2629 else {
2630 int j;
2631 ch = CHILD(ch, 0);
2632 REQ(ch, simple_stmt);
2633 for (j = 0; j < NCH(ch); j += 2) {
2634 /* statement terminates with a semi-colon ';' */
2635 if (NCH(CHILD(ch, j)) == 0) {
2636 assert((j + 1) == NCH(ch));
2637 break;
2638 }
2639 s = ast_for_stmt(c, CHILD(ch, j));
2640 if (!s)
2641 return NULL;
2642 asdl_seq_SET(seq, pos++, s);
2643 }
2644 }
2645 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 }
2647 assert(pos == seq->size);
2648 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649}
2650
2651static stmt_ty
2652ast_for_if_stmt(struct compiling *c, const node *n)
2653{
2654 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2655 ['else' ':' suite]
2656 */
2657 char *s;
2658
2659 REQ(n, if_stmt);
2660
2661 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002662 expr_ty expression;
2663 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002665 expression = ast_for_expr(c, CHILD(n, 1));
2666 if (!expression)
2667 return NULL;
2668 suite_seq = ast_for_suite(c, CHILD(n, 3));
2669 if (!suite_seq)
2670 return NULL;
2671
2672 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2673 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002675
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 s = STR(CHILD(n, 4));
2677 /* s[2], the third character in the string, will be
2678 's' for el_s_e, or
2679 'i' for el_i_f
2680 */
2681 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002682 expr_ty expression;
2683 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002685 expression = ast_for_expr(c, CHILD(n, 1));
2686 if (!expression)
2687 return NULL;
2688 seq1 = ast_for_suite(c, CHILD(n, 3));
2689 if (!seq1)
2690 return NULL;
2691 seq2 = ast_for_suite(c, CHILD(n, 6));
2692 if (!seq2)
2693 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002695 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2696 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 }
2698 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002699 int i, n_elif, has_else = 0;
2700 expr_ty expression;
2701 asdl_seq *suite_seq;
2702 asdl_seq *orelse = NULL;
2703 n_elif = NCH(n) - 4;
2704 /* must reference the child n_elif+1 since 'else' token is third,
2705 not fourth, child from the end. */
2706 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2707 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2708 has_else = 1;
2709 n_elif -= 3;
2710 }
2711 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002713 if (has_else) {
2714 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002716 orelse = asdl_seq_new(1, c->c_arena);
2717 if (!orelse)
2718 return NULL;
2719 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2720 if (!expression)
2721 return NULL;
2722 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2723 if (!suite_seq)
2724 return NULL;
2725 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2726 if (!suite_seq2)
2727 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002729 asdl_seq_SET(orelse, 0,
2730 If(expression, suite_seq, suite_seq2,
2731 LINENO(CHILD(n, NCH(n) - 6)),
2732 CHILD(n, NCH(n) - 6)->n_col_offset,
2733 c->c_arena));
2734 /* the just-created orelse handled the last elif */
2735 n_elif--;
2736 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002738 for (i = 0; i < n_elif; i++) {
2739 int off = 5 + (n_elif - i - 1) * 4;
2740 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2741 if (!newobj)
2742 return NULL;
2743 expression = ast_for_expr(c, CHILD(n, off));
2744 if (!expression)
2745 return NULL;
2746 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2747 if (!suite_seq)
2748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002750 asdl_seq_SET(newobj, 0,
2751 If(expression, suite_seq, orelse,
2752 LINENO(CHILD(n, off)),
2753 CHILD(n, off)->n_col_offset, c->c_arena));
2754 orelse = newobj;
2755 }
2756 expression = ast_for_expr(c, CHILD(n, 1));
2757 if (!expression)
2758 return NULL;
2759 suite_seq = ast_for_suite(c, CHILD(n, 3));
2760 if (!suite_seq)
2761 return NULL;
2762 return If(expression, suite_seq, orelse,
2763 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002765
2766 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002767 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769}
2770
2771static stmt_ty
2772ast_for_while_stmt(struct compiling *c, const node *n)
2773{
2774 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2775 REQ(n, while_stmt);
2776
2777 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002778 expr_ty expression;
2779 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002781 expression = ast_for_expr(c, CHILD(n, 1));
2782 if (!expression)
2783 return NULL;
2784 suite_seq = ast_for_suite(c, CHILD(n, 3));
2785 if (!suite_seq)
2786 return NULL;
2787 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2788 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 }
2790 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002791 expr_ty expression;
2792 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002794 expression = ast_for_expr(c, CHILD(n, 1));
2795 if (!expression)
2796 return NULL;
2797 seq1 = ast_for_suite(c, CHILD(n, 3));
2798 if (!seq1)
2799 return NULL;
2800 seq2 = ast_for_suite(c, CHILD(n, 6));
2801 if (!seq2)
2802 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002804 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2805 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002807
2808 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002809 "wrong number of tokens for 'while' statement: %d",
2810 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002811 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812}
2813
2814static stmt_ty
2815ast_for_for_stmt(struct compiling *c, const node *n)
2816{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002817 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 expr_ty expression;
2819 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002820 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2822 REQ(n, for_stmt);
2823
2824 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002825 seq = ast_for_suite(c, CHILD(n, 8));
2826 if (!seq)
2827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 }
2829
Neal Norwitzedef2be2006-07-12 05:26:17 +00002830 node_target = CHILD(n, 1);
2831 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002832 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002833 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002834 /* Check the # of children rather than the length of _target, since
2835 for x, in ... has 1 element in _target, but still requires a Tuple. */
2836 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002837 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002839 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002841 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002842 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002845 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002846 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002848 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002849 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850}
2851
2852static excepthandler_ty
2853ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2854{
Collin Winter62903052007-05-18 23:11:24 +00002855 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 REQ(exc, except_clause);
2857 REQ(body, suite);
2858
2859 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002860 asdl_seq *suite_seq = ast_for_suite(c, body);
2861 if (!suite_seq)
2862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863
Georg Brandla48f3ab2008-03-30 06:40:17 +00002864 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002865 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 }
2867 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002868 expr_ty expression;
2869 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002871 expression = ast_for_expr(c, CHILD(exc, 1));
2872 if (!expression)
2873 return NULL;
2874 suite_seq = ast_for_suite(c, body);
2875 if (!suite_seq)
2876 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877
Georg Brandla48f3ab2008-03-30 06:40:17 +00002878 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002879 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 }
2881 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002882 asdl_seq *suite_seq;
2883 expr_ty expression;
2884 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2885 if (!e)
2886 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002887 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002888 return NULL;
2889 expression = ast_for_expr(c, CHILD(exc, 1));
2890 if (!expression)
2891 return NULL;
2892 suite_seq = ast_for_suite(c, body);
2893 if (!suite_seq)
2894 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895
Georg Brandla48f3ab2008-03-30 06:40:17 +00002896 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002897 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002899
2900 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002901 "wrong number of children for 'except' clause: %d",
2902 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002903 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904}
2905
2906static stmt_ty
2907ast_for_try_stmt(struct compiling *c, const node *n)
2908{
Neal Norwitzf599f422005-12-17 21:33:47 +00002909 const int nch = NCH(n);
2910 int n_except = (nch - 3)/3;
2911 asdl_seq *body, *orelse = NULL, *finally = NULL;
2912
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 REQ(n, try_stmt);
2914
Neal Norwitzf599f422005-12-17 21:33:47 +00002915 body = ast_for_suite(c, CHILD(n, 2));
2916 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002917 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918
Neal Norwitzf599f422005-12-17 21:33:47 +00002919 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002920 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2921 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2922 /* we can assume it's an "else",
2923 because nch >= 9 for try-else-finally and
2924 it would otherwise have a type of except_clause */
2925 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2926 if (orelse == NULL)
2927 return NULL;
2928 n_except--;
2929 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002931 finally = ast_for_suite(c, CHILD(n, nch - 1));
2932 if (finally == NULL)
2933 return NULL;
2934 n_except--;
2935 }
2936 else {
2937 /* we can assume it's an "else",
2938 otherwise it would have a type of except_clause */
2939 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2940 if (orelse == NULL)
2941 return NULL;
2942 n_except--;
2943 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002945 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002946 ast_error(n, "malformed 'try' statement");
2947 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002949
2950 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002951 int i;
2952 stmt_ty except_st;
2953 /* process except statements to create a try ... except */
2954 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2955 if (handlers == NULL)
2956 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002957
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002958 for (i = 0; i < n_except; i++) {
2959 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2960 CHILD(n, 5 + i * 3));
2961 if (!e)
2962 return NULL;
2963 asdl_seq_SET(handlers, i, e);
2964 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002965
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002966 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2967 n->n_col_offset, c->c_arena);
2968 if (!finally)
2969 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002970
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002971 /* if a 'finally' is present too, we nest the TryExcept within a
2972 TryFinally to emulate try ... except ... finally */
2973 body = asdl_seq_new(1, c->c_arena);
2974 if (body == NULL)
2975 return NULL;
2976 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002977 }
2978
2979 /* must be a try ... finally (except clauses are in body, if any exist) */
2980 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002981 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982}
2983
Georg Brandl944f6842009-05-25 21:02:56 +00002984/* with_item: test ['as' expr] */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002985static stmt_ty
Georg Brandl944f6842009-05-25 21:02:56 +00002986ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002987{
2988 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002989
Georg Brandl944f6842009-05-25 21:02:56 +00002990 REQ(n, with_item);
2991 context_expr = ast_for_expr(c, CHILD(n, 0));
Collin Winter77c67bd2007-03-16 04:11:30 +00002992 if (!context_expr)
2993 return NULL;
Georg Brandl944f6842009-05-25 21:02:56 +00002994 if (NCH(n) == 3) {
2995 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002996
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002997 if (!optional_vars) {
2998 return NULL;
2999 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003000 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003001 return NULL;
3002 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003003 }
3004
Georg Brandl944f6842009-05-25 21:02:56 +00003005 return With(context_expr, optional_vars, content, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003006 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003007}
3008
Georg Brandl944f6842009-05-25 21:02:56 +00003009/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3010static stmt_ty
3011ast_for_with_stmt(struct compiling *c, const node *n)
3012{
3013 int i;
3014 stmt_ty ret;
3015 asdl_seq *inner;
3016
3017 REQ(n, with_stmt);
3018
3019 /* process the with items inside-out */
3020 i = NCH(n) - 1;
3021 /* the suite of the innermost with item is the suite of the with stmt */
3022 inner = ast_for_suite(c, CHILD(n, i));
3023 if (!inner)
3024 return NULL;
3025
3026 for (;;) {
3027 i -= 2;
3028 ret = ast_for_with_item(c, CHILD(n, i), inner);
3029 if (!ret)
3030 return NULL;
3031 /* was this the last item? */
3032 if (i == 1)
3033 break;
3034 /* if not, wrap the result so far in a new sequence */
3035 inner = asdl_seq_new(1, c->c_arena);
3036 if (!inner)
3037 return NULL;
3038 asdl_seq_SET(inner, 0, ret);
3039 }
3040
3041 return ret;
3042}
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 Peterson9e6310d2008-11-25 03:43:14 +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 Peterson9e6310d2008-11-25 03:43:14 +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 Peterson9e6310d2008-11-25 03:43:14 +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 Peterson9e6310d2008-11-25 03:43:14 +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 n = CHILD(n, 0);
3106 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3107 | flow_stmt | import_stmt | global_stmt | exec_stmt
3108 | assert_stmt
3109 */
3110 switch (TYPE(n)) {
3111 case expr_stmt:
3112 return ast_for_expr_stmt(c, n);
3113 case print_stmt:
3114 return ast_for_print_stmt(c, n);
3115 case del_stmt:
3116 return ast_for_del_stmt(c, n);
3117 case pass_stmt:
3118 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3119 case flow_stmt:
3120 return ast_for_flow_stmt(c, n);
3121 case import_stmt:
3122 return ast_for_import_stmt(c, n);
3123 case global_stmt:
3124 return ast_for_global_stmt(c, n);
3125 case exec_stmt:
3126 return ast_for_exec_stmt(c, n);
3127 case assert_stmt:
3128 return ast_for_assert_stmt(c, n);
3129 default:
3130 PyErr_Format(PyExc_SystemError,
3131 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3132 TYPE(n), NCH(n));
3133 return NULL;
3134 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 }
3136 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003137 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003138 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003139 */
3140 node *ch = CHILD(n, 0);
3141 REQ(n, compound_stmt);
3142 switch (TYPE(ch)) {
3143 case if_stmt:
3144 return ast_for_if_stmt(c, ch);
3145 case while_stmt:
3146 return ast_for_while_stmt(c, ch);
3147 case for_stmt:
3148 return ast_for_for_stmt(c, ch);
3149 case try_stmt:
3150 return ast_for_try_stmt(c, ch);
3151 case with_stmt:
3152 return ast_for_with_stmt(c, ch);
3153 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003154 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003155 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003156 return ast_for_classdef(c, ch, NULL);
3157 case decorated:
3158 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003159 default:
3160 PyErr_Format(PyExc_SystemError,
3161 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3162 TYPE(n), NCH(n));
3163 return NULL;
3164 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 }
3166}
3167
3168static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003169parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003171 const char *end;
3172 long x;
3173 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003175 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003176 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177#endif
3178
Mark Dickinson422ce062008-12-05 17:59:46 +00003179 assert(s != NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003180 errno = 0;
3181 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003183 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003185 if (*end == 'l' || *end == 'L')
3186 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003187 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003188 if (*end == '\0') {
3189 if (errno != 0)
3190 return PyLong_FromString((char *)s, (char **)0, 0);
3191 return PyInt_FromLong(x);
3192 }
3193 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003195 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003196 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003197 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003198 complex.imag = PyOS_ascii_atof(s);
3199 PyFPE_END_PROTECT(complex)
3200 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003201 }
3202 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003204 {
3205 PyFPE_START_PROTECT("atof", return 0)
3206 dx = PyOS_ascii_atof(s);
3207 PyFPE_END_PROTECT(dx)
3208 return PyFloat_FromDouble(dx);
3209 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210}
3211
3212static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003213decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214{
3215#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003216 Py_FatalError("decode_utf8 should not be called in this build.");
3217 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003219 PyObject *u, *v;
3220 char *s, *t;
3221 t = s = (char *)*sPtr;
3222 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3223 while (s < end && (*s & 0x80)) s++;
3224 *sPtr = s;
3225 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3226 if (u == NULL)
3227 return NULL;
3228 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3229 Py_DECREF(u);
3230 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231#endif
3232}
3233
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003234#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003236decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003238 PyObject *v, *u;
3239 char *buf;
3240 char *p;
3241 const char *end;
3242 if (encoding == NULL) {
3243 buf = (char *)s;
3244 u = NULL;
3245 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3246 buf = (char *)s;
3247 u = NULL;
3248 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003249 /* check for integer overflow */
3250 if (len > PY_SIZE_MAX / 4)
3251 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003252 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003253 u = PyString_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003254 if (u == NULL)
3255 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003256 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003257 end = s + len;
3258 while (s < end) {
3259 if (*s == '\\') {
3260 *p++ = *s++;
3261 if (*s & 0x80) {
3262 strcpy(p, "u005c");
3263 p += 5;
3264 }
3265 }
3266 if (*s & 0x80) { /* XXX inefficient */
3267 PyObject *w;
3268 char *r;
3269 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003270 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003271 if (w == NULL) {
3272 Py_DECREF(u);
3273 return NULL;
3274 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003275 r = PyString_AsString(w);
3276 rn = PyString_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003277 assert(rn % 2 == 0);
3278 for (i = 0; i < rn; i += 2) {
3279 sprintf(p, "\\u%02x%02x",
3280 r[i + 0] & 0xFF,
3281 r[i + 1] & 0xFF);
3282 p += 6;
3283 }
3284 Py_DECREF(w);
3285 } else {
3286 *p++ = *s++;
3287 }
3288 }
3289 len = p - buf;
3290 s = buf;
3291 }
3292 if (rawmode)
3293 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3294 else
3295 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3296 Py_XDECREF(u);
3297 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003299#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300
3301/* s is a Python string literal, including the bracketing quote characters,
3302 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3303 * parsestr parses it, and returns the decoded Python string object.
3304 */
3305static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003306parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003308 size_t len;
3309 int quote = Py_CHARMASK(*s);
3310 int rawmode = 0;
3311 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003312 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003314 if (isalpha(quote) || quote == '_') {
3315 if (quote == 'u' || quote == 'U') {
3316 quote = *++s;
3317 unicode = 1;
3318 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003319 if (quote == 'b' || quote == 'B') {
3320 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003321 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003322 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003323 if (quote == 'r' || quote == 'R') {
3324 quote = *++s;
3325 rawmode = 1;
3326 }
3327 }
3328 if (quote != '\'' && quote != '\"') {
3329 PyErr_BadInternalCall();
3330 return NULL;
3331 }
3332 s++;
3333 len = strlen(s);
3334 if (len > INT_MAX) {
3335 PyErr_SetString(PyExc_OverflowError,
3336 "string to parse is too long");
3337 return NULL;
3338 }
3339 if (s[--len] != quote) {
3340 PyErr_BadInternalCall();
3341 return NULL;
3342 }
3343 if (len >= 4 && s[0] == quote && s[1] == quote) {
3344 s += 2;
3345 len -= 2;
3346 if (s[--len] != quote || s[--len] != quote) {
3347 PyErr_BadInternalCall();
3348 return NULL;
3349 }
3350 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003352 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003353 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003354 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003356 need_encoding = (c->c_encoding != NULL &&
3357 strcmp(c->c_encoding, "utf-8") != 0 &&
3358 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003359 if (rawmode || strchr(s, '\\') == NULL) {
3360 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003362 /* This should not happen - we never see any other
3363 encoding. */
3364 Py_FatalError(
3365 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003367 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3368 if (u == NULL)
3369 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003370 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003371 Py_DECREF(u);
3372 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003374 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003375 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003376 }
3377 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003379 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003380 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381}
3382
3383/* Build a Python string object out of a STRING atom. This takes care of
3384 * compile-time literal catenation, calling parsestr() on each piece, and
3385 * pasting the intermediate results together.
3386 */
3387static PyObject *
3388parsestrplus(struct compiling *c, const node *n)
3389{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003390 PyObject *v;
3391 int i;
3392 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003393 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003394 /* String literal concatenation */
3395 for (i = 1; i < NCH(n); i++) {
3396 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003397 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003398 if (s == NULL)
3399 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003400 if (PyString_Check(v) && PyString_Check(s)) {
3401 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003402 if (v == NULL)
3403 goto onError;
3404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003406 else {
3407 PyObject *temp = PyUnicode_Concat(v, s);
3408 Py_DECREF(s);
3409 Py_DECREF(v);
3410 v = temp;
3411 if (v == NULL)
3412 goto onError;
3413 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003415 }
3416 }
3417 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003418
3419 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003420 Py_XDECREF(v);
3421 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422}