blob: 29b0e829d4d7c317b04fae9d58d6cfe897363948 [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);
Neal Norwitzadb69fc2005-12-17 20:54:49 +000050 PyArena_AddPyObject(arena, id);
51 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052}
53
Neal Norwitzadb69fc2005-12-17 20:54:49 +000054#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000055
56/* This routine provides an invalid object for the syntax error.
57 The outermost routine must unpack this error and create the
58 proper object. We do this so that we don't have to pass
59 the filename to everything function.
60
61 XXX Maybe we should just pass the filename...
62*/
63
64static int
65ast_error(const node *n, const char *errstr)
66{
67 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
68 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000069 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000070 PyErr_SetObject(PyExc_SyntaxError, u);
71 Py_DECREF(u);
72 return 0;
73}
74
75static void
76ast_error_finish(const char *filename)
77{
78 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000079 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080
81 assert(PyErr_Occurred());
82 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000083 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084
85 PyErr_Fetch(&type, &value, &tback);
86 errstr = PyTuple_GetItem(value, 0);
87 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000088 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089 Py_INCREF(errstr);
90 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000091 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000092 Py_DECREF(errstr);
93 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000094 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 Py_DECREF(value);
96
97 loc = PyErr_ProgramText(filename, lineno);
98 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000099 Py_INCREF(Py_None);
100 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000101 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000102 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000104 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000105 Py_DECREF(errstr);
106 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000107 }
Georg Brandl7784f122006-05-26 20:04:44 +0000108 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109 Py_DECREF(errstr);
110 Py_DECREF(tmp);
111 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000112 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113 PyErr_Restore(type, value, tback);
114}
115
Benjamin Petersoncbd78132008-06-08 15:45:23 +0000116static int
117ast_warn(struct compiling *c, const node *n, char *msg)
118{
119 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),
120 NULL, NULL) < 0) {
121 /* if -Werr, change it to a SyntaxError */
122 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))
123 ast_error(n, msg);
124 return 0;
125 }
126 return 1;
127}
128
Benjamin Petersond5efd202008-06-08 22:52:37 +0000129static int
130forbidden_check(struct compiling *c, const node *n, const char *x)
131{
132 if (!strcmp(x, "None"))
133 return ast_error(n, "assignment to None");
Benjamin Peterson399b1fe2008-10-25 02:53:28 +0000134 if (Py_Py3kWarningFlag) {
135 if (!(strcmp(x, "True") && strcmp(x, "False")) &&
136 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
137 return 0;
138 if (!strcmp(x, "nonlocal") &&
139 !ast_warn(c, n, "nonlocal is a keyword in 3.x"))
140 return 0;
141 }
Benjamin Petersond5efd202008-06-08 22:52:37 +0000142 return 1;
143}
144
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145/* num_stmts() returns number of contained statements.
146
147 Use this routine to determine how big a sequence is needed for
148 the statements in a parse tree. Its raison d'etre is this bit of
149 grammar:
150
151 stmt: simple_stmt | compound_stmt
152 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
153
154 A simple_stmt can contain multiple small_stmt elements joined
155 by semicolons. If the arg is a simple_stmt, the number of
156 small_stmt elements is returned.
157*/
158
159static int
160num_stmts(const node *n)
161{
162 int i, l;
163 node *ch;
164
165 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000166 case single_input:
167 if (TYPE(CHILD(n, 0)) == NEWLINE)
168 return 0;
169 else
170 return num_stmts(CHILD(n, 0));
171 case file_input:
172 l = 0;
173 for (i = 0; i < NCH(n); i++) {
174 ch = CHILD(n, i);
175 if (TYPE(ch) == stmt)
176 l += num_stmts(ch);
177 }
178 return l;
179 case stmt:
180 return num_stmts(CHILD(n, 0));
181 case compound_stmt:
182 return 1;
183 case simple_stmt:
184 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
185 case suite:
186 if (NCH(n) == 1)
187 return num_stmts(CHILD(n, 0));
188 else {
189 l = 0;
190 for (i = 2; i < (NCH(n) - 1); i++)
191 l += num_stmts(CHILD(n, i));
192 return l;
193 }
194 default: {
195 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000197 sprintf(buf, "Non-statement found: %d %d\n",
198 TYPE(n), NCH(n));
199 Py_FatalError(buf);
200 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201 }
202 assert(0);
203 return 0;
204}
205
206/* Transform the CST rooted at node * to the appropriate AST
207*/
208
209mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000210PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000211 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000213 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214 asdl_seq *stmts = NULL;
215 stmt_ty s;
216 node *ch;
217 struct compiling c;
218
219 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000220 c.c_encoding = "utf-8";
221 if (TYPE(n) == encoding_decl) {
222 ast_error(n, "encoding declaration in Unicode string");
223 goto error;
224 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000226 c.c_encoding = STR(n);
227 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000229 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230 }
Christian Heimes3c608332008-03-26 22:01:37 +0000231 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000232 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000233 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234
Jeremy Hyltona8293132006-02-28 17:58:27 +0000235 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000237 case file_input:
238 stmts = asdl_seq_new(num_stmts(n), arena);
239 if (!stmts)
240 return NULL;
241 for (i = 0; i < NCH(n) - 1; i++) {
242 ch = CHILD(n, i);
243 if (TYPE(ch) == NEWLINE)
244 continue;
245 REQ(ch, stmt);
246 num = num_stmts(ch);
247 if (num == 1) {
248 s = ast_for_stmt(&c, ch);
249 if (!s)
250 goto error;
251 asdl_seq_SET(stmts, k++, s);
252 }
253 else {
254 ch = CHILD(ch, 0);
255 REQ(ch, simple_stmt);
256 for (j = 0; j < num; j++) {
257 s = ast_for_stmt(&c, CHILD(ch, j * 2));
258 if (!s)
259 goto error;
260 asdl_seq_SET(stmts, k++, s);
261 }
262 }
263 }
264 return Module(stmts, arena);
265 case eval_input: {
266 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000268 /* XXX Why not gen_for here? */
269 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
270 if (!testlist_ast)
271 goto error;
272 return Expression(testlist_ast, arena);
273 }
274 case single_input:
275 if (TYPE(CHILD(n, 0)) == NEWLINE) {
276 stmts = asdl_seq_new(1, arena);
277 if (!stmts)
278 goto error;
279 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
280 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000281 if (!asdl_seq_GET(stmts, 0))
282 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000283 return Interactive(stmts, arena);
284 }
285 else {
286 n = CHILD(n, 0);
287 num = num_stmts(n);
288 stmts = asdl_seq_new(num, arena);
289 if (!stmts)
290 goto error;
291 if (num == 1) {
292 s = ast_for_stmt(&c, n);
293 if (!s)
294 goto error;
295 asdl_seq_SET(stmts, 0, s);
296 }
297 else {
298 /* Only a simple_stmt can contain multiple statements. */
299 REQ(n, simple_stmt);
300 for (i = 0; i < NCH(n); i += 2) {
301 if (TYPE(CHILD(n, i)) == NEWLINE)
302 break;
303 s = ast_for_stmt(&c, CHILD(n, i));
304 if (!s)
305 goto error;
306 asdl_seq_SET(stmts, i / 2, s);
307 }
308 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000310 return Interactive(stmts, arena);
311 }
312 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000313 PyErr_Format(PyExc_SystemError,
314 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000315 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000316 }
317 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318 ast_error_finish(filename);
319 return NULL;
320}
321
322/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
323*/
324
325static operator_ty
326get_operator(const node *n)
327{
328 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000329 case VBAR:
330 return BitOr;
331 case CIRCUMFLEX:
332 return BitXor;
333 case AMPER:
334 return BitAnd;
335 case LEFTSHIFT:
336 return LShift;
337 case RIGHTSHIFT:
338 return RShift;
339 case PLUS:
340 return Add;
341 case MINUS:
342 return Sub;
343 case STAR:
344 return Mult;
345 case SLASH:
346 return Div;
347 case DOUBLESLASH:
348 return FloorDiv;
349 case PERCENT:
350 return Mod;
351 default:
352 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353 }
354}
355
Jeremy Hyltona8293132006-02-28 17:58:27 +0000356/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357
358 Only sets context for expr kinds that "can appear in assignment context"
359 (according to ../Parser/Python.asdl). For other expr kinds, it sets
360 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361*/
362
363static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000364set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365{
366 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000367 /* If a particular expression type can't be used for assign / delete,
368 set expr_name to its name and an error message will be generated.
369 */
370 const char* expr_name = NULL;
371
372 /* The ast defines augmented store and load contexts, but the
373 implementation here doesn't actually use them. The code may be
374 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000375 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000377 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000378 */
379 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380
381 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000382 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000383 if (ctx == Store && !forbidden_check(c, n,
384 PyBytes_AS_STRING(e->v.Attribute.attr)))
385 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000386 e->v.Attribute.ctx = ctx;
387 break;
388 case Subscript_kind:
389 e->v.Subscript.ctx = ctx;
390 break;
391 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000392 if (ctx == Store && !forbidden_check(c, n,
393 PyBytes_AS_STRING(e->v.Name.id)))
394 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000395 e->v.Name.ctx = ctx;
396 break;
397 case List_kind:
398 e->v.List.ctx = ctx;
399 s = e->v.List.elts;
400 break;
401 case Tuple_kind:
402 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
403 return ast_error(n, "can't assign to ()");
404 e->v.Tuple.ctx = ctx;
405 s = e->v.Tuple.elts;
406 break;
407 case Lambda_kind:
408 expr_name = "lambda";
409 break;
410 case Call_kind:
411 expr_name = "function call";
412 break;
413 case BoolOp_kind:
414 case BinOp_kind:
415 case UnaryOp_kind:
416 expr_name = "operator";
417 break;
418 case GeneratorExp_kind:
419 expr_name = "generator expression";
420 break;
421 case Yield_kind:
422 expr_name = "yield expression";
423 break;
424 case ListComp_kind:
425 expr_name = "list comprehension";
426 break;
427 case Dict_kind:
428 case Num_kind:
429 case Str_kind:
430 expr_name = "literal";
431 break;
432 case Compare_kind:
433 expr_name = "comparison";
434 break;
435 case Repr_kind:
436 expr_name = "repr";
437 break;
438 case IfExp_kind:
439 expr_name = "conditional expression";
440 break;
441 default:
442 PyErr_Format(PyExc_SystemError,
443 "unexpected expression in assignment %d (line %d)",
444 e->kind, e->lineno);
445 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000447 /* Check for error string set by switch */
448 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000449 char buf[300];
450 PyOS_snprintf(buf, sizeof(buf),
451 "can't %s %s",
452 ctx == Store ? "assign to" : "delete",
453 expr_name);
454 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000455 }
456
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000458 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000459 */
460 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000461 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000463 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000464 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000465 return 0;
466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 }
468 return 1;
469}
470
471static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000472ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473{
474 REQ(n, augassign);
475 n = CHILD(n, 0);
476 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000477 case '+':
478 return Add;
479 case '-':
480 return Sub;
481 case '/':
482 if (STR(n)[1] == '/')
483 return FloorDiv;
484 else
485 return Div;
486 case '%':
487 return Mod;
488 case '<':
489 return LShift;
490 case '>':
491 return RShift;
492 case '&':
493 return BitAnd;
494 case '^':
495 return BitXor;
496 case '|':
497 return BitOr;
498 case '*':
499 if (STR(n)[1] == '*')
500 return Pow;
501 else
502 return Mult;
503 default:
504 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
505 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 }
507}
508
509static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000510ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511{
512 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000513 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 */
515 REQ(n, comp_op);
516 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000517 n = CHILD(n, 0);
518 switch (TYPE(n)) {
519 case LESS:
520 return Lt;
521 case GREATER:
522 return Gt;
523 case EQEQUAL: /* == */
524 return Eq;
525 case LESSEQUAL:
526 return LtE;
527 case GREATEREQUAL:
528 return GtE;
529 case NOTEQUAL:
530 return NotEq;
531 case NAME:
532 if (strcmp(STR(n), "in") == 0)
533 return In;
534 if (strcmp(STR(n), "is") == 0)
535 return Is;
536 default:
537 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
538 STR(n));
539 return (cmpop_ty)0;
540 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 }
542 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000543 /* handle "not in" and "is not" */
544 switch (TYPE(CHILD(n, 0))) {
545 case NAME:
546 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
547 return NotIn;
548 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
549 return IsNot;
550 default:
551 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
552 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
553 return (cmpop_ty)0;
554 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 }
Neal Norwitz79792652005-11-14 04:25:03 +0000556 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000557 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000558 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559}
560
561static asdl_seq *
562seq_for_testlist(struct compiling *c, const node *n)
563{
564 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000565 asdl_seq *seq;
566 expr_ty expression;
567 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000568 assert(TYPE(n) == testlist ||
569 TYPE(n) == listmaker ||
570 TYPE(n) == testlist_gexp ||
571 TYPE(n) == testlist_safe ||
572 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000574 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000576 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577
578 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000579 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000581 expression = ast_for_expr(c, CHILD(n, i));
582 if (!expression)
583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000585 assert(i / 2 < seq->size);
586 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587 }
588 return seq;
589}
590
591static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000592compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593{
594 int i, len = (NCH(n) + 1) / 2;
595 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000596 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000598 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599
Neal Norwitz3a230172006-09-22 08:18:10 +0000600 /* fpdef: NAME | '(' fplist ')'
601 fplist: fpdef (',' fpdef)* [',']
602 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000605 const node *fpdef_node = CHILD(n, 2*i);
606 const node *child;
607 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000608set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000609 /* fpdef_node is either a NAME or an fplist */
610 child = CHILD(fpdef_node, 0);
611 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000612 if (!forbidden_check(c, n, STR(child)))
613 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000614 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
615 child->n_col_offset, c->c_arena);
616 }
617 else {
618 assert(TYPE(fpdef_node) == fpdef);
619 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
620 child = CHILD(fpdef_node, 1);
621 assert(TYPE(child) == fplist);
622 /* NCH == 1 means we have (x), we need to elide the extra parens */
623 if (NCH(child) == 1) {
624 fpdef_node = CHILD(child, 0);
625 assert(TYPE(fpdef_node) == fpdef);
626 goto set_name;
627 }
628 arg = compiler_complex_args(c, child);
629 }
630 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 }
632
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000633 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000634 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000635 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 return result;
637}
638
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639
Jeremy Hyltona8293132006-02-28 17:58:27 +0000640/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641
642static arguments_ty
643ast_for_arguments(struct compiling *c, const node *n)
644{
645 /* parameters: '(' [varargslist] ')'
646 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000647 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000649 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 asdl_seq *args, *defaults;
651 identifier vararg = NULL, kwarg = NULL;
652 node *ch;
653
654 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000655 if (NCH(n) == 2) /* () as argument list */
656 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
657 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 }
659 REQ(n, varargslist);
660
661 /* first count the number of normal args & defaults */
662 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000663 ch = CHILD(n, i);
664 if (TYPE(ch) == fpdef)
665 n_args++;
666 if (TYPE(ch) == EQUAL)
667 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000669 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000671 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000672 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000674 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675
676 /* fpdef: NAME | '(' fplist ')'
677 fplist: fpdef (',' fpdef)* [',']
678 */
679 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000680 j = 0; /* index for defaults */
681 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000683 ch = CHILD(n, i);
684 switch (TYPE(ch)) {
685 case fpdef:
686 handle_fpdef:
687 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
688 anything other than EQUAL or a comma? */
689 /* XXX Should NCH(n) check be made a separate check? */
690 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
691 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
692 if (!expression)
693 goto error;
694 assert(defaults != NULL);
695 asdl_seq_SET(defaults, j++, expression);
696 i += 2;
697 found_default = 1;
698 }
699 else if (found_default) {
700 ast_error(n,
701 "non-default argument follows default argument");
702 goto error;
703 }
704 if (NCH(ch) == 3) {
705 ch = CHILD(ch, 1);
706 /* def foo((x)): is not complex, special case. */
707 if (NCH(ch) != 1) {
708 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000709 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
710 "tuple parameter unpacking has been removed in 3.x"))
711 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000712 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000713 if (!asdl_seq_GET(args, k-1))
714 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000715 } else {
716 /* def foo((x)): setup for checking NAME below. */
717 /* Loop because there can be many parens and tuple
718 unpacking mixed in. */
719 ch = CHILD(ch, 0);
720 assert(TYPE(ch) == fpdef);
721 goto handle_fpdef;
722 }
723 }
724 if (TYPE(CHILD(ch, 0)) == NAME) {
725 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000726 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000727 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000728 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
729 Param, LINENO(ch), ch->n_col_offset,
730 c->c_arena);
731 if (!name)
732 goto error;
733 asdl_seq_SET(args, k++, name);
734
735 }
736 i += 2; /* the name and the comma */
737 break;
738 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000739 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000740 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000741 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
742 i += 3;
743 break;
744 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000745 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000746 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000747 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
748 i += 3;
749 break;
750 default:
751 PyErr_Format(PyExc_SystemError,
752 "unexpected node in varargslist: %d @ %d",
753 TYPE(ch), i);
754 goto error;
755 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 }
757
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000758 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759
760 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000761 Py_XDECREF(vararg);
762 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763 return NULL;
764}
765
766static expr_ty
767ast_for_dotted_name(struct compiling *c, const node *n)
768{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000769 expr_ty e;
770 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000771 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 int i;
773
774 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000775
776 lineno = LINENO(n);
777 col_offset = n->n_col_offset;
778
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 id = NEW_IDENTIFIER(CHILD(n, 0));
780 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000781 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000782 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
786 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000787 id = NEW_IDENTIFIER(CHILD(n, i));
788 if (!id)
789 return NULL;
790 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
791 if (!e)
792 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 }
794
795 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796}
797
798static expr_ty
799ast_for_decorator(struct compiling *c, const node *n)
800{
801 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
802 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000803 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804
805 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000806 REQ(CHILD(n, 0), AT);
807 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808
809 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
810 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000811 return NULL;
812
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000814 d = name_expr;
815 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 }
817 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000818 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
819 n->n_col_offset, c->c_arena);
820 if (!d)
821 return NULL;
822 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 }
824 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000825 d = ast_for_call(c, CHILD(n, 3), name_expr);
826 if (!d)
827 return NULL;
828 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 }
830
831 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832}
833
834static asdl_seq*
835ast_for_decorators(struct compiling *c, const node *n)
836{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000837 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000838 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 int i;
840
841 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000842 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000844 return NULL;
845
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000847 d = ast_for_decorator(c, CHILD(n, i));
848 if (!d)
849 return NULL;
850 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 }
852 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853}
854
855static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000856ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857{
Christian Heimes5224d282008-02-23 15:01:05 +0000858 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000859 identifier name;
860 arguments_ty args;
861 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000862 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
864 REQ(n, funcdef);
865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 name = NEW_IDENTIFIER(CHILD(n, name_i));
867 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000868 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000869 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000870 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 args = ast_for_arguments(c, CHILD(n, name_i + 1));
872 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000873 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874 body = ast_for_suite(c, CHILD(n, name_i + 3));
875 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000876 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000878 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000879 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880}
881
Christian Heimes5224d282008-02-23 15:01:05 +0000882static stmt_ty
883ast_for_decorated(struct compiling *c, const node *n)
884{
885 /* decorated: decorators (classdef | funcdef) */
886 stmt_ty thing = NULL;
887 asdl_seq *decorator_seq = NULL;
888
889 REQ(n, decorated);
890
891 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
892 if (!decorator_seq)
893 return NULL;
894
895 assert(TYPE(CHILD(n, 1)) == funcdef ||
896 TYPE(CHILD(n, 1)) == classdef);
897
898 if (TYPE(CHILD(n, 1)) == funcdef) {
899 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
900 } else if (TYPE(CHILD(n, 1)) == classdef) {
901 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
902 }
903 /* we count the decorators in when talking about the class' or
904 function's line number */
905 if (thing) {
906 thing->lineno = LINENO(n);
907 thing->col_offset = n->n_col_offset;
908 }
909 return thing;
910}
911
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912static expr_ty
913ast_for_lambdef(struct compiling *c, const node *n)
914{
915 /* lambdef: 'lambda' [varargslist] ':' test */
916 arguments_ty args;
917 expr_ty expression;
918
919 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000920 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
921 if (!args)
922 return NULL;
923 expression = ast_for_expr(c, CHILD(n, 2));
924 if (!expression)
925 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 }
927 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000928 args = ast_for_arguments(c, CHILD(n, 1));
929 if (!args)
930 return NULL;
931 expression = ast_for_expr(c, CHILD(n, 3));
932 if (!expression)
933 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934 }
935
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000936 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937}
938
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000939static expr_ty
940ast_for_ifexpr(struct compiling *c, const node *n)
941{
942 /* test: or_test 'if' or_test 'else' test */
943 expr_ty expression, body, orelse;
944
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000945 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000946 body = ast_for_expr(c, CHILD(n, 0));
947 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000948 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000949 expression = ast_for_expr(c, CHILD(n, 2));
950 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000951 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000952 orelse = ast_for_expr(c, CHILD(n, 4));
953 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000954 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000955 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000956 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000957}
958
Neal Norwitze4d4f002006-09-05 03:58:26 +0000959/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
960 so there is only a single version. Possibly for loops can also re-use
961 the code.
962*/
963
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964/* Count the number of 'for' loop in a list comprehension.
965
966 Helper for ast_for_listcomp().
967*/
968
969static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000970count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971{
972 int n_fors = 0;
973 node *ch = CHILD(n, 1);
974
975 count_list_for:
976 n_fors++;
977 REQ(ch, list_for);
978 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000979 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000981 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 count_list_iter:
983 REQ(ch, list_iter);
984 ch = CHILD(ch, 0);
985 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000986 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000988 if (NCH(ch) == 3) {
989 ch = CHILD(ch, 2);
990 goto count_list_iter;
991 }
992 else
993 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000995
996 /* Should never be reached */
997 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
998 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999}
1000
1001/* Count the number of 'if' statements in a list comprehension.
1002
1003 Helper for ast_for_listcomp().
1004*/
1005
1006static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001007count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008{
1009 int n_ifs = 0;
1010
1011 count_list_iter:
1012 REQ(n, list_iter);
1013 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001014 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 n = CHILD(n, 0);
1016 REQ(n, list_if);
1017 n_ifs++;
1018 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001019 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 n = CHILD(n, 2);
1021 goto count_list_iter;
1022}
1023
1024static expr_ty
1025ast_for_listcomp(struct compiling *c, const node *n)
1026{
1027 /* listmaker: test ( list_for | (',' test)* [','] )
1028 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1029 list_iter: list_for | list_if
1030 list_if: 'if' test [list_iter]
1031 testlist_safe: test [(',' test)+ [',']]
1032 */
1033 expr_ty elt;
1034 asdl_seq *listcomps;
1035 int i, n_fors;
1036 node *ch;
1037
1038 REQ(n, listmaker);
1039 assert(NCH(n) > 1);
1040
1041 elt = ast_for_expr(c, CHILD(n, 0));
1042 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001043 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001045 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001049 listcomps = asdl_seq_new(n_fors, c->c_arena);
1050 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001051 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 ch = CHILD(n, 1);
1054 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001055 comprehension_ty lc;
1056 asdl_seq *t;
1057 expr_ty expression;
1058 node *for_ch;
1059
1060 REQ(ch, list_for);
1061
1062 for_ch = CHILD(ch, 1);
1063 t = ast_for_exprlist(c, for_ch, Store);
1064 if (!t)
1065 return NULL;
1066 expression = ast_for_testlist(c, CHILD(ch, 3));
1067 if (!expression)
1068 return NULL;
1069
1070 /* Check the # of children rather than the length of t, since
1071 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1072 */
1073 if (NCH(for_ch) == 1)
1074 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1075 c->c_arena);
1076 else
1077 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1078 c->c_arena),
1079 expression, NULL, c->c_arena);
1080 if (!lc)
1081 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001083 if (NCH(ch) == 5) {
1084 int j, n_ifs;
1085 asdl_seq *ifs;
1086 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001088 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001089 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001090 if (n_ifs == -1)
1091 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001093 ifs = asdl_seq_new(n_ifs, c->c_arena);
1094 if (!ifs)
1095 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001097 for (j = 0; j < n_ifs; j++) {
1098 REQ(ch, list_iter);
1099 ch = CHILD(ch, 0);
1100 REQ(ch, list_if);
1101
1102 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1103 if (!list_for_expr)
1104 return NULL;
1105
1106 asdl_seq_SET(ifs, j, list_for_expr);
1107 if (NCH(ch) == 3)
1108 ch = CHILD(ch, 2);
1109 }
1110 /* on exit, must guarantee that ch is a list_for */
1111 if (TYPE(ch) == list_iter)
1112 ch = CHILD(ch, 0);
1113 lc->ifs = ifs;
1114 }
1115 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 }
1117
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001118 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119}
1120
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001121/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122
1123 Helper for ast_for_genexp().
1124*/
1125
1126static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001127count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001129 int n_fors = 0;
1130 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131
1132 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001133 n_fors++;
1134 REQ(ch, gen_for);
1135 if (NCH(ch) == 5)
1136 ch = CHILD(ch, 4);
1137 else
1138 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001140 REQ(ch, gen_iter);
1141 ch = CHILD(ch, 0);
1142 if (TYPE(ch) == gen_for)
1143 goto count_gen_for;
1144 else if (TYPE(ch) == gen_if) {
1145 if (NCH(ch) == 3) {
1146 ch = CHILD(ch, 2);
1147 goto count_gen_iter;
1148 }
1149 else
1150 return n_fors;
1151 }
1152
1153 /* Should never be reached */
1154 PyErr_SetString(PyExc_SystemError,
1155 "logic error in count_gen_fors");
1156 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157}
1158
1159/* Count the number of 'if' statements in a generator expression.
1160
1161 Helper for ast_for_genexp().
1162*/
1163
1164static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001165count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001167 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001169 while (1) {
1170 REQ(n, gen_iter);
1171 if (TYPE(CHILD(n, 0)) == gen_for)
1172 return n_ifs;
1173 n = CHILD(n, 0);
1174 REQ(n, gen_if);
1175 n_ifs++;
1176 if (NCH(n) == 2)
1177 return n_ifs;
1178 n = CHILD(n, 2);
1179 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180}
1181
Jeremy Hyltona8293132006-02-28 17:58:27 +00001182/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183static expr_ty
1184ast_for_genexp(struct compiling *c, const node *n)
1185{
1186 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001187 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 expr_ty elt;
1189 asdl_seq *genexps;
1190 int i, n_fors;
1191 node *ch;
1192
1193 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1194 assert(NCH(n) > 1);
1195
1196 elt = ast_for_expr(c, CHILD(n, 0));
1197 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001200 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001202 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001203
1204 genexps = asdl_seq_new(n_fors, c->c_arena);
1205 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001206 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001207
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 ch = CHILD(n, 1);
1209 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001210 comprehension_ty ge;
1211 asdl_seq *t;
1212 expr_ty expression;
1213 node *for_ch;
1214
1215 REQ(ch, gen_for);
1216
1217 for_ch = CHILD(ch, 1);
1218 t = ast_for_exprlist(c, for_ch, Store);
1219 if (!t)
1220 return NULL;
1221 expression = ast_for_expr(c, CHILD(ch, 3));
1222 if (!expression)
1223 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001224
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001225 /* Check the # of children rather than the length of t, since
1226 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1227 if (NCH(for_ch) == 1)
1228 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1229 NULL, c->c_arena);
1230 else
1231 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1232 c->c_arena),
1233 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001234
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001235 if (!ge)
1236 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001237
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001238 if (NCH(ch) == 5) {
1239 int j, n_ifs;
1240 asdl_seq *ifs;
1241
1242 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001243 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001244 if (n_ifs == -1)
1245 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001246
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001247 ifs = asdl_seq_new(n_ifs, c->c_arena);
1248 if (!ifs)
1249 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001250
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001251 for (j = 0; j < n_ifs; j++) {
1252 REQ(ch, gen_iter);
1253 ch = CHILD(ch, 0);
1254 REQ(ch, gen_if);
1255
1256 expression = ast_for_expr(c, CHILD(ch, 1));
1257 if (!expression)
1258 return NULL;
1259 asdl_seq_SET(ifs, j, expression);
1260 if (NCH(ch) == 3)
1261 ch = CHILD(ch, 2);
1262 }
1263 /* on exit, must guarantee that ch is a gen_for */
1264 if (TYPE(ch) == gen_iter)
1265 ch = CHILD(ch, 0);
1266 ge->ifs = ifs;
1267 }
1268 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 }
1270
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001271 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272}
1273
1274static expr_ty
1275ast_for_atom(struct compiling *c, const node *n)
1276{
1277 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1278 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1279 */
1280 node *ch = CHILD(n, 0);
1281
1282 switch (TYPE(ch)) {
1283 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001284 /* All names start in Load context, but may later be
1285 changed. */
1286 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1287 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001289 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001290 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001291#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001292 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1293 PyObject *type, *value, *tback, *errstr;
1294 PyErr_Fetch(&type, &value, &tback);
1295 errstr = ((PyUnicodeErrorObject *)value)->reason;
1296 if (errstr) {
1297 char *s = "";
1298 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001299 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001300 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1301 ast_error(n, buf);
1302 } else {
1303 ast_error(n, "(unicode error) unknown error");
1304 }
1305 Py_DECREF(type);
1306 Py_DECREF(value);
1307 Py_XDECREF(tback);
1308 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001309#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001310 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001311 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001312 PyArena_AddPyObject(c->c_arena, str);
1313 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 }
1315 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001316 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001317 if (!pynum)
1318 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001319
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001320 PyArena_AddPyObject(c->c_arena, pynum);
1321 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322 }
1323 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001324 ch = CHILD(n, 1);
1325
1326 if (TYPE(ch) == RPAR)
1327 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1328
1329 if (TYPE(ch) == yield_expr)
1330 return ast_for_expr(c, ch);
1331
1332 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1333 return ast_for_genexp(c, ch);
1334
1335 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001337 ch = CHILD(n, 1);
1338
1339 if (TYPE(ch) == RSQB)
1340 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1341
1342 REQ(ch, listmaker);
1343 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1344 asdl_seq *elts = seq_for_testlist(c, ch);
1345 if (!elts)
1346 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001347
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001348 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1349 }
1350 else
1351 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001353 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1354 int i, size;
1355 asdl_seq *keys, *values;
1356
1357 ch = CHILD(n, 1);
1358 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1359 keys = asdl_seq_new(size, c->c_arena);
1360 if (!keys)
1361 return NULL;
1362
1363 values = asdl_seq_new(size, c->c_arena);
1364 if (!values)
1365 return NULL;
1366
1367 for (i = 0; i < NCH(ch); i += 4) {
1368 expr_ty expression;
1369
1370 expression = ast_for_expr(c, CHILD(ch, i));
1371 if (!expression)
1372 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001373
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001374 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001375
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001376 expression = ast_for_expr(c, CHILD(ch, i + 2));
1377 if (!expression)
1378 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001379
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001380 asdl_seq_SET(values, i / 4, expression);
1381 }
1382 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 }
1384 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001385 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001386 if (Py_Py3kWarningFlag &&
1387 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001388 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001389 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001390 if (!expression)
1391 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001392
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001393 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 }
1395 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001396 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1397 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 }
1399}
1400
1401static slice_ty
1402ast_for_slice(struct compiling *c, const node *n)
1403{
1404 node *ch;
1405 expr_ty lower = NULL, upper = NULL, step = NULL;
1406
1407 REQ(n, subscript);
1408
1409 /*
1410 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1411 sliceop: ':' [test]
1412 */
1413 ch = CHILD(n, 0);
1414 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001415 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416
1417 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001418 /* 'step' variable hold no significance in terms of being used over
1419 other vars */
1420 step = ast_for_expr(c, ch);
1421 if (!step)
1422 return NULL;
1423
1424 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 }
1426
1427 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001428 lower = ast_for_expr(c, ch);
1429 if (!lower)
1430 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 }
1432
1433 /* If there's an upper bound it's in the second or third position. */
1434 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001435 if (NCH(n) > 1) {
1436 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001438 if (TYPE(n2) == test) {
1439 upper = ast_for_expr(c, n2);
1440 if (!upper)
1441 return NULL;
1442 }
1443 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001445 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001447 if (TYPE(n2) == test) {
1448 upper = ast_for_expr(c, n2);
1449 if (!upper)
1450 return NULL;
1451 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452 }
1453
1454 ch = CHILD(n, NCH(n) - 1);
1455 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001456 if (NCH(ch) == 1) {
1457 /* No expression, so step is None */
1458 ch = CHILD(ch, 0);
1459 step = Name(new_identifier("None", c->c_arena), Load,
1460 LINENO(ch), ch->n_col_offset, c->c_arena);
1461 if (!step)
1462 return NULL;
1463 } else {
1464 ch = CHILD(ch, 1);
1465 if (TYPE(ch) == test) {
1466 step = ast_for_expr(c, ch);
1467 if (!step)
1468 return NULL;
1469 }
1470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 }
1472
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001473 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474}
1475
1476static expr_ty
1477ast_for_binop(struct compiling *c, const node *n)
1478{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001479 /* Must account for a sequence of expressions.
1480 How should A op B op C by represented?
1481 BinOp(BinOp(A, op, B), op, C).
1482 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001484 int i, nops;
1485 expr_ty expr1, expr2, result;
1486 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001488 expr1 = ast_for_expr(c, CHILD(n, 0));
1489 if (!expr1)
1490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001492 expr2 = ast_for_expr(c, CHILD(n, 2));
1493 if (!expr2)
1494 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001496 newoperator = get_operator(CHILD(n, 1));
1497 if (!newoperator)
1498 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001500 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1501 c->c_arena);
1502 if (!result)
1503 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001505 nops = (NCH(n) - 1) / 2;
1506 for (i = 1; i < nops; i++) {
1507 expr_ty tmp_result, tmp;
1508 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001510 newoperator = get_operator(next_oper);
1511 if (!newoperator)
1512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001514 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1515 if (!tmp)
1516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001518 tmp_result = BinOp(result, newoperator, tmp,
1519 LINENO(next_oper), next_oper->n_col_offset,
1520 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001521 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001522 return NULL;
1523 result = tmp_result;
1524 }
1525 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526}
1527
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001528static expr_ty
1529ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1530{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001531 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1532 subscriptlist: subscript (',' subscript)* [',']
1533 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1534 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001535 REQ(n, trailer);
1536 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001537 if (NCH(n) == 2)
1538 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1539 n->n_col_offset, c->c_arena);
1540 else
1541 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001542 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001543 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001544 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1545 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001546 }
1547 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001548 REQ(CHILD(n, 0), LSQB);
1549 REQ(CHILD(n, 2), RSQB);
1550 n = CHILD(n, 1);
1551 if (NCH(n) == 1) {
1552 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1553 if (!slc)
1554 return NULL;
1555 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1556 c->c_arena);
1557 }
1558 else {
1559 /* The grammar is ambiguous here. The ambiguity is resolved
1560 by treating the sequence as a tuple literal if there are
1561 no slice features.
1562 */
1563 int j;
1564 slice_ty slc;
1565 expr_ty e;
1566 bool simple = true;
1567 asdl_seq *slices, *elts;
1568 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1569 if (!slices)
1570 return NULL;
1571 for (j = 0; j < NCH(n); j += 2) {
1572 slc = ast_for_slice(c, CHILD(n, j));
1573 if (!slc)
1574 return NULL;
1575 if (slc->kind != Index_kind)
1576 simple = false;
1577 asdl_seq_SET(slices, j / 2, slc);
1578 }
1579 if (!simple) {
1580 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1581 Load, LINENO(n), n->n_col_offset, c->c_arena);
1582 }
1583 /* extract Index values and put them in a Tuple */
1584 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1585 if (!elts)
1586 return NULL;
1587 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1588 slc = (slice_ty)asdl_seq_GET(slices, j);
1589 assert(slc->kind == Index_kind && slc->v.Index.value);
1590 asdl_seq_SET(elts, j, slc->v.Index.value);
1591 }
1592 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1593 if (!e)
1594 return NULL;
1595 return Subscript(left_expr, Index(e, c->c_arena),
1596 Load, LINENO(n), n->n_col_offset, c->c_arena);
1597 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001598 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001599}
1600
1601static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001602ast_for_factor(struct compiling *c, const node *n)
1603{
1604 node *pfactor, *ppower, *patom, *pnum;
1605 expr_ty expression;
1606
1607 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001608 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001609 constant. The peephole optimizer already does something like
1610 this but it doesn't handle the case where the constant is
1611 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1612 PyLongObject.
1613 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001614 if (TYPE(CHILD(n, 0)) == MINUS &&
1615 NCH(n) == 2 &&
1616 TYPE((pfactor = CHILD(n, 1))) == factor &&
1617 NCH(pfactor) == 1 &&
1618 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1619 NCH(ppower) == 1 &&
1620 TYPE((patom = CHILD(ppower, 0))) == atom &&
1621 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1622 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1623 if (s == NULL)
1624 return NULL;
1625 s[0] = '-';
1626 strcpy(s + 1, STR(pnum));
1627 PyObject_FREE(STR(pnum));
1628 STR(pnum) = s;
1629 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001630 }
1631
1632 expression = ast_for_expr(c, CHILD(n, 1));
1633 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001634 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001635
1636 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001637 case PLUS:
1638 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1639 c->c_arena);
1640 case MINUS:
1641 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1642 c->c_arena);
1643 case TILDE:
1644 return UnaryOp(Invert, expression, LINENO(n),
1645 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001646 }
1647 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001648 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001649 return NULL;
1650}
1651
1652static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001653ast_for_power(struct compiling *c, const node *n)
1654{
1655 /* power: atom trailer* ('**' factor)*
1656 */
1657 int i;
1658 expr_ty e, tmp;
1659 REQ(n, power);
1660 e = ast_for_atom(c, CHILD(n, 0));
1661 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001662 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001663 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001664 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001665 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001666 node *ch = CHILD(n, i);
1667 if (TYPE(ch) != trailer)
1668 break;
1669 tmp = ast_for_trailer(c, ch, e);
1670 if (!tmp)
1671 return NULL;
1672 tmp->lineno = e->lineno;
1673 tmp->col_offset = e->col_offset;
1674 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001675 }
1676 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001677 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1678 if (!f)
1679 return NULL;
1680 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1681 if (!tmp)
1682 return NULL;
1683 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001684 }
1685 return e;
1686}
1687
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688/* Do not name a variable 'expr'! Will cause a compile error.
1689*/
1690
1691static expr_ty
1692ast_for_expr(struct compiling *c, const node *n)
1693{
1694 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001695 test: or_test ['if' or_test 'else' test] | lambdef
1696 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 and_test: not_test ('and' not_test)*
1698 not_test: 'not' not_test | comparison
1699 comparison: expr (comp_op expr)*
1700 expr: xor_expr ('|' xor_expr)*
1701 xor_expr: and_expr ('^' and_expr)*
1702 and_expr: shift_expr ('&' shift_expr)*
1703 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1704 arith_expr: term (('+'|'-') term)*
1705 term: factor (('*'|'/'|'%'|'//') factor)*
1706 factor: ('+'|'-'|'~') factor | power
1707 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001708
1709 As well as modified versions that exist for backward compatibility,
1710 to explicitly allow:
1711 [ x for x in lambda: 0, lambda: 1 ]
1712 (which would be ambiguous without these extra rules)
1713
1714 old_test: or_test | old_lambdef
1715 old_lambdef: 'lambda' [vararglist] ':' old_test
1716
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 */
1718
1719 asdl_seq *seq;
1720 int i;
1721
1722 loop:
1723 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001724 case test:
1725 case old_test:
1726 if (TYPE(CHILD(n, 0)) == lambdef ||
1727 TYPE(CHILD(n, 0)) == old_lambdef)
1728 return ast_for_lambdef(c, CHILD(n, 0));
1729 else if (NCH(n) > 1)
1730 return ast_for_ifexpr(c, n);
1731 /* Fallthrough */
1732 case or_test:
1733 case and_test:
1734 if (NCH(n) == 1) {
1735 n = CHILD(n, 0);
1736 goto loop;
1737 }
1738 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1739 if (!seq)
1740 return NULL;
1741 for (i = 0; i < NCH(n); i += 2) {
1742 expr_ty e = ast_for_expr(c, CHILD(n, i));
1743 if (!e)
1744 return NULL;
1745 asdl_seq_SET(seq, i / 2, e);
1746 }
1747 if (!strcmp(STR(CHILD(n, 1)), "and"))
1748 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1749 c->c_arena);
1750 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1751 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1752 case not_test:
1753 if (NCH(n) == 1) {
1754 n = CHILD(n, 0);
1755 goto loop;
1756 }
1757 else {
1758 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1759 if (!expression)
1760 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001762 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1763 c->c_arena);
1764 }
1765 case comparison:
1766 if (NCH(n) == 1) {
1767 n = CHILD(n, 0);
1768 goto loop;
1769 }
1770 else {
1771 expr_ty expression;
1772 asdl_int_seq *ops;
1773 asdl_seq *cmps;
1774 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1775 if (!ops)
1776 return NULL;
1777 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1778 if (!cmps) {
1779 return NULL;
1780 }
1781 for (i = 1; i < NCH(n); i += 2) {
1782 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001784 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001785 if (!newoperator) {
1786 return NULL;
1787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001789 expression = ast_for_expr(c, CHILD(n, i + 1));
1790 if (!expression) {
1791 return NULL;
1792 }
1793
1794 asdl_seq_SET(ops, i / 2, newoperator);
1795 asdl_seq_SET(cmps, i / 2, expression);
1796 }
1797 expression = ast_for_expr(c, CHILD(n, 0));
1798 if (!expression) {
1799 return NULL;
1800 }
1801
1802 return Compare(expression, ops, cmps, LINENO(n),
1803 n->n_col_offset, c->c_arena);
1804 }
1805 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001807 /* The next five cases all handle BinOps. The main body of code
1808 is the same in each case, but the switch turned inside out to
1809 reuse the code for each type of operator.
1810 */
1811 case expr:
1812 case xor_expr:
1813 case and_expr:
1814 case shift_expr:
1815 case arith_expr:
1816 case term:
1817 if (NCH(n) == 1) {
1818 n = CHILD(n, 0);
1819 goto loop;
1820 }
1821 return ast_for_binop(c, n);
1822 case yield_expr: {
1823 expr_ty exp = NULL;
1824 if (NCH(n) == 2) {
1825 exp = ast_for_testlist(c, CHILD(n, 1));
1826 if (!exp)
1827 return NULL;
1828 }
1829 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1830 }
1831 case factor:
1832 if (NCH(n) == 1) {
1833 n = CHILD(n, 0);
1834 goto loop;
1835 }
1836 return ast_for_factor(c, n);
1837 case power:
1838 return ast_for_power(c, n);
1839 default:
1840 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1841 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001843 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 return NULL;
1845}
1846
1847static expr_ty
1848ast_for_call(struct compiling *c, const node *n, expr_ty func)
1849{
1850 /*
1851 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001852 | '**' test)
1853 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 */
1855
1856 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001857 asdl_seq *args;
1858 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 expr_ty vararg = NULL, kwarg = NULL;
1860
1861 REQ(n, arglist);
1862
1863 nargs = 0;
1864 nkeywords = 0;
1865 ngens = 0;
1866 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001867 node *ch = CHILD(n, i);
1868 if (TYPE(ch) == argument) {
1869 if (NCH(ch) == 1)
1870 nargs++;
1871 else if (TYPE(CHILD(ch, 1)) == gen_for)
1872 ngens++;
1873 else
1874 nkeywords++;
1875 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 }
1877 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001878 ast_error(n, "Generator expression must be parenthesized "
1879 "if not sole argument");
1880 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 }
1882
1883 if (nargs + nkeywords + ngens > 255) {
1884 ast_error(n, "more than 255 arguments");
1885 return NULL;
1886 }
1887
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001888 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001890 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001893 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 nargs = 0;
1895 nkeywords = 0;
1896 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001897 node *ch = CHILD(n, i);
1898 if (TYPE(ch) == argument) {
1899 expr_ty e;
1900 if (NCH(ch) == 1) {
1901 if (nkeywords) {
1902 ast_error(CHILD(ch, 0),
1903 "non-keyword arg after keyword arg");
1904 return NULL;
1905 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001906 if (vararg) {
1907 ast_error(CHILD(ch, 0),
1908 "only named arguments may follow *expression");
1909 return NULL;
1910 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001911 e = ast_for_expr(c, CHILD(ch, 0));
1912 if (!e)
1913 return NULL;
1914 asdl_seq_SET(args, nargs++, e);
1915 }
1916 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1917 e = ast_for_genexp(c, ch);
1918 if (!e)
1919 return NULL;
1920 asdl_seq_SET(args, nargs++, e);
1921 }
1922 else {
1923 keyword_ty kw;
1924 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001925 int k;
1926 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001928 /* CHILD(ch, 0) is test, but must be an identifier? */
1929 e = ast_for_expr(c, CHILD(ch, 0));
1930 if (!e)
1931 return NULL;
1932 /* f(lambda x: x[0] = 3) ends up getting parsed with
1933 * LHS test = lambda x: x[0], and RHS test = 3.
1934 * SF bug 132313 points out that complaining about a keyword
1935 * then is very confusing.
1936 */
1937 if (e->kind == Lambda_kind) {
1938 ast_error(CHILD(ch, 0),
1939 "lambda cannot contain assignment");
1940 return NULL;
1941 } else if (e->kind != Name_kind) {
1942 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1943 return NULL;
1944 }
1945 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001946 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001947 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001948 for (k = 0; k < nkeywords; k++) {
1949 tmp = PyString_AS_STRING(
1950 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
1951 if (!strcmp(tmp, PyString_AS_STRING(key))) {
1952 ast_error(CHILD(ch, 0), "keyword argument repeated");
1953 return NULL;
1954 }
1955 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001956 e = ast_for_expr(c, CHILD(ch, 2));
1957 if (!e)
1958 return NULL;
1959 kw = keyword(key, e, c->c_arena);
1960 if (!kw)
1961 return NULL;
1962 asdl_seq_SET(keywords, nkeywords++, kw);
1963 }
1964 }
1965 else if (TYPE(ch) == STAR) {
1966 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001967 if (!vararg)
1968 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001969 i++;
1970 }
1971 else if (TYPE(ch) == DOUBLESTAR) {
1972 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001973 if (!kwarg)
1974 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001975 i++;
1976 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 }
1978
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001979 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1980 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981}
1982
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001984ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001986 /* testlist_gexp: test (',' test)* [','] */
1987 /* testlist: test (',' test)* [','] */
1988 /* testlist_safe: test (',' test)+ [','] */
1989 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001991 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001992 if (NCH(n) > 1)
1993 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001994 }
1995 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001996 assert(TYPE(n) == testlist ||
1997 TYPE(n) == testlist_safe ||
1998 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001999 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002001 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002003 asdl_seq *tmp = seq_for_testlist(c, n);
2004 if (!tmp)
2005 return NULL;
2006 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002008}
2009
2010static expr_ty
2011ast_for_testlist_gexp(struct compiling *c, const node* n)
2012{
2013 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2014 /* argument: test [ gen_for ] */
2015 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002016 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002017 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002018 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002019}
2020
2021/* like ast_for_testlist() but returns a sequence */
2022static asdl_seq*
2023ast_for_class_bases(struct compiling *c, const node* n)
2024{
2025 /* testlist: test (',' test)* [','] */
2026 assert(NCH(n) > 0);
2027 REQ(n, testlist);
2028 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002029 expr_ty base;
2030 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2031 if (!bases)
2032 return NULL;
2033 base = ast_for_expr(c, CHILD(n, 0));
2034 if (!base)
2035 return NULL;
2036 asdl_seq_SET(bases, 0, base);
2037 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002038 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002039
2040 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041}
2042
2043static stmt_ty
2044ast_for_expr_stmt(struct compiling *c, const node *n)
2045{
2046 REQ(n, expr_stmt);
2047 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002048 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 testlist: test (',' test)* [',']
2050 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002051 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 test: ... here starts the operator precendence dance
2053 */
2054
2055 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002056 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2057 if (!e)
2058 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002060 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 }
2062 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002063 expr_ty expr1, expr2;
2064 operator_ty newoperator;
2065 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002067 expr1 = ast_for_testlist(c, ch);
2068 if (!expr1)
2069 return NULL;
2070 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2071 switch (expr1->kind) {
2072 case GeneratorExp_kind:
2073 ast_error(ch, "augmented assignment to generator "
2074 "expression not possible");
2075 return NULL;
2076 case Yield_kind:
2077 ast_error(ch, "augmented assignment to yield "
2078 "expression not possible");
2079 return NULL;
2080 case Name_kind: {
Christian Heimes593daf52008-05-26 12:51:38 +00002081 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
Benjamin Petersond5efd202008-06-08 22:52:37 +00002082 if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') &&
2083 !forbidden_check(c, ch, var_name))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002084 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002085 break;
2086 }
2087 case Attribute_kind:
2088 case Subscript_kind:
2089 break;
2090 default:
2091 ast_error(ch, "illegal expression for augmented "
2092 "assignment");
2093 return NULL;
2094 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002095 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002096 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002098 ch = CHILD(n, 2);
2099 if (TYPE(ch) == testlist)
2100 expr2 = ast_for_testlist(c, ch);
2101 else
2102 expr2 = ast_for_expr(c, ch);
2103 if (!expr2)
2104 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002106 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002107 if (!newoperator)
2108 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002110 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2111 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 }
2113 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002114 int i;
2115 asdl_seq *targets;
2116 node *value;
2117 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002119 /* a normal assignment */
2120 REQ(CHILD(n, 1), EQUAL);
2121 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2122 if (!targets)
2123 return NULL;
2124 for (i = 0; i < NCH(n) - 2; i += 2) {
2125 expr_ty e;
2126 node *ch = CHILD(n, i);
2127 if (TYPE(ch) == yield_expr) {
2128 ast_error(ch, "assignment to yield expression not possible");
2129 return NULL;
2130 }
2131 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002133 /* set context to assign */
2134 if (!e)
2135 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002137 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002138 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002140 asdl_seq_SET(targets, i / 2, e);
2141 }
2142 value = CHILD(n, NCH(n) - 1);
2143 if (TYPE(value) == testlist)
2144 expression = ast_for_testlist(c, value);
2145 else
2146 expression = ast_for_expr(c, value);
2147 if (!expression)
2148 return NULL;
2149 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2150 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152}
2153
2154static stmt_ty
2155ast_for_print_stmt(struct compiling *c, const node *n)
2156{
2157 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002158 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 */
2160 expr_ty dest = NULL, expression;
2161 asdl_seq *seq;
2162 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002163 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164
2165 REQ(n, print_stmt);
2166 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002167 dest = ast_for_expr(c, CHILD(n, 2));
2168 if (!dest)
2169 return NULL;
2170 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002172 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002174 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002175 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002176 expression = ast_for_expr(c, CHILD(n, i));
2177 if (!expression)
2178 return NULL;
2179 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 }
2181 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002182 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183}
2184
2185static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002186ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187{
2188 asdl_seq *seq;
2189 int i;
2190 expr_ty e;
2191
2192 REQ(n, exprlist);
2193
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002194 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002196 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002198 e = ast_for_expr(c, CHILD(n, i));
2199 if (!e)
2200 return NULL;
2201 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002202 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 }
2205 return seq;
2206}
2207
2208static stmt_ty
2209ast_for_del_stmt(struct compiling *c, const node *n)
2210{
2211 asdl_seq *expr_list;
2212
2213 /* del_stmt: 'del' exprlist */
2214 REQ(n, del_stmt);
2215
2216 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2217 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002218 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002219 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220}
2221
2222static stmt_ty
2223ast_for_flow_stmt(struct compiling *c, const node *n)
2224{
2225 /*
2226 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002227 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 break_stmt: 'break'
2229 continue_stmt: 'continue'
2230 return_stmt: 'return' [testlist]
2231 yield_stmt: yield_expr
2232 yield_expr: 'yield' testlist
2233 raise_stmt: 'raise' [test [',' test [',' test]]]
2234 */
2235 node *ch;
2236
2237 REQ(n, flow_stmt);
2238 ch = CHILD(n, 0);
2239 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002240 case break_stmt:
2241 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2242 case continue_stmt:
2243 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2244 case yield_stmt: { /* will reduce to yield_expr */
2245 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2246 if (!exp)
2247 return NULL;
2248 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2249 }
2250 case return_stmt:
2251 if (NCH(ch) == 1)
2252 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2253 else {
2254 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2255 if (!expression)
2256 return NULL;
2257 return Return(expression, LINENO(n), n->n_col_offset,
2258 c->c_arena);
2259 }
2260 case raise_stmt:
2261 if (NCH(ch) == 1)
2262 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2263 c->c_arena);
2264 else if (NCH(ch) == 2) {
2265 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2266 if (!expression)
2267 return NULL;
2268 return Raise(expression, NULL, NULL, LINENO(n),
2269 n->n_col_offset, c->c_arena);
2270 }
2271 else if (NCH(ch) == 4) {
2272 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002274 expr1 = ast_for_expr(c, CHILD(ch, 1));
2275 if (!expr1)
2276 return NULL;
2277 expr2 = ast_for_expr(c, CHILD(ch, 3));
2278 if (!expr2)
2279 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002281 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2282 c->c_arena);
2283 }
2284 else if (NCH(ch) == 6) {
2285 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002287 expr1 = ast_for_expr(c, CHILD(ch, 1));
2288 if (!expr1)
2289 return NULL;
2290 expr2 = ast_for_expr(c, CHILD(ch, 3));
2291 if (!expr2)
2292 return NULL;
2293 expr3 = ast_for_expr(c, CHILD(ch, 5));
2294 if (!expr3)
2295 return NULL;
2296
2297 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2298 c->c_arena);
2299 }
2300 default:
2301 PyErr_Format(PyExc_SystemError,
2302 "unexpected flow_stmt: %d", TYPE(ch));
2303 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002305
2306 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308}
2309
2310static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002311alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312{
2313 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002314 import_as_name: NAME ['as' NAME]
2315 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 dotted_name: NAME ('.' NAME)*
2317 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002318 PyObject *str;
2319
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 loop:
2321 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002322 case import_as_name:
2323 str = NULL;
2324 if (NCH(n) == 3) {
2325 str = NEW_IDENTIFIER(CHILD(n, 2));
2326 }
2327 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2328 case dotted_as_name:
2329 if (NCH(n) == 1) {
2330 n = CHILD(n, 0);
2331 goto loop;
2332 }
2333 else {
2334 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2335 if (!a)
2336 return NULL;
2337 assert(!a->asname);
2338 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2339 return a;
2340 }
2341 break;
2342 case dotted_name:
2343 if (NCH(n) == 1)
2344 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2345 else {
2346 /* Create a string of the form "a.b.c" */
2347 int i;
2348 size_t len;
2349 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002351 len = 0;
2352 for (i = 0; i < NCH(n); i += 2)
2353 /* length of string plus one for the dot */
2354 len += strlen(STR(CHILD(n, i))) + 1;
2355 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002356 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002357 if (!str)
2358 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002359 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002360 if (!s)
2361 return NULL;
2362 for (i = 0; i < NCH(n); i += 2) {
2363 char *sch = STR(CHILD(n, i));
2364 strcpy(s, STR(CHILD(n, i)));
2365 s += strlen(sch);
2366 *s++ = '.';
2367 }
2368 --s;
2369 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002370 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002371 PyArena_AddPyObject(c->c_arena, str);
2372 return alias(str, NULL, c->c_arena);
2373 }
2374 break;
2375 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002376 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002377 PyArena_AddPyObject(c->c_arena, str);
2378 return alias(str, NULL, c->c_arena);
2379 default:
2380 PyErr_Format(PyExc_SystemError,
2381 "unexpected import name: %d", TYPE(n));
2382 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002384
2385 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 return NULL;
2387}
2388
2389static stmt_ty
2390ast_for_import_stmt(struct compiling *c, const node *n)
2391{
2392 /*
2393 import_stmt: import_name | import_from
2394 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002395 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002396 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002398 int lineno;
2399 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400 int i;
2401 asdl_seq *aliases;
2402
2403 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002404 lineno = LINENO(n);
2405 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002407 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002408 n = CHILD(n, 1);
2409 REQ(n, dotted_as_names);
2410 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2411 if (!aliases)
2412 return NULL;
2413 for (i = 0; i < NCH(n); i += 2) {
2414 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2415 if (!import_alias)
2416 return NULL;
2417 asdl_seq_SET(aliases, i / 2, import_alias);
2418 }
2419 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002421 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002422 int n_children;
2423 int idx, ndots = 0;
2424 alias_ty mod = NULL;
2425 identifier modname;
2426
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002427 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002428 optional module name */
2429 for (idx = 1; idx < NCH(n); idx++) {
2430 if (TYPE(CHILD(n, idx)) == dotted_name) {
2431 mod = alias_for_import_name(c, CHILD(n, idx));
2432 idx++;
2433 break;
2434 } else if (TYPE(CHILD(n, idx)) != DOT) {
2435 break;
2436 }
2437 ndots++;
2438 }
2439 idx++; /* skip over the 'import' keyword */
2440 switch (TYPE(CHILD(n, idx))) {
2441 case STAR:
2442 /* from ... import * */
2443 n = CHILD(n, idx);
2444 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002445 break;
2446 case LPAR:
2447 /* from ... import (x, y, z) */
2448 n = CHILD(n, idx + 1);
2449 n_children = NCH(n);
2450 break;
2451 case import_as_names:
2452 /* from ... import x, y, z */
2453 n = CHILD(n, idx);
2454 n_children = NCH(n);
2455 if (n_children % 2 == 0) {
2456 ast_error(n, "trailing comma not allowed without"
2457 " surrounding parentheses");
2458 return NULL;
2459 }
2460 break;
2461 default:
2462 ast_error(n, "Unexpected node-type in from-import");
2463 return NULL;
2464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002466 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2467 if (!aliases)
2468 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002470 /* handle "from ... import *" special b/c there's no children */
2471 if (TYPE(n) == STAR) {
2472 alias_ty import_alias = alias_for_import_name(c, n);
2473 if (!import_alias)
2474 return NULL;
2475 asdl_seq_SET(aliases, 0, import_alias);
2476 }
2477 else {
2478 for (i = 0; i < NCH(n); i += 2) {
2479 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2480 if (!import_alias)
2481 return NULL;
2482 asdl_seq_SET(aliases, i / 2, import_alias);
2483 }
2484 }
2485 if (mod != NULL)
2486 modname = mod->name;
2487 else
2488 modname = new_identifier("", c->c_arena);
2489 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2490 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 }
Neal Norwitz79792652005-11-14 04:25:03 +00002492 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002493 "unknown import statement: starts with command '%s'",
2494 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 return NULL;
2496}
2497
2498static stmt_ty
2499ast_for_global_stmt(struct compiling *c, const node *n)
2500{
2501 /* global_stmt: 'global' NAME (',' NAME)* */
2502 identifier name;
2503 asdl_seq *s;
2504 int i;
2505
2506 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002507 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002509 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002511 name = NEW_IDENTIFIER(CHILD(n, i));
2512 if (!name)
2513 return NULL;
2514 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002516 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517}
2518
2519static stmt_ty
2520ast_for_exec_stmt(struct compiling *c, const node *n)
2521{
2522 expr_ty expr1, globals = NULL, locals = NULL;
2523 int n_children = NCH(n);
2524 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002525 PyErr_Format(PyExc_SystemError,
2526 "poorly formed 'exec' statement: %d parts to statement",
2527 n_children);
2528 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 }
2530
2531 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2532 REQ(n, exec_stmt);
2533 expr1 = ast_for_expr(c, CHILD(n, 1));
2534 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002537 globals = ast_for_expr(c, CHILD(n, 3));
2538 if (!globals)
2539 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 }
2541 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002542 locals = ast_for_expr(c, CHILD(n, 5));
2543 if (!locals)
2544 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 }
2546
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002547 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2548 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549}
2550
2551static stmt_ty
2552ast_for_assert_stmt(struct compiling *c, const node *n)
2553{
2554 /* assert_stmt: 'assert' test [',' test] */
2555 REQ(n, assert_stmt);
2556 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002557 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2558 if (!expression)
2559 return NULL;
2560 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2561 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 }
2563 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002564 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002566 expr1 = ast_for_expr(c, CHILD(n, 1));
2567 if (!expr1)
2568 return NULL;
2569 expr2 = ast_for_expr(c, CHILD(n, 3));
2570 if (!expr2)
2571 return NULL;
2572
2573 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 }
Neal Norwitz79792652005-11-14 04:25:03 +00002575 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002576 "improper number of parts to 'assert' statement: %d",
2577 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 return NULL;
2579}
2580
2581static asdl_seq *
2582ast_for_suite(struct compiling *c, const node *n)
2583{
2584 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002585 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 stmt_ty s;
2587 int i, total, num, end, pos = 0;
2588 node *ch;
2589
2590 REQ(n, suite);
2591
2592 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002593 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002595 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002597 n = CHILD(n, 0);
2598 /* simple_stmt always ends with a NEWLINE,
2599 and may have a trailing SEMI
2600 */
2601 end = NCH(n) - 1;
2602 if (TYPE(CHILD(n, end - 1)) == SEMI)
2603 end--;
2604 /* loop by 2 to skip semi-colons */
2605 for (i = 0; i < end; i += 2) {
2606 ch = CHILD(n, i);
2607 s = ast_for_stmt(c, ch);
2608 if (!s)
2609 return NULL;
2610 asdl_seq_SET(seq, pos++, s);
2611 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 }
2613 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002614 for (i = 2; i < (NCH(n) - 1); i++) {
2615 ch = CHILD(n, i);
2616 REQ(ch, stmt);
2617 num = num_stmts(ch);
2618 if (num == 1) {
2619 /* small_stmt or compound_stmt with only one child */
2620 s = ast_for_stmt(c, ch);
2621 if (!s)
2622 return NULL;
2623 asdl_seq_SET(seq, pos++, s);
2624 }
2625 else {
2626 int j;
2627 ch = CHILD(ch, 0);
2628 REQ(ch, simple_stmt);
2629 for (j = 0; j < NCH(ch); j += 2) {
2630 /* statement terminates with a semi-colon ';' */
2631 if (NCH(CHILD(ch, j)) == 0) {
2632 assert((j + 1) == NCH(ch));
2633 break;
2634 }
2635 s = ast_for_stmt(c, CHILD(ch, j));
2636 if (!s)
2637 return NULL;
2638 asdl_seq_SET(seq, pos++, s);
2639 }
2640 }
2641 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 }
2643 assert(pos == seq->size);
2644 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645}
2646
2647static stmt_ty
2648ast_for_if_stmt(struct compiling *c, const node *n)
2649{
2650 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2651 ['else' ':' suite]
2652 */
2653 char *s;
2654
2655 REQ(n, if_stmt);
2656
2657 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002658 expr_ty expression;
2659 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002661 expression = ast_for_expr(c, CHILD(n, 1));
2662 if (!expression)
2663 return NULL;
2664 suite_seq = ast_for_suite(c, CHILD(n, 3));
2665 if (!suite_seq)
2666 return NULL;
2667
2668 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2669 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002671
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 s = STR(CHILD(n, 4));
2673 /* s[2], the third character in the string, will be
2674 's' for el_s_e, or
2675 'i' for el_i_f
2676 */
2677 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002678 expr_ty expression;
2679 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002681 expression = ast_for_expr(c, CHILD(n, 1));
2682 if (!expression)
2683 return NULL;
2684 seq1 = ast_for_suite(c, CHILD(n, 3));
2685 if (!seq1)
2686 return NULL;
2687 seq2 = ast_for_suite(c, CHILD(n, 6));
2688 if (!seq2)
2689 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002691 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2692 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 }
2694 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002695 int i, n_elif, has_else = 0;
2696 expr_ty expression;
2697 asdl_seq *suite_seq;
2698 asdl_seq *orelse = NULL;
2699 n_elif = NCH(n) - 4;
2700 /* must reference the child n_elif+1 since 'else' token is third,
2701 not fourth, child from the end. */
2702 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2703 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2704 has_else = 1;
2705 n_elif -= 3;
2706 }
2707 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002709 if (has_else) {
2710 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002712 orelse = asdl_seq_new(1, c->c_arena);
2713 if (!orelse)
2714 return NULL;
2715 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2716 if (!expression)
2717 return NULL;
2718 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2719 if (!suite_seq)
2720 return NULL;
2721 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2722 if (!suite_seq2)
2723 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002725 asdl_seq_SET(orelse, 0,
2726 If(expression, suite_seq, suite_seq2,
2727 LINENO(CHILD(n, NCH(n) - 6)),
2728 CHILD(n, NCH(n) - 6)->n_col_offset,
2729 c->c_arena));
2730 /* the just-created orelse handled the last elif */
2731 n_elif--;
2732 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002734 for (i = 0; i < n_elif; i++) {
2735 int off = 5 + (n_elif - i - 1) * 4;
2736 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2737 if (!newobj)
2738 return NULL;
2739 expression = ast_for_expr(c, CHILD(n, off));
2740 if (!expression)
2741 return NULL;
2742 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2743 if (!suite_seq)
2744 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002746 asdl_seq_SET(newobj, 0,
2747 If(expression, suite_seq, orelse,
2748 LINENO(CHILD(n, off)),
2749 CHILD(n, off)->n_col_offset, c->c_arena));
2750 orelse = newobj;
2751 }
2752 expression = ast_for_expr(c, CHILD(n, 1));
2753 if (!expression)
2754 return NULL;
2755 suite_seq = ast_for_suite(c, CHILD(n, 3));
2756 if (!suite_seq)
2757 return NULL;
2758 return If(expression, suite_seq, orelse,
2759 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002761
2762 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002763 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002764 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765}
2766
2767static stmt_ty
2768ast_for_while_stmt(struct compiling *c, const node *n)
2769{
2770 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2771 REQ(n, while_stmt);
2772
2773 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002774 expr_ty expression;
2775 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002777 expression = ast_for_expr(c, CHILD(n, 1));
2778 if (!expression)
2779 return NULL;
2780 suite_seq = ast_for_suite(c, CHILD(n, 3));
2781 if (!suite_seq)
2782 return NULL;
2783 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2784 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 }
2786 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002787 expr_ty expression;
2788 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002790 expression = ast_for_expr(c, CHILD(n, 1));
2791 if (!expression)
2792 return NULL;
2793 seq1 = ast_for_suite(c, CHILD(n, 3));
2794 if (!seq1)
2795 return NULL;
2796 seq2 = ast_for_suite(c, CHILD(n, 6));
2797 if (!seq2)
2798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002800 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2801 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002803
2804 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002805 "wrong number of tokens for 'while' statement: %d",
2806 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002807 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808}
2809
2810static stmt_ty
2811ast_for_for_stmt(struct compiling *c, const node *n)
2812{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002813 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 expr_ty expression;
2815 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002816 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2818 REQ(n, for_stmt);
2819
2820 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002821 seq = ast_for_suite(c, CHILD(n, 8));
2822 if (!seq)
2823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 }
2825
Neal Norwitzedef2be2006-07-12 05:26:17 +00002826 node_target = CHILD(n, 1);
2827 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002828 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002829 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002830 /* Check the # of children rather than the length of _target, since
2831 for x, in ... has 1 element in _target, but still requires a Tuple. */
2832 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002833 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002835 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002837 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002838 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002839 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002841 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002842 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002844 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002845 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846}
2847
2848static excepthandler_ty
2849ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2850{
Collin Winter62903052007-05-18 23:11:24 +00002851 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 REQ(exc, except_clause);
2853 REQ(body, suite);
2854
2855 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002856 asdl_seq *suite_seq = ast_for_suite(c, body);
2857 if (!suite_seq)
2858 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859
Georg Brandla48f3ab2008-03-30 06:40:17 +00002860 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002861 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 }
2863 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002864 expr_ty expression;
2865 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002867 expression = ast_for_expr(c, CHILD(exc, 1));
2868 if (!expression)
2869 return NULL;
2870 suite_seq = ast_for_suite(c, body);
2871 if (!suite_seq)
2872 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873
Georg Brandla48f3ab2008-03-30 06:40:17 +00002874 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002875 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 }
2877 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002878 asdl_seq *suite_seq;
2879 expr_ty expression;
2880 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2881 if (!e)
2882 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002883 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002884 return NULL;
2885 expression = ast_for_expr(c, CHILD(exc, 1));
2886 if (!expression)
2887 return NULL;
2888 suite_seq = ast_for_suite(c, body);
2889 if (!suite_seq)
2890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891
Georg Brandla48f3ab2008-03-30 06:40:17 +00002892 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002893 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002895
2896 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002897 "wrong number of children for 'except' clause: %d",
2898 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002899 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900}
2901
2902static stmt_ty
2903ast_for_try_stmt(struct compiling *c, const node *n)
2904{
Neal Norwitzf599f422005-12-17 21:33:47 +00002905 const int nch = NCH(n);
2906 int n_except = (nch - 3)/3;
2907 asdl_seq *body, *orelse = NULL, *finally = NULL;
2908
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 REQ(n, try_stmt);
2910
Neal Norwitzf599f422005-12-17 21:33:47 +00002911 body = ast_for_suite(c, CHILD(n, 2));
2912 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002913 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914
Neal Norwitzf599f422005-12-17 21:33:47 +00002915 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002916 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2917 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2918 /* we can assume it's an "else",
2919 because nch >= 9 for try-else-finally and
2920 it would otherwise have a type of except_clause */
2921 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2922 if (orelse == NULL)
2923 return NULL;
2924 n_except--;
2925 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002927 finally = ast_for_suite(c, CHILD(n, nch - 1));
2928 if (finally == NULL)
2929 return NULL;
2930 n_except--;
2931 }
2932 else {
2933 /* we can assume it's an "else",
2934 otherwise it would have a type of except_clause */
2935 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2936 if (orelse == NULL)
2937 return NULL;
2938 n_except--;
2939 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002941 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002942 ast_error(n, "malformed 'try' statement");
2943 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002945
2946 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002947 int i;
2948 stmt_ty except_st;
2949 /* process except statements to create a try ... except */
2950 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2951 if (handlers == NULL)
2952 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002953
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002954 for (i = 0; i < n_except; i++) {
2955 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2956 CHILD(n, 5 + i * 3));
2957 if (!e)
2958 return NULL;
2959 asdl_seq_SET(handlers, i, e);
2960 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002961
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002962 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2963 n->n_col_offset, c->c_arena);
2964 if (!finally)
2965 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002966
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002967 /* if a 'finally' is present too, we nest the TryExcept within a
2968 TryFinally to emulate try ... except ... finally */
2969 body = asdl_seq_new(1, c->c_arena);
2970 if (body == NULL)
2971 return NULL;
2972 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002973 }
2974
2975 /* must be a try ... finally (except clauses are in body, if any exist) */
2976 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002977 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978}
2979
Guido van Rossumc2e20742006-02-27 22:32:47 +00002980static expr_ty
2981ast_for_with_var(struct compiling *c, const node *n)
2982{
2983 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002984 return ast_for_expr(c, CHILD(n, 1));
2985}
2986
2987/* with_stmt: 'with' test [ with_var ] ':' suite */
2988static stmt_ty
2989ast_for_with_stmt(struct compiling *c, const node *n)
2990{
2991 expr_ty context_expr, optional_vars = NULL;
2992 int suite_index = 3; /* skip 'with', test, and ':' */
2993 asdl_seq *suite_seq;
2994
2995 assert(TYPE(n) == with_stmt);
2996 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002997 if (!context_expr)
2998 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002999 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003000 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003001
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003002 if (!optional_vars) {
3003 return NULL;
3004 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003005 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003006 return NULL;
3007 }
3008 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003009 }
3010
3011 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
3012 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003013 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003014 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003015 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003016 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003017}
3018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003020ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021{
3022 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 asdl_seq *bases, *s;
3024
3025 REQ(n, classdef);
3026
Benjamin Petersond5efd202008-06-08 22:52:37 +00003027 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003028 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029
3030 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003031 s = ast_for_suite(c, CHILD(n, 3));
3032 if (!s)
3033 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003034 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3035 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 }
3037 /* check for empty base list */
3038 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003039 s = ast_for_suite(c, CHILD(n,5));
3040 if (!s)
3041 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003042 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3043 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044 }
3045
3046 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003047 bases = ast_for_class_bases(c, CHILD(n, 3));
3048 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003049 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050
3051 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003052 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003053 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003054 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3055 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056}
3057
3058static stmt_ty
3059ast_for_stmt(struct compiling *c, const node *n)
3060{
3061 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003062 assert(NCH(n) == 1);
3063 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064 }
3065 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003066 assert(num_stmts(n) == 1);
3067 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 }
3069 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003070 REQ(n, small_stmt);
3071 n = CHILD(n, 0);
3072 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3073 | flow_stmt | import_stmt | global_stmt | exec_stmt
3074 | assert_stmt
3075 */
3076 switch (TYPE(n)) {
3077 case expr_stmt:
3078 return ast_for_expr_stmt(c, n);
3079 case print_stmt:
3080 return ast_for_print_stmt(c, n);
3081 case del_stmt:
3082 return ast_for_del_stmt(c, n);
3083 case pass_stmt:
3084 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3085 case flow_stmt:
3086 return ast_for_flow_stmt(c, n);
3087 case import_stmt:
3088 return ast_for_import_stmt(c, n);
3089 case global_stmt:
3090 return ast_for_global_stmt(c, n);
3091 case exec_stmt:
3092 return ast_for_exec_stmt(c, n);
3093 case assert_stmt:
3094 return ast_for_assert_stmt(c, n);
3095 default:
3096 PyErr_Format(PyExc_SystemError,
3097 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3098 TYPE(n), NCH(n));
3099 return NULL;
3100 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003101 }
3102 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003103 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003104 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003105 */
3106 node *ch = CHILD(n, 0);
3107 REQ(n, compound_stmt);
3108 switch (TYPE(ch)) {
3109 case if_stmt:
3110 return ast_for_if_stmt(c, ch);
3111 case while_stmt:
3112 return ast_for_while_stmt(c, ch);
3113 case for_stmt:
3114 return ast_for_for_stmt(c, ch);
3115 case try_stmt:
3116 return ast_for_try_stmt(c, ch);
3117 case with_stmt:
3118 return ast_for_with_stmt(c, ch);
3119 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003120 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003121 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003122 return ast_for_classdef(c, ch, NULL);
3123 case decorated:
3124 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003125 default:
3126 PyErr_Format(PyExc_SystemError,
3127 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3128 TYPE(n), NCH(n));
3129 return NULL;
3130 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 }
3132}
3133
3134static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003135parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003137 const char *end;
3138 long x;
3139 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003141 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003142 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143#endif
3144
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003145 errno = 0;
3146 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003148 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003150 if (*end == 'l' || *end == 'L')
3151 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003152 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003153 if (*end == '\0') {
3154 if (errno != 0)
3155 return PyLong_FromString((char *)s, (char **)0, 0);
3156 return PyInt_FromLong(x);
3157 }
3158 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003160 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003161 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003162 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003163 complex.imag = PyOS_ascii_atof(s);
3164 PyFPE_END_PROTECT(complex)
3165 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003166 }
3167 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003169 {
3170 PyFPE_START_PROTECT("atof", return 0)
3171 dx = PyOS_ascii_atof(s);
3172 PyFPE_END_PROTECT(dx)
3173 return PyFloat_FromDouble(dx);
3174 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175}
3176
3177static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003178decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179{
3180#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003181 Py_FatalError("decode_utf8 should not be called in this build.");
3182 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003184 PyObject *u, *v;
3185 char *s, *t;
3186 t = s = (char *)*sPtr;
3187 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3188 while (s < end && (*s & 0x80)) s++;
3189 *sPtr = s;
3190 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3191 if (u == NULL)
3192 return NULL;
3193 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3194 Py_DECREF(u);
3195 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196#endif
3197}
3198
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003199#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003201decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003203 PyObject *v, *u;
3204 char *buf;
3205 char *p;
3206 const char *end;
3207 if (encoding == NULL) {
3208 buf = (char *)s;
3209 u = NULL;
3210 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3211 buf = (char *)s;
3212 u = NULL;
3213 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003214 /* check for integer overflow */
3215 if (len > PY_SIZE_MAX / 4)
3216 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003217 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003218 u = PyString_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003219 if (u == NULL)
3220 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003221 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003222 end = s + len;
3223 while (s < end) {
3224 if (*s == '\\') {
3225 *p++ = *s++;
3226 if (*s & 0x80) {
3227 strcpy(p, "u005c");
3228 p += 5;
3229 }
3230 }
3231 if (*s & 0x80) { /* XXX inefficient */
3232 PyObject *w;
3233 char *r;
3234 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003235 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003236 if (w == NULL) {
3237 Py_DECREF(u);
3238 return NULL;
3239 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003240 r = PyString_AsString(w);
3241 rn = PyString_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003242 assert(rn % 2 == 0);
3243 for (i = 0; i < rn; i += 2) {
3244 sprintf(p, "\\u%02x%02x",
3245 r[i + 0] & 0xFF,
3246 r[i + 1] & 0xFF);
3247 p += 6;
3248 }
3249 Py_DECREF(w);
3250 } else {
3251 *p++ = *s++;
3252 }
3253 }
3254 len = p - buf;
3255 s = buf;
3256 }
3257 if (rawmode)
3258 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3259 else
3260 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3261 Py_XDECREF(u);
3262 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003264#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265
3266/* s is a Python string literal, including the bracketing quote characters,
3267 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3268 * parsestr parses it, and returns the decoded Python string object.
3269 */
3270static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003271parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003273 size_t len;
3274 int quote = Py_CHARMASK(*s);
3275 int rawmode = 0;
3276 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003277 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003279 if (isalpha(quote) || quote == '_') {
3280 if (quote == 'u' || quote == 'U') {
3281 quote = *++s;
3282 unicode = 1;
3283 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003284 if (quote == 'b' || quote == 'B') {
3285 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003286 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003287 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003288 if (quote == 'r' || quote == 'R') {
3289 quote = *++s;
3290 rawmode = 1;
3291 }
3292 }
3293 if (quote != '\'' && quote != '\"') {
3294 PyErr_BadInternalCall();
3295 return NULL;
3296 }
3297 s++;
3298 len = strlen(s);
3299 if (len > INT_MAX) {
3300 PyErr_SetString(PyExc_OverflowError,
3301 "string to parse is too long");
3302 return NULL;
3303 }
3304 if (s[--len] != quote) {
3305 PyErr_BadInternalCall();
3306 return NULL;
3307 }
3308 if (len >= 4 && s[0] == quote && s[1] == quote) {
3309 s += 2;
3310 len -= 2;
3311 if (s[--len] != quote || s[--len] != quote) {
3312 PyErr_BadInternalCall();
3313 return NULL;
3314 }
3315 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003317 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003318 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003319 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003321 need_encoding = (c->c_encoding != NULL &&
3322 strcmp(c->c_encoding, "utf-8") != 0 &&
3323 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003324 if (rawmode || strchr(s, '\\') == NULL) {
3325 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003327 /* This should not happen - we never see any other
3328 encoding. */
3329 Py_FatalError(
3330 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003332 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3333 if (u == NULL)
3334 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003335 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003336 Py_DECREF(u);
3337 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003339 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003340 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003341 }
3342 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003343
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003344 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003345 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003346}
3347
3348/* Build a Python string object out of a STRING atom. This takes care of
3349 * compile-time literal catenation, calling parsestr() on each piece, and
3350 * pasting the intermediate results together.
3351 */
3352static PyObject *
3353parsestrplus(struct compiling *c, const node *n)
3354{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003355 PyObject *v;
3356 int i;
3357 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003358 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003359 /* String literal concatenation */
3360 for (i = 1; i < NCH(n); i++) {
3361 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003362 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003363 if (s == NULL)
3364 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003365 if (PyString_Check(v) && PyString_Check(s)) {
3366 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003367 if (v == NULL)
3368 goto onError;
3369 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003371 else {
3372 PyObject *temp = PyUnicode_Concat(v, s);
3373 Py_DECREF(s);
3374 Py_DECREF(v);
3375 v = temp;
3376 if (v == NULL)
3377 goto onError;
3378 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003380 }
3381 }
3382 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383
3384 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003385 Py_XDECREF(v);
3386 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387}