Sha256: 30b9a4694e76cbc64ae4538049ce924d4c0f5ff777039d3e223c3dd8513b2dc7

Contents?: true

Size: 1.12 KB

Versions: 11

Compression:

Stored size: 1.12 KB

Contents

require "funkr/adt/matcher"

module Funkr
  class ADT

    def initialize(const, *data)
      @const, @data = const, data
    end
    
    def self.adt(*constructs)
      build_adt(constructs)
      build_matcher(constructs)
    end

    def self.match_method=(method)
      @match_method = method
    end

    self.match_method = :safe
    
    def self.matcher; @matcher; end
    
    def match
      m = self.class.matcher.new(normal_form)
      yield m
      m.run_match
    end
    
    def to_s
      format("%s%s%s",
             @const,
             @data.any? ? " : " : "",
             @data.map(&:inspect).join(" ") )
    end
    
    private

    attr_reader :const, :data
    
    def normal_form; [@const, *@data]; end
    
    def self.build_adt(constructs)
      constructs.each do |c,*d|
        define_singleton_method(c) do |*data|
          self.new(c,*data)
        end
        define_method(format("%s?",c).to_sym) do
          const() == c
        end
      end
    end
    
    def self.build_matcher(constructs)
      @matcher = Class.new(Funkr::Matcher) do
        build_matchers(constructs)
      end
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
funkr-0.0.21 lib/funkr/adt/adt.rb
funkr-0.0.20 lib/funkr/adt/adt.rb
funkr-0.0.19 lib/funkr/adt/adt.rb
funkr-0.0.18 lib/funkr/adt/adt.rb
funkr-0.0.17 lib/funkr/adt/adt.rb
funkr-0.0.16 lib/funkr/adt/adt.rb
funkr-0.0.15 lib/funkr/adt/adt.rb
funkr-0.0.14 lib/funkr/adt/adt.rb
funkr-0.0.12 lib/funkr/adt/adt.rb
funkr-0.0.11 lib/funkr/adt/adt.rb
funkr-0.0.10 lib/funkr/adt/adt.rb