blob: a7d5713169d2a10daa9f61a76a559078c8c9d06e [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 *);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000028static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000029static expr_ty ast_for_testlist(struct compiling *, const node *);
30static expr_ty ast_for_testlist_gexp(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 *);
36static PyObject *parsestr(const char *s, const char *encoding);
37static PyObject *parsestrplus(struct compiling *, const node *n);
38
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039#ifndef LINENO
Thomas Wouters89f507f2006-12-13 04:49:30 +000040#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041#endif
42
Neal Norwitzadb69fc2005-12-17 20:54:49 +000043static identifier
44new_identifier(const char* n, PyArena *arena) {
45 PyObject* id = PyString_InternFromString(n);
46 PyArena_AddPyObject(arena, id);
47 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048}
49
Neal Norwitzadb69fc2005-12-17 20:54:49 +000050#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000051
52/* This routine provides an invalid object for the syntax error.
53 The outermost routine must unpack this error and create the
54 proper object. We do this so that we don't have to pass
55 the filename to everything function.
56
57 XXX Maybe we should just pass the filename...
58*/
59
60static int
61ast_error(const node *n, const char *errstr)
62{
63 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
64 if (!u)
Thomas Wouters89f507f2006-12-13 04:49:30 +000065 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000066 PyErr_SetObject(PyExc_SyntaxError, u);
67 Py_DECREF(u);
68 return 0;
69}
70
71static void
72ast_error_finish(const char *filename)
73{
74 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000075 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000076
77 assert(PyErr_Occurred());
78 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Thomas Wouters89f507f2006-12-13 04:49:30 +000079 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080
81 PyErr_Fetch(&type, &value, &tback);
82 errstr = PyTuple_GetItem(value, 0);
83 if (!errstr)
Thomas Wouters89f507f2006-12-13 04:49:30 +000084 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085 Py_INCREF(errstr);
86 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000087 if (lineno == -1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +000088 Py_DECREF(errstr);
89 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000090 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091 Py_DECREF(value);
92
93 loc = PyErr_ProgramText(filename, lineno);
94 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +000095 Py_INCREF(Py_None);
96 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +000098 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000100 if (!tmp) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101 Py_DECREF(errstr);
102 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000103 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(errstr);
106 Py_DECREF(tmp);
107 if (!value)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000108 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109 PyErr_Restore(type, value, tback);
110}
111
112/* num_stmts() returns number of contained statements.
113
114 Use this routine to determine how big a sequence is needed for
115 the statements in a parse tree. Its raison d'etre is this bit of
116 grammar:
117
118 stmt: simple_stmt | compound_stmt
119 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
120
121 A simple_stmt can contain multiple small_stmt elements joined
122 by semicolons. If the arg is a simple_stmt, the number of
123 small_stmt elements is returned.
124*/
125
126static int
127num_stmts(const node *n)
128{
129 int i, l;
130 node *ch;
131
132 switch (TYPE(n)) {
133 case single_input:
134 if (TYPE(CHILD(n, 0)) == NEWLINE)
135 return 0;
136 else
137 return num_stmts(CHILD(n, 0));
138 case file_input:
139 l = 0;
140 for (i = 0; i < NCH(n); i++) {
141 ch = CHILD(n, i);
142 if (TYPE(ch) == stmt)
143 l += num_stmts(ch);
144 }
145 return l;
146 case stmt:
147 return num_stmts(CHILD(n, 0));
148 case compound_stmt:
149 return 1;
150 case simple_stmt:
151 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
152 case suite:
153 if (NCH(n) == 1)
154 return num_stmts(CHILD(n, 0));
155 else {
156 l = 0;
157 for (i = 2; i < (NCH(n) - 1); i++)
158 l += num_stmts(CHILD(n, i));
159 return l;
160 }
161 default: {
162 char buf[128];
163
164 sprintf(buf, "Non-statement found: %d %d\n",
165 TYPE(n), NCH(n));
166 Py_FatalError(buf);
167 }
168 }
169 assert(0);
170 return 0;
171}
172
173/* Transform the CST rooted at node * to the appropriate AST
174*/
175
176mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000177PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
178 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000180 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181 asdl_seq *stmts = NULL;
182 stmt_ty s;
183 node *ch;
184 struct compiling c;
185
186 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000187 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000188 if (TYPE(n) == encoding_decl) {
189 ast_error(n, "encoding declaration in Unicode string");
190 goto error;
191 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192 } else if (TYPE(n) == encoding_decl) {
193 c.c_encoding = STR(n);
194 n = CHILD(n, 0);
195 } else {
196 c.c_encoding = NULL;
197 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000198 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
Jeremy Hyltona8293132006-02-28 17:58:27 +0000200 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201 switch (TYPE(n)) {
202 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000203 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204 if (!stmts)
205 return NULL;
206 for (i = 0; i < NCH(n) - 1; i++) {
207 ch = CHILD(n, i);
208 if (TYPE(ch) == NEWLINE)
209 continue;
210 REQ(ch, stmt);
211 num = num_stmts(ch);
212 if (num == 1) {
213 s = ast_for_stmt(&c, ch);
214 if (!s)
215 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000216 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 }
218 else {
219 ch = CHILD(ch, 0);
220 REQ(ch, simple_stmt);
221 for (j = 0; j < num; j++) {
222 s = ast_for_stmt(&c, CHILD(ch, j * 2));
223 if (!s)
224 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000225 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 }
227 }
228 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000229 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230 case eval_input: {
231 expr_ty testlist_ast;
232
233 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000234 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235 if (!testlist_ast)
236 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000237 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 }
239 case single_input:
240 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000241 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 if (!stmts)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000243 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
245 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000246 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247 }
248 else {
249 n = CHILD(n, 0);
250 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000251 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 if (!stmts)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000253 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000255 s = ast_for_stmt(&c, n);
256 if (!s)
257 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 asdl_seq_SET(stmts, 0, s);
259 }
260 else {
261 /* Only a simple_stmt can contain multiple statements. */
262 REQ(n, simple_stmt);
263 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 if (TYPE(CHILD(n, i)) == NEWLINE)
265 break;
266 s = ast_for_stmt(&c, CHILD(n, i));
267 if (!s)
268 goto error;
269 asdl_seq_SET(stmts, i / 2, s);
270 }
271 }
272
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000273 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 }
275 default:
276 goto error;
277 }
278 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 ast_error_finish(filename);
280 return NULL;
281}
282
283/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
284*/
285
286static operator_ty
287get_operator(const node *n)
288{
289 switch (TYPE(n)) {
290 case VBAR:
291 return BitOr;
292 case CIRCUMFLEX:
293 return BitXor;
294 case AMPER:
295 return BitAnd;
296 case LEFTSHIFT:
297 return LShift;
298 case RIGHTSHIFT:
299 return RShift;
300 case PLUS:
301 return Add;
302 case MINUS:
303 return Sub;
304 case STAR:
305 return Mult;
306 case SLASH:
307 return Div;
308 case DOUBLESLASH:
309 return FloorDiv;
310 case PERCENT:
311 return Mod;
312 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314 }
315}
316
Jeremy Hyltona8293132006-02-28 17:58:27 +0000317/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318
319 Only sets context for expr kinds that "can appear in assignment context"
320 (according to ../Parser/Python.asdl). For other expr kinds, it sets
321 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322*/
323
324static int
325set_context(expr_ty e, expr_context_ty ctx, const node *n)
326{
327 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000328 /* If a particular expression type can't be used for assign / delete,
329 set expr_name to its name and an error message will be generated.
330 */
331 const char* expr_name = NULL;
332
333 /* The ast defines augmented store and load contexts, but the
334 implementation here doesn't actually use them. The code may be
335 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000336 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000337 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000338 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000339 */
340 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341
342 switch (e->kind) {
343 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000344 if (ctx == Store &&
345 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
346 return ast_error(n, "assignment to None");
347 }
348 e->v.Attribute.ctx = ctx;
349 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000351 e->v.Subscript.ctx = ctx;
352 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353 case Name_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 if (ctx == Store &&
355 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
356 return ast_error(n, "assignment to None");
357 }
358 e->v.Name.ctx = ctx;
359 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000361 e->v.List.ctx = ctx;
362 s = e->v.List.elts;
363 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364 case Tuple_kind:
365 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
366 return ast_error(n, "can't assign to ()");
Thomas Wouters89f507f2006-12-13 04:49:30 +0000367 e->v.Tuple.ctx = ctx;
368 s = e->v.Tuple.elts;
369 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000370 case Lambda_kind:
371 expr_name = "lambda";
372 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000374 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000375 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000378 case UnaryOp_kind:
379 expr_name = "operator";
380 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 expr_name = "generator expression";
383 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000384 case Yield_kind:
385 expr_name = "yield expression";
386 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000387 case ListComp_kind:
388 expr_name = "list comprehension";
389 break;
390 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000391 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392 case Num_kind:
393 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000394 expr_name = "literal";
395 break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000396 case Ellipsis_kind:
397 expr_name = "Ellipsis";
398 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000399 case Compare_kind:
400 expr_name = "comparison";
401 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000402 case IfExp_kind:
403 expr_name = "conditional expression";
404 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000405 default:
406 PyErr_Format(PyExc_SystemError,
407 "unexpected expression in assignment %d (line %d)",
408 e->kind, e->lineno);
409 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000411 /* Check for error string set by switch */
412 if (expr_name) {
413 char buf[300];
414 PyOS_snprintf(buf, sizeof(buf),
415 "can't %s %s",
416 ctx == Store ? "assign to" : "delete",
417 expr_name);
418 return ast_error(n, buf);
419 }
420
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000422 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423 */
424 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000425 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426
Thomas Wouters89f507f2006-12-13 04:49:30 +0000427 for (i = 0; i < asdl_seq_LEN(s); i++) {
428 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
429 return 0;
430 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431 }
432 return 1;
433}
434
435static operator_ty
436ast_for_augassign(const node *n)
437{
438 REQ(n, augassign);
439 n = CHILD(n, 0);
440 switch (STR(n)[0]) {
441 case '+':
442 return Add;
443 case '-':
444 return Sub;
445 case '/':
446 if (STR(n)[1] == '/')
447 return FloorDiv;
448 else
449 return Div;
450 case '%':
451 return Mod;
452 case '<':
453 return LShift;
454 case '>':
455 return RShift;
456 case '&':
457 return BitAnd;
458 case '^':
459 return BitXor;
460 case '|':
461 return BitOr;
462 case '*':
463 if (STR(n)[1] == '*')
464 return Pow;
465 else
466 return Mult;
467 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000468 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470 }
471}
472
473static cmpop_ty
474ast_for_comp_op(const node *n)
475{
Guido van Rossumb053cd82006-08-24 03:53:23 +0000476 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 |'is' 'not'
478 */
479 REQ(n, comp_op);
480 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000481 n = CHILD(n, 0);
482 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483 case LESS:
484 return Lt;
485 case GREATER:
486 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000487 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488 return Eq;
489 case LESSEQUAL:
490 return LtE;
491 case GREATEREQUAL:
492 return GtE;
493 case NOTEQUAL:
494 return NotEq;
495 case NAME:
496 if (strcmp(STR(n), "in") == 0)
497 return In;
498 if (strcmp(STR(n), "is") == 0)
499 return Is;
500 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000501 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000504 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505 }
506 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000507 /* handle "not in" and "is not" */
508 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 case NAME:
510 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
511 return NotIn;
512 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
513 return IsNot;
514 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000515 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000518 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 }
Neal Norwitz79792652005-11-14 04:25:03 +0000520 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000522 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523}
524
525static asdl_seq *
526seq_for_testlist(struct compiling *c, const node *n)
527{
528 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000529 asdl_seq *seq;
530 expr_ty expression;
531 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532 assert(TYPE(n) == testlist
Thomas Wouters89f507f2006-12-13 04:49:30 +0000533 || TYPE(n) == listmaker
534 || TYPE(n) == testlist_gexp
535 || TYPE(n) == testlist_safe
536 || TYPE(n) == testlist1
537 );
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) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000544 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
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
987 if (NCH(n) == 6) { /* 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{
1024 /* lambdef: 'lambda' [varargslist] ':' test */
1025 arguments_ty args;
1026 expr_ty expression;
1027
1028 if (NCH(n) == 3) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001029 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1030 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 if (!args)
1032 return NULL;
1033 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001034 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 }
1037 else {
1038 args = ast_for_arguments(c, CHILD(n, 1));
1039 if (!args)
1040 return NULL;
1041 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001042 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 }
1045
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001046 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047}
1048
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001049static expr_ty
1050ast_for_ifexpr(struct compiling *c, const node *n)
1051{
1052 /* test: or_test 'if' or_test 'else' test */
1053 expr_ty expression, body, orelse;
1054
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001055 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001056 body = ast_for_expr(c, CHILD(n, 0));
1057 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001058 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001059 expression = ast_for_expr(c, CHILD(n, 2));
1060 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001061 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001062 orelse = ast_for_expr(c, CHILD(n, 4));
1063 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001064 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001065 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1066 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001067}
1068
Thomas Wouters89f507f2006-12-13 04:49:30 +00001069/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
1070 so there is only a single version. Possibly for loops can also re-use
1071 the code.
1072*/
1073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074/* Count the number of 'for' loop in a list comprehension.
1075
1076 Helper for ast_for_listcomp().
1077*/
1078
1079static int
1080count_list_fors(const node *n)
1081{
1082 int n_fors = 0;
1083 node *ch = CHILD(n, 1);
1084
1085 count_list_for:
1086 n_fors++;
1087 REQ(ch, list_for);
1088 if (NCH(ch) == 5)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001089 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090 else
Thomas Wouters89f507f2006-12-13 04:49:30 +00001091 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 count_list_iter:
1093 REQ(ch, list_iter);
1094 ch = CHILD(ch, 0);
1095 if (TYPE(ch) == list_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001096 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 else if (TYPE(ch) == list_if) {
1098 if (NCH(ch) == 3) {
1099 ch = CHILD(ch, 2);
1100 goto count_list_iter;
1101 }
1102 else
1103 return n_fors;
1104 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001105
1106 /* Should never be reached */
1107 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1108 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109}
1110
1111/* Count the number of 'if' statements in a list comprehension.
1112
1113 Helper for ast_for_listcomp().
1114*/
1115
1116static int
1117count_list_ifs(const node *n)
1118{
1119 int n_ifs = 0;
1120
1121 count_list_iter:
1122 REQ(n, list_iter);
1123 if (TYPE(CHILD(n, 0)) == list_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001124 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 n = CHILD(n, 0);
1126 REQ(n, list_if);
1127 n_ifs++;
1128 if (NCH(n) == 2)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001129 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 n = CHILD(n, 2);
1131 goto count_list_iter;
1132}
1133
1134static expr_ty
1135ast_for_listcomp(struct compiling *c, const node *n)
1136{
1137 /* listmaker: test ( list_for | (',' test)* [','] )
1138 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1139 list_iter: list_for | list_if
1140 list_if: 'if' test [list_iter]
1141 testlist_safe: test [(',' test)+ [',']]
1142 */
1143 expr_ty elt;
1144 asdl_seq *listcomps;
1145 int i, n_fors;
1146 node *ch;
1147
1148 REQ(n, listmaker);
1149 assert(NCH(n) > 1);
1150
1151 elt = ast_for_expr(c, CHILD(n, 0));
1152 if (!elt)
1153 return NULL;
1154
1155 n_fors = count_list_fors(n);
1156 if (n_fors == -1)
1157 return NULL;
1158
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159 listcomps = asdl_seq_new(n_fors, c->c_arena);
1160 if (!listcomps)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001161 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001162
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 ch = CHILD(n, 1);
1164 for (i = 0; i < n_fors; i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 comprehension_ty lc;
1166 asdl_seq *t;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 expr_ty expression;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001168 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169
Thomas Wouters89f507f2006-12-13 04:49:30 +00001170 REQ(ch, list_for);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171
Thomas Wouters89f507f2006-12-13 04:49:30 +00001172 for_ch = CHILD(ch, 1);
1173 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001174 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001176 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001177 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179
Thomas Wouters89f507f2006-12-13 04:49:30 +00001180 /* Check the # of children rather than the length of t, since
1181 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
1182 if (NCH(for_ch) == 1)
1183 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001184 c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001185 else
1186 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001187 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001188 expression, NULL, c->c_arena);
1189 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191
Thomas Wouters89f507f2006-12-13 04:49:30 +00001192 if (NCH(ch) == 5) {
1193 int j, n_ifs;
1194 asdl_seq *ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195
Thomas Wouters89f507f2006-12-13 04:49:30 +00001196 ch = CHILD(ch, 4);
1197 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001198 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200
Thomas Wouters89f507f2006-12-13 04:49:30 +00001201 ifs = asdl_seq_new(n_ifs, c->c_arena);
1202 if (!ifs)
1203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204
Thomas Wouters89f507f2006-12-13 04:49:30 +00001205 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001206 REQ(ch, list_iter);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001207 ch = CHILD(ch, 0);
1208 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209
Thomas Wouters89f507f2006-12-13 04:49:30 +00001210 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1211 if (NCH(ch) == 3)
1212 ch = CHILD(ch, 2);
1213 }
1214 /* on exit, must guarantee that ch is a list_for */
1215 if (TYPE(ch) == list_iter)
1216 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 lc->ifs = ifs;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001218 }
1219 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 }
1221
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001222 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223}
1224
1225/*
1226 Count the number of 'for' loops in a generator expression.
1227
1228 Helper for ast_for_genexp().
1229*/
1230
1231static int
1232count_gen_fors(const node *n)
1233{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001234 int n_fors = 0;
1235 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236
1237 count_gen_for:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001238 n_fors++;
1239 REQ(ch, gen_for);
1240 if (NCH(ch) == 5)
1241 ch = CHILD(ch, 4);
1242 else
1243 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 count_gen_iter:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001245 REQ(ch, gen_iter);
1246 ch = CHILD(ch, 0);
1247 if (TYPE(ch) == gen_for)
1248 goto count_gen_for;
1249 else if (TYPE(ch) == gen_if) {
1250 if (NCH(ch) == 3) {
1251 ch = CHILD(ch, 2);
1252 goto count_gen_iter;
1253 }
1254 else
1255 return n_fors;
1256 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001257
Thomas Wouters89f507f2006-12-13 04:49:30 +00001258 /* Should never be reached */
1259 PyErr_SetString(PyExc_SystemError,
1260 "logic error in count_gen_fors");
1261 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262}
1263
1264/* Count the number of 'if' statements in a generator expression.
1265
1266 Helper for ast_for_genexp().
1267*/
1268
1269static int
1270count_gen_ifs(const node *n)
1271{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001272 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273
Thomas Wouters89f507f2006-12-13 04:49:30 +00001274 while (1) {
1275 REQ(n, gen_iter);
1276 if (TYPE(CHILD(n, 0)) == gen_for)
1277 return n_ifs;
1278 n = CHILD(n, 0);
1279 REQ(n, gen_if);
1280 n_ifs++;
1281 if (NCH(n) == 2)
1282 return n_ifs;
1283 n = CHILD(n, 2);
1284 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285}
1286
Jeremy Hyltona8293132006-02-28 17:58:27 +00001287/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288static expr_ty
1289ast_for_genexp(struct compiling *c, const node *n)
1290{
1291 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Thomas Wouters89f507f2006-12-13 04:49:30 +00001292 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293 expr_ty elt;
1294 asdl_seq *genexps;
1295 int i, n_fors;
1296 node *ch;
1297
1298 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1299 assert(NCH(n) > 1);
1300
1301 elt = ast_for_expr(c, CHILD(n, 0));
1302 if (!elt)
1303 return NULL;
1304
1305 n_fors = count_gen_fors(n);
1306 if (n_fors == -1)
1307 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001308
1309 genexps = asdl_seq_new(n_fors, c->c_arena);
1310 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001312
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 ch = CHILD(n, 1);
1314 for (i = 0; i < n_fors; i++) {
1315 comprehension_ty ge;
1316 asdl_seq *t;
1317 expr_ty expression;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001318 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319
1320 REQ(ch, gen_for);
1321
Thomas Wouters89f507f2006-12-13 04:49:30 +00001322 for_ch = CHILD(ch, 1);
1323 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001324 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001326 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001327 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001328 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001329
Thomas Wouters89f507f2006-12-13 04:49:30 +00001330 /* Check the # of children rather than the length of t, since
1331 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1332 if (NCH(for_ch) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001334 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001336 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1337 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001338 expression, NULL, c->c_arena);
1339
1340 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001342
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 if (NCH(ch) == 5) {
1344 int j, n_ifs;
1345 asdl_seq *ifs;
1346
1347 ch = CHILD(ch, 4);
1348 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001349 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001351
1352 ifs = asdl_seq_new(n_ifs, c->c_arena);
1353 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001355
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 for (j = 0; j < n_ifs; j++) {
1357 REQ(ch, gen_iter);
1358 ch = CHILD(ch, 0);
1359 REQ(ch, gen_if);
1360
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001361 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001362 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001363 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001364 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 if (NCH(ch) == 3)
1366 ch = CHILD(ch, 2);
1367 }
1368 /* on exit, must guarantee that ch is a gen_for */
1369 if (TYPE(ch) == gen_iter)
1370 ch = CHILD(ch, 0);
1371 ge->ifs = ifs;
1372 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001373 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 }
1375
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001376 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377}
1378
1379static expr_ty
1380ast_for_atom(struct compiling *c, const node *n)
1381{
1382 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
Guido van Rossum86e58e22006-08-28 15:27:34 +00001383 | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING+
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384 */
1385 node *ch = CHILD(n, 0);
1386
1387 switch (TYPE(ch)) {
1388 case NAME:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001389 /* All names start in Load context, but may later be
1390 changed. */
1391 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 case STRING: {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001393 PyObject *str = parsestrplus(c, n);
1394 if (!str)
1395 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001396
Thomas Wouters89f507f2006-12-13 04:49:30 +00001397 PyArena_AddPyObject(c->c_arena, str);
1398 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 }
1400 case NUMBER: {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001401 PyObject *pynum = parsenumber(STR(ch));
1402 if (!pynum)
1403 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001404
Thomas Wouters89f507f2006-12-13 04:49:30 +00001405 PyArena_AddPyObject(c->c_arena, pynum);
1406 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001408 case DOT: /* Ellipsis */
1409 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001411 ch = CHILD(n, 1);
1412
1413 if (TYPE(ch) == RPAR)
1414 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1415
1416 if (TYPE(ch) == yield_expr)
1417 return ast_for_expr(c, ch);
1418
1419 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1420 return ast_for_genexp(c, ch);
1421
1422 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001424 ch = CHILD(n, 1);
1425
1426 if (TYPE(ch) == RSQB)
1427 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1428
1429 REQ(ch, listmaker);
1430 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1431 asdl_seq *elts = seq_for_testlist(c, ch);
1432 if (!elts)
1433 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001434
Thomas Wouters89f507f2006-12-13 04:49:30 +00001435 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1436 }
1437 else
1438 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 case LBRACE: {
Neal Norwitzc1505362006-12-28 06:47:50 +00001440 /* dictsetmaker: test ':' test (',' test ':' test)* [','] |
1441 * test (',' test)* [','] */
1442 int i, size;
1443 asdl_seq *keys, *values;
1444
1445 ch = CHILD(n, 1);
1446 if (NCH(ch) == 1 || (NCH(ch) > 0 && STR(CHILD(ch, 1))[0] == ',')) {
Guido van Rossum86e58e22006-08-28 15:27:34 +00001447 /* it's a set */
1448 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
1449 keys = asdl_seq_new(size, c->c_arena);
1450 if (!keys)
1451 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001452
Guido van Rossum86e58e22006-08-28 15:27:34 +00001453 for (i = 0; i < NCH(ch); i += 2) {
1454 expr_ty expression;
1455 expression = ast_for_expr(c, CHILD(ch, i));
1456 if (!expression)
1457 return NULL;
1458 asdl_seq_SET(keys, i / 2, expression);
1459 }
1460 return Set(keys, LINENO(n), n->n_col_offset, c->c_arena);
1461 } else {
1462 /* it's a dict */
1463 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1464 keys = asdl_seq_new(size, c->c_arena);
1465 if (!keys)
1466 return NULL;
1467
1468 values = asdl_seq_new(size, c->c_arena);
1469 if (!values)
1470 return NULL;
1471
1472 for (i = 0; i < NCH(ch); i += 4) {
1473 expr_ty expression;
1474
1475 expression = ast_for_expr(c, CHILD(ch, i));
1476 if (!expression)
1477 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001478
Guido van Rossum86e58e22006-08-28 15:27:34 +00001479 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitze76adcd2005-11-15 05:04:31 +00001480
Guido van Rossum86e58e22006-08-28 15:27:34 +00001481 expression = ast_for_expr(c, CHILD(ch, i + 2));
1482 if (!expression)
1483 return NULL;
1484
1485 asdl_seq_SET(values, i / 4, expression);
1486 }
1487 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001491 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1492 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 }
1494}
1495
1496static slice_ty
1497ast_for_slice(struct compiling *c, const node *n)
1498{
1499 node *ch;
1500 expr_ty lower = NULL, upper = NULL, step = NULL;
1501
1502 REQ(n, subscript);
1503
1504 /*
Georg Brandl52318d62006-09-06 07:06:08 +00001505 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 sliceop: ':' [test]
1507 */
1508 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 if (NCH(n) == 1 && TYPE(ch) == test) {
1510 /* 'step' variable hold no significance in terms of being used over
1511 other vars */
1512 step = ast_for_expr(c, ch);
1513 if (!step)
1514 return NULL;
1515
Thomas Wouters89f507f2006-12-13 04:49:30 +00001516 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 }
1518
1519 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001520 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 if (!lower)
1522 return NULL;
1523 }
1524
1525 /* If there's an upper bound it's in the second or third position. */
1526 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001527 if (NCH(n) > 1) {
1528 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529
Thomas Wouters89f507f2006-12-13 04:49:30 +00001530 if (TYPE(n2) == test) {
1531 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 if (!upper)
1533 return NULL;
1534 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001535 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001537 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538
Thomas Wouters89f507f2006-12-13 04:49:30 +00001539 if (TYPE(n2) == test) {
1540 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 if (!upper)
1542 return NULL;
1543 }
1544 }
1545
1546 ch = CHILD(n, NCH(n) - 1);
1547 if (TYPE(ch) == sliceop) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001548 if (NCH(ch) == 1) {
1549 /* No expression, so step is None */
1550 ch = CHILD(ch, 0);
1551 step = Name(new_identifier("None", c->c_arena), Load,
1552 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 if (!step)
1554 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001555 } else {
1556 ch = CHILD(ch, 1);
1557 if (TYPE(ch) == test) {
1558 step = ast_for_expr(c, ch);
1559 if (!step)
1560 return NULL;
1561 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 }
1563 }
1564
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001565 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566}
1567
1568static expr_ty
1569ast_for_binop(struct compiling *c, const node *n)
1570{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001571 /* Must account for a sequence of expressions.
1572 How should A op B op C by represented?
1573 BinOp(BinOp(A, op, B), op, C).
1574 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575
Thomas Wouters89f507f2006-12-13 04:49:30 +00001576 int i, nops;
1577 expr_ty expr1, expr2, result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001578 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579
1580 expr1 = ast_for_expr(c, CHILD(n, 0));
1581 if (!expr1)
1582 return NULL;
1583
1584 expr2 = ast_for_expr(c, CHILD(n, 2));
1585 if (!expr2)
1586 return NULL;
1587
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001588 newoperator = get_operator(CHILD(n, 1));
1589 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590 return NULL;
1591
Thomas Wouters89f507f2006-12-13 04:49:30 +00001592 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001593 c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001594 if (!result)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 return NULL;
1596
Thomas Wouters89f507f2006-12-13 04:49:30 +00001597 nops = (NCH(n) - 1) / 2;
1598 for (i = 1; i < nops; i++) {
1599 expr_ty tmp_result, tmp;
1600 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601
Thomas Wouters89f507f2006-12-13 04:49:30 +00001602 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001603 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604 return NULL;
1605
1606 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1607 if (!tmp)
1608 return NULL;
1609
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001610 tmp_result = BinOp(result, newoperator, tmp,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001611 LINENO(next_oper), next_oper->n_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001612 c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001613 if (!tmp)
1614 return NULL;
1615 result = tmp_result;
1616 }
1617 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618}
1619
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001620static expr_ty
1621ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1622{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001623 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1624 subscriptlist: subscript (',' subscript)* [',']
1625 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1626 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001627 REQ(n, trailer);
1628 if (TYPE(CHILD(n, 0)) == LPAR) {
1629 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001630 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1631 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001632 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001633 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001634 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001635 else if (TYPE(CHILD(n, 0)) == DOT ) {
1636 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001637 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001638 }
1639 else {
1640 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001641 REQ(CHILD(n, 2), RSQB);
1642 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001643 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001644 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1645 if (!slc)
1646 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001647 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1648 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001649 }
1650 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001651 /* The grammar is ambiguous here. The ambiguity is resolved
1652 by treating the sequence as a tuple literal if there are
1653 no slice features.
1654 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001655 int j;
1656 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001657 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001658 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001659 asdl_seq *slices, *elts;
1660 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001661 if (!slices)
1662 return NULL;
1663 for (j = 0; j < NCH(n); j += 2) {
1664 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001665 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001666 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001667 if (slc->kind != Index_kind)
1668 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001669 asdl_seq_SET(slices, j / 2, slc);
1670 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001671 if (!simple) {
1672 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001673 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001674 }
1675 /* extract Index values and put them in a Tuple */
1676 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001677 if (!elts)
1678 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001679 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1680 slc = (slice_ty)asdl_seq_GET(slices, j);
1681 assert(slc->kind == Index_kind && slc->v.Index.value);
1682 asdl_seq_SET(elts, j, slc->v.Index.value);
1683 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001684 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001685 if (!e)
1686 return NULL;
1687 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001688 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001689 }
1690 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001691}
1692
1693static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001694ast_for_factor(struct compiling *c, const node *n)
1695{
1696 node *pfactor, *ppower, *patom, *pnum;
1697 expr_ty expression;
1698
1699 /* If the unary - operator is applied to a constant, don't generate
1700 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1701 constant. The peephole optimizer already does something like
1702 this but it doesn't handle the case where the constant is
1703 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1704 PyLongObject.
1705 */
1706 if (TYPE(CHILD(n, 0)) == MINUS
1707 && NCH(n) == 2
1708 && TYPE((pfactor = CHILD(n, 1))) == factor
1709 && NCH(pfactor) == 1
1710 && TYPE((ppower = CHILD(pfactor, 0))) == power
1711 && NCH(ppower) == 1
1712 && TYPE((patom = CHILD(ppower, 0))) == atom
1713 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1714 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1715 if (s == NULL)
1716 return NULL;
1717 s[0] = '-';
1718 strcpy(s + 1, STR(pnum));
1719 PyObject_FREE(STR(pnum));
1720 STR(pnum) = s;
1721 return ast_for_atom(c, patom);
1722 }
1723
1724 expression = ast_for_expr(c, CHILD(n, 1));
1725 if (!expression)
1726 return NULL;
1727
1728 switch (TYPE(CHILD(n, 0))) {
1729 case PLUS:
1730 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1731 c->c_arena);
1732 case MINUS:
1733 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1734 c->c_arena);
1735 case TILDE:
1736 return UnaryOp(Invert, expression, LINENO(n),
1737 n->n_col_offset, c->c_arena);
1738 }
1739 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1740 TYPE(CHILD(n, 0)));
1741 return NULL;
1742}
1743
1744static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001745ast_for_power(struct compiling *c, const node *n)
1746{
1747 /* power: atom trailer* ('**' factor)*
1748 */
1749 int i;
1750 expr_ty e, tmp;
1751 REQ(n, power);
1752 e = ast_for_atom(c, CHILD(n, 0));
1753 if (!e)
1754 return NULL;
1755 if (NCH(n) == 1)
1756 return e;
1757 for (i = 1; i < NCH(n); i++) {
1758 node *ch = CHILD(n, i);
1759 if (TYPE(ch) != trailer)
1760 break;
1761 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001762 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001763 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001764 tmp->lineno = e->lineno;
1765 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001766 e = tmp;
1767 }
1768 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1769 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001770 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001771 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001772 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001773 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001774 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001775 e = tmp;
1776 }
1777 return e;
1778}
1779
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780/* Do not name a variable 'expr'! Will cause a compile error.
1781*/
1782
1783static expr_ty
1784ast_for_expr(struct compiling *c, const node *n)
1785{
1786 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001787 test: or_test ['if' or_test 'else' test] | lambdef
1788 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789 and_test: not_test ('and' not_test)*
1790 not_test: 'not' not_test | comparison
1791 comparison: expr (comp_op expr)*
1792 expr: xor_expr ('|' xor_expr)*
1793 xor_expr: and_expr ('^' and_expr)*
1794 and_expr: shift_expr ('&' shift_expr)*
1795 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1796 arith_expr: term (('+'|'-') term)*
1797 term: factor (('*'|'/'|'%'|'//') factor)*
1798 factor: ('+'|'-'|'~') factor | power
1799 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001800
1801 As well as modified versions that exist for backward compatibility,
1802 to explicitly allow:
1803 [ x for x in lambda: 0, lambda: 1 ]
1804 (which would be ambiguous without these extra rules)
1805
1806 old_test: or_test | old_lambdef
1807 old_lambdef: 'lambda' [vararglist] ':' old_test
1808
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 */
1810
1811 asdl_seq *seq;
1812 int i;
1813
1814 loop:
1815 switch (TYPE(n)) {
1816 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001817 case old_test:
1818 if (TYPE(CHILD(n, 0)) == lambdef ||
1819 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001821 else if (NCH(n) > 1)
1822 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001823 /* Fallthrough */
1824 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 case and_test:
1826 if (NCH(n) == 1) {
1827 n = CHILD(n, 0);
1828 goto loop;
1829 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001830 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 if (!seq)
1832 return NULL;
1833 for (i = 0; i < NCH(n); i += 2) {
1834 expr_ty e = ast_for_expr(c, CHILD(n, i));
1835 if (!e)
1836 return NULL;
1837 asdl_seq_SET(seq, i / 2, e);
1838 }
1839 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001840 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1841 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001842 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001843 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 case not_test:
1845 if (NCH(n) == 1) {
1846 n = CHILD(n, 0);
1847 goto loop;
1848 }
1849 else {
1850 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1851 if (!expression)
1852 return NULL;
1853
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001854 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1855 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 }
1857 case comparison:
1858 if (NCH(n) == 1) {
1859 n = CHILD(n, 0);
1860 goto loop;
1861 }
1862 else {
1863 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001864 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001865 asdl_seq *cmps;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001866 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 if (!ops)
1868 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871 return NULL;
1872 }
1873 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001874 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001876 newoperator = ast_for_comp_op(CHILD(n, i));
1877 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001879 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880
1881 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001882 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001884 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001886 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 asdl_seq_SET(cmps, i / 2, expression);
1888 }
1889 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001890 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001894 return Compare(expression, ops, cmps, LINENO(n),
1895 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 }
1897 break;
1898
1899 /* The next five cases all handle BinOps. The main body of code
1900 is the same in each case, but the switch turned inside out to
1901 reuse the code for each type of operator.
1902 */
1903 case expr:
1904 case xor_expr:
1905 case and_expr:
1906 case shift_expr:
1907 case arith_expr:
1908 case term:
1909 if (NCH(n) == 1) {
1910 n = CHILD(n, 0);
1911 goto loop;
1912 }
1913 return ast_for_binop(c, n);
1914 case yield_expr: {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001915 expr_ty exp = NULL;
1916 if (NCH(n) == 2) {
1917 exp = ast_for_testlist(c, CHILD(n, 1));
1918 if (!exp)
1919 return NULL;
1920 }
1921 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1922 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001923 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924 if (NCH(n) == 1) {
1925 n = CHILD(n, 0);
1926 goto loop;
1927 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001928 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001929 case power:
1930 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001932 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933 return NULL;
1934 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001935 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 return NULL;
1937}
1938
1939static expr_ty
1940ast_for_call(struct compiling *c, const node *n, expr_ty func)
1941{
1942 /*
1943 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1944 | '**' test)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001945 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 */
1947
1948 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001949 asdl_seq *args;
1950 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 expr_ty vararg = NULL, kwarg = NULL;
1952
1953 REQ(n, arglist);
1954
1955 nargs = 0;
1956 nkeywords = 0;
1957 ngens = 0;
1958 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001959 node *ch = CHILD(n, i);
1960 if (TYPE(ch) == argument) {
1961 if (NCH(ch) == 1)
1962 nargs++;
1963 else if (TYPE(CHILD(ch, 1)) == gen_for)
1964 ngens++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965 else
Thomas Wouters89f507f2006-12-13 04:49:30 +00001966 nkeywords++;
1967 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968 }
1969 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001970 ast_error(n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00001971 "if not sole argument");
1972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 }
1974
1975 if (nargs + nkeywords + ngens > 255) {
1976 ast_error(n, "more than 255 arguments");
1977 return NULL;
1978 }
1979
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001980 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982 return NULL;
1983 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 nargs = 0;
1987 nkeywords = 0;
1988 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001989 node *ch = CHILD(n, i);
1990 if (TYPE(ch) == argument) {
1991 expr_ty e;
1992 if (NCH(ch) == 1) {
1993 if (nkeywords) {
1994 ast_error(CHILD(ch, 0),
1995 "non-keyword arg after keyword arg");
1996 return NULL;
1997 }
1998 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002000 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002001 asdl_seq_SET(args, nargs++, e);
2002 }
2003 else if (TYPE(CHILD(ch, 1)) == gen_for) {
2004 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002006 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002007 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002009 else {
2010 keyword_ty kw;
2011 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012
Thomas Wouters89f507f2006-12-13 04:49:30 +00002013 /* CHILD(ch, 0) is test, but must be an identifier? */
2014 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 /* f(lambda x: x[0] = 3) ends up getting parsed with
2018 * LHS test = lambda x: x[0], and RHS test = 3.
2019 * SF bug 132313 points out that complaining about a keyword
2020 * then is very confusing.
2021 */
2022 if (e->kind == Lambda_kind) {
2023 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002024 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 } else if (e->kind != Name_kind) {
2026 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002027 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002029 key = e->v.Name.id;
2030 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002032 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002033 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002035 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002036 asdl_seq_SET(keywords, nkeywords++, kw);
2037 }
2038 }
2039 else if (TYPE(ch) == STAR) {
2040 vararg = ast_for_expr(c, CHILD(n, i+1));
2041 i++;
2042 }
2043 else if (TYPE(ch) == DOUBLESTAR) {
2044 kwarg = ast_for_expr(c, CHILD(n, i+1));
2045 i++;
2046 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 }
2048
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002049 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050}
2051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002053ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002055 /* testlist_gexp: test (',' test)* [','] */
2056 /* testlist: test (',' test)* [','] */
2057 /* testlist_safe: test (',' test)+ [','] */
2058 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002060 if (TYPE(n) == testlist_gexp) {
2061 if (NCH(n) > 1)
2062 assert(TYPE(CHILD(n, 1)) != gen_for);
2063 }
2064 else {
2065 assert(TYPE(n) == testlist ||
2066 TYPE(n) == testlist_safe ||
2067 TYPE(n) == testlist1);
2068 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002070 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 else {
2072 asdl_seq *tmp = seq_for_testlist(c, n);
2073 if (!tmp)
2074 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002075 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002077}
2078
2079static expr_ty
2080ast_for_testlist_gexp(struct compiling *c, const node* n)
2081{
2082 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2083 /* argument: test [ gen_for ] */
2084 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002085 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002086 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002087 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002088}
2089
2090/* like ast_for_testlist() but returns a sequence */
2091static asdl_seq*
2092ast_for_class_bases(struct compiling *c, const node* n)
2093{
2094 /* testlist: test (',' test)* [','] */
2095 assert(NCH(n) > 0);
2096 REQ(n, testlist);
2097 if (NCH(n) == 1) {
2098 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002099 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002100 if (!bases)
2101 return NULL;
2102 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002103 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002104 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002105 asdl_seq_SET(bases, 0, base);
2106 return bases;
2107 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002108
2109 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110}
2111
2112static stmt_ty
2113ast_for_expr_stmt(struct compiling *c, const node *n)
2114{
2115 REQ(n, expr_stmt);
2116 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2117 | ('=' (yield_expr|testlist))*)
2118 testlist: test (',' test)* [',']
2119 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002120 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 test: ... here starts the operator precendence dance
2122 */
2123
2124 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002125 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 if (!e)
2127 return NULL;
2128
Thomas Wouters89f507f2006-12-13 04:49:30 +00002129 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 }
2131 else if (TYPE(CHILD(n, 1)) == augassign) {
2132 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002133 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002134 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135
Thomas Wouters89f507f2006-12-13 04:49:30 +00002136 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 if (!expr1)
2138 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002139 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00002140 switch (expr1->kind) {
2141 case GeneratorExp_kind:
2142 ast_error(ch, "augmented assignment to generator "
2143 "expression not possible");
2144 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002145 case Yield_kind:
2146 ast_error(ch, "augmented assignment to yield "
2147 "expression not possible");
2148 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00002149 case Name_kind: {
2150 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2151 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2152 ast_error(ch, "assignment to None");
2153 return NULL;
2154 }
2155 break;
2156 }
2157 case Attribute_kind:
2158 case Subscript_kind:
2159 break;
2160 default:
2161 ast_error(ch, "illegal expression for augmented "
2162 "assignment");
2163 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002165 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 ch = CHILD(n, 2);
2168 if (TYPE(ch) == testlist)
2169 expr2 = ast_for_testlist(c, ch);
2170 else
2171 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002172 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 return NULL;
2174
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002175 newoperator = ast_for_augassign(CHILD(n, 1));
2176 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 return NULL;
2178
Thomas Wouters89f507f2006-12-13 04:49:30 +00002179 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 }
2181 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002182 int i;
2183 asdl_seq *targets;
2184 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 expr_ty expression;
2186
Thomas Wouters89f507f2006-12-13 04:49:30 +00002187 /* a normal assignment */
2188 REQ(CHILD(n, 1), EQUAL);
2189 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2190 if (!targets)
2191 return NULL;
2192 for (i = 0; i < NCH(n) - 2; i += 2) {
2193 expr_ty e;
2194 node *ch = CHILD(n, i);
2195 if (TYPE(ch) == yield_expr) {
2196 ast_error(ch, "assignment to yield expression not possible");
2197 return NULL;
2198 }
2199 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200
Thomas Wouters89f507f2006-12-13 04:49:30 +00002201 /* set context to assign */
2202 if (!e)
2203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204
Thomas Wouters89f507f2006-12-13 04:49:30 +00002205 if (!set_context(e, Store, CHILD(n, i)))
2206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207
Thomas Wouters89f507f2006-12-13 04:49:30 +00002208 asdl_seq_SET(targets, i / 2, e);
2209 }
2210 value = CHILD(n, NCH(n) - 1);
2211 if (TYPE(value) == testlist)
2212 expression = ast_for_testlist(c, value);
2213 else
2214 expression = ast_for_expr(c, value);
2215 if (!expression)
2216 return NULL;
2217 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219}
2220
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002222ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223{
2224 asdl_seq *seq;
2225 int i;
2226 expr_ty e;
2227
2228 REQ(n, exprlist);
2229
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002230 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002232 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002233 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002234 e = ast_for_expr(c, CHILD(n, i));
2235 if (!e)
2236 return NULL;
2237 asdl_seq_SET(seq, i / 2, e);
2238 if (context && !set_context(e, context, CHILD(n, i)))
2239 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 }
2241 return seq;
2242}
2243
2244static stmt_ty
2245ast_for_del_stmt(struct compiling *c, const node *n)
2246{
2247 asdl_seq *expr_list;
2248
2249 /* del_stmt: 'del' exprlist */
2250 REQ(n, del_stmt);
2251
2252 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2253 if (!expr_list)
2254 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002255 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256}
2257
2258static stmt_ty
2259ast_for_flow_stmt(struct compiling *c, const node *n)
2260{
2261 /*
2262 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2263 | yield_stmt
2264 break_stmt: 'break'
2265 continue_stmt: 'continue'
2266 return_stmt: 'return' [testlist]
2267 yield_stmt: yield_expr
2268 yield_expr: 'yield' testlist
2269 raise_stmt: 'raise' [test [',' test [',' test]]]
2270 */
2271 node *ch;
2272
2273 REQ(n, flow_stmt);
2274 ch = CHILD(n, 0);
2275 switch (TYPE(ch)) {
2276 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002277 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002279 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002281 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2282 if (!exp)
2283 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002284 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 }
2286 case return_stmt:
2287 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002288 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002290 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 if (!expression)
2292 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002293 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 }
2295 case raise_stmt:
2296 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002297 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 else if (NCH(ch) == 2) {
2299 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2300 if (!expression)
2301 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002302 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303 }
2304 else if (NCH(ch) == 4) {
2305 expr_ty expr1, expr2;
2306
2307 expr1 = ast_for_expr(c, CHILD(ch, 1));
2308 if (!expr1)
2309 return NULL;
2310 expr2 = ast_for_expr(c, CHILD(ch, 3));
2311 if (!expr2)
2312 return NULL;
2313
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002314 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315 }
2316 else if (NCH(ch) == 6) {
2317 expr_ty expr1, expr2, expr3;
2318
2319 expr1 = ast_for_expr(c, CHILD(ch, 1));
2320 if (!expr1)
2321 return NULL;
2322 expr2 = ast_for_expr(c, CHILD(ch, 3));
2323 if (!expr2)
2324 return NULL;
2325 expr3 = ast_for_expr(c, CHILD(ch, 5));
2326 if (!expr3)
2327 return NULL;
2328
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002329 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 }
2331 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002332 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 "unexpected flow_stmt: %d", TYPE(ch));
2334 return NULL;
2335 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002336
2337 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2338 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339}
2340
2341static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002342alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343{
2344 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002345 import_as_name: NAME ['as' NAME]
2346 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 dotted_name: NAME ('.' NAME)*
2348 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002349 PyObject *str;
2350
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 loop:
2352 switch (TYPE(n)) {
2353 case import_as_name:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002354 str = NULL;
2355 if (NCH(n) == 3) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002356 str = NEW_IDENTIFIER(CHILD(n, 2));
2357 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002358 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 case dotted_as_name:
2360 if (NCH(n) == 1) {
2361 n = CHILD(n, 0);
2362 goto loop;
2363 }
2364 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002365 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002366 if (!a)
2367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368 assert(!a->asname);
2369 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2370 return a;
2371 }
2372 break;
2373 case dotted_name:
2374 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002375 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 else {
2377 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002378 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002379 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380 char *s;
2381
2382 len = 0;
2383 for (i = 0; i < NCH(n); i += 2)
2384 /* length of string plus one for the dot */
2385 len += strlen(STR(CHILD(n, i))) + 1;
2386 len--; /* the last name doesn't have a dot */
2387 str = PyString_FromStringAndSize(NULL, len);
2388 if (!str)
2389 return NULL;
2390 s = PyString_AS_STRING(str);
2391 if (!s)
2392 return NULL;
2393 for (i = 0; i < NCH(n); i += 2) {
2394 char *sch = STR(CHILD(n, i));
2395 strcpy(s, STR(CHILD(n, i)));
2396 s += strlen(sch);
2397 *s++ = '.';
2398 }
2399 --s;
2400 *s = '\0';
2401 PyString_InternInPlace(&str);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002402 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002403 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 }
2405 break;
2406 case STAR:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002407 str = PyString_InternFromString("*");
2408 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002409 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002411 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 "unexpected import name: %d", TYPE(n));
2413 return NULL;
2414 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002415
2416 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002417 return NULL;
2418}
2419
2420static stmt_ty
2421ast_for_import_stmt(struct compiling *c, const node *n)
2422{
2423 /*
2424 import_stmt: import_name | import_from
2425 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002426 import_from: 'from' ('.'* dotted_name | '.') 'import'
2427 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002429 int lineno;
2430 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 int i;
2432 asdl_seq *aliases;
2433
2434 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002435 lineno = LINENO(n);
2436 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002438 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002440 REQ(n, dotted_as_names);
2441 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2442 if (!aliases)
2443 return NULL;
2444 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002445 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002446 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002448 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002450 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002452 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002454 int idx, ndots = 0;
2455 alias_ty mod = NULL;
2456 identifier modname;
2457
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002458 /* Count the number of dots (for relative imports) and check for the
2459 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002460 for (idx = 1; idx < NCH(n); idx++) {
2461 if (TYPE(CHILD(n, idx)) == dotted_name) {
2462 mod = alias_for_import_name(c, CHILD(n, idx));
2463 idx++;
2464 break;
2465 } else if (TYPE(CHILD(n, idx)) != DOT) {
2466 break;
2467 }
2468 ndots++;
2469 }
2470 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002471 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002472 case STAR:
2473 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002474 n = CHILD(n, idx);
2475 n_children = 1;
2476 if (ndots) {
2477 ast_error(n, "'import *' not allowed with 'from .'");
2478 return NULL;
2479 }
2480 break;
2481 case LPAR:
2482 /* from ... import (x, y, z) */
2483 n = CHILD(n, idx + 1);
2484 n_children = NCH(n);
2485 break;
2486 case import_as_names:
2487 /* from ... import x, y, z */
2488 n = CHILD(n, idx);
2489 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00002490 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 ast_error(n, "trailing comma not allowed without"
2492 " surrounding parentheses");
2493 return NULL;
2494 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002495 break;
2496 default:
2497 ast_error(n, "Unexpected node-type in from-import");
2498 return NULL;
2499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500
Thomas Wouters89f507f2006-12-13 04:49:30 +00002501 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2502 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504
2505 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002506 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002507 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002508 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002510 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002512 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002513 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00002514 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2515 if (!import_alias)
2516 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002517 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002518 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002520 if (mod != NULL)
2521 modname = mod->name;
2522 else
2523 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002524 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002525 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 }
Neal Norwitz79792652005-11-14 04:25:03 +00002527 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 "unknown import statement: starts with command '%s'",
2529 STR(CHILD(n, 0)));
2530 return NULL;
2531}
2532
2533static stmt_ty
2534ast_for_global_stmt(struct compiling *c, const node *n)
2535{
2536 /* global_stmt: 'global' NAME (',' NAME)* */
2537 identifier name;
2538 asdl_seq *s;
2539 int i;
2540
2541 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002542 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002544 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002546 name = NEW_IDENTIFIER(CHILD(n, i));
2547 if (!name)
2548 return NULL;
2549 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002551 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552}
2553
2554static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555ast_for_assert_stmt(struct compiling *c, const node *n)
2556{
2557 /* assert_stmt: 'assert' test [',' test] */
2558 REQ(n, assert_stmt);
2559 if (NCH(n) == 2) {
2560 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2561 if (!expression)
2562 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002563 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564 }
2565 else if (NCH(n) == 4) {
2566 expr_ty expr1, expr2;
2567
2568 expr1 = ast_for_expr(c, CHILD(n, 1));
2569 if (!expr1)
2570 return NULL;
2571 expr2 = ast_for_expr(c, CHILD(n, 3));
2572 if (!expr2)
2573 return NULL;
2574
Thomas Wouters89f507f2006-12-13 04:49:30 +00002575 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 }
Neal Norwitz79792652005-11-14 04:25:03 +00002577 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 "improper number of parts to 'assert' statement: %d",
2579 NCH(n));
2580 return NULL;
2581}
2582
2583static asdl_seq *
2584ast_for_suite(struct compiling *c, const node *n)
2585{
2586 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002587 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 stmt_ty s;
2589 int i, total, num, end, pos = 0;
2590 node *ch;
2591
2592 REQ(n, suite);
2593
2594 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002595 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002597 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002599 n = CHILD(n, 0);
2600 /* simple_stmt always ends with a NEWLINE,
2601 and may have a trailing SEMI
2602 */
2603 end = NCH(n) - 1;
2604 if (TYPE(CHILD(n, end - 1)) == SEMI)
2605 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002607 for (i = 0; i < end; i += 2) {
2608 ch = CHILD(n, i);
2609 s = ast_for_stmt(c, ch);
2610 if (!s)
2611 return NULL;
2612 asdl_seq_SET(seq, pos++, s);
2613 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 }
2615 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002616 for (i = 2; i < (NCH(n) - 1); i++) {
2617 ch = CHILD(n, i);
2618 REQ(ch, stmt);
2619 num = num_stmts(ch);
2620 if (num == 1) {
2621 /* small_stmt or compound_stmt with only one child */
2622 s = ast_for_stmt(c, ch);
2623 if (!s)
2624 return NULL;
2625 asdl_seq_SET(seq, pos++, s);
2626 }
2627 else {
2628 int j;
2629 ch = CHILD(ch, 0);
2630 REQ(ch, simple_stmt);
2631 for (j = 0; j < NCH(ch); j += 2) {
2632 /* statement terminates with a semi-colon ';' */
2633 if (NCH(CHILD(ch, j)) == 0) {
2634 assert((j + 1) == NCH(ch));
2635 break;
2636 }
2637 s = ast_for_stmt(c, CHILD(ch, j));
2638 if (!s)
2639 return NULL;
2640 asdl_seq_SET(seq, pos++, s);
2641 }
2642 }
2643 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 }
2645 assert(pos == seq->size);
2646 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647}
2648
2649static stmt_ty
2650ast_for_if_stmt(struct compiling *c, const node *n)
2651{
2652 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2653 ['else' ':' suite]
2654 */
2655 char *s;
2656
2657 REQ(n, if_stmt);
2658
2659 if (NCH(n) == 4) {
2660 expr_ty expression;
2661 asdl_seq *suite_seq;
2662
2663 expression = ast_for_expr(c, CHILD(n, 1));
2664 if (!expression)
2665 return NULL;
2666 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002667 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 return NULL;
2669
Thomas Wouters89f507f2006-12-13 04:49:30 +00002670 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002672
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 s = STR(CHILD(n, 4));
2674 /* s[2], the third character in the string, will be
2675 's' for el_s_e, or
2676 'i' for el_i_f
2677 */
2678 if (s[2] == 's') {
2679 expr_ty expression;
2680 asdl_seq *seq1, *seq2;
2681
2682 expression = ast_for_expr(c, CHILD(n, 1));
2683 if (!expression)
2684 return NULL;
2685 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002686 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687 return NULL;
2688 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002689 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690 return NULL;
2691
Thomas Wouters89f507f2006-12-13 04:49:30 +00002692 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 }
2694 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002695 int i, n_elif, has_else = 0;
2696 asdl_seq *orelse = NULL;
2697 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 /* must reference the child n_elif+1 since 'else' token is third,
2699 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002700 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2701 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2702 has_else = 1;
2703 n_elif -= 3;
2704 }
2705 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706
Thomas Wouters89f507f2006-12-13 04:49:30 +00002707 if (has_else) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 expr_ty expression;
2709 asdl_seq *seq1, *seq2;
2710
Thomas Wouters89f507f2006-12-13 04:49:30 +00002711 orelse = asdl_seq_new(1, c->c_arena);
2712 if (!orelse)
2713 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002715 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002718 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002721 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723
Thomas Wouters89f507f2006-12-13 04:49:30 +00002724 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
2725 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002726 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 /* the just-created orelse handled the last elif */
2728 n_elif--;
2729 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730
Thomas Wouters89f507f2006-12-13 04:49:30 +00002731 for (i = 0; i < n_elif; i++) {
2732 int off = 5 + (n_elif - i - 1) * 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 expr_ty expression;
2734 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002735 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2736 if (!newobj)
2737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002739 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002742 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744
Thomas Wouters89f507f2006-12-13 04:49:30 +00002745 asdl_seq_SET(newobj, 0,
2746 If(expression, suite_seq, orelse,
2747 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
2748 orelse = newobj;
2749 }
2750 return If(ast_for_expr(c, CHILD(n, 1)),
2751 ast_for_suite(c, CHILD(n, 3)),
2752 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002754
2755 PyErr_Format(PyExc_SystemError,
2756 "unexpected token in 'if' statement: %s", s);
2757 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758}
2759
2760static stmt_ty
2761ast_for_while_stmt(struct compiling *c, const node *n)
2762{
2763 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2764 REQ(n, while_stmt);
2765
2766 if (NCH(n) == 4) {
2767 expr_ty expression;
2768 asdl_seq *suite_seq;
2769
2770 expression = ast_for_expr(c, CHILD(n, 1));
2771 if (!expression)
2772 return NULL;
2773 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002774 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002776 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 }
2778 else if (NCH(n) == 7) {
2779 expr_ty expression;
2780 asdl_seq *seq1, *seq2;
2781
2782 expression = ast_for_expr(c, CHILD(n, 1));
2783 if (!expression)
2784 return NULL;
2785 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002786 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787 return NULL;
2788 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002789 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 return NULL;
2791
Thomas Wouters89f507f2006-12-13 04:49:30 +00002792 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002794
2795 PyErr_Format(PyExc_SystemError,
2796 "wrong number of tokens for 'while' statement: %d",
2797 NCH(n));
2798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799}
2800
2801static stmt_ty
2802ast_for_for_stmt(struct compiling *c, const node *n)
2803{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002804 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 expr_ty expression;
2806 expr_ty target;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002807 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2809 REQ(n, for_stmt);
2810
2811 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002812 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 if (!seq)
2814 return NULL;
2815 }
2816
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002817 node_target = CHILD(n, 1);
2818 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002819 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002821 /* Check the # of children rather than the length of _target, since
2822 for x, in ... has 1 element in _target, but still requires a Tuple. */
2823 if (NCH(node_target) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002824 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 else
Thomas Wouters89f507f2006-12-13 04:49:30 +00002826 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002828 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002829 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 return NULL;
2831 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002832 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 return NULL;
2834
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002835 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2836 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837}
2838
2839static excepthandler_ty
2840ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2841{
2842 /* except_clause: 'except' [test [',' test]] */
2843 REQ(exc, except_clause);
2844 REQ(body, suite);
2845
2846 if (NCH(exc) == 1) {
2847 asdl_seq *suite_seq = ast_for_suite(c, body);
2848 if (!suite_seq)
2849 return NULL;
2850
Thomas Wouters89f507f2006-12-13 04:49:30 +00002851 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002852 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 }
2854 else if (NCH(exc) == 2) {
2855 expr_ty expression;
2856 asdl_seq *suite_seq;
2857
2858 expression = ast_for_expr(c, CHILD(exc, 1));
2859 if (!expression)
2860 return NULL;
2861 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002862 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 return NULL;
2864
Thomas Wouters89f507f2006-12-13 04:49:30 +00002865 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002866 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 }
2868 else if (NCH(exc) == 4) {
2869 asdl_seq *suite_seq;
2870 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00002871 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002872 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002875 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 return NULL;
2877 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002878 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 return NULL;
2880
Thomas Wouters89f507f2006-12-13 04:49:30 +00002881 return excepthandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002882 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002884
2885 PyErr_Format(PyExc_SystemError,
2886 "wrong number of children for 'except' clause: %d",
2887 NCH(exc));
2888 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889}
2890
2891static stmt_ty
2892ast_for_try_stmt(struct compiling *c, const node *n)
2893{
Neal Norwitzf599f422005-12-17 21:33:47 +00002894 const int nch = NCH(n);
2895 int n_except = (nch - 3)/3;
2896 asdl_seq *body, *orelse = NULL, *finally = NULL;
2897
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898 REQ(n, try_stmt);
2899
Neal Norwitzf599f422005-12-17 21:33:47 +00002900 body = ast_for_suite(c, CHILD(n, 2));
2901 if (body == NULL)
2902 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903
Neal Norwitzf599f422005-12-17 21:33:47 +00002904 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2905 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2906 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2907 /* we can assume it's an "else",
2908 because nch >= 9 for try-else-finally and
2909 it would otherwise have a type of except_clause */
2910 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2911 if (orelse == NULL)
2912 return NULL;
2913 n_except--;
2914 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915
Neal Norwitzf599f422005-12-17 21:33:47 +00002916 finally = ast_for_suite(c, CHILD(n, nch - 1));
2917 if (finally == NULL)
2918 return NULL;
2919 n_except--;
2920 }
2921 else {
2922 /* we can assume it's an "else",
2923 otherwise it would have a type of except_clause */
2924 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2925 if (orelse == NULL)
2926 return NULL;
2927 n_except--;
2928 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002930 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002931 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002932 return NULL;
2933 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002934
2935 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002936 int i;
2937 stmt_ty except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002938 /* process except statements to create a try ... except */
2939 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2940 if (handlers == NULL)
2941 return NULL;
2942
2943 for (i = 0; i < n_except; i++) {
2944 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2945 CHILD(n, 5 + i * 3));
2946 if (!e)
2947 return NULL;
2948 asdl_seq_SET(handlers, i, e);
2949 }
2950
Thomas Wouters89f507f2006-12-13 04:49:30 +00002951 except_st = TryExcept(body, handlers, orelse, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002952 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002953 if (!finally)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002954 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002955
2956 /* if a 'finally' is present too, we nest the TryExcept within a
2957 TryFinally to emulate try ... except ... finally */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002958 body = asdl_seq_new(1, c->c_arena);
2959 if (body == NULL)
2960 return NULL;
2961 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002962 }
2963
2964 /* must be a try ... finally (except clauses are in body, if any exist) */
2965 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002966 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967}
2968
Guido van Rossumc2e20742006-02-27 22:32:47 +00002969static expr_ty
2970ast_for_with_var(struct compiling *c, const node *n)
2971{
2972 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002973 return ast_for_expr(c, CHILD(n, 1));
2974}
2975
2976/* with_stmt: 'with' test [ with_var ] ':' suite */
2977static stmt_ty
2978ast_for_with_stmt(struct compiling *c, const node *n)
2979{
2980 expr_ty context_expr, optional_vars = NULL;
2981 int suite_index = 3; /* skip 'with', test, and ':' */
2982 asdl_seq *suite_seq;
2983
2984 assert(TYPE(n) == with_stmt);
2985 context_expr = ast_for_expr(c, CHILD(n, 1));
2986 if (TYPE(CHILD(n, 2)) == with_var) {
2987 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2988
2989 if (!optional_vars) {
2990 return NULL;
2991 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002992 if (!set_context(optional_vars, Store, n)) {
2993 return NULL;
2994 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002995 suite_index = 4;
2996 }
2997
2998 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2999 if (!suite_seq) {
3000 return NULL;
3001 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003002 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Thomas Wouters89f507f2006-12-13 04:49:30 +00003003 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003004}
3005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006static stmt_ty
3007ast_for_classdef(struct compiling *c, const node *n)
3008{
3009 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 asdl_seq *bases, *s;
3011
3012 REQ(n, classdef);
3013
3014 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003015 ast_error(n, "assignment to None");
3016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017 }
3018
3019 if (NCH(n) == 4) {
3020 s = ast_for_suite(c, CHILD(n, 3));
3021 if (!s)
3022 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003023 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003024 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 }
3026 /* check for empty base list */
3027 if (TYPE(CHILD(n,3)) == RPAR) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003028 s = ast_for_suite(c, CHILD(n,5));
3029 if (!s)
3030 return NULL;
3031 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003032 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033 }
3034
3035 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003036 bases = ast_for_class_bases(c, CHILD(n, 3));
3037 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039
3040 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003041 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003043 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
3044 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045}
3046
3047static stmt_ty
3048ast_for_stmt(struct compiling *c, const node *n)
3049{
3050 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003051 assert(NCH(n) == 1);
3052 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053 }
3054 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003055 assert(num_stmts(n) == 1);
3056 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 }
3058 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003059 REQ(n, small_stmt);
3060 n = CHILD(n, 0);
Guido van Rossum452bf512007-02-09 05:32:43 +00003061 /* small_stmt: expr_stmt | del_stmt | pass_stmt
Neal Norwitzc1505362006-12-28 06:47:50 +00003062 | flow_stmt | import_stmt | global_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003063 */
3064 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065 case expr_stmt:
3066 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003067 case del_stmt:
3068 return ast_for_del_stmt(c, n);
3069 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003070 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 case flow_stmt:
3072 return ast_for_flow_stmt(c, n);
3073 case import_stmt:
3074 return ast_for_import_stmt(c, n);
3075 case global_stmt:
3076 return ast_for_global_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 case assert_stmt:
3078 return ast_for_assert_stmt(c, n);
3079 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003080 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3082 TYPE(n), NCH(n));
3083 return NULL;
3084 }
3085 }
3086 else {
3087 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003088 | funcdef | classdef
3089 */
3090 node *ch = CHILD(n, 0);
3091 REQ(n, compound_stmt);
3092 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003093 case if_stmt:
3094 return ast_for_if_stmt(c, ch);
3095 case while_stmt:
3096 return ast_for_while_stmt(c, ch);
3097 case for_stmt:
3098 return ast_for_for_stmt(c, ch);
3099 case try_stmt:
3100 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003101 case with_stmt:
3102 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103 case funcdef:
3104 return ast_for_funcdef(c, ch);
3105 case classdef:
3106 return ast_for_classdef(c, ch);
3107 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003108 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3110 TYPE(n), NCH(n));
3111 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003112 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 }
3114}
3115
3116static PyObject *
3117parsenumber(const char *s)
3118{
Thomas Wouters89f507f2006-12-13 04:49:30 +00003119 const char *end;
3120 long x;
3121 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122#ifndef WITHOUT_COMPLEX
Thomas Wouters89f507f2006-12-13 04:49:30 +00003123 Py_complex c;
3124 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125#endif
3126
Thomas Wouters89f507f2006-12-13 04:49:30 +00003127 errno = 0;
3128 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129#ifndef WITHOUT_COMPLEX
Thomas Wouters89f507f2006-12-13 04:49:30 +00003130 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00003132 if (*end == 'l' || *end == 'L')
3133 return PyLong_FromString((char *)s, (char **)0, 0);
3134 if (s[0] == '0') {
3135 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3136 if (x < 0 && errno == 0) {
3137 return PyLong_FromString((char *)s,
3138 (char **)0,
3139 0);
3140 }
3141 }
3142 else
3143 x = PyOS_strtol((char *)s, (char **)&end, 0);
3144 if (*end == '\0') {
3145 if (errno != 0)
3146 return PyLong_FromString((char *)s, (char **)0, 0);
3147 return PyInt_FromLong(x);
3148 }
3149 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150#ifndef WITHOUT_COMPLEX
Thomas Wouters89f507f2006-12-13 04:49:30 +00003151 if (imflag) {
3152 c.real = 0.;
3153 PyFPE_START_PROTECT("atof", return 0)
3154 c.imag = PyOS_ascii_atof(s);
3155 PyFPE_END_PROTECT(c)
3156 return PyComplex_FromCComplex(c);
3157 }
3158 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00003160 {
3161 PyFPE_START_PROTECT("atof", return 0)
3162 dx = PyOS_ascii_atof(s);
3163 PyFPE_END_PROTECT(dx)
3164 return PyFloat_FromDouble(dx);
3165 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166}
3167
3168static PyObject *
3169decode_utf8(const char **sPtr, const char *end, char* encoding)
3170{
3171#ifndef Py_USING_UNICODE
Thomas Wouters89f507f2006-12-13 04:49:30 +00003172 Py_FatalError("decode_utf8 should not be called in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173 return NULL;
3174#else
Thomas Wouters89f507f2006-12-13 04:49:30 +00003175 PyObject *u, *v;
3176 char *s, *t;
3177 t = s = (char *)*sPtr;
3178 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3179 while (s < end && (*s & 0x80)) s++;
3180 *sPtr = s;
3181 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3182 if (u == NULL)
3183 return NULL;
3184 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3185 Py_DECREF(u);
3186 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187#endif
3188}
3189
3190static PyObject *
3191decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3192{
Thomas Wouters89f507f2006-12-13 04:49:30 +00003193 PyObject *v, *u;
3194 char *buf;
3195 char *p;
3196 const char *end;
3197 if (encoding == NULL) {
3198 buf = (char *)s;
3199 u = NULL;
3200 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3201 buf = (char *)s;
3202 u = NULL;
3203 } else {
3204 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3205 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3206 if (u == NULL)
3207 return NULL;
3208 p = buf = PyString_AsString(u);
3209 end = s + len;
3210 while (s < end) {
3211 if (*s == '\\') {
3212 *p++ = *s++;
3213 if (*s & 0x80) {
3214 strcpy(p, "u005c");
3215 p += 5;
3216 }
3217 }
3218 if (*s & 0x80) { /* XXX inefficient */
3219 PyObject *w;
3220 char *r;
3221 Py_ssize_t rn, i;
3222 w = decode_utf8(&s, end, "utf-16-be");
3223 if (w == NULL) {
3224 Py_DECREF(u);
3225 return NULL;
3226 }
3227 r = PyString_AsString(w);
3228 rn = PyString_Size(w);
3229 assert(rn % 2 == 0);
3230 for (i = 0; i < rn; i += 2) {
3231 sprintf(p, "\\u%02x%02x",
3232 r[i + 0] & 0xFF,
3233 r[i + 1] & 0xFF);
3234 p += 6;
3235 }
3236 Py_DECREF(w);
3237 } else {
3238 *p++ = *s++;
3239 }
3240 }
3241 len = p - buf;
3242 s = buf;
3243 }
3244 if (rawmode)
3245 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3246 else
3247 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3248 Py_XDECREF(u);
3249 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003250}
3251
3252/* s is a Python string literal, including the bracketing quote characters,
3253 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3254 * parsestr parses it, and returns the decoded Python string object.
3255 */
3256static PyObject *
3257parsestr(const char *s, const char *encoding)
3258{
Thomas Wouters89f507f2006-12-13 04:49:30 +00003259 size_t len;
3260 int quote = Py_CHARMASK(*s);
3261 int rawmode = 0;
3262 int need_encoding;
3263 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264
Thomas Wouters89f507f2006-12-13 04:49:30 +00003265 if (isalpha(quote) || quote == '_') {
3266 if (quote == 'u' || quote == 'U') {
3267 quote = *++s;
3268 unicode = 1;
3269 }
3270 if (quote == 'r' || quote == 'R') {
3271 quote = *++s;
3272 rawmode = 1;
3273 }
3274 }
3275 if (quote != '\'' && quote != '\"') {
3276 PyErr_BadInternalCall();
3277 return NULL;
3278 }
3279 s++;
3280 len = strlen(s);
3281 if (len > INT_MAX) {
3282 PyErr_SetString(PyExc_OverflowError,
3283 "string to parse is too long");
3284 return NULL;
3285 }
3286 if (s[--len] != quote) {
3287 PyErr_BadInternalCall();
3288 return NULL;
3289 }
3290 if (len >= 4 && s[0] == quote && s[1] == quote) {
3291 s += 2;
3292 len -= 2;
3293 if (s[--len] != quote || s[--len] != quote) {
3294 PyErr_BadInternalCall();
3295 return NULL;
3296 }
3297 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298#ifdef Py_USING_UNICODE
Thomas Wouters89f507f2006-12-13 04:49:30 +00003299 if (unicode || Py_UnicodeFlag) {
3300 return decode_unicode(s, len, rawmode, encoding);
3301 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00003303 need_encoding = (encoding != NULL &&
3304 strcmp(encoding, "utf-8") != 0 &&
3305 strcmp(encoding, "iso-8859-1") != 0);
3306 if (rawmode || strchr(s, '\\') == NULL) {
3307 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308#ifndef Py_USING_UNICODE
Thomas Wouters89f507f2006-12-13 04:49:30 +00003309 /* This should not happen - we never see any other
3310 encoding. */
3311 Py_FatalError(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003312 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313#else
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3315 if (u == NULL)
3316 return NULL;
3317 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3318 Py_DECREF(u);
3319 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 } else {
3322 return PyString_FromStringAndSize(s, len);
3323 }
3324 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325
Thomas Wouters89f507f2006-12-13 04:49:30 +00003326 return PyString_DecodeEscape(s, len, NULL, unicode,
3327 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328}
3329
3330/* Build a Python string object out of a STRING atom. This takes care of
3331 * compile-time literal catenation, calling parsestr() on each piece, and
3332 * pasting the intermediate results together.
3333 */
3334static PyObject *
3335parsestrplus(struct compiling *c, const node *n)
3336{
Thomas Wouters89f507f2006-12-13 04:49:30 +00003337 PyObject *v;
3338 int i;
3339 REQ(CHILD(n, 0), STRING);
3340 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3341 /* String literal concatenation */
3342 for (i = 1; i < NCH(n); i++) {
3343 PyObject *s;
3344 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3345 if (s == NULL)
3346 goto onError;
3347 if (PyString_Check(v) && PyString_Check(s)) {
3348 PyString_ConcatAndDel(&v, s);
3349 if (v == NULL)
3350 goto onError;
3351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352#ifdef Py_USING_UNICODE
Thomas Wouters89f507f2006-12-13 04:49:30 +00003353 else {
3354 PyObject *temp = PyUnicode_Concat(v, s);
3355 Py_DECREF(s);
3356 Py_DECREF(v);
3357 v = temp;
3358 if (v == NULL)
3359 goto onError;
3360 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 }
3363 }
3364 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365
3366 onError:
Thomas Wouters89f507f2006-12-13 04:49:30 +00003367 Py_XDECREF(v);
3368 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369}