blob: 9e0c1846c00d690ec5e042eb9207d756194bfd1a [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
563 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 for (i = 0; i < len; i++) {
565 const node *child = CHILD(CHILD(n, 2*i), 0);
566 expr_ty arg;
567 if (TYPE(child) == NAME) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000568 if (!strcmp(STR(child), "None")) {
569 ast_error(child, "assignment to None");
570 return NULL;
571 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000572 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
573 child->n_col_offset, c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +0000574 }
Jeremy Hyltona8293132006-02-28 17:58:27 +0000575 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000576 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000577 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 asdl_seq_SET(args, i, arg);
579 }
580
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000581 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000582 if (!set_context(result, Store, n))
583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584 return result;
585}
586
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Jeremy Hyltona8293132006-02-28 17:58:27 +0000588/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
590static arguments_ty
591ast_for_arguments(struct compiling *c, const node *n)
592{
593 /* parameters: '(' [varargslist] ')'
594 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
595 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
596 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000597 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 asdl_seq *args, *defaults;
599 identifier vararg = NULL, kwarg = NULL;
600 node *ch;
601
602 if (TYPE(n) == parameters) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000603 if (NCH(n) == 2) /* () as argument list */
604 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
605 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 }
607 REQ(n, varargslist);
608
609 /* first count the number of normal args & defaults */
610 for (i = 0; i < NCH(n); i++) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000611 ch = CHILD(n, i);
612 if (TYPE(ch) == fpdef)
613 n_args++;
614 if (TYPE(ch) == EQUAL)
615 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000617 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 if (!args && n_args)
Neal Norwitzd0421322006-09-05 04:00:12 +0000619 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000620 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 if (!defaults && n_defaults)
Neal Norwitzd0421322006-09-05 04:00:12 +0000622 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623
624 /* fpdef: NAME | '(' fplist ')'
625 fplist: fpdef (',' fpdef)* [',']
626 */
627 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000628 j = 0; /* index for defaults */
629 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630 while (i < NCH(n)) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000631 ch = CHILD(n, i);
632 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 case fpdef:
634 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
635 anything other than EQUAL or a comma? */
636 /* XXX Should NCH(n) check be made a separate check? */
637 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000638 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
639 if (!expression)
640 goto error;
Neal Norwitzc173b482006-07-30 19:18:13 +0000641 assert(defaults != NULL);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000642 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 i += 2;
Neal Norwitzd0421322006-09-05 04:00:12 +0000644 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 }
Neal Norwitzd0421322006-09-05 04:00:12 +0000646 else if (found_default) {
647 ast_error(n,
648 "non-default argument follows default argument");
649 goto error;
650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 if (NCH(ch) == 3) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000652 ch = CHILD(ch, 1);
653 /* def foo((x)): is not complex, special case. */
654 if (NCH(ch) != 1) {
655 /* We have complex arguments, setup for unpacking. */
656 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
657 } else {
658 /* def foo((x)): setup for checking NAME below. */
659 ch = CHILD(ch, 0);
660 }
Neal Norwitz33b730e2006-03-27 08:58:23 +0000661 }
662 if (TYPE(CHILD(ch, 0)) == NAME) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000663 expr_ty name;
664 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
665 ast_error(CHILD(ch, 0), "assignment to None");
666 goto error;
667 }
Armin Rigo31441302005-10-21 12:57:31 +0000668 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000669 Param, LINENO(ch), ch->n_col_offset,
670 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 if (!name)
672 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000673 asdl_seq_SET(args, k++, name);
Neal Norwitzd0421322006-09-05 04:00:12 +0000674
675 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 i += 2; /* the name and the comma */
677 break;
678 case STAR:
Neal Norwitzd0421322006-09-05 04:00:12 +0000679 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
680 ast_error(CHILD(n, i+1), "assignment to None");
681 goto error;
682 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
684 i += 3;
685 break;
686 case DOUBLESTAR:
Neal Norwitzd0421322006-09-05 04:00:12 +0000687 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
688 ast_error(CHILD(n, i+1), "assignment to None");
689 goto error;
690 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
692 i += 3;
693 break;
694 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000695 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 "unexpected node in varargslist: %d @ %d",
697 TYPE(ch), i);
698 goto error;
Neal Norwitzd0421322006-09-05 04:00:12 +0000699 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 }
701
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000702 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
704 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000705 Py_XDECREF(vararg);
706 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 return NULL;
708}
709
710static expr_ty
711ast_for_dotted_name(struct compiling *c, const node *n)
712{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000713 expr_ty e;
714 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000715 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716 int i;
717
718 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000719
720 lineno = LINENO(n);
721 col_offset = n->n_col_offset;
722
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 id = NEW_IDENTIFIER(CHILD(n, 0));
724 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000725 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000726 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 if (!e)
Neal Norwitzd0421322006-09-05 04:00:12 +0000728 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729
730 for (i = 2; i < NCH(n); i+=2) {
731 id = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitzd0421322006-09-05 04:00:12 +0000732 if (!id)
733 return NULL;
734 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
735 if (!e)
736 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 }
738
739 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740}
741
742static expr_ty
743ast_for_decorator(struct compiling *c, const node *n)
744{
745 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
746 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000747 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748
749 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000750 REQ(CHILD(n, 0), AT);
751 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752
753 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
754 if (!name_expr)
Neal Norwitzd0421322006-09-05 04:00:12 +0000755 return NULL;
756
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 if (NCH(n) == 3) { /* No arguments */
Neal Norwitzd0421322006-09-05 04:00:12 +0000758 d = name_expr;
759 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 }
761 else if (NCH(n) == 5) { /* Call with no arguments */
Neal Norwitzd0421322006-09-05 04:00:12 +0000762 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000763 n->n_col_offset, c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +0000764 if (!d)
765 return NULL;
766 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 }
768 else {
Neal Norwitzd0421322006-09-05 04:00:12 +0000769 d = ast_for_call(c, CHILD(n, 3), name_expr);
770 if (!d)
771 return NULL;
772 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 }
774
775 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776}
777
778static asdl_seq*
779ast_for_decorators(struct compiling *c, const node *n)
780{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000781 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000782 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 int i;
784
785 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000786 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 if (!decorator_seq)
788 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +0000789
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000791 d = ast_for_decorator(c, CHILD(n, i));
Neal Norwitzd0421322006-09-05 04:00:12 +0000792 if (!d)
793 return NULL;
794 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 }
796 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797}
798
799static stmt_ty
800ast_for_funcdef(struct compiling *c, const node *n)
801{
802 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000803 identifier name;
804 arguments_ty args;
805 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 asdl_seq *decorator_seq = NULL;
807 int name_i;
808
809 REQ(n, funcdef);
810
811 if (NCH(n) == 6) { /* decorators are present */
Neal Norwitzd0421322006-09-05 04:00:12 +0000812 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
813 if (!decorator_seq)
814 return NULL;
815 name_i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 }
817 else {
Neal Norwitzd0421322006-09-05 04:00:12 +0000818 name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 }
820
821 name = NEW_IDENTIFIER(CHILD(n, name_i));
822 if (!name)
Neal Norwitzd0421322006-09-05 04:00:12 +0000823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000825 ast_error(CHILD(n, name_i), "assignment to None");
826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 }
828 args = ast_for_arguments(c, CHILD(n, name_i + 1));
829 if (!args)
Neal Norwitzd0421322006-09-05 04:00:12 +0000830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 body = ast_for_suite(c, CHILD(n, name_i + 3));
832 if (!body)
Neal Norwitzd0421322006-09-05 04:00:12 +0000833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000835 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
836 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837}
838
839static expr_ty
840ast_for_lambdef(struct compiling *c, const node *n)
841{
842 /* lambdef: 'lambda' [varargslist] ':' test */
843 arguments_ty args;
844 expr_ty expression;
845
846 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000847 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 if (!args)
849 return NULL;
850 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000851 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 }
854 else {
855 args = ast_for_arguments(c, CHILD(n, 1));
856 if (!args)
857 return NULL;
858 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000859 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 }
862
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000863 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864}
865
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000866static expr_ty
867ast_for_ifexpr(struct compiling *c, const node *n)
868{
869 /* test: or_test 'if' or_test 'else' test */
870 expr_ty expression, body, orelse;
871
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000872 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000873 body = ast_for_expr(c, CHILD(n, 0));
874 if (!body)
Neal Norwitzd0421322006-09-05 04:00:12 +0000875 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000876 expression = ast_for_expr(c, CHILD(n, 2));
877 if (!expression)
Neal Norwitzd0421322006-09-05 04:00:12 +0000878 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000879 orelse = ast_for_expr(c, CHILD(n, 4));
880 if (!orelse)
Neal Norwitzd0421322006-09-05 04:00:12 +0000881 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000882 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
883 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000884}
885
Neal Norwitze4d4f002006-09-05 03:58:26 +0000886/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
887 so there is only a single version. Possibly for loops can also re-use
888 the code.
889*/
890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891/* Count the number of 'for' loop in a list comprehension.
892
893 Helper for ast_for_listcomp().
894*/
895
896static int
897count_list_fors(const node *n)
898{
899 int n_fors = 0;
900 node *ch = CHILD(n, 1);
901
902 count_list_for:
903 n_fors++;
904 REQ(ch, list_for);
905 if (NCH(ch) == 5)
Neal Norwitzd0421322006-09-05 04:00:12 +0000906 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907 else
Neal Norwitzd0421322006-09-05 04:00:12 +0000908 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 count_list_iter:
910 REQ(ch, list_iter);
911 ch = CHILD(ch, 0);
912 if (TYPE(ch) == list_for)
Neal Norwitzd0421322006-09-05 04:00:12 +0000913 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 else if (TYPE(ch) == list_if) {
915 if (NCH(ch) == 3) {
916 ch = CHILD(ch, 2);
917 goto count_list_iter;
918 }
919 else
920 return n_fors;
921 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000922
923 /* Should never be reached */
924 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
925 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926}
927
928/* Count the number of 'if' statements in a list comprehension.
929
930 Helper for ast_for_listcomp().
931*/
932
933static int
934count_list_ifs(const node *n)
935{
936 int n_ifs = 0;
937
938 count_list_iter:
939 REQ(n, list_iter);
940 if (TYPE(CHILD(n, 0)) == list_for)
Neal Norwitzd0421322006-09-05 04:00:12 +0000941 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 n = CHILD(n, 0);
943 REQ(n, list_if);
944 n_ifs++;
945 if (NCH(n) == 2)
Neal Norwitzd0421322006-09-05 04:00:12 +0000946 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 n = CHILD(n, 2);
948 goto count_list_iter;
949}
950
951static expr_ty
952ast_for_listcomp(struct compiling *c, const node *n)
953{
954 /* listmaker: test ( list_for | (',' test)* [','] )
955 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
956 list_iter: list_for | list_if
957 list_if: 'if' test [list_iter]
958 testlist_safe: test [(',' test)+ [',']]
959 */
960 expr_ty elt;
961 asdl_seq *listcomps;
962 int i, n_fors;
963 node *ch;
964
965 REQ(n, listmaker);
966 assert(NCH(n) > 1);
967
968 elt = ast_for_expr(c, CHILD(n, 0));
969 if (!elt)
970 return NULL;
971
972 n_fors = count_list_fors(n);
973 if (n_fors == -1)
974 return NULL;
975
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976 listcomps = asdl_seq_new(n_fors, c->c_arena);
977 if (!listcomps)
Neal Norwitzd0421322006-09-05 04:00:12 +0000978 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000979
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 ch = CHILD(n, 1);
981 for (i = 0; i < n_fors; i++) {
Neal Norwitzd0421322006-09-05 04:00:12 +0000982 comprehension_ty lc;
983 asdl_seq *t;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 expr_ty expression;
Neal Norwitzdac090d2006-09-05 03:53:08 +0000985 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986
Neal Norwitzd0421322006-09-05 04:00:12 +0000987 REQ(ch, list_for);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988
Neal Norwitzdac090d2006-09-05 03:53:08 +0000989 for_ch = CHILD(ch, 1);
990 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000991 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000993 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000994 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996
Neal Norwitzdac090d2006-09-05 03:53:08 +0000997 /* Check the # of children rather than the length of t, since
998 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
Neal Norwitzd0421322006-09-05 04:00:12 +0000999 if (NCH(for_ch) == 1)
1000 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001001 c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +00001002 else
1003 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001004 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001005 expression, NULL, c->c_arena);
1006 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008
Neal Norwitzd0421322006-09-05 04:00:12 +00001009 if (NCH(ch) == 5) {
1010 int j, n_ifs;
1011 asdl_seq *ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012
Neal Norwitzd0421322006-09-05 04:00:12 +00001013 ch = CHILD(ch, 4);
1014 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001015 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017
Neal Norwitzd0421322006-09-05 04:00:12 +00001018 ifs = asdl_seq_new(n_ifs, c->c_arena);
1019 if (!ifs)
1020 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021
Neal Norwitzd0421322006-09-05 04:00:12 +00001022 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001023 REQ(ch, list_iter);
Neal Norwitzd0421322006-09-05 04:00:12 +00001024 ch = CHILD(ch, 0);
1025 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
Neal Norwitzd0421322006-09-05 04:00:12 +00001027 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1028 if (NCH(ch) == 3)
1029 ch = CHILD(ch, 2);
1030 }
1031 /* on exit, must guarantee that ch is a list_for */
1032 if (TYPE(ch) == list_iter)
1033 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 lc->ifs = ifs;
Neal Norwitzd0421322006-09-05 04:00:12 +00001035 }
1036 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 }
1038
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001039 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040}
1041
1042/*
1043 Count the number of 'for' loops in a generator expression.
1044
1045 Helper for ast_for_genexp().
1046*/
1047
1048static int
1049count_gen_fors(const node *n)
1050{
Neal Norwitzd0421322006-09-05 04:00:12 +00001051 int n_fors = 0;
1052 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053
1054 count_gen_for:
Neal Norwitzd0421322006-09-05 04:00:12 +00001055 n_fors++;
1056 REQ(ch, gen_for);
1057 if (NCH(ch) == 5)
1058 ch = CHILD(ch, 4);
1059 else
1060 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 count_gen_iter:
Neal Norwitzd0421322006-09-05 04:00:12 +00001062 REQ(ch, gen_iter);
1063 ch = CHILD(ch, 0);
1064 if (TYPE(ch) == gen_for)
1065 goto count_gen_for;
1066 else if (TYPE(ch) == gen_if) {
1067 if (NCH(ch) == 3) {
1068 ch = CHILD(ch, 2);
1069 goto count_gen_iter;
1070 }
1071 else
1072 return n_fors;
1073 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001074
Neal Norwitzd0421322006-09-05 04:00:12 +00001075 /* Should never be reached */
1076 PyErr_SetString(PyExc_SystemError,
1077 "logic error in count_gen_fors");
1078 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079}
1080
1081/* Count the number of 'if' statements in a generator expression.
1082
1083 Helper for ast_for_genexp().
1084*/
1085
1086static int
1087count_gen_ifs(const node *n)
1088{
Neal Norwitzd0421322006-09-05 04:00:12 +00001089 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090
Neal Norwitzd0421322006-09-05 04:00:12 +00001091 while (1) {
1092 REQ(n, gen_iter);
1093 if (TYPE(CHILD(n, 0)) == gen_for)
1094 return n_ifs;
1095 n = CHILD(n, 0);
1096 REQ(n, gen_if);
1097 n_ifs++;
1098 if (NCH(n) == 2)
1099 return n_ifs;
1100 n = CHILD(n, 2);
1101 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102}
1103
Jeremy Hyltona8293132006-02-28 17:58:27 +00001104/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105static expr_ty
1106ast_for_genexp(struct compiling *c, const node *n)
1107{
1108 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Neal Norwitzd0421322006-09-05 04:00:12 +00001109 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 expr_ty elt;
1111 asdl_seq *genexps;
1112 int i, n_fors;
1113 node *ch;
1114
1115 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1116 assert(NCH(n) > 1);
1117
1118 elt = ast_for_expr(c, CHILD(n, 0));
1119 if (!elt)
1120 return NULL;
1121
1122 n_fors = count_gen_fors(n);
1123 if (n_fors == -1)
1124 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001125
1126 genexps = asdl_seq_new(n_fors, c->c_arena);
1127 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001129
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 ch = CHILD(n, 1);
1131 for (i = 0; i < n_fors; i++) {
1132 comprehension_ty ge;
1133 asdl_seq *t;
1134 expr_ty expression;
Neal Norwitzdac090d2006-09-05 03:53:08 +00001135 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136
1137 REQ(ch, gen_for);
1138
Neal Norwitzdac090d2006-09-05 03:53:08 +00001139 for_ch = CHILD(ch, 1);
1140 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001141 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001143 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001144 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001146
Neal Norwitzdac090d2006-09-05 03:53:08 +00001147 /* Check the # of children rather than the length of t, since
1148 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1149 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001150 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001151 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001153 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1154 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001155 expression, NULL, c->c_arena);
1156
1157 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 if (NCH(ch) == 5) {
1161 int j, n_ifs;
1162 asdl_seq *ifs;
1163
1164 ch = CHILD(ch, 4);
1165 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001166 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001168
1169 ifs = asdl_seq_new(n_ifs, c->c_arena);
1170 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 for (j = 0; j < n_ifs; j++) {
1174 REQ(ch, gen_iter);
1175 ch = CHILD(ch, 0);
1176 REQ(ch, gen_if);
1177
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001178 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001179 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001180 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001181 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 if (NCH(ch) == 3)
1183 ch = CHILD(ch, 2);
1184 }
1185 /* on exit, must guarantee that ch is a gen_for */
1186 if (TYPE(ch) == gen_iter)
1187 ch = CHILD(ch, 0);
1188 ge->ifs = ifs;
1189 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001190 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 }
1192
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001193 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194}
1195
1196static expr_ty
1197ast_for_atom(struct compiling *c, const node *n)
1198{
1199 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1200 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1201 */
1202 node *ch = CHILD(n, 0);
1203
1204 switch (TYPE(ch)) {
1205 case NAME:
Neal Norwitzd0421322006-09-05 04:00:12 +00001206 /* All names start in Load context, but may later be
1207 changed. */
1208 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 case STRING: {
Neal Norwitzd0421322006-09-05 04:00:12 +00001210 PyObject *str = parsestrplus(c, n);
1211 if (!str)
1212 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001213
Neal Norwitzd0421322006-09-05 04:00:12 +00001214 PyArena_AddPyObject(c->c_arena, str);
1215 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 }
1217 case NUMBER: {
Neal Norwitzd0421322006-09-05 04:00:12 +00001218 PyObject *pynum = parsenumber(STR(ch));
1219 if (!pynum)
1220 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001221
Neal Norwitzd0421322006-09-05 04:00:12 +00001222 PyArena_AddPyObject(c->c_arena, pynum);
1223 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 }
1225 case LPAR: /* some parenthesized expressions */
Neal Norwitzd0421322006-09-05 04:00:12 +00001226 ch = CHILD(n, 1);
1227
1228 if (TYPE(ch) == RPAR)
1229 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1230
1231 if (TYPE(ch) == yield_expr)
1232 return ast_for_expr(c, ch);
1233
1234 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1235 return ast_for_genexp(c, ch);
1236
1237 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 case LSQB: /* list (or list comprehension) */
Neal Norwitzd0421322006-09-05 04:00:12 +00001239 ch = CHILD(n, 1);
1240
1241 if (TYPE(ch) == RSQB)
1242 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1243
1244 REQ(ch, listmaker);
1245 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1246 asdl_seq *elts = seq_for_testlist(c, ch);
1247 if (!elts)
1248 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001249
Neal Norwitzd0421322006-09-05 04:00:12 +00001250 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1251 }
1252 else
1253 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 case LBRACE: {
Neal Norwitzd0421322006-09-05 04:00:12 +00001255 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1256 int i, size;
1257 asdl_seq *keys, *values;
1258
1259 ch = CHILD(n, 1);
1260 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1261 keys = asdl_seq_new(size, c->c_arena);
1262 if (!keys)
1263 return NULL;
1264
1265 values = asdl_seq_new(size, c->c_arena);
1266 if (!values)
1267 return NULL;
1268
1269 for (i = 0; i < NCH(ch); i += 4) {
1270 expr_ty expression;
1271
1272 expression = ast_for_expr(c, CHILD(ch, i));
1273 if (!expression)
1274 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001275
Neal Norwitzd0421322006-09-05 04:00:12 +00001276 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001277
Neal Norwitzd0421322006-09-05 04:00:12 +00001278 expression = ast_for_expr(c, CHILD(ch, i + 2));
1279 if (!expression)
1280 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001281
Neal Norwitzd0421322006-09-05 04:00:12 +00001282 asdl_seq_SET(values, i / 4, expression);
1283 }
1284 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 }
1286 case BACKQUOTE: { /* repr */
Neal Norwitzd0421322006-09-05 04:00:12 +00001287 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
1288 if (!expression)
1289 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001290
Neal Norwitzd0421322006-09-05 04:00:12 +00001291 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 }
1293 default:
Neal Norwitzd0421322006-09-05 04:00:12 +00001294 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 }
1297}
1298
1299static slice_ty
1300ast_for_slice(struct compiling *c, const node *n)
1301{
1302 node *ch;
1303 expr_ty lower = NULL, upper = NULL, step = NULL;
1304
1305 REQ(n, subscript);
1306
1307 /*
1308 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1309 sliceop: ':' [test]
1310 */
1311 ch = CHILD(n, 0);
1312 if (TYPE(ch) == DOT)
Neal Norwitzd0421322006-09-05 04:00:12 +00001313 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314
1315 if (NCH(n) == 1 && TYPE(ch) == test) {
1316 /* 'step' variable hold no significance in terms of being used over
1317 other vars */
1318 step = ast_for_expr(c, ch);
1319 if (!step)
1320 return NULL;
1321
Neal Norwitzd0421322006-09-05 04:00:12 +00001322 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 }
1324
1325 if (TYPE(ch) == test) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001326 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 if (!lower)
1328 return NULL;
1329 }
1330
1331 /* If there's an upper bound it's in the second or third position. */
1332 if (TYPE(ch) == COLON) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001333 if (NCH(n) > 1) {
1334 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335
Neal Norwitzd0421322006-09-05 04:00:12 +00001336 if (TYPE(n2) == test) {
1337 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338 if (!upper)
1339 return NULL;
1340 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001341 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 } else if (NCH(n) > 2) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001343 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344
Neal Norwitzd0421322006-09-05 04:00:12 +00001345 if (TYPE(n2) == test) {
1346 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 if (!upper)
1348 return NULL;
1349 }
1350 }
1351
1352 ch = CHILD(n, NCH(n) - 1);
1353 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001354 if (NCH(ch) == 1) {
1355 /* No expression, so step is None */
1356 ch = CHILD(ch, 0);
1357 step = Name(new_identifier("None", c->c_arena), Load,
1358 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 if (!step)
1360 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001361 } else {
1362 ch = CHILD(ch, 1);
1363 if (TYPE(ch) == test) {
1364 step = ast_for_expr(c, ch);
1365 if (!step)
1366 return NULL;
1367 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 }
1369 }
1370
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001371 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372}
1373
1374static expr_ty
1375ast_for_binop(struct compiling *c, const node *n)
1376{
Neal Norwitzd0421322006-09-05 04:00:12 +00001377 /* Must account for a sequence of expressions.
1378 How should A op B op C by represented?
1379 BinOp(BinOp(A, op, B), op, C).
1380 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381
Neal Norwitzd0421322006-09-05 04:00:12 +00001382 int i, nops;
1383 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001384 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385
1386 expr1 = ast_for_expr(c, CHILD(n, 0));
1387 if (!expr1)
1388 return NULL;
1389
1390 expr2 = ast_for_expr(c, CHILD(n, 2));
1391 if (!expr2)
1392 return NULL;
1393
Anthony Baxtera863d332006-04-11 07:43:46 +00001394 newoperator = get_operator(CHILD(n, 1));
1395 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 return NULL;
1397
Neal Norwitzd0421322006-09-05 04:00:12 +00001398 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001399 c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +00001400 if (!result)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 return NULL;
1402
Neal Norwitzd0421322006-09-05 04:00:12 +00001403 nops = (NCH(n) - 1) / 2;
1404 for (i = 1; i < nops; i++) {
1405 expr_ty tmp_result, tmp;
1406 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407
Neal Norwitzd0421322006-09-05 04:00:12 +00001408 newoperator = get_operator(next_oper);
Anthony Baxtera863d332006-04-11 07:43:46 +00001409 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 return NULL;
1411
1412 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1413 if (!tmp)
1414 return NULL;
1415
Anthony Baxtera863d332006-04-11 07:43:46 +00001416 tmp_result = BinOp(result, newoperator, tmp,
Neal Norwitzd0421322006-09-05 04:00:12 +00001417 LINENO(next_oper), next_oper->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001418 c->c_arena);
Neal Norwitzd0421322006-09-05 04:00:12 +00001419 if (!tmp)
1420 return NULL;
1421 result = tmp_result;
1422 }
1423 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424}
1425
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001426static expr_ty
1427ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1428{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001429 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1430 subscriptlist: subscript (',' subscript)* [',']
1431 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1432 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001433 REQ(n, trailer);
1434 if (TYPE(CHILD(n, 0)) == LPAR) {
1435 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001436 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1437 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001438 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001439 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001440 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001441 else if (TYPE(CHILD(n, 0)) == DOT ) {
1442 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001443 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001444 }
1445 else {
1446 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001447 REQ(CHILD(n, 2), RSQB);
1448 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001449 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001450 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1451 if (!slc)
1452 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001453 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1454 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001455 }
1456 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001457 /* The grammar is ambiguous here. The ambiguity is resolved
1458 by treating the sequence as a tuple literal if there are
1459 no slice features.
1460 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001461 int j;
1462 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001463 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001464 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001465 asdl_seq *slices, *elts;
1466 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001467 if (!slices)
1468 return NULL;
1469 for (j = 0; j < NCH(n); j += 2) {
1470 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001471 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001472 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001473 if (slc->kind != Index_kind)
1474 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001475 asdl_seq_SET(slices, j / 2, slc);
1476 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001477 if (!simple) {
1478 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001480 }
1481 /* extract Index values and put them in a Tuple */
1482 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001483 if (!elts)
1484 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001485 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1486 slc = (slice_ty)asdl_seq_GET(slices, j);
1487 assert(slc->kind == Index_kind && slc->v.Index.value);
1488 asdl_seq_SET(elts, j, slc->v.Index.value);
1489 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001490 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001491 if (!e)
1492 return NULL;
1493 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001494 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001495 }
1496 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001497}
1498
1499static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001500ast_for_factor(struct compiling *c, const node *n)
1501{
1502 node *pfactor, *ppower, *patom, *pnum;
1503 expr_ty expression;
1504
1505 /* If the unary - operator is applied to a constant, don't generate
1506 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1507 constant. The peephole optimizer already does something like
1508 this but it doesn't handle the case where the constant is
1509 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1510 PyLongObject.
1511 */
1512 if (TYPE(CHILD(n, 0)) == MINUS
1513 && NCH(n) == 2
1514 && TYPE((pfactor = CHILD(n, 1))) == factor
1515 && NCH(pfactor) == 1
1516 && TYPE((ppower = CHILD(pfactor, 0))) == power
1517 && NCH(ppower) == 1
1518 && TYPE((patom = CHILD(ppower, 0))) == atom
1519 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1520 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1521 if (s == NULL)
1522 return NULL;
1523 s[0] = '-';
1524 strcpy(s + 1, STR(pnum));
1525 PyObject_FREE(STR(pnum));
1526 STR(pnum) = s;
1527 return ast_for_atom(c, patom);
1528 }
1529
1530 expression = ast_for_expr(c, CHILD(n, 1));
1531 if (!expression)
1532 return NULL;
1533
1534 switch (TYPE(CHILD(n, 0))) {
1535 case PLUS:
1536 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1537 c->c_arena);
1538 case MINUS:
1539 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1540 c->c_arena);
1541 case TILDE:
1542 return UnaryOp(Invert, expression, LINENO(n),
1543 n->n_col_offset, c->c_arena);
1544 }
1545 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1546 TYPE(CHILD(n, 0)));
1547 return NULL;
1548}
1549
1550static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001551ast_for_power(struct compiling *c, const node *n)
1552{
1553 /* power: atom trailer* ('**' factor)*
1554 */
1555 int i;
1556 expr_ty e, tmp;
1557 REQ(n, power);
1558 e = ast_for_atom(c, CHILD(n, 0));
1559 if (!e)
1560 return NULL;
1561 if (NCH(n) == 1)
1562 return e;
1563 for (i = 1; i < NCH(n); i++) {
1564 node *ch = CHILD(n, i);
1565 if (TYPE(ch) != trailer)
1566 break;
1567 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001568 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001569 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001570 tmp->lineno = e->lineno;
1571 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001572 e = tmp;
1573 }
1574 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1575 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001576 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001577 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001578 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001579 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001580 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001581 e = tmp;
1582 }
1583 return e;
1584}
1585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586/* Do not name a variable 'expr'! Will cause a compile error.
1587*/
1588
1589static expr_ty
1590ast_for_expr(struct compiling *c, const node *n)
1591{
1592 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001593 test: or_test ['if' or_test 'else' test] | lambdef
1594 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 and_test: not_test ('and' not_test)*
1596 not_test: 'not' not_test | comparison
1597 comparison: expr (comp_op expr)*
1598 expr: xor_expr ('|' xor_expr)*
1599 xor_expr: and_expr ('^' and_expr)*
1600 and_expr: shift_expr ('&' shift_expr)*
1601 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1602 arith_expr: term (('+'|'-') term)*
1603 term: factor (('*'|'/'|'%'|'//') factor)*
1604 factor: ('+'|'-'|'~') factor | power
1605 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001606
1607 As well as modified versions that exist for backward compatibility,
1608 to explicitly allow:
1609 [ x for x in lambda: 0, lambda: 1 ]
1610 (which would be ambiguous without these extra rules)
1611
1612 old_test: or_test | old_lambdef
1613 old_lambdef: 'lambda' [vararglist] ':' old_test
1614
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 */
1616
1617 asdl_seq *seq;
1618 int i;
1619
1620 loop:
1621 switch (TYPE(n)) {
1622 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001623 case old_test:
1624 if (TYPE(CHILD(n, 0)) == lambdef ||
1625 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001627 else if (NCH(n) > 1)
1628 return ast_for_ifexpr(c, n);
Neal Norwitzd0421322006-09-05 04:00:12 +00001629 /* Fallthrough */
1630 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631 case and_test:
1632 if (NCH(n) == 1) {
1633 n = CHILD(n, 0);
1634 goto loop;
1635 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001636 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637 if (!seq)
1638 return NULL;
1639 for (i = 0; i < NCH(n); i += 2) {
1640 expr_ty e = ast_for_expr(c, CHILD(n, i));
1641 if (!e)
1642 return NULL;
1643 asdl_seq_SET(seq, i / 2, e);
1644 }
1645 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001646 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1647 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001648 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001649 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 case not_test:
1651 if (NCH(n) == 1) {
1652 n = CHILD(n, 0);
1653 goto loop;
1654 }
1655 else {
1656 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1657 if (!expression)
1658 return NULL;
1659
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001660 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1661 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 }
1663 case comparison:
1664 if (NCH(n) == 1) {
1665 n = CHILD(n, 0);
1666 goto loop;
1667 }
1668 else {
1669 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001670 asdl_int_seq *ops;
Neal Norwitzd0421322006-09-05 04:00:12 +00001671 asdl_seq *cmps;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001672 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 if (!ops)
1674 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001675 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 return NULL;
1678 }
1679 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001680 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681
Anthony Baxtera863d332006-04-11 07:43:46 +00001682 newoperator = ast_for_comp_op(CHILD(n, i));
1683 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686
1687 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001688 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001690 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001692 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 asdl_seq_SET(cmps, i / 2, expression);
1694 }
1695 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001696 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001698 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001700 return Compare(expression, ops, cmps, LINENO(n),
1701 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 }
1703 break;
1704
1705 /* The next five cases all handle BinOps. The main body of code
1706 is the same in each case, but the switch turned inside out to
1707 reuse the code for each type of operator.
1708 */
1709 case expr:
1710 case xor_expr:
1711 case and_expr:
1712 case shift_expr:
1713 case arith_expr:
1714 case term:
1715 if (NCH(n) == 1) {
1716 n = CHILD(n, 0);
1717 goto loop;
1718 }
1719 return ast_for_binop(c, n);
1720 case yield_expr: {
Neal Norwitzd0421322006-09-05 04:00:12 +00001721 expr_ty exp = NULL;
1722 if (NCH(n) == 2) {
1723 exp = ast_for_testlist(c, CHILD(n, 1));
1724 if (!exp)
1725 return NULL;
1726 }
1727 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1728 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001729 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 if (NCH(n) == 1) {
1731 n = CHILD(n, 0);
1732 goto loop;
1733 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001734 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001735 case power:
1736 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001738 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739 return NULL;
1740 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001741 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 return NULL;
1743}
1744
1745static expr_ty
1746ast_for_call(struct compiling *c, const node *n, expr_ty func)
1747{
1748 /*
1749 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1750 | '**' test)
Neal Norwitzd0421322006-09-05 04:00:12 +00001751 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752 */
1753
1754 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001755 asdl_seq *args;
1756 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 expr_ty vararg = NULL, kwarg = NULL;
1758
1759 REQ(n, arglist);
1760
1761 nargs = 0;
1762 nkeywords = 0;
1763 ngens = 0;
1764 for (i = 0; i < NCH(n); i++) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001765 node *ch = CHILD(n, i);
1766 if (TYPE(ch) == argument) {
1767 if (NCH(ch) == 1)
1768 nargs++;
1769 else if (TYPE(CHILD(ch, 1)) == gen_for)
1770 ngens++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 else
Neal Norwitzd0421322006-09-05 04:00:12 +00001772 nkeywords++;
1773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 }
1775 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001776 ast_error(n, "Generator expression must be parenthesized "
Neal Norwitzd0421322006-09-05 04:00:12 +00001777 "if not sole argument");
1778 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779 }
1780
1781 if (nargs + nkeywords + ngens > 255) {
1782 ast_error(n, "more than 255 arguments");
1783 return NULL;
1784 }
1785
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001786 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001788 return NULL;
1789 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001791 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 nargs = 0;
1793 nkeywords = 0;
1794 for (i = 0; i < NCH(n); i++) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001795 node *ch = CHILD(n, i);
1796 if (TYPE(ch) == argument) {
1797 expr_ty e;
1798 if (NCH(ch) == 1) {
1799 if (nkeywords) {
1800 ast_error(CHILD(ch, 0),
1801 "non-keyword arg after keyword arg");
1802 return NULL;
1803 }
1804 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001806 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001807 asdl_seq_SET(args, nargs++, e);
1808 }
1809 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1810 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001812 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001813 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001815 else {
1816 keyword_ty kw;
1817 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818
Neal Norwitzd0421322006-09-05 04:00:12 +00001819 /* CHILD(ch, 0) is test, but must be an identifier? */
1820 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001822 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 /* f(lambda x: x[0] = 3) ends up getting parsed with
1824 * LHS test = lambda x: x[0], and RHS test = 3.
1825 * SF bug 132313 points out that complaining about a keyword
1826 * then is very confusing.
1827 */
1828 if (e->kind == Lambda_kind) {
1829 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 } else if (e->kind != Name_kind) {
1832 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001835 key = e->v.Name.id;
1836 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001838 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001839 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001841 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001842 asdl_seq_SET(keywords, nkeywords++, kw);
1843 }
1844 }
1845 else if (TYPE(ch) == STAR) {
1846 vararg = ast_for_expr(c, CHILD(n, i+1));
1847 i++;
1848 }
1849 else if (TYPE(ch) == DOUBLESTAR) {
1850 kwarg = ast_for_expr(c, CHILD(n, i+1));
1851 i++;
1852 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 }
1854
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001855 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856}
1857
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001859ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001861 /* testlist_gexp: test (',' test)* [','] */
1862 /* testlist: test (',' test)* [','] */
1863 /* testlist_safe: test (',' test)+ [','] */
1864 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001866 if (TYPE(n) == testlist_gexp) {
1867 if (NCH(n) > 1)
1868 assert(TYPE(CHILD(n, 1)) != gen_for);
1869 }
1870 else {
1871 assert(TYPE(n) == testlist ||
1872 TYPE(n) == testlist_safe ||
1873 TYPE(n) == testlist1);
1874 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 if (NCH(n) == 1)
Neal Norwitzd0421322006-09-05 04:00:12 +00001876 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 else {
1878 asdl_seq *tmp = seq_for_testlist(c, n);
1879 if (!tmp)
1880 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00001881 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001883}
1884
1885static expr_ty
1886ast_for_testlist_gexp(struct compiling *c, const node* n)
1887{
1888 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1889 /* argument: test [ gen_for ] */
1890 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001891 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neal Norwitzd0421322006-09-05 04:00:12 +00001892 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001893 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001894}
1895
1896/* like ast_for_testlist() but returns a sequence */
1897static asdl_seq*
1898ast_for_class_bases(struct compiling *c, const node* n)
1899{
1900 /* testlist: test (',' test)* [','] */
1901 assert(NCH(n) > 0);
1902 REQ(n, testlist);
1903 if (NCH(n) == 1) {
1904 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001905 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001906 if (!bases)
1907 return NULL;
1908 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001909 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001910 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001911 asdl_seq_SET(bases, 0, base);
1912 return bases;
1913 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001914
1915 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916}
1917
1918static stmt_ty
1919ast_for_expr_stmt(struct compiling *c, const node *n)
1920{
1921 REQ(n, expr_stmt);
1922 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1923 | ('=' (yield_expr|testlist))*)
1924 testlist: test (',' test)* [',']
1925 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Neal Norwitzd0421322006-09-05 04:00:12 +00001926 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927 test: ... here starts the operator precendence dance
1928 */
1929
1930 if (NCH(n) == 1) {
Neal Norwitzd0421322006-09-05 04:00:12 +00001931 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932 if (!e)
1933 return NULL;
1934
Neal Norwitzd0421322006-09-05 04:00:12 +00001935 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 }
1937 else if (TYPE(CHILD(n, 1)) == augassign) {
1938 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00001939 operator_ty newoperator;
Neal Norwitzd0421322006-09-05 04:00:12 +00001940 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941
Neal Norwitzd0421322006-09-05 04:00:12 +00001942 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 if (!expr1)
1944 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001945 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001946 switch (expr1->kind) {
1947 case GeneratorExp_kind:
1948 ast_error(ch, "augmented assignment to generator "
1949 "expression not possible");
1950 return NULL;
Neal Norwitz0d62a062006-07-30 06:53:31 +00001951 case Yield_kind:
1952 ast_error(ch, "augmented assignment to yield "
1953 "expression not possible");
1954 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001955 case Name_kind: {
1956 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1957 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1958 ast_error(ch, "assignment to None");
1959 return NULL;
1960 }
1961 break;
1962 }
1963 case Attribute_kind:
1964 case Subscript_kind:
1965 break;
1966 default:
1967 ast_error(ch, "illegal expression for augmented "
1968 "assignment");
1969 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 }
Neal Norwitzd0421322006-09-05 04:00:12 +00001971 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972
Neal Norwitzd0421322006-09-05 04:00:12 +00001973 ch = CHILD(n, 2);
1974 if (TYPE(ch) == testlist)
1975 expr2 = ast_for_testlist(c, ch);
1976 else
1977 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001978 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 return NULL;
1980
Anthony Baxtera863d332006-04-11 07:43:46 +00001981 newoperator = ast_for_augassign(CHILD(n, 1));
1982 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 return NULL;
1984
Neal Norwitzd0421322006-09-05 04:00:12 +00001985 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 }
1987 else {
Neal Norwitzd0421322006-09-05 04:00:12 +00001988 int i;
1989 asdl_seq *targets;
1990 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 expr_ty expression;
1992
Neal Norwitzd0421322006-09-05 04:00:12 +00001993 /* a normal assignment */
1994 REQ(CHILD(n, 1), EQUAL);
1995 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
1996 if (!targets)
1997 return NULL;
1998 for (i = 0; i < NCH(n) - 2; i += 2) {
1999 expr_ty e;
2000 node *ch = CHILD(n, i);
2001 if (TYPE(ch) == yield_expr) {
2002 ast_error(ch, "assignment to yield expression not possible");
2003 return NULL;
2004 }
2005 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006
Neal Norwitzd0421322006-09-05 04:00:12 +00002007 /* set context to assign */
2008 if (!e)
2009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010
Neal Norwitzd0421322006-09-05 04:00:12 +00002011 if (!set_context(e, Store, CHILD(n, i)))
2012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013
Neal Norwitzd0421322006-09-05 04:00:12 +00002014 asdl_seq_SET(targets, i / 2, e);
2015 }
2016 value = CHILD(n, NCH(n) - 1);
2017 if (TYPE(value) == testlist)
2018 expression = ast_for_testlist(c, value);
2019 else
2020 expression = ast_for_expr(c, value);
2021 if (!expression)
2022 return NULL;
2023 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025}
2026
2027static stmt_ty
2028ast_for_print_stmt(struct compiling *c, const node *n)
2029{
2030 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2031 | '>>' test [ (',' test)+ [','] ] )
2032 */
2033 expr_ty dest = NULL, expression;
2034 asdl_seq *seq;
2035 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002036 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037
2038 REQ(n, print_stmt);
2039 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002040 dest = ast_for_expr(c, CHILD(n, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 if (!dest)
2042 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002043 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002045 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 if (!seq)
Neal Norwitzd0421322006-09-05 04:00:12 +00002047 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002048 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002050 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002052 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 }
2054 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002055 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056}
2057
2058static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002059ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060{
2061 asdl_seq *seq;
2062 int i;
2063 expr_ty e;
2064
2065 REQ(n, exprlist);
2066
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002067 seq = asdl_seq_new((NCH(n) + 1) / 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 Hylton3e0055f2005-10-20 19:59:25 +00002070 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002071 e = ast_for_expr(c, CHILD(n, i));
2072 if (!e)
2073 return NULL;
2074 asdl_seq_SET(seq, i / 2, e);
2075 if (context && !set_context(e, context, CHILD(n, i)))
2076 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 }
2078 return seq;
2079}
2080
2081static stmt_ty
2082ast_for_del_stmt(struct compiling *c, const node *n)
2083{
2084 asdl_seq *expr_list;
2085
2086 /* del_stmt: 'del' exprlist */
2087 REQ(n, del_stmt);
2088
2089 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2090 if (!expr_list)
2091 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002092 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093}
2094
2095static stmt_ty
2096ast_for_flow_stmt(struct compiling *c, const node *n)
2097{
2098 /*
2099 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2100 | yield_stmt
2101 break_stmt: 'break'
2102 continue_stmt: 'continue'
2103 return_stmt: 'return' [testlist]
2104 yield_stmt: yield_expr
2105 yield_expr: 'yield' testlist
2106 raise_stmt: 'raise' [test [',' test [',' test]]]
2107 */
2108 node *ch;
2109
2110 REQ(n, flow_stmt);
2111 ch = CHILD(n, 0);
2112 switch (TYPE(ch)) {
2113 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002114 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002116 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 case yield_stmt: { /* will reduce to yield_expr */
Neal Norwitzd0421322006-09-05 04:00:12 +00002118 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2119 if (!exp)
2120 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002121 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 }
2123 case return_stmt:
2124 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002125 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002127 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 if (!expression)
2129 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002130 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 }
2132 case raise_stmt:
2133 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002134 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 else if (NCH(ch) == 2) {
2136 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2137 if (!expression)
2138 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002139 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 }
2141 else if (NCH(ch) == 4) {
2142 expr_ty expr1, expr2;
2143
2144 expr1 = ast_for_expr(c, CHILD(ch, 1));
2145 if (!expr1)
2146 return NULL;
2147 expr2 = ast_for_expr(c, CHILD(ch, 3));
2148 if (!expr2)
2149 return NULL;
2150
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002151 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 }
2153 else if (NCH(ch) == 6) {
2154 expr_ty expr1, expr2, expr3;
2155
2156 expr1 = ast_for_expr(c, CHILD(ch, 1));
2157 if (!expr1)
2158 return NULL;
2159 expr2 = ast_for_expr(c, CHILD(ch, 3));
2160 if (!expr2)
2161 return NULL;
2162 expr3 = ast_for_expr(c, CHILD(ch, 5));
2163 if (!expr3)
2164 return NULL;
2165
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002166 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 }
2168 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002169 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 "unexpected flow_stmt: %d", TYPE(ch));
2171 return NULL;
2172 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002173
2174 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2175 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176}
2177
2178static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002179alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180{
2181 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002182 import_as_name: NAME ['as' NAME]
2183 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 dotted_name: NAME ('.' NAME)*
2185 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002186 PyObject *str;
2187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 loop:
2189 switch (TYPE(n)) {
2190 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002191 str = NULL;
2192 if (NCH(n) == 3) {
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002193 str = NEW_IDENTIFIER(CHILD(n, 2));
2194 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002195 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 case dotted_as_name:
2197 if (NCH(n) == 1) {
2198 n = CHILD(n, 0);
2199 goto loop;
2200 }
2201 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002202 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00002203 if (!a)
2204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205 assert(!a->asname);
2206 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2207 return a;
2208 }
2209 break;
2210 case dotted_name:
2211 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002212 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 else {
2214 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002215 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002216 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217 char *s;
2218
2219 len = 0;
2220 for (i = 0; i < NCH(n); i += 2)
2221 /* length of string plus one for the dot */
2222 len += strlen(STR(CHILD(n, i))) + 1;
2223 len--; /* the last name doesn't have a dot */
2224 str = PyString_FromStringAndSize(NULL, len);
2225 if (!str)
2226 return NULL;
2227 s = PyString_AS_STRING(str);
2228 if (!s)
2229 return NULL;
2230 for (i = 0; i < NCH(n); i += 2) {
2231 char *sch = STR(CHILD(n, i));
2232 strcpy(s, STR(CHILD(n, i)));
2233 s += strlen(sch);
2234 *s++ = '.';
2235 }
2236 --s;
2237 *s = '\0';
2238 PyString_InternInPlace(&str);
Neal Norwitzd0421322006-09-05 04:00:12 +00002239 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002240 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 }
2242 break;
2243 case STAR:
Neal Norwitzd0421322006-09-05 04:00:12 +00002244 str = PyString_InternFromString("*");
2245 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002246 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002248 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 "unexpected import name: %d", TYPE(n));
2250 return NULL;
2251 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002252
2253 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 return NULL;
2255}
2256
2257static stmt_ty
2258ast_for_import_stmt(struct compiling *c, const node *n)
2259{
2260 /*
2261 import_stmt: import_name | import_from
2262 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002263 import_from: 'from' ('.'* dotted_name | '.') 'import'
2264 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002266 int lineno;
2267 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 int i;
2269 asdl_seq *aliases;
2270
2271 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002272 lineno = LINENO(n);
2273 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002275 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 n = CHILD(n, 1);
Neal Norwitzd0421322006-09-05 04:00:12 +00002277 REQ(n, dotted_as_names);
2278 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2279 if (!aliases)
2280 return NULL;
2281 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002282 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002283 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002285 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 }
Neal Norwitzd0421322006-09-05 04:00:12 +00002287 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002289 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 int n_children;
Neal Norwitzd0421322006-09-05 04:00:12 +00002291 int idx, ndots = 0;
2292 alias_ty mod = NULL;
2293 identifier modname;
2294
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002295 /* Count the number of dots (for relative imports) and check for the
2296 optional module name */
Neal Norwitzd0421322006-09-05 04:00:12 +00002297 for (idx = 1; idx < NCH(n); idx++) {
2298 if (TYPE(CHILD(n, idx)) == dotted_name) {
2299 mod = alias_for_import_name(c, CHILD(n, idx));
2300 idx++;
2301 break;
2302 } else if (TYPE(CHILD(n, idx)) != DOT) {
2303 break;
2304 }
2305 ndots++;
2306 }
2307 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002308 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002309 case STAR:
2310 /* from ... import * */
Neal Norwitzd0421322006-09-05 04:00:12 +00002311 n = CHILD(n, idx);
2312 n_children = 1;
2313 if (ndots) {
2314 ast_error(n, "'import *' not allowed with 'from .'");
2315 return NULL;
2316 }
2317 break;
2318 case LPAR:
2319 /* from ... import (x, y, z) */
2320 n = CHILD(n, idx + 1);
2321 n_children = NCH(n);
2322 break;
2323 case import_as_names:
2324 /* from ... import x, y, z */
2325 n = CHILD(n, idx);
2326 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00002327 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 ast_error(n, "trailing comma not allowed without"
2329 " surrounding parentheses");
2330 return NULL;
2331 }
Neal Norwitzd0421322006-09-05 04:00:12 +00002332 break;
2333 default:
2334 ast_error(n, "Unexpected node-type in from-import");
2335 return NULL;
2336 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337
Neal Norwitzd0421322006-09-05 04:00:12 +00002338 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2339 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341
2342 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002343 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002344 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002345 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002347 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002349 else {
Neal Norwitzd0421322006-09-05 04:00:12 +00002350 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00002351 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2352 if (!import_alias)
2353 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002354 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002355 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002357 if (mod != NULL)
2358 modname = mod->name;
2359 else
2360 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002361 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002362 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002363 }
Neal Norwitz79792652005-11-14 04:25:03 +00002364 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365 "unknown import statement: starts with command '%s'",
2366 STR(CHILD(n, 0)));
2367 return NULL;
2368}
2369
2370static stmt_ty
2371ast_for_global_stmt(struct compiling *c, const node *n)
2372{
2373 /* global_stmt: 'global' NAME (',' NAME)* */
2374 identifier name;
2375 asdl_seq *s;
2376 int i;
2377
2378 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002379 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380 if (!s)
Neal Norwitzd0421322006-09-05 04:00:12 +00002381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382 for (i = 1; i < NCH(n); i += 2) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002383 name = NEW_IDENTIFIER(CHILD(n, i));
2384 if (!name)
2385 return NULL;
2386 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002388 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389}
2390
2391static stmt_ty
2392ast_for_exec_stmt(struct compiling *c, const node *n)
2393{
2394 expr_ty expr1, globals = NULL, locals = NULL;
2395 int n_children = NCH(n);
2396 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002397 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 "poorly formed 'exec' statement: %d parts to statement",
2399 n_children);
2400 return NULL;
2401 }
2402
2403 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2404 REQ(n, exec_stmt);
2405 expr1 = ast_for_expr(c, CHILD(n, 1));
2406 if (!expr1)
2407 return NULL;
2408 if (n_children >= 4) {
2409 globals = ast_for_expr(c, CHILD(n, 3));
2410 if (!globals)
2411 return NULL;
2412 }
2413 if (n_children == 6) {
2414 locals = ast_for_expr(c, CHILD(n, 5));
2415 if (!locals)
2416 return NULL;
2417 }
2418
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002419 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420}
2421
2422static stmt_ty
2423ast_for_assert_stmt(struct compiling *c, const node *n)
2424{
2425 /* assert_stmt: 'assert' test [',' test] */
2426 REQ(n, assert_stmt);
2427 if (NCH(n) == 2) {
2428 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2429 if (!expression)
2430 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002431 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432 }
2433 else if (NCH(n) == 4) {
2434 expr_ty expr1, expr2;
2435
2436 expr1 = ast_for_expr(c, CHILD(n, 1));
2437 if (!expr1)
2438 return NULL;
2439 expr2 = ast_for_expr(c, CHILD(n, 3));
2440 if (!expr2)
2441 return NULL;
2442
Neal Norwitzd0421322006-09-05 04:00:12 +00002443 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 }
Neal Norwitz79792652005-11-14 04:25:03 +00002445 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 "improper number of parts to 'assert' statement: %d",
2447 NCH(n));
2448 return NULL;
2449}
2450
2451static asdl_seq *
2452ast_for_suite(struct compiling *c, const node *n)
2453{
2454 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002455 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 stmt_ty s;
2457 int i, total, num, end, pos = 0;
2458 node *ch;
2459
2460 REQ(n, suite);
2461
2462 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002463 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 if (!seq)
Neal Norwitzd0421322006-09-05 04:00:12 +00002465 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002467 n = CHILD(n, 0);
2468 /* simple_stmt always ends with a NEWLINE,
2469 and may have a trailing SEMI
2470 */
2471 end = NCH(n) - 1;
2472 if (TYPE(CHILD(n, end - 1)) == SEMI)
2473 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 /* loop by 2 to skip semi-colons */
Neal Norwitzd0421322006-09-05 04:00:12 +00002475 for (i = 0; i < end; i += 2) {
2476 ch = CHILD(n, i);
2477 s = ast_for_stmt(c, ch);
2478 if (!s)
2479 return NULL;
2480 asdl_seq_SET(seq, pos++, s);
2481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 }
2483 else {
Neal Norwitzd0421322006-09-05 04:00:12 +00002484 for (i = 2; i < (NCH(n) - 1); i++) {
2485 ch = CHILD(n, i);
2486 REQ(ch, stmt);
2487 num = num_stmts(ch);
2488 if (num == 1) {
2489 /* small_stmt or compound_stmt with only one child */
2490 s = ast_for_stmt(c, ch);
2491 if (!s)
2492 return NULL;
2493 asdl_seq_SET(seq, pos++, s);
2494 }
2495 else {
2496 int j;
2497 ch = CHILD(ch, 0);
2498 REQ(ch, simple_stmt);
2499 for (j = 0; j < NCH(ch); j += 2) {
2500 /* statement terminates with a semi-colon ';' */
2501 if (NCH(CHILD(ch, j)) == 0) {
2502 assert((j + 1) == NCH(ch));
2503 break;
2504 }
2505 s = ast_for_stmt(c, CHILD(ch, j));
2506 if (!s)
2507 return NULL;
2508 asdl_seq_SET(seq, pos++, s);
2509 }
2510 }
2511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 }
2513 assert(pos == seq->size);
2514 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515}
2516
2517static stmt_ty
2518ast_for_if_stmt(struct compiling *c, const node *n)
2519{
2520 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2521 ['else' ':' suite]
2522 */
2523 char *s;
2524
2525 REQ(n, if_stmt);
2526
2527 if (NCH(n) == 4) {
2528 expr_ty expression;
2529 asdl_seq *suite_seq;
2530
2531 expression = ast_for_expr(c, CHILD(n, 1));
2532 if (!expression)
2533 return NULL;
2534 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002535 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 return NULL;
2537
Neal Norwitzd0421322006-09-05 04:00:12 +00002538 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002540
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 s = STR(CHILD(n, 4));
2542 /* s[2], the third character in the string, will be
2543 's' for el_s_e, or
2544 'i' for el_i_f
2545 */
2546 if (s[2] == 's') {
2547 expr_ty expression;
2548 asdl_seq *seq1, *seq2;
2549
2550 expression = ast_for_expr(c, CHILD(n, 1));
2551 if (!expression)
2552 return NULL;
2553 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002554 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 return NULL;
2556 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002557 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 return NULL;
2559
Neal Norwitzd0421322006-09-05 04:00:12 +00002560 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
2562 else if (s[2] == 'i') {
Neal Norwitzd0421322006-09-05 04:00:12 +00002563 int i, n_elif, has_else = 0;
2564 asdl_seq *orelse = NULL;
2565 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 /* must reference the child n_elif+1 since 'else' token is third,
2567 not fourth, child from the end. */
Neal Norwitzd0421322006-09-05 04:00:12 +00002568 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2569 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2570 has_else = 1;
2571 n_elif -= 3;
2572 }
2573 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574
Neal Norwitzd0421322006-09-05 04:00:12 +00002575 if (has_else) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 expr_ty expression;
2577 asdl_seq *seq1, *seq2;
2578
Neal Norwitzd0421322006-09-05 04:00:12 +00002579 orelse = asdl_seq_new(1, c->c_arena);
2580 if (!orelse)
2581 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002583 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002586 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002589 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591
Neal Norwitzd0421322006-09-05 04:00:12 +00002592 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
2593 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002594 c->c_arena));
Neal Norwitzd0421322006-09-05 04:00:12 +00002595 /* the just-created orelse handled the last elif */
2596 n_elif--;
2597 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598
Neal Norwitzd0421322006-09-05 04:00:12 +00002599 for (i = 0; i < n_elif; i++) {
2600 int off = 5 + (n_elif - i - 1) * 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 expr_ty expression;
2602 asdl_seq *suite_seq;
Neal Norwitzd0421322006-09-05 04:00:12 +00002603 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2604 if (!newobj)
2605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002607 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002610 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612
Neal Norwitzd0421322006-09-05 04:00:12 +00002613 asdl_seq_SET(newobj, 0,
2614 If(expression, suite_seq, orelse,
2615 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
2616 orelse = newobj;
2617 }
2618 return If(ast_for_expr(c, CHILD(n, 1)),
2619 ast_for_suite(c, CHILD(n, 3)),
2620 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002622
2623 PyErr_Format(PyExc_SystemError,
2624 "unexpected token in 'if' statement: %s", s);
2625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626}
2627
2628static stmt_ty
2629ast_for_while_stmt(struct compiling *c, const node *n)
2630{
2631 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2632 REQ(n, while_stmt);
2633
2634 if (NCH(n) == 4) {
2635 expr_ty expression;
2636 asdl_seq *suite_seq;
2637
2638 expression = ast_for_expr(c, CHILD(n, 1));
2639 if (!expression)
2640 return NULL;
2641 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002642 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002644 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 }
2646 else if (NCH(n) == 7) {
2647 expr_ty expression;
2648 asdl_seq *seq1, *seq2;
2649
2650 expression = ast_for_expr(c, CHILD(n, 1));
2651 if (!expression)
2652 return NULL;
2653 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002654 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 return NULL;
2656 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002657 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 return NULL;
2659
Neal Norwitzd0421322006-09-05 04:00:12 +00002660 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002662
2663 PyErr_Format(PyExc_SystemError,
2664 "wrong number of tokens for 'while' statement: %d",
2665 NCH(n));
2666 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667}
2668
2669static stmt_ty
2670ast_for_for_stmt(struct compiling *c, const node *n)
2671{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002672 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 expr_ty expression;
2674 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002675 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2677 REQ(n, for_stmt);
2678
2679 if (NCH(n) == 9) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002680 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681 if (!seq)
2682 return NULL;
2683 }
2684
Neal Norwitzedef2be2006-07-12 05:26:17 +00002685 node_target = CHILD(n, 1);
2686 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002687 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002689 /* Check the # of children rather than the length of _target, since
2690 for x, in ... has 1 element in _target, but still requires a Tuple. */
2691 if (NCH(node_target) == 1)
Neal Norwitzd0421322006-09-05 04:00:12 +00002692 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 else
Neal Norwitzd0421322006-09-05 04:00:12 +00002694 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002696 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002697 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 return NULL;
2699 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002700 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 return NULL;
2702
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002703 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2704 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705}
2706
2707static excepthandler_ty
2708ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2709{
2710 /* except_clause: 'except' [test [',' test]] */
2711 REQ(exc, except_clause);
2712 REQ(body, suite);
2713
2714 if (NCH(exc) == 1) {
2715 asdl_seq *suite_seq = ast_for_suite(c, body);
2716 if (!suite_seq)
2717 return NULL;
2718
Neal Norwitzd0421322006-09-05 04:00:12 +00002719 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002720 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721 }
2722 else if (NCH(exc) == 2) {
2723 expr_ty expression;
2724 asdl_seq *suite_seq;
2725
2726 expression = ast_for_expr(c, CHILD(exc, 1));
2727 if (!expression)
2728 return NULL;
2729 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002730 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 return NULL;
2732
Neal Norwitzd0421322006-09-05 04:00:12 +00002733 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002734 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 }
2736 else if (NCH(exc) == 4) {
2737 asdl_seq *suite_seq;
2738 expr_ty expression;
Neal Norwitzd0421322006-09-05 04:00:12 +00002739 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2740 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002742 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 return NULL;
2744 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002745 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 return NULL;
2747 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002748 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 return NULL;
2750
Neal Norwitzd0421322006-09-05 04:00:12 +00002751 return excepthandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002752 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002754
2755 PyErr_Format(PyExc_SystemError,
2756 "wrong number of children for 'except' clause: %d",
2757 NCH(exc));
2758 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759}
2760
2761static stmt_ty
2762ast_for_try_stmt(struct compiling *c, const node *n)
2763{
Neal Norwitzf599f422005-12-17 21:33:47 +00002764 const int nch = NCH(n);
2765 int n_except = (nch - 3)/3;
2766 asdl_seq *body, *orelse = NULL, *finally = NULL;
2767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 REQ(n, try_stmt);
2769
Neal Norwitzf599f422005-12-17 21:33:47 +00002770 body = ast_for_suite(c, CHILD(n, 2));
2771 if (body == NULL)
2772 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773
Neal Norwitzf599f422005-12-17 21:33:47 +00002774 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2775 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2776 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2777 /* we can assume it's an "else",
2778 because nch >= 9 for try-else-finally and
2779 it would otherwise have a type of except_clause */
2780 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2781 if (orelse == NULL)
2782 return NULL;
2783 n_except--;
2784 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785
Neal Norwitzf599f422005-12-17 21:33:47 +00002786 finally = ast_for_suite(c, CHILD(n, nch - 1));
2787 if (finally == NULL)
2788 return NULL;
2789 n_except--;
2790 }
2791 else {
2792 /* we can assume it's an "else",
2793 otherwise it would have a type of except_clause */
2794 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2795 if (orelse == NULL)
2796 return NULL;
2797 n_except--;
2798 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002800 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002801 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 return NULL;
2803 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002804
2805 if (n_except > 0) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002806 int i;
2807 stmt_ty except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002808 /* process except statements to create a try ... except */
2809 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2810 if (handlers == NULL)
2811 return NULL;
2812
2813 for (i = 0; i < n_except; i++) {
2814 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2815 CHILD(n, 5 + i * 3));
2816 if (!e)
2817 return NULL;
2818 asdl_seq_SET(handlers, i, e);
2819 }
2820
Neal Norwitzd0421322006-09-05 04:00:12 +00002821 except_st = TryExcept(body, handlers, orelse, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002822 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002823 if (!finally)
Neal Norwitzd0421322006-09-05 04:00:12 +00002824 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002825
2826 /* if a 'finally' is present too, we nest the TryExcept within a
2827 TryFinally to emulate try ... except ... finally */
Neal Norwitzd0421322006-09-05 04:00:12 +00002828 body = asdl_seq_new(1, c->c_arena);
2829 if (body == NULL)
2830 return NULL;
2831 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002832 }
2833
2834 /* must be a try ... finally (except clauses are in body, if any exist) */
2835 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002836 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837}
2838
Guido van Rossumc2e20742006-02-27 22:32:47 +00002839static expr_ty
2840ast_for_with_var(struct compiling *c, const node *n)
2841{
2842 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002843 return ast_for_expr(c, CHILD(n, 1));
2844}
2845
2846/* with_stmt: 'with' test [ with_var ] ':' suite */
2847static stmt_ty
2848ast_for_with_stmt(struct compiling *c, const node *n)
2849{
2850 expr_ty context_expr, optional_vars = NULL;
2851 int suite_index = 3; /* skip 'with', test, and ':' */
2852 asdl_seq *suite_seq;
2853
2854 assert(TYPE(n) == with_stmt);
2855 context_expr = ast_for_expr(c, CHILD(n, 1));
2856 if (TYPE(CHILD(n, 2)) == with_var) {
2857 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2858
2859 if (!optional_vars) {
2860 return NULL;
2861 }
Neal Norwitzd0421322006-09-05 04:00:12 +00002862 if (!set_context(optional_vars, Store, n)) {
2863 return NULL;
2864 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002865 suite_index = 4;
2866 }
2867
2868 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2869 if (!suite_seq) {
2870 return NULL;
2871 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002872 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Neal Norwitzd0421322006-09-05 04:00:12 +00002873 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002874}
2875
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876static stmt_ty
2877ast_for_classdef(struct compiling *c, const node *n)
2878{
2879 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 asdl_seq *bases, *s;
2881
2882 REQ(n, classdef);
2883
2884 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002885 ast_error(n, "assignment to None");
2886 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 }
2888
2889 if (NCH(n) == 4) {
2890 s = ast_for_suite(c, CHILD(n, 3));
2891 if (!s)
2892 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002893 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002894 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 }
2896 /* check for empty base list */
2897 if (TYPE(CHILD(n,3)) == RPAR) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002898 s = ast_for_suite(c, CHILD(n,5));
2899 if (!s)
2900 return NULL;
2901 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002902 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 }
2904
2905 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002906 bases = ast_for_class_bases(c, CHILD(n, 3));
2907 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909
2910 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002911 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002913 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2914 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915}
2916
2917static stmt_ty
2918ast_for_stmt(struct compiling *c, const node *n)
2919{
2920 if (TYPE(n) == stmt) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002921 assert(NCH(n) == 1);
2922 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923 }
2924 if (TYPE(n) == simple_stmt) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002925 assert(num_stmts(n) == 1);
2926 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927 }
2928 if (TYPE(n) == small_stmt) {
Neal Norwitzd0421322006-09-05 04:00:12 +00002929 REQ(n, small_stmt);
2930 n = CHILD(n, 0);
2931 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2932 | flow_stmt | import_stmt | global_stmt | exec_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933 | assert_stmt
Neal Norwitzd0421322006-09-05 04:00:12 +00002934 */
2935 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 case expr_stmt:
2937 return ast_for_expr_stmt(c, n);
2938 case print_stmt:
2939 return ast_for_print_stmt(c, n);
2940 case del_stmt:
2941 return ast_for_del_stmt(c, n);
2942 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002943 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 case flow_stmt:
2945 return ast_for_flow_stmt(c, n);
2946 case import_stmt:
2947 return ast_for_import_stmt(c, n);
2948 case global_stmt:
2949 return ast_for_global_stmt(c, n);
2950 case exec_stmt:
2951 return ast_for_exec_stmt(c, n);
2952 case assert_stmt:
2953 return ast_for_assert_stmt(c, n);
2954 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002955 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2957 TYPE(n), NCH(n));
2958 return NULL;
2959 }
2960 }
2961 else {
2962 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Neal Norwitzd0421322006-09-05 04:00:12 +00002963 | funcdef | classdef
2964 */
2965 node *ch = CHILD(n, 0);
2966 REQ(n, compound_stmt);
2967 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 case if_stmt:
2969 return ast_for_if_stmt(c, ch);
2970 case while_stmt:
2971 return ast_for_while_stmt(c, ch);
2972 case for_stmt:
2973 return ast_for_for_stmt(c, ch);
2974 case try_stmt:
2975 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002976 case with_stmt:
2977 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978 case funcdef:
2979 return ast_for_funcdef(c, ch);
2980 case classdef:
2981 return ast_for_classdef(c, ch);
2982 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002983 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2985 TYPE(n), NCH(n));
2986 return NULL;
Neal Norwitzd0421322006-09-05 04:00:12 +00002987 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 }
2989}
2990
2991static PyObject *
2992parsenumber(const char *s)
2993{
Neal Norwitzd0421322006-09-05 04:00:12 +00002994 const char *end;
2995 long x;
2996 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997#ifndef WITHOUT_COMPLEX
Neal Norwitzd0421322006-09-05 04:00:12 +00002998 Py_complex c;
2999 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000#endif
3001
Neal Norwitzd0421322006-09-05 04:00:12 +00003002 errno = 0;
3003 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004#ifndef WITHOUT_COMPLEX
Neal Norwitzd0421322006-09-05 04:00:12 +00003005 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003007 if (*end == 'l' || *end == 'L')
3008 return PyLong_FromString((char *)s, (char **)0, 0);
3009 if (s[0] == '0') {
3010 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3011 if (x < 0 && errno == 0) {
3012 return PyLong_FromString((char *)s,
3013 (char **)0,
3014 0);
3015 }
3016 }
3017 else
3018 x = PyOS_strtol((char *)s, (char **)&end, 0);
3019 if (*end == '\0') {
3020 if (errno != 0)
3021 return PyLong_FromString((char *)s, (char **)0, 0);
3022 return PyInt_FromLong(x);
3023 }
3024 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025#ifndef WITHOUT_COMPLEX
Neal Norwitzd0421322006-09-05 04:00:12 +00003026 if (imflag) {
3027 c.real = 0.;
3028 PyFPE_START_PROTECT("atof", return 0)
3029 c.imag = PyOS_ascii_atof(s);
3030 PyFPE_END_PROTECT(c)
3031 return PyComplex_FromCComplex(c);
3032 }
3033 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003035 {
3036 PyFPE_START_PROTECT("atof", return 0)
3037 dx = PyOS_ascii_atof(s);
3038 PyFPE_END_PROTECT(dx)
3039 return PyFloat_FromDouble(dx);
3040 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041}
3042
3043static PyObject *
3044decode_utf8(const char **sPtr, const char *end, char* encoding)
3045{
3046#ifndef Py_USING_UNICODE
Neal Norwitzd0421322006-09-05 04:00:12 +00003047 Py_FatalError("decode_utf8 should not be called in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048 return NULL;
3049#else
Neal Norwitzd0421322006-09-05 04:00:12 +00003050 PyObject *u, *v;
3051 char *s, *t;
3052 t = s = (char *)*sPtr;
3053 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3054 while (s < end && (*s & 0x80)) s++;
3055 *sPtr = s;
3056 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3057 if (u == NULL)
3058 return NULL;
3059 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3060 Py_DECREF(u);
3061 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062#endif
3063}
3064
3065static PyObject *
3066decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3067{
Neal Norwitzd0421322006-09-05 04:00:12 +00003068 PyObject *v, *u;
3069 char *buf;
3070 char *p;
3071 const char *end;
3072 if (encoding == NULL) {
3073 buf = (char *)s;
3074 u = NULL;
3075 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3076 buf = (char *)s;
3077 u = NULL;
3078 } else {
3079 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3080 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3081 if (u == NULL)
3082 return NULL;
3083 p = buf = PyString_AsString(u);
3084 end = s + len;
3085 while (s < end) {
3086 if (*s == '\\') {
3087 *p++ = *s++;
3088 if (*s & 0x80) {
3089 strcpy(p, "u005c");
3090 p += 5;
3091 }
3092 }
3093 if (*s & 0x80) { /* XXX inefficient */
3094 PyObject *w;
3095 char *r;
3096 Py_ssize_t rn, i;
3097 w = decode_utf8(&s, end, "utf-16-be");
3098 if (w == NULL) {
3099 Py_DECREF(u);
3100 return NULL;
3101 }
3102 r = PyString_AsString(w);
3103 rn = PyString_Size(w);
3104 assert(rn % 2 == 0);
3105 for (i = 0; i < rn; i += 2) {
3106 sprintf(p, "\\u%02x%02x",
3107 r[i + 0] & 0xFF,
3108 r[i + 1] & 0xFF);
3109 p += 6;
3110 }
3111 Py_DECREF(w);
3112 } else {
3113 *p++ = *s++;
3114 }
3115 }
3116 len = p - buf;
3117 s = buf;
3118 }
3119 if (rawmode)
3120 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3121 else
3122 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3123 Py_XDECREF(u);
3124 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125}
3126
3127/* s is a Python string literal, including the bracketing quote characters,
3128 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3129 * parsestr parses it, and returns the decoded Python string object.
3130 */
3131static PyObject *
3132parsestr(const char *s, const char *encoding)
3133{
Neal Norwitzd0421322006-09-05 04:00:12 +00003134 size_t len;
3135 int quote = Py_CHARMASK(*s);
3136 int rawmode = 0;
3137 int need_encoding;
3138 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139
Neal Norwitzd0421322006-09-05 04:00:12 +00003140 if (isalpha(quote) || quote == '_') {
3141 if (quote == 'u' || quote == 'U') {
3142 quote = *++s;
3143 unicode = 1;
3144 }
3145 if (quote == 'r' || quote == 'R') {
3146 quote = *++s;
3147 rawmode = 1;
3148 }
3149 }
3150 if (quote != '\'' && quote != '\"') {
3151 PyErr_BadInternalCall();
3152 return NULL;
3153 }
3154 s++;
3155 len = strlen(s);
3156 if (len > INT_MAX) {
3157 PyErr_SetString(PyExc_OverflowError,
3158 "string to parse is too long");
3159 return NULL;
3160 }
3161 if (s[--len] != quote) {
3162 PyErr_BadInternalCall();
3163 return NULL;
3164 }
3165 if (len >= 4 && s[0] == quote && s[1] == quote) {
3166 s += 2;
3167 len -= 2;
3168 if (s[--len] != quote || s[--len] != quote) {
3169 PyErr_BadInternalCall();
3170 return NULL;
3171 }
3172 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173#ifdef Py_USING_UNICODE
Neal Norwitzd0421322006-09-05 04:00:12 +00003174 if (unicode || Py_UnicodeFlag) {
3175 return decode_unicode(s, len, rawmode, encoding);
3176 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003178 need_encoding = (encoding != NULL &&
3179 strcmp(encoding, "utf-8") != 0 &&
3180 strcmp(encoding, "iso-8859-1") != 0);
3181 if (rawmode || strchr(s, '\\') == NULL) {
3182 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183#ifndef Py_USING_UNICODE
Neal Norwitzd0421322006-09-05 04:00:12 +00003184 /* This should not happen - we never see any other
3185 encoding. */
3186 Py_FatalError(
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003187 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188#else
Neal Norwitzd0421322006-09-05 04:00:12 +00003189 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3190 if (u == NULL)
3191 return NULL;
3192 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3193 Py_DECREF(u);
3194 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003196 } else {
3197 return PyString_FromStringAndSize(s, len);
3198 }
3199 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200
Neal Norwitzd0421322006-09-05 04:00:12 +00003201 return PyString_DecodeEscape(s, len, NULL, unicode,
3202 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203}
3204
3205/* Build a Python string object out of a STRING atom. This takes care of
3206 * compile-time literal catenation, calling parsestr() on each piece, and
3207 * pasting the intermediate results together.
3208 */
3209static PyObject *
3210parsestrplus(struct compiling *c, const node *n)
3211{
Neal Norwitzd0421322006-09-05 04:00:12 +00003212 PyObject *v;
3213 int i;
3214 REQ(CHILD(n, 0), STRING);
3215 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3216 /* String literal concatenation */
3217 for (i = 1; i < NCH(n); i++) {
3218 PyObject *s;
3219 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3220 if (s == NULL)
3221 goto onError;
3222 if (PyString_Check(v) && PyString_Check(s)) {
3223 PyString_ConcatAndDel(&v, s);
3224 if (v == NULL)
3225 goto onError;
3226 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227#ifdef Py_USING_UNICODE
Neal Norwitzd0421322006-09-05 04:00:12 +00003228 else {
3229 PyObject *temp = PyUnicode_Concat(v, s);
3230 Py_DECREF(s);
3231 Py_DECREF(v);
3232 v = temp;
3233 if (v == NULL)
3234 goto onError;
3235 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236#endif
Neal Norwitzd0421322006-09-05 04:00:12 +00003237 }
3238 }
3239 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240
3241 onError:
Neal Norwitzd0421322006-09-05 04:00:12 +00003242 Py_XDECREF(v);
3243 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244}