rubype 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d3acd50cc84b3a88ae2270111796825a1d7b916
4
- data.tar.gz: 53745d33a11a70db20b39199df8cd54115cf3e6e
3
+ metadata.gz: 6c7e1779556b1e9829b3b5043bc96aba5caaacb1
4
+ data.tar.gz: b2c48bd090e006e27a176803c0833d3a39883226
5
5
  SHA512:
6
- metadata.gz: 28374048f7e03c8611d69d1cc6ee842ed93e0cda775968f0e9f4d1e7da31f29a6694fd136445d8c93864403fa0ed97001bc08cdbeb14d85447326c81070829e8
7
- data.tar.gz: b0faae01aa81746ca428efd63993969b6f4c4d4f8437b5a4fd76051b7bd7ecf48f1cd78bed70bb2470a140c57dcbe8dd161c0b27bc41bc023a282c799f4ccc51
6
+ metadata.gz: 165eeb9c15017ab93dfefcf66236ff8096274bbd65e87fa082e0ae0fd62c10712c6636085fdd46dc96d06a59b1ddf318f9e97dcacd7df7bb78052441459bbcd6
7
+ data.tar.gz: 5983be0188e80c963ce8994949ff461abc99ea9217e8cde4265c088b2c4f565b58ee4146762e29a2ed76ac8b098436b3203c0b7caef1f7b4fea2be74ad45f2b9
data/README.md CHANGED
@@ -12,12 +12,12 @@ class MyClass
12
12
  def sum(x, y)
13
13
  x + y
14
14
  end
15
- type Numeric, Numeric >= Numeric, :sum
15
+ typesig sum: [Numeric, Numeric => Numeric]
16
16
 
17
17
  def wrong_sum(x, y)
18
18
  'string'
19
19
  end
20
- type Numeric, Numeric >= Numeric, :sum
20
+ typesig wrong_sum: [Numeric, Numeric => Numeric]
21
21
  end
22
22
 
23
23
  MyClass.new.sum(1, 2)
@@ -29,23 +29,14 @@ MyClass.new.sum(1, 'string')
29
29
  MyClass.new.wrong_sum(1, 2)
30
30
  #=> TypeError: Expected wrong_sum to return Numeric but got "str" instead
31
31
 
32
- # ex2: (Ruby 2.1.0+)
33
- class MyClass
34
- type Numeric, Numeric >= Numeric, def sum(x, y)
35
- x + y
36
- end
37
-
38
- type Numeric, Numeric >= Numeric, def wrong_sum(x, y)
39
- 'string'
40
- end
41
- end
42
32
 
43
- # ex3: (Ruby 2.1.0+)
33
+ # ex2
44
34
  class People
45
35
  type People >= Any, def marry(people)
46
36
  # Your Ruby code as usual
47
37
  end
48
38
  end
39
+ typesig marry: [People => Any]
49
40
 
50
41
  People.new.marry(People.new)
51
42
  #=> no error
@@ -63,9 +54,9 @@ class MyClass
63
54
  def sum(x, y)
64
55
  x + y
65
56
  end
66
- type Numeric, Numeric >= Numeric, :sum
57
+ typesig sum: [Numeric, Numeric => Numeric]
67
58
 
68
- def wrong_sum(x, y)
59
+ def sum_without_type(x, y)
69
60
  'string'
70
61
  end
71
62
  end
@@ -79,7 +70,7 @@ class MyClass
79
70
  def foo(any_obj)
80
71
  1
81
72
  end
82
- type Any >= Numeric, :foo
73
+ typesig sum: [Any => Numeric]
83
74
  end
84
75
 
85
76
  # It's totally OK!!
data/Rakefile CHANGED
@@ -5,10 +5,15 @@ Rake::TestTask.new(:test) do |t|
5
5
  t.libs << "test"
6
6
  end
7
7
 
8
- task :default => :test
9
-
10
8
  require "rake/extensiontask"
11
9
 
12
10
  Rake::ExtensionTask.new("rubype") do |ext|
13
11
  ext.lib_dir = "lib/rubype"
14
12
  end
13
+
14
+ task :compile_and_test do
15
+ Rake::Task['compile'].invoke
16
+ Rake::Task['test'].invoke
17
+ end
18
+
19
+ task default: :compile_and_test
data/ext/rubype/rubype.c CHANGED
@@ -1,9 +1,38 @@
1
1
  #include "rubype.h"
2
2
 
3
- VALUE rb_mRubype;
3
+ VALUE rb_mRubype, rb_cAny, rb_mBoolean, rb_cTypePair;
4
+
5
+ #define STR2SYM(x) ID2SYM(rb_intern(x))
6
+
7
+ static VALUE
8
+ rb_mod_prepend(int argc, VALUE *argv, VALUE module)
9
+ {
10
+ int i;
11
+ ID id_prepend_features, id_prepended;
12
+
13
+ CONST_ID(id_prepend_features, "prepend_features");
14
+ CONST_ID(id_prepended, "prepended");
15
+ for (i = 0; i < argc; i++)
16
+ Check_Type(argv[i], T_MODULE);
17
+ while (argc--) {
18
+ rb_funcall(argv[argc], id_prepend_features, 1, module);
19
+ rb_funcall(argv[argc], id_prepended, 1, module);
20
+ }
21
+
22
+ return module;
23
+ }
4
24
 
5
25
  void
6
26
  Init_rubype(void)
7
27
  {
8
- rb_mRubype = rb_define_module("Rubype");
28
+ // rb_mRubype = rb_define_module("Rubype");
29
+ // rb_cAny = rb_define_class("Any", rb_cObject);
30
+ // rb_mBoolean = rb_define_module("Boolean");
31
+ // rb_include_module(rb_cTrueClass, rb_mBoolean);
32
+ // rb_include_module(rb_cFalseClass, rb_mBoolean);
33
+ // rb_define_class(
34
+ // "TypePair",
35
+ // rb_funcall(rb_cStruct, rb_intern("new"), 2, STR2SYM("last_arg_type"), STR2SYM("rtn_type"))
36
+ // );
37
+ rb_define_method(rb_cModule, "prepend", rb_mod_prepend, -1);
9
38
  }
@@ -1,3 +1,3 @@
1
1
  module Rubype
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/rubype.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'rubype/type_pair'
1
+ require "rubype/rubype"
2
2
 
3
3
  # Builtin Contracts
4
4
  class Any; end
@@ -13,13 +13,14 @@ class Module
13
13
  @__rubype__
14
14
  end
15
15
 
16
- def type(*arguments)
17
- *arg_types, type_pair, meth = arguments
16
+ def typesig(hash)
17
+ meth = hash.keys.first
18
+ *arg_types, type_pair = hash.values.first
18
19
 
19
20
  __rubype__.send(:define_method, meth) do |*args, &block|
20
- ::Rubype.assert_arg_type(meth, args, arg_types << type_pair.last_arg_type)
21
+ ::Rubype.assert_arg_type(meth, args, arg_types << type_pair.keys.first)
21
22
  rtn = super(*args, &block)
22
- ::Rubype.assert_trn_type(meth, rtn, type_pair.rtn_type)
23
+ ::Rubype.assert_trn_type(meth, rtn, type_pair.values.first)
23
24
  rtn
24
25
  end
25
26
  self
data/rubype.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.extensions = ["ext/rubype/extconf.rb"]
12
12
  spec.summary = %q{Ruby with type.}
13
13
  spec.description = %q{Ruby with type.}
14
- spec.homepage = "http://azabuhs.org/"
14
+ spec.homepage = "http://gogotanaka.me/"
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0")
data/test/test_rubype.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'minitest_helper'
2
2
  class TypePair
3
3
  def to_s
4
- "#{last_arg_type} >= #{rtn_type}"
4
+ "#{last_arg_type} => #{rtn_type}"
5
5
  end
6
6
  end
7
7
  class TestRubype < MiniTest::Unit::TestCase
@@ -13,66 +13,60 @@ class TestRubype < MiniTest::Unit::TestCase
13
13
  @hash = { test: :hash }
14
14
  end
15
15
 
16
- def test_type_list
17
- assert_equal_to_s "Numeric >= Numeric", Numeric >= Numeric
18
- assert_equal_to_s "Numeric >= Array", Numeric >= Array
19
- assert_equal_to_s "Array >= String", Array >= String
20
- end
21
-
22
16
  def test_correct_type
23
- assert_correct_type [Numeric >= Numeric], [@numeric], @numeric
24
- assert_correct_type [Numeric >= Array ], [@numeric], @array
25
- assert_correct_type [Numeric >= String ], [@numeric], @string
26
- assert_correct_type [Numeric >= Hash ], [@numeric], @hash
27
- assert_correct_type [Numeric >= Symbol ], [@numeric], @symbol
28
- assert_correct_type [Numeric >= Boolean], [@numeric], true
29
- assert_correct_type [Numeric >= Boolean], [@numeric], false
30
-
31
- assert_correct_type [Boolean, Numeric >= Numeric], [true, @numeric], @numeric
32
- assert_correct_type [Boolean, Array >= Array ], [true, @array ], @array
33
- assert_correct_type [Boolean, String >= String ], [true, @string ], @string
34
- assert_correct_type [Boolean, Hash >= Hash ], [true, @hash ], @hash
35
- assert_correct_type [Boolean, Symbol >= Symbol ], [true, @symbol ], @symbol
17
+ assert_correct_type [Numeric => Numeric], [@numeric], @numeric
18
+ assert_correct_type [Numeric => Array ], [@numeric], @array
19
+ assert_correct_type [Numeric => String ], [@numeric], @string
20
+ assert_correct_type [Numeric => Hash ], [@numeric], @hash
21
+ assert_correct_type [Numeric => Symbol ], [@numeric], @symbol
22
+ assert_correct_type [Numeric => Boolean], [@numeric], true
23
+ assert_correct_type [Numeric => Boolean], [@numeric], false
24
+
25
+ assert_correct_type [Boolean, Numeric => Numeric], [true, @numeric], @numeric
26
+ assert_correct_type [Boolean, Array => Array ], [true, @array ], @array
27
+ assert_correct_type [Boolean, String => String ], [true, @string ], @string
28
+ assert_correct_type [Boolean, Hash => Hash ], [true, @hash ], @hash
29
+ assert_correct_type [Boolean, Symbol => Symbol ], [true, @symbol ], @symbol
36
30
  end
37
31
 
38
32
  def test_wrong_return_type
39
- assert_wrong_rtn [Numeric >= Numeric], [@numeric], @array
40
- assert_wrong_rtn [Numeric >= Numeric], [@numeric], @string
41
- assert_wrong_rtn [Numeric >= Numeric], [@numeric], @hash
42
- assert_wrong_rtn [Numeric >= Numeric], [@numeric], @symbol
43
- assert_wrong_rtn [Numeric >= Numeric], [@numeric], true
44
-
45
- assert_wrong_rtn [Numeric, Numeric >= Numeric], [@numeric, @numeric], @array
46
- assert_wrong_rtn [Numeric, Numeric >= Numeric], [@numeric, @numeric], @string
47
- assert_wrong_rtn [Numeric, Numeric >= Numeric], [@numeric, @numeric], @hash
48
- assert_wrong_rtn [Numeric, Numeric >= Numeric], [@numeric, @numeric], @symbol
49
- assert_wrong_rtn [Numeric, Numeric >= Numeric], [@numeric, @numeric], true
33
+ assert_wrong_rtn [Numeric => Numeric], [@numeric], @array
34
+ assert_wrong_rtn [Numeric => Numeric], [@numeric], @string
35
+ assert_wrong_rtn [Numeric => Numeric], [@numeric], @hash
36
+ assert_wrong_rtn [Numeric => Numeric], [@numeric], @symbol
37
+ assert_wrong_rtn [Numeric => Numeric], [@numeric], true
38
+
39
+ assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], @array
40
+ assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], @string
41
+ assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], @hash
42
+ assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], @symbol
43
+ assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], true
50
44
  end
51
45
 
52
46
  def test_wrong_args_type
53
- assert_wrong_arg [Numeric >= Numeric], [@array ], @numeric
54
- assert_wrong_arg [Numeric >= Numeric], [@string], @numeric
55
- assert_wrong_arg [Numeric >= Numeric], [@hash ], @numeric
56
- assert_wrong_arg [Numeric >= Numeric], [@symbol], @numeric
57
- assert_wrong_arg [Numeric >= Numeric], [true ], @numeric
58
-
59
- assert_wrong_arg [Numeric, Numeric >= Numeric], [@numeric, @array ], @numeric
60
- assert_wrong_arg [Numeric, Numeric >= Numeric], [@numeric, @string], @numeric
61
- assert_wrong_arg [Numeric, Numeric >= Numeric], [@numeric, @hash ], @numeric
62
- assert_wrong_arg [Numeric, Numeric >= Numeric], [@numeric, @symbol], @numeric
63
- assert_wrong_arg [Numeric, Numeric >= Numeric], [@numeric, true ], @numeric
47
+ assert_wrong_arg [Numeric => Numeric], [@array ], @numeric
48
+ assert_wrong_arg [Numeric => Numeric], [@string], @numeric
49
+ assert_wrong_arg [Numeric => Numeric], [@hash ], @numeric
50
+ assert_wrong_arg [Numeric => Numeric], [@symbol], @numeric
51
+ assert_wrong_arg [Numeric => Numeric], [true ], @numeric
52
+
53
+ assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, @array ], @numeric
54
+ assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, @string], @numeric
55
+ assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, @hash ], @numeric
56
+ assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, @symbol], @numeric
57
+ assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, true ], @numeric
64
58
  end
65
59
 
66
60
  def test_any
67
- assert_correct_type [Any >= Any], [@array ], @numeric
68
- assert_correct_type [Any >= Any], [@string], @numeric
69
- assert_correct_type [Any >= Any], [@hash ], @numeric
70
- assert_correct_type [Any >= Any], [@symbol], @numeric
71
-
72
- assert_correct_type [Any, Any >= Any], [@numeric, @array ], @numeric
73
- assert_correct_type [Any, Any >= Any], [@numeric, @string], @numeric
74
- assert_correct_type [Any, Any >= Any], [@numeric, @hash ], @numeric
75
- assert_correct_type [Any, Any >= Any], [@numeric, @symbol], @numeric
61
+ assert_correct_type [Any => Any], [@array ], @numeric
62
+ assert_correct_type [Any => Any], [@string], @numeric
63
+ assert_correct_type [Any => Any], [@hash ], @numeric
64
+ assert_correct_type [Any => Any], [@symbol], @numeric
65
+
66
+ assert_correct_type [Any, Any => Any], [@numeric, @array ], @numeric
67
+ assert_correct_type [Any, Any => Any], [@numeric, @string], @numeric
68
+ assert_correct_type [Any, Any => Any], [@numeric, @hash ], @numeric
69
+ assert_correct_type [Any, Any => Any], [@numeric, @symbol], @numeric
76
70
  end
77
71
 
78
72
  private
@@ -97,7 +91,7 @@ class TestRubype < MiniTest::Unit::TestCase
97
91
  def call(#{arg_literal(args.count)})
98
92
  #{obj_literal(val)}
99
93
  end
100
- type #{type_list.map(&:to_s).join(',')}, :call
94
+ typesig call: #{obj_literal(type_list)}
101
95
  RUBY_CODE
102
96
 
103
97
  klass.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubype
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-06 00:00:00.000000000 Z
11
+ date: 2014-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,12 +86,11 @@ files:
86
86
  - ext/rubype/rubype.c
87
87
  - ext/rubype/rubype.h
88
88
  - lib/rubype.rb
89
- - lib/rubype/type_pair.rb
90
89
  - lib/rubype/version.rb
91
90
  - rubype.gemspec
92
91
  - test/minitest_helper.rb
93
92
  - test/test_rubype.rb
94
- homepage: http://azabuhs.org/
93
+ homepage: http://gogotanaka.me/
95
94
  licenses:
96
95
  - MIT
97
96
  metadata: {}
@@ -1,7 +0,0 @@
1
- class TypePair < Struct.new(:last_arg_type, :rtn_type); end
2
-
3
- class Module
4
- def >=(r)
5
- TypePair.new(self, r)
6
- end
7
- end