Skip to content

Commit

Permalink
* eval.c (svalue_to_avalue): new conversion scheme between single
Browse files Browse the repository at this point in the history
  value and array values.

* eval.c (avalue_to_svalue): ditto.

* eval.c (rb_eval): REXPAND now uses avalue_to_svalue(), return
  and yield too.

* eval.c (rb_yield_0): use avalue_to_svalue().

* eval.c (proc_invoke): Proc#call gives avaules, whereas
  Proc#yield gives mvalues.

* eval.c (bmcall): convert given value (svalue) to avalue.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1544 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Jun 23, 2001
1 parent c0648ec commit 0869399
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 52 deletions.
60 changes: 47 additions & 13 deletions eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ call_trace_func(event, file, line, self, id, klass)
}

static VALUE
svalue_to_mvalue(v)
svalue_to_avalue(v)
VALUE v;
{
if (NIL_P(v)) return rb_ary_new2(0);
Expand All @@ -2016,7 +2016,7 @@ svalue_to_mvalue(v)
}

static VALUE
mvalue_to_svalue(v)
avalue_to_svalue(v)
VALUE v;
{
if (TYPE(v) != T_ARRAY) {
Expand All @@ -2031,6 +2031,36 @@ mvalue_to_svalue(v)
return v;
}

static VALUE
svalue_to_mvalue(v)
VALUE v;
{
if (NIL_P(v)) return rb_ary_new2(0);
if (TYPE(v) == T_ARRAY) {
return v;
}
else {
v = rb_ary_to_ary(v);
}
return v;
}

static VALUE
mvalue_to_svalue(v)
VALUE v;
{
if (TYPE(v) != T_ARRAY) {
v = rb_ary_to_ary(v);
}
if (RARRAY(v)->len == 0) {
return Qnil;
}
if (RARRAY(v)->len == 1 && TYPE(RARRAY(v)->ptr[0]) != T_ARRAY) {
return RARRAY(v)->ptr[0];
}
return v;
}

static void return_check _((void));
#define return_value(v) prot_tag->retval = (v)

Expand Down Expand Up @@ -2379,17 +2409,13 @@ rb_eval(self, n)
result = rb_ary_to_ary(rb_eval(self, node->nd_head));
break;

case NODE_SVALUE:
result = mvalue_to_svalue(rb_eval(self, node->nd_head));
break;

case NODE_MVALUE:
result = svalue_to_mvalue(rb_eval(self, node->nd_head));
case NODE_REXPAND:
result = avalue_to_svalue(rb_eval(self, node->nd_head));
break;

case NODE_YIELD:
if (node->nd_stts) {
result = mvalue_to_svalue(rb_eval(self, node->nd_stts));
result = avalue_to_svalue(rb_eval(self, node->nd_stts));
}
else {
result = Qnil;
Expand Down Expand Up @@ -2532,7 +2558,7 @@ rb_eval(self, n)

case NODE_RETURN:
if (node->nd_stts) {
return_value(mvalue_to_svalue(rb_eval(self, node->nd_stts)));
return_value(avalue_to_svalue(rb_eval(self, node->nd_stts)));
}
else {
return_value(Qnil);
Expand Down Expand Up @@ -3630,13 +3656,17 @@ rb_yield_0(val, self, klass, pcall)
massign(self, block->var, val, pcall);
}
else {
if (pcall) val = avalue_to_svalue(val);
assign(self, block->var, val, pcall);
}
}
}
POP_TAG();
if (state) goto pop_state;
}
else if (pcall) {
val = avalue_to_svalue(val);
}

PUSH_ITER(block->iter);
PUSH_TAG(PROT_NONE);
Expand Down Expand Up @@ -3738,7 +3768,9 @@ massign(self, node, val, pcall)
NODE *list;
int i = 0, len;

val = svalue_to_mvalue(val);
if (!pcall) {
val = svalue_to_mvalue(val);
}
len = RARRAY(val)->len;
list = node->nd_head;
for (i=0; list && i<len; i++) {
Expand Down Expand Up @@ -6339,7 +6371,9 @@ proc_invoke(proc, args, pcall)
PUSH_ITER(ITER_CUR);
ruby_frame->iter = ITER_CUR;

args = mvalue_to_svalue(args);
if (!pcall) {
args = avalue_to_svalue(args);
}
PUSH_TAG(PROT_NONE);
state = EXEC_TAG();
if (state == 0) {
Expand Down Expand Up @@ -6811,7 +6845,7 @@ static VALUE
bmcall(args, method)
VALUE args, method;
{
args = svalue_to_mvalue(args);
args = svalue_to_avalue(args);
return method_call(RARRAY(args)->len, RARRAY(args)->ptr, method);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def debug_variable_info(input, binding)

when /^\s*i(?:nstance)?\s+/
obj = debug_eval($', binding)
var_list(obj.instance_variables, binding)
var_list(obj.instance_variables, obj.instance_eval{binding()})

when /^\s*c(?:onst(?:ant)?)?\s+/
obj = debug_eval($', binding)
Expand Down
4 changes: 4 additions & 0 deletions misc/ruby-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ An end of a defun is found by moving forward from the beginning of one."
"\\|")
"\\)\\>\\([^_]\\|$\\)")
2)
;; regexps
'("/\\(\\(\\\\/\\|[^/\n]\\)*\\)/\\([iop]*\\)"
(1 font-lock-string-face)
(3 font-lock-constant-face))
;; variables
'("\\(^\\|[^_:.@$]\\|\\.\\.\\)\\b\\(nil\\|self\\|true\\|false\\)\\b\\([^_]\\|$\\)"
2 font-lock-variable-name-face)
Expand Down
6 changes: 2 additions & 4 deletions node.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ enum node_type {
NODE_ARGSPUSH,
NODE_RESTARGS,
NODE_RESTARY,
NODE_SVALUE,
NODE_MVALUE,
NODE_REXPAND,
NODE_BLOCK_ARG,
NODE_BLOCK_PASS,
NODE_DEFN,
Expand Down Expand Up @@ -308,8 +307,7 @@ typedef struct RNode {
#define NEW_ARGSPUSH(a,b) rb_node_newnode(NODE_ARGSPUSH,a,b,0)
#define NEW_RESTARGS(a) rb_node_newnode(NODE_RESTARGS,a,0,0)
#define NEW_RESTARY(a) rb_node_newnode(NODE_RESTARY,a,0,0)
#define NEW_SVALUE(a) rb_node_newnode(NODE_SVALUE,a,0,0)
#define NEW_MVALUE(a) rb_node_newnode(NODE_MVALUE,a,0,0)
#define NEW_REXPAND(a) rb_node_newnode(NODE_REXPAND,a,0,0)
#define NEW_BLOCK_ARG(v) rb_node_newnode(NODE_BLOCK_ARG,v,0,local_cnt(v))
#define NEW_BLOCK_PASS(b) rb_node_newnode(NODE_BLOCK_PASS,0,b,0)
#define NEW_ALIAS(n,o) rb_node_newnode(NODE_ALIAS,o,n,0)
Expand Down
4 changes: 2 additions & 2 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ stmt : kALIAS fitem {lex_state = EXPR_FNAME;} fitem
}
| lhs '=' mrhs_basic
{
$$ = node_assign($1, NEW_SVALUE($3));
$$ = node_assign($1, NEW_REXPAND($3));
}
| mlhs '=' mrhs
{
Expand Down Expand Up @@ -1134,7 +1134,7 @@ mrhs : arg
}
| mrhs_basic
{
$$ = NEW_SVALUE($1);
$$ = NEW_REXPAND($1);
}

mrhs_basic : args ',' arg
Expand Down
64 changes: 32 additions & 32 deletions sample/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,40 @@ def test_ok(cond)

*a = nil; test_ok(a == [])
*a = 1; test_ok(a == [1])
*a = []; test_ok(a == [[]])
*a = [1]; test_ok(a == [[1]])
*a = [nil]; test_ok(a == [[nil]])
*a = [[]]; test_ok(a == [[[]]])
*a = [*[]]; test_ok(a == [[]])
*a = [*[1]]; test_ok(a == [[1]])
*a = []; test_ok(a == [])
*a = [1]; test_ok(a == [1])
*a = [nil]; test_ok(a == [nil])
*a = [[]]; test_ok(a == [[]])
*a = [*[]]; test_ok(a == [])
*a = [*[1]]; test_ok(a == [1])
*a = [*[1,2]]; test_ok(a == [1,2])

*a = *nil; test_ok(a == [])
*a = *1; test_ok(a == [1])
*a = *[]; test_ok(a == [])
*a = *[1]; test_ok(a == [1])
*a = *[nil]; test_ok(a == [])
*a = *[[]]; test_ok(a == [[]])
*a = *[[]]; test_ok(a == [])
*a = *[*[]]; test_ok(a == [])
*a = *[*[1]]; test_ok(a == [1])
*a = *[*[1,2]]; test_ok(a == [1,2])

a,b,*c = nil; test_ok([a,b,c] == [nil, nil, []])
a,b,*c = 1; test_ok([a,b,c] == [1, nil, []])
a,b,*c = []; test_ok([a,b,c] == [[], 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, []])
a,b,*c = [*[]]; test_ok([a,b,c] == [[], nil, []])
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, []])
a,b,*c = [*[]]; test_ok([a,b,c] == [nil, nil, []])
a,b,*c = [*[1]]; test_ok([a,b,c] == [1, nil, []])
a,b,*c = [*[1,2]]; test_ok([a,b,c] == [1, 2, []])

a,b,*c = *nil; test_ok([a,b,c] == [nil, nil, []])
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, []])
a,b,*c = *[[]]; test_ok([a,b,c] == [nil, 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 = *[*[1,2]]; test_ok([a,b,c] == [1, 2, []])
Expand All @@ -124,40 +124,40 @@ def f; yield *[*[1,2]]; end; f {|a| test_ok(a == [1,2])}

def f; yield nil; end; f {|*a| test_ok(a == [])}
def f; yield 1; end; f {|*a| test_ok(a == [1])}
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 [*[]]; end; f {|*a| test_ok(a == [[]])}
def f; yield [*[1]]; end; f {|*a| test_ok(a == [[1]])}
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 [*[]]; 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 *nil; end; f {|*a| test_ok(a == [])}
def f; yield *1; end; f {|*a| test_ok(a == [1])}
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 == [])}
def f; yield *[[]]; end; f {|*a| test_ok(a == [[]])}
def f; yield *[[]]; end; f {|*a| test_ok(a == [])}
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 nil; 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 []; end; f {|a,b,*c| test_ok([a,b,c] == [[], 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, []])}
def f; yield [*[]]; end; f {|a,b,*c| test_ok([a,b,c] == [[], nil, []])}
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, []])}
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, []])}

def f; yield *nil; 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 *[]; 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, []])}
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, 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, []])}
Expand Down Expand Up @@ -666,17 +666,17 @@ def f(a)
IterTest.new([[0]]).each0 { |x| $x = x }
test_ok($x == [0])
IterTest.new([[1]]).each1 { |x| $x = x }
test_ok($x == [1])
test_ok($x == 1)
IterTest.new([[2]]).each2 { |x| $x = x }
test_ok($x == [[2]])
test_ok($x == [2])
IterTest.new([[3]]).each3 { |x| $x = x }
test_ok($x == 3)
IterTest.new([[4]]).each4 { |x| $x = x }
test_ok($x == [4])
IterTest.new([[5]]).each5 { |x| $x = x }
test_ok($x == [5])
test_ok($x == 5)
IterTest.new([[6]]).each6 { |x| $x = x }
test_ok($x == [[6]])
test_ok($x == [6])
IterTest.new([[7]]).each7 { |x| $x = x }
test_ok($x == 7)
IterTest.new([[8]]).each8 { |x| $x = x }
Expand Down

0 comments on commit 0869399

Please sign in to comment.