[ruby/yarp] Add global variables to the constant pool

https://github.com/ruby/yarp/commit/b48067b067
This commit is contained in:
Kevin Newton 2023-09-01 09:40:08 -04:00 коммит произвёл git
Родитель d1f83c37f8
Коммит c666077182
24 изменённых файлов: 111 добавлений и 65 удалений

Просмотреть файл

@ -62,7 +62,7 @@ module YARP
#
# $foo && $foo = bar
def visit_global_variable_and_write_node(node)
desugar_and_write_node(node, GlobalVariableReadNode, GlobalVariableWriteNode)
desugar_and_write_node(node, GlobalVariableReadNode, GlobalVariableWriteNode, arguments: [node.name])
end
# $foo ||= bar
@ -71,7 +71,7 @@ module YARP
#
# defined?($foo) ? $foo : $foo = bar
def visit_global_variable_or_write_node(node)
desugar_or_write_defined_node(node, GlobalVariableReadNode, GlobalVariableWriteNode)
desugar_or_write_defined_node(node, GlobalVariableReadNode, GlobalVariableWriteNode, arguments: [node.name])
end
# $foo += bar
@ -80,7 +80,7 @@ module YARP
#
# $foo = $foo + bar
def visit_global_variable_operator_write_node(node)
desugar_operator_write_node(node, GlobalVariableReadNode, GlobalVariableWriteNode)
desugar_operator_write_node(node, GlobalVariableReadNode, GlobalVariableWriteNode, arguments: [node.name])
end
# @foo &&= bar

Просмотреть файл

@ -1006,8 +1006,8 @@ module YARP
expected = BeginNode(
Location(),
StatementsNode([
GlobalVariableWriteNode(Location(), NilNode(), Location()),
GlobalVariableWriteNode(Location(), NilNode(), Location())
GlobalVariableWriteNode(:$+, Location(), NilNode(), Location()),
GlobalVariableWriteNode(:$1466, Location(), NilNode(), Location())
]),
nil,
nil,

Просмотреть файл

@ -31,7 +31,7 @@ ProgramNode(0...199)(
(61...66)
),
AliasNode(86...97)(
GlobalVariableReadNode(92...94)(),
GlobalVariableReadNode(92...94)(:$a),
BackReferenceReadNode(95...97)(),
(86...91)
),
@ -41,8 +41,8 @@ ProgramNode(0...199)(
(99...104)
),
AliasNode(114...129)(
GlobalVariableReadNode(120...124)(),
GlobalVariableReadNode(125...129)(),
GlobalVariableReadNode(120...124)(:$foo),
GlobalVariableReadNode(125...129)(:$bar),
(114...119)
),
AliasNode(131...143)(

Просмотреть файл

@ -1,30 +1,30 @@
ProgramNode(0...349)(
[],
StatementsNode(0...349)(
[GlobalVariableReadNode(0...16)(),
GlobalVariableReadNode(18...20)(),
GlobalVariableReadNode(22...25)(),
GlobalVariableReadNode(27...37)(),
GlobalVariableReadNode(39...45)(),
GlobalVariableReadNode(47...54)(),
GlobalVariableReadNode(56...63)(),
GlobalVariableReadNode(65...67)(),
GlobalVariableReadNode(69...71)(),
GlobalVariableReadNode(73...75)(),
[GlobalVariableReadNode(0...16)(:$global_variable),
GlobalVariableReadNode(18...20)(:$_),
GlobalVariableReadNode(22...25)(:$-w),
GlobalVariableReadNode(27...37)(:$LOAD_PATH),
GlobalVariableReadNode(39...45)(:$stdin),
GlobalVariableReadNode(47...54)(:$stdout),
GlobalVariableReadNode(56...63)(:$stderr),
GlobalVariableReadNode(65...67)(:$!),
GlobalVariableReadNode(69...71)(:$?),
GlobalVariableReadNode(73...75)(:$~),
BackReferenceReadNode(77...79)(),
BackReferenceReadNode(81...83)(),
BackReferenceReadNode(85...87)(),
BackReferenceReadNode(89...91)(),
GlobalVariableReadNode(93...95)(),
GlobalVariableReadNode(97...99)(),
GlobalVariableReadNode(101...103)(),
GlobalVariableReadNode(105...111)(),
GlobalVariableReadNode(113...122)(),
GlobalVariableReadNode(124...126)(),
GlobalVariableReadNode(128...131)(),
GlobalVariableReadNode(133...149)(),
GlobalVariableReadNode(151...159)(),
GlobalVariableReadNode(161...164)(),
GlobalVariableReadNode(93...95)(:$:),
GlobalVariableReadNode(97...99)(:$;),
GlobalVariableReadNode(101...103)(:$,),
GlobalVariableReadNode(105...111)(:$DEBUG),
GlobalVariableReadNode(113...122)(:$FILENAME),
GlobalVariableReadNode(124...126)(:$0),
GlobalVariableReadNode(128...131)(:$-0),
GlobalVariableReadNode(133...149)(:$LOADED_FEATURES),
GlobalVariableReadNode(151...159)(:$VERBOSE),
GlobalVariableReadNode(161...164)(:$-K),
SymbolNode(166...183)((166...167), (167...183), nil, "$global_variable"),
SymbolNode(185...188)((185...186), (186...188), nil, "$_"),
SymbolNode(190...194)((190...191), (191...194), nil, "$-w"),

Просмотреть файл

@ -173,7 +173,7 @@ ProgramNode(0...1194)(
),
DefNode(161...175)(
(170...171),
GlobalVariableReadNode(165...169)(),
GlobalVariableReadNode(165...169)(:$var),
nil,
nil,
[],

Просмотреть файл

@ -959,7 +959,7 @@ ProgramNode(0...3743)(
"foo"
),
PinnedVariableNode(988...993)(
GlobalVariableReadNode(989...993)(),
GlobalVariableReadNode(989...993)(:$bar),
(988...989)
),
(985...987)

Просмотреть файл

@ -21,7 +21,7 @@ ProgramNode(0...293)(
[StringNode(29...33)(nil, (29...33), nil, "aaa "),
EmbeddedVariableNode(33...38)(
(33...34),
GlobalVariableReadNode(34...38)()
GlobalVariableReadNode(34...38)(:$bbb)
)],
(38...39),
0

Просмотреть файл

@ -2,7 +2,7 @@ ProgramNode(0...15)(
[],
StatementsNode(0...15)(
[AliasNode(0...15)(
GlobalVariableReadNode(6...12)(),
GlobalVariableReadNode(6...12)(:$MATCH),
BackReferenceReadNode(13...15)(),
(0...5)
)]

Просмотреть файл

@ -1,7 +1,12 @@
ProgramNode(0...22)(
[:测试],
StatementsNode(0...22)(
[GlobalVariableWriteNode(0...11)((0...7), IntegerNode(10...11)(), (8...9)),
[GlobalVariableWriteNode(0...11)(
:$测试,
(0...7),
IntegerNode(10...11)(),
(8...9)
),
LocalVariableWriteNode(12...22)(
:测试,
0,

Просмотреть файл

@ -498,7 +498,7 @@ ProgramNode(0...747)(
(628...629)
),
PinnedVariableNode(633...636)(
GlobalVariableReadNode(634...636)(),
GlobalVariableReadNode(634...636)(:$b),
(633...634)
),
PinnedVariableNode(638...642)(

Просмотреть файл

@ -177,7 +177,7 @@ ProgramNode(0...498)(
(405...406),
[EmbeddedVariableNode(406...411)(
(406...407),
GlobalVariableReadNode(407...411)()
GlobalVariableReadNode(407...411)(:$foo)
)],
(411...412)
),

Просмотреть файл

@ -2,8 +2,8 @@ ProgramNode(0...31)(
[],
StatementsNode(0...31)(
[AliasNode(0...15)(
GlobalVariableReadNode(6...10)(),
GlobalVariableReadNode(11...15)(),
GlobalVariableReadNode(6...10)(:$foo),
GlobalVariableReadNode(11...15)(:$bar),
(0...5)
),
AliasNode(16...31)(

Просмотреть файл

@ -1,10 +1,15 @@
ProgramNode(0...704)(
[:a, :b, :foo, :c, :x],
StatementsNode(0...704)(
[GlobalVariableWriteNode(0...6)((0...2), IntegerNode(5...6)(), (3...4)),
[GlobalVariableWriteNode(0...6)(
:$a,
(0...2),
IntegerNode(5...6)(),
(3...4)
),
MultiWriteNode(7...24)(
[GlobalVariableTargetNode(8...10)(),
GlobalVariableTargetNode(12...14)()],
[GlobalVariableTargetNode(8...10)(:$a),
GlobalVariableTargetNode(12...14)(:$b)],
(16...17),
ArrayNode(18...24)(
[IntegerNode(19...20)(), IntegerNode(22...23)()],

Просмотреть файл

@ -66,7 +66,7 @@ ProgramNode(0...299)(
[StringNode(153...154)(nil, (153...154), nil, "a"),
EmbeddedVariableNode(154...157)(
(154...155),
GlobalVariableReadNode(155...157)()
GlobalVariableReadNode(155...157)(:$a)
)],
(157...158)
),

Просмотреть файл

@ -163,7 +163,7 @@ ProgramNode(0...916)(
StringNode(215...216)(nil, (215...216), nil, " "),
EmbeddedVariableNode(216...219)(
(216...217),
GlobalVariableReadNode(217...219)()
GlobalVariableReadNode(217...219)(:$a)
)],
(219...220)
),

Просмотреть файл

@ -4,7 +4,7 @@ ProgramNode(0...66)(
[CallNode(0...1)(nil, nil, (0...1), nil, nil, nil, nil, 2, "a"),
InstanceVariableReadNode(2...4)(:@a),
ClassVariableReadNode(5...8)(:@@a),
GlobalVariableReadNode(9...11)(),
GlobalVariableReadNode(9...11)(:$a),
NumberedReferenceReadNode(12...14)(1),
BackReferenceReadNode(15...17)(),
ConstantReadNode(18...23)(),

Просмотреть файл

@ -234,7 +234,7 @@ ProgramNode(0...608)(
[StringNode(587...588)(nil, (587...588), nil, "a"),
EmbeddedVariableNode(588...591)(
(588...589),
GlobalVariableReadNode(589...591)()
GlobalVariableReadNode(589...591)(:$a)
)],
(591...592)
),

Просмотреть файл

@ -27,11 +27,12 @@ ProgramNode(0...293)(
(42...43)
),
GlobalVariableWriteNode(50...58)(
:$abc,
(50...54),
IntegerNode(57...58)(),
(55...56)
),
GlobalVariableReadNode(60...64)(),
GlobalVariableReadNode(60...64)(:$abc),
InstanceVariableReadNode(66...70)(:@abc),
InstanceVariableWriteNode(72...80)(
:@abc,
@ -48,14 +49,15 @@ ProgramNode(0...293)(
(89...90)
),
MultiWriteNode(94...108)(
[GlobalVariableTargetNode(94...98)(),
GlobalVariableTargetNode(100...104)()],
[GlobalVariableTargetNode(94...98)(:$foo),
GlobalVariableTargetNode(100...104)(:$bar)],
(105...106),
IntegerNode(107...108)(),
nil,
nil
),
GlobalVariableWriteNode(110...121)(
:$foo,
(110...114),
ArrayNode(117...121)(
[IntegerNode(117...118)(), IntegerNode(120...121)()],

Просмотреть файл

@ -2,13 +2,13 @@ ProgramNode(0...24)(
[],
StatementsNode(0...24)(
[AliasNode(0...11)(
GlobalVariableReadNode(6...8)(),
GlobalVariableReadNode(6...8)(:$a),
BackReferenceReadNode(9...11)(),
(0...5)
),
AliasNode(13...24)(
GlobalVariableReadNode(19...21)(),
GlobalVariableReadNode(22...24)(),
GlobalVariableReadNode(19...21)(:$a),
GlobalVariableReadNode(22...24)(:$b),
(13...18)
)]
)

Просмотреть файл

@ -1,4 +1,4 @@
ProgramNode(0...4)(
[],
StatementsNode(0...4)([GlobalVariableReadNode(0...4)()])
StatementsNode(0...4)([GlobalVariableReadNode(0...4)(:$foo)])
)

Просмотреть файл

@ -1,6 +1,11 @@
ProgramNode(0...9)(
[],
StatementsNode(0...9)(
[GlobalVariableWriteNode(0...9)((0...4), IntegerNode(7...9)(), (5...6))]
[GlobalVariableWriteNode(0...9)(
:$var,
(0...4),
IntegerNode(7...9)(),
(5...6)
)]
)
)

Просмотреть файл

@ -15,7 +15,7 @@ ProgramNode(0...14)(
StringNode(9...10)(nil, (9...10), nil, " "),
EmbeddedVariableNode(10...13)(
(10...11),
GlobalVariableReadNode(11...13)()
GlobalVariableReadNode(11...13)(:$a)
)],
(13...14)
)]

Просмотреть файл

@ -1166,6 +1166,8 @@ nodes:
^^^^^
- name: GlobalVariableAndWriteNode
fields:
- name: name
type: constant
- name: name_loc
type: location
- name: operator_loc
@ -1179,6 +1181,8 @@ nodes:
^^^^^^^^^^^^^^^^^
- name: GlobalVariableOperatorWriteNode
fields:
- name: name
type: constant
- name: name_loc
type: location
- name: operator_loc
@ -1194,6 +1198,8 @@ nodes:
^^^^^^^^^^^^^^^^
- name: GlobalVariableOrWriteNode
fields:
- name: name
type: constant
- name: name_loc
type: location
- name: operator_loc
@ -1206,12 +1212,18 @@ nodes:
$target ||= value
^^^^^^^^^^^^^^^^^
- name: GlobalVariableReadNode
fields:
- name: name
type: constant
comment: |
Represents referencing a global variable.
$foo
^^^^
- name: GlobalVariableTargetNode
fields:
- name: name
type: constant
comment: |
Represents writing to a global variable in a context that doesn't have an explicit value.
@ -1219,6 +1231,8 @@ nodes:
^^^^ ^^^^
- name: GlobalVariableWriteNode
fields:
- name: name
type: constant
- name: name_loc
type: location
- name: value

Просмотреть файл

@ -2310,10 +2310,23 @@ yp_hash_pattern_node_node_list_create(yp_parser_t *parser, yp_node_list_t *assoc
return node;
}
// Retrieve the name from a node that will become a global variable write node.
static yp_constant_id_t
yp_global_variable_write_name(yp_parser_t *parser, yp_node_t *target) {
if (YP_NODE_TYPE_P(target, YP_NODE_GLOBAL_VARIABLE_READ_NODE)) {
return ((yp_global_variable_read_node_t *) target)->name;
}
assert(YP_NODE_TYPE_P(target, YP_NODE_BACK_REFERENCE_READ_NODE) || YP_NODE_TYPE_P(target, YP_NODE_NUMBERED_REFERENCE_READ_NODE));
// This will only ever happen in the event of a syntax error, but we
// still need to provide something for the node.
return yp_parser_constant_id_location(parser, target->location.start, target->location.end);
}
// Allocate and initialize a new GlobalVariableAndWriteNode node.
static yp_global_variable_and_write_node_t *
yp_global_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_GLOBAL_VARIABLE_READ_NODE) || YP_NODE_TYPE_P(target, YP_NODE_BACK_REFERENCE_READ_NODE) || YP_NODE_TYPE_P(target, YP_NODE_NUMBERED_REFERENCE_READ_NODE));
assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL);
yp_global_variable_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_global_variable_and_write_node_t);
@ -2325,6 +2338,7 @@ yp_global_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target,
.end = value->location.end
}
},
.name = yp_global_variable_write_name(parser, target),
.name_loc = target->location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value
@ -2346,6 +2360,7 @@ yp_global_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *ta
.end = value->location.end
}
},
.name = yp_global_variable_write_name(parser, target),
.name_loc = target->location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value,
@ -2358,7 +2373,6 @@ yp_global_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *ta
// Allocate and initialize a new GlobalVariableOrWriteNode node.
static yp_global_variable_or_write_node_t *
yp_global_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_GLOBAL_VARIABLE_READ_NODE) || YP_NODE_TYPE_P(target, YP_NODE_BACK_REFERENCE_READ_NODE) || YP_NODE_TYPE_P(target, YP_NODE_NUMBERED_REFERENCE_READ_NODE));
assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL);
yp_global_variable_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_global_variable_or_write_node_t);
@ -2370,6 +2384,7 @@ yp_global_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target,
.end = value->location.end
}
},
.name = yp_global_variable_write_name(parser, target),
.name_loc = target->location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value
@ -2387,7 +2402,8 @@ yp_global_variable_read_node_create(yp_parser_t *parser, const yp_token_t *name)
{
.type = YP_NODE_GLOBAL_VARIABLE_READ_NODE,
.location = YP_LOCATION_TOKEN_VALUE(name),
}
},
.name = yp_parser_constant_id_location(parser, name->start, name->end)
};
return node;
@ -2395,18 +2411,19 @@ yp_global_variable_read_node_create(yp_parser_t *parser, const yp_token_t *name)
// Allocate a new GlobalVariableWriteNode node.
static yp_global_variable_write_node_t *
yp_global_variable_write_node_create(yp_parser_t *parser, const yp_location_t *name_loc, const yp_token_t *operator, yp_node_t *value) {
yp_global_variable_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) {
yp_global_variable_write_node_t *node = YP_ALLOC_NODE(parser, yp_global_variable_write_node_t);
*node = (yp_global_variable_write_node_t) {
{
.type = YP_NODE_GLOBAL_VARIABLE_WRITE_NODE,
.location = {
.start = name_loc->start,
.start = target->location.start,
.end = value->location.end
},
},
.name_loc = *name_loc,
.name = yp_global_variable_write_name(parser, target),
.name_loc = YP_LOCATION_NODE_VALUE(target),
.operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator),
.value = value
};
@ -8068,16 +8085,15 @@ 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_write_node_create(parser, (yp_class_variable_read_node_t *) target, operator, value);
yp_class_variable_write_node_t *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;
return (yp_node_t *) node;
}
case YP_NODE_CONSTANT_PATH_NODE:
return (yp_node_t *) yp_constant_path_write_node_create(parser, (yp_constant_path_node_t *) target, operator, value);
case YP_NODE_CONSTANT_READ_NODE: {
yp_constant_write_node_t *node = yp_constant_write_node_create(parser, &target->location, operator, value);
yp_node_destroy(parser, target);
return (yp_node_t *) node;
}
case YP_NODE_BACK_REFERENCE_READ_NODE:
@ -8085,10 +8101,9 @@ parse_write(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_nod
yp_diagnostic_list_append(&parser->error_list, target->location.start, target->location.end, "Can't set variable");
/* fallthrough */
case YP_NODE_GLOBAL_VARIABLE_READ_NODE: {
yp_global_variable_write_node_t *result = yp_global_variable_write_node_create(parser, &target->location, operator, value);
yp_global_variable_write_node_t *node = yp_global_variable_write_node_create(parser, target, operator, value);
yp_node_destroy(parser, target);
return (yp_node_t *) result;
return (yp_node_t *) node;
}
case YP_NODE_LOCAL_VARIABLE_READ_NODE: {
yp_local_variable_read_node_t *local_read = (yp_local_variable_read_node_t *) target;