Sha256: 78a2ff7b1487280464ba5252a8219fdc674e98eeb378aa14125a185651dc19f3

Contents?: true

Size: 1007 Bytes

Versions: 2

Compression:

Stored size: 1007 Bytes

Contents

require_relative '../function'

module Dentaku
  module AST
    class If < Function
      attr_reader :predicate, :left, :right

      def self.min_param_count
        3
      end

      def self.max_param_count
        3
      end

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

      def args
        [predicate, left, right]
      end

      def value(context = {})
        predicate.value(context) ? left.value(context) : right.value(context)
      end

      def node_type
        :condition
      end

      def type
        left.type
      end

      def dependencies(context = {})
        predicate.value(context) ? left.dependencies(context) : right.dependencies(context)
      rescue Dentaku::Error, Dentaku::ArgumentError, Dentaku::ZeroDivisionError
        args.flat_map { |arg| arg.dependencies(context) }.uniq
      end
    end
  end
end

Dentaku::AST::Function.register_class(:if, Dentaku::AST::If)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dentaku-3.5.4 lib/dentaku/ast/functions/if.rb
dentaku-3.5.3 lib/dentaku/ast/functions/if.rb