comparison Python/Python-ast.c @ 41464:060c79f6d919

Hide list comp variables and support set comprehensions
author Nick Coghlan <ncoghlan@gmail.com>
date Sun, 15 Apr 2007 12:05:43 +0000
parents 5e24f636ac75
children 3e5fdb93c95a
comparison
equal deleted inserted replaced
41457:bab9c3817092 41464:060c79f6d919
187 static char *Set_fields[]={ 187 static char *Set_fields[]={
188 "elts", 188 "elts",
189 }; 189 };
190 static PyTypeObject *ListComp_type; 190 static PyTypeObject *ListComp_type;
191 static char *ListComp_fields[]={ 191 static char *ListComp_fields[]={
192 "elt",
193 "generators",
194 };
195 static PyTypeObject *SetComp_type;
196 static char *SetComp_fields[]={
192 "elt", 197 "elt",
193 "generators", 198 "generators",
194 }; 199 };
195 static PyTypeObject *GeneratorExp_type; 200 static PyTypeObject *GeneratorExp_type;
196 static char *GeneratorExp_fields[]={ 201 static char *GeneratorExp_fields[]={
541 if (!Dict_type) return 0; 546 if (!Dict_type) return 0;
542 Set_type = make_type("Set", expr_type, Set_fields, 1); 547 Set_type = make_type("Set", expr_type, Set_fields, 1);
543 if (!Set_type) return 0; 548 if (!Set_type) return 0;
544 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2); 549 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
545 if (!ListComp_type) return 0; 550 if (!ListComp_type) return 0;
551 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
552 if (!SetComp_type) return 0;
546 GeneratorExp_type = make_type("GeneratorExp", expr_type, 553 GeneratorExp_type = make_type("GeneratorExp", expr_type,
547 GeneratorExp_fields, 2); 554 GeneratorExp_fields, 2);
548 if (!GeneratorExp_type) return 0; 555 if (!GeneratorExp_type) return 0;
549 Yield_type = make_type("Yield", expr_type, Yield_fields, 1); 556 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
550 if (!Yield_type) return 0; 557 if (!Yield_type) return 0;
1417 p->col_offset = col_offset; 1424 p->col_offset = col_offset;
1418 return p; 1425 return p;
1419 } 1426 }
1420 1427
1421 expr_ty 1428 expr_ty
1429 SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1430 *arena)
1431 {
1432 expr_ty p;
1433 if (!elt) {
1434 PyErr_SetString(PyExc_ValueError,
1435 "field elt is required for SetComp");
1436 return NULL;
1437 }
1438 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1439 if (!p)
1440 return NULL;
1441 p->kind = SetComp_kind;
1442 p->v.SetComp.elt = elt;
1443 p->v.SetComp.generators = generators;
1444 p->lineno = lineno;
1445 p->col_offset = col_offset;
1446 return p;
1447 }
1448
1449 expr_ty
1422 GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, 1450 GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1423 PyArena *arena) 1451 PyArena *arena)
1424 { 1452 {
1425 expr_ty p; 1453 expr_ty p;
1426 if (!elt) { 1454 if (!elt) {
2408 if (!value) goto failed; 2436 if (!value) goto failed;
2409 if (PyObject_SetAttrString(result, "elt", value) == -1) 2437 if (PyObject_SetAttrString(result, "elt", value) == -1)
2410 goto failed; 2438 goto failed;
2411 Py_DECREF(value); 2439 Py_DECREF(value);
2412 value = ast2obj_list(o->v.ListComp.generators, 2440 value = ast2obj_list(o->v.ListComp.generators,
2441 ast2obj_comprehension);
2442 if (!value) goto failed;
2443 if (PyObject_SetAttrString(result, "generators", value) == -1)
2444 goto failed;
2445 Py_DECREF(value);
2446 break;
2447 case SetComp_kind:
2448 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2449 if (!result) goto failed;
2450 value = ast2obj_expr(o->v.SetComp.elt);
2451 if (!value) goto failed;
2452 if (PyObject_SetAttrString(result, "elt", value) == -1)
2453 goto failed;
2454 Py_DECREF(value);
2455 value = ast2obj_list(o->v.SetComp.generators,
2413 ast2obj_comprehension); 2456 ast2obj_comprehension);
2414 if (!value) goto failed; 2457 if (!value) goto failed;
2415 if (PyObject_SetAttrString(result, "generators", value) == -1) 2458 if (PyObject_SetAttrString(result, "generators", value) == -1)
2416 goto failed; 2459 goto failed;
2417 Py_DECREF(value); 2460 Py_DECREF(value);
3117 return; 3160 return;
3118 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return; 3161 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
3119 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return; 3162 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
3120 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return; 3163 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return;
3121 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0) 3164 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
3165 return;
3166 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
3122 return; 3167 return;
3123 if (PyDict_SetItemString(d, "GeneratorExp", 3168 if (PyDict_SetItemString(d, "GeneratorExp",
3124 (PyObject*)GeneratorExp_type) < 0) return; 3169 (PyObject*)GeneratorExp_type) < 0) return;
3125 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return; 3170 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
3126 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) 3171 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)