Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4c77944

Browse files
committedSep 28, 2023
Mark ranges as static literal
1 parent 81265ed commit 4c77944

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed
 

‎src/prism.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ pm_array_node_elements_append(pm_array_node_t *node, pm_node_t *element) {
907907

908908
// If the element is not a static literal, then the array is not a static
909909
// literal. Turn that flag off.
910-
if (PM_NODE_TYPE_P(element, PM_ARRAY_NODE) || PM_NODE_TYPE_P(element, PM_HASH_NODE) || (element->flags & PM_NODE_FLAG_STATIC_LITERAL) == 0) {
910+
if (PM_NODE_TYPE_P(element, PM_ARRAY_NODE) || PM_NODE_TYPE_P(element, PM_HASH_NODE) || PM_NODE_TYPE_P(element, PM_RANGE_NODE) || (element->flags & PM_NODE_FLAG_STATIC_LITERAL) == 0) {
911911
node->base.flags &= (pm_node_flags_t) ~PM_NODE_FLAG_STATIC_LITERAL;
912912
}
913913
}
@@ -1051,8 +1051,10 @@ pm_assoc_node_create(pm_parser_t *parser, pm_node_t *key, const pm_token_t *oper
10511051
end = key->location.end;
10521052
}
10531053

1054+
// If the key and value of this assoc node are both static literals, then
1055+
// we can mark this node as a static literal.
10541056
pm_node_flags_t flags = 0;
1055-
if (value && !PM_NODE_TYPE_P(value, PM_ARRAY_NODE) && !PM_NODE_TYPE_P(value, PM_HASH_NODE)) {
1057+
if (value && !PM_NODE_TYPE_P(value, PM_ARRAY_NODE) && !PM_NODE_TYPE_P(value, PM_HASH_NODE) && !PM_NODE_TYPE_P(value, PM_RANGE_NODE)) {
10561058
flags = key->flags & value->flags & PM_NODE_FLAG_STATIC_LITERAL;
10571059
}
10581060

@@ -3870,10 +3872,27 @@ pm_pre_execution_node_create(pm_parser_t *parser, const pm_token_t *keyword, con
38703872
static pm_range_node_t *
38713873
pm_range_node_create(pm_parser_t *parser, pm_node_t *left, const pm_token_t *operator, pm_node_t *right) {
38723874
pm_range_node_t *node = PM_ALLOC_NODE(parser, pm_range_node_t);
3875+
pm_node_flags_t flags = 0;
3876+
3877+
// Indicate that this node an exclusive range if the operator is `...`.
3878+
if (operator->type == PM_TOKEN_DOT_DOT_DOT || operator->type == PM_TOKEN_UDOT_DOT_DOT) {
3879+
flags |= PM_RANGE_FLAGS_EXCLUDE_END;
3880+
}
3881+
3882+
// Indicate that this node is a static literal (i.e., can be compiled with
3883+
// a putobject in CRuby) if the left and right are implicit nil, explicit
3884+
// nil, or integers.
3885+
if (
3886+
(left == NULL || PM_NODE_TYPE_P(left, PM_NIL_NODE) || PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) &&
3887+
(right == NULL || PM_NODE_TYPE_P(right, PM_NIL_NODE) || PM_NODE_TYPE_P(right, PM_INTEGER_NODE))
3888+
) {
3889+
flags |= PM_NODE_FLAG_STATIC_LITERAL;
3890+
}
38733891

38743892
*node = (pm_range_node_t) {
38753893
{
38763894
.type = PM_RANGE_NODE,
3895+
.flags = flags,
38773896
.location = {
38783897
.start = (left == NULL ? operator->start : left->location.start),
38793898
.end = (right == NULL ? operator->end : right->location.end)
@@ -3884,15 +3903,6 @@ pm_range_node_create(pm_parser_t *parser, pm_node_t *left, const pm_token_t *ope
38843903
.operator_loc = PM_LOCATION_TOKEN_VALUE(operator)
38853904
};
38863905

3887-
switch (operator->type) {
3888-
case PM_TOKEN_DOT_DOT_DOT:
3889-
case PM_TOKEN_UDOT_DOT_DOT:
3890-
node->base.flags |= PM_RANGE_FLAGS_EXCLUDE_END;
3891-
break;
3892-
default:
3893-
break;
3894-
}
3895-
38963906
return node;
38973907
}
38983908

0 commit comments

Comments
 (0)
Please sign in to comment.