Skip to content

Commit

Permalink
Support the flipflop flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Aug 10, 2023
1 parent 7dfaef3 commit 6315890
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ flags:
values:
- name: EXCLUDE_END
comment: "... operator"
- name: FLIP_FLOP
comment: "a range used as a flip flop"
- name: RegularExpressionFlags
values:
- name: IGNORE_CASE
Expand Down
32 changes: 32 additions & 0 deletions src/yarp.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,32 @@ yp_parser_constant_id_token(yp_parser_t *parser, const yp_token_t *token) {
return yp_parser_constant_id_location(parser, token->start, token->end);
}

// Mark any range nodes in this subtree as flipflops.
static void
yp_flip_flop(yp_node_t *node) {
switch (YP_NODE_TYPE(node)) {
case YP_NODE_AND_NODE: {
yp_and_node_t *cast = (yp_and_node_t *) node;
yp_flip_flop(cast->left);
yp_flip_flop(cast->right);
break;
}
case YP_NODE_OR_NODE: {
yp_or_node_t *cast = (yp_or_node_t *) node;
yp_flip_flop(cast->left);
yp_flip_flop(cast->right);
break;
}
case YP_NODE_RANGE_NODE: {
yp_range_node_t *cast = (yp_range_node_t *) node;
cast->flags |= YP_RANGE_NODE_FLAGS_FLIP_FLOP;
break;
}
default:
break;
}
}

// In a lot of places in the tree you can have tokens that are not provided but
// that do not cause an error. For example, in a method call without
// parentheses. In these cases we set the token to the "not provided" type. For
Expand Down Expand Up @@ -2269,6 +2295,7 @@ yp_if_node_create(yp_parser_t *parser,
yp_node_t *consequent,
const yp_token_t *end_keyword
) {
yp_flip_flop(predicate);
yp_if_node_t *node = YP_ALLOC_NODE(parser, yp_if_node_t);

const char *end;
Expand Down Expand Up @@ -2304,6 +2331,7 @@ yp_if_node_create(yp_parser_t *parser,
// Allocate and initialize new IfNode node in the modifier form.
static yp_if_node_t *
yp_if_node_modifier_create(yp_parser_t *parser, yp_node_t *statement, const yp_token_t *if_keyword, yp_node_t *predicate) {
yp_flip_flop(predicate);
yp_if_node_t *node = YP_ALLOC_NODE(parser, yp_if_node_t);

yp_statements_node_t *statements = yp_statements_node_create(parser);
Expand Down Expand Up @@ -2331,6 +2359,8 @@ yp_if_node_modifier_create(yp_parser_t *parser, yp_node_t *statement, const yp_t
// Allocate and initialize an if node from a ternary expression.
static yp_if_node_t *
yp_if_node_ternary_create(yp_parser_t *parser, yp_node_t *predicate, yp_node_t *true_expression, const yp_token_t *colon, yp_node_t *false_expression) {
yp_flip_flop(predicate);

yp_statements_node_t *if_statements = yp_statements_node_create(parser);
yp_statements_node_body_append(if_statements, true_expression);

Expand Down Expand Up @@ -3996,6 +4026,7 @@ yp_undef_node_append(yp_undef_node_t *node, yp_node_t *name) {
// Allocate a new UnlessNode node.
static yp_unless_node_t *
yp_unless_node_create(yp_parser_t *parser, const yp_token_t *keyword, yp_node_t *predicate, yp_statements_node_t *statements) {
yp_flip_flop(predicate);
yp_unless_node_t *node = YP_ALLOC_NODE(parser, yp_unless_node_t);

const char *end;
Expand Down Expand Up @@ -4027,6 +4058,7 @@ yp_unless_node_create(yp_parser_t *parser, const yp_token_t *keyword, yp_node_t
// Allocate and initialize new UnlessNode node in the modifier form.
static yp_unless_node_t *
yp_unless_node_modifier_create(yp_parser_t *parser, yp_node_t *statement, const yp_token_t *unless_keyword, yp_node_t *predicate) {
yp_flip_flop(predicate);
yp_unless_node_t *node = YP_ALLOC_NODE(parser, yp_unless_node_t);

yp_statements_node_t *statements = yp_statements_node_create(parser);
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/seattlerb/flip2_env_lvar.txt

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

8 changes: 4 additions & 4 deletions test/snapshots/unparser/corpus/semantic/and.txt

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

2 changes: 1 addition & 1 deletion test/snapshots/whitequark/cond_eflipflop.txt

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

2 changes: 1 addition & 1 deletion test/snapshots/whitequark/cond_iflipflop.txt

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

0 comments on commit 6315890

Please sign in to comment.