Skip to content

Commit

Permalink
[PRISM] Fix case without predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Jan 15, 2024
1 parent e0312f9 commit 7c6d7fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
44 changes: 20 additions & 24 deletions prism_compile.c
Expand Up @@ -3408,42 +3408,38 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
for (size_t i = 0; i < conditions.size; i++) {
label = NEW_LABEL(lineno);
conditions_labels[i] = label;
if (has_predicate) {
pm_when_node_t *when_node = (pm_when_node_t *)conditions.nodes[i];
pm_when_node_t *when_node = (pm_when_node_t *)conditions.nodes[i];

for (size_t i = 0; i < when_node->conditions.size; i++) {
pm_node_t *condition_node = when_node->conditions.nodes[i];
for (size_t i = 0; i < when_node->conditions.size; i++) {
pm_node_t *condition_node = when_node->conditions.nodes[i];

if (PM_NODE_TYPE_P(condition_node, PM_SPLAT_NODE)) {
ADD_INSN (ret, &dummy_line_node, dup);
PM_COMPILE_NOT_POPPED(condition_node);
ADD_INSN1(ret, &dummy_line_node, splatarray, Qfalse);
ADD_INSN1(ret, &dummy_line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
}
else {
PM_COMPILE_NOT_POPPED(condition_node);
if (PM_NODE_TYPE_P(condition_node, PM_SPLAT_NODE)) {
ADD_INSN (ret, &dummy_line_node, dup);
PM_COMPILE_NOT_POPPED(condition_node);
ADD_INSN1(ret, &dummy_line_node, splatarray, Qfalse);
ADD_INSN1(ret, &dummy_line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
}
else {
PM_COMPILE_NOT_POPPED(condition_node);
if (has_predicate) {
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idEqq, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
}

ADD_INSNL(ret, &dummy_line_node, branchif, label);
}
}
else {
ADD_INSNL(ret, &dummy_line_node, jump, label);
PM_PUTNIL;

ADD_INSNL(ret, &dummy_line_node, branchif, label);
}
}

if (has_predicate) {
PM_POP;
}

if (case_node->consequent) {
PM_COMPILE((pm_node_t *)case_node->consequent);
}
else {
PM_PUTNIL_UNLESS_POPPED;
}
if (case_node->consequent) {
PM_COMPILE((pm_node_t *)case_node->consequent);
}
else {
PM_PUTNIL_UNLESS_POPPED;
}

ADD_INSNL(ret, &dummy_line_node, jump, end_label);
Expand Down
10 changes: 10 additions & 0 deletions test/ruby/test_compile_prism.rb
Expand Up @@ -794,6 +794,16 @@ def self.prism_test_case_node
:ng
end
RUBY

# Test case without predicate
assert_prism_eval(<<~RUBY)
case
when 1 == 2
:ng
else
:ok
end
RUBY
end

def test_ElseNode
Expand Down

0 comments on commit 7c6d7fb

Please sign in to comment.