* eval.c (rb_eval): *a = [1,2] now assigns [[1,2]] to a.

consistent with *a = [1], which set [[1]] to a.

* node.h: merge NODE_RESTARY to NODE_SPLAT.

* parse.y: rules simplified a bit by removing NODE_RESTARY.

* sample/test.rb: updated for new assignment behavior.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4452 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2003-08-27 19:43:50 +00:00
Родитель a5729ea05a
Коммит 22b955d9bf
6 изменённых файлов: 82 добавлений и 49 удалений

Просмотреть файл

@ -1,3 +1,14 @@
Thu Aug 28 02:37:45 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
* eval.c (rb_eval): *a = [1,2] now assigns [[1,2]] to a.
consistent with *a = [1], which set [[1]] to a.
* node.h: merge NODE_RESTARY to NODE_SPLAT.
* parse.y: rules simplified a bit by removing NODE_RESTARY.
* sample/test.rb: updated for new assignment behavior.
Wed Aug 27 22:33:24 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* error.c (rb_bug): should not use other methods; this function is

18
eval.c
Просмотреть файл

@ -2348,7 +2348,7 @@ svalue_to_mrhs(v, lhs)
return rb_ary_new3(1, v);
}
/* no lhs means splat lhs only */
if (!lhs && RARRAY(tmp)->len <= 1) {
if (!lhs) {
return rb_ary_new3(1, v);
}
return tmp;
@ -2399,8 +2399,8 @@ static VALUE
splat_value(v)
VALUE v;
{
if (NIL_P(v)) return rb_ary_new3(1, Qnil);
return rb_Array(v);
if (NIL_P(v)) return rb_ary_new3(1, Qnil);
return rb_Array(v);
}
static VALUE
@ -2784,17 +2784,12 @@ rb_eval(self, n)
JUMP_TAG(TAG_RETRY);
break;
case NODE_RESTARY:
case NODE_RESTARY2:
case NODE_SPLAT:
result = splat_value(rb_eval(self, node->nd_head));
break;
case NODE_SPLAT:
result = avalue_splat(splat_value(rb_eval(self, node->nd_head)));
break;
case NODE_SVALUE:
result = rb_eval(self, node->nd_head);
result = avalue_splat(rb_eval(self, node->nd_head));
if (result == Qundef) result = Qnil;
break;
@ -3157,8 +3152,7 @@ rb_eval(self, n)
break;
case NODE_MASGN:
result = svalue_to_mrhs(rb_eval(self, node->nd_value), node->nd_head);
result = massign(self, node, result, 0);
result = massign(self, node, rb_eval(self, node->nd_value), 0);
break;
case NODE_LASGN:

4
node.h
Просмотреть файл

@ -87,8 +87,6 @@ enum node_type {
NODE_ARGS,
NODE_ARGSCAT,
NODE_ARGSPUSH,
NODE_RESTARY,
NODE_RESTARY2,
NODE_SPLAT,
NODE_SVALUE,
NODE_BLOCK_ARG,
@ -309,8 +307,6 @@ typedef struct RNode {
#define NEW_ARGS(f,o,r) NEW_NODE(NODE_ARGS,o,r,f)
#define NEW_ARGSCAT(a,b) NEW_NODE(NODE_ARGSCAT,a,b,0)
#define NEW_ARGSPUSH(a,b) NEW_NODE(NODE_ARGSPUSH,a,b,0)
#define NEW_RESTARY(a) NEW_NODE(NODE_RESTARY,a,0,0)
#define NEW_RESTARY2(a) NEW_NODE(NODE_RESTARY2,a,0,0)
#define NEW_SPLAT(a) NEW_NODE(NODE_SPLAT,a,0,0)
#define NEW_SVALUE(a) NEW_NODE(NODE_SVALUE,a,0,0)
#define NEW_BLOCK_ARG(v) NEW_NODE(NODE_BLOCK_ARG,v,0,local_cnt(v))

22
parse.y
Просмотреть файл

@ -491,7 +491,7 @@ stmt : kALIAS fitem {lex_state = EXPR_FNAME;} fitem
| mlhs '=' command_call
{
value_expr($3);
$1->nd_value = NEW_RESTARY($3);
$1->nd_value = ($1->nd_head) ? NEW_SPLAT($3) : NEW_ARRAY($3);
$$ = $1;
}
| var_lhs tOP_ASGN command_call
@ -583,7 +583,7 @@ stmt : kALIAS fitem {lex_state = EXPR_FNAME;} fitem
}
| mlhs '=' arg_value
{
$1->nd_value = $3;
$1->nd_value = ($1->nd_head) ? NEW_SPLAT($3) : NEW_ARRAY($3);
$$ = $1;
}
| mlhs '=' mrhs
@ -1225,7 +1225,7 @@ aref_args : none
| tSTAR arg opt_nl
{
value_expr($2);
$$ = NEW_RESTARY2($2);
$$ = NEW_BEGIN($2);
}
;
@ -1290,7 +1290,7 @@ call_args : command
}
| tSTAR arg_value opt_block_arg
{
$$ = arg_blk_pass(NEW_RESTARY($2), $3);
$$ = arg_blk_pass(NEW_SPLAT($2), $3);
}
| block_arg
;
@ -1345,7 +1345,7 @@ call_args2 : arg_value ',' args opt_block_arg
}
| tSTAR arg_value opt_block_arg
{
$$ = arg_blk_pass(NEW_RESTARY($2), $3);
$$ = arg_blk_pass(NEW_SPLAT($2), $3);
}
| block_arg
;
@ -5395,8 +5395,8 @@ ret_args(node)
if (nd_type(node) == NODE_ARRAY && node->nd_next == 0) {
node = node->nd_head;
}
if (nd_type(node) == NODE_RESTARY) {
nd_set_type(node, NODE_SPLAT);
if (nd_type(node) == NODE_SPLAT) {
node = NEW_SVALUE(node);
}
}
return node;
@ -5414,9 +5414,8 @@ new_yield(node)
node = node->nd_head;
state = Qfalse;
}
if (nd_type(node) == NODE_RESTARY) {
nd_set_type(node, NODE_SPLAT);
state = Qfalse;
if (nd_type(node) == NODE_SPLAT) {
state = Qtrue;
}
}
else {
@ -5465,8 +5464,7 @@ arg_prepend(node1, node2)
case NODE_ARRAY:
return list_concat(NEW_LIST(node1), node2);
case NODE_RESTARY:
case NODE_RESTARY2:
case NODE_SPLAT:
return arg_concat(node1, node2->nd_head);
case NODE_BLOCK_PASS:

Просмотреть файл

@ -71,10 +71,10 @@ a = *[*[1,2]]; test_ok(a == [1,2])
*a = [1]; test_ok(a == [[1]])
*a = [nil]; test_ok(a == [[nil]])
*a = [[]]; test_ok(a == [[[]]])
*a = [1,2]; test_ok(a == [1,2])
*a = [1,2]; test_ok(a == [[1,2]])
*a = [*[]]; test_ok(a == [[]])
*a = [*[1]]; test_ok(a == [[1]])
*a = [*[1,2]]; test_ok(a == [1,2])
*a = [*[1,2]]; test_ok(a == [[1,2]])
*a = *nil; test_ok(a == [nil])
*a = *1; test_ok(a == [1])
@ -103,7 +103,7 @@ a,b,*c = *1; test_ok([a,b,c] == [1,nil,[]])
a,b,*c = *[]; test_ok([a,b,c] == [nil,nil,[]])
a,b,*c = *[1]; test_ok([a,b,c] == [1,nil,[]])
a,b,*c = *[nil]; test_ok([a,b,c] == [nil,nil,[]])
a,b,*c = *[[]]; test_ok([a,b,c] == [nil,nil,[]])
a,b,*c = *[[]]; test_ok([a,b,c] == [[],nil,[]])
a,b,*c = *[1,2]; test_ok([a,b,c] == [1,2,[]])
a,b,*c = *[*[]]; test_ok([a,b,c] == [nil,nil,[]])
a,b,*c = *[*[1]]; test_ok([a,b,c] == [1,nil,[]])
@ -133,10 +133,10 @@ def f; yield []; end; f {|*a| test_ok(a == [[]])}
def f; yield [1]; end; f {|*a| test_ok(a == [[1]])}
def f; yield [nil]; end; f {|*a| test_ok(a == [[nil]])}
def f; yield [[]]; end; f {|*a| test_ok(a == [[[]]])}
def f; yield [1,2]; end; f {|*a| test_ok(a == [1,2])}
def f; yield [1,2]; end; f {|*a| test_ok(a == [[1,2]])}
def f; yield [*[]]; end; f {|*a| test_ok(a == [[]])}
def f; yield [*[1]]; end; f {|*a| test_ok(a == [[1]])}
def f; yield [*[1,2]]; end; f {|*a| test_ok(a == [1,2])}
def f; yield [*[1,2]]; end; f {|*a| test_ok(a == [[1,2]])}
def f; yield *nil; end; f {|*a| test_ok(a == [nil])}
def f; yield *1; end; f {|*a| test_ok(a == [1])}
@ -164,7 +164,7 @@ def f; yield *1; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])}
def f; yield *[]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
def f; yield *[1]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])}
def f; yield *[nil]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
def f; yield *[[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
def f; yield *[[]]; end; f {|a,b,*c| test_ok([a,b,c] == [[],nil,[]])}
def f; yield *[*[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
def f; yield *[*[1]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])}
def f; yield *[*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,2,[]])}
@ -190,6 +190,16 @@ def r; return *[*[]]; end; a = r(); test_ok(a == nil)
def r; return *[*[1]]; end; a = r(); test_ok(a == 1)
def r; return *[*[1,2]]; end; a = r(); test_ok(a == [1,2])
def r; return *nil; end; a = *r(); test_ok(a == nil)
def r; return *1; end; a = *r(); test_ok(a == 1)
def r; return *[]; end; a = *r(); test_ok(a == nil)
def r; return *[1]; end; a = *r(); test_ok(a == 1)
def r; return *[nil]; end; a = *r(); test_ok(a == nil)
def r; return *[[]]; end; a = *r(); test_ok(a == nil)
def r; return *[*[]]; end; a = *r(); test_ok(a == nil)
def r; return *[*[1]]; end; a = *r(); test_ok(a == 1)
def r; return *[*[1,2]]; end; a = *r(); test_ok(a == [1,2])
def r; return; end; *a = r(); test_ok(a == [nil])
def r; return nil; end; *a = r(); test_ok(a == [nil])
def r; return 1; end; *a = r(); test_ok(a == [1])
@ -197,10 +207,10 @@ def r; return []; end; *a = r(); test_ok(a == [[]])
def r; return [1]; end; *a = r(); test_ok(a == [[1]])
def r; return [nil]; end; *a = r(); test_ok(a == [[nil]])
def r; return [[]]; end; *a = r(); test_ok(a == [[[]]])
def r; return [1,2]; end; *a = r(); test_ok(a == [1,2])
def r; return [1,2]; end; *a = r(); test_ok(a == [[1,2]])
def r; return [*[]]; end; *a = r(); test_ok(a == [[]])
def r; return [*[1]]; end; *a = r(); test_ok(a == [[1]])
def r; return [*[1,2]]; end; *a = r(); test_ok(a == [1,2])
def r; return [*[1,2]]; end; *a = r(); test_ok(a == [[1,2]])
def r; return *nil; end; *a = r(); test_ok(a == [nil])
def r; return *1; end; *a = r(); test_ok(a == [1])
@ -208,10 +218,21 @@ def r; return *[]; end; *a = r(); test_ok(a == [nil])
def r; return *[1]; end; *a = r(); test_ok(a == [1])
def r; return *[nil]; end; *a = r(); test_ok(a == [nil])
def r; return *[[]]; end; *a = r(); test_ok(a == [[]])
def r; return *[1,2]; end; *a = r(); test_ok(a == [1,2])
def r; return *[1,2]; end; *a = r(); test_ok(a == [[1,2]])
def r; return *[*[]]; end; *a = r(); test_ok(a == [nil])
def r; return *[*[1]]; end; *a = r(); test_ok(a == [1])
def r; return *[*[1,2]]; end; *a = r(); test_ok(a == [1,2])
def r; return *[*[1,2]]; end; *a = r(); test_ok(a == [[1,2]])
def r; return *nil; end; *a = *r(); test_ok(a == [nil])
def r; return *1; end; *a = *r(); test_ok(a == [1])
def r; return *[]; end; *a = *r(); test_ok(a == [nil])
def r; return *[1]; end; *a = *r(); test_ok(a == [1])
def r; return *[nil]; end; *a = *r(); test_ok(a == [nil])
def r; return *[[]]; end; *a = *r(); test_ok(a == [])
def r; return *[1,2]; end; *a = *r(); test_ok(a == [1,2])
def r; return *[*[]]; end; *a = *r(); test_ok(a == [nil])
def r; return *[*[1]]; end; *a = *r(); test_ok(a == [1])
def r; return *[*[1,2]]; end; *a = *r(); test_ok(a == [1,2])
def r; return; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
def r; return nil; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
@ -264,9 +285,9 @@ test_ok(f.call(42,55) == [42,55])
a,=*[1]
test_ok(a == 1)
a,=*[[1]]
test_ok(a == 1)
a,=*[[[1]]]
test_ok(a == [1])
a,=*[[[1]]]
test_ok(a == [[1]])
x, (y, z) = 1, 2, 3
test_ok([1,2,nil] == [x,y,z])
@ -303,10 +324,10 @@ a = loop do break *[*[1,2]]; end; test_ok(a == [1,2])
*a = loop do break [1]; end; test_ok(a == [[1]])
*a = loop do break [nil]; end; test_ok(a == [[nil]])
*a = loop do break [[]]; end; test_ok(a == [[[]]])
*a = loop do break [1,2]; end; test_ok(a == [1,2])
*a = loop do break [1,2]; end; test_ok(a == [[1,2]])
*a = loop do break [*[]]; end; test_ok(a == [[]])
*a = loop do break [*[1]]; end; test_ok(a == [[1]])
*a = loop do break [*[1,2]]; end; test_ok(a == [1,2])
*a = loop do break [*[1,2]]; end; test_ok(a == [[1,2]])
*a = loop do break *nil; end; test_ok(a == [nil])
*a = loop do break *1; end; test_ok(a == [1])
@ -314,10 +335,21 @@ a = loop do break *[*[1,2]]; end; test_ok(a == [1,2])
*a = loop do break *[1]; end; test_ok(a == [1])
*a = loop do break *[nil]; end; test_ok(a == [nil])
*a = loop do break *[[]]; end; test_ok(a == [[]])
*a = loop do break *[1,2]; end; test_ok(a == [1,2])
*a = loop do break *[1,2]; end; test_ok(a == [[1,2]])
*a = loop do break *[*[]]; end; test_ok(a == [nil])
*a = loop do break *[*[1]]; end; test_ok(a == [1])
*a = loop do break *[*[1,2]]; end; test_ok(a == [1,2])
*a = loop do break *[*[1,2]]; end; test_ok(a == [[1,2]])
*a = *loop do break *nil; end; test_ok(a == [nil])
*a = *loop do break *1; end; test_ok(a == [1])
*a = *loop do break *[]; end; test_ok(a == [nil])
*a = *loop do break *[1]; end; test_ok(a == [1])
*a = *loop do break *[nil]; end; test_ok(a == [nil])
*a = *loop do break *[[]]; end; test_ok(a == [])
*a = *loop do break *[1,2]; end; test_ok(a == [1,2])
*a = *loop do break *[*[]]; end; test_ok(a == [nil])
*a = *loop do break *[*[1]]; end; test_ok(a == [1])
*a = *loop do break *[*[1,2]]; end; test_ok(a == [1,2])
a,b,*c = loop do break; end; test_ok([a,b,c] == [nil,nil,[]])
a,b,*c = loop do break nil; end; test_ok([a,b,c] == [nil,nil,[]])
@ -372,17 +404,18 @@ r([[]]){next []}
r([[1]]){next [1]}
r([[nil]]){next [nil]}
r([[[]]]){next [[]]}
r([1,2]){next [1,2]}
r([[1,2]]){next [1,2]}
r([[]]){next [*[]]}
r([[1]]){next [*[1]]}
r([1,2]){next [*[1,2]]}
r([[1,2]]){next [*[1,2]]}
def r(val); *a = *yield(); test_ok(a == val); end
r([nil]){next *nil}
r([1]){next *1}
r([nil]){next *[]}
r([1]){next *[1]}
r([nil]){next *[nil]}
r([[]]){next *[[]]}
r([]){next *[[]]}
r([1,2]){next *[1,2]}
r([nil]){next *[*[]]}
r([1]){next *[*[1]]}
@ -401,6 +434,7 @@ r([nil,nil,[]]){next [*[]]}
r([1,nil,[]]){next [*[1]]}
r([1,2,[]]){next [*[1,2]]}
def r(val); a,b,*c = *yield(); test_ok([a,b,c] == val); end
r([nil,nil,[]]){next *nil}
r([1,nil,[]]){next *1}
r([nil,nil,[]]){next *[]}
@ -716,7 +750,7 @@ test_ok(($x * 5).join(":") == '1:1:1:1:1')
test_ok(($x * 1).join(":") == '1')
test_ok(($x * 0).join(":") == '')
*$x = (1..7).to_a
*$x = *(1..7).to_a
test_ok($x.size == 7)
test_ok($x == [1, 2, 3, 4, 5, 6, 7])

Просмотреть файл

@ -3066,7 +3066,7 @@ rb_str_scan(str, pat)
while (!NIL_P(result = scan_once(str, pat, &start))) {
match = rb_backref_get();
rb_match_busy(match);
rb_yield_splat(result);
rb_yield(result);
rb_backref_set(match); /* restore $~ value */
}
rb_backref_set(match);