Skip to content

Commit

Permalink
Fix splat and multiple assignments
Browse files Browse the repository at this point in the history
Case1: From variable

Code:

    a = [1, 2, 3, 4, 5]
    b, c, *d = a

    p [a, b, c, d]

Before:

    [[1, 2, 3, 4, 5], 1, 2, []]

After:

    [[1, 2, 3, 4, 5], 1, 2, [3, 4, 5]]

Ruby:

    [[1, 2, 3, 4, 5], 1, 2, [3, 4, 5]]

Case2: From variables

Code:

    a = [1, 2, 3]
    b = [4, 5, 6, 7]
    c, d, *e, f, g = *a, *b

    p [a, b, c, d, e, f, g]

Before:

    [[1, 2, 3], [4, 5, 6, 7], 1, 2, [], 6, 7]

After:

    [[1, 2, 3], [4, 5, 6, 7], 1, 2, [3, 4, 5], 6, 7]

Ruby:

    [[1, 2, 3], [4, 5, 6, 7], 1, 2, [3, 4, 5], 6, 7]

Case 3: "for"

Code:

    a = [1, 2, 3, 4, 5, 6, 7]
    for b, c, *d, e, f in [a] do
      p [a, b, c, d, e, f]
    end

Before:

    [[1, 2, 3, 4, 5, 6, 7], 1, 2, [], nil, nil]

After:

    [[1, 2, 3, 4, 5, 6, 7], 1, 2, [3, 4, 5], 6, 7]

Ruby:

    [[1, 2, 3, 4, 5, 6, 7], 1, 2, [3, 4, 5], 6, 7]
  • Loading branch information
kou committed Dec 23, 2014
1 parent e618438 commit 5ec676a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/codegen.c
Expand Up @@ -1001,7 +1001,9 @@ gen_vmassignment(codegen_scope *s, node *tree, int rhs, int val)
}
if (val) {
genop(s, MKOP_AB(OP_MOVE, cursp(), rhs));
push();
}
else {
pop();
}
genop(s, MKOP_ABC(OP_APOST, cursp(), n, post));
n = 1;
Expand All @@ -1016,6 +1018,7 @@ gen_vmassignment(codegen_scope *s, node *tree, int rhs, int val)
n++;
}
}
push();
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/t/syntax.rb
Expand Up @@ -181,6 +181,38 @@ def b
assert_equal [1,nil,2], [a,b,c]
end

assert('Splat and multiple assignment from variable') do
a = [1, 2, 3]
b, *c = a

assert_equal 1, b
assert_equal [2, 3], c
end

assert('Splat and multiple assignment from variables') do
a = [1, 2, 3]
b = [4, 5, 6, 7]
c, d, *e, f, g = *a, *b

assert_equal 1, c
assert_equal 2, d
assert_equal [3, 4, 5], e
assert_equal 6, f
assert_equal 7, g
end

assert('Splat and multiple assignment in for') do
a = [1, 2, 3, 4, 5, 6, 7]
for b, c, *d, e, f in [a] do
end

assert_equal 1, b
assert_equal 2, c
assert_equal [3, 4, 5], d
assert_equal 6, e
assert_equal 7, f
end

assert('Return values of case statements') do
a = [] << case 1
when 3 then 2
Expand Down

0 comments on commit 5ec676a

Please sign in to comment.