blob: bba599b9ef445d98de25ae46f5edd6318bc6b735 [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;
81 int lineno;
82
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 }
104 tmp = Py_BuildValue("(ziOO)", filename, lineno, Py_None, loc);
105 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) {
264 stmt_ty s;
265 if (TYPE(CHILD(n, i)) == NEWLINE)
266 break;
267 s = ast_for_stmt(&c, CHILD(n, i));
268 if (!s)
269 goto error;
270 asdl_seq_SET(stmts, i / 2, s);
271 }
272 }
273
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000274 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 }
276 default:
277 goto error;
278 }
279 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 ast_error_finish(filename);
281 return NULL;
282}
283
284/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
285*/
286
287static operator_ty
288get_operator(const node *n)
289{
290 switch (TYPE(n)) {
291 case VBAR:
292 return BitOr;
293 case CIRCUMFLEX:
294 return BitXor;
295 case AMPER:
296 return BitAnd;
297 case LEFTSHIFT:
298 return LShift;
299 case RIGHTSHIFT:
300 return RShift;
301 case PLUS:
302 return Add;
303 case MINUS:
304 return Sub;
305 case STAR:
306 return Mult;
307 case SLASH:
308 return Div;
309 case DOUBLESLASH:
310 return FloorDiv;
311 case PERCENT:
312 return Mod;
313 default:
314 return 0;
315 }
316}
317
318/* Set the context ctx for expr_ty e returning 0 on success, -1 on error.
319
320 Only sets context for expr kinds that "can appear in assignment context"
321 (according to ../Parser/Python.asdl). For other expr kinds, it sets
322 an appropriate syntax error and returns false.
323
324 If e is a sequential type, items in sequence will also have their context
325 set.
326
327*/
328
329static int
330set_context(expr_ty e, expr_context_ty ctx, const node *n)
331{
332 asdl_seq *s = NULL;
333
334 switch (e->kind) {
335 case Attribute_kind:
336 if (ctx == Store &&
337 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
338 return ast_error(n, "assignment to None");
339 }
340 e->v.Attribute.ctx = ctx;
341 break;
342 case Subscript_kind:
343 e->v.Subscript.ctx = ctx;
344 break;
345 case Name_kind:
346 if (ctx == Store &&
347 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
348 return ast_error(n, "assignment to None");
349 }
350 e->v.Name.ctx = ctx;
351 break;
352 case List_kind:
353 e->v.List.ctx = ctx;
354 s = e->v.List.elts;
355 break;
356 case Tuple_kind:
357 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
358 return ast_error(n, "can't assign to ()");
359 e->v.Tuple.ctx = ctx;
360 s = e->v.Tuple.elts;
361 break;
362 case Call_kind:
363 if (ctx == Store)
364 return ast_error(n, "can't assign to function call");
365 else if (ctx == Del)
366 return ast_error(n, "can't delete function call");
367 else
368 return ast_error(n, "unexpected operation on function call");
369 break;
370 case BinOp_kind:
371 return ast_error(n, "can't assign to operator");
372 case GeneratorExp_kind:
373 return ast_error(n, "assignment to generator expression "
374 "not possible");
375 case Num_kind:
376 case Str_kind:
377 return ast_error(n, "can't assign to literal");
378 default: {
379 char buf[300];
380 PyOS_snprintf(buf, sizeof(buf),
381 "unexpected expression in assignment %d (line %d)",
382 e->kind, e->lineno);
383 return ast_error(n, buf);
384 }
385 }
386 /* If the LHS is a list or tuple, we need to set the assignment
387 context for all the tuple elements.
388 */
389 if (s) {
390 int i;
391
392 for (i = 0; i < asdl_seq_LEN(s); i++) {
393 if (!set_context(asdl_seq_GET(s, i), ctx, n))
394 return 0;
395 }
396 }
397 return 1;
398}
399
400static operator_ty
401ast_for_augassign(const node *n)
402{
403 REQ(n, augassign);
404 n = CHILD(n, 0);
405 switch (STR(n)[0]) {
406 case '+':
407 return Add;
408 case '-':
409 return Sub;
410 case '/':
411 if (STR(n)[1] == '/')
412 return FloorDiv;
413 else
414 return Div;
415 case '%':
416 return Mod;
417 case '<':
418 return LShift;
419 case '>':
420 return RShift;
421 case '&':
422 return BitAnd;
423 case '^':
424 return BitXor;
425 case '|':
426 return BitOr;
427 case '*':
428 if (STR(n)[1] == '*')
429 return Pow;
430 else
431 return Mult;
432 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000433 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434 return 0;
435 }
436}
437
438static cmpop_ty
439ast_for_comp_op(const node *n)
440{
441 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
442 |'is' 'not'
443 */
444 REQ(n, comp_op);
445 if (NCH(n) == 1) {
446 n = CHILD(n, 0);
447 switch (TYPE(n)) {
448 case LESS:
449 return Lt;
450 case GREATER:
451 return Gt;
452 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 return Eq;
454 case LESSEQUAL:
455 return LtE;
456 case GREATEREQUAL:
457 return GtE;
458 case NOTEQUAL:
459 return NotEq;
460 case NAME:
461 if (strcmp(STR(n), "in") == 0)
462 return In;
463 if (strcmp(STR(n), "is") == 0)
464 return Is;
465 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000466 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 STR(n));
468 return 0;
469 }
470 }
471 else if (NCH(n) == 2) {
472 /* handle "not in" and "is not" */
473 switch (TYPE(CHILD(n, 0))) {
474 case NAME:
475 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
476 return NotIn;
477 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
478 return IsNot;
479 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000480 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
482 return 0;
483 }
484 }
Neal Norwitz79792652005-11-14 04:25:03 +0000485 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486 NCH(n));
487 return 0;
488}
489
490static asdl_seq *
491seq_for_testlist(struct compiling *c, const node *n)
492{
493 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000494 asdl_seq *seq;
495 expr_ty expression;
496 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497 assert(TYPE(n) == testlist
498 || TYPE(n) == listmaker
499 || TYPE(n) == testlist_gexp
500 || TYPE(n) == testlist_safe
501 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000503 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 if (!seq)
505 return NULL;
506
507 for (i = 0; i < NCH(n); i += 2) {
508 REQ(CHILD(n, i), test);
509
510 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000511 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513
514 assert(i / 2 < seq->size);
515 asdl_seq_SET(seq, i / 2, expression);
516 }
517 return seq;
518}
519
520static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000521compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522{
523 int i, len = (NCH(n) + 1) / 2;
524 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000525 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 if (!args)
527 return NULL;
528
529 REQ(n, fplist);
530
531 for (i = 0; i < len; i++) {
532 const node *child = CHILD(CHILD(n, 2*i), 0);
533 expr_ty arg;
534 if (TYPE(child) == NAME) {
535 if (!strcmp(STR(child), "None")) {
536 ast_error(child, "assignment to None");
537 return NULL;
538 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000539 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
540 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 }
542 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000543 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544 set_context(arg, Store, n);
545 asdl_seq_SET(args, i, arg);
546 }
547
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000548 result = Tuple(args, Store, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549 set_context(result, Store, n);
550 return result;
551}
552
553/* Create AST for argument list.
554
555 XXX TO DO:
556 - check for invalid argument lists like normal after default
557*/
558
559static arguments_ty
560ast_for_arguments(struct compiling *c, const node *n)
561{
562 /* parameters: '(' [varargslist] ')'
563 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
564 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
565 */
566 int i, n_args = 0, n_defaults = 0, found_default = 0;
567 asdl_seq *args, *defaults;
568 identifier vararg = NULL, kwarg = NULL;
569 node *ch;
570
571 if (TYPE(n) == parameters) {
572 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000573 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 n = CHILD(n, 1);
575 }
576 REQ(n, varargslist);
577
578 /* first count the number of normal args & defaults */
579 for (i = 0; i < NCH(n); i++) {
580 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000581 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 if (TYPE(ch) == EQUAL)
584 n_defaults++;
585 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000586 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587 if (!args && n_args)
588 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000589 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 if (!defaults && n_defaults)
591 goto error;
592
593 /* fpdef: NAME | '(' fplist ')'
594 fplist: fpdef (',' fpdef)* [',']
595 */
596 i = 0;
597 while (i < NCH(n)) {
598 ch = CHILD(n, i);
599 switch (TYPE(ch)) {
600 case fpdef:
601 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
602 anything other than EQUAL or a comma? */
603 /* XXX Should NCH(n) check be made a separate check? */
604 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
605 asdl_seq_APPEND(defaults,
606 ast_for_expr(c, CHILD(n, i + 2)));
607 i += 2;
608 found_default = 1;
609 }
610 else if (found_default) {
611 ast_error(n,
612 "non-default argument follows default argument");
613 goto error;
614 }
615
616 if (NCH(ch) == 3) {
617 asdl_seq_APPEND(args,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000618 compiler_complex_args(c, CHILD(ch, 1)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619 }
620 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000621 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
623 ast_error(CHILD(ch, 0), "assignment to None");
624 goto error;
625 }
Armin Rigo31441302005-10-21 12:57:31 +0000626 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000627 Param, LINENO(ch), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628 if (!name)
629 goto error;
630 asdl_seq_APPEND(args, name);
631
632 }
633 i += 2; /* the name and the comma */
634 break;
635 case STAR:
636 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
637 ast_error(CHILD(n, i+1), "assignment to None");
638 goto error;
639 }
640 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
641 i += 3;
642 break;
643 case DOUBLESTAR:
644 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
645 ast_error(CHILD(n, i+1), "assignment to None");
646 goto error;
647 }
648 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
649 i += 3;
650 break;
651 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000652 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 "unexpected node in varargslist: %d @ %d",
654 TYPE(ch), i);
655 goto error;
656 }
657 }
658
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000659 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
661 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000662 Py_XDECREF(vararg);
663 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664 return NULL;
665}
666
667static expr_ty
668ast_for_dotted_name(struct compiling *c, const node *n)
669{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000670 expr_ty e;
671 identifier id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 int i;
673
674 REQ(n, dotted_name);
675
676 id = NEW_IDENTIFIER(CHILD(n, 0));
677 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000678 return NULL;
679 e = Name(id, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000681 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
683 for (i = 2; i < NCH(n); i+=2) {
684 id = NEW_IDENTIFIER(CHILD(n, i));
685 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000686 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000687 e = Attribute(e, id, Load, LINENO(CHILD(n, i)), c->c_arena);
688 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000689 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 }
691
692 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693}
694
695static expr_ty
696ast_for_decorator(struct compiling *c, const node *n)
697{
698 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
699 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000700 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701
702 REQ(n, decorator);
703
704 if ((NCH(n) < 3 && NCH(n) != 5 && NCH(n) != 6)
705 || TYPE(CHILD(n, 0)) != AT || TYPE(RCHILD(n, -1)) != NEWLINE) {
706 ast_error(n, "Invalid decorator node");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000707 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708 }
709
710 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
711 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000712 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713
714 if (NCH(n) == 3) { /* No arguments */
715 d = name_expr;
716 name_expr = NULL;
717 }
718 else if (NCH(n) == 5) { /* Call with no arguments */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000719 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 name_expr = NULL;
723 }
724 else {
725 d = ast_for_call(c, CHILD(n, 3), name_expr);
726 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000727 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728 name_expr = NULL;
729 }
730
731 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732}
733
734static asdl_seq*
735ast_for_decorators(struct compiling *c, const node *n)
736{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000737 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000738 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 int i;
740
741 REQ(n, decorators);
742
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000743 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 if (!decorator_seq)
745 return NULL;
746
747 for (i = 0; i < NCH(n); i++) {
748 d = ast_for_decorator(c, CHILD(n, i));
749 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000750 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 asdl_seq_APPEND(decorator_seq, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 }
753 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754}
755
756static stmt_ty
757ast_for_funcdef(struct compiling *c, const node *n)
758{
759 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000760 identifier name;
761 arguments_ty args;
762 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763 asdl_seq *decorator_seq = NULL;
764 int name_i;
765
766 REQ(n, funcdef);
767
768 if (NCH(n) == 6) { /* decorators are present */
769 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
770 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 name_i = 2;
773 }
774 else {
775 name_i = 1;
776 }
777
778 name = NEW_IDENTIFIER(CHILD(n, name_i));
779 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000782 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000783 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 }
785 args = ast_for_arguments(c, CHILD(n, name_i + 1));
786 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000787 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 body = ast_for_suite(c, CHILD(n, name_i + 3));
789 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000790 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000792 return FunctionDef(name, args, body, decorator_seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793}
794
795static expr_ty
796ast_for_lambdef(struct compiling *c, const node *n)
797{
798 /* lambdef: 'lambda' [varargslist] ':' test */
799 arguments_ty args;
800 expr_ty expression;
801
802 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000803 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 if (!args)
805 return NULL;
806 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000807 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 }
810 else {
811 args = ast_for_arguments(c, CHILD(n, 1));
812 if (!args)
813 return NULL;
814 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000815 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 }
818
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000819 return Lambda(args, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820}
821
822/* Count the number of 'for' loop in a list comprehension.
823
824 Helper for ast_for_listcomp().
825*/
826
827static int
828count_list_fors(const node *n)
829{
830 int n_fors = 0;
831 node *ch = CHILD(n, 1);
832
833 count_list_for:
834 n_fors++;
835 REQ(ch, list_for);
836 if (NCH(ch) == 5)
837 ch = CHILD(ch, 4);
838 else
839 return n_fors;
840 count_list_iter:
841 REQ(ch, list_iter);
842 ch = CHILD(ch, 0);
843 if (TYPE(ch) == list_for)
844 goto count_list_for;
845 else if (TYPE(ch) == list_if) {
846 if (NCH(ch) == 3) {
847 ch = CHILD(ch, 2);
848 goto count_list_iter;
849 }
850 else
851 return n_fors;
852 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000853
854 /* Should never be reached */
855 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
856 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857}
858
859/* Count the number of 'if' statements in a list comprehension.
860
861 Helper for ast_for_listcomp().
862*/
863
864static int
865count_list_ifs(const node *n)
866{
867 int n_ifs = 0;
868
869 count_list_iter:
870 REQ(n, list_iter);
871 if (TYPE(CHILD(n, 0)) == list_for)
872 return n_ifs;
873 n = CHILD(n, 0);
874 REQ(n, list_if);
875 n_ifs++;
876 if (NCH(n) == 2)
877 return n_ifs;
878 n = CHILD(n, 2);
879 goto count_list_iter;
880}
881
882static expr_ty
883ast_for_listcomp(struct compiling *c, const node *n)
884{
885 /* listmaker: test ( list_for | (',' test)* [','] )
886 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
887 list_iter: list_for | list_if
888 list_if: 'if' test [list_iter]
889 testlist_safe: test [(',' test)+ [',']]
890 */
891 expr_ty elt;
892 asdl_seq *listcomps;
893 int i, n_fors;
894 node *ch;
895
896 REQ(n, listmaker);
897 assert(NCH(n) > 1);
898
899 elt = ast_for_expr(c, CHILD(n, 0));
900 if (!elt)
901 return NULL;
902
903 n_fors = count_list_fors(n);
904 if (n_fors == -1)
905 return NULL;
906
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000907 listcomps = asdl_seq_new(n_fors, c->c_arena);
908 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000910
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 ch = CHILD(n, 1);
912 for (i = 0; i < n_fors; i++) {
913 comprehension_ty lc;
914 asdl_seq *t;
915 expr_ty expression;
916
917 REQ(ch, list_for);
918
919 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000920 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000922 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000923 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000926 if (asdl_seq_LEN(t) == 1)
927 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
928 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000930 lc = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
931 expression, NULL, c->c_arena);
932 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934
935 if (NCH(ch) == 5) {
936 int j, n_ifs;
937 asdl_seq *ifs;
938
939 ch = CHILD(ch, 4);
940 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000941 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000944 ifs = asdl_seq_new(n_ifs, c->c_arena);
945 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947
948 for (j = 0; j < n_ifs; j++) {
949 REQ(ch, list_iter);
950
951 ch = CHILD(ch, 0);
952 REQ(ch, list_if);
953
954 asdl_seq_APPEND(ifs, ast_for_expr(c, CHILD(ch, 1)));
955 if (NCH(ch) == 3)
956 ch = CHILD(ch, 2);
957 }
958 /* on exit, must guarantee that ch is a list_for */
959 if (TYPE(ch) == list_iter)
960 ch = CHILD(ch, 0);
961 lc->ifs = ifs;
962 }
963 asdl_seq_APPEND(listcomps, lc);
964 }
965
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000966 return ListComp(elt, listcomps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967}
968
969/*
970 Count the number of 'for' loops in a generator expression.
971
972 Helper for ast_for_genexp().
973*/
974
975static int
976count_gen_fors(const node *n)
977{
978 int n_fors = 0;
979 node *ch = CHILD(n, 1);
980
981 count_gen_for:
982 n_fors++;
983 REQ(ch, gen_for);
984 if (NCH(ch) == 5)
985 ch = CHILD(ch, 4);
986 else
987 return n_fors;
988 count_gen_iter:
989 REQ(ch, gen_iter);
990 ch = CHILD(ch, 0);
991 if (TYPE(ch) == gen_for)
992 goto count_gen_for;
993 else if (TYPE(ch) == gen_if) {
994 if (NCH(ch) == 3) {
995 ch = CHILD(ch, 2);
996 goto count_gen_iter;
997 }
998 else
999 return n_fors;
1000 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001001
1002 /* Should never be reached */
1003 PyErr_SetString(PyExc_SystemError,
1004 "logic error in count_gen_fors");
1005 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006}
1007
1008/* Count the number of 'if' statements in a generator expression.
1009
1010 Helper for ast_for_genexp().
1011*/
1012
1013static int
1014count_gen_ifs(const node *n)
1015{
1016 int n_ifs = 0;
1017
1018 while (1) {
1019 REQ(n, gen_iter);
1020 if (TYPE(CHILD(n, 0)) == gen_for)
1021 return n_ifs;
1022 n = CHILD(n, 0);
1023 REQ(n, gen_if);
1024 n_ifs++;
1025 if (NCH(n) == 2)
1026 return n_ifs;
1027 n = CHILD(n, 2);
1028 }
1029}
1030
1031static expr_ty
1032ast_for_genexp(struct compiling *c, const node *n)
1033{
1034 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1035 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1036 expr_ty elt;
1037 asdl_seq *genexps;
1038 int i, n_fors;
1039 node *ch;
1040
1041 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1042 assert(NCH(n) > 1);
1043
1044 elt = ast_for_expr(c, CHILD(n, 0));
1045 if (!elt)
1046 return NULL;
1047
1048 n_fors = count_gen_fors(n);
1049 if (n_fors == -1)
1050 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001051
1052 genexps = asdl_seq_new(n_fors, c->c_arena);
1053 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056 ch = CHILD(n, 1);
1057 for (i = 0; i < n_fors; i++) {
1058 comprehension_ty ge;
1059 asdl_seq *t;
1060 expr_ty expression;
1061
1062 REQ(ch, gen_for);
1063
1064 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001065 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001067 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001068 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001070
1071 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001073 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001075 ge = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
1076 expression, NULL, c->c_arena);
1077
1078 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 if (NCH(ch) == 5) {
1082 int j, n_ifs;
1083 asdl_seq *ifs;
1084
1085 ch = CHILD(ch, 4);
1086 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001087 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001089
1090 ifs = asdl_seq_new(n_ifs, c->c_arena);
1091 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001093
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 for (j = 0; j < n_ifs; j++) {
1095 REQ(ch, gen_iter);
1096 ch = CHILD(ch, 0);
1097 REQ(ch, gen_if);
1098
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001099 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001100 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001101 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001102 asdl_seq_APPEND(ifs, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 if (NCH(ch) == 3)
1104 ch = CHILD(ch, 2);
1105 }
1106 /* on exit, must guarantee that ch is a gen_for */
1107 if (TYPE(ch) == gen_iter)
1108 ch = CHILD(ch, 0);
1109 ge->ifs = ifs;
1110 }
1111 asdl_seq_APPEND(genexps, ge);
1112 }
1113
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001114 return GeneratorExp(elt, genexps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115}
1116
1117static expr_ty
1118ast_for_atom(struct compiling *c, const node *n)
1119{
1120 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1121 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1122 */
1123 node *ch = CHILD(n, 0);
1124
1125 switch (TYPE(ch)) {
1126 case NAME:
1127 /* All names start in Load context, but may later be
1128 changed. */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001129 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 case STRING: {
1131 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 if (!str)
1133 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001134
1135 PyArena_AddPyObject(c->c_arena, str);
1136 return Str(str, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 }
1138 case NUMBER: {
1139 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 if (!pynum)
1141 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001142
1143 PyArena_AddPyObject(c->c_arena, pynum);
1144 return Num(pynum, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 }
1146 case LPAR: /* some parenthesized expressions */
1147 ch = CHILD(n, 1);
1148
1149 if (TYPE(ch) == RPAR)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001150 return Tuple(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151
1152 if (TYPE(ch) == yield_expr)
1153 return ast_for_expr(c, ch);
1154
1155 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1156 return ast_for_genexp(c, ch);
1157
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001158 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 case LSQB: /* list (or list comprehension) */
1160 ch = CHILD(n, 1);
1161
1162 if (TYPE(ch) == RSQB)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001163 return List(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164
1165 REQ(ch, listmaker);
1166 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1167 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 if (!elts)
1169 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001170
1171 return List(elts, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 }
1173 else
1174 return ast_for_listcomp(c, ch);
1175 case LBRACE: {
1176 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1177 int i, size;
1178 asdl_seq *keys, *values;
1179
1180 ch = CHILD(n, 1);
1181 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001182 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 if (!keys)
1184 return NULL;
1185
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001186 values = asdl_seq_new(size, c->c_arena);
1187 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189
1190 for (i = 0; i < NCH(ch); i += 4) {
1191 expr_ty expression;
1192
1193 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001194 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001196
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001198
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 asdl_seq_SET(values, i / 4, expression);
1204 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001205 return Dict(keys, values, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 }
1207 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001208 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 if (!expression)
1210 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001211
1212 return Repr(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 }
1214 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001215 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 return NULL;
1217 }
1218}
1219
1220static slice_ty
1221ast_for_slice(struct compiling *c, const node *n)
1222{
1223 node *ch;
1224 expr_ty lower = NULL, upper = NULL, step = NULL;
1225
1226 REQ(n, subscript);
1227
1228 /*
1229 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1230 sliceop: ':' [test]
1231 */
1232 ch = CHILD(n, 0);
1233 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001234 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235
1236 if (NCH(n) == 1 && TYPE(ch) == test) {
1237 /* 'step' variable hold no significance in terms of being used over
1238 other vars */
1239 step = ast_for_expr(c, ch);
1240 if (!step)
1241 return NULL;
1242
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001243 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 }
1245
1246 if (TYPE(ch) == test) {
1247 lower = ast_for_expr(c, ch);
1248 if (!lower)
1249 return NULL;
1250 }
1251
1252 /* If there's an upper bound it's in the second or third position. */
1253 if (TYPE(ch) == COLON) {
1254 if (NCH(n) > 1) {
1255 node *n2 = CHILD(n, 1);
1256
1257 if (TYPE(n2) == test) {
1258 upper = ast_for_expr(c, n2);
1259 if (!upper)
1260 return NULL;
1261 }
1262 }
1263 } else if (NCH(n) > 2) {
1264 node *n2 = CHILD(n, 2);
1265
1266 if (TYPE(n2) == test) {
1267 upper = ast_for_expr(c, n2);
1268 if (!upper)
1269 return NULL;
1270 }
1271 }
1272
1273 ch = CHILD(n, NCH(n) - 1);
1274 if (TYPE(ch) == sliceop) {
1275 if (NCH(ch) == 1)
1276 /* XXX: If only 1 child, then should just be a colon. Should we
1277 just skip assigning and just get to the return? */
1278 ch = CHILD(ch, 0);
1279 else
1280 ch = CHILD(ch, 1);
1281 if (TYPE(ch) == test) {
1282 step = ast_for_expr(c, ch);
1283 if (!step)
1284 return NULL;
1285 }
1286 }
1287
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001288 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289}
1290
1291static expr_ty
1292ast_for_binop(struct compiling *c, const node *n)
1293{
1294 /* Must account for a sequence of expressions.
1295 How should A op B op C by represented?
1296 BinOp(BinOp(A, op, B), op, C).
1297 */
1298
1299 int i, nops;
1300 expr_ty expr1, expr2, result;
1301 operator_ty operator;
1302
1303 expr1 = ast_for_expr(c, CHILD(n, 0));
1304 if (!expr1)
1305 return NULL;
1306
1307 expr2 = ast_for_expr(c, CHILD(n, 2));
1308 if (!expr2)
1309 return NULL;
1310
1311 operator = get_operator(CHILD(n, 1));
1312 if (!operator)
1313 return NULL;
1314
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001315 result = BinOp(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 if (!result)
1317 return NULL;
1318
1319 nops = (NCH(n) - 1) / 2;
1320 for (i = 1; i < nops; i++) {
1321 expr_ty tmp_result, tmp;
1322 const node* next_oper = CHILD(n, i * 2 + 1);
1323
1324 operator = get_operator(next_oper);
1325 if (!operator)
1326 return NULL;
1327
1328 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1329 if (!tmp)
1330 return NULL;
1331
1332 tmp_result = BinOp(result, operator, tmp,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001333 LINENO(next_oper), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 if (!tmp)
1335 return NULL;
1336 result = tmp_result;
1337 }
1338 return result;
1339}
1340
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001341static expr_ty
1342ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1343{
1344 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1345 expr_ty e;
1346 REQ(n, trailer);
1347 if (TYPE(CHILD(n, 0)) == LPAR) {
1348 if (NCH(n) == 2)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001349 e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001350 else
1351 e = ast_for_call(c, CHILD(n, 1), left_expr);
1352 }
1353 else if (TYPE(CHILD(n, 0)) == LSQB) {
1354 REQ(CHILD(n, 2), RSQB);
1355 n = CHILD(n, 1);
1356 if (NCH(n) <= 2) {
1357 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1358 if (!slc)
1359 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001360 e = Subscript(left_expr, slc, Load, LINENO(n), c->c_arena);
1361 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001362 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001363 }
1364 else {
1365 int j;
1366 slice_ty slc;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001367 asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001368 if (!slices)
1369 return NULL;
1370 for (j = 0; j < NCH(n); j += 2) {
1371 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001372 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001373 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001374 asdl_seq_SET(slices, j / 2, slc);
1375 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001376 e = Subscript(left_expr, ExtSlice(slices, c->c_arena),
1377 Load, LINENO(n), c->c_arena);
1378 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001379 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001380 }
1381 }
1382 else {
1383 assert(TYPE(CHILD(n, 0)) == DOT);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001384 e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n),
1385 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001386 }
1387 return e;
1388}
1389
1390static expr_ty
1391ast_for_power(struct compiling *c, const node *n)
1392{
1393 /* power: atom trailer* ('**' factor)*
1394 */
1395 int i;
1396 expr_ty e, tmp;
1397 REQ(n, power);
1398 e = ast_for_atom(c, CHILD(n, 0));
1399 if (!e)
1400 return NULL;
1401 if (NCH(n) == 1)
1402 return e;
1403 for (i = 1; i < NCH(n); i++) {
1404 node *ch = CHILD(n, i);
1405 if (TYPE(ch) != trailer)
1406 break;
1407 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001408 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001409 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001410 e = tmp;
1411 }
1412 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1413 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001414 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001415 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001416 tmp = BinOp(e, Pow, f, LINENO(n), c->c_arena);
1417 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001418 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001419 e = tmp;
1420 }
1421 return e;
1422}
1423
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424/* Do not name a variable 'expr'! Will cause a compile error.
1425*/
1426
1427static expr_ty
1428ast_for_expr(struct compiling *c, const node *n)
1429{
1430 /* handle the full range of simple expressions
1431 test: and_test ('or' and_test)* | lambdef
1432 and_test: not_test ('and' not_test)*
1433 not_test: 'not' not_test | comparison
1434 comparison: expr (comp_op expr)*
1435 expr: xor_expr ('|' xor_expr)*
1436 xor_expr: and_expr ('^' and_expr)*
1437 and_expr: shift_expr ('&' shift_expr)*
1438 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1439 arith_expr: term (('+'|'-') term)*
1440 term: factor (('*'|'/'|'%'|'//') factor)*
1441 factor: ('+'|'-'|'~') factor | power
1442 power: atom trailer* ('**' factor)*
1443 */
1444
1445 asdl_seq *seq;
1446 int i;
1447
1448 loop:
1449 switch (TYPE(n)) {
1450 case test:
1451 if (TYPE(CHILD(n, 0)) == lambdef)
1452 return ast_for_lambdef(c, CHILD(n, 0));
1453 /* Fall through to and_test */
1454 case and_test:
1455 if (NCH(n) == 1) {
1456 n = CHILD(n, 0);
1457 goto loop;
1458 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001459 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460 if (!seq)
1461 return NULL;
1462 for (i = 0; i < NCH(n); i += 2) {
1463 expr_ty e = ast_for_expr(c, CHILD(n, i));
1464 if (!e)
1465 return NULL;
1466 asdl_seq_SET(seq, i / 2, e);
1467 }
1468 if (!strcmp(STR(CHILD(n, 1)), "and"))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001469 return BoolOp(And, seq, LINENO(n), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001470 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1471 return BoolOp(Or, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 case not_test:
1473 if (NCH(n) == 1) {
1474 n = CHILD(n, 0);
1475 goto loop;
1476 }
1477 else {
1478 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1479 if (!expression)
1480 return NULL;
1481
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001482 return UnaryOp(Not, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483 }
1484 case comparison:
1485 if (NCH(n) == 1) {
1486 n = CHILD(n, 0);
1487 goto loop;
1488 }
1489 else {
1490 expr_ty expression;
1491 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001492 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 if (!ops)
1494 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001495 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 return NULL;
1498 }
1499 for (i = 1; i < NCH(n); i += 2) {
1500 /* XXX cmpop_ty is just an enum */
1501 cmpop_ty operator;
1502
1503 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001504 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001506 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507
1508 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001509 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512
1513 asdl_seq_SET(ops, i / 2, (void *)operator);
1514 asdl_seq_SET(cmps, i / 2, expression);
1515 }
1516 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001517 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001519 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001521 return Compare(expression, ops, cmps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 }
1523 break;
1524
1525 /* The next five cases all handle BinOps. The main body of code
1526 is the same in each case, but the switch turned inside out to
1527 reuse the code for each type of operator.
1528 */
1529 case expr:
1530 case xor_expr:
1531 case and_expr:
1532 case shift_expr:
1533 case arith_expr:
1534 case term:
1535 if (NCH(n) == 1) {
1536 n = CHILD(n, 0);
1537 goto loop;
1538 }
1539 return ast_for_binop(c, n);
1540 case yield_expr: {
1541 expr_ty exp = NULL;
1542 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001543 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 if (!exp)
1545 return NULL;
1546 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001547 return Yield(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 }
1549 case factor: {
1550 expr_ty expression;
1551
1552 if (NCH(n) == 1) {
1553 n = CHILD(n, 0);
1554 goto loop;
1555 }
1556
1557 expression = ast_for_expr(c, CHILD(n, 1));
1558 if (!expression)
1559 return NULL;
1560
1561 switch (TYPE(CHILD(n, 0))) {
1562 case PLUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001563 return UnaryOp(UAdd, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 case MINUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001565 return UnaryOp(USub, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 case TILDE:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001567 return UnaryOp(Invert, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001569 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1570 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 break;
1572 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001573 case power:
1574 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001576 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 return NULL;
1578 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001579 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580 return NULL;
1581}
1582
1583static expr_ty
1584ast_for_call(struct compiling *c, const node *n, expr_ty func)
1585{
1586 /*
1587 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1588 | '**' test)
1589 argument: [test '='] test [gen_for] # Really [keyword '='] test
1590 */
1591
1592 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001593 asdl_seq *args;
1594 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 expr_ty vararg = NULL, kwarg = NULL;
1596
1597 REQ(n, arglist);
1598
1599 nargs = 0;
1600 nkeywords = 0;
1601 ngens = 0;
1602 for (i = 0; i < NCH(n); i++) {
1603 node *ch = CHILD(n, i);
1604 if (TYPE(ch) == argument) {
1605 if (NCH(ch) == 1)
1606 nargs++;
1607 else if (TYPE(CHILD(ch, 1)) == gen_for)
1608 ngens++;
1609 else
1610 nkeywords++;
1611 }
1612 }
1613 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
1614 ast_error(n, "Generator expression must be parenthesised "
1615 "if not sole argument");
1616 return NULL;
1617 }
1618
1619 if (nargs + nkeywords + ngens > 255) {
1620 ast_error(n, "more than 255 arguments");
1621 return NULL;
1622 }
1623
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001624 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001626 return NULL;
1627 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001629 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 nargs = 0;
1631 nkeywords = 0;
1632 for (i = 0; i < NCH(n); i++) {
1633 node *ch = CHILD(n, i);
1634 if (TYPE(ch) == argument) {
1635 expr_ty e;
1636 if (NCH(ch) == 1) {
1637 e = ast_for_expr(c, CHILD(ch, 0));
1638 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001639 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 asdl_seq_SET(args, nargs++, e);
1641 }
1642 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1643 e = ast_for_genexp(c, ch);
1644 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646 asdl_seq_SET(args, nargs++, e);
1647 }
1648 else {
1649 keyword_ty kw;
1650 identifier key;
1651
1652 /* CHILD(ch, 0) is test, but must be an identifier? */
1653 e = ast_for_expr(c, CHILD(ch, 0));
1654 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001655 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 /* f(lambda x: x[0] = 3) ends up getting parsed with
1657 * LHS test = lambda x: x[0], and RHS test = 3.
1658 * SF bug 132313 points out that complaining about a keyword
1659 * then is very confusing.
1660 */
1661 if (e->kind == Lambda_kind) {
1662 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001663 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664 } else if (e->kind != Name_kind) {
1665 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001666 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 }
1668 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669 e = ast_for_expr(c, CHILD(ch, 2));
1670 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001671 return NULL;
1672 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001674 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 asdl_seq_SET(keywords, nkeywords++, kw);
1676 }
1677 }
1678 else if (TYPE(ch) == STAR) {
1679 vararg = ast_for_expr(c, CHILD(n, i+1));
1680 i++;
1681 }
1682 else if (TYPE(ch) == DOUBLESTAR) {
1683 kwarg = ast_for_expr(c, CHILD(n, i+1));
1684 i++;
1685 }
1686 }
1687
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001688 return Call(func, args, keywords, vararg, kwarg, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689}
1690
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001692ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001694 /* testlist_gexp: test (',' test)* [','] */
1695 /* testlist: test (',' test)* [','] */
1696 /* testlist_safe: test (',' test)+ [','] */
1697 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001699 if (TYPE(n) == testlist_gexp) {
1700 if (NCH(n) > 1)
1701 assert(TYPE(CHILD(n, 1)) != gen_for);
1702 }
1703 else {
1704 assert(TYPE(n) == testlist ||
1705 TYPE(n) == testlist_safe ||
1706 TYPE(n) == testlist1);
1707 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 if (NCH(n) == 1)
1709 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 else {
1711 asdl_seq *tmp = seq_for_testlist(c, n);
1712 if (!tmp)
1713 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001714 return Tuple(tmp, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001716}
1717
1718static expr_ty
1719ast_for_testlist_gexp(struct compiling *c, const node* n)
1720{
1721 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1722 /* argument: test [ gen_for ] */
1723 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001724 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001725 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001726 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001727}
1728
1729/* like ast_for_testlist() but returns a sequence */
1730static asdl_seq*
1731ast_for_class_bases(struct compiling *c, const node* n)
1732{
1733 /* testlist: test (',' test)* [','] */
1734 assert(NCH(n) > 0);
1735 REQ(n, testlist);
1736 if (NCH(n) == 1) {
1737 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001738 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001739 if (!bases)
1740 return NULL;
1741 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001742 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001743 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001744 asdl_seq_SET(bases, 0, base);
1745 return bases;
1746 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001747
1748 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749}
1750
1751static stmt_ty
1752ast_for_expr_stmt(struct compiling *c, const node *n)
1753{
1754 REQ(n, expr_stmt);
1755 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1756 | ('=' (yield_expr|testlist))*)
1757 testlist: test (',' test)* [',']
1758 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1759 | '<<=' | '>>=' | '**=' | '//='
1760 test: ... here starts the operator precendence dance
1761 */
1762
1763 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001764 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 if (!e)
1766 return NULL;
1767
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001768 return Expr(e, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769 }
1770 else if (TYPE(CHILD(n, 1)) == augassign) {
1771 expr_ty expr1, expr2;
1772 operator_ty operator;
1773 node *ch = CHILD(n, 0);
1774
1775 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001776 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001778 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch),
1779 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780
1781 if (!expr1)
1782 return NULL;
1783 if (expr1->kind == GeneratorExp_kind) {
1784 ast_error(ch, "augmented assignment to generator "
1785 "expression not possible");
1786 return NULL;
1787 }
1788 if (expr1->kind == Name_kind) {
1789 char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1790 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1791 ast_error(ch, "assignment to None");
1792 return NULL;
1793 }
1794 }
1795
1796 ch = CHILD(n, 2);
1797 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001798 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001800 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001801 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 return NULL;
1803
1804 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001805 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 return NULL;
1807
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001808 return AugAssign(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 }
1810 else {
1811 int i;
1812 asdl_seq *targets;
1813 node *value;
1814 expr_ty expression;
1815
1816 /* a normal assignment */
1817 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001818 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 if (!targets)
1820 return NULL;
1821 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001822 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 node *ch = CHILD(n, i);
1824 if (TYPE(ch) == yield_expr) {
1825 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001828 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829
1830 /* set context to assign */
1831 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001832 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833
Neal Norwitz84456bd2005-12-18 03:16:20 +00001834 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001835 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836
1837 asdl_seq_SET(targets, i / 2, e);
1838 }
1839 value = CHILD(n, NCH(n) - 1);
1840 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001841 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 else
1843 expression = ast_for_expr(c, value);
1844 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845 return NULL;
1846 return Assign(targets, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848}
1849
1850static stmt_ty
1851ast_for_print_stmt(struct compiling *c, const node *n)
1852{
1853 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1854 | '>>' test [ (',' test)+ [','] ] )
1855 */
1856 expr_ty dest = NULL, expression;
1857 asdl_seq *seq;
1858 bool nl;
1859 int i, start = 1;
1860
1861 REQ(n, print_stmt);
1862 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1863 dest = ast_for_expr(c, CHILD(n, 2));
1864 if (!dest)
1865 return NULL;
1866 start = 4;
1867 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001868 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 if (!seq)
1870 return NULL;
1871 for (i = start; i < NCH(n); i += 2) {
1872 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001873 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875
1876 asdl_seq_APPEND(seq, expression);
1877 }
1878 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001879 return Print(dest, seq, nl, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880}
1881
1882static asdl_seq *
1883ast_for_exprlist(struct compiling *c, const node *n, int context)
1884{
1885 asdl_seq *seq;
1886 int i;
1887 expr_ty e;
1888
1889 REQ(n, exprlist);
1890
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 if (!seq)
1893 return NULL;
1894 for (i = 0; i < NCH(n); i += 2) {
1895 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001896 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001897 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00001898 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001899 if (context && !set_context(e, context, CHILD(n, i)))
1900 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 }
1902 return seq;
1903}
1904
1905static stmt_ty
1906ast_for_del_stmt(struct compiling *c, const node *n)
1907{
1908 asdl_seq *expr_list;
1909
1910 /* del_stmt: 'del' exprlist */
1911 REQ(n, del_stmt);
1912
1913 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
1914 if (!expr_list)
1915 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001916 return Delete(expr_list, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917}
1918
1919static stmt_ty
1920ast_for_flow_stmt(struct compiling *c, const node *n)
1921{
1922 /*
1923 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
1924 | yield_stmt
1925 break_stmt: 'break'
1926 continue_stmt: 'continue'
1927 return_stmt: 'return' [testlist]
1928 yield_stmt: yield_expr
1929 yield_expr: 'yield' testlist
1930 raise_stmt: 'raise' [test [',' test [',' test]]]
1931 */
1932 node *ch;
1933
1934 REQ(n, flow_stmt);
1935 ch = CHILD(n, 0);
1936 switch (TYPE(ch)) {
1937 case break_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001938 return Break(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 case continue_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001940 return Continue(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941 case yield_stmt: { /* will reduce to yield_expr */
1942 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
1943 if (!exp)
1944 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001945 return Expr(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 }
1947 case return_stmt:
1948 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001949 return Return(NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001951 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952 if (!expression)
1953 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001954 return Return(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955 }
1956 case raise_stmt:
1957 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001958 return Raise(NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959 else if (NCH(ch) == 2) {
1960 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
1961 if (!expression)
1962 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001963 return Raise(expression, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964 }
1965 else if (NCH(ch) == 4) {
1966 expr_ty expr1, expr2;
1967
1968 expr1 = ast_for_expr(c, CHILD(ch, 1));
1969 if (!expr1)
1970 return NULL;
1971 expr2 = ast_for_expr(c, CHILD(ch, 3));
1972 if (!expr2)
1973 return NULL;
1974
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001975 return Raise(expr1, expr2, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 }
1977 else if (NCH(ch) == 6) {
1978 expr_ty expr1, expr2, expr3;
1979
1980 expr1 = ast_for_expr(c, CHILD(ch, 1));
1981 if (!expr1)
1982 return NULL;
1983 expr2 = ast_for_expr(c, CHILD(ch, 3));
1984 if (!expr2)
1985 return NULL;
1986 expr3 = ast_for_expr(c, CHILD(ch, 5));
1987 if (!expr3)
1988 return NULL;
1989
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001990 return Raise(expr1, expr2, expr3, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 }
1992 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001993 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 "unexpected flow_stmt: %d", TYPE(ch));
1995 return NULL;
1996 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001997
1998 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
1999 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000}
2001
2002static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002003alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004{
2005 /*
2006 import_as_name: NAME [NAME NAME]
2007 dotted_as_name: dotted_name [NAME NAME]
2008 dotted_name: NAME ('.' NAME)*
2009 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002010 PyObject *str;
2011
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 loop:
2013 switch (TYPE(n)) {
2014 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002015 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2016 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 case dotted_as_name:
2018 if (NCH(n) == 1) {
2019 n = CHILD(n, 0);
2020 goto loop;
2021 }
2022 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002023 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 assert(!a->asname);
2025 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2026 return a;
2027 }
2028 break;
2029 case dotted_name:
2030 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002031 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 else {
2033 /* Create a string of the form "a.b.c" */
2034 int i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 char *s;
2036
2037 len = 0;
2038 for (i = 0; i < NCH(n); i += 2)
2039 /* length of string plus one for the dot */
2040 len += strlen(STR(CHILD(n, i))) + 1;
2041 len--; /* the last name doesn't have a dot */
2042 str = PyString_FromStringAndSize(NULL, len);
2043 if (!str)
2044 return NULL;
2045 s = PyString_AS_STRING(str);
2046 if (!s)
2047 return NULL;
2048 for (i = 0; i < NCH(n); i += 2) {
2049 char *sch = STR(CHILD(n, i));
2050 strcpy(s, STR(CHILD(n, i)));
2051 s += strlen(sch);
2052 *s++ = '.';
2053 }
2054 --s;
2055 *s = '\0';
2056 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002057 PyArena_AddPyObject(c->c_arena, str);
2058 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 }
2060 break;
2061 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002062 str = PyString_InternFromString("*");
2063 PyArena_AddPyObject(c->c_arena, str);
2064 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002066 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 "unexpected import name: %d", TYPE(n));
2068 return NULL;
2069 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002070
2071 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 return NULL;
2073}
2074
2075static stmt_ty
2076ast_for_import_stmt(struct compiling *c, const node *n)
2077{
2078 /*
2079 import_stmt: import_name | import_from
2080 import_name: 'import' dotted_as_names
2081 import_from: 'from' dotted_name 'import' ('*' |
2082 '(' import_as_names ')' |
2083 import_as_names)
2084 */
2085 int i;
2086 asdl_seq *aliases;
2087
2088 REQ(n, import_stmt);
2089 n = CHILD(n, 0);
2090 if (STR(CHILD(n, 0))[0] == 'i') { /* import */
2091 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002092 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002093 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 if (!aliases)
2095 return NULL;
2096 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002097 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002098 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 asdl_seq_SET(aliases, i / 2, import_alias);
2101 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002102 return Import(aliases, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 }
2104 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 int n_children;
2106 const char *from_modules;
2107 int lineno = LINENO(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002108 alias_ty mod = alias_for_import_name(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 if (!mod)
2110 return NULL;
2111
2112 /* XXX this needs to be cleaned up */
2113
2114 from_modules = STR(CHILD(n, 3));
2115 if (!from_modules) {
2116 n = CHILD(n, 3); /* from ... import x, y, z */
2117 if (NCH(n) % 2 == 0) {
2118 /* it ends with a comma, not valid but the parser allows it */
2119 ast_error(n, "trailing comma not allowed without"
2120 " surrounding parentheses");
2121 return NULL;
2122 }
2123 }
2124 else if (from_modules[0] == '*') {
2125 n = CHILD(n, 3); /* from ... import * */
2126 }
2127 else if (from_modules[0] == '(')
2128 n = CHILD(n, 4); /* from ... import (x, y, z) */
Neal Norwitze76adcd2005-11-15 05:04:31 +00002129 else {
2130 /* XXX: don't we need to call ast_error(n, "..."); */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002132 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133
2134 n_children = NCH(n);
2135 if (from_modules && from_modules[0] == '*')
2136 n_children = 1;
2137
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002138 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002139 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141
2142 /* handle "from ... import *" special b/c there's no children */
2143 if (from_modules && from_modules[0] == '*') {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002144 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002145 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 asdl_seq_APPEND(aliases, import_alias);
2148 }
2149
2150 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002151 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002152 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 asdl_seq_APPEND(aliases, import_alias);
2155 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002156 return ImportFrom(mod->name, aliases, lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 }
Neal Norwitz79792652005-11-14 04:25:03 +00002158 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 "unknown import statement: starts with command '%s'",
2160 STR(CHILD(n, 0)));
2161 return NULL;
2162}
2163
2164static stmt_ty
2165ast_for_global_stmt(struct compiling *c, const node *n)
2166{
2167 /* global_stmt: 'global' NAME (',' NAME)* */
2168 identifier name;
2169 asdl_seq *s;
2170 int i;
2171
2172 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002173 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 if (!s)
2175 return NULL;
2176 for (i = 1; i < NCH(n); i += 2) {
2177 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002178 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 asdl_seq_SET(s, i / 2, name);
2181 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002182 return Global(s, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183}
2184
2185static stmt_ty
2186ast_for_exec_stmt(struct compiling *c, const node *n)
2187{
2188 expr_ty expr1, globals = NULL, locals = NULL;
2189 int n_children = NCH(n);
2190 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002191 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 "poorly formed 'exec' statement: %d parts to statement",
2193 n_children);
2194 return NULL;
2195 }
2196
2197 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2198 REQ(n, exec_stmt);
2199 expr1 = ast_for_expr(c, CHILD(n, 1));
2200 if (!expr1)
2201 return NULL;
2202 if (n_children >= 4) {
2203 globals = ast_for_expr(c, CHILD(n, 3));
2204 if (!globals)
2205 return NULL;
2206 }
2207 if (n_children == 6) {
2208 locals = ast_for_expr(c, CHILD(n, 5));
2209 if (!locals)
2210 return NULL;
2211 }
2212
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002213 return Exec(expr1, globals, locals, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214}
2215
2216static stmt_ty
2217ast_for_assert_stmt(struct compiling *c, const node *n)
2218{
2219 /* assert_stmt: 'assert' test [',' test] */
2220 REQ(n, assert_stmt);
2221 if (NCH(n) == 2) {
2222 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2223 if (!expression)
2224 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002225 return Assert(expression, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 }
2227 else if (NCH(n) == 4) {
2228 expr_ty expr1, expr2;
2229
2230 expr1 = ast_for_expr(c, CHILD(n, 1));
2231 if (!expr1)
2232 return NULL;
2233 expr2 = ast_for_expr(c, CHILD(n, 3));
2234 if (!expr2)
2235 return NULL;
2236
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002237 return Assert(expr1, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 }
Neal Norwitz79792652005-11-14 04:25:03 +00002239 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 "improper number of parts to 'assert' statement: %d",
2241 NCH(n));
2242 return NULL;
2243}
2244
2245static asdl_seq *
2246ast_for_suite(struct compiling *c, const node *n)
2247{
2248 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002249 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 stmt_ty s;
2251 int i, total, num, end, pos = 0;
2252 node *ch;
2253
2254 REQ(n, suite);
2255
2256 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002257 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 if (!seq)
2259 return NULL;
2260 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2261 n = CHILD(n, 0);
2262 /* simple_stmt always ends with a NEWLINE,
2263 and may have a trailing SEMI
2264 */
2265 end = NCH(n) - 1;
2266 if (TYPE(CHILD(n, end - 1)) == SEMI)
2267 end--;
2268 /* loop by 2 to skip semi-colons */
2269 for (i = 0; i < end; i += 2) {
2270 ch = CHILD(n, i);
2271 s = ast_for_stmt(c, ch);
2272 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 asdl_seq_SET(seq, pos++, s);
2275 }
2276 }
2277 else {
2278 for (i = 2; i < (NCH(n) - 1); i++) {
2279 ch = CHILD(n, i);
2280 REQ(ch, stmt);
2281 num = num_stmts(ch);
2282 if (num == 1) {
2283 /* small_stmt or compound_stmt with only one child */
2284 s = ast_for_stmt(c, ch);
2285 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002286 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 asdl_seq_SET(seq, pos++, s);
2288 }
2289 else {
2290 int j;
2291 ch = CHILD(ch, 0);
2292 REQ(ch, simple_stmt);
2293 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002294 /* statement terminates with a semi-colon ';' */
2295 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002296 assert((j + 1) == NCH(ch));
2297 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002298 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 s = ast_for_stmt(c, CHILD(ch, j));
2300 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002301 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 asdl_seq_SET(seq, pos++, s);
2303 }
2304 }
2305 }
2306 }
2307 assert(pos == seq->size);
2308 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309}
2310
2311static stmt_ty
2312ast_for_if_stmt(struct compiling *c, const node *n)
2313{
2314 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2315 ['else' ':' suite]
2316 */
2317 char *s;
2318
2319 REQ(n, if_stmt);
2320
2321 if (NCH(n) == 4) {
2322 expr_ty expression;
2323 asdl_seq *suite_seq;
2324
2325 expression = ast_for_expr(c, CHILD(n, 1));
2326 if (!expression)
2327 return NULL;
2328 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002329 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 return NULL;
2331
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002332 return If(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002334
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335 s = STR(CHILD(n, 4));
2336 /* s[2], the third character in the string, will be
2337 's' for el_s_e, or
2338 'i' for el_i_f
2339 */
2340 if (s[2] == 's') {
2341 expr_ty expression;
2342 asdl_seq *seq1, *seq2;
2343
2344 expression = ast_for_expr(c, CHILD(n, 1));
2345 if (!expression)
2346 return NULL;
2347 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002348 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349 return NULL;
2350 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002351 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352 return NULL;
2353
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002354 return If(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355 }
2356 else if (s[2] == 'i') {
2357 int i, n_elif, has_else = 0;
2358 asdl_seq *orelse = NULL;
2359 n_elif = NCH(n) - 4;
2360 /* must reference the child n_elif+1 since 'else' token is third,
2361 not fourth, child from the end. */
2362 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2363 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2364 has_else = 1;
2365 n_elif -= 3;
2366 }
2367 n_elif /= 4;
2368
2369 if (has_else) {
2370 expr_ty expression;
2371 asdl_seq *seq1, *seq2;
2372
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002373 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 if (!orelse)
2375 return NULL;
2376 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002377 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002380 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002383 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385
2386 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002387 LINENO(CHILD(n, NCH(n) - 6)),
2388 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 /* the just-created orelse handled the last elif */
2390 n_elif--;
2391 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392
2393 for (i = 0; i < n_elif; i++) {
2394 int off = 5 + (n_elif - i - 1) * 4;
2395 expr_ty expression;
2396 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002397 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002398 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 return NULL;
2400 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002401 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002404 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406
2407 asdl_seq_SET(new, 0,
2408 If(expression, suite_seq, orelse,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002409 LINENO(CHILD(n, off)), c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410 orelse = new;
2411 }
2412 return If(ast_for_expr(c, CHILD(n, 1)),
2413 ast_for_suite(c, CHILD(n, 3)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002414 orelse, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002416
2417 PyErr_Format(PyExc_SystemError,
2418 "unexpected token in 'if' statement: %s", s);
2419 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420}
2421
2422static stmt_ty
2423ast_for_while_stmt(struct compiling *c, const node *n)
2424{
2425 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2426 REQ(n, while_stmt);
2427
2428 if (NCH(n) == 4) {
2429 expr_ty expression;
2430 asdl_seq *suite_seq;
2431
2432 expression = ast_for_expr(c, CHILD(n, 1));
2433 if (!expression)
2434 return NULL;
2435 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002436 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002438 return While(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439 }
2440 else if (NCH(n) == 7) {
2441 expr_ty expression;
2442 asdl_seq *seq1, *seq2;
2443
2444 expression = ast_for_expr(c, CHILD(n, 1));
2445 if (!expression)
2446 return NULL;
2447 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002448 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 return NULL;
2450 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002451 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 return NULL;
2453
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002454 return While(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002456
2457 PyErr_Format(PyExc_SystemError,
2458 "wrong number of tokens for 'while' statement: %d",
2459 NCH(n));
2460 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461}
2462
2463static stmt_ty
2464ast_for_for_stmt(struct compiling *c, const node *n)
2465{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002466 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 expr_ty expression;
2468 expr_ty target;
2469 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2470 REQ(n, for_stmt);
2471
2472 if (NCH(n) == 9) {
2473 seq = ast_for_suite(c, CHILD(n, 8));
2474 if (!seq)
2475 return NULL;
2476 }
2477
2478 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002479 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002481 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002484 target = Tuple(_target, Store, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002486 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002487 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 return NULL;
2489 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002490 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 return NULL;
2492
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002493 return For(target, expression, suite_seq, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494}
2495
2496static excepthandler_ty
2497ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2498{
2499 /* except_clause: 'except' [test [',' test]] */
2500 REQ(exc, except_clause);
2501 REQ(body, suite);
2502
2503 if (NCH(exc) == 1) {
2504 asdl_seq *suite_seq = ast_for_suite(c, body);
2505 if (!suite_seq)
2506 return NULL;
2507
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002508 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509 }
2510 else if (NCH(exc) == 2) {
2511 expr_ty expression;
2512 asdl_seq *suite_seq;
2513
2514 expression = ast_for_expr(c, CHILD(exc, 1));
2515 if (!expression)
2516 return NULL;
2517 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002518 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 return NULL;
2520
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002521 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 }
2523 else if (NCH(exc) == 4) {
2524 asdl_seq *suite_seq;
2525 expr_ty expression;
2526 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2527 if (!e)
2528 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002529 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 return NULL;
2531 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002532 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 return NULL;
2534 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002535 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 return NULL;
2537
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002538 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002540
2541 PyErr_Format(PyExc_SystemError,
2542 "wrong number of children for 'except' clause: %d",
2543 NCH(exc));
2544 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545}
2546
2547static stmt_ty
2548ast_for_try_stmt(struct compiling *c, const node *n)
2549{
Neal Norwitzf599f422005-12-17 21:33:47 +00002550 const int nch = NCH(n);
2551 int n_except = (nch - 3)/3;
2552 asdl_seq *body, *orelse = NULL, *finally = NULL;
2553
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 REQ(n, try_stmt);
2555
Neal Norwitzf599f422005-12-17 21:33:47 +00002556 body = ast_for_suite(c, CHILD(n, 2));
2557 if (body == NULL)
2558 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559
Neal Norwitzf599f422005-12-17 21:33:47 +00002560 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2561 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2562 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2563 /* we can assume it's an "else",
2564 because nch >= 9 for try-else-finally and
2565 it would otherwise have a type of except_clause */
2566 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2567 if (orelse == NULL)
2568 return NULL;
2569 n_except--;
2570 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571
Neal Norwitzf599f422005-12-17 21:33:47 +00002572 finally = ast_for_suite(c, CHILD(n, nch - 1));
2573 if (finally == NULL)
2574 return NULL;
2575 n_except--;
2576 }
2577 else {
2578 /* we can assume it's an "else",
2579 otherwise it would have a type of except_clause */
2580 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2581 if (orelse == NULL)
2582 return NULL;
2583 n_except--;
2584 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002586 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002587 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 return NULL;
2589 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002590
2591 if (n_except > 0) {
2592 int i;
2593 stmt_ty except_st;
2594 /* process except statements to create a try ... except */
2595 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2596 if (handlers == NULL)
2597 return NULL;
2598
2599 for (i = 0; i < n_except; i++) {
2600 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2601 CHILD(n, 5 + i * 3));
2602 if (!e)
2603 return NULL;
2604 asdl_seq_SET(handlers, i, e);
2605 }
2606
2607 except_st = TryExcept(body, handlers, orelse, LINENO(n), c->c_arena);
2608 if (!finally)
2609 return except_st;
2610
2611 /* if a 'finally' is present too, we nest the TryExcept within a
2612 TryFinally to emulate try ... except ... finally */
2613 body = asdl_seq_new(1, c->c_arena);
2614 if (body == NULL)
2615 return NULL;
2616 asdl_seq_SET(body, 0, except_st);
2617 }
2618
2619 /* must be a try ... finally (except clauses are in body, if any exist) */
2620 assert(finally != NULL);
2621 return TryFinally(body, finally, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622}
2623
2624static stmt_ty
2625ast_for_classdef(struct compiling *c, const node *n)
2626{
2627 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 asdl_seq *bases, *s;
2629
2630 REQ(n, classdef);
2631
2632 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2633 ast_error(n, "assignment to None");
2634 return NULL;
2635 }
2636
2637 if (NCH(n) == 4) {
2638 s = ast_for_suite(c, CHILD(n, 3));
2639 if (!s)
2640 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002641 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2642 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 }
2644 /* check for empty base list */
2645 if (TYPE(CHILD(n,3)) == RPAR) {
2646 s = ast_for_suite(c, CHILD(n,5));
2647 if (!s)
2648 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002649 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2650 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 }
2652
2653 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002654 bases = ast_for_class_bases(c, CHILD(n, 3));
2655 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657
2658 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002659 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002661 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2662 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663}
2664
2665static stmt_ty
2666ast_for_stmt(struct compiling *c, const node *n)
2667{
2668 if (TYPE(n) == stmt) {
2669 assert(NCH(n) == 1);
2670 n = CHILD(n, 0);
2671 }
2672 if (TYPE(n) == simple_stmt) {
2673 assert(num_stmts(n) == 1);
2674 n = CHILD(n, 0);
2675 }
2676 if (TYPE(n) == small_stmt) {
2677 REQ(n, small_stmt);
2678 n = CHILD(n, 0);
2679 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2680 | flow_stmt | import_stmt | global_stmt | exec_stmt
2681 | assert_stmt
2682 */
2683 switch (TYPE(n)) {
2684 case expr_stmt:
2685 return ast_for_expr_stmt(c, n);
2686 case print_stmt:
2687 return ast_for_print_stmt(c, n);
2688 case del_stmt:
2689 return ast_for_del_stmt(c, n);
2690 case pass_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002691 return Pass(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 case flow_stmt:
2693 return ast_for_flow_stmt(c, n);
2694 case import_stmt:
2695 return ast_for_import_stmt(c, n);
2696 case global_stmt:
2697 return ast_for_global_stmt(c, n);
2698 case exec_stmt:
2699 return ast_for_exec_stmt(c, n);
2700 case assert_stmt:
2701 return ast_for_assert_stmt(c, n);
2702 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002703 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2705 TYPE(n), NCH(n));
2706 return NULL;
2707 }
2708 }
2709 else {
2710 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2711 | funcdef | classdef
2712 */
2713 node *ch = CHILD(n, 0);
2714 REQ(n, compound_stmt);
2715 switch (TYPE(ch)) {
2716 case if_stmt:
2717 return ast_for_if_stmt(c, ch);
2718 case while_stmt:
2719 return ast_for_while_stmt(c, ch);
2720 case for_stmt:
2721 return ast_for_for_stmt(c, ch);
2722 case try_stmt:
2723 return ast_for_try_stmt(c, ch);
2724 case funcdef:
2725 return ast_for_funcdef(c, ch);
2726 case classdef:
2727 return ast_for_classdef(c, ch);
2728 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002729 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2731 TYPE(n), NCH(n));
2732 return NULL;
2733 }
2734 }
2735}
2736
2737static PyObject *
2738parsenumber(const char *s)
2739{
2740 const char *end;
2741 long x;
2742 double dx;
2743#ifndef WITHOUT_COMPLEX
2744 Py_complex c;
2745 int imflag;
2746#endif
2747
2748 errno = 0;
2749 end = s + strlen(s) - 1;
2750#ifndef WITHOUT_COMPLEX
2751 imflag = *end == 'j' || *end == 'J';
2752#endif
2753 if (*end == 'l' || *end == 'L')
2754 return PyLong_FromString((char *)s, (char **)0, 0);
2755 if (s[0] == '0') {
2756 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2757 if (x < 0 && errno == 0) {
2758 return PyLong_FromString((char *)s,
2759 (char **)0,
2760 0);
2761 }
2762 }
2763 else
2764 x = PyOS_strtol((char *)s, (char **)&end, 0);
2765 if (*end == '\0') {
2766 if (errno != 0)
2767 return PyLong_FromString((char *)s, (char **)0, 0);
2768 return PyInt_FromLong(x);
2769 }
2770 /* XXX Huge floats may silently fail */
2771#ifndef WITHOUT_COMPLEX
2772 if (imflag) {
2773 c.real = 0.;
2774 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002775 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 PyFPE_END_PROTECT(c)
2777 return PyComplex_FromCComplex(c);
2778 }
2779 else
2780#endif
2781 {
2782 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002783 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 PyFPE_END_PROTECT(dx)
2785 return PyFloat_FromDouble(dx);
2786 }
2787}
2788
2789static PyObject *
2790decode_utf8(const char **sPtr, const char *end, char* encoding)
2791{
2792#ifndef Py_USING_UNICODE
2793 Py_FatalError("decode_utf8 should not be called in this build.");
2794 return NULL;
2795#else
2796 PyObject *u, *v;
2797 char *s, *t;
2798 t = s = (char *)*sPtr;
2799 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2800 while (s < end && (*s & 0x80)) s++;
2801 *sPtr = s;
2802 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2803 if (u == NULL)
2804 return NULL;
2805 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2806 Py_DECREF(u);
2807 return v;
2808#endif
2809}
2810
2811static PyObject *
2812decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2813{
2814 PyObject *v, *u;
2815 char *buf;
2816 char *p;
2817 const char *end;
2818 if (encoding == NULL) {
2819 buf = (char *)s;
2820 u = NULL;
2821 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2822 buf = (char *)s;
2823 u = NULL;
2824 } else {
2825 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2826 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2827 if (u == NULL)
2828 return NULL;
2829 p = buf = PyString_AsString(u);
2830 end = s + len;
2831 while (s < end) {
2832 if (*s == '\\') {
2833 *p++ = *s++;
2834 if (*s & 0x80) {
2835 strcpy(p, "u005c");
2836 p += 5;
2837 }
2838 }
2839 if (*s & 0x80) { /* XXX inefficient */
2840 PyObject *w;
2841 char *r;
2842 int rn, i;
2843 w = decode_utf8(&s, end, "utf-16-be");
2844 if (w == NULL) {
2845 Py_DECREF(u);
2846 return NULL;
2847 }
2848 r = PyString_AsString(w);
2849 rn = PyString_Size(w);
2850 assert(rn % 2 == 0);
2851 for (i = 0; i < rn; i += 2) {
2852 sprintf(p, "\\u%02x%02x",
2853 r[i + 0] & 0xFF,
2854 r[i + 1] & 0xFF);
2855 p += 6;
2856 }
2857 Py_DECREF(w);
2858 } else {
2859 *p++ = *s++;
2860 }
2861 }
2862 len = p - buf;
2863 s = buf;
2864 }
2865 if (rawmode)
2866 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
2867 else
2868 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
2869 Py_XDECREF(u);
2870 return v;
2871}
2872
2873/* s is a Python string literal, including the bracketing quote characters,
2874 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
2875 * parsestr parses it, and returns the decoded Python string object.
2876 */
2877static PyObject *
2878parsestr(const char *s, const char *encoding)
2879{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00002881 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 int rawmode = 0;
2883 int need_encoding;
2884 int unicode = 0;
2885
2886 if (isalpha(quote) || quote == '_') {
2887 if (quote == 'u' || quote == 'U') {
2888 quote = *++s;
2889 unicode = 1;
2890 }
2891 if (quote == 'r' || quote == 'R') {
2892 quote = *++s;
2893 rawmode = 1;
2894 }
2895 }
2896 if (quote != '\'' && quote != '\"') {
2897 PyErr_BadInternalCall();
2898 return NULL;
2899 }
2900 s++;
2901 len = strlen(s);
2902 if (len > INT_MAX) {
2903 PyErr_SetString(PyExc_OverflowError,
2904 "string to parse is too long");
2905 return NULL;
2906 }
2907 if (s[--len] != quote) {
2908 PyErr_BadInternalCall();
2909 return NULL;
2910 }
2911 if (len >= 4 && s[0] == quote && s[1] == quote) {
2912 s += 2;
2913 len -= 2;
2914 if (s[--len] != quote || s[--len] != quote) {
2915 PyErr_BadInternalCall();
2916 return NULL;
2917 }
2918 }
2919#ifdef Py_USING_UNICODE
2920 if (unicode || Py_UnicodeFlag) {
2921 return decode_unicode(s, len, rawmode, encoding);
2922 }
2923#endif
2924 need_encoding = (encoding != NULL &&
2925 strcmp(encoding, "utf-8") != 0 &&
2926 strcmp(encoding, "iso-8859-1") != 0);
2927 if (rawmode || strchr(s, '\\') == NULL) {
2928 if (need_encoding) {
2929#ifndef Py_USING_UNICODE
2930 /* This should not happen - we never see any other
2931 encoding. */
2932 Py_FatalError("cannot deal with encodings in this build.");
2933#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002934 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935 if (u == NULL)
2936 return NULL;
2937 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2938 Py_DECREF(u);
2939 return v;
2940#endif
2941 } else {
2942 return PyString_FromStringAndSize(s, len);
2943 }
2944 }
2945
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002946 return PyString_DecodeEscape(s, len, NULL, unicode,
2947 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948}
2949
2950/* Build a Python string object out of a STRING atom. This takes care of
2951 * compile-time literal catenation, calling parsestr() on each piece, and
2952 * pasting the intermediate results together.
2953 */
2954static PyObject *
2955parsestrplus(struct compiling *c, const node *n)
2956{
2957 PyObject *v;
2958 int i;
2959 REQ(CHILD(n, 0), STRING);
2960 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
2961 /* String literal concatenation */
2962 for (i = 1; i < NCH(n); i++) {
2963 PyObject *s;
2964 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
2965 if (s == NULL)
2966 goto onError;
2967 if (PyString_Check(v) && PyString_Check(s)) {
2968 PyString_ConcatAndDel(&v, s);
2969 if (v == NULL)
2970 goto onError;
2971 }
2972#ifdef Py_USING_UNICODE
2973 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002974 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 Py_DECREF(v);
2977 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002978 if (v == NULL)
2979 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 }
2981#endif
2982 }
2983 }
2984 return v;
2985
2986 onError:
2987 Py_XDECREF(v);
2988 return NULL;
2989}