Sha256: d13880ee7cae3a255eb60559a17767ef7504b38dc1b6a586e3b1ba9031dc430f

Contents?: true

Size: 946 Bytes

Versions: 5

Compression:

Stored size: 946 Bytes

Contents

module Funkr

  # Should not be used directly
  module Matchers

    class SafeMatcher
      
      def initialize(match)
        @const, *@data = match
        @runner = nil
        @undefined = self.class.constructs.clone
      end
      
      def self.build_matchers(constructs)
        @constructs = constructs
        constructs.each do |c|
          name, *data = c
          define_method(name) do |&b|
            @undefined.delete(name)
            if @const == name then
              @runner = b
            end
          end
        end
      end
      
      def self.constructs; @constructs; end
      
      def self.match_with(normal_form) # &block
        m = self.new(normal_form)
        yield m
        m.run_match
      end

      def run_match
        if @undefined.any? then
          raise "Incomplete match, missing : #{@undefined.join(" ")}"
        end
        @runner.call(*@data)
      end
      
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
funkr-0.0.28 lib/funkr/adt/matcher.rb
funkr-0.0.27 lib/funkr/adt/matcher.rb
funkr-0.0.26 lib/funkr/adt/matcher.rb
funkr-0.0.25 lib/funkr/adt/matcher.rb
funkr-0.0.24 lib/funkr/adt/matcher.rb