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