RubyToC 1.0.0.4 → 1.0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +15 -0
- data/Makefile +7 -2
- data/Manifest.txt +18 -88
- data/README.txt +2 -1
- data/Rakefile +52 -0
- data/bin/ruby_to_c_show +97 -0
- data/{validate.sh → bin/ruby_to_c_validate} +0 -0
- data/{rewriter.rb → lib/rewriter.rb} +19 -5
- data/{ruby_to_c.rb → lib/ruby_to_ansi_c.rb} +71 -80
- data/lib/ruby_to_ruby_c.rb +167 -0
- data/{support.rb → lib/support.rb} +20 -19
- data/{type_checker.rb → lib/type_checker.rb} +17 -11
- data/{typed_sexp_processor.rb → lib/typed_sexp_processor.rb} +30 -30
- data/test/r2ctestcase.rb +1261 -0
- data/{test_all.rb → test/test_all.rb} +0 -0
- data/{test_extras.rb → test/test_extras.rb} +59 -61
- data/{test_rewriter.rb → test/test_rewriter.rb} +38 -38
- data/{test_ruby_to_c.rb → test/test_ruby_to_ansi_c.rb} +128 -173
- data/test/test_ruby_to_ruby_c.rb +27 -0
- data/{test_support.rb → test/test_support.rb} +80 -43
- data/{test_type_checker.rb → test/test_type_checker.rb} +121 -130
- data/{test_typed_sexp_processor.rb → test/test_typed_sexp_processor.rb} +70 -65
- metadata +55 -48
- data/rewrite.rb +0 -32
- data/translate.rb +0 -31
- data/type.rb +0 -33
- data/zcomparable.rb +0 -300
@@ -1,11 +1,9 @@
|
|
1
1
|
#!/usr/local/bin/ruby -w
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'test_sexp'
|
4
4
|
require 'typed_sexp_processor'
|
5
5
|
require 'test/unit'
|
6
6
|
|
7
|
-
# Fake test classes:
|
8
|
-
|
9
7
|
class TestTypedSexp < TestSexp
|
10
8
|
|
11
9
|
def setup
|
@@ -13,11 +11,68 @@ class TestTypedSexp < TestSexp
|
|
13
11
|
@sexp.sexp_type = Type.str
|
14
12
|
end
|
15
13
|
|
14
|
+
def test__set_sexp_type
|
15
|
+
assert_equal Type.str, @sexp.sexp_type
|
16
|
+
@sexp._set_sexp_type Type.bool
|
17
|
+
assert_equal Type.bool, @sexp.sexp_type
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_equals2_tsexp
|
21
|
+
sexp2 = s(1, 2, 3)
|
22
|
+
sexp3 = t(1, 2, 3, Type.str)
|
23
|
+
assert_equal(@sexp, sexp3)
|
24
|
+
assert_not_equal(@sexp, sexp2)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_equals_array_typed
|
28
|
+
# can't use assert_equals because it uses array as receiver
|
29
|
+
assert_not_equal(@sexp, [1, 2, 3, Type.str],
|
30
|
+
"Sexp must not be equal to equivalent array")
|
31
|
+
# both directions just in case
|
32
|
+
assert_not_equal([1, 2, 3, Type.str], @sexp,
|
33
|
+
"Sexp must not be equal to equivalent array")
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_equals_not_body_typed
|
37
|
+
sexp2 = t(1, 2, 5)
|
38
|
+
sexp2.sexp_type = Type.str
|
39
|
+
assert_not_equal(@sexp, sexp2)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_equals_not_type
|
43
|
+
sexp2 = t(1, 2, 3)
|
44
|
+
sexp2.sexp_type = Type.long
|
45
|
+
assert_not_equal(@sexp, sexp2)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_equals_sexp
|
49
|
+
sexp2 = t(1, 2, 3, Type.str)
|
50
|
+
assert_equal(@sexp, sexp2)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_equals_sexp_typed
|
54
|
+
sexp2 = t(1, 2, 3)
|
55
|
+
sexp2.sexp_type = Type.str
|
56
|
+
assert_equal(@sexp, sexp2)
|
57
|
+
end
|
58
|
+
|
16
59
|
def test_new_nested_typed
|
17
60
|
@sexp = TypedSexp.new(:lasgn, "var", TypedSexp.new(:str, "foo", Type.str), Type.str)
|
18
61
|
assert_equal('t(:lasgn, "var", t(:str, "foo", Type.str), Type.str)',
|
19
62
|
@sexp.inspect)
|
20
63
|
end
|
64
|
+
|
65
|
+
def test_pretty_print_typed
|
66
|
+
util_pretty_print("t(Type.str)",
|
67
|
+
t(Type.str))
|
68
|
+
util_pretty_print("t(:a, Type.long)",
|
69
|
+
t(:a, Type.long))
|
70
|
+
util_pretty_print("t(:a, :b, Type.long)",
|
71
|
+
t(:a, :b, Type.long))
|
72
|
+
util_pretty_print("t(:a, t(:b, Type.long), Type.str)",
|
73
|
+
t(:a, t(:b, Type.long), Type.str))
|
74
|
+
end
|
75
|
+
|
21
76
|
def test_sexp_type
|
22
77
|
assert_equal(Type.str, @sexp.sexp_type)
|
23
78
|
end
|
@@ -29,13 +84,6 @@ class TestTypedSexp < TestSexp
|
|
29
84
|
assert_equal(24, @sexp.sexp_type)
|
30
85
|
end
|
31
86
|
|
32
|
-
def test_sexp_type_array_homo
|
33
|
-
@sexp = t(:array, t(:lit, 1, Type.long),
|
34
|
-
t(:lit, 2, Type.long))
|
35
|
-
assert_equal(Type.homo, @sexp.sexp_type)
|
36
|
-
assert_equal([Type.long, Type.long], @sexp.sexp_types)
|
37
|
-
end
|
38
|
-
|
39
87
|
def test_sexp_type_array_hetero
|
40
88
|
@sexp = t(:array, t(:lit, 1, Type.long),
|
41
89
|
t(:str, "foo", Type.str))
|
@@ -43,6 +91,13 @@ class TestTypedSexp < TestSexp
|
|
43
91
|
assert_equal([Type.long, Type.str], @sexp.sexp_types)
|
44
92
|
end
|
45
93
|
|
94
|
+
def test_sexp_type_array_homo
|
95
|
+
@sexp = t(:array, t(:lit, 1, Type.long),
|
96
|
+
t(:lit, 2, Type.long))
|
97
|
+
assert_equal(Type.homo, @sexp.sexp_type)
|
98
|
+
assert_equal([Type.long, Type.long], @sexp.sexp_types)
|
99
|
+
end
|
100
|
+
|
46
101
|
def test_sexp_type_array_nested
|
47
102
|
@sexp = t(:array, t(:lit, 1, Type.long),
|
48
103
|
t(:array, t(:lit, 1, Type.long)))
|
@@ -50,12 +105,6 @@ class TestTypedSexp < TestSexp
|
|
50
105
|
assert_equal([Type.long, Type.homo], @sexp.sexp_types)
|
51
106
|
end
|
52
107
|
|
53
|
-
def test__set_sexp_type
|
54
|
-
assert_equal Type.str, @sexp.sexp_type
|
55
|
-
@sexp._set_sexp_type Type.bool
|
56
|
-
assert_equal Type.bool, @sexp.sexp_type
|
57
|
-
end
|
58
|
-
|
59
108
|
def test_sexp_types
|
60
109
|
assert_raises(RuntimeError) do
|
61
110
|
@sexp.sexp_types
|
@@ -66,47 +115,13 @@ class TestTypedSexp < TestSexp
|
|
66
115
|
assert_equal([Type.long, Type.str], @sexp.sexp_types)
|
67
116
|
end
|
68
117
|
|
69
|
-
def
|
70
|
-
|
71
|
-
assert_equal(@sexp
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_equals_not_body_typed
|
75
|
-
sexp2 = t(1, 2, 5)
|
76
|
-
sexp2.sexp_type = Type.str
|
77
|
-
assert_not_equal(@sexp, sexp2)
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_equals_not_type
|
81
|
-
sexp2 = t(1, 2, 3)
|
82
|
-
sexp2.sexp_type = Type.long
|
83
|
-
assert_not_equal(@sexp, sexp2)
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_equals_sexp_typed
|
87
|
-
sexp2 = t(1, 2, 3)
|
88
|
-
sexp2.sexp_type = Type.str
|
89
|
-
assert_equal(@sexp, sexp2)
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_equals_array_typed
|
93
|
-
# can't use assert_equals because it uses array as receiver
|
94
|
-
assert_not_equal(@sexp, [1, 2, 3, Type.str],
|
95
|
-
"Sexp must not be equal to equivalent array")
|
96
|
-
# both directions just in case
|
97
|
-
assert_not_equal([1, 2, 3, Type.str], @sexp,
|
98
|
-
"Sexp must not be equal to equivalent array")
|
118
|
+
def test_to_a
|
119
|
+
@sexp = t(1, 2, 3)
|
120
|
+
assert_equal([1, 2, 3], @sexp.to_a)
|
99
121
|
end
|
100
122
|
|
101
|
-
def
|
102
|
-
|
103
|
-
t(Type.str))
|
104
|
-
util_pretty_print("t(:a, Type.long)",
|
105
|
-
t(:a, Type.long))
|
106
|
-
util_pretty_print("t(:a, :b, Type.long)",
|
107
|
-
t(:a, :b, Type.long))
|
108
|
-
util_pretty_print("t(:a, t(:b, Type.long), Type.str)",
|
109
|
-
t(:a, t(:b, Type.long), Type.str))
|
123
|
+
def test_to_a_typed
|
124
|
+
assert_equal([1, 2, 3, Type.str], @sexp.to_a)
|
110
125
|
end
|
111
126
|
|
112
127
|
def test_to_s_typed
|
@@ -121,14 +136,4 @@ class TestTypedSexp < TestSexp
|
|
121
136
|
assert_equal("#{n}(:a, #{n}(:b, Type.long), Type.str)",
|
122
137
|
k.new(:a, k.new(:b, Type.long), Type.str).inspect)
|
123
138
|
end
|
124
|
-
|
125
|
-
def test_to_a
|
126
|
-
@sexp = t(1, 2, 3)
|
127
|
-
assert_equal([1, 2, 3], @sexp.to_a)
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_to_a_typed
|
131
|
-
assert_equal([1, 2, 3, Type.str], @sexp.to_a)
|
132
|
-
end
|
133
|
-
|
134
139
|
end
|
metadata
CHANGED
@@ -1,74 +1,81 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.8.
|
2
|
+
rubygems_version: 0.8.11.15
|
3
3
|
specification_version: 1
|
4
4
|
name: RubyToC
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.0.
|
7
|
-
date:
|
6
|
+
version: 1.0.0.5
|
7
|
+
date: 2006-05-12 00:00:00 -07:00
|
8
8
|
summary: Ruby (subset) to C translator.
|
9
9
|
require_paths:
|
10
|
-
|
10
|
+
- lib
|
11
11
|
email: ryand-ruby@zenspider.com
|
12
12
|
homepage: http://rubyforge.org/projects/ruby2c/
|
13
13
|
rubyforge_project: ruby2c
|
14
14
|
description: ruby_to_c translates a static ruby subset to C. Hopefully it works.
|
15
|
-
autorequire:
|
15
|
+
autorequire:
|
16
16
|
default_executable:
|
17
17
|
bindir: bin
|
18
18
|
has_rdoc: true
|
19
19
|
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
20
|
requirements:
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
version: 0.0.0
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
25
24
|
version:
|
26
25
|
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
27
29
|
authors:
|
28
|
-
|
30
|
+
- Ryan Davis
|
29
31
|
files:
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
32
|
+
- History.txt
|
33
|
+
- Makefile
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
- Rakefile
|
37
|
+
- bin/ruby_to_c_show
|
38
|
+
- bin/ruby_to_c_validate
|
39
|
+
- demo/char.rb
|
40
|
+
- demo/factorial.rb
|
41
|
+
- demo/hello.rb
|
42
|
+
- demo/misc.rb
|
43
|
+
- demo/newarray.rb
|
44
|
+
- demo/strcat.rb
|
45
|
+
- lib/rewriter.rb
|
46
|
+
- lib/ruby_to_ansi_c.rb
|
47
|
+
- lib/ruby_to_ruby_c.rb
|
48
|
+
- lib/support.rb
|
49
|
+
- lib/type_checker.rb
|
50
|
+
- lib/typed_sexp_processor.rb
|
51
|
+
- test/r2ctestcase.rb
|
52
|
+
- test/test_all.rb
|
53
|
+
- test/test_extras.rb
|
54
|
+
- test/test_rewriter.rb
|
55
|
+
- test/test_ruby_to_ansi_c.rb
|
56
|
+
- test/test_ruby_to_ruby_c.rb
|
57
|
+
- test/test_support.rb
|
58
|
+
- test/test_type_checker.rb
|
59
|
+
- test/test_typed_sexp_processor.rb
|
57
60
|
test_files:
|
58
|
-
|
61
|
+
- test/test_all.rb
|
59
62
|
rdoc_options: []
|
63
|
+
|
60
64
|
extra_rdoc_files: []
|
65
|
+
|
61
66
|
executables: []
|
67
|
+
|
62
68
|
extensions: []
|
69
|
+
|
63
70
|
requirements: []
|
71
|
+
|
64
72
|
dependencies:
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
version:
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: ParseTree
|
75
|
+
version_requirement:
|
76
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 0.0.0
|
81
|
+
version:
|
data/rewrite.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
#!/usr/local/bin/ruby -w
|
2
|
-
|
3
|
-
require 'pp'
|
4
|
-
require 'rewriter'
|
5
|
-
|
6
|
-
old_classes = []
|
7
|
-
ObjectSpace.each_object(Class) do |klass|
|
8
|
-
old_classes << klass
|
9
|
-
end
|
10
|
-
|
11
|
-
ARGV.each do |name|
|
12
|
-
require name
|
13
|
-
end
|
14
|
-
|
15
|
-
new_classes = []
|
16
|
-
ObjectSpace.each_object(Class) do |klass|
|
17
|
-
new_classes << klass
|
18
|
-
end
|
19
|
-
|
20
|
-
new_classes -= old_classes
|
21
|
-
|
22
|
-
parser = ParseTree.new
|
23
|
-
rewriter = Rewriter.new
|
24
|
-
|
25
|
-
new_classes.each do |klass|
|
26
|
-
sexp = parser.parse_tree klass
|
27
|
-
sexp.each do |exp|
|
28
|
-
exp = rewriter.process(exp)
|
29
|
-
pp exp
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
data/translate.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
#!/usr/local/bin/ruby -ws
|
2
|
-
|
3
|
-
begin require 'rubygems' rescue LoadError end
|
4
|
-
require 'parse_tree'
|
5
|
-
require 'ruby_to_c'
|
6
|
-
|
7
|
-
old_classes = []
|
8
|
-
ObjectSpace.each_object(Class) do |klass|
|
9
|
-
old_classes << klass
|
10
|
-
end
|
11
|
-
|
12
|
-
ARGV.each do |name|
|
13
|
-
require name
|
14
|
-
end
|
15
|
-
|
16
|
-
new_classes = []
|
17
|
-
ObjectSpace.each_object(Class) do |klass|
|
18
|
-
new_classes << klass
|
19
|
-
end
|
20
|
-
|
21
|
-
new_classes -= old_classes
|
22
|
-
new_classes = [ eval($c) ] if defined? $c
|
23
|
-
|
24
|
-
rubytoc = RubyToC.translator
|
25
|
-
|
26
|
-
code = ParseTree.new(false).parse_tree(*new_classes).map do |klass|
|
27
|
-
rubytoc.process(klass)
|
28
|
-
end # rescue nil
|
29
|
-
|
30
|
-
puts rubytoc.processors.last.preamble
|
31
|
-
puts code.join("\n\n")
|
data/type.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
#!/usr/local/bin/ruby -ws
|
2
|
-
|
3
|
-
require 'pp'
|
4
|
-
require 'type_checker'
|
5
|
-
|
6
|
-
old_classes = []
|
7
|
-
ObjectSpace.each_object(Class) do |klass|
|
8
|
-
old_classes << klass
|
9
|
-
end
|
10
|
-
|
11
|
-
ARGV.each do |name|
|
12
|
-
require name
|
13
|
-
end
|
14
|
-
|
15
|
-
new_classes = []
|
16
|
-
ObjectSpace.each_object(Class) do |klass|
|
17
|
-
new_classes << klass
|
18
|
-
end
|
19
|
-
|
20
|
-
new_classes -= old_classes
|
21
|
-
new_classes = [ eval($c) ] if defined? $c
|
22
|
-
|
23
|
-
parser = ParseTree.new(false)
|
24
|
-
rewriter = Rewriter.new
|
25
|
-
type_checker = TypeChecker.new
|
26
|
-
|
27
|
-
new_classes.each do |klass|
|
28
|
-
sexp = parser.parse_tree klass
|
29
|
-
sexp.each do |exp|
|
30
|
-
exp = type_checker.process(rewriter.process(exp))
|
31
|
-
pp exp.to_a
|
32
|
-
end
|
33
|
-
end
|
data/zcomparable.rb
DELETED
@@ -1,300 +0,0 @@
|
|
1
|
-
|
2
|
-
class ZComparable
|
3
|
-
|
4
|
-
# int
|
5
|
-
# rb_cmpint(val, a, b)
|
6
|
-
# VALUE val, a, b;
|
7
|
-
# {
|
8
|
-
# if (NIL_P(val)) {
|
9
|
-
# zrb_cmperr(a, b);
|
10
|
-
# }
|
11
|
-
# if (FIXNUM_P(val)) return FIX2INT(val);
|
12
|
-
# if (TYPE(val) == T_BIGNUM) {
|
13
|
-
# if (RBIGNUM(val)->sign) return 1;
|
14
|
-
# return -1;
|
15
|
-
# }
|
16
|
-
# if (RTEST(rb_funcall(val, '>', 1, INT2FIX(0)))) return 1;
|
17
|
-
# if (RTEST(rb_funcall(val, '<', 1, INT2FIX(0)))) return -1;
|
18
|
-
# return 0;
|
19
|
-
# }
|
20
|
-
|
21
|
-
def zrb_cmpint(val, a, b)
|
22
|
-
return val.to_i
|
23
|
-
end
|
24
|
-
|
25
|
-
# void
|
26
|
-
# rb_cmperr(x, y)
|
27
|
-
# VALUE x, y;
|
28
|
-
# {
|
29
|
-
# const char *classname;
|
30
|
-
#
|
31
|
-
# if (SPECIAL_CONST_P(y)) {
|
32
|
-
# y = rb_inspect(y);
|
33
|
-
# classname = StringValuePtr(y);
|
34
|
-
# }
|
35
|
-
# else {
|
36
|
-
# classname = rb_obj_classname(y);
|
37
|
-
# }
|
38
|
-
# rb_raise(rb_eArgError, "comparison of %s with %s failed",
|
39
|
-
# rb_obj_classname(x), classname);
|
40
|
-
# }
|
41
|
-
|
42
|
-
def zrb_cmperr(x, y)
|
43
|
-
$stderr.puts("comparison of " + x.class.to_s + " with " + y.class.to_s + " failed")
|
44
|
-
exit 1
|
45
|
-
# raise ArgumentError, "comparison of #{x.class} with #{y.class} failed"
|
46
|
-
end
|
47
|
-
|
48
|
-
# #define cmperr() (rb_cmperr(x, y), Qnil)
|
49
|
-
|
50
|
-
# static VALUE
|
51
|
-
# cmp_eq(a)
|
52
|
-
# VALUE *a;
|
53
|
-
# {
|
54
|
-
# VALUE c = rb_funcall(a[0], cmp, 1, a[1]);
|
55
|
-
|
56
|
-
# if (NIL_P(c)) return Qnil;
|
57
|
-
# if (rb_cmpint(c, a[0], a[1]) == 0) return Qtrue;
|
58
|
-
# return Qfalse;
|
59
|
-
# }
|
60
|
-
|
61
|
-
def zcmp_eq(a, b)
|
62
|
-
c = a <=> b
|
63
|
-
return false if c.nil?
|
64
|
-
return c.to_i == 0
|
65
|
-
end
|
66
|
-
|
67
|
-
# static VALUE
|
68
|
-
# cmp_failed()
|
69
|
-
# {
|
70
|
-
# return Qnil;
|
71
|
-
# }
|
72
|
-
|
73
|
-
# TODO RETIRE me
|
74
|
-
def zcmp_failed
|
75
|
-
return nil
|
76
|
-
end
|
77
|
-
|
78
|
-
# /*
|
79
|
-
# * call-seq:
|
80
|
-
# * obj == other => true or false
|
81
|
-
# *
|
82
|
-
# * Compares two objects based on the receiver's <code><=></code>
|
83
|
-
# * method, returning true if it returns 0. Also returns true if
|
84
|
-
# * _obj_ and _other_ are the same object.
|
85
|
-
# */
|
86
|
-
|
87
|
-
# static VALUE
|
88
|
-
# cmp_equal(x, y)
|
89
|
-
# VALUE x, y;
|
90
|
-
# {
|
91
|
-
# VALUE a[2];
|
92
|
-
#
|
93
|
-
# if (x == y) return Qtrue;
|
94
|
-
#
|
95
|
-
# a[0] = x; a[1] = y;
|
96
|
-
# return rb_rescue(cmp_eq, (VALUE)a, cmp_failed, 0);
|
97
|
-
# }
|
98
|
-
|
99
|
-
def zcmp_equal(x, y)
|
100
|
-
|
101
|
-
if x.equal? y then
|
102
|
-
return true
|
103
|
-
else
|
104
|
-
return zcmp_eq(x, y) # HACK rescue false
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
108
|
-
|
109
|
-
# /*
|
110
|
-
# * call-seq:
|
111
|
-
# * obj > other => true or false
|
112
|
-
# *
|
113
|
-
# * Compares two objects based on the receiver's <code><=></code>
|
114
|
-
# * method, returning true if it returns 1.
|
115
|
-
# */
|
116
|
-
|
117
|
-
# static VALUE
|
118
|
-
# cmp_gt(x, y)
|
119
|
-
# VALUE x, y;
|
120
|
-
# {
|
121
|
-
# VALUE c = rb_funcall(x, cmp, 1, y);
|
122
|
-
|
123
|
-
# if (NIL_P(c)) return cmperr();
|
124
|
-
# if (rb_cmpint(c, x, y) > 0) return Qtrue;
|
125
|
-
# return Qfalse;
|
126
|
-
# }
|
127
|
-
|
128
|
-
def zcmp_gt(x, y)
|
129
|
-
c = x <=> y
|
130
|
-
|
131
|
-
zrb_cmperr(x, y) if c.nil?
|
132
|
-
|
133
|
-
return c.to_i > 0
|
134
|
-
end
|
135
|
-
|
136
|
-
# /*
|
137
|
-
# * call-seq:
|
138
|
-
# * obj >= other => true or false
|
139
|
-
# *
|
140
|
-
# * Compares two objects based on the receiver's <code><=></code>
|
141
|
-
# * method, returning true if it returns 0 or 1.
|
142
|
-
# */
|
143
|
-
|
144
|
-
# static VALUE
|
145
|
-
# cmp_ge(x, y)
|
146
|
-
# VALUE x, y;
|
147
|
-
# {
|
148
|
-
# VALUE c = rb_funcall(x, cmp, 1, y);
|
149
|
-
|
150
|
-
# if (NIL_P(c)) return cmperr();
|
151
|
-
# if (rb_cmpint(c, x, y) >= 0) return Qtrue;
|
152
|
-
# return Qfalse;
|
153
|
-
# }
|
154
|
-
|
155
|
-
def zcmp_ge(x, y)
|
156
|
-
c = x <=> y
|
157
|
-
|
158
|
-
zrb_cmperr(x, y) if c.nil?
|
159
|
-
|
160
|
-
return c.to_i >= 0
|
161
|
-
end
|
162
|
-
|
163
|
-
# /*
|
164
|
-
# * call-seq:
|
165
|
-
# * obj < other => true or false
|
166
|
-
# *
|
167
|
-
# * Compares two objects based on the receiver's <code><=></code>
|
168
|
-
# * method, returning true if it returns -1.
|
169
|
-
# */
|
170
|
-
|
171
|
-
# static VALUE
|
172
|
-
# cmp_lt(x, y)
|
173
|
-
# VALUE x, y;
|
174
|
-
# {
|
175
|
-
# VALUE c = rb_funcall(x, cmp, 1, y);
|
176
|
-
#
|
177
|
-
# if (NIL_P(c)) return cmperr();
|
178
|
-
# if (rb_cmpint(c, x, y) < 0) return Qtrue;
|
179
|
-
# return Qfalse;
|
180
|
-
# }
|
181
|
-
|
182
|
-
def zcmp_lt(x, y)
|
183
|
-
c = x <=> y
|
184
|
-
|
185
|
-
zrb_cmperr(x, y) if c.nil?
|
186
|
-
|
187
|
-
return c.to_i < 0
|
188
|
-
end
|
189
|
-
|
190
|
-
# /*
|
191
|
-
# * call-seq:
|
192
|
-
# * obj <= other => true or false
|
193
|
-
# *
|
194
|
-
# * Compares two objects based on the receiver's <code><=></code>
|
195
|
-
# * method, returning true if it returns -1 or 0.
|
196
|
-
# */
|
197
|
-
|
198
|
-
# static VALUE
|
199
|
-
# cmp_le(x, y)
|
200
|
-
# VALUE x, y;
|
201
|
-
# {
|
202
|
-
# VALUE c = rb_funcall(x, cmp, 1, y);
|
203
|
-
|
204
|
-
# if (NIL_P(c)) return cmperr();
|
205
|
-
# if (rb_cmpint(c, x, y) <= 0) return Qtrue;
|
206
|
-
# return Qfalse;
|
207
|
-
# }
|
208
|
-
|
209
|
-
def zcmp_le(x, y)
|
210
|
-
c = x <=> y
|
211
|
-
|
212
|
-
zrb_cmperr(x, y) if c.nil?
|
213
|
-
|
214
|
-
return c.to_i <= 0
|
215
|
-
end
|
216
|
-
|
217
|
-
# /*
|
218
|
-
# * call-seq:
|
219
|
-
# * obj.between?(min, max) => true or false
|
220
|
-
# *
|
221
|
-
# * Returns <code>false</code> if <i>obj</i> <code><=></code>
|
222
|
-
# * <i>min</i> is less than zero or if <i>anObject</i> <code><=></code>
|
223
|
-
# * <i>max</i> is greater than zero, <code>true</code> otherwise.
|
224
|
-
# *
|
225
|
-
# * 3.between?(1, 5) #=> true
|
226
|
-
# * 6.between?(1, 5) #=> false
|
227
|
-
# * 'cat'.between?('ant', 'dog') #=> true
|
228
|
-
# * 'gnu'.between?('ant', 'dog') #=> false
|
229
|
-
# *
|
230
|
-
# */
|
231
|
-
|
232
|
-
# static VALUE
|
233
|
-
# cmp_between(x, min, max)
|
234
|
-
# VALUE x, min, max;
|
235
|
-
# {
|
236
|
-
# if (RTEST(cmp_lt(x, min))) return Qfalse;
|
237
|
-
# if (RTEST(cmp_gt(x, max))) return Qfalse;
|
238
|
-
# return Qtrue;
|
239
|
-
# }
|
240
|
-
|
241
|
-
def zcmp_between(x, min, max)
|
242
|
-
|
243
|
-
return false if zcmp_lt(x, min)
|
244
|
-
return false if zcmp_gt(x, max)
|
245
|
-
return true
|
246
|
-
|
247
|
-
end
|
248
|
-
|
249
|
-
# /*
|
250
|
-
# * The <code>Comparable</code> mixin is used by classes whose objects
|
251
|
-
# * may be ordered. The class must define the <code><=></code> operator,
|
252
|
-
# * which compares the receiver against another object, returning -1, 0,
|
253
|
-
# * or +1 depending on whether the receiver is less than, equal to, or
|
254
|
-
# * greater than the other object. <code>Comparable</code> uses
|
255
|
-
# * <code><=></code> to implement the conventional comparison operators
|
256
|
-
# * (<code><</code>, <code><=</code>, <code>==</code>, <code>>=</code>,
|
257
|
-
# * and <code>></code>) and the method <code>between?</code>.
|
258
|
-
# *
|
259
|
-
# * class SizeMatters
|
260
|
-
# * include Comparable
|
261
|
-
# * attr :str
|
262
|
-
# * def <=>(anOther)
|
263
|
-
# * str.size <=> anOther.str.size
|
264
|
-
# * end
|
265
|
-
# * def initialize(str)
|
266
|
-
# * @str = str
|
267
|
-
# * end
|
268
|
-
# * def inspect
|
269
|
-
# * @str
|
270
|
-
# * end
|
271
|
-
# * end
|
272
|
-
# *
|
273
|
-
# * s1 = SizeMatters.new("Z")
|
274
|
-
# * s2 = SizeMatters.new("YY")
|
275
|
-
# * s3 = SizeMatters.new("XXX")
|
276
|
-
# * s4 = SizeMatters.new("WWWW")
|
277
|
-
# * s5 = SizeMatters.new("VVVVV")
|
278
|
-
# *
|
279
|
-
# * s1 < s2 #=> true
|
280
|
-
# * s4.between?(s1, s3) #=> false
|
281
|
-
# * s4.between?(s3, s5) #=> true
|
282
|
-
# * [ s3, s2, s5, s4, s1 ].sort #=> [Z, YY, XXX, WWWW, VVVVV]
|
283
|
-
# *
|
284
|
-
# */
|
285
|
-
|
286
|
-
# void
|
287
|
-
# Init_Comparable()
|
288
|
-
# {
|
289
|
-
# rb_mComparable = rb_define_module("Comparable");
|
290
|
-
# rb_define_method(rb_mComparable, "==", cmp_equal, 1);
|
291
|
-
# rb_define_method(rb_mComparable, ">", cmp_gt, 1);
|
292
|
-
# rb_define_method(rb_mComparable, ">=", cmp_ge, 1);
|
293
|
-
# rb_define_method(rb_mComparable, "<", cmp_lt, 1);
|
294
|
-
# rb_define_method(rb_mComparable, "<=", cmp_le, 1);
|
295
|
-
# rb_define_method(rb_mComparable, "between?", cmp_between, 2);
|
296
|
-
|
297
|
-
# cmp = rb_intern("<=>");
|
298
|
-
# }
|
299
|
-
|
300
|
-
end
|