blob: dde0d04f87365642044b0689362e8030d1e8d176 [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) {
255 stmt_ty s = ast_for_stmt(&c, n);
256 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++) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001095 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 REQ(ch, gen_iter);
1097 ch = CHILD(ch, 0);
1098 REQ(ch, gen_if);
1099
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001100 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001101 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001102 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001103 asdl_seq_APPEND(ifs, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104 if (NCH(ch) == 3)
1105 ch = CHILD(ch, 2);
1106 }
1107 /* on exit, must guarantee that ch is a gen_for */
1108 if (TYPE(ch) == gen_iter)
1109 ch = CHILD(ch, 0);
1110 ge->ifs = ifs;
1111 }
1112 asdl_seq_APPEND(genexps, ge);
1113 }
1114
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001115 return GeneratorExp(elt, genexps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116}
1117
1118static expr_ty
1119ast_for_atom(struct compiling *c, const node *n)
1120{
1121 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1122 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1123 */
1124 node *ch = CHILD(n, 0);
1125
1126 switch (TYPE(ch)) {
1127 case NAME:
1128 /* All names start in Load context, but may later be
1129 changed. */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001130 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 case STRING: {
1132 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 if (!str)
1134 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001135
1136 PyArena_AddPyObject(c->c_arena, str);
1137 return Str(str, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 }
1139 case NUMBER: {
1140 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 if (!pynum)
1142 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001143
1144 PyArena_AddPyObject(c->c_arena, pynum);
1145 return Num(pynum, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 }
1147 case LPAR: /* some parenthesized expressions */
1148 ch = CHILD(n, 1);
1149
1150 if (TYPE(ch) == RPAR)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001151 return Tuple(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152
1153 if (TYPE(ch) == yield_expr)
1154 return ast_for_expr(c, ch);
1155
1156 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1157 return ast_for_genexp(c, ch);
1158
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001159 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 case LSQB: /* list (or list comprehension) */
1161 ch = CHILD(n, 1);
1162
1163 if (TYPE(ch) == RSQB)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001164 return List(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165
1166 REQ(ch, listmaker);
1167 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1168 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 if (!elts)
1170 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001171
1172 return List(elts, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 }
1174 else
1175 return ast_for_listcomp(c, ch);
1176 case LBRACE: {
1177 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1178 int i, size;
1179 asdl_seq *keys, *values;
1180
1181 ch = CHILD(n, 1);
1182 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001183 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 if (!keys)
1185 return NULL;
1186
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187 values = asdl_seq_new(size, c->c_arena);
1188 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190
1191 for (i = 0; i < NCH(ch); i += 4) {
1192 expr_ty expression;
1193
1194 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001195 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001197
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001199
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001201 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001203
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 asdl_seq_SET(values, i / 4, expression);
1205 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001206 return Dict(keys, values, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 }
1208 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001209 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 if (!expression)
1211 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001212
1213 return Repr(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 }
1215 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001216 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 return NULL;
1218 }
1219}
1220
1221static slice_ty
1222ast_for_slice(struct compiling *c, const node *n)
1223{
1224 node *ch;
1225 expr_ty lower = NULL, upper = NULL, step = NULL;
1226
1227 REQ(n, subscript);
1228
1229 /*
1230 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1231 sliceop: ':' [test]
1232 */
1233 ch = CHILD(n, 0);
1234 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001235 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236
1237 if (NCH(n) == 1 && TYPE(ch) == test) {
1238 /* 'step' variable hold no significance in terms of being used over
1239 other vars */
1240 step = ast_for_expr(c, ch);
1241 if (!step)
1242 return NULL;
1243
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001244 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 }
1246
1247 if (TYPE(ch) == test) {
1248 lower = ast_for_expr(c, ch);
1249 if (!lower)
1250 return NULL;
1251 }
1252
1253 /* If there's an upper bound it's in the second or third position. */
1254 if (TYPE(ch) == COLON) {
1255 if (NCH(n) > 1) {
1256 node *n2 = CHILD(n, 1);
1257
1258 if (TYPE(n2) == test) {
1259 upper = ast_for_expr(c, n2);
1260 if (!upper)
1261 return NULL;
1262 }
1263 }
1264 } else if (NCH(n) > 2) {
1265 node *n2 = CHILD(n, 2);
1266
1267 if (TYPE(n2) == test) {
1268 upper = ast_for_expr(c, n2);
1269 if (!upper)
1270 return NULL;
1271 }
1272 }
1273
1274 ch = CHILD(n, NCH(n) - 1);
1275 if (TYPE(ch) == sliceop) {
1276 if (NCH(ch) == 1)
1277 /* XXX: If only 1 child, then should just be a colon. Should we
1278 just skip assigning and just get to the return? */
1279 ch = CHILD(ch, 0);
1280 else
1281 ch = CHILD(ch, 1);
1282 if (TYPE(ch) == test) {
1283 step = ast_for_expr(c, ch);
1284 if (!step)
1285 return NULL;
1286 }
1287 }
1288
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001289 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290}
1291
1292static expr_ty
1293ast_for_binop(struct compiling *c, const node *n)
1294{
1295 /* Must account for a sequence of expressions.
1296 How should A op B op C by represented?
1297 BinOp(BinOp(A, op, B), op, C).
1298 */
1299
1300 int i, nops;
1301 expr_ty expr1, expr2, result;
1302 operator_ty operator;
1303
1304 expr1 = ast_for_expr(c, CHILD(n, 0));
1305 if (!expr1)
1306 return NULL;
1307
1308 expr2 = ast_for_expr(c, CHILD(n, 2));
1309 if (!expr2)
1310 return NULL;
1311
1312 operator = get_operator(CHILD(n, 1));
1313 if (!operator)
1314 return NULL;
1315
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001316 result = BinOp(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 if (!result)
1318 return NULL;
1319
1320 nops = (NCH(n) - 1) / 2;
1321 for (i = 1; i < nops; i++) {
1322 expr_ty tmp_result, tmp;
1323 const node* next_oper = CHILD(n, i * 2 + 1);
1324
1325 operator = get_operator(next_oper);
1326 if (!operator)
1327 return NULL;
1328
1329 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1330 if (!tmp)
1331 return NULL;
1332
1333 tmp_result = BinOp(result, operator, tmp,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001334 LINENO(next_oper), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 if (!tmp)
1336 return NULL;
1337 result = tmp_result;
1338 }
1339 return result;
1340}
1341
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001342static expr_ty
1343ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1344{
1345 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1346 expr_ty e;
1347 REQ(n, trailer);
1348 if (TYPE(CHILD(n, 0)) == LPAR) {
1349 if (NCH(n) == 2)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001350 e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001351 else
1352 e = ast_for_call(c, CHILD(n, 1), left_expr);
1353 }
1354 else if (TYPE(CHILD(n, 0)) == LSQB) {
1355 REQ(CHILD(n, 2), RSQB);
1356 n = CHILD(n, 1);
1357 if (NCH(n) <= 2) {
1358 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1359 if (!slc)
1360 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001361 e = Subscript(left_expr, slc, Load, LINENO(n), c->c_arena);
1362 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001363 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001364 }
1365 else {
1366 int j;
1367 slice_ty slc;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001368 asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001369 if (!slices)
1370 return NULL;
1371 for (j = 0; j < NCH(n); j += 2) {
1372 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001373 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001374 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001375 asdl_seq_SET(slices, j / 2, slc);
1376 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001377 e = Subscript(left_expr, ExtSlice(slices, c->c_arena),
1378 Load, LINENO(n), c->c_arena);
1379 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001380 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001381 }
1382 }
1383 else {
1384 assert(TYPE(CHILD(n, 0)) == DOT);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001385 e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n),
1386 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001387 }
1388 return e;
1389}
1390
1391static expr_ty
1392ast_for_power(struct compiling *c, const node *n)
1393{
1394 /* power: atom trailer* ('**' factor)*
1395 */
1396 int i;
1397 expr_ty e, tmp;
1398 REQ(n, power);
1399 e = ast_for_atom(c, CHILD(n, 0));
1400 if (!e)
1401 return NULL;
1402 if (NCH(n) == 1)
1403 return e;
1404 for (i = 1; i < NCH(n); i++) {
1405 node *ch = CHILD(n, i);
1406 if (TYPE(ch) != trailer)
1407 break;
1408 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001409 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001410 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001411 e = tmp;
1412 }
1413 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1414 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001415 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001416 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001417 tmp = BinOp(e, Pow, f, LINENO(n), c->c_arena);
1418 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001419 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001420 e = tmp;
1421 }
1422 return e;
1423}
1424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425/* Do not name a variable 'expr'! Will cause a compile error.
1426*/
1427
1428static expr_ty
1429ast_for_expr(struct compiling *c, const node *n)
1430{
1431 /* handle the full range of simple expressions
1432 test: and_test ('or' and_test)* | lambdef
1433 and_test: not_test ('and' not_test)*
1434 not_test: 'not' not_test | comparison
1435 comparison: expr (comp_op expr)*
1436 expr: xor_expr ('|' xor_expr)*
1437 xor_expr: and_expr ('^' and_expr)*
1438 and_expr: shift_expr ('&' shift_expr)*
1439 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1440 arith_expr: term (('+'|'-') term)*
1441 term: factor (('*'|'/'|'%'|'//') factor)*
1442 factor: ('+'|'-'|'~') factor | power
1443 power: atom trailer* ('**' factor)*
1444 */
1445
1446 asdl_seq *seq;
1447 int i;
1448
1449 loop:
1450 switch (TYPE(n)) {
1451 case test:
1452 if (TYPE(CHILD(n, 0)) == lambdef)
1453 return ast_for_lambdef(c, CHILD(n, 0));
1454 /* Fall through to and_test */
1455 case and_test:
1456 if (NCH(n) == 1) {
1457 n = CHILD(n, 0);
1458 goto loop;
1459 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001460 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 if (!seq)
1462 return NULL;
1463 for (i = 0; i < NCH(n); i += 2) {
1464 expr_ty e = ast_for_expr(c, CHILD(n, i));
1465 if (!e)
1466 return NULL;
1467 asdl_seq_SET(seq, i / 2, e);
1468 }
1469 if (!strcmp(STR(CHILD(n, 1)), "and"))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001470 return BoolOp(And, seq, LINENO(n), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001471 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1472 return BoolOp(Or, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 case not_test:
1474 if (NCH(n) == 1) {
1475 n = CHILD(n, 0);
1476 goto loop;
1477 }
1478 else {
1479 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1480 if (!expression)
1481 return NULL;
1482
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001483 return UnaryOp(Not, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 }
1485 case comparison:
1486 if (NCH(n) == 1) {
1487 n = CHILD(n, 0);
1488 goto loop;
1489 }
1490 else {
1491 expr_ty expression;
1492 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001493 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 if (!ops)
1495 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001496 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 return NULL;
1499 }
1500 for (i = 1; i < NCH(n); i += 2) {
1501 /* XXX cmpop_ty is just an enum */
1502 cmpop_ty operator;
1503
1504 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001505 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508
1509 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001510 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001512 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513
1514 asdl_seq_SET(ops, i / 2, (void *)operator);
1515 asdl_seq_SET(cmps, i / 2, expression);
1516 }
1517 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001518 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001520 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001522 return Compare(expression, ops, cmps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 }
1524 break;
1525
1526 /* The next five cases all handle BinOps. The main body of code
1527 is the same in each case, but the switch turned inside out to
1528 reuse the code for each type of operator.
1529 */
1530 case expr:
1531 case xor_expr:
1532 case and_expr:
1533 case shift_expr:
1534 case arith_expr:
1535 case term:
1536 if (NCH(n) == 1) {
1537 n = CHILD(n, 0);
1538 goto loop;
1539 }
1540 return ast_for_binop(c, n);
1541 case yield_expr: {
1542 expr_ty exp = NULL;
1543 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001544 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 if (!exp)
1546 return NULL;
1547 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001548 return Yield(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 }
1550 case factor: {
1551 expr_ty expression;
1552
1553 if (NCH(n) == 1) {
1554 n = CHILD(n, 0);
1555 goto loop;
1556 }
1557
1558 expression = ast_for_expr(c, CHILD(n, 1));
1559 if (!expression)
1560 return NULL;
1561
1562 switch (TYPE(CHILD(n, 0))) {
1563 case PLUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001564 return UnaryOp(UAdd, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 case MINUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001566 return UnaryOp(USub, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 case TILDE:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001568 return UnaryOp(Invert, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001570 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1571 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 break;
1573 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001574 case power:
1575 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001577 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 return NULL;
1579 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001580 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 return NULL;
1582}
1583
1584static expr_ty
1585ast_for_call(struct compiling *c, const node *n, expr_ty func)
1586{
1587 /*
1588 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1589 | '**' test)
1590 argument: [test '='] test [gen_for] # Really [keyword '='] test
1591 */
1592
1593 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001594 asdl_seq *args;
1595 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 expr_ty vararg = NULL, kwarg = NULL;
1597
1598 REQ(n, arglist);
1599
1600 nargs = 0;
1601 nkeywords = 0;
1602 ngens = 0;
1603 for (i = 0; i < NCH(n); i++) {
1604 node *ch = CHILD(n, i);
1605 if (TYPE(ch) == argument) {
1606 if (NCH(ch) == 1)
1607 nargs++;
1608 else if (TYPE(CHILD(ch, 1)) == gen_for)
1609 ngens++;
1610 else
1611 nkeywords++;
1612 }
1613 }
1614 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
1615 ast_error(n, "Generator expression must be parenthesised "
1616 "if not sole argument");
1617 return NULL;
1618 }
1619
1620 if (nargs + nkeywords + ngens > 255) {
1621 ast_error(n, "more than 255 arguments");
1622 return NULL;
1623 }
1624
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001625 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001627 return NULL;
1628 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001630 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631 nargs = 0;
1632 nkeywords = 0;
1633 for (i = 0; i < NCH(n); i++) {
1634 node *ch = CHILD(n, i);
1635 if (TYPE(ch) == argument) {
1636 expr_ty e;
1637 if (NCH(ch) == 1) {
1638 e = ast_for_expr(c, CHILD(ch, 0));
1639 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641 asdl_seq_SET(args, nargs++, e);
1642 }
1643 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1644 e = ast_for_genexp(c, ch);
1645 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001646 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 asdl_seq_SET(args, nargs++, e);
1648 }
1649 else {
1650 keyword_ty kw;
1651 identifier key;
1652
1653 /* CHILD(ch, 0) is test, but must be an identifier? */
1654 e = ast_for_expr(c, CHILD(ch, 0));
1655 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001656 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 /* f(lambda x: x[0] = 3) ends up getting parsed with
1658 * LHS test = lambda x: x[0], and RHS test = 3.
1659 * SF bug 132313 points out that complaining about a keyword
1660 * then is very confusing.
1661 */
1662 if (e->kind == Lambda_kind) {
1663 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001664 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 } else if (e->kind != Name_kind) {
1666 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001667 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 }
1669 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 e = ast_for_expr(c, CHILD(ch, 2));
1671 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001672 return NULL;
1673 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001675 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 asdl_seq_SET(keywords, nkeywords++, kw);
1677 }
1678 }
1679 else if (TYPE(ch) == STAR) {
1680 vararg = ast_for_expr(c, CHILD(n, i+1));
1681 i++;
1682 }
1683 else if (TYPE(ch) == DOUBLESTAR) {
1684 kwarg = ast_for_expr(c, CHILD(n, i+1));
1685 i++;
1686 }
1687 }
1688
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001689 return Call(func, args, keywords, vararg, kwarg, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690}
1691
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001693ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001695 /* testlist_gexp: test (',' test)* [','] */
1696 /* testlist: test (',' test)* [','] */
1697 /* testlist_safe: test (',' test)+ [','] */
1698 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001700 if (TYPE(n) == testlist_gexp) {
1701 if (NCH(n) > 1)
1702 assert(TYPE(CHILD(n, 1)) != gen_for);
1703 }
1704 else {
1705 assert(TYPE(n) == testlist ||
1706 TYPE(n) == testlist_safe ||
1707 TYPE(n) == testlist1);
1708 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 if (NCH(n) == 1)
1710 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 else {
1712 asdl_seq *tmp = seq_for_testlist(c, n);
1713 if (!tmp)
1714 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001715 return Tuple(tmp, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001717}
1718
1719static expr_ty
1720ast_for_testlist_gexp(struct compiling *c, const node* n)
1721{
1722 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1723 /* argument: test [ gen_for ] */
1724 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001725 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001726 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001727 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001728}
1729
1730/* like ast_for_testlist() but returns a sequence */
1731static asdl_seq*
1732ast_for_class_bases(struct compiling *c, const node* n)
1733{
1734 /* testlist: test (',' test)* [','] */
1735 assert(NCH(n) > 0);
1736 REQ(n, testlist);
1737 if (NCH(n) == 1) {
1738 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001739 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001740 if (!bases)
1741 return NULL;
1742 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001743 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001744 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001745 asdl_seq_SET(bases, 0, base);
1746 return bases;
1747 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001748
1749 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750}
1751
1752static stmt_ty
1753ast_for_expr_stmt(struct compiling *c, const node *n)
1754{
1755 REQ(n, expr_stmt);
1756 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1757 | ('=' (yield_expr|testlist))*)
1758 testlist: test (',' test)* [',']
1759 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1760 | '<<=' | '>>=' | '**=' | '//='
1761 test: ... here starts the operator precendence dance
1762 */
1763
1764 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001765 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 if (!e)
1767 return NULL;
1768
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001769 return Expr(e, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 }
1771 else if (TYPE(CHILD(n, 1)) == augassign) {
1772 expr_ty expr1, expr2;
1773 operator_ty operator;
1774 node *ch = CHILD(n, 0);
1775
1776 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001777 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001779 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch),
1780 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781
1782 if (!expr1)
1783 return NULL;
1784 if (expr1->kind == GeneratorExp_kind) {
1785 ast_error(ch, "augmented assignment to generator "
1786 "expression not possible");
1787 return NULL;
1788 }
1789 if (expr1->kind == Name_kind) {
1790 char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1791 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1792 ast_error(ch, "assignment to None");
1793 return NULL;
1794 }
1795 }
1796
1797 ch = CHILD(n, 2);
1798 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001799 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001801 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001802 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803 return NULL;
1804
1805 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001806 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 return NULL;
1808
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001809 return AugAssign(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 }
1811 else {
1812 int i;
1813 asdl_seq *targets;
1814 node *value;
1815 expr_ty expression;
1816
1817 /* a normal assignment */
1818 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001819 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 if (!targets)
1821 return NULL;
1822 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001823 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824 node *ch = CHILD(n, i);
1825 if (TYPE(ch) == yield_expr) {
1826 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001829 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830
1831 /* set context to assign */
1832 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834
Neal Norwitz84456bd2005-12-18 03:16:20 +00001835 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837
1838 asdl_seq_SET(targets, i / 2, e);
1839 }
1840 value = CHILD(n, NCH(n) - 1);
1841 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001842 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 else
1844 expression = ast_for_expr(c, value);
1845 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001846 return NULL;
1847 return Assign(targets, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849}
1850
1851static stmt_ty
1852ast_for_print_stmt(struct compiling *c, const node *n)
1853{
1854 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1855 | '>>' test [ (',' test)+ [','] ] )
1856 */
1857 expr_ty dest = NULL, expression;
1858 asdl_seq *seq;
1859 bool nl;
1860 int i, start = 1;
1861
1862 REQ(n, print_stmt);
1863 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1864 dest = ast_for_expr(c, CHILD(n, 2));
1865 if (!dest)
1866 return NULL;
1867 start = 4;
1868 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 if (!seq)
1871 return NULL;
1872 for (i = start; i < NCH(n); i += 2) {
1873 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001874 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876
1877 asdl_seq_APPEND(seq, expression);
1878 }
1879 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001880 return Print(dest, seq, nl, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881}
1882
1883static asdl_seq *
1884ast_for_exprlist(struct compiling *c, const node *n, int context)
1885{
1886 asdl_seq *seq;
1887 int i;
1888 expr_ty e;
1889
1890 REQ(n, exprlist);
1891
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 if (!seq)
1894 return NULL;
1895 for (i = 0; i < NCH(n); i += 2) {
1896 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001897 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001898 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00001899 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001900 if (context && !set_context(e, context, CHILD(n, i)))
1901 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902 }
1903 return seq;
1904}
1905
1906static stmt_ty
1907ast_for_del_stmt(struct compiling *c, const node *n)
1908{
1909 asdl_seq *expr_list;
1910
1911 /* del_stmt: 'del' exprlist */
1912 REQ(n, del_stmt);
1913
1914 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
1915 if (!expr_list)
1916 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001917 return Delete(expr_list, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918}
1919
1920static stmt_ty
1921ast_for_flow_stmt(struct compiling *c, const node *n)
1922{
1923 /*
1924 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
1925 | yield_stmt
1926 break_stmt: 'break'
1927 continue_stmt: 'continue'
1928 return_stmt: 'return' [testlist]
1929 yield_stmt: yield_expr
1930 yield_expr: 'yield' testlist
1931 raise_stmt: 'raise' [test [',' test [',' test]]]
1932 */
1933 node *ch;
1934
1935 REQ(n, flow_stmt);
1936 ch = CHILD(n, 0);
1937 switch (TYPE(ch)) {
1938 case break_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001939 return Break(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 case continue_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001941 return Continue(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 case yield_stmt: { /* will reduce to yield_expr */
1943 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
1944 if (!exp)
1945 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001946 return Expr(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947 }
1948 case return_stmt:
1949 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001950 return Return(NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001952 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953 if (!expression)
1954 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001955 return Return(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956 }
1957 case raise_stmt:
1958 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001959 return Raise(NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 else if (NCH(ch) == 2) {
1961 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
1962 if (!expression)
1963 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001964 return Raise(expression, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965 }
1966 else if (NCH(ch) == 4) {
1967 expr_ty expr1, expr2;
1968
1969 expr1 = ast_for_expr(c, CHILD(ch, 1));
1970 if (!expr1)
1971 return NULL;
1972 expr2 = ast_for_expr(c, CHILD(ch, 3));
1973 if (!expr2)
1974 return NULL;
1975
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001976 return Raise(expr1, expr2, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 }
1978 else if (NCH(ch) == 6) {
1979 expr_ty expr1, expr2, expr3;
1980
1981 expr1 = ast_for_expr(c, CHILD(ch, 1));
1982 if (!expr1)
1983 return NULL;
1984 expr2 = ast_for_expr(c, CHILD(ch, 3));
1985 if (!expr2)
1986 return NULL;
1987 expr3 = ast_for_expr(c, CHILD(ch, 5));
1988 if (!expr3)
1989 return NULL;
1990
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001991 return Raise(expr1, expr2, expr3, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 }
1993 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001994 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 "unexpected flow_stmt: %d", TYPE(ch));
1996 return NULL;
1997 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001998
1999 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001}
2002
2003static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002004alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005{
2006 /*
2007 import_as_name: NAME [NAME NAME]
2008 dotted_as_name: dotted_name [NAME NAME]
2009 dotted_name: NAME ('.' NAME)*
2010 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002011 PyObject *str;
2012
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 loop:
2014 switch (TYPE(n)) {
2015 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002016 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2017 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 case dotted_as_name:
2019 if (NCH(n) == 1) {
2020 n = CHILD(n, 0);
2021 goto loop;
2022 }
2023 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002024 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 assert(!a->asname);
2026 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2027 return a;
2028 }
2029 break;
2030 case dotted_name:
2031 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002032 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 else {
2034 /* Create a string of the form "a.b.c" */
2035 int i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 char *s;
2037
2038 len = 0;
2039 for (i = 0; i < NCH(n); i += 2)
2040 /* length of string plus one for the dot */
2041 len += strlen(STR(CHILD(n, i))) + 1;
2042 len--; /* the last name doesn't have a dot */
2043 str = PyString_FromStringAndSize(NULL, len);
2044 if (!str)
2045 return NULL;
2046 s = PyString_AS_STRING(str);
2047 if (!s)
2048 return NULL;
2049 for (i = 0; i < NCH(n); i += 2) {
2050 char *sch = STR(CHILD(n, i));
2051 strcpy(s, STR(CHILD(n, i)));
2052 s += strlen(sch);
2053 *s++ = '.';
2054 }
2055 --s;
2056 *s = '\0';
2057 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002058 PyArena_AddPyObject(c->c_arena, str);
2059 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 }
2061 break;
2062 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002063 str = PyString_InternFromString("*");
2064 PyArena_AddPyObject(c->c_arena, str);
2065 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002067 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 "unexpected import name: %d", TYPE(n));
2069 return NULL;
2070 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002071
2072 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 return NULL;
2074}
2075
2076static stmt_ty
2077ast_for_import_stmt(struct compiling *c, const node *n)
2078{
2079 /*
2080 import_stmt: import_name | import_from
2081 import_name: 'import' dotted_as_names
2082 import_from: 'from' dotted_name 'import' ('*' |
2083 '(' import_as_names ')' |
2084 import_as_names)
2085 */
2086 int i;
2087 asdl_seq *aliases;
2088
2089 REQ(n, import_stmt);
2090 n = CHILD(n, 0);
2091 if (STR(CHILD(n, 0))[0] == 'i') { /* import */
2092 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002093 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002094 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 if (!aliases)
2096 return NULL;
2097 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002098 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002099 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 asdl_seq_SET(aliases, i / 2, import_alias);
2102 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002103 return Import(aliases, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 }
2105 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 int n_children;
2107 const char *from_modules;
2108 int lineno = LINENO(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002109 alias_ty mod = alias_for_import_name(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 if (!mod)
2111 return NULL;
2112
2113 /* XXX this needs to be cleaned up */
2114
2115 from_modules = STR(CHILD(n, 3));
2116 if (!from_modules) {
2117 n = CHILD(n, 3); /* from ... import x, y, z */
2118 if (NCH(n) % 2 == 0) {
2119 /* it ends with a comma, not valid but the parser allows it */
2120 ast_error(n, "trailing comma not allowed without"
2121 " surrounding parentheses");
2122 return NULL;
2123 }
2124 }
2125 else if (from_modules[0] == '*') {
2126 n = CHILD(n, 3); /* from ... import * */
2127 }
2128 else if (from_modules[0] == '(')
2129 n = CHILD(n, 4); /* from ... import (x, y, z) */
Neal Norwitze76adcd2005-11-15 05:04:31 +00002130 else {
2131 /* XXX: don't we need to call ast_error(n, "..."); */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002133 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
2135 n_children = NCH(n);
2136 if (from_modules && from_modules[0] == '*')
2137 n_children = 1;
2138
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002139 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002140 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142
2143 /* handle "from ... import *" special b/c there's no children */
2144 if (from_modules && from_modules[0] == '*') {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002145 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002146 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 asdl_seq_APPEND(aliases, import_alias);
2149 }
2150
2151 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002152 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002153 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 asdl_seq_APPEND(aliases, import_alias);
2156 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002157 return ImportFrom(mod->name, aliases, lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 }
Neal Norwitz79792652005-11-14 04:25:03 +00002159 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 "unknown import statement: starts with command '%s'",
2161 STR(CHILD(n, 0)));
2162 return NULL;
2163}
2164
2165static stmt_ty
2166ast_for_global_stmt(struct compiling *c, const node *n)
2167{
2168 /* global_stmt: 'global' NAME (',' NAME)* */
2169 identifier name;
2170 asdl_seq *s;
2171 int i;
2172
2173 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002174 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 if (!s)
2176 return NULL;
2177 for (i = 1; i < NCH(n); i += 2) {
2178 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002179 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 asdl_seq_SET(s, i / 2, name);
2182 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002183 return Global(s, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184}
2185
2186static stmt_ty
2187ast_for_exec_stmt(struct compiling *c, const node *n)
2188{
2189 expr_ty expr1, globals = NULL, locals = NULL;
2190 int n_children = NCH(n);
2191 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002192 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 "poorly formed 'exec' statement: %d parts to statement",
2194 n_children);
2195 return NULL;
2196 }
2197
2198 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2199 REQ(n, exec_stmt);
2200 expr1 = ast_for_expr(c, CHILD(n, 1));
2201 if (!expr1)
2202 return NULL;
2203 if (n_children >= 4) {
2204 globals = ast_for_expr(c, CHILD(n, 3));
2205 if (!globals)
2206 return NULL;
2207 }
2208 if (n_children == 6) {
2209 locals = ast_for_expr(c, CHILD(n, 5));
2210 if (!locals)
2211 return NULL;
2212 }
2213
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002214 return Exec(expr1, globals, locals, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215}
2216
2217static stmt_ty
2218ast_for_assert_stmt(struct compiling *c, const node *n)
2219{
2220 /* assert_stmt: 'assert' test [',' test] */
2221 REQ(n, assert_stmt);
2222 if (NCH(n) == 2) {
2223 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2224 if (!expression)
2225 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002226 return Assert(expression, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 }
2228 else if (NCH(n) == 4) {
2229 expr_ty expr1, expr2;
2230
2231 expr1 = ast_for_expr(c, CHILD(n, 1));
2232 if (!expr1)
2233 return NULL;
2234 expr2 = ast_for_expr(c, CHILD(n, 3));
2235 if (!expr2)
2236 return NULL;
2237
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002238 return Assert(expr1, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 }
Neal Norwitz79792652005-11-14 04:25:03 +00002240 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 "improper number of parts to 'assert' statement: %d",
2242 NCH(n));
2243 return NULL;
2244}
2245
2246static asdl_seq *
2247ast_for_suite(struct compiling *c, const node *n)
2248{
2249 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002250 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 stmt_ty s;
2252 int i, total, num, end, pos = 0;
2253 node *ch;
2254
2255 REQ(n, suite);
2256
2257 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002258 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 if (!seq)
2260 return NULL;
2261 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2262 n = CHILD(n, 0);
2263 /* simple_stmt always ends with a NEWLINE,
2264 and may have a trailing SEMI
2265 */
2266 end = NCH(n) - 1;
2267 if (TYPE(CHILD(n, end - 1)) == SEMI)
2268 end--;
2269 /* loop by 2 to skip semi-colons */
2270 for (i = 0; i < end; i += 2) {
2271 ch = CHILD(n, i);
2272 s = ast_for_stmt(c, ch);
2273 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 asdl_seq_SET(seq, pos++, s);
2276 }
2277 }
2278 else {
2279 for (i = 2; i < (NCH(n) - 1); i++) {
2280 ch = CHILD(n, i);
2281 REQ(ch, stmt);
2282 num = num_stmts(ch);
2283 if (num == 1) {
2284 /* small_stmt or compound_stmt with only one child */
2285 s = ast_for_stmt(c, ch);
2286 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002287 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 asdl_seq_SET(seq, pos++, s);
2289 }
2290 else {
2291 int j;
2292 ch = CHILD(ch, 0);
2293 REQ(ch, simple_stmt);
2294 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002295 /* statement terminates with a semi-colon ';' */
2296 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002297 assert((j + 1) == NCH(ch));
2298 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 s = ast_for_stmt(c, CHILD(ch, j));
2301 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002302 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303 asdl_seq_SET(seq, pos++, s);
2304 }
2305 }
2306 }
2307 }
2308 assert(pos == seq->size);
2309 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310}
2311
2312static stmt_ty
2313ast_for_if_stmt(struct compiling *c, const node *n)
2314{
2315 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2316 ['else' ':' suite]
2317 */
2318 char *s;
2319
2320 REQ(n, if_stmt);
2321
2322 if (NCH(n) == 4) {
2323 expr_ty expression;
2324 asdl_seq *suite_seq;
2325
2326 expression = ast_for_expr(c, CHILD(n, 1));
2327 if (!expression)
2328 return NULL;
2329 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002330 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 return NULL;
2332
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002333 return If(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002335
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 s = STR(CHILD(n, 4));
2337 /* s[2], the third character in the string, will be
2338 's' for el_s_e, or
2339 'i' for el_i_f
2340 */
2341 if (s[2] == 's') {
2342 expr_ty expression;
2343 asdl_seq *seq1, *seq2;
2344
2345 expression = ast_for_expr(c, CHILD(n, 1));
2346 if (!expression)
2347 return NULL;
2348 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002349 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 return NULL;
2351 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002352 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002353 return NULL;
2354
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002355 return If(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 }
2357 else if (s[2] == 'i') {
2358 int i, n_elif, has_else = 0;
2359 asdl_seq *orelse = NULL;
2360 n_elif = NCH(n) - 4;
2361 /* must reference the child n_elif+1 since 'else' token is third,
2362 not fourth, child from the end. */
2363 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2364 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2365 has_else = 1;
2366 n_elif -= 3;
2367 }
2368 n_elif /= 4;
2369
2370 if (has_else) {
2371 expr_ty expression;
2372 asdl_seq *seq1, *seq2;
2373
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002374 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375 if (!orelse)
2376 return NULL;
2377 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002378 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002381 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002384 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386
2387 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002388 LINENO(CHILD(n, NCH(n) - 6)),
2389 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390 /* the just-created orelse handled the last elif */
2391 n_elif--;
2392 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393
2394 for (i = 0; i < n_elif; i++) {
2395 int off = 5 + (n_elif - i - 1) * 4;
2396 expr_ty expression;
2397 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002398 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002399 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400 return NULL;
2401 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002402 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002405 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407
2408 asdl_seq_SET(new, 0,
2409 If(expression, suite_seq, orelse,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002410 LINENO(CHILD(n, off)), c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411 orelse = new;
2412 }
2413 return If(ast_for_expr(c, CHILD(n, 1)),
2414 ast_for_suite(c, CHILD(n, 3)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002415 orelse, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002417
2418 PyErr_Format(PyExc_SystemError,
2419 "unexpected token in 'if' statement: %s", s);
2420 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421}
2422
2423static stmt_ty
2424ast_for_while_stmt(struct compiling *c, const node *n)
2425{
2426 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2427 REQ(n, while_stmt);
2428
2429 if (NCH(n) == 4) {
2430 expr_ty expression;
2431 asdl_seq *suite_seq;
2432
2433 expression = ast_for_expr(c, CHILD(n, 1));
2434 if (!expression)
2435 return NULL;
2436 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002437 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002439 return While(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 }
2441 else if (NCH(n) == 7) {
2442 expr_ty expression;
2443 asdl_seq *seq1, *seq2;
2444
2445 expression = ast_for_expr(c, CHILD(n, 1));
2446 if (!expression)
2447 return NULL;
2448 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002449 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 return NULL;
2451 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002452 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 return NULL;
2454
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002455 return While(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002457
2458 PyErr_Format(PyExc_SystemError,
2459 "wrong number of tokens for 'while' statement: %d",
2460 NCH(n));
2461 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462}
2463
2464static stmt_ty
2465ast_for_for_stmt(struct compiling *c, const node *n)
2466{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002467 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 expr_ty expression;
2469 expr_ty target;
2470 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2471 REQ(n, for_stmt);
2472
2473 if (NCH(n) == 9) {
2474 seq = ast_for_suite(c, CHILD(n, 8));
2475 if (!seq)
2476 return NULL;
2477 }
2478
2479 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002480 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002482 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002485 target = Tuple(_target, Store, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002487 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002488 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 return NULL;
2490 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002491 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 return NULL;
2493
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002494 return For(target, expression, suite_seq, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495}
2496
2497static excepthandler_ty
2498ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2499{
2500 /* except_clause: 'except' [test [',' test]] */
2501 REQ(exc, except_clause);
2502 REQ(body, suite);
2503
2504 if (NCH(exc) == 1) {
2505 asdl_seq *suite_seq = ast_for_suite(c, body);
2506 if (!suite_seq)
2507 return NULL;
2508
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002509 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 }
2511 else if (NCH(exc) == 2) {
2512 expr_ty expression;
2513 asdl_seq *suite_seq;
2514
2515 expression = ast_for_expr(c, CHILD(exc, 1));
2516 if (!expression)
2517 return NULL;
2518 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002519 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 return NULL;
2521
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002522 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 }
2524 else if (NCH(exc) == 4) {
2525 asdl_seq *suite_seq;
2526 expr_ty expression;
2527 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2528 if (!e)
2529 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002530 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 return NULL;
2532 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002533 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 return NULL;
2535 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002536 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 return NULL;
2538
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002539 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002541
2542 PyErr_Format(PyExc_SystemError,
2543 "wrong number of children for 'except' clause: %d",
2544 NCH(exc));
2545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546}
2547
2548static stmt_ty
2549ast_for_try_stmt(struct compiling *c, const node *n)
2550{
Neal Norwitzf599f422005-12-17 21:33:47 +00002551 const int nch = NCH(n);
2552 int n_except = (nch - 3)/3;
2553 asdl_seq *body, *orelse = NULL, *finally = NULL;
2554
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 REQ(n, try_stmt);
2556
Neal Norwitzf599f422005-12-17 21:33:47 +00002557 body = ast_for_suite(c, CHILD(n, 2));
2558 if (body == NULL)
2559 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560
Neal Norwitzf599f422005-12-17 21:33:47 +00002561 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2562 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2563 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2564 /* we can assume it's an "else",
2565 because nch >= 9 for try-else-finally and
2566 it would otherwise have a type of except_clause */
2567 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2568 if (orelse == NULL)
2569 return NULL;
2570 n_except--;
2571 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572
Neal Norwitzf599f422005-12-17 21:33:47 +00002573 finally = ast_for_suite(c, CHILD(n, nch - 1));
2574 if (finally == NULL)
2575 return NULL;
2576 n_except--;
2577 }
2578 else {
2579 /* we can assume it's an "else",
2580 otherwise it would have a type of except_clause */
2581 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2582 if (orelse == NULL)
2583 return NULL;
2584 n_except--;
2585 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002587 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002588 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 return NULL;
2590 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002591
2592 if (n_except > 0) {
2593 int i;
2594 stmt_ty except_st;
2595 /* process except statements to create a try ... except */
2596 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2597 if (handlers == NULL)
2598 return NULL;
2599
2600 for (i = 0; i < n_except; i++) {
2601 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2602 CHILD(n, 5 + i * 3));
2603 if (!e)
2604 return NULL;
2605 asdl_seq_SET(handlers, i, e);
2606 }
2607
2608 except_st = TryExcept(body, handlers, orelse, LINENO(n), c->c_arena);
2609 if (!finally)
2610 return except_st;
2611
2612 /* if a 'finally' is present too, we nest the TryExcept within a
2613 TryFinally to emulate try ... except ... finally */
2614 body = asdl_seq_new(1, c->c_arena);
2615 if (body == NULL)
2616 return NULL;
2617 asdl_seq_SET(body, 0, except_st);
2618 }
2619
2620 /* must be a try ... finally (except clauses are in body, if any exist) */
2621 assert(finally != NULL);
2622 return TryFinally(body, finally, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623}
2624
2625static stmt_ty
2626ast_for_classdef(struct compiling *c, const node *n)
2627{
2628 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 asdl_seq *bases, *s;
2630
2631 REQ(n, classdef);
2632
2633 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2634 ast_error(n, "assignment to None");
2635 return NULL;
2636 }
2637
2638 if (NCH(n) == 4) {
2639 s = ast_for_suite(c, CHILD(n, 3));
2640 if (!s)
2641 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002642 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2643 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 }
2645 /* check for empty base list */
2646 if (TYPE(CHILD(n,3)) == RPAR) {
2647 s = ast_for_suite(c, CHILD(n,5));
2648 if (!s)
2649 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002650 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2651 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 }
2653
2654 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002655 bases = ast_for_class_bases(c, CHILD(n, 3));
2656 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658
2659 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002660 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002662 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2663 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664}
2665
2666static stmt_ty
2667ast_for_stmt(struct compiling *c, const node *n)
2668{
2669 if (TYPE(n) == stmt) {
2670 assert(NCH(n) == 1);
2671 n = CHILD(n, 0);
2672 }
2673 if (TYPE(n) == simple_stmt) {
2674 assert(num_stmts(n) == 1);
2675 n = CHILD(n, 0);
2676 }
2677 if (TYPE(n) == small_stmt) {
2678 REQ(n, small_stmt);
2679 n = CHILD(n, 0);
2680 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2681 | flow_stmt | import_stmt | global_stmt | exec_stmt
2682 | assert_stmt
2683 */
2684 switch (TYPE(n)) {
2685 case expr_stmt:
2686 return ast_for_expr_stmt(c, n);
2687 case print_stmt:
2688 return ast_for_print_stmt(c, n);
2689 case del_stmt:
2690 return ast_for_del_stmt(c, n);
2691 case pass_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002692 return Pass(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 case flow_stmt:
2694 return ast_for_flow_stmt(c, n);
2695 case import_stmt:
2696 return ast_for_import_stmt(c, n);
2697 case global_stmt:
2698 return ast_for_global_stmt(c, n);
2699 case exec_stmt:
2700 return ast_for_exec_stmt(c, n);
2701 case assert_stmt:
2702 return ast_for_assert_stmt(c, n);
2703 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002704 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2706 TYPE(n), NCH(n));
2707 return NULL;
2708 }
2709 }
2710 else {
2711 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2712 | funcdef | classdef
2713 */
2714 node *ch = CHILD(n, 0);
2715 REQ(n, compound_stmt);
2716 switch (TYPE(ch)) {
2717 case if_stmt:
2718 return ast_for_if_stmt(c, ch);
2719 case while_stmt:
2720 return ast_for_while_stmt(c, ch);
2721 case for_stmt:
2722 return ast_for_for_stmt(c, ch);
2723 case try_stmt:
2724 return ast_for_try_stmt(c, ch);
2725 case funcdef:
2726 return ast_for_funcdef(c, ch);
2727 case classdef:
2728 return ast_for_classdef(c, ch);
2729 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002730 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2732 TYPE(n), NCH(n));
2733 return NULL;
2734 }
2735 }
2736}
2737
2738static PyObject *
2739parsenumber(const char *s)
2740{
2741 const char *end;
2742 long x;
2743 double dx;
2744#ifndef WITHOUT_COMPLEX
2745 Py_complex c;
2746 int imflag;
2747#endif
2748
2749 errno = 0;
2750 end = s + strlen(s) - 1;
2751#ifndef WITHOUT_COMPLEX
2752 imflag = *end == 'j' || *end == 'J';
2753#endif
2754 if (*end == 'l' || *end == 'L')
2755 return PyLong_FromString((char *)s, (char **)0, 0);
2756 if (s[0] == '0') {
2757 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2758 if (x < 0 && errno == 0) {
2759 return PyLong_FromString((char *)s,
2760 (char **)0,
2761 0);
2762 }
2763 }
2764 else
2765 x = PyOS_strtol((char *)s, (char **)&end, 0);
2766 if (*end == '\0') {
2767 if (errno != 0)
2768 return PyLong_FromString((char *)s, (char **)0, 0);
2769 return PyInt_FromLong(x);
2770 }
2771 /* XXX Huge floats may silently fail */
2772#ifndef WITHOUT_COMPLEX
2773 if (imflag) {
2774 c.real = 0.;
2775 PyFPE_START_PROTECT("atof", return 0)
2776 c.imag = atof(s);
2777 PyFPE_END_PROTECT(c)
2778 return PyComplex_FromCComplex(c);
2779 }
2780 else
2781#endif
2782 {
2783 PyFPE_START_PROTECT("atof", return 0)
2784 dx = atof(s);
2785 PyFPE_END_PROTECT(dx)
2786 return PyFloat_FromDouble(dx);
2787 }
2788}
2789
2790static PyObject *
2791decode_utf8(const char **sPtr, const char *end, char* encoding)
2792{
2793#ifndef Py_USING_UNICODE
2794 Py_FatalError("decode_utf8 should not be called in this build.");
2795 return NULL;
2796#else
2797 PyObject *u, *v;
2798 char *s, *t;
2799 t = s = (char *)*sPtr;
2800 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2801 while (s < end && (*s & 0x80)) s++;
2802 *sPtr = s;
2803 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2804 if (u == NULL)
2805 return NULL;
2806 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2807 Py_DECREF(u);
2808 return v;
2809#endif
2810}
2811
2812static PyObject *
2813decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2814{
2815 PyObject *v, *u;
2816 char *buf;
2817 char *p;
2818 const char *end;
2819 if (encoding == NULL) {
2820 buf = (char *)s;
2821 u = NULL;
2822 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2823 buf = (char *)s;
2824 u = NULL;
2825 } else {
2826 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2827 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2828 if (u == NULL)
2829 return NULL;
2830 p = buf = PyString_AsString(u);
2831 end = s + len;
2832 while (s < end) {
2833 if (*s == '\\') {
2834 *p++ = *s++;
2835 if (*s & 0x80) {
2836 strcpy(p, "u005c");
2837 p += 5;
2838 }
2839 }
2840 if (*s & 0x80) { /* XXX inefficient */
2841 PyObject *w;
2842 char *r;
2843 int rn, i;
2844 w = decode_utf8(&s, end, "utf-16-be");
2845 if (w == NULL) {
2846 Py_DECREF(u);
2847 return NULL;
2848 }
2849 r = PyString_AsString(w);
2850 rn = PyString_Size(w);
2851 assert(rn % 2 == 0);
2852 for (i = 0; i < rn; i += 2) {
2853 sprintf(p, "\\u%02x%02x",
2854 r[i + 0] & 0xFF,
2855 r[i + 1] & 0xFF);
2856 p += 6;
2857 }
2858 Py_DECREF(w);
2859 } else {
2860 *p++ = *s++;
2861 }
2862 }
2863 len = p - buf;
2864 s = buf;
2865 }
2866 if (rawmode)
2867 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
2868 else
2869 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
2870 Py_XDECREF(u);
2871 return v;
2872}
2873
2874/* s is a Python string literal, including the bracketing quote characters,
2875 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
2876 * parsestr parses it, and returns the decoded Python string object.
2877 */
2878static PyObject *
2879parsestr(const char *s, const char *encoding)
2880{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 size_t len;
2882 int quote = *s;
2883 int rawmode = 0;
2884 int need_encoding;
2885 int unicode = 0;
2886
2887 if (isalpha(quote) || quote == '_') {
2888 if (quote == 'u' || quote == 'U') {
2889 quote = *++s;
2890 unicode = 1;
2891 }
2892 if (quote == 'r' || quote == 'R') {
2893 quote = *++s;
2894 rawmode = 1;
2895 }
2896 }
2897 if (quote != '\'' && quote != '\"') {
2898 PyErr_BadInternalCall();
2899 return NULL;
2900 }
2901 s++;
2902 len = strlen(s);
2903 if (len > INT_MAX) {
2904 PyErr_SetString(PyExc_OverflowError,
2905 "string to parse is too long");
2906 return NULL;
2907 }
2908 if (s[--len] != quote) {
2909 PyErr_BadInternalCall();
2910 return NULL;
2911 }
2912 if (len >= 4 && s[0] == quote && s[1] == quote) {
2913 s += 2;
2914 len -= 2;
2915 if (s[--len] != quote || s[--len] != quote) {
2916 PyErr_BadInternalCall();
2917 return NULL;
2918 }
2919 }
2920#ifdef Py_USING_UNICODE
2921 if (unicode || Py_UnicodeFlag) {
2922 return decode_unicode(s, len, rawmode, encoding);
2923 }
2924#endif
2925 need_encoding = (encoding != NULL &&
2926 strcmp(encoding, "utf-8") != 0 &&
2927 strcmp(encoding, "iso-8859-1") != 0);
2928 if (rawmode || strchr(s, '\\') == NULL) {
2929 if (need_encoding) {
2930#ifndef Py_USING_UNICODE
2931 /* This should not happen - we never see any other
2932 encoding. */
2933 Py_FatalError("cannot deal with encodings in this build.");
2934#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002935 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 if (u == NULL)
2937 return NULL;
2938 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2939 Py_DECREF(u);
2940 return v;
2941#endif
2942 } else {
2943 return PyString_FromStringAndSize(s, len);
2944 }
2945 }
2946
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002947 return PyString_DecodeEscape(s, len, NULL, unicode,
2948 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949}
2950
2951/* Build a Python string object out of a STRING atom. This takes care of
2952 * compile-time literal catenation, calling parsestr() on each piece, and
2953 * pasting the intermediate results together.
2954 */
2955static PyObject *
2956parsestrplus(struct compiling *c, const node *n)
2957{
2958 PyObject *v;
2959 int i;
2960 REQ(CHILD(n, 0), STRING);
2961 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
2962 /* String literal concatenation */
2963 for (i = 1; i < NCH(n); i++) {
2964 PyObject *s;
2965 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
2966 if (s == NULL)
2967 goto onError;
2968 if (PyString_Check(v) && PyString_Check(s)) {
2969 PyString_ConcatAndDel(&v, s);
2970 if (v == NULL)
2971 goto onError;
2972 }
2973#ifdef Py_USING_UNICODE
2974 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002975 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 Py_DECREF(v);
2978 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002979 if (v == NULL)
2980 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981 }
2982#endif
2983 }
2984 }
2985 return v;
2986
2987 onError:
2988 Py_XDECREF(v);
2989 return NULL;
2990}