Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix instance variable constants, and class variable constants #1365

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ nodes:
^^^^^^^^^^^^^
- name: ClassVariableAndWriteNode
child_nodes:
- name: name
type: constant
- name: name_loc
type: location
- name: operator_loc
Expand All @@ -740,6 +742,8 @@ nodes:
^^^^^^^^^^^^^^^^
- name: ClassVariableOperatorWriteNode
child_nodes:
- name: name
type: constant
- name: name_loc
type: location
- name: operator_loc
Expand All @@ -755,6 +759,8 @@ nodes:
^^^^^^^^^^^^^^^^^
- name: ClassVariableOrWriteNode
child_nodes:
- name: name
type: constant
- name: name_loc
type: location
- name: operator_loc
Expand All @@ -767,19 +773,27 @@ nodes:
@@target ||= value
^^^^^^^^^^^^^^^^^^
- name: ClassVariableReadNode
child_nodes:
- name: name
type: constant
comment: |
Represents referencing a class variable.

@@foo
^^^^^
- name: ClassVariableTargetNode
child_nodes:
- name: name
type: constant
comment: |
Represents writing to a class variable in a context that doesn't have an explicit value.

@@foo, @@bar = baz
^^^^^ ^^^^^
- name: ClassVariableWriteNode
child_nodes:
- name: name
type: constant
- name: name_loc
type: location
- name: value
Expand Down
14 changes: 7 additions & 7 deletions lib/yarp/desugar_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DesugarVisitor < MutationVisitor
#
# @@foo && @@foo = bar
def visit_class_variable_and_write_node(node)
desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode)
desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
end

# @@foo ||= bar
Expand All @@ -17,7 +17,7 @@ def visit_class_variable_and_write_node(node)
#
# defined?(@@foo) ? @@foo : @@foo = bar
def visit_class_variable_or_write_node(node)
desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode)
desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
end

# @@foo += bar
Expand All @@ -26,7 +26,7 @@ def visit_class_variable_or_write_node(node)
#
# @@foo = @@foo + bar
def visit_class_variable_operator_write_node(node)
desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode)
desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
end

# Foo &&= bar
Expand Down Expand Up @@ -245,15 +245,15 @@ def desugar_or_write_node(node, read_class, write_class, arguments: [])
end

# Don't desugar `x ||= y` to `defined?(x) ? x : x = y`
def desugar_or_write_defined_node(node, read_class, write_class)
def desugar_or_write_defined_node(node, read_class, write_class, arguments: [])
IfNode.new(
node.operator_loc,
DefinedNode.new(nil, read_class.new(node.name_loc), nil, node.operator_loc, node.name_loc),
StatementsNode.new([read_class.new(node.name_loc)], node.location),
DefinedNode.new(nil, read_class.new(*arguments, node.name_loc), nil, node.operator_loc, node.name_loc),
StatementsNode.new([read_class.new(*arguments, node.name_loc)], node.location),
ElseNode.new(
node.operator_loc,
StatementsNode.new(
[write_class.new(node.name_loc, node.value, node.operator_loc, node.location)],
[write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location)],
node.location
),
node.operator_loc,
Expand Down
46 changes: 28 additions & 18 deletions src/yarp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1558,20 +1558,20 @@ yp_class_node_create(yp_parser_t *parser, yp_constant_id_list_t *locals, const y

// Allocate and initialize a new ClassVariableAndWriteNode node.
static yp_class_variable_and_write_node_t *
yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
assert(YP_NODE_TYPE_P(target, YP_NODE_CLASS_VARIABLE_READ_NODE));
yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL);
yp_class_variable_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_and_write_node_t);

*node = (yp_class_variable_and_write_node_t) {
{
.type = YP_NODE_CLASS_VARIABLE_AND_WRITE_NODE,
.location = {
.start = target->location.start,
.start = target->base.location.start,
.end = value->location.end
}
},
.name_loc = target->location,
.name = target->name,
.name_loc = target->base.location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value
};
Expand All @@ -1581,18 +1581,19 @@ yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target,

// Allocate and initialize a new ClassVariableOperatorWriteNode node.
static yp_class_variable_operator_write_node_t *
yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
yp_class_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_operator_write_node_t);

*node = (yp_class_variable_operator_write_node_t) {
{
.type = YP_NODE_CLASS_VARIABLE_OPERATOR_WRITE_NODE,
.location = {
.start = target->location.start,
.start = target->base.location.start,
.end = value->location.end
}
},
.name_loc = target->location,
.name = target->name,
.name_loc = target->base.location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value,
.operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1)
Expand All @@ -1603,20 +1604,20 @@ yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *tar

// Allocate and initialize a new ClassVariableOrWriteNode node.
static yp_class_variable_or_write_node_t *
yp_class_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
assert(YP_NODE_TYPE_P(target, YP_NODE_CLASS_VARIABLE_READ_NODE));
yp_class_variable_or_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL);
yp_class_variable_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_or_write_node_t);

*node = (yp_class_variable_or_write_node_t) {
{
.type = YP_NODE_CLASS_VARIABLE_OR_WRITE_NODE,
.location = {
.start = target->location.start,
.start = target->base.location.start,
.end = value->location.end
}
},
.name_loc = target->location,
.name = target->name,
.name_loc = target->base.location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value
};
Expand All @@ -1629,13 +1630,21 @@ static yp_class_variable_read_node_t *
yp_class_variable_read_node_create(yp_parser_t *parser, const yp_token_t *token) {
assert(token->type == YP_TOKEN_CLASS_VARIABLE);
yp_class_variable_read_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_read_node_t);
*node = (yp_class_variable_read_node_t) {{ .type = YP_NODE_CLASS_VARIABLE_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(token) }};

*node = (yp_class_variable_read_node_t) {
{
.type = YP_NODE_CLASS_VARIABLE_READ_NODE,
.location = YP_LOCATION_TOKEN_VALUE(token)
},
.name = yp_parser_constant_id_location(parser, token->start, token->end)
};

return node;
}

// Initialize a new ClassVariableWriteNode node from a ClassVariableRead node.
static yp_class_variable_write_node_t *
yp_class_variable_read_node_to_class_variable_write_node(yp_parser_t *parser, yp_class_variable_read_node_t *read_node, yp_token_t *operator, yp_node_t *value) {
yp_class_variable_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *read_node, yp_token_t *operator, yp_node_t *value) {
yp_class_variable_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_write_node_t);

*node = (yp_class_variable_write_node_t) {
Expand All @@ -1646,6 +1655,7 @@ yp_class_variable_read_node_to_class_variable_write_node(yp_parser_t *parser, yp
.end = value->location.end
},
},
.name = read_node->name,
.name_loc = YP_LOCATION_NODE_VALUE((yp_node_t *) read_node),
.operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator),
.value = value
Expand Down Expand Up @@ -2702,7 +2712,7 @@ yp_instance_variable_read_node_create(yp_parser_t *parser, const yp_token_t *tok
.type = YP_NODE_INSTANCE_VARIABLE_READ_NODE,
.location = YP_LOCATION_TOKEN_VALUE(token)
},
.name = yp_parser_constant_id_location(parser, token->start + 1, token->end)
.name = yp_parser_constant_id_location(parser, token->start, token->end)
};

return node;
Expand Down Expand Up @@ -8007,7 +8017,7 @@ parse_write(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_nod
case YP_NODE_MISSING_NODE:
return target;
case YP_NODE_CLASS_VARIABLE_READ_NODE: {
yp_class_variable_write_node_t *write_node = yp_class_variable_read_node_to_class_variable_write_node(parser, (yp_class_variable_read_node_t *) target, operator, value);
yp_class_variable_write_node_t *write_node = yp_class_variable_write_node_create(parser, (yp_class_variable_read_node_t *) target, operator, value);
yp_node_destroy(parser, target);
return (yp_node_t *) write_node;
}
Expand Down Expand Up @@ -12837,7 +12847,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
parser_lex(parser);

yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after &&=");
yp_node_t *result = (yp_node_t *) yp_class_variable_and_write_node_create(parser, node, &token, value);
yp_node_t *result = (yp_node_t *) yp_class_variable_and_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value);

yp_node_destroy(parser, node);
return result;
Expand Down Expand Up @@ -12938,7 +12948,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
parser_lex(parser);

yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after ||=");
yp_node_t *result = (yp_node_t *) yp_class_variable_or_write_node_create(parser, node, &token, value);
yp_node_t *result = (yp_node_t *) yp_class_variable_or_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value);

yp_node_destroy(parser, node);
return result;
Expand Down Expand Up @@ -13049,7 +13059,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
parser_lex(parser);

yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after the operator.");
yp_node_t *result = (yp_node_t *) yp_class_variable_operator_write_node_create(parser, node, &token, value);
yp_node_t *result = (yp_node_t *) yp_class_variable_operator_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value);

yp_node_destroy(parser, node);
return result;
Expand Down
4 changes: 2 additions & 2 deletions test/yarp/snapshots/methods.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/yarp/snapshots/patterns.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/yarp/snapshots/seattlerb/case_in.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/yarp/snapshots/seattlerb/lasgn_ivar_env.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/yarp/snapshots/strings.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading