Sha256: b42d67c6ca8e3df812aa7ec353f7ab51a01b4f6733b302c47e4de7c4cc13d9b5

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Hotcell
  class Expression < Hotcell::Node
    HANDLERS = {
      PLUS: ->(value1, value2) { value1 + value2 },
      MINUS: ->(value1, value2) { value1 - value2 },
      UMINUS: ->(value) { -value },
      UPLUS: ->(value) { value },
      MULTIPLY: ->(value1, value2) { value1 * value2 },
      POWER: ->(value1, value2) { value1 ** value2 },
      DIVIDE: ->(value1, value2) { value1 / value2 },
      MODULO: ->(value1, value2) { value1 % value2 },
      AND: ->(value1, value2) { value1 && value2 },
      OR: ->(value1, value2) { value1 || value2 },
      NOT: ->(value) { !value },
      EQUAL: ->(value1, value2) { value1 == value2 },
      INEQUAL: ->(value1, value2) { value1 != value2 },
      GT: ->(value1, value2) { value1 > value2 },
      GTE: ->(value1, value2) { value1 >= value2 },
      LT: ->(value1, value2) { value1 < value2 },
      LTE: ->(value1, value2) { value1 <= value2 },
      TERNARY: ->(value1, value2, value3) { value1 ? value2 : value3 },
      RANGE: ->(value1, value2) { Range.new value1, value2 },
      ERANGE: ->(value1, value2) { Range.new value1, value2, true },
      DSTRING: ->(*values) { values.map(&:to_s).join }
    }

    def optimize
      handler = HANDLERS[name]
      reducible = handler && children.none? { |child| child.is_a? Hotcell::Node }
      reducible ? handler.call(*children) : super
    end

    def process context, *values
      handler = HANDLERS[name]
      raise "Could not find handler for `#{name}`" unless handler
      handler.call *values
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hotcell-0.3.0 lib/hotcell/node/expression.rb