Skip to content

Commit be5cb60

Browse files
committed
Add class variables to the constant pool
1 parent 1f94f55 commit be5cb60

File tree

18 files changed

+81
-41
lines changed

18 files changed

+81
-41
lines changed

config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,8 @@ nodes:
727727
^^^^^^^^^^^^^
728728
- name: ClassVariableAndWriteNode
729729
child_nodes:
730+
- name: name
731+
type: constant
730732
- name: name_loc
731733
type: location
732734
- name: operator_loc
@@ -740,6 +742,8 @@ nodes:
740742
^^^^^^^^^^^^^^^^
741743
- name: ClassVariableOperatorWriteNode
742744
child_nodes:
745+
- name: name
746+
type: constant
743747
- name: name_loc
744748
type: location
745749
- name: operator_loc
@@ -755,6 +759,8 @@ nodes:
755759
^^^^^^^^^^^^^^^^^
756760
- name: ClassVariableOrWriteNode
757761
child_nodes:
762+
- name: name
763+
type: constant
758764
- name: name_loc
759765
type: location
760766
- name: operator_loc
@@ -767,19 +773,27 @@ nodes:
767773
@@target ||= value
768774
^^^^^^^^^^^^^^^^^^
769775
- name: ClassVariableReadNode
776+
child_nodes:
777+
- name: name
778+
type: constant
770779
comment: |
771780
Represents referencing a class variable.
772781
773782
@@foo
774783
^^^^^
775784
- name: ClassVariableTargetNode
785+
child_nodes:
786+
- name: name
787+
type: constant
776788
comment: |
777789
Represents writing to a class variable in a context that doesn't have an explicit value.
778790
779791
@@foo, @@bar = baz
780792
^^^^^ ^^^^^
781793
- name: ClassVariableWriteNode
782794
child_nodes:
795+
- name: name
796+
type: constant
783797
- name: name_loc
784798
type: location
785799
- name: value

lib/yarp/desugar_visitor.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class DesugarVisitor < MutationVisitor
88
#
99
# @@foo && @@foo = bar
1010
def visit_class_variable_and_write_node(node)
11-
desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode)
11+
desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
1212
end
1313

1414
# @@foo ||= bar
@@ -17,7 +17,7 @@ def visit_class_variable_and_write_node(node)
1717
#
1818
# defined?(@@foo) ? @@foo : @@foo = bar
1919
def visit_class_variable_or_write_node(node)
20-
desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode)
20+
desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
2121
end
2222

2323
# @@foo += bar
@@ -26,7 +26,7 @@ def visit_class_variable_or_write_node(node)
2626
#
2727
# @@foo = @@foo + bar
2828
def visit_class_variable_operator_write_node(node)
29-
desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode)
29+
desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
3030
end
3131

3232
# Foo &&= bar
@@ -245,15 +245,15 @@ def desugar_or_write_node(node, read_class, write_class, arguments: [])
245245
end
246246

247247
# Don't desugar `x ||= y` to `defined?(x) ? x : x = y`
248-
def desugar_or_write_defined_node(node, read_class, write_class)
248+
def desugar_or_write_defined_node(node, read_class, write_class, arguments: [])
249249
IfNode.new(
250250
node.operator_loc,
251-
DefinedNode.new(nil, read_class.new(node.name_loc), nil, node.operator_loc, node.name_loc),
252-
StatementsNode.new([read_class.new(node.name_loc)], node.location),
251+
DefinedNode.new(nil, read_class.new(*arguments, node.name_loc), nil, node.operator_loc, node.name_loc),
252+
StatementsNode.new([read_class.new(*arguments, node.name_loc)], node.location),
253253
ElseNode.new(
254254
node.operator_loc,
255255
StatementsNode.new(
256-
[write_class.new(node.name_loc, node.value, node.operator_loc, node.location)],
256+
[write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location)],
257257
node.location
258258
),
259259
node.operator_loc,

src/yarp.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,20 +1558,20 @@ yp_class_node_create(yp_parser_t *parser, yp_constant_id_list_t *locals, const y
15581558

15591559
// Allocate and initialize a new ClassVariableAndWriteNode node.
15601560
static yp_class_variable_and_write_node_t *
1561-
yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
1562-
assert(YP_NODE_TYPE_P(target, YP_NODE_CLASS_VARIABLE_READ_NODE));
1561+
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) {
15631562
assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL);
15641563
yp_class_variable_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_and_write_node_t);
15651564

15661565
*node = (yp_class_variable_and_write_node_t) {
15671566
{
15681567
.type = YP_NODE_CLASS_VARIABLE_AND_WRITE_NODE,
15691568
.location = {
1570-
.start = target->location.start,
1569+
.start = target->base.location.start,
15711570
.end = value->location.end
15721571
}
15731572
},
1574-
.name_loc = target->location,
1573+
.name = target->name,
1574+
.name_loc = target->base.location,
15751575
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
15761576
.value = value
15771577
};
@@ -1581,18 +1581,19 @@ yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target,
15811581

15821582
// Allocate and initialize a new ClassVariableOperatorWriteNode node.
15831583
static yp_class_variable_operator_write_node_t *
1584-
yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
1584+
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) {
15851585
yp_class_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_operator_write_node_t);
15861586

15871587
*node = (yp_class_variable_operator_write_node_t) {
15881588
{
15891589
.type = YP_NODE_CLASS_VARIABLE_OPERATOR_WRITE_NODE,
15901590
.location = {
1591-
.start = target->location.start,
1591+
.start = target->base.location.start,
15921592
.end = value->location.end
15931593
}
15941594
},
1595-
.name_loc = target->location,
1595+
.name = target->name,
1596+
.name_loc = target->base.location,
15961597
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
15971598
.value = value,
15981599
.operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1)
@@ -1603,20 +1604,20 @@ yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *tar
16031604

16041605
// Allocate and initialize a new ClassVariableOrWriteNode node.
16051606
static yp_class_variable_or_write_node_t *
1606-
yp_class_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
1607-
assert(YP_NODE_TYPE_P(target, YP_NODE_CLASS_VARIABLE_READ_NODE));
1607+
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) {
16081608
assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL);
16091609
yp_class_variable_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_or_write_node_t);
16101610

16111611
*node = (yp_class_variable_or_write_node_t) {
16121612
{
16131613
.type = YP_NODE_CLASS_VARIABLE_OR_WRITE_NODE,
16141614
.location = {
1615-
.start = target->location.start,
1615+
.start = target->base.location.start,
16161616
.end = value->location.end
16171617
}
16181618
},
1619-
.name_loc = target->location,
1619+
.name = target->name,
1620+
.name_loc = target->base.location,
16201621
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
16211622
.value = value
16221623
};
@@ -1629,13 +1630,21 @@ static yp_class_variable_read_node_t *
16291630
yp_class_variable_read_node_create(yp_parser_t *parser, const yp_token_t *token) {
16301631
assert(token->type == YP_TOKEN_CLASS_VARIABLE);
16311632
yp_class_variable_read_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_read_node_t);
1632-
*node = (yp_class_variable_read_node_t) {{ .type = YP_NODE_CLASS_VARIABLE_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(token) }};
1633+
1634+
*node = (yp_class_variable_read_node_t) {
1635+
{
1636+
.type = YP_NODE_CLASS_VARIABLE_READ_NODE,
1637+
.location = YP_LOCATION_TOKEN_VALUE(token)
1638+
},
1639+
.name = yp_parser_constant_id_location(parser, token->start, token->end)
1640+
};
1641+
16331642
return node;
16341643
}
16351644

16361645
// Initialize a new ClassVariableWriteNode node from a ClassVariableRead node.
16371646
static yp_class_variable_write_node_t *
1638-
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) {
1647+
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) {
16391648
yp_class_variable_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_write_node_t);
16401649

16411650
*node = (yp_class_variable_write_node_t) {
@@ -1646,6 +1655,7 @@ yp_class_variable_read_node_to_class_variable_write_node(yp_parser_t *parser, yp
16461655
.end = value->location.end
16471656
},
16481657
},
1658+
.name = read_node->name,
16491659
.name_loc = YP_LOCATION_NODE_VALUE((yp_node_t *) read_node),
16501660
.operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator),
16511661
.value = value
@@ -8007,7 +8017,7 @@ parse_write(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_nod
80078017
case YP_NODE_MISSING_NODE:
80088018
return target;
80098019
case YP_NODE_CLASS_VARIABLE_READ_NODE: {
8010-
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);
8020+
yp_class_variable_write_node_t *write_node = yp_class_variable_write_node_create(parser, (yp_class_variable_read_node_t *) target, operator, value);
80118021
yp_node_destroy(parser, target);
80128022
return (yp_node_t *) write_node;
80138023
}
@@ -12837,7 +12847,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
1283712847
parser_lex(parser);
1283812848

1283912849
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after &&=");
12840-
yp_node_t *result = (yp_node_t *) yp_class_variable_and_write_node_create(parser, node, &token, value);
12850+
yp_node_t *result = (yp_node_t *) yp_class_variable_and_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value);
1284112851

1284212852
yp_node_destroy(parser, node);
1284312853
return result;
@@ -12938,7 +12948,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
1293812948
parser_lex(parser);
1293912949

1294012950
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after ||=");
12941-
yp_node_t *result = (yp_node_t *) yp_class_variable_or_write_node_create(parser, node, &token, value);
12951+
yp_node_t *result = (yp_node_t *) yp_class_variable_or_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value);
1294212952

1294312953
yp_node_destroy(parser, node);
1294412954
return result;
@@ -13049,7 +13059,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
1304913059
parser_lex(parser);
1305013060

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

1305413064
yp_node_destroy(parser, node);
1305513065
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/strings.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/unparser/corpus/literal/assignment.txt

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/unparser/corpus/literal/dstr.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/unparser/corpus/literal/literal.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)