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