1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

nested local variables

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@240 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 1998-06-12 09:35:19 +00:00
parent 1bc6f594d3
commit 83687c4eb4
5 changed files with 38 additions and 43 deletions

View file

@ -1,3 +1,7 @@
Fri Jun 12 17:58:18 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
* eval.c (eval): write back the_dyna_var into the block.
Thu Jun 11 18:19:18 1998 Yukihiro Matsumoto <matz@netlab.co.jp> Thu Jun 11 18:19:18 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
* experimental release 1.1b9_25. * experimental release 1.1b9_25.

59
eval.c
View file

@ -454,24 +454,25 @@ new_dvar(id, value)
return vars; return vars;
} }
static struct RVarmap* static void
push_dvar(id, value) push_dvar(id, value)
ID id; ID id;
VALUE value; VALUE value;
{ {
struct RVarmap* vars = new_dvar(id, value); the_dyna_vars = new_dvar(id, value);
}
if (the_dyna_vars) { static void
vars->next = the_dyna_vars->next; mark_dvar(vars)
struct RVarmap* vars;
{
if (!vars) {
the_dyna_vars = new_dvar(0, 0);
the_dyna_vars->next = vars; the_dyna_vars->next = vars;
} }
else { else {
vars->id = id;
vars->val = 0;
the_dyna_vars = vars; the_dyna_vars = vars;
} }
return vars;
} }
VALUE VALUE
@ -502,42 +503,21 @@ dyna_var_ref(id)
return Qnil; return Qnil;
} }
static void
dvar_add_compiling(id)
ID id;
{
struct RVarmap *vars = the_dyna_vars;
while (vars) {
if (vars->id == 0) break;
if (vars->id == id) {
return;
}
vars = vars->next;
}
the_dyna_vars = new_dvar(id, 0);
}
VALUE VALUE
dyna_var_asgn(id, value) dyna_var_asgn(id, value)
ID id; ID id;
VALUE value; VALUE value;
{ {
if (id == 0) { struct RVarmap *vars = the_dyna_vars;
dvar_add_compiling((ID)value);
}
else {
struct RVarmap *vars = the_dyna_vars;
while (vars) { while (vars) {
if (vars->id == id) { if (vars->id == id) {
vars->val = value; vars->val = value;
return value; return value;
}
vars = vars->next;
} }
push_dvar(id, value); vars = vars->next;
} }
push_dvar(id, value);
return value; return value;
} }
@ -2844,8 +2824,7 @@ rb_yield_0(val, self)
old_scope = the_scope; old_scope = the_scope;
the_scope = block->scope; the_scope = block->scope;
the_block = block->prev; the_block = block->prev;
the_dyna_vars = new_dvar(0, 0); mark_dvar(block->d_vars);
the_dyna_vars->next = block->d_vars;
the_class = block->klass; the_class = block->klass;
if (!self) self = block->self; if (!self) self = block->self;
node = block->body; node = block->body;
@ -3018,7 +2997,6 @@ rb_iterate(it_proc, data1, bl_proc, data2)
iter_retry: iter_retry:
PUSH_ITER(ITER_PRE); PUSH_ITER(ITER_PRE);
PUSH_BLOCK(0, node); PUSH_BLOCK(0, node);
_block.d_vars = new_dvar(0,0);
PUSH_TAG(PROT_NONE); PUSH_TAG(PROT_NONE);
state = EXEC_TAG(); state = EXEC_TAG();
@ -3819,7 +3797,7 @@ eval(self, src, scope, file, line)
old_block = the_block; old_block = the_block;
the_block = data->prev; the_block = data->prev;
old_d_vars = the_dyna_vars; old_d_vars = the_dyna_vars;
the_dyna_vars = data->d_vars; mark_dvar(data->d_vars);
old_vmode = scope_vmode; old_vmode = scope_vmode;
scope_vmode = data->vmode; scope_vmode = data->vmode;
@ -3856,6 +3834,7 @@ eval(self, src, scope, file, line)
the_frame = the_frame->prev; the_frame = the_frame->prev;
the_scope = old_scope; the_scope = old_scope;
the_block = old_block; the_block = old_block;
data->d_vars = the_dyna_vars;
the_dyna_vars = old_d_vars; the_dyna_vars = old_d_vars;
data->vmode = scope_vmode; /* write back visibility mode */ data->vmode = scope_vmode; /* write back visibility mode */
scope_vmode = old_vmode; scope_vmode = old_vmode;

View file

@ -9,5 +9,5 @@
#md5 #md5
#socket #socket
#tkutil #tkutil
tcltklib #tcltklib
#gtk #gtk

View file

@ -236,6 +236,7 @@ static void top_local_setup();
%% %%
program : { program : {
$<vars>$ = the_dyna_vars;
lex_state = EXPR_BEG; lex_state = EXPR_BEG;
top_local_init(); top_local_init();
NEW_CREF0(); /* initialize constant c-ref */ NEW_CREF0(); /* initialize constant c-ref */
@ -248,6 +249,7 @@ program : {
top_local_setup(); top_local_setup();
cur_cref = 0; cur_cref = 0;
class_nest = 0; class_nest = 0;
the_dyna_vars = $<vars>1;
} }
compstmt : stmts opt_terms compstmt : stmts opt_terms
@ -3424,7 +3426,7 @@ assignable(id, val)
} }
else{ else{
if (!dyna_var_defined(id)) { if (!dyna_var_defined(id)) {
dyna_var_asgn(0, id); dyna_var_asgn(id, 0);
} }
lhs = NEW_DASGN(id, val); lhs = NEW_DASGN(id, val);
} }

View file

@ -653,7 +653,6 @@ ok(!defined?(iii)) # out of scope
$x=0 $x=0
$proc.call(5) $proc.call(5)
$proc2.call $proc2.call
p $x
ok($x == 5) ok($x == 5)
if defined? Process.kill if defined? Process.kill
@ -739,6 +738,17 @@ rescue NameError # must raise error
end end
ok(!$bad) ok(!$bad)
x = proc{}
eval "i = 1", x
ok(eval("i", x) == 1)
x = proc{proc{}}.call
eval "i = 22", x
ok(eval("i", x) == 22)
$x = []
x = proc{proc{}}.call
eval "(0..9).each{|i4| $x[i4] = proc{i4*2}}", x
ok($x[4].call == 8)
check "system" check "system"
ok(`echo foobar` == "foobar\n") ok(`echo foobar` == "foobar\n")
ok(`./ruby -e 'print "foobar"'` == 'foobar') ok(`./ruby -e 'print "foobar"'` == 'foobar')