blob: d7e3b360479a4d9c96f4c2951fa3bcbf7f58697d [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 */
21 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022};
23
24static asdl_seq *seq_for_testlist(struct compiling *, const node *);
25static expr_ty ast_for_expr(struct compiling *, const node *);
26static stmt_ty ast_for_stmt(struct compiling *, const node *);
27static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +000028static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
29 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000030static expr_ty ast_for_testlist(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000031
32/* Note different signature for ast_for_call */
33static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
34
35static PyObject *parsenumber(const char *);
Thomas Wouters00e41de2007-02-23 19:56:57 +000036static PyObject *parsestr(const node *n, const char *encoding, int *bytesmode);
37static PyObject *parsestrplus(struct compiling *, const node *n,
38 int *bytesmode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040#ifndef LINENO
Thomas Wouters89f507f2006-12-13 04:49:30 +000041#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042#endif
43
Nick Coghlan650f0d02007-04-15 12:05:43 +000044#define COMP_GENEXP 0
45#define COMP_LISTCOMP 1
46#define COMP_SETCOMP 2
47
Neal Norwitzadb69fc2005-12-17 20:54:49 +000048static identifier
49new_identifier(const char* n, PyArena *arena) {
50 PyObject* id = PyString_InternFromString(n);
51 PyArena_AddPyObject(arena, id);
52 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053}
54
Neal Norwitzadb69fc2005-12-17 20:54:49 +000055#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056
57/* This routine provides an invalid object for the syntax error.
58 The outermost routine must unpack this error and create the
59 proper object. We do this so that we don't have to pass
60 the filename to everything function.
61
62 XXX Maybe we should just pass the filename...
63*/
64
65static int
66ast_error(const node *n, const char *errstr)
67{
68 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
69 if (!u)
Thomas Wouters89f507f2006-12-13 04:49:30 +000070 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000071 PyErr_SetObject(PyExc_SyntaxError, u);
72 Py_DECREF(u);
73 return 0;
74}
75
76static void
77ast_error_finish(const char *filename)
78{
79 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000080 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
82 assert(PyErr_Occurred());
83 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Thomas Wouters89f507f2006-12-13 04:49:30 +000084 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085
86 PyErr_Fetch(&type, &value, &tback);
87 errstr = PyTuple_GetItem(value, 0);
88 if (!errstr)
Thomas Wouters89f507f2006-12-13 04:49:30 +000089 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000090 Py_INCREF(errstr);
91 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000092 if (lineno == -1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +000093 Py_DECREF(errstr);
94 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000095 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096 Py_DECREF(value);
97
98 loc = PyErr_ProgramText(filename, lineno);
99 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000100 Py_INCREF(Py_None);
101 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000103 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000105 if (!tmp) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000106 Py_DECREF(errstr);
107 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000108 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 Py_DECREF(errstr);
111 Py_DECREF(tmp);
112 if (!value)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114 PyErr_Restore(type, value, tback);
115}
116
117/* num_stmts() returns number of contained statements.
118
119 Use this routine to determine how big a sequence is needed for
120 the statements in a parse tree. Its raison d'etre is this bit of
121 grammar:
122
123 stmt: simple_stmt | compound_stmt
124 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
125
126 A simple_stmt can contain multiple small_stmt elements joined
127 by semicolons. If the arg is a simple_stmt, the number of
128 small_stmt elements is returned.
129*/
130
131static int
132num_stmts(const node *n)
133{
134 int i, l;
135 node *ch;
136
137 switch (TYPE(n)) {
138 case single_input:
139 if (TYPE(CHILD(n, 0)) == NEWLINE)
140 return 0;
141 else
142 return num_stmts(CHILD(n, 0));
143 case file_input:
144 l = 0;
145 for (i = 0; i < NCH(n); i++) {
146 ch = CHILD(n, i);
147 if (TYPE(ch) == stmt)
148 l += num_stmts(ch);
149 }
150 return l;
151 case stmt:
152 return num_stmts(CHILD(n, 0));
153 case compound_stmt:
154 return 1;
155 case simple_stmt:
156 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
157 case suite:
158 if (NCH(n) == 1)
159 return num_stmts(CHILD(n, 0));
160 else {
161 l = 0;
162 for (i = 2; i < (NCH(n) - 1); i++)
163 l += num_stmts(CHILD(n, i));
164 return l;
165 }
166 default: {
167 char buf[128];
168
169 sprintf(buf, "Non-statement found: %d %d\n",
170 TYPE(n), NCH(n));
171 Py_FatalError(buf);
172 }
173 }
174 assert(0);
175 return 0;
176}
177
178/* Transform the CST rooted at node * to the appropriate AST
179*/
180
181mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000182PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
183 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000185 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186 asdl_seq *stmts = NULL;
187 stmt_ty s;
188 node *ch;
189 struct compiling c;
190
191 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000192 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193 if (TYPE(n) == encoding_decl) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000194 ast_error(n, "encoding declaration in Unicode string");
195 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 } else if (TYPE(n) == encoding_decl) {
198 c.c_encoding = STR(n);
199 n = CHILD(n, 0);
200 } else {
201 c.c_encoding = NULL;
202 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000203 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204
Jeremy Hyltona8293132006-02-28 17:58:27 +0000205 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206 switch (TYPE(n)) {
207 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000208 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 if (!stmts)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211 for (i = 0; i < NCH(n) - 1; i++) {
212 ch = CHILD(n, i);
213 if (TYPE(ch) == NEWLINE)
214 continue;
215 REQ(ch, stmt);
216 num = num_stmts(ch);
217 if (num == 1) {
218 s = ast_for_stmt(&c, ch);
219 if (!s)
220 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000221 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222 }
223 else {
224 ch = CHILD(ch, 0);
225 REQ(ch, simple_stmt);
226 for (j = 0; j < num; j++) {
227 s = ast_for_stmt(&c, CHILD(ch, j * 2));
228 if (!s)
229 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000230 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 }
232 }
233 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000234 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235 case eval_input: {
236 expr_ty testlist_ast;
237
Nick Coghlan650f0d02007-04-15 12:05:43 +0000238 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000239 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 if (!testlist_ast)
241 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000242 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243 }
244 case single_input:
245 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000246 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247 if (!stmts)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000248 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000249 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
250 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000251 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 }
253 else {
254 n = CHILD(n, 0);
255 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000256 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 if (!stmts)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000258 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000260 s = ast_for_stmt(&c, n);
261 if (!s)
262 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263 asdl_seq_SET(stmts, 0, s);
264 }
265 else {
266 /* Only a simple_stmt can contain multiple statements. */
267 REQ(n, simple_stmt);
268 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 if (TYPE(CHILD(n, i)) == NEWLINE)
270 break;
271 s = ast_for_stmt(&c, CHILD(n, i));
272 if (!s)
273 goto error;
274 asdl_seq_SET(stmts, i / 2, s);
275 }
276 }
277
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000278 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 }
280 default:
281 goto error;
282 }
283 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284 ast_error_finish(filename);
285 return NULL;
286}
287
288/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
289*/
290
291static operator_ty
292get_operator(const node *n)
293{
294 switch (TYPE(n)) {
295 case VBAR:
296 return BitOr;
297 case CIRCUMFLEX:
298 return BitXor;
299 case AMPER:
300 return BitAnd;
301 case LEFTSHIFT:
302 return LShift;
303 case RIGHTSHIFT:
304 return RShift;
305 case PLUS:
306 return Add;
307 case MINUS:
308 return Sub;
309 case STAR:
310 return Mult;
311 case SLASH:
312 return Div;
313 case DOUBLESLASH:
314 return FloorDiv;
315 case PERCENT:
316 return Mod;
317 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319 }
320}
321
Jeremy Hyltona8293132006-02-28 17:58:27 +0000322/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323
324 Only sets context for expr kinds that "can appear in assignment context"
325 (according to ../Parser/Python.asdl). For other expr kinds, it sets
326 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327*/
328
329static int
330set_context(expr_ty e, expr_context_ty ctx, const node *n)
331{
332 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000333 /* If a particular expression type can't be used for assign / delete,
334 set expr_name to its name and an error message will be generated.
335 */
336 const char* expr_name = NULL;
337
338 /* The ast defines augmented store and load contexts, but the
339 implementation here doesn't actually use them. The code may be
340 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000341 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000342 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000343 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000344 */
345 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346
347 switch (e->kind) {
348 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000349 if (ctx == Store &&
Guido van Rossumd8faa362007-04-27 19:54:29 +0000350 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
351 return ast_error(n, "assignment to None");
Thomas Wouters89f507f2006-12-13 04:49:30 +0000352 }
353 e->v.Attribute.ctx = ctx;
354 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000356 e->v.Subscript.ctx = ctx;
357 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358 case Name_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000359 if (ctx == Store &&
360 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
361 return ast_error(n, "assignment to None");
362 }
363 e->v.Name.ctx = ctx;
364 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 e->v.List.ctx = ctx;
367 s = e->v.List.elts;
368 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369 case Tuple_kind:
370 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
371 return ast_error(n, "can't assign to ()");
Thomas Wouters89f507f2006-12-13 04:49:30 +0000372 e->v.Tuple.ctx = ctx;
373 s = e->v.Tuple.elts;
374 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000375 case Lambda_kind:
376 expr_name = "lambda";
377 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000380 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000381 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000383 case UnaryOp_kind:
384 expr_name = "operator";
385 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000387 expr_name = "generator expression";
388 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000389 case Yield_kind:
390 expr_name = "yield expression";
391 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000392 case ListComp_kind:
393 expr_name = "list comprehension";
394 break;
395 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000396 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 case Num_kind:
398 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000399 expr_name = "literal";
400 break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000401 case Ellipsis_kind:
402 expr_name = "Ellipsis";
403 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000404 case Compare_kind:
405 expr_name = "comparison";
406 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000407 case IfExp_kind:
408 expr_name = "conditional expression";
409 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000410 default:
411 PyErr_Format(PyExc_SystemError,
412 "unexpected expression in assignment %d (line %d)",
413 e->kind, e->lineno);
414 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000416 /* Check for error string set by switch */
417 if (expr_name) {
418 char buf[300];
419 PyOS_snprintf(buf, sizeof(buf),
420 "can't %s %s",
421 ctx == Store ? "assign to" : "delete",
422 expr_name);
423 return ast_error(n, buf);
424 }
425
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000427 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 */
429 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000430 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431
Thomas Wouters89f507f2006-12-13 04:49:30 +0000432 for (i = 0; i < asdl_seq_LEN(s); i++) {
433 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
434 return 0;
435 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436 }
437 return 1;
438}
439
440static operator_ty
441ast_for_augassign(const node *n)
442{
443 REQ(n, augassign);
444 n = CHILD(n, 0);
445 switch (STR(n)[0]) {
446 case '+':
447 return Add;
448 case '-':
449 return Sub;
450 case '/':
451 if (STR(n)[1] == '/')
452 return FloorDiv;
453 else
454 return Div;
455 case '%':
456 return Mod;
457 case '<':
458 return LShift;
459 case '>':
460 return RShift;
461 case '&':
462 return BitAnd;
463 case '^':
464 return BitXor;
465 case '|':
466 return BitOr;
467 case '*':
468 if (STR(n)[1] == '*')
469 return Pow;
470 else
471 return Mult;
472 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000473 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 }
476}
477
478static cmpop_ty
479ast_for_comp_op(const node *n)
480{
Guido van Rossumb053cd82006-08-24 03:53:23 +0000481 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 |'is' 'not'
483 */
484 REQ(n, comp_op);
485 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000486 n = CHILD(n, 0);
487 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488 case LESS:
489 return Lt;
490 case GREATER:
491 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000492 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 return Eq;
494 case LESSEQUAL:
495 return LtE;
496 case GREATEREQUAL:
497 return GtE;
498 case NOTEQUAL:
499 return NotEq;
500 case NAME:
501 if (strcmp(STR(n), "in") == 0)
502 return In;
503 if (strcmp(STR(n), "is") == 0)
504 return Is;
505 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000506 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000509 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 }
511 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000512 /* handle "not in" and "is not" */
513 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 case NAME:
515 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
516 return NotIn;
517 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
518 return IsNot;
519 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000520 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000522 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000523 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 }
Neal Norwitz79792652005-11-14 04:25:03 +0000525 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528}
529
530static asdl_seq *
531seq_for_testlist(struct compiling *c, const node *n)
532{
533 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000534 asdl_seq *seq;
535 expr_ty expression;
536 int i;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000537 assert(TYPE(n) == testlist || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000539 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540 if (!seq)
541 return NULL;
542
543 for (i = 0; i < NCH(n); i += 2) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000544 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == test_nocond);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
546 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000547 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549
550 assert(i / 2 < seq->size);
551 asdl_seq_SET(seq, i / 2, expression);
552 }
553 return seq;
554}
555
Neal Norwitzc1505362006-12-28 06:47:50 +0000556static arg_ty
557compiler_simple_arg(struct compiling *c, const node *n)
558{
559 identifier name;
560 expr_ty annotation = NULL;
561 node *ch;
562
563 assert(TYPE(n) == tname || TYPE(n) == vname);
564 ch = CHILD(n, 0);
565 if (!strcmp(STR(ch), "None")) {
566 ast_error(ch, "assignment to None");
567 return NULL;
568 }
569 name = NEW_IDENTIFIER(ch);
570 if (!name)
571 return NULL;
572
573 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
574 annotation = ast_for_expr(c, CHILD(n, 2));
575 if (!annotation)
576 return NULL;
577 }
578
579 return SimpleArg(name, annotation, c->c_arena);
580}
581
582static arg_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000583compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584{
585 int i, len = (NCH(n) + 1) / 2;
Neal Norwitzc1505362006-12-28 06:47:50 +0000586 arg_ty arg;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000587 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588 if (!args)
589 return NULL;
590
Neal Norwitzc1505362006-12-28 06:47:50 +0000591 assert(TYPE(n) == tfplist || TYPE(n) == vfplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592 for (i = 0; i < len; i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +0000593 const node *child = CHILD(n, 2*i);
594 /* def foo(((x), y)): -- x is not nested complex, special case. */
595 while (NCH(child) == 3 && NCH(CHILD(child, 1)) == 1)
596 child = CHILD(CHILD(child, 1), 0);
597
598 /* child either holds a tname or '(', a tfplist, ')' */
599 switch (TYPE(CHILD(child, 0))) {
600 case tname:
601 case vname:
602 arg = compiler_simple_arg(c, CHILD(child, 0));
603 break;
604 case LPAR:
605 arg = compiler_complex_args(c, CHILD(child, 1));
606 break;
607 default:
608 PyErr_Format(PyExc_SystemError,
609 "unexpected node in args: %d @ %d",
610 TYPE(CHILD(child, 0)), i);
611 arg = NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000612 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000613 if (!arg)
614 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 asdl_seq_SET(args, i, arg);
616 }
617
Neal Norwitzc1505362006-12-28 06:47:50 +0000618 return NestedArgs(args, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619}
620
Guido van Rossum4f72a782006-10-27 23:31:49 +0000621/* returns -1 if failed to handle keyword only arguments
622 returns new position to keep processing if successful
Neal Norwitzc1505362006-12-28 06:47:50 +0000623 (',' tname ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +0000624 ^^^
625 start pointing here
626 */
627static int
628handle_keywordonly_args(struct compiling *c, const node *n, int start,
629 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
630{
631 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +0000632 expr_ty expression, annotation;
633 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000634 int i = start;
635 int j = 0; /* index for kwdefaults and kwonlyargs */
636 assert(kwonlyargs != NULL);
637 assert(kwdefaults != NULL);
638 while (i < NCH(n)) {
639 ch = CHILD(n, i);
640 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +0000641 case vname:
642 case tname:
Guido van Rossum4f72a782006-10-27 23:31:49 +0000643 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +0000644 expression = ast_for_expr(c, CHILD(n, i + 2));
Guido van Rossum4f72a782006-10-27 23:31:49 +0000645 if (!expression) {
646 ast_error(ch, "assignment to None");
647 goto error;
648 }
649 asdl_seq_SET(kwdefaults, j, expression);
650 i += 2; /* '=' and test */
651 }
652 else { /* setting NULL if no default value exists */
653 asdl_seq_SET(kwdefaults, j, NULL);
654 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000655 if (NCH(ch) == 3) {
656 /* ch is NAME ':' test */
657 annotation = ast_for_expr(c, CHILD(ch, 2));
658 if (!annotation) {
659 ast_error(ch, "expected expression");
660 goto error;
661 }
662 }
663 else {
664 annotation = NULL;
665 }
666 ch = CHILD(ch, 0);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000667 if (!strcmp(STR(ch), "None")) {
668 ast_error(ch, "assignment to None");
669 goto error;
670 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000671 arg = SimpleArg(NEW_IDENTIFIER(ch), annotation, c->c_arena);
672 if (!arg) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000673 ast_error(ch, "expecting name");
674 goto error;
675 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000676 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000677 i += 2; /* the name and the comma */
678 break;
679 case DOUBLESTAR:
680 return i;
681 default:
682 ast_error(ch, "unexpected node");
683 goto error;
684 }
685 }
686 return i;
687 error:
688 return -1;
689}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690
Jeremy Hyltona8293132006-02-28 17:58:27 +0000691/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692
693static arguments_ty
694ast_for_arguments(struct compiling *c, const node *n)
695{
Neal Norwitzc1505362006-12-28 06:47:50 +0000696 /* This function handles both typedargslist (function definition)
697 and varargslist (lambda definition).
698
699 parameters: '(' [typedargslist] ')'
700 typedargslist: ((tfpdef ['=' test] ',')*
701 ('*' [tname] (',' tname ['=' test])* [',' '**' tname]
702 | '**' tname)
703 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
704 varargslist: ((vfpdef ['=' test] ',')*
705 ('*' [vname] (',' vname ['=' test])* [',' '**' vname]
706 | '**' vname)
707 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708 */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000709 int i, j, k, nposargs = 0, nkwonlyargs = 0;
710 int nposdefaults = 0, found_default = 0;
711 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 identifier vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000713 arg_ty arg;
714 expr_ty varargannotation = NULL, kwargannotation = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715 node *ch;
716
717 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000718 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzc1505362006-12-28 06:47:50 +0000719 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
720 NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000721 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000723 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724
Guido van Rossum4f72a782006-10-27 23:31:49 +0000725 /* first count the number of positional args & defaults */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000727 ch = CHILD(n, i);
728 if (TYPE(ch) == STAR) {
Neal Norwitzc1505362006-12-28 06:47:50 +0000729 if (TYPE(CHILD(n, i+1)) == tname
730 || TYPE(CHILD(n, i+1)) == vname) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000731 /* skip NAME of vararg */
732 /* so that following can count only keyword only args */
733 i += 2;
734 }
735 else {
736 i++;
737 }
738 break;
739 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000740 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000741 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000743 /* count the number of keyword only args &
744 defaults for keyword only args */
745 for ( ; i < NCH(n); ++i) {
746 ch = CHILD(n, i);
747 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000748 if (TYPE(ch) == tname || TYPE(ch) == vname) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000749 }
750
751 posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL);
752 if (!posargs && nposargs)
Neal Norwitzc1505362006-12-28 06:47:50 +0000753 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000754 kwonlyargs = (nkwonlyargs ?
755 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
756 if (!kwonlyargs && nkwonlyargs)
Neal Norwitzc1505362006-12-28 06:47:50 +0000757 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000758 posdefaults = (nposdefaults ?
759 asdl_seq_new(nposdefaults, c->c_arena) : NULL);
760 if (!posdefaults && nposdefaults)
Neal Norwitzc1505362006-12-28 06:47:50 +0000761 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000762 /* The length of kwonlyargs and kwdefaults are same
763 since we set NULL as default for keyword only argument w/o default
764 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +0000765 kwdefaults = (nkwonlyargs ?
Guido van Rossum4f72a782006-10-27 23:31:49 +0000766 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
767 if (!kwdefaults && nkwonlyargs)
Neal Norwitzc1505362006-12-28 06:47:50 +0000768 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000769
770 if (nposargs + nkwonlyargs > 255) {
Neal Norwitzc1505362006-12-28 06:47:50 +0000771 ast_error(n, "more than 255 arguments");
772 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774
Neal Norwitzc1505362006-12-28 06:47:50 +0000775 /* tname: NAME [':' test]
776 tfpdef: tname | '(' tfplist ')'
777 tfplist: tfpdef (',' tfpdef)* [',']
778 vname: NAME
779 vfpdef: NAME | '(' vfplist ')'
780 vfplist: vfpdef (',' vfpdef)* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 */
782 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000783 j = 0; /* index for defaults */
784 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000786 ch = CHILD(n, i);
787 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +0000788 case tfpdef:
789 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
791 anything other than EQUAL or a comma? */
792 /* XXX Should NCH(n) check be made a separate check? */
793 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000794 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
795 if (!expression)
Guido van Rossum4f72a782006-10-27 23:31:49 +0000796 goto error;
797 assert(posdefaults != NULL);
798 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000800 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000802 else if (found_default) {
803 ast_error(n,
804 "non-default argument follows default argument");
805 goto error;
806 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000807 /* def foo((x)): is not complex, special case. */
808 while (NCH(ch) == 3 && NCH(CHILD(ch, 1)) == 1)
809 ch = CHILD(CHILD(ch, 1), 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000810
Neal Norwitzc1505362006-12-28 06:47:50 +0000811 if (NCH(ch) != 1)
812 arg = compiler_complex_args(c, CHILD(ch, 1));
813 else
814 arg = compiler_simple_arg(c, CHILD(ch, 0));
815 if (!arg)
816 goto error;
817 asdl_seq_SET(posargs, k++, arg);
818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 i += 2; /* the name and the comma */
820 break;
821 case STAR:
Guido van Rossum4f72a782006-10-27 23:31:49 +0000822 if (i+1 >= NCH(n)) {
823 ast_error(CHILD(n, i), "no name for vararg");
Neal Norwitzc1505362006-12-28 06:47:50 +0000824 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000825 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000826 ch = CHILD(n, i+1); /* tname or COMMA */
827 if (TYPE(ch) == COMMA) {
828 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000829 i += 2; /* now follows keyword only arguments */
830 res = handle_keywordonly_args(c, n, i,
831 kwonlyargs, kwdefaults);
832 if (res == -1) goto error;
833 i = res; /* res has new position to process */
834 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000835 else if (!strcmp(STR(CHILD(ch, 0)), "None")) {
836 ast_error(CHILD(ch, 0), "assignment to None");
837 goto error;
838 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000839 else {
Neal Norwitzc1505362006-12-28 06:47:50 +0000840 vararg = NEW_IDENTIFIER(CHILD(ch, 0));
841 if (NCH(ch) > 1) {
842 /* there is an annotation on the vararg */
843 varargannotation = ast_for_expr(c, CHILD(ch, 2));
844 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000845 i += 3;
Neal Norwitzc1505362006-12-28 06:47:50 +0000846 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tname
847 || TYPE(CHILD(n, i)) == vname)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000848 int res = 0;
849 res = handle_keywordonly_args(c, n, i,
850 kwonlyargs, kwdefaults);
851 if (res == -1) goto error;
852 i = res; /* res has new position to process */
853 }
854 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 break;
856 case DOUBLESTAR:
Neal Norwitzc1505362006-12-28 06:47:50 +0000857 ch = CHILD(n, i+1); /* tname */
858 assert(TYPE(ch) == tname || TYPE(ch) == vname);
859 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
860 ast_error(CHILD(ch, 0), "assignment to None");
Guido van Rossum4f72a782006-10-27 23:31:49 +0000861 goto error;
862 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000863 kwarg = NEW_IDENTIFIER(CHILD(ch, 0));
864 if (NCH(ch) > 1) {
865 /* there is an annotation on the kwarg */
866 kwargannotation = ast_for_expr(c, CHILD(ch, 2));
867 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 i += 3;
869 break;
870 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000871 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 "unexpected node in varargslist: %d @ %d",
873 TYPE(ch), i);
874 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000875 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000877 return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg,
878 kwargannotation, posdefaults, kwdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000880 Py_XDECREF(vararg);
881 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 return NULL;
883}
884
885static expr_ty
886ast_for_dotted_name(struct compiling *c, const node *n)
887{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000888 expr_ty e;
889 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000890 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 int i;
892
893 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000894
895 lineno = LINENO(n);
896 col_offset = n->n_col_offset;
897
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898 id = NEW_IDENTIFIER(CHILD(n, 0));
899 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000900 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000901 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000903 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904
905 for (i = 2; i < NCH(n); i+=2) {
906 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000907 if (!id)
908 return NULL;
909 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
910 if (!e)
911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 }
913
914 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915}
916
917static expr_ty
918ast_for_decorator(struct compiling *c, const node *n)
919{
920 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
921 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000922 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923
924 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000925 REQ(CHILD(n, 0), AT);
926 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927
928 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
929 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000930 return NULL;
931
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000933 d = name_expr;
934 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935 }
936 else if (NCH(n) == 5) { /* Call with no arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000937 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000938 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000939 if (!d)
940 return NULL;
941 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 }
943 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000944 d = ast_for_call(c, CHILD(n, 3), name_expr);
945 if (!d)
946 return NULL;
947 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 }
949
950 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951}
952
953static asdl_seq*
954ast_for_decorators(struct compiling *c, const node *n)
955{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000956 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000957 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 int i;
959
960 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000961 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962 if (!decorator_seq)
963 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000964
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000966 d = ast_for_decorator(c, CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000967 if (!d)
968 return NULL;
969 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 }
971 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972}
973
974static stmt_ty
975ast_for_funcdef(struct compiling *c, const node *n)
976{
Neal Norwitzc1505362006-12-28 06:47:50 +0000977 /* funcdef: 'def' [decorators] NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000978 identifier name;
979 arguments_ty args;
980 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 asdl_seq *decorator_seq = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000982 expr_ty returns = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983 int name_i;
984
985 REQ(n, funcdef);
986
Nick Coghlan71011e22007-04-23 11:05:01 +0000987 if (NCH(n) == 6 || NCH(n) == 8) { /* decorators are present */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000988 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
989 if (!decorator_seq)
990 return NULL;
991 name_i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 }
993 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000994 name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 }
996
997 name = NEW_IDENTIFIER(CHILD(n, name_i));
998 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000999 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001001 ast_error(CHILD(n, name_i), "assignment to None");
1002 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 }
1004 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1005 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001006 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001007 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1008 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1009 if (!returns)
1010 return NULL;
1011 name_i += 2;
1012 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 body = ast_for_suite(c, CHILD(n, name_i + 3));
1014 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001015 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016
Neal Norwitzc1505362006-12-28 06:47:50 +00001017 return FunctionDef(name, args, body, decorator_seq, returns, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001018 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019}
1020
1021static expr_ty
1022ast_for_lambdef(struct compiling *c, const node *n)
1023{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001024 /* lambdef: 'lambda' [varargslist] ':' test
1025 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 arguments_ty args;
1027 expr_ty expression;
1028
1029 if (NCH(n) == 3) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001030 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1031 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 if (!args)
1033 return NULL;
1034 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001035 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 }
1038 else {
1039 args = ast_for_arguments(c, CHILD(n, 1));
1040 if (!args)
1041 return NULL;
1042 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001043 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 }
1046
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001047 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048}
1049
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001050static expr_ty
1051ast_for_ifexpr(struct compiling *c, const node *n)
1052{
1053 /* test: or_test 'if' or_test 'else' test */
1054 expr_ty expression, body, orelse;
1055
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001056 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001057 body = ast_for_expr(c, CHILD(n, 0));
1058 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001059 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001060 expression = ast_for_expr(c, CHILD(n, 2));
1061 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001062 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001063 orelse = ast_for_expr(c, CHILD(n, 4));
1064 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001065 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001066 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1067 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001068}
1069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001071 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072
Nick Coghlan650f0d02007-04-15 12:05:43 +00001073 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074*/
1075
1076static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001077count_comp_fors(const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001079 int n_fors = 0;
1080 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081
Guido van Rossumd8faa362007-04-27 19:54:29 +00001082 count_comp_for:
1083 n_fors++;
1084 REQ(ch, comp_for);
1085 if (NCH(ch) == 5)
1086 ch = CHILD(ch, 4);
1087 else
1088 return n_fors;
1089 count_comp_iter:
1090 REQ(ch, comp_iter);
1091 ch = CHILD(ch, 0);
1092 if (TYPE(ch) == comp_for)
1093 goto count_comp_for;
1094 else if (TYPE(ch) == comp_if) {
1095 if (NCH(ch) == 3) {
1096 ch = CHILD(ch, 2);
1097 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001098 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001099 else
1100 return n_fors;
1101 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001102
Guido van Rossumd8faa362007-04-27 19:54:29 +00001103 /* Should never be reached */
1104 PyErr_SetString(PyExc_SystemError,
1105 "logic error in count_comp_fors");
1106 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107}
1108
Nick Coghlan650f0d02007-04-15 12:05:43 +00001109/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110
Nick Coghlan650f0d02007-04-15 12:05:43 +00001111 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112*/
1113
1114static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001115count_comp_ifs(const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001117 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118
Guido van Rossumd8faa362007-04-27 19:54:29 +00001119 while (1) {
1120 REQ(n, comp_iter);
1121 if (TYPE(CHILD(n, 0)) == comp_for)
1122 return n_ifs;
1123 n = CHILD(n, 0);
1124 REQ(n, comp_if);
1125 n_ifs++;
1126 if (NCH(n) == 2)
1127 return n_ifs;
1128 n = CHILD(n, 2);
1129 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130}
1131
1132static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001133ast_for_comprehension(struct compiling *c, const node *n, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001135 /* testlist_comp: test ( comp_for | (',' test)* [','] )
1136 argument: [test '='] test [comp_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 expr_ty elt;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001138 asdl_seq *comps;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 int i, n_fors;
1140 node *ch;
1141
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 assert(NCH(n) > 1);
1143
1144 elt = ast_for_expr(c, CHILD(n, 0));
1145 if (!elt)
1146 return NULL;
1147
Nick Coghlan650f0d02007-04-15 12:05:43 +00001148 n_fors = count_comp_fors(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 if (n_fors == -1)
1150 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001151
Nick Coghlan650f0d02007-04-15 12:05:43 +00001152 comps = asdl_seq_new(n_fors, c->c_arena);
1153 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 ch = CHILD(n, 1);
1157 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001158 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 asdl_seq *t;
1160 expr_ty expression;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001161 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162
Nick Coghlan650f0d02007-04-15 12:05:43 +00001163 REQ(ch, comp_for);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 for_ch = CHILD(ch, 1);
1166 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001167 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001169 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001170 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001172
Thomas Wouters89f507f2006-12-13 04:49:30 +00001173 /* Check the # of children rather than the length of t, since
1174 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1175 if (NCH(for_ch) == 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001176 comp = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1177 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001179 comp = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1180 c->c_arena),
1181 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001182
Nick Coghlan650f0d02007-04-15 12:05:43 +00001183 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 if (NCH(ch) == 5) {
1187 int j, n_ifs;
1188 asdl_seq *ifs;
1189
1190 ch = CHILD(ch, 4);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001191 n_ifs = count_comp_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001192 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001194
1195 ifs = asdl_seq_new(n_ifs, c->c_arena);
1196 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001198
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 for (j = 0; j < n_ifs; j++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001200 REQ(ch, comp_iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 ch = CHILD(ch, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001202 REQ(ch, comp_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001204 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001205 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001206 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001207 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 if (NCH(ch) == 3)
1209 ch = CHILD(ch, 2);
1210 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001211 /* on exit, must guarantee that ch is a comp_for */
1212 if (TYPE(ch) == comp_iter)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 ch = CHILD(ch, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001214 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001216 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001218
1219 if (type == COMP_GENEXP)
1220 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1221 else if (type == COMP_LISTCOMP)
1222 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1223 else if (type == COMP_SETCOMP)
1224 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1225 else
1226 /* Should never happen */
1227 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228}
1229
1230static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001231ast_for_genexp(struct compiling *c, const node *n)
1232{
1233 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
1234 return ast_for_comprehension(c, n, COMP_GENEXP);
1235}
1236
1237static expr_ty
1238ast_for_listcomp(struct compiling *c, const node *n)
1239{
1240 assert(TYPE(n) == (testlist_comp));
1241 return ast_for_comprehension(c, n, COMP_LISTCOMP);
1242}
1243
1244static expr_ty
1245ast_for_setcomp(struct compiling *c, const node *n)
1246{
1247 assert(TYPE(n) == (dictorsetmaker));
1248 return ast_for_comprehension(c, n, COMP_SETCOMP);
1249}
1250
1251
1252static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253ast_for_atom(struct compiling *c, const node *n)
1254{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001255 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
1256 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 */
1258 node *ch = CHILD(n, 0);
Thomas Wouters00e41de2007-02-23 19:56:57 +00001259 int bytesmode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260
1261 switch (TYPE(ch)) {
1262 case NAME:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001263 /* All names start in Load context, but may later be
1264 changed. */
1265 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 case STRING: {
Thomas Wouters00e41de2007-02-23 19:56:57 +00001267 PyObject *str = parsestrplus(c, n, &bytesmode);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001268 if (!str)
1269 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001270
Thomas Wouters89f507f2006-12-13 04:49:30 +00001271 PyArena_AddPyObject(c->c_arena, str);
Thomas Wouters00e41de2007-02-23 19:56:57 +00001272 if (bytesmode)
1273 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
1274 else
1275 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 }
1277 case NUMBER: {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001278 PyObject *pynum = parsenumber(STR(ch));
1279 if (!pynum)
1280 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001281
Thomas Wouters89f507f2006-12-13 04:49:30 +00001282 PyArena_AddPyObject(c->c_arena, pynum);
1283 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 }
Georg Brandldde00282007-03-18 19:01:53 +00001285 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00001286 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001288 ch = CHILD(n, 1);
1289
1290 if (TYPE(ch) == RPAR)
1291 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1292
1293 if (TYPE(ch) == yield_expr)
1294 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001295
1296 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
1297 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001298 return ast_for_genexp(c, ch);
1299
Nick Coghlan650f0d02007-04-15 12:05:43 +00001300 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001302 ch = CHILD(n, 1);
1303
1304 if (TYPE(ch) == RSQB)
1305 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1306
Nick Coghlan650f0d02007-04-15 12:05:43 +00001307 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001308 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1309 asdl_seq *elts = seq_for_testlist(c, ch);
1310 if (!elts)
1311 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001312
Thomas Wouters89f507f2006-12-13 04:49:30 +00001313 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1314 }
1315 else
1316 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 case LBRACE: {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001318 /* dictorsetmaker: test ':' test (',' test ':' test)* [','] |
1319 * test (gen_for | (',' test)* [',']) */
Neal Norwitzc1505362006-12-28 06:47:50 +00001320 int i, size;
1321 asdl_seq *keys, *values;
1322
1323 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001324 if (TYPE(ch) == RBRACE) {
1325 /* it's an empty dict */
1326 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
1327 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1328 /* it's a simple set */
Guido van Rossum4d2adcc2007-04-17 21:58:50 +00001329 asdl_seq *elts;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001330 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
Guido van Rossum4d2adcc2007-04-17 21:58:50 +00001331 elts = asdl_seq_new(size, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001332 if (!elts)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001333 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001334 for (i = 0; i < NCH(ch); i += 2) {
1335 expr_ty expression;
1336 expression = ast_for_expr(c, CHILD(ch, i));
1337 if (!expression)
1338 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001339 asdl_seq_SET(elts, i / 2, expression);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001340 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001341 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1342 } else if (TYPE(CHILD(ch, 1)) == comp_for) {
1343 /* it's a set comprehension */
1344 return ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001345 } else {
1346 /* it's a dict */
1347 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1348 keys = asdl_seq_new(size, c->c_arena);
1349 if (!keys)
1350 return NULL;
1351
1352 values = asdl_seq_new(size, c->c_arena);
1353 if (!values)
1354 return NULL;
1355
1356 for (i = 0; i < NCH(ch); i += 4) {
1357 expr_ty expression;
1358
1359 expression = ast_for_expr(c, CHILD(ch, i));
1360 if (!expression)
1361 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001362
Guido van Rossum86e58e22006-08-28 15:27:34 +00001363 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitze76adcd2005-11-15 05:04:31 +00001364
Guido van Rossum86e58e22006-08-28 15:27:34 +00001365 expression = ast_for_expr(c, CHILD(ch, i + 2));
1366 if (!expression)
1367 return NULL;
1368
1369 asdl_seq_SET(values, i / 4, expression);
1370 }
1371 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1372 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001375 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1376 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 }
1378}
1379
1380static slice_ty
1381ast_for_slice(struct compiling *c, const node *n)
1382{
1383 node *ch;
1384 expr_ty lower = NULL, upper = NULL, step = NULL;
1385
1386 REQ(n, subscript);
1387
1388 /*
Georg Brandl52318d62006-09-06 07:06:08 +00001389 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 sliceop: ':' [test]
1391 */
1392 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 if (NCH(n) == 1 && TYPE(ch) == test) {
1394 /* 'step' variable hold no significance in terms of being used over
1395 other vars */
1396 step = ast_for_expr(c, ch);
1397 if (!step)
1398 return NULL;
1399
Thomas Wouters89f507f2006-12-13 04:49:30 +00001400 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 }
1402
1403 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001404 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 if (!lower)
1406 return NULL;
1407 }
1408
1409 /* If there's an upper bound it's in the second or third position. */
1410 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001411 if (NCH(n) > 1) {
1412 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413
Thomas Wouters89f507f2006-12-13 04:49:30 +00001414 if (TYPE(n2) == test) {
1415 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 if (!upper)
1417 return NULL;
1418 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001419 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001421 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422
Thomas Wouters89f507f2006-12-13 04:49:30 +00001423 if (TYPE(n2) == test) {
1424 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 if (!upper)
1426 return NULL;
1427 }
1428 }
1429
1430 ch = CHILD(n, NCH(n) - 1);
1431 if (TYPE(ch) == sliceop) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001432 if (NCH(ch) == 1) {
1433 /* No expression, so step is None */
1434 ch = CHILD(ch, 0);
1435 step = Name(new_identifier("None", c->c_arena), Load,
1436 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 if (!step)
1438 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001439 } else {
1440 ch = CHILD(ch, 1);
1441 if (TYPE(ch) == test) {
1442 step = ast_for_expr(c, ch);
1443 if (!step)
1444 return NULL;
1445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 }
1447 }
1448
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001449 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450}
1451
1452static expr_ty
1453ast_for_binop(struct compiling *c, const node *n)
1454{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001455 /* Must account for a sequence of expressions.
1456 How should A op B op C by represented?
1457 BinOp(BinOp(A, op, B), op, C).
1458 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459
Guido van Rossumd8faa362007-04-27 19:54:29 +00001460 int i, nops;
1461 expr_ty expr1, expr2, result;
1462 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463
Guido van Rossumd8faa362007-04-27 19:54:29 +00001464 expr1 = ast_for_expr(c, CHILD(n, 0));
1465 if (!expr1)
1466 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467
Guido van Rossumd8faa362007-04-27 19:54:29 +00001468 expr2 = ast_for_expr(c, CHILD(n, 2));
1469 if (!expr2)
1470 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471
Guido van Rossumd8faa362007-04-27 19:54:29 +00001472 newoperator = get_operator(CHILD(n, 1));
1473 if (!newoperator)
1474 return NULL;
1475
1476 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1477 c->c_arena);
1478 if (!result)
1479 return NULL;
1480
1481 nops = (NCH(n) - 1) / 2;
1482 for (i = 1; i < nops; i++) {
1483 expr_ty tmp_result, tmp;
1484 const node* next_oper = CHILD(n, i * 2 + 1);
1485
1486 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001487 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 return NULL;
1489
Guido van Rossumd8faa362007-04-27 19:54:29 +00001490 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1491 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492 return NULL;
1493
Guido van Rossumd8faa362007-04-27 19:54:29 +00001494 tmp_result = BinOp(result, newoperator, tmp,
1495 LINENO(next_oper), next_oper->n_col_offset,
1496 c->c_arena);
1497 if (!tmp)
1498 return NULL;
1499 result = tmp_result;
1500 }
1501 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502}
1503
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001504static expr_ty
1505ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1506{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001507 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1508 subscriptlist: subscript (',' subscript)* [',']
1509 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1510 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001511 REQ(n, trailer);
1512 if (TYPE(CHILD(n, 0)) == LPAR) {
1513 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001514 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1515 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001516 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001517 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001518 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001519 else if (TYPE(CHILD(n, 0)) == DOT ) {
1520 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001521 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001522 }
1523 else {
1524 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001525 REQ(CHILD(n, 2), RSQB);
1526 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001527 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001528 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1529 if (!slc)
1530 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001531 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1532 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001533 }
1534 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001535 /* The grammar is ambiguous here. The ambiguity is resolved
1536 by treating the sequence as a tuple literal if there are
1537 no slice features.
1538 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001539 int j;
1540 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001541 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001542 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001543 asdl_seq *slices, *elts;
1544 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001545 if (!slices)
1546 return NULL;
1547 for (j = 0; j < NCH(n); j += 2) {
1548 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001549 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001550 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001551 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001552 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001553 asdl_seq_SET(slices, j / 2, slc);
1554 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001555 if (!simple) {
1556 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001557 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001558 }
1559 /* extract Index values and put them in a Tuple */
1560 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001561 if (!elts)
1562 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001563 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1564 slc = (slice_ty)asdl_seq_GET(slices, j);
1565 assert(slc->kind == Index_kind && slc->v.Index.value);
1566 asdl_seq_SET(elts, j, slc->v.Index.value);
1567 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001568 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001569 if (!e)
1570 return NULL;
1571 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001572 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001573 }
1574 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001575}
1576
1577static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001578ast_for_factor(struct compiling *c, const node *n)
1579{
1580 node *pfactor, *ppower, *patom, *pnum;
1581 expr_ty expression;
1582
1583 /* If the unary - operator is applied to a constant, don't generate
1584 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1585 constant. The peephole optimizer already does something like
1586 this but it doesn't handle the case where the constant is
1587 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1588 PyLongObject.
1589 */
1590 if (TYPE(CHILD(n, 0)) == MINUS
1591 && NCH(n) == 2
1592 && TYPE((pfactor = CHILD(n, 1))) == factor
1593 && NCH(pfactor) == 1
1594 && TYPE((ppower = CHILD(pfactor, 0))) == power
1595 && NCH(ppower) == 1
1596 && TYPE((patom = CHILD(ppower, 0))) == atom
1597 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1598 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1599 if (s == NULL)
1600 return NULL;
1601 s[0] = '-';
1602 strcpy(s + 1, STR(pnum));
1603 PyObject_FREE(STR(pnum));
1604 STR(pnum) = s;
1605 return ast_for_atom(c, patom);
1606 }
1607
1608 expression = ast_for_expr(c, CHILD(n, 1));
1609 if (!expression)
1610 return NULL;
1611
1612 switch (TYPE(CHILD(n, 0))) {
1613 case PLUS:
1614 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1615 c->c_arena);
1616 case MINUS:
1617 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1618 c->c_arena);
1619 case TILDE:
1620 return UnaryOp(Invert, expression, LINENO(n),
1621 n->n_col_offset, c->c_arena);
1622 }
1623 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1624 TYPE(CHILD(n, 0)));
1625 return NULL;
1626}
1627
1628static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001629ast_for_power(struct compiling *c, const node *n)
1630{
1631 /* power: atom trailer* ('**' factor)*
1632 */
1633 int i;
1634 expr_ty e, tmp;
1635 REQ(n, power);
1636 e = ast_for_atom(c, CHILD(n, 0));
1637 if (!e)
1638 return NULL;
1639 if (NCH(n) == 1)
1640 return e;
1641 for (i = 1; i < NCH(n); i++) {
1642 node *ch = CHILD(n, i);
1643 if (TYPE(ch) != trailer)
1644 break;
1645 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001646 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001647 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001648 tmp->lineno = e->lineno;
1649 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001650 e = tmp;
1651 }
1652 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1653 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001654 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001655 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001656 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001657 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001658 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001659 e = tmp;
1660 }
1661 return e;
1662}
1663
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664/* Do not name a variable 'expr'! Will cause a compile error.
1665*/
1666
1667static expr_ty
1668ast_for_expr(struct compiling *c, const node *n)
1669{
1670 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001671 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00001672 test_nocond: or_test | lambdef_nocond
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001673 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 and_test: not_test ('and' not_test)*
1675 not_test: 'not' not_test | comparison
1676 comparison: expr (comp_op expr)*
1677 expr: xor_expr ('|' xor_expr)*
1678 xor_expr: and_expr ('^' and_expr)*
1679 and_expr: shift_expr ('&' shift_expr)*
1680 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1681 arith_expr: term (('+'|'-') term)*
1682 term: factor (('*'|'/'|'%'|'//') factor)*
1683 factor: ('+'|'-'|'~') factor | power
1684 power: atom trailer* ('**' factor)*
1685 */
1686
1687 asdl_seq *seq;
1688 int i;
1689
1690 loop:
1691 switch (TYPE(n)) {
1692 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00001693 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001694 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00001695 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001697 else if (NCH(n) > 1)
1698 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001699 /* Fallthrough */
1700 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 case and_test:
1702 if (NCH(n) == 1) {
1703 n = CHILD(n, 0);
1704 goto loop;
1705 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001706 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 if (!seq)
1708 return NULL;
1709 for (i = 0; i < NCH(n); i += 2) {
1710 expr_ty e = ast_for_expr(c, CHILD(n, i));
1711 if (!e)
1712 return NULL;
1713 asdl_seq_SET(seq, i / 2, e);
1714 }
1715 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001716 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1717 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001718 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001719 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 case not_test:
1721 if (NCH(n) == 1) {
1722 n = CHILD(n, 0);
1723 goto loop;
1724 }
1725 else {
1726 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1727 if (!expression)
1728 return NULL;
1729
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001730 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1731 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 }
1733 case comparison:
1734 if (NCH(n) == 1) {
1735 n = CHILD(n, 0);
1736 goto loop;
1737 }
1738 else {
1739 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001740 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001741 asdl_seq *cmps;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001742 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 if (!ops)
1744 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001745 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 return NULL;
1748 }
1749 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001750 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001752 newoperator = ast_for_comp_op(CHILD(n, i));
1753 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001755 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756
1757 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001758 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001760 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 asdl_seq_SET(cmps, i / 2, expression);
1764 }
1765 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001766 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001768 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001770 return Compare(expression, ops, cmps, LINENO(n),
1771 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 }
1773 break;
1774
1775 /* The next five cases all handle BinOps. The main body of code
1776 is the same in each case, but the switch turned inside out to
1777 reuse the code for each type of operator.
1778 */
1779 case expr:
1780 case xor_expr:
1781 case and_expr:
1782 case shift_expr:
1783 case arith_expr:
1784 case term:
1785 if (NCH(n) == 1) {
1786 n = CHILD(n, 0);
1787 goto loop;
1788 }
1789 return ast_for_binop(c, n);
1790 case yield_expr: {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001791 expr_ty exp = NULL;
1792 if (NCH(n) == 2) {
1793 exp = ast_for_testlist(c, CHILD(n, 1));
1794 if (!exp)
1795 return NULL;
1796 }
1797 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1798 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001799 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800 if (NCH(n) == 1) {
1801 n = CHILD(n, 0);
1802 goto loop;
1803 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001804 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001805 case power:
1806 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001808 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 return NULL;
1810 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001811 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812 return NULL;
1813}
1814
1815static expr_ty
1816ast_for_call(struct compiling *c, const node *n, expr_ty func)
1817{
1818 /*
1819 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1820 | '**' test)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001821 argument: [test '='] test [comp_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 */
1823
1824 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001825 asdl_seq *args;
1826 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 expr_ty vararg = NULL, kwarg = NULL;
1828
1829 REQ(n, arglist);
1830
1831 nargs = 0;
1832 nkeywords = 0;
1833 ngens = 0;
1834 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001835 node *ch = CHILD(n, i);
1836 if (TYPE(ch) == argument) {
1837 if (NCH(ch) == 1)
1838 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001839 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001840 ngens++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 else
Thomas Wouters89f507f2006-12-13 04:49:30 +00001842 nkeywords++;
1843 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 }
1845 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001846 ast_error(n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00001847 "if not sole argument");
1848 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 }
1850
1851 if (nargs + nkeywords + ngens > 255) {
1852 ast_error(n, "more than 255 arguments");
1853 return NULL;
1854 }
1855
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858 return NULL;
1859 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001861 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 nargs = 0;
1863 nkeywords = 0;
1864 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001865 node *ch = CHILD(n, i);
1866 if (TYPE(ch) == argument) {
1867 expr_ty e;
1868 if (NCH(ch) == 1) {
1869 if (nkeywords) {
1870 ast_error(CHILD(ch, 0),
1871 "non-keyword arg after keyword arg");
1872 return NULL;
1873 }
1874 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001876 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001877 asdl_seq_SET(args, nargs++, e);
1878 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001879 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001880 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001882 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001883 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001885 else {
1886 keyword_ty kw;
1887 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888
Thomas Wouters89f507f2006-12-13 04:49:30 +00001889 /* CHILD(ch, 0) is test, but must be an identifier? */
1890 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 /* f(lambda x: x[0] = 3) ends up getting parsed with
1894 * LHS test = lambda x: x[0], and RHS test = 3.
1895 * SF bug 132313 points out that complaining about a keyword
1896 * then is very confusing.
1897 */
1898 if (e->kind == Lambda_kind) {
1899 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001900 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 } else if (e->kind != Name_kind) {
1902 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001903 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001905 key = e->v.Name.id;
1906 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001908 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001909 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001911 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001912 asdl_seq_SET(keywords, nkeywords++, kw);
1913 }
1914 }
1915 else if (TYPE(ch) == STAR) {
1916 vararg = ast_for_expr(c, CHILD(n, i+1));
1917 i++;
1918 }
1919 else if (TYPE(ch) == DOUBLESTAR) {
1920 kwarg = ast_for_expr(c, CHILD(n, i+1));
1921 i++;
1922 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923 }
1924
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001925 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926}
1927
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001929ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001931 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001932 /* testlist: test (',' test)* [','] */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001933 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001935 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001936 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001937 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001938 }
1939 else {
1940 assert(TYPE(n) == testlist ||
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001941 TYPE(n) == testlist1);
1942 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001944 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 else {
1946 asdl_seq *tmp = seq_for_testlist(c, n);
1947 if (!tmp)
1948 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001949 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001951}
1952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953static stmt_ty
1954ast_for_expr_stmt(struct compiling *c, const node *n)
1955{
1956 REQ(n, expr_stmt);
1957 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1958 | ('=' (yield_expr|testlist))*)
1959 testlist: test (',' test)* [',']
1960 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00001961 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 test: ... here starts the operator precendence dance
1963 */
1964
1965 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001966 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 if (!e)
1968 return NULL;
1969
Thomas Wouters89f507f2006-12-13 04:49:30 +00001970 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 }
1972 else if (TYPE(CHILD(n, 1)) == augassign) {
1973 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001974 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001975 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976
Thomas Wouters89f507f2006-12-13 04:49:30 +00001977 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978 if (!expr1)
1979 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001980 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001981 switch (expr1->kind) {
1982 case GeneratorExp_kind:
1983 ast_error(ch, "augmented assignment to generator "
1984 "expression not possible");
1985 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001986 case Yield_kind:
1987 ast_error(ch, "augmented assignment to yield "
1988 "expression not possible");
1989 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001990 case Name_kind: {
1991 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1992 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1993 ast_error(ch, "assignment to None");
1994 return NULL;
1995 }
1996 break;
1997 }
1998 case Attribute_kind:
1999 case Subscript_kind:
2000 break;
2001 default:
2002 ast_error(ch, "illegal expression for augmented "
2003 "assignment");
2004 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002006 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007
Thomas Wouters89f507f2006-12-13 04:49:30 +00002008 ch = CHILD(n, 2);
2009 if (TYPE(ch) == testlist)
2010 expr2 = ast_for_testlist(c, ch);
2011 else
2012 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002013 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 return NULL;
2015
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002016 newoperator = ast_for_augassign(CHILD(n, 1));
2017 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 return NULL;
2019
Thomas Wouters89f507f2006-12-13 04:49:30 +00002020 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 }
2022 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002023 int i;
2024 asdl_seq *targets;
2025 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026 expr_ty expression;
2027
Thomas Wouters89f507f2006-12-13 04:49:30 +00002028 /* a normal assignment */
2029 REQ(CHILD(n, 1), EQUAL);
2030 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2031 if (!targets)
2032 return NULL;
2033 for (i = 0; i < NCH(n) - 2; i += 2) {
2034 expr_ty e;
2035 node *ch = CHILD(n, i);
2036 if (TYPE(ch) == yield_expr) {
2037 ast_error(ch, "assignment to yield expression not possible");
2038 return NULL;
2039 }
2040 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041
Thomas Wouters89f507f2006-12-13 04:49:30 +00002042 /* set context to assign */
2043 if (!e)
2044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045
Thomas Wouters89f507f2006-12-13 04:49:30 +00002046 if (!set_context(e, Store, CHILD(n, i)))
2047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048
Thomas Wouters89f507f2006-12-13 04:49:30 +00002049 asdl_seq_SET(targets, i / 2, e);
2050 }
2051 value = CHILD(n, NCH(n) - 1);
2052 if (TYPE(value) == testlist)
2053 expression = ast_for_testlist(c, value);
2054 else
2055 expression = ast_for_expr(c, value);
2056 if (!expression)
2057 return NULL;
2058 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060}
2061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002063ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064{
2065 asdl_seq *seq;
2066 int i;
2067 expr_ty e;
2068
2069 REQ(n, exprlist);
2070
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002071 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002073 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002075 e = ast_for_expr(c, CHILD(n, i));
2076 if (!e)
2077 return NULL;
2078 asdl_seq_SET(seq, i / 2, e);
2079 if (context && !set_context(e, context, CHILD(n, i)))
2080 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 }
2082 return seq;
2083}
2084
2085static stmt_ty
2086ast_for_del_stmt(struct compiling *c, const node *n)
2087{
2088 asdl_seq *expr_list;
2089
2090 /* del_stmt: 'del' exprlist */
2091 REQ(n, del_stmt);
2092
2093 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2094 if (!expr_list)
2095 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002096 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097}
2098
2099static stmt_ty
2100ast_for_flow_stmt(struct compiling *c, const node *n)
2101{
2102 /*
2103 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2104 | yield_stmt
2105 break_stmt: 'break'
2106 continue_stmt: 'continue'
2107 return_stmt: 'return' [testlist]
2108 yield_stmt: yield_expr
2109 yield_expr: 'yield' testlist
2110 raise_stmt: 'raise' [test [',' test [',' test]]]
2111 */
2112 node *ch;
2113
2114 REQ(n, flow_stmt);
2115 ch = CHILD(n, 0);
2116 switch (TYPE(ch)) {
2117 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002118 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002120 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002122 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2123 if (!exp)
2124 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002125 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 }
2127 case return_stmt:
2128 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002129 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002131 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 if (!expression)
2133 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002134 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 }
2136 case raise_stmt:
2137 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002138 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 else if (NCH(ch) == 2) {
2140 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2141 if (!expression)
2142 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002143 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 }
2145 else if (NCH(ch) == 4) {
2146 expr_ty expr1, expr2;
2147
2148 expr1 = ast_for_expr(c, CHILD(ch, 1));
2149 if (!expr1)
2150 return NULL;
2151 expr2 = ast_for_expr(c, CHILD(ch, 3));
2152 if (!expr2)
2153 return NULL;
2154
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002155 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156 }
2157 else if (NCH(ch) == 6) {
2158 expr_ty expr1, expr2, expr3;
2159
2160 expr1 = ast_for_expr(c, CHILD(ch, 1));
2161 if (!expr1)
2162 return NULL;
2163 expr2 = ast_for_expr(c, CHILD(ch, 3));
2164 if (!expr2)
2165 return NULL;
2166 expr3 = ast_for_expr(c, CHILD(ch, 5));
2167 if (!expr3)
2168 return NULL;
2169
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002170 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 }
2172 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002173 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 "unexpected flow_stmt: %d", TYPE(ch));
2175 return NULL;
2176 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002177
2178 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2179 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180}
2181
2182static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002183alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184{
2185 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002186 import_as_name: NAME ['as' NAME]
2187 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 dotted_name: NAME ('.' NAME)*
2189 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002190 PyObject *str;
2191
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 loop:
2193 switch (TYPE(n)) {
2194 case import_as_name:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002195 str = NULL;
2196 if (NCH(n) == 3) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002197 str = NEW_IDENTIFIER(CHILD(n, 2));
2198 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002199 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 case dotted_as_name:
2201 if (NCH(n) == 1) {
2202 n = CHILD(n, 0);
2203 goto loop;
2204 }
2205 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002206 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002207 if (!a)
2208 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 assert(!a->asname);
2210 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2211 return a;
2212 }
2213 break;
2214 case dotted_name:
2215 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002216 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217 else {
2218 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002219 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002220 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221 char *s;
2222
2223 len = 0;
2224 for (i = 0; i < NCH(n); i += 2)
2225 /* length of string plus one for the dot */
2226 len += strlen(STR(CHILD(n, i))) + 1;
2227 len--; /* the last name doesn't have a dot */
2228 str = PyString_FromStringAndSize(NULL, len);
2229 if (!str)
2230 return NULL;
2231 s = PyString_AS_STRING(str);
2232 if (!s)
2233 return NULL;
2234 for (i = 0; i < NCH(n); i += 2) {
2235 char *sch = STR(CHILD(n, i));
2236 strcpy(s, STR(CHILD(n, i)));
2237 s += strlen(sch);
2238 *s++ = '.';
2239 }
2240 --s;
2241 *s = '\0';
2242 PyString_InternInPlace(&str);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002243 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002244 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 }
2246 break;
2247 case STAR:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002248 str = PyString_InternFromString("*");
2249 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002250 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002252 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 "unexpected import name: %d", TYPE(n));
2254 return NULL;
2255 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002256
2257 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 return NULL;
2259}
2260
2261static stmt_ty
2262ast_for_import_stmt(struct compiling *c, const node *n)
2263{
2264 /*
2265 import_stmt: import_name | import_from
2266 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00002267 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
2268 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002270 int lineno;
2271 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 int i;
2273 asdl_seq *aliases;
2274
2275 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002276 lineno = LINENO(n);
2277 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002279 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002281 REQ(n, dotted_as_names);
2282 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2283 if (!aliases)
2284 return NULL;
2285 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002286 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002287 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002289 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002291 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002293 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002295 int idx, ndots = 0;
2296 alias_ty mod = NULL;
2297 identifier modname;
2298
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002299 /* Count the number of dots (for relative imports) and check for the
2300 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002301 for (idx = 1; idx < NCH(n); idx++) {
2302 if (TYPE(CHILD(n, idx)) == dotted_name) {
2303 mod = alias_for_import_name(c, CHILD(n, idx));
2304 idx++;
2305 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00002306 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
2307 /* three consecutive dots are tokenized as one ELLIPSIS */
2308 ndots += 3;
2309 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002310 } else if (TYPE(CHILD(n, idx)) != DOT) {
2311 break;
2312 }
2313 ndots++;
2314 }
2315 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002316 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002317 case STAR:
2318 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002319 n = CHILD(n, idx);
2320 n_children = 1;
2321 if (ndots) {
2322 ast_error(n, "'import *' not allowed with 'from .'");
2323 return NULL;
2324 }
2325 break;
2326 case LPAR:
2327 /* from ... import (x, y, z) */
2328 n = CHILD(n, idx + 1);
2329 n_children = NCH(n);
2330 break;
2331 case import_as_names:
2332 /* from ... import x, y, z */
2333 n = CHILD(n, idx);
2334 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00002335 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 ast_error(n, "trailing comma not allowed without"
2337 " surrounding parentheses");
2338 return NULL;
2339 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002340 break;
2341 default:
2342 ast_error(n, "Unexpected node-type in from-import");
2343 return NULL;
2344 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345
Thomas Wouters89f507f2006-12-13 04:49:30 +00002346 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2347 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349
2350 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002351 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002352 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002353 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002355 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002357 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002358 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00002359 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2360 if (!import_alias)
2361 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002362 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002365 if (mod != NULL)
2366 modname = mod->name;
2367 else
2368 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002369 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002370 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371 }
Neal Norwitz79792652005-11-14 04:25:03 +00002372 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 "unknown import statement: starts with command '%s'",
2374 STR(CHILD(n, 0)));
2375 return NULL;
2376}
2377
2378static stmt_ty
2379ast_for_global_stmt(struct compiling *c, const node *n)
2380{
2381 /* global_stmt: 'global' NAME (',' NAME)* */
2382 identifier name;
2383 asdl_seq *s;
2384 int i;
2385
2386 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002387 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002389 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002391 name = NEW_IDENTIFIER(CHILD(n, i));
2392 if (!name)
2393 return NULL;
2394 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002396 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397}
2398
2399static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00002400ast_for_nonlocal_stmt(struct compiling *c, const node *n)
2401{
2402 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
2403 identifier name;
2404 asdl_seq *s;
2405 int i;
2406
2407 REQ(n, nonlocal_stmt);
2408 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
2409 if (!s)
2410 return NULL;
2411 for (i = 1; i < NCH(n); i += 2) {
2412 name = NEW_IDENTIFIER(CHILD(n, i));
2413 if (!name)
2414 return NULL;
2415 asdl_seq_SET(s, i / 2, name);
2416 }
2417 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
2418}
2419
2420static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421ast_for_assert_stmt(struct compiling *c, const node *n)
2422{
2423 /* assert_stmt: 'assert' test [',' test] */
2424 REQ(n, assert_stmt);
2425 if (NCH(n) == 2) {
2426 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2427 if (!expression)
2428 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002429 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 }
2431 else if (NCH(n) == 4) {
2432 expr_ty expr1, expr2;
2433
2434 expr1 = ast_for_expr(c, CHILD(n, 1));
2435 if (!expr1)
2436 return NULL;
2437 expr2 = ast_for_expr(c, CHILD(n, 3));
2438 if (!expr2)
2439 return NULL;
2440
Thomas Wouters89f507f2006-12-13 04:49:30 +00002441 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 }
Neal Norwitz79792652005-11-14 04:25:03 +00002443 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 "improper number of parts to 'assert' statement: %d",
2445 NCH(n));
2446 return NULL;
2447}
2448
2449static asdl_seq *
2450ast_for_suite(struct compiling *c, const node *n)
2451{
2452 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002453 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 stmt_ty s;
2455 int i, total, num, end, pos = 0;
2456 node *ch;
2457
2458 REQ(n, suite);
2459
2460 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002461 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002465 n = CHILD(n, 0);
2466 /* simple_stmt always ends with a NEWLINE,
2467 and may have a trailing SEMI
2468 */
2469 end = NCH(n) - 1;
2470 if (TYPE(CHILD(n, end - 1)) == SEMI)
2471 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002473 for (i = 0; i < end; i += 2) {
2474 ch = CHILD(n, i);
2475 s = ast_for_stmt(c, ch);
2476 if (!s)
2477 return NULL;
2478 asdl_seq_SET(seq, pos++, s);
2479 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 }
2481 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002482 for (i = 2; i < (NCH(n) - 1); i++) {
2483 ch = CHILD(n, i);
2484 REQ(ch, stmt);
2485 num = num_stmts(ch);
2486 if (num == 1) {
2487 /* small_stmt or compound_stmt with only one child */
2488 s = ast_for_stmt(c, ch);
2489 if (!s)
2490 return NULL;
2491 asdl_seq_SET(seq, pos++, s);
2492 }
2493 else {
2494 int j;
2495 ch = CHILD(ch, 0);
2496 REQ(ch, simple_stmt);
2497 for (j = 0; j < NCH(ch); j += 2) {
2498 /* statement terminates with a semi-colon ';' */
2499 if (NCH(CHILD(ch, j)) == 0) {
2500 assert((j + 1) == NCH(ch));
2501 break;
2502 }
2503 s = ast_for_stmt(c, CHILD(ch, j));
2504 if (!s)
2505 return NULL;
2506 asdl_seq_SET(seq, pos++, s);
2507 }
2508 }
2509 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 }
2511 assert(pos == seq->size);
2512 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513}
2514
2515static stmt_ty
2516ast_for_if_stmt(struct compiling *c, const node *n)
2517{
2518 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2519 ['else' ':' suite]
2520 */
2521 char *s;
2522
2523 REQ(n, if_stmt);
2524
2525 if (NCH(n) == 4) {
2526 expr_ty expression;
2527 asdl_seq *suite_seq;
2528
2529 expression = ast_for_expr(c, CHILD(n, 1));
2530 if (!expression)
2531 return NULL;
2532 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002533 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 return NULL;
2535
Guido van Rossumd8faa362007-04-27 19:54:29 +00002536 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2537 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002539
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 s = STR(CHILD(n, 4));
2541 /* s[2], the third character in the string, will be
2542 's' for el_s_e, or
2543 'i' for el_i_f
2544 */
2545 if (s[2] == 's') {
2546 expr_ty expression;
2547 asdl_seq *seq1, *seq2;
2548
2549 expression = ast_for_expr(c, CHILD(n, 1));
2550 if (!expression)
2551 return NULL;
2552 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002553 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 return NULL;
2555 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002556 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 return NULL;
2558
Guido van Rossumd8faa362007-04-27 19:54:29 +00002559 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2560 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
2562 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002563 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002564 expr_ty expression;
2565 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002566 asdl_seq *orelse = NULL;
2567 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 /* must reference the child n_elif+1 since 'else' token is third,
2569 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002570 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2571 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2572 has_else = 1;
2573 n_elif -= 3;
2574 }
2575 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576
Thomas Wouters89f507f2006-12-13 04:49:30 +00002577 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00002578 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579
Thomas Wouters89f507f2006-12-13 04:49:30 +00002580 orelse = asdl_seq_new(1, c->c_arena);
2581 if (!orelse)
2582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002584 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002586 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2587 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002589 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2590 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592
Guido van Rossumd8faa362007-04-27 19:54:29 +00002593 asdl_seq_SET(orelse, 0,
2594 If(expression, suite_seq, suite_seq2,
2595 LINENO(CHILD(n, NCH(n) - 6)),
2596 CHILD(n, NCH(n) - 6)->n_col_offset,
2597 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002598 /* the just-created orelse handled the last elif */
2599 n_elif--;
2600 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601
Thomas Wouters89f507f2006-12-13 04:49:30 +00002602 for (i = 0; i < n_elif; i++) {
2603 int off = 5 + (n_elif - i - 1) * 4;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002604 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2605 if (!newobj)
2606 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002608 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002611 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613
Thomas Wouters89f507f2006-12-13 04:49:30 +00002614 asdl_seq_SET(newobj, 0,
2615 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002616 LINENO(CHILD(n, off)),
2617 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002618 orelse = newobj;
2619 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002620 expression = ast_for_expr(c, CHILD(n, 1));
2621 if (!expression)
2622 return NULL;
2623 suite_seq = ast_for_suite(c, CHILD(n, 3));
2624 if (!suite_seq)
2625 return NULL;
2626 return If(expression, suite_seq, orelse,
2627 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002629
2630 PyErr_Format(PyExc_SystemError,
2631 "unexpected token in 'if' statement: %s", s);
2632 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633}
2634
2635static stmt_ty
2636ast_for_while_stmt(struct compiling *c, const node *n)
2637{
2638 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2639 REQ(n, while_stmt);
2640
2641 if (NCH(n) == 4) {
2642 expr_ty expression;
2643 asdl_seq *suite_seq;
2644
2645 expression = ast_for_expr(c, CHILD(n, 1));
2646 if (!expression)
2647 return NULL;
2648 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002649 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002651 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 }
2653 else if (NCH(n) == 7) {
2654 expr_ty expression;
2655 asdl_seq *seq1, *seq2;
2656
2657 expression = ast_for_expr(c, CHILD(n, 1));
2658 if (!expression)
2659 return NULL;
2660 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002661 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 return NULL;
2663 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002664 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 return NULL;
2666
Thomas Wouters89f507f2006-12-13 04:49:30 +00002667 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002669
2670 PyErr_Format(PyExc_SystemError,
2671 "wrong number of tokens for 'while' statement: %d",
2672 NCH(n));
2673 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674}
2675
2676static stmt_ty
2677ast_for_for_stmt(struct compiling *c, const node *n)
2678{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002679 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680 expr_ty expression;
2681 expr_ty target;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002682 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2684 REQ(n, for_stmt);
2685
2686 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002687 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 if (!seq)
2689 return NULL;
2690 }
2691
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002692 node_target = CHILD(n, 1);
2693 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002694 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002696 /* Check the # of children rather than the length of _target, since
2697 for x, in ... has 1 element in _target, but still requires a Tuple. */
2698 if (NCH(node_target) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002699 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 else
Thomas Wouters89f507f2006-12-13 04:49:30 +00002701 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002703 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002704 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 return NULL;
2706 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002707 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 return NULL;
2709
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002710 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2711 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712}
2713
2714static excepthandler_ty
2715ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2716{
2717 /* except_clause: 'except' [test [',' test]] */
2718 REQ(exc, except_clause);
2719 REQ(body, suite);
2720
2721 if (NCH(exc) == 1) {
2722 asdl_seq *suite_seq = ast_for_suite(c, body);
2723 if (!suite_seq)
2724 return NULL;
2725
Thomas Wouters89f507f2006-12-13 04:49:30 +00002726 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002727 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728 }
2729 else if (NCH(exc) == 2) {
2730 expr_ty expression;
2731 asdl_seq *suite_seq;
2732
2733 expression = ast_for_expr(c, CHILD(exc, 1));
2734 if (!expression)
2735 return NULL;
2736 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002737 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 return NULL;
2739
Thomas Wouters89f507f2006-12-13 04:49:30 +00002740 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002741 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 }
2743 else if (NCH(exc) == 4) {
2744 asdl_seq *suite_seq;
2745 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00002746 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002747 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002750 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 return NULL;
2752 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002753 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 return NULL;
2755
Thomas Wouters89f507f2006-12-13 04:49:30 +00002756 return excepthandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002757 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002759
2760 PyErr_Format(PyExc_SystemError,
2761 "wrong number of children for 'except' clause: %d",
2762 NCH(exc));
2763 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764}
2765
2766static stmt_ty
2767ast_for_try_stmt(struct compiling *c, const node *n)
2768{
Neal Norwitzf599f422005-12-17 21:33:47 +00002769 const int nch = NCH(n);
2770 int n_except = (nch - 3)/3;
2771 asdl_seq *body, *orelse = NULL, *finally = NULL;
2772
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 REQ(n, try_stmt);
2774
Neal Norwitzf599f422005-12-17 21:33:47 +00002775 body = ast_for_suite(c, CHILD(n, 2));
2776 if (body == NULL)
2777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778
Neal Norwitzf599f422005-12-17 21:33:47 +00002779 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2780 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2781 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2782 /* we can assume it's an "else",
2783 because nch >= 9 for try-else-finally and
2784 it would otherwise have a type of except_clause */
2785 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2786 if (orelse == NULL)
2787 return NULL;
2788 n_except--;
2789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790
Neal Norwitzf599f422005-12-17 21:33:47 +00002791 finally = ast_for_suite(c, CHILD(n, nch - 1));
2792 if (finally == NULL)
2793 return NULL;
2794 n_except--;
2795 }
2796 else {
2797 /* we can assume it's an "else",
2798 otherwise it would have a type of except_clause */
2799 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2800 if (orelse == NULL)
2801 return NULL;
2802 n_except--;
2803 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002805 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002806 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 return NULL;
2808 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002809
2810 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002811 int i;
2812 stmt_ty except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002813 /* process except statements to create a try ... except */
2814 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2815 if (handlers == NULL)
2816 return NULL;
2817
2818 for (i = 0; i < n_except; i++) {
2819 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2820 CHILD(n, 5 + i * 3));
2821 if (!e)
2822 return NULL;
2823 asdl_seq_SET(handlers, i, e);
2824 }
2825
Thomas Wouters89f507f2006-12-13 04:49:30 +00002826 except_st = TryExcept(body, handlers, orelse, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002827 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002828 if (!finally)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002829 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002830
2831 /* if a 'finally' is present too, we nest the TryExcept within a
2832 TryFinally to emulate try ... except ... finally */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002833 body = asdl_seq_new(1, c->c_arena);
2834 if (body == NULL)
2835 return NULL;
2836 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002837 }
2838
2839 /* must be a try ... finally (except clauses are in body, if any exist) */
2840 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002841 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842}
2843
Guido van Rossumc2e20742006-02-27 22:32:47 +00002844static expr_ty
2845ast_for_with_var(struct compiling *c, const node *n)
2846{
2847 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002848 return ast_for_expr(c, CHILD(n, 1));
2849}
2850
2851/* with_stmt: 'with' test [ with_var ] ':' suite */
2852static stmt_ty
2853ast_for_with_stmt(struct compiling *c, const node *n)
2854{
2855 expr_ty context_expr, optional_vars = NULL;
2856 int suite_index = 3; /* skip 'with', test, and ':' */
2857 asdl_seq *suite_seq;
2858
2859 assert(TYPE(n) == with_stmt);
2860 context_expr = ast_for_expr(c, CHILD(n, 1));
2861 if (TYPE(CHILD(n, 2)) == with_var) {
2862 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2863
2864 if (!optional_vars) {
2865 return NULL;
2866 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002867 if (!set_context(optional_vars, Store, n)) {
2868 return NULL;
2869 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002870 suite_index = 4;
2871 }
2872
2873 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2874 if (!suite_seq) {
2875 return NULL;
2876 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002877 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Thomas Wouters89f507f2006-12-13 04:49:30 +00002878 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002879}
2880
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881static stmt_ty
2882ast_for_classdef(struct compiling *c, const node *n)
2883{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002884 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
2885 asdl_seq *s;
2886 expr_ty call, dummy;
2887
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888 REQ(n, classdef);
2889
2890 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002891 ast_error(n, "assignment to None");
2892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 }
2894
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002895 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 s = ast_for_suite(c, CHILD(n, 3));
2897 if (!s)
2898 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002899 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, NULL, NULL, NULL, s,
2900 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002902
2903 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002904 s = ast_for_suite(c, CHILD(n,5));
2905 if (!s)
2906 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002907 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, NULL, NULL, NULL, s,
2908 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 }
2910
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002911 /* class NAME '(' arglist ')' ':' suite */
2912 /* build up a fake Call node so we can extract its pieces */
2913 dummy = Name(NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n), n->n_col_offset, c->c_arena);
2914 call = ast_for_call(c, CHILD(n, 3), dummy);
2915 if (!call)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002918 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002920
2921 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)),
2922 call->v.Call.args, call->v.Call.keywords,
2923 call->v.Call.starargs, call->v.Call.kwargs, s,
2924 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925}
2926
2927static stmt_ty
2928ast_for_stmt(struct compiling *c, const node *n)
2929{
2930 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002931 assert(NCH(n) == 1);
2932 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933 }
2934 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002935 assert(num_stmts(n) == 1);
2936 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937 }
2938 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00002939 REQ(n, small_stmt);
2940 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00002941 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
2942 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00002943 */
2944 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 case expr_stmt:
2946 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 case del_stmt:
2948 return ast_for_del_stmt(c, n);
2949 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002950 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 case flow_stmt:
2952 return ast_for_flow_stmt(c, n);
2953 case import_stmt:
2954 return ast_for_import_stmt(c, n);
2955 case global_stmt:
2956 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00002957 case nonlocal_stmt:
2958 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959 case assert_stmt:
2960 return ast_for_assert_stmt(c, n);
2961 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002962 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2964 TYPE(n), NCH(n));
2965 return NULL;
2966 }
2967 }
2968 else {
2969 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00002970 | funcdef | classdef
2971 */
2972 node *ch = CHILD(n, 0);
2973 REQ(n, compound_stmt);
2974 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 case if_stmt:
2976 return ast_for_if_stmt(c, ch);
2977 case while_stmt:
2978 return ast_for_while_stmt(c, ch);
2979 case for_stmt:
2980 return ast_for_for_stmt(c, ch);
2981 case try_stmt:
2982 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002983 case with_stmt:
2984 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 case funcdef:
2986 return ast_for_funcdef(c, ch);
2987 case classdef:
2988 return ast_for_classdef(c, ch);
2989 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002990 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2992 TYPE(n), NCH(n));
2993 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002994 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 }
2996}
2997
2998static PyObject *
2999parsenumber(const char *s)
3000{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003001 const char *end;
3002 long x;
3003 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004#ifndef WITHOUT_COMPLEX
Guido van Rossumd8faa362007-04-27 19:54:29 +00003005 Py_complex c;
3006 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007#endif
3008
Guido van Rossumd8faa362007-04-27 19:54:29 +00003009 errno = 0;
3010 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011#ifndef WITHOUT_COMPLEX
Guido van Rossumd8faa362007-04-27 19:54:29 +00003012 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00003014 if (*end == 'l' || *end == 'L')
3015 return PyLong_FromString((char *)s, (char **)0, 0);
3016 if (s[0] == '0') {
3017 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3018 if (x < 0 && errno == 0) {
3019 return PyLong_FromString((char *)s,
3020 (char **)0,
3021 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003022 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003023 }
3024 else
3025 x = PyOS_strtol((char *)s, (char **)&end, 0);
3026 if (*end == '\0') {
3027 if (errno != 0)
3028 return PyLong_FromString((char *)s, (char **)0, 0);
3029 return PyInt_FromLong(x);
3030 }
3031 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032#ifndef WITHOUT_COMPLEX
Guido van Rossumd8faa362007-04-27 19:54:29 +00003033 if (imflag) {
3034 c.real = 0.;
3035 PyFPE_START_PROTECT("atof", return 0)
3036 c.imag = PyOS_ascii_atof(s);
3037 PyFPE_END_PROTECT(c)
3038 return PyComplex_FromCComplex(c);
3039 }
3040 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00003042 {
3043 PyFPE_START_PROTECT("atof", return 0)
3044 dx = PyOS_ascii_atof(s);
3045 PyFPE_END_PROTECT(dx)
3046 return PyFloat_FromDouble(dx);
3047 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048}
3049
3050static PyObject *
3051decode_utf8(const char **sPtr, const char *end, char* encoding)
3052{
3053#ifndef Py_USING_UNICODE
Guido van Rossumd8faa362007-04-27 19:54:29 +00003054 Py_FatalError("decode_utf8 should not be called in this build.");
3055 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056#else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003057 PyObject *u, *v;
3058 char *s, *t;
3059 t = s = (char *)*sPtr;
3060 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3061 while (s < end && (*s & 0x80)) s++;
3062 *sPtr = s;
3063 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3064 if (u == NULL)
3065 return NULL;
3066 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3067 Py_DECREF(u);
3068 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003069#endif
3070}
3071
3072static PyObject *
3073decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3074{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003075 PyObject *v, *u;
3076 char *buf;
3077 char *p;
3078 const char *end;
3079 if (encoding == NULL) {
3080 buf = (char *)s;
3081 u = NULL;
3082 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3083 buf = (char *)s;
3084 u = NULL;
3085 } else {
3086 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3087 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3088 if (u == NULL)
3089 return NULL;
3090 p = buf = PyString_AsString(u);
3091 end = s + len;
3092 while (s < end) {
3093 if (*s == '\\') {
3094 *p++ = *s++;
3095 if (*s & 0x80) {
3096 strcpy(p, "u005c");
3097 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003098 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003099 }
3100 if (*s & 0x80) { /* XXX inefficient */
3101 PyObject *w;
3102 char *r;
3103 Py_ssize_t rn, i;
3104 w = decode_utf8(&s, end, "utf-16-be");
3105 if (w == NULL) {
3106 Py_DECREF(u);
3107 return NULL;
3108 }
3109 r = PyString_AsString(w);
3110 rn = PyString_Size(w);
3111 assert(rn % 2 == 0);
3112 for (i = 0; i < rn; i += 2) {
3113 sprintf(p, "\\u%02x%02x",
3114 r[i + 0] & 0xFF,
3115 r[i + 1] & 0xFF);
3116 p += 6;
3117 }
3118 Py_DECREF(w);
3119 } else {
3120 *p++ = *s++;
3121 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003122 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003123 len = p - buf;
3124 s = buf;
3125 }
3126 if (rawmode)
3127 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3128 else
3129 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3130 Py_XDECREF(u);
3131 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132}
3133
3134/* s is a Python string literal, including the bracketing quote characters,
3135 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3136 * parsestr parses it, and returns the decoded Python string object.
3137 */
3138static PyObject *
Thomas Wouters00e41de2007-02-23 19:56:57 +00003139parsestr(const node *n, const char *encoding, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003141 size_t len;
3142 const char *s = STR(n);
3143 int quote = Py_CHARMASK(*s);
3144 int rawmode = 0;
3145 int need_encoding;
3146 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147
Guido van Rossumd8faa362007-04-27 19:54:29 +00003148 if (isalpha(quote) || quote == '_') {
3149 if (quote == 'u' || quote == 'U') {
3150 quote = *++s;
3151 unicode = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003152 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003153 if (quote == 'b' || quote == 'B') {
3154 quote = *++s;
3155 *bytesmode = 1;
3156 }
3157 if (quote == 'r' || quote == 'R') {
3158 quote = *++s;
3159 rawmode = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003160 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003161 }
3162 if (quote != '\'' && quote != '\"') {
3163 PyErr_BadInternalCall();
3164 return NULL;
3165 }
3166 if (unicode && *bytesmode) {
3167 ast_error(n, "string cannot be both bytes and unicode");
3168 return NULL;
3169 }
3170 s++;
3171 len = strlen(s);
3172 if (len > INT_MAX) {
3173 PyErr_SetString(PyExc_OverflowError,
3174 "string to parse is too long");
3175 return NULL;
3176 }
3177 if (s[--len] != quote) {
3178 PyErr_BadInternalCall();
3179 return NULL;
3180 }
3181 if (len >= 4 && s[0] == quote && s[1] == quote) {
3182 s += 2;
3183 len -= 2;
3184 if (s[--len] != quote || s[--len] != quote) {
3185 PyErr_BadInternalCall();
3186 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003187 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003188 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189#ifdef Py_USING_UNICODE
Guido van Rossum572dbf82007-04-27 23:53:51 +00003190 if (!*bytesmode) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003191 return decode_unicode(s, len, rawmode, encoding);
3192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00003194 if (*bytesmode) {
3195 /* Disallow non-ascii characters (but not escapes) */
3196 const char *c;
3197 for (c = s; *c; c++) {
3198 if (Py_CHARMASK(*c) >= 0x80) {
3199 ast_error(n, "bytes can only contain ASCII "
3200 "literal characters.");
3201 return NULL;
3202 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00003203 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003204 }
3205 need_encoding = (!*bytesmode && encoding != NULL &&
3206 strcmp(encoding, "utf-8") != 0 &&
3207 strcmp(encoding, "iso-8859-1") != 0);
3208 if (rawmode || strchr(s, '\\') == NULL) {
3209 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210#ifndef Py_USING_UNICODE
Guido van Rossumd8faa362007-04-27 19:54:29 +00003211 /* This should not happen - we never see any other
3212 encoding. */
3213 Py_FatalError(
3214 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215#else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003216 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3217 if (u == NULL)
3218 return NULL;
3219 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3220 Py_DECREF(u);
3221 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222#endif
Guido van Rossumd8faa362007-04-27 19:54:29 +00003223 } else {
3224 return PyString_FromStringAndSize(s, len);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003225 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003226 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227
Guido van Rossumd8faa362007-04-27 19:54:29 +00003228 return PyString_DecodeEscape(s, len, NULL, unicode,
3229 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230}
3231
3232/* Build a Python string object out of a STRING atom. This takes care of
3233 * compile-time literal catenation, calling parsestr() on each piece, and
3234 * pasting the intermediate results together.
3235 */
3236static PyObject *
Thomas Wouters00e41de2007-02-23 19:56:57 +00003237parsestrplus(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003239 PyObject *v;
3240 int i;
3241 REQ(CHILD(n, 0), STRING);
3242 v = parsestr(CHILD(n, 0), c->c_encoding, bytesmode);
3243 if (v != NULL) {
3244 /* String literal concatenation */
3245 for (i = 1; i < NCH(n); i++) {
3246 PyObject *s;
3247 int subbm = 0;
3248 s = parsestr(CHILD(n, i), c->c_encoding, &subbm);
3249 if (s == NULL)
3250 goto onError;
3251 if (*bytesmode != subbm) {
3252 ast_error(n, "cannot mix bytes and nonbytes"
3253 "literals");
3254 goto onError;
3255 }
3256 if (PyString_Check(v) && PyString_Check(s)) {
3257 PyString_ConcatAndDel(&v, s);
3258 if (v == NULL)
3259 goto onError;
3260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261#ifdef Py_USING_UNICODE
Guido van Rossumd8faa362007-04-27 19:54:29 +00003262 else {
3263 PyObject *temp = PyUnicode_Concat(v, s);
3264 Py_DECREF(s);
3265 Py_DECREF(v);
3266 v = temp;
3267 if (v == NULL)
3268 goto onError;
3269 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00003271 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003272 }
3273 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274
Guido van Rossumd8faa362007-04-27 19:54:29 +00003275 onError:
3276 Py_XDECREF(v);
3277 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278}