1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

parse.y: Remove special handling of tOROP and tANDOP

The complexity is no longer considered necessary.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61701 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2018-01-09 01:49:40 +00:00
parent 580424d533
commit da4067bab8
3 changed files with 20 additions and 41 deletions

View file

@ -5748,9 +5748,8 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
ADD_SEND_WITH_FLAG(ret, line, idAREF, argc, INT2FIX(flag)); ADD_SEND_WITH_FLAG(ret, line, idAREF, argc, INT2FIX(flag));
flag |= asgnflag; flag |= asgnflag;
if (id == 0 || id == 1) { if (id == idOROP || id == idANDOP) {
/* 0: or, 1: and /* a[x] ||= y or a[x] &&= y
a[x] ||= y
unless/if a[x] unless/if a[x]
a[x]= y a[x]= y
@ -5762,12 +5761,10 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
LABEL *lfin = NEW_LABEL(line); LABEL *lfin = NEW_LABEL(line);
ADD_INSN(ret, line, dup); ADD_INSN(ret, line, dup);
if (id == 0) { if (id == idOROP) {
/* or */
ADD_INSNL(ret, line, branchif, label); ADD_INSNL(ret, line, branchif, label);
} }
else { else { /* idANDOP */
/* and */
ADD_INSNL(ret, line, branchunless, label); ADD_INSNL(ret, line, branchunless, label);
} }
ADD_INSN(ret, line, pop); ADD_INSN(ret, line, pop);
@ -5894,12 +5891,12 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
ADD_INSN(ret, line, dup); ADD_INSN(ret, line, dup);
ADD_SEND(ret, line, vid, INT2FIX(0)); ADD_SEND(ret, line, vid, INT2FIX(0));
if (atype == 0 || atype == 1) { /* 0: OR or 1: AND */ if (atype == idOROP || atype == idANDOP) {
ADD_INSN(ret, line, dup); ADD_INSN(ret, line, dup);
if (atype == 0) { if (atype == idOROP) {
ADD_INSNL(ret, line, branchif, lcfin); ADD_INSNL(ret, line, branchif, lcfin);
} }
else { else { /* idANDOP */
ADD_INSNL(ret, line, branchunless, lcfin); ADD_INSNL(ret, line, branchunless, lcfin);
} }
ADD_INSN(ret, line, pop); ADD_INSN(ret, line, pop);
@ -5959,7 +5956,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
} }
mid = node->nd_head->nd_mid; mid = node->nd_head->nd_mid;
/* cref */ /* cref */
if (node->nd_aid == 0) { if (node->nd_aid == idOROP) {
lassign = NEW_LABEL(line); lassign = NEW_LABEL(line);
ADD_INSN(ret, line, dup); /* cref cref */ ADD_INSN(ret, line, dup); /* cref cref */
ADD_INSN3(ret, line, defined, INT2FIX(DEFINED_CONST), ADD_INSN3(ret, line, defined, INT2FIX(DEFINED_CONST),
@ -5969,12 +5966,12 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
ADD_INSN(ret, line, dup); /* cref cref */ ADD_INSN(ret, line, dup); /* cref cref */
ADD_INSN1(ret, line, getconstant, ID2SYM(mid)); /* cref obj */ ADD_INSN1(ret, line, getconstant, ID2SYM(mid)); /* cref obj */
if (node->nd_aid == 0 || node->nd_aid == 1) { if (node->nd_aid == idOROP || node->nd_aid == idANDOP) {
lfin = NEW_LABEL(line); lfin = NEW_LABEL(line);
if (!popped) ADD_INSN(ret, line, dup); /* cref [obj] obj */ if (!popped) ADD_INSN(ret, line, dup); /* cref [obj] obj */
if (node->nd_aid == 0) if (node->nd_aid == idOROP)
ADD_INSNL(ret, line, branchif, lfin); ADD_INSNL(ret, line, branchif, lfin);
else else /* idANDOP */
ADD_INSNL(ret, line, branchunless, lfin); ADD_INSNL(ret, line, branchunless, lfin);
/* cref [obj] */ /* cref [obj] */
if (!popped) ADD_INSN(ret, line, pop); /* cref */ if (!popped) ADD_INSN(ret, line, pop); /* cref */

18
node.c
View file

@ -22,7 +22,6 @@
#define A_INT(val) rb_str_catf(buf, "%d", (val)) #define A_INT(val) rb_str_catf(buf, "%d", (val))
#define A_LONG(val) rb_str_catf(buf, "%ld", (val)) #define A_LONG(val) rb_str_catf(buf, "%ld", (val))
#define A_LIT(lit) AR(rb_inspect(lit)) #define A_LIT(lit) AR(rb_inspect(lit))
#define A_OPERATOR(id) add_operator(buf, (id))
#define A_NODE_HEADER(node, term) \ #define A_NODE_HEADER(node, term) \
rb_str_catf(buf, "@ %s (line: %d, code_range: (%d,%d)-(%d,%d))%s"term, \ rb_str_catf(buf, "@ %s (line: %d, code_range: (%d,%d)-(%d,%d))%s"term, \
ruby_node_name(nd_type(node)), nd_line(node), \ ruby_node_name(nd_type(node)), nd_line(node), \
@ -63,7 +62,6 @@
#define F_INT(name, ann) SIMPLE_FIELD1(#name, ann) A_INT(node->name) #define F_INT(name, ann) SIMPLE_FIELD1(#name, ann) A_INT(node->name)
#define F_LONG(name, ann) SIMPLE_FIELD1(#name, ann) A_LONG(node->name) #define F_LONG(name, ann) SIMPLE_FIELD1(#name, ann) A_LONG(node->name)
#define F_LIT(name, ann) SIMPLE_FIELD1(#name, ann) A_LIT(node->name) #define F_LIT(name, ann) SIMPLE_FIELD1(#name, ann) A_LIT(node->name)
#define F_OPERATOR(name, ann) SIMPLE_FIELD1(#name, ann) A_OPERATOR(node->name)
#define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc) #define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc)
#define F_NODE(name, ann) \ #define F_NODE(name, ann) \
@ -99,16 +97,6 @@ add_id(VALUE buf, ID id)
} }
} }
static void
add_operator(VALUE buf, ID id)
{
switch (id) {
case 0: A("0 (||)"); break;
case 1: A("1 (&&)"); break;
default: A_ID(id);
}
}
struct add_option_arg { struct add_option_arg {
VALUE buf, indent; VALUE buf, indent;
st_index_t count; st_index_t count;
@ -435,7 +423,7 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]"); ANN("format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
ANN("example: ary[1] += foo"); ANN("example: ary[1] += foo");
F_NODE(nd_recv, "receiver"); F_NODE(nd_recv, "receiver");
F_OPERATOR(nd_mid, "operator"); F_ID(nd_mid, "operator");
F_NODE(nd_args->nd_head, "index"); F_NODE(nd_args->nd_head, "index");
LAST_NODE; LAST_NODE;
F_NODE(nd_args->nd_body, "rvalue"); F_NODE(nd_args->nd_body, "rvalue");
@ -451,7 +439,7 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
if (node->nd_next->nd_aid) A("? "); if (node->nd_next->nd_aid) A("? ");
A_ID(node->nd_next->nd_vid); A_ID(node->nd_next->nd_vid);
} }
F_OPERATOR(nd_next->nd_mid, "operator"); F_ID(nd_next->nd_mid, "operator");
LAST_NODE; LAST_NODE;
F_NODE(nd_value, "rvalue"); F_NODE(nd_value, "rvalue");
return; return;
@ -476,7 +464,7 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("format: [nd_head](constant) [nd_aid]= [nd_value]"); ANN("format: [nd_head](constant) [nd_aid]= [nd_value]");
ANN("example: A::B ||= 1"); ANN("example: A::B ||= 1");
F_NODE(nd_head, "constant"); F_NODE(nd_head, "constant");
F_OPERATOR(nd_aid, "operator"); F_ID(nd_aid, "operator");
LAST_NODE; LAST_NODE;
F_NODE(nd_value, "rvalue"); F_NODE(nd_value, "rvalue");
return; return;

18
parse.y
View file

@ -495,12 +495,6 @@ static NODE *new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs, ID
#define new_attr_op_assign(lhs, type, attr, op, rhs, cr) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs), (cr)) #define new_attr_op_assign(lhs, type, attr, op, rhs, cr) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs), (cr))
static NODE *new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *cr); static NODE *new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *cr);
#define new_const_op_assign(lhs, op, rhs, cr) new_const_op_assign_gen(parser, (lhs), (op), (rhs), (cr)) #define new_const_op_assign(lhs, op, rhs, cr) new_const_op_assign_gen(parser, (lhs), (op), (rhs), (cr))
static ID change_shortcut_operator_id(ID id)
{
if (id == tOROP) return 0;
if (id == tANDOP) return 1;
return id;
}
static NODE *const_path_field_gen(struct parser_params *parser, NODE *head, ID mid, const YYLTYPE *cr); static NODE *const_path_field_gen(struct parser_params *parser, NODE *head, ID mid, const YYLTYPE *cr);
#define const_path_field(w, n, cr) const_path_field_gen(parser, w, n, cr) #define const_path_field(w, n, cr) const_path_field_gen(parser, w, n, cr)
@ -1464,7 +1458,7 @@ command_asgn : lhs '=' command_rhs
value_expr($6); value_expr($6);
$3 = make_array($3, &@3); $3 = make_array($3, &@3);
args = arg_concat($3, $6, &@$); args = arg_concat($3, $6, &@$);
$$ = NEW_OP_ASGN1($1, change_shortcut_operator_id($5), args, &@$); $$ = NEW_OP_ASGN1($1, $5, args, &@$);
fixpos($$, $1); fixpos($$, $1);
/*% /*%
$$ = dispatch2(aref_field, $1, escape_Qundef($3)); $$ = dispatch2(aref_field, $1, escape_Qundef($3));
@ -2094,7 +2088,7 @@ arg : lhs '=' arg_rhs
else { else {
args = arg_concat($3, $6, &@$); args = arg_concat($3, $6, &@$);
} }
$$ = NEW_OP_ASGN1($1, change_shortcut_operator_id($5), args, &@$); $$ = NEW_OP_ASGN1($1, $5, args, &@$);
fixpos($$, $1); fixpos($$, $1);
/*% /*%
$1 = dispatch2(aref_field, $1, escape_Qundef($3)); $1 = dispatch2(aref_field, $1, escape_Qundef($3));
@ -8369,7 +8363,7 @@ parser_yylex(struct parser_params *parser)
if ((c = nextc()) == '&') { if ((c = nextc()) == '&') {
SET_LEX_STATE(EXPR_BEG); SET_LEX_STATE(EXPR_BEG);
if ((c = nextc()) == '=') { if ((c = nextc()) == '=') {
set_yylval_id(tANDOP); set_yylval_id(idANDOP);
SET_LEX_STATE(EXPR_BEG); SET_LEX_STATE(EXPR_BEG);
return tOP_ASGN; return tOP_ASGN;
} }
@ -8408,7 +8402,7 @@ parser_yylex(struct parser_params *parser)
if ((c = nextc()) == '|') { if ((c = nextc()) == '|') {
SET_LEX_STATE(EXPR_BEG); SET_LEX_STATE(EXPR_BEG);
if ((c = nextc()) == '=') { if ((c = nextc()) == '=') {
set_yylval_id(tOROP); set_yylval_id(idOROP);
SET_LEX_STATE(EXPR_BEG); SET_LEX_STATE(EXPR_BEG);
return tOP_ASGN; return tOP_ASGN;
} }
@ -10562,7 +10556,7 @@ new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs,
{ {
NODE *asgn; NODE *asgn;
asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, change_shortcut_operator_id(op), rhs, cr); asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, cr);
fixpos(asgn, lhs); fixpos(asgn, lhs);
return asgn; return asgn;
} }
@ -10573,7 +10567,7 @@ new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rh
NODE *asgn; NODE *asgn;
if (lhs) { if (lhs) {
asgn = NEW_OP_CDECL(lhs, change_shortcut_operator_id(op), rhs, cr); asgn = NEW_OP_CDECL(lhs, op, rhs, cr);
} }
else { else {
asgn = NEW_BEGIN(0, cr); asgn = NEW_BEGIN(0, cr);