blob: e56d1651d6fbbf5c9e5e00bd9b2ccfff95726fe8 [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);
581 if (TYPE(ch) == fpdef) {
582 n_args++;
583 }
584 if (TYPE(ch) == EQUAL)
585 n_defaults++;
586 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000587 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588 if (!args && n_args)
589 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000590 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 if (!defaults && n_defaults)
592 goto error;
593
594 /* fpdef: NAME | '(' fplist ')'
595 fplist: fpdef (',' fpdef)* [',']
596 */
597 i = 0;
598 while (i < NCH(n)) {
599 ch = CHILD(n, i);
600 switch (TYPE(ch)) {
601 case fpdef:
602 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
603 anything other than EQUAL or a comma? */
604 /* XXX Should NCH(n) check be made a separate check? */
605 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
606 asdl_seq_APPEND(defaults,
607 ast_for_expr(c, CHILD(n, i + 2)));
608 i += 2;
609 found_default = 1;
610 }
611 else if (found_default) {
612 ast_error(n,
613 "non-default argument follows default argument");
614 goto error;
615 }
616
617 if (NCH(ch) == 3) {
618 asdl_seq_APPEND(args,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000619 compiler_complex_args(c, CHILD(ch, 1)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 }
621 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000622 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
624 ast_error(CHILD(ch, 0), "assignment to None");
625 goto error;
626 }
Armin Rigo31441302005-10-21 12:57:31 +0000627 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000628 Param, LINENO(ch), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629 if (!name)
630 goto error;
631 asdl_seq_APPEND(args, name);
632
633 }
634 i += 2; /* the name and the comma */
635 break;
636 case STAR:
637 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
638 ast_error(CHILD(n, i+1), "assignment to None");
639 goto error;
640 }
641 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
642 i += 3;
643 break;
644 case DOUBLESTAR:
645 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
646 ast_error(CHILD(n, i+1), "assignment to None");
647 goto error;
648 }
649 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
650 i += 3;
651 break;
652 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000653 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 "unexpected node in varargslist: %d @ %d",
655 TYPE(ch), i);
656 goto error;
657 }
658 }
659
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000660 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661
662 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000663 Py_XDECREF(vararg);
664 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 return NULL;
666}
667
668static expr_ty
669ast_for_dotted_name(struct compiling *c, const node *n)
670{
671 expr_ty e = NULL;
672 expr_ty attrib = NULL;
673 identifier id = NULL;
674 int i;
675
676 REQ(n, dotted_name);
677
678 id = NEW_IDENTIFIER(CHILD(n, 0));
679 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000680 return NULL;
681 e = Name(id, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000683 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684 id = NULL;
685
686 for (i = 2; i < NCH(n); i+=2) {
687 id = NEW_IDENTIFIER(CHILD(n, i));
688 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000689 return NULL;
690 attrib = Attribute(e, id, Load, LINENO(CHILD(n, i)), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 if (!attrib)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000692 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 e = attrib;
694 attrib = NULL;
695 }
696
697 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698}
699
700static expr_ty
701ast_for_decorator(struct compiling *c, const node *n)
702{
703 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
704 expr_ty d = NULL;
705 expr_ty name_expr = NULL;
706
707 REQ(n, decorator);
708
709 if ((NCH(n) < 3 && NCH(n) != 5 && NCH(n) != 6)
710 || TYPE(CHILD(n, 0)) != AT || TYPE(RCHILD(n, -1)) != NEWLINE) {
711 ast_error(n, "Invalid decorator node");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000712 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713 }
714
715 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
716 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000717 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718
719 if (NCH(n) == 3) { /* No arguments */
720 d = name_expr;
721 name_expr = NULL;
722 }
723 else if (NCH(n) == 5) { /* Call with no arguments */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000724 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000726 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 name_expr = NULL;
728 }
729 else {
730 d = ast_for_call(c, CHILD(n, 3), name_expr);
731 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000732 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733 name_expr = NULL;
734 }
735
736 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737}
738
739static asdl_seq*
740ast_for_decorators(struct compiling *c, const node *n)
741{
742 asdl_seq* decorator_seq = NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000743 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 int i;
745
746 REQ(n, decorators);
747
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000748 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 if (!decorator_seq)
750 return NULL;
751
752 for (i = 0; i < NCH(n); i++) {
753 d = ast_for_decorator(c, CHILD(n, i));
754 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 asdl_seq_APPEND(decorator_seq, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 }
758 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759}
760
761static stmt_ty
762ast_for_funcdef(struct compiling *c, const node *n)
763{
764 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
765 identifier name = NULL;
766 arguments_ty args = NULL;
767 asdl_seq *body = NULL;
768 asdl_seq *decorator_seq = NULL;
769 int name_i;
770
771 REQ(n, funcdef);
772
773 if (NCH(n) == 6) { /* decorators are present */
774 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
775 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000776 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 name_i = 2;
778 }
779 else {
780 name_i = 1;
781 }
782
783 name = NEW_IDENTIFIER(CHILD(n, name_i));
784 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000785 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000787 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000788 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 }
790 args = ast_for_arguments(c, CHILD(n, name_i + 1));
791 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000792 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 body = ast_for_suite(c, CHILD(n, name_i + 3));
794 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000795 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000797 return FunctionDef(name, args, body, decorator_seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798}
799
800static expr_ty
801ast_for_lambdef(struct compiling *c, const node *n)
802{
803 /* lambdef: 'lambda' [varargslist] ':' test */
804 arguments_ty args;
805 expr_ty expression;
806
807 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000808 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 if (!args)
810 return NULL;
811 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000812 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 }
815 else {
816 args = ast_for_arguments(c, CHILD(n, 1));
817 if (!args)
818 return NULL;
819 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000820 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 }
823
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000824 return Lambda(args, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825}
826
827/* Count the number of 'for' loop in a list comprehension.
828
829 Helper for ast_for_listcomp().
830*/
831
832static int
833count_list_fors(const node *n)
834{
835 int n_fors = 0;
836 node *ch = CHILD(n, 1);
837
838 count_list_for:
839 n_fors++;
840 REQ(ch, list_for);
841 if (NCH(ch) == 5)
842 ch = CHILD(ch, 4);
843 else
844 return n_fors;
845 count_list_iter:
846 REQ(ch, list_iter);
847 ch = CHILD(ch, 0);
848 if (TYPE(ch) == list_for)
849 goto count_list_for;
850 else if (TYPE(ch) == list_if) {
851 if (NCH(ch) == 3) {
852 ch = CHILD(ch, 2);
853 goto count_list_iter;
854 }
855 else
856 return n_fors;
857 }
858 else {
859 /* Should never be reached */
Neal Norwitz7b3d5e12005-11-13 21:17:28 +0000860 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 return -1;
862 }
863}
864
865/* Count the number of 'if' statements in a list comprehension.
866
867 Helper for ast_for_listcomp().
868*/
869
870static int
871count_list_ifs(const node *n)
872{
873 int n_ifs = 0;
874
875 count_list_iter:
876 REQ(n, list_iter);
877 if (TYPE(CHILD(n, 0)) == list_for)
878 return n_ifs;
879 n = CHILD(n, 0);
880 REQ(n, list_if);
881 n_ifs++;
882 if (NCH(n) == 2)
883 return n_ifs;
884 n = CHILD(n, 2);
885 goto count_list_iter;
886}
887
888static expr_ty
889ast_for_listcomp(struct compiling *c, const node *n)
890{
891 /* listmaker: test ( list_for | (',' test)* [','] )
892 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
893 list_iter: list_for | list_if
894 list_if: 'if' test [list_iter]
895 testlist_safe: test [(',' test)+ [',']]
896 */
897 expr_ty elt;
898 asdl_seq *listcomps;
899 int i, n_fors;
900 node *ch;
901
902 REQ(n, listmaker);
903 assert(NCH(n) > 1);
904
905 elt = ast_for_expr(c, CHILD(n, 0));
906 if (!elt)
907 return NULL;
908
909 n_fors = count_list_fors(n);
910 if (n_fors == -1)
911 return NULL;
912
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000913 listcomps = asdl_seq_new(n_fors, c->c_arena);
914 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000916
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917 ch = CHILD(n, 1);
918 for (i = 0; i < n_fors; i++) {
919 comprehension_ty lc;
920 asdl_seq *t;
921 expr_ty expression;
922
923 REQ(ch, list_for);
924
925 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000926 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000928 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000929 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000932 if (asdl_seq_LEN(t) == 1)
933 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
934 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000936 lc = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
937 expression, NULL, c->c_arena);
938 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940
941 if (NCH(ch) == 5) {
942 int j, n_ifs;
943 asdl_seq *ifs;
944
945 ch = CHILD(ch, 4);
946 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000947 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000950 ifs = asdl_seq_new(n_ifs, c->c_arena);
951 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953
954 for (j = 0; j < n_ifs; j++) {
955 REQ(ch, list_iter);
956
957 ch = CHILD(ch, 0);
958 REQ(ch, list_if);
959
960 asdl_seq_APPEND(ifs, ast_for_expr(c, CHILD(ch, 1)));
961 if (NCH(ch) == 3)
962 ch = CHILD(ch, 2);
963 }
964 /* on exit, must guarantee that ch is a list_for */
965 if (TYPE(ch) == list_iter)
966 ch = CHILD(ch, 0);
967 lc->ifs = ifs;
968 }
969 asdl_seq_APPEND(listcomps, lc);
970 }
971
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000972 return ListComp(elt, listcomps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973}
974
975/*
976 Count the number of 'for' loops in a generator expression.
977
978 Helper for ast_for_genexp().
979*/
980
981static int
982count_gen_fors(const node *n)
983{
984 int n_fors = 0;
985 node *ch = CHILD(n, 1);
986
987 count_gen_for:
988 n_fors++;
989 REQ(ch, gen_for);
990 if (NCH(ch) == 5)
991 ch = CHILD(ch, 4);
992 else
993 return n_fors;
994 count_gen_iter:
995 REQ(ch, gen_iter);
996 ch = CHILD(ch, 0);
997 if (TYPE(ch) == gen_for)
998 goto count_gen_for;
999 else if (TYPE(ch) == gen_if) {
1000 if (NCH(ch) == 3) {
1001 ch = CHILD(ch, 2);
1002 goto count_gen_iter;
1003 }
1004 else
1005 return n_fors;
1006 }
1007 else {
1008 /* Should never be reached */
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00001009 PyErr_SetString(PyExc_SystemError,
1010 "logic error in count_gen_fors");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011 return -1;
1012 }
1013}
1014
1015/* Count the number of 'if' statements in a generator expression.
1016
1017 Helper for ast_for_genexp().
1018*/
1019
1020static int
1021count_gen_ifs(const node *n)
1022{
1023 int n_ifs = 0;
1024
1025 while (1) {
1026 REQ(n, gen_iter);
1027 if (TYPE(CHILD(n, 0)) == gen_for)
1028 return n_ifs;
1029 n = CHILD(n, 0);
1030 REQ(n, gen_if);
1031 n_ifs++;
1032 if (NCH(n) == 2)
1033 return n_ifs;
1034 n = CHILD(n, 2);
1035 }
1036}
1037
1038static expr_ty
1039ast_for_genexp(struct compiling *c, const node *n)
1040{
1041 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1042 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1043 expr_ty elt;
1044 asdl_seq *genexps;
1045 int i, n_fors;
1046 node *ch;
1047
1048 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1049 assert(NCH(n) > 1);
1050
1051 elt = ast_for_expr(c, CHILD(n, 0));
1052 if (!elt)
1053 return NULL;
1054
1055 n_fors = count_gen_fors(n);
1056 if (n_fors == -1)
1057 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001058
1059 genexps = asdl_seq_new(n_fors, c->c_arena);
1060 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 ch = CHILD(n, 1);
1064 for (i = 0; i < n_fors; i++) {
1065 comprehension_ty ge;
1066 asdl_seq *t;
1067 expr_ty expression;
1068
1069 REQ(ch, gen_for);
1070
1071 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001072 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001074 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001075 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001077
1078 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001080 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001082 ge = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
1083 expression, NULL, c->c_arena);
1084
1085 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001087
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 if (NCH(ch) == 5) {
1089 int j, n_ifs;
1090 asdl_seq *ifs;
1091
1092 ch = CHILD(ch, 4);
1093 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001094 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001096
1097 ifs = asdl_seq_new(n_ifs, c->c_arena);
1098 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001100
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101 for (j = 0; j < n_ifs; j++) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001102 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 REQ(ch, gen_iter);
1104 ch = CHILD(ch, 0);
1105 REQ(ch, gen_if);
1106
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001107 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001108 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001109 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001110 asdl_seq_APPEND(ifs, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 if (NCH(ch) == 3)
1112 ch = CHILD(ch, 2);
1113 }
1114 /* on exit, must guarantee that ch is a gen_for */
1115 if (TYPE(ch) == gen_iter)
1116 ch = CHILD(ch, 0);
1117 ge->ifs = ifs;
1118 }
1119 asdl_seq_APPEND(genexps, ge);
1120 }
1121
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001122 return GeneratorExp(elt, genexps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123}
1124
1125static expr_ty
1126ast_for_atom(struct compiling *c, const node *n)
1127{
1128 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1129 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1130 */
1131 node *ch = CHILD(n, 0);
1132
1133 switch (TYPE(ch)) {
1134 case NAME:
1135 /* All names start in Load context, but may later be
1136 changed. */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001137 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 case STRING: {
1139 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 if (!str)
1141 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001142
1143 PyArena_AddPyObject(c->c_arena, str);
1144 return Str(str, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 }
1146 case NUMBER: {
1147 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 if (!pynum)
1149 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001150
1151 PyArena_AddPyObject(c->c_arena, pynum);
1152 return Num(pynum, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 }
1154 case LPAR: /* some parenthesized expressions */
1155 ch = CHILD(n, 1);
1156
1157 if (TYPE(ch) == RPAR)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001158 return Tuple(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159
1160 if (TYPE(ch) == yield_expr)
1161 return ast_for_expr(c, ch);
1162
1163 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1164 return ast_for_genexp(c, ch);
1165
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001166 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 case LSQB: /* list (or list comprehension) */
1168 ch = CHILD(n, 1);
1169
1170 if (TYPE(ch) == RSQB)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001171 return List(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172
1173 REQ(ch, listmaker);
1174 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1175 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 if (!elts)
1177 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001178
1179 return List(elts, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 }
1181 else
1182 return ast_for_listcomp(c, ch);
1183 case LBRACE: {
1184 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1185 int i, size;
1186 asdl_seq *keys, *values;
1187
1188 ch = CHILD(n, 1);
1189 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001190 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 if (!keys)
1192 return NULL;
1193
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001194 values = asdl_seq_new(size, c->c_arena);
1195 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197
1198 for (i = 0; i < NCH(ch); i += 4) {
1199 expr_ty expression;
1200
1201 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001202 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001206
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001208 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001210
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 asdl_seq_SET(values, i / 4, expression);
1212 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001213 return Dict(keys, values, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 }
1215 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001216 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 if (!expression)
1218 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001219
1220 return Repr(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 }
1222 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001223 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 return NULL;
1225 }
1226}
1227
1228static slice_ty
1229ast_for_slice(struct compiling *c, const node *n)
1230{
1231 node *ch;
1232 expr_ty lower = NULL, upper = NULL, step = NULL;
1233
1234 REQ(n, subscript);
1235
1236 /*
1237 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1238 sliceop: ':' [test]
1239 */
1240 ch = CHILD(n, 0);
1241 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001242 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243
1244 if (NCH(n) == 1 && TYPE(ch) == test) {
1245 /* 'step' variable hold no significance in terms of being used over
1246 other vars */
1247 step = ast_for_expr(c, ch);
1248 if (!step)
1249 return NULL;
1250
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001251 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 }
1253
1254 if (TYPE(ch) == test) {
1255 lower = ast_for_expr(c, ch);
1256 if (!lower)
1257 return NULL;
1258 }
1259
1260 /* If there's an upper bound it's in the second or third position. */
1261 if (TYPE(ch) == COLON) {
1262 if (NCH(n) > 1) {
1263 node *n2 = CHILD(n, 1);
1264
1265 if (TYPE(n2) == test) {
1266 upper = ast_for_expr(c, n2);
1267 if (!upper)
1268 return NULL;
1269 }
1270 }
1271 } else if (NCH(n) > 2) {
1272 node *n2 = CHILD(n, 2);
1273
1274 if (TYPE(n2) == test) {
1275 upper = ast_for_expr(c, n2);
1276 if (!upper)
1277 return NULL;
1278 }
1279 }
1280
1281 ch = CHILD(n, NCH(n) - 1);
1282 if (TYPE(ch) == sliceop) {
1283 if (NCH(ch) == 1)
1284 /* XXX: If only 1 child, then should just be a colon. Should we
1285 just skip assigning and just get to the return? */
1286 ch = CHILD(ch, 0);
1287 else
1288 ch = CHILD(ch, 1);
1289 if (TYPE(ch) == test) {
1290 step = ast_for_expr(c, ch);
1291 if (!step)
1292 return NULL;
1293 }
1294 }
1295
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001296 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297}
1298
1299static expr_ty
1300ast_for_binop(struct compiling *c, const node *n)
1301{
1302 /* Must account for a sequence of expressions.
1303 How should A op B op C by represented?
1304 BinOp(BinOp(A, op, B), op, C).
1305 */
1306
1307 int i, nops;
1308 expr_ty expr1, expr2, result;
1309 operator_ty operator;
1310
1311 expr1 = ast_for_expr(c, CHILD(n, 0));
1312 if (!expr1)
1313 return NULL;
1314
1315 expr2 = ast_for_expr(c, CHILD(n, 2));
1316 if (!expr2)
1317 return NULL;
1318
1319 operator = get_operator(CHILD(n, 1));
1320 if (!operator)
1321 return NULL;
1322
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001323 result = BinOp(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 if (!result)
1325 return NULL;
1326
1327 nops = (NCH(n) - 1) / 2;
1328 for (i = 1; i < nops; i++) {
1329 expr_ty tmp_result, tmp;
1330 const node* next_oper = CHILD(n, i * 2 + 1);
1331
1332 operator = get_operator(next_oper);
1333 if (!operator)
1334 return NULL;
1335
1336 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1337 if (!tmp)
1338 return NULL;
1339
1340 tmp_result = BinOp(result, operator, tmp,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001341 LINENO(next_oper), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 if (!tmp)
1343 return NULL;
1344 result = tmp_result;
1345 }
1346 return result;
1347}
1348
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001349static expr_ty
1350ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1351{
1352 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1353 expr_ty e;
1354 REQ(n, trailer);
1355 if (TYPE(CHILD(n, 0)) == LPAR) {
1356 if (NCH(n) == 2)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001357 e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001358 else
1359 e = ast_for_call(c, CHILD(n, 1), left_expr);
1360 }
1361 else if (TYPE(CHILD(n, 0)) == LSQB) {
1362 REQ(CHILD(n, 2), RSQB);
1363 n = CHILD(n, 1);
1364 if (NCH(n) <= 2) {
1365 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1366 if (!slc)
1367 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001368 e = Subscript(left_expr, slc, Load, LINENO(n), c->c_arena);
1369 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001370 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001371 }
1372 else {
1373 int j;
1374 slice_ty slc;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001375 asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001376 if (!slices)
1377 return NULL;
1378 for (j = 0; j < NCH(n); j += 2) {
1379 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001380 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001381 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001382 asdl_seq_SET(slices, j / 2, slc);
1383 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001384 e = Subscript(left_expr, ExtSlice(slices, c->c_arena),
1385 Load, LINENO(n), c->c_arena);
1386 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001387 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001388 }
1389 }
1390 else {
1391 assert(TYPE(CHILD(n, 0)) == DOT);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001392 e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n),
1393 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001394 }
1395 return e;
1396}
1397
1398static expr_ty
1399ast_for_power(struct compiling *c, const node *n)
1400{
1401 /* power: atom trailer* ('**' factor)*
1402 */
1403 int i;
1404 expr_ty e, tmp;
1405 REQ(n, power);
1406 e = ast_for_atom(c, CHILD(n, 0));
1407 if (!e)
1408 return NULL;
1409 if (NCH(n) == 1)
1410 return e;
1411 for (i = 1; i < NCH(n); i++) {
1412 node *ch = CHILD(n, i);
1413 if (TYPE(ch) != trailer)
1414 break;
1415 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001416 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001417 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001418 e = tmp;
1419 }
1420 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1421 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001422 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001423 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001424 tmp = BinOp(e, Pow, f, LINENO(n), c->c_arena);
1425 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001426 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001427 e = tmp;
1428 }
1429 return e;
1430}
1431
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432/* Do not name a variable 'expr'! Will cause a compile error.
1433*/
1434
1435static expr_ty
1436ast_for_expr(struct compiling *c, const node *n)
1437{
1438 /* handle the full range of simple expressions
1439 test: and_test ('or' and_test)* | lambdef
1440 and_test: not_test ('and' not_test)*
1441 not_test: 'not' not_test | comparison
1442 comparison: expr (comp_op expr)*
1443 expr: xor_expr ('|' xor_expr)*
1444 xor_expr: and_expr ('^' and_expr)*
1445 and_expr: shift_expr ('&' shift_expr)*
1446 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1447 arith_expr: term (('+'|'-') term)*
1448 term: factor (('*'|'/'|'%'|'//') factor)*
1449 factor: ('+'|'-'|'~') factor | power
1450 power: atom trailer* ('**' factor)*
1451 */
1452
1453 asdl_seq *seq;
1454 int i;
1455
1456 loop:
1457 switch (TYPE(n)) {
1458 case test:
1459 if (TYPE(CHILD(n, 0)) == lambdef)
1460 return ast_for_lambdef(c, CHILD(n, 0));
1461 /* Fall through to and_test */
1462 case and_test:
1463 if (NCH(n) == 1) {
1464 n = CHILD(n, 0);
1465 goto loop;
1466 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001467 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 if (!seq)
1469 return NULL;
1470 for (i = 0; i < NCH(n); i += 2) {
1471 expr_ty e = ast_for_expr(c, CHILD(n, i));
1472 if (!e)
1473 return NULL;
1474 asdl_seq_SET(seq, i / 2, e);
1475 }
1476 if (!strcmp(STR(CHILD(n, 1)), "and"))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001477 return BoolOp(And, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 else {
1479 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001480 return BoolOp(Or, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 }
1482 break;
1483 case not_test:
1484 if (NCH(n) == 1) {
1485 n = CHILD(n, 0);
1486 goto loop;
1487 }
1488 else {
1489 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1490 if (!expression)
1491 return NULL;
1492
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001493 return UnaryOp(Not, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 }
1495 case comparison:
1496 if (NCH(n) == 1) {
1497 n = CHILD(n, 0);
1498 goto loop;
1499 }
1500 else {
1501 expr_ty expression;
1502 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001503 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 if (!ops)
1505 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001506 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 return NULL;
1509 }
1510 for (i = 1; i < NCH(n); i += 2) {
1511 /* XXX cmpop_ty is just an enum */
1512 cmpop_ty operator;
1513
1514 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001515 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518
1519 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001520 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523
1524 asdl_seq_SET(ops, i / 2, (void *)operator);
1525 asdl_seq_SET(cmps, i / 2, expression);
1526 }
1527 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001528 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001530 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001532 return Compare(expression, ops, cmps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 }
1534 break;
1535
1536 /* The next five cases all handle BinOps. The main body of code
1537 is the same in each case, but the switch turned inside out to
1538 reuse the code for each type of operator.
1539 */
1540 case expr:
1541 case xor_expr:
1542 case and_expr:
1543 case shift_expr:
1544 case arith_expr:
1545 case term:
1546 if (NCH(n) == 1) {
1547 n = CHILD(n, 0);
1548 goto loop;
1549 }
1550 return ast_for_binop(c, n);
1551 case yield_expr: {
1552 expr_ty exp = NULL;
1553 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001554 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555 if (!exp)
1556 return NULL;
1557 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001558 return Yield(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 }
1560 case factor: {
1561 expr_ty expression;
1562
1563 if (NCH(n) == 1) {
1564 n = CHILD(n, 0);
1565 goto loop;
1566 }
1567
1568 expression = ast_for_expr(c, CHILD(n, 1));
1569 if (!expression)
1570 return NULL;
1571
1572 switch (TYPE(CHILD(n, 0))) {
1573 case PLUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001574 return UnaryOp(UAdd, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 case MINUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001576 return UnaryOp(USub, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 case TILDE:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001578 return UnaryOp(Invert, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001580 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1581 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582 break;
1583 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001584 case power:
1585 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001587 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 return NULL;
1589 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001590 /* should never get here unless if error is set*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 return NULL;
1592}
1593
1594static expr_ty
1595ast_for_call(struct compiling *c, const node *n, expr_ty func)
1596{
1597 /*
1598 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1599 | '**' test)
1600 argument: [test '='] test [gen_for] # Really [keyword '='] test
1601 */
1602
1603 int i, nargs, nkeywords, ngens;
1604 asdl_seq *args = NULL;
1605 asdl_seq *keywords = NULL;
1606 expr_ty vararg = NULL, kwarg = NULL;
1607
1608 REQ(n, arglist);
1609
1610 nargs = 0;
1611 nkeywords = 0;
1612 ngens = 0;
1613 for (i = 0; i < NCH(n); i++) {
1614 node *ch = CHILD(n, i);
1615 if (TYPE(ch) == argument) {
1616 if (NCH(ch) == 1)
1617 nargs++;
1618 else if (TYPE(CHILD(ch, 1)) == gen_for)
1619 ngens++;
1620 else
1621 nkeywords++;
1622 }
1623 }
1624 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
1625 ast_error(n, "Generator expression must be parenthesised "
1626 "if not sole argument");
1627 return NULL;
1628 }
1629
1630 if (nargs + nkeywords + ngens > 255) {
1631 ast_error(n, "more than 255 arguments");
1632 return NULL;
1633 }
1634
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001635 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001637 return NULL;
1638 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641 nargs = 0;
1642 nkeywords = 0;
1643 for (i = 0; i < NCH(n); i++) {
1644 node *ch = CHILD(n, i);
1645 if (TYPE(ch) == argument) {
1646 expr_ty e;
1647 if (NCH(ch) == 1) {
1648 e = ast_for_expr(c, CHILD(ch, 0));
1649 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001650 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651 asdl_seq_SET(args, nargs++, e);
1652 }
1653 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1654 e = ast_for_genexp(c, ch);
1655 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001656 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 asdl_seq_SET(args, nargs++, e);
1658 }
1659 else {
1660 keyword_ty kw;
1661 identifier key;
1662
1663 /* CHILD(ch, 0) is test, but must be an identifier? */
1664 e = ast_for_expr(c, CHILD(ch, 0));
1665 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001666 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 /* f(lambda x: x[0] = 3) ends up getting parsed with
1668 * LHS test = lambda x: x[0], and RHS test = 3.
1669 * SF bug 132313 points out that complaining about a keyword
1670 * then is very confusing.
1671 */
1672 if (e->kind == Lambda_kind) {
1673 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001674 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 } else if (e->kind != Name_kind) {
1676 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001677 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 }
1679 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 e = ast_for_expr(c, CHILD(ch, 2));
1681 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001682 return NULL;
1683 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001685 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 asdl_seq_SET(keywords, nkeywords++, kw);
1687 }
1688 }
1689 else if (TYPE(ch) == STAR) {
1690 vararg = ast_for_expr(c, CHILD(n, i+1));
1691 i++;
1692 }
1693 else if (TYPE(ch) == DOUBLESTAR) {
1694 kwarg = ast_for_expr(c, CHILD(n, i+1));
1695 i++;
1696 }
1697 }
1698
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001699 return Call(func, args, keywords, vararg, kwarg, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700}
1701
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001703ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001705 /* testlist_gexp: test (',' test)* [','] */
1706 /* testlist: test (',' test)* [','] */
1707 /* testlist_safe: test (',' test)+ [','] */
1708 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001710 if (TYPE(n) == testlist_gexp) {
1711 if (NCH(n) > 1)
1712 assert(TYPE(CHILD(n, 1)) != gen_for);
1713 }
1714 else {
1715 assert(TYPE(n) == testlist ||
1716 TYPE(n) == testlist_safe ||
1717 TYPE(n) == testlist1);
1718 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 if (NCH(n) == 1)
1720 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721 else {
1722 asdl_seq *tmp = seq_for_testlist(c, n);
1723 if (!tmp)
1724 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001725 return Tuple(tmp, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001727}
1728
1729static expr_ty
1730ast_for_testlist_gexp(struct compiling *c, const node* n)
1731{
1732 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1733 /* argument: test [ gen_for ] */
1734 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
1735 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) {
1736 return ast_for_genexp(c, n);
1737 }
1738 else
1739 return ast_for_testlist(c, n);
1740}
1741
1742/* like ast_for_testlist() but returns a sequence */
1743static asdl_seq*
1744ast_for_class_bases(struct compiling *c, const node* n)
1745{
1746 /* testlist: test (',' test)* [','] */
1747 assert(NCH(n) > 0);
1748 REQ(n, testlist);
1749 if (NCH(n) == 1) {
1750 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001751 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001752 if (!bases)
1753 return NULL;
1754 base = ast_for_expr(c, CHILD(n, 0));
1755 if (!base) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001756 return NULL;
1757 }
1758 asdl_seq_SET(bases, 0, base);
1759 return bases;
1760 }
1761 else {
1762 return seq_for_testlist(c, n);
1763 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764}
1765
1766static stmt_ty
1767ast_for_expr_stmt(struct compiling *c, const node *n)
1768{
1769 REQ(n, expr_stmt);
1770 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1771 | ('=' (yield_expr|testlist))*)
1772 testlist: test (',' test)* [',']
1773 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1774 | '<<=' | '>>=' | '**=' | '//='
1775 test: ... here starts the operator precendence dance
1776 */
1777
1778 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001779 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 if (!e)
1781 return NULL;
1782
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001783 return Expr(e, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784 }
1785 else if (TYPE(CHILD(n, 1)) == augassign) {
1786 expr_ty expr1, expr2;
1787 operator_ty operator;
1788 node *ch = CHILD(n, 0);
1789
1790 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001791 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001793 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch),
1794 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795
1796 if (!expr1)
1797 return NULL;
1798 if (expr1->kind == GeneratorExp_kind) {
1799 ast_error(ch, "augmented assignment to generator "
1800 "expression not possible");
1801 return NULL;
1802 }
1803 if (expr1->kind == Name_kind) {
1804 char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1805 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1806 ast_error(ch, "assignment to None");
1807 return NULL;
1808 }
1809 }
1810
1811 ch = CHILD(n, 2);
1812 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001813 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001815 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), c->c_arena);
Neal Norwitze8c05362005-11-14 00:18:03 +00001816 if (!expr2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 return NULL;
Neal Norwitze8c05362005-11-14 00:18:03 +00001818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819
1820 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitze8c05362005-11-14 00:18:03 +00001821 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 return NULL;
Neal Norwitze8c05362005-11-14 00:18:03 +00001823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001825 return AugAssign(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 }
1827 else {
1828 int i;
1829 asdl_seq *targets;
1830 node *value;
1831 expr_ty expression;
1832
1833 /* a normal assignment */
1834 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001835 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 if (!targets)
1837 return NULL;
1838 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001839 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 node *ch = CHILD(n, i);
1841 if (TYPE(ch) == yield_expr) {
1842 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001845 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846
1847 /* set context to assign */
1848 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850
1851 if (!set_context(e, Store, CHILD(n, i))) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 }
1854
1855 asdl_seq_SET(targets, i / 2, e);
1856 }
1857 value = CHILD(n, NCH(n) - 1);
1858 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001859 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 else
1861 expression = ast_for_expr(c, value);
1862 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001863 return NULL;
1864 return Assign(targets, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866}
1867
1868static stmt_ty
1869ast_for_print_stmt(struct compiling *c, const node *n)
1870{
1871 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1872 | '>>' test [ (',' test)+ [','] ] )
1873 */
1874 expr_ty dest = NULL, expression;
1875 asdl_seq *seq;
1876 bool nl;
1877 int i, start = 1;
1878
1879 REQ(n, print_stmt);
1880 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1881 dest = ast_for_expr(c, CHILD(n, 2));
1882 if (!dest)
1883 return NULL;
1884 start = 4;
1885 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 if (!seq)
1888 return NULL;
1889 for (i = start; i < NCH(n); i += 2) {
1890 expression = ast_for_expr(c, CHILD(n, i));
1891 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 return NULL;
1893 }
1894
1895 asdl_seq_APPEND(seq, expression);
1896 }
1897 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001898 return Print(dest, seq, nl, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899}
1900
1901static asdl_seq *
1902ast_for_exprlist(struct compiling *c, const node *n, int context)
1903{
1904 asdl_seq *seq;
1905 int i;
1906 expr_ty e;
1907
1908 REQ(n, exprlist);
1909
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001910 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 if (!seq)
1912 return NULL;
1913 for (i = 0; i < NCH(n); i += 2) {
1914 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001915 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001916 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00001917 asdl_seq_SET(seq, i / 2, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 if (context) {
1919 if (!set_context(e, context, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922 }
1923 return seq;
1924}
1925
1926static stmt_ty
1927ast_for_del_stmt(struct compiling *c, const node *n)
1928{
1929 asdl_seq *expr_list;
1930
1931 /* del_stmt: 'del' exprlist */
1932 REQ(n, del_stmt);
1933
1934 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
1935 if (!expr_list)
1936 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001937 return Delete(expr_list, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938}
1939
1940static stmt_ty
1941ast_for_flow_stmt(struct compiling *c, const node *n)
1942{
1943 /*
1944 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
1945 | yield_stmt
1946 break_stmt: 'break'
1947 continue_stmt: 'continue'
1948 return_stmt: 'return' [testlist]
1949 yield_stmt: yield_expr
1950 yield_expr: 'yield' testlist
1951 raise_stmt: 'raise' [test [',' test [',' test]]]
1952 */
1953 node *ch;
1954
1955 REQ(n, flow_stmt);
1956 ch = CHILD(n, 0);
1957 switch (TYPE(ch)) {
1958 case break_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001959 return Break(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 case continue_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001961 return Continue(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 case yield_stmt: { /* will reduce to yield_expr */
1963 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
1964 if (!exp)
1965 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001966 return Expr(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 }
1968 case return_stmt:
1969 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001970 return Return(NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001972 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 if (!expression)
1974 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001975 return Return(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 }
1977 case raise_stmt:
1978 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001979 return Raise(NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980 else if (NCH(ch) == 2) {
1981 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
1982 if (!expression)
1983 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001984 return Raise(expression, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 }
1986 else if (NCH(ch) == 4) {
1987 expr_ty expr1, expr2;
1988
1989 expr1 = ast_for_expr(c, CHILD(ch, 1));
1990 if (!expr1)
1991 return NULL;
1992 expr2 = ast_for_expr(c, CHILD(ch, 3));
1993 if (!expr2)
1994 return NULL;
1995
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001996 return Raise(expr1, expr2, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 }
1998 else if (NCH(ch) == 6) {
1999 expr_ty expr1, expr2, expr3;
2000
2001 expr1 = ast_for_expr(c, CHILD(ch, 1));
2002 if (!expr1)
2003 return NULL;
2004 expr2 = ast_for_expr(c, CHILD(ch, 3));
2005 if (!expr2)
2006 return NULL;
2007 expr3 = ast_for_expr(c, CHILD(ch, 5));
2008 if (!expr3)
2009 return NULL;
2010
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002011 return Raise(expr1, expr2, expr3, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 }
2013 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002014 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 "unexpected flow_stmt: %d", TYPE(ch));
2016 return NULL;
2017 }
2018}
2019
2020static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002021alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022{
2023 /*
2024 import_as_name: NAME [NAME NAME]
2025 dotted_as_name: dotted_name [NAME NAME]
2026 dotted_name: NAME ('.' NAME)*
2027 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002028 PyObject *str;
2029
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 loop:
2031 switch (TYPE(n)) {
2032 case import_as_name:
2033 if (NCH(n) == 3)
2034 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002035 NEW_IDENTIFIER(CHILD(n, 2)), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 else
2037 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002038 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 break;
2040 case dotted_as_name:
2041 if (NCH(n) == 1) {
2042 n = CHILD(n, 0);
2043 goto loop;
2044 }
2045 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002046 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 assert(!a->asname);
2048 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2049 return a;
2050 }
2051 break;
2052 case dotted_name:
2053 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002054 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 else {
2056 /* Create a string of the form "a.b.c" */
2057 int i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 char *s;
2059
2060 len = 0;
2061 for (i = 0; i < NCH(n); i += 2)
2062 /* length of string plus one for the dot */
2063 len += strlen(STR(CHILD(n, i))) + 1;
2064 len--; /* the last name doesn't have a dot */
2065 str = PyString_FromStringAndSize(NULL, len);
2066 if (!str)
2067 return NULL;
2068 s = PyString_AS_STRING(str);
2069 if (!s)
2070 return NULL;
2071 for (i = 0; i < NCH(n); i += 2) {
2072 char *sch = STR(CHILD(n, i));
2073 strcpy(s, STR(CHILD(n, i)));
2074 s += strlen(sch);
2075 *s++ = '.';
2076 }
2077 --s;
2078 *s = '\0';
2079 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002080 PyArena_AddPyObject(c->c_arena, str);
2081 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 }
2083 break;
2084 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002085 str = PyString_InternFromString("*");
2086 PyArena_AddPyObject(c->c_arena, str);
2087 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002089 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 "unexpected import name: %d", TYPE(n));
2091 return NULL;
2092 }
2093 return NULL;
2094}
2095
2096static stmt_ty
2097ast_for_import_stmt(struct compiling *c, const node *n)
2098{
2099 /*
2100 import_stmt: import_name | import_from
2101 import_name: 'import' dotted_as_names
2102 import_from: 'from' dotted_name 'import' ('*' |
2103 '(' import_as_names ')' |
2104 import_as_names)
2105 */
2106 int i;
2107 asdl_seq *aliases;
2108
2109 REQ(n, import_stmt);
2110 n = CHILD(n, 0);
2111 if (STR(CHILD(n, 0))[0] == 'i') { /* import */
2112 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002113 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002114 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 if (!aliases)
2116 return NULL;
2117 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002118 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 if (!import_alias) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 return NULL;
2121 }
2122 asdl_seq_SET(aliases, i / 2, import_alias);
2123 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002124 return Import(aliases, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 }
2126 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 int n_children;
2128 const char *from_modules;
2129 int lineno = LINENO(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002130 alias_ty mod = alias_for_import_name(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 if (!mod)
2132 return NULL;
2133
2134 /* XXX this needs to be cleaned up */
2135
2136 from_modules = STR(CHILD(n, 3));
2137 if (!from_modules) {
2138 n = CHILD(n, 3); /* from ... import x, y, z */
2139 if (NCH(n) % 2 == 0) {
2140 /* it ends with a comma, not valid but the parser allows it */
2141 ast_error(n, "trailing comma not allowed without"
2142 " surrounding parentheses");
2143 return NULL;
2144 }
2145 }
2146 else if (from_modules[0] == '*') {
2147 n = CHILD(n, 3); /* from ... import * */
2148 }
2149 else if (from_modules[0] == '(')
2150 n = CHILD(n, 4); /* from ... import (x, y, z) */
Neal Norwitze76adcd2005-11-15 05:04:31 +00002151 else {
2152 /* XXX: don't we need to call ast_error(n, "..."); */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002154 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155
2156 n_children = NCH(n);
2157 if (from_modules && from_modules[0] == '*')
2158 n_children = 1;
2159
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002160 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 if (!aliases) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 return NULL;
2163 }
2164
2165 /* handle "from ... import *" special b/c there's no children */
2166 if (from_modules && from_modules[0] == '*') {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002167 alias_ty import_alias = alias_for_import_name(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 if (!import_alias) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 return NULL;
2170 }
2171 asdl_seq_APPEND(aliases, import_alias);
2172 }
2173
2174 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002175 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 if (!import_alias) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 return NULL;
2178 }
2179 asdl_seq_APPEND(aliases, import_alias);
2180 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002181 return ImportFrom(mod->name, aliases, lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 }
Neal Norwitz79792652005-11-14 04:25:03 +00002183 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 "unknown import statement: starts with command '%s'",
2185 STR(CHILD(n, 0)));
2186 return NULL;
2187}
2188
2189static stmt_ty
2190ast_for_global_stmt(struct compiling *c, const node *n)
2191{
2192 /* global_stmt: 'global' NAME (',' NAME)* */
2193 identifier name;
2194 asdl_seq *s;
2195 int i;
2196
2197 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002198 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 if (!s)
2200 return NULL;
2201 for (i = 1; i < NCH(n); i += 2) {
2202 name = NEW_IDENTIFIER(CHILD(n, i));
2203 if (!name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 return NULL;
2205 }
2206 asdl_seq_SET(s, i / 2, name);
2207 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002208 return Global(s, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209}
2210
2211static stmt_ty
2212ast_for_exec_stmt(struct compiling *c, const node *n)
2213{
2214 expr_ty expr1, globals = NULL, locals = NULL;
2215 int n_children = NCH(n);
2216 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002217 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 "poorly formed 'exec' statement: %d parts to statement",
2219 n_children);
2220 return NULL;
2221 }
2222
2223 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2224 REQ(n, exec_stmt);
2225 expr1 = ast_for_expr(c, CHILD(n, 1));
2226 if (!expr1)
2227 return NULL;
2228 if (n_children >= 4) {
2229 globals = ast_for_expr(c, CHILD(n, 3));
2230 if (!globals)
2231 return NULL;
2232 }
2233 if (n_children == 6) {
2234 locals = ast_for_expr(c, CHILD(n, 5));
2235 if (!locals)
2236 return NULL;
2237 }
2238
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002239 return Exec(expr1, globals, locals, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240}
2241
2242static stmt_ty
2243ast_for_assert_stmt(struct compiling *c, const node *n)
2244{
2245 /* assert_stmt: 'assert' test [',' test] */
2246 REQ(n, assert_stmt);
2247 if (NCH(n) == 2) {
2248 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2249 if (!expression)
2250 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002251 return Assert(expression, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 }
2253 else if (NCH(n) == 4) {
2254 expr_ty expr1, expr2;
2255
2256 expr1 = ast_for_expr(c, CHILD(n, 1));
2257 if (!expr1)
2258 return NULL;
2259 expr2 = ast_for_expr(c, CHILD(n, 3));
2260 if (!expr2)
2261 return NULL;
2262
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002263 return Assert(expr1, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 }
Neal Norwitz79792652005-11-14 04:25:03 +00002265 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 "improper number of parts to 'assert' statement: %d",
2267 NCH(n));
2268 return NULL;
2269}
2270
2271static asdl_seq *
2272ast_for_suite(struct compiling *c, const node *n)
2273{
2274 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2275 asdl_seq *seq = NULL;
2276 stmt_ty s;
2277 int i, total, num, end, pos = 0;
2278 node *ch;
2279
2280 REQ(n, suite);
2281
2282 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002283 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 if (!seq)
2285 return NULL;
2286 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2287 n = CHILD(n, 0);
2288 /* simple_stmt always ends with a NEWLINE,
2289 and may have a trailing SEMI
2290 */
2291 end = NCH(n) - 1;
2292 if (TYPE(CHILD(n, end - 1)) == SEMI)
2293 end--;
2294 /* loop by 2 to skip semi-colons */
2295 for (i = 0; i < end; i += 2) {
2296 ch = CHILD(n, i);
2297 s = ast_for_stmt(c, ch);
2298 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002299 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 asdl_seq_SET(seq, pos++, s);
2301 }
2302 }
2303 else {
2304 for (i = 2; i < (NCH(n) - 1); i++) {
2305 ch = CHILD(n, i);
2306 REQ(ch, stmt);
2307 num = num_stmts(ch);
2308 if (num == 1) {
2309 /* small_stmt or compound_stmt with only one child */
2310 s = ast_for_stmt(c, ch);
2311 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002312 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 asdl_seq_SET(seq, pos++, s);
2314 }
2315 else {
2316 int j;
2317 ch = CHILD(ch, 0);
2318 REQ(ch, simple_stmt);
2319 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002320 /* statement terminates with a semi-colon ';' */
2321 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002322 assert((j + 1) == NCH(ch));
2323 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002324 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 s = ast_for_stmt(c, CHILD(ch, j));
2326 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002327 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 asdl_seq_SET(seq, pos++, s);
2329 }
2330 }
2331 }
2332 }
2333 assert(pos == seq->size);
2334 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335}
2336
2337static stmt_ty
2338ast_for_if_stmt(struct compiling *c, const node *n)
2339{
2340 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2341 ['else' ':' suite]
2342 */
2343 char *s;
2344
2345 REQ(n, if_stmt);
2346
2347 if (NCH(n) == 4) {
2348 expr_ty expression;
2349 asdl_seq *suite_seq;
2350
2351 expression = ast_for_expr(c, CHILD(n, 1));
2352 if (!expression)
2353 return NULL;
2354 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002355 if (!suite_seq) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002358
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002359 return If(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360 }
2361 s = STR(CHILD(n, 4));
2362 /* s[2], the third character in the string, will be
2363 's' for el_s_e, or
2364 'i' for el_i_f
2365 */
2366 if (s[2] == 's') {
2367 expr_ty expression;
2368 asdl_seq *seq1, *seq2;
2369
2370 expression = ast_for_expr(c, CHILD(n, 1));
2371 if (!expression)
2372 return NULL;
2373 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002374 if (!seq1) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002378 if (!seq2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002382 return If(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 }
2384 else if (s[2] == 'i') {
2385 int i, n_elif, has_else = 0;
2386 asdl_seq *orelse = NULL;
2387 n_elif = NCH(n) - 4;
2388 /* must reference the child n_elif+1 since 'else' token is third,
2389 not fourth, child from the end. */
2390 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2391 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2392 has_else = 1;
2393 n_elif -= 3;
2394 }
2395 n_elif /= 4;
2396
2397 if (has_else) {
2398 expr_ty expression;
2399 asdl_seq *seq1, *seq2;
2400
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002401 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 if (!orelse)
2403 return NULL;
2404 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2405 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 return NULL;
2407 }
2408 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2409 if (!seq1) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410 return NULL;
2411 }
2412 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2413 if (!seq2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414 return NULL;
2415 }
2416
2417 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002418 LINENO(CHILD(n, NCH(n) - 6)),
2419 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 /* the just-created orelse handled the last elif */
2421 n_elif--;
2422 }
2423 else
2424 orelse = NULL;
2425
2426 for (i = 0; i < n_elif; i++) {
2427 int off = 5 + (n_elif - i - 1) * 4;
2428 expr_ty expression;
2429 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002430 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002431 if (!new) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002433 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 expression = ast_for_expr(c, CHILD(n, off));
2435 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 return NULL;
2437 }
2438 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2439 if (!suite_seq) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 return NULL;
2441 }
2442
2443 asdl_seq_SET(new, 0,
2444 If(expression, suite_seq, orelse,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002445 LINENO(CHILD(n, off)), c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 orelse = new;
2447 }
2448 return If(ast_for_expr(c, CHILD(n, 1)),
2449 ast_for_suite(c, CHILD(n, 3)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002450 orelse, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 }
2452 else {
Neal Norwitz79792652005-11-14 04:25:03 +00002453 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 "unexpected token in 'if' statement: %s", s);
2455 return NULL;
2456 }
2457}
2458
2459static stmt_ty
2460ast_for_while_stmt(struct compiling *c, const node *n)
2461{
2462 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2463 REQ(n, while_stmt);
2464
2465 if (NCH(n) == 4) {
2466 expr_ty expression;
2467 asdl_seq *suite_seq;
2468
2469 expression = ast_for_expr(c, CHILD(n, 1));
2470 if (!expression)
2471 return NULL;
2472 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002473 if (!suite_seq) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002475 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002476 return While(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 }
2478 else if (NCH(n) == 7) {
2479 expr_ty expression;
2480 asdl_seq *seq1, *seq2;
2481
2482 expression = ast_for_expr(c, CHILD(n, 1));
2483 if (!expression)
2484 return NULL;
2485 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002486 if (!seq1) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002490 if (!seq2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002494 return While(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 }
2496 else {
Neal Norwitz79792652005-11-14 04:25:03 +00002497 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 "wrong number of tokens for 'while' statement: %d",
2499 NCH(n));
2500 return NULL;
2501 }
2502}
2503
2504static stmt_ty
2505ast_for_for_stmt(struct compiling *c, const node *n)
2506{
2507 asdl_seq *_target = NULL, *seq = NULL, *suite_seq = NULL;
2508 expr_ty expression;
2509 expr_ty target;
2510 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2511 REQ(n, for_stmt);
2512
2513 if (NCH(n) == 9) {
2514 seq = ast_for_suite(c, CHILD(n, 8));
2515 if (!seq)
2516 return NULL;
2517 }
2518
2519 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002520 if (!_target) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 if (asdl_seq_LEN(_target) == 1) {
2524 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 }
2526 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002527 target = Tuple(_target, Store, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002529 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002530 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002532 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002534 if (!suite_seq) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002536 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002538 return For(target, expression, suite_seq, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539}
2540
2541static excepthandler_ty
2542ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2543{
2544 /* except_clause: 'except' [test [',' test]] */
2545 REQ(exc, except_clause);
2546 REQ(body, suite);
2547
2548 if (NCH(exc) == 1) {
2549 asdl_seq *suite_seq = ast_for_suite(c, body);
2550 if (!suite_seq)
2551 return NULL;
2552
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002553 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 }
2555 else if (NCH(exc) == 2) {
2556 expr_ty expression;
2557 asdl_seq *suite_seq;
2558
2559 expression = ast_for_expr(c, CHILD(exc, 1));
2560 if (!expression)
2561 return NULL;
2562 suite_seq = ast_for_suite(c, body);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002563 if (!suite_seq) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002565 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002567 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 }
2569 else if (NCH(exc) == 4) {
2570 asdl_seq *suite_seq;
2571 expr_ty expression;
2572 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2573 if (!e)
2574 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002575 if (!set_context(e, Store, CHILD(exc, 3))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002577 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002579 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002581 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 suite_seq = ast_for_suite(c, body);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002583 if (!suite_seq) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002585 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002587 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 }
2589 else {
Neal Norwitz79792652005-11-14 04:25:03 +00002590 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 "wrong number of children for 'except' clause: %d",
2592 NCH(exc));
2593 return NULL;
2594 }
2595}
2596
2597static stmt_ty
2598ast_for_try_stmt(struct compiling *c, const node *n)
2599{
Neal Norwitzf599f422005-12-17 21:33:47 +00002600 const int nch = NCH(n);
2601 int n_except = (nch - 3)/3;
2602 asdl_seq *body, *orelse = NULL, *finally = NULL;
2603
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 REQ(n, try_stmt);
2605
Neal Norwitzf599f422005-12-17 21:33:47 +00002606 body = ast_for_suite(c, CHILD(n, 2));
2607 if (body == NULL)
2608 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609
Neal Norwitzf599f422005-12-17 21:33:47 +00002610 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2611 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2612 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2613 /* we can assume it's an "else",
2614 because nch >= 9 for try-else-finally and
2615 it would otherwise have a type of except_clause */
2616 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2617 if (orelse == NULL)
2618 return NULL;
2619 n_except--;
2620 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621
Neal Norwitzf599f422005-12-17 21:33:47 +00002622 finally = ast_for_suite(c, CHILD(n, nch - 1));
2623 if (finally == NULL)
2624 return NULL;
2625 n_except--;
2626 }
2627 else {
2628 /* we can assume it's an "else",
2629 otherwise it would have a type of except_clause */
2630 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2631 if (orelse == NULL)
2632 return NULL;
2633 n_except--;
2634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002636 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002637 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 return NULL;
2639 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002640
2641 if (n_except > 0) {
2642 int i;
2643 stmt_ty except_st;
2644 /* process except statements to create a try ... except */
2645 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2646 if (handlers == NULL)
2647 return NULL;
2648
2649 for (i = 0; i < n_except; i++) {
2650 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2651 CHILD(n, 5 + i * 3));
2652 if (!e)
2653 return NULL;
2654 asdl_seq_SET(handlers, i, e);
2655 }
2656
2657 except_st = TryExcept(body, handlers, orelse, LINENO(n), c->c_arena);
2658 if (!finally)
2659 return except_st;
2660
2661 /* if a 'finally' is present too, we nest the TryExcept within a
2662 TryFinally to emulate try ... except ... finally */
2663 body = asdl_seq_new(1, c->c_arena);
2664 if (body == NULL)
2665 return NULL;
2666 asdl_seq_SET(body, 0, except_st);
2667 }
2668
2669 /* must be a try ... finally (except clauses are in body, if any exist) */
2670 assert(finally != NULL);
2671 return TryFinally(body, finally, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672}
2673
2674static stmt_ty
2675ast_for_classdef(struct compiling *c, const node *n)
2676{
2677 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678 asdl_seq *bases, *s;
2679
2680 REQ(n, classdef);
2681
2682 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2683 ast_error(n, "assignment to None");
2684 return NULL;
2685 }
2686
2687 if (NCH(n) == 4) {
2688 s = ast_for_suite(c, CHILD(n, 3));
2689 if (!s)
2690 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002691 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2692 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 }
2694 /* check for empty base list */
2695 if (TYPE(CHILD(n,3)) == RPAR) {
2696 s = ast_for_suite(c, CHILD(n,5));
2697 if (!s)
2698 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002699 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2700 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 }
2702
2703 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002704 bases = ast_for_class_bases(c, CHILD(n, 3));
2705 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707
2708 s = ast_for_suite(c, CHILD(n, 6));
2709 if (!s) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 return NULL;
2711 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002712 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2713 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714}
2715
2716static stmt_ty
2717ast_for_stmt(struct compiling *c, const node *n)
2718{
2719 if (TYPE(n) == stmt) {
2720 assert(NCH(n) == 1);
2721 n = CHILD(n, 0);
2722 }
2723 if (TYPE(n) == simple_stmt) {
2724 assert(num_stmts(n) == 1);
2725 n = CHILD(n, 0);
2726 }
2727 if (TYPE(n) == small_stmt) {
2728 REQ(n, small_stmt);
2729 n = CHILD(n, 0);
2730 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2731 | flow_stmt | import_stmt | global_stmt | exec_stmt
2732 | assert_stmt
2733 */
2734 switch (TYPE(n)) {
2735 case expr_stmt:
2736 return ast_for_expr_stmt(c, n);
2737 case print_stmt:
2738 return ast_for_print_stmt(c, n);
2739 case del_stmt:
2740 return ast_for_del_stmt(c, n);
2741 case pass_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002742 return Pass(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 case flow_stmt:
2744 return ast_for_flow_stmt(c, n);
2745 case import_stmt:
2746 return ast_for_import_stmt(c, n);
2747 case global_stmt:
2748 return ast_for_global_stmt(c, n);
2749 case exec_stmt:
2750 return ast_for_exec_stmt(c, n);
2751 case assert_stmt:
2752 return ast_for_assert_stmt(c, n);
2753 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002754 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2756 TYPE(n), NCH(n));
2757 return NULL;
2758 }
2759 }
2760 else {
2761 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2762 | funcdef | classdef
2763 */
2764 node *ch = CHILD(n, 0);
2765 REQ(n, compound_stmt);
2766 switch (TYPE(ch)) {
2767 case if_stmt:
2768 return ast_for_if_stmt(c, ch);
2769 case while_stmt:
2770 return ast_for_while_stmt(c, ch);
2771 case for_stmt:
2772 return ast_for_for_stmt(c, ch);
2773 case try_stmt:
2774 return ast_for_try_stmt(c, ch);
2775 case funcdef:
2776 return ast_for_funcdef(c, ch);
2777 case classdef:
2778 return ast_for_classdef(c, ch);
2779 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002780 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2782 TYPE(n), NCH(n));
2783 return NULL;
2784 }
2785 }
2786}
2787
2788static PyObject *
2789parsenumber(const char *s)
2790{
2791 const char *end;
2792 long x;
2793 double dx;
2794#ifndef WITHOUT_COMPLEX
2795 Py_complex c;
2796 int imflag;
2797#endif
2798
2799 errno = 0;
2800 end = s + strlen(s) - 1;
2801#ifndef WITHOUT_COMPLEX
2802 imflag = *end == 'j' || *end == 'J';
2803#endif
2804 if (*end == 'l' || *end == 'L')
2805 return PyLong_FromString((char *)s, (char **)0, 0);
2806 if (s[0] == '0') {
2807 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2808 if (x < 0 && errno == 0) {
2809 return PyLong_FromString((char *)s,
2810 (char **)0,
2811 0);
2812 }
2813 }
2814 else
2815 x = PyOS_strtol((char *)s, (char **)&end, 0);
2816 if (*end == '\0') {
2817 if (errno != 0)
2818 return PyLong_FromString((char *)s, (char **)0, 0);
2819 return PyInt_FromLong(x);
2820 }
2821 /* XXX Huge floats may silently fail */
2822#ifndef WITHOUT_COMPLEX
2823 if (imflag) {
2824 c.real = 0.;
2825 PyFPE_START_PROTECT("atof", return 0)
2826 c.imag = atof(s);
2827 PyFPE_END_PROTECT(c)
2828 return PyComplex_FromCComplex(c);
2829 }
2830 else
2831#endif
2832 {
2833 PyFPE_START_PROTECT("atof", return 0)
2834 dx = atof(s);
2835 PyFPE_END_PROTECT(dx)
2836 return PyFloat_FromDouble(dx);
2837 }
2838}
2839
2840static PyObject *
2841decode_utf8(const char **sPtr, const char *end, char* encoding)
2842{
2843#ifndef Py_USING_UNICODE
2844 Py_FatalError("decode_utf8 should not be called in this build.");
2845 return NULL;
2846#else
2847 PyObject *u, *v;
2848 char *s, *t;
2849 t = s = (char *)*sPtr;
2850 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2851 while (s < end && (*s & 0x80)) s++;
2852 *sPtr = s;
2853 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2854 if (u == NULL)
2855 return NULL;
2856 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2857 Py_DECREF(u);
2858 return v;
2859#endif
2860}
2861
2862static PyObject *
2863decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2864{
2865 PyObject *v, *u;
2866 char *buf;
2867 char *p;
2868 const char *end;
2869 if (encoding == NULL) {
2870 buf = (char *)s;
2871 u = NULL;
2872 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2873 buf = (char *)s;
2874 u = NULL;
2875 } else {
2876 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2877 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2878 if (u == NULL)
2879 return NULL;
2880 p = buf = PyString_AsString(u);
2881 end = s + len;
2882 while (s < end) {
2883 if (*s == '\\') {
2884 *p++ = *s++;
2885 if (*s & 0x80) {
2886 strcpy(p, "u005c");
2887 p += 5;
2888 }
2889 }
2890 if (*s & 0x80) { /* XXX inefficient */
2891 PyObject *w;
2892 char *r;
2893 int rn, i;
2894 w = decode_utf8(&s, end, "utf-16-be");
2895 if (w == NULL) {
2896 Py_DECREF(u);
2897 return NULL;
2898 }
2899 r = PyString_AsString(w);
2900 rn = PyString_Size(w);
2901 assert(rn % 2 == 0);
2902 for (i = 0; i < rn; i += 2) {
2903 sprintf(p, "\\u%02x%02x",
2904 r[i + 0] & 0xFF,
2905 r[i + 1] & 0xFF);
2906 p += 6;
2907 }
2908 Py_DECREF(w);
2909 } else {
2910 *p++ = *s++;
2911 }
2912 }
2913 len = p - buf;
2914 s = buf;
2915 }
2916 if (rawmode)
2917 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
2918 else
2919 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
2920 Py_XDECREF(u);
2921 return v;
2922}
2923
2924/* s is a Python string literal, including the bracketing quote characters,
2925 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
2926 * parsestr parses it, and returns the decoded Python string object.
2927 */
2928static PyObject *
2929parsestr(const char *s, const char *encoding)
2930{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931 size_t len;
2932 int quote = *s;
2933 int rawmode = 0;
2934 int need_encoding;
2935 int unicode = 0;
2936
2937 if (isalpha(quote) || quote == '_') {
2938 if (quote == 'u' || quote == 'U') {
2939 quote = *++s;
2940 unicode = 1;
2941 }
2942 if (quote == 'r' || quote == 'R') {
2943 quote = *++s;
2944 rawmode = 1;
2945 }
2946 }
2947 if (quote != '\'' && quote != '\"') {
2948 PyErr_BadInternalCall();
2949 return NULL;
2950 }
2951 s++;
2952 len = strlen(s);
2953 if (len > INT_MAX) {
2954 PyErr_SetString(PyExc_OverflowError,
2955 "string to parse is too long");
2956 return NULL;
2957 }
2958 if (s[--len] != quote) {
2959 PyErr_BadInternalCall();
2960 return NULL;
2961 }
2962 if (len >= 4 && s[0] == quote && s[1] == quote) {
2963 s += 2;
2964 len -= 2;
2965 if (s[--len] != quote || s[--len] != quote) {
2966 PyErr_BadInternalCall();
2967 return NULL;
2968 }
2969 }
2970#ifdef Py_USING_UNICODE
2971 if (unicode || Py_UnicodeFlag) {
2972 return decode_unicode(s, len, rawmode, encoding);
2973 }
2974#endif
2975 need_encoding = (encoding != NULL &&
2976 strcmp(encoding, "utf-8") != 0 &&
2977 strcmp(encoding, "iso-8859-1") != 0);
2978 if (rawmode || strchr(s, '\\') == NULL) {
2979 if (need_encoding) {
2980#ifndef Py_USING_UNICODE
2981 /* This should not happen - we never see any other
2982 encoding. */
2983 Py_FatalError("cannot deal with encodings in this build.");
2984#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002985 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 if (u == NULL)
2987 return NULL;
2988 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2989 Py_DECREF(u);
2990 return v;
2991#endif
2992 } else {
2993 return PyString_FromStringAndSize(s, len);
2994 }
2995 }
2996
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002997 return PyString_DecodeEscape(s, len, NULL, unicode,
2998 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999}
3000
3001/* Build a Python string object out of a STRING atom. This takes care of
3002 * compile-time literal catenation, calling parsestr() on each piece, and
3003 * pasting the intermediate results together.
3004 */
3005static PyObject *
3006parsestrplus(struct compiling *c, const node *n)
3007{
3008 PyObject *v;
3009 int i;
3010 REQ(CHILD(n, 0), STRING);
3011 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3012 /* String literal concatenation */
3013 for (i = 1; i < NCH(n); i++) {
3014 PyObject *s;
3015 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3016 if (s == NULL)
3017 goto onError;
3018 if (PyString_Check(v) && PyString_Check(s)) {
3019 PyString_ConcatAndDel(&v, s);
3020 if (v == NULL)
3021 goto onError;
3022 }
3023#ifdef Py_USING_UNICODE
3024 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003025 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 Py_DECREF(v);
3028 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003029 if (v == NULL)
3030 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 }
3032#endif
3033 }
3034 }
3035 return v;
3036
3037 onError:
3038 Py_XDECREF(v);
3039 return NULL;
3040}