[ruby/yarp] Rename constant pool fields to name or operator

* `constant_id` and `operator_id` are confusing.
* See https://github.com/ruby/yarp/issues/1296

https://github.com/ruby/yarp/commit/09d0a144df
This commit is contained in:
Benoit Daloze 2023-08-27 17:56:53 +02:00 committed by git
parent 412e586afe
commit 5937d01f7f
4 changed files with 38 additions and 38 deletions

View File

@ -462,10 +462,10 @@ module YARP
# order here so that we can compare properly. # order here so that we can compare properly.
if params if params
sorted = [ sorted = [
*params.requireds.grep(RequiredParameterNode).map(&:constant_id), *params.requireds.grep(RequiredParameterNode).map(&:name),
*params.optionals.map(&:constant_id), *params.optionals.map(&:name),
*((params.rest.name ? params.rest.name.to_sym : :*) if params.rest && params.rest.operator != ","), *((params.rest.name ? params.rest.name.to_sym : :*) if params.rest && params.rest.operator != ","),
*params.posts.grep(RequiredParameterNode).map(&:constant_id), *params.posts.grep(RequiredParameterNode).map(&:name),
*params.keywords.reject(&:value).map { |param| param.name.chomp(":").to_sym }, *params.keywords.reject(&:value).map { |param| param.name.chomp(":").to_sym },
*params.keywords.select(&:value).map { |param| param.name.chomp(":").to_sym } *params.keywords.select(&:value).map { |param| param.name.chomp(":").to_sym }
] ]
@ -485,9 +485,9 @@ module YARP
when RequiredDestructuredParameterNode when RequiredDestructuredParameterNode
param_stack.concat(param.parameters.reverse) param_stack.concat(param.parameters.reverse)
when RequiredParameterNode when RequiredParameterNode
sorted << param.constant_id sorted << param.name
when SplatNode when SplatNode
sorted << param.expression.constant_id if param.expression sorted << param.expression.name if param.expression
end end
end end

View File

@ -210,8 +210,8 @@ module YARP
# foo && foo = bar # foo && foo = bar
def visit_local_variable_and_write_node(node) def visit_local_variable_and_write_node(node)
AndNode.new( AndNode.new(
LocalVariableReadNode.new(node.constant_id, node.depth, node.name_loc), LocalVariableReadNode.new(node.name, node.depth, node.name_loc),
LocalVariableWriteNode.new(node.constant_id, node.depth, node.name_loc, node.value, node.operator_loc, node.location), LocalVariableWriteNode.new(node.name, node.depth, node.name_loc, node.value, node.operator_loc, node.location),
node.operator_loc, node.operator_loc,
node.location node.location
) )
@ -224,8 +224,8 @@ module YARP
# foo || foo = bar # foo || foo = bar
def visit_local_variable_or_write_node(node) def visit_local_variable_or_write_node(node)
OrNode.new( OrNode.new(
LocalVariableReadNode.new(node.constant_id, node.depth, node.name_loc), LocalVariableReadNode.new(node.name, node.depth, node.name_loc),
LocalVariableWriteNode.new(node.constant_id, node.depth, node.name_loc, node.value, node.operator_loc, node.location), LocalVariableWriteNode.new(node.name, node.depth, node.name_loc, node.value, node.operator_loc, node.location),
node.operator_loc, node.operator_loc,
node.location node.location
) )
@ -237,7 +237,7 @@ module YARP
# #
# foo = foo + bar # foo = foo + bar
def visit_local_variable_operator_write_node(node) def visit_local_variable_operator_write_node(node)
desugar_operator_write_node(node, LocalVariableWriteNode, LocalVariableReadNode, arguments: [node.constant_id, node.depth]) desugar_operator_write_node(node, LocalVariableWriteNode, LocalVariableReadNode, arguments: [node.name, node.depth])
end end
private private

View File

@ -662,7 +662,7 @@ nodes:
type: location type: location
- name: value - name: value
type: node type: node
- name: operator_id - name: operator
type: constant type: constant
comment: | comment: |
Represents the use of an assignment operator on a call. Represents the use of an assignment operator on a call.
@ -1492,7 +1492,7 @@ nodes:
type: location type: location
- name: value - name: value
type: node type: node
- name: constant_id - name: name
type: constant type: constant
- name: depth - name: depth
type: uint32 type: uint32
@ -1509,9 +1509,9 @@ nodes:
type: location type: location
- name: value - name: value
type: node type: node
- name: constant_id - name: name
type: constant type: constant
- name: operator_id - name: operator
type: constant type: constant
- name: depth - name: depth
type: uint32 type: uint32
@ -1528,7 +1528,7 @@ nodes:
type: location type: location
- name: value - name: value
type: node type: node
- name: constant_id - name: name
type: constant type: constant
- name: depth - name: depth
type: uint32 type: uint32
@ -1539,7 +1539,7 @@ nodes:
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- name: LocalVariableReadNode - name: LocalVariableReadNode
child_nodes: child_nodes:
- name: constant_id - name: name
type: constant type: constant
- name: depth - name: depth
type: uint32 type: uint32
@ -1552,7 +1552,7 @@ nodes:
^^^ ^^^
- name: LocalVariableTargetNode - name: LocalVariableTargetNode
child_nodes: child_nodes:
- name: constant_id - name: name
type: constant type: constant
- name: depth - name: depth
type: uint32 type: uint32
@ -1563,7 +1563,7 @@ nodes:
^^^ ^^^ ^^^ ^^^
- name: LocalVariableWriteNode - name: LocalVariableWriteNode
child_nodes: child_nodes:
- name: constant_id - name: name
type: constant type: constant
- name: depth - name: depth
type: uint32 type: uint32
@ -1682,7 +1682,7 @@ nodes:
^^ ^^
- name: OptionalParameterNode - name: OptionalParameterNode
child_nodes: child_nodes:
- name: constant_id - name: name
type: constant type: constant
- name: name_loc - name: name_loc
type: location type: location
@ -1883,7 +1883,7 @@ nodes:
end end
- name: RequiredParameterNode - name: RequiredParameterNode
child_nodes: child_nodes:
- name: constant_id - name: name
type: constant type: constant
comment: | comment: |
Represents a required parameter to a method, block, or lambda definition. Represents a required parameter to a method, block, or lambda definition.

View File

@ -1434,7 +1434,7 @@ yp_call_operator_write_node_create(yp_parser_t *parser, yp_call_node_t *target,
.target = target, .target = target,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value, .value = value,
.operator_id = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1)
}; };
return node; return node;
@ -2959,7 +2959,7 @@ yp_lambda_node_create(
// Allocate and initialize a new LocalVariableAndWriteNode node. // Allocate and initialize a new LocalVariableAndWriteNode node.
static yp_local_variable_and_write_node_t * static yp_local_variable_and_write_node_t *
yp_local_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value, yp_constant_id_t constant_id, uint32_t depth) { yp_local_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value, yp_constant_id_t name, uint32_t depth) {
assert(YP_NODE_TYPE_P(target, YP_NODE_LOCAL_VARIABLE_READ_NODE) || YP_NODE_TYPE_P(target, YP_NODE_CALL_NODE)); assert(YP_NODE_TYPE_P(target, YP_NODE_LOCAL_VARIABLE_READ_NODE) || YP_NODE_TYPE_P(target, YP_NODE_CALL_NODE));
assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL); assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL);
yp_local_variable_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_local_variable_and_write_node_t); yp_local_variable_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_local_variable_and_write_node_t);
@ -2975,7 +2975,7 @@ yp_local_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target,
.name_loc = target->location, .name_loc = target->location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value, .value = value,
.constant_id = constant_id, .name = name,
.depth = depth .depth = depth
}; };
@ -2984,7 +2984,7 @@ yp_local_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target,
// Allocate and initialize a new LocalVariableOperatorWriteNode node. // Allocate and initialize a new LocalVariableOperatorWriteNode node.
static yp_local_variable_operator_write_node_t * static yp_local_variable_operator_write_node_t *
yp_local_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value, yp_constant_id_t constant_id, uint32_t depth) { yp_local_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value, yp_constant_id_t name, uint32_t depth) {
yp_local_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_local_variable_operator_write_node_t); yp_local_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_local_variable_operator_write_node_t);
*node = (yp_local_variable_operator_write_node_t) { *node = (yp_local_variable_operator_write_node_t) {
@ -2998,8 +2998,8 @@ yp_local_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *tar
.name_loc = target->location, .name_loc = target->location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value, .value = value,
.constant_id = constant_id, .name = name,
.operator_id = yp_parser_constant_id_location(parser, operator->start, operator->end - 1), .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1),
.depth = depth .depth = depth
}; };
@ -3008,7 +3008,7 @@ yp_local_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *tar
// Allocate and initialize a new LocalVariableOrWriteNode node. // Allocate and initialize a new LocalVariableOrWriteNode node.
static yp_local_variable_or_write_node_t * static yp_local_variable_or_write_node_t *
yp_local_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value, yp_constant_id_t constant_id, uint32_t depth) { yp_local_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value, yp_constant_id_t name, uint32_t depth) {
assert(YP_NODE_TYPE_P(target, YP_NODE_LOCAL_VARIABLE_READ_NODE) || YP_NODE_TYPE_P(target, YP_NODE_CALL_NODE)); assert(YP_NODE_TYPE_P(target, YP_NODE_LOCAL_VARIABLE_READ_NODE) || YP_NODE_TYPE_P(target, YP_NODE_CALL_NODE));
assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL); assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL);
yp_local_variable_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_local_variable_or_write_node_t); yp_local_variable_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_local_variable_or_write_node_t);
@ -3024,7 +3024,7 @@ yp_local_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, c
.name_loc = target->location, .name_loc = target->location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value, .value = value,
.constant_id = constant_id, .name = name,
.depth = depth .depth = depth
}; };
@ -3041,7 +3041,7 @@ yp_local_variable_read_node_create(yp_parser_t *parser, const yp_token_t *name,
.type = YP_NODE_LOCAL_VARIABLE_READ_NODE, .type = YP_NODE_LOCAL_VARIABLE_READ_NODE,
.location = YP_LOCATION_TOKEN_VALUE(name) .location = YP_LOCATION_TOKEN_VALUE(name)
}, },
.constant_id = yp_parser_constant_id_token(parser, name), .name = yp_parser_constant_id_token(parser, name),
.depth = depth .depth = depth
}; };
@ -3050,7 +3050,7 @@ yp_local_variable_read_node_create(yp_parser_t *parser, const yp_token_t *name,
// Allocate and initialize a new LocalVariableWriteNode node. // Allocate and initialize a new LocalVariableWriteNode node.
static yp_local_variable_write_node_t * static yp_local_variable_write_node_t *
yp_local_variable_write_node_create(yp_parser_t *parser, yp_constant_id_t constant_id, uint32_t depth, yp_node_t *value, const yp_location_t *name_loc, const yp_token_t *operator) { yp_local_variable_write_node_create(yp_parser_t *parser, yp_constant_id_t name, uint32_t depth, yp_node_t *value, const yp_location_t *name_loc, const yp_token_t *operator) {
yp_local_variable_write_node_t *node = YP_ALLOC_NODE(parser, yp_local_variable_write_node_t); yp_local_variable_write_node_t *node = YP_ALLOC_NODE(parser, yp_local_variable_write_node_t);
*node = (yp_local_variable_write_node_t) { *node = (yp_local_variable_write_node_t) {
@ -3061,7 +3061,7 @@ yp_local_variable_write_node_create(yp_parser_t *parser, yp_constant_id_t consta
.end = value->location.end .end = value->location.end
} }
}, },
.constant_id = constant_id, .name = name,
.depth = depth, .depth = depth,
.value = value, .value = value,
.name_loc = *name_loc, .name_loc = *name_loc,
@ -3081,7 +3081,7 @@ yp_local_variable_target_node_create(yp_parser_t *parser, const yp_token_t *name
.type = YP_NODE_LOCAL_VARIABLE_TARGET_NODE, .type = YP_NODE_LOCAL_VARIABLE_TARGET_NODE,
.location = YP_LOCATION_TOKEN_VALUE(name) .location = YP_LOCATION_TOKEN_VALUE(name)
}, },
.constant_id = yp_parser_constant_id_token(parser, name), .name = yp_parser_constant_id_token(parser, name),
.depth = 0 .depth = 0
}; };
@ -3279,7 +3279,7 @@ yp_optional_parameter_node_create(yp_parser_t *parser, const yp_token_t *name, c
.end = value->location.end .end = value->location.end
} }
}, },
.constant_id = yp_parser_constant_id_token(parser, name), .name = yp_parser_constant_id_token(parser, name),
.name_loc = YP_LOCATION_TOKEN_VALUE(name), .name_loc = YP_LOCATION_TOKEN_VALUE(name),
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value .value = value
@ -3630,7 +3630,7 @@ yp_required_parameter_node_create(yp_parser_t *parser, const yp_token_t *token)
.type = YP_NODE_REQUIRED_PARAMETER_NODE, .type = YP_NODE_REQUIRED_PARAMETER_NODE,
.location = YP_LOCATION_TOKEN_VALUE(token) .location = YP_LOCATION_TOKEN_VALUE(token)
}, },
.constant_id = yp_parser_constant_id_token(parser, token) .name = yp_parser_constant_id_token(parser, token)
}; };
return node; return node;
@ -7987,7 +7987,7 @@ parse_write(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_nod
case YP_NODE_LOCAL_VARIABLE_READ_NODE: { case YP_NODE_LOCAL_VARIABLE_READ_NODE: {
yp_local_variable_read_node_t *local_read = (yp_local_variable_read_node_t *) target; yp_local_variable_read_node_t *local_read = (yp_local_variable_read_node_t *) target;
yp_constant_id_t constant_id = local_read->constant_id; yp_constant_id_t constant_id = local_read->name;
uint32_t depth = local_read->depth; uint32_t depth = local_read->depth;
yp_location_t name_loc = target->location; yp_location_t name_loc = target->location;
@ -12793,7 +12793,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
parser_lex(parser); parser_lex(parser);
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after &&="); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after &&=");
yp_node_t *result = (yp_node_t *) yp_local_variable_and_write_node_create(parser, node, &token, value, cast->constant_id, cast->depth); yp_node_t *result = (yp_node_t *) yp_local_variable_and_write_node_create(parser, node, &token, value, cast->name, cast->depth);
yp_node_destroy(parser, node); yp_node_destroy(parser, node);
return result; return result;
@ -12894,7 +12894,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
parser_lex(parser); parser_lex(parser);
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after ||="); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after ||=");
yp_node_t *result = (yp_node_t *) yp_local_variable_or_write_node_create(parser, node, &token, value, cast->constant_id, cast->depth); yp_node_t *result = (yp_node_t *) yp_local_variable_or_write_node_create(parser, node, &token, value, cast->name, cast->depth);
yp_node_destroy(parser, node); yp_node_destroy(parser, node);
return result; return result;
@ -13005,7 +13005,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
parser_lex(parser); parser_lex(parser);
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after the operator."); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after the operator.");
yp_node_t *result = (yp_node_t *) yp_local_variable_operator_write_node_create(parser, node, &token, value, cast->constant_id, cast->depth); yp_node_t *result = (yp_node_t *) yp_local_variable_operator_write_node_create(parser, node, &token, value, cast->name, cast->depth);
yp_node_destroy(parser, node); yp_node_destroy(parser, node);
return result; return result;