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