Skip to content

Commit f049932

Browse files
committed
Add instance variable names to the constant pool
1 parent 157be4f commit f049932

30 files changed

+103
-59
lines changed

config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,8 @@ nodes:
12981298
^^^^^^^^^^^
12991299
- name: InstanceVariableAndWriteNode
13001300
child_nodes:
1301+
- name: name
1302+
type: constant
13011303
- name: name_loc
13021304
type: location
13031305
- name: operator_loc
@@ -1311,6 +1313,8 @@ nodes:
13111313
^^^^^^^^^^^^^^^^^
13121314
- name: InstanceVariableOperatorWriteNode
13131315
child_nodes:
1316+
- name: name
1317+
type: constant
13141318
- name: name_loc
13151319
type: location
13161320
- name: operator_loc
@@ -1326,6 +1330,8 @@ nodes:
13261330
^^^^^^^^^^^^^^^^
13271331
- name: InstanceVariableOrWriteNode
13281332
child_nodes:
1333+
- name: name
1334+
type: constant
13291335
- name: name_loc
13301336
type: location
13311337
- name: operator_loc
@@ -1338,19 +1344,27 @@ nodes:
13381344
@target ||= value
13391345
^^^^^^^^^^^^^^^^^
13401346
- name: InstanceVariableReadNode
1347+
child_nodes:
1348+
- name: name
1349+
type: constant
13411350
comment: |
13421351
Represents referencing an instance variable.
13431352
13441353
@foo
13451354
^^^^
13461355
- name: InstanceVariableTargetNode
1356+
child_nodes:
1357+
- name: name
1358+
type: constant
13471359
comment: |
13481360
Represents writing to an instance variable in a context that doesn't have an explicit value.
13491361
13501362
@foo, @bar = baz
13511363
^^^^ ^^^^
13521364
- name: InstanceVariableWriteNode
13531365
child_nodes:
1366+
- name: name
1367+
type: constant
13541368
- name: name_loc
13551369
type: location
13561370
- name: value

lib/yarp/desugar_visitor.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ def visit_global_variable_operator_write_node(node)
173173
# @foo && @foo = bar
174174
def visit_instance_variable_and_write_node(node)
175175
AndNode.new(
176-
InstanceVariableReadNode.new(node.name_loc),
177-
InstanceVariableWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
176+
InstanceVariableReadNode.new(node.name, node.name_loc),
177+
InstanceVariableWriteNode.new(node.name, node.name_loc, node.value, node.operator_loc, node.location),
178178
node.operator_loc,
179179
node.location
180180
)
@@ -187,8 +187,8 @@ def visit_instance_variable_and_write_node(node)
187187
# @foo || @foo = bar
188188
def visit_instance_variable_or_write_node(node)
189189
OrNode.new(
190-
InstanceVariableReadNode.new(node.name_loc),
191-
InstanceVariableWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
190+
InstanceVariableReadNode.new(node.name, node.name_loc),
191+
InstanceVariableWriteNode.new(node.name, node.name_loc, node.value, node.operator_loc, node.location),
192192
node.operator_loc,
193193
node.location
194194
)
@@ -200,7 +200,7 @@ def visit_instance_variable_or_write_node(node)
200200
#
201201
# @foo = @foo + bar
202202
def visit_instance_variable_operator_write_node(node)
203-
desugar_operator_write_node(node, InstanceVariableWriteNode, InstanceVariableReadNode)
203+
desugar_operator_write_node(node, InstanceVariableWriteNode, InstanceVariableReadNode, arguments: [node.name])
204204
end
205205

206206
# foo &&= bar

src/yarp.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,20 +2621,20 @@ yp_in_node_create(yp_parser_t *parser, yp_node_t *pattern, yp_statements_node_t
26212621

26222622
// Allocate and initialize a new InstanceVariableAndWriteNode node.
26232623
static yp_instance_variable_and_write_node_t *
2624-
yp_instance_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
2625-
assert(YP_NODE_TYPE_P(target, YP_NODE_INSTANCE_VARIABLE_READ_NODE));
2624+
yp_instance_variable_and_write_node_create(yp_parser_t *parser, yp_instance_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
26262625
assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL);
26272626
yp_instance_variable_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_instance_variable_and_write_node_t);
26282627

26292628
*node = (yp_instance_variable_and_write_node_t) {
26302629
{
26312630
.type = YP_NODE_INSTANCE_VARIABLE_AND_WRITE_NODE,
26322631
.location = {
2633-
.start = target->location.start,
2632+
.start = target->base.location.start,
26342633
.end = value->location.end
26352634
}
26362635
},
2637-
.name_loc = target->location,
2636+
.name = target->name,
2637+
.name_loc = target->base.location,
26382638
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
26392639
.value = value
26402640
};
@@ -2644,18 +2644,19 @@ yp_instance_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *targe
26442644

26452645
// Allocate and initialize a new InstanceVariableOperatorWriteNode node.
26462646
static yp_instance_variable_operator_write_node_t *
2647-
yp_instance_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
2647+
yp_instance_variable_operator_write_node_create(yp_parser_t *parser, yp_instance_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
26482648
yp_instance_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_instance_variable_operator_write_node_t);
26492649

26502650
*node = (yp_instance_variable_operator_write_node_t) {
26512651
{
26522652
.type = YP_NODE_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE,
26532653
.location = {
2654-
.start = target->location.start,
2654+
.start = target->base.location.start,
26552655
.end = value->location.end
26562656
}
26572657
},
2658-
.name_loc = target->location,
2658+
.name = target->name,
2659+
.name_loc = target->base.location,
26592660
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
26602661
.value = value,
26612662
.operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1)
@@ -2666,20 +2667,20 @@ yp_instance_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *
26662667

26672668
// Allocate and initialize a new InstanceVariableOrWriteNode node.
26682669
static yp_instance_variable_or_write_node_t *
2669-
yp_instance_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
2670-
assert(YP_NODE_TYPE_P(target, YP_NODE_INSTANCE_VARIABLE_READ_NODE));
2670+
yp_instance_variable_or_write_node_create(yp_parser_t *parser, yp_instance_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
26712671
assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL);
26722672
yp_instance_variable_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_instance_variable_or_write_node_t);
26732673

26742674
*node = (yp_instance_variable_or_write_node_t) {
26752675
{
26762676
.type = YP_NODE_INSTANCE_VARIABLE_OR_WRITE_NODE,
26772677
.location = {
2678-
.start = target->location.start,
2678+
.start = target->base.location.start,
26792679
.end = value->location.end
26802680
}
26812681
},
2682-
.name_loc = target->location,
2682+
.name = target->name,
2683+
.name_loc = target->base.location,
26832684
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
26842685
.value = value
26852686
};
@@ -2693,9 +2694,13 @@ yp_instance_variable_read_node_create(yp_parser_t *parser, const yp_token_t *tok
26932694
assert(token->type == YP_TOKEN_INSTANCE_VARIABLE);
26942695
yp_instance_variable_read_node_t *node = YP_ALLOC_NODE(parser, yp_instance_variable_read_node_t);
26952696

2696-
*node = (yp_instance_variable_read_node_t) {{
2697-
.type = YP_NODE_INSTANCE_VARIABLE_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(token)
2698-
}};
2697+
*node = (yp_instance_variable_read_node_t) {
2698+
{
2699+
.type = YP_NODE_INSTANCE_VARIABLE_READ_NODE,
2700+
.location = YP_LOCATION_TOKEN_VALUE(token)
2701+
},
2702+
.name = yp_parser_constant_id_location(parser, token->start + 1, token->end)
2703+
};
26992704

27002705
return node;
27012706
}
@@ -2712,6 +2717,7 @@ yp_instance_variable_write_node_create(yp_parser_t *parser, yp_instance_variable
27122717
.end = value->location.end
27132718
}
27142719
},
2720+
.name = read_node->name,
27152721
.name_loc = YP_LOCATION_NODE_BASE_VALUE(read_node),
27162722
.operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator),
27172723
.value = value
@@ -12812,7 +12818,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
1281212818
parser_lex(parser);
1281312819

1281412820
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after &&=");
12815-
yp_node_t *result = (yp_node_t *) yp_instance_variable_and_write_node_create(parser, node, &token, value);
12821+
yp_node_t *result = (yp_node_t *) yp_instance_variable_and_write_node_create(parser, (yp_instance_variable_read_node_t *) node, &token, value);
1281612822

1281712823
yp_node_destroy(parser, node);
1281812824
return result;
@@ -12913,7 +12919,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
1291312919
parser_lex(parser);
1291412920

1291512921
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after ||=");
12916-
yp_node_t *result = (yp_node_t *) yp_instance_variable_or_write_node_create(parser, node, &token, value);
12922+
yp_node_t *result = (yp_node_t *) yp_instance_variable_or_write_node_create(parser, (yp_instance_variable_read_node_t *) node, &token, value);
1291712923

1291812924
yp_node_destroy(parser, node);
1291912925
return result;
@@ -13024,7 +13030,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
1302413030
parser_lex(parser);
1302513031

1302613032
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after the operator.");
13027-
yp_node_t *result = (yp_node_t *) yp_instance_variable_operator_write_node_create(parser, node, &token, value);
13033+
yp_node_t *result = (yp_node_t *) yp_instance_variable_operator_write_node_create(parser, (yp_instance_variable_read_node_t *) node, &token, value);
1302813034

1302913035
yp_node_destroy(parser, node);
1303013036
return result;

test/yarp/snapshots/methods.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/patterns.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/seattlerb/case_in.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes_windows.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/seattlerb/lasgn_ivar_env.txt

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/seattlerb/parse_line_call_ivar_arg_no_parens_line_break.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)