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