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

Add comment for LiteralExpander select #12926

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/compiler/crystal/semantic/literal_expander.cr
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,48 @@ module Crystal
final_exp
end

# Convert a `select` statement into a `case` statement based on `Channel.select`
#
# From:
#
# select
# when foo then body
# when x = bar then x.baz
# end
#
# To:
#
# %index, %value = ::Channel.select({foo_select_action, bar_select_action})
# case %index
# when 0
# body
# when 1
# x = value.as(typeof(foo))
# x.baz
# else
# ::raise("BUG: invalid select index")
# end
#
#
# If there's an `else` branch, use `Channel.non_blocking_select`.
#
# From:
#
# select
# when foo then body
# else qux
# end
#
# To:
#
# %index, %value = ::Channel.non_blocking_select({foo_select_action})
# case %index
# when 0
# body
# else
# qux
# end
#
def expand(node : Select)
index_name = @program.new_temp_var_name
value_name = @program.new_temp_var_name
Expand Down