Rename NODE_ARRAY to NODE_LIST to reflect its actual use cases

and NODE_ZARRAY to NODE_ZLIST.

NODE_ARRAY is used not only by an Array literal, but also the contents
of Hash literals, method call arguments, dynamic string literals, etc.
In addition, the structure of NODE_ARRAY is a linked list, not an array.

This is very confusing, so I believe `NODE_LIST` is a better name.
This commit is contained in:
Yusuke Endoh 2019-09-07 10:42:00 +09:00
Родитель f223ab47e6
Коммит 99c9431ea1
7 изменённых файлов: 63 добавлений и 64 удалений

6
ast.c
Просмотреть файл

@ -337,7 +337,7 @@ dump_array(rb_ast_t *ast, NODE *node)
VALUE ary = rb_ary_new();
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
while (node->nd_next && nd_type(node->nd_next) == NODE_ARRAY) {
while (node->nd_next && nd_type(node->nd_next) == NODE_LIST) {
node = node->nd_next;
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
}
@ -491,12 +491,12 @@ node_children(rb_ast_t *ast, NODE *node)
return rb_ary_new_from_node_args(ast, 1, node->nd_args);
case NODE_ZSUPER:
return rb_ary_new_from_node_args(ast, 0);
case NODE_ARRAY:
case NODE_LIST:
goto ary;
case NODE_VALUES:
ary:
return dump_array(ast, node);
case NODE_ZARRAY:
case NODE_ZLIST:
return rb_ary_new_from_node_args(ast, 0);
case NODE_HASH:
return rb_ary_new_from_node_args(ast, 1, node->nd_head);

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

@ -3758,7 +3758,7 @@ compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *co
case NODE_LIT: /* NODE_LIT is always true */
case NODE_TRUE:
case NODE_STR:
case NODE_ZARRAY:
case NODE_ZLIST:
case NODE_LAMBDA:
/* printf("useless condition eliminate (%s)\n", ruby_node_name(nd_type(cond))); */
ADD_INSNL(ret, nd_line(cond), jump, then_label);
@ -3768,7 +3768,7 @@ compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *co
/* printf("useless condition eliminate (%s)\n", ruby_node_name(nd_type(cond))); */
ADD_INSNL(ret, nd_line(cond), jump, else_label);
break;
case NODE_ARRAY:
case NODE_LIST:
case NODE_ARGSCAT:
case NODE_DREGX:
case NODE_DSTR:
@ -3802,13 +3802,13 @@ compile_keyword_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
{
if (kw_arg_ptr == NULL) return FALSE;
if (nd_type(root_node) == NODE_HASH && !root_node->nd_brace && root_node->nd_head && nd_type(root_node->nd_head) == NODE_ARRAY) {
if (nd_type(root_node) == NODE_HASH && !root_node->nd_brace && root_node->nd_head && nd_type(root_node->nd_head) == NODE_LIST) {
const NODE *node = root_node->nd_head;
while (node) {
const NODE *key_node = node->nd_head;
assert(nd_type(node) == NODE_ARRAY);
assert(nd_type(node) == NODE_LIST);
if (!key_node) {
if (flag) *flag |= VM_CALL_KW_SPLAT;
return FALSE;
@ -3857,7 +3857,7 @@ compile_args(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node_roo
for (; node; len++, node = node->nd_next) {
if (CPDEBUG > 0) {
EXPECT_NODE("compile_args", node, NODE_ARRAY, -1);
EXPECT_NODE("compile_args", node, NODE_LIST, -1);
}
if (node->nd_next == NULL /* last node */ &&
@ -3929,7 +3929,7 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node_ro
int line = (int)nd_line(node);
int len = 0;
if (nd_type(node) == NODE_ZARRAY) {
if (nd_type(node) == NODE_ZLIST) {
if (!popped) {
switch (type) {
case COMPILE_ARRAY_TYPE_ARRAY: ADD_INSN1(ret, line, newarray, INT2FIX(0)); break;
@ -3952,7 +3952,7 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node_ro
for (i=0; i<max && node; i++, len++, node = node->nd_next) {
if (CPDEBUG > 0) {
EXPECT_NODE("compile_array", node, NODE_ARRAY, -1);
EXPECT_NODE("compile_array", node, NODE_LIST, -1);
}
if (type == COMPILE_ARRAY_TYPE_HASH && !node->nd_head) {
@ -4184,7 +4184,7 @@ when_splat_vals(rb_iseq_t *iseq, LINK_ANCHOR *const cond_seq, const NODE *vals,
const int line = nd_line(vals);
switch (nd_type(vals)) {
case NODE_ARRAY:
case NODE_LIST:
if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
return COMPILE_NG;
break;
@ -4294,7 +4294,7 @@ compile_massign_opt(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
mem[memindex++] = (v); \
}
if (rhsn == 0 || nd_type(rhsn) != NODE_ARRAY) {
if (rhsn == 0 || nd_type(rhsn) != NODE_LIST) {
return 0;
}
@ -4514,7 +4514,7 @@ defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
expr_type = DEFINED_FALSE;
break;
case NODE_ARRAY:{
case NODE_LIST:{
const NODE *vals = node;
do {
@ -4529,7 +4529,7 @@ defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
/* fall through */
case NODE_STR:
case NODE_LIT:
case NODE_ZARRAY:
case NODE_ZLIST:
case NODE_AND:
case NODE_OR:
default:
@ -4815,7 +4815,7 @@ check_keyword(const NODE *node)
{
/* This check is essentially a code clone of compile_keyword_arg. */
if (nd_type(node) == NODE_ARRAY) {
if (nd_type(node) == NODE_LIST) {
while (node->nd_next) {
node = node->nd_next;
}
@ -4840,7 +4840,7 @@ setup_args_core(rb_iseq_t *iseq, LINK_ANCHOR *const args, const NODE *argn,
}
case NODE_ARGSCAT:
case NODE_ARGSPUSH: {
int next_is_array = (nd_type(argn->nd_head) == NODE_ARRAY);
int next_is_array = (nd_type(argn->nd_head) == NODE_LIST);
VALUE argc = setup_args_core(iseq, args, argn->nd_head, 1, NULL, NULL);
NO_CHECK(COMPILE(args, "args (cat: splat)", argn->nd_body));
if (flag) {
@ -4867,7 +4867,7 @@ setup_args_core(rb_iseq_t *iseq, LINK_ANCHOR *const args, const NODE *argn,
return argc;
}
}
case NODE_ARRAY: {
case NODE_LIST: {
int len = compile_args(iseq, args, argn, keywords, flag);
return INT2FIX(len);
}
@ -5136,7 +5136,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_nod
vals = node->nd_head;
if (vals) {
switch (nd_type(vals)) {
case NODE_ARRAY:
case NODE_LIST:
only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
if (only_special_literals < 0) return COMPILE_NG;
break;
@ -5151,7 +5151,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_nod
}
}
else {
EXPECT_NODE_NONULL("NODE_CASE", node, NODE_ARRAY, COMPILE_NG);
EXPECT_NODE_NONULL("NODE_CASE", node, NODE_LIST, COMPILE_NG);
}
node = node->nd_next;
@ -5234,11 +5234,11 @@ compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_no
vals = node->nd_head;
if (!vals) {
COMPILE_ERROR(ERROR_ARGS "NODE_WHEN: must be NODE_ARRAY, but 0");
COMPILE_ERROR(ERROR_ARGS "NODE_WHEN: must be NODE_LIST, but 0");
return COMPILE_NG;
}
switch (nd_type(vals)) {
case NODE_ARRAY:
case NODE_LIST:
while (vals) {
LABEL *lnext;
val = vals->nd_head;
@ -5639,8 +5639,8 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c
case NODE_DSTR:
case NODE_DSYM:
case NODE_DREGX:
case NODE_ARRAY:
case NODE_ZARRAY:
case NODE_LIST:
case NODE_ZLIST:
case NODE_LAMBDA:
case NODE_DOT2:
case NODE_DOT3:
@ -5729,7 +5729,7 @@ iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *c
fin = NEW_LABEL(line);
n = node->nd_head;
if (! (nd_type(n) == NODE_ARRAY && n->nd_alen == 2)) {
if (! (nd_type(n) == NODE_LIST && n->nd_alen == 2)) {
COMPILE_ERROR(ERROR_ARGS "unexpected node");
return COMPILE_NG;
}
@ -6322,7 +6322,7 @@ compile_resbody(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
narg = resq->nd_args;
if (narg) {
switch (nd_type(narg)) {
case NODE_ARRAY:
case NODE_LIST:
while (narg) {
ADD_GETLOCAL(ret, line, LVAR_ERRINFO, 0);
CHECK(COMPILE(ret, "rescue arg", narg->nd_head));
@ -6549,7 +6549,7 @@ compile_call_precheck_freeze(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE
* obj["literal"] -> opt_aref_with(obj, "literal")
*/
if (node->nd_mid == idAREF && !private_recv_p(node) && node->nd_args &&
nd_type(node->nd_args) == NODE_ARRAY && node->nd_args->nd_alen == 1 &&
nd_type(node->nd_args) == NODE_LIST && node->nd_args->nd_alen == 1 &&
nd_type(node->nd_args->nd_head) == NODE_STR &&
ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
@ -6977,7 +6977,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
}
asgnflag = COMPILE_RECV(ret, "NODE_OP_ASGN1 recv", node);
switch (nd_type(node->nd_args->nd_head)) {
case NODE_ZARRAY:
case NODE_ZLIST:
argc = INT2FIX(0);
break;
case NODE_BLOCK_PASS:
@ -7434,11 +7434,11 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
}
break;
}
case NODE_ARRAY:{
case NODE_LIST:{
CHECK(compile_array(iseq, ret, node, COMPILE_ARRAY_TYPE_ARRAY, popped) >= 0);
break;
}
case NODE_ZARRAY:{
case NODE_ZLIST:{
if (!popped) {
ADD_INSN1(ret, line, newarray, INT2FIX(0));
}
@ -7458,16 +7458,16 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
}
case NODE_HASH:{
DECL_ANCHOR(list);
enum node_type type = node->nd_head ? nd_type(node->nd_head) : NODE_ZARRAY;
enum node_type type = node->nd_head ? nd_type(node->nd_head) : NODE_ZLIST;
INIT_ANCHOR(list);
switch (type) {
case NODE_ARRAY:
case NODE_LIST:
CHECK(compile_array(iseq, list, node->nd_head, COMPILE_ARRAY_TYPE_HASH, popped) >= 0);
ADD_SEQ(ret, list);
break;
case NODE_ZARRAY:
case NODE_ZLIST:
if (popped) break;
ADD_INSN1(ret, line, newhash, INT2FIX(0));
break;
@ -8116,7 +8116,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
* obj["literal"] = value -> opt_aset_with(obj, "literal", value)
*/
if (mid == idASET && !private_recv_p(node) && node->nd_args &&
nd_type(node->nd_args) == NODE_ARRAY && node->nd_args->nd_alen == 2 &&
nd_type(node->nd_args) == NODE_LIST && node->nd_args->nd_alen == 2 &&
nd_type(node->nd_args->nd_head) == NODE_STR &&
ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&

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

@ -416,8 +416,8 @@ count_nodes(int argc, VALUE *argv, VALUE os)
COUNT_NODE(NODE_QCALL);
COUNT_NODE(NODE_SUPER);
COUNT_NODE(NODE_ZSUPER);
COUNT_NODE(NODE_ARRAY);
COUNT_NODE(NODE_ZARRAY);
COUNT_NODE(NODE_LIST);
COUNT_NODE(NODE_ZLIST);
COUNT_NODE(NODE_VALUES);
COUNT_NODE(NODE_HASH);
COUNT_NODE(NODE_RETURN);

10
node.c
Просмотреть файл

@ -114,7 +114,7 @@ dump_array(VALUE buf, VALUE indent, int comment, const NODE *node)
const char *next_indent = default_indent;
F_LONG(nd_alen, "length");
F_NODE(nd_head, "element");
while (node->nd_next && nd_type(node->nd_next) == NODE_ARRAY) {
while (node->nd_next && nd_type(node->nd_next) == NODE_LIST) {
node = node->nd_next;
F_NODE(nd_head, "element");
}
@ -557,8 +557,8 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("example: super");
return;
case NODE_ARRAY:
ANN("array constructor");
case NODE_LIST:
ANN("list constructor");
ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
ANN("example: [1, 2, 3]");
goto ary;
@ -570,8 +570,8 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
dump_array(buf, indent, comment, node);
return;
case NODE_ZARRAY:
ANN("empty array constructor");
case NODE_ZLIST:
ANN("empty list constructor");
ANN("format: []");
ANN("example: []");
return;

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

@ -64,8 +64,8 @@ enum node_type {
NODE_QCALL,
NODE_SUPER,
NODE_ZSUPER,
NODE_ARRAY,
NODE_ZARRAY,
NODE_LIST,
NODE_ZLIST,
NODE_VALUES,
NODE_HASH,
NODE_RETURN,
@ -309,9 +309,8 @@ typedef struct RNode {
#define NEW_ENSURE(b,en,loc) NEW_NODE(NODE_ENSURE,b,0,en,loc)
#define NEW_RETURN(s,loc) NEW_NODE(NODE_RETURN,s,0,0,loc)
#define NEW_YIELD(a,loc) NEW_NODE(NODE_YIELD,a,0,0,loc)
#define NEW_LIST(a,loc) NEW_ARRAY(a,loc)
#define NEW_ARRAY(a,loc) NEW_NODE(NODE_ARRAY,a,1,0,loc)
#define NEW_ZARRAY(loc) NEW_NODE(NODE_ZARRAY,0,0,0,loc)
#define NEW_LIST(a,loc) NEW_NODE(NODE_LIST,a,1,0,loc)
#define NEW_ZLIST(loc) NEW_NODE(NODE_ZLIST,0,0,0,loc)
#define NEW_HASH(a,loc) NEW_NODE(NODE_HASH,a,0,0,loc)
#define NEW_MASGN(l,r,loc) NEW_NODE(NODE_MASGN,l,0,r,loc)
#define NEW_GASGN(v,val,loc) NEW_NODE(NODE_GASGN,v,val,rb_global_entry(v),loc)

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

@ -460,7 +460,7 @@ static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc
static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
#define make_array(ary, loc) ((ary) ? (nd_set_loc(ary, loc), ary) : NEW_ZARRAY(loc))
#define make_array(ary, loc) ((ary) ? (nd_set_loc(ary, loc), ary) : NEW_ZLIST(loc))
static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
@ -5130,8 +5130,8 @@ singleton : var_ref
case NODE_DXSTR:
case NODE_DREGX:
case NODE_LIT:
case NODE_ARRAY:
case NODE_ZARRAY:
case NODE_LIST:
case NODE_ZLIST:
yyerror1(&@3, "can't define singleton method for literals");
break;
default:
@ -5166,7 +5166,7 @@ assocs : assoc
}
else if (tail) {
if (assocs->nd_head &&
!tail->nd_head && nd_type(tail->nd_next) == NODE_ARRAY &&
!tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
nd_type(tail->nd_next->nd_head) == NODE_HASH) {
/* DSTAR */
tail = tail->nd_next->nd_head->nd_head;
@ -7133,7 +7133,7 @@ heredoc_dedent(struct parser_params *p, NODE *root)
if (!root) return root;
prev_node = node = str_node = root;
if (nd_type(root) == NODE_ARRAY) str_node = root->nd_head;
if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
while (str_node) {
VALUE lit = str_node->nd_lit;
@ -7161,7 +7161,7 @@ heredoc_dedent(struct parser_params *p, NODE *root)
str_node = 0;
while ((node = (prev_node = node)->nd_next) != 0) {
next_str:
if (nd_type(node) != NODE_ARRAY) break;
if (nd_type(node) != NODE_LIST) break;
if ((str_node = node->nd_head) != 0) {
enum node_type type = nd_type(str_node);
if (type == NODE_STR || type == NODE_DSTR) break;
@ -9609,7 +9609,7 @@ literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *l
goto append;
}
else {
nd_set_type(tail, NODE_ARRAY);
nd_set_type(tail, NODE_LIST);
tail->nd_head = NEW_STR(tail->nd_lit, loc);
list_concat(head, tail);
}
@ -10428,7 +10428,7 @@ arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc
{
if (!node1) return NEW_LIST(node2, &node2->nd_loc);
switch (nd_type(node1)) {
case NODE_ARRAY:
case NODE_LIST:
return list_append(p, node1, node2);
case NODE_BLOCK_PASS:
node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
@ -10440,7 +10440,7 @@ arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc
nd_set_type(node1, NODE_ARGSCAT);
return node1;
case NODE_ARGSCAT:
if (nd_type(node1->nd_body) != NODE_ARRAY) break;
if (nd_type(node1->nd_body) != NODE_LIST) break;
node1->nd_body = list_append(p, node1->nd_body, node2);
node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
return node1;
@ -10460,13 +10460,13 @@ arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc
node1->nd_head = NEW_LIST(node2, loc);
return node1;
case NODE_ARGSPUSH:
if (nd_type(node2) != NODE_ARRAY) break;
if (nd_type(node2) != NODE_LIST) break;
node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
nd_set_type(node1, NODE_ARGSCAT);
return node1;
case NODE_ARGSCAT:
if (nd_type(node2) != NODE_ARRAY ||
nd_type(node1->nd_body) != NODE_ARRAY) break;
if (nd_type(node2) != NODE_LIST ||
nd_type(node1->nd_body) != NODE_LIST) break;
node1->nd_body = list_concat(node1->nd_body, node2);
return node1;
}
@ -10487,7 +10487,7 @@ static NODE *
rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
{
NODE *n1;
if ((nd_type(rest_arg) == NODE_ARRAY) && (n1 = splat_array(args)) != 0) {
if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
return list_concat(n1, rest_arg);
}
return arg_concat(p, args, rest_arg, loc);
@ -10497,7 +10497,7 @@ static NODE *
splat_array(NODE* node)
{
if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
if (nd_type(node) == NODE_ARRAY) return node;
if (nd_type(node) == NODE_LIST) return node;
return 0;
}
@ -10820,7 +10820,7 @@ is_static_content(NODE *node)
switch (nd_type(node)) {
case NODE_HASH:
if (!(node = node->nd_head)) break;
case NODE_ARRAY:
case NODE_LIST:
do {
if (!is_static_content(node->nd_head)) return 0;
} while ((node = node->nd_next) != 0);
@ -10829,7 +10829,7 @@ is_static_content(NODE *node)
case NODE_NIL:
case NODE_TRUE:
case NODE_FALSE:
case NODE_ZARRAY:
case NODE_ZLIST:
break;
default:
return 0;
@ -11012,7 +11012,7 @@ ret_args(struct parser_params *p, NODE *node)
{
if (node) {
no_blockarg(p, node);
if (nd_type(node) == NODE_ARRAY) {
if (nd_type(node) == NODE_LIST) {
if (node->nd_next == 0) {
node = node->nd_head;
}
@ -11990,7 +11990,7 @@ parser_append_options(struct parser_params *p, NODE *node)
if (p->do_print) {
NODE *print = NEW_FCALL(rb_intern("print"),
NEW_ARRAY(NEW_GVAR(idLASTLINE, LOC), LOC),
NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
LOC);
node = block_append(p, node, print);
}

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

@ -74,7 +74,7 @@ class TestAst < Test::Unit::TestCase
children = node.children.grep(RubyVM::AbstractSyntaxTree::Node)
return true if children.empty?
# These NODE_D* has NODE_ARRAY as nd_next->nd_next whose last locations
# These NODE_D* has NODE_LIST as nd_next->nd_next whose last locations
# we can not update when item is appended.
return true if [:DSTR, :DXSTR, :DREGX, :DSYM].include? node.type