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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, left, right) ⇒ BinaryExpressionNode



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/hotdog/commands/search.rb', line 239

def initialize(op, left, right)
  case op || "or"
  when "&&", "&", /\Aand\z/i
    @op = :AND
  when "||", "|", /\Aor\z/i
    @op = :OR
  else
    raise(SyntaxError.new("unknown binary operator: #{op.inspect}"))
  end
  @left = left
  @right = right
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



237
238
239
# File 'lib/hotdog/commands/search.rb', line 237

def left
  @left
end

#opObject (readonly)

Returns the value of attribute op.



237
238
239
# File 'lib/hotdog/commands/search.rb', line 237

def op
  @op
end

#rightObject (readonly)

Returns the value of attribute right.



237
238
239
# File 'lib/hotdog/commands/search.rb', line 237

def right
  @right
end

Instance Method Details

#==(other) ⇒ Object



289
290
291
# File 'lib/hotdog/commands/search.rb', line 289

def ==(other)
  self.class === other and @op == other.op and @left == other.left and @right == other.right
end

#evaluate(environment, options = {}) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/hotdog/commands/search.rb', line 252

def evaluate(environment, options={})
  case @op
  when :AND
    left_values = @left.evaluate(environment)
    environment.logger.debug("lhs: #{left_values.length} value(s)")
    if left_values.empty?
      []
    else
      right_values = @right.evaluate(environment)
      environment.logger.debug("rhs: #{right_values.length} value(s)")
      (left_values & right_values).tap do |values|
        environment.logger.debug("lhs AND rhs: #{values.length} value(s)")
      end
    end
  when :OR
    left_values = @left.evaluate(environment)
    environment.logger.debug("lhs: #{left_values.length} value(s)")
    right_values = @right.evaluate(environment)
    environment.logger.debug("rhs: #{right_values.length} value(s)")
    (left_values | right_values).uniq.tap do |values|
      environment.logger.debug("lhs OR rhs: #{values.length} value(s)")
    end
  else
    []
  end
end

#optimize(options = {}) ⇒ Object



279
280
281
282
283
284
285
286
287
# File 'lib/hotdog/commands/search.rb', line 279

def optimize(options={})
  @left = @left.optimize(options)
  @right = @right.optimize(options)
  if @left == @right
    @left
  else
    self
  end
end