blob: 2cc41b274f16e015147f28587585e79dc98f2a53 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* Data structure used internally */
19struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000020 char *c_encoding; /* source encoding */
Christian Heimes3c608332008-03-26 22:01:37 +000021 int c_future_unicode; /* __future__ unicode literals flag */
Neal Norwitzadb69fc2005-12-17 20:54:49 +000022 PyArena *c_arena; /* arena for allocating memeory */
Christian Heimesffcd1e12007-11-24 01:36:02 +000023 const char *c_filename; /* filename */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024};
25
26static asdl_seq *seq_for_testlist(struct compiling *, const node *);
27static expr_ty ast_for_expr(struct compiling *, const node *);
28static stmt_ty ast_for_stmt(struct compiling *, const node *);
29static asdl_seq *ast_for_suite(struct compiling *, const node *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000030static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
31 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000032static expr_ty ast_for_testlist(struct compiling *, const node *);
Christian Heimes5224d282008-02-23 15:01:05 +000033static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000034static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035
36/* Note different signature for ast_for_call */
37static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
38
Benjamin Peterson2b30ea02008-05-03 15:56:42 +000039static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes3c608332008-03-26 22:01:37 +000040static PyObject *parsestr(struct compiling *, const char *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041static PyObject *parsestrplus(struct compiling *, const node *n);
42
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000044#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#endif
46
Neal Norwitzadb69fc2005-12-17 20:54:49 +000047static identifier
48new_identifier(const char* n, PyArena *arena) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000049 PyObject* id = PyString_InternFromString(n);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +000050 if (id != NULL)
51 PyArena_AddPyObject(arena, id);
Neal Norwitzadb69fc2005-12-17 20:54:49 +000052 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053}
54
Neal Norwitzadb69fc2005-12-17 20:54:49 +000055#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056
57/* This routine provides an invalid object for the syntax error.
58 The outermost routine must unpack this error and create the
59 proper object. We do this so that we don't have to pass
60 the filename to everything function.
61
62 XXX Maybe we should just pass the filename...
63*/
64
65static int
66ast_error(const node *n, const char *errstr)
67{
68 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
69 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000070 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000071 PyErr_SetObject(PyExc_SyntaxError, u);
72 Py_DECREF(u);
73 return 0;
74}
75
76static void
77ast_error_finish(const char *filename)
78{
79 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000080 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
82 assert(PyErr_Occurred());
83 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000084 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085
86 PyErr_Fetch(&type, &value, &tback);
87 errstr = PyTuple_GetItem(value, 0);
88 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000089 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000090 Py_INCREF(errstr);
91 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000092 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000093 Py_DECREF(errstr);
94 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000095 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096 Py_DECREF(value);
97
98 loc = PyErr_ProgramText(filename, lineno);
99 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000100 Py_INCREF(Py_None);
101 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000103 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000105 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000106 Py_DECREF(errstr);
107 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000108 }
Georg Brandl7784f122006-05-26 20:04:44 +0000109 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 Py_DECREF(errstr);
111 Py_DECREF(tmp);
112 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000113 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114 PyErr_Restore(type, value, tback);
115}
116
Benjamin Petersoncbd78132008-06-08 15:45:23 +0000117static int
118ast_warn(struct compiling *c, const node *n, char *msg)
119{
120 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),
121 NULL, NULL) < 0) {
122 /* if -Werr, change it to a SyntaxError */
123 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))
124 ast_error(n, msg);
125 return 0;
126 }
127 return 1;
128}
129
Benjamin Petersond5efd202008-06-08 22:52:37 +0000130static int
131forbidden_check(struct compiling *c, const node *n, const char *x)
132{
133 if (!strcmp(x, "None"))
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000134 return ast_error(n, "cannot assign to None");
135 if (!strcmp(x, "__debug__"))
136 return ast_error(n, "cannot assign to __debug__");
Benjamin Peterson399b1fe2008-10-25 02:53:28 +0000137 if (Py_Py3kWarningFlag) {
138 if (!(strcmp(x, "True") && strcmp(x, "False")) &&
139 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
140 return 0;
141 if (!strcmp(x, "nonlocal") &&
142 !ast_warn(c, n, "nonlocal is a keyword in 3.x"))
143 return 0;
144 }
Benjamin Petersond5efd202008-06-08 22:52:37 +0000145 return 1;
146}
147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148/* num_stmts() returns number of contained statements.
149
150 Use this routine to determine how big a sequence is needed for
151 the statements in a parse tree. Its raison d'etre is this bit of
152 grammar:
153
154 stmt: simple_stmt | compound_stmt
155 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
156
157 A simple_stmt can contain multiple small_stmt elements joined
158 by semicolons. If the arg is a simple_stmt, the number of
159 small_stmt elements is returned.
160*/
161
162static int
163num_stmts(const node *n)
164{
165 int i, l;
166 node *ch;
167
168 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000169 case single_input:
170 if (TYPE(CHILD(n, 0)) == NEWLINE)
171 return 0;
172 else
173 return num_stmts(CHILD(n, 0));
174 case file_input:
175 l = 0;
176 for (i = 0; i < NCH(n); i++) {
177 ch = CHILD(n, i);
178 if (TYPE(ch) == stmt)
179 l += num_stmts(ch);
180 }
181 return l;
182 case stmt:
183 return num_stmts(CHILD(n, 0));
184 case compound_stmt:
185 return 1;
186 case simple_stmt:
187 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
188 case suite:
189 if (NCH(n) == 1)
190 return num_stmts(CHILD(n, 0));
191 else {
192 l = 0;
193 for (i = 2; i < (NCH(n) - 1); i++)
194 l += num_stmts(CHILD(n, i));
195 return l;
196 }
197 default: {
198 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +0000200 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000201 TYPE(n), NCH(n));
202 Py_FatalError(buf);
203 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204 }
205 assert(0);
206 return 0;
207}
208
209/* Transform the CST rooted at node * to the appropriate AST
210*/
211
212mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000213PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000214 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000216 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 asdl_seq *stmts = NULL;
218 stmt_ty s;
219 node *ch;
220 struct compiling c;
221
222 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000223 c.c_encoding = "utf-8";
224 if (TYPE(n) == encoding_decl) {
225 ast_error(n, "encoding declaration in Unicode string");
226 goto error;
227 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000229 c.c_encoding = STR(n);
230 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000232 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 }
Christian Heimes3c608332008-03-26 22:01:37 +0000234 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000235 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000236 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237
Jeremy Hyltona8293132006-02-28 17:58:27 +0000238 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000240 case file_input:
241 stmts = asdl_seq_new(num_stmts(n), arena);
242 if (!stmts)
243 return NULL;
244 for (i = 0; i < NCH(n) - 1; i++) {
245 ch = CHILD(n, i);
246 if (TYPE(ch) == NEWLINE)
247 continue;
248 REQ(ch, stmt);
249 num = num_stmts(ch);
250 if (num == 1) {
251 s = ast_for_stmt(&c, ch);
252 if (!s)
253 goto error;
254 asdl_seq_SET(stmts, k++, s);
255 }
256 else {
257 ch = CHILD(ch, 0);
258 REQ(ch, simple_stmt);
259 for (j = 0; j < num; j++) {
260 s = ast_for_stmt(&c, CHILD(ch, j * 2));
261 if (!s)
262 goto error;
263 asdl_seq_SET(stmts, k++, s);
264 }
265 }
266 }
267 return Module(stmts, arena);
268 case eval_input: {
269 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000271 /* XXX Why not gen_for here? */
272 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
273 if (!testlist_ast)
274 goto error;
275 return Expression(testlist_ast, arena);
276 }
277 case single_input:
278 if (TYPE(CHILD(n, 0)) == NEWLINE) {
279 stmts = asdl_seq_new(1, arena);
280 if (!stmts)
281 goto error;
282 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
283 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000284 if (!asdl_seq_GET(stmts, 0))
285 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000286 return Interactive(stmts, arena);
287 }
288 else {
289 n = CHILD(n, 0);
290 num = num_stmts(n);
291 stmts = asdl_seq_new(num, arena);
292 if (!stmts)
293 goto error;
294 if (num == 1) {
295 s = ast_for_stmt(&c, n);
296 if (!s)
297 goto error;
298 asdl_seq_SET(stmts, 0, s);
299 }
300 else {
301 /* Only a simple_stmt can contain multiple statements. */
302 REQ(n, simple_stmt);
303 for (i = 0; i < NCH(n); i += 2) {
304 if (TYPE(CHILD(n, i)) == NEWLINE)
305 break;
306 s = ast_for_stmt(&c, CHILD(n, i));
307 if (!s)
308 goto error;
309 asdl_seq_SET(stmts, i / 2, s);
310 }
311 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000313 return Interactive(stmts, arena);
314 }
315 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000316 PyErr_Format(PyExc_SystemError,
317 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000318 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319 }
320 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 ast_error_finish(filename);
322 return NULL;
323}
324
325/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
326*/
327
328static operator_ty
329get_operator(const node *n)
330{
331 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000332 case VBAR:
333 return BitOr;
334 case CIRCUMFLEX:
335 return BitXor;
336 case AMPER:
337 return BitAnd;
338 case LEFTSHIFT:
339 return LShift;
340 case RIGHTSHIFT:
341 return RShift;
342 case PLUS:
343 return Add;
344 case MINUS:
345 return Sub;
346 case STAR:
347 return Mult;
348 case SLASH:
349 return Div;
350 case DOUBLESLASH:
351 return FloorDiv;
352 case PERCENT:
353 return Mod;
354 default:
355 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356 }
357}
358
Jeremy Hyltona8293132006-02-28 17:58:27 +0000359/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360
361 Only sets context for expr kinds that "can appear in assignment context"
362 (according to ../Parser/Python.asdl). For other expr kinds, it sets
363 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364*/
365
366static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000367set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368{
369 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000370 /* If a particular expression type can't be used for assign / delete,
371 set expr_name to its name and an error message will be generated.
372 */
373 const char* expr_name = NULL;
374
375 /* The ast defines augmented store and load contexts, but the
376 implementation here doesn't actually use them. The code may be
377 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000378 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000380 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000381 */
382 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383
384 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000385 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000386 if (ctx == Store && !forbidden_check(c, n,
387 PyBytes_AS_STRING(e->v.Attribute.attr)))
388 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000389 e->v.Attribute.ctx = ctx;
390 break;
391 case Subscript_kind:
392 e->v.Subscript.ctx = ctx;
393 break;
394 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000395 if (ctx == Store && !forbidden_check(c, n,
396 PyBytes_AS_STRING(e->v.Name.id)))
397 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000398 e->v.Name.ctx = ctx;
399 break;
400 case List_kind:
401 e->v.List.ctx = ctx;
402 s = e->v.List.elts;
403 break;
404 case Tuple_kind:
405 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
406 return ast_error(n, "can't assign to ()");
407 e->v.Tuple.ctx = ctx;
408 s = e->v.Tuple.elts;
409 break;
410 case Lambda_kind:
411 expr_name = "lambda";
412 break;
413 case Call_kind:
414 expr_name = "function call";
415 break;
416 case BoolOp_kind:
417 case BinOp_kind:
418 case UnaryOp_kind:
419 expr_name = "operator";
420 break;
421 case GeneratorExp_kind:
422 expr_name = "generator expression";
423 break;
424 case Yield_kind:
425 expr_name = "yield expression";
426 break;
427 case ListComp_kind:
428 expr_name = "list comprehension";
429 break;
430 case Dict_kind:
431 case Num_kind:
432 case Str_kind:
433 expr_name = "literal";
434 break;
435 case Compare_kind:
436 expr_name = "comparison";
437 break;
438 case Repr_kind:
439 expr_name = "repr";
440 break;
441 case IfExp_kind:
442 expr_name = "conditional expression";
443 break;
444 default:
445 PyErr_Format(PyExc_SystemError,
446 "unexpected expression in assignment %d (line %d)",
447 e->kind, e->lineno);
448 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000450 /* Check for error string set by switch */
451 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000452 char buf[300];
453 PyOS_snprintf(buf, sizeof(buf),
454 "can't %s %s",
455 ctx == Store ? "assign to" : "delete",
456 expr_name);
457 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000458 }
459
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000461 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462 */
463 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000464 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000466 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000467 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000468 return 0;
469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470 }
471 return 1;
472}
473
474static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000475ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476{
477 REQ(n, augassign);
478 n = CHILD(n, 0);
479 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000480 case '+':
481 return Add;
482 case '-':
483 return Sub;
484 case '/':
485 if (STR(n)[1] == '/')
486 return FloorDiv;
487 else
488 return Div;
489 case '%':
490 return Mod;
491 case '<':
492 return LShift;
493 case '>':
494 return RShift;
495 case '&':
496 return BitAnd;
497 case '^':
498 return BitXor;
499 case '|':
500 return BitOr;
501 case '*':
502 if (STR(n)[1] == '*')
503 return Pow;
504 else
505 return Mult;
506 default:
507 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
508 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 }
510}
511
512static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000513ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514{
515 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000516 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 */
518 REQ(n, comp_op);
519 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000520 n = CHILD(n, 0);
521 switch (TYPE(n)) {
522 case LESS:
523 return Lt;
524 case GREATER:
525 return Gt;
526 case EQEQUAL: /* == */
527 return Eq;
528 case LESSEQUAL:
529 return LtE;
530 case GREATEREQUAL:
531 return GtE;
532 case NOTEQUAL:
533 return NotEq;
534 case NAME:
535 if (strcmp(STR(n), "in") == 0)
536 return In;
537 if (strcmp(STR(n), "is") == 0)
538 return Is;
539 default:
540 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
541 STR(n));
542 return (cmpop_ty)0;
543 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544 }
545 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000546 /* handle "not in" and "is not" */
547 switch (TYPE(CHILD(n, 0))) {
548 case NAME:
549 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
550 return NotIn;
551 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
552 return IsNot;
553 default:
554 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
555 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
556 return (cmpop_ty)0;
557 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558 }
Neal Norwitz79792652005-11-14 04:25:03 +0000559 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000560 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000561 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562}
563
564static asdl_seq *
565seq_for_testlist(struct compiling *c, const node *n)
566{
567 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000568 asdl_seq *seq;
569 expr_ty expression;
570 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000571 assert(TYPE(n) == testlist ||
572 TYPE(n) == listmaker ||
573 TYPE(n) == testlist_gexp ||
574 TYPE(n) == testlist_safe ||
575 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000577 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000579 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
581 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000582 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000584 expression = ast_for_expr(c, CHILD(n, i));
585 if (!expression)
586 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000588 assert(i / 2 < seq->size);
589 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 }
591 return seq;
592}
593
594static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000595compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596{
597 int i, len = (NCH(n) + 1) / 2;
598 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000599 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000601 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Neal Norwitz3a230172006-09-22 08:18:10 +0000603 /* fpdef: NAME | '(' fplist ')'
604 fplist: fpdef (',' fpdef)* [',']
605 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607 for (i = 0; i < len; i++) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000608 PyObject *arg_id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000609 const node *fpdef_node = CHILD(n, 2*i);
610 const node *child;
611 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000612set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000613 /* fpdef_node is either a NAME or an fplist */
614 child = CHILD(fpdef_node, 0);
615 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000616 if (!forbidden_check(c, n, STR(child)))
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000617 return NULL;
618 arg_id = NEW_IDENTIFIER(child);
619 if (!arg_id)
620 return NULL;
621 arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
622 c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000623 }
624 else {
625 assert(TYPE(fpdef_node) == fpdef);
626 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
627 child = CHILD(fpdef_node, 1);
628 assert(TYPE(child) == fplist);
629 /* NCH == 1 means we have (x), we need to elide the extra parens */
630 if (NCH(child) == 1) {
631 fpdef_node = CHILD(child, 0);
632 assert(TYPE(fpdef_node) == fpdef);
633 goto set_name;
634 }
635 arg = compiler_complex_args(c, child);
636 }
637 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 }
639
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000640 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000641 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000642 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 return result;
644}
645
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646
Jeremy Hyltona8293132006-02-28 17:58:27 +0000647/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648
649static arguments_ty
650ast_for_arguments(struct compiling *c, const node *n)
651{
652 /* parameters: '(' [varargslist] ')'
653 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000654 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000656 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657 asdl_seq *args, *defaults;
658 identifier vararg = NULL, kwarg = NULL;
659 node *ch;
660
661 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000662 if (NCH(n) == 2) /* () as argument list */
663 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
664 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 }
666 REQ(n, varargslist);
667
668 /* first count the number of normal args & defaults */
669 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000670 ch = CHILD(n, i);
671 if (TYPE(ch) == fpdef)
672 n_args++;
673 if (TYPE(ch) == EQUAL)
674 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000676 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000678 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000679 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000681 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
683 /* fpdef: NAME | '(' fplist ')'
684 fplist: fpdef (',' fpdef)* [',']
685 */
686 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000687 j = 0; /* index for defaults */
688 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000690 ch = CHILD(n, i);
691 switch (TYPE(ch)) {
692 case fpdef:
693 handle_fpdef:
694 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
695 anything other than EQUAL or a comma? */
696 /* XXX Should NCH(n) check be made a separate check? */
697 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
698 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
699 if (!expression)
700 goto error;
701 assert(defaults != NULL);
702 asdl_seq_SET(defaults, j++, expression);
703 i += 2;
704 found_default = 1;
705 }
706 else if (found_default) {
707 ast_error(n,
708 "non-default argument follows default argument");
709 goto error;
710 }
711 if (NCH(ch) == 3) {
712 ch = CHILD(ch, 1);
713 /* def foo((x)): is not complex, special case. */
714 if (NCH(ch) != 1) {
715 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000716 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
717 "tuple parameter unpacking has been removed in 3.x"))
718 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000719 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000720 if (!asdl_seq_GET(args, k-1))
721 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000722 } else {
723 /* def foo((x)): setup for checking NAME below. */
724 /* Loop because there can be many parens and tuple
725 unpacking mixed in. */
726 ch = CHILD(ch, 0);
727 assert(TYPE(ch) == fpdef);
728 goto handle_fpdef;
729 }
730 }
731 if (TYPE(CHILD(ch, 0)) == NAME) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000732 PyObject *id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000733 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000734 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000735 goto error;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000736 id = NEW_IDENTIFIER(CHILD(ch, 0));
737 if (!id)
738 goto error;
739 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000740 c->c_arena);
741 if (!name)
742 goto error;
743 asdl_seq_SET(args, k++, name);
744
745 }
746 i += 2; /* the name and the comma */
747 break;
748 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000749 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000750 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000751 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000752 if (!vararg)
753 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000754 i += 3;
755 break;
756 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000757 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000758 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000759 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000760 if (!kwarg)
761 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000762 i += 3;
763 break;
764 default:
765 PyErr_Format(PyExc_SystemError,
766 "unexpected node in varargslist: %d @ %d",
767 TYPE(ch), i);
768 goto error;
769 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 }
771
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000772 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773
774 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000775 Py_XDECREF(vararg);
776 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 return NULL;
778}
779
780static expr_ty
781ast_for_dotted_name(struct compiling *c, const node *n)
782{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000783 expr_ty e;
784 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000785 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 int i;
787
788 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000789
790 lineno = LINENO(n);
791 col_offset = n->n_col_offset;
792
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 id = NEW_IDENTIFIER(CHILD(n, 0));
794 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000795 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000796 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799
800 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000801 id = NEW_IDENTIFIER(CHILD(n, i));
802 if (!id)
803 return NULL;
804 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
805 if (!e)
806 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808
809 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810}
811
812static expr_ty
813ast_for_decorator(struct compiling *c, const node *n)
814{
815 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
816 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000817 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818
819 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000820 REQ(CHILD(n, 0), AT);
821 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822
823 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
824 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000825 return NULL;
826
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000828 d = name_expr;
829 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 }
831 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000832 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
833 n->n_col_offset, c->c_arena);
834 if (!d)
835 return NULL;
836 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 }
838 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000839 d = ast_for_call(c, CHILD(n, 3), name_expr);
840 if (!d)
841 return NULL;
842 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 }
844
845 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846}
847
848static asdl_seq*
849ast_for_decorators(struct compiling *c, const node *n)
850{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000851 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000852 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 int i;
854
855 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000856 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000858 return NULL;
859
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000861 d = ast_for_decorator(c, CHILD(n, i));
862 if (!d)
863 return NULL;
864 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 }
866 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867}
868
869static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000870ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871{
Christian Heimes5224d282008-02-23 15:01:05 +0000872 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000873 identifier name;
874 arguments_ty args;
875 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000876 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877
878 REQ(n, funcdef);
879
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 name = NEW_IDENTIFIER(CHILD(n, name_i));
881 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000882 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000883 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000884 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885 args = ast_for_arguments(c, CHILD(n, name_i + 1));
886 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000887 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 body = ast_for_suite(c, CHILD(n, name_i + 3));
889 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000892 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000893 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894}
895
Christian Heimes5224d282008-02-23 15:01:05 +0000896static stmt_ty
897ast_for_decorated(struct compiling *c, const node *n)
898{
899 /* decorated: decorators (classdef | funcdef) */
900 stmt_ty thing = NULL;
901 asdl_seq *decorator_seq = NULL;
902
903 REQ(n, decorated);
904
905 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
906 if (!decorator_seq)
907 return NULL;
908
909 assert(TYPE(CHILD(n, 1)) == funcdef ||
910 TYPE(CHILD(n, 1)) == classdef);
911
912 if (TYPE(CHILD(n, 1)) == funcdef) {
913 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
914 } else if (TYPE(CHILD(n, 1)) == classdef) {
915 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
916 }
917 /* we count the decorators in when talking about the class' or
918 function's line number */
919 if (thing) {
920 thing->lineno = LINENO(n);
921 thing->col_offset = n->n_col_offset;
922 }
923 return thing;
924}
925
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926static expr_ty
927ast_for_lambdef(struct compiling *c, const node *n)
928{
929 /* lambdef: 'lambda' [varargslist] ':' test */
930 arguments_ty args;
931 expr_ty expression;
932
933 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000934 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
935 if (!args)
936 return NULL;
937 expression = ast_for_expr(c, CHILD(n, 2));
938 if (!expression)
939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 }
941 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000942 args = ast_for_arguments(c, CHILD(n, 1));
943 if (!args)
944 return NULL;
945 expression = ast_for_expr(c, CHILD(n, 3));
946 if (!expression)
947 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 }
949
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000950 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951}
952
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000953static expr_ty
954ast_for_ifexpr(struct compiling *c, const node *n)
955{
956 /* test: or_test 'if' or_test 'else' test */
957 expr_ty expression, body, orelse;
958
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000959 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000960 body = ast_for_expr(c, CHILD(n, 0));
961 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000962 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000963 expression = ast_for_expr(c, CHILD(n, 2));
964 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000965 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000966 orelse = ast_for_expr(c, CHILD(n, 4));
967 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000968 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000969 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000970 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000971}
972
Neal Norwitze4d4f002006-09-05 03:58:26 +0000973/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
974 so there is only a single version. Possibly for loops can also re-use
975 the code.
976*/
977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978/* Count the number of 'for' loop in a list comprehension.
979
980 Helper for ast_for_listcomp().
981*/
982
983static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000984count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985{
986 int n_fors = 0;
987 node *ch = CHILD(n, 1);
988
989 count_list_for:
990 n_fors++;
991 REQ(ch, list_for);
992 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000993 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000995 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 count_list_iter:
997 REQ(ch, list_iter);
998 ch = CHILD(ch, 0);
999 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001000 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001002 if (NCH(ch) == 3) {
1003 ch = CHILD(ch, 2);
1004 goto count_list_iter;
1005 }
1006 else
1007 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001009
1010 /* Should never be reached */
1011 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1012 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013}
1014
1015/* Count the number of 'if' statements in a list comprehension.
1016
1017 Helper for ast_for_listcomp().
1018*/
1019
1020static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001021count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022{
1023 int n_ifs = 0;
1024
1025 count_list_iter:
1026 REQ(n, list_iter);
1027 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001028 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 n = CHILD(n, 0);
1030 REQ(n, list_if);
1031 n_ifs++;
1032 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001033 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 n = CHILD(n, 2);
1035 goto count_list_iter;
1036}
1037
1038static expr_ty
1039ast_for_listcomp(struct compiling *c, const node *n)
1040{
1041 /* listmaker: test ( list_for | (',' test)* [','] )
1042 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1043 list_iter: list_for | list_if
1044 list_if: 'if' test [list_iter]
1045 testlist_safe: test [(',' test)+ [',']]
1046 */
1047 expr_ty elt;
1048 asdl_seq *listcomps;
1049 int i, n_fors;
1050 node *ch;
1051
1052 REQ(n, listmaker);
1053 assert(NCH(n) > 1);
1054
1055 elt = ast_for_expr(c, CHILD(n, 0));
1056 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001057 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001059 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001063 listcomps = asdl_seq_new(n_fors, c->c_arena);
1064 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001065 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001066
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 ch = CHILD(n, 1);
1068 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001069 comprehension_ty lc;
1070 asdl_seq *t;
1071 expr_ty expression;
1072 node *for_ch;
1073
1074 REQ(ch, list_for);
1075
1076 for_ch = CHILD(ch, 1);
1077 t = ast_for_exprlist(c, for_ch, Store);
1078 if (!t)
1079 return NULL;
1080 expression = ast_for_testlist(c, CHILD(ch, 3));
1081 if (!expression)
1082 return NULL;
1083
1084 /* Check the # of children rather than the length of t, since
1085 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1086 */
1087 if (NCH(for_ch) == 1)
1088 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1089 c->c_arena);
1090 else
1091 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1092 c->c_arena),
1093 expression, NULL, c->c_arena);
1094 if (!lc)
1095 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001097 if (NCH(ch) == 5) {
1098 int j, n_ifs;
1099 asdl_seq *ifs;
1100 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001102 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001103 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001104 if (n_ifs == -1)
1105 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001107 ifs = asdl_seq_new(n_ifs, c->c_arena);
1108 if (!ifs)
1109 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001111 for (j = 0; j < n_ifs; j++) {
1112 REQ(ch, list_iter);
1113 ch = CHILD(ch, 0);
1114 REQ(ch, list_if);
1115
1116 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1117 if (!list_for_expr)
1118 return NULL;
1119
1120 asdl_seq_SET(ifs, j, list_for_expr);
1121 if (NCH(ch) == 3)
1122 ch = CHILD(ch, 2);
1123 }
1124 /* on exit, must guarantee that ch is a list_for */
1125 if (TYPE(ch) == list_iter)
1126 ch = CHILD(ch, 0);
1127 lc->ifs = ifs;
1128 }
1129 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 }
1131
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001132 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133}
1134
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001135/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136
1137 Helper for ast_for_genexp().
1138*/
1139
1140static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001141count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001143 int n_fors = 0;
1144 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145
1146 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001147 n_fors++;
1148 REQ(ch, gen_for);
1149 if (NCH(ch) == 5)
1150 ch = CHILD(ch, 4);
1151 else
1152 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001154 REQ(ch, gen_iter);
1155 ch = CHILD(ch, 0);
1156 if (TYPE(ch) == gen_for)
1157 goto count_gen_for;
1158 else if (TYPE(ch) == gen_if) {
1159 if (NCH(ch) == 3) {
1160 ch = CHILD(ch, 2);
1161 goto count_gen_iter;
1162 }
1163 else
1164 return n_fors;
1165 }
1166
1167 /* Should never be reached */
1168 PyErr_SetString(PyExc_SystemError,
1169 "logic error in count_gen_fors");
1170 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171}
1172
1173/* Count the number of 'if' statements in a generator expression.
1174
1175 Helper for ast_for_genexp().
1176*/
1177
1178static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001179count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001181 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001183 while (1) {
1184 REQ(n, gen_iter);
1185 if (TYPE(CHILD(n, 0)) == gen_for)
1186 return n_ifs;
1187 n = CHILD(n, 0);
1188 REQ(n, gen_if);
1189 n_ifs++;
1190 if (NCH(n) == 2)
1191 return n_ifs;
1192 n = CHILD(n, 2);
1193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194}
1195
Jeremy Hyltona8293132006-02-28 17:58:27 +00001196/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197static expr_ty
1198ast_for_genexp(struct compiling *c, const node *n)
1199{
1200 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001201 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 expr_ty elt;
1203 asdl_seq *genexps;
1204 int i, n_fors;
1205 node *ch;
1206
1207 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1208 assert(NCH(n) > 1);
1209
1210 elt = ast_for_expr(c, CHILD(n, 0));
1211 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001214 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001216 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001217
1218 genexps = asdl_seq_new(n_fors, c->c_arena);
1219 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001220 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001221
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 ch = CHILD(n, 1);
1223 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001224 comprehension_ty ge;
1225 asdl_seq *t;
1226 expr_ty expression;
1227 node *for_ch;
1228
1229 REQ(ch, gen_for);
1230
1231 for_ch = CHILD(ch, 1);
1232 t = ast_for_exprlist(c, for_ch, Store);
1233 if (!t)
1234 return NULL;
1235 expression = ast_for_expr(c, CHILD(ch, 3));
1236 if (!expression)
1237 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001238
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001239 /* Check the # of children rather than the length of t, since
1240 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1241 if (NCH(for_ch) == 1)
1242 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1243 NULL, c->c_arena);
1244 else
1245 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1246 c->c_arena),
1247 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001248
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001249 if (!ge)
1250 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001251
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001252 if (NCH(ch) == 5) {
1253 int j, n_ifs;
1254 asdl_seq *ifs;
1255
1256 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001257 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001258 if (n_ifs == -1)
1259 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001260
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001261 ifs = asdl_seq_new(n_ifs, c->c_arena);
1262 if (!ifs)
1263 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001264
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001265 for (j = 0; j < n_ifs; j++) {
1266 REQ(ch, gen_iter);
1267 ch = CHILD(ch, 0);
1268 REQ(ch, gen_if);
1269
1270 expression = ast_for_expr(c, CHILD(ch, 1));
1271 if (!expression)
1272 return NULL;
1273 asdl_seq_SET(ifs, j, expression);
1274 if (NCH(ch) == 3)
1275 ch = CHILD(ch, 2);
1276 }
1277 /* on exit, must guarantee that ch is a gen_for */
1278 if (TYPE(ch) == gen_iter)
1279 ch = CHILD(ch, 0);
1280 ge->ifs = ifs;
1281 }
1282 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 }
1284
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001285 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286}
1287
1288static expr_ty
1289ast_for_atom(struct compiling *c, const node *n)
1290{
1291 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1292 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1293 */
1294 node *ch = CHILD(n, 0);
1295
1296 switch (TYPE(ch)) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001297 case NAME: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001298 /* All names start in Load context, but may later be
1299 changed. */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001300 PyObject *name = NEW_IDENTIFIER(ch);
1301 if (!name)
1302 return NULL;
1303 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1304 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001306 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001307 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001308#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001309 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1310 PyObject *type, *value, *tback, *errstr;
1311 PyErr_Fetch(&type, &value, &tback);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001312 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001313 if (errstr) {
1314 char *s = "";
1315 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001316 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001317 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1318 ast_error(n, buf);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001319 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001320 } else {
1321 ast_error(n, "(unicode error) unknown error");
1322 }
1323 Py_DECREF(type);
1324 Py_DECREF(value);
1325 Py_XDECREF(tback);
1326 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001327#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001328 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001329 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001330 PyArena_AddPyObject(c->c_arena, str);
1331 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 }
1333 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001334 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001335 if (!pynum)
1336 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001337
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001338 PyArena_AddPyObject(c->c_arena, pynum);
1339 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 }
1341 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001342 ch = CHILD(n, 1);
1343
1344 if (TYPE(ch) == RPAR)
1345 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1346
1347 if (TYPE(ch) == yield_expr)
1348 return ast_for_expr(c, ch);
1349
1350 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1351 return ast_for_genexp(c, ch);
1352
1353 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) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001474 if (NCH(ch) == 1) {
1475 /* No expression, so step is None */
1476 ch = CHILD(ch, 0);
1477 step = Name(new_identifier("None", c->c_arena), Load,
1478 LINENO(ch), ch->n_col_offset, c->c_arena);
1479 if (!step)
1480 return NULL;
1481 } else {
1482 ch = CHILD(ch, 1);
1483 if (TYPE(ch) == test) {
1484 step = ast_for_expr(c, ch);
1485 if (!step)
1486 return NULL;
1487 }
1488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 }
1490
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001491 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492}
1493
1494static expr_ty
1495ast_for_binop(struct compiling *c, const node *n)
1496{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001497 /* Must account for a sequence of expressions.
1498 How should A op B op C by represented?
1499 BinOp(BinOp(A, op, B), op, C).
1500 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001502 int i, nops;
1503 expr_ty expr1, expr2, result;
1504 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001506 expr1 = ast_for_expr(c, CHILD(n, 0));
1507 if (!expr1)
1508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001510 expr2 = ast_for_expr(c, CHILD(n, 2));
1511 if (!expr2)
1512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001514 newoperator = get_operator(CHILD(n, 1));
1515 if (!newoperator)
1516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001518 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1519 c->c_arena);
1520 if (!result)
1521 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001523 nops = (NCH(n) - 1) / 2;
1524 for (i = 1; i < nops; i++) {
1525 expr_ty tmp_result, tmp;
1526 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001528 newoperator = get_operator(next_oper);
1529 if (!newoperator)
1530 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001532 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1533 if (!tmp)
1534 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001536 tmp_result = BinOp(result, newoperator, tmp,
1537 LINENO(next_oper), next_oper->n_col_offset,
1538 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001539 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001540 return NULL;
1541 result = tmp_result;
1542 }
1543 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544}
1545
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001546static expr_ty
1547ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1548{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001549 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1550 subscriptlist: subscript (',' subscript)* [',']
1551 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1552 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001553 REQ(n, trailer);
1554 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001555 if (NCH(n) == 2)
1556 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1557 n->n_col_offset, c->c_arena);
1558 else
1559 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001560 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001561 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001562 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1563 if (!attr_id)
1564 return NULL;
1565 return Attribute(left_expr, attr_id, Load,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001566 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001567 }
1568 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001569 REQ(CHILD(n, 0), LSQB);
1570 REQ(CHILD(n, 2), RSQB);
1571 n = CHILD(n, 1);
1572 if (NCH(n) == 1) {
1573 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1574 if (!slc)
1575 return NULL;
1576 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1577 c->c_arena);
1578 }
1579 else {
1580 /* The grammar is ambiguous here. The ambiguity is resolved
1581 by treating the sequence as a tuple literal if there are
1582 no slice features.
1583 */
1584 int j;
1585 slice_ty slc;
1586 expr_ty e;
1587 bool simple = true;
1588 asdl_seq *slices, *elts;
1589 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1590 if (!slices)
1591 return NULL;
1592 for (j = 0; j < NCH(n); j += 2) {
1593 slc = ast_for_slice(c, CHILD(n, j));
1594 if (!slc)
1595 return NULL;
1596 if (slc->kind != Index_kind)
1597 simple = false;
1598 asdl_seq_SET(slices, j / 2, slc);
1599 }
1600 if (!simple) {
1601 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1602 Load, LINENO(n), n->n_col_offset, c->c_arena);
1603 }
1604 /* extract Index values and put them in a Tuple */
1605 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1606 if (!elts)
1607 return NULL;
1608 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1609 slc = (slice_ty)asdl_seq_GET(slices, j);
1610 assert(slc->kind == Index_kind && slc->v.Index.value);
1611 asdl_seq_SET(elts, j, slc->v.Index.value);
1612 }
1613 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1614 if (!e)
1615 return NULL;
1616 return Subscript(left_expr, Index(e, c->c_arena),
1617 Load, LINENO(n), n->n_col_offset, c->c_arena);
1618 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001619 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001620}
1621
1622static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001623ast_for_factor(struct compiling *c, const node *n)
1624{
1625 node *pfactor, *ppower, *patom, *pnum;
1626 expr_ty expression;
1627
1628 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001629 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001630 constant. The peephole optimizer already does something like
1631 this but it doesn't handle the case where the constant is
1632 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1633 PyLongObject.
1634 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001635 if (TYPE(CHILD(n, 0)) == MINUS &&
1636 NCH(n) == 2 &&
1637 TYPE((pfactor = CHILD(n, 1))) == factor &&
1638 NCH(pfactor) == 1 &&
1639 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1640 NCH(ppower) == 1 &&
1641 TYPE((patom = CHILD(ppower, 0))) == atom &&
1642 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1643 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1644 if (s == NULL)
1645 return NULL;
1646 s[0] = '-';
1647 strcpy(s + 1, STR(pnum));
1648 PyObject_FREE(STR(pnum));
1649 STR(pnum) = s;
1650 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001651 }
1652
1653 expression = ast_for_expr(c, CHILD(n, 1));
1654 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001655 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001656
1657 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001658 case PLUS:
1659 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1660 c->c_arena);
1661 case MINUS:
1662 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1663 c->c_arena);
1664 case TILDE:
1665 return UnaryOp(Invert, expression, LINENO(n),
1666 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001667 }
1668 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001669 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001670 return NULL;
1671}
1672
1673static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001674ast_for_power(struct compiling *c, const node *n)
1675{
1676 /* power: atom trailer* ('**' factor)*
1677 */
1678 int i;
1679 expr_ty e, tmp;
1680 REQ(n, power);
1681 e = ast_for_atom(c, CHILD(n, 0));
1682 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001683 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001684 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001685 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001686 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001687 node *ch = CHILD(n, i);
1688 if (TYPE(ch) != trailer)
1689 break;
1690 tmp = ast_for_trailer(c, ch, e);
1691 if (!tmp)
1692 return NULL;
1693 tmp->lineno = e->lineno;
1694 tmp->col_offset = e->col_offset;
1695 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001696 }
1697 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001698 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1699 if (!f)
1700 return NULL;
1701 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1702 if (!tmp)
1703 return NULL;
1704 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001705 }
1706 return e;
1707}
1708
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709/* Do not name a variable 'expr'! Will cause a compile error.
1710*/
1711
1712static expr_ty
1713ast_for_expr(struct compiling *c, const node *n)
1714{
1715 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001716 test: or_test ['if' or_test 'else' test] | lambdef
1717 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 and_test: not_test ('and' not_test)*
1719 not_test: 'not' not_test | comparison
1720 comparison: expr (comp_op expr)*
1721 expr: xor_expr ('|' xor_expr)*
1722 xor_expr: and_expr ('^' and_expr)*
1723 and_expr: shift_expr ('&' shift_expr)*
1724 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1725 arith_expr: term (('+'|'-') term)*
1726 term: factor (('*'|'/'|'%'|'//') factor)*
1727 factor: ('+'|'-'|'~') factor | power
1728 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001729
1730 As well as modified versions that exist for backward compatibility,
1731 to explicitly allow:
1732 [ x for x in lambda: 0, lambda: 1 ]
1733 (which would be ambiguous without these extra rules)
1734
1735 old_test: or_test | old_lambdef
1736 old_lambdef: 'lambda' [vararglist] ':' old_test
1737
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 */
1739
1740 asdl_seq *seq;
1741 int i;
1742
1743 loop:
1744 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001745 case test:
1746 case old_test:
1747 if (TYPE(CHILD(n, 0)) == lambdef ||
1748 TYPE(CHILD(n, 0)) == old_lambdef)
1749 return ast_for_lambdef(c, CHILD(n, 0));
1750 else if (NCH(n) > 1)
1751 return ast_for_ifexpr(c, n);
1752 /* Fallthrough */
1753 case or_test:
1754 case and_test:
1755 if (NCH(n) == 1) {
1756 n = CHILD(n, 0);
1757 goto loop;
1758 }
1759 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1760 if (!seq)
1761 return NULL;
1762 for (i = 0; i < NCH(n); i += 2) {
1763 expr_ty e = ast_for_expr(c, CHILD(n, i));
1764 if (!e)
1765 return NULL;
1766 asdl_seq_SET(seq, i / 2, e);
1767 }
1768 if (!strcmp(STR(CHILD(n, 1)), "and"))
1769 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1770 c->c_arena);
1771 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1772 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1773 case not_test:
1774 if (NCH(n) == 1) {
1775 n = CHILD(n, 0);
1776 goto loop;
1777 }
1778 else {
1779 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1780 if (!expression)
1781 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001783 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1784 c->c_arena);
1785 }
1786 case comparison:
1787 if (NCH(n) == 1) {
1788 n = CHILD(n, 0);
1789 goto loop;
1790 }
1791 else {
1792 expr_ty expression;
1793 asdl_int_seq *ops;
1794 asdl_seq *cmps;
1795 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1796 if (!ops)
1797 return NULL;
1798 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1799 if (!cmps) {
1800 return NULL;
1801 }
1802 for (i = 1; i < NCH(n); i += 2) {
1803 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001805 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001806 if (!newoperator) {
1807 return NULL;
1808 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001810 expression = ast_for_expr(c, CHILD(n, i + 1));
1811 if (!expression) {
1812 return NULL;
1813 }
1814
1815 asdl_seq_SET(ops, i / 2, newoperator);
1816 asdl_seq_SET(cmps, i / 2, expression);
1817 }
1818 expression = ast_for_expr(c, CHILD(n, 0));
1819 if (!expression) {
1820 return NULL;
1821 }
1822
1823 return Compare(expression, ops, cmps, LINENO(n),
1824 n->n_col_offset, c->c_arena);
1825 }
1826 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001828 /* The next five cases all handle BinOps. The main body of code
1829 is the same in each case, but the switch turned inside out to
1830 reuse the code for each type of operator.
1831 */
1832 case expr:
1833 case xor_expr:
1834 case and_expr:
1835 case shift_expr:
1836 case arith_expr:
1837 case term:
1838 if (NCH(n) == 1) {
1839 n = CHILD(n, 0);
1840 goto loop;
1841 }
1842 return ast_for_binop(c, n);
1843 case yield_expr: {
1844 expr_ty exp = NULL;
1845 if (NCH(n) == 2) {
1846 exp = ast_for_testlist(c, CHILD(n, 1));
1847 if (!exp)
1848 return NULL;
1849 }
1850 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1851 }
1852 case factor:
1853 if (NCH(n) == 1) {
1854 n = CHILD(n, 0);
1855 goto loop;
1856 }
1857 return ast_for_factor(c, n);
1858 case power:
1859 return ast_for_power(c, n);
1860 default:
1861 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001864 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 return NULL;
1866}
1867
1868static expr_ty
1869ast_for_call(struct compiling *c, const node *n, expr_ty func)
1870{
1871 /*
1872 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001873 | '**' test)
1874 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 */
1876
1877 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001878 asdl_seq *args;
1879 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 expr_ty vararg = NULL, kwarg = NULL;
1881
1882 REQ(n, arglist);
1883
1884 nargs = 0;
1885 nkeywords = 0;
1886 ngens = 0;
1887 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001888 node *ch = CHILD(n, i);
1889 if (TYPE(ch) == argument) {
1890 if (NCH(ch) == 1)
1891 nargs++;
1892 else if (TYPE(CHILD(ch, 1)) == gen_for)
1893 ngens++;
1894 else
1895 nkeywords++;
1896 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 }
1898 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001899 ast_error(n, "Generator expression must be parenthesized "
1900 "if not sole argument");
1901 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902 }
1903
1904 if (nargs + nkeywords + ngens > 255) {
1905 ast_error(n, "more than 255 arguments");
1906 return NULL;
1907 }
1908
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001909 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001911 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001912 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001914 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 nargs = 0;
1916 nkeywords = 0;
1917 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001918 node *ch = CHILD(n, i);
1919 if (TYPE(ch) == argument) {
1920 expr_ty e;
1921 if (NCH(ch) == 1) {
1922 if (nkeywords) {
1923 ast_error(CHILD(ch, 0),
1924 "non-keyword arg after keyword arg");
1925 return NULL;
1926 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001927 if (vararg) {
1928 ast_error(CHILD(ch, 0),
1929 "only named arguments may follow *expression");
1930 return NULL;
1931 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001932 e = ast_for_expr(c, CHILD(ch, 0));
1933 if (!e)
1934 return NULL;
1935 asdl_seq_SET(args, nargs++, e);
1936 }
1937 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1938 e = ast_for_genexp(c, ch);
1939 if (!e)
1940 return NULL;
1941 asdl_seq_SET(args, nargs++, e);
1942 }
1943 else {
1944 keyword_ty kw;
1945 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001946 int k;
1947 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001949 /* CHILD(ch, 0) is test, but must be an identifier? */
1950 e = ast_for_expr(c, CHILD(ch, 0));
1951 if (!e)
1952 return NULL;
1953 /* f(lambda x: x[0] = 3) ends up getting parsed with
1954 * LHS test = lambda x: x[0], and RHS test = 3.
1955 * SF bug 132313 points out that complaining about a keyword
1956 * then is very confusing.
1957 */
1958 if (e->kind == Lambda_kind) {
1959 ast_error(CHILD(ch, 0),
1960 "lambda cannot contain assignment");
1961 return NULL;
1962 } else if (e->kind != Name_kind) {
1963 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1964 return NULL;
1965 }
1966 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001967 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001968 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001969 for (k = 0; k < nkeywords; k++) {
1970 tmp = PyString_AS_STRING(
1971 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
1972 if (!strcmp(tmp, PyString_AS_STRING(key))) {
1973 ast_error(CHILD(ch, 0), "keyword argument repeated");
1974 return NULL;
1975 }
1976 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001977 e = ast_for_expr(c, CHILD(ch, 2));
1978 if (!e)
1979 return NULL;
1980 kw = keyword(key, e, c->c_arena);
1981 if (!kw)
1982 return NULL;
1983 asdl_seq_SET(keywords, nkeywords++, kw);
1984 }
1985 }
1986 else if (TYPE(ch) == STAR) {
1987 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001988 if (!vararg)
1989 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001990 i++;
1991 }
1992 else if (TYPE(ch) == DOUBLESTAR) {
1993 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001994 if (!kwarg)
1995 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001996 i++;
1997 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 }
1999
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002000 return Call(func, args, keywords, vararg, kwarg, func->lineno,
2001 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002}
2003
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002005ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002007 /* testlist_gexp: test (',' test)* [','] */
2008 /* testlist: test (',' test)* [','] */
2009 /* testlist_safe: test (',' test)+ [','] */
2010 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002012 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002013 if (NCH(n) > 1)
2014 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002015 }
2016 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002017 assert(TYPE(n) == testlist ||
2018 TYPE(n) == testlist_safe ||
2019 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002020 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002022 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002024 asdl_seq *tmp = seq_for_testlist(c, n);
2025 if (!tmp)
2026 return NULL;
2027 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002029}
2030
2031static expr_ty
2032ast_for_testlist_gexp(struct compiling *c, const node* n)
2033{
2034 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2035 /* argument: test [ gen_for ] */
2036 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002037 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002038 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002039 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002040}
2041
2042/* like ast_for_testlist() but returns a sequence */
2043static asdl_seq*
2044ast_for_class_bases(struct compiling *c, const node* n)
2045{
2046 /* testlist: test (',' test)* [','] */
2047 assert(NCH(n) > 0);
2048 REQ(n, testlist);
2049 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002050 expr_ty base;
2051 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2052 if (!bases)
2053 return NULL;
2054 base = ast_for_expr(c, CHILD(n, 0));
2055 if (!base)
2056 return NULL;
2057 asdl_seq_SET(bases, 0, base);
2058 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002059 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002060
2061 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062}
2063
2064static stmt_ty
2065ast_for_expr_stmt(struct compiling *c, const node *n)
2066{
2067 REQ(n, expr_stmt);
2068 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002069 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 testlist: test (',' test)* [',']
2071 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002072 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 test: ... here starts the operator precendence dance
2074 */
2075
2076 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002077 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2078 if (!e)
2079 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002081 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 }
2083 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002084 expr_ty expr1, expr2;
2085 operator_ty newoperator;
2086 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002088 expr1 = ast_for_testlist(c, ch);
2089 if (!expr1)
2090 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002091 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002092 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002094 ch = CHILD(n, 2);
2095 if (TYPE(ch) == testlist)
2096 expr2 = ast_for_testlist(c, ch);
2097 else
2098 expr2 = ast_for_expr(c, ch);
2099 if (!expr2)
2100 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002102 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002103 if (!newoperator)
2104 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002106 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2107 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 }
2109 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002110 int i;
2111 asdl_seq *targets;
2112 node *value;
2113 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002115 /* a normal assignment */
2116 REQ(CHILD(n, 1), EQUAL);
2117 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2118 if (!targets)
2119 return NULL;
2120 for (i = 0; i < NCH(n) - 2; i += 2) {
2121 expr_ty e;
2122 node *ch = CHILD(n, i);
2123 if (TYPE(ch) == yield_expr) {
2124 ast_error(ch, "assignment to yield expression not possible");
2125 return NULL;
2126 }
2127 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002129 /* set context to assign */
2130 if (!e)
2131 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002133 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002134 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002136 asdl_seq_SET(targets, i / 2, e);
2137 }
2138 value = CHILD(n, NCH(n) - 1);
2139 if (TYPE(value) == testlist)
2140 expression = ast_for_testlist(c, value);
2141 else
2142 expression = ast_for_expr(c, value);
2143 if (!expression)
2144 return NULL;
2145 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2146 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148}
2149
2150static stmt_ty
2151ast_for_print_stmt(struct compiling *c, const node *n)
2152{
2153 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002154 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 */
2156 expr_ty dest = NULL, expression;
2157 asdl_seq *seq;
2158 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002159 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160
2161 REQ(n, print_stmt);
2162 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002163 dest = ast_for_expr(c, CHILD(n, 2));
2164 if (!dest)
2165 return NULL;
2166 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002168 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002170 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002171 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002172 expression = ast_for_expr(c, CHILD(n, i));
2173 if (!expression)
2174 return NULL;
2175 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 }
2177 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002178 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179}
2180
2181static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002182ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183{
2184 asdl_seq *seq;
2185 int i;
2186 expr_ty e;
2187
2188 REQ(n, exprlist);
2189
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002190 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002192 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002194 e = ast_for_expr(c, CHILD(n, i));
2195 if (!e)
2196 return NULL;
2197 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002198 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 }
2201 return seq;
2202}
2203
2204static stmt_ty
2205ast_for_del_stmt(struct compiling *c, const node *n)
2206{
2207 asdl_seq *expr_list;
2208
2209 /* del_stmt: 'del' exprlist */
2210 REQ(n, del_stmt);
2211
2212 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2213 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002214 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002215 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216}
2217
2218static stmt_ty
2219ast_for_flow_stmt(struct compiling *c, const node *n)
2220{
2221 /*
2222 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002223 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 break_stmt: 'break'
2225 continue_stmt: 'continue'
2226 return_stmt: 'return' [testlist]
2227 yield_stmt: yield_expr
2228 yield_expr: 'yield' testlist
2229 raise_stmt: 'raise' [test [',' test [',' test]]]
2230 */
2231 node *ch;
2232
2233 REQ(n, flow_stmt);
2234 ch = CHILD(n, 0);
2235 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002236 case break_stmt:
2237 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2238 case continue_stmt:
2239 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2240 case yield_stmt: { /* will reduce to yield_expr */
2241 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2242 if (!exp)
2243 return NULL;
2244 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2245 }
2246 case return_stmt:
2247 if (NCH(ch) == 1)
2248 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2249 else {
2250 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2251 if (!expression)
2252 return NULL;
2253 return Return(expression, LINENO(n), n->n_col_offset,
2254 c->c_arena);
2255 }
2256 case raise_stmt:
2257 if (NCH(ch) == 1)
2258 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2259 c->c_arena);
2260 else if (NCH(ch) == 2) {
2261 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2262 if (!expression)
2263 return NULL;
2264 return Raise(expression, NULL, NULL, LINENO(n),
2265 n->n_col_offset, c->c_arena);
2266 }
2267 else if (NCH(ch) == 4) {
2268 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002270 expr1 = ast_for_expr(c, CHILD(ch, 1));
2271 if (!expr1)
2272 return NULL;
2273 expr2 = ast_for_expr(c, CHILD(ch, 3));
2274 if (!expr2)
2275 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002277 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2278 c->c_arena);
2279 }
2280 else if (NCH(ch) == 6) {
2281 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002283 expr1 = ast_for_expr(c, CHILD(ch, 1));
2284 if (!expr1)
2285 return NULL;
2286 expr2 = ast_for_expr(c, CHILD(ch, 3));
2287 if (!expr2)
2288 return NULL;
2289 expr3 = ast_for_expr(c, CHILD(ch, 5));
2290 if (!expr3)
2291 return NULL;
2292
2293 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2294 c->c_arena);
2295 }
2296 default:
2297 PyErr_Format(PyExc_SystemError,
2298 "unexpected flow_stmt: %d", TYPE(ch));
2299 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002301
2302 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2303 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304}
2305
2306static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002307alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308{
2309 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002310 import_as_name: NAME ['as' NAME]
2311 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 dotted_name: NAME ('.' NAME)*
2313 */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002314 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002315
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 loop:
2317 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002318 case import_as_name:
2319 str = NULL;
2320 if (NCH(n) == 3) {
2321 str = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002322 if (!str)
2323 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002324 }
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002325 name = NEW_IDENTIFIER(CHILD(n, 0));
2326 if (!name)
2327 return NULL;
2328 return alias(name, str, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002329 case dotted_as_name:
2330 if (NCH(n) == 1) {
2331 n = CHILD(n, 0);
2332 goto loop;
2333 }
2334 else {
2335 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2336 if (!a)
2337 return NULL;
2338 assert(!a->asname);
2339 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002340 if (!a->asname)
2341 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002342 return a;
2343 }
2344 break;
2345 case dotted_name:
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002346 if (NCH(n) == 1) {
2347 name = NEW_IDENTIFIER(CHILD(n, 0));
2348 if (!name)
2349 return NULL;
2350 return alias(name, NULL, c->c_arena);
2351 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002352 else {
2353 /* Create a string of the form "a.b.c" */
2354 int i;
2355 size_t len;
2356 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002358 len = 0;
2359 for (i = 0; i < NCH(n); i += 2)
2360 /* length of string plus one for the dot */
2361 len += strlen(STR(CHILD(n, i))) + 1;
2362 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002363 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002364 if (!str)
2365 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002366 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002367 if (!s)
2368 return NULL;
2369 for (i = 0; i < NCH(n); i += 2) {
2370 char *sch = STR(CHILD(n, i));
2371 strcpy(s, STR(CHILD(n, i)));
2372 s += strlen(sch);
2373 *s++ = '.';
2374 }
2375 --s;
2376 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002377 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002378 PyArena_AddPyObject(c->c_arena, str);
2379 return alias(str, NULL, c->c_arena);
2380 }
2381 break;
2382 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002383 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002384 PyArena_AddPyObject(c->c_arena, str);
2385 return alias(str, NULL, c->c_arena);
2386 default:
2387 PyErr_Format(PyExc_SystemError,
2388 "unexpected import name: %d", TYPE(n));
2389 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002391
2392 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 return NULL;
2394}
2395
2396static stmt_ty
2397ast_for_import_stmt(struct compiling *c, const node *n)
2398{
2399 /*
2400 import_stmt: import_name | import_from
2401 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002402 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002403 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002405 int lineno;
2406 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 int i;
2408 asdl_seq *aliases;
2409
2410 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002411 lineno = LINENO(n);
2412 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002414 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002415 n = CHILD(n, 1);
2416 REQ(n, dotted_as_names);
2417 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2418 if (!aliases)
2419 return NULL;
2420 for (i = 0; i < NCH(n); i += 2) {
2421 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2422 if (!import_alias)
2423 return NULL;
2424 asdl_seq_SET(aliases, i / 2, import_alias);
2425 }
2426 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002428 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002429 int n_children;
2430 int idx, ndots = 0;
2431 alias_ty mod = NULL;
2432 identifier modname;
2433
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002434 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002435 optional module name */
2436 for (idx = 1; idx < NCH(n); idx++) {
2437 if (TYPE(CHILD(n, idx)) == dotted_name) {
2438 mod = alias_for_import_name(c, CHILD(n, idx));
2439 idx++;
2440 break;
2441 } else if (TYPE(CHILD(n, idx)) != DOT) {
2442 break;
2443 }
2444 ndots++;
2445 }
2446 idx++; /* skip over the 'import' keyword */
2447 switch (TYPE(CHILD(n, idx))) {
2448 case STAR:
2449 /* from ... import * */
2450 n = CHILD(n, idx);
2451 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002452 break;
2453 case LPAR:
2454 /* from ... import (x, y, z) */
2455 n = CHILD(n, idx + 1);
2456 n_children = NCH(n);
2457 break;
2458 case import_as_names:
2459 /* from ... import x, y, z */
2460 n = CHILD(n, idx);
2461 n_children = NCH(n);
2462 if (n_children % 2 == 0) {
2463 ast_error(n, "trailing comma not allowed without"
2464 " surrounding parentheses");
2465 return NULL;
2466 }
2467 break;
2468 default:
2469 ast_error(n, "Unexpected node-type in from-import");
2470 return NULL;
2471 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002473 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2474 if (!aliases)
2475 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002477 /* handle "from ... import *" special b/c there's no children */
2478 if (TYPE(n) == STAR) {
2479 alias_ty import_alias = alias_for_import_name(c, n);
2480 if (!import_alias)
2481 return NULL;
2482 asdl_seq_SET(aliases, 0, import_alias);
2483 }
2484 else {
2485 for (i = 0; i < NCH(n); i += 2) {
2486 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2487 if (!import_alias)
2488 return NULL;
2489 asdl_seq_SET(aliases, i / 2, import_alias);
2490 }
2491 }
2492 if (mod != NULL)
2493 modname = mod->name;
2494 else
2495 modname = new_identifier("", c->c_arena);
2496 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2497 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 }
Neal Norwitz79792652005-11-14 04:25:03 +00002499 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002500 "unknown import statement: starts with command '%s'",
2501 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502 return NULL;
2503}
2504
2505static stmt_ty
2506ast_for_global_stmt(struct compiling *c, const node *n)
2507{
2508 /* global_stmt: 'global' NAME (',' NAME)* */
2509 identifier name;
2510 asdl_seq *s;
2511 int i;
2512
2513 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002514 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002518 name = NEW_IDENTIFIER(CHILD(n, i));
2519 if (!name)
2520 return NULL;
2521 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002523 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524}
2525
2526static stmt_ty
2527ast_for_exec_stmt(struct compiling *c, const node *n)
2528{
2529 expr_ty expr1, globals = NULL, locals = NULL;
2530 int n_children = NCH(n);
2531 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002532 PyErr_Format(PyExc_SystemError,
2533 "poorly formed 'exec' statement: %d parts to statement",
2534 n_children);
2535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 }
2537
2538 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2539 REQ(n, exec_stmt);
2540 expr1 = ast_for_expr(c, CHILD(n, 1));
2541 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002542 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002544 globals = ast_for_expr(c, CHILD(n, 3));
2545 if (!globals)
2546 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 }
2548 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002549 locals = ast_for_expr(c, CHILD(n, 5));
2550 if (!locals)
2551 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 }
2553
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002554 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2555 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556}
2557
2558static stmt_ty
2559ast_for_assert_stmt(struct compiling *c, const node *n)
2560{
2561 /* assert_stmt: 'assert' test [',' test] */
2562 REQ(n, assert_stmt);
2563 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002564 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2565 if (!expression)
2566 return NULL;
2567 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2568 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 }
2570 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002571 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002573 expr1 = ast_for_expr(c, CHILD(n, 1));
2574 if (!expr1)
2575 return NULL;
2576 expr2 = ast_for_expr(c, CHILD(n, 3));
2577 if (!expr2)
2578 return NULL;
2579
2580 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 }
Neal Norwitz79792652005-11-14 04:25:03 +00002582 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002583 "improper number of parts to 'assert' statement: %d",
2584 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 return NULL;
2586}
2587
2588static asdl_seq *
2589ast_for_suite(struct compiling *c, const node *n)
2590{
2591 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002592 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 stmt_ty s;
2594 int i, total, num, end, pos = 0;
2595 node *ch;
2596
2597 REQ(n, suite);
2598
2599 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002600 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002602 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002604 n = CHILD(n, 0);
2605 /* simple_stmt always ends with a NEWLINE,
2606 and may have a trailing SEMI
2607 */
2608 end = NCH(n) - 1;
2609 if (TYPE(CHILD(n, end - 1)) == SEMI)
2610 end--;
2611 /* loop by 2 to skip semi-colons */
2612 for (i = 0; i < end; i += 2) {
2613 ch = CHILD(n, i);
2614 s = ast_for_stmt(c, ch);
2615 if (!s)
2616 return NULL;
2617 asdl_seq_SET(seq, pos++, s);
2618 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 }
2620 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002621 for (i = 2; i < (NCH(n) - 1); i++) {
2622 ch = CHILD(n, i);
2623 REQ(ch, stmt);
2624 num = num_stmts(ch);
2625 if (num == 1) {
2626 /* small_stmt or compound_stmt with only one child */
2627 s = ast_for_stmt(c, ch);
2628 if (!s)
2629 return NULL;
2630 asdl_seq_SET(seq, pos++, s);
2631 }
2632 else {
2633 int j;
2634 ch = CHILD(ch, 0);
2635 REQ(ch, simple_stmt);
2636 for (j = 0; j < NCH(ch); j += 2) {
2637 /* statement terminates with a semi-colon ';' */
2638 if (NCH(CHILD(ch, j)) == 0) {
2639 assert((j + 1) == NCH(ch));
2640 break;
2641 }
2642 s = ast_for_stmt(c, CHILD(ch, j));
2643 if (!s)
2644 return NULL;
2645 asdl_seq_SET(seq, pos++, s);
2646 }
2647 }
2648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 }
2650 assert(pos == seq->size);
2651 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652}
2653
2654static stmt_ty
2655ast_for_if_stmt(struct compiling *c, const node *n)
2656{
2657 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2658 ['else' ':' suite]
2659 */
2660 char *s;
2661
2662 REQ(n, if_stmt);
2663
2664 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002665 expr_ty expression;
2666 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002668 expression = ast_for_expr(c, CHILD(n, 1));
2669 if (!expression)
2670 return NULL;
2671 suite_seq = ast_for_suite(c, CHILD(n, 3));
2672 if (!suite_seq)
2673 return NULL;
2674
2675 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2676 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002678
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 s = STR(CHILD(n, 4));
2680 /* s[2], the third character in the string, will be
2681 's' for el_s_e, or
2682 'i' for el_i_f
2683 */
2684 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002685 expr_ty expression;
2686 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002688 expression = ast_for_expr(c, CHILD(n, 1));
2689 if (!expression)
2690 return NULL;
2691 seq1 = ast_for_suite(c, CHILD(n, 3));
2692 if (!seq1)
2693 return NULL;
2694 seq2 = ast_for_suite(c, CHILD(n, 6));
2695 if (!seq2)
2696 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002698 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2699 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 }
2701 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002702 int i, n_elif, has_else = 0;
2703 expr_ty expression;
2704 asdl_seq *suite_seq;
2705 asdl_seq *orelse = NULL;
2706 n_elif = NCH(n) - 4;
2707 /* must reference the child n_elif+1 since 'else' token is third,
2708 not fourth, child from the end. */
2709 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2710 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2711 has_else = 1;
2712 n_elif -= 3;
2713 }
2714 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002716 if (has_else) {
2717 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002719 orelse = asdl_seq_new(1, c->c_arena);
2720 if (!orelse)
2721 return NULL;
2722 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2723 if (!expression)
2724 return NULL;
2725 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2726 if (!suite_seq)
2727 return NULL;
2728 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2729 if (!suite_seq2)
2730 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002732 asdl_seq_SET(orelse, 0,
2733 If(expression, suite_seq, suite_seq2,
2734 LINENO(CHILD(n, NCH(n) - 6)),
2735 CHILD(n, NCH(n) - 6)->n_col_offset,
2736 c->c_arena));
2737 /* the just-created orelse handled the last elif */
2738 n_elif--;
2739 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002741 for (i = 0; i < n_elif; i++) {
2742 int off = 5 + (n_elif - i - 1) * 4;
2743 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2744 if (!newobj)
2745 return NULL;
2746 expression = ast_for_expr(c, CHILD(n, off));
2747 if (!expression)
2748 return NULL;
2749 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2750 if (!suite_seq)
2751 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002753 asdl_seq_SET(newobj, 0,
2754 If(expression, suite_seq, orelse,
2755 LINENO(CHILD(n, off)),
2756 CHILD(n, off)->n_col_offset, c->c_arena));
2757 orelse = newobj;
2758 }
2759 expression = ast_for_expr(c, CHILD(n, 1));
2760 if (!expression)
2761 return NULL;
2762 suite_seq = ast_for_suite(c, CHILD(n, 3));
2763 if (!suite_seq)
2764 return NULL;
2765 return If(expression, suite_seq, orelse,
2766 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002768
2769 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002770 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772}
2773
2774static stmt_ty
2775ast_for_while_stmt(struct compiling *c, const node *n)
2776{
2777 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2778 REQ(n, while_stmt);
2779
2780 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002781 expr_ty expression;
2782 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002784 expression = ast_for_expr(c, CHILD(n, 1));
2785 if (!expression)
2786 return NULL;
2787 suite_seq = ast_for_suite(c, CHILD(n, 3));
2788 if (!suite_seq)
2789 return NULL;
2790 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2791 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792 }
2793 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002794 expr_ty expression;
2795 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002797 expression = ast_for_expr(c, CHILD(n, 1));
2798 if (!expression)
2799 return NULL;
2800 seq1 = ast_for_suite(c, CHILD(n, 3));
2801 if (!seq1)
2802 return NULL;
2803 seq2 = ast_for_suite(c, CHILD(n, 6));
2804 if (!seq2)
2805 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002807 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2808 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002810
2811 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002812 "wrong number of tokens for 'while' statement: %d",
2813 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815}
2816
2817static stmt_ty
2818ast_for_for_stmt(struct compiling *c, const node *n)
2819{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002820 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 expr_ty expression;
2822 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002823 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2825 REQ(n, for_stmt);
2826
2827 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002828 seq = ast_for_suite(c, CHILD(n, 8));
2829 if (!seq)
2830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 }
2832
Neal Norwitzedef2be2006-07-12 05:26:17 +00002833 node_target = CHILD(n, 1);
2834 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002835 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002836 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002837 /* Check the # of children rather than the length of _target, since
2838 for x, in ... has 1 element in _target, but still requires a Tuple. */
2839 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002840 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002842 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002844 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002845 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002846 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002848 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002849 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002851 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002852 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853}
2854
2855static excepthandler_ty
2856ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2857{
Collin Winter62903052007-05-18 23:11:24 +00002858 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 REQ(exc, except_clause);
2860 REQ(body, suite);
2861
2862 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002863 asdl_seq *suite_seq = ast_for_suite(c, body);
2864 if (!suite_seq)
2865 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866
Georg Brandla48f3ab2008-03-30 06:40:17 +00002867 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002868 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 }
2870 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002871 expr_ty expression;
2872 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002874 expression = ast_for_expr(c, CHILD(exc, 1));
2875 if (!expression)
2876 return NULL;
2877 suite_seq = ast_for_suite(c, body);
2878 if (!suite_seq)
2879 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880
Georg Brandla48f3ab2008-03-30 06:40:17 +00002881 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002882 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 }
2884 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002885 asdl_seq *suite_seq;
2886 expr_ty expression;
2887 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2888 if (!e)
2889 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002890 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002891 return NULL;
2892 expression = ast_for_expr(c, CHILD(exc, 1));
2893 if (!expression)
2894 return NULL;
2895 suite_seq = ast_for_suite(c, body);
2896 if (!suite_seq)
2897 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898
Georg Brandla48f3ab2008-03-30 06:40:17 +00002899 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002900 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002902
2903 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002904 "wrong number of children for 'except' clause: %d",
2905 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002906 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907}
2908
2909static stmt_ty
2910ast_for_try_stmt(struct compiling *c, const node *n)
2911{
Neal Norwitzf599f422005-12-17 21:33:47 +00002912 const int nch = NCH(n);
2913 int n_except = (nch - 3)/3;
2914 asdl_seq *body, *orelse = NULL, *finally = NULL;
2915
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 REQ(n, try_stmt);
2917
Neal Norwitzf599f422005-12-17 21:33:47 +00002918 body = ast_for_suite(c, CHILD(n, 2));
2919 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921
Neal Norwitzf599f422005-12-17 21:33:47 +00002922 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002923 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2924 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2925 /* we can assume it's an "else",
2926 because nch >= 9 for try-else-finally and
2927 it would otherwise have a type of except_clause */
2928 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2929 if (orelse == NULL)
2930 return NULL;
2931 n_except--;
2932 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002934 finally = ast_for_suite(c, CHILD(n, nch - 1));
2935 if (finally == NULL)
2936 return NULL;
2937 n_except--;
2938 }
2939 else {
2940 /* we can assume it's an "else",
2941 otherwise it would have a type of except_clause */
2942 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2943 if (orelse == NULL)
2944 return NULL;
2945 n_except--;
2946 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002948 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002949 ast_error(n, "malformed 'try' statement");
2950 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002952
2953 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002954 int i;
2955 stmt_ty except_st;
2956 /* process except statements to create a try ... except */
2957 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2958 if (handlers == NULL)
2959 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002960
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002961 for (i = 0; i < n_except; i++) {
2962 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2963 CHILD(n, 5 + i * 3));
2964 if (!e)
2965 return NULL;
2966 asdl_seq_SET(handlers, i, e);
2967 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002968
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002969 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2970 n->n_col_offset, c->c_arena);
2971 if (!finally)
2972 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002973
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002974 /* if a 'finally' is present too, we nest the TryExcept within a
2975 TryFinally to emulate try ... except ... finally */
2976 body = asdl_seq_new(1, c->c_arena);
2977 if (body == NULL)
2978 return NULL;
2979 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002980 }
2981
2982 /* must be a try ... finally (except clauses are in body, if any exist) */
2983 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002984 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985}
2986
Georg Brandl944f6842009-05-25 21:02:56 +00002987/* with_item: test ['as' expr] */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002988static stmt_ty
Georg Brandl944f6842009-05-25 21:02:56 +00002989ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002990{
2991 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002992
Georg Brandl944f6842009-05-25 21:02:56 +00002993 REQ(n, with_item);
2994 context_expr = ast_for_expr(c, CHILD(n, 0));
Collin Winter77c67bd2007-03-16 04:11:30 +00002995 if (!context_expr)
2996 return NULL;
Georg Brandl944f6842009-05-25 21:02:56 +00002997 if (NCH(n) == 3) {
2998 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002999
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003000 if (!optional_vars) {
3001 return NULL;
3002 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003003 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003004 return NULL;
3005 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003006 }
3007
Georg Brandl944f6842009-05-25 21:02:56 +00003008 return With(context_expr, optional_vars, content, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003009 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003010}
3011
Georg Brandl944f6842009-05-25 21:02:56 +00003012/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3013static stmt_ty
3014ast_for_with_stmt(struct compiling *c, const node *n)
3015{
3016 int i;
3017 stmt_ty ret;
3018 asdl_seq *inner;
3019
3020 REQ(n, with_stmt);
3021
3022 /* process the with items inside-out */
3023 i = NCH(n) - 1;
3024 /* the suite of the innermost with item is the suite of the with stmt */
3025 inner = ast_for_suite(c, CHILD(n, i));
3026 if (!inner)
3027 return NULL;
3028
3029 for (;;) {
3030 i -= 2;
3031 ret = ast_for_with_item(c, CHILD(n, i), inner);
3032 if (!ret)
3033 return NULL;
3034 /* was this the last item? */
3035 if (i == 1)
3036 break;
3037 /* if not, wrap the result so far in a new sequence */
3038 inner = asdl_seq_new(1, c->c_arena);
3039 if (!inner)
3040 return NULL;
3041 asdl_seq_SET(inner, 0, ret);
3042 }
3043
3044 return ret;
3045}
3046
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003048ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049{
3050 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003051 PyObject *classname;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052 asdl_seq *bases, *s;
3053
3054 REQ(n, classdef);
3055
Benjamin Petersond5efd202008-06-08 22:52:37 +00003056 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003057 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058
3059 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003060 s = ast_for_suite(c, CHILD(n, 3));
3061 if (!s)
3062 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003063 classname = NEW_IDENTIFIER(CHILD(n, 1));
3064 if (!classname)
3065 return NULL;
3066 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3067 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 }
3069 /* check for empty base list */
3070 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003071 s = ast_for_suite(c, CHILD(n,5));
3072 if (!s)
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003073 return NULL;
3074 classname = NEW_IDENTIFIER(CHILD(n, 1));
3075 if (!classname)
3076 return NULL;
3077 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3078 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 }
3080
3081 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003082 bases = ast_for_class_bases(c, CHILD(n, 3));
3083 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003084 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085
3086 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003087 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003088 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003089 classname = NEW_IDENTIFIER(CHILD(n, 1));
3090 if (!classname)
3091 return NULL;
3092 return ClassDef(classname, bases, s, decorator_seq,
Christian Heimes5224d282008-02-23 15:01:05 +00003093 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003094}
3095
3096static stmt_ty
3097ast_for_stmt(struct compiling *c, const node *n)
3098{
3099 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003100 assert(NCH(n) == 1);
3101 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102 }
3103 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003104 assert(num_stmts(n) == 1);
3105 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 }
3107 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003108 n = CHILD(n, 0);
3109 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3110 | flow_stmt | import_stmt | global_stmt | exec_stmt
3111 | assert_stmt
3112 */
3113 switch (TYPE(n)) {
3114 case expr_stmt:
3115 return ast_for_expr_stmt(c, n);
3116 case print_stmt:
3117 return ast_for_print_stmt(c, n);
3118 case del_stmt:
3119 return ast_for_del_stmt(c, n);
3120 case pass_stmt:
3121 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3122 case flow_stmt:
3123 return ast_for_flow_stmt(c, n);
3124 case import_stmt:
3125 return ast_for_import_stmt(c, n);
3126 case global_stmt:
3127 return ast_for_global_stmt(c, n);
3128 case exec_stmt:
3129 return ast_for_exec_stmt(c, n);
3130 case assert_stmt:
3131 return ast_for_assert_stmt(c, n);
3132 default:
3133 PyErr_Format(PyExc_SystemError,
3134 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3135 TYPE(n), NCH(n));
3136 return NULL;
3137 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 }
3139 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003140 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003141 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003142 */
3143 node *ch = CHILD(n, 0);
3144 REQ(n, compound_stmt);
3145 switch (TYPE(ch)) {
3146 case if_stmt:
3147 return ast_for_if_stmt(c, ch);
3148 case while_stmt:
3149 return ast_for_while_stmt(c, ch);
3150 case for_stmt:
3151 return ast_for_for_stmt(c, ch);
3152 case try_stmt:
3153 return ast_for_try_stmt(c, ch);
3154 case with_stmt:
3155 return ast_for_with_stmt(c, ch);
3156 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003157 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003158 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003159 return ast_for_classdef(c, ch, NULL);
3160 case decorated:
3161 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003162 default:
3163 PyErr_Format(PyExc_SystemError,
3164 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3165 TYPE(n), NCH(n));
3166 return NULL;
3167 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 }
3169}
3170
3171static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003172parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003174 const char *end;
3175 long x;
3176 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003178 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003179 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180#endif
3181
Mark Dickinson422ce062008-12-05 17:59:46 +00003182 assert(s != NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003183 errno = 0;
3184 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003186 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003188 if (*end == 'l' || *end == 'L')
3189 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003190 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003191 if (*end == '\0') {
3192 if (errno != 0)
3193 return PyLong_FromString((char *)s, (char **)0, 0);
3194 return PyInt_FromLong(x);
3195 }
3196 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003198 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003199 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003200 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003201 complex.imag = PyOS_ascii_atof(s);
3202 PyFPE_END_PROTECT(complex)
3203 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003204 }
3205 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003207 {
3208 PyFPE_START_PROTECT("atof", return 0)
3209 dx = PyOS_ascii_atof(s);
3210 PyFPE_END_PROTECT(dx)
3211 return PyFloat_FromDouble(dx);
3212 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213}
3214
3215static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003216decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217{
3218#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003219 Py_FatalError("decode_utf8 should not be called in this build.");
3220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003222 PyObject *u, *v;
3223 char *s, *t;
3224 t = s = (char *)*sPtr;
3225 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3226 while (s < end && (*s & 0x80)) s++;
3227 *sPtr = s;
3228 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3229 if (u == NULL)
3230 return NULL;
3231 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3232 Py_DECREF(u);
3233 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234#endif
3235}
3236
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003237#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003239decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003241 PyObject *v, *u;
3242 char *buf;
3243 char *p;
3244 const char *end;
3245 if (encoding == NULL) {
3246 buf = (char *)s;
3247 u = NULL;
3248 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3249 buf = (char *)s;
3250 u = NULL;
3251 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003252 /* check for integer overflow */
3253 if (len > PY_SIZE_MAX / 4)
3254 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003255 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003256 u = PyString_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003257 if (u == NULL)
3258 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003259 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003260 end = s + len;
3261 while (s < end) {
3262 if (*s == '\\') {
3263 *p++ = *s++;
3264 if (*s & 0x80) {
3265 strcpy(p, "u005c");
3266 p += 5;
3267 }
3268 }
3269 if (*s & 0x80) { /* XXX inefficient */
3270 PyObject *w;
3271 char *r;
3272 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003273 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003274 if (w == NULL) {
3275 Py_DECREF(u);
3276 return NULL;
3277 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003278 r = PyString_AsString(w);
3279 rn = PyString_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003280 assert(rn % 2 == 0);
3281 for (i = 0; i < rn; i += 2) {
3282 sprintf(p, "\\u%02x%02x",
3283 r[i + 0] & 0xFF,
3284 r[i + 1] & 0xFF);
3285 p += 6;
3286 }
3287 Py_DECREF(w);
3288 } else {
3289 *p++ = *s++;
3290 }
3291 }
3292 len = p - buf;
3293 s = buf;
3294 }
3295 if (rawmode)
3296 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3297 else
3298 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3299 Py_XDECREF(u);
3300 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003302#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303
3304/* s is a Python string literal, including the bracketing quote characters,
3305 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3306 * parsestr parses it, and returns the decoded Python string object.
3307 */
3308static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003309parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003311 size_t len;
3312 int quote = Py_CHARMASK(*s);
3313 int rawmode = 0;
3314 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003315 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003317 if (isalpha(quote) || quote == '_') {
3318 if (quote == 'u' || quote == 'U') {
3319 quote = *++s;
3320 unicode = 1;
3321 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003322 if (quote == 'b' || quote == 'B') {
3323 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003324 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003325 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003326 if (quote == 'r' || quote == 'R') {
3327 quote = *++s;
3328 rawmode = 1;
3329 }
3330 }
3331 if (quote != '\'' && quote != '\"') {
3332 PyErr_BadInternalCall();
3333 return NULL;
3334 }
3335 s++;
3336 len = strlen(s);
3337 if (len > INT_MAX) {
3338 PyErr_SetString(PyExc_OverflowError,
3339 "string to parse is too long");
3340 return NULL;
3341 }
3342 if (s[--len] != quote) {
3343 PyErr_BadInternalCall();
3344 return NULL;
3345 }
3346 if (len >= 4 && s[0] == quote && s[1] == quote) {
3347 s += 2;
3348 len -= 2;
3349 if (s[--len] != quote || s[--len] != quote) {
3350 PyErr_BadInternalCall();
3351 return NULL;
3352 }
3353 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003355 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003356 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003359 need_encoding = (c->c_encoding != NULL &&
3360 strcmp(c->c_encoding, "utf-8") != 0 &&
3361 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003362 if (rawmode || strchr(s, '\\') == NULL) {
3363 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003365 /* This should not happen - we never see any other
3366 encoding. */
3367 Py_FatalError(
3368 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003370 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3371 if (u == NULL)
3372 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003373 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003374 Py_DECREF(u);
3375 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003377 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003378 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003379 }
3380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003382 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003383 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384}
3385
3386/* Build a Python string object out of a STRING atom. This takes care of
3387 * compile-time literal catenation, calling parsestr() on each piece, and
3388 * pasting the intermediate results together.
3389 */
3390static PyObject *
3391parsestrplus(struct compiling *c, const node *n)
3392{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003393 PyObject *v;
3394 int i;
3395 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003396 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003397 /* String literal concatenation */
3398 for (i = 1; i < NCH(n); i++) {
3399 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003400 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003401 if (s == NULL)
3402 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003403 if (PyString_Check(v) && PyString_Check(s)) {
3404 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003405 if (v == NULL)
3406 goto onError;
3407 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003409 else {
3410 PyObject *temp = PyUnicode_Concat(v, s);
3411 Py_DECREF(s);
3412 Py_DECREF(v);
3413 v = temp;
3414 if (v == NULL)
3415 goto onError;
3416 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003418 }
3419 }
3420 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421
3422 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003423 Py_XDECREF(v);
3424 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425}