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