blob: 5fbea395d85d989edc8ff60f4d6b02f7f186d2df [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/* XXX TO DO
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
21 - syntax errors
22*/
23
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024/* Data structure used internally */
25struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000026 char *c_encoding; /* source encoding */
27 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028};
29
30static asdl_seq *seq_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_expr(struct compiling *, const node *);
32static stmt_ty ast_for_stmt(struct compiling *, const node *);
33static asdl_seq *ast_for_suite(struct compiling *, const node *);
34static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 value = Py_BuildValue("(OO)", errstr, tmp);
111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194 } else if (TYPE(n) == encoding_decl) {
195 c.c_encoding = STR(n);
196 n = CHILD(n, 0);
197 } else {
198 c.c_encoding = NULL;
199 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000200 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
Jeremy Hyltona8293132006-02-28 17:58:27 +0000202 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203 switch (TYPE(n)) {
204 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000205 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206 if (!stmts)
207 return NULL;
208 for (i = 0; i < NCH(n) - 1; i++) {
209 ch = CHILD(n, i);
210 if (TYPE(ch) == NEWLINE)
211 continue;
212 REQ(ch, stmt);
213 num = num_stmts(ch);
214 if (num == 1) {
215 s = ast_for_stmt(&c, ch);
216 if (!s)
217 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000218 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219 }
220 else {
221 ch = CHILD(ch, 0);
222 REQ(ch, simple_stmt);
223 for (j = 0; j < num; j++) {
224 s = ast_for_stmt(&c, CHILD(ch, j * 2));
225 if (!s)
226 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000227 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 }
229 }
230 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000231 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 case eval_input: {
233 expr_ty testlist_ast;
234
235 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000236 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 if (!testlist_ast)
238 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000239 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 }
241 case single_input:
242 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 if (!stmts)
245 goto error;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000246 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 }
249 else {
250 n = CHILD(n, 0);
251 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 if (!stmts)
254 goto error;
255 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000256 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 if (!s)
258 goto error;
259 asdl_seq_SET(stmts, 0, s);
260 }
261 else {
262 /* Only a simple_stmt can contain multiple statements. */
263 REQ(n, simple_stmt);
264 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 if (TYPE(CHILD(n, i)) == NEWLINE)
266 break;
267 s = ast_for_stmt(&c, CHILD(n, i));
268 if (!s)
269 goto error;
270 asdl_seq_SET(stmts, i / 2, s);
271 }
272 }
273
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000274 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 }
276 default:
277 goto error;
278 }
279 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 ast_error_finish(filename);
281 return NULL;
282}
283
284/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
285*/
286
287static operator_ty
288get_operator(const node *n)
289{
290 switch (TYPE(n)) {
291 case VBAR:
292 return BitOr;
293 case CIRCUMFLEX:
294 return BitXor;
295 case AMPER:
296 return BitAnd;
297 case LEFTSHIFT:
298 return LShift;
299 case RIGHTSHIFT:
300 return RShift;
301 case PLUS:
302 return Add;
303 case MINUS:
304 return Sub;
305 case STAR:
306 return Mult;
307 case SLASH:
308 return Div;
309 case DOUBLESLASH:
310 return FloorDiv;
311 case PERCENT:
312 return Mod;
313 default:
314 return 0;
315 }
316}
317
Jeremy Hyltona8293132006-02-28 17:58:27 +0000318/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319
320 Only sets context for expr kinds that "can appear in assignment context"
321 (according to ../Parser/Python.asdl). For other expr kinds, it sets
322 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323*/
324
325static int
326set_context(expr_ty e, expr_context_ty ctx, const node *n)
327{
328 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000329 /* If a particular expression type can't be used for assign / delete,
330 set expr_name to its name and an error message will be generated.
331 */
332 const char* expr_name = NULL;
333
334 /* The ast defines augmented store and load contexts, but the
335 implementation here doesn't actually use them. The code may be
336 a little more complex than necessary as a result. It also means
337 that expressions in an augmented assignment have no context.
338 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000339 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000340 */
341 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342
343 switch (e->kind) {
344 case Attribute_kind:
345 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000346 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347 return ast_error(n, "assignment to None");
348 }
349 e->v.Attribute.ctx = ctx;
350 break;
351 case Subscript_kind:
352 e->v.Subscript.ctx = ctx;
353 break;
354 case Name_kind:
355 if (ctx == Store &&
356 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
357 return ast_error(n, "assignment to None");
358 }
359 e->v.Name.ctx = ctx;
360 break;
361 case List_kind:
362 e->v.List.ctx = ctx;
363 s = e->v.List.elts;
364 break;
365 case Tuple_kind:
366 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
367 return ast_error(n, "can't assign to ()");
368 e->v.Tuple.ctx = ctx;
369 s = e->v.Tuple.elts;
370 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000371 case Lambda_kind:
372 expr_name = "lambda";
373 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000375 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000377 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 case UnaryOp_kind:
380 expr_name = "operator";
381 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000383 expr_name = "generator expression";
384 break;
385 case ListComp_kind:
386 expr_name = "list comprehension";
387 break;
388 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389 case Num_kind:
390 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000391 expr_name = "literal";
392 break;
393 case Compare_kind:
394 expr_name = "comparison";
395 break;
396 case Repr_kind:
397 expr_name = "repr";
398 break;
399 default:
400 PyErr_Format(PyExc_SystemError,
401 "unexpected expression in assignment %d (line %d)",
402 e->kind, e->lineno);
403 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000405 /* Check for error string set by switch */
406 if (expr_name) {
407 char buf[300];
408 PyOS_snprintf(buf, sizeof(buf),
409 "can't %s %s",
410 ctx == Store ? "assign to" : "delete",
411 expr_name);
412 return ast_error(n, buf);
413 }
414
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000416 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417 */
418 if (s) {
419 int i;
420
421 for (i = 0; i < asdl_seq_LEN(s); i++) {
422 if (!set_context(asdl_seq_GET(s, i), ctx, n))
423 return 0;
424 }
425 }
426 return 1;
427}
428
429static operator_ty
430ast_for_augassign(const node *n)
431{
432 REQ(n, augassign);
433 n = CHILD(n, 0);
434 switch (STR(n)[0]) {
435 case '+':
436 return Add;
437 case '-':
438 return Sub;
439 case '/':
440 if (STR(n)[1] == '/')
441 return FloorDiv;
442 else
443 return Div;
444 case '%':
445 return Mod;
446 case '<':
447 return LShift;
448 case '>':
449 return RShift;
450 case '&':
451 return BitAnd;
452 case '^':
453 return BitXor;
454 case '|':
455 return BitOr;
456 case '*':
457 if (STR(n)[1] == '*')
458 return Pow;
459 else
460 return Mult;
461 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000462 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 return 0;
464 }
465}
466
467static cmpop_ty
468ast_for_comp_op(const node *n)
469{
470 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
471 |'is' 'not'
472 */
473 REQ(n, comp_op);
474 if (NCH(n) == 1) {
475 n = CHILD(n, 0);
476 switch (TYPE(n)) {
477 case LESS:
478 return Lt;
479 case GREATER:
480 return Gt;
481 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 return Eq;
483 case LESSEQUAL:
484 return LtE;
485 case GREATEREQUAL:
486 return GtE;
487 case NOTEQUAL:
488 return NotEq;
489 case NAME:
490 if (strcmp(STR(n), "in") == 0)
491 return In;
492 if (strcmp(STR(n), "is") == 0)
493 return Is;
494 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000495 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 STR(n));
497 return 0;
498 }
499 }
500 else if (NCH(n) == 2) {
501 /* handle "not in" and "is not" */
502 switch (TYPE(CHILD(n, 0))) {
503 case NAME:
504 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
505 return NotIn;
506 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
507 return IsNot;
508 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000509 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
511 return 0;
512 }
513 }
Neal Norwitz79792652005-11-14 04:25:03 +0000514 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 NCH(n));
516 return 0;
517}
518
519static asdl_seq *
520seq_for_testlist(struct compiling *c, const node *n)
521{
522 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000523 asdl_seq *seq;
524 expr_ty expression;
525 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 assert(TYPE(n) == testlist
527 || TYPE(n) == listmaker
528 || TYPE(n) == testlist_gexp
529 || TYPE(n) == testlist_safe
530 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000532 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533 if (!seq)
534 return NULL;
535
536 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000537 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
539 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000540 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
543 assert(i / 2 < seq->size);
544 asdl_seq_SET(seq, i / 2, expression);
545 }
546 return seq;
547}
548
549static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000550compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551{
552 int i, len = (NCH(n) + 1) / 2;
553 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000554 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 if (!args)
556 return NULL;
557
558 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559 for (i = 0; i < len; i++) {
560 const node *child = CHILD(CHILD(n, 2*i), 0);
561 expr_ty arg;
562 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000563 if (!strcmp(STR(child), "None")) {
564 ast_error(child, "assignment to None");
565 return NULL;
566 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000567 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child), child->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000568 c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000569 }
570 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000571 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000572 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 asdl_seq_SET(args, i, arg);
574 }
575
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000576 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000577 if (!set_context(result, Store, n))
578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579 return result;
580}
581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582
Jeremy Hyltona8293132006-02-28 17:58:27 +0000583/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
585static arguments_ty
586ast_for_arguments(struct compiling *c, const node *n)
587{
588 /* parameters: '(' [varargslist] ')'
589 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
590 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
591 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000592 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 asdl_seq *args, *defaults;
594 identifier vararg = NULL, kwarg = NULL;
595 node *ch;
596
597 if (TYPE(n) == parameters) {
598 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000599 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 n = CHILD(n, 1);
601 }
602 REQ(n, varargslist);
603
604 /* first count the number of normal args & defaults */
605 for (i = 0; i < NCH(n); i++) {
606 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000607 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 if (TYPE(ch) == EQUAL)
610 n_defaults++;
611 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000612 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 if (!args && n_args)
614 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000615 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 if (!defaults && n_defaults)
617 goto error;
618
619 /* fpdef: NAME | '(' fplist ')'
620 fplist: fpdef (',' fpdef)* [',']
621 */
622 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000623 j = 0; /* index for defaults */
624 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 while (i < NCH(n)) {
626 ch = CHILD(n, i);
627 switch (TYPE(ch)) {
628 case fpdef:
629 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
630 anything other than EQUAL or a comma? */
631 /* XXX Should NCH(n) check be made a separate check? */
632 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000633 asdl_seq_SET(defaults, j++,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 ast_for_expr(c, CHILD(n, i + 2)));
635 i += 2;
636 found_default = 1;
637 }
638 else if (found_default) {
639 ast_error(n,
640 "non-default argument follows default argument");
641 goto error;
642 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 if (NCH(ch) == 3) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000644 asdl_seq_SET(args, k++,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000645 compiler_complex_args(c, CHILD(ch, 1)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 }
647 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000648 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
650 ast_error(CHILD(ch, 0), "assignment to None");
651 goto error;
652 }
Armin Rigo31441302005-10-21 12:57:31 +0000653 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000654 Param, LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 if (!name)
656 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000657 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
659 }
660 i += 2; /* the name and the comma */
661 break;
662 case STAR:
663 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
664 ast_error(CHILD(n, i+1), "assignment to None");
665 goto error;
666 }
667 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
668 i += 3;
669 break;
670 case DOUBLESTAR:
671 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
672 ast_error(CHILD(n, i+1), "assignment to None");
673 goto error;
674 }
675 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
676 i += 3;
677 break;
678 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000679 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 "unexpected node in varargslist: %d @ %d",
681 TYPE(ch), i);
682 goto error;
683 }
684 }
685
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000686 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687
688 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000689 Py_XDECREF(vararg);
690 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 return NULL;
692}
693
694static expr_ty
695ast_for_dotted_name(struct compiling *c, const node *n)
696{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000697 expr_ty e;
698 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000699 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 int i;
701
702 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000703
704 lineno = LINENO(n);
705 col_offset = n->n_col_offset;
706
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 id = NEW_IDENTIFIER(CHILD(n, 0));
708 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000709 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000710 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000712 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713
714 for (i = 2; i < NCH(n); i+=2) {
715 id = NEW_IDENTIFIER(CHILD(n, i));
716 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000717 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000718 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000719 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000720 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 }
722
723 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724}
725
726static expr_ty
727ast_for_decorator(struct compiling *c, const node *n)
728{
729 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
730 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000731 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732
733 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000734 REQ(CHILD(n, 0), AT);
735 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
737 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
738 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740
741 if (NCH(n) == 3) { /* No arguments */
742 d = name_expr;
743 name_expr = NULL;
744 }
745 else if (NCH(n) == 5) { /* Call with no arguments */
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000746 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 name_expr = NULL;
750 }
751 else {
752 d = ast_for_call(c, CHILD(n, 3), name_expr);
753 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000754 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 name_expr = NULL;
756 }
757
758 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759}
760
761static asdl_seq*
762ast_for_decorators(struct compiling *c, const node *n)
763{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000764 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000765 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 int i;
767
768 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000769 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 if (!decorator_seq)
771 return NULL;
772
773 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000774 d = ast_for_decorator(c, CHILD(n, i));
775 if (!d)
776 return NULL;
777 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 }
779 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780}
781
782static stmt_ty
783ast_for_funcdef(struct compiling *c, const node *n)
784{
785 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000786 identifier name;
787 arguments_ty args;
788 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 asdl_seq *decorator_seq = NULL;
790 int name_i;
791
792 REQ(n, funcdef);
793
794 if (NCH(n) == 6) { /* decorators are present */
795 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
796 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000797 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 name_i = 2;
799 }
800 else {
801 name_i = 1;
802 }
803
804 name = NEW_IDENTIFIER(CHILD(n, name_i));
805 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000806 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000808 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811 args = ast_for_arguments(c, CHILD(n, name_i + 1));
812 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 body = ast_for_suite(c, CHILD(n, name_i + 3));
815 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000818 return FunctionDef(name, args, body, decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819}
820
821static expr_ty
822ast_for_lambdef(struct compiling *c, const node *n)
823{
824 /* lambdef: 'lambda' [varargslist] ':' test */
825 arguments_ty args;
826 expr_ty expression;
827
828 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000829 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 if (!args)
831 return NULL;
832 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 }
836 else {
837 args = ast_for_arguments(c, CHILD(n, 1));
838 if (!args)
839 return NULL;
840 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000841 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 }
844
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000845 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846}
847
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000848static expr_ty
849ast_for_ifexpr(struct compiling *c, const node *n)
850{
851 /* test: or_test 'if' or_test 'else' test */
852 expr_ty expression, body, orelse;
853
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000854 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000855 body = ast_for_expr(c, CHILD(n, 0));
856 if (!body)
857 return NULL;
858 expression = ast_for_expr(c, CHILD(n, 2));
859 if (!expression)
860 return NULL;
861 orelse = ast_for_expr(c, CHILD(n, 4));
862 if (!orelse)
863 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000864 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000865}
866
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867/* Count the number of 'for' loop in a list comprehension.
868
869 Helper for ast_for_listcomp().
870*/
871
872static int
873count_list_fors(const node *n)
874{
875 int n_fors = 0;
876 node *ch = CHILD(n, 1);
877
878 count_list_for:
879 n_fors++;
880 REQ(ch, list_for);
881 if (NCH(ch) == 5)
882 ch = CHILD(ch, 4);
883 else
884 return n_fors;
885 count_list_iter:
886 REQ(ch, list_iter);
887 ch = CHILD(ch, 0);
888 if (TYPE(ch) == list_for)
889 goto count_list_for;
890 else if (TYPE(ch) == list_if) {
891 if (NCH(ch) == 3) {
892 ch = CHILD(ch, 2);
893 goto count_list_iter;
894 }
895 else
896 return n_fors;
897 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000898
899 /* Should never be reached */
900 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
901 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902}
903
904/* Count the number of 'if' statements in a list comprehension.
905
906 Helper for ast_for_listcomp().
907*/
908
909static int
910count_list_ifs(const node *n)
911{
912 int n_ifs = 0;
913
914 count_list_iter:
915 REQ(n, list_iter);
916 if (TYPE(CHILD(n, 0)) == list_for)
917 return n_ifs;
918 n = CHILD(n, 0);
919 REQ(n, list_if);
920 n_ifs++;
921 if (NCH(n) == 2)
922 return n_ifs;
923 n = CHILD(n, 2);
924 goto count_list_iter;
925}
926
927static expr_ty
928ast_for_listcomp(struct compiling *c, const node *n)
929{
930 /* listmaker: test ( list_for | (',' test)* [','] )
931 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
932 list_iter: list_for | list_if
933 list_if: 'if' test [list_iter]
934 testlist_safe: test [(',' test)+ [',']]
935 */
936 expr_ty elt;
937 asdl_seq *listcomps;
938 int i, n_fors;
939 node *ch;
940
941 REQ(n, listmaker);
942 assert(NCH(n) > 1);
943
944 elt = ast_for_expr(c, CHILD(n, 0));
945 if (!elt)
946 return NULL;
947
948 n_fors = count_list_fors(n);
949 if (n_fors == -1)
950 return NULL;
951
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000952 listcomps = asdl_seq_new(n_fors, c->c_arena);
953 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 ch = CHILD(n, 1);
957 for (i = 0; i < n_fors; i++) {
958 comprehension_ty lc;
959 asdl_seq *t;
960 expr_ty expression;
961
962 REQ(ch, list_for);
963
964 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000965 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000967 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000968 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000971 if (asdl_seq_LEN(t) == 1)
972 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
973 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000975 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976 expression, NULL, c->c_arena);
977 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979
980 if (NCH(ch) == 5) {
981 int j, n_ifs;
982 asdl_seq *ifs;
983
984 ch = CHILD(ch, 4);
985 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000986 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000989 ifs = asdl_seq_new(n_ifs, c->c_arena);
990 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992
993 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000994 REQ(ch, list_iter);
995 ch = CHILD(ch, 0);
996 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
Jeremy Hyltona8293132006-02-28 17:58:27 +0000998 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
999 if (NCH(ch) == 3)
1000 ch = CHILD(ch, 2);
1001 }
1002 /* on exit, must guarantee that ch is a list_for */
1003 if (TYPE(ch) == list_iter)
1004 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001006 }
1007 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 }
1009
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001010 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011}
1012
1013/*
1014 Count the number of 'for' loops in a generator expression.
1015
1016 Helper for ast_for_genexp().
1017*/
1018
1019static int
1020count_gen_fors(const node *n)
1021{
1022 int n_fors = 0;
1023 node *ch = CHILD(n, 1);
1024
1025 count_gen_for:
1026 n_fors++;
1027 REQ(ch, gen_for);
1028 if (NCH(ch) == 5)
1029 ch = CHILD(ch, 4);
1030 else
1031 return n_fors;
1032 count_gen_iter:
1033 REQ(ch, gen_iter);
1034 ch = CHILD(ch, 0);
1035 if (TYPE(ch) == gen_for)
1036 goto count_gen_for;
1037 else if (TYPE(ch) == gen_if) {
1038 if (NCH(ch) == 3) {
1039 ch = CHILD(ch, 2);
1040 goto count_gen_iter;
1041 }
1042 else
1043 return n_fors;
1044 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001045
1046 /* Should never be reached */
1047 PyErr_SetString(PyExc_SystemError,
1048 "logic error in count_gen_fors");
1049 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050}
1051
1052/* Count the number of 'if' statements in a generator expression.
1053
1054 Helper for ast_for_genexp().
1055*/
1056
1057static int
1058count_gen_ifs(const node *n)
1059{
1060 int n_ifs = 0;
1061
1062 while (1) {
1063 REQ(n, gen_iter);
1064 if (TYPE(CHILD(n, 0)) == gen_for)
1065 return n_ifs;
1066 n = CHILD(n, 0);
1067 REQ(n, gen_if);
1068 n_ifs++;
1069 if (NCH(n) == 2)
1070 return n_ifs;
1071 n = CHILD(n, 2);
1072 }
1073}
1074
Jeremy Hyltona8293132006-02-28 17:58:27 +00001075/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076static expr_ty
1077ast_for_genexp(struct compiling *c, const node *n)
1078{
1079 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1080 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1081 expr_ty elt;
1082 asdl_seq *genexps;
1083 int i, n_fors;
1084 node *ch;
1085
1086 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1087 assert(NCH(n) > 1);
1088
1089 elt = ast_for_expr(c, CHILD(n, 0));
1090 if (!elt)
1091 return NULL;
1092
1093 n_fors = count_gen_fors(n);
1094 if (n_fors == -1)
1095 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001096
1097 genexps = asdl_seq_new(n_fors, c->c_arena);
1098 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001100
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101 ch = CHILD(n, 1);
1102 for (i = 0; i < n_fors; i++) {
1103 comprehension_ty ge;
1104 asdl_seq *t;
1105 expr_ty expression;
1106
1107 REQ(ch, gen_for);
1108
1109 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001110 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001112 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001113 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001115
1116 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001118 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001120 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001121 expression, NULL, c->c_arena);
1122
1123 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 if (NCH(ch) == 5) {
1127 int j, n_ifs;
1128 asdl_seq *ifs;
1129
1130 ch = CHILD(ch, 4);
1131 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001132 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001134
1135 ifs = asdl_seq_new(n_ifs, c->c_arena);
1136 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001138
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 for (j = 0; j < n_ifs; j++) {
1140 REQ(ch, gen_iter);
1141 ch = CHILD(ch, 0);
1142 REQ(ch, gen_if);
1143
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001144 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001145 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001146 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001147 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 if (NCH(ch) == 3)
1149 ch = CHILD(ch, 2);
1150 }
1151 /* on exit, must guarantee that ch is a gen_for */
1152 if (TYPE(ch) == gen_iter)
1153 ch = CHILD(ch, 0);
1154 ge->ifs = ifs;
1155 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001156 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 }
1158
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001159 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160}
1161
1162static expr_ty
1163ast_for_atom(struct compiling *c, const node *n)
1164{
1165 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1166 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1167 */
1168 node *ch = CHILD(n, 0);
1169
1170 switch (TYPE(ch)) {
1171 case NAME:
1172 /* All names start in Load context, but may later be
1173 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001174 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 case STRING: {
1176 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 if (!str)
1178 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001179
1180 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001181 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 }
1183 case NUMBER: {
1184 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 if (!pynum)
1186 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187
1188 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001189 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 }
1191 case LPAR: /* some parenthesized expressions */
1192 ch = CHILD(n, 1);
1193
1194 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001195 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196
1197 if (TYPE(ch) == yield_expr)
1198 return ast_for_expr(c, ch);
1199
1200 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1201 return ast_for_genexp(c, ch);
1202
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001203 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 case LSQB: /* list (or list comprehension) */
1205 ch = CHILD(n, 1);
1206
1207 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001208 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209
1210 REQ(ch, listmaker);
1211 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1212 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 if (!elts)
1214 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001215
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001216 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 }
1218 else
1219 return ast_for_listcomp(c, ch);
1220 case LBRACE: {
1221 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1222 int i, size;
1223 asdl_seq *keys, *values;
1224
1225 ch = CHILD(n, 1);
1226 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001227 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228 if (!keys)
1229 return NULL;
1230
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001231 values = asdl_seq_new(size, c->c_arena);
1232 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234
1235 for (i = 0; i < NCH(ch); i += 4) {
1236 expr_ty expression;
1237
1238 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001243
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001245 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001247
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 asdl_seq_SET(values, i / 4, expression);
1249 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001250 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 }
1252 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001253 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 if (!expression)
1255 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001256
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001257 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 }
1259 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001260 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 return NULL;
1262 }
1263}
1264
1265static slice_ty
1266ast_for_slice(struct compiling *c, const node *n)
1267{
1268 node *ch;
1269 expr_ty lower = NULL, upper = NULL, step = NULL;
1270
1271 REQ(n, subscript);
1272
1273 /*
1274 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1275 sliceop: ':' [test]
1276 */
1277 ch = CHILD(n, 0);
1278 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001279 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280
1281 if (NCH(n) == 1 && TYPE(ch) == test) {
1282 /* 'step' variable hold no significance in terms of being used over
1283 other vars */
1284 step = ast_for_expr(c, ch);
1285 if (!step)
1286 return NULL;
1287
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001288 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 }
1290
1291 if (TYPE(ch) == test) {
1292 lower = ast_for_expr(c, ch);
1293 if (!lower)
1294 return NULL;
1295 }
1296
1297 /* If there's an upper bound it's in the second or third position. */
1298 if (TYPE(ch) == COLON) {
1299 if (NCH(n) > 1) {
1300 node *n2 = CHILD(n, 1);
1301
1302 if (TYPE(n2) == test) {
1303 upper = ast_for_expr(c, n2);
1304 if (!upper)
1305 return NULL;
1306 }
1307 }
1308 } else if (NCH(n) > 2) {
1309 node *n2 = CHILD(n, 2);
1310
1311 if (TYPE(n2) == test) {
1312 upper = ast_for_expr(c, n2);
1313 if (!upper)
1314 return NULL;
1315 }
1316 }
1317
1318 ch = CHILD(n, NCH(n) - 1);
1319 if (TYPE(ch) == sliceop) {
1320 if (NCH(ch) == 1)
1321 /* XXX: If only 1 child, then should just be a colon. Should we
1322 just skip assigning and just get to the return? */
1323 ch = CHILD(ch, 0);
1324 else
1325 ch = CHILD(ch, 1);
1326 if (TYPE(ch) == test) {
1327 step = ast_for_expr(c, ch);
1328 if (!step)
1329 return NULL;
1330 }
1331 }
1332
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001333 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334}
1335
1336static expr_ty
1337ast_for_binop(struct compiling *c, const node *n)
1338{
1339 /* Must account for a sequence of expressions.
1340 How should A op B op C by represented?
1341 BinOp(BinOp(A, op, B), op, C).
1342 */
1343
1344 int i, nops;
1345 expr_ty expr1, expr2, result;
1346 operator_ty operator;
1347
1348 expr1 = ast_for_expr(c, CHILD(n, 0));
1349 if (!expr1)
1350 return NULL;
1351
1352 expr2 = ast_for_expr(c, CHILD(n, 2));
1353 if (!expr2)
1354 return NULL;
1355
1356 operator = get_operator(CHILD(n, 1));
1357 if (!operator)
1358 return NULL;
1359
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001360 result = BinOp(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 if (!result)
1362 return NULL;
1363
1364 nops = (NCH(n) - 1) / 2;
1365 for (i = 1; i < nops; i++) {
1366 expr_ty tmp_result, tmp;
1367 const node* next_oper = CHILD(n, i * 2 + 1);
1368
1369 operator = get_operator(next_oper);
1370 if (!operator)
1371 return NULL;
1372
1373 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1374 if (!tmp)
1375 return NULL;
1376
1377 tmp_result = BinOp(result, operator, tmp,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001378 LINENO(next_oper), next_oper->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 if (!tmp)
1380 return NULL;
1381 result = tmp_result;
1382 }
1383 return result;
1384}
1385
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001386static expr_ty
1387ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1388{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001389 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1390 subscriptlist: subscript (',' subscript)* [',']
1391 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1392 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001393 REQ(n, trailer);
1394 if (TYPE(CHILD(n, 0)) == LPAR) {
1395 if (NCH(n) == 2)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001396 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001397 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001398 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001399 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001400 else if (TYPE(CHILD(n, 0)) == DOT ) {
1401 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001402 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001403 }
1404 else {
1405 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001406 REQ(CHILD(n, 2), RSQB);
1407 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001408 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001409 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1410 if (!slc)
1411 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001412 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001413 }
1414 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001415 /* The grammar is ambiguous here. The ambiguity is resolved
1416 by treating the sequence as a tuple literal if there are
1417 no slice features.
1418 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001419 int j;
1420 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001421 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001422 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001423 asdl_seq *slices, *elts;
1424 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001425 if (!slices)
1426 return NULL;
1427 for (j = 0; j < NCH(n); j += 2) {
1428 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001429 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001430 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001431 if (slc->kind != Index_kind)
1432 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001433 asdl_seq_SET(slices, j / 2, slc);
1434 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001435 if (!simple) {
1436 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001437 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001438 }
1439 /* extract Index values and put them in a Tuple */
1440 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1441 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1442 slc = (slice_ty)asdl_seq_GET(slices, j);
1443 assert(slc->kind == Index_kind && slc->v.Index.value);
1444 asdl_seq_SET(elts, j, slc->v.Index.value);
1445 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001446 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001447 if (!e)
1448 return NULL;
1449 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001450 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001451 }
1452 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001453}
1454
1455static expr_ty
1456ast_for_power(struct compiling *c, const node *n)
1457{
1458 /* power: atom trailer* ('**' factor)*
1459 */
1460 int i;
1461 expr_ty e, tmp;
1462 REQ(n, power);
1463 e = ast_for_atom(c, CHILD(n, 0));
1464 if (!e)
1465 return NULL;
1466 if (NCH(n) == 1)
1467 return e;
1468 for (i = 1; i < NCH(n); i++) {
1469 node *ch = CHILD(n, i);
1470 if (TYPE(ch) != trailer)
1471 break;
1472 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001473 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001474 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001475 tmp->lineno = e->lineno;
1476 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001477 e = tmp;
1478 }
1479 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1480 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001481 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001482 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001483 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001484 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001485 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001486 e = tmp;
1487 }
1488 return e;
1489}
1490
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491/* Do not name a variable 'expr'! Will cause a compile error.
1492*/
1493
1494static expr_ty
1495ast_for_expr(struct compiling *c, const node *n)
1496{
1497 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001498 test: or_test ['if' or_test 'else' test] | lambdef
1499 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 and_test: not_test ('and' not_test)*
1501 not_test: 'not' not_test | comparison
1502 comparison: expr (comp_op expr)*
1503 expr: xor_expr ('|' xor_expr)*
1504 xor_expr: and_expr ('^' and_expr)*
1505 and_expr: shift_expr ('&' shift_expr)*
1506 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1507 arith_expr: term (('+'|'-') term)*
1508 term: factor (('*'|'/'|'%'|'//') factor)*
1509 factor: ('+'|'-'|'~') factor | power
1510 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001511
1512 As well as modified versions that exist for backward compatibility,
1513 to explicitly allow:
1514 [ x for x in lambda: 0, lambda: 1 ]
1515 (which would be ambiguous without these extra rules)
1516
1517 old_test: or_test | old_lambdef
1518 old_lambdef: 'lambda' [vararglist] ':' old_test
1519
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520 */
1521
1522 asdl_seq *seq;
1523 int i;
1524
1525 loop:
1526 switch (TYPE(n)) {
1527 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001528 case old_test:
1529 if (TYPE(CHILD(n, 0)) == lambdef ||
1530 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001532 else if (NCH(n) > 1)
1533 return ast_for_ifexpr(c, n);
1534 /* Fallthrough */
1535 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 case and_test:
1537 if (NCH(n) == 1) {
1538 n = CHILD(n, 0);
1539 goto loop;
1540 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001541 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542 if (!seq)
1543 return NULL;
1544 for (i = 0; i < NCH(n); i += 2) {
1545 expr_ty e = ast_for_expr(c, CHILD(n, i));
1546 if (!e)
1547 return NULL;
1548 asdl_seq_SET(seq, i / 2, e);
1549 }
1550 if (!strcmp(STR(CHILD(n, 1)), "and"))
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001551 return BoolOp(And, seq, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001552 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001553 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 case not_test:
1555 if (NCH(n) == 1) {
1556 n = CHILD(n, 0);
1557 goto loop;
1558 }
1559 else {
1560 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1561 if (!expression)
1562 return NULL;
1563
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001564 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 }
1566 case comparison:
1567 if (NCH(n) == 1) {
1568 n = CHILD(n, 0);
1569 goto loop;
1570 }
1571 else {
1572 expr_ty expression;
1573 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001574 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 if (!ops)
1576 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001577 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 return NULL;
1580 }
1581 for (i = 1; i < NCH(n); i += 2) {
1582 /* XXX cmpop_ty is just an enum */
1583 cmpop_ty operator;
1584
1585 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001586 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001588 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589
1590 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001591 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001593 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001595 asdl_seq_SET(ops, i / 2, (void *)(Py_uintptr_t)operator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 asdl_seq_SET(cmps, i / 2, expression);
1597 }
1598 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001599 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001601 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001603 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604 }
1605 break;
1606
1607 /* The next five cases all handle BinOps. The main body of code
1608 is the same in each case, but the switch turned inside out to
1609 reuse the code for each type of operator.
1610 */
1611 case expr:
1612 case xor_expr:
1613 case and_expr:
1614 case shift_expr:
1615 case arith_expr:
1616 case term:
1617 if (NCH(n) == 1) {
1618 n = CHILD(n, 0);
1619 goto loop;
1620 }
1621 return ast_for_binop(c, n);
1622 case yield_expr: {
1623 expr_ty exp = NULL;
1624 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001625 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 if (!exp)
1627 return NULL;
1628 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001629 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 }
1631 case factor: {
1632 expr_ty expression;
1633
1634 if (NCH(n) == 1) {
1635 n = CHILD(n, 0);
1636 goto loop;
1637 }
1638
1639 expression = ast_for_expr(c, CHILD(n, 1));
1640 if (!expression)
1641 return NULL;
1642
1643 switch (TYPE(CHILD(n, 0))) {
1644 case PLUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001645 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646 case MINUS:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001647 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 case TILDE:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001649 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001651 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1652 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 break;
1654 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001655 case power:
1656 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001658 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 return NULL;
1660 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001661 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 return NULL;
1663}
1664
1665static expr_ty
1666ast_for_call(struct compiling *c, const node *n, expr_ty func)
1667{
1668 /*
1669 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1670 | '**' test)
1671 argument: [test '='] test [gen_for] # Really [keyword '='] test
1672 */
1673
1674 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001675 asdl_seq *args;
1676 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 expr_ty vararg = NULL, kwarg = NULL;
1678
1679 REQ(n, arglist);
1680
1681 nargs = 0;
1682 nkeywords = 0;
1683 ngens = 0;
1684 for (i = 0; i < NCH(n); i++) {
1685 node *ch = CHILD(n, i);
1686 if (TYPE(ch) == argument) {
1687 if (NCH(ch) == 1)
1688 nargs++;
1689 else if (TYPE(CHILD(ch, 1)) == gen_for)
1690 ngens++;
1691 else
1692 nkeywords++;
1693 }
1694 }
1695 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001696 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 "if not sole argument");
1698 return NULL;
1699 }
1700
1701 if (nargs + nkeywords + ngens > 255) {
1702 ast_error(n, "more than 255 arguments");
1703 return NULL;
1704 }
1705
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001706 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001708 return NULL;
1709 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001711 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 nargs = 0;
1713 nkeywords = 0;
1714 for (i = 0; i < NCH(n); i++) {
1715 node *ch = CHILD(n, i);
1716 if (TYPE(ch) == argument) {
1717 expr_ty e;
1718 if (NCH(ch) == 1) {
1719 e = ast_for_expr(c, CHILD(ch, 0));
1720 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 asdl_seq_SET(args, nargs++, e);
1723 }
1724 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1725 e = ast_for_genexp(c, ch);
1726 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001727 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 asdl_seq_SET(args, nargs++, e);
1729 }
1730 else {
1731 keyword_ty kw;
1732 identifier key;
1733
1734 /* CHILD(ch, 0) is test, but must be an identifier? */
1735 e = ast_for_expr(c, CHILD(ch, 0));
1736 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 /* f(lambda x: x[0] = 3) ends up getting parsed with
1739 * LHS test = lambda x: x[0], and RHS test = 3.
1740 * SF bug 132313 points out that complaining about a keyword
1741 * then is very confusing.
1742 */
1743 if (e->kind == Lambda_kind) {
1744 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001745 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 } else if (e->kind != Name_kind) {
1747 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 }
1750 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751 e = ast_for_expr(c, CHILD(ch, 2));
1752 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001753 return NULL;
1754 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001756 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 asdl_seq_SET(keywords, nkeywords++, kw);
1758 }
1759 }
1760 else if (TYPE(ch) == STAR) {
1761 vararg = ast_for_expr(c, CHILD(n, i+1));
1762 i++;
1763 }
1764 else if (TYPE(ch) == DOUBLESTAR) {
1765 kwarg = ast_for_expr(c, CHILD(n, i+1));
1766 i++;
1767 }
1768 }
1769
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001770 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771}
1772
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001774ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001776 /* testlist_gexp: test (',' test)* [','] */
1777 /* testlist: test (',' test)* [','] */
1778 /* testlist_safe: test (',' test)+ [','] */
1779 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001781 if (TYPE(n) == testlist_gexp) {
1782 if (NCH(n) > 1)
1783 assert(TYPE(CHILD(n, 1)) != gen_for);
1784 }
1785 else {
1786 assert(TYPE(n) == testlist ||
1787 TYPE(n) == testlist_safe ||
1788 TYPE(n) == testlist1);
1789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 if (NCH(n) == 1)
1791 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 else {
1793 asdl_seq *tmp = seq_for_testlist(c, n);
1794 if (!tmp)
1795 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001796 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001798}
1799
1800static expr_ty
1801ast_for_testlist_gexp(struct compiling *c, const node* n)
1802{
1803 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1804 /* argument: test [ gen_for ] */
1805 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001806 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001807 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001808 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001809}
1810
1811/* like ast_for_testlist() but returns a sequence */
1812static asdl_seq*
1813ast_for_class_bases(struct compiling *c, const node* n)
1814{
1815 /* testlist: test (',' test)* [','] */
1816 assert(NCH(n) > 0);
1817 REQ(n, testlist);
1818 if (NCH(n) == 1) {
1819 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001820 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001821 if (!bases)
1822 return NULL;
1823 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001824 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001825 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001826 asdl_seq_SET(bases, 0, base);
1827 return bases;
1828 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001829
1830 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831}
1832
1833static stmt_ty
1834ast_for_expr_stmt(struct compiling *c, const node *n)
1835{
1836 REQ(n, expr_stmt);
1837 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1838 | ('=' (yield_expr|testlist))*)
1839 testlist: test (',' test)* [',']
1840 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1841 | '<<=' | '>>=' | '**=' | '//='
1842 test: ... here starts the operator precendence dance
1843 */
1844
1845 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001846 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 if (!e)
1848 return NULL;
1849
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001850 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 }
1852 else if (TYPE(CHILD(n, 1)) == augassign) {
1853 expr_ty expr1, expr2;
1854 operator_ty operator;
1855 node *ch = CHILD(n, 0);
1856
1857 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001858 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001860 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001861 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862
1863 if (!expr1)
1864 return NULL;
Neal Norwitz96e48d42006-02-05 02:07:19 +00001865 /* TODO(jhylton): Figure out why set_context() can't be used here. */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001866 switch (expr1->kind) {
1867 case GeneratorExp_kind:
1868 ast_error(ch, "augmented assignment to generator "
1869 "expression not possible");
1870 return NULL;
1871 case Name_kind: {
1872 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1873 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1874 ast_error(ch, "assignment to None");
1875 return NULL;
1876 }
1877 break;
1878 }
1879 case Attribute_kind:
1880 case Subscript_kind:
1881 break;
1882 default:
1883 ast_error(ch, "illegal expression for augmented "
1884 "assignment");
1885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887
1888 ch = CHILD(n, 2);
1889 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001890 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001892 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), ch->n_col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001893 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 return NULL;
1895
1896 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001897 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 return NULL;
1899
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001900 return AugAssign(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 }
1902 else {
1903 int i;
1904 asdl_seq *targets;
1905 node *value;
1906 expr_ty expression;
1907
1908 /* a normal assignment */
1909 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001910 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 if (!targets)
1912 return NULL;
1913 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001914 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 node *ch = CHILD(n, i);
1916 if (TYPE(ch) == yield_expr) {
1917 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001918 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001920 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921
1922 /* set context to assign */
1923 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925
Neal Norwitz84456bd2005-12-18 03:16:20 +00001926 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001927 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928
1929 asdl_seq_SET(targets, i / 2, e);
1930 }
1931 value = CHILD(n, NCH(n) - 1);
1932 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001933 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 else
1935 expression = ast_for_expr(c, value);
1936 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001937 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001938 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940}
1941
1942static stmt_ty
1943ast_for_print_stmt(struct compiling *c, const node *n)
1944{
1945 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1946 | '>>' test [ (',' test)+ [','] ] )
1947 */
1948 expr_ty dest = NULL, expression;
1949 asdl_seq *seq;
1950 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001951 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952
1953 REQ(n, print_stmt);
1954 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1955 dest = ast_for_expr(c, CHILD(n, 2));
1956 if (!dest)
1957 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001958 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001960 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00001962 return NULL;
1963 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001965 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001967 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968 }
1969 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001970 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971}
1972
1973static asdl_seq *
1974ast_for_exprlist(struct compiling *c, const node *n, int context)
1975{
1976 asdl_seq *seq;
1977 int i;
1978 expr_ty e;
1979
1980 REQ(n, exprlist);
1981
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 if (!seq)
1984 return NULL;
1985 for (i = 0; i < NCH(n); i += 2) {
1986 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001987 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001988 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00001989 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001990 if (context && !set_context(e, context, CHILD(n, i)))
1991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 }
1993 return seq;
1994}
1995
1996static stmt_ty
1997ast_for_del_stmt(struct compiling *c, const node *n)
1998{
1999 asdl_seq *expr_list;
2000
2001 /* del_stmt: 'del' exprlist */
2002 REQ(n, del_stmt);
2003
2004 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2005 if (!expr_list)
2006 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002007 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008}
2009
2010static stmt_ty
2011ast_for_flow_stmt(struct compiling *c, const node *n)
2012{
2013 /*
2014 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2015 | yield_stmt
2016 break_stmt: 'break'
2017 continue_stmt: 'continue'
2018 return_stmt: 'return' [testlist]
2019 yield_stmt: yield_expr
2020 yield_expr: 'yield' testlist
2021 raise_stmt: 'raise' [test [',' test [',' test]]]
2022 */
2023 node *ch;
2024
2025 REQ(n, flow_stmt);
2026 ch = CHILD(n, 0);
2027 switch (TYPE(ch)) {
2028 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002029 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002031 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 case yield_stmt: { /* will reduce to yield_expr */
2033 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2034 if (!exp)
2035 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002036 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 }
2038 case return_stmt:
2039 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002040 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002042 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 if (!expression)
2044 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002045 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 }
2047 case raise_stmt:
2048 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002049 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 else if (NCH(ch) == 2) {
2051 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2052 if (!expression)
2053 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002054 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 }
2056 else if (NCH(ch) == 4) {
2057 expr_ty expr1, expr2;
2058
2059 expr1 = ast_for_expr(c, CHILD(ch, 1));
2060 if (!expr1)
2061 return NULL;
2062 expr2 = ast_for_expr(c, CHILD(ch, 3));
2063 if (!expr2)
2064 return NULL;
2065
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002066 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 }
2068 else if (NCH(ch) == 6) {
2069 expr_ty expr1, expr2, expr3;
2070
2071 expr1 = ast_for_expr(c, CHILD(ch, 1));
2072 if (!expr1)
2073 return NULL;
2074 expr2 = ast_for_expr(c, CHILD(ch, 3));
2075 if (!expr2)
2076 return NULL;
2077 expr3 = ast_for_expr(c, CHILD(ch, 5));
2078 if (!expr3)
2079 return NULL;
2080
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002081 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 }
2083 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002084 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 "unexpected flow_stmt: %d", TYPE(ch));
2086 return NULL;
2087 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002088
2089 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2090 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091}
2092
2093static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002094alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095{
2096 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002097 import_as_name: NAME ['as' NAME]
2098 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 dotted_name: NAME ('.' NAME)*
2100 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002101 PyObject *str;
2102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 loop:
2104 switch (TYPE(n)) {
2105 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002106 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2107 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 case dotted_as_name:
2109 if (NCH(n) == 1) {
2110 n = CHILD(n, 0);
2111 goto loop;
2112 }
2113 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002114 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 assert(!a->asname);
2116 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2117 return a;
2118 }
2119 break;
2120 case dotted_name:
2121 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002122 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 else {
2124 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002125 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002126 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 char *s;
2128
2129 len = 0;
2130 for (i = 0; i < NCH(n); i += 2)
2131 /* length of string plus one for the dot */
2132 len += strlen(STR(CHILD(n, i))) + 1;
2133 len--; /* the last name doesn't have a dot */
2134 str = PyString_FromStringAndSize(NULL, len);
2135 if (!str)
2136 return NULL;
2137 s = PyString_AS_STRING(str);
2138 if (!s)
2139 return NULL;
2140 for (i = 0; i < NCH(n); i += 2) {
2141 char *sch = STR(CHILD(n, i));
2142 strcpy(s, STR(CHILD(n, i)));
2143 s += strlen(sch);
2144 *s++ = '.';
2145 }
2146 --s;
2147 *s = '\0';
2148 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002149 PyArena_AddPyObject(c->c_arena, str);
2150 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 }
2152 break;
2153 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002154 str = PyString_InternFromString("*");
2155 PyArena_AddPyObject(c->c_arena, str);
2156 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002158 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 "unexpected import name: %d", TYPE(n));
2160 return NULL;
2161 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002162
2163 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 return NULL;
2165}
2166
2167static stmt_ty
2168ast_for_import_stmt(struct compiling *c, const node *n)
2169{
2170 /*
2171 import_stmt: import_name | import_from
2172 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002173 import_from: 'from' ('.'* dotted_name | '.') 'import'
2174 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002176 int lineno;
2177 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 int i;
2179 asdl_seq *aliases;
2180
2181 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002182 lineno = LINENO(n);
2183 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002185 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002187 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002188 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 if (!aliases)
2190 return NULL;
2191 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002192 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002193 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 asdl_seq_SET(aliases, i / 2, import_alias);
2196 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002197 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002199 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002201 int idx, ndots = 0;
2202 alias_ty mod = NULL;
2203 identifier modname;
2204
2205 /* Count the number of dots (for relative imports) and check for the
2206 optional module name */
2207 for (idx = 1; idx < NCH(n); idx++) {
2208 if (TYPE(CHILD(n, idx)) == dotted_name) {
2209 mod = alias_for_import_name(c, CHILD(n, idx));
2210 idx++;
2211 break;
2212 } else if (TYPE(CHILD(n, idx)) != DOT) {
2213 break;
2214 }
2215 ndots++;
2216 }
2217 idx++; /* skip over the 'import' keyword */
2218 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002219 case STAR:
2220 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002221 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002222 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002223 if (ndots) {
2224 ast_error(n, "'import *' not allowed with 'from .'");
2225 return NULL;
2226 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002227 break;
2228 case LPAR:
2229 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002230 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002231 n_children = NCH(n);
2232 break;
2233 case import_as_names:
2234 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002235 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002236 n_children = NCH(n);
2237 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 ast_error(n, "trailing comma not allowed without"
2239 " surrounding parentheses");
2240 return NULL;
2241 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002242 break;
2243 default:
2244 ast_error(n, "Unexpected node-type in from-import");
2245 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002246 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002248 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002249 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251
2252 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002253 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002254 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002255 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002257 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002259 else {
2260 for (i = 0; i < NCH(n); i += 2) {
2261 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2262 if (!import_alias)
2263 return NULL;
2264 asdl_seq_SET(aliases, i / 2, import_alias);
2265 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002267 if (mod != NULL)
2268 modname = mod->name;
2269 else
2270 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002271 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002272 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 }
Neal Norwitz79792652005-11-14 04:25:03 +00002274 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 "unknown import statement: starts with command '%s'",
2276 STR(CHILD(n, 0)));
2277 return NULL;
2278}
2279
2280static stmt_ty
2281ast_for_global_stmt(struct compiling *c, const node *n)
2282{
2283 /* global_stmt: 'global' NAME (',' NAME)* */
2284 identifier name;
2285 asdl_seq *s;
2286 int i;
2287
2288 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002289 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 if (!s)
2291 return NULL;
2292 for (i = 1; i < NCH(n); i += 2) {
2293 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002294 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 asdl_seq_SET(s, i / 2, name);
2297 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002298 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299}
2300
2301static stmt_ty
2302ast_for_exec_stmt(struct compiling *c, const node *n)
2303{
2304 expr_ty expr1, globals = NULL, locals = NULL;
2305 int n_children = NCH(n);
2306 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002307 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 "poorly formed 'exec' statement: %d parts to statement",
2309 n_children);
2310 return NULL;
2311 }
2312
2313 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2314 REQ(n, exec_stmt);
2315 expr1 = ast_for_expr(c, CHILD(n, 1));
2316 if (!expr1)
2317 return NULL;
2318 if (n_children >= 4) {
2319 globals = ast_for_expr(c, CHILD(n, 3));
2320 if (!globals)
2321 return NULL;
2322 }
2323 if (n_children == 6) {
2324 locals = ast_for_expr(c, CHILD(n, 5));
2325 if (!locals)
2326 return NULL;
2327 }
2328
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002329 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330}
2331
2332static stmt_ty
2333ast_for_assert_stmt(struct compiling *c, const node *n)
2334{
2335 /* assert_stmt: 'assert' test [',' test] */
2336 REQ(n, assert_stmt);
2337 if (NCH(n) == 2) {
2338 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2339 if (!expression)
2340 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002341 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 }
2343 else if (NCH(n) == 4) {
2344 expr_ty expr1, expr2;
2345
2346 expr1 = ast_for_expr(c, CHILD(n, 1));
2347 if (!expr1)
2348 return NULL;
2349 expr2 = ast_for_expr(c, CHILD(n, 3));
2350 if (!expr2)
2351 return NULL;
2352
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002353 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 }
Neal Norwitz79792652005-11-14 04:25:03 +00002355 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 "improper number of parts to 'assert' statement: %d",
2357 NCH(n));
2358 return NULL;
2359}
2360
2361static asdl_seq *
2362ast_for_suite(struct compiling *c, const node *n)
2363{
2364 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002365 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366 stmt_ty s;
2367 int i, total, num, end, pos = 0;
2368 node *ch;
2369
2370 REQ(n, suite);
2371
2372 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002373 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 if (!seq)
2375 return NULL;
2376 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2377 n = CHILD(n, 0);
2378 /* simple_stmt always ends with a NEWLINE,
2379 and may have a trailing SEMI
2380 */
2381 end = NCH(n) - 1;
2382 if (TYPE(CHILD(n, end - 1)) == SEMI)
2383 end--;
2384 /* loop by 2 to skip semi-colons */
2385 for (i = 0; i < end; i += 2) {
2386 ch = CHILD(n, i);
2387 s = ast_for_stmt(c, ch);
2388 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002389 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390 asdl_seq_SET(seq, pos++, s);
2391 }
2392 }
2393 else {
2394 for (i = 2; i < (NCH(n) - 1); i++) {
2395 ch = CHILD(n, i);
2396 REQ(ch, stmt);
2397 num = num_stmts(ch);
2398 if (num == 1) {
2399 /* small_stmt or compound_stmt with only one child */
2400 s = ast_for_stmt(c, ch);
2401 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002402 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 asdl_seq_SET(seq, pos++, s);
2404 }
2405 else {
2406 int j;
2407 ch = CHILD(ch, 0);
2408 REQ(ch, simple_stmt);
2409 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002410 /* statement terminates with a semi-colon ';' */
2411 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002412 assert((j + 1) == NCH(ch));
2413 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002414 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 s = ast_for_stmt(c, CHILD(ch, j));
2416 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002417 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418 asdl_seq_SET(seq, pos++, s);
2419 }
2420 }
2421 }
2422 }
2423 assert(pos == seq->size);
2424 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425}
2426
2427static stmt_ty
2428ast_for_if_stmt(struct compiling *c, const node *n)
2429{
2430 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2431 ['else' ':' suite]
2432 */
2433 char *s;
2434
2435 REQ(n, if_stmt);
2436
2437 if (NCH(n) == 4) {
2438 expr_ty expression;
2439 asdl_seq *suite_seq;
2440
2441 expression = ast_for_expr(c, CHILD(n, 1));
2442 if (!expression)
2443 return NULL;
2444 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002445 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 return NULL;
2447
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002448 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002450
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 s = STR(CHILD(n, 4));
2452 /* s[2], the third character in the string, will be
2453 's' for el_s_e, or
2454 'i' for el_i_f
2455 */
2456 if (s[2] == 's') {
2457 expr_ty expression;
2458 asdl_seq *seq1, *seq2;
2459
2460 expression = ast_for_expr(c, CHILD(n, 1));
2461 if (!expression)
2462 return NULL;
2463 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002464 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 return NULL;
2466 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002467 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 return NULL;
2469
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002470 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 }
2472 else if (s[2] == 'i') {
2473 int i, n_elif, has_else = 0;
2474 asdl_seq *orelse = NULL;
2475 n_elif = NCH(n) - 4;
2476 /* must reference the child n_elif+1 since 'else' token is third,
2477 not fourth, child from the end. */
2478 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2479 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2480 has_else = 1;
2481 n_elif -= 3;
2482 }
2483 n_elif /= 4;
2484
2485 if (has_else) {
2486 expr_ty expression;
2487 asdl_seq *seq1, *seq2;
2488
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002489 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 if (!orelse)
2491 return NULL;
2492 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002493 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002496 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002499 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501
2502 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002503 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002504 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 /* the just-created orelse handled the last elif */
2506 n_elif--;
2507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508
2509 for (i = 0; i < n_elif; i++) {
2510 int off = 5 + (n_elif - i - 1) * 4;
2511 expr_ty expression;
2512 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002513 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002514 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 return NULL;
2516 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002517 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002520 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522
2523 asdl_seq_SET(new, 0,
2524 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002525 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 orelse = new;
2527 }
2528 return If(ast_for_expr(c, CHILD(n, 1)),
2529 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002530 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002532
2533 PyErr_Format(PyExc_SystemError,
2534 "unexpected token in 'if' statement: %s", s);
2535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536}
2537
2538static stmt_ty
2539ast_for_while_stmt(struct compiling *c, const node *n)
2540{
2541 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2542 REQ(n, while_stmt);
2543
2544 if (NCH(n) == 4) {
2545 expr_ty expression;
2546 asdl_seq *suite_seq;
2547
2548 expression = ast_for_expr(c, CHILD(n, 1));
2549 if (!expression)
2550 return NULL;
2551 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002552 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002554 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 }
2556 else if (NCH(n) == 7) {
2557 expr_ty expression;
2558 asdl_seq *seq1, *seq2;
2559
2560 expression = ast_for_expr(c, CHILD(n, 1));
2561 if (!expression)
2562 return NULL;
2563 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002564 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 return NULL;
2566 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002567 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 return NULL;
2569
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002570 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002572
2573 PyErr_Format(PyExc_SystemError,
2574 "wrong number of tokens for 'while' statement: %d",
2575 NCH(n));
2576 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577}
2578
2579static stmt_ty
2580ast_for_for_stmt(struct compiling *c, const node *n)
2581{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002582 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 expr_ty expression;
2584 expr_ty target;
2585 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2586 REQ(n, for_stmt);
2587
2588 if (NCH(n) == 9) {
2589 seq = ast_for_suite(c, CHILD(n, 8));
2590 if (!seq)
2591 return NULL;
2592 }
2593
2594 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002595 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002597 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002600 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002602 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002603 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 return NULL;
2605 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002606 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 return NULL;
2608
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002609 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610}
2611
2612static excepthandler_ty
2613ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2614{
2615 /* except_clause: 'except' [test [',' test]] */
2616 REQ(exc, except_clause);
2617 REQ(body, suite);
2618
2619 if (NCH(exc) == 1) {
2620 asdl_seq *suite_seq = ast_for_suite(c, body);
2621 if (!suite_seq)
2622 return NULL;
2623
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002624 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 }
2626 else if (NCH(exc) == 2) {
2627 expr_ty expression;
2628 asdl_seq *suite_seq;
2629
2630 expression = ast_for_expr(c, CHILD(exc, 1));
2631 if (!expression)
2632 return NULL;
2633 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002634 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 return NULL;
2636
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002637 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 }
2639 else if (NCH(exc) == 4) {
2640 asdl_seq *suite_seq;
2641 expr_ty expression;
2642 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2643 if (!e)
2644 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002645 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 return NULL;
2647 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002648 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 return NULL;
2650 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002651 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 return NULL;
2653
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002654 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002656
2657 PyErr_Format(PyExc_SystemError,
2658 "wrong number of children for 'except' clause: %d",
2659 NCH(exc));
2660 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661}
2662
2663static stmt_ty
2664ast_for_try_stmt(struct compiling *c, const node *n)
2665{
Neal Norwitzf599f422005-12-17 21:33:47 +00002666 const int nch = NCH(n);
2667 int n_except = (nch - 3)/3;
2668 asdl_seq *body, *orelse = NULL, *finally = NULL;
2669
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 REQ(n, try_stmt);
2671
Neal Norwitzf599f422005-12-17 21:33:47 +00002672 body = ast_for_suite(c, CHILD(n, 2));
2673 if (body == NULL)
2674 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675
Neal Norwitzf599f422005-12-17 21:33:47 +00002676 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2677 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2678 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2679 /* we can assume it's an "else",
2680 because nch >= 9 for try-else-finally and
2681 it would otherwise have a type of except_clause */
2682 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2683 if (orelse == NULL)
2684 return NULL;
2685 n_except--;
2686 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687
Neal Norwitzf599f422005-12-17 21:33:47 +00002688 finally = ast_for_suite(c, CHILD(n, nch - 1));
2689 if (finally == NULL)
2690 return NULL;
2691 n_except--;
2692 }
2693 else {
2694 /* we can assume it's an "else",
2695 otherwise it would have a type of except_clause */
2696 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2697 if (orelse == NULL)
2698 return NULL;
2699 n_except--;
2700 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002702 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002703 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 return NULL;
2705 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002706
2707 if (n_except > 0) {
2708 int i;
2709 stmt_ty except_st;
2710 /* process except statements to create a try ... except */
2711 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2712 if (handlers == NULL)
2713 return NULL;
2714
2715 for (i = 0; i < n_except; i++) {
2716 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2717 CHILD(n, 5 + i * 3));
2718 if (!e)
2719 return NULL;
2720 asdl_seq_SET(handlers, i, e);
2721 }
2722
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002723 except_st = TryExcept(body, handlers, orelse, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002724 if (!finally)
2725 return except_st;
2726
2727 /* if a 'finally' is present too, we nest the TryExcept within a
2728 TryFinally to emulate try ... except ... finally */
2729 body = asdl_seq_new(1, c->c_arena);
2730 if (body == NULL)
2731 return NULL;
2732 asdl_seq_SET(body, 0, except_st);
2733 }
2734
2735 /* must be a try ... finally (except clauses are in body, if any exist) */
2736 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002737 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738}
2739
Guido van Rossumc2e20742006-02-27 22:32:47 +00002740static expr_ty
2741ast_for_with_var(struct compiling *c, const node *n)
2742{
2743 REQ(n, with_var);
2744 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2745 ast_error(n, "expected \"with [expr] as [var]\"");
2746 return NULL;
2747 }
2748 return ast_for_expr(c, CHILD(n, 1));
2749}
2750
2751/* with_stmt: 'with' test [ with_var ] ':' suite */
2752static stmt_ty
2753ast_for_with_stmt(struct compiling *c, const node *n)
2754{
2755 expr_ty context_expr, optional_vars = NULL;
2756 int suite_index = 3; /* skip 'with', test, and ':' */
2757 asdl_seq *suite_seq;
2758
2759 assert(TYPE(n) == with_stmt);
2760 context_expr = ast_for_expr(c, CHILD(n, 1));
2761 if (TYPE(CHILD(n, 2)) == with_var) {
2762 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2763
2764 if (!optional_vars) {
2765 return NULL;
2766 }
2767 if (!set_context(optional_vars, Store, n)) {
2768 return NULL;
2769 }
2770 suite_index = 4;
2771 }
2772
2773 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2774 if (!suite_seq) {
2775 return NULL;
2776 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002777 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2778 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002779}
2780
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781static stmt_ty
2782ast_for_classdef(struct compiling *c, const node *n)
2783{
2784 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 asdl_seq *bases, *s;
2786
2787 REQ(n, classdef);
2788
2789 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2790 ast_error(n, "assignment to None");
2791 return NULL;
2792 }
2793
2794 if (NCH(n) == 4) {
2795 s = ast_for_suite(c, CHILD(n, 3));
2796 if (!s)
2797 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002798 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002799 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 }
2801 /* check for empty base list */
2802 if (TYPE(CHILD(n,3)) == RPAR) {
2803 s = ast_for_suite(c, CHILD(n,5));
2804 if (!s)
2805 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002806 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002807 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 }
2809
2810 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002811 bases = ast_for_class_bases(c, CHILD(n, 3));
2812 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814
2815 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002816 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002818 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n), n->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002819 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820}
2821
2822static stmt_ty
2823ast_for_stmt(struct compiling *c, const node *n)
2824{
2825 if (TYPE(n) == stmt) {
2826 assert(NCH(n) == 1);
2827 n = CHILD(n, 0);
2828 }
2829 if (TYPE(n) == simple_stmt) {
2830 assert(num_stmts(n) == 1);
2831 n = CHILD(n, 0);
2832 }
2833 if (TYPE(n) == small_stmt) {
2834 REQ(n, small_stmt);
2835 n = CHILD(n, 0);
2836 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2837 | flow_stmt | import_stmt | global_stmt | exec_stmt
2838 | assert_stmt
2839 */
2840 switch (TYPE(n)) {
2841 case expr_stmt:
2842 return ast_for_expr_stmt(c, n);
2843 case print_stmt:
2844 return ast_for_print_stmt(c, n);
2845 case del_stmt:
2846 return ast_for_del_stmt(c, n);
2847 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002848 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 case flow_stmt:
2850 return ast_for_flow_stmt(c, n);
2851 case import_stmt:
2852 return ast_for_import_stmt(c, n);
2853 case global_stmt:
2854 return ast_for_global_stmt(c, n);
2855 case exec_stmt:
2856 return ast_for_exec_stmt(c, n);
2857 case assert_stmt:
2858 return ast_for_assert_stmt(c, n);
2859 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002860 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2862 TYPE(n), NCH(n));
2863 return NULL;
2864 }
2865 }
2866 else {
2867 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2868 | funcdef | classdef
2869 */
2870 node *ch = CHILD(n, 0);
2871 REQ(n, compound_stmt);
2872 switch (TYPE(ch)) {
2873 case if_stmt:
2874 return ast_for_if_stmt(c, ch);
2875 case while_stmt:
2876 return ast_for_while_stmt(c, ch);
2877 case for_stmt:
2878 return ast_for_for_stmt(c, ch);
2879 case try_stmt:
2880 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002881 case with_stmt:
2882 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 case funcdef:
2884 return ast_for_funcdef(c, ch);
2885 case classdef:
2886 return ast_for_classdef(c, ch);
2887 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002888 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2890 TYPE(n), NCH(n));
2891 return NULL;
2892 }
2893 }
2894}
2895
2896static PyObject *
2897parsenumber(const char *s)
2898{
2899 const char *end;
2900 long x;
2901 double dx;
2902#ifndef WITHOUT_COMPLEX
2903 Py_complex c;
2904 int imflag;
2905#endif
2906
2907 errno = 0;
2908 end = s + strlen(s) - 1;
2909#ifndef WITHOUT_COMPLEX
2910 imflag = *end == 'j' || *end == 'J';
2911#endif
2912 if (*end == 'l' || *end == 'L')
2913 return PyLong_FromString((char *)s, (char **)0, 0);
2914 if (s[0] == '0') {
2915 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2916 if (x < 0 && errno == 0) {
2917 return PyLong_FromString((char *)s,
2918 (char **)0,
2919 0);
2920 }
2921 }
2922 else
2923 x = PyOS_strtol((char *)s, (char **)&end, 0);
2924 if (*end == '\0') {
2925 if (errno != 0)
2926 return PyLong_FromString((char *)s, (char **)0, 0);
2927 return PyInt_FromLong(x);
2928 }
2929 /* XXX Huge floats may silently fail */
2930#ifndef WITHOUT_COMPLEX
2931 if (imflag) {
2932 c.real = 0.;
2933 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002934 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935 PyFPE_END_PROTECT(c)
2936 return PyComplex_FromCComplex(c);
2937 }
2938 else
2939#endif
2940 {
2941 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002942 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 PyFPE_END_PROTECT(dx)
2944 return PyFloat_FromDouble(dx);
2945 }
2946}
2947
2948static PyObject *
2949decode_utf8(const char **sPtr, const char *end, char* encoding)
2950{
2951#ifndef Py_USING_UNICODE
2952 Py_FatalError("decode_utf8 should not be called in this build.");
2953 return NULL;
2954#else
2955 PyObject *u, *v;
2956 char *s, *t;
2957 t = s = (char *)*sPtr;
2958 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2959 while (s < end && (*s & 0x80)) s++;
2960 *sPtr = s;
2961 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2962 if (u == NULL)
2963 return NULL;
2964 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2965 Py_DECREF(u);
2966 return v;
2967#endif
2968}
2969
2970static PyObject *
2971decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2972{
2973 PyObject *v, *u;
2974 char *buf;
2975 char *p;
2976 const char *end;
2977 if (encoding == NULL) {
2978 buf = (char *)s;
2979 u = NULL;
2980 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2981 buf = (char *)s;
2982 u = NULL;
2983 } else {
2984 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2985 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2986 if (u == NULL)
2987 return NULL;
2988 p = buf = PyString_AsString(u);
2989 end = s + len;
2990 while (s < end) {
2991 if (*s == '\\') {
2992 *p++ = *s++;
2993 if (*s & 0x80) {
2994 strcpy(p, "u005c");
2995 p += 5;
2996 }
2997 }
2998 if (*s & 0x80) { /* XXX inefficient */
2999 PyObject *w;
3000 char *r;
3001 int rn, i;
3002 w = decode_utf8(&s, end, "utf-16-be");
3003 if (w == NULL) {
3004 Py_DECREF(u);
3005 return NULL;
3006 }
3007 r = PyString_AsString(w);
3008 rn = PyString_Size(w);
3009 assert(rn % 2 == 0);
3010 for (i = 0; i < rn; i += 2) {
3011 sprintf(p, "\\u%02x%02x",
3012 r[i + 0] & 0xFF,
3013 r[i + 1] & 0xFF);
3014 p += 6;
3015 }
3016 Py_DECREF(w);
3017 } else {
3018 *p++ = *s++;
3019 }
3020 }
3021 len = p - buf;
3022 s = buf;
3023 }
3024 if (rawmode)
3025 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3026 else
3027 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3028 Py_XDECREF(u);
3029 return v;
3030}
3031
3032/* s is a Python string literal, including the bracketing quote characters,
3033 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3034 * parsestr parses it, and returns the decoded Python string object.
3035 */
3036static PyObject *
3037parsestr(const char *s, const char *encoding)
3038{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003040 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 int rawmode = 0;
3042 int need_encoding;
3043 int unicode = 0;
3044
3045 if (isalpha(quote) || quote == '_') {
3046 if (quote == 'u' || quote == 'U') {
3047 quote = *++s;
3048 unicode = 1;
3049 }
3050 if (quote == 'r' || quote == 'R') {
3051 quote = *++s;
3052 rawmode = 1;
3053 }
3054 }
3055 if (quote != '\'' && quote != '\"') {
3056 PyErr_BadInternalCall();
3057 return NULL;
3058 }
3059 s++;
3060 len = strlen(s);
3061 if (len > INT_MAX) {
3062 PyErr_SetString(PyExc_OverflowError,
3063 "string to parse is too long");
3064 return NULL;
3065 }
3066 if (s[--len] != quote) {
3067 PyErr_BadInternalCall();
3068 return NULL;
3069 }
3070 if (len >= 4 && s[0] == quote && s[1] == quote) {
3071 s += 2;
3072 len -= 2;
3073 if (s[--len] != quote || s[--len] != quote) {
3074 PyErr_BadInternalCall();
3075 return NULL;
3076 }
3077 }
3078#ifdef Py_USING_UNICODE
3079 if (unicode || Py_UnicodeFlag) {
3080 return decode_unicode(s, len, rawmode, encoding);
3081 }
3082#endif
3083 need_encoding = (encoding != NULL &&
3084 strcmp(encoding, "utf-8") != 0 &&
3085 strcmp(encoding, "iso-8859-1") != 0);
3086 if (rawmode || strchr(s, '\\') == NULL) {
3087 if (need_encoding) {
3088#ifndef Py_USING_UNICODE
3089 /* This should not happen - we never see any other
3090 encoding. */
3091 Py_FatalError("cannot deal with encodings in this build.");
3092#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003093 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003094 if (u == NULL)
3095 return NULL;
3096 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3097 Py_DECREF(u);
3098 return v;
3099#endif
3100 } else {
3101 return PyString_FromStringAndSize(s, len);
3102 }
3103 }
3104
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003105 return PyString_DecodeEscape(s, len, NULL, unicode,
3106 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107}
3108
3109/* Build a Python string object out of a STRING atom. This takes care of
3110 * compile-time literal catenation, calling parsestr() on each piece, and
3111 * pasting the intermediate results together.
3112 */
3113static PyObject *
3114parsestrplus(struct compiling *c, const node *n)
3115{
3116 PyObject *v;
3117 int i;
3118 REQ(CHILD(n, 0), STRING);
3119 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3120 /* String literal concatenation */
3121 for (i = 1; i < NCH(n); i++) {
3122 PyObject *s;
3123 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3124 if (s == NULL)
3125 goto onError;
3126 if (PyString_Check(v) && PyString_Check(s)) {
3127 PyString_ConcatAndDel(&v, s);
3128 if (v == NULL)
3129 goto onError;
3130 }
3131#ifdef Py_USING_UNICODE
3132 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003133 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 Py_DECREF(v);
3136 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003137 if (v == NULL)
3138 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 }
3140#endif
3141 }
3142 }
3143 return v;
3144
3145 onError:
3146 Py_XDECREF(v);
3147 return NULL;
3148}