parse.y: switch

* parse.y (gettable_gen, assignable_gen): rewrite sequential if-else
  as switch.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36973 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-09-15 09:26:28 +00:00
parent b520b21839
commit 66d03f5f3c

85
parse.y
View File

@ -56,6 +56,7 @@ static ID register_symid_str(ID, VALUE);
#define is_const_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CONST) #define is_const_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CONST)
#define is_class_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CLASS) #define is_class_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CLASS)
#define is_junk_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_JUNK) #define is_junk_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_JUNK)
#define id_type(id) (is_notop_id(id) ? ((id)&ID_SCOPE_MASK) : -1)
#define is_asgn_or_id(id) ((is_notop_id(id)) && \ #define is_asgn_or_id(id) ((is_notop_id(id)) && \
(((id)&ID_SCOPE_MASK) == ID_GLOBAL || \ (((id)&ID_SCOPE_MASK) == ID_GLOBAL || \
@ -8504,44 +8505,36 @@ match_op_gen(struct parser_params *parser, NODE *node1, NODE *node2)
static NODE* static NODE*
gettable_gen(struct parser_params *parser, ID id) gettable_gen(struct parser_params *parser, ID id)
{ {
if (id == keyword_self) { switch (id) {
case keyword_self:
return NEW_SELF(); return NEW_SELF();
} case keyword_nil:
else if (id == keyword_nil) {
return NEW_NIL(); return NEW_NIL();
} case keyword_true:
else if (id == keyword_true) {
return NEW_TRUE(); return NEW_TRUE();
} case keyword_false:
else if (id == keyword_false) {
return NEW_FALSE(); return NEW_FALSE();
} case keyword__FILE__:
else if (id == keyword__FILE__) {
return NEW_STR(rb_external_str_new_with_enc(ruby_sourcefile, strlen(ruby_sourcefile), return NEW_STR(rb_external_str_new_with_enc(ruby_sourcefile, strlen(ruby_sourcefile),
rb_filesystem_encoding())); rb_filesystem_encoding()));
} case keyword__LINE__:
else if (id == keyword__LINE__) {
return NEW_LIT(INT2FIX(ruby_sourceline)); return NEW_LIT(INT2FIX(ruby_sourceline));
} case keyword__ENCODING__:
else if (id == keyword__ENCODING__) {
return NEW_LIT(rb_enc_from_encoding(parser->enc)); return NEW_LIT(rb_enc_from_encoding(parser->enc));
} }
else if (is_local_id(id)) { switch (id_type(id)) {
case ID_LOCAL:
if (dyna_in_block() && dvar_defined(id)) return NEW_DVAR(id); if (dyna_in_block() && dvar_defined(id)) return NEW_DVAR(id);
if (local_id(id)) return NEW_LVAR(id); if (local_id(id)) return NEW_LVAR(id);
/* method call without arguments */ /* method call without arguments */
return NEW_VCALL(id); return NEW_VCALL(id);
} case ID_GLOBAL:
else if (is_global_id(id)) {
return NEW_GVAR(id); return NEW_GVAR(id);
} case ID_INSTANCE:
else if (is_instance_id(id)) {
return NEW_IVAR(id); return NEW_IVAR(id);
} case ID_CONST:
else if (is_const_id(id)) {
return NEW_CONST(id); return NEW_CONST(id);
} case ID_CLASS:
else if (is_class_id(id)) {
return NEW_CVAR(id); return NEW_CVAR(id);
} }
compile_error(PARSER_ARG "identifier %s is not valid to get", rb_id2name(id)); compile_error(PARSER_ARG "identifier %s is not valid to get", rb_id2name(id));
@ -8599,28 +8592,31 @@ assignable_gen(struct parser_params *parser, ID id, NODE *val)
# define assignable_result(x) (x) # define assignable_result(x) (x)
#endif #endif
if (!id) return assignable_result(0); if (!id) return assignable_result(0);
if (id == keyword_self) { switch (id) {
case keyword_self:
yyerror("Can't change the value of self"); yyerror("Can't change the value of self");
} break;
else if (id == keyword_nil) { case keyword_nil:
yyerror("Can't assign to nil"); yyerror("Can't assign to nil");
} break;
else if (id == keyword_true) { case keyword_true:
yyerror("Can't assign to true"); yyerror("Can't assign to true");
} break;
else if (id == keyword_false) { case keyword_false:
yyerror("Can't assign to false"); yyerror("Can't assign to false");
} break;
else if (id == keyword__FILE__) { case keyword__FILE__:
yyerror("Can't assign to __FILE__"); yyerror("Can't assign to __FILE__");
} break;
else if (id == keyword__LINE__) { case keyword__LINE__:
yyerror("Can't assign to __LINE__"); yyerror("Can't assign to __LINE__");
} break;
else if (id == keyword__ENCODING__) { case keyword__ENCODING__:
yyerror("Can't assign to __ENCODING__"); yyerror("Can't assign to __ENCODING__");
break;
} }
else if (is_local_id(id)) { switch (id_type(id)) {
case ID_LOCAL:
if (dyna_in_block()) { if (dyna_in_block()) {
if (dvar_curr(id)) { if (dvar_curr(id)) {
return assignable_result(NEW_DASGN_CURR(id, val)); return assignable_result(NEW_DASGN_CURR(id, val));
@ -8642,22 +8638,19 @@ assignable_gen(struct parser_params *parser, ID id, NODE *val)
} }
return assignable_result(NEW_LASGN(id, val)); return assignable_result(NEW_LASGN(id, val));
} }
} break;
else if (is_global_id(id)) { case ID_GLOBAL:
return assignable_result(NEW_GASGN(id, val)); return assignable_result(NEW_GASGN(id, val));
} case ID_INSTANCE:
else if (is_instance_id(id)) {
return assignable_result(NEW_IASGN(id, val)); return assignable_result(NEW_IASGN(id, val));
} case ID_CONST:
else if (is_const_id(id)) {
if (!in_def && !in_single) if (!in_def && !in_single)
return assignable_result(NEW_CDECL(id, val, 0)); return assignable_result(NEW_CDECL(id, val, 0));
yyerror("dynamic constant assignment"); yyerror("dynamic constant assignment");
} break;
else if (is_class_id(id)) { case ID_CLASS:
return assignable_result(NEW_CVASGN(id, val)); return assignable_result(NEW_CVASGN(id, val));
} default:
else {
compile_error(PARSER_ARG "identifier %s is not valid to set", rb_id2name(id)); compile_error(PARSER_ARG "identifier %s is not valid to set", rb_id2name(id));
} }
return assignable_result(0); return assignable_result(0);