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