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