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