blob: f49026807f6fbad360931be6dc8e47ac27407ef1 [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 *);
Martin v. Löwis28457502006-04-11 09:17:27 +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
Neal Norwitzd0421322006-09-05 04:00:12 +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)
Neal Norwitzd0421322006-09-05 04:00:12 +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))
Neal Norwitzd0421322006-09-05 04:00:12 +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)
Neal Norwitzd0421322006-09-05 04:00:12 +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) {
Neal Norwitzd0421322006-09-05 04:00:12 +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) {
Neal Norwitzd0421322006-09-05 04:00:12 +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) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000101 Py_DECREF(errstr);
102 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000103 }
Georg Brandl7784f122006-05-26 20:04:44 +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)
Neal Norwitzd0421322006-09-05 04:00:12 +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";
Neal Norwitze98ccf62006-03-23 05:39:47 +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)
Neal Norwitzd0421322006-09-05 04:00:12 +0000243 goto error;
Jeremy Hylton2f327c12006-04-04 04:00:23 +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)
Neal Norwitzd0421322006-09-05 04:00:12 +0000253 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254 if (num == 1) {
Neal Norwitzd0421322006-09-05 04:00:12 +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:
Martin v. Löwis28457502006-04-11 09:17:27 +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
Neil Schemenauer0e07b602006-07-09 16:16:34 +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:
Neal Norwitzd0421322006-09-05 04:00:12 +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:
Neal Norwitzd0421322006-09-05 04:00:12 +0000351 e->v.Subscript.ctx = ctx;
352 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353 case Name_kind:
Neal Norwitzd0421322006-09-05 04:00:12 +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:
Neal Norwitzd0421322006-09-05 04:00:12 +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 ()");
Neal Norwitzd0421322006-09-05 04:00:12 +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";
Neal Norwitzd0421322006-09-05 04:00:12 +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;
Neal Norwitz0d62a062006-07-30 06:53:31 +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:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000391 case Num_kind:
392 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000393 expr_name = "literal";
394 break;
395 case Compare_kind:
396 expr_name = "comparison";
397 break;
398 case Repr_kind:
399 expr_name = "repr";
400 break;
Neal Norwitz373f0a72006-05-15 07:04:36 +0000401 case IfExp_kind:
402 expr_name = "conditional expression";
403 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000404 default:
405 PyErr_Format(PyExc_SystemError,
406 "unexpected expression in assignment %d (line %d)",
407 e->kind, e->lineno);
408 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000410 /* Check for error string set by switch */
411 if (expr_name) {
412 char buf[300];
413 PyOS_snprintf(buf, sizeof(buf),
414 "can't %s %s",
415 ctx == Store ? "assign to" : "delete",
416 expr_name);
417 return ast_error(n, buf);
418 }
419
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000421 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422 */
423 if (s) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000424 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425
Neal Norwitzd0421322006-09-05 04:00:12 +0000426 for (i = 0; i < asdl_seq_LEN(s); i++) {
427 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
428 return 0;
429 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430 }
431 return 1;
432}
433
434static operator_ty
435ast_for_augassign(const node *n)
436{
437 REQ(n, augassign);
438 n = CHILD(n, 0);
439 switch (STR(n)[0]) {
440 case '+':
441 return Add;
442 case '-':
443 return Sub;
444 case '/':
445 if (STR(n)[1] == '/')
446 return FloorDiv;
447 else
448 return Div;
449 case '%':
450 return Mod;
451 case '<':
452 return LShift;
453 case '>':
454 return RShift;
455 case '&':
456 return BitAnd;
457 case '^':
458 return BitXor;
459 case '|':
460 return BitOr;
461 case '*':
462 if (STR(n)[1] == '*')
463 return Pow;
464 else
465 return Mult;
466 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000467 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000468 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469 }
470}
471
472static cmpop_ty
473ast_for_comp_op(const node *n)
474{
475 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
476 |'is' 'not'
477 */
478 REQ(n, comp_op);
479 if (NCH(n) == 1) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000480 n = CHILD(n, 0);
481 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 case LESS:
483 return Lt;
484 case GREATER:
485 return Gt;
Neal Norwitzd0421322006-09-05 04:00:12 +0000486 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 return Eq;
488 case LESSEQUAL:
489 return LtE;
490 case GREATEREQUAL:
491 return GtE;
492 case NOTEQUAL:
493 return NotEq;
494 case NAME:
495 if (strcmp(STR(n), "in") == 0)
496 return In;
497 if (strcmp(STR(n), "is") == 0)
498 return Is;
499 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000500 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000502 return (cmpop_ty)0;
Neal Norwitzd0421322006-09-05 04:00:12 +0000503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 }
505 else if (NCH(n) == 2) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000506 /* handle "not in" and "is not" */
507 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508 case NAME:
509 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
510 return NotIn;
511 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
512 return IsNot;
513 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000514 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Martin v. Löwis28457502006-04-11 09:17:27 +0000516 return (cmpop_ty)0;
Neal Norwitzd0421322006-09-05 04:00:12 +0000517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 }
Neal Norwitz79792652005-11-14 04:25:03 +0000519 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000521 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522}
523
524static asdl_seq *
525seq_for_testlist(struct compiling *c, const node *n)
526{
527 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000528 asdl_seq *seq;
529 expr_ty expression;
530 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 assert(TYPE(n) == testlist
Neal Norwitzd0421322006-09-05 04:00:12 +0000532 || TYPE(n) == listmaker
533 || TYPE(n) == testlist_gexp
534 || TYPE(n) == testlist_safe
535 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000537 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 if (!seq)
539 return NULL;
540
541 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000542 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
544 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000545 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
548 assert(i / 2 < seq->size);
549 asdl_seq_SET(seq, i / 2, expression);
550 }
551 return seq;
552}
553
554static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000555compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556{
557 int i, len = (NCH(n) + 1) / 2;
558 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000559 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560 if (!args)
561 return NULL;
562
Neal Norwitz3a230172006-09-22 08:18:10 +0000563 /* fpdef: NAME | '(' fplist ')'
564 fplist: fpdef (',' fpdef)* [',']
565 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 for (i = 0; i < len; i++) {
Neal Norwitz3a230172006-09-22 08:18:10 +0000568 const node *fpdef_node = CHILD(n, 2*i);
569 const node *child;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000571set_name:
572 /* fpdef_node is either a NAME or an fplist */
573 child = CHILD(fpdef_node, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 if (TYPE(child) == NAME) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000575 if (!strcmp(STR(child), "None")) {
576 ast_error(child, "assignment to None");
577 return NULL;
578 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000579 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
580 child->n_col_offset, c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +0000581 }
Jeremy Hyltona8293132006-02-28 17:58:27 +0000582 else {
Neal Norwitz3a230172006-09-22 08:18:10 +0000583 assert(TYPE(fpdef_node) == fpdef);
584 /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */
585 child = CHILD(fpdef_node, 1);
586 assert(TYPE(child) == fplist);
587 /* NCH == 1 means we have (x), we need to elide the extra parens */
588 if (NCH(child) == 1) {
589 fpdef_node = CHILD(child, 0);
590 assert(TYPE(fpdef_node) == fpdef);
591 goto set_name;
592 }
593 arg = compiler_complex_args(c, child);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000594 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595 asdl_seq_SET(args, i, arg);
596 }
597
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000598 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000599 if (!set_context(result, Store, n))
600 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 return result;
602}
603
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604
Jeremy Hyltona8293132006-02-28 17:58:27 +0000605/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606
607static arguments_ty
608ast_for_arguments(struct compiling *c, const node *n)
609{
610 /* parameters: '(' [varargslist] ')'
611 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
612 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
613 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000614 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 asdl_seq *args, *defaults;
616 identifier vararg = NULL, kwarg = NULL;
617 node *ch;
618
619 if (TYPE(n) == parameters) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000620 if (NCH(n) == 2) /* () as argument list */
621 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
622 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 }
624 REQ(n, varargslist);
625
626 /* first count the number of normal args & defaults */
627 for (i = 0; i < NCH(n); i++) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000628 ch = CHILD(n, i);
629 if (TYPE(ch) == fpdef)
630 n_args++;
631 if (TYPE(ch) == EQUAL)
632 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000634 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635 if (!args && n_args)
Neal Norwitzd0421322006-09-05 04:00:12 +0000636 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000637 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 if (!defaults && n_defaults)
Neal Norwitzd0421322006-09-05 04:00:12 +0000639 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640
641 /* fpdef: NAME | '(' fplist ')'
642 fplist: fpdef (',' fpdef)* [',']
643 */
644 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000645 j = 0; /* index for defaults */
646 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 while (i < NCH(n)) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000648 ch = CHILD(n, i);
649 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 case fpdef:
Neal Norwitz3a230172006-09-22 08:18:10 +0000651 handle_fpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
653 anything other than EQUAL or a comma? */
654 /* XXX Should NCH(n) check be made a separate check? */
655 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000656 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
657 if (!expression)
658 goto error;
Neal Norwitzc173b482006-07-30 19:18:13 +0000659 assert(defaults != NULL);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000660 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 i += 2;
Neal Norwitzd0421322006-09-05 04:00:12 +0000662 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 }
Neal Norwitzd0421322006-09-05 04:00:12 +0000664 else if (found_default) {
665 ast_error(n,
666 "non-default argument follows default argument");
667 goto error;
668 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 if (NCH(ch) == 3) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000670 ch = CHILD(ch, 1);
671 /* def foo((x)): is not complex, special case. */
672 if (NCH(ch) != 1) {
673 /* We have complex arguments, setup for unpacking. */
674 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
675 } else {
676 /* def foo((x)): setup for checking NAME below. */
Neal Norwitz3a230172006-09-22 08:18:10 +0000677 /* Loop because there can be many parens and tuple
678 unpacking mixed in. */
Neal Norwitzd0421322006-09-05 04:00:12 +0000679 ch = CHILD(ch, 0);
Neal Norwitz3a230172006-09-22 08:18:10 +0000680 assert(TYPE(ch) == fpdef);
681 goto handle_fpdef;
Neal Norwitzd0421322006-09-05 04:00:12 +0000682 }
Neal Norwitz33b730e2006-03-27 08:58:23 +0000683 }
684 if (TYPE(CHILD(ch, 0)) == NAME) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000685 expr_ty name;
686 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
687 ast_error(CHILD(ch, 0), "assignment to None");
688 goto error;
689 }
Armin Rigo31441302005-10-21 12:57:31 +0000690 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000691 Param, LINENO(ch), ch->n_col_offset,
692 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 if (!name)
694 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000695 asdl_seq_SET(args, k++, name);
Neal Norwitzd0421322006-09-05 04:00:12 +0000696
697 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698 i += 2; /* the name and the comma */
699 break;
700 case STAR:
Neal Norwitzd0421322006-09-05 04:00:12 +0000701 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
702 ast_error(CHILD(n, i+1), "assignment to None");
703 goto error;
704 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
706 i += 3;
707 break;
708 case DOUBLESTAR:
Neal Norwitzd0421322006-09-05 04:00:12 +0000709 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
710 ast_error(CHILD(n, i+1), "assignment to None");
711 goto error;
712 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
714 i += 3;
715 break;
716 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000717 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 "unexpected node in varargslist: %d @ %d",
719 TYPE(ch), i);
720 goto error;
Neal Norwitzd0421322006-09-05 04:00:12 +0000721 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 }
723
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000724 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725
726 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000727 Py_XDECREF(vararg);
728 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 return NULL;
730}
731
732static expr_ty
733ast_for_dotted_name(struct compiling *c, const node *n)
734{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000735 expr_ty e;
736 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000737 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 int i;
739
740 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000741
742 lineno = LINENO(n);
743 col_offset = n->n_col_offset;
744
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 id = NEW_IDENTIFIER(CHILD(n, 0));
746 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000747 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000748 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 if (!e)
Neal Norwitzd0421322006-09-05 04:00:12 +0000750 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751
752 for (i = 2; i < NCH(n); i+=2) {
753 id = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitzd0421322006-09-05 04:00:12 +0000754 if (!id)
755 return NULL;
756 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
757 if (!e)
758 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 }
760
761 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762}
763
764static expr_ty
765ast_for_decorator(struct compiling *c, const node *n)
766{
767 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
768 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000769 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770
771 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000772 REQ(CHILD(n, 0), AT);
773 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774
775 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
776 if (!name_expr)
Neal Norwitzd0421322006-09-05 04:00:12 +0000777 return NULL;
778
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 if (NCH(n) == 3) { /* No arguments */
Neal Norwitzd0421322006-09-05 04:00:12 +0000780 d = name_expr;
781 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 }
783 else if (NCH(n) == 5) { /* Call with no arguments */
Neal Norwitzd0421322006-09-05 04:00:12 +0000784 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000785 n->n_col_offset, c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +0000786 if (!d)
787 return NULL;
788 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 }
790 else {
Neal Norwitzd0421322006-09-05 04:00:12 +0000791 d = ast_for_call(c, CHILD(n, 3), name_expr);
792 if (!d)
793 return NULL;
794 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 }
796
797 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798}
799
800static asdl_seq*
801ast_for_decorators(struct compiling *c, const node *n)
802{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000803 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000804 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 int i;
806
807 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000808 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 if (!decorator_seq)
810 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +0000811
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000813 d = ast_for_decorator(c, CHILD(n, i));
Neal Norwitzd0421322006-09-05 04:00:12 +0000814 if (!d)
815 return NULL;
816 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 }
818 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819}
820
821static stmt_ty
822ast_for_funcdef(struct compiling *c, const node *n)
823{
824 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000825 identifier name;
826 arguments_ty args;
827 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 asdl_seq *decorator_seq = NULL;
829 int name_i;
830
831 REQ(n, funcdef);
832
833 if (NCH(n) == 6) { /* decorators are present */
Neal Norwitzd0421322006-09-05 04:00:12 +0000834 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
835 if (!decorator_seq)
836 return NULL;
837 name_i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 }
839 else {
Neal Norwitzd0421322006-09-05 04:00:12 +0000840 name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 }
842
843 name = NEW_IDENTIFIER(CHILD(n, name_i));
844 if (!name)
Neal Norwitzd0421322006-09-05 04:00:12 +0000845 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000847 ast_error(CHILD(n, name_i), "assignment to None");
848 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 }
850 args = ast_for_arguments(c, CHILD(n, name_i + 1));
851 if (!args)
Neal Norwitzd0421322006-09-05 04:00:12 +0000852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 body = ast_for_suite(c, CHILD(n, name_i + 3));
854 if (!body)
Neal Norwitzd0421322006-09-05 04:00:12 +0000855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000857 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
858 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859}
860
861static expr_ty
862ast_for_lambdef(struct compiling *c, const node *n)
863{
864 /* lambdef: 'lambda' [varargslist] ':' test */
865 arguments_ty args;
866 expr_ty expression;
867
868 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000869 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 if (!args)
871 return NULL;
872 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000873 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 }
876 else {
877 args = ast_for_arguments(c, CHILD(n, 1));
878 if (!args)
879 return NULL;
880 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000881 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883 }
884
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000885 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886}
887
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000888static expr_ty
889ast_for_ifexpr(struct compiling *c, const node *n)
890{
891 /* test: or_test 'if' or_test 'else' test */
892 expr_ty expression, body, orelse;
893
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000894 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000895 body = ast_for_expr(c, CHILD(n, 0));
896 if (!body)
Neal Norwitzd0421322006-09-05 04:00:12 +0000897 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000898 expression = ast_for_expr(c, CHILD(n, 2));
899 if (!expression)
Neal Norwitzd0421322006-09-05 04:00:12 +0000900 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000901 orelse = ast_for_expr(c, CHILD(n, 4));
902 if (!orelse)
Neal Norwitzd0421322006-09-05 04:00:12 +0000903 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000904 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
905 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000906}
907
Neal Norwitze4d4f002006-09-05 03:58:26 +0000908/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
909 so there is only a single version. Possibly for loops can also re-use
910 the code.
911*/
912
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913/* Count the number of 'for' loop in a list comprehension.
914
915 Helper for ast_for_listcomp().
916*/
917
918static int
919count_list_fors(const node *n)
920{
921 int n_fors = 0;
922 node *ch = CHILD(n, 1);
923
924 count_list_for:
925 n_fors++;
926 REQ(ch, list_for);
927 if (NCH(ch) == 5)
Neal Norwitzd0421322006-09-05 04:00:12 +0000928 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 else
Neal Norwitzd0421322006-09-05 04:00:12 +0000930 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 count_list_iter:
932 REQ(ch, list_iter);
933 ch = CHILD(ch, 0);
934 if (TYPE(ch) == list_for)
Neal Norwitzd0421322006-09-05 04:00:12 +0000935 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 else if (TYPE(ch) == list_if) {
937 if (NCH(ch) == 3) {
938 ch = CHILD(ch, 2);
939 goto count_list_iter;
940 }
941 else
942 return n_fors;
943 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000944
945 /* Should never be reached */
946 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
947 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948}
949
950/* Count the number of 'if' statements in a list comprehension.
951
952 Helper for ast_for_listcomp().
953*/
954
955static int
956count_list_ifs(const node *n)
957{
958 int n_ifs = 0;
959
960 count_list_iter:
961 REQ(n, list_iter);
962 if (TYPE(CHILD(n, 0)) == list_for)
Neal Norwitzd0421322006-09-05 04:00:12 +0000963 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964 n = CHILD(n, 0);
965 REQ(n, list_if);
966 n_ifs++;
967 if (NCH(n) == 2)
Neal Norwitzd0421322006-09-05 04:00:12 +0000968 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969 n = CHILD(n, 2);
970 goto count_list_iter;
971}
972
973static expr_ty
974ast_for_listcomp(struct compiling *c, const node *n)
975{
976 /* listmaker: test ( list_for | (',' test)* [','] )
977 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
978 list_iter: list_for | list_if
979 list_if: 'if' test [list_iter]
980 testlist_safe: test [(',' test)+ [',']]
981 */
982 expr_ty elt;
983 asdl_seq *listcomps;
984 int i, n_fors;
985 node *ch;
986
987 REQ(n, listmaker);
988 assert(NCH(n) > 1);
989
990 elt = ast_for_expr(c, CHILD(n, 0));
991 if (!elt)
992 return NULL;
993
994 n_fors = count_list_fors(n);
995 if (n_fors == -1)
996 return NULL;
997
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000998 listcomps = asdl_seq_new(n_fors, c->c_arena);
999 if (!listcomps)
Neal Norwitzd0421322006-09-05 04:00:12 +00001000 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001001
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 ch = CHILD(n, 1);
1003 for (i = 0; i < n_fors; i++) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001004 comprehension_ty lc;
1005 asdl_seq *t;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 expr_ty expression;
Neal Norwitzdac090d2006-09-05 03:53:08 +00001007 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008
Neal Norwitzd0421322006-09-05 04:00:12 +00001009 REQ(ch, list_for);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010
Neal Norwitzdac090d2006-09-05 03:53:08 +00001011 for_ch = CHILD(ch, 1);
1012 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001013 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001015 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001016 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018
Neal Norwitzdac090d2006-09-05 03:53:08 +00001019 /* Check the # of children rather than the length of t, since
1020 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
Neal Norwitzd0421322006-09-05 04:00:12 +00001021 if (NCH(for_ch) == 1)
1022 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001023 c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +00001024 else
1025 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001026 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001027 expression, NULL, c->c_arena);
1028 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030
Neal Norwitzd0421322006-09-05 04:00:12 +00001031 if (NCH(ch) == 5) {
1032 int j, n_ifs;
1033 asdl_seq *ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034
Neal Norwitzd0421322006-09-05 04:00:12 +00001035 ch = CHILD(ch, 4);
1036 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001037 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039
Neal Norwitzd0421322006-09-05 04:00:12 +00001040 ifs = asdl_seq_new(n_ifs, c->c_arena);
1041 if (!ifs)
1042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043
Neal Norwitzd0421322006-09-05 04:00:12 +00001044 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001045 REQ(ch, list_iter);
Neal Norwitzd0421322006-09-05 04:00:12 +00001046 ch = CHILD(ch, 0);
1047 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048
Neal Norwitzd0421322006-09-05 04:00:12 +00001049 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1050 if (NCH(ch) == 3)
1051 ch = CHILD(ch, 2);
1052 }
1053 /* on exit, must guarantee that ch is a list_for */
1054 if (TYPE(ch) == list_iter)
1055 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056 lc->ifs = ifs;
Neal Norwitzd0421322006-09-05 04:00:12 +00001057 }
1058 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 }
1060
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001061 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062}
1063
1064/*
1065 Count the number of 'for' loops in a generator expression.
1066
1067 Helper for ast_for_genexp().
1068*/
1069
1070static int
1071count_gen_fors(const node *n)
1072{
Neal Norwitzd0421322006-09-05 04:00:12 +00001073 int n_fors = 0;
1074 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075
1076 count_gen_for:
Neal Norwitzd0421322006-09-05 04:00:12 +00001077 n_fors++;
1078 REQ(ch, gen_for);
1079 if (NCH(ch) == 5)
1080 ch = CHILD(ch, 4);
1081 else
1082 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 count_gen_iter:
Neal Norwitzd0421322006-09-05 04:00:12 +00001084 REQ(ch, gen_iter);
1085 ch = CHILD(ch, 0);
1086 if (TYPE(ch) == gen_for)
1087 goto count_gen_for;
1088 else if (TYPE(ch) == gen_if) {
1089 if (NCH(ch) == 3) {
1090 ch = CHILD(ch, 2);
1091 goto count_gen_iter;
1092 }
1093 else
1094 return n_fors;
1095 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001096
Neal Norwitzd0421322006-09-05 04:00:12 +00001097 /* Should never be reached */
1098 PyErr_SetString(PyExc_SystemError,
1099 "logic error in count_gen_fors");
1100 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101}
1102
1103/* Count the number of 'if' statements in a generator expression.
1104
1105 Helper for ast_for_genexp().
1106*/
1107
1108static int
1109count_gen_ifs(const node *n)
1110{
Neal Norwitzd0421322006-09-05 04:00:12 +00001111 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112
Neal Norwitzd0421322006-09-05 04:00:12 +00001113 while (1) {
1114 REQ(n, gen_iter);
1115 if (TYPE(CHILD(n, 0)) == gen_for)
1116 return n_ifs;
1117 n = CHILD(n, 0);
1118 REQ(n, gen_if);
1119 n_ifs++;
1120 if (NCH(n) == 2)
1121 return n_ifs;
1122 n = CHILD(n, 2);
1123 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124}
1125
Jeremy Hyltona8293132006-02-28 17:58:27 +00001126/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127static expr_ty
1128ast_for_genexp(struct compiling *c, const node *n)
1129{
1130 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Neal Norwitzd0421322006-09-05 04:00:12 +00001131 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 expr_ty elt;
1133 asdl_seq *genexps;
1134 int i, n_fors;
1135 node *ch;
1136
1137 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1138 assert(NCH(n) > 1);
1139
1140 elt = ast_for_expr(c, CHILD(n, 0));
1141 if (!elt)
1142 return NULL;
1143
1144 n_fors = count_gen_fors(n);
1145 if (n_fors == -1)
1146 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001147
1148 genexps = asdl_seq_new(n_fors, c->c_arena);
1149 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 ch = CHILD(n, 1);
1153 for (i = 0; i < n_fors; i++) {
1154 comprehension_ty ge;
1155 asdl_seq *t;
1156 expr_ty expression;
Neal Norwitzdac090d2006-09-05 03:53:08 +00001157 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158
1159 REQ(ch, gen_for);
1160
Neal Norwitzdac090d2006-09-05 03:53:08 +00001161 for_ch = CHILD(ch, 1);
1162 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001163 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001165 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001166 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001168
Neal Norwitzdac090d2006-09-05 03:53:08 +00001169 /* Check the # of children rather than the length of t, since
1170 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1171 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001172 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001173 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001175 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1176 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001177 expression, NULL, c->c_arena);
1178
1179 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001181
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 if (NCH(ch) == 5) {
1183 int j, n_ifs;
1184 asdl_seq *ifs;
1185
1186 ch = CHILD(ch, 4);
1187 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001188 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001190
1191 ifs = asdl_seq_new(n_ifs, c->c_arena);
1192 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 for (j = 0; j < n_ifs; j++) {
1196 REQ(ch, gen_iter);
1197 ch = CHILD(ch, 0);
1198 REQ(ch, gen_if);
1199
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001200 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001201 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001202 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001203 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 if (NCH(ch) == 3)
1205 ch = CHILD(ch, 2);
1206 }
1207 /* on exit, must guarantee that ch is a gen_for */
1208 if (TYPE(ch) == gen_iter)
1209 ch = CHILD(ch, 0);
1210 ge->ifs = ifs;
1211 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001212 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 }
1214
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001215 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216}
1217
1218static expr_ty
1219ast_for_atom(struct compiling *c, const node *n)
1220{
1221 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1222 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1223 */
1224 node *ch = CHILD(n, 0);
1225
1226 switch (TYPE(ch)) {
1227 case NAME:
Neal Norwitzd0421322006-09-05 04:00:12 +00001228 /* All names start in Load context, but may later be
1229 changed. */
1230 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 case STRING: {
Neal Norwitzd0421322006-09-05 04:00:12 +00001232 PyObject *str = parsestrplus(c, n);
1233 if (!str)
1234 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001235
Neal Norwitzd0421322006-09-05 04:00:12 +00001236 PyArena_AddPyObject(c->c_arena, str);
1237 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 }
1239 case NUMBER: {
Neal Norwitzd0421322006-09-05 04:00:12 +00001240 PyObject *pynum = parsenumber(STR(ch));
1241 if (!pynum)
1242 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001243
Neal Norwitzd0421322006-09-05 04:00:12 +00001244 PyArena_AddPyObject(c->c_arena, pynum);
1245 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 }
1247 case LPAR: /* some parenthesized expressions */
Neal Norwitzd0421322006-09-05 04:00:12 +00001248 ch = CHILD(n, 1);
1249
1250 if (TYPE(ch) == RPAR)
1251 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1252
1253 if (TYPE(ch) == yield_expr)
1254 return ast_for_expr(c, ch);
1255
1256 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1257 return ast_for_genexp(c, ch);
1258
1259 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 case LSQB: /* list (or list comprehension) */
Neal Norwitzd0421322006-09-05 04:00:12 +00001261 ch = CHILD(n, 1);
1262
1263 if (TYPE(ch) == RSQB)
1264 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1265
1266 REQ(ch, listmaker);
1267 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1268 asdl_seq *elts = seq_for_testlist(c, ch);
1269 if (!elts)
1270 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001271
Neal Norwitzd0421322006-09-05 04:00:12 +00001272 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1273 }
1274 else
1275 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 case LBRACE: {
Neal Norwitzd0421322006-09-05 04:00:12 +00001277 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1278 int i, size;
1279 asdl_seq *keys, *values;
1280
1281 ch = CHILD(n, 1);
1282 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1283 keys = asdl_seq_new(size, c->c_arena);
1284 if (!keys)
1285 return NULL;
1286
1287 values = asdl_seq_new(size, c->c_arena);
1288 if (!values)
1289 return NULL;
1290
1291 for (i = 0; i < NCH(ch); i += 4) {
1292 expr_ty expression;
1293
1294 expression = ast_for_expr(c, CHILD(ch, i));
1295 if (!expression)
1296 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001297
Neal Norwitzd0421322006-09-05 04:00:12 +00001298 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001299
Neal Norwitzd0421322006-09-05 04:00:12 +00001300 expression = ast_for_expr(c, CHILD(ch, i + 2));
1301 if (!expression)
1302 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001303
Neal Norwitzd0421322006-09-05 04:00:12 +00001304 asdl_seq_SET(values, i / 4, expression);
1305 }
1306 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 }
1308 case BACKQUOTE: { /* repr */
Neal Norwitzd0421322006-09-05 04:00:12 +00001309 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
1310 if (!expression)
1311 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001312
Neal Norwitzd0421322006-09-05 04:00:12 +00001313 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 }
1315 default:
Neal Norwitzd0421322006-09-05 04:00:12 +00001316 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1317 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 }
1319}
1320
1321static slice_ty
1322ast_for_slice(struct compiling *c, const node *n)
1323{
1324 node *ch;
1325 expr_ty lower = NULL, upper = NULL, step = NULL;
1326
1327 REQ(n, subscript);
1328
1329 /*
1330 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1331 sliceop: ':' [test]
1332 */
1333 ch = CHILD(n, 0);
1334 if (TYPE(ch) == DOT)
Neal Norwitzd0421322006-09-05 04:00:12 +00001335 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336
1337 if (NCH(n) == 1 && TYPE(ch) == test) {
1338 /* 'step' variable hold no significance in terms of being used over
1339 other vars */
1340 step = ast_for_expr(c, ch);
1341 if (!step)
1342 return NULL;
1343
Neal Norwitzd0421322006-09-05 04:00:12 +00001344 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 }
1346
1347 if (TYPE(ch) == test) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001348 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 if (!lower)
1350 return NULL;
1351 }
1352
1353 /* If there's an upper bound it's in the second or third position. */
1354 if (TYPE(ch) == COLON) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001355 if (NCH(n) > 1) {
1356 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357
Neal Norwitzd0421322006-09-05 04:00:12 +00001358 if (TYPE(n2) == test) {
1359 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 if (!upper)
1361 return NULL;
1362 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 } else if (NCH(n) > 2) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001365 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366
Neal Norwitzd0421322006-09-05 04:00:12 +00001367 if (TYPE(n2) == test) {
1368 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 if (!upper)
1370 return NULL;
1371 }
1372 }
1373
1374 ch = CHILD(n, NCH(n) - 1);
1375 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001376 if (NCH(ch) == 1) {
1377 /* No expression, so step is None */
1378 ch = CHILD(ch, 0);
1379 step = Name(new_identifier("None", c->c_arena), Load,
1380 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 if (!step)
1382 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001383 } else {
1384 ch = CHILD(ch, 1);
1385 if (TYPE(ch) == test) {
1386 step = ast_for_expr(c, ch);
1387 if (!step)
1388 return NULL;
1389 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 }
1391 }
1392
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001393 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394}
1395
1396static expr_ty
1397ast_for_binop(struct compiling *c, const node *n)
1398{
Neal Norwitzd0421322006-09-05 04:00:12 +00001399 /* Must account for a sequence of expressions.
1400 How should A op B op C by represented?
1401 BinOp(BinOp(A, op, B), op, C).
1402 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403
Neal Norwitzd0421322006-09-05 04:00:12 +00001404 int i, nops;
1405 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001406 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407
1408 expr1 = ast_for_expr(c, CHILD(n, 0));
1409 if (!expr1)
1410 return NULL;
1411
1412 expr2 = ast_for_expr(c, CHILD(n, 2));
1413 if (!expr2)
1414 return NULL;
1415
Anthony Baxtera863d332006-04-11 07:43:46 +00001416 newoperator = get_operator(CHILD(n, 1));
1417 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 return NULL;
1419
Neal Norwitzd0421322006-09-05 04:00:12 +00001420 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001421 c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +00001422 if (!result)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 return NULL;
1424
Neal Norwitzd0421322006-09-05 04:00:12 +00001425 nops = (NCH(n) - 1) / 2;
1426 for (i = 1; i < nops; i++) {
1427 expr_ty tmp_result, tmp;
1428 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429
Neal Norwitzd0421322006-09-05 04:00:12 +00001430 newoperator = get_operator(next_oper);
Anthony Baxtera863d332006-04-11 07:43:46 +00001431 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 return NULL;
1433
1434 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1435 if (!tmp)
1436 return NULL;
1437
Anthony Baxtera863d332006-04-11 07:43:46 +00001438 tmp_result = BinOp(result, newoperator, tmp,
Neal Norwitzd0421322006-09-05 04:00:12 +00001439 LINENO(next_oper), next_oper->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001440 c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +00001441 if (!tmp)
1442 return NULL;
1443 result = tmp_result;
1444 }
1445 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446}
1447
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001448static expr_ty
1449ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1450{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001451 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1452 subscriptlist: subscript (',' subscript)* [',']
1453 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1454 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001455 REQ(n, trailer);
1456 if (TYPE(CHILD(n, 0)) == LPAR) {
1457 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001458 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1459 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001460 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001461 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001462 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001463 else if (TYPE(CHILD(n, 0)) == DOT ) {
1464 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001465 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001466 }
1467 else {
1468 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001469 REQ(CHILD(n, 2), RSQB);
1470 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001471 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001472 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1473 if (!slc)
1474 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001475 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1476 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001477 }
1478 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001479 /* The grammar is ambiguous here. The ambiguity is resolved
1480 by treating the sequence as a tuple literal if there are
1481 no slice features.
1482 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001483 int j;
1484 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001485 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001486 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001487 asdl_seq *slices, *elts;
1488 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001489 if (!slices)
1490 return NULL;
1491 for (j = 0; j < NCH(n); j += 2) {
1492 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001493 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001494 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001495 if (slc->kind != Index_kind)
1496 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001497 asdl_seq_SET(slices, j / 2, slc);
1498 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001499 if (!simple) {
1500 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001501 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001502 }
1503 /* extract Index values and put them in a Tuple */
1504 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001505 if (!elts)
1506 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001507 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1508 slc = (slice_ty)asdl_seq_GET(slices, j);
1509 assert(slc->kind == Index_kind && slc->v.Index.value);
1510 asdl_seq_SET(elts, j, slc->v.Index.value);
1511 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001512 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001513 if (!e)
1514 return NULL;
1515 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001516 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001517 }
1518 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001519}
1520
1521static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001522ast_for_factor(struct compiling *c, const node *n)
1523{
1524 node *pfactor, *ppower, *patom, *pnum;
1525 expr_ty expression;
1526
1527 /* If the unary - operator is applied to a constant, don't generate
1528 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1529 constant. The peephole optimizer already does something like
1530 this but it doesn't handle the case where the constant is
1531 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1532 PyLongObject.
1533 */
1534 if (TYPE(CHILD(n, 0)) == MINUS
1535 && NCH(n) == 2
1536 && TYPE((pfactor = CHILD(n, 1))) == factor
1537 && NCH(pfactor) == 1
1538 && TYPE((ppower = CHILD(pfactor, 0))) == power
1539 && NCH(ppower) == 1
1540 && TYPE((patom = CHILD(ppower, 0))) == atom
1541 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1542 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1543 if (s == NULL)
1544 return NULL;
1545 s[0] = '-';
1546 strcpy(s + 1, STR(pnum));
1547 PyObject_FREE(STR(pnum));
1548 STR(pnum) = s;
1549 return ast_for_atom(c, patom);
1550 }
1551
1552 expression = ast_for_expr(c, CHILD(n, 1));
1553 if (!expression)
1554 return NULL;
1555
1556 switch (TYPE(CHILD(n, 0))) {
1557 case PLUS:
1558 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1559 c->c_arena);
1560 case MINUS:
1561 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1562 c->c_arena);
1563 case TILDE:
1564 return UnaryOp(Invert, expression, LINENO(n),
1565 n->n_col_offset, c->c_arena);
1566 }
1567 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1568 TYPE(CHILD(n, 0)));
1569 return NULL;
1570}
1571
1572static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001573ast_for_power(struct compiling *c, const node *n)
1574{
1575 /* power: atom trailer* ('**' factor)*
1576 */
1577 int i;
1578 expr_ty e, tmp;
1579 REQ(n, power);
1580 e = ast_for_atom(c, CHILD(n, 0));
1581 if (!e)
1582 return NULL;
1583 if (NCH(n) == 1)
1584 return e;
1585 for (i = 1; i < NCH(n); i++) {
1586 node *ch = CHILD(n, i);
1587 if (TYPE(ch) != trailer)
1588 break;
1589 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001590 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001591 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001592 tmp->lineno = e->lineno;
1593 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001594 e = tmp;
1595 }
1596 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1597 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001598 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001599 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001600 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001601 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001602 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001603 e = tmp;
1604 }
1605 return e;
1606}
1607
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608/* Do not name a variable 'expr'! Will cause a compile error.
1609*/
1610
1611static expr_ty
1612ast_for_expr(struct compiling *c, const node *n)
1613{
1614 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001615 test: or_test ['if' or_test 'else' test] | lambdef
1616 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 and_test: not_test ('and' not_test)*
1618 not_test: 'not' not_test | comparison
1619 comparison: expr (comp_op expr)*
1620 expr: xor_expr ('|' xor_expr)*
1621 xor_expr: and_expr ('^' and_expr)*
1622 and_expr: shift_expr ('&' shift_expr)*
1623 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1624 arith_expr: term (('+'|'-') term)*
1625 term: factor (('*'|'/'|'%'|'//') factor)*
1626 factor: ('+'|'-'|'~') factor | power
1627 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001628
1629 As well as modified versions that exist for backward compatibility,
1630 to explicitly allow:
1631 [ x for x in lambda: 0, lambda: 1 ]
1632 (which would be ambiguous without these extra rules)
1633
1634 old_test: or_test | old_lambdef
1635 old_lambdef: 'lambda' [vararglist] ':' old_test
1636
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637 */
1638
1639 asdl_seq *seq;
1640 int i;
1641
1642 loop:
1643 switch (TYPE(n)) {
1644 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001645 case old_test:
1646 if (TYPE(CHILD(n, 0)) == lambdef ||
1647 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001649 else if (NCH(n) > 1)
1650 return ast_for_ifexpr(c, n);
Neal Norwitzd0421322006-09-05 04:00:12 +00001651 /* Fallthrough */
1652 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 case and_test:
1654 if (NCH(n) == 1) {
1655 n = CHILD(n, 0);
1656 goto loop;
1657 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001658 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 if (!seq)
1660 return NULL;
1661 for (i = 0; i < NCH(n); i += 2) {
1662 expr_ty e = ast_for_expr(c, CHILD(n, i));
1663 if (!e)
1664 return NULL;
1665 asdl_seq_SET(seq, i / 2, e);
1666 }
1667 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001668 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1669 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001670 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001671 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672 case not_test:
1673 if (NCH(n) == 1) {
1674 n = CHILD(n, 0);
1675 goto loop;
1676 }
1677 else {
1678 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1679 if (!expression)
1680 return NULL;
1681
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001682 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1683 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 }
1685 case comparison:
1686 if (NCH(n) == 1) {
1687 n = CHILD(n, 0);
1688 goto loop;
1689 }
1690 else {
1691 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001692 asdl_int_seq *ops;
Neal Norwitzd0421322006-09-05 04:00:12 +00001693 asdl_seq *cmps;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001694 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 if (!ops)
1696 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001697 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 return NULL;
1700 }
1701 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001702 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703
Anthony Baxtera863d332006-04-11 07:43:46 +00001704 newoperator = ast_for_comp_op(CHILD(n, i));
1705 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001707 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708
1709 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001710 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001712 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001714 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 asdl_seq_SET(cmps, i / 2, expression);
1716 }
1717 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001718 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001720 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001722 return Compare(expression, ops, cmps, LINENO(n),
1723 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 }
1725 break;
1726
1727 /* The next five cases all handle BinOps. The main body of code
1728 is the same in each case, but the switch turned inside out to
1729 reuse the code for each type of operator.
1730 */
1731 case expr:
1732 case xor_expr:
1733 case and_expr:
1734 case shift_expr:
1735 case arith_expr:
1736 case term:
1737 if (NCH(n) == 1) {
1738 n = CHILD(n, 0);
1739 goto loop;
1740 }
1741 return ast_for_binop(c, n);
1742 case yield_expr: {
Neal Norwitzd0421322006-09-05 04:00:12 +00001743 expr_ty exp = NULL;
1744 if (NCH(n) == 2) {
1745 exp = ast_for_testlist(c, CHILD(n, 1));
1746 if (!exp)
1747 return NULL;
1748 }
1749 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1750 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001751 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752 if (NCH(n) == 1) {
1753 n = CHILD(n, 0);
1754 goto loop;
1755 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001756 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001757 case power:
1758 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001760 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 return NULL;
1762 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001763 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764 return NULL;
1765}
1766
1767static expr_ty
1768ast_for_call(struct compiling *c, const node *n, expr_ty func)
1769{
1770 /*
1771 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1772 | '**' test)
Neal Norwitzd0421322006-09-05 04:00:12 +00001773 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 */
1775
1776 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001777 asdl_seq *args;
1778 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779 expr_ty vararg = NULL, kwarg = NULL;
1780
1781 REQ(n, arglist);
1782
1783 nargs = 0;
1784 nkeywords = 0;
1785 ngens = 0;
1786 for (i = 0; i < NCH(n); i++) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001787 node *ch = CHILD(n, i);
1788 if (TYPE(ch) == argument) {
1789 if (NCH(ch) == 1)
1790 nargs++;
1791 else if (TYPE(CHILD(ch, 1)) == gen_for)
1792 ngens++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 else
Neal Norwitzd0421322006-09-05 04:00:12 +00001794 nkeywords++;
1795 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 }
1797 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001798 ast_error(n, "Generator expression must be parenthesized "
Neal Norwitzd0421322006-09-05 04:00:12 +00001799 "if not sole argument");
1800 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801 }
1802
1803 if (nargs + nkeywords + ngens > 255) {
1804 ast_error(n, "more than 255 arguments");
1805 return NULL;
1806 }
1807
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001808 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001810 return NULL;
1811 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 nargs = 0;
1815 nkeywords = 0;
1816 for (i = 0; i < NCH(n); i++) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001817 node *ch = CHILD(n, i);
1818 if (TYPE(ch) == argument) {
1819 expr_ty e;
1820 if (NCH(ch) == 1) {
1821 if (nkeywords) {
1822 ast_error(CHILD(ch, 0),
1823 "non-keyword arg after keyword arg");
1824 return NULL;
1825 }
1826 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001828 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001829 asdl_seq_SET(args, nargs++, e);
1830 }
1831 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1832 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001834 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001835 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001837 else {
1838 keyword_ty kw;
1839 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840
Neal Norwitzd0421322006-09-05 04:00:12 +00001841 /* CHILD(ch, 0) is test, but must be an identifier? */
1842 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 /* f(lambda x: x[0] = 3) ends up getting parsed with
1846 * LHS test = lambda x: x[0], and RHS test = 3.
1847 * SF bug 132313 points out that complaining about a keyword
1848 * then is very confusing.
1849 */
1850 if (e->kind == Lambda_kind) {
1851 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 } else if (e->kind != Name_kind) {
1854 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001857 key = e->v.Name.id;
1858 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001861 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001863 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001864 asdl_seq_SET(keywords, nkeywords++, kw);
1865 }
1866 }
1867 else if (TYPE(ch) == STAR) {
1868 vararg = ast_for_expr(c, CHILD(n, i+1));
1869 i++;
1870 }
1871 else if (TYPE(ch) == DOUBLESTAR) {
1872 kwarg = ast_for_expr(c, CHILD(n, i+1));
1873 i++;
1874 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 }
1876
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001877 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878}
1879
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001881ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001883 /* testlist_gexp: test (',' test)* [','] */
1884 /* testlist: test (',' test)* [','] */
1885 /* testlist_safe: test (',' test)+ [','] */
1886 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001888 if (TYPE(n) == testlist_gexp) {
1889 if (NCH(n) > 1)
1890 assert(TYPE(CHILD(n, 1)) != gen_for);
1891 }
1892 else {
1893 assert(TYPE(n) == testlist ||
1894 TYPE(n) == testlist_safe ||
1895 TYPE(n) == testlist1);
1896 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 if (NCH(n) == 1)
Neal Norwitzd0421322006-09-05 04:00:12 +00001898 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 else {
1900 asdl_seq *tmp = seq_for_testlist(c, n);
1901 if (!tmp)
1902 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001903 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001905}
1906
1907static expr_ty
1908ast_for_testlist_gexp(struct compiling *c, const node* n)
1909{
1910 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1911 /* argument: test [ gen_for ] */
1912 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001913 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neal Norwitzd0421322006-09-05 04:00:12 +00001914 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001915 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001916}
1917
1918/* like ast_for_testlist() but returns a sequence */
1919static asdl_seq*
1920ast_for_class_bases(struct compiling *c, const node* n)
1921{
1922 /* testlist: test (',' test)* [','] */
1923 assert(NCH(n) > 0);
1924 REQ(n, testlist);
1925 if (NCH(n) == 1) {
1926 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001927 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001928 if (!bases)
1929 return NULL;
1930 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001931 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001932 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001933 asdl_seq_SET(bases, 0, base);
1934 return bases;
1935 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001936
1937 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938}
1939
1940static stmt_ty
1941ast_for_expr_stmt(struct compiling *c, const node *n)
1942{
1943 REQ(n, expr_stmt);
1944 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1945 | ('=' (yield_expr|testlist))*)
1946 testlist: test (',' test)* [',']
1947 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Neal Norwitzd0421322006-09-05 04:00:12 +00001948 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 test: ... here starts the operator precendence dance
1950 */
1951
1952 if (NCH(n) == 1) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001953 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 if (!e)
1955 return NULL;
1956
Neal Norwitzd0421322006-09-05 04:00:12 +00001957 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 }
1959 else if (TYPE(CHILD(n, 1)) == augassign) {
1960 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00001961 operator_ty newoperator;
Neal Norwitzd0421322006-09-05 04:00:12 +00001962 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963
Neal Norwitzd0421322006-09-05 04:00:12 +00001964 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965 if (!expr1)
1966 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001967 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001968 switch (expr1->kind) {
1969 case GeneratorExp_kind:
1970 ast_error(ch, "augmented assignment to generator "
1971 "expression not possible");
1972 return NULL;
Neal Norwitz0d62a062006-07-30 06:53:31 +00001973 case Yield_kind:
1974 ast_error(ch, "augmented assignment to yield "
1975 "expression not possible");
1976 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001977 case Name_kind: {
1978 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1979 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1980 ast_error(ch, "assignment to None");
1981 return NULL;
1982 }
1983 break;
1984 }
1985 case Attribute_kind:
1986 case Subscript_kind:
1987 break;
1988 default:
1989 ast_error(ch, "illegal expression for augmented "
1990 "assignment");
1991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001993 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994
Neal Norwitzd0421322006-09-05 04:00:12 +00001995 ch = CHILD(n, 2);
1996 if (TYPE(ch) == testlist)
1997 expr2 = ast_for_testlist(c, ch);
1998 else
1999 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002000 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 return NULL;
2002
Anthony Baxtera863d332006-04-11 07:43:46 +00002003 newoperator = ast_for_augassign(CHILD(n, 1));
2004 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 return NULL;
2006
Neal Norwitzd0421322006-09-05 04:00:12 +00002007 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 }
2009 else {
Neal Norwitzd0421322006-09-05 04:00:12 +00002010 int i;
2011 asdl_seq *targets;
2012 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 expr_ty expression;
2014
Neal Norwitzd0421322006-09-05 04:00:12 +00002015 /* a normal assignment */
2016 REQ(CHILD(n, 1), EQUAL);
2017 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2018 if (!targets)
2019 return NULL;
2020 for (i = 0; i < NCH(n) - 2; i += 2) {
2021 expr_ty e;
2022 node *ch = CHILD(n, i);
2023 if (TYPE(ch) == yield_expr) {
2024 ast_error(ch, "assignment to yield expression not possible");
2025 return NULL;
2026 }
2027 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028
Neal Norwitzd0421322006-09-05 04:00:12 +00002029 /* set context to assign */
2030 if (!e)
2031 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032
Neal Norwitzd0421322006-09-05 04:00:12 +00002033 if (!set_context(e, Store, CHILD(n, i)))
2034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035
Neal Norwitzd0421322006-09-05 04:00:12 +00002036 asdl_seq_SET(targets, i / 2, e);
2037 }
2038 value = CHILD(n, NCH(n) - 1);
2039 if (TYPE(value) == testlist)
2040 expression = ast_for_testlist(c, value);
2041 else
2042 expression = ast_for_expr(c, value);
2043 if (!expression)
2044 return NULL;
2045 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047}
2048
2049static stmt_ty
2050ast_for_print_stmt(struct compiling *c, const node *n)
2051{
2052 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2053 | '>>' test [ (',' test)+ [','] ] )
2054 */
2055 expr_ty dest = NULL, expression;
2056 asdl_seq *seq;
2057 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002058 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059
2060 REQ(n, print_stmt);
2061 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002062 dest = ast_for_expr(c, CHILD(n, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 if (!dest)
2064 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002065 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002067 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 if (!seq)
Neal Norwitzd0421322006-09-05 04:00:12 +00002069 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002070 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002072 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002074 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 }
2076 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002077 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078}
2079
2080static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002081ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082{
2083 asdl_seq *seq;
2084 int i;
2085 expr_ty e;
2086
2087 REQ(n, exprlist);
2088
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002089 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 if (!seq)
Neal Norwitzd0421322006-09-05 04:00:12 +00002091 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002093 e = ast_for_expr(c, CHILD(n, i));
2094 if (!e)
2095 return NULL;
2096 asdl_seq_SET(seq, i / 2, e);
2097 if (context && !set_context(e, context, CHILD(n, i)))
2098 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 }
2100 return seq;
2101}
2102
2103static stmt_ty
2104ast_for_del_stmt(struct compiling *c, const node *n)
2105{
2106 asdl_seq *expr_list;
2107
2108 /* del_stmt: 'del' exprlist */
2109 REQ(n, del_stmt);
2110
2111 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2112 if (!expr_list)
2113 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002114 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115}
2116
2117static stmt_ty
2118ast_for_flow_stmt(struct compiling *c, const node *n)
2119{
2120 /*
2121 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2122 | yield_stmt
2123 break_stmt: 'break'
2124 continue_stmt: 'continue'
2125 return_stmt: 'return' [testlist]
2126 yield_stmt: yield_expr
2127 yield_expr: 'yield' testlist
2128 raise_stmt: 'raise' [test [',' test [',' test]]]
2129 */
2130 node *ch;
2131
2132 REQ(n, flow_stmt);
2133 ch = CHILD(n, 0);
2134 switch (TYPE(ch)) {
2135 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002136 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002138 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 case yield_stmt: { /* will reduce to yield_expr */
Neal Norwitzd0421322006-09-05 04:00:12 +00002140 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2141 if (!exp)
2142 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002143 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 }
2145 case return_stmt:
2146 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002147 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002149 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 if (!expression)
2151 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002152 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 }
2154 case raise_stmt:
2155 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002156 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 else if (NCH(ch) == 2) {
2158 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2159 if (!expression)
2160 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002161 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 }
2163 else if (NCH(ch) == 4) {
2164 expr_ty expr1, expr2;
2165
2166 expr1 = ast_for_expr(c, CHILD(ch, 1));
2167 if (!expr1)
2168 return NULL;
2169 expr2 = ast_for_expr(c, CHILD(ch, 3));
2170 if (!expr2)
2171 return NULL;
2172
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002173 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 }
2175 else if (NCH(ch) == 6) {
2176 expr_ty expr1, expr2, expr3;
2177
2178 expr1 = ast_for_expr(c, CHILD(ch, 1));
2179 if (!expr1)
2180 return NULL;
2181 expr2 = ast_for_expr(c, CHILD(ch, 3));
2182 if (!expr2)
2183 return NULL;
2184 expr3 = ast_for_expr(c, CHILD(ch, 5));
2185 if (!expr3)
2186 return NULL;
2187
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002188 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 }
2190 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002191 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 "unexpected flow_stmt: %d", TYPE(ch));
2193 return NULL;
2194 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002195
2196 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2197 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198}
2199
2200static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002201alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202{
2203 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002204 import_as_name: NAME ['as' NAME]
2205 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 dotted_name: NAME ('.' NAME)*
2207 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002208 PyObject *str;
2209
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 loop:
2211 switch (TYPE(n)) {
2212 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002213 str = NULL;
2214 if (NCH(n) == 3) {
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002215 str = NEW_IDENTIFIER(CHILD(n, 2));
2216 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002217 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 case dotted_as_name:
2219 if (NCH(n) == 1) {
2220 n = CHILD(n, 0);
2221 goto loop;
2222 }
2223 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002224 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00002225 if (!a)
2226 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 assert(!a->asname);
2228 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2229 return a;
2230 }
2231 break;
2232 case dotted_name:
2233 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002234 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 else {
2236 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002237 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002238 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 char *s;
2240
2241 len = 0;
2242 for (i = 0; i < NCH(n); i += 2)
2243 /* length of string plus one for the dot */
2244 len += strlen(STR(CHILD(n, i))) + 1;
2245 len--; /* the last name doesn't have a dot */
2246 str = PyString_FromStringAndSize(NULL, len);
2247 if (!str)
2248 return NULL;
2249 s = PyString_AS_STRING(str);
2250 if (!s)
2251 return NULL;
2252 for (i = 0; i < NCH(n); i += 2) {
2253 char *sch = STR(CHILD(n, i));
2254 strcpy(s, STR(CHILD(n, i)));
2255 s += strlen(sch);
2256 *s++ = '.';
2257 }
2258 --s;
2259 *s = '\0';
2260 PyString_InternInPlace(&str);
Neal Norwitzd0421322006-09-05 04:00:12 +00002261 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002262 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 }
2264 break;
2265 case STAR:
Neal Norwitzd0421322006-09-05 04:00:12 +00002266 str = PyString_InternFromString("*");
2267 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002268 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002270 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 "unexpected import name: %d", TYPE(n));
2272 return NULL;
2273 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002274
2275 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 return NULL;
2277}
2278
2279static stmt_ty
2280ast_for_import_stmt(struct compiling *c, const node *n)
2281{
2282 /*
2283 import_stmt: import_name | import_from
2284 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002285 import_from: 'from' ('.'* dotted_name | '.') 'import'
2286 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002288 int lineno;
2289 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 int i;
2291 asdl_seq *aliases;
2292
2293 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002294 lineno = LINENO(n);
2295 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002297 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 n = CHILD(n, 1);
Neal Norwitzd0421322006-09-05 04:00:12 +00002299 REQ(n, dotted_as_names);
2300 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2301 if (!aliases)
2302 return NULL;
2303 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002304 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002305 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002307 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 }
Neal Norwitzd0421322006-09-05 04:00:12 +00002309 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002311 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 int n_children;
Neal Norwitzd0421322006-09-05 04:00:12 +00002313 int idx, ndots = 0;
2314 alias_ty mod = NULL;
2315 identifier modname;
2316
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002317 /* Count the number of dots (for relative imports) and check for the
2318 optional module name */
Neal Norwitzd0421322006-09-05 04:00:12 +00002319 for (idx = 1; idx < NCH(n); idx++) {
2320 if (TYPE(CHILD(n, idx)) == dotted_name) {
2321 mod = alias_for_import_name(c, CHILD(n, idx));
2322 idx++;
2323 break;
2324 } else if (TYPE(CHILD(n, idx)) != DOT) {
2325 break;
2326 }
2327 ndots++;
2328 }
2329 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002330 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002331 case STAR:
2332 /* from ... import * */
Neal Norwitzd0421322006-09-05 04:00:12 +00002333 n = CHILD(n, idx);
2334 n_children = 1;
2335 if (ndots) {
2336 ast_error(n, "'import *' not allowed with 'from .'");
2337 return NULL;
2338 }
2339 break;
2340 case LPAR:
2341 /* from ... import (x, y, z) */
2342 n = CHILD(n, idx + 1);
2343 n_children = NCH(n);
2344 break;
2345 case import_as_names:
2346 /* from ... import x, y, z */
2347 n = CHILD(n, idx);
2348 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00002349 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 ast_error(n, "trailing comma not allowed without"
2351 " surrounding parentheses");
2352 return NULL;
2353 }
Neal Norwitzd0421322006-09-05 04:00:12 +00002354 break;
2355 default:
2356 ast_error(n, "Unexpected node-type in from-import");
2357 return NULL;
2358 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359
Neal Norwitzd0421322006-09-05 04:00:12 +00002360 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2361 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002362 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002363
2364 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002365 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002366 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002367 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002369 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002371 else {
Neal Norwitzd0421322006-09-05 04:00:12 +00002372 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00002373 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2374 if (!import_alias)
2375 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002376 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002377 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002379 if (mod != NULL)
2380 modname = mod->name;
2381 else
2382 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002383 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002384 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 }
Neal Norwitz79792652005-11-14 04:25:03 +00002386 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 "unknown import statement: starts with command '%s'",
2388 STR(CHILD(n, 0)));
2389 return NULL;
2390}
2391
2392static stmt_ty
2393ast_for_global_stmt(struct compiling *c, const node *n)
2394{
2395 /* global_stmt: 'global' NAME (',' NAME)* */
2396 identifier name;
2397 asdl_seq *s;
2398 int i;
2399
2400 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002401 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 if (!s)
Neal Norwitzd0421322006-09-05 04:00:12 +00002403 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 for (i = 1; i < NCH(n); i += 2) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002405 name = NEW_IDENTIFIER(CHILD(n, i));
2406 if (!name)
2407 return NULL;
2408 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002410 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411}
2412
2413static stmt_ty
2414ast_for_exec_stmt(struct compiling *c, const node *n)
2415{
2416 expr_ty expr1, globals = NULL, locals = NULL;
2417 int n_children = NCH(n);
2418 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002419 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 "poorly formed 'exec' statement: %d parts to statement",
2421 n_children);
2422 return NULL;
2423 }
2424
2425 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2426 REQ(n, exec_stmt);
2427 expr1 = ast_for_expr(c, CHILD(n, 1));
2428 if (!expr1)
2429 return NULL;
2430 if (n_children >= 4) {
2431 globals = ast_for_expr(c, CHILD(n, 3));
2432 if (!globals)
2433 return NULL;
2434 }
2435 if (n_children == 6) {
2436 locals = ast_for_expr(c, CHILD(n, 5));
2437 if (!locals)
2438 return NULL;
2439 }
2440
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002441 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442}
2443
2444static stmt_ty
2445ast_for_assert_stmt(struct compiling *c, const node *n)
2446{
2447 /* assert_stmt: 'assert' test [',' test] */
2448 REQ(n, assert_stmt);
2449 if (NCH(n) == 2) {
2450 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2451 if (!expression)
2452 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002453 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 }
2455 else if (NCH(n) == 4) {
2456 expr_ty expr1, expr2;
2457
2458 expr1 = ast_for_expr(c, CHILD(n, 1));
2459 if (!expr1)
2460 return NULL;
2461 expr2 = ast_for_expr(c, CHILD(n, 3));
2462 if (!expr2)
2463 return NULL;
2464
Neal Norwitzd0421322006-09-05 04:00:12 +00002465 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 }
Neal Norwitz79792652005-11-14 04:25:03 +00002467 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 "improper number of parts to 'assert' statement: %d",
2469 NCH(n));
2470 return NULL;
2471}
2472
2473static asdl_seq *
2474ast_for_suite(struct compiling *c, const node *n)
2475{
2476 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002477 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 stmt_ty s;
2479 int i, total, num, end, pos = 0;
2480 node *ch;
2481
2482 REQ(n, suite);
2483
2484 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002485 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 if (!seq)
Neal Norwitzd0421322006-09-05 04:00:12 +00002487 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002489 n = CHILD(n, 0);
2490 /* simple_stmt always ends with a NEWLINE,
2491 and may have a trailing SEMI
2492 */
2493 end = NCH(n) - 1;
2494 if (TYPE(CHILD(n, end - 1)) == SEMI)
2495 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 /* loop by 2 to skip semi-colons */
Neal Norwitzd0421322006-09-05 04:00:12 +00002497 for (i = 0; i < end; i += 2) {
2498 ch = CHILD(n, i);
2499 s = ast_for_stmt(c, ch);
2500 if (!s)
2501 return NULL;
2502 asdl_seq_SET(seq, pos++, s);
2503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 }
2505 else {
Neal Norwitzd0421322006-09-05 04:00:12 +00002506 for (i = 2; i < (NCH(n) - 1); i++) {
2507 ch = CHILD(n, i);
2508 REQ(ch, stmt);
2509 num = num_stmts(ch);
2510 if (num == 1) {
2511 /* small_stmt or compound_stmt with only one child */
2512 s = ast_for_stmt(c, ch);
2513 if (!s)
2514 return NULL;
2515 asdl_seq_SET(seq, pos++, s);
2516 }
2517 else {
2518 int j;
2519 ch = CHILD(ch, 0);
2520 REQ(ch, simple_stmt);
2521 for (j = 0; j < NCH(ch); j += 2) {
2522 /* statement terminates with a semi-colon ';' */
2523 if (NCH(CHILD(ch, j)) == 0) {
2524 assert((j + 1) == NCH(ch));
2525 break;
2526 }
2527 s = ast_for_stmt(c, CHILD(ch, j));
2528 if (!s)
2529 return NULL;
2530 asdl_seq_SET(seq, pos++, s);
2531 }
2532 }
2533 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 }
2535 assert(pos == seq->size);
2536 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537}
2538
2539static stmt_ty
2540ast_for_if_stmt(struct compiling *c, const node *n)
2541{
2542 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2543 ['else' ':' suite]
2544 */
2545 char *s;
2546
2547 REQ(n, if_stmt);
2548
2549 if (NCH(n) == 4) {
2550 expr_ty expression;
2551 asdl_seq *suite_seq;
2552
2553 expression = ast_for_expr(c, CHILD(n, 1));
2554 if (!expression)
2555 return NULL;
2556 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002557 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 return NULL;
2559
Neal Norwitzd0421322006-09-05 04:00:12 +00002560 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002562
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 s = STR(CHILD(n, 4));
2564 /* s[2], the third character in the string, will be
2565 's' for el_s_e, or
2566 'i' for el_i_f
2567 */
2568 if (s[2] == 's') {
2569 expr_ty expression;
2570 asdl_seq *seq1, *seq2;
2571
2572 expression = ast_for_expr(c, CHILD(n, 1));
2573 if (!expression)
2574 return NULL;
2575 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002576 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 return NULL;
2578 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002579 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 return NULL;
2581
Neal Norwitzd0421322006-09-05 04:00:12 +00002582 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 }
2584 else if (s[2] == 'i') {
Neal Norwitzd0421322006-09-05 04:00:12 +00002585 int i, n_elif, has_else = 0;
2586 asdl_seq *orelse = NULL;
2587 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 /* must reference the child n_elif+1 since 'else' token is third,
2589 not fourth, child from the end. */
Neal Norwitzd0421322006-09-05 04:00:12 +00002590 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2591 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2592 has_else = 1;
2593 n_elif -= 3;
2594 }
2595 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596
Neal Norwitzd0421322006-09-05 04:00:12 +00002597 if (has_else) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 expr_ty expression;
2599 asdl_seq *seq1, *seq2;
2600
Neal Norwitzd0421322006-09-05 04:00:12 +00002601 orelse = asdl_seq_new(1, c->c_arena);
2602 if (!orelse)
2603 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002605 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002608 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002611 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613
Neal Norwitzd0421322006-09-05 04:00:12 +00002614 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
2615 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002616 c->c_arena));
Neal Norwitzd0421322006-09-05 04:00:12 +00002617 /* the just-created orelse handled the last elif */
2618 n_elif--;
2619 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620
Neal Norwitzd0421322006-09-05 04:00:12 +00002621 for (i = 0; i < n_elif; i++) {
2622 int off = 5 + (n_elif - i - 1) * 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 expr_ty expression;
2624 asdl_seq *suite_seq;
Neal Norwitzd0421322006-09-05 04:00:12 +00002625 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2626 if (!newobj)
2627 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002629 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002632 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634
Neal Norwitzd0421322006-09-05 04:00:12 +00002635 asdl_seq_SET(newobj, 0,
2636 If(expression, suite_seq, orelse,
2637 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
2638 orelse = newobj;
2639 }
2640 return If(ast_for_expr(c, CHILD(n, 1)),
2641 ast_for_suite(c, CHILD(n, 3)),
2642 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002644
2645 PyErr_Format(PyExc_SystemError,
2646 "unexpected token in 'if' statement: %s", s);
2647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648}
2649
2650static stmt_ty
2651ast_for_while_stmt(struct compiling *c, const node *n)
2652{
2653 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2654 REQ(n, while_stmt);
2655
2656 if (NCH(n) == 4) {
2657 expr_ty expression;
2658 asdl_seq *suite_seq;
2659
2660 expression = ast_for_expr(c, CHILD(n, 1));
2661 if (!expression)
2662 return NULL;
2663 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002664 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002666 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 }
2668 else if (NCH(n) == 7) {
2669 expr_ty expression;
2670 asdl_seq *seq1, *seq2;
2671
2672 expression = ast_for_expr(c, CHILD(n, 1));
2673 if (!expression)
2674 return NULL;
2675 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002676 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 return NULL;
2678 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002679 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680 return NULL;
2681
Neal Norwitzd0421322006-09-05 04:00:12 +00002682 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002684
2685 PyErr_Format(PyExc_SystemError,
2686 "wrong number of tokens for 'while' statement: %d",
2687 NCH(n));
2688 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689}
2690
2691static stmt_ty
2692ast_for_for_stmt(struct compiling *c, const node *n)
2693{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002694 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 expr_ty expression;
2696 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002697 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2699 REQ(n, for_stmt);
2700
2701 if (NCH(n) == 9) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002702 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 if (!seq)
2704 return NULL;
2705 }
2706
Neal Norwitzedef2be2006-07-12 05:26:17 +00002707 node_target = CHILD(n, 1);
2708 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002709 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002711 /* Check the # of children rather than the length of _target, since
2712 for x, in ... has 1 element in _target, but still requires a Tuple. */
2713 if (NCH(node_target) == 1)
Neal Norwitzd0421322006-09-05 04:00:12 +00002714 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 else
Neal Norwitzd0421322006-09-05 04:00:12 +00002716 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002718 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002719 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 return NULL;
2721 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002722 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 return NULL;
2724
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002725 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2726 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727}
2728
2729static excepthandler_ty
2730ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2731{
2732 /* except_clause: 'except' [test [',' test]] */
2733 REQ(exc, except_clause);
2734 REQ(body, suite);
2735
2736 if (NCH(exc) == 1) {
2737 asdl_seq *suite_seq = ast_for_suite(c, body);
2738 if (!suite_seq)
2739 return NULL;
2740
Neal Norwitzd0421322006-09-05 04:00:12 +00002741 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002742 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 }
2744 else if (NCH(exc) == 2) {
2745 expr_ty expression;
2746 asdl_seq *suite_seq;
2747
2748 expression = ast_for_expr(c, CHILD(exc, 1));
2749 if (!expression)
2750 return NULL;
2751 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002752 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 return NULL;
2754
Neal Norwitzd0421322006-09-05 04:00:12 +00002755 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002756 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 }
2758 else if (NCH(exc) == 4) {
2759 asdl_seq *suite_seq;
2760 expr_ty expression;
Neal Norwitzd0421322006-09-05 04:00:12 +00002761 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2762 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002764 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765 return NULL;
2766 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002767 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 return NULL;
2769 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002770 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771 return NULL;
2772
Neal Norwitzd0421322006-09-05 04:00:12 +00002773 return excepthandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002774 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002776
2777 PyErr_Format(PyExc_SystemError,
2778 "wrong number of children for 'except' clause: %d",
2779 NCH(exc));
2780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781}
2782
2783static stmt_ty
2784ast_for_try_stmt(struct compiling *c, const node *n)
2785{
Neal Norwitzf599f422005-12-17 21:33:47 +00002786 const int nch = NCH(n);
2787 int n_except = (nch - 3)/3;
2788 asdl_seq *body, *orelse = NULL, *finally = NULL;
2789
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 REQ(n, try_stmt);
2791
Neal Norwitzf599f422005-12-17 21:33:47 +00002792 body = ast_for_suite(c, CHILD(n, 2));
2793 if (body == NULL)
2794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795
Neal Norwitzf599f422005-12-17 21:33:47 +00002796 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2797 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2798 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2799 /* we can assume it's an "else",
2800 because nch >= 9 for try-else-finally and
2801 it would otherwise have a type of except_clause */
2802 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2803 if (orelse == NULL)
2804 return NULL;
2805 n_except--;
2806 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807
Neal Norwitzf599f422005-12-17 21:33:47 +00002808 finally = ast_for_suite(c, CHILD(n, nch - 1));
2809 if (finally == NULL)
2810 return NULL;
2811 n_except--;
2812 }
2813 else {
2814 /* we can assume it's an "else",
2815 otherwise it would have a type of except_clause */
2816 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2817 if (orelse == NULL)
2818 return NULL;
2819 n_except--;
2820 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002822 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002823 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 return NULL;
2825 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002826
2827 if (n_except > 0) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002828 int i;
2829 stmt_ty except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002830 /* process except statements to create a try ... except */
2831 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2832 if (handlers == NULL)
2833 return NULL;
2834
2835 for (i = 0; i < n_except; i++) {
2836 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2837 CHILD(n, 5 + i * 3));
2838 if (!e)
2839 return NULL;
2840 asdl_seq_SET(handlers, i, e);
2841 }
2842
Neal Norwitzd0421322006-09-05 04:00:12 +00002843 except_st = TryExcept(body, handlers, orelse, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002844 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002845 if (!finally)
Neal Norwitzd0421322006-09-05 04:00:12 +00002846 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002847
2848 /* if a 'finally' is present too, we nest the TryExcept within a
2849 TryFinally to emulate try ... except ... finally */
Neal Norwitzd0421322006-09-05 04:00:12 +00002850 body = asdl_seq_new(1, c->c_arena);
2851 if (body == NULL)
2852 return NULL;
2853 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002854 }
2855
2856 /* must be a try ... finally (except clauses are in body, if any exist) */
2857 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002858 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859}
2860
Guido van Rossumc2e20742006-02-27 22:32:47 +00002861static expr_ty
2862ast_for_with_var(struct compiling *c, const node *n)
2863{
2864 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002865 return ast_for_expr(c, CHILD(n, 1));
2866}
2867
2868/* with_stmt: 'with' test [ with_var ] ':' suite */
2869static stmt_ty
2870ast_for_with_stmt(struct compiling *c, const node *n)
2871{
2872 expr_ty context_expr, optional_vars = NULL;
2873 int suite_index = 3; /* skip 'with', test, and ':' */
2874 asdl_seq *suite_seq;
2875
2876 assert(TYPE(n) == with_stmt);
2877 context_expr = ast_for_expr(c, CHILD(n, 1));
2878 if (TYPE(CHILD(n, 2)) == with_var) {
2879 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2880
2881 if (!optional_vars) {
2882 return NULL;
2883 }
Neal Norwitzd0421322006-09-05 04:00:12 +00002884 if (!set_context(optional_vars, Store, n)) {
2885 return NULL;
2886 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002887 suite_index = 4;
2888 }
2889
2890 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2891 if (!suite_seq) {
2892 return NULL;
2893 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002894 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Neal Norwitzd0421322006-09-05 04:00:12 +00002895 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002896}
2897
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898static stmt_ty
2899ast_for_classdef(struct compiling *c, const node *n)
2900{
2901 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902 asdl_seq *bases, *s;
2903
2904 REQ(n, classdef);
2905
2906 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002907 ast_error(n, "assignment to None");
2908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 }
2910
2911 if (NCH(n) == 4) {
2912 s = ast_for_suite(c, CHILD(n, 3));
2913 if (!s)
2914 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002915 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002916 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 }
2918 /* check for empty base list */
2919 if (TYPE(CHILD(n,3)) == RPAR) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002920 s = ast_for_suite(c, CHILD(n,5));
2921 if (!s)
2922 return NULL;
2923 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002924 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 }
2926
2927 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002928 bases = ast_for_class_bases(c, CHILD(n, 3));
2929 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931
2932 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002933 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002935 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2936 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937}
2938
2939static stmt_ty
2940ast_for_stmt(struct compiling *c, const node *n)
2941{
2942 if (TYPE(n) == stmt) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002943 assert(NCH(n) == 1);
2944 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 }
2946 if (TYPE(n) == simple_stmt) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002947 assert(num_stmts(n) == 1);
2948 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 }
2950 if (TYPE(n) == small_stmt) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002951 REQ(n, small_stmt);
2952 n = CHILD(n, 0);
2953 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2954 | flow_stmt | import_stmt | global_stmt | exec_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 | assert_stmt
Neal Norwitzd0421322006-09-05 04:00:12 +00002956 */
2957 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 case expr_stmt:
2959 return ast_for_expr_stmt(c, n);
2960 case print_stmt:
2961 return ast_for_print_stmt(c, n);
2962 case del_stmt:
2963 return ast_for_del_stmt(c, n);
2964 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002965 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 case flow_stmt:
2967 return ast_for_flow_stmt(c, n);
2968 case import_stmt:
2969 return ast_for_import_stmt(c, n);
2970 case global_stmt:
2971 return ast_for_global_stmt(c, n);
2972 case exec_stmt:
2973 return ast_for_exec_stmt(c, n);
2974 case assert_stmt:
2975 return ast_for_assert_stmt(c, n);
2976 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002977 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2979 TYPE(n), NCH(n));
2980 return NULL;
2981 }
2982 }
2983 else {
2984 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Neal Norwitzd0421322006-09-05 04:00:12 +00002985 | funcdef | classdef
2986 */
2987 node *ch = CHILD(n, 0);
2988 REQ(n, compound_stmt);
2989 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 case if_stmt:
2991 return ast_for_if_stmt(c, ch);
2992 case while_stmt:
2993 return ast_for_while_stmt(c, ch);
2994 case for_stmt:
2995 return ast_for_for_stmt(c, ch);
2996 case try_stmt:
2997 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002998 case with_stmt:
2999 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000 case funcdef:
3001 return ast_for_funcdef(c, ch);
3002 case classdef:
3003 return ast_for_classdef(c, ch);
3004 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003005 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3007 TYPE(n), NCH(n));
3008 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00003009 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 }
3011}
3012
3013static PyObject *
3014parsenumber(const char *s)
3015{
Neal Norwitzd0421322006-09-05 04:00:12 +00003016 const char *end;
3017 long x;
3018 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019#ifndef WITHOUT_COMPLEX
Neal Norwitzd0421322006-09-05 04:00:12 +00003020 Py_complex c;
3021 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022#endif
3023
Neal Norwitzd0421322006-09-05 04:00:12 +00003024 errno = 0;
3025 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026#ifndef WITHOUT_COMPLEX
Neal Norwitzd0421322006-09-05 04:00:12 +00003027 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003029 if (*end == 'l' || *end == 'L')
3030 return PyLong_FromString((char *)s, (char **)0, 0);
3031 if (s[0] == '0') {
3032 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3033 if (x < 0 && errno == 0) {
3034 return PyLong_FromString((char *)s,
3035 (char **)0,
3036 0);
3037 }
3038 }
3039 else
3040 x = PyOS_strtol((char *)s, (char **)&end, 0);
3041 if (*end == '\0') {
3042 if (errno != 0)
3043 return PyLong_FromString((char *)s, (char **)0, 0);
3044 return PyInt_FromLong(x);
3045 }
3046 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047#ifndef WITHOUT_COMPLEX
Neal Norwitzd0421322006-09-05 04:00:12 +00003048 if (imflag) {
3049 c.real = 0.;
3050 PyFPE_START_PROTECT("atof", return 0)
3051 c.imag = PyOS_ascii_atof(s);
3052 PyFPE_END_PROTECT(c)
3053 return PyComplex_FromCComplex(c);
3054 }
3055 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003057 {
3058 PyFPE_START_PROTECT("atof", return 0)
3059 dx = PyOS_ascii_atof(s);
3060 PyFPE_END_PROTECT(dx)
3061 return PyFloat_FromDouble(dx);
3062 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063}
3064
3065static PyObject *
3066decode_utf8(const char **sPtr, const char *end, char* encoding)
3067{
3068#ifndef Py_USING_UNICODE
Neal Norwitzd0421322006-09-05 04:00:12 +00003069 Py_FatalError("decode_utf8 should not be called in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070 return NULL;
3071#else
Neal Norwitzd0421322006-09-05 04:00:12 +00003072 PyObject *u, *v;
3073 char *s, *t;
3074 t = s = (char *)*sPtr;
3075 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3076 while (s < end && (*s & 0x80)) s++;
3077 *sPtr = s;
3078 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3079 if (u == NULL)
3080 return NULL;
3081 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3082 Py_DECREF(u);
3083 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084#endif
3085}
3086
3087static PyObject *
3088decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3089{
Neal Norwitzd0421322006-09-05 04:00:12 +00003090 PyObject *v, *u;
3091 char *buf;
3092 char *p;
3093 const char *end;
3094 if (encoding == NULL) {
3095 buf = (char *)s;
3096 u = NULL;
3097 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3098 buf = (char *)s;
3099 u = NULL;
3100 } else {
3101 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3102 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3103 if (u == NULL)
3104 return NULL;
3105 p = buf = PyString_AsString(u);
3106 end = s + len;
3107 while (s < end) {
3108 if (*s == '\\') {
3109 *p++ = *s++;
3110 if (*s & 0x80) {
3111 strcpy(p, "u005c");
3112 p += 5;
3113 }
3114 }
3115 if (*s & 0x80) { /* XXX inefficient */
3116 PyObject *w;
3117 char *r;
3118 Py_ssize_t rn, i;
3119 w = decode_utf8(&s, end, "utf-16-be");
3120 if (w == NULL) {
3121 Py_DECREF(u);
3122 return NULL;
3123 }
3124 r = PyString_AsString(w);
3125 rn = PyString_Size(w);
3126 assert(rn % 2 == 0);
3127 for (i = 0; i < rn; i += 2) {
3128 sprintf(p, "\\u%02x%02x",
3129 r[i + 0] & 0xFF,
3130 r[i + 1] & 0xFF);
3131 p += 6;
3132 }
3133 Py_DECREF(w);
3134 } else {
3135 *p++ = *s++;
3136 }
3137 }
3138 len = p - buf;
3139 s = buf;
3140 }
3141 if (rawmode)
3142 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3143 else
3144 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3145 Py_XDECREF(u);
3146 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147}
3148
3149/* s is a Python string literal, including the bracketing quote characters,
3150 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3151 * parsestr parses it, and returns the decoded Python string object.
3152 */
3153static PyObject *
3154parsestr(const char *s, const char *encoding)
3155{
Neal Norwitzd0421322006-09-05 04:00:12 +00003156 size_t len;
3157 int quote = Py_CHARMASK(*s);
3158 int rawmode = 0;
3159 int need_encoding;
3160 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161
Neal Norwitzd0421322006-09-05 04:00:12 +00003162 if (isalpha(quote) || quote == '_') {
3163 if (quote == 'u' || quote == 'U') {
3164 quote = *++s;
3165 unicode = 1;
3166 }
3167 if (quote == 'r' || quote == 'R') {
3168 quote = *++s;
3169 rawmode = 1;
3170 }
3171 }
3172 if (quote != '\'' && quote != '\"') {
3173 PyErr_BadInternalCall();
3174 return NULL;
3175 }
3176 s++;
3177 len = strlen(s);
3178 if (len > INT_MAX) {
3179 PyErr_SetString(PyExc_OverflowError,
3180 "string to parse is too long");
3181 return NULL;
3182 }
3183 if (s[--len] != quote) {
3184 PyErr_BadInternalCall();
3185 return NULL;
3186 }
3187 if (len >= 4 && s[0] == quote && s[1] == quote) {
3188 s += 2;
3189 len -= 2;
3190 if (s[--len] != quote || s[--len] != quote) {
3191 PyErr_BadInternalCall();
3192 return NULL;
3193 }
3194 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195#ifdef Py_USING_UNICODE
Neal Norwitzd0421322006-09-05 04:00:12 +00003196 if (unicode || Py_UnicodeFlag) {
3197 return decode_unicode(s, len, rawmode, encoding);
3198 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003200 need_encoding = (encoding != NULL &&
3201 strcmp(encoding, "utf-8") != 0 &&
3202 strcmp(encoding, "iso-8859-1") != 0);
3203 if (rawmode || strchr(s, '\\') == NULL) {
3204 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205#ifndef Py_USING_UNICODE
Neal Norwitzd0421322006-09-05 04:00:12 +00003206 /* This should not happen - we never see any other
3207 encoding. */
3208 Py_FatalError(
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003209 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210#else
Neal Norwitzd0421322006-09-05 04:00:12 +00003211 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3212 if (u == NULL)
3213 return NULL;
3214 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3215 Py_DECREF(u);
3216 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003218 } else {
3219 return PyString_FromStringAndSize(s, len);
3220 }
3221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222
Neal Norwitzd0421322006-09-05 04:00:12 +00003223 return PyString_DecodeEscape(s, len, NULL, unicode,
3224 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225}
3226
3227/* Build a Python string object out of a STRING atom. This takes care of
3228 * compile-time literal catenation, calling parsestr() on each piece, and
3229 * pasting the intermediate results together.
3230 */
3231static PyObject *
3232parsestrplus(struct compiling *c, const node *n)
3233{
Neal Norwitzd0421322006-09-05 04:00:12 +00003234 PyObject *v;
3235 int i;
3236 REQ(CHILD(n, 0), STRING);
3237 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3238 /* String literal concatenation */
3239 for (i = 1; i < NCH(n); i++) {
3240 PyObject *s;
3241 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3242 if (s == NULL)
3243 goto onError;
3244 if (PyString_Check(v) && PyString_Check(s)) {
3245 PyString_ConcatAndDel(&v, s);
3246 if (v == NULL)
3247 goto onError;
3248 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249#ifdef Py_USING_UNICODE
Neal Norwitzd0421322006-09-05 04:00:12 +00003250 else {
3251 PyObject *temp = PyUnicode_Concat(v, s);
3252 Py_DECREF(s);
3253 Py_DECREF(v);
3254 v = temp;
3255 if (v == NULL)
3256 goto onError;
3257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003259 }
3260 }
3261 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262
3263 onError:
Neal Norwitzd0421322006-09-05 04:00:12 +00003264 Py_XDECREF(v);
3265 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266}