blob: 9b32f8b8e575266ba33e90417d4ecf068be3d4a0 [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{
186 int i, j, num;
187 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
202 switch (TYPE(n)) {
203 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000204 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205 if (!stmts)
206 return NULL;
207 for (i = 0; i < NCH(n) - 1; i++) {
208 ch = CHILD(n, i);
209 if (TYPE(ch) == NEWLINE)
210 continue;
211 REQ(ch, stmt);
212 num = num_stmts(ch);
213 if (num == 1) {
214 s = ast_for_stmt(&c, ch);
215 if (!s)
216 goto error;
217 asdl_seq_APPEND(stmts, s);
218 }
219 else {
220 ch = CHILD(ch, 0);
221 REQ(ch, simple_stmt);
222 for (j = 0; j < num; j++) {
223 s = ast_for_stmt(&c, CHILD(ch, j * 2));
224 if (!s)
225 goto error;
226 asdl_seq_APPEND(stmts, s);
227 }
228 }
229 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000230 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 case eval_input: {
232 expr_ty testlist_ast;
233
234 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000235 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 if (!testlist_ast)
237 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000238 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 }
240 case single_input:
241 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000242 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243 if (!stmts)
244 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000245 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, arena));
246 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247 }
248 else {
249 n = CHILD(n, 0);
250 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000251 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 if (!stmts)
253 goto error;
254 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000255 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256 if (!s)
257 goto error;
258 asdl_seq_SET(stmts, 0, s);
259 }
260 else {
261 /* Only a simple_stmt can contain multiple statements. */
262 REQ(n, simple_stmt);
263 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 if (TYPE(CHILD(n, i)) == NEWLINE)
265 break;
266 s = ast_for_stmt(&c, CHILD(n, i));
267 if (!s)
268 goto error;
269 asdl_seq_SET(stmts, i / 2, s);
270 }
271 }
272
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000273 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 }
275 default:
276 goto error;
277 }
278 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 ast_error_finish(filename);
280 return NULL;
281}
282
283/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
284*/
285
286static operator_ty
287get_operator(const node *n)
288{
289 switch (TYPE(n)) {
290 case VBAR:
291 return BitOr;
292 case CIRCUMFLEX:
293 return BitXor;
294 case AMPER:
295 return BitAnd;
296 case LEFTSHIFT:
297 return LShift;
298 case RIGHTSHIFT:
299 return RShift;
300 case PLUS:
301 return Add;
302 case MINUS:
303 return Sub;
304 case STAR:
305 return Mult;
306 case SLASH:
307 return Div;
308 case DOUBLESLASH:
309 return FloorDiv;
310 case PERCENT:
311 return Mod;
312 default:
313 return 0;
314 }
315}
316
317/* Set the context ctx for expr_ty e returning 0 on success, -1 on error.
318
319 Only sets context for expr kinds that "can appear in assignment context"
320 (according to ../Parser/Python.asdl). For other expr kinds, it sets
321 an appropriate syntax error and returns false.
322
323 If e is a sequential type, items in sequence will also have their context
324 set.
325
326*/
327
328static int
329set_context(expr_ty e, expr_context_ty ctx, const node *n)
330{
331 asdl_seq *s = NULL;
332
333 switch (e->kind) {
334 case Attribute_kind:
335 if (ctx == Store &&
336 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
337 return ast_error(n, "assignment to None");
338 }
339 e->v.Attribute.ctx = ctx;
340 break;
341 case Subscript_kind:
342 e->v.Subscript.ctx = ctx;
343 break;
344 case Name_kind:
345 if (ctx == Store &&
346 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
347 return ast_error(n, "assignment to None");
348 }
349 e->v.Name.ctx = ctx;
350 break;
351 case List_kind:
352 e->v.List.ctx = ctx;
353 s = e->v.List.elts;
354 break;
355 case Tuple_kind:
356 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
357 return ast_error(n, "can't assign to ()");
358 e->v.Tuple.ctx = ctx;
359 s = e->v.Tuple.elts;
360 break;
361 case Call_kind:
362 if (ctx == Store)
363 return ast_error(n, "can't assign to function call");
364 else if (ctx == Del)
365 return ast_error(n, "can't delete function call");
366 else
367 return ast_error(n, "unexpected operation on function call");
368 break;
369 case BinOp_kind:
370 return ast_error(n, "can't assign to operator");
371 case GeneratorExp_kind:
372 return ast_error(n, "assignment to generator expression "
373 "not possible");
374 case Num_kind:
375 case Str_kind:
376 return ast_error(n, "can't assign to literal");
377 default: {
378 char buf[300];
379 PyOS_snprintf(buf, sizeof(buf),
380 "unexpected expression in assignment %d (line %d)",
381 e->kind, e->lineno);
382 return ast_error(n, buf);
383 }
384 }
385 /* If the LHS is a list or tuple, we need to set the assignment
386 context for all the tuple elements.
387 */
388 if (s) {
389 int i;
390
391 for (i = 0; i < asdl_seq_LEN(s); i++) {
392 if (!set_context(asdl_seq_GET(s, i), ctx, n))
393 return 0;
394 }
395 }
396 return 1;
397}
398
399static operator_ty
400ast_for_augassign(const node *n)
401{
402 REQ(n, augassign);
403 n = CHILD(n, 0);
404 switch (STR(n)[0]) {
405 case '+':
406 return Add;
407 case '-':
408 return Sub;
409 case '/':
410 if (STR(n)[1] == '/')
411 return FloorDiv;
412 else
413 return Div;
414 case '%':
415 return Mod;
416 case '<':
417 return LShift;
418 case '>':
419 return RShift;
420 case '&':
421 return BitAnd;
422 case '^':
423 return BitXor;
424 case '|':
425 return BitOr;
426 case '*':
427 if (STR(n)[1] == '*')
428 return Pow;
429 else
430 return Mult;
431 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000432 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433 return 0;
434 }
435}
436
437static cmpop_ty
438ast_for_comp_op(const node *n)
439{
440 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
441 |'is' 'not'
442 */
443 REQ(n, comp_op);
444 if (NCH(n) == 1) {
445 n = CHILD(n, 0);
446 switch (TYPE(n)) {
447 case LESS:
448 return Lt;
449 case GREATER:
450 return Gt;
451 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 return Eq;
453 case LESSEQUAL:
454 return LtE;
455 case GREATEREQUAL:
456 return GtE;
457 case NOTEQUAL:
458 return NotEq;
459 case NAME:
460 if (strcmp(STR(n), "in") == 0)
461 return In;
462 if (strcmp(STR(n), "is") == 0)
463 return Is;
464 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000465 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466 STR(n));
467 return 0;
468 }
469 }
470 else if (NCH(n) == 2) {
471 /* handle "not in" and "is not" */
472 switch (TYPE(CHILD(n, 0))) {
473 case NAME:
474 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
475 return NotIn;
476 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
477 return IsNot;
478 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000479 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
481 return 0;
482 }
483 }
Neal Norwitz79792652005-11-14 04:25:03 +0000484 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485 NCH(n));
486 return 0;
487}
488
489static asdl_seq *
490seq_for_testlist(struct compiling *c, const node *n)
491{
492 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000493 asdl_seq *seq;
494 expr_ty expression;
495 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 assert(TYPE(n) == testlist
497 || TYPE(n) == listmaker
498 || TYPE(n) == testlist_gexp
499 || TYPE(n) == testlist_safe
500 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000502 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503 if (!seq)
504 return NULL;
505
506 for (i = 0; i < NCH(n); i += 2) {
507 REQ(CHILD(n, i), test);
508
509 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000510 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512
513 assert(i / 2 < seq->size);
514 asdl_seq_SET(seq, i / 2, expression);
515 }
516 return seq;
517}
518
519static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000520compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521{
522 int i, len = (NCH(n) + 1) / 2;
523 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000524 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 if (!args)
526 return NULL;
527
528 REQ(n, fplist);
529
530 for (i = 0; i < len; i++) {
531 const node *child = CHILD(CHILD(n, 2*i), 0);
532 expr_ty arg;
533 if (TYPE(child) == NAME) {
534 if (!strcmp(STR(child), "None")) {
535 ast_error(child, "assignment to None");
536 return NULL;
537 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000538 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
539 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540 }
541 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000542 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543 set_context(arg, Store, n);
544 asdl_seq_SET(args, i, arg);
545 }
546
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000547 result = Tuple(args, Store, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548 set_context(result, Store, n);
549 return result;
550}
551
552/* Create AST for argument list.
553
554 XXX TO DO:
555 - check for invalid argument lists like normal after default
556*/
557
558static arguments_ty
559ast_for_arguments(struct compiling *c, const node *n)
560{
561 /* parameters: '(' [varargslist] ')'
562 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
563 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
564 */
565 int i, n_args = 0, n_defaults = 0, found_default = 0;
566 asdl_seq *args, *defaults;
567 identifier vararg = NULL, kwarg = NULL;
568 node *ch;
569
570 if (TYPE(n) == parameters) {
571 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000572 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 n = CHILD(n, 1);
574 }
575 REQ(n, varargslist);
576
577 /* first count the number of normal args & defaults */
578 for (i = 0; i < NCH(n); i++) {
579 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000580 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582 if (TYPE(ch) == EQUAL)
583 n_defaults++;
584 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000585 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586 if (!args && n_args)
587 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000588 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589 if (!defaults && n_defaults)
590 goto error;
591
592 /* fpdef: NAME | '(' fplist ')'
593 fplist: fpdef (',' fpdef)* [',']
594 */
595 i = 0;
596 while (i < NCH(n)) {
597 ch = CHILD(n, i);
598 switch (TYPE(ch)) {
599 case fpdef:
600 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
601 anything other than EQUAL or a comma? */
602 /* XXX Should NCH(n) check be made a separate check? */
603 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
604 asdl_seq_APPEND(defaults,
605 ast_for_expr(c, CHILD(n, i + 2)));
606 i += 2;
607 found_default = 1;
608 }
609 else if (found_default) {
610 ast_error(n,
611 "non-default argument follows default argument");
612 goto error;
613 }
614
615 if (NCH(ch) == 3) {
616 asdl_seq_APPEND(args,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000617 compiler_complex_args(c, CHILD(ch, 1)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 }
619 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000620 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
622 ast_error(CHILD(ch, 0), "assignment to None");
623 goto error;
624 }
Armin Rigo31441302005-10-21 12:57:31 +0000625 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000626 Param, LINENO(ch), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627 if (!name)
628 goto error;
629 asdl_seq_APPEND(args, name);
630
631 }
632 i += 2; /* the name and the comma */
633 break;
634 case STAR:
635 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
636 ast_error(CHILD(n, i+1), "assignment to None");
637 goto error;
638 }
639 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
640 i += 3;
641 break;
642 case DOUBLESTAR:
643 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
644 ast_error(CHILD(n, i+1), "assignment to None");
645 goto error;
646 }
647 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
648 i += 3;
649 break;
650 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000651 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652 "unexpected node in varargslist: %d @ %d",
653 TYPE(ch), i);
654 goto error;
655 }
656 }
657
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000658 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659
660 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000661 Py_XDECREF(vararg);
662 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 return NULL;
664}
665
666static expr_ty
667ast_for_dotted_name(struct compiling *c, const node *n)
668{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000669 expr_ty e;
670 identifier id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 int i;
672
673 REQ(n, dotted_name);
674
675 id = NEW_IDENTIFIER(CHILD(n, 0));
676 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000677 return NULL;
678 e = Name(id, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000680 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681
682 for (i = 2; i < NCH(n); i+=2) {
683 id = NEW_IDENTIFIER(CHILD(n, i));
684 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000685 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000686 e = Attribute(e, id, Load, LINENO(CHILD(n, i)), c->c_arena);
687 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000688 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 }
690
691 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692}
693
694static expr_ty
695ast_for_decorator(struct compiling *c, const node *n)
696{
697 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
698 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000699 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700
701 REQ(n, decorator);
702
703 if ((NCH(n) < 3 && NCH(n) != 5 && NCH(n) != 6)
704 || TYPE(CHILD(n, 0)) != AT || TYPE(RCHILD(n, -1)) != NEWLINE) {
705 ast_error(n, "Invalid decorator node");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000706 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 }
708
709 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
710 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000711 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712
713 if (NCH(n) == 3) { /* No arguments */
714 d = name_expr;
715 name_expr = NULL;
716 }
717 else if (NCH(n) == 5) { /* Call with no arguments */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000718 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000720 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 name_expr = NULL;
722 }
723 else {
724 d = ast_for_call(c, CHILD(n, 3), name_expr);
725 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000726 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 name_expr = NULL;
728 }
729
730 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731}
732
733static asdl_seq*
734ast_for_decorators(struct compiling *c, const node *n)
735{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000736 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000737 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 int i;
739
740 REQ(n, decorators);
741
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000742 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 if (!decorator_seq)
744 return NULL;
745
746 for (i = 0; i < NCH(n); i++) {
747 d = ast_for_decorator(c, CHILD(n, i));
748 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000749 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 asdl_seq_APPEND(decorator_seq, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 }
752 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753}
754
755static stmt_ty
756ast_for_funcdef(struct compiling *c, const node *n)
757{
758 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000759 identifier name;
760 arguments_ty args;
761 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762 asdl_seq *decorator_seq = NULL;
763 int name_i;
764
765 REQ(n, funcdef);
766
767 if (NCH(n) == 6) { /* decorators are present */
768 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
769 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000770 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 name_i = 2;
772 }
773 else {
774 name_i = 1;
775 }
776
777 name = NEW_IDENTIFIER(CHILD(n, name_i));
778 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000779 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000781 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000782 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 }
784 args = ast_for_arguments(c, CHILD(n, name_i + 1));
785 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000786 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 body = ast_for_suite(c, CHILD(n, name_i + 3));
788 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000789 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000791 return FunctionDef(name, args, body, decorator_seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792}
793
794static expr_ty
795ast_for_lambdef(struct compiling *c, const node *n)
796{
797 /* lambdef: 'lambda' [varargslist] ':' test */
798 arguments_ty args;
799 expr_ty expression;
800
801 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000802 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 if (!args)
804 return NULL;
805 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000806 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 }
809 else {
810 args = ast_for_arguments(c, CHILD(n, 1));
811 if (!args)
812 return NULL;
813 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000814 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 }
817
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000818 return Lambda(args, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819}
820
821/* Count the number of 'for' loop in a list comprehension.
822
823 Helper for ast_for_listcomp().
824*/
825
826static int
827count_list_fors(const node *n)
828{
829 int n_fors = 0;
830 node *ch = CHILD(n, 1);
831
832 count_list_for:
833 n_fors++;
834 REQ(ch, list_for);
835 if (NCH(ch) == 5)
836 ch = CHILD(ch, 4);
837 else
838 return n_fors;
839 count_list_iter:
840 REQ(ch, list_iter);
841 ch = CHILD(ch, 0);
842 if (TYPE(ch) == list_for)
843 goto count_list_for;
844 else if (TYPE(ch) == list_if) {
845 if (NCH(ch) == 3) {
846 ch = CHILD(ch, 2);
847 goto count_list_iter;
848 }
849 else
850 return n_fors;
851 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000852
853 /* Should never be reached */
854 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
855 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856}
857
858/* Count the number of 'if' statements in a list comprehension.
859
860 Helper for ast_for_listcomp().
861*/
862
863static int
864count_list_ifs(const node *n)
865{
866 int n_ifs = 0;
867
868 count_list_iter:
869 REQ(n, list_iter);
870 if (TYPE(CHILD(n, 0)) == list_for)
871 return n_ifs;
872 n = CHILD(n, 0);
873 REQ(n, list_if);
874 n_ifs++;
875 if (NCH(n) == 2)
876 return n_ifs;
877 n = CHILD(n, 2);
878 goto count_list_iter;
879}
880
881static expr_ty
882ast_for_listcomp(struct compiling *c, const node *n)
883{
884 /* listmaker: test ( list_for | (',' test)* [','] )
885 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
886 list_iter: list_for | list_if
887 list_if: 'if' test [list_iter]
888 testlist_safe: test [(',' test)+ [',']]
889 */
890 expr_ty elt;
891 asdl_seq *listcomps;
892 int i, n_fors;
893 node *ch;
894
895 REQ(n, listmaker);
896 assert(NCH(n) > 1);
897
898 elt = ast_for_expr(c, CHILD(n, 0));
899 if (!elt)
900 return NULL;
901
902 n_fors = count_list_fors(n);
903 if (n_fors == -1)
904 return NULL;
905
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000906 listcomps = asdl_seq_new(n_fors, c->c_arena);
907 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000909
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 ch = CHILD(n, 1);
911 for (i = 0; i < n_fors; i++) {
912 comprehension_ty lc;
913 asdl_seq *t;
914 expr_ty expression;
915
916 REQ(ch, list_for);
917
918 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000919 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000921 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000922 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000925 if (asdl_seq_LEN(t) == 1)
926 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
927 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000929 lc = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
930 expression, NULL, c->c_arena);
931 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933
934 if (NCH(ch) == 5) {
935 int j, n_ifs;
936 asdl_seq *ifs;
937
938 ch = CHILD(ch, 4);
939 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000940 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000943 ifs = asdl_seq_new(n_ifs, c->c_arena);
944 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946
947 for (j = 0; j < n_ifs; j++) {
948 REQ(ch, list_iter);
949
950 ch = CHILD(ch, 0);
951 REQ(ch, list_if);
952
953 asdl_seq_APPEND(ifs, ast_for_expr(c, CHILD(ch, 1)));
954 if (NCH(ch) == 3)
955 ch = CHILD(ch, 2);
956 }
957 /* on exit, must guarantee that ch is a list_for */
958 if (TYPE(ch) == list_iter)
959 ch = CHILD(ch, 0);
960 lc->ifs = ifs;
961 }
962 asdl_seq_APPEND(listcomps, lc);
963 }
964
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000965 return ListComp(elt, listcomps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966}
967
968/*
969 Count the number of 'for' loops in a generator expression.
970
971 Helper for ast_for_genexp().
972*/
973
974static int
975count_gen_fors(const node *n)
976{
977 int n_fors = 0;
978 node *ch = CHILD(n, 1);
979
980 count_gen_for:
981 n_fors++;
982 REQ(ch, gen_for);
983 if (NCH(ch) == 5)
984 ch = CHILD(ch, 4);
985 else
986 return n_fors;
987 count_gen_iter:
988 REQ(ch, gen_iter);
989 ch = CHILD(ch, 0);
990 if (TYPE(ch) == gen_for)
991 goto count_gen_for;
992 else if (TYPE(ch) == gen_if) {
993 if (NCH(ch) == 3) {
994 ch = CHILD(ch, 2);
995 goto count_gen_iter;
996 }
997 else
998 return n_fors;
999 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001000
1001 /* Should never be reached */
1002 PyErr_SetString(PyExc_SystemError,
1003 "logic error in count_gen_fors");
1004 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005}
1006
1007/* Count the number of 'if' statements in a generator expression.
1008
1009 Helper for ast_for_genexp().
1010*/
1011
1012static int
1013count_gen_ifs(const node *n)
1014{
1015 int n_ifs = 0;
1016
1017 while (1) {
1018 REQ(n, gen_iter);
1019 if (TYPE(CHILD(n, 0)) == gen_for)
1020 return n_ifs;
1021 n = CHILD(n, 0);
1022 REQ(n, gen_if);
1023 n_ifs++;
1024 if (NCH(n) == 2)
1025 return n_ifs;
1026 n = CHILD(n, 2);
1027 }
1028}
1029
1030static expr_ty
1031ast_for_genexp(struct compiling *c, const node *n)
1032{
1033 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1034 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1035 expr_ty elt;
1036 asdl_seq *genexps;
1037 int i, n_fors;
1038 node *ch;
1039
1040 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1041 assert(NCH(n) > 1);
1042
1043 elt = ast_for_expr(c, CHILD(n, 0));
1044 if (!elt)
1045 return NULL;
1046
1047 n_fors = count_gen_fors(n);
1048 if (n_fors == -1)
1049 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001050
1051 genexps = asdl_seq_new(n_fors, c->c_arena);
1052 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 ch = CHILD(n, 1);
1056 for (i = 0; i < n_fors; i++) {
1057 comprehension_ty ge;
1058 asdl_seq *t;
1059 expr_ty expression;
1060
1061 REQ(ch, gen_for);
1062
1063 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001064 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001066 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001067 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001069
1070 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001072 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001074 ge = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
1075 expression, NULL, c->c_arena);
1076
1077 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001079
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 if (NCH(ch) == 5) {
1081 int j, n_ifs;
1082 asdl_seq *ifs;
1083
1084 ch = CHILD(ch, 4);
1085 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001086 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001088
1089 ifs = asdl_seq_new(n_ifs, c->c_arena);
1090 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 for (j = 0; j < n_ifs; j++) {
1094 REQ(ch, gen_iter);
1095 ch = CHILD(ch, 0);
1096 REQ(ch, gen_if);
1097
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001098 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001099 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001100 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001101 asdl_seq_APPEND(ifs, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 if (NCH(ch) == 3)
1103 ch = CHILD(ch, 2);
1104 }
1105 /* on exit, must guarantee that ch is a gen_for */
1106 if (TYPE(ch) == gen_iter)
1107 ch = CHILD(ch, 0);
1108 ge->ifs = ifs;
1109 }
1110 asdl_seq_APPEND(genexps, ge);
1111 }
1112
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001113 return GeneratorExp(elt, genexps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114}
1115
1116static expr_ty
1117ast_for_atom(struct compiling *c, const node *n)
1118{
1119 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1120 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1121 */
1122 node *ch = CHILD(n, 0);
1123
1124 switch (TYPE(ch)) {
1125 case NAME:
1126 /* All names start in Load context, but may later be
1127 changed. */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001128 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 case STRING: {
1130 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 if (!str)
1132 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001133
1134 PyArena_AddPyObject(c->c_arena, str);
1135 return Str(str, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 }
1137 case NUMBER: {
1138 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 if (!pynum)
1140 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001141
1142 PyArena_AddPyObject(c->c_arena, pynum);
1143 return Num(pynum, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 }
1145 case LPAR: /* some parenthesized expressions */
1146 ch = CHILD(n, 1);
1147
1148 if (TYPE(ch) == RPAR)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001149 return Tuple(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150
1151 if (TYPE(ch) == yield_expr)
1152 return ast_for_expr(c, ch);
1153
1154 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1155 return ast_for_genexp(c, ch);
1156
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001157 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 case LSQB: /* list (or list comprehension) */
1159 ch = CHILD(n, 1);
1160
1161 if (TYPE(ch) == RSQB)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001162 return List(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163
1164 REQ(ch, listmaker);
1165 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1166 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 if (!elts)
1168 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001169
1170 return List(elts, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 }
1172 else
1173 return ast_for_listcomp(c, ch);
1174 case LBRACE: {
1175 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1176 int i, size;
1177 asdl_seq *keys, *values;
1178
1179 ch = CHILD(n, 1);
1180 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001181 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 if (!keys)
1183 return NULL;
1184
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001185 values = asdl_seq_new(size, c->c_arena);
1186 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188
1189 for (i = 0; i < NCH(ch); i += 4) {
1190 expr_ty expression;
1191
1192 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001193 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001195
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001197
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001199 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001201
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 asdl_seq_SET(values, i / 4, expression);
1203 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204 return Dict(keys, values, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 }
1206 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001207 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 if (!expression)
1209 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001210
1211 return Repr(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 }
1213 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001214 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 return NULL;
1216 }
1217}
1218
1219static slice_ty
1220ast_for_slice(struct compiling *c, const node *n)
1221{
1222 node *ch;
1223 expr_ty lower = NULL, upper = NULL, step = NULL;
1224
1225 REQ(n, subscript);
1226
1227 /*
1228 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1229 sliceop: ':' [test]
1230 */
1231 ch = CHILD(n, 0);
1232 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001233 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234
1235 if (NCH(n) == 1 && TYPE(ch) == test) {
1236 /* 'step' variable hold no significance in terms of being used over
1237 other vars */
1238 step = ast_for_expr(c, ch);
1239 if (!step)
1240 return NULL;
1241
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001242 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 }
1244
1245 if (TYPE(ch) == test) {
1246 lower = ast_for_expr(c, ch);
1247 if (!lower)
1248 return NULL;
1249 }
1250
1251 /* If there's an upper bound it's in the second or third position. */
1252 if (TYPE(ch) == COLON) {
1253 if (NCH(n) > 1) {
1254 node *n2 = CHILD(n, 1);
1255
1256 if (TYPE(n2) == test) {
1257 upper = ast_for_expr(c, n2);
1258 if (!upper)
1259 return NULL;
1260 }
1261 }
1262 } else if (NCH(n) > 2) {
1263 node *n2 = CHILD(n, 2);
1264
1265 if (TYPE(n2) == test) {
1266 upper = ast_for_expr(c, n2);
1267 if (!upper)
1268 return NULL;
1269 }
1270 }
1271
1272 ch = CHILD(n, NCH(n) - 1);
1273 if (TYPE(ch) == sliceop) {
1274 if (NCH(ch) == 1)
1275 /* XXX: If only 1 child, then should just be a colon. Should we
1276 just skip assigning and just get to the return? */
1277 ch = CHILD(ch, 0);
1278 else
1279 ch = CHILD(ch, 1);
1280 if (TYPE(ch) == test) {
1281 step = ast_for_expr(c, ch);
1282 if (!step)
1283 return NULL;
1284 }
1285 }
1286
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001287 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288}
1289
1290static expr_ty
1291ast_for_binop(struct compiling *c, const node *n)
1292{
1293 /* Must account for a sequence of expressions.
1294 How should A op B op C by represented?
1295 BinOp(BinOp(A, op, B), op, C).
1296 */
1297
1298 int i, nops;
1299 expr_ty expr1, expr2, result;
1300 operator_ty operator;
1301
1302 expr1 = ast_for_expr(c, CHILD(n, 0));
1303 if (!expr1)
1304 return NULL;
1305
1306 expr2 = ast_for_expr(c, CHILD(n, 2));
1307 if (!expr2)
1308 return NULL;
1309
1310 operator = get_operator(CHILD(n, 1));
1311 if (!operator)
1312 return NULL;
1313
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001314 result = BinOp(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315 if (!result)
1316 return NULL;
1317
1318 nops = (NCH(n) - 1) / 2;
1319 for (i = 1; i < nops; i++) {
1320 expr_ty tmp_result, tmp;
1321 const node* next_oper = CHILD(n, i * 2 + 1);
1322
1323 operator = get_operator(next_oper);
1324 if (!operator)
1325 return NULL;
1326
1327 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1328 if (!tmp)
1329 return NULL;
1330
1331 tmp_result = BinOp(result, operator, tmp,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001332 LINENO(next_oper), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 if (!tmp)
1334 return NULL;
1335 result = tmp_result;
1336 }
1337 return result;
1338}
1339
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001340static expr_ty
1341ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1342{
1343 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1344 expr_ty e;
1345 REQ(n, trailer);
1346 if (TYPE(CHILD(n, 0)) == LPAR) {
1347 if (NCH(n) == 2)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001348 e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001349 else
1350 e = ast_for_call(c, CHILD(n, 1), left_expr);
1351 }
1352 else if (TYPE(CHILD(n, 0)) == LSQB) {
1353 REQ(CHILD(n, 2), RSQB);
1354 n = CHILD(n, 1);
1355 if (NCH(n) <= 2) {
1356 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1357 if (!slc)
1358 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001359 e = Subscript(left_expr, slc, Load, LINENO(n), c->c_arena);
1360 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001361 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001362 }
1363 else {
1364 int j;
1365 slice_ty slc;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001366 asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001367 if (!slices)
1368 return NULL;
1369 for (j = 0; j < NCH(n); j += 2) {
1370 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001371 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001372 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001373 asdl_seq_SET(slices, j / 2, slc);
1374 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001375 e = Subscript(left_expr, ExtSlice(slices, c->c_arena),
1376 Load, LINENO(n), c->c_arena);
1377 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001378 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001379 }
1380 }
1381 else {
1382 assert(TYPE(CHILD(n, 0)) == DOT);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001383 e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n),
1384 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001385 }
1386 return e;
1387}
1388
1389static expr_ty
1390ast_for_power(struct compiling *c, const node *n)
1391{
1392 /* power: atom trailer* ('**' factor)*
1393 */
1394 int i;
1395 expr_ty e, tmp;
1396 REQ(n, power);
1397 e = ast_for_atom(c, CHILD(n, 0));
1398 if (!e)
1399 return NULL;
1400 if (NCH(n) == 1)
1401 return e;
1402 for (i = 1; i < NCH(n); i++) {
1403 node *ch = CHILD(n, i);
1404 if (TYPE(ch) != trailer)
1405 break;
1406 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001407 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001408 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001409 e = tmp;
1410 }
1411 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1412 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001413 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001414 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001415 tmp = BinOp(e, Pow, f, LINENO(n), c->c_arena);
1416 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001417 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001418 e = tmp;
1419 }
1420 return e;
1421}
1422
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423/* Do not name a variable 'expr'! Will cause a compile error.
1424*/
1425
1426static expr_ty
1427ast_for_expr(struct compiling *c, const node *n)
1428{
1429 /* handle the full range of simple expressions
1430 test: and_test ('or' and_test)* | lambdef
1431 and_test: not_test ('and' not_test)*
1432 not_test: 'not' not_test | comparison
1433 comparison: expr (comp_op expr)*
1434 expr: xor_expr ('|' xor_expr)*
1435 xor_expr: and_expr ('^' and_expr)*
1436 and_expr: shift_expr ('&' shift_expr)*
1437 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1438 arith_expr: term (('+'|'-') term)*
1439 term: factor (('*'|'/'|'%'|'//') factor)*
1440 factor: ('+'|'-'|'~') factor | power
1441 power: atom trailer* ('**' factor)*
1442 */
1443
1444 asdl_seq *seq;
1445 int i;
1446
1447 loop:
1448 switch (TYPE(n)) {
1449 case test:
1450 if (TYPE(CHILD(n, 0)) == lambdef)
1451 return ast_for_lambdef(c, CHILD(n, 0));
1452 /* Fall through to and_test */
1453 case and_test:
1454 if (NCH(n) == 1) {
1455 n = CHILD(n, 0);
1456 goto loop;
1457 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001458 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 if (!seq)
1460 return NULL;
1461 for (i = 0; i < NCH(n); i += 2) {
1462 expr_ty e = ast_for_expr(c, CHILD(n, i));
1463 if (!e)
1464 return NULL;
1465 asdl_seq_SET(seq, i / 2, e);
1466 }
1467 if (!strcmp(STR(CHILD(n, 1)), "and"))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001468 return BoolOp(And, seq, LINENO(n), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001469 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1470 return BoolOp(Or, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 case not_test:
1472 if (NCH(n) == 1) {
1473 n = CHILD(n, 0);
1474 goto loop;
1475 }
1476 else {
1477 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1478 if (!expression)
1479 return NULL;
1480
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001481 return UnaryOp(Not, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 }
1483 case comparison:
1484 if (NCH(n) == 1) {
1485 n = CHILD(n, 0);
1486 goto loop;
1487 }
1488 else {
1489 expr_ty expression;
1490 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001491 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492 if (!ops)
1493 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001494 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 return NULL;
1497 }
1498 for (i = 1; i < NCH(n); i += 2) {
1499 /* XXX cmpop_ty is just an enum */
1500 cmpop_ty operator;
1501
1502 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001503 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001505 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506
1507 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001508 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001512 asdl_seq_SET(ops, i / 2, (void *)(Py_uintptr_t)operator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 asdl_seq_SET(cmps, i / 2, expression);
1514 }
1515 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001516 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001518 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001520 return Compare(expression, ops, cmps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 }
1522 break;
1523
1524 /* The next five cases all handle BinOps. The main body of code
1525 is the same in each case, but the switch turned inside out to
1526 reuse the code for each type of operator.
1527 */
1528 case expr:
1529 case xor_expr:
1530 case and_expr:
1531 case shift_expr:
1532 case arith_expr:
1533 case term:
1534 if (NCH(n) == 1) {
1535 n = CHILD(n, 0);
1536 goto loop;
1537 }
1538 return ast_for_binop(c, n);
1539 case yield_expr: {
1540 expr_ty exp = NULL;
1541 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001542 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 if (!exp)
1544 return NULL;
1545 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001546 return Yield(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 }
1548 case factor: {
1549 expr_ty expression;
1550
1551 if (NCH(n) == 1) {
1552 n = CHILD(n, 0);
1553 goto loop;
1554 }
1555
1556 expression = ast_for_expr(c, CHILD(n, 1));
1557 if (!expression)
1558 return NULL;
1559
1560 switch (TYPE(CHILD(n, 0))) {
1561 case PLUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001562 return UnaryOp(UAdd, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 case MINUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001564 return UnaryOp(USub, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 case TILDE:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001566 return UnaryOp(Invert, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001568 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1569 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 break;
1571 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001572 case power:
1573 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001575 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576 return NULL;
1577 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001578 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 return NULL;
1580}
1581
1582static expr_ty
1583ast_for_call(struct compiling *c, const node *n, expr_ty func)
1584{
1585 /*
1586 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1587 | '**' test)
1588 argument: [test '='] test [gen_for] # Really [keyword '='] test
1589 */
1590
1591 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001592 asdl_seq *args;
1593 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594 expr_ty vararg = NULL, kwarg = NULL;
1595
1596 REQ(n, arglist);
1597
1598 nargs = 0;
1599 nkeywords = 0;
1600 ngens = 0;
1601 for (i = 0; i < NCH(n); i++) {
1602 node *ch = CHILD(n, i);
1603 if (TYPE(ch) == argument) {
1604 if (NCH(ch) == 1)
1605 nargs++;
1606 else if (TYPE(CHILD(ch, 1)) == gen_for)
1607 ngens++;
1608 else
1609 nkeywords++;
1610 }
1611 }
1612 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
1613 ast_error(n, "Generator expression must be parenthesised "
1614 "if not sole argument");
1615 return NULL;
1616 }
1617
1618 if (nargs + nkeywords + ngens > 255) {
1619 ast_error(n, "more than 255 arguments");
1620 return NULL;
1621 }
1622
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001623 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001625 return NULL;
1626 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001628 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 nargs = 0;
1630 nkeywords = 0;
1631 for (i = 0; i < NCH(n); i++) {
1632 node *ch = CHILD(n, i);
1633 if (TYPE(ch) == argument) {
1634 expr_ty e;
1635 if (NCH(ch) == 1) {
1636 e = ast_for_expr(c, CHILD(ch, 0));
1637 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001638 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639 asdl_seq_SET(args, nargs++, e);
1640 }
1641 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1642 e = ast_for_genexp(c, ch);
1643 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001644 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 asdl_seq_SET(args, nargs++, e);
1646 }
1647 else {
1648 keyword_ty kw;
1649 identifier key;
1650
1651 /* CHILD(ch, 0) is test, but must be an identifier? */
1652 e = ast_for_expr(c, CHILD(ch, 0));
1653 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001654 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 /* f(lambda x: x[0] = 3) ends up getting parsed with
1656 * LHS test = lambda x: x[0], and RHS test = 3.
1657 * SF bug 132313 points out that complaining about a keyword
1658 * then is very confusing.
1659 */
1660 if (e->kind == Lambda_kind) {
1661 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001662 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663 } else if (e->kind != Name_kind) {
1664 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001665 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666 }
1667 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 e = ast_for_expr(c, CHILD(ch, 2));
1669 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001670 return NULL;
1671 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001673 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 asdl_seq_SET(keywords, nkeywords++, kw);
1675 }
1676 }
1677 else if (TYPE(ch) == STAR) {
1678 vararg = ast_for_expr(c, CHILD(n, i+1));
1679 i++;
1680 }
1681 else if (TYPE(ch) == DOUBLESTAR) {
1682 kwarg = ast_for_expr(c, CHILD(n, i+1));
1683 i++;
1684 }
1685 }
1686
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001687 return Call(func, args, keywords, vararg, kwarg, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688}
1689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001691ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001693 /* testlist_gexp: test (',' test)* [','] */
1694 /* testlist: test (',' test)* [','] */
1695 /* testlist_safe: test (',' test)+ [','] */
1696 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001698 if (TYPE(n) == testlist_gexp) {
1699 if (NCH(n) > 1)
1700 assert(TYPE(CHILD(n, 1)) != gen_for);
1701 }
1702 else {
1703 assert(TYPE(n) == testlist ||
1704 TYPE(n) == testlist_safe ||
1705 TYPE(n) == testlist1);
1706 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 if (NCH(n) == 1)
1708 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 else {
1710 asdl_seq *tmp = seq_for_testlist(c, n);
1711 if (!tmp)
1712 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001713 return Tuple(tmp, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001715}
1716
1717static expr_ty
1718ast_for_testlist_gexp(struct compiling *c, const node* n)
1719{
1720 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1721 /* argument: test [ gen_for ] */
1722 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001723 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001724 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001725 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001726}
1727
1728/* like ast_for_testlist() but returns a sequence */
1729static asdl_seq*
1730ast_for_class_bases(struct compiling *c, const node* n)
1731{
1732 /* testlist: test (',' test)* [','] */
1733 assert(NCH(n) > 0);
1734 REQ(n, testlist);
1735 if (NCH(n) == 1) {
1736 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001737 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001738 if (!bases)
1739 return NULL;
1740 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001741 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001742 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001743 asdl_seq_SET(bases, 0, base);
1744 return bases;
1745 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001746
1747 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748}
1749
1750static stmt_ty
1751ast_for_expr_stmt(struct compiling *c, const node *n)
1752{
1753 REQ(n, expr_stmt);
1754 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1755 | ('=' (yield_expr|testlist))*)
1756 testlist: test (',' test)* [',']
1757 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1758 | '<<=' | '>>=' | '**=' | '//='
1759 test: ... here starts the operator precendence dance
1760 */
1761
1762 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001763 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764 if (!e)
1765 return NULL;
1766
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001767 return Expr(e, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 }
1769 else if (TYPE(CHILD(n, 1)) == augassign) {
1770 expr_ty expr1, expr2;
1771 operator_ty operator;
1772 node *ch = CHILD(n, 0);
1773
1774 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001775 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001777 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch),
1778 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779
1780 if (!expr1)
1781 return NULL;
1782 if (expr1->kind == GeneratorExp_kind) {
1783 ast_error(ch, "augmented assignment to generator "
1784 "expression not possible");
1785 return NULL;
1786 }
1787 if (expr1->kind == Name_kind) {
1788 char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1789 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1790 ast_error(ch, "assignment to None");
1791 return NULL;
1792 }
1793 }
1794
1795 ch = CHILD(n, 2);
1796 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001797 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001799 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001800 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801 return NULL;
1802
1803 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001804 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 return NULL;
1806
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001807 return AugAssign(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 }
1809 else {
1810 int i;
1811 asdl_seq *targets;
1812 node *value;
1813 expr_ty expression;
1814
1815 /* a normal assignment */
1816 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 if (!targets)
1819 return NULL;
1820 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001821 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 node *ch = CHILD(n, i);
1823 if (TYPE(ch) == yield_expr) {
1824 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001825 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001827 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828
1829 /* set context to assign */
1830 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001831 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832
Neal Norwitz84456bd2005-12-18 03:16:20 +00001833 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835
1836 asdl_seq_SET(targets, i / 2, e);
1837 }
1838 value = CHILD(n, NCH(n) - 1);
1839 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001840 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 else
1842 expression = ast_for_expr(c, value);
1843 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844 return NULL;
1845 return Assign(targets, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847}
1848
1849static stmt_ty
1850ast_for_print_stmt(struct compiling *c, const node *n)
1851{
1852 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1853 | '>>' test [ (',' test)+ [','] ] )
1854 */
1855 expr_ty dest = NULL, expression;
1856 asdl_seq *seq;
1857 bool nl;
1858 int i, start = 1;
1859
1860 REQ(n, print_stmt);
1861 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1862 dest = ast_for_expr(c, CHILD(n, 2));
1863 if (!dest)
1864 return NULL;
1865 start = 4;
1866 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001867 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 if (!seq)
1869 return NULL;
1870 for (i = start; i < NCH(n); i += 2) {
1871 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001872 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874
1875 asdl_seq_APPEND(seq, expression);
1876 }
1877 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001878 return Print(dest, seq, nl, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879}
1880
1881static asdl_seq *
1882ast_for_exprlist(struct compiling *c, const node *n, int context)
1883{
1884 asdl_seq *seq;
1885 int i;
1886 expr_ty e;
1887
1888 REQ(n, exprlist);
1889
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001890 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 if (!seq)
1892 return NULL;
1893 for (i = 0; i < NCH(n); i += 2) {
1894 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001895 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001896 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00001897 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001898 if (context && !set_context(e, context, CHILD(n, i)))
1899 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 }
1901 return seq;
1902}
1903
1904static stmt_ty
1905ast_for_del_stmt(struct compiling *c, const node *n)
1906{
1907 asdl_seq *expr_list;
1908
1909 /* del_stmt: 'del' exprlist */
1910 REQ(n, del_stmt);
1911
1912 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
1913 if (!expr_list)
1914 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001915 return Delete(expr_list, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916}
1917
1918static stmt_ty
1919ast_for_flow_stmt(struct compiling *c, const node *n)
1920{
1921 /*
1922 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
1923 | yield_stmt
1924 break_stmt: 'break'
1925 continue_stmt: 'continue'
1926 return_stmt: 'return' [testlist]
1927 yield_stmt: yield_expr
1928 yield_expr: 'yield' testlist
1929 raise_stmt: 'raise' [test [',' test [',' test]]]
1930 */
1931 node *ch;
1932
1933 REQ(n, flow_stmt);
1934 ch = CHILD(n, 0);
1935 switch (TYPE(ch)) {
1936 case break_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001937 return Break(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938 case continue_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001939 return Continue(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 case yield_stmt: { /* will reduce to yield_expr */
1941 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
1942 if (!exp)
1943 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001944 return Expr(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 }
1946 case return_stmt:
1947 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001948 return Return(NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001950 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 if (!expression)
1952 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001953 return Return(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 }
1955 case raise_stmt:
1956 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001957 return Raise(NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 else if (NCH(ch) == 2) {
1959 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
1960 if (!expression)
1961 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001962 return Raise(expression, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963 }
1964 else if (NCH(ch) == 4) {
1965 expr_ty expr1, expr2;
1966
1967 expr1 = ast_for_expr(c, CHILD(ch, 1));
1968 if (!expr1)
1969 return NULL;
1970 expr2 = ast_for_expr(c, CHILD(ch, 3));
1971 if (!expr2)
1972 return NULL;
1973
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001974 return Raise(expr1, expr2, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975 }
1976 else if (NCH(ch) == 6) {
1977 expr_ty expr1, expr2, expr3;
1978
1979 expr1 = ast_for_expr(c, CHILD(ch, 1));
1980 if (!expr1)
1981 return NULL;
1982 expr2 = ast_for_expr(c, CHILD(ch, 3));
1983 if (!expr2)
1984 return NULL;
1985 expr3 = ast_for_expr(c, CHILD(ch, 5));
1986 if (!expr3)
1987 return NULL;
1988
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001989 return Raise(expr1, expr2, expr3, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 }
1991 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001992 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 "unexpected flow_stmt: %d", TYPE(ch));
1994 return NULL;
1995 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001996
1997 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
1998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999}
2000
2001static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002002alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003{
2004 /*
2005 import_as_name: NAME [NAME NAME]
2006 dotted_as_name: dotted_name [NAME NAME]
2007 dotted_name: NAME ('.' NAME)*
2008 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002009 PyObject *str;
2010
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 loop:
2012 switch (TYPE(n)) {
2013 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002014 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2015 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 case dotted_as_name:
2017 if (NCH(n) == 1) {
2018 n = CHILD(n, 0);
2019 goto loop;
2020 }
2021 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002022 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 assert(!a->asname);
2024 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2025 return a;
2026 }
2027 break;
2028 case dotted_name:
2029 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002030 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 else {
2032 /* Create a string of the form "a.b.c" */
Neal Norwitz46b7bda2006-01-08 01:06:06 +00002033 size_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 char *s;
2035
2036 len = 0;
2037 for (i = 0; i < NCH(n); i += 2)
2038 /* length of string plus one for the dot */
2039 len += strlen(STR(CHILD(n, i))) + 1;
2040 len--; /* the last name doesn't have a dot */
2041 str = PyString_FromStringAndSize(NULL, len);
2042 if (!str)
2043 return NULL;
2044 s = PyString_AS_STRING(str);
2045 if (!s)
2046 return NULL;
2047 for (i = 0; i < NCH(n); i += 2) {
2048 char *sch = STR(CHILD(n, i));
2049 strcpy(s, STR(CHILD(n, i)));
2050 s += strlen(sch);
2051 *s++ = '.';
2052 }
2053 --s;
2054 *s = '\0';
2055 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002056 PyArena_AddPyObject(c->c_arena, str);
2057 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 }
2059 break;
2060 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002061 str = PyString_InternFromString("*");
2062 PyArena_AddPyObject(c->c_arena, str);
2063 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002065 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 "unexpected import name: %d", TYPE(n));
2067 return NULL;
2068 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002069
2070 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 return NULL;
2072}
2073
2074static stmt_ty
2075ast_for_import_stmt(struct compiling *c, const node *n)
2076{
2077 /*
2078 import_stmt: import_name | import_from
2079 import_name: 'import' dotted_as_names
2080 import_from: 'from' dotted_name 'import' ('*' |
2081 '(' import_as_names ')' |
2082 import_as_names)
2083 */
2084 int i;
2085 asdl_seq *aliases;
2086
2087 REQ(n, import_stmt);
2088 n = CHILD(n, 0);
2089 if (STR(CHILD(n, 0))[0] == 'i') { /* import */
2090 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002091 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002092 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 if (!aliases)
2094 return NULL;
2095 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002096 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002097 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 asdl_seq_SET(aliases, i / 2, import_alias);
2100 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002101 return Import(aliases, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 }
2103 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 int n_children;
2105 const char *from_modules;
2106 int lineno = LINENO(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002107 alias_ty mod = alias_for_import_name(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 if (!mod)
2109 return NULL;
2110
2111 /* XXX this needs to be cleaned up */
2112
2113 from_modules = STR(CHILD(n, 3));
2114 if (!from_modules) {
2115 n = CHILD(n, 3); /* from ... import x, y, z */
2116 if (NCH(n) % 2 == 0) {
2117 /* it ends with a comma, not valid but the parser allows it */
2118 ast_error(n, "trailing comma not allowed without"
2119 " surrounding parentheses");
2120 return NULL;
2121 }
2122 }
2123 else if (from_modules[0] == '*') {
2124 n = CHILD(n, 3); /* from ... import * */
2125 }
2126 else if (from_modules[0] == '(')
2127 n = CHILD(n, 4); /* from ... import (x, y, z) */
Neal Norwitze76adcd2005-11-15 05:04:31 +00002128 else {
2129 /* XXX: don't we need to call ast_error(n, "..."); */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002131 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132
2133 n_children = NCH(n);
2134 if (from_modules && from_modules[0] == '*')
2135 n_children = 1;
2136
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002137 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002138 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140
2141 /* handle "from ... import *" special b/c there's no children */
2142 if (from_modules && from_modules[0] == '*') {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002143 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002144 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 asdl_seq_APPEND(aliases, import_alias);
2147 }
2148
2149 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002150 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002151 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 asdl_seq_APPEND(aliases, import_alias);
2154 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002155 return ImportFrom(mod->name, aliases, lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156 }
Neal Norwitz79792652005-11-14 04:25:03 +00002157 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 "unknown import statement: starts with command '%s'",
2159 STR(CHILD(n, 0)));
2160 return NULL;
2161}
2162
2163static stmt_ty
2164ast_for_global_stmt(struct compiling *c, const node *n)
2165{
2166 /* global_stmt: 'global' NAME (',' NAME)* */
2167 identifier name;
2168 asdl_seq *s;
2169 int i;
2170
2171 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002172 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 if (!s)
2174 return NULL;
2175 for (i = 1; i < NCH(n); i += 2) {
2176 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002177 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 asdl_seq_SET(s, i / 2, name);
2180 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002181 return Global(s, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182}
2183
2184static stmt_ty
2185ast_for_exec_stmt(struct compiling *c, const node *n)
2186{
2187 expr_ty expr1, globals = NULL, locals = NULL;
2188 int n_children = NCH(n);
2189 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002190 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 "poorly formed 'exec' statement: %d parts to statement",
2192 n_children);
2193 return NULL;
2194 }
2195
2196 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2197 REQ(n, exec_stmt);
2198 expr1 = ast_for_expr(c, CHILD(n, 1));
2199 if (!expr1)
2200 return NULL;
2201 if (n_children >= 4) {
2202 globals = ast_for_expr(c, CHILD(n, 3));
2203 if (!globals)
2204 return NULL;
2205 }
2206 if (n_children == 6) {
2207 locals = ast_for_expr(c, CHILD(n, 5));
2208 if (!locals)
2209 return NULL;
2210 }
2211
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002212 return Exec(expr1, globals, locals, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213}
2214
2215static stmt_ty
2216ast_for_assert_stmt(struct compiling *c, const node *n)
2217{
2218 /* assert_stmt: 'assert' test [',' test] */
2219 REQ(n, assert_stmt);
2220 if (NCH(n) == 2) {
2221 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2222 if (!expression)
2223 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002224 return Assert(expression, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 }
2226 else if (NCH(n) == 4) {
2227 expr_ty expr1, expr2;
2228
2229 expr1 = ast_for_expr(c, CHILD(n, 1));
2230 if (!expr1)
2231 return NULL;
2232 expr2 = ast_for_expr(c, CHILD(n, 3));
2233 if (!expr2)
2234 return NULL;
2235
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002236 return Assert(expr1, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 }
Neal Norwitz79792652005-11-14 04:25:03 +00002238 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 "improper number of parts to 'assert' statement: %d",
2240 NCH(n));
2241 return NULL;
2242}
2243
2244static asdl_seq *
2245ast_for_suite(struct compiling *c, const node *n)
2246{
2247 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002248 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 stmt_ty s;
2250 int i, total, num, end, pos = 0;
2251 node *ch;
2252
2253 REQ(n, suite);
2254
2255 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002256 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 if (!seq)
2258 return NULL;
2259 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2260 n = CHILD(n, 0);
2261 /* simple_stmt always ends with a NEWLINE,
2262 and may have a trailing SEMI
2263 */
2264 end = NCH(n) - 1;
2265 if (TYPE(CHILD(n, end - 1)) == SEMI)
2266 end--;
2267 /* loop by 2 to skip semi-colons */
2268 for (i = 0; i < end; i += 2) {
2269 ch = CHILD(n, i);
2270 s = ast_for_stmt(c, ch);
2271 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002272 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 asdl_seq_SET(seq, pos++, s);
2274 }
2275 }
2276 else {
2277 for (i = 2; i < (NCH(n) - 1); i++) {
2278 ch = CHILD(n, i);
2279 REQ(ch, stmt);
2280 num = num_stmts(ch);
2281 if (num == 1) {
2282 /* small_stmt or compound_stmt with only one child */
2283 s = ast_for_stmt(c, ch);
2284 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002285 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 asdl_seq_SET(seq, pos++, s);
2287 }
2288 else {
2289 int j;
2290 ch = CHILD(ch, 0);
2291 REQ(ch, simple_stmt);
2292 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002293 /* statement terminates with a semi-colon ';' */
2294 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002295 assert((j + 1) == NCH(ch));
2296 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002297 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 s = ast_for_stmt(c, CHILD(ch, j));
2299 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002300 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 asdl_seq_SET(seq, pos++, s);
2302 }
2303 }
2304 }
2305 }
2306 assert(pos == seq->size);
2307 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308}
2309
2310static stmt_ty
2311ast_for_if_stmt(struct compiling *c, const node *n)
2312{
2313 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2314 ['else' ':' suite]
2315 */
2316 char *s;
2317
2318 REQ(n, if_stmt);
2319
2320 if (NCH(n) == 4) {
2321 expr_ty expression;
2322 asdl_seq *suite_seq;
2323
2324 expression = ast_for_expr(c, CHILD(n, 1));
2325 if (!expression)
2326 return NULL;
2327 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002328 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002329 return NULL;
2330
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002331 return If(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 s = STR(CHILD(n, 4));
2335 /* s[2], the third character in the string, will be
2336 's' for el_s_e, or
2337 'i' for el_i_f
2338 */
2339 if (s[2] == 's') {
2340 expr_ty expression;
2341 asdl_seq *seq1, *seq2;
2342
2343 expression = ast_for_expr(c, CHILD(n, 1));
2344 if (!expression)
2345 return NULL;
2346 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002347 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 return NULL;
2349 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002350 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 return NULL;
2352
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002353 return If(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 }
2355 else if (s[2] == 'i') {
2356 int i, n_elif, has_else = 0;
2357 asdl_seq *orelse = NULL;
2358 n_elif = NCH(n) - 4;
2359 /* must reference the child n_elif+1 since 'else' token is third,
2360 not fourth, child from the end. */
2361 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2362 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2363 has_else = 1;
2364 n_elif -= 3;
2365 }
2366 n_elif /= 4;
2367
2368 if (has_else) {
2369 expr_ty expression;
2370 asdl_seq *seq1, *seq2;
2371
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002372 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 if (!orelse)
2374 return NULL;
2375 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002376 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002379 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002382 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384
2385 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002386 LINENO(CHILD(n, NCH(n) - 6)),
2387 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 /* the just-created orelse handled the last elif */
2389 n_elif--;
2390 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391
2392 for (i = 0; i < n_elif; i++) {
2393 int off = 5 + (n_elif - i - 1) * 4;
2394 expr_ty expression;
2395 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002396 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002397 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 return NULL;
2399 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002400 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002403 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405
2406 asdl_seq_SET(new, 0,
2407 If(expression, suite_seq, orelse,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002408 LINENO(CHILD(n, off)), c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409 orelse = new;
2410 }
2411 return If(ast_for_expr(c, CHILD(n, 1)),
2412 ast_for_suite(c, CHILD(n, 3)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002413 orelse, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002415
2416 PyErr_Format(PyExc_SystemError,
2417 "unexpected token in 'if' statement: %s", s);
2418 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002419}
2420
2421static stmt_ty
2422ast_for_while_stmt(struct compiling *c, const node *n)
2423{
2424 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2425 REQ(n, while_stmt);
2426
2427 if (NCH(n) == 4) {
2428 expr_ty expression;
2429 asdl_seq *suite_seq;
2430
2431 expression = ast_for_expr(c, CHILD(n, 1));
2432 if (!expression)
2433 return NULL;
2434 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002435 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002437 return While(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 }
2439 else if (NCH(n) == 7) {
2440 expr_ty expression;
2441 asdl_seq *seq1, *seq2;
2442
2443 expression = ast_for_expr(c, CHILD(n, 1));
2444 if (!expression)
2445 return NULL;
2446 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002447 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 return NULL;
2449 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002450 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 return NULL;
2452
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002453 return While(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002455
2456 PyErr_Format(PyExc_SystemError,
2457 "wrong number of tokens for 'while' statement: %d",
2458 NCH(n));
2459 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460}
2461
2462static stmt_ty
2463ast_for_for_stmt(struct compiling *c, const node *n)
2464{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002465 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 expr_ty expression;
2467 expr_ty target;
2468 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2469 REQ(n, for_stmt);
2470
2471 if (NCH(n) == 9) {
2472 seq = ast_for_suite(c, CHILD(n, 8));
2473 if (!seq)
2474 return NULL;
2475 }
2476
2477 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002478 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002480 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002483 target = Tuple(_target, Store, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002485 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002486 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 return NULL;
2488 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002489 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 return NULL;
2491
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002492 return For(target, expression, suite_seq, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493}
2494
2495static excepthandler_ty
2496ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2497{
2498 /* except_clause: 'except' [test [',' test]] */
2499 REQ(exc, except_clause);
2500 REQ(body, suite);
2501
2502 if (NCH(exc) == 1) {
2503 asdl_seq *suite_seq = ast_for_suite(c, body);
2504 if (!suite_seq)
2505 return NULL;
2506
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002507 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 }
2509 else if (NCH(exc) == 2) {
2510 expr_ty expression;
2511 asdl_seq *suite_seq;
2512
2513 expression = ast_for_expr(c, CHILD(exc, 1));
2514 if (!expression)
2515 return NULL;
2516 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002517 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 return NULL;
2519
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002520 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 }
2522 else if (NCH(exc) == 4) {
2523 asdl_seq *suite_seq;
2524 expr_ty expression;
2525 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2526 if (!e)
2527 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002528 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 return NULL;
2530 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002531 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 return NULL;
2533 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002534 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 return NULL;
2536
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002537 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002539
2540 PyErr_Format(PyExc_SystemError,
2541 "wrong number of children for 'except' clause: %d",
2542 NCH(exc));
2543 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544}
2545
2546static stmt_ty
2547ast_for_try_stmt(struct compiling *c, const node *n)
2548{
Neal Norwitzf599f422005-12-17 21:33:47 +00002549 const int nch = NCH(n);
2550 int n_except = (nch - 3)/3;
2551 asdl_seq *body, *orelse = NULL, *finally = NULL;
2552
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 REQ(n, try_stmt);
2554
Neal Norwitzf599f422005-12-17 21:33:47 +00002555 body = ast_for_suite(c, CHILD(n, 2));
2556 if (body == NULL)
2557 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558
Neal Norwitzf599f422005-12-17 21:33:47 +00002559 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2560 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2561 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2562 /* we can assume it's an "else",
2563 because nch >= 9 for try-else-finally and
2564 it would otherwise have a type of except_clause */
2565 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2566 if (orelse == NULL)
2567 return NULL;
2568 n_except--;
2569 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570
Neal Norwitzf599f422005-12-17 21:33:47 +00002571 finally = ast_for_suite(c, CHILD(n, nch - 1));
2572 if (finally == NULL)
2573 return NULL;
2574 n_except--;
2575 }
2576 else {
2577 /* we can assume it's an "else",
2578 otherwise it would have a type of except_clause */
2579 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2580 if (orelse == NULL)
2581 return NULL;
2582 n_except--;
2583 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002585 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002586 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 return NULL;
2588 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002589
2590 if (n_except > 0) {
2591 int i;
2592 stmt_ty except_st;
2593 /* process except statements to create a try ... except */
2594 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2595 if (handlers == NULL)
2596 return NULL;
2597
2598 for (i = 0; i < n_except; i++) {
2599 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2600 CHILD(n, 5 + i * 3));
2601 if (!e)
2602 return NULL;
2603 asdl_seq_SET(handlers, i, e);
2604 }
2605
2606 except_st = TryExcept(body, handlers, orelse, LINENO(n), c->c_arena);
2607 if (!finally)
2608 return except_st;
2609
2610 /* if a 'finally' is present too, we nest the TryExcept within a
2611 TryFinally to emulate try ... except ... finally */
2612 body = asdl_seq_new(1, c->c_arena);
2613 if (body == NULL)
2614 return NULL;
2615 asdl_seq_SET(body, 0, except_st);
2616 }
2617
2618 /* must be a try ... finally (except clauses are in body, if any exist) */
2619 assert(finally != NULL);
2620 return TryFinally(body, finally, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621}
2622
2623static stmt_ty
2624ast_for_classdef(struct compiling *c, const node *n)
2625{
2626 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 asdl_seq *bases, *s;
2628
2629 REQ(n, classdef);
2630
2631 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2632 ast_error(n, "assignment to None");
2633 return NULL;
2634 }
2635
2636 if (NCH(n) == 4) {
2637 s = ast_for_suite(c, CHILD(n, 3));
2638 if (!s)
2639 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002640 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2641 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 }
2643 /* check for empty base list */
2644 if (TYPE(CHILD(n,3)) == RPAR) {
2645 s = ast_for_suite(c, CHILD(n,5));
2646 if (!s)
2647 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002648 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2649 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 }
2651
2652 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002653 bases = ast_for_class_bases(c, CHILD(n, 3));
2654 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656
2657 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002658 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002660 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2661 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662}
2663
2664static stmt_ty
2665ast_for_stmt(struct compiling *c, const node *n)
2666{
2667 if (TYPE(n) == stmt) {
2668 assert(NCH(n) == 1);
2669 n = CHILD(n, 0);
2670 }
2671 if (TYPE(n) == simple_stmt) {
2672 assert(num_stmts(n) == 1);
2673 n = CHILD(n, 0);
2674 }
2675 if (TYPE(n) == small_stmt) {
2676 REQ(n, small_stmt);
2677 n = CHILD(n, 0);
2678 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2679 | flow_stmt | import_stmt | global_stmt | exec_stmt
2680 | assert_stmt
2681 */
2682 switch (TYPE(n)) {
2683 case expr_stmt:
2684 return ast_for_expr_stmt(c, n);
2685 case print_stmt:
2686 return ast_for_print_stmt(c, n);
2687 case del_stmt:
2688 return ast_for_del_stmt(c, n);
2689 case pass_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002690 return Pass(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 case flow_stmt:
2692 return ast_for_flow_stmt(c, n);
2693 case import_stmt:
2694 return ast_for_import_stmt(c, n);
2695 case global_stmt:
2696 return ast_for_global_stmt(c, n);
2697 case exec_stmt:
2698 return ast_for_exec_stmt(c, n);
2699 case assert_stmt:
2700 return ast_for_assert_stmt(c, n);
2701 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002702 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2704 TYPE(n), NCH(n));
2705 return NULL;
2706 }
2707 }
2708 else {
2709 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2710 | funcdef | classdef
2711 */
2712 node *ch = CHILD(n, 0);
2713 REQ(n, compound_stmt);
2714 switch (TYPE(ch)) {
2715 case if_stmt:
2716 return ast_for_if_stmt(c, ch);
2717 case while_stmt:
2718 return ast_for_while_stmt(c, ch);
2719 case for_stmt:
2720 return ast_for_for_stmt(c, ch);
2721 case try_stmt:
2722 return ast_for_try_stmt(c, ch);
2723 case funcdef:
2724 return ast_for_funcdef(c, ch);
2725 case classdef:
2726 return ast_for_classdef(c, ch);
2727 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002728 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2730 TYPE(n), NCH(n));
2731 return NULL;
2732 }
2733 }
2734}
2735
2736static PyObject *
2737parsenumber(const char *s)
2738{
2739 const char *end;
2740 long x;
2741 double dx;
2742#ifndef WITHOUT_COMPLEX
2743 Py_complex c;
2744 int imflag;
2745#endif
2746
2747 errno = 0;
2748 end = s + strlen(s) - 1;
2749#ifndef WITHOUT_COMPLEX
2750 imflag = *end == 'j' || *end == 'J';
2751#endif
2752 if (*end == 'l' || *end == 'L')
2753 return PyLong_FromString((char *)s, (char **)0, 0);
2754 if (s[0] == '0') {
2755 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2756 if (x < 0 && errno == 0) {
2757 return PyLong_FromString((char *)s,
2758 (char **)0,
2759 0);
2760 }
2761 }
2762 else
2763 x = PyOS_strtol((char *)s, (char **)&end, 0);
2764 if (*end == '\0') {
2765 if (errno != 0)
2766 return PyLong_FromString((char *)s, (char **)0, 0);
2767 return PyInt_FromLong(x);
2768 }
2769 /* XXX Huge floats may silently fail */
2770#ifndef WITHOUT_COMPLEX
2771 if (imflag) {
2772 c.real = 0.;
2773 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002774 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 PyFPE_END_PROTECT(c)
2776 return PyComplex_FromCComplex(c);
2777 }
2778 else
2779#endif
2780 {
2781 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002782 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 PyFPE_END_PROTECT(dx)
2784 return PyFloat_FromDouble(dx);
2785 }
2786}
2787
2788static PyObject *
2789decode_utf8(const char **sPtr, const char *end, char* encoding)
2790{
2791#ifndef Py_USING_UNICODE
2792 Py_FatalError("decode_utf8 should not be called in this build.");
2793 return NULL;
2794#else
2795 PyObject *u, *v;
2796 char *s, *t;
2797 t = s = (char *)*sPtr;
2798 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2799 while (s < end && (*s & 0x80)) s++;
2800 *sPtr = s;
2801 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2802 if (u == NULL)
2803 return NULL;
2804 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2805 Py_DECREF(u);
2806 return v;
2807#endif
2808}
2809
2810static PyObject *
2811decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2812{
2813 PyObject *v, *u;
2814 char *buf;
2815 char *p;
2816 const char *end;
2817 if (encoding == NULL) {
2818 buf = (char *)s;
2819 u = NULL;
2820 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2821 buf = (char *)s;
2822 u = NULL;
2823 } else {
2824 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2825 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2826 if (u == NULL)
2827 return NULL;
2828 p = buf = PyString_AsString(u);
2829 end = s + len;
2830 while (s < end) {
2831 if (*s == '\\') {
2832 *p++ = *s++;
2833 if (*s & 0x80) {
2834 strcpy(p, "u005c");
2835 p += 5;
2836 }
2837 }
2838 if (*s & 0x80) { /* XXX inefficient */
2839 PyObject *w;
2840 char *r;
2841 int rn, i;
2842 w = decode_utf8(&s, end, "utf-16-be");
2843 if (w == NULL) {
2844 Py_DECREF(u);
2845 return NULL;
2846 }
2847 r = PyString_AsString(w);
2848 rn = PyString_Size(w);
2849 assert(rn % 2 == 0);
2850 for (i = 0; i < rn; i += 2) {
2851 sprintf(p, "\\u%02x%02x",
2852 r[i + 0] & 0xFF,
2853 r[i + 1] & 0xFF);
2854 p += 6;
2855 }
2856 Py_DECREF(w);
2857 } else {
2858 *p++ = *s++;
2859 }
2860 }
2861 len = p - buf;
2862 s = buf;
2863 }
2864 if (rawmode)
2865 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
2866 else
2867 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
2868 Py_XDECREF(u);
2869 return v;
2870}
2871
2872/* s is a Python string literal, including the bracketing quote characters,
2873 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
2874 * parsestr parses it, and returns the decoded Python string object.
2875 */
2876static PyObject *
2877parsestr(const char *s, const char *encoding)
2878{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00002880 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 int rawmode = 0;
2882 int need_encoding;
2883 int unicode = 0;
2884
2885 if (isalpha(quote) || quote == '_') {
2886 if (quote == 'u' || quote == 'U') {
2887 quote = *++s;
2888 unicode = 1;
2889 }
2890 if (quote == 'r' || quote == 'R') {
2891 quote = *++s;
2892 rawmode = 1;
2893 }
2894 }
2895 if (quote != '\'' && quote != '\"') {
2896 PyErr_BadInternalCall();
2897 return NULL;
2898 }
2899 s++;
2900 len = strlen(s);
2901 if (len > INT_MAX) {
2902 PyErr_SetString(PyExc_OverflowError,
2903 "string to parse is too long");
2904 return NULL;
2905 }
2906 if (s[--len] != quote) {
2907 PyErr_BadInternalCall();
2908 return NULL;
2909 }
2910 if (len >= 4 && s[0] == quote && s[1] == quote) {
2911 s += 2;
2912 len -= 2;
2913 if (s[--len] != quote || s[--len] != quote) {
2914 PyErr_BadInternalCall();
2915 return NULL;
2916 }
2917 }
2918#ifdef Py_USING_UNICODE
2919 if (unicode || Py_UnicodeFlag) {
2920 return decode_unicode(s, len, rawmode, encoding);
2921 }
2922#endif
2923 need_encoding = (encoding != NULL &&
2924 strcmp(encoding, "utf-8") != 0 &&
2925 strcmp(encoding, "iso-8859-1") != 0);
2926 if (rawmode || strchr(s, '\\') == NULL) {
2927 if (need_encoding) {
2928#ifndef Py_USING_UNICODE
2929 /* This should not happen - we never see any other
2930 encoding. */
2931 Py_FatalError("cannot deal with encodings in this build.");
2932#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002933 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934 if (u == NULL)
2935 return NULL;
2936 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2937 Py_DECREF(u);
2938 return v;
2939#endif
2940 } else {
2941 return PyString_FromStringAndSize(s, len);
2942 }
2943 }
2944
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002945 return PyString_DecodeEscape(s, len, NULL, unicode,
2946 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947}
2948
2949/* Build a Python string object out of a STRING atom. This takes care of
2950 * compile-time literal catenation, calling parsestr() on each piece, and
2951 * pasting the intermediate results together.
2952 */
2953static PyObject *
2954parsestrplus(struct compiling *c, const node *n)
2955{
2956 PyObject *v;
2957 int i;
2958 REQ(CHILD(n, 0), STRING);
2959 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
2960 /* String literal concatenation */
2961 for (i = 1; i < NCH(n); i++) {
2962 PyObject *s;
2963 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
2964 if (s == NULL)
2965 goto onError;
2966 if (PyString_Check(v) && PyString_Check(s)) {
2967 PyString_ConcatAndDel(&v, s);
2968 if (v == NULL)
2969 goto onError;
2970 }
2971#ifdef Py_USING_UNICODE
2972 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002973 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 Py_DECREF(v);
2976 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002977 if (v == NULL)
2978 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 }
2980#endif
2981 }
2982 }
2983 return v;
2984
2985 onError:
2986 Py_XDECREF(v);
2987 return NULL;
2988}