Sha256: d16d7066482da5d8fdc293fea5e17669c19a4a9b4edd285c637b821adcad03af

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

class Predicate
  class ToS < Sexpr::Processor

    def on_tautology(sexpr)
      "true"
    end

    def on_contradiction(sexpr)
      "false"
    end

    def on_qualified_identifier(sexpr)
      "#{sexpr.qualifier}.#{sexpr.name}"
    end

    def on_identifier(sexpr)
      if s = options[:scope]
        "#{s}.#{sexpr.last.to_s}"
      else
        "@#{sexpr.last.to_s}"
      end
    end

    def on_not(sexpr)
      "not(" << apply(sexpr.last, sexpr) << ")"
    end

    def on_and(sexpr)
      sexpr.sexpr_body.map{|term|
        apply(term, sexpr)
      }.join(" AND ")
    end

    def on_or(sexpr)
      sexpr.sexpr_body.map{|term|
        apply(term, sexpr)
      }.join(" OR ")
    end

    def on_dyadic(sexpr)
      sexpr.sexpr_body.map{|term|
        apply(term, sexpr)
      }.join(" #{sexpr.operator_symbol} ")
    end
    alias :on_eq  :on_dyadic
    alias :on_neq :on_dyadic
    alias :on_lt  :on_dyadic
    alias :on_lte :on_dyadic
    alias :on_gt  :on_dyadic
    alias :on_gte :on_dyadic

    def on_in(sexpr)
      "#{apply(sexpr.identifier)} IN #{apply(sexpr.right)}"
    end

    def on_intersect(sexpr)
      "#{apply(sexpr.identifier)} INTERSECTS #{to_literal(sexpr.values)}"
    end

    def on_literal(sexpr)
      to_literal(sexpr.last)
    end

    def on_opaque(sexpr)
      "OPAQUE #{sexpr.last}"
    end

    def on_match(sexpr)
      "#{apply(sexpr.left)} =~ #{apply(sexpr.right)}"
    end

    def on_native(sexpr)
      sexpr.last.inspect
    end

    def on_missing(sexpr)
      raise "Unimplemented: #{sexpr.first}"
    end

  protected

    def to_literal(x)
      case x
      when Placeholder then "$#{x.object_id}"
      when Array then "{" << x.map{|y| to_literal(y) }.join(',') << "}"
      else x.inspect
      end
    end

    def apply(sexpr, parent = nil)
      code = super(sexpr)
      if parent && (parent.priority >= sexpr.priority)
        code = "(" << code << ")"
      end
      code
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
predicate-2.4.0 lib/predicate/processors/to_s.rb
predicate-2.3.3 lib/predicate/processors/to_s.rb
predicate-2.3.2 lib/predicate/processors/to_s.rb
predicate-2.3.1 lib/predicate/processors/to_s.rb
predicate-2.3.0 lib/predicate/processors/to_s.rb