comparison Python/compile.c @ 35298:b2fe5f8fcfdd legacy-trunk

PEP 342 implementation. Per Guido's comments, the generator throw() method still needs to support string exceptions, and allow None for the third argument. Documentation updates are needed, too.
author Phillip J. Eby <pje@telecommunity.com>
date Tue, 02 Aug 2005 00:46:46 +0000
parents 70681267a74a
children f3996aa78cde
comparison
equal deleted inserted replaced
35297:26bf6c27e9ce 35298:b2fe5f8fcfdd
2143 if (NCH(n) == 5) 2143 if (NCH(n) == 5)
2144 com_gen_iter(c, CHILD(n, 4), t); 2144 com_gen_iter(c, CHILD(n, 4), t);
2145 else { 2145 else {
2146 com_test(c, t); 2146 com_test(c, t);
2147 com_addbyte(c, YIELD_VALUE); 2147 com_addbyte(c, YIELD_VALUE);
2148 com_addbyte(c, POP_TOP);
2148 com_pop(c, 1); 2149 com_pop(c, 1);
2149 } 2150 }
2150 2151
2151 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); 2152 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2152 c->c_begin = save_begin; 2153 c->c_begin = save_begin;
2191 if (NCH(n) == 3) 2192 if (NCH(n) == 3)
2192 com_gen_iter(c, CHILD(n, 2), t); 2193 com_gen_iter(c, CHILD(n, 2), t);
2193 else { 2194 else {
2194 com_test(c, t); 2195 com_test(c, t);
2195 com_addbyte(c, YIELD_VALUE); 2196 com_addbyte(c, YIELD_VALUE);
2197 com_addbyte(c, POP_TOP);
2196 com_pop(c, 1); 2198 com_pop(c, 1);
2197 } 2199 }
2198 com_addfwref(c, JUMP_FORWARD, &anchor); 2200 com_addfwref(c, JUMP_FORWARD, &anchor);
2199 com_backpatch(c, a); 2201 com_backpatch(c, a);
2200 /* We jump here with an extra entry which we now pop */ 2202 /* We jump here with an extra entry which we now pop */
2352 com_addbyte(c, STORE_SUBSCR); 2354 com_addbyte(c, STORE_SUBSCR);
2353 com_pop(c, 3); 2355 com_pop(c, 3);
2354 } 2356 }
2355 } 2357 }
2356 2358
2359
2360 /* forward reference */
2361 static void com_yield_expr(struct compiling *c, node *n);
2362
2357 static void 2363 static void
2358 com_atom(struct compiling *c, node *n) 2364 com_atom(struct compiling *c, node *n)
2359 { 2365 {
2360 node *ch; 2366 node *ch;
2361 PyObject *v; 2367 PyObject *v;
2367 if (TYPE(CHILD(n, 1)) == RPAR) { 2373 if (TYPE(CHILD(n, 1)) == RPAR) {
2368 com_addoparg(c, BUILD_TUPLE, 0); 2374 com_addoparg(c, BUILD_TUPLE, 0);
2369 com_push(c, 1); 2375 com_push(c, 1);
2370 } 2376 }
2371 else 2377 else
2372 com_testlist_gexp(c, CHILD(n, 1)); 2378 if (TYPE(CHILD(n, 1)) == yield_expr)
2379 com_yield_expr(c, CHILD(n, 1));
2380 else
2381 com_testlist_gexp(c, CHILD(n, 1));
2373 break; 2382 break;
2374 case LSQB: /* '[' [listmaker] ']' */ 2383 case LSQB: /* '[' [listmaker] ']' */
2375 if (TYPE(CHILD(n, 1)) == RSQB) { 2384 if (TYPE(CHILD(n, 1)) == RSQB) {
2376 com_addoparg(c, BUILD_LIST, 0); 2385 com_addoparg(c, BUILD_LIST, 0);
2377 com_push(c, 1); 2386 com_push(c, 1);
3434 com_assign_sequence(c, n, assigning); 3443 com_assign_sequence(c, n, assigning);
3435 return; 3444 return;
3436 } 3445 }
3437 n = CHILD(n, 0); 3446 n = CHILD(n, 0);
3438 break; 3447 break;
3439 3448 case yield_expr:
3449 com_error(c, PyExc_SyntaxError,
3450 "assignment to yield expression not possible");
3451 return;
3452
3440 case test: 3453 case test:
3441 case and_test: 3454 case and_test:
3442 case not_test: 3455 case not_test:
3443 case comparison: 3456 case comparison:
3444 case expr: 3457 case expr:
3491 "can't assign to ()"); 3504 "can't assign to ()");
3492 return; 3505 return;
3493 } 3506 }
3494 if (assigning > OP_APPLY) { 3507 if (assigning > OP_APPLY) {
3495 com_error(c, PyExc_SyntaxError, 3508 com_error(c, PyExc_SyntaxError,
3496 "augmented assign to tuple literal or generator expression not possible"); 3509 "augmented assign to tuple literal, yield, or generator expression not possible");
3497 return; 3510 return;
3498 } 3511 }
3499 break; 3512 break;
3500 case LSQB: 3513 case LSQB:
3501 n = CHILD(n, 1); 3514 n = CHILD(n, 1);
3727 com_addbyte(c, RETURN_VALUE); 3740 com_addbyte(c, RETURN_VALUE);
3728 com_pop(c, 1); 3741 com_pop(c, 1);
3729 } 3742 }
3730 3743
3731 static void 3744 static void
3732 com_yield_stmt(struct compiling *c, node *n) 3745 com_yield_expr(struct compiling *c, node *n)
3733 { 3746 {
3734 int i; 3747 int i;
3735 REQ(n, yield_stmt); /* 'yield' testlist */ 3748 REQ(n, yield_expr); /* 'yield' testlist */
3736 if (!c->c_infunction) { 3749 if (!c->c_infunction) {
3737 com_error(c, PyExc_SyntaxError, "'yield' outside function"); 3750 com_error(c, PyExc_SyntaxError, "'yield' outside function");
3738 } 3751 }
3739 3752
3740 for (i = 0; i < c->c_nblocks; ++i) { 3753 /* for (i = 0; i < c->c_nblocks; ++i) {
3741 if (c->c_block[i] == SETUP_FINALLY) { 3754 if (c->c_block[i] == SETUP_FINALLY) {
3742 com_error(c, PyExc_SyntaxError, 3755 com_error(c, PyExc_SyntaxError,
3743 "'yield' not allowed in a 'try' block " 3756 "'yield' not allowed in a 'try' block "
3744 "with a 'finally' clause"); 3757 "with a 'finally' clause");
3745 return; 3758 return;
3746 } 3759 }
3747 } 3760 } */
3748 com_node(c, CHILD(n, 1)); 3761
3762 if (NCH(n) < 2) {
3763 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3764 com_push(c, 1);
3765 }
3766 else
3767 com_node(c, CHILD(n, 1));
3749 com_addbyte(c, YIELD_VALUE); 3768 com_addbyte(c, YIELD_VALUE);
3769 }
3770
3771 static void
3772 com_yield_stmt(struct compiling *c, node *n)
3773 {
3774 REQ(n, yield_stmt); /* yield_expr */
3775 com_node(c, CHILD(n, 0));
3776 com_addbyte(c, POP_TOP);
3750 com_pop(c, 1); 3777 com_pop(c, 1);
3751 } 3778 }
3779
3752 3780
3753 static void 3781 static void
3754 com_raise_stmt(struct compiling *c, node *n) 3782 com_raise_stmt(struct compiling *c, node *n)
3755 { 3783 {
3756 int i; 3784 int i;
4766 com_suite(c, n); 4794 com_suite(c, n);
4767 break; 4795 break;
4768 4796
4769 /* Expression nodes */ 4797 /* Expression nodes */
4770 4798
4799 case yield_expr:
4800 com_yield_expr(c, n);
4801 break;
4802
4771 case testlist: 4803 case testlist:
4772 case testlist1: 4804 case testlist1:
4773 case testlist_safe: 4805 case testlist_safe:
4774 com_list(c, n, 0); 4806 com_list(c, n, 0);
4775 break; 4807 break;
5025 /* argument: test gen_for */ 5057 /* argument: test gen_for */
5026 REQ(CHILD(n, 0), test); 5058 REQ(CHILD(n, 0), test);
5027 REQ(CHILD(n, 1), gen_for); 5059 REQ(CHILD(n, 1), gen_for);
5028 5060
5029 c->c_name = "<generator expression>"; 5061 c->c_name = "<generator expression>";
5062 c->c_infunction = 1;
5030 com_gen_for(c, CHILD(n, 1), CHILD(n, 0), 1); 5063 com_gen_for(c, CHILD(n, 1), CHILD(n, 0), 1);
5064 c->c_infunction = 0;
5031 5065
5032 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); 5066 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
5033 com_push(c, 1); 5067 com_push(c, 1);
5034 com_addbyte(c, RETURN_VALUE); 5068 com_addbyte(c, RETURN_VALUE);
5035 com_pop(c, 1); 5069 com_pop(c, 1);
6113 return 0; 6147 return 0;
6114 } 6148 }
6115 6149
6116 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) 6150 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
6117 6151
6118 /* Look for a yield stmt under n. Return 1 if found, else 0. 6152 /* Look for a yield stmt or expr under n. Return 1 if found, else 0.
6119 This hack is used to look inside "if 0:" blocks (which are normally 6153 This hack is used to look inside "if 0:" blocks (which are normally
6120 ignored) in case those are the only places a yield occurs (so that this 6154 ignored) in case those are the only places a yield occurs (so that this
6121 function is a generator). */ 6155 function is a generator). */
6122 static int 6156 static int
6123 look_for_yield(node *n) 6157 look_for_yield(node *n)
6135 /* Stuff in nested functions and classes can't make 6169 /* Stuff in nested functions and classes can't make
6136 the parent a generator. */ 6170 the parent a generator. */
6137 return 0; 6171 return 0;
6138 6172
6139 case yield_stmt: 6173 case yield_stmt:
6174 case yield_expr:
6140 return GENERATOR; 6175 return GENERATOR;
6141 6176
6142 default: 6177 default:
6143 if (look_for_yield(kid)) 6178 if (look_for_yield(kid))
6144 return GENERATOR; 6179 return GENERATOR;
6245 } 6280 }
6246 break; 6281 break;
6247 case del_stmt: 6282 case del_stmt:
6248 symtable_assign(st, CHILD(n, 1), 0); 6283 symtable_assign(st, CHILD(n, 1), 0);
6249 break; 6284 break;
6250 case yield_stmt: 6285 case yield_expr:
6251 st->st_cur->ste_generator = 1; 6286 st->st_cur->ste_generator = 1;
6287 if (NCH(n)==1)
6288 break;
6252 n = CHILD(n, 1); 6289 n = CHILD(n, 1);
6253 goto loop; 6290 goto loop;
6254 case expr_stmt: 6291 case expr_stmt:
6255 if (NCH(n) == 1) 6292 if (NCH(n) == 1)
6256 n = CHILD(n, 0); 6293 n = CHILD(n, 0);
6339 break; 6376 break;
6340 } 6377 }
6341 /* fall through */ 6378 /* fall through */
6342 6379
6343 case atom: 6380 case atom:
6344 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { 6381 if (TYPE(n) == atom) {
6345 symtable_add_use(st, STR(CHILD(n, 0))); 6382 if (TYPE(CHILD(n, 0)) == NAME) {
6346 break; 6383 symtable_add_use(st, STR(CHILD(n, 0)));
6384 break;
6385 }
6386 else if (TYPE(CHILD(n,0)) == LPAR) {
6387 n = CHILD(n,1);
6388 goto loop;
6389 }
6347 } 6390 }
6348 /* fall through */ 6391 /* fall through */
6349 default: 6392 default:
6350 /* Walk over every non-token child with a special case 6393 /* Walk over every non-token child with a special case
6351 for one child. 6394 for one child.
6737 return; 6780 return;
6738 } 6781 }
6739 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); 6782 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
6740 } 6783 }
6741 return; 6784 return;
6785
6786 case yield_expr:
6787 st->st_cur->ste_generator = 1;
6788 if (NCH(n)==2) {
6789 n = CHILD(n, 1);
6790 goto loop;
6791 }
6792 return;
6793
6742 case dotted_as_name: 6794 case dotted_as_name:
6743 if (NCH(n) == 3) 6795 if (NCH(n) == 3)
6744 symtable_add_def(st, STR(CHILD(n, 2)), 6796 symtable_add_def(st, STR(CHILD(n, 2)),
6745 DEF_LOCAL | def_flag); 6797 DEF_LOCAL | def_flag);
6746 else 6798 else