mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* test/ruby/sentence.rb (Sentence): include Enumerable.
(Sentence#each): defined. * test/ruby/test_assignment.rb: use Sentence#expand. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13253 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
85e3b47032
commit
4c86818f47
3 changed files with 77 additions and 55 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
Sat Aug 25 03:49:14 2007 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* test/ruby/sentence.rb (Sentence): include Enumerable.
|
||||||
|
(Sentence#each): defined.
|
||||||
|
|
||||||
|
* test/ruby/test_assignment.rb: use Sentence#expand.
|
||||||
|
|
||||||
Sat Aug 25 03:08:57 2007 Koichi Sasada <ko1@atdot.net>
|
Sat Aug 25 03:08:57 2007 Koichi Sasada <ko1@atdot.net>
|
||||||
|
|
||||||
* prelude.rb: fix Mutex#synchronize definition.
|
* prelude.rb: fix Mutex#synchronize definition.
|
||||||
|
|
|
@ -170,6 +170,21 @@ class Sentence
|
||||||
@sent.length
|
@sent.length
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# iterates over children.
|
||||||
|
#
|
||||||
|
# Sentence(%w[2 * 7], "+", %w[3 * 5]).each {|v| p v }
|
||||||
|
# #=>
|
||||||
|
# #<Sentence: "2" "*" "7">
|
||||||
|
# "+"
|
||||||
|
# #<Sentence: "3" "*" "5">
|
||||||
|
#
|
||||||
|
def each # :yield: element
|
||||||
|
@sent.each_index {|i|
|
||||||
|
yield self[i]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
include Enumerable
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"#<#{self.class}: #{inner_inspect(@sent, '')}>"
|
"#<#{self.class}: #{inner_inspect(@sent, '')}>"
|
||||||
end
|
end
|
||||||
|
@ -206,7 +221,7 @@ class Sentence
|
||||||
# Sentence.new(%w[2 + 3]).subst(/\A\d+\z/) {|s| ((s.to_i)*2).to_s }
|
# Sentence.new(%w[2 + 3]).subst(/\A\d+\z/) {|s| ((s.to_i)*2).to_s }
|
||||||
# #=> #<Sentence: "4" "+" "6">
|
# #=> #<Sentence: "4" "+" "6">
|
||||||
#
|
#
|
||||||
def subst(target, &b)
|
def subst(target, &b) # :yield: string
|
||||||
Sentence.new(subst_rec(@sent, target, &b))
|
Sentence.new(subst_rec(@sent, target, &b))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -231,7 +246,7 @@ class Sentence
|
||||||
# Sentence(%w[2 * 7], "+", %w[3 * 5]).find_subtree {|s| s[1] == "*" }
|
# Sentence(%w[2 * 7], "+", %w[3 * 5]).find_subtree {|s| s[1] == "*" }
|
||||||
# #=> #<Sentence: "2" "*" "7">
|
# #=> #<Sentence: "2" "*" "7">
|
||||||
#
|
#
|
||||||
def find_subtree(&b)
|
def find_subtree(&b) # :yield: sentence
|
||||||
find_subtree_rec(@sent, &b)
|
find_subtree_rec(@sent, &b)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -268,25 +283,23 @@ class Sentence
|
||||||
# s.expand {|s| s[0] == "3" }
|
# s.expand {|s| s[0] == "3" }
|
||||||
# #=> #<Sentence: (("2" "*" "7") "+" "3" "*" "5")>
|
# #=> #<Sentence: (("2" "*" "7") "+" "3" "*" "5")>
|
||||||
#
|
#
|
||||||
def expand(&b)
|
def expand(&b) # :yield: sentence
|
||||||
Sentence.new(expand_rec(@sent, &b))
|
Sentence.new(expand_rec(@sent, &b))
|
||||||
end
|
end
|
||||||
|
|
||||||
# :stopdoc:
|
# :stopdoc:
|
||||||
def expand_rec(obj, r=[], &b)
|
def expand_rec(obj, r=[], &b)
|
||||||
if obj.respond_to? :to_ary
|
if obj.respond_to? :to_ary
|
||||||
s = Sentence.new(obj)
|
obj.each {|o|
|
||||||
if b.call s
|
s = Sentence.new(o)
|
||||||
obj.each {|o|
|
if b.call s
|
||||||
expand_rec(o, r, &b)
|
expand_rec(o, r, &b)
|
||||||
}
|
else
|
||||||
else
|
a = []
|
||||||
a = []
|
|
||||||
obj.each {|o|
|
|
||||||
expand_rec(o, a, &b)
|
expand_rec(o, a, &b)
|
||||||
}
|
r << a
|
||||||
r << a
|
end
|
||||||
end
|
}
|
||||||
else
|
else
|
||||||
r << obj
|
r << obj
|
||||||
end
|
end
|
||||||
|
@ -482,16 +495,10 @@ class Sentence
|
||||||
|
|
||||||
def self.expand_emptyable_syms(rhs, emptyable_syms)
|
def self.expand_emptyable_syms(rhs, emptyable_syms)
|
||||||
if rhs.empty?
|
if rhs.empty?
|
||||||
elsif rhs.length == 1
|
yield []
|
||||||
if emptyable_syms[rhs[0]]
|
|
||||||
yield rhs
|
|
||||||
yield []
|
|
||||||
else
|
|
||||||
yield rhs
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
rest = rhs.dup
|
first = rhs[0]
|
||||||
first = rest.shift
|
rest = rhs[1..-1]
|
||||||
if emptyable_syms[first]
|
if emptyable_syms[first]
|
||||||
expand_emptyable_syms(rest, emptyable_syms) {|rhs2|
|
expand_emptyable_syms(rest, emptyable_syms) {|rhs2|
|
||||||
yield [first] + rhs2
|
yield [first] + rhs2
|
||||||
|
|
|
@ -545,23 +545,12 @@ class TestAssignmentGen < Test::Unit::TestCase
|
||||||
return r, vars
|
return r, vars
|
||||||
end
|
end
|
||||||
|
|
||||||
def expand_except_paren(obj, r=[])
|
def expand_except_paren(obj)
|
||||||
if obj.respond_to? :to_ary
|
return obj if obj.respond_to? :to_str
|
||||||
if (obj[0] == '(' && obj[-1] == ')') || (obj[0] == '[' && obj[-1] == ']')
|
obj.expand {|s|
|
||||||
a = []
|
!(s[0] == '(' && s[-1] == ')') &&
|
||||||
obj[1...-1].each {|o|
|
!(s[0] == '[' && s[-1] == ']')
|
||||||
expand_except_paren(o, a)
|
}
|
||||||
}
|
|
||||||
r << a
|
|
||||||
else
|
|
||||||
obj.each {|o|
|
|
||||||
expand_except_paren(o, r)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
else
|
|
||||||
r << obj
|
|
||||||
end
|
|
||||||
r
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_single_element(ary)
|
def extract_single_element(ary)
|
||||||
|
@ -593,25 +582,34 @@ class TestAssignmentGen < Test::Unit::TestCase
|
||||||
pre << e
|
pre << e
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
pre.map! {|e| extract_single_element(e) }
|
||||||
|
if star
|
||||||
|
if star == ['*']
|
||||||
|
star = nil
|
||||||
|
else
|
||||||
|
star = extract_single_element(star[1..-1])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
post.map! {|e| extract_single_element(e) } if post
|
||||||
|
|
||||||
until pre.empty?
|
until pre.empty?
|
||||||
emu_assign_single(extract_single_element(pre.shift), rv.shift, h)
|
emu_assign_single(pre.shift, rv.shift, h)
|
||||||
end
|
end
|
||||||
|
|
||||||
if post
|
if post
|
||||||
if rv.length < post.length
|
if rv.length < post.length
|
||||||
until post.empty?
|
until post.empty?
|
||||||
emu_assign_single(extract_single_element(post.shift), rv.shift, h)
|
emu_assign_single(post.shift, rv.shift, h)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
until post.empty?
|
until post.empty?
|
||||||
emu_assign_single(extract_single_element(post.pop), rv.pop, h)
|
emu_assign_single(post.pop, rv.pop, h)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if star && 1 < star.length
|
if star
|
||||||
emu_assign_single(extract_single_element(star[1..-1]), rv, h)
|
emu_assign_single(star, rv, h)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -622,8 +620,14 @@ class TestAssignmentGen < Test::Unit::TestCase
|
||||||
else
|
else
|
||||||
raise "unexpected lhs string: #{lhs.inspect}"
|
raise "unexpected lhs string: #{lhs.inspect}"
|
||||||
end
|
end
|
||||||
elsif lhs.respond_to? :to_ary
|
elsif Sentence === lhs
|
||||||
emu_assign_ary(lhs, rv, h)
|
if lhs[0] == '(' && lhs[-1] == ')'
|
||||||
|
emu_assign_ary(lhs[1...-1], rv, h)
|
||||||
|
elsif lhs.length == 1 && String === lhs[0] && /\A[a-z0-9]+\z/ =~ lhs[0]
|
||||||
|
h[lhs[0]] = rv
|
||||||
|
else
|
||||||
|
raise "unexpected lhs sentence: #{lhs.inspect}"
|
||||||
|
end
|
||||||
else
|
else
|
||||||
raise "unexpected lhs: #{lhs.inspect}"
|
raise "unexpected lhs: #{lhs.inspect}"
|
||||||
end
|
end
|
||||||
|
@ -633,9 +637,9 @@ class TestAssignmentGen < Test::Unit::TestCase
|
||||||
def emu_assign(assign)
|
def emu_assign(assign)
|
||||||
lhs = expand_except_paren(assign[0])
|
lhs = expand_except_paren(assign[0])
|
||||||
rhs = expand_except_paren(assign[2])
|
rhs = expand_except_paren(assign[2])
|
||||||
lopen = lhs.any? {|e| e == '*' || e == ',' }
|
lopen = Sentence === lhs && lhs[-1] != ')' && lhs.any? {|e| e == '*' || e == ',' }
|
||||||
ropen = rhs.any? {|e| e == '*' || e == ',' }
|
ropen = Sentence === rhs && rhs[-1] != ']' && rhs.any? {|e| e == '*' || e == ',' }
|
||||||
lhs = extract_single_element(lhs) if !lopen
|
lhs = Sentence.new(['(']+lhs.to_a+[')']) if lopen
|
||||||
begin
|
begin
|
||||||
rv = eval((ropen ? ["[",assign[2],"]"] : assign[2]).join(''))
|
rv = eval((ropen ? ["[",assign[2],"]"] : assign[2]).join(''))
|
||||||
rescue Exception
|
rescue Exception
|
||||||
|
@ -657,14 +661,18 @@ class TestAssignmentGen < Test::Unit::TestCase
|
||||||
h
|
h
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def check(assign)
|
||||||
|
assign, vars = rename_var(assign)
|
||||||
|
sent = assign.to_s
|
||||||
|
bruby = do_assign(assign, vars).to_a.sort
|
||||||
|
bemu = emu_assign(assign).to_a.sort
|
||||||
|
assert_equal(bemu, bruby, sent)
|
||||||
|
end
|
||||||
|
|
||||||
def test_assignment
|
def test_assignment
|
||||||
syntax = Sentence.expand_syntax(Syntax)
|
syntax = Sentence.expand_syntax(Syntax)
|
||||||
Sentence.each(syntax, :xassign, 3) {|assign|
|
Sentence.each(syntax, :xassign, 4) {|assign|
|
||||||
assign, vars = rename_var(assign)
|
check(assign)
|
||||||
sent = assign.to_s
|
|
||||||
bruby = do_assign(assign, vars).to_a.sort
|
|
||||||
bemu = emu_assign(assign.to_a).to_a.sort
|
|
||||||
assert_equal(bemu, bruby, sent)
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue