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