Class: Hotdog::Commands::Search::BinaryExpressionNode

Inherits:
ExpressionNode
  • Object
show all
Defined in:
lib/hotdog/commands/search.rb

Instance Method Summary collapse

Constructor Details

#initialize(op, left, right) ⇒ BinaryExpressionNode

Returns a new instance of BinaryExpressionNode.



156
157
158
159
160
# File 'lib/hotdog/commands/search.rb', line 156

def initialize(op, left, right)
  @op = op
  @left = left
  @right = right
end

Instance Method Details

#evaluate(environment) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/hotdog/commands/search.rb', line 161

def evaluate(environment)
  case @op
  when "&&", "&", /\Aand\z/i
    left_values = @left.evaluate(environment)
    if left_values.empty?
      []
    else
      (left_values & @right.evaluate(environment)).uniq
    end
  when "||", "|", /\Aor\z/i
    left_values = @left.evaluate(environment)
    (left_values | @right.evaluate(environment)).uniq
  else
    raise(SyntaxError.new("unknown binary operator: #{@op}"))
  end
end