2022-06-09 12:14:08 +02:00
|
|
|
class Logic::BinaryOperator < Logic::Term
|
|
|
|
attr_reader :left, :right
|
|
|
|
|
2022-07-06 09:44:54 +02:00
|
|
|
def initialize(left, right)
|
2022-06-09 12:14:08 +02:00
|
|
|
@left, @right = left, right
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_h
|
|
|
|
{
|
2022-07-05 14:47:32 +02:00
|
|
|
"term" => self.class.name,
|
2022-06-09 12:14:08 +02:00
|
|
|
"left" => @left.to_h,
|
2022-07-06 09:44:54 +02:00
|
|
|
"right" => @right.to_h
|
2022-06-09 12:14:08 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_h(h)
|
2022-07-06 09:44:54 +02:00
|
|
|
self.new(Logic.from_h(h['left']), Logic.from_h(h['right']))
|
2022-06-09 12:14:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def errors(stable_ids = [])
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
if @left.type != :number || @right.type != :number
|
|
|
|
errors += ["les types sont incompatibles : #{self}"]
|
|
|
|
end
|
|
|
|
|
|
|
|
errors + @left.errors(stable_ids) + @right.errors(stable_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def type = :boolean
|
|
|
|
|
|
|
|
def compute(champs = [])
|
|
|
|
l = @left.compute(champs)
|
|
|
|
r = @right.compute(champs)
|
|
|
|
|
2022-07-18 16:19:28 +02:00
|
|
|
l&.send(operation, r) || false
|
2022-06-09 12:14:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s = "(#{@left} #{operation} #{@right})"
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
self.class == other.class &&
|
|
|
|
@left == other.left &&
|
|
|
|
@right == other.right
|
|
|
|
end
|
|
|
|
end
|