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