Sha256: 8696f697ee5687091b17d1e759c7d103e6b392309d8267a3ab52218604926b20

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

module Pione::Model

  # BinaryOperator represents applications between Pione model objects.
  # @example
  #   1 + 1
  # @example
  #   true == false
  # @example
  #   $X * $Y
  class BinaryOperator < BasicModel
    attr_reader :symbol
    attr_reader :left
    attr_reader :right

    # Create a binary operator.
    # @param [String] symbol
    #   operator's symbol
    # @param [BasicModel] left
    #   left operand
    # @param [BasicModel] right
    #   right operand
    def initialize(symbol, left, right)
      @symbol = symbol
      @left = left
      @right = right
      super()
    end

    # Returns false because Message has complex form.
    # @return [Boolean]
    #   false
    def atomic?
      false
    end

    # Returns true if the left or right expression includes variable.
    # @return [Boolean]
    #    true if the left or right expression includes variable
    def include_variable?
      @left.include_variable? or @right.include_variable?
    end

    # @api private
    def pione_model_type
      @left.pione_model_type.method_interface[@symbol].output
    end

    # Evaluates the application expression and returns application result.
    # @param [VariableTable] vtable
    #   variable table for the evaluation
    # @return [BasicModel]
    #   evaluation result object
    def eval(vtable)
      left = @left.eval(vtable)
      right = @right.eval(vtable)
      left.call_pione_method(@symbol, right)
    end

    # @api private
    def textize
      "%s%s%s" % [@left.textize, @symbol.to_s, @right.textize]
    end

    # @api private
    def ==(other)
      return false unless other.kind_of?(self.class)
      return false unless @symbol == other.symbol
      return false unless @left == other.left
      return false unless @right == other.right
      return true
    end

    alias :eql? :"=="

    # @api private
    def hash
      @symbol.hash + @left.hash + @right.hash
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pione-0.1.2 lib/pione/model/binary-operator.rb
pione-0.1.1 lib/pione/model/binary-operator.rb
pione-0.1.0 lib/pione/model/binary-operator.rb