From 7567fbf080552e04df4b5d2af9d601cf1d345b11 Mon Sep 17 00:00:00 2001 From: ko1 Date: Fri, 15 Jun 2007 03:27:33 +0000 Subject: [PATCH] * vm.c (th_yield_setup_args): |v| should work as |v,|. ex) def m;yield 1, 2; end; m{|v| p v} #=> 1 * parse.y: apply above change for "for" statement. * test/ruby/test_assignment.rb: ditto * test/ruby/test_basicinstructions.rb: ditto. * test/ruby/test_iterator.rb: ditto. * test/ruby/test_yield.rb: ditto. * compile.c (iseq_compile_each): fix debug. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12544 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 17 ++++++++++++ compile.c | 2 +- parse.y | 42 ++++++++++++++++++++++++++--- test/ruby/test_assignment.rb | 26 +++++++++--------- test/ruby/test_basicinstructions.rb | 4 +-- test/ruby/test_iterator.rb | 3 ++- test/ruby/test_yield.rb | 4 +-- vm.c | 11 -------- 8 files changed, 75 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index b9b4360660..21555328a3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +Fri Jun 15 12:25:33 2007 Koichi Sasada + + * vm.c (th_yield_setup_args): |v| should work as |v,|. + ex) def m;yield 1, 2; end; m{|v| p v} #=> 1 + + * parse.y: apply above change for "for" statement. + + * test/ruby/test_assignment.rb: ditto + + * test/ruby/test_basicinstructions.rb: ditto. + + * test/ruby/test_iterator.rb: ditto. + + * test/ruby/test_yield.rb: ditto. + + * compile.c (iseq_compile_each): fix debug. + Fri Jun 15 12:22:10 2007 Koichi Sasada * eval.c (ruby_finalize_1): rb_thread_t#errinfo should be clear with diff --git a/compile.c b/compile.c index 3ce622fde9..d0fb08f8c1 100644 --- a/compile.c +++ b/compile.c @@ -3232,7 +3232,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) case NODE_DASGN_CURR:{ int idx, lv, ls; COMPILE(ret, "dvalue", node->nd_value); - debugp_param("dassn id", rb_str_new2(rb_id2name(node->nd_vid))); + debugp_param("dassn id", rb_str_new2(rb_id2name(node->nd_vid) ? rb_id2name(node->nd_vid) : "*")); if (!poped) { ADD_INSN(ret, nd_line(node), dup); diff --git a/parse.y b/parse.y index 455a69e0f4..21d9fd1f53 100644 --- a/parse.y +++ b/parse.y @@ -2691,12 +2691,46 @@ primary : literal keyword_end { /*%%%*/ + /* + * for a, b, c in e + * #=> + * e.each{|*x| a, b, c = x + * + * for a in e + * #=> + * e.each{|x| a, = x} + */ ID id = internal_id(); ID *tbl = ALLOC_N(ID, 2); - NODE *args =new_args(NEW_NODE(NODE_ARGS_AUX, 0, 1 /* nd_plen */, - node_assign($2, NEW_DVAR(id))), - 0, 0, 0, 0); - NODE *scope = NEW_NODE(NODE_SCOPE, tbl, $8, args); + 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(cond( + 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))); + } + else { + m->nd_next = node_assign(NEW_MASGN(NEW_LIST($2), 0), NEW_DVAR(id)); + } + + args = new_args(m, 0, id, 0, 0); + scope = NEW_NODE(NODE_SCOPE, tbl, $8, args); tbl[0] = 1; tbl[1] = id; $$ = NEW_FOR(0, $5, scope); fixpos($$, $2); diff --git a/test/ruby/test_assignment.rb b/test/ruby/test_assignment.rb index 69db708187..966757bc98 100644 --- a/test/ruby/test_assignment.rb +++ b/test/ruby/test_assignment.rb @@ -90,22 +90,22 @@ class TestAssignment < Test::Unit::TestCase def f; yield([*[1]]); end; f {|a| assert_equal([1], a)}; undef f def f; yield([*[1,2]]); end; f {|a| assert_equal([1,2], a)}; undef f - def f; yield(*[1]); end; f {|a| assert_equal([1], a)}; undef f - def f; yield(*[nil]); end; f {|a| assert_equal([nil], a)}; undef f - def f; yield(*[[]]); end; f {|a| assert_equal([[]], a)}; undef f - def f; yield(*[*[1]]); end; f {|a| assert_equal([1], a)}; undef f + def f; yield(*[1]); end; f {|a| assert_equal(1, a)}; undef f + def f; yield(*[nil]); end; f {|a| assert_equal(nil, a)}; undef f + def f; yield(*[[]]); end; f {|a| assert_equal([], a)}; undef f + def f; yield(*[*[1]]); end; f {|a| assert_equal(1, a)}; undef f def f; yield; end; f {|*a| assert_equal([], a)}; undef f def f; yield(nil); end; f {|*a| assert_equal([nil], a)}; undef f def f; yield(1); end; f {|*a| assert_equal([1], a)}; undef f - def f; yield([]); end; f {|*a| assert_equal([], a)}; undef f - def f; yield([1]); end; f {|*a| assert_equal([1], a)}; undef f - def f; yield([nil]); end; f {|*a| assert_equal([nil], a)}; undef f - def f; yield([[]]); end; f {|*a| assert_equal([[]], a)}; undef f - def f; yield([1,2]); end; f {|*a| assert_equal([1,2], a)}; undef f - def f; yield([*[]]); end; f {|*a| assert_equal([], a)}; undef f - def f; yield([*[1]]); end; f {|*a| assert_equal([1], a)}; undef f - def f; yield([*[1,2]]); end; f {|*a| assert_equal([1,2], a)}; undef f + def f; yield([]); end; f {|*a| assert_equal([[]], a)}; undef f + def f; yield([1]); end; f {|*a| assert_equal([[1]], a)}; undef f + def f; yield([nil]); end; f {|*a| assert_equal([[nil]], a)}; undef f + def f; yield([[]]); end; f {|*a| assert_equal([[[]]], a)}; undef f + def f; yield([1,2]); end; f {|*a| assert_equal([[1,2]], a)}; undef f + def f; yield([*[]]); end; f {|*a| assert_equal([[]], a)}; undef f + def f; yield([*[1]]); end; f {|*a| assert_equal([[1]], a)}; undef f + def f; yield([*[1,2]]); end; f {|*a| assert_equal([[1,2]], a)}; undef f def f; yield(*[]); end; f {|*a| assert_equal([], a)}; undef f def f; yield(*[1]); end; f {|*a| assert_equal([1], a)}; undef f @@ -129,7 +129,7 @@ class TestAssignment < Test::Unit::TestCase def f; yield(*[]); end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])}; undef f def f; yield(*[1]); end; f {|a,b,*c| assert_equal([1,nil,[]], [a,b,c])}; undef f def f; yield(*[nil]); end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])}; undef f - def f; yield(*[[]]); end; f {|a,b,*c| assert_equal([[],nil,[]], [a,b,c])}; undef f + def f; yield(*[[]]); end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])}; undef f def f; yield(*[*[]]); end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])}; undef f def f; yield(*[*[1]]); end; f {|a,b,*c| assert_equal([1,nil,[]], [a,b,c])}; undef f def f; yield(*[*[1,2]]); end; f {|a,b,*c| assert_equal([1,2,[]], [a,b,c])}; undef f diff --git a/test/ruby/test_basicinstructions.rb b/test/ruby/test_basicinstructions.rb index 3e52ef62d3..4d3f03049f 100644 --- a/test/ruby/test_basicinstructions.rb +++ b/test/ruby/test_basicinstructions.rb @@ -621,8 +621,8 @@ class TestBasicInstructions < Test::Unit::TestCase assert_equal [1, 2, 3], [1, *a] a = nil - assert_equal [nil], [*a] # FIXME: []? [nil]? error? - assert_equal [1], [1, *a] # FIXME: [1, nil]? error? + assert_equal [], [*a] + assert_equal [1], [1, *a] end end diff --git a/test/ruby/test_iterator.rb b/test/ruby/test_iterator.rb index cc0fe76d7b..19677a4fd9 100644 --- a/test/ruby/test_iterator.rb +++ b/test/ruby/test_iterator.rb @@ -365,7 +365,7 @@ class TestIterator < Test::Unit::TestCase def test_assoc_yield [{:key=>:value}, H.new].each {|h| - h.each{|a| assert_equal([:key, :value], a)} + h.each{|a| assert_equal(:key, a)} # changed at 1.9 h.each{|*a| assert_equal([:key, :value], a)} h.each{|k,v| assert_equal([:key, :value], [k,v])} } @@ -480,3 +480,4 @@ class TestIterator < Test::Unit::TestCase assert_equal(["b"], ["a", "b", "c"].grep(IterString.new("b")) {|s| s}) end end +GC.stress=true diff --git a/test/ruby/test_yield.rb b/test/ruby/test_yield.rb index bc00407e4d..e5e288be1a 100644 --- a/test/ruby/test_yield.rb +++ b/test/ruby/test_yield.rb @@ -16,8 +16,8 @@ class TestRubyYield < Test::Unit::TestCase assert_equal :a, k assert_equal 1, v end - h.each do |kv| - assert_equal [:a, 1], kv + h.each do |k| + assert_equal :a, k # changed at 1.9 end end diff --git a/vm.c b/vm.c index 94d3034dbf..7bd77399ee 100644 --- a/vm.c +++ b/vm.c @@ -718,17 +718,6 @@ th_yield_setup_args(rb_thread_t *th, rb_iseq_t *iseq, } if (iseq->arg_rest == -1) { - if (lambda == 0 && iseq->argc == 1) { - if (argc > 1) { - /* yield 1, 2, 3 for iter{|a| ...} - * - * ruby 1.8 warns on this timing. - * rb_warn("multiple values for a block parameter (%d for %d)", argc, iseq->argc); - */ - argv[0] = rb_ary_new4(argc, argv); - th->mark_stack_len = argc = 1; - } - } if (iseq->argc < argc) { if (lambda) {