Skip to content

Commit 6be28ee

Browse files
committed
Fix up unary minus precedence
1 parent c75047a commit 6be28ee

File tree

3 files changed

+112
-63
lines changed

3 files changed

+112
-63
lines changed

src/yarp.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7940,7 +7940,7 @@ yp_binding_powers_t yp_binding_powers[YP_TOKEN_MAXIMUM] = {
79407940

79417941
// -@
79427942
[YP_TOKEN_UMINUS] = RIGHT_ASSOCIATIVE_UNARY(YP_BINDING_POWER_UMINUS),
7943-
[YP_TOKEN_UMINUS_NUM] = RIGHT_ASSOCIATIVE_UNARY(YP_BINDING_POWER_UMINUS),
7943+
[YP_TOKEN_UMINUS_NUM] = { YP_BINDING_POWER_UMINUS, YP_BINDING_POWER_MAX, false },
79447944

79457945
// **
79467946
[YP_TOKEN_STAR_STAR] = RIGHT_ASSOCIATIVE(YP_BINDING_POWER_EXPONENT),
@@ -12790,16 +12790,23 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
1279012790
yp_token_t operator = parser->previous;
1279112791
yp_node_t *node = parse_expression(parser, yp_binding_powers[parser->previous.type].right, YP_ERR_UNARY_RECEIVER_MINUS);
1279212792

12793-
switch (YP_NODE_TYPE(node)) {
12794-
case YP_INTEGER_NODE:
12795-
case YP_FLOAT_NODE:
12796-
case YP_RATIONAL_NODE:
12797-
case YP_IMAGINARY_NODE:
12798-
parse_negative_numeric(node);
12799-
break;
12800-
default:
12801-
node = (yp_node_t *) yp_call_node_unary_create(parser, &operator, node, "-@");
12802-
break;
12793+
if (accept(parser, YP_TOKEN_STAR_STAR)) {
12794+
yp_token_t exponent_operator = parser->previous;
12795+
yp_node_t *exponent = parse_expression(parser, yp_binding_powers[exponent_operator.type].right, YP_ERR_EXPECT_ARGUMENT);
12796+
node = (yp_node_t *) yp_call_node_binary_create(parser, node, &exponent_operator, exponent);
12797+
node = (yp_node_t *) yp_call_node_unary_create(parser, &operator, node, "-@");
12798+
} else {
12799+
switch (YP_NODE_TYPE(node)) {
12800+
case YP_INTEGER_NODE:
12801+
case YP_FLOAT_NODE:
12802+
case YP_RATIONAL_NODE:
12803+
case YP_IMAGINARY_NODE:
12804+
parse_negative_numeric(node);
12805+
break;
12806+
default:
12807+
node = (yp_node_t *) yp_call_node_unary_create(parser, &operator, node, "-@");
12808+
break;
12809+
}
1280312810
}
1280412811

1280512812
return node;

test/yarp/fixtures/arithmetic.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ foo !bar
77
foo ~bar
88

99
foo << bar << baz
10+
11+
-1**2
12+
13+
-1.zero?

test/yarp/snapshots/arithmetic.txt

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

0 commit comments

Comments
 (0)