Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Crystal::Select::When with Crystal::When #14497

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1451,9 +1451,9 @@ module Crystal

it_parses "case 1; when 2 then /foo/; end", Case.new(1.int32, [When.new([2.int32] of ASTNode, RegexLiteral.new("foo".string))], else: nil, exhaustive: false)

it_parses "select\nwhen foo\n2\nend", Select.new([Select::When.new("foo".call, 2.int32)])
it_parses "select\nwhen foo\n2\nwhen bar\n4\nend", Select.new([Select::When.new("foo".call, 2.int32), Select::When.new("bar".call, 4.int32)])
it_parses "select\nwhen foo\n2\nelse\n3\nend", Select.new([Select::When.new("foo".call, 2.int32)], 3.int32)
it_parses "select\nwhen foo\n2\nend", Select.new([When.new("foo".call, 2.int32)])
it_parses "select\nwhen foo\n2\nwhen bar\n4\nend", Select.new([When.new("foo".call, 2.int32), When.new("bar".call, 4.int32)])
it_parses "select\nwhen foo\n2\nelse\n3\nend", Select.new([When.new("foo".call, 2.int32)], 3.int32)

assert_syntax_error "select\nwhen 1\n2\nend", "invalid select when expression: must be an assignment or call"

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/literal_expander.cr
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ module Crystal
case_whens = [] of When

node.whens.each_with_index do |a_when, index|
condition = a_when.condition
condition = a_when.conds.first
case condition
when Call
cloned_call = condition.clone
Expand Down
11 changes: 5 additions & 6 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,10 @@ module Crystal
@body = Expressions.from body
end

def self.new(cond : ASTNode, body : ASTNode? = nil, exhaustive = false)
new([cond] of ASTNode, body, exhaustive)
end

def accept_children(visitor)
@conds.each &.accept visitor
@body.accept visitor
Expand Down Expand Up @@ -1360,19 +1364,14 @@ module Crystal
end

class Select < ASTNode
record When, condition : ASTNode, body : ASTNode

property whens : Array(When)
property else : ASTNode?

def initialize(@whens, @else = nil)
end

def accept_children(visitor)
@whens.each do |select_when|
select_when.condition.accept visitor
select_when.body.accept visitor
end
@whens.each &.accept visitor
@else.try &.accept visitor
end

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2955,7 +2955,7 @@ module Crystal
next_token_skip_space
skip_statement_end

whens = [] of Select::When
whens = [] of When

while true
case @token.value
Expand All @@ -2978,7 +2978,7 @@ module Crystal
body = parse_expressions
skip_space_or_newline

whens << Select::When.new(condition, body)
whens << When.new(condition, body)
when Keyword::ELSE
if whens.size == 0
unexpected_token "expecting when"
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/syntax/to_s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ module Crystal
node.whens.each do |a_when|
append_indent
@str << "when "
a_when.condition.accept self
a_when.conds.first.accept self
newline
accept_with_indent a_when.body
end
Expand Down
4 changes: 1 addition & 3 deletions src/compiler/crystal/syntax/transformer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ module Crystal
end

def transform(node : Select)
node.whens.map! do |a_when|
Select::When.new(a_when.condition.transform(self), a_when.body.transform(self))
end
transform_many node.whens

if node_else = node.else
node.else = node_else.transform(self)
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3879,7 +3879,7 @@ module Crystal
write_keyword :when
skip_space_or_newline(@indent + 2)
write " "
a_when.condition.accept self
a_when.conds.first.accept self
found_comment = skip_space(@indent + 2)
if @token.type.op_semicolon? || @token.keyword?(:then)
sep = @token.type.op_semicolon? ? "; " : " then "
Expand Down