Project

General

Profile

Feature #6070 » for_loop_scope_fix_0224.diff

shugo (Shugo Maeda), 02/25/2012 11:42 PM

View differences:

compile.c
iseq->compile_data->ensure_node_stack = iseq->compile_data->ensure_node_stack->prev;
break;
}
case NODE_ITER:
case NODE_FOR:{
case NODE_ITER:{
VALUE prevblock = iseq->compile_data->current_block;
LABEL *retry_label = NEW_LABEL(nd_line(node));
LABEL *retry_end_l = NEW_LABEL(nd_line(node));
ADD_LABEL(ret, retry_label);
if (nd_type(node) == NODE_FOR) {
COMPILE(ret, "iter caller (for)", node->nd_iter);
iseq->compile_data->current_block =
NEW_CHILD_ISEQVAL(node->nd_body, make_name_for_block(iseq),
ISEQ_TYPE_BLOCK, nd_line(node));
ADD_SEND_R(ret, nd_line(node), ID2SYM(idEach), INT2FIX(0),
iseq->compile_data->current_block, INT2FIX(0));
}
else {
iseq->compile_data->current_block =
NEW_CHILD_ISEQVAL(node->nd_body, make_name_for_block(iseq),
ISEQ_TYPE_BLOCK, nd_line(node));
COMPILE(ret, "iter caller", node->nd_iter);
}
iseq->compile_data->current_block =
NEW_CHILD_ISEQVAL(node->nd_body, make_name_for_block(iseq),
ISEQ_TYPE_BLOCK, nd_line(node));
COMPILE(ret, "iter caller", node->nd_iter);
ADD_LABEL(ret, retry_end_l);
if (poped) {
ext/objspace/objspace.c
COUNT_NODE(NODE_WHILE);
COUNT_NODE(NODE_UNTIL);
COUNT_NODE(NODE_ITER);
COUNT_NODE(NODE_FOR);
COUNT_NODE(NODE_BREAK);
COUNT_NODE(NODE_NEXT);
COUNT_NODE(NODE_REDO);
gc.c
case T_NODE:
switch (nd_type(obj)) {
case NODE_IF: /* 1,2,3 */
case NODE_FOR:
case NODE_ITER:
case NODE_WHEN:
case NODE_MASGN:
lib/rss/parser.rb
if a_uri.is_a?(String) or !a_uri.respond_to?(:include?)
a_uri = [a_uri]
end
val = nil
unless a_uri == [""]
for prefix, uri in ns
if a_uri.include?(uri)
node.c
ANN("method call with block");
ANN("format: [nd_iter] { [nd_body] }");
ANN("example: 3.times { foo }");
goto iter;
case NODE_FOR:
ANN("for statement");
ANN("format: for * in [nd_iter] do [nd_body] end");
ANN("example: for i in 1..3 do foo end");
iter:
F_NODE(nd_iter, "iteration receiver");
LAST_NODE;
F_NODE(nd_body, "body");
node.h
#define NODE_UNTIL NODE_UNTIL
NODE_ITER,
#define NODE_ITER NODE_ITER
NODE_FOR,
#define NODE_FOR NODE_FOR
NODE_BREAK,
#define NODE_BREAK NODE_BREAK
NODE_NEXT,
......
#define NEW_OPT_N(b) NEW_NODE(NODE_OPT_N,0,b,0)
#define NEW_WHILE(c,b,n) NEW_NODE(NODE_WHILE,c,b,n)
#define NEW_UNTIL(c,b,n) NEW_NODE(NODE_UNTIL,c,b,n)
#define NEW_FOR(v,i,b) NEW_NODE(NODE_FOR,v,b,i)
#define NEW_ITER(a,b) NEW_NODE(NODE_ITER,0,NEW_SCOPE(a,b),0)
#define NEW_LAMBDA(a,b) NEW_NODE(NODE_LAMBDA,0,NEW_SCOPE(a,b),0)
#define NEW_BREAK(s) NEW_NODE(NODE_BREAK,s,0,0)
parse.y
%type <node> command_asgn mrhs superclass block_call block_command
%type <node> f_block_optarg f_block_opt
%type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
%type <node> assoc_list assocs assoc undef_list backref string_dvar
%type <node> block_param opt_block_param block_param_def f_opt
%type <node> f_kwarg f_kw f_block_kwarg f_block_kw
%type <node> bv_decls opt_bv_decl bvar
......
$$ = dispatch2(case, Qnil, $3);
%*/
}
| k_for for_var keyword_in
{COND_PUSH(1);}
| k_for
{
$<vars>1 = dyna_push();
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
block_param keyword_in
{
COND_PUSH(1);
$<node>$ = NEW_NODE(NODE_MEMO, lvtbl->args, lvtbl->vars,
lvtbl->used);
lvtbl->args = lvtbl->args->prev;
lvtbl->vars = lvtbl->vars->prev;
if (lvtbl->used) {
lvtbl->used = lvtbl->used->prev;
}
}
expr_value do
{COND_POP();}
{
lvtbl->args = (struct vtable *) $<node>5->u1.value;
lvtbl->vars = (struct vtable *) $<node>5->u2.value;
lvtbl->used = (struct vtable *) $<node>5->u3.value;
COND_POP();
}
compstmt
k_end
{
......
* #=>
* e.each{|x| a, = x}
*/
ID id = internal_id();
ID *tbl = ALLOC_N(ID, 2);
NODE *m = NEW_ARGS_AUX(0, 0);
NODE *args, *scope;
if (nd_type($2) == NODE_MASGN) {
/* if args.length == 1 && args[0].kind_of?(Array)
* args = args[0]
* end
*/
NODE *one = NEW_LIST(NEW_LIT(INT2FIX(1)));
NODE *zero = NEW_LIST(NEW_LIT(INT2FIX(0)));
m->nd_next = block_append(
NEW_IF(
NEW_NODE(NODE_AND,
NEW_CALL(NEW_CALL(NEW_DVAR(id), rb_intern("length"), 0),
rb_intern("=="), one),
NEW_CALL(NEW_CALL(NEW_DVAR(id), rb_intern("[]"), zero),
rb_intern("kind_of?"), NEW_LIST(NEW_LIT(rb_cArray))),
0),
NEW_DASGN_CURR(id,
NEW_CALL(NEW_DVAR(id), rb_intern("[]"), zero)),
0),
node_assign($2, NEW_DVAR(id)));
args = new_args(m, 0, id, 0, 0, 0, 0);
}
else {
if (nd_type($2) == NODE_LASGN ||
nd_type($2) == NODE_DASGN ||
nd_type($2) == NODE_DASGN_CURR) {
$2->nd_value = NEW_DVAR(id);
m->nd_plen = 1;
m->nd_next = $2;
args = new_args(m, 0, 0, 0, 0, 0, 0);
}
else {
m->nd_next = node_assign(NEW_MASGN(NEW_LIST($2), 0), NEW_DVAR(id));
args = new_args(m, 0, id, 0, 0, 0, 0);
}
}
scope = NEW_NODE(NODE_SCOPE, tbl, $8, args);
tbl[0] = 1; tbl[1] = id;
$$ = NEW_FOR(0, $5, scope);
fixpos($$, $2);
$$ = NEW_ITER($3, $9);
$$->nd_iter = NEW_CALL($6, rb_intern("each"), 0);
nd_set_line($$, $<num>2);
/*%
$$ = dispatch3(for, $2, $5, $8);
$$ = dispatch3(for, $3, $6, $9);
%*/
dyna_pop($<vars>1);
}
| k_class cpath superclass
{
......
}
;
for_var : lhs
| mlhs
;
f_marg : f_norm_arg
{
$$ = assignable($1, 0);
sample/test.rb
test_ok($x[4].call == 8)
x = Proc.new{binding}.call
eval "for i6 in 1..1; j6=i6; end", x
test_ok(eval("defined? i6", x))
test_ok(eval("defined? j6", x))
test_ok(eval("defined? i6", x) == nil)
test_ok(eval("defined? j6", x) == nil)
Proc.new {
p = binding
test/ruby/test_eval.rb
assert_equal(8, $x[4].call)
x = proc{binding}.call
eval "for i6 in 1..1; j6=i6; end", x
assert(eval("defined? i6", x))
assert(eval("defined? j6", x))
assert_equal(nil, eval("defined? i6", x))
assert_equal(nil, eval("defined? j6", x))
proc {
p = binding
(4-4/4)