Skip to content

Commit

Permalink
Fix up unary minus precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Sep 14, 2023
1 parent c75047a commit 6be28ee
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 63 deletions.
29 changes: 18 additions & 11 deletions src/yarp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7940,7 +7940,7 @@ yp_binding_powers_t yp_binding_powers[YP_TOKEN_MAXIMUM] = {

// -@
[YP_TOKEN_UMINUS] = RIGHT_ASSOCIATIVE_UNARY(YP_BINDING_POWER_UMINUS),
[YP_TOKEN_UMINUS_NUM] = RIGHT_ASSOCIATIVE_UNARY(YP_BINDING_POWER_UMINUS),
[YP_TOKEN_UMINUS_NUM] = { YP_BINDING_POWER_UMINUS, YP_BINDING_POWER_MAX, false },

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

switch (YP_NODE_TYPE(node)) {
case YP_INTEGER_NODE:
case YP_FLOAT_NODE:
case YP_RATIONAL_NODE:
case YP_IMAGINARY_NODE:
parse_negative_numeric(node);
break;
default:
node = (yp_node_t *) yp_call_node_unary_create(parser, &operator, node, "-@");
break;
if (accept(parser, YP_TOKEN_STAR_STAR)) {
yp_token_t exponent_operator = parser->previous;
yp_node_t *exponent = parse_expression(parser, yp_binding_powers[exponent_operator.type].right, YP_ERR_EXPECT_ARGUMENT);
node = (yp_node_t *) yp_call_node_binary_create(parser, node, &exponent_operator, exponent);
node = (yp_node_t *) yp_call_node_unary_create(parser, &operator, node, "-@");
} else {
switch (YP_NODE_TYPE(node)) {
case YP_INTEGER_NODE:
case YP_FLOAT_NODE:
case YP_RATIONAL_NODE:
case YP_IMAGINARY_NODE:
parse_negative_numeric(node);
break;
default:
node = (yp_node_t *) yp_call_node_unary_create(parser, &operator, node, "-@");
break;
}
}

return node;
Expand Down
4 changes: 4 additions & 0 deletions test/yarp/fixtures/arithmetic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ foo !bar
foo ~bar

foo << bar << baz

-1**2

-1.zero?
142 changes: 90 additions & 52 deletions test/yarp/snapshots/arithmetic.txt

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

0 comments on commit 6be28ee

Please sign in to comment.