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