Class: Y2R::AST::YCP::Case

Inherits:
Node
  • Object
show all
Defined in:
lib/y2r/ast/ycp.rb

Instance Method Summary collapse

Methods inherited from Node

#compile_as_copy_if_needed, #compile_statements, #compile_statements_inside_block, #compile_statements_with_whitespace, #creates_local_scope?, #needs_copy?, #never_nil?, #optimize_last_statement, #optimize_next, #optimize_return, #remove_duplicate_imports, transfers_comments

Instance Method Details

#always_returns?Boolean

Returns:

  • (Boolean)


1042
1043
1044
# File 'lib/y2r/ast/ycp.rb', line 1042

def always_returns?
  body.always_returns?
end

#compile(context) ⇒ Object



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
# File 'lib/y2r/ast/ycp.rb', line 1020

def compile(context)
  if body.statements.last.is_a?(Break)
    # The following dance is here because we want ot keep the AST nodes
    # immutable and thus avoid modifying their data.

    body_without_break = body.dup
    body_without_break.statements = body.statements[0..-2]
  elsif body.always_returns?
    body_without_break = body
  else
    raise NotImplementedError,
          "Case without a break or return encountered. These are not supported."
  end

  context.inside self do |inner_context|
    Ruby::When.new(
      :values => values.map { |v| v.compile(inner_context) },
      :body   => body_without_break.compile(inner_context)
    )
  end
end

#symbolsObject



1016
1017
1018
# File 'lib/y2r/ast/ycp.rb', line 1016

def symbols
  []
end