Skip to content

Commit

Permalink
Introduce pattern matching [EXPERIMENTAL]
Browse files Browse the repository at this point in the history
[ruby-core:87945] [Feature #14912]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
k-tsj committed Apr 17, 2019
1 parent b077654 commit 9738f96
Show file tree
Hide file tree
Showing 21 changed files with 2,840 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -14,6 +14,8 @@ sufficient information, see the ChangeLog file or Redmine

=== Language changes

* Introduce pattern matching [Feature #14912]

* Method reference operator, <code>.:</code> is introduced as an
experimental feature. [Feature #12125] [Feature #13581]

Expand Down
8 changes: 8 additions & 0 deletions array.c
Expand Up @@ -6544,6 +6544,12 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary)
return v;
}

static VALUE
rb_ary_deconstruct(VALUE ary)
{
return ary;
}

/*
* Arrays are ordered, integer-indexed collections of any object.
*
Expand Down Expand Up @@ -6910,5 +6916,7 @@ Init_Array(void)
rb_define_method(rb_cArray, "dig", rb_ary_dig, -1);
rb_define_method(rb_cArray, "sum", rb_ary_sum, -1);

rb_define_method(rb_cArray, "deconstruct", rb_ary_deconstruct, 0);

id_random = rb_intern("random");
}
20 changes: 20 additions & 0 deletions ast.c
Expand Up @@ -364,8 +364,12 @@ node_children(rb_ast_t *ast, NODE *node)
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
case NODE_CASE2:
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
case NODE_CASE3:
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
case NODE_WHEN:
return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
case NODE_IN:
return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
case NODE_WHILE:
goto loop;
case NODE_UNTIL:
Expand Down Expand Up @@ -622,6 +626,22 @@ node_children(rb_ast_t *ast, NODE *node)
}
return rb_ary_new_from_args(3, locals, NEW_CHILD(ast, node->nd_args), NEW_CHILD(ast, node->nd_body));
}
case NODE_ARYPTN:
{
struct rb_ary_pattern_info *apinfo = node->nd_apinfo;
return rb_ary_new_from_args(4,
NEW_CHILD(ast, node->nd_pconst),
NEW_CHILD(ast, apinfo->pre_args),
NEW_CHILD(ast, apinfo->rest_arg),
NEW_CHILD(ast, apinfo->post_args));
}
case NODE_HSHPTN:
{
return rb_ary_new_from_args(3,
NEW_CHILD(ast, node->nd_pconst),
NEW_CHILD(ast, node->nd_pkwargs),
NEW_CHILD(ast, node->nd_pkwrestarg));
}
case NODE_ARGS_AUX:
case NODE_LAST:
break;
Expand Down

0 comments on commit 9738f96

Please sign in to comment.