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