Skip to content

Commit

Permalink
Make ** be right associative (crystal-lang#9684)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 5, 2020
1 parent 5be111a commit 719d314
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ module Crystal
it_parses "foo[] /2", Call.new(Call.new("foo".call, "[]"), "/", 2.int32)
it_parses "foo[1] /2", Call.new(Call.new("foo".call, "[]", 1.int32), "/", 2.int32)
it_parses "[1] /2", Call.new(([1.int32] of ASTNode).array, "/", 2.int32)
it_parses "2**3**4", Call.new(2.int32, "**", Call.new(3.int32, "**", 4.int32))

it_parses "!1", Not.new(1.int32)
it_parses "- 1", Call.new(1.int32, "-")
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ module Crystal
RangeLiteral.new(exp, right, exclusive).at(location).at_end(right)
end

macro parse_operator(name, next_operator, node, operators)
macro parse_operator(name, next_operator, node, operators, right_associative = false)
def parse_{{name.id}}
location = @token.location

Expand All @@ -521,7 +521,7 @@ module Crystal

slash_is_regex!
next_token_skip_space_or_newline
right = parse_{{next_operator.id}}
right = parse_{{(right_associative ? name : next_operator).id}}
left = ({{node.id}}).at(location).at_end(right)
left.name_location = name_location if left.is_a?(Call)
else
Expand Down Expand Up @@ -579,7 +579,7 @@ module Crystal
end

parse_operator :mul_or_div, :pow, "Call.new left, method, right", %(:"*", :"/", :"//", :"%", :"&*")
parse_operator :pow, :prefix, "Call.new left, method, right", %(:"**", :"&**")
parse_operator :pow, :prefix, "Call.new left, method, right", %(:"**", :"&**"), right_associative: true

def parse_prefix
name_location = @token.location
Expand Down

0 comments on commit 719d314

Please sign in to comment.