Class: Y2R::AST::YCP::YEBinary

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

Constant Summary collapse

OPS_TO_OPS =
{
  "&&" => "&&",
  "||" => "||"
}
OPS_TO_OPS_OPTIONAL =
{
  "+"  => "+",
  "-"  => "-",
  "*"  => "*",
  "/"  => "/",
  "%"  => "%",
  "&"  => "&",
  "|"  => "|",
  "^"  => "^",
  "<<" => "<<",
  ">>" => ">>",
}
OPS_TO_METHODS =
{
  "+"  => "add",
  "-"  => "subtract",
  "*"  => "multiply",
  "/"  => "divide",
  "%"  => "modulo",
  "&"  => "bitwise_and",
  "|"  => "bitwise_or",
  "^"  => "bitwise_xor",
  "<<" => "shift_left",
  ">>" => "shift_right"
}

Instance Method Summary collapse

Methods inherited from Node

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

Instance Method Details

#compile(context) ⇒ Object



2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
# File 'lib/y2r/ast/ycp.rb', line 2104

def compile(context)
  if OPS_TO_OPS[name]
    Ruby::BinaryOperator.new(
      :op  => OPS_TO_OPS[name],
      :lhs => lhs.compile(context),
      :rhs => rhs.compile(context)
    )
  elsif OPS_TO_METHODS[name]
    if never_nil?
      Ruby::BinaryOperator.new(
        :op  => OPS_TO_OPS_OPTIONAL[name],
        :lhs => lhs.compile(context),
        :rhs => rhs.compile(context)
      )
    else
      Ruby::MethodCall.new(
        :receiver => Ruby::Variable.new(:name => "Ops"),
        :name     => OPS_TO_METHODS[name],
        :args     => [lhs.compile(context), rhs.compile(context)],
        :block    => nil,
        :parens   => true
      )
    end
  else
    raise "Unknown binary operator: #{name.inspect}."
  end
end

#never_nil?Boolean

Returns:

  • (Boolean)


2134
2135
2136
# File 'lib/y2r/ast/ycp.rb', line 2134

def never_nil?
  return lhs.never_nil? && rhs.never_nil?
end