| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* | 
 | 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" | 
 | 10 | #include "ast.h" | 
 | 11 | #include "token.h" | 
 | 12 | #include "parsetok.h" | 
 | 13 | #include "graminit.h" | 
 | 14 |  | 
 | 15 | #include <assert.h> | 
 | 16 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 17 | /* XXX TO DO | 
 | 18 |    - re-indent this file (should be done) | 
 | 19 |    - internal error checking (freeing memory, etc.) | 
 | 20 |    - syntax errors | 
 | 21 | */ | 
 | 22 |  | 
 | 23 |  | 
 | 24 | /* Data structure used internally */ | 
 | 25 | struct compiling { | 
 | 26 | 	char *c_encoding; /* source encoding */ | 
 | 27 | }; | 
 | 28 |  | 
 | 29 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); | 
 | 30 | static expr_ty ast_for_expr(struct compiling *, const node *); | 
 | 31 | static stmt_ty ast_for_stmt(struct compiling *, const node *); | 
 | 32 | static asdl_seq *ast_for_suite(struct compiling *, const node *); | 
 | 33 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int); | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 34 | static expr_ty ast_for_testlist(struct compiling *, const node *); | 
 | 35 | static expr_ty ast_for_testlist_gexp(struct compiling *, const node *); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 36 |  | 
 | 37 | /* Note different signature for ast_for_call */ | 
 | 38 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); | 
 | 39 |  | 
 | 40 | static PyObject *parsenumber(const char *); | 
 | 41 | static PyObject *parsestr(const char *s, const char *encoding); | 
 | 42 | static PyObject *parsestrplus(struct compiling *, const node *n); | 
 | 43 |  | 
 | 44 | extern grammar _PyParser_Grammar; /* From graminit.c */ | 
 | 45 |  | 
 | 46 | #ifndef LINENO | 
 | 47 | #define LINENO(n)	((n)->n_lineno) | 
 | 48 | #endif | 
 | 49 |  | 
 | 50 | #define NEW_IDENTIFIER(n) PyString_InternFromString(STR(n)) | 
 | 51 |  | 
 | 52 | static void | 
 | 53 | asdl_stmt_seq_free(asdl_seq* seq) | 
 | 54 | { | 
 | 55 |     int n, i; | 
 | 56 |  | 
 | 57 |     if (!seq) | 
 | 58 | 	return; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 59 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 60 |     n = asdl_seq_LEN(seq); | 
 | 61 |     for (i = 0; i < n; i++) | 
 | 62 | 	free_stmt(asdl_seq_GET(seq, i)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 63 |     asdl_seq_free(seq); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 64 | } | 
 | 65 |  | 
 | 66 | static void | 
 | 67 | asdl_expr_seq_free(asdl_seq* seq) | 
 | 68 | { | 
 | 69 |     int n, i; | 
 | 70 |  | 
 | 71 |     if (!seq) | 
 | 72 | 	return; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 73 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 74 |     n = asdl_seq_LEN(seq); | 
 | 75 |     for (i = 0; i < n; i++) | 
 | 76 | 	free_expr(asdl_seq_GET(seq, i)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 77 |     asdl_seq_free(seq); /* ok */ | 
 | 78 | } | 
 | 79 |  | 
 | 80 | static void | 
 | 81 | asdl_alias_seq_free(asdl_seq* seq) | 
 | 82 | { | 
 | 83 |     int n, i; | 
 | 84 |  | 
 | 85 |     if (!seq) | 
 | 86 | 	return; | 
 | 87 |  | 
 | 88 |     n = asdl_seq_LEN(seq); | 
 | 89 |     for (i = 0; i < n; i++) | 
 | 90 | 	free_alias(asdl_seq_GET(seq, i)); | 
 | 91 |     asdl_seq_free(seq); /* ok */ | 
 | 92 | } | 
 | 93 |  | 
 | 94 | static void | 
 | 95 | asdl_comprehension_seq_free(asdl_seq* seq) | 
 | 96 | { | 
 | 97 |     int n, i; | 
 | 98 |  | 
 | 99 |     if (!seq) | 
 | 100 | 	return; | 
 | 101 |  | 
 | 102 |     n = asdl_seq_LEN(seq); | 
 | 103 |     for (i = 0; i < n; i++) | 
 | 104 | 	free_comprehension(asdl_seq_GET(seq, i)); | 
 | 105 |     asdl_seq_free(seq); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 106 | } | 
 | 107 |  | 
 | 108 | /* This routine provides an invalid object for the syntax error. | 
 | 109 |    The outermost routine must unpack this error and create the | 
 | 110 |    proper object.  We do this so that we don't have to pass | 
 | 111 |    the filename to everything function. | 
 | 112 |  | 
 | 113 |    XXX Maybe we should just pass the filename... | 
 | 114 | */ | 
 | 115 |  | 
 | 116 | static int | 
 | 117 | ast_error(const node *n, const char *errstr) | 
 | 118 | { | 
 | 119 |     PyObject *u = Py_BuildValue("zi", errstr, LINENO(n)); | 
 | 120 |     if (!u) | 
 | 121 | 	return 0; | 
 | 122 |     PyErr_SetObject(PyExc_SyntaxError, u); | 
 | 123 |     Py_DECREF(u); | 
 | 124 |     return 0; | 
 | 125 | } | 
 | 126 |  | 
 | 127 | static void | 
 | 128 | ast_error_finish(const char *filename) | 
 | 129 | { | 
 | 130 |     PyObject *type, *value, *tback, *errstr, *loc, *tmp; | 
 | 131 |     int lineno; | 
 | 132 |  | 
 | 133 |     assert(PyErr_Occurred()); | 
 | 134 |     if (!PyErr_ExceptionMatches(PyExc_SyntaxError)) | 
 | 135 | 	return; | 
 | 136 |  | 
 | 137 |     PyErr_Fetch(&type, &value, &tback); | 
 | 138 |     errstr = PyTuple_GetItem(value, 0); | 
 | 139 |     if (!errstr) | 
 | 140 | 	return; | 
 | 141 |     Py_INCREF(errstr); | 
 | 142 |     lineno = PyInt_AsLong(PyTuple_GetItem(value, 1)); | 
 | 143 |     if (lineno == -1) | 
 | 144 | 	return; | 
 | 145 |     Py_DECREF(value); | 
 | 146 |  | 
 | 147 |     loc = PyErr_ProgramText(filename, lineno); | 
 | 148 |     if (!loc) { | 
 | 149 | 	Py_INCREF(Py_None); | 
 | 150 | 	loc = Py_None; | 
 | 151 |     } | 
 | 152 |     tmp = Py_BuildValue("(ziOO)", filename, lineno, Py_None, loc); | 
 | 153 |     Py_DECREF(loc); | 
 | 154 |     if (!tmp) | 
 | 155 | 	return; | 
 | 156 |     value = Py_BuildValue("(OO)", errstr, tmp); | 
 | 157 |     Py_DECREF(errstr); | 
 | 158 |     Py_DECREF(tmp); | 
 | 159 |     if (!value) | 
 | 160 | 	return; | 
 | 161 |     PyErr_Restore(type, value, tback); | 
 | 162 | } | 
 | 163 |  | 
 | 164 | /* num_stmts() returns number of contained statements. | 
 | 165 |  | 
 | 166 |    Use this routine to determine how big a sequence is needed for | 
 | 167 |    the statements in a parse tree.  Its raison d'etre is this bit of | 
 | 168 |    grammar: | 
 | 169 |  | 
 | 170 |    stmt: simple_stmt | compound_stmt | 
 | 171 |    simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | 
 | 172 |  | 
 | 173 |    A simple_stmt can contain multiple small_stmt elements joined | 
 | 174 |    by semicolons.  If the arg is a simple_stmt, the number of | 
 | 175 |    small_stmt elements is returned. | 
 | 176 | */ | 
 | 177 |  | 
 | 178 | static int | 
 | 179 | num_stmts(const node *n) | 
 | 180 | { | 
 | 181 |     int i, l; | 
 | 182 |     node *ch; | 
 | 183 |  | 
 | 184 |     switch (TYPE(n)) { | 
 | 185 |         case single_input: | 
 | 186 |             if (TYPE(CHILD(n, 0)) == NEWLINE) | 
 | 187 |                 return 0; | 
 | 188 |             else | 
 | 189 |                 return num_stmts(CHILD(n, 0)); | 
 | 190 |         case file_input: | 
 | 191 |             l = 0; | 
 | 192 |             for (i = 0; i < NCH(n); i++) { | 
 | 193 |                 ch = CHILD(n, i); | 
 | 194 |                 if (TYPE(ch) == stmt) | 
 | 195 |                     l += num_stmts(ch); | 
 | 196 |             } | 
 | 197 |             return l; | 
 | 198 |         case stmt: | 
 | 199 |             return num_stmts(CHILD(n, 0)); | 
 | 200 |         case compound_stmt: | 
 | 201 |             return 1; | 
 | 202 |         case simple_stmt: | 
 | 203 |             return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ | 
 | 204 |         case suite: | 
 | 205 |             if (NCH(n) == 1) | 
 | 206 |                 return num_stmts(CHILD(n, 0)); | 
 | 207 |             else { | 
 | 208 |                 l = 0; | 
 | 209 |                 for (i = 2; i < (NCH(n) - 1); i++) | 
 | 210 |                     l += num_stmts(CHILD(n, i)); | 
 | 211 |                 return l; | 
 | 212 |             } | 
 | 213 |         default: { | 
 | 214 |             char buf[128]; | 
 | 215 |  | 
 | 216 |             sprintf(buf, "Non-statement found: %d %d\n", | 
 | 217 |                     TYPE(n), NCH(n)); | 
 | 218 |             Py_FatalError(buf); | 
 | 219 |         } | 
 | 220 |     } | 
 | 221 |     assert(0); | 
 | 222 |     return 0; | 
 | 223 | } | 
 | 224 |  | 
 | 225 | /* Transform the CST rooted at node * to the appropriate AST | 
 | 226 | */ | 
 | 227 |  | 
 | 228 | mod_ty | 
 | 229 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename) | 
 | 230 | { | 
 | 231 |     int i, j, num; | 
 | 232 |     asdl_seq *stmts = NULL; | 
 | 233 |     stmt_ty s; | 
 | 234 |     node *ch; | 
 | 235 |     struct compiling c; | 
 | 236 |  | 
 | 237 |     if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { | 
 | 238 |             c.c_encoding = "utf-8"; | 
 | 239 |     } else if (TYPE(n) == encoding_decl) { | 
 | 240 |         c.c_encoding = STR(n); | 
 | 241 |         n = CHILD(n, 0); | 
 | 242 |     } else { | 
 | 243 |         c.c_encoding = NULL; | 
 | 244 |     } | 
 | 245 |  | 
 | 246 |     switch (TYPE(n)) { | 
 | 247 |         case file_input: | 
 | 248 |             stmts = asdl_seq_new(num_stmts(n)); | 
 | 249 |             if (!stmts) | 
 | 250 |                     return NULL; | 
 | 251 |             for (i = 0; i < NCH(n) - 1; i++) { | 
 | 252 |                 ch = CHILD(n, i); | 
 | 253 |                 if (TYPE(ch) == NEWLINE) | 
 | 254 |                     continue; | 
 | 255 |                 REQ(ch, stmt); | 
 | 256 |                 num = num_stmts(ch); | 
 | 257 |                 if (num == 1) { | 
 | 258 |                     s = ast_for_stmt(&c, ch); | 
 | 259 |                     if (!s) | 
 | 260 |                         goto error; | 
 | 261 |                     asdl_seq_APPEND(stmts, s); | 
 | 262 |                 } | 
 | 263 |                 else { | 
 | 264 |                     ch = CHILD(ch, 0); | 
 | 265 |                     REQ(ch, simple_stmt); | 
 | 266 |                     for (j = 0; j < num; j++) { | 
 | 267 |                         s = ast_for_stmt(&c, CHILD(ch, j * 2)); | 
 | 268 |                         if (!s) | 
 | 269 |                             goto error; | 
 | 270 |                         asdl_seq_APPEND(stmts, s); | 
 | 271 |                     } | 
 | 272 |                 } | 
 | 273 |             } | 
 | 274 |             return Module(stmts); | 
 | 275 |         case eval_input: { | 
 | 276 |             expr_ty testlist_ast; | 
 | 277 |  | 
 | 278 |             /* XXX Why not gen_for here? */ | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 279 |             testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 280 |             if (!testlist_ast) | 
 | 281 |                 goto error; | 
 | 282 |             return Expression(testlist_ast); | 
 | 283 |         } | 
 | 284 |         case single_input: | 
 | 285 |             if (TYPE(CHILD(n, 0)) == NEWLINE) { | 
 | 286 |                 stmts = asdl_seq_new(1); | 
 | 287 |                 if (!stmts) | 
 | 288 | 		    goto error; | 
 | 289 |                 asdl_seq_SET(stmts, 0, Pass(n->n_lineno)); | 
 | 290 |                 return Interactive(stmts); | 
 | 291 |             } | 
 | 292 |             else { | 
 | 293 |                 n = CHILD(n, 0); | 
 | 294 |                 num = num_stmts(n); | 
 | 295 |                 stmts = asdl_seq_new(num); | 
 | 296 |                 if (!stmts) | 
 | 297 | 		    goto error; | 
 | 298 |                 if (num == 1) { | 
 | 299 | 		    stmt_ty s = ast_for_stmt(&c, n); | 
 | 300 | 		    if (!s) | 
 | 301 | 			goto error; | 
 | 302 |                     asdl_seq_SET(stmts, 0, s); | 
 | 303 |                 } | 
 | 304 |                 else { | 
 | 305 |                     /* Only a simple_stmt can contain multiple statements. */ | 
 | 306 |                     REQ(n, simple_stmt); | 
 | 307 |                     for (i = 0; i < NCH(n); i += 2) { | 
 | 308 |                         stmt_ty s; | 
 | 309 |                         if (TYPE(CHILD(n, i)) == NEWLINE) | 
 | 310 |                             break; | 
 | 311 |                         s = ast_for_stmt(&c, CHILD(n, i)); | 
 | 312 |                         if (!s) | 
 | 313 |                             goto error; | 
 | 314 |                         asdl_seq_SET(stmts, i / 2, s); | 
 | 315 |                     } | 
 | 316 |                 } | 
 | 317 |  | 
 | 318 |                 return Interactive(stmts); | 
 | 319 |             } | 
 | 320 |         default: | 
 | 321 |             goto error; | 
 | 322 |     } | 
 | 323 |  error: | 
 | 324 |     if (stmts) | 
 | 325 | 	asdl_stmt_seq_free(stmts); | 
 | 326 |     ast_error_finish(filename); | 
 | 327 |     return NULL; | 
 | 328 | } | 
 | 329 |  | 
 | 330 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) | 
 | 331 | */ | 
 | 332 |  | 
 | 333 | static operator_ty | 
 | 334 | get_operator(const node *n) | 
 | 335 | { | 
 | 336 |     switch (TYPE(n)) { | 
 | 337 |         case VBAR: | 
 | 338 |             return BitOr; | 
 | 339 |         case CIRCUMFLEX: | 
 | 340 |             return BitXor; | 
 | 341 |         case AMPER: | 
 | 342 |             return BitAnd; | 
 | 343 |         case LEFTSHIFT: | 
 | 344 |             return LShift; | 
 | 345 |         case RIGHTSHIFT: | 
 | 346 |             return RShift; | 
 | 347 |         case PLUS: | 
 | 348 |             return Add; | 
 | 349 |         case MINUS: | 
 | 350 |             return Sub; | 
 | 351 |         case STAR: | 
 | 352 |             return Mult; | 
 | 353 |         case SLASH: | 
 | 354 |             return Div; | 
 | 355 |         case DOUBLESLASH: | 
 | 356 |             return FloorDiv; | 
 | 357 |         case PERCENT: | 
 | 358 |             return Mod; | 
 | 359 |         default: | 
 | 360 |             return 0; | 
 | 361 |     } | 
 | 362 | } | 
 | 363 |  | 
 | 364 | /* Set the context ctx for expr_ty e returning 0 on success, -1 on error. | 
 | 365 |  | 
 | 366 |    Only sets context for expr kinds that "can appear in assignment context" | 
 | 367 |    (according to ../Parser/Python.asdl).  For other expr kinds, it sets | 
 | 368 |    an appropriate syntax error and returns false. | 
 | 369 |  | 
 | 370 |    If e is a sequential type, items in sequence will also have their context | 
 | 371 |    set. | 
 | 372 |  | 
 | 373 | */ | 
 | 374 |  | 
 | 375 | static int | 
 | 376 | set_context(expr_ty e, expr_context_ty ctx, const node *n) | 
 | 377 | { | 
 | 378 |     asdl_seq *s = NULL; | 
 | 379 |  | 
 | 380 |     switch (e->kind) { | 
 | 381 |         case Attribute_kind: | 
 | 382 | 	    if (ctx == Store && | 
 | 383 | 		!strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) { | 
 | 384 | 		    return ast_error(n, "assignment to None"); | 
 | 385 | 	    } | 
 | 386 | 	    e->v.Attribute.ctx = ctx; | 
 | 387 | 	    break; | 
 | 388 |         case Subscript_kind: | 
 | 389 | 	    e->v.Subscript.ctx = ctx; | 
 | 390 | 	    break; | 
 | 391 |         case Name_kind: | 
 | 392 | 	    if (ctx == Store && | 
 | 393 | 		!strcmp(PyString_AS_STRING(e->v.Name.id), "None")) { | 
 | 394 | 		    return ast_error(n, "assignment to None"); | 
 | 395 | 	    } | 
 | 396 | 	    e->v.Name.ctx = ctx; | 
 | 397 | 	    break; | 
 | 398 |         case List_kind: | 
 | 399 | 	    e->v.List.ctx = ctx; | 
 | 400 | 	    s = e->v.List.elts; | 
 | 401 | 	    break; | 
 | 402 |         case Tuple_kind: | 
 | 403 |             if (asdl_seq_LEN(e->v.Tuple.elts) == 0)  | 
 | 404 |                 return ast_error(n, "can't assign to ()"); | 
 | 405 | 	    e->v.Tuple.ctx = ctx; | 
 | 406 | 	    s = e->v.Tuple.elts; | 
 | 407 | 	    break; | 
 | 408 |         case Call_kind: | 
 | 409 | 	    if (ctx == Store) | 
 | 410 | 		return ast_error(n, "can't assign to function call"); | 
 | 411 | 	    else if (ctx == Del) | 
 | 412 | 		return ast_error(n, "can't delete function call"); | 
 | 413 | 	    else | 
 | 414 | 		return ast_error(n, "unexpected operation on function call"); | 
 | 415 | 	    break; | 
 | 416 |         case BinOp_kind: | 
 | 417 |             return ast_error(n, "can't assign to operator"); | 
 | 418 |         case GeneratorExp_kind: | 
 | 419 |             return ast_error(n, "assignment to generator expression " | 
 | 420 |                              "not possible"); | 
 | 421 |         case Num_kind: | 
 | 422 |         case Str_kind: | 
 | 423 | 	    return ast_error(n, "can't assign to literal"); | 
 | 424 |         default: { | 
 | 425 | 	   char buf[300]; | 
 | 426 | 	   PyOS_snprintf(buf, sizeof(buf),  | 
 | 427 | 			 "unexpected expression in assignment %d (line %d)",  | 
 | 428 | 			 e->kind, e->lineno); | 
 | 429 | 	   return ast_error(n, buf); | 
 | 430 |        } | 
 | 431 |     } | 
 | 432 |     /* If the LHS is a list or tuple, we need to set the assignment | 
 | 433 |        context for all the tuple elements.   | 
 | 434 |     */ | 
 | 435 |     if (s) { | 
 | 436 | 	int i; | 
 | 437 |  | 
 | 438 | 	for (i = 0; i < asdl_seq_LEN(s); i++) { | 
 | 439 | 	    if (!set_context(asdl_seq_GET(s, i), ctx, n)) | 
 | 440 | 		return 0; | 
 | 441 | 	} | 
 | 442 |     } | 
 | 443 |     return 1; | 
 | 444 | } | 
 | 445 |  | 
 | 446 | static operator_ty | 
 | 447 | ast_for_augassign(const node *n) | 
 | 448 | { | 
 | 449 |     REQ(n, augassign); | 
 | 450 |     n = CHILD(n, 0); | 
 | 451 |     switch (STR(n)[0]) { | 
 | 452 |         case '+': | 
 | 453 |             return Add; | 
 | 454 |         case '-': | 
 | 455 |             return Sub; | 
 | 456 |         case '/': | 
 | 457 |             if (STR(n)[1] == '/') | 
 | 458 |                 return FloorDiv; | 
 | 459 |             else | 
 | 460 |                 return Div; | 
 | 461 |         case '%': | 
 | 462 |             return Mod; | 
 | 463 |         case '<': | 
 | 464 |             return LShift; | 
 | 465 |         case '>': | 
 | 466 |             return RShift; | 
 | 467 |         case '&': | 
 | 468 |             return BitAnd; | 
 | 469 |         case '^': | 
 | 470 |             return BitXor; | 
 | 471 |         case '|': | 
 | 472 |             return BitOr; | 
 | 473 |         case '*': | 
 | 474 |             if (STR(n)[1] == '*') | 
 | 475 |                 return Pow; | 
 | 476 |             else | 
 | 477 |                 return Mult; | 
 | 478 |         default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 479 |             PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 480 |             return 0; | 
 | 481 |     } | 
 | 482 | } | 
 | 483 |  | 
 | 484 | static cmpop_ty | 
 | 485 | ast_for_comp_op(const node *n) | 
 | 486 | { | 
 | 487 |     /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is' | 
 | 488 |                |'is' 'not' | 
 | 489 |     */ | 
 | 490 |     REQ(n, comp_op); | 
 | 491 |     if (NCH(n) == 1) { | 
 | 492 | 	n = CHILD(n, 0); | 
 | 493 | 	switch (TYPE(n)) { | 
 | 494 |             case LESS: | 
 | 495 |                 return Lt; | 
 | 496 |             case GREATER: | 
 | 497 |                 return Gt; | 
 | 498 |             case EQEQUAL:			/* == */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 499 |                 return Eq; | 
 | 500 |             case LESSEQUAL: | 
 | 501 |                 return LtE; | 
 | 502 |             case GREATEREQUAL: | 
 | 503 |                 return GtE; | 
 | 504 |             case NOTEQUAL: | 
 | 505 |                 return NotEq; | 
 | 506 |             case NAME: | 
 | 507 |                 if (strcmp(STR(n), "in") == 0) | 
 | 508 |                     return In; | 
 | 509 |                 if (strcmp(STR(n), "is") == 0) | 
 | 510 |                     return Is; | 
 | 511 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 512 |                 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 513 |                              STR(n)); | 
 | 514 |                 return 0; | 
 | 515 | 	} | 
 | 516 |     } | 
 | 517 |     else if (NCH(n) == 2) { | 
 | 518 | 	/* handle "not in" and "is not" */ | 
 | 519 | 	switch (TYPE(CHILD(n, 0))) { | 
 | 520 |             case NAME: | 
 | 521 |                 if (strcmp(STR(CHILD(n, 1)), "in") == 0) | 
 | 522 |                     return NotIn; | 
 | 523 |                 if (strcmp(STR(CHILD(n, 0)), "is") == 0) | 
 | 524 |                     return IsNot; | 
 | 525 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 526 |                 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 527 |                              STR(CHILD(n, 0)), STR(CHILD(n, 1))); | 
 | 528 |                 return 0; | 
 | 529 | 	} | 
 | 530 |     } | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 531 |     PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 532 |                  NCH(n)); | 
 | 533 |     return 0; | 
 | 534 | } | 
 | 535 |  | 
 | 536 | static asdl_seq * | 
 | 537 | seq_for_testlist(struct compiling *c, const node *n) | 
 | 538 | { | 
 | 539 |     /* testlist: test (',' test)* [','] */ | 
| Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 540 |     asdl_seq *seq; | 
 | 541 |     expr_ty expression; | 
 | 542 |     int i; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 543 |     assert(TYPE(n) == testlist | 
 | 544 | 	   || TYPE(n) == listmaker | 
 | 545 | 	   || TYPE(n) == testlist_gexp | 
 | 546 | 	   || TYPE(n) == testlist_safe | 
 | 547 | 	   ); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 548 |  | 
 | 549 |     seq = asdl_seq_new((NCH(n) + 1) / 2); | 
 | 550 |     if (!seq) | 
 | 551 |         return NULL; | 
 | 552 |  | 
 | 553 |     for (i = 0; i < NCH(n); i += 2) { | 
 | 554 |         REQ(CHILD(n, i), test); | 
 | 555 |  | 
 | 556 |         expression = ast_for_expr(c, CHILD(n, i)); | 
 | 557 |         if (!expression) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 558 |             asdl_expr_seq_free(seq); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 559 |             return NULL; | 
 | 560 |         } | 
 | 561 |  | 
 | 562 |         assert(i / 2 < seq->size); | 
 | 563 |         asdl_seq_SET(seq, i / 2, expression); | 
 | 564 |     } | 
 | 565 |     return seq; | 
 | 566 | } | 
 | 567 |  | 
 | 568 | static expr_ty | 
 | 569 | compiler_complex_args(const node *n) | 
 | 570 | { | 
 | 571 |     int i, len = (NCH(n) + 1) / 2; | 
 | 572 |     expr_ty result; | 
 | 573 |     asdl_seq *args = asdl_seq_new(len); | 
 | 574 |     if (!args) | 
 | 575 |         return NULL; | 
 | 576 |  | 
 | 577 |     REQ(n, fplist); | 
 | 578 |  | 
 | 579 |     for (i = 0; i < len; i++) { | 
 | 580 |         const node *child = CHILD(CHILD(n, 2*i), 0); | 
 | 581 |         expr_ty arg; | 
 | 582 |         if (TYPE(child) == NAME) { | 
 | 583 | 		if (!strcmp(STR(child), "None")) { | 
 | 584 | 			ast_error(child, "assignment to None"); | 
 | 585 | 			return NULL; | 
 | 586 | 		} | 
 | 587 |             arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child)); | 
 | 588 | 	} | 
 | 589 |         else | 
 | 590 |             arg = compiler_complex_args(CHILD(CHILD(n, 2*i), 1)); | 
 | 591 | 	set_context(arg, Store, n); | 
 | 592 |         asdl_seq_SET(args, i, arg); | 
 | 593 |     } | 
 | 594 |  | 
 | 595 |     result = Tuple(args, Store, LINENO(n)); | 
 | 596 |     set_context(result, Store, n); | 
 | 597 |     return result; | 
 | 598 | } | 
 | 599 |  | 
 | 600 | /* Create AST for argument list. | 
 | 601 |  | 
 | 602 |    XXX TO DO: | 
 | 603 |        - check for invalid argument lists like normal after default | 
 | 604 | */ | 
 | 605 |  | 
 | 606 | static arguments_ty | 
 | 607 | ast_for_arguments(struct compiling *c, const node *n) | 
 | 608 | { | 
 | 609 |     /* parameters: '(' [varargslist] ')' | 
 | 610 |        varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | 
 | 611 |             | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [','] | 
 | 612 |     */ | 
 | 613 |     int i, n_args = 0, n_defaults = 0, found_default = 0; | 
 | 614 |     asdl_seq *args, *defaults; | 
 | 615 |     identifier vararg = NULL, kwarg = NULL; | 
 | 616 |     node *ch; | 
 | 617 |  | 
 | 618 |     if (TYPE(n) == parameters) { | 
 | 619 | 	if (NCH(n) == 2) /* () as argument list */ | 
 | 620 | 	    return arguments(NULL, NULL, NULL, NULL); | 
 | 621 | 	n = CHILD(n, 1); | 
 | 622 |     } | 
 | 623 |     REQ(n, varargslist); | 
 | 624 |  | 
 | 625 |     /* first count the number of normal args & defaults */ | 
 | 626 |     for (i = 0; i < NCH(n); i++) { | 
 | 627 | 	ch = CHILD(n, i); | 
 | 628 | 	if (TYPE(ch) == fpdef) { | 
 | 629 | 	    n_args++; | 
 | 630 | 	} | 
 | 631 | 	if (TYPE(ch) == EQUAL) | 
 | 632 | 	    n_defaults++; | 
 | 633 |     } | 
 | 634 |     args = (n_args ? asdl_seq_new(n_args) : NULL); | 
 | 635 |     if (!args && n_args) | 
 | 636 |     	return NULL; /* Don't need to go to NULL; nothing allocated */ | 
 | 637 |     defaults = (n_defaults ? asdl_seq_new(n_defaults) : NULL); | 
 | 638 |     if (!defaults && n_defaults) | 
 | 639 |         goto error; | 
 | 640 |  | 
 | 641 |     /* fpdef: NAME | '(' fplist ')' | 
 | 642 |        fplist: fpdef (',' fpdef)* [','] | 
 | 643 |     */ | 
 | 644 |     i = 0; | 
 | 645 |     while (i < NCH(n)) { | 
 | 646 | 	ch = CHILD(n, i); | 
 | 647 | 	switch (TYPE(ch)) { | 
 | 648 |             case fpdef: | 
 | 649 |                 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is | 
 | 650 |                    anything other than EQUAL or a comma? */ | 
 | 651 |                 /* XXX Should NCH(n) check be made a separate check? */ | 
 | 652 |                 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { | 
 | 653 |                     asdl_seq_APPEND(defaults,  | 
 | 654 | 				    ast_for_expr(c, CHILD(n, i + 2))); | 
 | 655 |                     i += 2; | 
 | 656 | 		    found_default = 1; | 
 | 657 |                 } | 
 | 658 | 		else if (found_default) { | 
 | 659 | 		    ast_error(n,  | 
 | 660 | 			     "non-default argument follows default argument"); | 
 | 661 | 		    goto error; | 
 | 662 | 		} | 
 | 663 |  | 
 | 664 |                 if (NCH(ch) == 3) { | 
 | 665 |                     asdl_seq_APPEND(args,  | 
 | 666 |                                     compiler_complex_args(CHILD(ch, 1)));  | 
 | 667 | 		} | 
 | 668 |                 else if (TYPE(CHILD(ch, 0)) == NAME) { | 
| Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 669 | 		    expr_ty name; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 670 | 		    if (!strcmp(STR(CHILD(ch, 0)), "None")) { | 
 | 671 | 			    ast_error(CHILD(ch, 0), "assignment to None"); | 
 | 672 | 			    goto error; | 
 | 673 | 		    } | 
| Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 674 |                     name = Name(NEW_IDENTIFIER(CHILD(ch, 0)), | 
 | 675 |                                 Param, LINENO(ch)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 676 |                     if (!name) | 
 | 677 |                         goto error; | 
 | 678 |                     asdl_seq_APPEND(args, name); | 
 | 679 | 					  | 
 | 680 | 		} | 
 | 681 |                 i += 2; /* the name and the comma */ | 
 | 682 |                 break; | 
 | 683 |             case STAR: | 
 | 684 | 		if (!strcmp(STR(CHILD(n, i+1)), "None")) { | 
 | 685 | 			ast_error(CHILD(n, i+1), "assignment to None"); | 
 | 686 | 			goto error; | 
 | 687 | 		} | 
 | 688 |                 vararg = NEW_IDENTIFIER(CHILD(n, i+1)); | 
 | 689 |                 i += 3; | 
 | 690 |                 break; | 
 | 691 |             case DOUBLESTAR: | 
 | 692 | 		if (!strcmp(STR(CHILD(n, i+1)), "None")) { | 
 | 693 | 			ast_error(CHILD(n, i+1), "assignment to None"); | 
 | 694 | 			goto error; | 
 | 695 | 		} | 
 | 696 |                 kwarg = NEW_IDENTIFIER(CHILD(n, i+1)); | 
 | 697 |                 i += 3; | 
 | 698 |                 break; | 
 | 699 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 700 |                 PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 701 |                              "unexpected node in varargslist: %d @ %d", | 
 | 702 |                              TYPE(ch), i); | 
 | 703 |                 goto error; | 
 | 704 | 	} | 
 | 705 |     } | 
 | 706 |  | 
 | 707 |     return arguments(args, vararg, kwarg, defaults); | 
 | 708 |  | 
 | 709 |  error: | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 710 |     Py_XDECREF(vararg); | 
 | 711 |     Py_XDECREF(kwarg); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 712 |     if (args) | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 713 |         asdl_expr_seq_free(args); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 714 |     if (defaults) | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 715 |         asdl_expr_seq_free(defaults); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 716 |     return NULL; | 
 | 717 | } | 
 | 718 |  | 
 | 719 | static expr_ty | 
 | 720 | ast_for_dotted_name(struct compiling *c, const node *n) | 
 | 721 | { | 
 | 722 |     expr_ty e = NULL; | 
 | 723 |     expr_ty attrib = NULL; | 
 | 724 |     identifier id = NULL; | 
 | 725 |     int i; | 
 | 726 |  | 
 | 727 |     REQ(n, dotted_name); | 
 | 728 |      | 
 | 729 |     id = NEW_IDENTIFIER(CHILD(n, 0)); | 
 | 730 |     if (!id) | 
 | 731 |         goto error; | 
 | 732 |     e = Name(id, Load, LINENO(n)); | 
 | 733 |     if (!e) | 
 | 734 | 	goto error; | 
 | 735 |     id = NULL; | 
 | 736 |  | 
 | 737 |     for (i = 2; i < NCH(n); i+=2) { | 
 | 738 |         id = NEW_IDENTIFIER(CHILD(n, i)); | 
 | 739 | 	if (!id) | 
 | 740 | 	    goto error; | 
 | 741 | 	attrib = Attribute(e, id, Load, LINENO(CHILD(n, i))); | 
 | 742 | 	if (!attrib) | 
 | 743 | 	    goto error; | 
 | 744 | 	e = attrib; | 
 | 745 | 	attrib = NULL; | 
 | 746 |     } | 
 | 747 |  | 
 | 748 |     return e; | 
 | 749 |      | 
 | 750 |   error: | 
 | 751 |     Py_XDECREF(id); | 
 | 752 |     free_expr(e); | 
 | 753 |     return NULL; | 
 | 754 | } | 
 | 755 |  | 
 | 756 | static expr_ty | 
 | 757 | ast_for_decorator(struct compiling *c, const node *n) | 
 | 758 | { | 
 | 759 |     /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ | 
 | 760 |     expr_ty d = NULL; | 
 | 761 |     expr_ty name_expr = NULL; | 
 | 762 |      | 
 | 763 |     REQ(n, decorator); | 
 | 764 |      | 
 | 765 |     if ((NCH(n) < 3 && NCH(n) != 5 && NCH(n) != 6) | 
 | 766 | 	|| TYPE(CHILD(n, 0)) != AT || TYPE(RCHILD(n, -1)) != NEWLINE) { | 
 | 767 | 	ast_error(n, "Invalid decorator node"); | 
 | 768 | 	goto error; | 
 | 769 |     } | 
 | 770 |      | 
 | 771 |     name_expr = ast_for_dotted_name(c, CHILD(n, 1)); | 
 | 772 |     if (!name_expr) | 
 | 773 | 	goto error; | 
 | 774 | 	 | 
 | 775 |     if (NCH(n) == 3) { /* No arguments */ | 
 | 776 | 	d = name_expr; | 
 | 777 | 	name_expr = NULL; | 
 | 778 |     } | 
 | 779 |     else if (NCH(n) == 5) { /* Call with no arguments */ | 
 | 780 | 	d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n)); | 
 | 781 | 	if (!d) | 
 | 782 | 	    goto error; | 
 | 783 | 	name_expr = NULL; | 
 | 784 |     } | 
 | 785 |     else { | 
 | 786 | 	d = ast_for_call(c, CHILD(n, 3), name_expr); | 
 | 787 | 	if (!d) | 
 | 788 | 	    goto error; | 
 | 789 | 	name_expr = NULL; | 
 | 790 |     } | 
 | 791 |  | 
 | 792 |     return d; | 
 | 793 |      | 
 | 794 |   error: | 
 | 795 |     free_expr(name_expr); | 
 | 796 |     free_expr(d); | 
 | 797 |     return NULL; | 
 | 798 | } | 
 | 799 |  | 
 | 800 | static asdl_seq* | 
 | 801 | ast_for_decorators(struct compiling *c, const node *n) | 
 | 802 | { | 
 | 803 |     asdl_seq* decorator_seq = NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 804 |     expr_ty d; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 805 |     int i; | 
 | 806 |      | 
 | 807 |     REQ(n, decorators); | 
 | 808 |  | 
 | 809 |     decorator_seq = asdl_seq_new(NCH(n)); | 
 | 810 |     if (!decorator_seq) | 
 | 811 |         return NULL; | 
 | 812 | 	 | 
 | 813 |     for (i = 0; i < NCH(n); i++) { | 
 | 814 | 	d = ast_for_decorator(c, CHILD(n, i)); | 
 | 815 | 	if (!d) | 
 | 816 | 	    goto error; | 
 | 817 | 	asdl_seq_APPEND(decorator_seq, d); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 818 |     } | 
 | 819 |     return decorator_seq; | 
 | 820 |   error: | 
 | 821 |     asdl_expr_seq_free(decorator_seq); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 822 |     return NULL; | 
 | 823 | } | 
 | 824 |  | 
 | 825 | static stmt_ty | 
 | 826 | ast_for_funcdef(struct compiling *c, const node *n) | 
 | 827 | { | 
 | 828 |     /* funcdef: 'def' [decorators] NAME parameters ':' suite */ | 
 | 829 |     identifier name = NULL; | 
 | 830 |     arguments_ty args = NULL; | 
 | 831 |     asdl_seq *body = NULL; | 
 | 832 |     asdl_seq *decorator_seq = NULL; | 
 | 833 |     int name_i; | 
 | 834 |  | 
 | 835 |     REQ(n, funcdef); | 
 | 836 |  | 
 | 837 |     if (NCH(n) == 6) { /* decorators are present */ | 
 | 838 | 	decorator_seq = ast_for_decorators(c, CHILD(n, 0)); | 
 | 839 | 	if (!decorator_seq) | 
 | 840 | 	    goto error; | 
 | 841 | 	name_i = 2; | 
 | 842 |     } | 
 | 843 |     else { | 
 | 844 | 	name_i = 1; | 
 | 845 |     } | 
 | 846 |  | 
 | 847 |     name = NEW_IDENTIFIER(CHILD(n, name_i)); | 
 | 848 |     if (!name) | 
 | 849 | 	goto error; | 
 | 850 |     else if (!strcmp(STR(CHILD(n, name_i)), "None")) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 851 | 	ast_error(CHILD(n, name_i), "assignment to None"); | 
 | 852 | 	goto error; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 853 |     } | 
 | 854 |     args = ast_for_arguments(c, CHILD(n, name_i + 1)); | 
 | 855 |     if (!args) | 
 | 856 | 	goto error; | 
 | 857 |     body = ast_for_suite(c, CHILD(n, name_i + 3)); | 
 | 858 |     if (!body) | 
 | 859 | 	goto error; | 
 | 860 |  | 
 | 861 |     return FunctionDef(name, args, body, decorator_seq, LINENO(n)); | 
 | 862 |  | 
 | 863 | error: | 
 | 864 |     asdl_stmt_seq_free(body); | 
 | 865 |     asdl_expr_seq_free(decorator_seq); | 
 | 866 |     free_arguments(args); | 
 | 867 |     Py_XDECREF(name); | 
 | 868 |     return NULL; | 
 | 869 | } | 
 | 870 |  | 
 | 871 | static expr_ty | 
 | 872 | ast_for_lambdef(struct compiling *c, const node *n) | 
 | 873 | { | 
 | 874 |     /* lambdef: 'lambda' [varargslist] ':' test */ | 
 | 875 |     arguments_ty args; | 
 | 876 |     expr_ty expression; | 
 | 877 |  | 
 | 878 |     if (NCH(n) == 3) { | 
 | 879 |         args = arguments(NULL, NULL, NULL, NULL); | 
 | 880 |         if (!args) | 
 | 881 |             return NULL; | 
 | 882 |         expression = ast_for_expr(c, CHILD(n, 2)); | 
 | 883 |         if (!expression) { | 
 | 884 |             free_arguments(args); | 
 | 885 |             return NULL; | 
 | 886 |         } | 
 | 887 |     } | 
 | 888 |     else { | 
 | 889 |         args = ast_for_arguments(c, CHILD(n, 1)); | 
 | 890 |         if (!args) | 
 | 891 |             return NULL; | 
 | 892 |         expression = ast_for_expr(c, CHILD(n, 3)); | 
 | 893 |         if (!expression) { | 
 | 894 |             free_arguments(args); | 
 | 895 |             return NULL; | 
 | 896 |         } | 
 | 897 |     } | 
 | 898 |  | 
 | 899 |     return Lambda(args, expression, LINENO(n)); | 
 | 900 | } | 
 | 901 |  | 
 | 902 | /* Count the number of 'for' loop in a list comprehension. | 
 | 903 |  | 
 | 904 |    Helper for ast_for_listcomp(). | 
 | 905 | */ | 
 | 906 |  | 
 | 907 | static int | 
 | 908 | count_list_fors(const node *n) | 
 | 909 | { | 
 | 910 |     int n_fors = 0; | 
 | 911 |     node *ch = CHILD(n, 1); | 
 | 912 |  | 
 | 913 |  count_list_for: | 
 | 914 |     n_fors++; | 
 | 915 |     REQ(ch, list_for); | 
 | 916 |     if (NCH(ch) == 5) | 
 | 917 | 	ch = CHILD(ch, 4); | 
 | 918 |     else | 
 | 919 | 	return n_fors; | 
 | 920 |  count_list_iter: | 
 | 921 |     REQ(ch, list_iter); | 
 | 922 |     ch = CHILD(ch, 0); | 
 | 923 |     if (TYPE(ch) == list_for) | 
 | 924 | 	goto count_list_for; | 
 | 925 |     else if (TYPE(ch) == list_if) { | 
 | 926 |         if (NCH(ch) == 3) { | 
 | 927 |             ch = CHILD(ch, 2); | 
 | 928 |             goto count_list_iter; | 
 | 929 |         } | 
 | 930 |         else | 
 | 931 |             return n_fors; | 
 | 932 |     } | 
 | 933 |     else { | 
 | 934 |         /* Should never be reached */ | 
| Neal Norwitz | 7b3d5e1 | 2005-11-13 21:17:28 +0000 | [diff] [blame] | 935 |         PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors"); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 936 |         return -1; | 
 | 937 |     } | 
 | 938 | } | 
 | 939 |  | 
 | 940 | /* Count the number of 'if' statements in a list comprehension. | 
 | 941 |  | 
 | 942 |    Helper for ast_for_listcomp(). | 
 | 943 | */ | 
 | 944 |  | 
 | 945 | static int | 
 | 946 | count_list_ifs(const node *n) | 
 | 947 | { | 
 | 948 |     int n_ifs = 0; | 
 | 949 |  | 
 | 950 |  count_list_iter: | 
 | 951 |     REQ(n, list_iter); | 
 | 952 |     if (TYPE(CHILD(n, 0)) == list_for) | 
 | 953 | 	return n_ifs; | 
 | 954 |     n = CHILD(n, 0); | 
 | 955 |     REQ(n, list_if); | 
 | 956 |     n_ifs++; | 
 | 957 |     if (NCH(n) == 2) | 
 | 958 | 	return n_ifs; | 
 | 959 |     n = CHILD(n, 2); | 
 | 960 |     goto count_list_iter; | 
 | 961 | } | 
 | 962 |  | 
 | 963 | static expr_ty | 
 | 964 | ast_for_listcomp(struct compiling *c, const node *n) | 
 | 965 | { | 
 | 966 |     /* listmaker: test ( list_for | (',' test)* [','] ) | 
 | 967 |        list_for: 'for' exprlist 'in' testlist_safe [list_iter] | 
 | 968 |        list_iter: list_for | list_if | 
 | 969 |        list_if: 'if' test [list_iter] | 
 | 970 |        testlist_safe: test [(',' test)+ [',']] | 
 | 971 |     */ | 
 | 972 |     expr_ty elt; | 
 | 973 |     asdl_seq *listcomps; | 
 | 974 |     int i, n_fors; | 
 | 975 |     node *ch; | 
 | 976 |  | 
 | 977 |     REQ(n, listmaker); | 
 | 978 |     assert(NCH(n) > 1); | 
 | 979 |  | 
 | 980 |     elt = ast_for_expr(c, CHILD(n, 0)); | 
 | 981 |     if (!elt) | 
 | 982 |         return NULL; | 
 | 983 |  | 
 | 984 |     n_fors = count_list_fors(n); | 
 | 985 |     if (n_fors == -1) | 
 | 986 |         return NULL; | 
 | 987 |  | 
 | 988 |     listcomps = asdl_seq_new(n_fors); | 
 | 989 |     if (!listcomps) { | 
 | 990 |         free_expr(elt); | 
 | 991 |     	return NULL; | 
 | 992 |     } | 
 | 993 |      | 
 | 994 |     ch = CHILD(n, 1); | 
 | 995 |     for (i = 0; i < n_fors; i++) { | 
 | 996 | 	comprehension_ty lc; | 
 | 997 | 	asdl_seq *t; | 
 | 998 |         expr_ty expression; | 
 | 999 |  | 
 | 1000 | 	REQ(ch, list_for); | 
 | 1001 |  | 
 | 1002 | 	t = ast_for_exprlist(c, CHILD(ch, 1), Store); | 
 | 1003 |         if (!t) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1004 |             asdl_comprehension_seq_free(listcomps); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1005 |             free_expr(elt); | 
 | 1006 |             return NULL; | 
 | 1007 |         } | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1008 |         expression = ast_for_testlist(c, CHILD(ch, 3)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1009 |         if (!expression) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1010 |             asdl_expr_seq_free(t); | 
 | 1011 |             asdl_comprehension_seq_free(listcomps); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1012 |             free_expr(elt); | 
 | 1013 |             return NULL; | 
 | 1014 |         } | 
 | 1015 |  | 
 | 1016 | 	if (asdl_seq_LEN(t) == 1) | 
 | 1017 | 	    lc = comprehension(asdl_seq_GET(t, 0), expression, NULL); | 
 | 1018 | 	else | 
 | 1019 | 	    lc = comprehension(Tuple(t, Store, LINENO(ch)), expression, NULL); | 
 | 1020 |  | 
 | 1021 |         if (!lc) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1022 |             asdl_expr_seq_free(t); | 
 | 1023 |             asdl_comprehension_seq_free(listcomps); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1024 |             free_expr(expression); | 
 | 1025 |             free_expr(elt); | 
 | 1026 |             return NULL; | 
 | 1027 |         } | 
 | 1028 |  | 
 | 1029 | 	if (NCH(ch) == 5) { | 
 | 1030 | 	    int j, n_ifs; | 
 | 1031 | 	    asdl_seq *ifs; | 
 | 1032 |  | 
 | 1033 | 	    ch = CHILD(ch, 4); | 
 | 1034 | 	    n_ifs = count_list_ifs(ch); | 
 | 1035 |             if (n_ifs == -1) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1036 |                 free_comprehension(lc); | 
 | 1037 |                 asdl_comprehension_seq_free(listcomps); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1038 |                 free_expr(elt); | 
 | 1039 |                 return NULL; | 
 | 1040 |             } | 
 | 1041 |  | 
 | 1042 | 	    ifs = asdl_seq_new(n_ifs); | 
 | 1043 | 	    if (!ifs) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1044 |                 free_comprehension(lc); | 
 | 1045 | 		asdl_comprehension_seq_free(listcomps); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1046 |                 free_expr(elt); | 
 | 1047 | 		return NULL; | 
 | 1048 | 	    } | 
 | 1049 |  | 
 | 1050 | 	    for (j = 0; j < n_ifs; j++) { | 
 | 1051 | 		REQ(ch, list_iter); | 
 | 1052 |  | 
 | 1053 | 		ch = CHILD(ch, 0); | 
 | 1054 | 		REQ(ch, list_if); | 
 | 1055 |  | 
 | 1056 | 		asdl_seq_APPEND(ifs, ast_for_expr(c, CHILD(ch, 1))); | 
 | 1057 | 		if (NCH(ch) == 3) | 
 | 1058 | 		    ch = CHILD(ch, 2); | 
 | 1059 | 	    } | 
 | 1060 | 	    /* on exit, must guarantee that ch is a list_for */ | 
 | 1061 | 	    if (TYPE(ch) == list_iter) | 
 | 1062 | 		ch = CHILD(ch, 0); | 
 | 1063 |             lc->ifs = ifs; | 
 | 1064 | 	} | 
 | 1065 | 	asdl_seq_APPEND(listcomps, lc); | 
 | 1066 |     } | 
 | 1067 |  | 
 | 1068 |     return ListComp(elt, listcomps, LINENO(n)); | 
 | 1069 | } | 
 | 1070 |  | 
 | 1071 | /* | 
 | 1072 |    Count the number of 'for' loops in a generator expression. | 
 | 1073 |  | 
 | 1074 |    Helper for ast_for_genexp(). | 
 | 1075 | */ | 
 | 1076 |  | 
 | 1077 | static int | 
 | 1078 | count_gen_fors(const node *n) | 
 | 1079 | { | 
 | 1080 | 	int n_fors = 0; | 
 | 1081 | 	node *ch = CHILD(n, 1); | 
 | 1082 |  | 
 | 1083 |  count_gen_for: | 
 | 1084 | 	n_fors++; | 
 | 1085 | 	REQ(ch, gen_for); | 
 | 1086 | 	if (NCH(ch) == 5) | 
 | 1087 | 		ch = CHILD(ch, 4); | 
 | 1088 | 	else | 
 | 1089 | 		return n_fors; | 
 | 1090 |  count_gen_iter: | 
 | 1091 | 	REQ(ch, gen_iter); | 
 | 1092 | 	ch = CHILD(ch, 0); | 
 | 1093 | 	if (TYPE(ch) == gen_for) | 
 | 1094 | 		goto count_gen_for; | 
 | 1095 | 	else if (TYPE(ch) == gen_if) { | 
 | 1096 | 		if (NCH(ch) == 3) { | 
 | 1097 | 			ch = CHILD(ch, 2); | 
 | 1098 | 			goto count_gen_iter; | 
 | 1099 | 		} | 
 | 1100 | 		else | 
 | 1101 | 		    return n_fors; | 
 | 1102 | 	} | 
 | 1103 | 	else { | 
 | 1104 | 		/* Should never be reached */ | 
| Neal Norwitz | 7b3d5e1 | 2005-11-13 21:17:28 +0000 | [diff] [blame] | 1105 | 		PyErr_SetString(PyExc_SystemError, | 
 | 1106 | 				"logic error in count_gen_fors"); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1107 | 		return -1; | 
 | 1108 | 	} | 
 | 1109 | } | 
 | 1110 |  | 
 | 1111 | /* Count the number of 'if' statements in a generator expression. | 
 | 1112 |  | 
 | 1113 |    Helper for ast_for_genexp(). | 
 | 1114 | */ | 
 | 1115 |  | 
 | 1116 | static int | 
 | 1117 | count_gen_ifs(const node *n) | 
 | 1118 | { | 
 | 1119 | 	int n_ifs = 0; | 
 | 1120 |  | 
 | 1121 | 	while (1) { | 
 | 1122 | 		REQ(n, gen_iter); | 
 | 1123 | 		if (TYPE(CHILD(n, 0)) == gen_for) | 
 | 1124 | 			return n_ifs; | 
 | 1125 | 		n = CHILD(n, 0); | 
 | 1126 | 		REQ(n, gen_if); | 
 | 1127 | 		n_ifs++; | 
 | 1128 | 		if (NCH(n) == 2) | 
 | 1129 | 			return n_ifs; | 
 | 1130 | 		n = CHILD(n, 2); | 
 | 1131 | 	} | 
 | 1132 | } | 
 | 1133 |  | 
 | 1134 | static expr_ty | 
 | 1135 | ast_for_genexp(struct compiling *c, const node *n) | 
 | 1136 | { | 
 | 1137 |     /* testlist_gexp: test ( gen_for | (',' test)* [','] ) | 
 | 1138 |        argument: [test '='] test [gen_for]	 # Really [keyword '='] test */ | 
 | 1139 |     expr_ty elt; | 
 | 1140 |     asdl_seq *genexps; | 
 | 1141 |     int i, n_fors; | 
 | 1142 |     node *ch; | 
 | 1143 |      | 
 | 1144 |     assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument)); | 
 | 1145 |     assert(NCH(n) > 1); | 
 | 1146 |      | 
 | 1147 |     elt = ast_for_expr(c, CHILD(n, 0)); | 
 | 1148 |     if (!elt) | 
 | 1149 |         return NULL; | 
 | 1150 |      | 
 | 1151 |     n_fors = count_gen_fors(n); | 
 | 1152 |     if (n_fors == -1) | 
 | 1153 |         return NULL; | 
 | 1154 |      | 
 | 1155 |     genexps = asdl_seq_new(n_fors); | 
 | 1156 |     if (!genexps) { | 
 | 1157 |         free_expr(elt); | 
 | 1158 |         return NULL; | 
 | 1159 |     } | 
 | 1160 |      | 
 | 1161 |     ch = CHILD(n, 1); | 
 | 1162 |     for (i = 0; i < n_fors; i++) { | 
 | 1163 |         comprehension_ty ge; | 
 | 1164 |         asdl_seq *t; | 
 | 1165 |         expr_ty expression; | 
 | 1166 |          | 
 | 1167 |         REQ(ch, gen_for); | 
 | 1168 |          | 
 | 1169 |         t = ast_for_exprlist(c, CHILD(ch, 1), Store); | 
 | 1170 |         if (!t) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1171 |             asdl_comprehension_seq_free(genexps); | 
 | 1172 | 	    asdl_expr_seq_free(t); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1173 |             free_expr(elt); | 
 | 1174 |             return NULL; | 
 | 1175 |         } | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1176 |         expression = ast_for_expr(c, CHILD(ch, 3)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1177 |         if (!expression) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1178 |             asdl_comprehension_seq_free(genexps); | 
 | 1179 | 	    asdl_expr_seq_free(t); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1180 |             free_expr(elt); | 
 | 1181 |             return NULL; | 
 | 1182 |         } | 
 | 1183 |          | 
 | 1184 |         if (asdl_seq_LEN(t) == 1) | 
 | 1185 |             ge = comprehension(asdl_seq_GET(t, 0), expression, | 
 | 1186 |                                NULL); | 
 | 1187 |         else | 
 | 1188 |             ge = comprehension(Tuple(t, Store, LINENO(ch)), | 
 | 1189 |                                expression, NULL); | 
 | 1190 |          | 
 | 1191 |         if (!ge) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1192 |             asdl_comprehension_seq_free(genexps); | 
 | 1193 | 	    asdl_expr_seq_free(t); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1194 |             free_expr(elt); | 
 | 1195 |             return NULL; | 
 | 1196 |         } | 
 | 1197 |          | 
 | 1198 |         if (NCH(ch) == 5) { | 
 | 1199 |             int j, n_ifs; | 
 | 1200 |             asdl_seq *ifs; | 
 | 1201 |              | 
 | 1202 |             ch = CHILD(ch, 4); | 
 | 1203 |             n_ifs = count_gen_ifs(ch); | 
 | 1204 |             if (n_ifs == -1) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1205 |                 asdl_comprehension_seq_free(genexps); | 
 | 1206 | 		free_comprehension(ge); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1207 |                 free_expr(elt); | 
 | 1208 |                 return NULL; | 
 | 1209 |             } | 
 | 1210 |              | 
 | 1211 |             ifs = asdl_seq_new(n_ifs); | 
 | 1212 |             if (!ifs) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1213 |                 asdl_comprehension_seq_free(genexps); | 
 | 1214 | 		free_comprehension(ge); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1215 |                 free_expr(elt); | 
 | 1216 |                 return NULL; | 
 | 1217 |             } | 
 | 1218 |              | 
 | 1219 |             for (j = 0; j < n_ifs; j++) { | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1220 |                 expr_ty expression; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1221 |                 REQ(ch, gen_iter); | 
 | 1222 |                 ch = CHILD(ch, 0); | 
 | 1223 |                 REQ(ch, gen_if); | 
 | 1224 |                  | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1225 |                 expression = ast_for_expr(c, CHILD(ch, 1)); | 
 | 1226 |                 if (!expression) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1227 | 		    asdl_expr_seq_free(ifs); | 
 | 1228 |                     asdl_comprehension_seq_free(genexps); | 
 | 1229 | 		    free_comprehension(ge); | 
 | 1230 |                     free_expr(elt); | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1231 |                     return NULL; | 
 | 1232 |                 } | 
 | 1233 |                 asdl_seq_APPEND(ifs, expression); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1234 |                 if (NCH(ch) == 3) | 
 | 1235 |                     ch = CHILD(ch, 2); | 
 | 1236 |             } | 
 | 1237 |             /* on exit, must guarantee that ch is a gen_for */ | 
 | 1238 |             if (TYPE(ch) == gen_iter) | 
 | 1239 |                 ch = CHILD(ch, 0); | 
 | 1240 |             ge->ifs = ifs; | 
 | 1241 |         } | 
 | 1242 |         asdl_seq_APPEND(genexps, ge); | 
 | 1243 |     } | 
 | 1244 |      | 
 | 1245 |     return GeneratorExp(elt, genexps, LINENO(n)); | 
 | 1246 | } | 
 | 1247 |  | 
 | 1248 | static expr_ty | 
 | 1249 | ast_for_atom(struct compiling *c, const node *n) | 
 | 1250 | { | 
 | 1251 |     /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']' | 
 | 1252 |        | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+ | 
 | 1253 |     */ | 
 | 1254 |     node *ch = CHILD(n, 0); | 
 | 1255 |      | 
 | 1256 |     switch (TYPE(ch)) { | 
 | 1257 |     case NAME: | 
 | 1258 | 	/* All names start in Load context, but may later be | 
 | 1259 | 	   changed. */ | 
 | 1260 | 	return Name(NEW_IDENTIFIER(ch), Load, LINENO(n)); | 
 | 1261 |     case STRING: { | 
 | 1262 | 	PyObject *str = parsestrplus(c, n); | 
 | 1263 | 	 | 
 | 1264 | 	if (!str) | 
 | 1265 | 	    return NULL; | 
 | 1266 | 	 | 
 | 1267 | 	return Str(str, LINENO(n)); | 
 | 1268 |     } | 
 | 1269 |     case NUMBER: { | 
 | 1270 | 	PyObject *pynum = parsenumber(STR(ch)); | 
 | 1271 | 	 | 
 | 1272 | 	if (!pynum) | 
 | 1273 | 	    return NULL; | 
 | 1274 | 	 | 
 | 1275 | 	return Num(pynum, LINENO(n)); | 
 | 1276 |     } | 
 | 1277 |     case LPAR: /* some parenthesized expressions */ | 
 | 1278 | 	ch = CHILD(n, 1); | 
 | 1279 | 	 | 
 | 1280 | 	if (TYPE(ch) == RPAR) | 
 | 1281 | 	    return Tuple(NULL, Load, LINENO(n)); | 
 | 1282 | 	 | 
 | 1283 | 	if (TYPE(ch) == yield_expr) | 
 | 1284 | 	    return ast_for_expr(c, ch); | 
 | 1285 | 	 | 
 | 1286 | 	if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for)) | 
 | 1287 | 	    return ast_for_genexp(c, ch); | 
 | 1288 | 	 | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1289 | 	return ast_for_testlist_gexp(c, ch); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1290 |     case LSQB: /* list (or list comprehension) */ | 
 | 1291 | 	ch = CHILD(n, 1); | 
 | 1292 | 	 | 
 | 1293 | 	if (TYPE(ch) == RSQB) | 
 | 1294 | 	    return List(NULL, Load, LINENO(n)); | 
 | 1295 | 	 | 
 | 1296 | 	REQ(ch, listmaker); | 
 | 1297 | 	if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { | 
 | 1298 | 	    asdl_seq *elts = seq_for_testlist(c, ch); | 
 | 1299 | 	     | 
 | 1300 | 	    if (!elts) | 
 | 1301 | 		return NULL; | 
 | 1302 | 	     | 
 | 1303 | 	    return List(elts, Load, LINENO(n)); | 
 | 1304 | 	} | 
 | 1305 | 	else | 
 | 1306 | 	    return ast_for_listcomp(c, ch); | 
 | 1307 |     case LBRACE: { | 
 | 1308 | 	/* dictmaker: test ':' test (',' test ':' test)* [','] */ | 
 | 1309 | 	int i, size; | 
 | 1310 | 	asdl_seq *keys, *values; | 
 | 1311 | 	 | 
 | 1312 | 	ch = CHILD(n, 1); | 
 | 1313 | 	size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */ | 
 | 1314 | 	keys = asdl_seq_new(size); | 
 | 1315 | 	if (!keys) | 
 | 1316 | 	    return NULL; | 
 | 1317 | 	 | 
 | 1318 | 	values = asdl_seq_new(size); | 
 | 1319 | 	if (!values) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1320 | 	    asdl_seq_free(keys); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1321 | 	    return NULL; | 
 | 1322 | 	} | 
 | 1323 | 	 | 
 | 1324 | 	for (i = 0; i < NCH(ch); i += 4) { | 
 | 1325 | 	    expr_ty expression; | 
 | 1326 | 	     | 
 | 1327 | 	    expression = ast_for_expr(c, CHILD(ch, i)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1328 | 	    if (!expression) { | 
 | 1329 | 		asdl_expr_seq_free(keys); | 
 | 1330 | 		asdl_expr_seq_free(values); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1331 | 		return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1332 | 	    } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1333 | 	     | 
 | 1334 | 	    asdl_seq_SET(keys, i / 4, expression); | 
 | 1335 | 	     | 
 | 1336 | 	    expression = ast_for_expr(c, CHILD(ch, i + 2)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1337 | 	    if (!expression) { | 
 | 1338 | 		asdl_expr_seq_free(keys); | 
 | 1339 | 		asdl_expr_seq_free(values); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1340 | 		return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1341 | 	    } | 
 | 1342 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1343 | 	    asdl_seq_SET(values, i / 4, expression); | 
 | 1344 | 	} | 
 | 1345 | 	return Dict(keys, values, LINENO(n)); | 
 | 1346 |     } | 
 | 1347 |     case BACKQUOTE: { /* repr */ | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1348 | 	expr_ty expression = ast_for_testlist(c, CHILD(n, 1)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1349 | 	 | 
 | 1350 | 	if (!expression) | 
 | 1351 | 	    return NULL; | 
 | 1352 | 	 | 
 | 1353 | 	return Repr(expression, LINENO(n)); | 
 | 1354 |     } | 
 | 1355 |     default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1356 | 	PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1357 | 	return NULL; | 
 | 1358 |     } | 
 | 1359 | } | 
 | 1360 |  | 
 | 1361 | static slice_ty | 
 | 1362 | ast_for_slice(struct compiling *c, const node *n) | 
 | 1363 | { | 
 | 1364 |     node *ch; | 
 | 1365 |     expr_ty lower = NULL, upper = NULL, step = NULL; | 
 | 1366 |  | 
 | 1367 |     REQ(n, subscript); | 
 | 1368 |  | 
 | 1369 |     /* | 
 | 1370 |        subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] | 
 | 1371 |        sliceop: ':' [test] | 
 | 1372 |     */ | 
 | 1373 |     ch = CHILD(n, 0); | 
 | 1374 |     if (TYPE(ch) == DOT) | 
 | 1375 | 	return Ellipsis(); | 
 | 1376 |  | 
 | 1377 |     if (NCH(n) == 1 && TYPE(ch) == test) { | 
 | 1378 |         /* 'step' variable hold no significance in terms of being used over | 
 | 1379 |            other vars */ | 
 | 1380 |         step = ast_for_expr(c, ch);  | 
 | 1381 |         if (!step) | 
 | 1382 |             return NULL; | 
 | 1383 |              | 
 | 1384 | 	return Index(step); | 
 | 1385 |     } | 
 | 1386 |  | 
 | 1387 |     if (TYPE(ch) == test) { | 
 | 1388 | 	lower = ast_for_expr(c, ch); | 
 | 1389 |         if (!lower) | 
 | 1390 |             return NULL; | 
 | 1391 |     } | 
 | 1392 |  | 
 | 1393 |     /* If there's an upper bound it's in the second or third position. */ | 
 | 1394 |     if (TYPE(ch) == COLON) { | 
 | 1395 | 	if (NCH(n) > 1) { | 
 | 1396 | 	    node *n2 = CHILD(n, 1); | 
 | 1397 |  | 
 | 1398 | 	    if (TYPE(n2) == test) { | 
 | 1399 | 		upper = ast_for_expr(c, n2); | 
 | 1400 |                 if (!upper) | 
 | 1401 |                     return NULL; | 
 | 1402 |             } | 
 | 1403 | 	} | 
 | 1404 |     } else if (NCH(n) > 2) { | 
 | 1405 | 	node *n2 = CHILD(n, 2); | 
 | 1406 |  | 
 | 1407 | 	if (TYPE(n2) == test) { | 
 | 1408 | 	    upper = ast_for_expr(c, n2); | 
 | 1409 |             if (!upper) | 
 | 1410 |                 return NULL; | 
 | 1411 |         } | 
 | 1412 |     } | 
 | 1413 |  | 
 | 1414 |     ch = CHILD(n, NCH(n) - 1); | 
 | 1415 |     if (TYPE(ch) == sliceop) { | 
 | 1416 | 	if (NCH(ch) == 1) | 
 | 1417 |             /* XXX: If only 1 child, then should just be a colon.  Should we | 
 | 1418 |                just skip assigning and just get to the return? */ | 
 | 1419 | 	    ch = CHILD(ch, 0); | 
 | 1420 | 	else | 
 | 1421 | 	    ch = CHILD(ch, 1); | 
 | 1422 | 	if (TYPE(ch) == test) { | 
 | 1423 | 	    step = ast_for_expr(c, ch); | 
 | 1424 |             if (!step) | 
 | 1425 |                 return NULL; | 
 | 1426 |         } | 
 | 1427 |     } | 
 | 1428 |  | 
 | 1429 |     return Slice(lower, upper, step); | 
 | 1430 | } | 
 | 1431 |  | 
 | 1432 | static expr_ty | 
 | 1433 | ast_for_binop(struct compiling *c, const node *n) | 
 | 1434 | { | 
 | 1435 | 	/* Must account for a sequence of expressions. | 
 | 1436 | 	   How should A op B op C by represented?   | 
 | 1437 | 	   BinOp(BinOp(A, op, B), op, C). | 
 | 1438 | 	*/ | 
 | 1439 |  | 
 | 1440 | 	int i, nops; | 
 | 1441 | 	expr_ty expr1, expr2, result; | 
 | 1442 |         operator_ty operator; | 
 | 1443 |  | 
 | 1444 |         expr1 = ast_for_expr(c, CHILD(n, 0)); | 
 | 1445 |         if (!expr1) | 
 | 1446 |             return NULL; | 
 | 1447 |  | 
 | 1448 |         expr2 = ast_for_expr(c, CHILD(n, 2)); | 
 | 1449 |         if (!expr2) | 
 | 1450 |             return NULL; | 
 | 1451 |  | 
 | 1452 |         operator = get_operator(CHILD(n, 1)); | 
 | 1453 |         if (!operator) | 
 | 1454 |             return NULL; | 
 | 1455 |  | 
 | 1456 | 	result = BinOp(expr1, operator, expr2, LINENO(n)); | 
 | 1457 | 	if (!result) | 
 | 1458 |             return NULL; | 
 | 1459 |  | 
 | 1460 | 	nops = (NCH(n) - 1) / 2; | 
 | 1461 | 	for (i = 1; i < nops; i++) { | 
 | 1462 | 		expr_ty tmp_result, tmp; | 
 | 1463 | 		const node* next_oper = CHILD(n, i * 2 + 1); | 
 | 1464 |  | 
 | 1465 | 		operator = get_operator(next_oper); | 
 | 1466 |                 if (!operator) | 
 | 1467 |                     return NULL; | 
 | 1468 |  | 
 | 1469 |                 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); | 
 | 1470 |                 if (!tmp) | 
 | 1471 |                     return NULL; | 
 | 1472 |  | 
 | 1473 |                 tmp_result = BinOp(result, operator, tmp,  | 
 | 1474 | 				   LINENO(next_oper)); | 
 | 1475 | 		if (!tmp)  | 
 | 1476 | 			return NULL; | 
 | 1477 | 		result = tmp_result; | 
 | 1478 | 	} | 
 | 1479 | 	return result; | 
 | 1480 | } | 
 | 1481 |  | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1482 | static expr_ty | 
 | 1483 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) | 
 | 1484 | { | 
 | 1485 |     /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */ | 
 | 1486 |     expr_ty e; | 
 | 1487 |     REQ(n, trailer); | 
 | 1488 |     if (TYPE(CHILD(n, 0)) == LPAR) { | 
 | 1489 |         if (NCH(n) == 2) | 
 | 1490 |             e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n)); | 
 | 1491 |         else | 
 | 1492 |             e = ast_for_call(c, CHILD(n, 1), left_expr); | 
 | 1493 |     } | 
 | 1494 |     else if (TYPE(CHILD(n, 0)) == LSQB) { | 
 | 1495 |         REQ(CHILD(n, 2), RSQB); | 
 | 1496 |         n = CHILD(n, 1); | 
 | 1497 |         if (NCH(n) <= 2) { | 
 | 1498 |             slice_ty slc = ast_for_slice(c, CHILD(n, 0)); | 
 | 1499 |             if (!slc) | 
 | 1500 |                 return NULL; | 
 | 1501 |             e = Subscript(left_expr, slc, Load, LINENO(n)); | 
 | 1502 |             if (!e) { | 
 | 1503 |                 free_slice(slc); | 
 | 1504 |                 return NULL; | 
 | 1505 |             } | 
 | 1506 |         } | 
 | 1507 |         else { | 
 | 1508 |             int j; | 
 | 1509 |             slice_ty slc; | 
 | 1510 |             asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2); | 
 | 1511 |             if (!slices) | 
 | 1512 |                 return NULL; | 
 | 1513 |             for (j = 0; j < NCH(n); j += 2) { | 
 | 1514 |                 slc = ast_for_slice(c, CHILD(n, j)); | 
 | 1515 |                 if (!slc) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1516 | 		    for (j = j / 2; j >= 0; j--) | 
 | 1517 |                         free_slice(asdl_seq_GET(slices, j)); | 
 | 1518 |                     asdl_seq_free(slices); /* ok */ | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1519 |                     return NULL; | 
 | 1520 |                 } | 
 | 1521 |                 asdl_seq_SET(slices, j / 2, slc); | 
 | 1522 |             } | 
 | 1523 |             e = Subscript(left_expr, ExtSlice(slices), Load, LINENO(n)); | 
 | 1524 |             if (!e) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1525 | 		for (j = 0; j < asdl_seq_LEN(slices); j++) | 
 | 1526 |                     free_slice(asdl_seq_GET(slices, j)); | 
 | 1527 |                 asdl_seq_free(slices); /* ok */ | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1528 |                 return NULL; | 
 | 1529 |             } | 
 | 1530 |         } | 
 | 1531 |     } | 
 | 1532 |     else { | 
 | 1533 |         assert(TYPE(CHILD(n, 0)) == DOT); | 
 | 1534 |         e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n)); | 
 | 1535 |     } | 
 | 1536 |     return e; | 
 | 1537 | } | 
 | 1538 |  | 
 | 1539 | static expr_ty | 
 | 1540 | ast_for_power(struct compiling *c, const node *n) | 
 | 1541 | { | 
 | 1542 |     /* power: atom trailer* ('**' factor)* | 
 | 1543 |      */ | 
 | 1544 |     int i; | 
 | 1545 |     expr_ty e, tmp; | 
 | 1546 |     REQ(n, power); | 
 | 1547 |     e = ast_for_atom(c, CHILD(n, 0)); | 
 | 1548 |     if (!e) | 
 | 1549 |         return NULL; | 
 | 1550 |     if (NCH(n) == 1) | 
 | 1551 |         return e; | 
 | 1552 |     for (i = 1; i < NCH(n); i++) { | 
 | 1553 |         node *ch = CHILD(n, i); | 
 | 1554 |         if (TYPE(ch) != trailer) | 
 | 1555 |             break; | 
 | 1556 |         tmp = ast_for_trailer(c, ch, e); | 
 | 1557 |         if (!tmp) { | 
 | 1558 |             free_expr(e); | 
 | 1559 |             return NULL; | 
 | 1560 |         } | 
 | 1561 |         e = tmp; | 
 | 1562 |     } | 
 | 1563 |     if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { | 
 | 1564 |         expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); | 
 | 1565 |         if (!f) { | 
 | 1566 |             free_expr(e); | 
 | 1567 |             return NULL; | 
 | 1568 |         } | 
 | 1569 |         tmp = BinOp(e, Pow, f, LINENO(n)); | 
 | 1570 |         if (!tmp) { | 
 | 1571 |             free_expr(e); | 
 | 1572 |             return NULL; | 
 | 1573 |         } | 
 | 1574 |         e = tmp; | 
 | 1575 |     } | 
 | 1576 |     return e; | 
 | 1577 | } | 
 | 1578 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1579 | /* Do not name a variable 'expr'!  Will cause a compile error. | 
 | 1580 | */ | 
 | 1581 |  | 
 | 1582 | static expr_ty | 
 | 1583 | ast_for_expr(struct compiling *c, const node *n) | 
 | 1584 | { | 
 | 1585 |     /* handle the full range of simple expressions | 
 | 1586 |        test: and_test ('or' and_test)* | lambdef | 
 | 1587 |        and_test: not_test ('and' not_test)* | 
 | 1588 |        not_test: 'not' not_test | comparison | 
 | 1589 |        comparison: expr (comp_op expr)* | 
 | 1590 |        expr: xor_expr ('|' xor_expr)* | 
 | 1591 |        xor_expr: and_expr ('^' and_expr)* | 
 | 1592 |        and_expr: shift_expr ('&' shift_expr)* | 
 | 1593 |        shift_expr: arith_expr (('<<'|'>>') arith_expr)* | 
 | 1594 |        arith_expr: term (('+'|'-') term)* | 
 | 1595 |        term: factor (('*'|'/'|'%'|'//') factor)* | 
 | 1596 |        factor: ('+'|'-'|'~') factor | power | 
 | 1597 |        power: atom trailer* ('**' factor)* | 
 | 1598 |     */ | 
 | 1599 |  | 
 | 1600 |     asdl_seq *seq; | 
 | 1601 |     int i; | 
 | 1602 |  | 
 | 1603 |  loop: | 
 | 1604 |     switch (TYPE(n)) { | 
 | 1605 |         case test: | 
 | 1606 |             if (TYPE(CHILD(n, 0)) == lambdef) | 
 | 1607 |                 return ast_for_lambdef(c, CHILD(n, 0)); | 
 | 1608 |             /* Fall through to and_test */ | 
 | 1609 |         case and_test: | 
 | 1610 |             if (NCH(n) == 1) { | 
 | 1611 |                 n = CHILD(n, 0); | 
 | 1612 |                 goto loop; | 
 | 1613 |             } | 
 | 1614 |             seq = asdl_seq_new((NCH(n) + 1) / 2); | 
 | 1615 |             if (!seq) | 
 | 1616 |                 return NULL; | 
 | 1617 |             for (i = 0; i < NCH(n); i += 2) { | 
 | 1618 |                 expr_ty e = ast_for_expr(c, CHILD(n, i)); | 
 | 1619 |                 if (!e) | 
 | 1620 |                     return NULL; | 
 | 1621 |                 asdl_seq_SET(seq, i / 2, e); | 
 | 1622 |             } | 
 | 1623 |             if (!strcmp(STR(CHILD(n, 1)), "and")) | 
 | 1624 |                 return BoolOp(And, seq, LINENO(n)); | 
 | 1625 |             else { | 
 | 1626 |                 assert(!strcmp(STR(CHILD(n, 1)), "or")); | 
 | 1627 |                 return BoolOp(Or, seq, LINENO(n)); | 
 | 1628 |             } | 
 | 1629 |             break; | 
 | 1630 |         case not_test: | 
 | 1631 |             if (NCH(n) == 1) { | 
 | 1632 |                 n = CHILD(n, 0); | 
 | 1633 |                 goto loop; | 
 | 1634 |             } | 
 | 1635 |             else { | 
 | 1636 |                 expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 1637 |                 if (!expression) | 
 | 1638 |                     return NULL; | 
 | 1639 |  | 
 | 1640 |                 return UnaryOp(Not, expression, LINENO(n)); | 
 | 1641 |             } | 
 | 1642 |         case comparison: | 
 | 1643 |             if (NCH(n) == 1) { | 
 | 1644 |                 n = CHILD(n, 0); | 
 | 1645 |                 goto loop; | 
 | 1646 |             } | 
 | 1647 |             else { | 
 | 1648 |                 expr_ty expression; | 
 | 1649 |                 asdl_seq *ops, *cmps; | 
 | 1650 |                 ops = asdl_seq_new(NCH(n) / 2); | 
 | 1651 |                 if (!ops) | 
 | 1652 |                     return NULL; | 
 | 1653 |                 cmps = asdl_seq_new(NCH(n) / 2); | 
 | 1654 |                 if (!cmps) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1655 |                     asdl_seq_free(ops); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1656 |                     return NULL; | 
 | 1657 |                 } | 
 | 1658 |                 for (i = 1; i < NCH(n); i += 2) { | 
 | 1659 |                     /* XXX cmpop_ty is just an enum */ | 
 | 1660 |                     cmpop_ty operator; | 
 | 1661 |  | 
 | 1662 |                     operator = ast_for_comp_op(CHILD(n, i)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1663 |                     if (!operator) { | 
 | 1664 | 		        asdl_expr_seq_free(ops); | 
 | 1665 | 		        asdl_expr_seq_free(cmps); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1666 |                         return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1667 | 		    } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1668 |  | 
 | 1669 |                     expression = ast_for_expr(c, CHILD(n, i + 1)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1670 |                     if (!expression) { | 
 | 1671 | 		        asdl_expr_seq_free(ops); | 
 | 1672 | 		        asdl_expr_seq_free(cmps); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1673 |                         return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1674 | 		    } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1675 |                          | 
 | 1676 |                     asdl_seq_SET(ops, i / 2, (void *)operator); | 
 | 1677 |                     asdl_seq_SET(cmps, i / 2, expression); | 
 | 1678 |                 } | 
 | 1679 |                 expression = ast_for_expr(c, CHILD(n, 0)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1680 |                 if (!expression) { | 
 | 1681 | 		    asdl_expr_seq_free(ops); | 
 | 1682 | 		    asdl_expr_seq_free(cmps); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1683 |                     return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1684 | 		} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1685 |                      | 
 | 1686 |                 return Compare(expression, ops, cmps, LINENO(n)); | 
 | 1687 |             } | 
 | 1688 |             break; | 
 | 1689 |  | 
 | 1690 |         /* The next five cases all handle BinOps.  The main body of code | 
 | 1691 |            is the same in each case, but the switch turned inside out to | 
 | 1692 |            reuse the code for each type of operator. | 
 | 1693 |          */ | 
 | 1694 |         case expr: | 
 | 1695 |         case xor_expr: | 
 | 1696 |         case and_expr: | 
 | 1697 |         case shift_expr: | 
 | 1698 |         case arith_expr: | 
 | 1699 |         case term: | 
 | 1700 |             if (NCH(n) == 1) { | 
 | 1701 |                 n = CHILD(n, 0); | 
 | 1702 |                 goto loop; | 
 | 1703 |             } | 
 | 1704 |             return ast_for_binop(c, n); | 
 | 1705 |         case yield_expr: { | 
 | 1706 | 	    expr_ty exp = NULL; | 
 | 1707 | 	    if (NCH(n) == 2) { | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1708 | 		exp = ast_for_testlist(c, CHILD(n, 1)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1709 | 		if (!exp) | 
 | 1710 | 		    return NULL; | 
 | 1711 | 	    } | 
 | 1712 | 	    return Yield(exp, LINENO(n)); | 
 | 1713 | 	} | 
 | 1714 |         case factor: { | 
 | 1715 |             expr_ty expression; | 
 | 1716 |              | 
 | 1717 |             if (NCH(n) == 1) { | 
 | 1718 |                 n = CHILD(n, 0); | 
 | 1719 |                 goto loop; | 
 | 1720 |             } | 
 | 1721 |  | 
 | 1722 |             expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 1723 |             if (!expression) | 
 | 1724 |                 return NULL; | 
 | 1725 |  | 
 | 1726 |             switch (TYPE(CHILD(n, 0))) { | 
 | 1727 |                 case PLUS: | 
 | 1728 |                     return UnaryOp(UAdd, expression, LINENO(n)); | 
 | 1729 |                 case MINUS: | 
 | 1730 |                     return UnaryOp(USub, expression, LINENO(n)); | 
 | 1731 |                 case TILDE: | 
 | 1732 |                     return UnaryOp(Invert, expression, LINENO(n)); | 
 | 1733 |             } | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1734 |             PyErr_Format(PyExc_SystemError, "unhandled factor: %d", | 
 | 1735 | 	    		 TYPE(CHILD(n, 0))); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1736 |             break; | 
 | 1737 |         } | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1738 |         case power: | 
 | 1739 |             return ast_for_power(c, n); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1740 |         default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1741 |             PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1742 |             return NULL; | 
 | 1743 |     } | 
 | 1744 |     /* should never get here */ | 
 | 1745 |     return NULL; | 
 | 1746 | } | 
 | 1747 |  | 
 | 1748 | static expr_ty | 
 | 1749 | ast_for_call(struct compiling *c, const node *n, expr_ty func) | 
 | 1750 | { | 
 | 1751 |     /* | 
 | 1752 |       arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | 
 | 1753 |                | '**' test) | 
 | 1754 |       argument: [test '='] test [gen_for]	 # Really [keyword '='] test | 
 | 1755 |     */ | 
 | 1756 |  | 
 | 1757 |     int i, nargs, nkeywords, ngens; | 
 | 1758 |     asdl_seq *args = NULL; | 
 | 1759 |     asdl_seq *keywords = NULL; | 
 | 1760 |     expr_ty vararg = NULL, kwarg = NULL; | 
 | 1761 |  | 
 | 1762 |     REQ(n, arglist); | 
 | 1763 |  | 
 | 1764 |     nargs = 0; | 
 | 1765 |     nkeywords = 0; | 
 | 1766 |     ngens = 0; | 
 | 1767 |     for (i = 0; i < NCH(n); i++) { | 
 | 1768 | 	node *ch = CHILD(n, i); | 
 | 1769 | 	if (TYPE(ch) == argument) { | 
 | 1770 | 	    if (NCH(ch) == 1) | 
 | 1771 | 		nargs++; | 
 | 1772 | 	    else if (TYPE(CHILD(ch, 1)) == gen_for) | 
 | 1773 | 		ngens++; | 
 | 1774 |             else | 
 | 1775 | 		nkeywords++; | 
 | 1776 | 	} | 
 | 1777 |     } | 
 | 1778 |     if (ngens > 1 || (ngens && (nargs || nkeywords))) { | 
 | 1779 |         ast_error(n, "Generator expression must be parenthesised " | 
 | 1780 | 		  "if not sole argument"); | 
 | 1781 | 	return NULL; | 
 | 1782 |     } | 
 | 1783 |  | 
 | 1784 |     if (nargs + nkeywords + ngens > 255) { | 
 | 1785 |       ast_error(n, "more than 255 arguments"); | 
 | 1786 |       return NULL; | 
 | 1787 |     } | 
 | 1788 |  | 
 | 1789 |     args = asdl_seq_new(nargs + ngens); | 
 | 1790 |     if (!args) | 
 | 1791 |         goto error; | 
 | 1792 |     keywords = asdl_seq_new(nkeywords); | 
 | 1793 |     if (!keywords) | 
 | 1794 |         goto error; | 
 | 1795 |     nargs = 0; | 
 | 1796 |     nkeywords = 0; | 
 | 1797 |     for (i = 0; i < NCH(n); i++) { | 
 | 1798 | 	node *ch = CHILD(n, i); | 
 | 1799 | 	if (TYPE(ch) == argument) { | 
 | 1800 | 	    expr_ty e; | 
 | 1801 | 	    if (NCH(ch) == 1) { | 
 | 1802 | 		e = ast_for_expr(c, CHILD(ch, 0)); | 
 | 1803 |                 if (!e) | 
 | 1804 |                     goto error; | 
 | 1805 | 		asdl_seq_SET(args, nargs++, e); | 
 | 1806 | 	    }   | 
 | 1807 | 	    else if (TYPE(CHILD(ch, 1)) == gen_for) { | 
 | 1808 |         	e = ast_for_genexp(c, ch); | 
 | 1809 |                 if (!e) | 
 | 1810 |                     goto error; | 
 | 1811 | 		asdl_seq_SET(args, nargs++, e); | 
 | 1812 |             } | 
 | 1813 | 	    else { | 
 | 1814 | 		keyword_ty kw; | 
 | 1815 | 		identifier key; | 
 | 1816 |  | 
 | 1817 | 		/* CHILD(ch, 0) is test, but must be an identifier? */  | 
 | 1818 | 		e = ast_for_expr(c, CHILD(ch, 0)); | 
 | 1819 |                 if (!e) | 
 | 1820 |                     goto error; | 
 | 1821 |                 /* f(lambda x: x[0] = 3) ends up getting parsed with | 
 | 1822 |                  * LHS test = lambda x: x[0], and RHS test = 3. | 
 | 1823 |                  * SF bug 132313 points out that complaining about a keyword | 
 | 1824 |                  * then is very confusing. | 
 | 1825 |                  */ | 
 | 1826 |                 if (e->kind == Lambda_kind) { | 
 | 1827 |                   ast_error(CHILD(ch, 0), "lambda cannot contain assignment"); | 
 | 1828 |                   goto error; | 
 | 1829 |                 } else if (e->kind != Name_kind) { | 
 | 1830 |                   ast_error(CHILD(ch, 0), "keyword can't be an expression"); | 
 | 1831 |                   goto error; | 
 | 1832 |                 } | 
 | 1833 | 		key = e->v.Name.id; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1834 | 		free(e); /* XXX: is free correct here? */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1835 | 		e = ast_for_expr(c, CHILD(ch, 2)); | 
 | 1836 |                 if (!e) | 
 | 1837 |                     goto error; | 
 | 1838 | 		kw = keyword(key, e); | 
 | 1839 |                 if (!kw) | 
 | 1840 |                     goto error; | 
 | 1841 | 		asdl_seq_SET(keywords, nkeywords++, kw); | 
 | 1842 | 	    } | 
 | 1843 | 	} | 
 | 1844 | 	else if (TYPE(ch) == STAR) { | 
 | 1845 | 	    vararg = ast_for_expr(c, CHILD(n, i+1)); | 
 | 1846 | 	    i++; | 
 | 1847 | 	} | 
 | 1848 | 	else if (TYPE(ch) == DOUBLESTAR) { | 
 | 1849 | 	    kwarg = ast_for_expr(c, CHILD(n, i+1)); | 
 | 1850 | 	    i++; | 
 | 1851 | 	} | 
 | 1852 |     } | 
 | 1853 |  | 
 | 1854 |     return Call(func, args, keywords, vararg, kwarg, LINENO(n)); | 
 | 1855 |  | 
 | 1856 |  error: | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1857 |     free_expr(vararg); | 
 | 1858 |     free_expr(kwarg); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1859 |     if (args) | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1860 |         asdl_expr_seq_free(args); | 
 | 1861 |     if (keywords) { | 
 | 1862 | 	for (i = 0; i < asdl_seq_LEN(keywords); i++) | 
 | 1863 |             free_keyword(asdl_seq_GET(keywords, i)); | 
 | 1864 |         asdl_seq_free(keywords); /* ok */ | 
 | 1865 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1866 |     return NULL; | 
 | 1867 | } | 
 | 1868 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1869 | static expr_ty | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1870 | ast_for_testlist(struct compiling *c, const node* n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1871 | { | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1872 |     /* testlist_gexp: test (',' test)* [','] */ | 
 | 1873 |     /* testlist: test (',' test)* [','] */ | 
 | 1874 |     /* testlist_safe: test (',' test)+ [','] */ | 
 | 1875 |     /* testlist1: test (',' test)* */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1876 |     assert(NCH(n) > 0); | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1877 |     if (TYPE(n) == testlist_gexp) { | 
 | 1878 |         if (NCH(n) > 1) | 
 | 1879 |             assert(TYPE(CHILD(n, 1)) != gen_for); | 
 | 1880 |     } | 
 | 1881 |     else { | 
 | 1882 |         assert(TYPE(n) == testlist || | 
 | 1883 |                TYPE(n) == testlist_safe || | 
 | 1884 |                TYPE(n) == testlist1); | 
 | 1885 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1886 |     if (NCH(n) == 1) | 
 | 1887 | 	return ast_for_expr(c, CHILD(n, 0)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1888 |     else { | 
 | 1889 |         asdl_seq *tmp = seq_for_testlist(c, n); | 
 | 1890 |         if (!tmp) | 
 | 1891 |             return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1892 | 	return Tuple(tmp, Load, LINENO(n)); | 
 | 1893 |     } | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1894 | } | 
 | 1895 |  | 
 | 1896 | static expr_ty | 
 | 1897 | ast_for_testlist_gexp(struct compiling *c, const node* n) | 
 | 1898 | { | 
 | 1899 |     /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */ | 
 | 1900 |     /* argument: test [ gen_for ] */ | 
 | 1901 |     assert(TYPE(n) == testlist_gexp || TYPE(n) == argument); | 
 | 1902 |     if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) { | 
 | 1903 | 	return ast_for_genexp(c, n); | 
 | 1904 |     } | 
 | 1905 |     else | 
 | 1906 |         return ast_for_testlist(c, n); | 
 | 1907 | } | 
 | 1908 |  | 
 | 1909 | /* like ast_for_testlist() but returns a sequence */ | 
 | 1910 | static asdl_seq* | 
 | 1911 | ast_for_class_bases(struct compiling *c, const node* n) | 
 | 1912 | { | 
 | 1913 |     /* testlist: test (',' test)* [','] */ | 
 | 1914 |     assert(NCH(n) > 0); | 
 | 1915 |     REQ(n, testlist); | 
 | 1916 |     if (NCH(n) == 1) { | 
 | 1917 |         expr_ty base; | 
 | 1918 |         asdl_seq *bases = asdl_seq_new(1); | 
 | 1919 |         if (!bases) | 
 | 1920 |             return NULL; | 
 | 1921 |         base = ast_for_expr(c, CHILD(n, 0)); | 
 | 1922 |         if (!base) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 1923 |             asdl_seq_free(bases); /* ok */ | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1924 |             return NULL; | 
 | 1925 |         } | 
 | 1926 |         asdl_seq_SET(bases, 0, base); | 
 | 1927 |         return bases; | 
 | 1928 |     } | 
 | 1929 |     else { | 
 | 1930 |         return seq_for_testlist(c, n); | 
 | 1931 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1932 | } | 
 | 1933 |  | 
 | 1934 | static stmt_ty | 
 | 1935 | ast_for_expr_stmt(struct compiling *c, const node *n) | 
 | 1936 | { | 
 | 1937 |     REQ(n, expr_stmt); | 
 | 1938 |     /* expr_stmt: testlist (augassign (yield_expr|testlist)  | 
 | 1939 |                 | ('=' (yield_expr|testlist))*) | 
 | 1940 |        testlist: test (',' test)* [','] | 
 | 1941 |        augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | 
 | 1942 | 	        | '<<=' | '>>=' | '**=' | '//=' | 
 | 1943 |        test: ... here starts the operator precendence dance | 
 | 1944 |      */ | 
 | 1945 |  | 
 | 1946 |     if (NCH(n) == 1) { | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1947 | 	expr_ty e = ast_for_testlist(c, CHILD(n, 0)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1948 |         if (!e) | 
 | 1949 |             return NULL; | 
 | 1950 |  | 
 | 1951 | 	return Expr(e, LINENO(n)); | 
 | 1952 |     } | 
 | 1953 |     else if (TYPE(CHILD(n, 1)) == augassign) { | 
 | 1954 |         expr_ty expr1, expr2; | 
 | 1955 |         operator_ty operator; | 
 | 1956 | 	node *ch = CHILD(n, 0); | 
 | 1957 |  | 
 | 1958 | 	if (TYPE(ch) == testlist) | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1959 | 	    expr1 = ast_for_testlist(c, ch); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1960 | 	else | 
 | 1961 | 	    expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch)); | 
 | 1962 |  | 
 | 1963 |         if (!expr1) | 
 | 1964 |             return NULL; | 
 | 1965 |         if (expr1->kind == GeneratorExp_kind) { | 
| Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame] | 1966 | 	    free_expr(expr1); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1967 | 	    ast_error(ch, "augmented assignment to generator " | 
 | 1968 | 		      "expression not possible"); | 
 | 1969 | 	    return NULL; | 
 | 1970 |         } | 
 | 1971 | 	if (expr1->kind == Name_kind) { | 
 | 1972 | 		char *var_name = PyString_AS_STRING(expr1->v.Name.id); | 
 | 1973 | 		if (var_name[0] == 'N' && !strcmp(var_name, "None")) { | 
| Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame] | 1974 | 			free_expr(expr1); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1975 | 			ast_error(ch, "assignment to None"); | 
 | 1976 | 			return NULL; | 
 | 1977 | 		} | 
 | 1978 | 	} | 
 | 1979 |  | 
 | 1980 | 	ch = CHILD(n, 2); | 
 | 1981 | 	if (TYPE(ch) == testlist) | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1982 | 	    expr2 = ast_for_testlist(c, ch); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1983 | 	else | 
 | 1984 | 	    expr2 = Yield(ast_for_expr(c, ch), LINENO(ch)); | 
| Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame] | 1985 |         if (!expr2) { | 
 | 1986 |             free_expr(expr1); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1987 |             return NULL; | 
| Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame] | 1988 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1989 |  | 
 | 1990 |         operator = ast_for_augassign(CHILD(n, 1)); | 
| Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame] | 1991 |         if (!operator) { | 
 | 1992 |             free_expr(expr1); | 
 | 1993 |             free_expr(expr2); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1994 |             return NULL; | 
| Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame] | 1995 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1996 |  | 
 | 1997 | 	return AugAssign(expr1, operator, expr2, LINENO(n)); | 
 | 1998 |     } | 
 | 1999 |     else { | 
 | 2000 | 	int i; | 
 | 2001 | 	asdl_seq *targets; | 
 | 2002 | 	node *value; | 
 | 2003 |         expr_ty expression; | 
 | 2004 |  | 
 | 2005 | 	/* a normal assignment */ | 
 | 2006 | 	REQ(CHILD(n, 1), EQUAL); | 
 | 2007 | 	targets = asdl_seq_new(NCH(n) / 2); | 
 | 2008 | 	if (!targets) | 
 | 2009 | 	    return NULL; | 
 | 2010 | 	for (i = 0; i < NCH(n) - 2; i += 2) { | 
| Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 2011 | 	    expr_ty e; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2012 | 	    node *ch = CHILD(n, i); | 
 | 2013 | 	    if (TYPE(ch) == yield_expr) { | 
 | 2014 | 		ast_error(ch, "assignment to yield expression not possible"); | 
 | 2015 | 		goto error; | 
 | 2016 | 	    } | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2017 | 	    e = ast_for_testlist(c, ch); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2018 |  | 
 | 2019 | 	    /* set context to assign */ | 
 | 2020 | 	    if (!e)  | 
 | 2021 | 	      goto error; | 
 | 2022 |  | 
 | 2023 | 	    if (!set_context(e, Store, CHILD(n, i))) { | 
 | 2024 |               free_expr(e); | 
 | 2025 | 	      goto error; | 
 | 2026 |             } | 
 | 2027 |  | 
 | 2028 | 	    asdl_seq_SET(targets, i / 2, e); | 
 | 2029 | 	} | 
 | 2030 | 	value = CHILD(n, NCH(n) - 1); | 
 | 2031 | 	if (TYPE(value) == testlist) | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2032 | 	    expression = ast_for_testlist(c, value); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2033 | 	else | 
 | 2034 | 	    expression = ast_for_expr(c, value); | 
 | 2035 | 	if (!expression) | 
| Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame] | 2036 | 	    goto error; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2037 | 	return Assign(targets, expression, LINENO(n)); | 
 | 2038 |     error: | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2039 |         asdl_expr_seq_free(targets); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2040 |     } | 
 | 2041 |     return NULL; | 
 | 2042 | } | 
 | 2043 |  | 
 | 2044 | static stmt_ty | 
 | 2045 | ast_for_print_stmt(struct compiling *c, const node *n) | 
 | 2046 | { | 
 | 2047 |     /* print_stmt: 'print' ( [ test (',' test)* [','] ] | 
 | 2048 |                              | '>>' test [ (',' test)+ [','] ] ) | 
 | 2049 |      */ | 
 | 2050 |     expr_ty dest = NULL, expression; | 
 | 2051 |     asdl_seq *seq; | 
 | 2052 |     bool nl; | 
 | 2053 |     int i, start = 1; | 
 | 2054 |  | 
 | 2055 |     REQ(n, print_stmt); | 
 | 2056 |     if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { | 
 | 2057 | 	dest = ast_for_expr(c, CHILD(n, 2)); | 
 | 2058 |         if (!dest) | 
 | 2059 |             return NULL; | 
 | 2060 | 	start = 4; | 
 | 2061 |     } | 
 | 2062 |     seq = asdl_seq_new((NCH(n) + 1 - start) / 2); | 
 | 2063 |     if (!seq) | 
 | 2064 | 	return NULL; | 
 | 2065 |     for (i = start; i < NCH(n); i += 2) { | 
 | 2066 |         expression = ast_for_expr(c, CHILD(n, i)); | 
 | 2067 |         if (!expression) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2068 | 	    free_expr(dest); | 
 | 2069 | 	    asdl_expr_seq_free(seq); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2070 |             return NULL; | 
 | 2071 | 	} | 
 | 2072 |  | 
 | 2073 | 	asdl_seq_APPEND(seq, expression); | 
 | 2074 |     } | 
 | 2075 |     nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true; | 
 | 2076 |     return Print(dest, seq, nl, LINENO(n)); | 
 | 2077 | } | 
 | 2078 |  | 
 | 2079 | static asdl_seq * | 
 | 2080 | ast_for_exprlist(struct compiling *c, const node *n, int context) | 
 | 2081 | { | 
 | 2082 |     asdl_seq *seq; | 
 | 2083 |     int i; | 
 | 2084 |     expr_ty e; | 
 | 2085 |  | 
 | 2086 |     REQ(n, exprlist); | 
 | 2087 |  | 
 | 2088 |     seq = asdl_seq_new((NCH(n) + 1) / 2); | 
 | 2089 |     if (!seq) | 
 | 2090 | 	return NULL; | 
 | 2091 |     for (i = 0; i < NCH(n); i += 2) { | 
 | 2092 | 	e = ast_for_expr(c, CHILD(n, i)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2093 | 	if (!e) | 
 | 2094 | 	    goto error; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2095 | 	if (context) { | 
 | 2096 | 	    if (!set_context(e, context, CHILD(n, i))) | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2097 | 	    	goto error; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2098 |         } | 
 | 2099 | 	asdl_seq_SET(seq, i / 2, e); | 
 | 2100 |     } | 
 | 2101 |     return seq; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2102 |  | 
 | 2103 | error: | 
 | 2104 |     asdl_expr_seq_free(seq); | 
 | 2105 |     return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2106 | } | 
 | 2107 |  | 
 | 2108 | static stmt_ty | 
 | 2109 | ast_for_del_stmt(struct compiling *c, const node *n) | 
 | 2110 | { | 
 | 2111 |     asdl_seq *expr_list; | 
 | 2112 |      | 
 | 2113 |     /* del_stmt: 'del' exprlist */ | 
 | 2114 |     REQ(n, del_stmt); | 
 | 2115 |  | 
 | 2116 |     expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); | 
 | 2117 |     if (!expr_list) | 
 | 2118 |         return NULL; | 
 | 2119 |     return Delete(expr_list, LINENO(n)); | 
 | 2120 | } | 
 | 2121 |  | 
 | 2122 | static stmt_ty | 
 | 2123 | ast_for_flow_stmt(struct compiling *c, const node *n) | 
 | 2124 | { | 
 | 2125 |     /* | 
 | 2126 |       flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | 
 | 2127 |                  | yield_stmt | 
 | 2128 |       break_stmt: 'break' | 
 | 2129 |       continue_stmt: 'continue' | 
 | 2130 |       return_stmt: 'return' [testlist] | 
 | 2131 |       yield_stmt: yield_expr | 
 | 2132 |       yield_expr: 'yield' testlist | 
 | 2133 |       raise_stmt: 'raise' [test [',' test [',' test]]] | 
 | 2134 |     */ | 
 | 2135 |     node *ch; | 
 | 2136 |  | 
 | 2137 |     REQ(n, flow_stmt); | 
 | 2138 |     ch = CHILD(n, 0); | 
 | 2139 |     switch (TYPE(ch)) { | 
 | 2140 |         case break_stmt: | 
 | 2141 |             return Break(LINENO(n)); | 
 | 2142 |         case continue_stmt: | 
 | 2143 |             return Continue(LINENO(n)); | 
 | 2144 |         case yield_stmt: { /* will reduce to yield_expr */ | 
 | 2145 | 	    expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); | 
 | 2146 | 	    if (!exp) | 
 | 2147 | 		return NULL; | 
 | 2148 |             return Expr(exp, LINENO(n)); | 
 | 2149 |         } | 
 | 2150 |         case return_stmt: | 
 | 2151 |             if (NCH(ch) == 1) | 
 | 2152 |                 return Return(NULL, LINENO(n)); | 
 | 2153 |             else { | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2154 |                 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2155 |                 if (!expression) | 
 | 2156 |                     return NULL; | 
 | 2157 |                 return Return(expression, LINENO(n)); | 
 | 2158 |             } | 
 | 2159 |         case raise_stmt: | 
 | 2160 |             if (NCH(ch) == 1) | 
 | 2161 |                 return Raise(NULL, NULL, NULL, LINENO(n)); | 
 | 2162 |             else if (NCH(ch) == 2) { | 
 | 2163 |                 expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); | 
 | 2164 |                 if (!expression) | 
 | 2165 |                     return NULL; | 
 | 2166 |                 return Raise(expression, NULL, NULL, LINENO(n)); | 
 | 2167 |             } | 
 | 2168 |             else if (NCH(ch) == 4) { | 
 | 2169 |                 expr_ty expr1, expr2; | 
 | 2170 |  | 
 | 2171 |                 expr1 = ast_for_expr(c, CHILD(ch, 1)); | 
 | 2172 |                 if (!expr1) | 
 | 2173 |                     return NULL; | 
 | 2174 |                 expr2 = ast_for_expr(c, CHILD(ch, 3)); | 
 | 2175 |                 if (!expr2) | 
 | 2176 |                     return NULL; | 
 | 2177 |  | 
 | 2178 |                 return Raise(expr1, expr2, NULL, LINENO(n)); | 
 | 2179 |             } | 
 | 2180 |             else if (NCH(ch) == 6) { | 
 | 2181 |                 expr_ty expr1, expr2, expr3; | 
 | 2182 |  | 
 | 2183 |                 expr1 = ast_for_expr(c, CHILD(ch, 1)); | 
 | 2184 |                 if (!expr1) | 
 | 2185 |                     return NULL; | 
 | 2186 |                 expr2 = ast_for_expr(c, CHILD(ch, 3)); | 
 | 2187 |                 if (!expr2) | 
 | 2188 |                     return NULL; | 
 | 2189 |                 expr3 = ast_for_expr(c, CHILD(ch, 5)); | 
 | 2190 |                 if (!expr3) | 
 | 2191 |                     return NULL; | 
 | 2192 |                      | 
 | 2193 |                 return Raise(expr1, expr2, expr3, LINENO(n)); | 
 | 2194 |             } | 
 | 2195 |         default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2196 |             PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2197 |                          "unexpected flow_stmt: %d", TYPE(ch)); | 
 | 2198 |             return NULL; | 
 | 2199 |     } | 
 | 2200 | } | 
 | 2201 |  | 
 | 2202 | static alias_ty | 
 | 2203 | alias_for_import_name(const node *n) | 
 | 2204 | { | 
 | 2205 |     /* | 
 | 2206 |       import_as_name: NAME [NAME NAME] | 
 | 2207 |       dotted_as_name: dotted_name [NAME NAME] | 
 | 2208 |       dotted_name: NAME ('.' NAME)* | 
 | 2209 |     */ | 
 | 2210 |  loop: | 
 | 2211 |     switch (TYPE(n)) { | 
 | 2212 |         case import_as_name: | 
 | 2213 |             if (NCH(n) == 3) | 
 | 2214 |                 return alias(NEW_IDENTIFIER(CHILD(n, 0)), | 
 | 2215 |                              NEW_IDENTIFIER(CHILD(n, 2))); | 
 | 2216 |             else | 
 | 2217 |                 return alias(NEW_IDENTIFIER(CHILD(n, 0)), | 
 | 2218 |                              NULL); | 
 | 2219 |             break; | 
 | 2220 |         case dotted_as_name: | 
 | 2221 |             if (NCH(n) == 1) { | 
 | 2222 |                 n = CHILD(n, 0); | 
 | 2223 |                 goto loop; | 
 | 2224 |             } | 
 | 2225 |             else { | 
 | 2226 |                 alias_ty a = alias_for_import_name(CHILD(n, 0)); | 
 | 2227 |                 assert(!a->asname); | 
 | 2228 |                 a->asname = NEW_IDENTIFIER(CHILD(n, 2)); | 
 | 2229 |                 return a; | 
 | 2230 |             } | 
 | 2231 |             break; | 
 | 2232 |         case dotted_name: | 
 | 2233 |             if (NCH(n) == 1) | 
 | 2234 |                 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL); | 
 | 2235 |             else { | 
 | 2236 |                 /* Create a string of the form "a.b.c" */ | 
 | 2237 |                 int i, len; | 
 | 2238 |                 PyObject *str; | 
 | 2239 |                 char *s; | 
 | 2240 |  | 
 | 2241 |                 len = 0; | 
 | 2242 |                 for (i = 0; i < NCH(n); i += 2) | 
 | 2243 |                     /* length of string plus one for the dot */ | 
 | 2244 |                     len += strlen(STR(CHILD(n, i))) + 1; | 
 | 2245 |                 len--; /* the last name doesn't have a dot */ | 
 | 2246 |                 str = PyString_FromStringAndSize(NULL, len); | 
 | 2247 |                 if (!str) | 
 | 2248 |                     return NULL; | 
 | 2249 |                 s = PyString_AS_STRING(str); | 
 | 2250 |                 if (!s) | 
 | 2251 |                     return NULL; | 
 | 2252 |                 for (i = 0; i < NCH(n); i += 2) { | 
 | 2253 |                     char *sch = STR(CHILD(n, i)); | 
 | 2254 |                     strcpy(s, STR(CHILD(n, i))); | 
 | 2255 |                     s += strlen(sch); | 
 | 2256 |                     *s++ = '.'; | 
 | 2257 |                 } | 
 | 2258 |                 --s; | 
 | 2259 |                 *s = '\0'; | 
 | 2260 |                 PyString_InternInPlace(&str); | 
 | 2261 |                 return alias(str, NULL); | 
 | 2262 |             } | 
 | 2263 |             break; | 
 | 2264 |         case STAR: | 
 | 2265 |             return alias(PyString_InternFromString("*"), NULL); | 
 | 2266 |         default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2267 |             PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2268 |                          "unexpected import name: %d", TYPE(n)); | 
 | 2269 |             return NULL; | 
 | 2270 |     } | 
 | 2271 |     return NULL; | 
 | 2272 | } | 
 | 2273 |  | 
 | 2274 | static stmt_ty | 
 | 2275 | ast_for_import_stmt(struct compiling *c, const node *n) | 
 | 2276 | { | 
 | 2277 |     /* | 
 | 2278 |       import_stmt: import_name | import_from | 
 | 2279 |       import_name: 'import' dotted_as_names | 
 | 2280 |       import_from: 'from' dotted_name 'import' ('*' |  | 
 | 2281 |                                                 '(' import_as_names ')' |  | 
 | 2282 |                                                 import_as_names) | 
 | 2283 |     */ | 
 | 2284 |     int i; | 
 | 2285 |     asdl_seq *aliases; | 
 | 2286 |  | 
 | 2287 |     REQ(n, import_stmt); | 
 | 2288 |     n = CHILD(n, 0); | 
 | 2289 |     if (STR(CHILD(n, 0))[0] == 'i') { /* import */ | 
 | 2290 |         n = CHILD(n, 1); | 
| Neil Schemenauer | 147b759 | 2005-10-23 03:38:19 +0000 | [diff] [blame] | 2291 | 	REQ(n, dotted_as_names); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2292 | 	aliases = asdl_seq_new((NCH(n) + 1) / 2); | 
 | 2293 | 	if (!aliases) | 
 | 2294 | 		return NULL; | 
 | 2295 | 	for (i = 0; i < NCH(n); i += 2) { | 
 | 2296 |             alias_ty import_alias = alias_for_import_name(CHILD(n, i)); | 
 | 2297 |             if (!import_alias) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2298 |                 asdl_alias_seq_free(aliases); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2299 |                 return NULL; | 
 | 2300 |             } | 
 | 2301 | 	    asdl_seq_SET(aliases, i / 2, import_alias); | 
 | 2302 |         } | 
 | 2303 | 	return Import(aliases, LINENO(n)); | 
 | 2304 |     } | 
 | 2305 |     else if (STR(CHILD(n, 0))[0] == 'f') { /* from */ | 
 | 2306 | 	stmt_ty import; | 
 | 2307 |         int n_children; | 
 | 2308 |         const char *from_modules; | 
 | 2309 | 	int lineno = LINENO(n); | 
 | 2310 | 	alias_ty mod = alias_for_import_name(CHILD(n, 1)); | 
 | 2311 | 	if (!mod) | 
 | 2312 |             return NULL; | 
 | 2313 |  | 
 | 2314 |         /* XXX this needs to be cleaned up */ | 
 | 2315 |  | 
 | 2316 |         from_modules = STR(CHILD(n, 3)); | 
 | 2317 |         if (!from_modules) { | 
 | 2318 |             n = CHILD(n, 3);                  /* from ... import x, y, z */ | 
 | 2319 |             if (NCH(n) % 2 == 0) { | 
 | 2320 |                 /* it ends with a comma, not valid but the parser allows it */ | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2321 | 		free_alias(mod); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2322 |                 ast_error(n, "trailing comma not allowed without" | 
 | 2323 |                              " surrounding parentheses"); | 
 | 2324 |                 return NULL; | 
 | 2325 |             } | 
 | 2326 |         } | 
 | 2327 |         else if (from_modules[0] == '*') { | 
 | 2328 |             n = CHILD(n, 3); /* from ... import * */ | 
 | 2329 |         } | 
 | 2330 |         else if (from_modules[0] == '(') | 
 | 2331 |             n = CHILD(n, 4);                  /* from ... import (x, y, z) */ | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2332 |         else { | 
 | 2333 | 	    /* XXX: don't we need to call ast_error(n, "..."); */ | 
 | 2334 | 	    free_alias(mod); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2335 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2336 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2337 |  | 
 | 2338 |         n_children = NCH(n); | 
 | 2339 |         if (from_modules && from_modules[0] == '*') | 
 | 2340 |             n_children = 1; | 
 | 2341 |  | 
 | 2342 | 	aliases = asdl_seq_new((n_children + 1) / 2); | 
 | 2343 | 	if (!aliases) { | 
 | 2344 |             free_alias(mod); | 
 | 2345 |             return NULL; | 
 | 2346 | 	} | 
 | 2347 |  | 
 | 2348 |         /* handle "from ... import *" special b/c there's no children */ | 
 | 2349 |         if (from_modules && from_modules[0] == '*') { | 
 | 2350 |             alias_ty import_alias = alias_for_import_name(n); | 
 | 2351 |             if (!import_alias) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2352 |                 asdl_alias_seq_free(aliases); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2353 |                 free_alias(mod); | 
 | 2354 |                 return NULL; | 
 | 2355 |             } | 
 | 2356 | 	    asdl_seq_APPEND(aliases, import_alias); | 
 | 2357 |         } | 
 | 2358 |  | 
 | 2359 | 	for (i = 0; i < NCH(n); i += 2) { | 
 | 2360 |             alias_ty import_alias = alias_for_import_name(CHILD(n, i)); | 
 | 2361 |             if (!import_alias) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2362 |                 asdl_alias_seq_free(aliases); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2363 |                 free_alias(mod); | 
 | 2364 |                 return NULL; | 
 | 2365 |             } | 
 | 2366 | 	    asdl_seq_APPEND(aliases, import_alias); | 
 | 2367 |         } | 
 | 2368 |         Py_INCREF(mod->name); | 
 | 2369 | 	import = ImportFrom(mod->name, aliases, lineno); | 
 | 2370 | 	free_alias(mod); | 
 | 2371 | 	return import; | 
 | 2372 |     } | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2373 |     PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2374 |                  "unknown import statement: starts with command '%s'", | 
 | 2375 |                  STR(CHILD(n, 0))); | 
 | 2376 |     return NULL; | 
 | 2377 | } | 
 | 2378 |  | 
 | 2379 | static stmt_ty | 
 | 2380 | ast_for_global_stmt(struct compiling *c, const node *n) | 
 | 2381 | { | 
 | 2382 |     /* global_stmt: 'global' NAME (',' NAME)* */ | 
 | 2383 |     identifier name; | 
 | 2384 |     asdl_seq *s; | 
 | 2385 |     int i; | 
 | 2386 |  | 
 | 2387 |     REQ(n, global_stmt); | 
 | 2388 |     s = asdl_seq_new(NCH(n) / 2); | 
 | 2389 |     if (!s) | 
 | 2390 |     	return NULL; | 
 | 2391 |     for (i = 1; i < NCH(n); i += 2) { | 
 | 2392 | 	name = NEW_IDENTIFIER(CHILD(n, i)); | 
 | 2393 | 	if (!name) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2394 | 	    for (i = i / 2; i > 0; i--) | 
 | 2395 |                 Py_XDECREF((identifier) asdl_seq_GET(s, i)); | 
 | 2396 | 	    asdl_seq_free(s); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2397 | 	    return NULL; | 
 | 2398 | 	} | 
 | 2399 | 	asdl_seq_SET(s, i / 2, name); | 
 | 2400 |     } | 
 | 2401 |     return Global(s, LINENO(n)); | 
 | 2402 | } | 
 | 2403 |  | 
 | 2404 | static stmt_ty | 
 | 2405 | ast_for_exec_stmt(struct compiling *c, const node *n) | 
 | 2406 | { | 
 | 2407 |     expr_ty expr1, globals = NULL, locals = NULL; | 
 | 2408 |     int n_children = NCH(n); | 
 | 2409 |     if (n_children != 2 && n_children != 4 && n_children != 6) { | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2410 |         PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2411 |                      "poorly formed 'exec' statement: %d parts to statement", | 
 | 2412 |                      n_children); | 
 | 2413 |         return NULL; | 
 | 2414 |     } | 
 | 2415 |  | 
 | 2416 |     /* exec_stmt: 'exec' expr ['in' test [',' test]] */ | 
 | 2417 |     REQ(n, exec_stmt); | 
 | 2418 |     expr1 = ast_for_expr(c, CHILD(n, 1)); | 
 | 2419 |     if (!expr1) | 
 | 2420 |         return NULL; | 
 | 2421 |     if (n_children >= 4) { | 
 | 2422 |         globals = ast_for_expr(c, CHILD(n, 3)); | 
 | 2423 |         if (!globals) | 
 | 2424 |             return NULL; | 
 | 2425 |     } | 
 | 2426 |     if (n_children == 6) { | 
 | 2427 |         locals = ast_for_expr(c, CHILD(n, 5)); | 
 | 2428 |         if (!locals) | 
 | 2429 |             return NULL; | 
 | 2430 |     } | 
 | 2431 |  | 
 | 2432 |     return Exec(expr1, globals, locals, LINENO(n)); | 
 | 2433 | } | 
 | 2434 |  | 
 | 2435 | static stmt_ty | 
 | 2436 | ast_for_assert_stmt(struct compiling *c, const node *n) | 
 | 2437 | { | 
 | 2438 |     /* assert_stmt: 'assert' test [',' test] */ | 
 | 2439 |     REQ(n, assert_stmt); | 
 | 2440 |     if (NCH(n) == 2) { | 
 | 2441 |         expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 2442 |         if (!expression) | 
 | 2443 |             return NULL; | 
 | 2444 | 	return Assert(expression, NULL, LINENO(n)); | 
 | 2445 |     } | 
 | 2446 |     else if (NCH(n) == 4) { | 
 | 2447 |         expr_ty expr1, expr2; | 
 | 2448 |  | 
 | 2449 |         expr1 = ast_for_expr(c, CHILD(n, 1)); | 
 | 2450 |         if (!expr1) | 
 | 2451 |             return NULL; | 
 | 2452 |         expr2 = ast_for_expr(c, CHILD(n, 3)); | 
 | 2453 |         if (!expr2) | 
 | 2454 |             return NULL; | 
 | 2455 |              | 
 | 2456 | 	return Assert(expr1, expr2, LINENO(n)); | 
 | 2457 |     } | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2458 |     PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2459 |                  "improper number of parts to 'assert' statement: %d", | 
 | 2460 |                  NCH(n)); | 
 | 2461 |     return NULL; | 
 | 2462 | } | 
 | 2463 |  | 
 | 2464 | static asdl_seq * | 
 | 2465 | ast_for_suite(struct compiling *c, const node *n) | 
 | 2466 | { | 
 | 2467 |     /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ | 
 | 2468 |     asdl_seq *seq = NULL; | 
 | 2469 |     stmt_ty s; | 
 | 2470 |     int i, total, num, end, pos = 0; | 
 | 2471 |     node *ch; | 
 | 2472 |  | 
 | 2473 |     REQ(n, suite); | 
 | 2474 |  | 
 | 2475 |     total = num_stmts(n); | 
 | 2476 |     seq = asdl_seq_new(total); | 
 | 2477 |     if (!seq) | 
 | 2478 |     	return NULL; | 
 | 2479 |     if (TYPE(CHILD(n, 0)) == simple_stmt) { | 
 | 2480 | 	n = CHILD(n, 0); | 
 | 2481 | 	/* simple_stmt always ends with a NEWLINE, | 
 | 2482 | 	   and may have a trailing SEMI  | 
 | 2483 | 	*/ | 
 | 2484 | 	end = NCH(n) - 1; | 
 | 2485 | 	if (TYPE(CHILD(n, end - 1)) == SEMI) | 
 | 2486 | 	    end--; | 
 | 2487 |         /* loop by 2 to skip semi-colons */ | 
 | 2488 | 	for (i = 0; i < end; i += 2) { | 
 | 2489 | 	    ch = CHILD(n, i); | 
 | 2490 | 	    s = ast_for_stmt(c, ch); | 
 | 2491 | 	    if (!s) | 
 | 2492 | 		goto error; | 
 | 2493 | 	    asdl_seq_SET(seq, pos++, s); | 
 | 2494 | 	} | 
 | 2495 |     } | 
 | 2496 |     else { | 
 | 2497 | 	for (i = 2; i < (NCH(n) - 1); i++) { | 
 | 2498 | 	    ch = CHILD(n, i); | 
 | 2499 | 	    REQ(ch, stmt); | 
 | 2500 | 	    num = num_stmts(ch); | 
 | 2501 | 	    if (num == 1) { | 
 | 2502 | 		/* small_stmt or compound_stmt with only one child */ | 
 | 2503 | 		s = ast_for_stmt(c, ch); | 
 | 2504 | 		if (!s) | 
 | 2505 | 		    goto error; | 
 | 2506 | 		asdl_seq_SET(seq, pos++, s); | 
 | 2507 | 	    } | 
 | 2508 | 	    else { | 
 | 2509 | 		int j; | 
 | 2510 | 		ch = CHILD(ch, 0); | 
 | 2511 | 		REQ(ch, simple_stmt); | 
 | 2512 | 		for (j = 0; j < NCH(ch); j += 2) { | 
 | 2513 | 		    s = ast_for_stmt(c, CHILD(ch, j)); | 
 | 2514 | 		    if (!s) | 
 | 2515 | 			goto error; | 
 | 2516 | 		    asdl_seq_SET(seq, pos++, s); | 
 | 2517 | 		} | 
 | 2518 | 	    } | 
 | 2519 | 	} | 
 | 2520 |     } | 
 | 2521 |     assert(pos == seq->size); | 
 | 2522 |     return seq; | 
 | 2523 |  error: | 
 | 2524 |     if (seq) | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2525 | 	asdl_stmt_seq_free(seq); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2526 |     return NULL; | 
 | 2527 | } | 
 | 2528 |  | 
 | 2529 | static stmt_ty | 
 | 2530 | ast_for_if_stmt(struct compiling *c, const node *n) | 
 | 2531 | { | 
 | 2532 |     /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* | 
 | 2533 |        ['else' ':' suite] | 
 | 2534 |     */ | 
 | 2535 |     char *s; | 
 | 2536 |  | 
 | 2537 |     REQ(n, if_stmt); | 
 | 2538 |  | 
 | 2539 |     if (NCH(n) == 4) { | 
 | 2540 |         expr_ty expression; | 
 | 2541 |         asdl_seq *suite_seq; | 
 | 2542 |  | 
 | 2543 |         expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 2544 |         if (!expression) | 
 | 2545 |             return NULL; | 
 | 2546 |         suite_seq = ast_for_suite(c, CHILD(n, 3));  | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2547 |         if (!suite_seq) { | 
 | 2548 | 	    free_expr(expression); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2549 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2550 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2551 |              | 
 | 2552 | 	return If(expression, suite_seq, NULL, LINENO(n)); | 
 | 2553 |     } | 
 | 2554 |     s = STR(CHILD(n, 4)); | 
 | 2555 |     /* s[2], the third character in the string, will be | 
 | 2556 |        's' for el_s_e, or | 
 | 2557 |        'i' for el_i_f | 
 | 2558 |     */ | 
 | 2559 |     if (s[2] == 's') { | 
 | 2560 |         expr_ty expression; | 
 | 2561 |         asdl_seq *seq1, *seq2; | 
 | 2562 |  | 
 | 2563 |         expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 2564 |         if (!expression) | 
 | 2565 |             return NULL; | 
 | 2566 |         seq1 = ast_for_suite(c, CHILD(n, 3)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2567 |         if (!seq1) { | 
 | 2568 | 	    free_expr(expression); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2569 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2570 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2571 |         seq2 = ast_for_suite(c, CHILD(n, 6)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2572 |         if (!seq2) { | 
 | 2573 | 	    asdl_stmt_seq_free(seq1); | 
 | 2574 | 	    free_expr(expression); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2575 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2576 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2577 |  | 
 | 2578 | 	return If(expression, seq1, seq2, LINENO(n)); | 
 | 2579 |     } | 
 | 2580 |     else if (s[2] == 'i') { | 
 | 2581 | 	int i, n_elif, has_else = 0; | 
 | 2582 | 	asdl_seq *orelse = NULL; | 
 | 2583 | 	n_elif = NCH(n) - 4; | 
 | 2584 |         /* must reference the child n_elif+1 since 'else' token is third, | 
 | 2585 |            not fourth, child from the end. */ | 
 | 2586 | 	if (TYPE(CHILD(n, (n_elif + 1))) == NAME | 
 | 2587 | 	    && STR(CHILD(n, (n_elif + 1)))[2] == 's') { | 
 | 2588 | 	    has_else = 1; | 
 | 2589 | 	    n_elif -= 3; | 
 | 2590 | 	} | 
 | 2591 | 	n_elif /= 4; | 
 | 2592 |  | 
 | 2593 | 	if (has_else) { | 
 | 2594 |             expr_ty expression; | 
 | 2595 |             asdl_seq *seq1, *seq2; | 
 | 2596 |  | 
 | 2597 | 	    orelse = asdl_seq_new(1); | 
 | 2598 | 	    if (!orelse) | 
 | 2599 | 		return NULL; | 
 | 2600 |             expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); | 
 | 2601 |             if (!expression) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2602 |                 asdl_seq_free(orelse); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2603 |                 return NULL; | 
 | 2604 |             } | 
 | 2605 |             seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4)); | 
 | 2606 |             if (!seq1) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2607 |                 free_expr(expression); | 
 | 2608 |                 asdl_seq_free(orelse); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2609 |                 return NULL; | 
 | 2610 |             } | 
 | 2611 |             seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); | 
 | 2612 |             if (!seq2) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2613 |                 free_expr(expression); | 
 | 2614 |                 asdl_stmt_seq_free(seq1); | 
 | 2615 |                 asdl_seq_free(orelse); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2616 |                 return NULL; | 
 | 2617 |             } | 
 | 2618 |  | 
 | 2619 | 	    asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,  | 
 | 2620 | 				       LINENO(CHILD(n, NCH(n) - 6)))); | 
 | 2621 | 	    /* the just-created orelse handled the last elif */ | 
 | 2622 | 	    n_elif--; | 
 | 2623 | 	} | 
 | 2624 |         else | 
 | 2625 |             orelse  = NULL; | 
 | 2626 |  | 
 | 2627 | 	for (i = 0; i < n_elif; i++) { | 
 | 2628 | 	    int off = 5 + (n_elif - i - 1) * 4; | 
 | 2629 |             expr_ty expression; | 
 | 2630 |             asdl_seq *suite_seq; | 
 | 2631 | 	    asdl_seq *new = asdl_seq_new(1); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2632 | 	    if (!new) { | 
 | 2633 | 		asdl_stmt_seq_free(orelse); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2634 | 		return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2635 | 	    } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2636 |             expression = ast_for_expr(c, CHILD(n, off)); | 
 | 2637 |             if (!expression) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2638 | 		asdl_stmt_seq_free(orelse); | 
 | 2639 |                 asdl_seq_free(new); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2640 |                 return NULL; | 
 | 2641 |             } | 
 | 2642 |             suite_seq = ast_for_suite(c, CHILD(n, off + 2)); | 
 | 2643 |             if (!suite_seq) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2644 | 		asdl_stmt_seq_free(orelse); | 
 | 2645 | 	        free_expr(expression); | 
 | 2646 |                 asdl_seq_free(new); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 |                 return NULL; | 
 | 2648 |             } | 
 | 2649 |  | 
 | 2650 | 	    asdl_seq_SET(new, 0, | 
 | 2651 | 			 If(expression, suite_seq, orelse,  | 
 | 2652 | 			    LINENO(CHILD(n, off)))); | 
 | 2653 | 	    orelse = new; | 
 | 2654 | 	} | 
 | 2655 | 	return If(ast_for_expr(c, CHILD(n, 1)), | 
 | 2656 | 		  ast_for_suite(c, CHILD(n, 3)), | 
 | 2657 | 		  orelse, LINENO(n)); | 
 | 2658 |     } | 
 | 2659 |     else { | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2660 |         PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2661 |                      "unexpected token in 'if' statement: %s", s); | 
 | 2662 |         return NULL; | 
 | 2663 |     } | 
 | 2664 | } | 
 | 2665 |  | 
 | 2666 | static stmt_ty | 
 | 2667 | ast_for_while_stmt(struct compiling *c, const node *n) | 
 | 2668 | { | 
 | 2669 |     /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ | 
 | 2670 |     REQ(n, while_stmt); | 
 | 2671 |  | 
 | 2672 |     if (NCH(n) == 4) { | 
 | 2673 |         expr_ty expression; | 
 | 2674 |         asdl_seq *suite_seq; | 
 | 2675 |  | 
 | 2676 |         expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 2677 |         if (!expression) | 
 | 2678 |             return NULL; | 
 | 2679 |         suite_seq = ast_for_suite(c, CHILD(n, 3)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2680 |         if (!suite_seq) { | 
 | 2681 | 	    free_expr(expression); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2682 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2683 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2684 | 	return While(expression, suite_seq, NULL, LINENO(n)); | 
 | 2685 |     } | 
 | 2686 |     else if (NCH(n) == 7) { | 
 | 2687 |         expr_ty expression; | 
 | 2688 |         asdl_seq *seq1, *seq2; | 
 | 2689 |  | 
 | 2690 |         expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 2691 |         if (!expression) | 
 | 2692 |             return NULL; | 
 | 2693 |         seq1 = ast_for_suite(c, CHILD(n, 3)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2694 |         if (!seq1) { | 
 | 2695 | 	    free_expr(expression); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2696 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2697 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2698 |         seq2 = ast_for_suite(c, CHILD(n, 6)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2699 |         if (!seq2) { | 
 | 2700 | 	    asdl_stmt_seq_free(seq1); | 
 | 2701 | 	    free_expr(expression); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2702 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2703 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2704 |  | 
 | 2705 | 	return While(expression, seq1, seq2, LINENO(n)); | 
 | 2706 |     } | 
 | 2707 |     else { | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2708 |         PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2709 |                      "wrong number of tokens for 'while' statement: %d", | 
 | 2710 |                      NCH(n)); | 
 | 2711 |         return NULL; | 
 | 2712 |     } | 
 | 2713 | } | 
 | 2714 |  | 
 | 2715 | static stmt_ty | 
 | 2716 | ast_for_for_stmt(struct compiling *c, const node *n) | 
 | 2717 | { | 
 | 2718 |     asdl_seq *_target = NULL, *seq = NULL, *suite_seq = NULL; | 
 | 2719 |     expr_ty expression; | 
 | 2720 |     expr_ty target; | 
 | 2721 |     /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ | 
 | 2722 |     REQ(n, for_stmt); | 
 | 2723 |  | 
 | 2724 |     if (NCH(n) == 9) { | 
 | 2725 | 	seq = ast_for_suite(c, CHILD(n, 8)); | 
 | 2726 |         if (!seq) | 
 | 2727 |             return NULL; | 
 | 2728 |     } | 
 | 2729 |  | 
 | 2730 |     _target = ast_for_exprlist(c, CHILD(n, 1), Store); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2731 |     if (!_target) { | 
 | 2732 | 	asdl_stmt_seq_free(seq); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2733 |         return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2734 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2735 |     if (asdl_seq_LEN(_target) == 1) { | 
 | 2736 | 	target = asdl_seq_GET(_target, 0); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2737 | 	asdl_seq_free(_target); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2738 |     } | 
 | 2739 |     else | 
 | 2740 | 	target = Tuple(_target, Store, LINENO(n)); | 
 | 2741 |  | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2742 |     expression = ast_for_testlist(c, CHILD(n, 3)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2743 |     if (!expression) { | 
 | 2744 | 	free_expr(target); | 
 | 2745 | 	asdl_stmt_seq_free(seq); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2746 |         return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2747 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2748 |     suite_seq = ast_for_suite(c, CHILD(n, 5)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2749 |     if (!suite_seq) { | 
 | 2750 | 	free_expr(target); | 
 | 2751 | 	free_expr(expression); | 
 | 2752 | 	asdl_stmt_seq_free(seq); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2753 |         return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2754 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2755 |  | 
 | 2756 |     return For(target, expression, suite_seq, seq, LINENO(n)); | 
 | 2757 | } | 
 | 2758 |  | 
 | 2759 | static excepthandler_ty | 
 | 2760 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) | 
 | 2761 | { | 
 | 2762 |     /* except_clause: 'except' [test [',' test]] */ | 
 | 2763 |     REQ(exc, except_clause); | 
 | 2764 |     REQ(body, suite); | 
 | 2765 |  | 
 | 2766 |     if (NCH(exc) == 1) { | 
 | 2767 |         asdl_seq *suite_seq = ast_for_suite(c, body); | 
 | 2768 |         if (!suite_seq) | 
 | 2769 |             return NULL; | 
 | 2770 |  | 
 | 2771 | 	return excepthandler(NULL, NULL, suite_seq); | 
 | 2772 |     } | 
 | 2773 |     else if (NCH(exc) == 2) { | 
 | 2774 |         expr_ty expression; | 
 | 2775 |         asdl_seq *suite_seq; | 
 | 2776 |  | 
 | 2777 |         expression = ast_for_expr(c, CHILD(exc, 1)); | 
 | 2778 |         if (!expression) | 
 | 2779 |             return NULL; | 
 | 2780 |         suite_seq = ast_for_suite(c, body); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2781 |         if (!suite_seq) { | 
 | 2782 | 	    free_expr(expression); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2783 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2784 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2785 |  | 
 | 2786 | 	return excepthandler(expression, NULL, suite_seq); | 
 | 2787 |     } | 
 | 2788 |     else if (NCH(exc) == 4) { | 
 | 2789 |         asdl_seq *suite_seq; | 
 | 2790 |         expr_ty expression; | 
 | 2791 | 	expr_ty e = ast_for_expr(c, CHILD(exc, 3)); | 
 | 2792 | 	if (!e) | 
 | 2793 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2794 | 	if (!set_context(e, Store, CHILD(exc, 3))) { | 
 | 2795 | 	    free_expr(e); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2796 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2797 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2798 |         expression = ast_for_expr(c, CHILD(exc, 1)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2799 |         if (!expression) { | 
 | 2800 | 	    free_expr(e); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2801 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2802 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2803 |         suite_seq = ast_for_suite(c, body); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2804 |         if (!suite_seq) { | 
 | 2805 | 	    free_expr(expression); | 
 | 2806 | 	    free_expr(e); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2807 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2808 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2809 |  | 
 | 2810 | 	return excepthandler(expression, e, suite_seq); | 
 | 2811 |     } | 
 | 2812 |     else { | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2813 |         PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2814 |                      "wrong number of children for 'except' clause: %d", | 
 | 2815 |                      NCH(exc)); | 
 | 2816 |         return NULL; | 
 | 2817 |     } | 
 | 2818 | } | 
 | 2819 |  | 
 | 2820 | static stmt_ty | 
 | 2821 | ast_for_try_stmt(struct compiling *c, const node *n) | 
 | 2822 | { | 
 | 2823 |     REQ(n, try_stmt); | 
 | 2824 |  | 
 | 2825 |     if (TYPE(CHILD(n, 3)) == NAME) {/* must be 'finally' */ | 
 | 2826 | 	/* try_stmt: 'try' ':' suite 'finally' ':' suite) */ | 
 | 2827 |         asdl_seq *s1, *s2; | 
 | 2828 |         s1 = ast_for_suite(c, CHILD(n, 2)); | 
 | 2829 |         if (!s1) | 
 | 2830 |             return NULL; | 
 | 2831 |         s2 = ast_for_suite(c, CHILD(n, 5)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2832 |         if (!s2) { | 
 | 2833 | 	    asdl_stmt_seq_free(s1); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2834 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2835 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2836 |              | 
 | 2837 | 	return TryFinally(s1, s2, LINENO(n)); | 
 | 2838 |     } | 
 | 2839 |     else if (TYPE(CHILD(n, 3)) == except_clause) { | 
 | 2840 | 	/* try_stmt: ('try' ':' suite (except_clause ':' suite)+ | 
 | 2841 |            ['else' ':' suite] | 
 | 2842 | 	*/ | 
 | 2843 |         asdl_seq *suite_seq1, *suite_seq2; | 
 | 2844 | 	asdl_seq *handlers; | 
 | 2845 | 	int i, has_else = 0, n_except = NCH(n) - 3; | 
 | 2846 | 	if (TYPE(CHILD(n, NCH(n) - 3)) == NAME) { | 
 | 2847 | 	    has_else = 1; | 
 | 2848 | 	    n_except -= 3; | 
 | 2849 | 	} | 
 | 2850 | 	n_except /= 3; | 
 | 2851 | 	handlers = asdl_seq_new(n_except); | 
 | 2852 | 	if (!handlers) | 
 | 2853 | 		return NULL; | 
 | 2854 | 	for (i = 0; i < n_except; i++) { | 
 | 2855 |             excepthandler_ty e = ast_for_except_clause(c, | 
 | 2856 |                                                        CHILD(n, 3 + i * 3), | 
 | 2857 |                                                        CHILD(n, 5 + i * 3)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2858 |             if (!e) { | 
 | 2859 | 		for ( ; i >= 0; i--) | 
 | 2860 | 		    free_excepthandler(asdl_seq_GET(handlers, i)); | 
 | 2861 | 	        asdl_seq_free(handlers); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2862 |                 return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2863 | 	    } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2864 | 	    asdl_seq_SET(handlers, i, e); | 
 | 2865 |         } | 
 | 2866 |  | 
 | 2867 |         suite_seq1 = ast_for_suite(c, CHILD(n, 2)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2868 |         if (!suite_seq1) { | 
 | 2869 | 	    for (i = 0; i < asdl_seq_LEN(handlers); i++) | 
 | 2870 | 		free_excepthandler(asdl_seq_GET(handlers, i)); | 
 | 2871 | 	    asdl_seq_free(handlers); /* ok */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2872 |             return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2873 | 	} | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2874 |         if (has_else) { | 
 | 2875 |             suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2876 |             if (!suite_seq2) { | 
 | 2877 | 	        for (i = 0; i < asdl_seq_LEN(handlers); i++) | 
 | 2878 | 		    free_excepthandler(asdl_seq_GET(handlers, i)); | 
 | 2879 | 	        asdl_seq_free(handlers); /* ok */ | 
 | 2880 | 		asdl_stmt_seq_free(suite_seq1); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2881 |                 return NULL; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2882 | 	    } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2883 |         } | 
 | 2884 |         else | 
 | 2885 |             suite_seq2 = NULL; | 
 | 2886 |  | 
 | 2887 | 	return TryExcept(suite_seq1, handlers, suite_seq2, LINENO(n)); | 
 | 2888 |     } | 
 | 2889 |     else { | 
| Neal Norwitz | 7b3d5e1 | 2005-11-13 21:17:28 +0000 | [diff] [blame] | 2890 |         ast_error(n, "malformed 'try' statement"); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2891 |         return NULL; | 
 | 2892 |     } | 
 | 2893 | } | 
 | 2894 |  | 
 | 2895 | static stmt_ty | 
 | 2896 | ast_for_classdef(struct compiling *c, const node *n) | 
 | 2897 | { | 
 | 2898 |     /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2899 |     asdl_seq *bases, *s; | 
 | 2900 |      | 
 | 2901 |     REQ(n, classdef); | 
 | 2902 |  | 
 | 2903 |     if (!strcmp(STR(CHILD(n, 1)), "None")) { | 
 | 2904 | 	    ast_error(n, "assignment to None"); | 
 | 2905 | 	    return NULL; | 
 | 2906 |     } | 
 | 2907 |  | 
 | 2908 |     if (NCH(n) == 4) { | 
 | 2909 |         s = ast_for_suite(c, CHILD(n, 3)); | 
 | 2910 |         if (!s) | 
 | 2911 |             return NULL; | 
 | 2912 | 	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n)); | 
 | 2913 |     } | 
 | 2914 |     /* check for empty base list */ | 
 | 2915 |     if (TYPE(CHILD(n,3)) == RPAR) { | 
 | 2916 | 	s = ast_for_suite(c, CHILD(n,5)); | 
 | 2917 | 	if (!s) | 
 | 2918 | 		return NULL; | 
 | 2919 | 	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n)); | 
 | 2920 |     } | 
 | 2921 |  | 
 | 2922 |     /* else handle the base class list */ | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2923 |     bases = ast_for_class_bases(c, CHILD(n, 3)); | 
 | 2924 |     if (!bases) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2925 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2926 |  | 
 | 2927 |     s = ast_for_suite(c, CHILD(n, 6)); | 
 | 2928 |     if (!s) { | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame^] | 2929 |         asdl_expr_seq_free(bases); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2930 |         return NULL; | 
 | 2931 |     } | 
 | 2932 |     return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n)); | 
 | 2933 | } | 
 | 2934 |  | 
 | 2935 | static stmt_ty | 
 | 2936 | ast_for_stmt(struct compiling *c, const node *n) | 
 | 2937 | { | 
 | 2938 |     if (TYPE(n) == stmt) { | 
 | 2939 | 	assert(NCH(n) == 1); | 
 | 2940 | 	n = CHILD(n, 0); | 
 | 2941 |     } | 
 | 2942 |     if (TYPE(n) == simple_stmt) { | 
 | 2943 | 	assert(num_stmts(n) == 1); | 
 | 2944 | 	n = CHILD(n, 0); | 
 | 2945 |     } | 
 | 2946 |     if (TYPE(n) == small_stmt) { | 
 | 2947 | 	REQ(n, small_stmt); | 
 | 2948 | 	n = CHILD(n, 0); | 
 | 2949 | 	/* small_stmt: expr_stmt | print_stmt  | del_stmt | pass_stmt | 
 | 2950 | 	             | flow_stmt | import_stmt | global_stmt | exec_stmt | 
 | 2951 |                      | assert_stmt | 
 | 2952 | 	*/ | 
 | 2953 | 	switch (TYPE(n)) { | 
 | 2954 |             case expr_stmt: | 
 | 2955 |                 return ast_for_expr_stmt(c, n); | 
 | 2956 |             case print_stmt: | 
 | 2957 |                 return ast_for_print_stmt(c, n); | 
 | 2958 |             case del_stmt: | 
 | 2959 |                 return ast_for_del_stmt(c, n); | 
 | 2960 |             case pass_stmt: | 
 | 2961 |                 return Pass(LINENO(n)); | 
 | 2962 |             case flow_stmt: | 
 | 2963 |                 return ast_for_flow_stmt(c, n); | 
 | 2964 |             case import_stmt: | 
 | 2965 |                 return ast_for_import_stmt(c, n); | 
 | 2966 |             case global_stmt: | 
 | 2967 |                 return ast_for_global_stmt(c, n); | 
 | 2968 |             case exec_stmt: | 
 | 2969 |                 return ast_for_exec_stmt(c, n); | 
 | 2970 |             case assert_stmt: | 
 | 2971 |                 return ast_for_assert_stmt(c, n); | 
 | 2972 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2973 |                 PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2974 |                              "unhandled small_stmt: TYPE=%d NCH=%d\n", | 
 | 2975 |                              TYPE(n), NCH(n)); | 
 | 2976 |                 return NULL; | 
 | 2977 |         } | 
 | 2978 |     } | 
 | 2979 |     else { | 
 | 2980 |         /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | 
 | 2981 | 	                | funcdef | classdef | 
 | 2982 | 	*/ | 
 | 2983 | 	node *ch = CHILD(n, 0); | 
 | 2984 | 	REQ(n, compound_stmt); | 
 | 2985 | 	switch (TYPE(ch)) { | 
 | 2986 |             case if_stmt: | 
 | 2987 |                 return ast_for_if_stmt(c, ch); | 
 | 2988 |             case while_stmt: | 
 | 2989 |                 return ast_for_while_stmt(c, ch); | 
 | 2990 |             case for_stmt: | 
 | 2991 |                 return ast_for_for_stmt(c, ch); | 
 | 2992 |             case try_stmt: | 
 | 2993 |                 return ast_for_try_stmt(c, ch); | 
 | 2994 |             case funcdef: | 
 | 2995 |                 return ast_for_funcdef(c, ch); | 
 | 2996 |             case classdef: | 
 | 2997 |                 return ast_for_classdef(c, ch); | 
 | 2998 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2999 |                 PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3000 |                              "unhandled small_stmt: TYPE=%d NCH=%d\n", | 
 | 3001 |                              TYPE(n), NCH(n)); | 
 | 3002 |                 return NULL; | 
 | 3003 | 	} | 
 | 3004 |     } | 
 | 3005 | } | 
 | 3006 |  | 
 | 3007 | static PyObject * | 
 | 3008 | parsenumber(const char *s) | 
 | 3009 | { | 
 | 3010 | 	const char *end; | 
 | 3011 | 	long x; | 
 | 3012 | 	double dx; | 
 | 3013 | #ifndef WITHOUT_COMPLEX | 
 | 3014 | 	Py_complex c; | 
 | 3015 | 	int imflag; | 
 | 3016 | #endif | 
 | 3017 |  | 
 | 3018 | 	errno = 0; | 
 | 3019 | 	end = s + strlen(s) - 1; | 
 | 3020 | #ifndef WITHOUT_COMPLEX | 
 | 3021 | 	imflag = *end == 'j' || *end == 'J'; | 
 | 3022 | #endif | 
 | 3023 | 	if (*end == 'l' || *end == 'L') | 
 | 3024 | 		return PyLong_FromString((char *)s, (char **)0, 0); | 
 | 3025 | 	if (s[0] == '0') { | 
 | 3026 | 		x = (long) PyOS_strtoul((char *)s, (char **)&end, 0); | 
 | 3027 |  		if (x < 0 && errno == 0) { | 
 | 3028 | 	 			return PyLong_FromString((char *)s, | 
 | 3029 | 							 (char **)0, | 
 | 3030 | 							 0); | 
 | 3031 | 		} | 
 | 3032 | 	} | 
 | 3033 | 	else | 
 | 3034 | 		x = PyOS_strtol((char *)s, (char **)&end, 0); | 
 | 3035 | 	if (*end == '\0') { | 
 | 3036 | 		if (errno != 0) | 
 | 3037 | 			return PyLong_FromString((char *)s, (char **)0, 0); | 
 | 3038 | 		return PyInt_FromLong(x); | 
 | 3039 | 	} | 
 | 3040 | 	/* XXX Huge floats may silently fail */ | 
 | 3041 | #ifndef WITHOUT_COMPLEX | 
 | 3042 | 	if (imflag) { | 
 | 3043 | 		c.real = 0.; | 
 | 3044 | 		PyFPE_START_PROTECT("atof", return 0) | 
 | 3045 | 		c.imag = atof(s); | 
 | 3046 | 		PyFPE_END_PROTECT(c) | 
 | 3047 | 		return PyComplex_FromCComplex(c); | 
 | 3048 | 	} | 
 | 3049 | 	else | 
 | 3050 | #endif | 
 | 3051 | 	{ | 
 | 3052 | 		PyFPE_START_PROTECT("atof", return 0) | 
 | 3053 | 		dx = atof(s); | 
 | 3054 | 		PyFPE_END_PROTECT(dx) | 
 | 3055 | 		return PyFloat_FromDouble(dx); | 
 | 3056 | 	} | 
 | 3057 | } | 
 | 3058 |  | 
 | 3059 | static PyObject * | 
 | 3060 | decode_utf8(const char **sPtr, const char *end, char* encoding) | 
 | 3061 | { | 
 | 3062 | #ifndef Py_USING_UNICODE | 
 | 3063 | 	Py_FatalError("decode_utf8 should not be called in this build."); | 
 | 3064 |         return NULL; | 
 | 3065 | #else | 
 | 3066 | 	PyObject *u, *v; | 
 | 3067 | 	char *s, *t; | 
 | 3068 | 	t = s = (char *)*sPtr; | 
 | 3069 | 	/* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ | 
 | 3070 | 	while (s < end && (*s & 0x80)) s++; | 
 | 3071 | 	*sPtr = s; | 
 | 3072 | 	u = PyUnicode_DecodeUTF8(t, s - t, NULL); | 
 | 3073 | 	if (u == NULL) | 
 | 3074 | 		return NULL; | 
 | 3075 | 	v = PyUnicode_AsEncodedString(u, encoding, NULL); | 
 | 3076 | 	Py_DECREF(u); | 
 | 3077 | 	return v; | 
 | 3078 | #endif | 
 | 3079 | } | 
 | 3080 |  | 
 | 3081 | static PyObject * | 
 | 3082 | decode_unicode(const char *s, size_t len, int rawmode, const char *encoding) | 
 | 3083 | { | 
 | 3084 | 	PyObject *v, *u; | 
 | 3085 | 	char *buf; | 
 | 3086 | 	char *p; | 
 | 3087 | 	const char *end; | 
 | 3088 | 	if (encoding == NULL) { | 
 | 3089 | 	     	buf = (char *)s; | 
 | 3090 | 		u = NULL; | 
 | 3091 | 	} else if (strcmp(encoding, "iso-8859-1") == 0) { | 
 | 3092 | 	     	buf = (char *)s; | 
 | 3093 | 		u = NULL; | 
 | 3094 | 	} else { | 
 | 3095 | 		/* "\XX" may become "\u005c\uHHLL" (12 bytes) */ | 
 | 3096 | 		u = PyString_FromStringAndSize((char *)NULL, len * 4); | 
 | 3097 | 		if (u == NULL) | 
 | 3098 | 			return NULL; | 
 | 3099 | 		p = buf = PyString_AsString(u); | 
 | 3100 | 		end = s + len; | 
 | 3101 | 		while (s < end) { | 
 | 3102 | 			if (*s == '\\') { | 
 | 3103 | 				*p++ = *s++; | 
 | 3104 | 				if (*s & 0x80) { | 
 | 3105 | 					strcpy(p, "u005c"); | 
 | 3106 | 					p += 5; | 
 | 3107 | 				} | 
 | 3108 | 			} | 
 | 3109 | 			if (*s & 0x80) { /* XXX inefficient */ | 
 | 3110 | 				PyObject *w; | 
 | 3111 | 				char *r; | 
 | 3112 | 				int rn, i; | 
 | 3113 | 				w = decode_utf8(&s, end, "utf-16-be"); | 
 | 3114 | 				if (w == NULL) { | 
 | 3115 | 					Py_DECREF(u); | 
 | 3116 | 					return NULL; | 
 | 3117 | 				} | 
 | 3118 | 				r = PyString_AsString(w); | 
 | 3119 | 				rn = PyString_Size(w); | 
 | 3120 | 				assert(rn % 2 == 0); | 
 | 3121 | 				for (i = 0; i < rn; i += 2) { | 
 | 3122 | 					sprintf(p, "\\u%02x%02x", | 
 | 3123 | 						r[i + 0] & 0xFF, | 
 | 3124 | 						r[i + 1] & 0xFF); | 
 | 3125 | 					p += 6; | 
 | 3126 | 				} | 
 | 3127 | 				Py_DECREF(w); | 
 | 3128 | 			} else { | 
 | 3129 | 				*p++ = *s++; | 
 | 3130 | 			} | 
 | 3131 | 		} | 
 | 3132 | 		len = p - buf; | 
 | 3133 | 		s = buf; | 
 | 3134 | 	} | 
 | 3135 | 	if (rawmode) | 
 | 3136 | 		v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL); | 
 | 3137 | 	else | 
 | 3138 | 		v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); | 
 | 3139 | 	Py_XDECREF(u); | 
 | 3140 | 	return v; | 
 | 3141 | } | 
 | 3142 |  | 
 | 3143 | /* s is a Python string literal, including the bracketing quote characters, | 
 | 3144 |  * and r &/or u prefixes (if any), and embedded escape sequences (if any). | 
 | 3145 |  * parsestr parses it, and returns the decoded Python string object. | 
 | 3146 |  */ | 
 | 3147 | static PyObject * | 
 | 3148 | parsestr(const char *s, const char *encoding) | 
 | 3149 | { | 
 | 3150 | 	PyObject *v; | 
 | 3151 | 	size_t len; | 
 | 3152 | 	int quote = *s; | 
 | 3153 | 	int rawmode = 0; | 
 | 3154 | 	int need_encoding; | 
 | 3155 | 	int unicode = 0; | 
 | 3156 |  | 
 | 3157 | 	if (isalpha(quote) || quote == '_') { | 
 | 3158 | 		if (quote == 'u' || quote == 'U') { | 
 | 3159 | 			quote = *++s; | 
 | 3160 | 			unicode = 1; | 
 | 3161 | 		} | 
 | 3162 | 		if (quote == 'r' || quote == 'R') { | 
 | 3163 | 			quote = *++s; | 
 | 3164 | 			rawmode = 1; | 
 | 3165 | 		} | 
 | 3166 | 	} | 
 | 3167 | 	if (quote != '\'' && quote != '\"') { | 
 | 3168 | 		PyErr_BadInternalCall(); | 
 | 3169 | 		return NULL; | 
 | 3170 | 	} | 
 | 3171 | 	s++; | 
 | 3172 | 	len = strlen(s); | 
 | 3173 | 	if (len > INT_MAX) { | 
 | 3174 | 		PyErr_SetString(PyExc_OverflowError,  | 
 | 3175 | 				"string to parse is too long"); | 
 | 3176 | 		return NULL; | 
 | 3177 | 	} | 
 | 3178 | 	if (s[--len] != quote) { | 
 | 3179 | 		PyErr_BadInternalCall(); | 
 | 3180 | 		return NULL; | 
 | 3181 | 	} | 
 | 3182 | 	if (len >= 4 && s[0] == quote && s[1] == quote) { | 
 | 3183 | 		s += 2; | 
 | 3184 | 		len -= 2; | 
 | 3185 | 		if (s[--len] != quote || s[--len] != quote) { | 
 | 3186 | 			PyErr_BadInternalCall(); | 
 | 3187 | 			return NULL; | 
 | 3188 | 		} | 
 | 3189 | 	} | 
 | 3190 | #ifdef Py_USING_UNICODE | 
 | 3191 | 	if (unicode || Py_UnicodeFlag) { | 
 | 3192 | 		return decode_unicode(s, len, rawmode, encoding); | 
 | 3193 | 	} | 
 | 3194 | #endif | 
 | 3195 | 	need_encoding = (encoding != NULL && | 
 | 3196 | 			 strcmp(encoding, "utf-8") != 0 && | 
 | 3197 | 			 strcmp(encoding, "iso-8859-1") != 0); | 
 | 3198 | 	if (rawmode || strchr(s, '\\') == NULL) { | 
 | 3199 | 		if (need_encoding) { | 
 | 3200 | #ifndef Py_USING_UNICODE | 
 | 3201 | 			/* This should not happen - we never see any other | 
 | 3202 | 			   encoding. */ | 
 | 3203 | 			Py_FatalError("cannot deal with encodings in this build."); | 
 | 3204 | #else | 
 | 3205 | 			PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); | 
 | 3206 | 			if (u == NULL) | 
 | 3207 | 				return NULL; | 
 | 3208 | 			v = PyUnicode_AsEncodedString(u, encoding, NULL); | 
 | 3209 | 			Py_DECREF(u); | 
 | 3210 | 			return v; | 
 | 3211 | #endif | 
 | 3212 | 		} else { | 
 | 3213 | 			return PyString_FromStringAndSize(s, len); | 
 | 3214 | 		} | 
 | 3215 | 	} | 
 | 3216 |  | 
 | 3217 | 	v = PyString_DecodeEscape(s, len, NULL, unicode, | 
 | 3218 | 				  need_encoding ? encoding : NULL); | 
 | 3219 | 	return v; | 
 | 3220 | } | 
 | 3221 |  | 
 | 3222 | /* Build a Python string object out of a STRING atom.  This takes care of | 
 | 3223 |  * compile-time literal catenation, calling parsestr() on each piece, and | 
 | 3224 |  * pasting the intermediate results together. | 
 | 3225 |  */ | 
 | 3226 | static PyObject * | 
 | 3227 | parsestrplus(struct compiling *c, const node *n) | 
 | 3228 | { | 
 | 3229 | 	PyObject *v; | 
 | 3230 | 	int i; | 
 | 3231 | 	REQ(CHILD(n, 0), STRING); | 
 | 3232 | 	if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) { | 
 | 3233 | 		/* String literal concatenation */ | 
 | 3234 | 		for (i = 1; i < NCH(n); i++) { | 
 | 3235 | 			PyObject *s; | 
 | 3236 | 			s = parsestr(STR(CHILD(n, i)), c->c_encoding); | 
 | 3237 | 			if (s == NULL) | 
 | 3238 | 				goto onError; | 
 | 3239 | 			if (PyString_Check(v) && PyString_Check(s)) { | 
 | 3240 | 				PyString_ConcatAndDel(&v, s); | 
 | 3241 | 				if (v == NULL) | 
 | 3242 | 				    goto onError; | 
 | 3243 | 			} | 
 | 3244 | #ifdef Py_USING_UNICODE | 
 | 3245 | 			else { | 
 | 3246 | 				PyObject *temp; | 
 | 3247 | 				temp = PyUnicode_Concat(v, s); | 
 | 3248 | 				Py_DECREF(s); | 
 | 3249 | 				if (temp == NULL) | 
 | 3250 | 					goto onError; | 
 | 3251 | 				Py_DECREF(v); | 
 | 3252 | 				v = temp; | 
 | 3253 | 			} | 
 | 3254 | #endif | 
 | 3255 | 		} | 
 | 3256 | 	} | 
 | 3257 | 	return v; | 
 | 3258 |  | 
 | 3259 |  onError: | 
 | 3260 | 	Py_XDECREF(v); | 
 | 3261 | 	return NULL; | 
 | 3262 | } |