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