blob: 8414c7448081832f8431f87c78b4b487b635501f [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");
134 if (Py_Py3kWarningFlag && !(strcmp(x, "True") && strcmp(x, "False")) &&
135 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
136 return 0;
137 return 1;
138}
139
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140/* num_stmts() returns number of contained statements.
141
142 Use this routine to determine how big a sequence is needed for
143 the statements in a parse tree. Its raison d'etre is this bit of
144 grammar:
145
146 stmt: simple_stmt | compound_stmt
147 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
148
149 A simple_stmt can contain multiple small_stmt elements joined
150 by semicolons. If the arg is a simple_stmt, the number of
151 small_stmt elements is returned.
152*/
153
154static int
155num_stmts(const node *n)
156{
157 int i, l;
158 node *ch;
159
160 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000161 case single_input:
162 if (TYPE(CHILD(n, 0)) == NEWLINE)
163 return 0;
164 else
165 return num_stmts(CHILD(n, 0));
166 case file_input:
167 l = 0;
168 for (i = 0; i < NCH(n); i++) {
169 ch = CHILD(n, i);
170 if (TYPE(ch) == stmt)
171 l += num_stmts(ch);
172 }
173 return l;
174 case stmt:
175 return num_stmts(CHILD(n, 0));
176 case compound_stmt:
177 return 1;
178 case simple_stmt:
179 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
180 case suite:
181 if (NCH(n) == 1)
182 return num_stmts(CHILD(n, 0));
183 else {
184 l = 0;
185 for (i = 2; i < (NCH(n) - 1); i++)
186 l += num_stmts(CHILD(n, i));
187 return l;
188 }
189 default: {
190 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000192 sprintf(buf, "Non-statement found: %d %d\n",
193 TYPE(n), NCH(n));
194 Py_FatalError(buf);
195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 }
197 assert(0);
198 return 0;
199}
200
201/* Transform the CST rooted at node * to the appropriate AST
202*/
203
204mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000205PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000206 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000208 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 asdl_seq *stmts = NULL;
210 stmt_ty s;
211 node *ch;
212 struct compiling c;
213
214 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000215 c.c_encoding = "utf-8";
216 if (TYPE(n) == encoding_decl) {
217 ast_error(n, "encoding declaration in Unicode string");
218 goto error;
219 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000221 c.c_encoding = STR(n);
222 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000224 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225 }
Christian Heimes3c608332008-03-26 22:01:37 +0000226 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000227 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000228 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
Jeremy Hyltona8293132006-02-28 17:58:27 +0000230 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000232 case file_input:
233 stmts = asdl_seq_new(num_stmts(n), arena);
234 if (!stmts)
235 return NULL;
236 for (i = 0; i < NCH(n) - 1; i++) {
237 ch = CHILD(n, i);
238 if (TYPE(ch) == NEWLINE)
239 continue;
240 REQ(ch, stmt);
241 num = num_stmts(ch);
242 if (num == 1) {
243 s = ast_for_stmt(&c, ch);
244 if (!s)
245 goto error;
246 asdl_seq_SET(stmts, k++, s);
247 }
248 else {
249 ch = CHILD(ch, 0);
250 REQ(ch, simple_stmt);
251 for (j = 0; j < num; j++) {
252 s = ast_for_stmt(&c, CHILD(ch, j * 2));
253 if (!s)
254 goto error;
255 asdl_seq_SET(stmts, k++, s);
256 }
257 }
258 }
259 return Module(stmts, arena);
260 case eval_input: {
261 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000263 /* XXX Why not gen_for here? */
264 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
265 if (!testlist_ast)
266 goto error;
267 return Expression(testlist_ast, arena);
268 }
269 case single_input:
270 if (TYPE(CHILD(n, 0)) == NEWLINE) {
271 stmts = asdl_seq_new(1, arena);
272 if (!stmts)
273 goto error;
274 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
275 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000276 if (!asdl_seq_GET(stmts, 0))
277 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000278 return Interactive(stmts, arena);
279 }
280 else {
281 n = CHILD(n, 0);
282 num = num_stmts(n);
283 stmts = asdl_seq_new(num, arena);
284 if (!stmts)
285 goto error;
286 if (num == 1) {
287 s = ast_for_stmt(&c, n);
288 if (!s)
289 goto error;
290 asdl_seq_SET(stmts, 0, s);
291 }
292 else {
293 /* Only a simple_stmt can contain multiple statements. */
294 REQ(n, simple_stmt);
295 for (i = 0; i < NCH(n); i += 2) {
296 if (TYPE(CHILD(n, i)) == NEWLINE)
297 break;
298 s = ast_for_stmt(&c, CHILD(n, i));
299 if (!s)
300 goto error;
301 asdl_seq_SET(stmts, i / 2, s);
302 }
303 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000305 return Interactive(stmts, arena);
306 }
307 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000308 PyErr_Format(PyExc_SystemError,
309 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000310 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311 }
312 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313 ast_error_finish(filename);
314 return NULL;
315}
316
317/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
318*/
319
320static operator_ty
321get_operator(const node *n)
322{
323 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000324 case VBAR:
325 return BitOr;
326 case CIRCUMFLEX:
327 return BitXor;
328 case AMPER:
329 return BitAnd;
330 case LEFTSHIFT:
331 return LShift;
332 case RIGHTSHIFT:
333 return RShift;
334 case PLUS:
335 return Add;
336 case MINUS:
337 return Sub;
338 case STAR:
339 return Mult;
340 case SLASH:
341 return Div;
342 case DOUBLESLASH:
343 return FloorDiv;
344 case PERCENT:
345 return Mod;
346 default:
347 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348 }
349}
350
Jeremy Hyltona8293132006-02-28 17:58:27 +0000351/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352
353 Only sets context for expr kinds that "can appear in assignment context"
354 (according to ../Parser/Python.asdl). For other expr kinds, it sets
355 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356*/
357
358static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000359set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360{
361 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000362 /* If a particular expression type can't be used for assign / delete,
363 set expr_name to its name and an error message will be generated.
364 */
365 const char* expr_name = NULL;
366
367 /* The ast defines augmented store and load contexts, but the
368 implementation here doesn't actually use them. The code may be
369 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000370 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000371 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000372 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000373 */
374 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375
376 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000377 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000378 if (ctx == Store && !forbidden_check(c, n,
379 PyBytes_AS_STRING(e->v.Attribute.attr)))
380 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000381 e->v.Attribute.ctx = ctx;
382 break;
383 case Subscript_kind:
384 e->v.Subscript.ctx = ctx;
385 break;
386 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000387 if (ctx == Store && !forbidden_check(c, n,
388 PyBytes_AS_STRING(e->v.Name.id)))
389 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000390 e->v.Name.ctx = ctx;
391 break;
392 case List_kind:
393 e->v.List.ctx = ctx;
394 s = e->v.List.elts;
395 break;
396 case Tuple_kind:
397 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
398 return ast_error(n, "can't assign to ()");
399 e->v.Tuple.ctx = ctx;
400 s = e->v.Tuple.elts;
401 break;
402 case Lambda_kind:
403 expr_name = "lambda";
404 break;
405 case Call_kind:
406 expr_name = "function call";
407 break;
408 case BoolOp_kind:
409 case BinOp_kind:
410 case UnaryOp_kind:
411 expr_name = "operator";
412 break;
413 case GeneratorExp_kind:
414 expr_name = "generator expression";
415 break;
416 case Yield_kind:
417 expr_name = "yield expression";
418 break;
419 case ListComp_kind:
420 expr_name = "list comprehension";
421 break;
422 case Dict_kind:
423 case Num_kind:
424 case Str_kind:
425 expr_name = "literal";
426 break;
427 case Compare_kind:
428 expr_name = "comparison";
429 break;
430 case Repr_kind:
431 expr_name = "repr";
432 break;
433 case IfExp_kind:
434 expr_name = "conditional expression";
435 break;
436 default:
437 PyErr_Format(PyExc_SystemError,
438 "unexpected expression in assignment %d (line %d)",
439 e->kind, e->lineno);
440 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000442 /* Check for error string set by switch */
443 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000444 char buf[300];
445 PyOS_snprintf(buf, sizeof(buf),
446 "can't %s %s",
447 ctx == Store ? "assign to" : "delete",
448 expr_name);
449 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000450 }
451
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000453 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454 */
455 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000456 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000458 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000459 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000460 return 0;
461 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462 }
463 return 1;
464}
465
466static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000467ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468{
469 REQ(n, augassign);
470 n = CHILD(n, 0);
471 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000472 case '+':
473 return Add;
474 case '-':
475 return Sub;
476 case '/':
477 if (STR(n)[1] == '/')
478 return FloorDiv;
479 else
480 return Div;
481 case '%':
482 return Mod;
483 case '<':
484 return LShift;
485 case '>':
486 return RShift;
487 case '&':
488 return BitAnd;
489 case '^':
490 return BitXor;
491 case '|':
492 return BitOr;
493 case '*':
494 if (STR(n)[1] == '*')
495 return Pow;
496 else
497 return Mult;
498 default:
499 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
500 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 }
502}
503
504static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000505ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506{
507 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000508 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 */
510 REQ(n, comp_op);
511 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000512 n = CHILD(n, 0);
513 switch (TYPE(n)) {
514 case LESS:
515 return Lt;
516 case GREATER:
517 return Gt;
518 case EQEQUAL: /* == */
519 return Eq;
520 case LESSEQUAL:
521 return LtE;
522 case GREATEREQUAL:
523 return GtE;
524 case NOTEQUAL:
525 return NotEq;
526 case NAME:
527 if (strcmp(STR(n), "in") == 0)
528 return In;
529 if (strcmp(STR(n), "is") == 0)
530 return Is;
531 default:
532 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
533 STR(n));
534 return (cmpop_ty)0;
535 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536 }
537 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000538 /* handle "not in" and "is not" */
539 switch (TYPE(CHILD(n, 0))) {
540 case NAME:
541 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
542 return NotIn;
543 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
544 return IsNot;
545 default:
546 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
547 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
548 return (cmpop_ty)0;
549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550 }
Neal Norwitz79792652005-11-14 04:25:03 +0000551 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000552 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000553 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554}
555
556static asdl_seq *
557seq_for_testlist(struct compiling *c, const node *n)
558{
559 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000560 asdl_seq *seq;
561 expr_ty expression;
562 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000563 assert(TYPE(n) == testlist ||
564 TYPE(n) == listmaker ||
565 TYPE(n) == testlist_gexp ||
566 TYPE(n) == testlist_safe ||
567 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000569 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000571 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572
573 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000574 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000576 expression = ast_for_expr(c, CHILD(n, i));
577 if (!expression)
578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000580 assert(i / 2 < seq->size);
581 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582 }
583 return seq;
584}
585
586static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000587compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588{
589 int i, len = (NCH(n) + 1) / 2;
590 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000591 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000593 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594
Neal Norwitz3a230172006-09-22 08:18:10 +0000595 /* fpdef: NAME | '(' fplist ')'
596 fplist: fpdef (',' fpdef)* [',']
597 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000600 const node *fpdef_node = CHILD(n, 2*i);
601 const node *child;
602 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000603set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000604 /* fpdef_node is either a NAME or an fplist */
605 child = CHILD(fpdef_node, 0);
606 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000607 if (!forbidden_check(c, n, STR(child)))
608 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000609 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
610 child->n_col_offset, c->c_arena);
611 }
612 else {
613 assert(TYPE(fpdef_node) == fpdef);
614 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
615 child = CHILD(fpdef_node, 1);
616 assert(TYPE(child) == fplist);
617 /* NCH == 1 means we have (x), we need to elide the extra parens */
618 if (NCH(child) == 1) {
619 fpdef_node = CHILD(child, 0);
620 assert(TYPE(fpdef_node) == fpdef);
621 goto set_name;
622 }
623 arg = compiler_complex_args(c, child);
624 }
625 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626 }
627
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000628 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000629 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000630 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 return result;
632}
633
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634
Jeremy Hyltona8293132006-02-28 17:58:27 +0000635/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636
637static arguments_ty
638ast_for_arguments(struct compiling *c, const node *n)
639{
640 /* parameters: '(' [varargslist] ')'
641 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000642 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000644 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 asdl_seq *args, *defaults;
646 identifier vararg = NULL, kwarg = NULL;
647 node *ch;
648
649 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000650 if (NCH(n) == 2) /* () as argument list */
651 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
652 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 }
654 REQ(n, varargslist);
655
656 /* first count the number of normal args & defaults */
657 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000658 ch = CHILD(n, i);
659 if (TYPE(ch) == fpdef)
660 n_args++;
661 if (TYPE(ch) == EQUAL)
662 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000664 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000666 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000667 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000669 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670
671 /* fpdef: NAME | '(' fplist ')'
672 fplist: fpdef (',' fpdef)* [',']
673 */
674 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000675 j = 0; /* index for defaults */
676 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000678 ch = CHILD(n, i);
679 switch (TYPE(ch)) {
680 case fpdef:
681 handle_fpdef:
682 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
683 anything other than EQUAL or a comma? */
684 /* XXX Should NCH(n) check be made a separate check? */
685 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
686 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
687 if (!expression)
688 goto error;
689 assert(defaults != NULL);
690 asdl_seq_SET(defaults, j++, expression);
691 i += 2;
692 found_default = 1;
693 }
694 else if (found_default) {
695 ast_error(n,
696 "non-default argument follows default argument");
697 goto error;
698 }
699 if (NCH(ch) == 3) {
700 ch = CHILD(ch, 1);
701 /* def foo((x)): is not complex, special case. */
702 if (NCH(ch) != 1) {
703 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000704 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
705 "tuple parameter unpacking has been removed in 3.x"))
706 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000707 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000708 if (!asdl_seq_GET(args, k-1))
709 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000710 } else {
711 /* def foo((x)): setup for checking NAME below. */
712 /* Loop because there can be many parens and tuple
713 unpacking mixed in. */
714 ch = CHILD(ch, 0);
715 assert(TYPE(ch) == fpdef);
716 goto handle_fpdef;
717 }
718 }
719 if (TYPE(CHILD(ch, 0)) == NAME) {
720 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000721 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000722 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000723 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
724 Param, LINENO(ch), ch->n_col_offset,
725 c->c_arena);
726 if (!name)
727 goto error;
728 asdl_seq_SET(args, k++, name);
729
730 }
731 i += 2; /* the name and the comma */
732 break;
733 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000734 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000735 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000736 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
737 i += 3;
738 break;
739 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000740 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000741 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000742 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
743 i += 3;
744 break;
745 default:
746 PyErr_Format(PyExc_SystemError,
747 "unexpected node in varargslist: %d @ %d",
748 TYPE(ch), i);
749 goto error;
750 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 }
752
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000753 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
755 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000756 Py_XDECREF(vararg);
757 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 return NULL;
759}
760
761static expr_ty
762ast_for_dotted_name(struct compiling *c, const node *n)
763{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000764 expr_ty e;
765 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000766 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 int i;
768
769 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000770
771 lineno = LINENO(n);
772 col_offset = n->n_col_offset;
773
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 id = NEW_IDENTIFIER(CHILD(n, 0));
775 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000776 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000777 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000779 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780
781 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000782 id = NEW_IDENTIFIER(CHILD(n, i));
783 if (!id)
784 return NULL;
785 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
786 if (!e)
787 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 }
789
790 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791}
792
793static expr_ty
794ast_for_decorator(struct compiling *c, const node *n)
795{
796 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
797 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000798 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799
800 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000801 REQ(CHILD(n, 0), AT);
802 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803
804 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
805 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000806 return NULL;
807
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000809 d = name_expr;
810 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 }
812 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000813 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
814 n->n_col_offset, c->c_arena);
815 if (!d)
816 return NULL;
817 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 }
819 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000820 d = ast_for_call(c, CHILD(n, 3), name_expr);
821 if (!d)
822 return NULL;
823 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 }
825
826 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827}
828
829static asdl_seq*
830ast_for_decorators(struct compiling *c, const node *n)
831{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000832 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000833 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 int i;
835
836 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000837 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000839 return NULL;
840
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000842 d = ast_for_decorator(c, CHILD(n, i));
843 if (!d)
844 return NULL;
845 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 }
847 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848}
849
850static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000851ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852{
Christian Heimes5224d282008-02-23 15:01:05 +0000853 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000854 identifier name;
855 arguments_ty args;
856 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000857 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858
859 REQ(n, funcdef);
860
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 name = NEW_IDENTIFIER(CHILD(n, name_i));
862 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000863 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000864 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000865 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 args = ast_for_arguments(c, CHILD(n, name_i + 1));
867 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000868 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 body = ast_for_suite(c, CHILD(n, name_i + 3));
870 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000871 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000873 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000874 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875}
876
Christian Heimes5224d282008-02-23 15:01:05 +0000877static stmt_ty
878ast_for_decorated(struct compiling *c, const node *n)
879{
880 /* decorated: decorators (classdef | funcdef) */
881 stmt_ty thing = NULL;
882 asdl_seq *decorator_seq = NULL;
883
884 REQ(n, decorated);
885
886 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
887 if (!decorator_seq)
888 return NULL;
889
890 assert(TYPE(CHILD(n, 1)) == funcdef ||
891 TYPE(CHILD(n, 1)) == classdef);
892
893 if (TYPE(CHILD(n, 1)) == funcdef) {
894 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
895 } else if (TYPE(CHILD(n, 1)) == classdef) {
896 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
897 }
898 /* we count the decorators in when talking about the class' or
899 function's line number */
900 if (thing) {
901 thing->lineno = LINENO(n);
902 thing->col_offset = n->n_col_offset;
903 }
904 return thing;
905}
906
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907static expr_ty
908ast_for_lambdef(struct compiling *c, const node *n)
909{
910 /* lambdef: 'lambda' [varargslist] ':' test */
911 arguments_ty args;
912 expr_ty expression;
913
914 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000915 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
916 if (!args)
917 return NULL;
918 expression = ast_for_expr(c, CHILD(n, 2));
919 if (!expression)
920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 }
922 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000923 args = ast_for_arguments(c, CHILD(n, 1));
924 if (!args)
925 return NULL;
926 expression = ast_for_expr(c, CHILD(n, 3));
927 if (!expression)
928 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 }
930
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000931 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932}
933
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000934static expr_ty
935ast_for_ifexpr(struct compiling *c, const node *n)
936{
937 /* test: or_test 'if' or_test 'else' test */
938 expr_ty expression, body, orelse;
939
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000940 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000941 body = ast_for_expr(c, CHILD(n, 0));
942 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000943 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000944 expression = ast_for_expr(c, CHILD(n, 2));
945 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000946 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000947 orelse = ast_for_expr(c, CHILD(n, 4));
948 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000949 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000950 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000951 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000952}
953
Neal Norwitze4d4f002006-09-05 03:58:26 +0000954/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
955 so there is only a single version. Possibly for loops can also re-use
956 the code.
957*/
958
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959/* Count the number of 'for' loop in a list comprehension.
960
961 Helper for ast_for_listcomp().
962*/
963
964static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000965count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966{
967 int n_fors = 0;
968 node *ch = CHILD(n, 1);
969
970 count_list_for:
971 n_fors++;
972 REQ(ch, list_for);
973 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000974 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000976 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 count_list_iter:
978 REQ(ch, list_iter);
979 ch = CHILD(ch, 0);
980 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000981 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000983 if (NCH(ch) == 3) {
984 ch = CHILD(ch, 2);
985 goto count_list_iter;
986 }
987 else
988 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000990
991 /* Should never be reached */
992 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
993 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994}
995
996/* Count the number of 'if' statements in a list comprehension.
997
998 Helper for ast_for_listcomp().
999*/
1000
1001static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001002count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003{
1004 int n_ifs = 0;
1005
1006 count_list_iter:
1007 REQ(n, list_iter);
1008 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001009 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 n = CHILD(n, 0);
1011 REQ(n, list_if);
1012 n_ifs++;
1013 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001014 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 n = CHILD(n, 2);
1016 goto count_list_iter;
1017}
1018
1019static expr_ty
1020ast_for_listcomp(struct compiling *c, const node *n)
1021{
1022 /* listmaker: test ( list_for | (',' test)* [','] )
1023 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1024 list_iter: list_for | list_if
1025 list_if: 'if' test [list_iter]
1026 testlist_safe: test [(',' test)+ [',']]
1027 */
1028 expr_ty elt;
1029 asdl_seq *listcomps;
1030 int i, n_fors;
1031 node *ch;
1032
1033 REQ(n, listmaker);
1034 assert(NCH(n) > 1);
1035
1036 elt = ast_for_expr(c, CHILD(n, 0));
1037 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001040 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001044 listcomps = asdl_seq_new(n_fors, c->c_arena);
1045 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001046 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001047
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048 ch = CHILD(n, 1);
1049 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001050 comprehension_ty lc;
1051 asdl_seq *t;
1052 expr_ty expression;
1053 node *for_ch;
1054
1055 REQ(ch, list_for);
1056
1057 for_ch = CHILD(ch, 1);
1058 t = ast_for_exprlist(c, for_ch, Store);
1059 if (!t)
1060 return NULL;
1061 expression = ast_for_testlist(c, CHILD(ch, 3));
1062 if (!expression)
1063 return NULL;
1064
1065 /* Check the # of children rather than the length of t, since
1066 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1067 */
1068 if (NCH(for_ch) == 1)
1069 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1070 c->c_arena);
1071 else
1072 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1073 c->c_arena),
1074 expression, NULL, c->c_arena);
1075 if (!lc)
1076 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001078 if (NCH(ch) == 5) {
1079 int j, n_ifs;
1080 asdl_seq *ifs;
1081 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001083 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001084 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001085 if (n_ifs == -1)
1086 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001088 ifs = asdl_seq_new(n_ifs, c->c_arena);
1089 if (!ifs)
1090 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001092 for (j = 0; j < n_ifs; j++) {
1093 REQ(ch, list_iter);
1094 ch = CHILD(ch, 0);
1095 REQ(ch, list_if);
1096
1097 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1098 if (!list_for_expr)
1099 return NULL;
1100
1101 asdl_seq_SET(ifs, j, list_for_expr);
1102 if (NCH(ch) == 3)
1103 ch = CHILD(ch, 2);
1104 }
1105 /* on exit, must guarantee that ch is a list_for */
1106 if (TYPE(ch) == list_iter)
1107 ch = CHILD(ch, 0);
1108 lc->ifs = ifs;
1109 }
1110 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 }
1112
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001113 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114}
1115
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001116/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117
1118 Helper for ast_for_genexp().
1119*/
1120
1121static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001122count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001124 int n_fors = 0;
1125 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126
1127 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001128 n_fors++;
1129 REQ(ch, gen_for);
1130 if (NCH(ch) == 5)
1131 ch = CHILD(ch, 4);
1132 else
1133 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001135 REQ(ch, gen_iter);
1136 ch = CHILD(ch, 0);
1137 if (TYPE(ch) == gen_for)
1138 goto count_gen_for;
1139 else if (TYPE(ch) == gen_if) {
1140 if (NCH(ch) == 3) {
1141 ch = CHILD(ch, 2);
1142 goto count_gen_iter;
1143 }
1144 else
1145 return n_fors;
1146 }
1147
1148 /* Should never be reached */
1149 PyErr_SetString(PyExc_SystemError,
1150 "logic error in count_gen_fors");
1151 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152}
1153
1154/* Count the number of 'if' statements in a generator expression.
1155
1156 Helper for ast_for_genexp().
1157*/
1158
1159static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001160count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001162 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001164 while (1) {
1165 REQ(n, gen_iter);
1166 if (TYPE(CHILD(n, 0)) == gen_for)
1167 return n_ifs;
1168 n = CHILD(n, 0);
1169 REQ(n, gen_if);
1170 n_ifs++;
1171 if (NCH(n) == 2)
1172 return n_ifs;
1173 n = CHILD(n, 2);
1174 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175}
1176
Jeremy Hyltona8293132006-02-28 17:58:27 +00001177/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178static expr_ty
1179ast_for_genexp(struct compiling *c, const node *n)
1180{
1181 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001182 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 expr_ty elt;
1184 asdl_seq *genexps;
1185 int i, n_fors;
1186 node *ch;
1187
1188 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1189 assert(NCH(n) > 1);
1190
1191 elt = ast_for_expr(c, CHILD(n, 0));
1192 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001193 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001195 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001197 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001198
1199 genexps = asdl_seq_new(n_fors, c->c_arena);
1200 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001201 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 ch = CHILD(n, 1);
1204 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001205 comprehension_ty ge;
1206 asdl_seq *t;
1207 expr_ty expression;
1208 node *for_ch;
1209
1210 REQ(ch, gen_for);
1211
1212 for_ch = CHILD(ch, 1);
1213 t = ast_for_exprlist(c, for_ch, Store);
1214 if (!t)
1215 return NULL;
1216 expression = ast_for_expr(c, CHILD(ch, 3));
1217 if (!expression)
1218 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001219
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001220 /* Check the # of children rather than the length of t, since
1221 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1222 if (NCH(for_ch) == 1)
1223 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1224 NULL, c->c_arena);
1225 else
1226 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1227 c->c_arena),
1228 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001229
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001230 if (!ge)
1231 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001232
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001233 if (NCH(ch) == 5) {
1234 int j, n_ifs;
1235 asdl_seq *ifs;
1236
1237 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001238 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001239 if (n_ifs == -1)
1240 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001242 ifs = asdl_seq_new(n_ifs, c->c_arena);
1243 if (!ifs)
1244 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001245
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001246 for (j = 0; j < n_ifs; j++) {
1247 REQ(ch, gen_iter);
1248 ch = CHILD(ch, 0);
1249 REQ(ch, gen_if);
1250
1251 expression = ast_for_expr(c, CHILD(ch, 1));
1252 if (!expression)
1253 return NULL;
1254 asdl_seq_SET(ifs, j, expression);
1255 if (NCH(ch) == 3)
1256 ch = CHILD(ch, 2);
1257 }
1258 /* on exit, must guarantee that ch is a gen_for */
1259 if (TYPE(ch) == gen_iter)
1260 ch = CHILD(ch, 0);
1261 ge->ifs = ifs;
1262 }
1263 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 }
1265
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001266 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267}
1268
1269static expr_ty
1270ast_for_atom(struct compiling *c, const node *n)
1271{
1272 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1273 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1274 */
1275 node *ch = CHILD(n, 0);
1276
1277 switch (TYPE(ch)) {
1278 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001279 /* All names start in Load context, but may later be
1280 changed. */
1281 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1282 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001284 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001285 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001286#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001287 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1288 PyObject *type, *value, *tback, *errstr;
1289 PyErr_Fetch(&type, &value, &tback);
1290 errstr = ((PyUnicodeErrorObject *)value)->reason;
1291 if (errstr) {
1292 char *s = "";
1293 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001294 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001295 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1296 ast_error(n, buf);
1297 } else {
1298 ast_error(n, "(unicode error) unknown error");
1299 }
1300 Py_DECREF(type);
1301 Py_DECREF(value);
1302 Py_XDECREF(tback);
1303 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001304#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001305 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001306 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001307 PyArena_AddPyObject(c->c_arena, str);
1308 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 }
1310 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001311 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001312 if (!pynum)
1313 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001314
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001315 PyArena_AddPyObject(c->c_arena, pynum);
1316 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 }
1318 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001319 ch = CHILD(n, 1);
1320
1321 if (TYPE(ch) == RPAR)
1322 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1323
1324 if (TYPE(ch) == yield_expr)
1325 return ast_for_expr(c, ch);
1326
1327 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1328 return ast_for_genexp(c, ch);
1329
1330 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001332 ch = CHILD(n, 1);
1333
1334 if (TYPE(ch) == RSQB)
1335 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1336
1337 REQ(ch, listmaker);
1338 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1339 asdl_seq *elts = seq_for_testlist(c, ch);
1340 if (!elts)
1341 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001342
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001343 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1344 }
1345 else
1346 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001348 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1349 int i, size;
1350 asdl_seq *keys, *values;
1351
1352 ch = CHILD(n, 1);
1353 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1354 keys = asdl_seq_new(size, c->c_arena);
1355 if (!keys)
1356 return NULL;
1357
1358 values = asdl_seq_new(size, c->c_arena);
1359 if (!values)
1360 return NULL;
1361
1362 for (i = 0; i < NCH(ch); i += 4) {
1363 expr_ty expression;
1364
1365 expression = ast_for_expr(c, CHILD(ch, i));
1366 if (!expression)
1367 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001368
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001369 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001370
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001371 expression = ast_for_expr(c, CHILD(ch, i + 2));
1372 if (!expression)
1373 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001374
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001375 asdl_seq_SET(values, i / 4, expression);
1376 }
1377 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 }
1379 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001380 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001381 if (Py_Py3kWarningFlag &&
1382 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001383 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001384 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001385 if (!expression)
1386 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001387
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001388 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389 }
1390 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001391 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1392 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 }
1394}
1395
1396static slice_ty
1397ast_for_slice(struct compiling *c, const node *n)
1398{
1399 node *ch;
1400 expr_ty lower = NULL, upper = NULL, step = NULL;
1401
1402 REQ(n, subscript);
1403
1404 /*
1405 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1406 sliceop: ':' [test]
1407 */
1408 ch = CHILD(n, 0);
1409 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001410 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411
1412 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001413 /* 'step' variable hold no significance in terms of being used over
1414 other vars */
1415 step = ast_for_expr(c, ch);
1416 if (!step)
1417 return NULL;
1418
1419 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 }
1421
1422 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001423 lower = ast_for_expr(c, ch);
1424 if (!lower)
1425 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 }
1427
1428 /* If there's an upper bound it's in the second or third position. */
1429 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001430 if (NCH(n) > 1) {
1431 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001433 if (TYPE(n2) == test) {
1434 upper = ast_for_expr(c, n2);
1435 if (!upper)
1436 return NULL;
1437 }
1438 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001440 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001442 if (TYPE(n2) == test) {
1443 upper = ast_for_expr(c, n2);
1444 if (!upper)
1445 return NULL;
1446 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 }
1448
1449 ch = CHILD(n, NCH(n) - 1);
1450 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001451 if (NCH(ch) == 1) {
1452 /* No expression, so step is None */
1453 ch = CHILD(ch, 0);
1454 step = Name(new_identifier("None", c->c_arena), Load,
1455 LINENO(ch), ch->n_col_offset, c->c_arena);
1456 if (!step)
1457 return NULL;
1458 } else {
1459 ch = CHILD(ch, 1);
1460 if (TYPE(ch) == test) {
1461 step = ast_for_expr(c, ch);
1462 if (!step)
1463 return NULL;
1464 }
1465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 }
1467
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001468 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469}
1470
1471static expr_ty
1472ast_for_binop(struct compiling *c, const node *n)
1473{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001474 /* Must account for a sequence of expressions.
1475 How should A op B op C by represented?
1476 BinOp(BinOp(A, op, B), op, C).
1477 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001479 int i, nops;
1480 expr_ty expr1, expr2, result;
1481 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001483 expr1 = ast_for_expr(c, CHILD(n, 0));
1484 if (!expr1)
1485 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001487 expr2 = ast_for_expr(c, CHILD(n, 2));
1488 if (!expr2)
1489 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001491 newoperator = get_operator(CHILD(n, 1));
1492 if (!newoperator)
1493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001495 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1496 c->c_arena);
1497 if (!result)
1498 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001500 nops = (NCH(n) - 1) / 2;
1501 for (i = 1; i < nops; i++) {
1502 expr_ty tmp_result, tmp;
1503 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001505 newoperator = get_operator(next_oper);
1506 if (!newoperator)
1507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001509 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1510 if (!tmp)
1511 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001513 tmp_result = BinOp(result, newoperator, tmp,
1514 LINENO(next_oper), next_oper->n_col_offset,
1515 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001516 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001517 return NULL;
1518 result = tmp_result;
1519 }
1520 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521}
1522
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001523static expr_ty
1524ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1525{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001526 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1527 subscriptlist: subscript (',' subscript)* [',']
1528 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1529 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001530 REQ(n, trailer);
1531 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001532 if (NCH(n) == 2)
1533 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1534 n->n_col_offset, c->c_arena);
1535 else
1536 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001537 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001538 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001539 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1540 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001541 }
1542 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001543 REQ(CHILD(n, 0), LSQB);
1544 REQ(CHILD(n, 2), RSQB);
1545 n = CHILD(n, 1);
1546 if (NCH(n) == 1) {
1547 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1548 if (!slc)
1549 return NULL;
1550 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1551 c->c_arena);
1552 }
1553 else {
1554 /* The grammar is ambiguous here. The ambiguity is resolved
1555 by treating the sequence as a tuple literal if there are
1556 no slice features.
1557 */
1558 int j;
1559 slice_ty slc;
1560 expr_ty e;
1561 bool simple = true;
1562 asdl_seq *slices, *elts;
1563 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1564 if (!slices)
1565 return NULL;
1566 for (j = 0; j < NCH(n); j += 2) {
1567 slc = ast_for_slice(c, CHILD(n, j));
1568 if (!slc)
1569 return NULL;
1570 if (slc->kind != Index_kind)
1571 simple = false;
1572 asdl_seq_SET(slices, j / 2, slc);
1573 }
1574 if (!simple) {
1575 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1576 Load, LINENO(n), n->n_col_offset, c->c_arena);
1577 }
1578 /* extract Index values and put them in a Tuple */
1579 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1580 if (!elts)
1581 return NULL;
1582 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1583 slc = (slice_ty)asdl_seq_GET(slices, j);
1584 assert(slc->kind == Index_kind && slc->v.Index.value);
1585 asdl_seq_SET(elts, j, slc->v.Index.value);
1586 }
1587 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1588 if (!e)
1589 return NULL;
1590 return Subscript(left_expr, Index(e, c->c_arena),
1591 Load, LINENO(n), n->n_col_offset, c->c_arena);
1592 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001593 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001594}
1595
1596static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001597ast_for_factor(struct compiling *c, const node *n)
1598{
1599 node *pfactor, *ppower, *patom, *pnum;
1600 expr_ty expression;
1601
1602 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001603 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001604 constant. The peephole optimizer already does something like
1605 this but it doesn't handle the case where the constant is
1606 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1607 PyLongObject.
1608 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001609 if (TYPE(CHILD(n, 0)) == MINUS &&
1610 NCH(n) == 2 &&
1611 TYPE((pfactor = CHILD(n, 1))) == factor &&
1612 NCH(pfactor) == 1 &&
1613 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1614 NCH(ppower) == 1 &&
1615 TYPE((patom = CHILD(ppower, 0))) == atom &&
1616 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1617 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1618 if (s == NULL)
1619 return NULL;
1620 s[0] = '-';
1621 strcpy(s + 1, STR(pnum));
1622 PyObject_FREE(STR(pnum));
1623 STR(pnum) = s;
1624 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001625 }
1626
1627 expression = ast_for_expr(c, CHILD(n, 1));
1628 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001629 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001630
1631 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001632 case PLUS:
1633 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1634 c->c_arena);
1635 case MINUS:
1636 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1637 c->c_arena);
1638 case TILDE:
1639 return UnaryOp(Invert, expression, LINENO(n),
1640 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001641 }
1642 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001643 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001644 return NULL;
1645}
1646
1647static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001648ast_for_power(struct compiling *c, const node *n)
1649{
1650 /* power: atom trailer* ('**' factor)*
1651 */
1652 int i;
1653 expr_ty e, tmp;
1654 REQ(n, power);
1655 e = ast_for_atom(c, CHILD(n, 0));
1656 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001657 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001658 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001659 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001660 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001661 node *ch = CHILD(n, i);
1662 if (TYPE(ch) != trailer)
1663 break;
1664 tmp = ast_for_trailer(c, ch, e);
1665 if (!tmp)
1666 return NULL;
1667 tmp->lineno = e->lineno;
1668 tmp->col_offset = e->col_offset;
1669 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001670 }
1671 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001672 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1673 if (!f)
1674 return NULL;
1675 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1676 if (!tmp)
1677 return NULL;
1678 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001679 }
1680 return e;
1681}
1682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683/* Do not name a variable 'expr'! Will cause a compile error.
1684*/
1685
1686static expr_ty
1687ast_for_expr(struct compiling *c, const node *n)
1688{
1689 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001690 test: or_test ['if' or_test 'else' test] | lambdef
1691 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 and_test: not_test ('and' not_test)*
1693 not_test: 'not' not_test | comparison
1694 comparison: expr (comp_op expr)*
1695 expr: xor_expr ('|' xor_expr)*
1696 xor_expr: and_expr ('^' and_expr)*
1697 and_expr: shift_expr ('&' shift_expr)*
1698 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1699 arith_expr: term (('+'|'-') term)*
1700 term: factor (('*'|'/'|'%'|'//') factor)*
1701 factor: ('+'|'-'|'~') factor | power
1702 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001703
1704 As well as modified versions that exist for backward compatibility,
1705 to explicitly allow:
1706 [ x for x in lambda: 0, lambda: 1 ]
1707 (which would be ambiguous without these extra rules)
1708
1709 old_test: or_test | old_lambdef
1710 old_lambdef: 'lambda' [vararglist] ':' old_test
1711
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 */
1713
1714 asdl_seq *seq;
1715 int i;
1716
1717 loop:
1718 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001719 case test:
1720 case old_test:
1721 if (TYPE(CHILD(n, 0)) == lambdef ||
1722 TYPE(CHILD(n, 0)) == old_lambdef)
1723 return ast_for_lambdef(c, CHILD(n, 0));
1724 else if (NCH(n) > 1)
1725 return ast_for_ifexpr(c, n);
1726 /* Fallthrough */
1727 case or_test:
1728 case and_test:
1729 if (NCH(n) == 1) {
1730 n = CHILD(n, 0);
1731 goto loop;
1732 }
1733 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1734 if (!seq)
1735 return NULL;
1736 for (i = 0; i < NCH(n); i += 2) {
1737 expr_ty e = ast_for_expr(c, CHILD(n, i));
1738 if (!e)
1739 return NULL;
1740 asdl_seq_SET(seq, i / 2, e);
1741 }
1742 if (!strcmp(STR(CHILD(n, 1)), "and"))
1743 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1744 c->c_arena);
1745 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1746 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1747 case not_test:
1748 if (NCH(n) == 1) {
1749 n = CHILD(n, 0);
1750 goto loop;
1751 }
1752 else {
1753 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1754 if (!expression)
1755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001757 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1758 c->c_arena);
1759 }
1760 case comparison:
1761 if (NCH(n) == 1) {
1762 n = CHILD(n, 0);
1763 goto loop;
1764 }
1765 else {
1766 expr_ty expression;
1767 asdl_int_seq *ops;
1768 asdl_seq *cmps;
1769 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1770 if (!ops)
1771 return NULL;
1772 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1773 if (!cmps) {
1774 return NULL;
1775 }
1776 for (i = 1; i < NCH(n); i += 2) {
1777 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001779 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001780 if (!newoperator) {
1781 return NULL;
1782 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001784 expression = ast_for_expr(c, CHILD(n, i + 1));
1785 if (!expression) {
1786 return NULL;
1787 }
1788
1789 asdl_seq_SET(ops, i / 2, newoperator);
1790 asdl_seq_SET(cmps, i / 2, expression);
1791 }
1792 expression = ast_for_expr(c, CHILD(n, 0));
1793 if (!expression) {
1794 return NULL;
1795 }
1796
1797 return Compare(expression, ops, cmps, LINENO(n),
1798 n->n_col_offset, c->c_arena);
1799 }
1800 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001802 /* The next five cases all handle BinOps. The main body of code
1803 is the same in each case, but the switch turned inside out to
1804 reuse the code for each type of operator.
1805 */
1806 case expr:
1807 case xor_expr:
1808 case and_expr:
1809 case shift_expr:
1810 case arith_expr:
1811 case term:
1812 if (NCH(n) == 1) {
1813 n = CHILD(n, 0);
1814 goto loop;
1815 }
1816 return ast_for_binop(c, n);
1817 case yield_expr: {
1818 expr_ty exp = NULL;
1819 if (NCH(n) == 2) {
1820 exp = ast_for_testlist(c, CHILD(n, 1));
1821 if (!exp)
1822 return NULL;
1823 }
1824 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1825 }
1826 case factor:
1827 if (NCH(n) == 1) {
1828 n = CHILD(n, 0);
1829 goto loop;
1830 }
1831 return ast_for_factor(c, n);
1832 case power:
1833 return ast_for_power(c, n);
1834 default:
1835 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001838 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 return NULL;
1840}
1841
1842static expr_ty
1843ast_for_call(struct compiling *c, const node *n, expr_ty func)
1844{
1845 /*
1846 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001847 | '**' test)
1848 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 */
1850
1851 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001852 asdl_seq *args;
1853 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 expr_ty vararg = NULL, kwarg = NULL;
1855
1856 REQ(n, arglist);
1857
1858 nargs = 0;
1859 nkeywords = 0;
1860 ngens = 0;
1861 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001862 node *ch = CHILD(n, i);
1863 if (TYPE(ch) == argument) {
1864 if (NCH(ch) == 1)
1865 nargs++;
1866 else if (TYPE(CHILD(ch, 1)) == gen_for)
1867 ngens++;
1868 else
1869 nkeywords++;
1870 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871 }
1872 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001873 ast_error(n, "Generator expression must be parenthesized "
1874 "if not sole argument");
1875 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 }
1877
1878 if (nargs + nkeywords + ngens > 255) {
1879 ast_error(n, "more than 255 arguments");
1880 return NULL;
1881 }
1882
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001883 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001885 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001888 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 nargs = 0;
1890 nkeywords = 0;
1891 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001892 node *ch = CHILD(n, i);
1893 if (TYPE(ch) == argument) {
1894 expr_ty e;
1895 if (NCH(ch) == 1) {
1896 if (nkeywords) {
1897 ast_error(CHILD(ch, 0),
1898 "non-keyword arg after keyword arg");
1899 return NULL;
1900 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001901 if (vararg) {
1902 ast_error(CHILD(ch, 0),
1903 "only named arguments may follow *expression");
1904 return NULL;
1905 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001906 e = ast_for_expr(c, CHILD(ch, 0));
1907 if (!e)
1908 return NULL;
1909 asdl_seq_SET(args, nargs++, e);
1910 }
1911 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1912 e = ast_for_genexp(c, ch);
1913 if (!e)
1914 return NULL;
1915 asdl_seq_SET(args, nargs++, e);
1916 }
1917 else {
1918 keyword_ty kw;
1919 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001920 int k;
1921 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001923 /* CHILD(ch, 0) is test, but must be an identifier? */
1924 e = ast_for_expr(c, CHILD(ch, 0));
1925 if (!e)
1926 return NULL;
1927 /* f(lambda x: x[0] = 3) ends up getting parsed with
1928 * LHS test = lambda x: x[0], and RHS test = 3.
1929 * SF bug 132313 points out that complaining about a keyword
1930 * then is very confusing.
1931 */
1932 if (e->kind == Lambda_kind) {
1933 ast_error(CHILD(ch, 0),
1934 "lambda cannot contain assignment");
1935 return NULL;
1936 } else if (e->kind != Name_kind) {
1937 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1938 return NULL;
1939 }
1940 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001941 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001942 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001943 for (k = 0; k < nkeywords; k++) {
1944 tmp = PyString_AS_STRING(
1945 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
1946 if (!strcmp(tmp, PyString_AS_STRING(key))) {
1947 ast_error(CHILD(ch, 0), "keyword argument repeated");
1948 return NULL;
1949 }
1950 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001951 e = ast_for_expr(c, CHILD(ch, 2));
1952 if (!e)
1953 return NULL;
1954 kw = keyword(key, e, c->c_arena);
1955 if (!kw)
1956 return NULL;
1957 asdl_seq_SET(keywords, nkeywords++, kw);
1958 }
1959 }
1960 else if (TYPE(ch) == STAR) {
1961 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001962 if (!vararg)
1963 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001964 i++;
1965 }
1966 else if (TYPE(ch) == DOUBLESTAR) {
1967 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001968 if (!kwarg)
1969 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001970 i++;
1971 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 }
1973
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001974 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1975 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976}
1977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001979ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001981 /* testlist_gexp: test (',' test)* [','] */
1982 /* testlist: test (',' test)* [','] */
1983 /* testlist_safe: test (',' test)+ [','] */
1984 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001986 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001987 if (NCH(n) > 1)
1988 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001989 }
1990 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001991 assert(TYPE(n) == testlist ||
1992 TYPE(n) == testlist_safe ||
1993 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001994 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001996 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001998 asdl_seq *tmp = seq_for_testlist(c, n);
1999 if (!tmp)
2000 return NULL;
2001 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002003}
2004
2005static expr_ty
2006ast_for_testlist_gexp(struct compiling *c, const node* n)
2007{
2008 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2009 /* argument: test [ gen_for ] */
2010 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002011 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002012 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002013 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002014}
2015
2016/* like ast_for_testlist() but returns a sequence */
2017static asdl_seq*
2018ast_for_class_bases(struct compiling *c, const node* n)
2019{
2020 /* testlist: test (',' test)* [','] */
2021 assert(NCH(n) > 0);
2022 REQ(n, testlist);
2023 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002024 expr_ty base;
2025 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2026 if (!bases)
2027 return NULL;
2028 base = ast_for_expr(c, CHILD(n, 0));
2029 if (!base)
2030 return NULL;
2031 asdl_seq_SET(bases, 0, base);
2032 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002033 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002034
2035 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036}
2037
2038static stmt_ty
2039ast_for_expr_stmt(struct compiling *c, const node *n)
2040{
2041 REQ(n, expr_stmt);
2042 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002043 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044 testlist: test (',' test)* [',']
2045 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002046 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 test: ... here starts the operator precendence dance
2048 */
2049
2050 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002051 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2052 if (!e)
2053 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002055 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 }
2057 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002058 expr_ty expr1, expr2;
2059 operator_ty newoperator;
2060 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002062 expr1 = ast_for_testlist(c, ch);
2063 if (!expr1)
2064 return NULL;
2065 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2066 switch (expr1->kind) {
2067 case GeneratorExp_kind:
2068 ast_error(ch, "augmented assignment to generator "
2069 "expression not possible");
2070 return NULL;
2071 case Yield_kind:
2072 ast_error(ch, "augmented assignment to yield "
2073 "expression not possible");
2074 return NULL;
2075 case Name_kind: {
Christian Heimes593daf52008-05-26 12:51:38 +00002076 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
Benjamin Petersond5efd202008-06-08 22:52:37 +00002077 if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') &&
2078 !forbidden_check(c, ch, var_name))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002079 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002080 break;
2081 }
2082 case Attribute_kind:
2083 case Subscript_kind:
2084 break;
2085 default:
2086 ast_error(ch, "illegal expression for augmented "
2087 "assignment");
2088 return NULL;
2089 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002090 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002091 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002093 ch = CHILD(n, 2);
2094 if (TYPE(ch) == testlist)
2095 expr2 = ast_for_testlist(c, ch);
2096 else
2097 expr2 = ast_for_expr(c, ch);
2098 if (!expr2)
2099 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002101 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002102 if (!newoperator)
2103 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002105 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2106 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 }
2108 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002109 int i;
2110 asdl_seq *targets;
2111 node *value;
2112 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002114 /* a normal assignment */
2115 REQ(CHILD(n, 1), EQUAL);
2116 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2117 if (!targets)
2118 return NULL;
2119 for (i = 0; i < NCH(n) - 2; i += 2) {
2120 expr_ty e;
2121 node *ch = CHILD(n, i);
2122 if (TYPE(ch) == yield_expr) {
2123 ast_error(ch, "assignment to yield expression not possible");
2124 return NULL;
2125 }
2126 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002128 /* set context to assign */
2129 if (!e)
2130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002132 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002133 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002135 asdl_seq_SET(targets, i / 2, e);
2136 }
2137 value = CHILD(n, NCH(n) - 1);
2138 if (TYPE(value) == testlist)
2139 expression = ast_for_testlist(c, value);
2140 else
2141 expression = ast_for_expr(c, value);
2142 if (!expression)
2143 return NULL;
2144 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2145 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147}
2148
2149static stmt_ty
2150ast_for_print_stmt(struct compiling *c, const node *n)
2151{
2152 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002153 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 */
2155 expr_ty dest = NULL, expression;
2156 asdl_seq *seq;
2157 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002158 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159
2160 REQ(n, print_stmt);
2161 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002162 dest = ast_for_expr(c, CHILD(n, 2));
2163 if (!dest)
2164 return NULL;
2165 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002167 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002169 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002170 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002171 expression = ast_for_expr(c, CHILD(n, i));
2172 if (!expression)
2173 return NULL;
2174 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 }
2176 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002177 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178}
2179
2180static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002181ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182{
2183 asdl_seq *seq;
2184 int i;
2185 expr_ty e;
2186
2187 REQ(n, exprlist);
2188
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002189 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002191 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002193 e = ast_for_expr(c, CHILD(n, i));
2194 if (!e)
2195 return NULL;
2196 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002197 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 }
2200 return seq;
2201}
2202
2203static stmt_ty
2204ast_for_del_stmt(struct compiling *c, const node *n)
2205{
2206 asdl_seq *expr_list;
2207
2208 /* del_stmt: 'del' exprlist */
2209 REQ(n, del_stmt);
2210
2211 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2212 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002213 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002214 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215}
2216
2217static stmt_ty
2218ast_for_flow_stmt(struct compiling *c, const node *n)
2219{
2220 /*
2221 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002222 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 break_stmt: 'break'
2224 continue_stmt: 'continue'
2225 return_stmt: 'return' [testlist]
2226 yield_stmt: yield_expr
2227 yield_expr: 'yield' testlist
2228 raise_stmt: 'raise' [test [',' test [',' test]]]
2229 */
2230 node *ch;
2231
2232 REQ(n, flow_stmt);
2233 ch = CHILD(n, 0);
2234 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002235 case break_stmt:
2236 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2237 case continue_stmt:
2238 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2239 case yield_stmt: { /* will reduce to yield_expr */
2240 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2241 if (!exp)
2242 return NULL;
2243 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2244 }
2245 case return_stmt:
2246 if (NCH(ch) == 1)
2247 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2248 else {
2249 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2250 if (!expression)
2251 return NULL;
2252 return Return(expression, LINENO(n), n->n_col_offset,
2253 c->c_arena);
2254 }
2255 case raise_stmt:
2256 if (NCH(ch) == 1)
2257 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2258 c->c_arena);
2259 else if (NCH(ch) == 2) {
2260 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2261 if (!expression)
2262 return NULL;
2263 return Raise(expression, NULL, NULL, LINENO(n),
2264 n->n_col_offset, c->c_arena);
2265 }
2266 else if (NCH(ch) == 4) {
2267 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002269 expr1 = ast_for_expr(c, CHILD(ch, 1));
2270 if (!expr1)
2271 return NULL;
2272 expr2 = ast_for_expr(c, CHILD(ch, 3));
2273 if (!expr2)
2274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002276 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2277 c->c_arena);
2278 }
2279 else if (NCH(ch) == 6) {
2280 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002282 expr1 = ast_for_expr(c, CHILD(ch, 1));
2283 if (!expr1)
2284 return NULL;
2285 expr2 = ast_for_expr(c, CHILD(ch, 3));
2286 if (!expr2)
2287 return NULL;
2288 expr3 = ast_for_expr(c, CHILD(ch, 5));
2289 if (!expr3)
2290 return NULL;
2291
2292 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2293 c->c_arena);
2294 }
2295 default:
2296 PyErr_Format(PyExc_SystemError,
2297 "unexpected flow_stmt: %d", TYPE(ch));
2298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002300
2301 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2302 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303}
2304
2305static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002306alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307{
2308 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002309 import_as_name: NAME ['as' NAME]
2310 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 dotted_name: NAME ('.' NAME)*
2312 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002313 PyObject *str;
2314
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315 loop:
2316 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002317 case import_as_name:
2318 str = NULL;
2319 if (NCH(n) == 3) {
2320 str = NEW_IDENTIFIER(CHILD(n, 2));
2321 }
2322 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2323 case dotted_as_name:
2324 if (NCH(n) == 1) {
2325 n = CHILD(n, 0);
2326 goto loop;
2327 }
2328 else {
2329 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2330 if (!a)
2331 return NULL;
2332 assert(!a->asname);
2333 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2334 return a;
2335 }
2336 break;
2337 case dotted_name:
2338 if (NCH(n) == 1)
2339 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2340 else {
2341 /* Create a string of the form "a.b.c" */
2342 int i;
2343 size_t len;
2344 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002346 len = 0;
2347 for (i = 0; i < NCH(n); i += 2)
2348 /* length of string plus one for the dot */
2349 len += strlen(STR(CHILD(n, i))) + 1;
2350 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002351 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002352 if (!str)
2353 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002354 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002355 if (!s)
2356 return NULL;
2357 for (i = 0; i < NCH(n); i += 2) {
2358 char *sch = STR(CHILD(n, i));
2359 strcpy(s, STR(CHILD(n, i)));
2360 s += strlen(sch);
2361 *s++ = '.';
2362 }
2363 --s;
2364 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002365 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002366 PyArena_AddPyObject(c->c_arena, str);
2367 return alias(str, NULL, c->c_arena);
2368 }
2369 break;
2370 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002371 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002372 PyArena_AddPyObject(c->c_arena, str);
2373 return alias(str, NULL, c->c_arena);
2374 default:
2375 PyErr_Format(PyExc_SystemError,
2376 "unexpected import name: %d", TYPE(n));
2377 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002379
2380 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381 return NULL;
2382}
2383
2384static stmt_ty
2385ast_for_import_stmt(struct compiling *c, const node *n)
2386{
2387 /*
2388 import_stmt: import_name | import_from
2389 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002390 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002391 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002393 int lineno;
2394 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 int i;
2396 asdl_seq *aliases;
2397
2398 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002399 lineno = LINENO(n);
2400 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002402 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002403 n = CHILD(n, 1);
2404 REQ(n, dotted_as_names);
2405 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2406 if (!aliases)
2407 return NULL;
2408 for (i = 0; i < NCH(n); i += 2) {
2409 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2410 if (!import_alias)
2411 return NULL;
2412 asdl_seq_SET(aliases, i / 2, import_alias);
2413 }
2414 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002416 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002417 int n_children;
2418 int idx, ndots = 0;
2419 alias_ty mod = NULL;
2420 identifier modname;
2421
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002422 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002423 optional module name */
2424 for (idx = 1; idx < NCH(n); idx++) {
2425 if (TYPE(CHILD(n, idx)) == dotted_name) {
2426 mod = alias_for_import_name(c, CHILD(n, idx));
2427 idx++;
2428 break;
2429 } else if (TYPE(CHILD(n, idx)) != DOT) {
2430 break;
2431 }
2432 ndots++;
2433 }
2434 idx++; /* skip over the 'import' keyword */
2435 switch (TYPE(CHILD(n, idx))) {
2436 case STAR:
2437 /* from ... import * */
2438 n = CHILD(n, idx);
2439 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002440 break;
2441 case LPAR:
2442 /* from ... import (x, y, z) */
2443 n = CHILD(n, idx + 1);
2444 n_children = NCH(n);
2445 break;
2446 case import_as_names:
2447 /* from ... import x, y, z */
2448 n = CHILD(n, idx);
2449 n_children = NCH(n);
2450 if (n_children % 2 == 0) {
2451 ast_error(n, "trailing comma not allowed without"
2452 " surrounding parentheses");
2453 return NULL;
2454 }
2455 break;
2456 default:
2457 ast_error(n, "Unexpected node-type in from-import");
2458 return NULL;
2459 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002461 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2462 if (!aliases)
2463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002465 /* handle "from ... import *" special b/c there's no children */
2466 if (TYPE(n) == STAR) {
2467 alias_ty import_alias = alias_for_import_name(c, n);
2468 if (!import_alias)
2469 return NULL;
2470 asdl_seq_SET(aliases, 0, import_alias);
2471 }
2472 else {
2473 for (i = 0; i < NCH(n); i += 2) {
2474 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2475 if (!import_alias)
2476 return NULL;
2477 asdl_seq_SET(aliases, i / 2, import_alias);
2478 }
2479 }
2480 if (mod != NULL)
2481 modname = mod->name;
2482 else
2483 modname = new_identifier("", c->c_arena);
2484 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2485 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 }
Neal Norwitz79792652005-11-14 04:25:03 +00002487 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002488 "unknown import statement: starts with command '%s'",
2489 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 return NULL;
2491}
2492
2493static stmt_ty
2494ast_for_global_stmt(struct compiling *c, const node *n)
2495{
2496 /* global_stmt: 'global' NAME (',' NAME)* */
2497 identifier name;
2498 asdl_seq *s;
2499 int i;
2500
2501 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002502 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002504 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002506 name = NEW_IDENTIFIER(CHILD(n, i));
2507 if (!name)
2508 return NULL;
2509 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002511 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512}
2513
2514static stmt_ty
2515ast_for_exec_stmt(struct compiling *c, const node *n)
2516{
2517 expr_ty expr1, globals = NULL, locals = NULL;
2518 int n_children = NCH(n);
2519 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002520 PyErr_Format(PyExc_SystemError,
2521 "poorly formed 'exec' statement: %d parts to statement",
2522 n_children);
2523 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 }
2525
2526 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2527 REQ(n, exec_stmt);
2528 expr1 = ast_for_expr(c, CHILD(n, 1));
2529 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002530 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002532 globals = ast_for_expr(c, CHILD(n, 3));
2533 if (!globals)
2534 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 }
2536 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002537 locals = ast_for_expr(c, CHILD(n, 5));
2538 if (!locals)
2539 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 }
2541
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002542 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2543 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544}
2545
2546static stmt_ty
2547ast_for_assert_stmt(struct compiling *c, const node *n)
2548{
2549 /* assert_stmt: 'assert' test [',' test] */
2550 REQ(n, assert_stmt);
2551 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002552 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2553 if (!expression)
2554 return NULL;
2555 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2556 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 }
2558 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002559 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002561 expr1 = ast_for_expr(c, CHILD(n, 1));
2562 if (!expr1)
2563 return NULL;
2564 expr2 = ast_for_expr(c, CHILD(n, 3));
2565 if (!expr2)
2566 return NULL;
2567
2568 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 }
Neal Norwitz79792652005-11-14 04:25:03 +00002570 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002571 "improper number of parts to 'assert' statement: %d",
2572 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 return NULL;
2574}
2575
2576static asdl_seq *
2577ast_for_suite(struct compiling *c, const node *n)
2578{
2579 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002580 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 stmt_ty s;
2582 int i, total, num, end, pos = 0;
2583 node *ch;
2584
2585 REQ(n, suite);
2586
2587 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002588 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002592 n = CHILD(n, 0);
2593 /* simple_stmt always ends with a NEWLINE,
2594 and may have a trailing SEMI
2595 */
2596 end = NCH(n) - 1;
2597 if (TYPE(CHILD(n, end - 1)) == SEMI)
2598 end--;
2599 /* loop by 2 to skip semi-colons */
2600 for (i = 0; i < end; i += 2) {
2601 ch = CHILD(n, i);
2602 s = ast_for_stmt(c, ch);
2603 if (!s)
2604 return NULL;
2605 asdl_seq_SET(seq, pos++, s);
2606 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 }
2608 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002609 for (i = 2; i < (NCH(n) - 1); i++) {
2610 ch = CHILD(n, i);
2611 REQ(ch, stmt);
2612 num = num_stmts(ch);
2613 if (num == 1) {
2614 /* small_stmt or compound_stmt with only one child */
2615 s = ast_for_stmt(c, ch);
2616 if (!s)
2617 return NULL;
2618 asdl_seq_SET(seq, pos++, s);
2619 }
2620 else {
2621 int j;
2622 ch = CHILD(ch, 0);
2623 REQ(ch, simple_stmt);
2624 for (j = 0; j < NCH(ch); j += 2) {
2625 /* statement terminates with a semi-colon ';' */
2626 if (NCH(CHILD(ch, j)) == 0) {
2627 assert((j + 1) == NCH(ch));
2628 break;
2629 }
2630 s = ast_for_stmt(c, CHILD(ch, j));
2631 if (!s)
2632 return NULL;
2633 asdl_seq_SET(seq, pos++, s);
2634 }
2635 }
2636 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 }
2638 assert(pos == seq->size);
2639 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640}
2641
2642static stmt_ty
2643ast_for_if_stmt(struct compiling *c, const node *n)
2644{
2645 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2646 ['else' ':' suite]
2647 */
2648 char *s;
2649
2650 REQ(n, if_stmt);
2651
2652 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002653 expr_ty expression;
2654 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002656 expression = ast_for_expr(c, CHILD(n, 1));
2657 if (!expression)
2658 return NULL;
2659 suite_seq = ast_for_suite(c, CHILD(n, 3));
2660 if (!suite_seq)
2661 return NULL;
2662
2663 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2664 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002666
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 s = STR(CHILD(n, 4));
2668 /* s[2], the third character in the string, will be
2669 's' for el_s_e, or
2670 'i' for el_i_f
2671 */
2672 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002673 expr_ty expression;
2674 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002676 expression = ast_for_expr(c, CHILD(n, 1));
2677 if (!expression)
2678 return NULL;
2679 seq1 = ast_for_suite(c, CHILD(n, 3));
2680 if (!seq1)
2681 return NULL;
2682 seq2 = ast_for_suite(c, CHILD(n, 6));
2683 if (!seq2)
2684 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002686 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2687 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 }
2689 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002690 int i, n_elif, has_else = 0;
2691 expr_ty expression;
2692 asdl_seq *suite_seq;
2693 asdl_seq *orelse = NULL;
2694 n_elif = NCH(n) - 4;
2695 /* must reference the child n_elif+1 since 'else' token is third,
2696 not fourth, child from the end. */
2697 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2698 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2699 has_else = 1;
2700 n_elif -= 3;
2701 }
2702 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002704 if (has_else) {
2705 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002707 orelse = asdl_seq_new(1, c->c_arena);
2708 if (!orelse)
2709 return NULL;
2710 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2711 if (!expression)
2712 return NULL;
2713 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2714 if (!suite_seq)
2715 return NULL;
2716 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2717 if (!suite_seq2)
2718 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002720 asdl_seq_SET(orelse, 0,
2721 If(expression, suite_seq, suite_seq2,
2722 LINENO(CHILD(n, NCH(n) - 6)),
2723 CHILD(n, NCH(n) - 6)->n_col_offset,
2724 c->c_arena));
2725 /* the just-created orelse handled the last elif */
2726 n_elif--;
2727 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002729 for (i = 0; i < n_elif; i++) {
2730 int off = 5 + (n_elif - i - 1) * 4;
2731 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2732 if (!newobj)
2733 return NULL;
2734 expression = ast_for_expr(c, CHILD(n, off));
2735 if (!expression)
2736 return NULL;
2737 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2738 if (!suite_seq)
2739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002741 asdl_seq_SET(newobj, 0,
2742 If(expression, suite_seq, orelse,
2743 LINENO(CHILD(n, off)),
2744 CHILD(n, off)->n_col_offset, c->c_arena));
2745 orelse = newobj;
2746 }
2747 expression = ast_for_expr(c, CHILD(n, 1));
2748 if (!expression)
2749 return NULL;
2750 suite_seq = ast_for_suite(c, CHILD(n, 3));
2751 if (!suite_seq)
2752 return NULL;
2753 return If(expression, suite_seq, orelse,
2754 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002756
2757 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002758 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002759 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760}
2761
2762static stmt_ty
2763ast_for_while_stmt(struct compiling *c, const node *n)
2764{
2765 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2766 REQ(n, while_stmt);
2767
2768 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002769 expr_ty expression;
2770 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002772 expression = ast_for_expr(c, CHILD(n, 1));
2773 if (!expression)
2774 return NULL;
2775 suite_seq = ast_for_suite(c, CHILD(n, 3));
2776 if (!suite_seq)
2777 return NULL;
2778 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2779 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 }
2781 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002782 expr_ty expression;
2783 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002785 expression = ast_for_expr(c, CHILD(n, 1));
2786 if (!expression)
2787 return NULL;
2788 seq1 = ast_for_suite(c, CHILD(n, 3));
2789 if (!seq1)
2790 return NULL;
2791 seq2 = ast_for_suite(c, CHILD(n, 6));
2792 if (!seq2)
2793 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002795 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2796 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002798
2799 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002800 "wrong number of tokens for 'while' statement: %d",
2801 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002802 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803}
2804
2805static stmt_ty
2806ast_for_for_stmt(struct compiling *c, const node *n)
2807{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002808 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 expr_ty expression;
2810 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002811 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2813 REQ(n, for_stmt);
2814
2815 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002816 seq = ast_for_suite(c, CHILD(n, 8));
2817 if (!seq)
2818 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 }
2820
Neal Norwitzedef2be2006-07-12 05:26:17 +00002821 node_target = CHILD(n, 1);
2822 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002823 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002824 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002825 /* Check the # of children rather than the length of _target, since
2826 for x, in ... has 1 element in _target, but still requires a Tuple. */
2827 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002828 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002830 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002832 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002833 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002836 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002837 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002839 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002840 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841}
2842
2843static excepthandler_ty
2844ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2845{
Collin Winter62903052007-05-18 23:11:24 +00002846 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 REQ(exc, except_clause);
2848 REQ(body, suite);
2849
2850 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002851 asdl_seq *suite_seq = ast_for_suite(c, body);
2852 if (!suite_seq)
2853 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854
Georg Brandla48f3ab2008-03-30 06:40:17 +00002855 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002856 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 }
2858 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002859 expr_ty expression;
2860 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002862 expression = ast_for_expr(c, CHILD(exc, 1));
2863 if (!expression)
2864 return NULL;
2865 suite_seq = ast_for_suite(c, body);
2866 if (!suite_seq)
2867 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868
Georg Brandla48f3ab2008-03-30 06:40:17 +00002869 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002870 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 }
2872 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002873 asdl_seq *suite_seq;
2874 expr_ty expression;
2875 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2876 if (!e)
2877 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002878 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002879 return NULL;
2880 expression = ast_for_expr(c, CHILD(exc, 1));
2881 if (!expression)
2882 return NULL;
2883 suite_seq = ast_for_suite(c, body);
2884 if (!suite_seq)
2885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886
Georg Brandla48f3ab2008-03-30 06:40:17 +00002887 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002888 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002890
2891 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002892 "wrong number of children for 'except' clause: %d",
2893 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002894 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895}
2896
2897static stmt_ty
2898ast_for_try_stmt(struct compiling *c, const node *n)
2899{
Neal Norwitzf599f422005-12-17 21:33:47 +00002900 const int nch = NCH(n);
2901 int n_except = (nch - 3)/3;
2902 asdl_seq *body, *orelse = NULL, *finally = NULL;
2903
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 REQ(n, try_stmt);
2905
Neal Norwitzf599f422005-12-17 21:33:47 +00002906 body = ast_for_suite(c, CHILD(n, 2));
2907 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909
Neal Norwitzf599f422005-12-17 21:33:47 +00002910 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002911 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2912 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2913 /* we can assume it's an "else",
2914 because nch >= 9 for try-else-finally and
2915 it would otherwise have a type of except_clause */
2916 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2917 if (orelse == NULL)
2918 return NULL;
2919 n_except--;
2920 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002922 finally = ast_for_suite(c, CHILD(n, nch - 1));
2923 if (finally == NULL)
2924 return NULL;
2925 n_except--;
2926 }
2927 else {
2928 /* we can assume it's an "else",
2929 otherwise it would have a type of except_clause */
2930 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2931 if (orelse == NULL)
2932 return NULL;
2933 n_except--;
2934 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002936 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002937 ast_error(n, "malformed 'try' statement");
2938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002940
2941 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002942 int i;
2943 stmt_ty except_st;
2944 /* process except statements to create a try ... except */
2945 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2946 if (handlers == NULL)
2947 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002948
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002949 for (i = 0; i < n_except; i++) {
2950 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2951 CHILD(n, 5 + i * 3));
2952 if (!e)
2953 return NULL;
2954 asdl_seq_SET(handlers, i, e);
2955 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002956
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002957 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2958 n->n_col_offset, c->c_arena);
2959 if (!finally)
2960 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002961
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002962 /* if a 'finally' is present too, we nest the TryExcept within a
2963 TryFinally to emulate try ... except ... finally */
2964 body = asdl_seq_new(1, c->c_arena);
2965 if (body == NULL)
2966 return NULL;
2967 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002968 }
2969
2970 /* must be a try ... finally (except clauses are in body, if any exist) */
2971 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002972 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973}
2974
Guido van Rossumc2e20742006-02-27 22:32:47 +00002975static expr_ty
2976ast_for_with_var(struct compiling *c, const node *n)
2977{
2978 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002979 return ast_for_expr(c, CHILD(n, 1));
2980}
2981
2982/* with_stmt: 'with' test [ with_var ] ':' suite */
2983static stmt_ty
2984ast_for_with_stmt(struct compiling *c, const node *n)
2985{
2986 expr_ty context_expr, optional_vars = NULL;
2987 int suite_index = 3; /* skip 'with', test, and ':' */
2988 asdl_seq *suite_seq;
2989
2990 assert(TYPE(n) == with_stmt);
2991 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002992 if (!context_expr)
2993 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002994 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002995 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002996
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002997 if (!optional_vars) {
2998 return NULL;
2999 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003000 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003001 return NULL;
3002 }
3003 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003004 }
3005
3006 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
3007 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003008 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003009 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003010 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003011 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003012}
3013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003015ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016{
3017 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 asdl_seq *bases, *s;
3019
3020 REQ(n, classdef);
3021
Benjamin Petersond5efd202008-06-08 22:52:37 +00003022 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003023 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024
3025 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003026 s = ast_for_suite(c, CHILD(n, 3));
3027 if (!s)
3028 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003029 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3030 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 }
3032 /* check for empty base list */
3033 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003034 s = ast_for_suite(c, CHILD(n,5));
3035 if (!s)
3036 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003037 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3038 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039 }
3040
3041 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003042 bases = ast_for_class_bases(c, CHILD(n, 3));
3043 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045
3046 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003047 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003048 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003049 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3050 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051}
3052
3053static stmt_ty
3054ast_for_stmt(struct compiling *c, const node *n)
3055{
3056 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003057 assert(NCH(n) == 1);
3058 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059 }
3060 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003061 assert(num_stmts(n) == 1);
3062 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063 }
3064 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003065 REQ(n, small_stmt);
3066 n = CHILD(n, 0);
3067 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3068 | flow_stmt | import_stmt | global_stmt | exec_stmt
3069 | assert_stmt
3070 */
3071 switch (TYPE(n)) {
3072 case expr_stmt:
3073 return ast_for_expr_stmt(c, n);
3074 case print_stmt:
3075 return ast_for_print_stmt(c, n);
3076 case del_stmt:
3077 return ast_for_del_stmt(c, n);
3078 case pass_stmt:
3079 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3080 case flow_stmt:
3081 return ast_for_flow_stmt(c, n);
3082 case import_stmt:
3083 return ast_for_import_stmt(c, n);
3084 case global_stmt:
3085 return ast_for_global_stmt(c, n);
3086 case exec_stmt:
3087 return ast_for_exec_stmt(c, n);
3088 case assert_stmt:
3089 return ast_for_assert_stmt(c, n);
3090 default:
3091 PyErr_Format(PyExc_SystemError,
3092 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3093 TYPE(n), NCH(n));
3094 return NULL;
3095 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096 }
3097 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003098 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003099 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003100 */
3101 node *ch = CHILD(n, 0);
3102 REQ(n, compound_stmt);
3103 switch (TYPE(ch)) {
3104 case if_stmt:
3105 return ast_for_if_stmt(c, ch);
3106 case while_stmt:
3107 return ast_for_while_stmt(c, ch);
3108 case for_stmt:
3109 return ast_for_for_stmt(c, ch);
3110 case try_stmt:
3111 return ast_for_try_stmt(c, ch);
3112 case with_stmt:
3113 return ast_for_with_stmt(c, ch);
3114 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003115 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003116 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003117 return ast_for_classdef(c, ch, NULL);
3118 case decorated:
3119 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003120 default:
3121 PyErr_Format(PyExc_SystemError,
3122 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3123 TYPE(n), NCH(n));
3124 return NULL;
3125 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 }
3127}
3128
3129static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003130parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003132 const char *end;
3133 long x;
3134 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003136 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003137 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138#endif
3139
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003140 errno = 0;
3141 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003143 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003145 if (*end == 'l' || *end == 'L')
3146 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003147 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003148 if (*end == '\0') {
3149 if (errno != 0)
3150 return PyLong_FromString((char *)s, (char **)0, 0);
3151 return PyInt_FromLong(x);
3152 }
3153 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003155 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003156 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003157 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003158 complex.imag = PyOS_ascii_atof(s);
3159 PyFPE_END_PROTECT(complex)
3160 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003161 }
3162 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003164 {
3165 PyFPE_START_PROTECT("atof", return 0)
3166 dx = PyOS_ascii_atof(s);
3167 PyFPE_END_PROTECT(dx)
3168 return PyFloat_FromDouble(dx);
3169 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170}
3171
3172static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003173decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174{
3175#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003176 Py_FatalError("decode_utf8 should not be called in this build.");
3177 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003179 PyObject *u, *v;
3180 char *s, *t;
3181 t = s = (char *)*sPtr;
3182 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3183 while (s < end && (*s & 0x80)) s++;
3184 *sPtr = s;
3185 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3186 if (u == NULL)
3187 return NULL;
3188 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3189 Py_DECREF(u);
3190 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191#endif
3192}
3193
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003194#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003196decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003198 PyObject *v, *u;
3199 char *buf;
3200 char *p;
3201 const char *end;
3202 if (encoding == NULL) {
3203 buf = (char *)s;
3204 u = NULL;
3205 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3206 buf = (char *)s;
3207 u = NULL;
3208 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003209 /* check for integer overflow */
3210 if (len > PY_SIZE_MAX / 4)
3211 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003212 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003213 u = PyString_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003214 if (u == NULL)
3215 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003216 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003217 end = s + len;
3218 while (s < end) {
3219 if (*s == '\\') {
3220 *p++ = *s++;
3221 if (*s & 0x80) {
3222 strcpy(p, "u005c");
3223 p += 5;
3224 }
3225 }
3226 if (*s & 0x80) { /* XXX inefficient */
3227 PyObject *w;
3228 char *r;
3229 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003230 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003231 if (w == NULL) {
3232 Py_DECREF(u);
3233 return NULL;
3234 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003235 r = PyString_AsString(w);
3236 rn = PyString_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003237 assert(rn % 2 == 0);
3238 for (i = 0; i < rn; i += 2) {
3239 sprintf(p, "\\u%02x%02x",
3240 r[i + 0] & 0xFF,
3241 r[i + 1] & 0xFF);
3242 p += 6;
3243 }
3244 Py_DECREF(w);
3245 } else {
3246 *p++ = *s++;
3247 }
3248 }
3249 len = p - buf;
3250 s = buf;
3251 }
3252 if (rawmode)
3253 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3254 else
3255 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3256 Py_XDECREF(u);
3257 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003259#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260
3261/* s is a Python string literal, including the bracketing quote characters,
3262 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3263 * parsestr parses it, and returns the decoded Python string object.
3264 */
3265static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003266parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003268 size_t len;
3269 int quote = Py_CHARMASK(*s);
3270 int rawmode = 0;
3271 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003272 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003274 if (isalpha(quote) || quote == '_') {
3275 if (quote == 'u' || quote == 'U') {
3276 quote = *++s;
3277 unicode = 1;
3278 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003279 if (quote == 'b' || quote == 'B') {
3280 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003281 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003282 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003283 if (quote == 'r' || quote == 'R') {
3284 quote = *++s;
3285 rawmode = 1;
3286 }
3287 }
3288 if (quote != '\'' && quote != '\"') {
3289 PyErr_BadInternalCall();
3290 return NULL;
3291 }
3292 s++;
3293 len = strlen(s);
3294 if (len > INT_MAX) {
3295 PyErr_SetString(PyExc_OverflowError,
3296 "string to parse is too long");
3297 return NULL;
3298 }
3299 if (s[--len] != quote) {
3300 PyErr_BadInternalCall();
3301 return NULL;
3302 }
3303 if (len >= 4 && s[0] == quote && s[1] == quote) {
3304 s += 2;
3305 len -= 2;
3306 if (s[--len] != quote || s[--len] != quote) {
3307 PyErr_BadInternalCall();
3308 return NULL;
3309 }
3310 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003312 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003313 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003314 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003316 need_encoding = (c->c_encoding != NULL &&
3317 strcmp(c->c_encoding, "utf-8") != 0 &&
3318 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003319 if (rawmode || strchr(s, '\\') == NULL) {
3320 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003322 /* This should not happen - we never see any other
3323 encoding. */
3324 Py_FatalError(
3325 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003327 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3328 if (u == NULL)
3329 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003330 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003331 Py_DECREF(u);
3332 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003334 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003335 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003336 }
3337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003339 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003340 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341}
3342
3343/* Build a Python string object out of a STRING atom. This takes care of
3344 * compile-time literal catenation, calling parsestr() on each piece, and
3345 * pasting the intermediate results together.
3346 */
3347static PyObject *
3348parsestrplus(struct compiling *c, const node *n)
3349{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003350 PyObject *v;
3351 int i;
3352 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003353 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003354 /* String literal concatenation */
3355 for (i = 1; i < NCH(n); i++) {
3356 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003357 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003358 if (s == NULL)
3359 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003360 if (PyString_Check(v) && PyString_Check(s)) {
3361 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003362 if (v == NULL)
3363 goto onError;
3364 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003366 else {
3367 PyObject *temp = PyUnicode_Concat(v, s);
3368 Py_DECREF(s);
3369 Py_DECREF(v);
3370 v = temp;
3371 if (v == NULL)
3372 goto onError;
3373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003375 }
3376 }
3377 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378
3379 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003380 Py_XDECREF(v);
3381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382}