blob: 3422c2e1b0dd51d714898b5ffb8ada59829139b3 [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:
Benjamin Peterson52c4bec2009-06-13 17:08:53 +0000405 if (asdl_seq_LEN(e->v.Tuple.elts)) {
406 e->v.Tuple.ctx = ctx;
407 s = e->v.Tuple.elts;
408 }
409 else {
410 expr_name = "()";
411 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000412 break;
413 case Lambda_kind:
414 expr_name = "lambda";
415 break;
416 case Call_kind:
417 expr_name = "function call";
418 break;
419 case BoolOp_kind:
420 case BinOp_kind:
421 case UnaryOp_kind:
422 expr_name = "operator";
423 break;
424 case GeneratorExp_kind:
425 expr_name = "generator expression";
426 break;
427 case Yield_kind:
428 expr_name = "yield expression";
429 break;
430 case ListComp_kind:
431 expr_name = "list comprehension";
432 break;
433 case Dict_kind:
434 case Num_kind:
435 case Str_kind:
436 expr_name = "literal";
437 break;
438 case Compare_kind:
439 expr_name = "comparison";
440 break;
441 case Repr_kind:
442 expr_name = "repr";
443 break;
444 case IfExp_kind:
445 expr_name = "conditional expression";
446 break;
447 default:
448 PyErr_Format(PyExc_SystemError,
449 "unexpected expression in assignment %d (line %d)",
450 e->kind, e->lineno);
451 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000453 /* Check for error string set by switch */
454 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000455 char buf[300];
456 PyOS_snprintf(buf, sizeof(buf),
457 "can't %s %s",
458 ctx == Store ? "assign to" : "delete",
459 expr_name);
460 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000461 }
462
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000464 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465 */
466 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000467 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000469 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000470 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000471 return 0;
472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473 }
474 return 1;
475}
476
477static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000478ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479{
480 REQ(n, augassign);
481 n = CHILD(n, 0);
482 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000483 case '+':
484 return Add;
485 case '-':
486 return Sub;
487 case '/':
488 if (STR(n)[1] == '/')
489 return FloorDiv;
490 else
491 return Div;
492 case '%':
493 return Mod;
494 case '<':
495 return LShift;
496 case '>':
497 return RShift;
498 case '&':
499 return BitAnd;
500 case '^':
501 return BitXor;
502 case '|':
503 return BitOr;
504 case '*':
505 if (STR(n)[1] == '*')
506 return Pow;
507 else
508 return Mult;
509 default:
510 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
511 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 }
513}
514
515static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000516ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517{
518 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000519 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 */
521 REQ(n, comp_op);
522 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000523 n = CHILD(n, 0);
524 switch (TYPE(n)) {
525 case LESS:
526 return Lt;
527 case GREATER:
528 return Gt;
529 case EQEQUAL: /* == */
530 return Eq;
531 case LESSEQUAL:
532 return LtE;
533 case GREATEREQUAL:
534 return GtE;
535 case NOTEQUAL:
536 return NotEq;
537 case NAME:
538 if (strcmp(STR(n), "in") == 0)
539 return In;
540 if (strcmp(STR(n), "is") == 0)
541 return Is;
542 default:
543 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
544 STR(n));
545 return (cmpop_ty)0;
546 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 }
548 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000549 /* handle "not in" and "is not" */
550 switch (TYPE(CHILD(n, 0))) {
551 case NAME:
552 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
553 return NotIn;
554 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
555 return IsNot;
556 default:
557 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
558 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
559 return (cmpop_ty)0;
560 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 }
Neal Norwitz79792652005-11-14 04:25:03 +0000562 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000563 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000564 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565}
566
567static asdl_seq *
568seq_for_testlist(struct compiling *c, const node *n)
569{
570 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000571 asdl_seq *seq;
572 expr_ty expression;
573 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000574 assert(TYPE(n) == testlist ||
575 TYPE(n) == listmaker ||
576 TYPE(n) == testlist_gexp ||
577 TYPE(n) == testlist_safe ||
578 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000580 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583
584 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000585 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000587 expression = ast_for_expr(c, CHILD(n, i));
588 if (!expression)
589 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000591 assert(i / 2 < seq->size);
592 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 }
594 return seq;
595}
596
597static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000598compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599{
600 int i, len = (NCH(n) + 1) / 2;
601 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000602 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000604 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
Neal Norwitz3a230172006-09-22 08:18:10 +0000606 /* fpdef: NAME | '(' fplist ')'
607 fplist: fpdef (',' fpdef)* [',']
608 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610 for (i = 0; i < len; i++) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000611 PyObject *arg_id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000612 const node *fpdef_node = CHILD(n, 2*i);
613 const node *child;
614 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000615set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000616 /* fpdef_node is either a NAME or an fplist */
617 child = CHILD(fpdef_node, 0);
618 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000619 if (!forbidden_check(c, n, STR(child)))
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000620 return NULL;
621 arg_id = NEW_IDENTIFIER(child);
622 if (!arg_id)
623 return NULL;
624 arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
625 c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000626 }
627 else {
628 assert(TYPE(fpdef_node) == fpdef);
629 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
630 child = CHILD(fpdef_node, 1);
631 assert(TYPE(child) == fplist);
632 /* NCH == 1 means we have (x), we need to elide the extra parens */
633 if (NCH(child) == 1) {
634 fpdef_node = CHILD(child, 0);
635 assert(TYPE(fpdef_node) == fpdef);
636 goto set_name;
637 }
638 arg = compiler_complex_args(c, child);
639 }
640 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641 }
642
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000643 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000644 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 return result;
647}
648
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649
Jeremy Hyltona8293132006-02-28 17:58:27 +0000650/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651
652static arguments_ty
653ast_for_arguments(struct compiling *c, const node *n)
654{
655 /* parameters: '(' [varargslist] ')'
656 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000657 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000659 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660 asdl_seq *args, *defaults;
661 identifier vararg = NULL, kwarg = NULL;
662 node *ch;
663
664 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000665 if (NCH(n) == 2) /* () as argument list */
666 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
667 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 }
669 REQ(n, varargslist);
670
671 /* first count the number of normal args & defaults */
672 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000673 ch = CHILD(n, i);
674 if (TYPE(ch) == fpdef)
675 n_args++;
676 if (TYPE(ch) == EQUAL)
677 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000679 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000681 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000682 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000684 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685
686 /* fpdef: NAME | '(' fplist ')'
687 fplist: fpdef (',' fpdef)* [',']
688 */
689 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000690 j = 0; /* index for defaults */
691 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000693 ch = CHILD(n, i);
694 switch (TYPE(ch)) {
695 case fpdef:
696 handle_fpdef:
697 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
698 anything other than EQUAL or a comma? */
699 /* XXX Should NCH(n) check be made a separate check? */
700 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
701 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
702 if (!expression)
703 goto error;
704 assert(defaults != NULL);
705 asdl_seq_SET(defaults, j++, expression);
706 i += 2;
707 found_default = 1;
708 }
709 else if (found_default) {
710 ast_error(n,
711 "non-default argument follows default argument");
712 goto error;
713 }
714 if (NCH(ch) == 3) {
715 ch = CHILD(ch, 1);
716 /* def foo((x)): is not complex, special case. */
717 if (NCH(ch) != 1) {
718 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000719 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
720 "tuple parameter unpacking has been removed in 3.x"))
721 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000722 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000723 if (!asdl_seq_GET(args, k-1))
724 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000725 } else {
726 /* def foo((x)): setup for checking NAME below. */
727 /* Loop because there can be many parens and tuple
728 unpacking mixed in. */
729 ch = CHILD(ch, 0);
730 assert(TYPE(ch) == fpdef);
731 goto handle_fpdef;
732 }
733 }
734 if (TYPE(CHILD(ch, 0)) == NAME) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000735 PyObject *id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000736 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000737 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000738 goto error;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000739 id = NEW_IDENTIFIER(CHILD(ch, 0));
740 if (!id)
741 goto error;
742 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000743 c->c_arena);
744 if (!name)
745 goto error;
746 asdl_seq_SET(args, k++, name);
747
748 }
749 i += 2; /* the name and the comma */
750 break;
751 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000752 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000753 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000754 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000755 if (!vararg)
756 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000757 i += 3;
758 break;
759 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000760 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000761 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000762 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000763 if (!kwarg)
764 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000765 i += 3;
766 break;
767 default:
768 PyErr_Format(PyExc_SystemError,
769 "unexpected node in varargslist: %d @ %d",
770 TYPE(ch), i);
771 goto error;
772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 }
774
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000775 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776
777 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000778 Py_XDECREF(vararg);
779 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 return NULL;
781}
782
783static expr_ty
784ast_for_dotted_name(struct compiling *c, const node *n)
785{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000786 expr_ty e;
787 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000788 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 int i;
790
791 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000792
793 lineno = LINENO(n);
794 col_offset = n->n_col_offset;
795
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 id = NEW_IDENTIFIER(CHILD(n, 0));
797 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000798 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000799 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000801 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802
803 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000804 id = NEW_IDENTIFIER(CHILD(n, i));
805 if (!id)
806 return NULL;
807 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
808 if (!e)
809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811
812 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813}
814
815static expr_ty
816ast_for_decorator(struct compiling *c, const node *n)
817{
818 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
819 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000820 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821
822 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000823 REQ(CHILD(n, 0), AT);
824 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825
826 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
827 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000828 return NULL;
829
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000831 d = name_expr;
832 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 }
834 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000835 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
836 n->n_col_offset, c->c_arena);
837 if (!d)
838 return NULL;
839 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 }
841 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000842 d = ast_for_call(c, CHILD(n, 3), name_expr);
843 if (!d)
844 return NULL;
845 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 }
847
848 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849}
850
851static asdl_seq*
852ast_for_decorators(struct compiling *c, const node *n)
853{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000854 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000855 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 int i;
857
858 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000859 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000861 return NULL;
862
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000864 d = ast_for_decorator(c, CHILD(n, i));
865 if (!d)
866 return NULL;
867 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 }
869 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870}
871
872static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000873ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874{
Christian Heimes5224d282008-02-23 15:01:05 +0000875 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000876 identifier name;
877 arguments_ty args;
878 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000879 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880
881 REQ(n, funcdef);
882
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883 name = NEW_IDENTIFIER(CHILD(n, name_i));
884 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000885 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000886 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000887 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 args = ast_for_arguments(c, CHILD(n, name_i + 1));
889 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 body = ast_for_suite(c, CHILD(n, name_i + 3));
892 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000893 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000895 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000896 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897}
898
Christian Heimes5224d282008-02-23 15:01:05 +0000899static stmt_ty
900ast_for_decorated(struct compiling *c, const node *n)
901{
902 /* decorated: decorators (classdef | funcdef) */
903 stmt_ty thing = NULL;
904 asdl_seq *decorator_seq = NULL;
905
906 REQ(n, decorated);
907
908 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
909 if (!decorator_seq)
910 return NULL;
911
912 assert(TYPE(CHILD(n, 1)) == funcdef ||
913 TYPE(CHILD(n, 1)) == classdef);
914
915 if (TYPE(CHILD(n, 1)) == funcdef) {
916 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
917 } else if (TYPE(CHILD(n, 1)) == classdef) {
918 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
919 }
920 /* we count the decorators in when talking about the class' or
921 function's line number */
922 if (thing) {
923 thing->lineno = LINENO(n);
924 thing->col_offset = n->n_col_offset;
925 }
926 return thing;
927}
928
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929static expr_ty
930ast_for_lambdef(struct compiling *c, const node *n)
931{
932 /* lambdef: 'lambda' [varargslist] ':' test */
933 arguments_ty args;
934 expr_ty expression;
935
936 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000937 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
938 if (!args)
939 return NULL;
940 expression = ast_for_expr(c, CHILD(n, 2));
941 if (!expression)
942 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 }
944 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000945 args = ast_for_arguments(c, CHILD(n, 1));
946 if (!args)
947 return NULL;
948 expression = ast_for_expr(c, CHILD(n, 3));
949 if (!expression)
950 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951 }
952
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000953 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954}
955
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000956static expr_ty
957ast_for_ifexpr(struct compiling *c, const node *n)
958{
959 /* test: or_test 'if' or_test 'else' test */
960 expr_ty expression, body, orelse;
961
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000962 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000963 body = ast_for_expr(c, CHILD(n, 0));
964 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000965 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000966 expression = ast_for_expr(c, CHILD(n, 2));
967 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000968 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000969 orelse = ast_for_expr(c, CHILD(n, 4));
970 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000971 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000972 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000973 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000974}
975
Neal Norwitze4d4f002006-09-05 03:58:26 +0000976/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
977 so there is only a single version. Possibly for loops can also re-use
978 the code.
979*/
980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981/* Count the number of 'for' loop in a list comprehension.
982
983 Helper for ast_for_listcomp().
984*/
985
986static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000987count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988{
989 int n_fors = 0;
990 node *ch = CHILD(n, 1);
991
992 count_list_for:
993 n_fors++;
994 REQ(ch, list_for);
995 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000996 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000998 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 count_list_iter:
1000 REQ(ch, list_iter);
1001 ch = CHILD(ch, 0);
1002 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001003 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001005 if (NCH(ch) == 3) {
1006 ch = CHILD(ch, 2);
1007 goto count_list_iter;
1008 }
1009 else
1010 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001012
1013 /* Should never be reached */
1014 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1015 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016}
1017
1018/* Count the number of 'if' statements in a list comprehension.
1019
1020 Helper for ast_for_listcomp().
1021*/
1022
1023static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001024count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025{
1026 int n_ifs = 0;
1027
1028 count_list_iter:
1029 REQ(n, list_iter);
1030 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001031 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 n = CHILD(n, 0);
1033 REQ(n, list_if);
1034 n_ifs++;
1035 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001036 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 n = CHILD(n, 2);
1038 goto count_list_iter;
1039}
1040
1041static expr_ty
1042ast_for_listcomp(struct compiling *c, const node *n)
1043{
1044 /* listmaker: test ( list_for | (',' test)* [','] )
1045 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1046 list_iter: list_for | list_if
1047 list_if: 'if' test [list_iter]
1048 testlist_safe: test [(',' test)+ [',']]
1049 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001050 expr_ty elt, first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051 asdl_seq *listcomps;
1052 int i, n_fors;
1053 node *ch;
1054
1055 REQ(n, listmaker);
1056 assert(NCH(n) > 1);
1057
1058 elt = ast_for_expr(c, CHILD(n, 0));
1059 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001060 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001062 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001064 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001066 listcomps = asdl_seq_new(n_fors, c->c_arena);
1067 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001068 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 ch = CHILD(n, 1);
1071 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001072 comprehension_ty lc;
1073 asdl_seq *t;
1074 expr_ty expression;
1075 node *for_ch;
1076
1077 REQ(ch, list_for);
1078
1079 for_ch = CHILD(ch, 1);
1080 t = ast_for_exprlist(c, for_ch, Store);
1081 if (!t)
1082 return NULL;
1083 expression = ast_for_testlist(c, CHILD(ch, 3));
1084 if (!expression)
1085 return NULL;
1086
1087 /* Check the # of children rather than the length of t, since
1088 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1089 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001090 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001091 if (NCH(for_ch) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001092 lc = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001093 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001094 lc = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001095 c->c_arena),
1096 expression, NULL, c->c_arena);
1097 if (!lc)
1098 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001100 if (NCH(ch) == 5) {
1101 int j, n_ifs;
1102 asdl_seq *ifs;
1103 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001105 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001106 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001107 if (n_ifs == -1)
1108 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001110 ifs = asdl_seq_new(n_ifs, c->c_arena);
1111 if (!ifs)
1112 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001114 for (j = 0; j < n_ifs; j++) {
1115 REQ(ch, list_iter);
1116 ch = CHILD(ch, 0);
1117 REQ(ch, list_if);
1118
1119 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1120 if (!list_for_expr)
1121 return NULL;
1122
1123 asdl_seq_SET(ifs, j, list_for_expr);
1124 if (NCH(ch) == 3)
1125 ch = CHILD(ch, 2);
1126 }
1127 /* on exit, must guarantee that ch is a list_for */
1128 if (TYPE(ch) == list_iter)
1129 ch = CHILD(ch, 0);
1130 lc->ifs = ifs;
1131 }
1132 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 }
1134
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001135 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136}
1137
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001138/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139
1140 Helper for ast_for_genexp().
1141*/
1142
1143static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001144count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001146 int n_fors = 0;
1147 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148
1149 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001150 n_fors++;
1151 REQ(ch, gen_for);
1152 if (NCH(ch) == 5)
1153 ch = CHILD(ch, 4);
1154 else
1155 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001157 REQ(ch, gen_iter);
1158 ch = CHILD(ch, 0);
1159 if (TYPE(ch) == gen_for)
1160 goto count_gen_for;
1161 else if (TYPE(ch) == gen_if) {
1162 if (NCH(ch) == 3) {
1163 ch = CHILD(ch, 2);
1164 goto count_gen_iter;
1165 }
1166 else
1167 return n_fors;
1168 }
1169
1170 /* Should never be reached */
1171 PyErr_SetString(PyExc_SystemError,
1172 "logic error in count_gen_fors");
1173 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174}
1175
1176/* Count the number of 'if' statements in a generator expression.
1177
1178 Helper for ast_for_genexp().
1179*/
1180
1181static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001182count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001184 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001186 while (1) {
1187 REQ(n, gen_iter);
1188 if (TYPE(CHILD(n, 0)) == gen_for)
1189 return n_ifs;
1190 n = CHILD(n, 0);
1191 REQ(n, gen_if);
1192 n_ifs++;
1193 if (NCH(n) == 2)
1194 return n_ifs;
1195 n = CHILD(n, 2);
1196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197}
1198
Jeremy Hyltona8293132006-02-28 17:58:27 +00001199/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200static expr_ty
1201ast_for_genexp(struct compiling *c, const node *n)
1202{
1203 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001204 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 expr_ty elt;
1206 asdl_seq *genexps;
1207 int i, n_fors;
1208 node *ch;
1209
1210 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1211 assert(NCH(n) > 1);
1212
1213 elt = ast_for_expr(c, CHILD(n, 0));
1214 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001215 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001217 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001219 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001220
1221 genexps = asdl_seq_new(n_fors, c->c_arena);
1222 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001223 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001224
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 ch = CHILD(n, 1);
1226 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001227 comprehension_ty ge;
1228 asdl_seq *t;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001229 expr_ty expression, first;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001230 node *for_ch;
1231
1232 REQ(ch, gen_for);
1233
1234 for_ch = CHILD(ch, 1);
1235 t = ast_for_exprlist(c, for_ch, Store);
1236 if (!t)
1237 return NULL;
1238 expression = ast_for_expr(c, CHILD(ch, 3));
1239 if (!expression)
1240 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001242 /* Check the # of children rather than the length of t, since
1243 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001244 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001245 if (NCH(for_ch) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001246 ge = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001247 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001248 ge = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001249 c->c_arena),
1250 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001251
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001252 if (!ge)
1253 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001254
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001255 if (NCH(ch) == 5) {
1256 int j, n_ifs;
1257 asdl_seq *ifs;
1258
1259 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001260 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001261 if (n_ifs == -1)
1262 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001263
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001264 ifs = asdl_seq_new(n_ifs, c->c_arena);
1265 if (!ifs)
1266 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001267
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001268 for (j = 0; j < n_ifs; j++) {
1269 REQ(ch, gen_iter);
1270 ch = CHILD(ch, 0);
1271 REQ(ch, gen_if);
1272
1273 expression = ast_for_expr(c, CHILD(ch, 1));
1274 if (!expression)
1275 return NULL;
1276 asdl_seq_SET(ifs, j, expression);
1277 if (NCH(ch) == 3)
1278 ch = CHILD(ch, 2);
1279 }
1280 /* on exit, must guarantee that ch is a gen_for */
1281 if (TYPE(ch) == gen_iter)
1282 ch = CHILD(ch, 0);
1283 ge->ifs = ifs;
1284 }
1285 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 }
1287
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001288 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289}
1290
1291static expr_ty
1292ast_for_atom(struct compiling *c, const node *n)
1293{
1294 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1295 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1296 */
1297 node *ch = CHILD(n, 0);
1298
1299 switch (TYPE(ch)) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001300 case NAME: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001301 /* All names start in Load context, but may later be
1302 changed. */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001303 PyObject *name = NEW_IDENTIFIER(ch);
1304 if (!name)
1305 return NULL;
1306 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1307 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001309 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001310 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001311#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001312 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1313 PyObject *type, *value, *tback, *errstr;
1314 PyErr_Fetch(&type, &value, &tback);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001315 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001316 if (errstr) {
1317 char *s = "";
1318 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001319 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001320 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1321 ast_error(n, buf);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001322 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001323 } else {
1324 ast_error(n, "(unicode error) unknown error");
1325 }
1326 Py_DECREF(type);
1327 Py_DECREF(value);
1328 Py_XDECREF(tback);
1329 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001330#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001331 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001332 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001333 PyArena_AddPyObject(c->c_arena, str);
1334 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 }
1336 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001337 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001338 if (!pynum)
1339 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001340
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001341 PyArena_AddPyObject(c->c_arena, pynum);
1342 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 }
1344 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001345 ch = CHILD(n, 1);
1346
1347 if (TYPE(ch) == RPAR)
1348 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1349
1350 if (TYPE(ch) == yield_expr)
1351 return ast_for_expr(c, ch);
1352
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001353 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001355 ch = CHILD(n, 1);
1356
1357 if (TYPE(ch) == RSQB)
1358 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1359
1360 REQ(ch, listmaker);
1361 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1362 asdl_seq *elts = seq_for_testlist(c, ch);
1363 if (!elts)
1364 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001365
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001366 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1367 }
1368 else
1369 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001371 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1372 int i, size;
1373 asdl_seq *keys, *values;
1374
1375 ch = CHILD(n, 1);
1376 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1377 keys = asdl_seq_new(size, c->c_arena);
1378 if (!keys)
1379 return NULL;
1380
1381 values = asdl_seq_new(size, c->c_arena);
1382 if (!values)
1383 return NULL;
1384
1385 for (i = 0; i < NCH(ch); i += 4) {
1386 expr_ty expression;
1387
1388 expression = ast_for_expr(c, CHILD(ch, i));
1389 if (!expression)
1390 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001391
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001392 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001393
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001394 expression = ast_for_expr(c, CHILD(ch, i + 2));
1395 if (!expression)
1396 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001397
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001398 asdl_seq_SET(values, i / 4, expression);
1399 }
1400 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 }
1402 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001403 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001404 if (Py_Py3kWarningFlag &&
1405 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001406 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001407 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001408 if (!expression)
1409 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001410
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001411 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 }
1413 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001414 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1415 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 }
1417}
1418
1419static slice_ty
1420ast_for_slice(struct compiling *c, const node *n)
1421{
1422 node *ch;
1423 expr_ty lower = NULL, upper = NULL, step = NULL;
1424
1425 REQ(n, subscript);
1426
1427 /*
1428 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1429 sliceop: ':' [test]
1430 */
1431 ch = CHILD(n, 0);
1432 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001433 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434
1435 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001436 /* 'step' variable hold no significance in terms of being used over
1437 other vars */
1438 step = ast_for_expr(c, ch);
1439 if (!step)
1440 return NULL;
1441
1442 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 }
1444
1445 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001446 lower = ast_for_expr(c, ch);
1447 if (!lower)
1448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 }
1450
1451 /* If there's an upper bound it's in the second or third position. */
1452 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001453 if (NCH(n) > 1) {
1454 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001456 if (TYPE(n2) == test) {
1457 upper = ast_for_expr(c, n2);
1458 if (!upper)
1459 return NULL;
1460 }
1461 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001463 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001465 if (TYPE(n2) == test) {
1466 upper = ast_for_expr(c, n2);
1467 if (!upper)
1468 return NULL;
1469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 }
1471
1472 ch = CHILD(n, NCH(n) - 1);
1473 if (TYPE(ch) == sliceop) {
Benjamin Peterson4879c902009-07-20 20:28:08 +00001474 if (NCH(ch) == 1) {
1475 /*
1476 This is an extended slice (ie "x[::]") with no expression in the
1477 step field. We set this literally to "None" in order to
1478 disambiguate it from x[:]. (The interpreter might have to call
1479 __getslice__ for x[:], but it must call __getitem__ for x[::].)
1480 */
1481 identifier none = new_identifier("None", c->c_arena);
1482 if (!none)
1483 return NULL;
1484 ch = CHILD(ch, 0);
1485 step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena);
1486 if (!step)
1487 return NULL;
1488 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001489 ch = CHILD(ch, 1);
1490 if (TYPE(ch) == test) {
1491 step = ast_for_expr(c, ch);
1492 if (!step)
1493 return NULL;
1494 }
1495 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 }
1497
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001498 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499}
1500
1501static expr_ty
1502ast_for_binop(struct compiling *c, const node *n)
1503{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001504 /* Must account for a sequence of expressions.
1505 How should A op B op C by represented?
1506 BinOp(BinOp(A, op, B), op, C).
1507 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001509 int i, nops;
1510 expr_ty expr1, expr2, result;
1511 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001513 expr1 = ast_for_expr(c, CHILD(n, 0));
1514 if (!expr1)
1515 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001517 expr2 = ast_for_expr(c, CHILD(n, 2));
1518 if (!expr2)
1519 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001521 newoperator = get_operator(CHILD(n, 1));
1522 if (!newoperator)
1523 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001525 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1526 c->c_arena);
1527 if (!result)
1528 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001530 nops = (NCH(n) - 1) / 2;
1531 for (i = 1; i < nops; i++) {
1532 expr_ty tmp_result, tmp;
1533 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001535 newoperator = get_operator(next_oper);
1536 if (!newoperator)
1537 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001539 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1540 if (!tmp)
1541 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001543 tmp_result = BinOp(result, newoperator, tmp,
1544 LINENO(next_oper), next_oper->n_col_offset,
1545 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001546 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001547 return NULL;
1548 result = tmp_result;
1549 }
1550 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551}
1552
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001553static expr_ty
1554ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1555{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001556 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1557 subscriptlist: subscript (',' subscript)* [',']
1558 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1559 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001560 REQ(n, trailer);
1561 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001562 if (NCH(n) == 2)
1563 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1564 n->n_col_offset, c->c_arena);
1565 else
1566 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001567 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001568 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001569 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1570 if (!attr_id)
1571 return NULL;
1572 return Attribute(left_expr, attr_id, Load,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001573 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001574 }
1575 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001576 REQ(CHILD(n, 0), LSQB);
1577 REQ(CHILD(n, 2), RSQB);
1578 n = CHILD(n, 1);
1579 if (NCH(n) == 1) {
1580 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1581 if (!slc)
1582 return NULL;
1583 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1584 c->c_arena);
1585 }
1586 else {
1587 /* The grammar is ambiguous here. The ambiguity is resolved
1588 by treating the sequence as a tuple literal if there are
1589 no slice features.
1590 */
1591 int j;
1592 slice_ty slc;
1593 expr_ty e;
1594 bool simple = true;
1595 asdl_seq *slices, *elts;
1596 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1597 if (!slices)
1598 return NULL;
1599 for (j = 0; j < NCH(n); j += 2) {
1600 slc = ast_for_slice(c, CHILD(n, j));
1601 if (!slc)
1602 return NULL;
1603 if (slc->kind != Index_kind)
1604 simple = false;
1605 asdl_seq_SET(slices, j / 2, slc);
1606 }
1607 if (!simple) {
1608 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1609 Load, LINENO(n), n->n_col_offset, c->c_arena);
1610 }
1611 /* extract Index values and put them in a Tuple */
1612 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1613 if (!elts)
1614 return NULL;
1615 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1616 slc = (slice_ty)asdl_seq_GET(slices, j);
1617 assert(slc->kind == Index_kind && slc->v.Index.value);
1618 asdl_seq_SET(elts, j, slc->v.Index.value);
1619 }
1620 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1621 if (!e)
1622 return NULL;
1623 return Subscript(left_expr, Index(e, c->c_arena),
1624 Load, LINENO(n), n->n_col_offset, c->c_arena);
1625 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001626 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001627}
1628
1629static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001630ast_for_factor(struct compiling *c, const node *n)
1631{
1632 node *pfactor, *ppower, *patom, *pnum;
1633 expr_ty expression;
1634
1635 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001636 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001637 constant. The peephole optimizer already does something like
1638 this but it doesn't handle the case where the constant is
1639 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1640 PyLongObject.
1641 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001642 if (TYPE(CHILD(n, 0)) == MINUS &&
1643 NCH(n) == 2 &&
1644 TYPE((pfactor = CHILD(n, 1))) == factor &&
1645 NCH(pfactor) == 1 &&
1646 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1647 NCH(ppower) == 1 &&
1648 TYPE((patom = CHILD(ppower, 0))) == atom &&
1649 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1650 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1651 if (s == NULL)
1652 return NULL;
1653 s[0] = '-';
1654 strcpy(s + 1, STR(pnum));
1655 PyObject_FREE(STR(pnum));
1656 STR(pnum) = s;
1657 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001658 }
1659
1660 expression = ast_for_expr(c, CHILD(n, 1));
1661 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001662 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001663
1664 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001665 case PLUS:
1666 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1667 c->c_arena);
1668 case MINUS:
1669 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1670 c->c_arena);
1671 case TILDE:
1672 return UnaryOp(Invert, expression, LINENO(n),
1673 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001674 }
1675 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001676 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001677 return NULL;
1678}
1679
1680static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001681ast_for_power(struct compiling *c, const node *n)
1682{
1683 /* power: atom trailer* ('**' factor)*
1684 */
1685 int i;
1686 expr_ty e, tmp;
1687 REQ(n, power);
1688 e = ast_for_atom(c, CHILD(n, 0));
1689 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001690 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001691 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001692 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001693 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001694 node *ch = CHILD(n, i);
1695 if (TYPE(ch) != trailer)
1696 break;
1697 tmp = ast_for_trailer(c, ch, e);
1698 if (!tmp)
1699 return NULL;
1700 tmp->lineno = e->lineno;
1701 tmp->col_offset = e->col_offset;
1702 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001703 }
1704 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001705 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1706 if (!f)
1707 return NULL;
1708 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1709 if (!tmp)
1710 return NULL;
1711 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001712 }
1713 return e;
1714}
1715
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716/* Do not name a variable 'expr'! Will cause a compile error.
1717*/
1718
1719static expr_ty
1720ast_for_expr(struct compiling *c, const node *n)
1721{
1722 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001723 test: or_test ['if' or_test 'else' test] | lambdef
1724 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 and_test: not_test ('and' not_test)*
1726 not_test: 'not' not_test | comparison
1727 comparison: expr (comp_op expr)*
1728 expr: xor_expr ('|' xor_expr)*
1729 xor_expr: and_expr ('^' and_expr)*
1730 and_expr: shift_expr ('&' shift_expr)*
1731 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1732 arith_expr: term (('+'|'-') term)*
1733 term: factor (('*'|'/'|'%'|'//') factor)*
1734 factor: ('+'|'-'|'~') factor | power
1735 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001736
1737 As well as modified versions that exist for backward compatibility,
1738 to explicitly allow:
1739 [ x for x in lambda: 0, lambda: 1 ]
1740 (which would be ambiguous without these extra rules)
1741
1742 old_test: or_test | old_lambdef
1743 old_lambdef: 'lambda' [vararglist] ':' old_test
1744
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 */
1746
1747 asdl_seq *seq;
1748 int i;
1749
1750 loop:
1751 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001752 case test:
1753 case old_test:
1754 if (TYPE(CHILD(n, 0)) == lambdef ||
1755 TYPE(CHILD(n, 0)) == old_lambdef)
1756 return ast_for_lambdef(c, CHILD(n, 0));
1757 else if (NCH(n) > 1)
1758 return ast_for_ifexpr(c, n);
1759 /* Fallthrough */
1760 case or_test:
1761 case and_test:
1762 if (NCH(n) == 1) {
1763 n = CHILD(n, 0);
1764 goto loop;
1765 }
1766 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1767 if (!seq)
1768 return NULL;
1769 for (i = 0; i < NCH(n); i += 2) {
1770 expr_ty e = ast_for_expr(c, CHILD(n, i));
1771 if (!e)
1772 return NULL;
1773 asdl_seq_SET(seq, i / 2, e);
1774 }
1775 if (!strcmp(STR(CHILD(n, 1)), "and"))
1776 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1777 c->c_arena);
1778 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1779 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1780 case not_test:
1781 if (NCH(n) == 1) {
1782 n = CHILD(n, 0);
1783 goto loop;
1784 }
1785 else {
1786 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1787 if (!expression)
1788 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001790 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1791 c->c_arena);
1792 }
1793 case comparison:
1794 if (NCH(n) == 1) {
1795 n = CHILD(n, 0);
1796 goto loop;
1797 }
1798 else {
1799 expr_ty expression;
1800 asdl_int_seq *ops;
1801 asdl_seq *cmps;
1802 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1803 if (!ops)
1804 return NULL;
1805 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1806 if (!cmps) {
1807 return NULL;
1808 }
1809 for (i = 1; i < NCH(n); i += 2) {
1810 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001812 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001813 if (!newoperator) {
1814 return NULL;
1815 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001817 expression = ast_for_expr(c, CHILD(n, i + 1));
1818 if (!expression) {
1819 return NULL;
1820 }
1821
1822 asdl_seq_SET(ops, i / 2, newoperator);
1823 asdl_seq_SET(cmps, i / 2, expression);
1824 }
1825 expression = ast_for_expr(c, CHILD(n, 0));
1826 if (!expression) {
1827 return NULL;
1828 }
1829
1830 return Compare(expression, ops, cmps, LINENO(n),
1831 n->n_col_offset, c->c_arena);
1832 }
1833 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001835 /* The next five cases all handle BinOps. The main body of code
1836 is the same in each case, but the switch turned inside out to
1837 reuse the code for each type of operator.
1838 */
1839 case expr:
1840 case xor_expr:
1841 case and_expr:
1842 case shift_expr:
1843 case arith_expr:
1844 case term:
1845 if (NCH(n) == 1) {
1846 n = CHILD(n, 0);
1847 goto loop;
1848 }
1849 return ast_for_binop(c, n);
1850 case yield_expr: {
1851 expr_ty exp = NULL;
1852 if (NCH(n) == 2) {
1853 exp = ast_for_testlist(c, CHILD(n, 1));
1854 if (!exp)
1855 return NULL;
1856 }
1857 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1858 }
1859 case factor:
1860 if (NCH(n) == 1) {
1861 n = CHILD(n, 0);
1862 goto loop;
1863 }
1864 return ast_for_factor(c, n);
1865 case power:
1866 return ast_for_power(c, n);
1867 default:
1868 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1869 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001871 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 return NULL;
1873}
1874
1875static expr_ty
1876ast_for_call(struct compiling *c, const node *n, expr_ty func)
1877{
1878 /*
1879 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001880 | '**' test)
1881 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 */
1883
1884 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001885 asdl_seq *args;
1886 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 expr_ty vararg = NULL, kwarg = NULL;
1888
1889 REQ(n, arglist);
1890
1891 nargs = 0;
1892 nkeywords = 0;
1893 ngens = 0;
1894 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001895 node *ch = CHILD(n, i);
1896 if (TYPE(ch) == argument) {
1897 if (NCH(ch) == 1)
1898 nargs++;
1899 else if (TYPE(CHILD(ch, 1)) == gen_for)
1900 ngens++;
1901 else
1902 nkeywords++;
1903 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904 }
1905 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001906 ast_error(n, "Generator expression must be parenthesized "
1907 "if not sole argument");
1908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 }
1910
1911 if (nargs + nkeywords + ngens > 255) {
1912 ast_error(n, "more than 255 arguments");
1913 return NULL;
1914 }
1915
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001916 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001918 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001919 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001921 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922 nargs = 0;
1923 nkeywords = 0;
1924 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001925 node *ch = CHILD(n, i);
1926 if (TYPE(ch) == argument) {
1927 expr_ty e;
1928 if (NCH(ch) == 1) {
1929 if (nkeywords) {
1930 ast_error(CHILD(ch, 0),
1931 "non-keyword arg after keyword arg");
1932 return NULL;
1933 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001934 if (vararg) {
1935 ast_error(CHILD(ch, 0),
1936 "only named arguments may follow *expression");
1937 return NULL;
1938 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001939 e = ast_for_expr(c, CHILD(ch, 0));
1940 if (!e)
1941 return NULL;
1942 asdl_seq_SET(args, nargs++, e);
1943 }
1944 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1945 e = ast_for_genexp(c, ch);
1946 if (!e)
1947 return NULL;
1948 asdl_seq_SET(args, nargs++, e);
1949 }
1950 else {
1951 keyword_ty kw;
1952 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001953 int k;
1954 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001956 /* CHILD(ch, 0) is test, but must be an identifier? */
1957 e = ast_for_expr(c, CHILD(ch, 0));
1958 if (!e)
1959 return NULL;
1960 /* f(lambda x: x[0] = 3) ends up getting parsed with
1961 * LHS test = lambda x: x[0], and RHS test = 3.
1962 * SF bug 132313 points out that complaining about a keyword
1963 * then is very confusing.
1964 */
1965 if (e->kind == Lambda_kind) {
1966 ast_error(CHILD(ch, 0),
1967 "lambda cannot contain assignment");
1968 return NULL;
1969 } else if (e->kind != Name_kind) {
1970 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1971 return NULL;
1972 }
1973 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001974 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001975 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001976 for (k = 0; k < nkeywords; k++) {
1977 tmp = PyString_AS_STRING(
1978 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
1979 if (!strcmp(tmp, PyString_AS_STRING(key))) {
1980 ast_error(CHILD(ch, 0), "keyword argument repeated");
1981 return NULL;
1982 }
1983 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001984 e = ast_for_expr(c, CHILD(ch, 2));
1985 if (!e)
1986 return NULL;
1987 kw = keyword(key, e, c->c_arena);
1988 if (!kw)
1989 return NULL;
1990 asdl_seq_SET(keywords, nkeywords++, kw);
1991 }
1992 }
1993 else if (TYPE(ch) == STAR) {
1994 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001995 if (!vararg)
1996 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001997 i++;
1998 }
1999 else if (TYPE(ch) == DOUBLESTAR) {
2000 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002001 if (!kwarg)
2002 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002003 i++;
2004 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 }
2006
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002007 return Call(func, args, keywords, vararg, kwarg, func->lineno,
2008 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009}
2010
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002012ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002014 /* testlist_gexp: test (',' test)* [','] */
2015 /* testlist: test (',' test)* [','] */
2016 /* testlist_safe: test (',' test)+ [','] */
2017 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002019 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002020 if (NCH(n) > 1)
2021 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002022 }
2023 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002024 assert(TYPE(n) == testlist ||
2025 TYPE(n) == testlist_safe ||
2026 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002027 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002029 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002031 asdl_seq *tmp = seq_for_testlist(c, n);
2032 if (!tmp)
2033 return NULL;
2034 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002036}
2037
2038static expr_ty
2039ast_for_testlist_gexp(struct compiling *c, const node* n)
2040{
2041 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2042 /* argument: test [ gen_for ] */
2043 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002044 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002045 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002046 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002047}
2048
2049/* like ast_for_testlist() but returns a sequence */
2050static asdl_seq*
2051ast_for_class_bases(struct compiling *c, const node* n)
2052{
2053 /* testlist: test (',' test)* [','] */
2054 assert(NCH(n) > 0);
2055 REQ(n, testlist);
2056 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002057 expr_ty base;
2058 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2059 if (!bases)
2060 return NULL;
2061 base = ast_for_expr(c, CHILD(n, 0));
2062 if (!base)
2063 return NULL;
2064 asdl_seq_SET(bases, 0, base);
2065 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002066 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002067
2068 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069}
2070
2071static stmt_ty
2072ast_for_expr_stmt(struct compiling *c, const node *n)
2073{
2074 REQ(n, expr_stmt);
2075 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002076 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 testlist: test (',' test)* [',']
2078 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002079 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080 test: ... here starts the operator precendence dance
2081 */
2082
2083 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002084 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2085 if (!e)
2086 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002088 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 }
2090 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002091 expr_ty expr1, expr2;
2092 operator_ty newoperator;
2093 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002095 expr1 = ast_for_testlist(c, ch);
2096 if (!expr1)
2097 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002098 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002099 return NULL;
Benjamin Peterson7adbb5a2009-10-03 20:23:24 +00002100 /* set_context checks that most expressions are not the left side.
2101 Augmented assignments can only have a name, a subscript, or an
2102 attribute on the left, though, so we have to explicitly check for
2103 those. */
2104 switch (expr1->kind) {
2105 case Name_kind:
2106 case Attribute_kind:
2107 case Subscript_kind:
2108 break;
2109 default:
2110 ast_error(ch, "illegal expression for augmented assignment");
2111 return NULL;
2112 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002114 ch = CHILD(n, 2);
2115 if (TYPE(ch) == testlist)
2116 expr2 = ast_for_testlist(c, ch);
2117 else
2118 expr2 = ast_for_expr(c, ch);
2119 if (!expr2)
2120 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002122 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002123 if (!newoperator)
2124 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002126 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2127 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 }
2129 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002130 int i;
2131 asdl_seq *targets;
2132 node *value;
2133 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002135 /* a normal assignment */
2136 REQ(CHILD(n, 1), EQUAL);
2137 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2138 if (!targets)
2139 return NULL;
2140 for (i = 0; i < NCH(n) - 2; i += 2) {
2141 expr_ty e;
2142 node *ch = CHILD(n, i);
Benjamin Petersonb2664812009-06-11 17:49:38 +00002143 if (TYPE(ch) == yield_expr) {
2144 ast_error(ch, "assignment to yield expression not possible");
2145 return NULL;
2146 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002147 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002149 /* set context to assign */
2150 if (!e)
2151 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002153 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002154 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002156 asdl_seq_SET(targets, i / 2, e);
2157 }
2158 value = CHILD(n, NCH(n) - 1);
2159 if (TYPE(value) == testlist)
2160 expression = ast_for_testlist(c, value);
2161 else
2162 expression = ast_for_expr(c, value);
2163 if (!expression)
2164 return NULL;
2165 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2166 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168}
2169
2170static stmt_ty
2171ast_for_print_stmt(struct compiling *c, const node *n)
2172{
2173 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002174 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 */
2176 expr_ty dest = NULL, expression;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002177 asdl_seq *seq = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 bool nl;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002179 int i, j, values_count, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180
2181 REQ(n, print_stmt);
2182 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002183 dest = ast_for_expr(c, CHILD(n, 2));
2184 if (!dest)
2185 return NULL;
2186 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 }
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002188 values_count = (NCH(n) + 1 - start) / 2;
2189 if (values_count) {
2190 seq = asdl_seq_new(values_count, c->c_arena);
2191 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002192 return NULL;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002193 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
2194 expression = ast_for_expr(c, CHILD(n, i));
2195 if (!expression)
2196 return NULL;
2197 asdl_seq_SET(seq, j, expression);
2198 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 }
2200 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002201 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202}
2203
2204static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002205ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206{
2207 asdl_seq *seq;
2208 int i;
2209 expr_ty e;
2210
2211 REQ(n, exprlist);
2212
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002213 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002215 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002217 e = ast_for_expr(c, CHILD(n, i));
2218 if (!e)
2219 return NULL;
2220 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002221 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002222 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 }
2224 return seq;
2225}
2226
2227static stmt_ty
2228ast_for_del_stmt(struct compiling *c, const node *n)
2229{
2230 asdl_seq *expr_list;
2231
2232 /* del_stmt: 'del' exprlist */
2233 REQ(n, del_stmt);
2234
2235 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2236 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002237 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002238 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239}
2240
2241static stmt_ty
2242ast_for_flow_stmt(struct compiling *c, const node *n)
2243{
2244 /*
2245 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002246 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 break_stmt: 'break'
2248 continue_stmt: 'continue'
2249 return_stmt: 'return' [testlist]
2250 yield_stmt: yield_expr
2251 yield_expr: 'yield' testlist
2252 raise_stmt: 'raise' [test [',' test [',' test]]]
2253 */
2254 node *ch;
2255
2256 REQ(n, flow_stmt);
2257 ch = CHILD(n, 0);
2258 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002259 case break_stmt:
2260 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2261 case continue_stmt:
2262 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2263 case yield_stmt: { /* will reduce to yield_expr */
2264 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2265 if (!exp)
2266 return NULL;
2267 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2268 }
2269 case return_stmt:
2270 if (NCH(ch) == 1)
2271 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2272 else {
2273 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2274 if (!expression)
2275 return NULL;
2276 return Return(expression, LINENO(n), n->n_col_offset,
2277 c->c_arena);
2278 }
2279 case raise_stmt:
2280 if (NCH(ch) == 1)
2281 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2282 c->c_arena);
2283 else if (NCH(ch) == 2) {
2284 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2285 if (!expression)
2286 return NULL;
2287 return Raise(expression, NULL, NULL, LINENO(n),
2288 n->n_col_offset, c->c_arena);
2289 }
2290 else if (NCH(ch) == 4) {
2291 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002293 expr1 = ast_for_expr(c, CHILD(ch, 1));
2294 if (!expr1)
2295 return NULL;
2296 expr2 = ast_for_expr(c, CHILD(ch, 3));
2297 if (!expr2)
2298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002300 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2301 c->c_arena);
2302 }
2303 else if (NCH(ch) == 6) {
2304 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002306 expr1 = ast_for_expr(c, CHILD(ch, 1));
2307 if (!expr1)
2308 return NULL;
2309 expr2 = ast_for_expr(c, CHILD(ch, 3));
2310 if (!expr2)
2311 return NULL;
2312 expr3 = ast_for_expr(c, CHILD(ch, 5));
2313 if (!expr3)
2314 return NULL;
2315
2316 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2317 c->c_arena);
2318 }
2319 default:
2320 PyErr_Format(PyExc_SystemError,
2321 "unexpected flow_stmt: %d", TYPE(ch));
2322 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002324
2325 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2326 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327}
2328
2329static alias_ty
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002330alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331{
2332 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002333 import_as_name: NAME ['as' NAME]
2334 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335 dotted_name: NAME ('.' NAME)*
2336 */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002337 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002338
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 loop:
2340 switch (TYPE(n)) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002341 case import_as_name: {
2342 node *name_node = CHILD(n, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002343 str = NULL;
2344 if (NCH(n) == 3) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002345 node *str_node = CHILD(n, 2);
2346 if (store && !forbidden_check(c, str_node, STR(str_node)))
2347 return NULL;
2348 str = NEW_IDENTIFIER(str_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002349 if (!str)
2350 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002351 }
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002352 else {
2353 if (!forbidden_check(c, name_node, STR(name_node)))
2354 return NULL;
2355 }
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002356 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002357 if (!name)
2358 return NULL;
2359 return alias(name, str, c->c_arena);
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002360 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002361 case dotted_as_name:
2362 if (NCH(n) == 1) {
2363 n = CHILD(n, 0);
2364 goto loop;
2365 }
2366 else {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002367 node *asname_node = CHILD(n, 2);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002368 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002369 if (!a)
2370 return NULL;
2371 assert(!a->asname);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002372 if (!forbidden_check(c, asname_node, STR(asname_node)))
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002373 return NULL;
2374 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002375 if (!a->asname)
2376 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002377 return a;
2378 }
2379 break;
2380 case dotted_name:
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002381 if (NCH(n) == 1) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002382 node *name_node = CHILD(n, 0);
2383 if (store && !forbidden_check(c, name_node, STR(name_node)))
2384 return NULL;
2385 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002386 if (!name)
2387 return NULL;
2388 return alias(name, NULL, c->c_arena);
2389 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002390 else {
2391 /* Create a string of the form "a.b.c" */
2392 int i;
2393 size_t len;
2394 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002396 len = 0;
2397 for (i = 0; i < NCH(n); i += 2)
2398 /* length of string plus one for the dot */
2399 len += strlen(STR(CHILD(n, i))) + 1;
2400 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002401 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002402 if (!str)
2403 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002404 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002405 if (!s)
2406 return NULL;
2407 for (i = 0; i < NCH(n); i += 2) {
2408 char *sch = STR(CHILD(n, i));
2409 strcpy(s, STR(CHILD(n, i)));
2410 s += strlen(sch);
2411 *s++ = '.';
2412 }
2413 --s;
2414 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002415 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002416 PyArena_AddPyObject(c->c_arena, str);
2417 return alias(str, NULL, c->c_arena);
2418 }
2419 break;
2420 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002421 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002422 PyArena_AddPyObject(c->c_arena, str);
2423 return alias(str, NULL, c->c_arena);
2424 default:
2425 PyErr_Format(PyExc_SystemError,
2426 "unexpected import name: %d", TYPE(n));
2427 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002429
2430 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 return NULL;
2432}
2433
2434static stmt_ty
2435ast_for_import_stmt(struct compiling *c, const node *n)
2436{
2437 /*
2438 import_stmt: import_name | import_from
2439 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002440 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002441 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002443 int lineno;
2444 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445 int i;
2446 asdl_seq *aliases;
2447
2448 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002449 lineno = LINENO(n);
2450 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002452 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002453 n = CHILD(n, 1);
2454 REQ(n, dotted_as_names);
2455 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2456 if (!aliases)
2457 return NULL;
2458 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002459 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002460 if (!import_alias)
2461 return NULL;
2462 asdl_seq_SET(aliases, i / 2, import_alias);
2463 }
2464 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002466 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002467 int n_children;
2468 int idx, ndots = 0;
2469 alias_ty mod = NULL;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002470 identifier modname = NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002471
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002472 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002473 optional module name */
2474 for (idx = 1; idx < NCH(n); idx++) {
2475 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002476 mod = alias_for_import_name(c, CHILD(n, idx), 0);
2477 if (!mod)
2478 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002479 idx++;
2480 break;
2481 } else if (TYPE(CHILD(n, idx)) != DOT) {
2482 break;
2483 }
2484 ndots++;
2485 }
2486 idx++; /* skip over the 'import' keyword */
2487 switch (TYPE(CHILD(n, idx))) {
2488 case STAR:
2489 /* from ... import * */
2490 n = CHILD(n, idx);
2491 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002492 break;
2493 case LPAR:
2494 /* from ... import (x, y, z) */
2495 n = CHILD(n, idx + 1);
2496 n_children = NCH(n);
2497 break;
2498 case import_as_names:
2499 /* from ... import x, y, z */
2500 n = CHILD(n, idx);
2501 n_children = NCH(n);
2502 if (n_children % 2 == 0) {
2503 ast_error(n, "trailing comma not allowed without"
2504 " surrounding parentheses");
2505 return NULL;
2506 }
2507 break;
2508 default:
2509 ast_error(n, "Unexpected node-type in from-import");
2510 return NULL;
2511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002513 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2514 if (!aliases)
2515 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002517 /* handle "from ... import *" special b/c there's no children */
2518 if (TYPE(n) == STAR) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002519 alias_ty import_alias = alias_for_import_name(c, n, 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002520 if (!import_alias)
2521 return NULL;
2522 asdl_seq_SET(aliases, 0, import_alias);
2523 }
2524 else {
2525 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002526 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002527 if (!import_alias)
2528 return NULL;
2529 asdl_seq_SET(aliases, i / 2, import_alias);
2530 }
2531 }
2532 if (mod != NULL)
2533 modname = mod->name;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002534 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2535 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 }
Neal Norwitz79792652005-11-14 04:25:03 +00002537 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002538 "unknown import statement: starts with command '%s'",
2539 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 return NULL;
2541}
2542
2543static stmt_ty
2544ast_for_global_stmt(struct compiling *c, const node *n)
2545{
2546 /* global_stmt: 'global' NAME (',' NAME)* */
2547 identifier name;
2548 asdl_seq *s;
2549 int i;
2550
2551 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002552 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002554 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002556 name = NEW_IDENTIFIER(CHILD(n, i));
2557 if (!name)
2558 return NULL;
2559 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002561 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562}
2563
2564static stmt_ty
2565ast_for_exec_stmt(struct compiling *c, const node *n)
2566{
2567 expr_ty expr1, globals = NULL, locals = NULL;
2568 int n_children = NCH(n);
2569 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002570 PyErr_Format(PyExc_SystemError,
2571 "poorly formed 'exec' statement: %d parts to statement",
2572 n_children);
2573 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 }
2575
2576 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2577 REQ(n, exec_stmt);
2578 expr1 = ast_for_expr(c, CHILD(n, 1));
2579 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002580 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002582 globals = ast_for_expr(c, CHILD(n, 3));
2583 if (!globals)
2584 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 }
2586 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002587 locals = ast_for_expr(c, CHILD(n, 5));
2588 if (!locals)
2589 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 }
2591
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002592 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2593 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594}
2595
2596static stmt_ty
2597ast_for_assert_stmt(struct compiling *c, const node *n)
2598{
2599 /* assert_stmt: 'assert' test [',' test] */
2600 REQ(n, assert_stmt);
2601 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002602 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2603 if (!expression)
2604 return NULL;
2605 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2606 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 }
2608 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002609 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002611 expr1 = ast_for_expr(c, CHILD(n, 1));
2612 if (!expr1)
2613 return NULL;
2614 expr2 = ast_for_expr(c, CHILD(n, 3));
2615 if (!expr2)
2616 return NULL;
2617
2618 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 }
Neal Norwitz79792652005-11-14 04:25:03 +00002620 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002621 "improper number of parts to 'assert' statement: %d",
2622 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 return NULL;
2624}
2625
2626static asdl_seq *
2627ast_for_suite(struct compiling *c, const node *n)
2628{
2629 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002630 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 stmt_ty s;
2632 int i, total, num, end, pos = 0;
2633 node *ch;
2634
2635 REQ(n, suite);
2636
2637 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002638 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002642 n = CHILD(n, 0);
2643 /* simple_stmt always ends with a NEWLINE,
2644 and may have a trailing SEMI
2645 */
2646 end = NCH(n) - 1;
2647 if (TYPE(CHILD(n, end - 1)) == SEMI)
2648 end--;
2649 /* loop by 2 to skip semi-colons */
2650 for (i = 0; i < end; i += 2) {
2651 ch = CHILD(n, i);
2652 s = ast_for_stmt(c, ch);
2653 if (!s)
2654 return NULL;
2655 asdl_seq_SET(seq, pos++, s);
2656 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 }
2658 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002659 for (i = 2; i < (NCH(n) - 1); i++) {
2660 ch = CHILD(n, i);
2661 REQ(ch, stmt);
2662 num = num_stmts(ch);
2663 if (num == 1) {
2664 /* small_stmt or compound_stmt with only one child */
2665 s = ast_for_stmt(c, ch);
2666 if (!s)
2667 return NULL;
2668 asdl_seq_SET(seq, pos++, s);
2669 }
2670 else {
2671 int j;
2672 ch = CHILD(ch, 0);
2673 REQ(ch, simple_stmt);
2674 for (j = 0; j < NCH(ch); j += 2) {
2675 /* statement terminates with a semi-colon ';' */
2676 if (NCH(CHILD(ch, j)) == 0) {
2677 assert((j + 1) == NCH(ch));
2678 break;
2679 }
2680 s = ast_for_stmt(c, CHILD(ch, j));
2681 if (!s)
2682 return NULL;
2683 asdl_seq_SET(seq, pos++, s);
2684 }
2685 }
2686 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687 }
2688 assert(pos == seq->size);
2689 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690}
2691
2692static stmt_ty
2693ast_for_if_stmt(struct compiling *c, const node *n)
2694{
2695 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2696 ['else' ':' suite]
2697 */
2698 char *s;
2699
2700 REQ(n, if_stmt);
2701
2702 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002703 expr_ty expression;
2704 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002706 expression = ast_for_expr(c, CHILD(n, 1));
2707 if (!expression)
2708 return NULL;
2709 suite_seq = ast_for_suite(c, CHILD(n, 3));
2710 if (!suite_seq)
2711 return NULL;
2712
2713 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2714 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002716
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717 s = STR(CHILD(n, 4));
2718 /* s[2], the third character in the string, will be
2719 's' for el_s_e, or
2720 'i' for el_i_f
2721 */
2722 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002723 expr_ty expression;
2724 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002726 expression = ast_for_expr(c, CHILD(n, 1));
2727 if (!expression)
2728 return NULL;
2729 seq1 = ast_for_suite(c, CHILD(n, 3));
2730 if (!seq1)
2731 return NULL;
2732 seq2 = ast_for_suite(c, CHILD(n, 6));
2733 if (!seq2)
2734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002736 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2737 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 }
2739 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002740 int i, n_elif, has_else = 0;
2741 expr_ty expression;
2742 asdl_seq *suite_seq;
2743 asdl_seq *orelse = NULL;
2744 n_elif = NCH(n) - 4;
2745 /* must reference the child n_elif+1 since 'else' token is third,
2746 not fourth, child from the end. */
2747 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2748 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2749 has_else = 1;
2750 n_elif -= 3;
2751 }
2752 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002754 if (has_else) {
2755 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002757 orelse = asdl_seq_new(1, c->c_arena);
2758 if (!orelse)
2759 return NULL;
2760 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2761 if (!expression)
2762 return NULL;
2763 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2764 if (!suite_seq)
2765 return NULL;
2766 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2767 if (!suite_seq2)
2768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002770 asdl_seq_SET(orelse, 0,
2771 If(expression, suite_seq, suite_seq2,
2772 LINENO(CHILD(n, NCH(n) - 6)),
2773 CHILD(n, NCH(n) - 6)->n_col_offset,
2774 c->c_arena));
2775 /* the just-created orelse handled the last elif */
2776 n_elif--;
2777 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002779 for (i = 0; i < n_elif; i++) {
2780 int off = 5 + (n_elif - i - 1) * 4;
2781 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2782 if (!newobj)
2783 return NULL;
2784 expression = ast_for_expr(c, CHILD(n, off));
2785 if (!expression)
2786 return NULL;
2787 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2788 if (!suite_seq)
2789 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002791 asdl_seq_SET(newobj, 0,
2792 If(expression, suite_seq, orelse,
2793 LINENO(CHILD(n, off)),
2794 CHILD(n, off)->n_col_offset, c->c_arena));
2795 orelse = newobj;
2796 }
2797 expression = ast_for_expr(c, CHILD(n, 1));
2798 if (!expression)
2799 return NULL;
2800 suite_seq = ast_for_suite(c, CHILD(n, 3));
2801 if (!suite_seq)
2802 return NULL;
2803 return If(expression, suite_seq, orelse,
2804 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002806
2807 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002808 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810}
2811
2812static stmt_ty
2813ast_for_while_stmt(struct compiling *c, const node *n)
2814{
2815 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2816 REQ(n, while_stmt);
2817
2818 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002819 expr_ty expression;
2820 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002822 expression = ast_for_expr(c, CHILD(n, 1));
2823 if (!expression)
2824 return NULL;
2825 suite_seq = ast_for_suite(c, CHILD(n, 3));
2826 if (!suite_seq)
2827 return NULL;
2828 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2829 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 }
2831 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002832 expr_ty expression;
2833 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002835 expression = ast_for_expr(c, CHILD(n, 1));
2836 if (!expression)
2837 return NULL;
2838 seq1 = ast_for_suite(c, CHILD(n, 3));
2839 if (!seq1)
2840 return NULL;
2841 seq2 = ast_for_suite(c, CHILD(n, 6));
2842 if (!seq2)
2843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002845 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2846 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002848
2849 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002850 "wrong number of tokens for 'while' statement: %d",
2851 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853}
2854
2855static stmt_ty
2856ast_for_for_stmt(struct compiling *c, const node *n)
2857{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002858 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 expr_ty expression;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002860 expr_ty target, first;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002861 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2863 REQ(n, for_stmt);
2864
2865 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002866 seq = ast_for_suite(c, CHILD(n, 8));
2867 if (!seq)
2868 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 }
2870
Neal Norwitzedef2be2006-07-12 05:26:17 +00002871 node_target = CHILD(n, 1);
2872 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002873 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002874 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002875 /* Check the # of children rather than the length of _target, since
2876 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002877 first = (expr_ty)asdl_seq_GET(_target, 0);
Neal Norwitzedef2be2006-07-12 05:26:17 +00002878 if (NCH(node_target) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002879 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002881 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002883 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002884 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002887 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002888 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002890 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002891 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892}
2893
2894static excepthandler_ty
2895ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2896{
Collin Winter62903052007-05-18 23:11:24 +00002897 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898 REQ(exc, except_clause);
2899 REQ(body, suite);
2900
2901 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002902 asdl_seq *suite_seq = ast_for_suite(c, body);
2903 if (!suite_seq)
2904 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905
Georg Brandla48f3ab2008-03-30 06:40:17 +00002906 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002907 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 }
2909 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002910 expr_ty expression;
2911 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002913 expression = ast_for_expr(c, CHILD(exc, 1));
2914 if (!expression)
2915 return NULL;
2916 suite_seq = ast_for_suite(c, body);
2917 if (!suite_seq)
2918 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919
Georg Brandla48f3ab2008-03-30 06:40:17 +00002920 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002921 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922 }
2923 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002924 asdl_seq *suite_seq;
2925 expr_ty expression;
2926 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2927 if (!e)
2928 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002929 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002930 return NULL;
2931 expression = ast_for_expr(c, CHILD(exc, 1));
2932 if (!expression)
2933 return NULL;
2934 suite_seq = ast_for_suite(c, body);
2935 if (!suite_seq)
2936 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937
Georg Brandla48f3ab2008-03-30 06:40:17 +00002938 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002939 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002941
2942 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002943 "wrong number of children for 'except' clause: %d",
2944 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002945 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946}
2947
2948static stmt_ty
2949ast_for_try_stmt(struct compiling *c, const node *n)
2950{
Neal Norwitzf599f422005-12-17 21:33:47 +00002951 const int nch = NCH(n);
2952 int n_except = (nch - 3)/3;
2953 asdl_seq *body, *orelse = NULL, *finally = NULL;
2954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 REQ(n, try_stmt);
2956
Neal Norwitzf599f422005-12-17 21:33:47 +00002957 body = ast_for_suite(c, CHILD(n, 2));
2958 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002959 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960
Neal Norwitzf599f422005-12-17 21:33:47 +00002961 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002962 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2963 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2964 /* we can assume it's an "else",
2965 because nch >= 9 for try-else-finally and
2966 it would otherwise have a type of except_clause */
2967 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2968 if (orelse == NULL)
2969 return NULL;
2970 n_except--;
2971 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002973 finally = ast_for_suite(c, CHILD(n, nch - 1));
2974 if (finally == NULL)
2975 return NULL;
2976 n_except--;
2977 }
2978 else {
2979 /* we can assume it's an "else",
2980 otherwise it would have a type of except_clause */
2981 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2982 if (orelse == NULL)
2983 return NULL;
2984 n_except--;
2985 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002987 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002988 ast_error(n, "malformed 'try' statement");
2989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002991
2992 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002993 int i;
2994 stmt_ty except_st;
2995 /* process except statements to create a try ... except */
2996 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2997 if (handlers == NULL)
2998 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002999
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003000 for (i = 0; i < n_except; i++) {
3001 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3002 CHILD(n, 5 + i * 3));
3003 if (!e)
3004 return NULL;
3005 asdl_seq_SET(handlers, i, e);
3006 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003007
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003008 except_st = TryExcept(body, handlers, orelse, LINENO(n),
3009 n->n_col_offset, c->c_arena);
3010 if (!finally)
3011 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00003012
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003013 /* if a 'finally' is present too, we nest the TryExcept within a
3014 TryFinally to emulate try ... except ... finally */
3015 body = asdl_seq_new(1, c->c_arena);
3016 if (body == NULL)
3017 return NULL;
3018 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00003019 }
3020
3021 /* must be a try ... finally (except clauses are in body, if any exist) */
3022 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003023 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024}
3025
Georg Brandl944f6842009-05-25 21:02:56 +00003026/* with_item: test ['as' expr] */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003027static stmt_ty
Georg Brandl944f6842009-05-25 21:02:56 +00003028ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003029{
3030 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003031
Georg Brandl944f6842009-05-25 21:02:56 +00003032 REQ(n, with_item);
3033 context_expr = ast_for_expr(c, CHILD(n, 0));
Collin Winter77c67bd2007-03-16 04:11:30 +00003034 if (!context_expr)
3035 return NULL;
Georg Brandl944f6842009-05-25 21:02:56 +00003036 if (NCH(n) == 3) {
3037 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003038
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003039 if (!optional_vars) {
3040 return NULL;
3041 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003042 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003043 return NULL;
3044 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003045 }
3046
Georg Brandl944f6842009-05-25 21:02:56 +00003047 return With(context_expr, optional_vars, content, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003048 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003049}
3050
Georg Brandl944f6842009-05-25 21:02:56 +00003051/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3052static stmt_ty
3053ast_for_with_stmt(struct compiling *c, const node *n)
3054{
3055 int i;
3056 stmt_ty ret;
3057 asdl_seq *inner;
3058
3059 REQ(n, with_stmt);
3060
3061 /* process the with items inside-out */
3062 i = NCH(n) - 1;
3063 /* the suite of the innermost with item is the suite of the with stmt */
3064 inner = ast_for_suite(c, CHILD(n, i));
3065 if (!inner)
3066 return NULL;
3067
3068 for (;;) {
3069 i -= 2;
3070 ret = ast_for_with_item(c, CHILD(n, i), inner);
3071 if (!ret)
3072 return NULL;
3073 /* was this the last item? */
3074 if (i == 1)
3075 break;
3076 /* if not, wrap the result so far in a new sequence */
3077 inner = asdl_seq_new(1, c->c_arena);
3078 if (!inner)
3079 return NULL;
3080 asdl_seq_SET(inner, 0, ret);
3081 }
3082
3083 return ret;
3084}
3085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003087ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088{
3089 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003090 PyObject *classname;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091 asdl_seq *bases, *s;
3092
3093 REQ(n, classdef);
3094
Benjamin Petersond5efd202008-06-08 22:52:37 +00003095 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003096 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097
3098 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003099 s = ast_for_suite(c, CHILD(n, 3));
3100 if (!s)
3101 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003102 classname = NEW_IDENTIFIER(CHILD(n, 1));
3103 if (!classname)
3104 return NULL;
3105 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3106 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 }
3108 /* check for empty base list */
3109 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003110 s = ast_for_suite(c, CHILD(n,5));
3111 if (!s)
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003112 return NULL;
3113 classname = NEW_IDENTIFIER(CHILD(n, 1));
3114 if (!classname)
3115 return NULL;
3116 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3117 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 }
3119
3120 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003121 bases = ast_for_class_bases(c, CHILD(n, 3));
3122 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003123 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124
3125 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003126 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003127 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003128 classname = NEW_IDENTIFIER(CHILD(n, 1));
3129 if (!classname)
3130 return NULL;
3131 return ClassDef(classname, bases, s, decorator_seq,
Christian Heimes5224d282008-02-23 15:01:05 +00003132 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133}
3134
3135static stmt_ty
3136ast_for_stmt(struct compiling *c, const node *n)
3137{
3138 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003139 assert(NCH(n) == 1);
3140 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 }
3142 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003143 assert(num_stmts(n) == 1);
3144 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 }
3146 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003147 n = CHILD(n, 0);
3148 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3149 | flow_stmt | import_stmt | global_stmt | exec_stmt
3150 | assert_stmt
3151 */
3152 switch (TYPE(n)) {
3153 case expr_stmt:
3154 return ast_for_expr_stmt(c, n);
3155 case print_stmt:
3156 return ast_for_print_stmt(c, n);
3157 case del_stmt:
3158 return ast_for_del_stmt(c, n);
3159 case pass_stmt:
3160 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3161 case flow_stmt:
3162 return ast_for_flow_stmt(c, n);
3163 case import_stmt:
3164 return ast_for_import_stmt(c, n);
3165 case global_stmt:
3166 return ast_for_global_stmt(c, n);
3167 case exec_stmt:
3168 return ast_for_exec_stmt(c, n);
3169 case assert_stmt:
3170 return ast_for_assert_stmt(c, n);
3171 default:
3172 PyErr_Format(PyExc_SystemError,
3173 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3174 TYPE(n), NCH(n));
3175 return NULL;
3176 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177 }
3178 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003179 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003180 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003181 */
3182 node *ch = CHILD(n, 0);
3183 REQ(n, compound_stmt);
3184 switch (TYPE(ch)) {
3185 case if_stmt:
3186 return ast_for_if_stmt(c, ch);
3187 case while_stmt:
3188 return ast_for_while_stmt(c, ch);
3189 case for_stmt:
3190 return ast_for_for_stmt(c, ch);
3191 case try_stmt:
3192 return ast_for_try_stmt(c, ch);
3193 case with_stmt:
3194 return ast_for_with_stmt(c, ch);
3195 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003196 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003197 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003198 return ast_for_classdef(c, ch, NULL);
3199 case decorated:
3200 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003201 default:
3202 PyErr_Format(PyExc_SystemError,
3203 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3204 TYPE(n), NCH(n));
3205 return NULL;
3206 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 }
3208}
3209
3210static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003211parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003213 const char *end;
3214 long x;
3215 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003217 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003218 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219#endif
3220
Mark Dickinson422ce062008-12-05 17:59:46 +00003221 assert(s != NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003222 errno = 0;
3223 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003225 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003227 if (*end == 'l' || *end == 'L')
3228 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003229 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003230 if (*end == '\0') {
3231 if (errno != 0)
3232 return PyLong_FromString((char *)s, (char **)0, 0);
3233 return PyInt_FromLong(x);
3234 }
3235 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003237 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003238 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003239 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003240 complex.imag = PyOS_ascii_atof(s);
3241 PyFPE_END_PROTECT(complex)
3242 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003243 }
3244 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003246 {
3247 PyFPE_START_PROTECT("atof", return 0)
3248 dx = PyOS_ascii_atof(s);
3249 PyFPE_END_PROTECT(dx)
3250 return PyFloat_FromDouble(dx);
3251 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252}
3253
3254static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003255decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256{
3257#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003258 Py_FatalError("decode_utf8 should not be called in this build.");
3259 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003261 PyObject *u, *v;
3262 char *s, *t;
3263 t = s = (char *)*sPtr;
3264 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3265 while (s < end && (*s & 0x80)) s++;
3266 *sPtr = s;
3267 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3268 if (u == NULL)
3269 return NULL;
3270 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3271 Py_DECREF(u);
3272 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273#endif
3274}
3275
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003276#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003278decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003280 PyObject *v, *u;
3281 char *buf;
3282 char *p;
3283 const char *end;
3284 if (encoding == NULL) {
3285 buf = (char *)s;
3286 u = NULL;
3287 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3288 buf = (char *)s;
3289 u = NULL;
3290 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003291 /* check for integer overflow */
3292 if (len > PY_SIZE_MAX / 4)
3293 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003294 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003295 u = PyString_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003296 if (u == NULL)
3297 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003298 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003299 end = s + len;
3300 while (s < end) {
3301 if (*s == '\\') {
3302 *p++ = *s++;
3303 if (*s & 0x80) {
3304 strcpy(p, "u005c");
3305 p += 5;
3306 }
3307 }
3308 if (*s & 0x80) { /* XXX inefficient */
3309 PyObject *w;
3310 char *r;
3311 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003312 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003313 if (w == NULL) {
3314 Py_DECREF(u);
3315 return NULL;
3316 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003317 r = PyString_AsString(w);
3318 rn = PyString_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003319 assert(rn % 2 == 0);
3320 for (i = 0; i < rn; i += 2) {
3321 sprintf(p, "\\u%02x%02x",
3322 r[i + 0] & 0xFF,
3323 r[i + 1] & 0xFF);
3324 p += 6;
3325 }
3326 Py_DECREF(w);
3327 } else {
3328 *p++ = *s++;
3329 }
3330 }
3331 len = p - buf;
3332 s = buf;
3333 }
3334 if (rawmode)
3335 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3336 else
3337 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3338 Py_XDECREF(u);
3339 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003341#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003342
3343/* s is a Python string literal, including the bracketing quote characters,
3344 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3345 * parsestr parses it, and returns the decoded Python string object.
3346 */
3347static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003348parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003350 size_t len;
3351 int quote = Py_CHARMASK(*s);
3352 int rawmode = 0;
3353 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003354 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003356 if (isalpha(quote) || quote == '_') {
3357 if (quote == 'u' || quote == 'U') {
3358 quote = *++s;
3359 unicode = 1;
3360 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003361 if (quote == 'b' || quote == 'B') {
3362 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003363 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003364 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003365 if (quote == 'r' || quote == 'R') {
3366 quote = *++s;
3367 rawmode = 1;
3368 }
3369 }
3370 if (quote != '\'' && quote != '\"') {
3371 PyErr_BadInternalCall();
3372 return NULL;
3373 }
3374 s++;
3375 len = strlen(s);
3376 if (len > INT_MAX) {
3377 PyErr_SetString(PyExc_OverflowError,
3378 "string to parse is too long");
3379 return NULL;
3380 }
3381 if (s[--len] != quote) {
3382 PyErr_BadInternalCall();
3383 return NULL;
3384 }
3385 if (len >= 4 && s[0] == quote && s[1] == quote) {
3386 s += 2;
3387 len -= 2;
3388 if (s[--len] != quote || s[--len] != quote) {
3389 PyErr_BadInternalCall();
3390 return NULL;
3391 }
3392 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003394 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003395 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003396 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003398 need_encoding = (c->c_encoding != NULL &&
3399 strcmp(c->c_encoding, "utf-8") != 0 &&
3400 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003401 if (rawmode || strchr(s, '\\') == NULL) {
3402 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003404 /* This should not happen - we never see any other
3405 encoding. */
3406 Py_FatalError(
3407 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003409 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3410 if (u == NULL)
3411 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003412 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003413 Py_DECREF(u);
3414 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003416 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003417 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003418 }
3419 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003421 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003422 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423}
3424
3425/* Build a Python string object out of a STRING atom. This takes care of
3426 * compile-time literal catenation, calling parsestr() on each piece, and
3427 * pasting the intermediate results together.
3428 */
3429static PyObject *
3430parsestrplus(struct compiling *c, const node *n)
3431{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003432 PyObject *v;
3433 int i;
3434 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003435 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003436 /* String literal concatenation */
3437 for (i = 1; i < NCH(n); i++) {
3438 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003439 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003440 if (s == NULL)
3441 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003442 if (PyString_Check(v) && PyString_Check(s)) {
3443 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003444 if (v == NULL)
3445 goto onError;
3446 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003448 else {
3449 PyObject *temp = PyUnicode_Concat(v, s);
3450 Py_DECREF(s);
3451 Py_DECREF(v);
3452 v = temp;
3453 if (v == NULL)
3454 goto onError;
3455 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003457 }
3458 }
3459 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460
3461 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003462 Py_XDECREF(v);
3463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464}