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 end
nil nil
end 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 end

View file

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