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

variable renaming refined.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12904 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2007-08-08 04:45:15 +00:00
parent 120a9d2f83
commit cc317d9208
3 changed files with 25 additions and 31 deletions

View file

@ -243,5 +243,17 @@ class SentGen
end
nil
end
def SentGen.subst(obj, target, &b)
if obj.respond_to? :to_ary
a = []
obj.each {|e| a << subst(e, target, &b) }
a
elsif target === obj
yield obj
else
obj
end
end
end

View file

@ -535,18 +535,14 @@ class TestAssignmentGen < Test::Unit::TestCase
[:mlhs," = ",:mrhs]],
}
def rename_var(obj, nbox=[0])
if obj.respond_to? :to_ary
a = []
obj.each {|e| a << rename_var(e, nbox) }
a
elsif obj == 'var'
n = nbox[0]
nbox[0] += 1
"v#{n}"
else
obj
end
def rename_var(obj)
vars = []
r = SentGen.subst(obj, 'var') {
var = "v#{vars.length}"
vars << var
var
}
return r, vars
end
def expand_except_paren(obj, r=[])
@ -649,10 +645,8 @@ class TestAssignmentGen < Test::Unit::TestCase
emu_assign_single(lhs, rv)
end
def do_assign(assign)
def do_assign(assign, vars)
assign = assign.join('')
vars = []
vars = assign.scan(/v[0-9]+/)
code = "#{assign}; [#{vars.join(",")}]"
begin
vals = eval(code)
@ -667,9 +661,9 @@ class TestAssignmentGen < Test::Unit::TestCase
def test_assignment
syntax = SentGen.expand_syntax(Syntax)
SentGen.each_tree(syntax, :xassign, 3) {|assign|
assign[0] = rename_var(assign[0])
assign[0], vars = rename_var(assign[0])
sent = [assign].join('')
bruby = do_assign(assign).to_a.sort
bruby = do_assign(assign, vars).to_a.sort
bemu = emu_assign(assign).to_a.sort
assert_equal(bemu, bruby, sent)
}

View file

@ -157,21 +157,9 @@ class TestRubyYieldGen < Test::Unit::TestCase
:test => [['def m(&b) b.yield', :command_args_noblock, ' end; r = m {', :block_param_def, 'vars', '}; undef m; r']]
}
def subst(obj, target, &b)
if obj.respond_to? :to_ary
a = []
obj.each {|e| a << subst(e, target, &b) }
a
elsif obj == target
yield obj
else
obj
end
end
def rename_var(obj)
vars = []
r = subst(obj, 'var') {
r = SentGen.subst(obj, 'var') {
var = "v#{vars.length}"
vars << var
var
@ -181,7 +169,7 @@ class TestRubyYieldGen < Test::Unit::TestCase
def check_nofork(t)
t, vars = rename_var(t)
t = subst(t, 'vars') { " [#{vars.join(",")}]" }
t = SentGen.subst(t, 'vars') { " [#{vars.join(",")}]" }
s = [t].join
#print "#{s}\t\t"
#STDOUT.flush