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