Skip to content
Browse files

Booya. Simple pattern matching for Ruby.

  • Loading branch information...
0 parents commit 1219bcd8495dfb7b14c57d85bb6807fd5a75ba0a @josevalim committed Mar 26, 2011
Showing with 267 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +1 −0 .watchr
  3. +4 −0 Gemfile
  4. +13 −0 Rakefile
  5. +21 −0 defm.gemspec
  6. +107 −0 lib/defm.rb
  7. +112 −0 test/defm_test.rb
  8. +5 −0 test/test_helper.rb
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
1 .watchr
@@ -0,0 +1 @@
+watch("(lib|test)/.*") { system("rake test") }
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in defm.gemspec
+gemspec
13 Rakefile
@@ -0,0 +1,13 @@
+require 'bundler'
+require 'rake/testtask'
+
+Bundler::GemHelper.install_tasks
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+task :default => :test
21 defm.gemspec
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "defm"
+
+Gem::Specification.new do |s|
+ s.name = "defm"
+ s.version = Defm::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["TODO: Write your name"]
+ s.email = ["TODO: Write your email address"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "defm"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
107 lib/defm.rb
@@ -0,0 +1,107 @@
+module Defm
+ VERSION = "0.0.1"
+
+ RespondTo = Struct.new(:method)
+
+ class Chain
+ def initialize
+ @array = []
+ end
+
+ def <<(item)
+ @array << item
+ end
+
+ def size
+ @array.size
+ end
+
+ def compile(method)
+ when_clauses = @array.group_by(&:arity).map do |key, value|
+ " when #{key}\n#{compile_methods(value)}"
+ end.join("\n")
+
+ <<-RUBY
+def #{method}(*args)
+ case args.size
+#{when_clauses}
+ end
+ method_missing(:#{method}, *args)
+end
+ RUBY
+ end
+
+ private
+
+ def compile_methods(methods)
+ first = methods.shift
+ string = "if #{first.condition}\n#{first.dispatch}\n"
+ methods.each { |m| string << "elsif #{m.condition}\n#{m.dispatch}\n" }
+ string << "end"
+ end
+ end
+
+ class Method
+ def initialize(compiled_method, args)
+ @compiled_method, @args = compiled_method, args
+ end
+
+ def arity
+ @args.size
+ end
+
+ def condition
+ condition_for_args("args", @args, 0)
+ end
+
+ def dispatch
+ "return #{@compiled_method}(*args)"
+ end
+
+ private
+
+ def inspect(arg)
+ arg.is_a?(Class) ? arg.name : arg.inspect
+ end
+
+ def condition_for_args(name, args, counter)
+ return "true" if args.empty?
+ condition = []
+ args.each_with_index do |arg, i|
+ condition << case arg
+ when Array
+ new_name, op = "argv#{counter}", :==
+ arg, op = arg[0..-2], :>= if arg.last == Tail
+ "((#{new_name} = #{name}[#{i}]) && (#{new_name}.size #{op} #{arg.size}) && (#{condition_for_args(new_name, arg, counter + 1)}))"
+ when RespondTo
+ "#{name}[#{i}].respond_to?(:#{arg.method}, true)"
+ else
+ "(#{inspect(arg)} === #{name}[#{i}])"
+ end
+ end
+ condition.join(" && ")
+ end
+ end
+end
+
+class Class
+ def defm(method, *args, &block)
+ existent = multi_methods[method]
+ compiled_method = "__defm_#{method}_#{existent.size}"
+ define_method compiled_method, &block
+ existent << Defm::Method.new(compiled_method, args)
+ class_eval existent.compile(method)
+ end
+
+ def multi_methods
+ @multi_methods ||= Hash.new { |h,k| h[k] = Defm::Chain.new }
+ end
+end
+
+class Symbol
+ def ~
+ Defm::RespondTo.new(self)
+ end
+end
+
+Tail = Object.new
112 test/defm_test.rb
@@ -0,0 +1,112 @@
+require 'test_helper'
+
+class DefmTest < Test::Unit::TestCase
+ class One
+ defm :simple do
+ 1
+ end
+
+ defm :integer do
+ true
+ end
+
+ defm :integer, 0 do
+ 0
+ end
+
+ defm :integer, 1 do
+ 1
+ end
+
+ defm :integer, Integer do |x|
+ x * 2
+ end
+
+ defm :string, "a" do |string|
+ string.upcase
+ end
+
+ defm :string, /ab/ do |string|
+ string * 2
+ end
+
+ defm :string, "abab" do |string|
+ raise "I should never be called"
+ end
+
+ defm :string, String do |string|
+ string.downcase
+ end
+
+ defm :array, 1, [0] do |x, (y,)|
+ x - y
+ end
+
+ defm :array, Integer, [2, Integer] do |x, (y, z)|
+ x + y * z
+ end
+
+ defm :array, Integer, [3, Integer, Tail] do |x, (y, z, *w)|
+ x * y * z * w.size
+ end
+
+ defm :duck_typing, ~:quack do |x|
+ x.quack
+ end
+ end
+
+ class Duck
+ def quack
+ "quack"
+ end
+ end
+
+ class Dog
+ end
+
+ def test_defines_a_method
+ assert_equal 1, One.new.simple
+ end
+
+ def test_defines_pattern_matching_with_integer
+ assert_equal true, One.new.integer
+ assert_equal 0, One.new.integer(0)
+ assert_equal 1, One.new.integer(1)
+ assert_equal 4, One.new.integer(2)
+ assert_equal 6, One.new.integer(3)
+ assert_raise NoMethodError do
+ One.new.integer(0, 1)
+ end
+ end
+
+ def test_defines_pattern_matching_with_string
+ assert_equal "A", One.new.string("a")
+ assert_equal "abab", One.new.string("ab")
+ assert_equal "abababab", One.new.string("abab")
+ assert_equal "ad", One.new.string("ad")
+ assert_equal "ad", One.new.string("AD")
+ end
+
+ def test_defines_pattern_matching_with_array
+ assert_equal 1, One.new.array(1, [0])
+ assert_equal 6, One.new.array(2, [2,2])
+ assert_equal 2, One.new.array(2, [2,0])
+
+ assert_raise NoMethodError do
+ One.new.array(2, [2,0,3])
+ end
+
+ assert_equal 0, One.new.array(2, [3,2])
+ assert_equal 12, One.new.array(2, [3,2,5])
+ assert_equal 24, One.new.array(2, [3,2,5,6])
+ assert_equal 36, One.new.array(2, [3,2,5,6,7])
+ end
+
+ def test_defines_pattern_matching_with_duck_typing
+ assert_equal "quack", One.new.duck_typing(Duck.new)
+
+ assert_raise NoMethodError do
+ One.new.duck_typing(Dog.new)
+ end
+ end
+end
5 test/test_helper.rb
@@ -0,0 +1,5 @@
+require 'rubygems'
+require 'bundler'
+require 'test/unit'
+
+Bundler.setup(:default, :test)

0 comments on commit 1219bcd

Please sign in to comment.
Something went wrong with that request. Please try again.