Skip to content

Commit

Permalink
move body of Comparison from compar.c to compar.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed May 9, 2012
1 parent db96db6 commit 6a0c30d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 161 deletions.
121 changes: 63 additions & 58 deletions mrblib/compar.rb
@@ -1,63 +1,68 @@
### move to compar.c
# module Comparable
# def == other
# cmp = self <=> other
# if cmp == 0
# true
# else
# false
# end
# end
module Comparable
# 15.3.3.2.1
def < other
cmp = self <=> other
if cmp.nil?
false
elsif cmp < 0
true
else
false
end
end

# def < other
# cmp = self <=> other
# if cmp.nil?
# false
# elsif cmp < 0
# true
# else
# false
# end
# end
# 15.3.3.2.2
def <= other
cmp = self <=> other
if cmp.nil?
false
elsif cmp <= 0
true
else
false
end
end

# def <= other
# cmp = self <=> other
# if cmp.nil?
# false
# elsif cmp <= 0
# true
# else
# false
# end
# end
# 15.3.3.2.3
def == other
cmp = self <=> other
if cmp == 0
true
else
false
end
end

# def > other
# cmp = self <=> other
# if cmp.nil?
# false
# elsif cmp > 0
# true
# else
# false
# end
# end
# 15.3.3.2.4
def > other
cmp = self <=> other
if cmp.nil?
false
elsif cmp > 0
true
else
false
end
end

# def >= other
# cmp = self <=> other
# if cmp.nil?
# false
# elsif cmp >= 0
# true
# else
# false
# end
# end
# 15.3.3.2.5
def >= other
cmp = self <=> other
if cmp.nil?
false
elsif cmp >= 0
true
else
false
end
end

# def between?(min,max)
# if self < min or self > max
# false
# else
# true
# end
# end
# end
# 15.3.3.2.6
def between?(min,max)
if self < min or self > max
false
else
true
end
end
end
104 changes: 1 addition & 103 deletions src/compar.c
Expand Up @@ -15,7 +15,6 @@ mrb_cmperr(mrb_state *mrb, mrb_value x, mrb_value y)

if (SPECIAL_CONST_P(y)) {
y = mrb_inspect(mrb, y);
//classname = StringValuePtr(y);
classname = mrb_string_value_ptr(mrb, y);
}
else {
Expand All @@ -42,109 +41,8 @@ mrb_cmpint(mrb_state *mrb, mrb_value val, mrb_value a, mrb_value b)
return 0;
}

static mrb_value
cmp_equal(mrb_state *mrb, mrb_value x)
{
mrb_value y, c;

/* *** TEMPORAL IMPLEMENT *** */

mrb_get_args(mrb, "o", &y);
if (mrb_obj_equal(mrb, x, y)) return mrb_true_value();

c = mrb_funcall(mrb, x, "<=>", 1, y);

if (mrb_cmpint(mrb, c, x, y) == 0) return mrb_true_value();
return mrb_false_value();
}

#include <stdio.h>
static mrb_value
cmp_gt(mrb_state *mrb, mrb_value x, mrb_value y)
{
mrb_value c;

c = mrb_funcall(mrb, x, "<=>", 1, y);

if (mrb_cmpint(mrb, c, x, y) > 0) return mrb_true_value();
return mrb_false_value();
}

static mrb_value
cmp_gt_m(mrb_state *mrb, mrb_value x)
{
mrb_value y;

mrb_get_args(mrb, "o", &y);
return cmp_gt(mrb, x, y);
}

static mrb_value
cmp_ge_m(mrb_state *mrb, mrb_value x)
{
mrb_value y, c;

mrb_get_args(mrb, "o", &y);
c = mrb_funcall(mrb, x, "<=>", 1, y);

if (mrb_cmpint(mrb, c, x, y) >= 0) return mrb_true_value();
return mrb_false_value();
}

static mrb_value
cmp_lt(mrb_state *mrb, mrb_value x, mrb_value y)
{
mrb_value c;

c = mrb_funcall(mrb, x, "<=>", 1, y);

if (mrb_cmpint(mrb, c, x, y) < 0) return mrb_true_value();
return mrb_false_value();
}

static mrb_value
cmp_lt_m(mrb_state *mrb, mrb_value x)
{
mrb_value y;

mrb_get_args(mrb, "o", &y);
return cmp_lt(mrb, x, y);
}

static mrb_value
cmp_le_m(mrb_state *mrb, mrb_value x)
{
mrb_value y, c;

mrb_get_args(mrb, "o", &y);
c = mrb_funcall(mrb, x, "<=>", 1, y);

if (mrb_cmpint(mrb, c, x, y) <= 0) return mrb_true_value();
return mrb_false_value();
}

static mrb_value
cmp_between(mrb_state *mrb, mrb_value x)
{
mrb_value min, max;

mrb_get_args(mrb, "oo", &min, &max);

if (mrb_test(cmp_lt(mrb, x, min))) return mrb_false_value();
if (mrb_test(cmp_gt(mrb, x, max))) return mrb_false_value();
return mrb_true_value();
}

void
mrb_init_comparable(mrb_state *mrb)
{
struct RClass *comp;

comp = mrb_define_module(mrb, "Comparable");
mrb_define_method(mrb, comp, "<", cmp_lt_m, ARGS_REQ(1)); /* 15.3.3.2.1 */
mrb_define_method(mrb, comp, "<=", cmp_le_m, ARGS_REQ(1)); /* 15.3.3.2.2 */
mrb_define_method(mrb, comp, "==", cmp_equal, ARGS_REQ(1)); /* 15.3.3.2.3 */
mrb_define_method(mrb, comp, ">", cmp_gt_m, ARGS_REQ(1)); /* 15.3.3.2.4 */
mrb_define_method(mrb, comp, ">=", cmp_ge_m, ARGS_REQ(1)); /* 15.3.3.2.5 */
mrb_define_method(mrb, comp, "between?", cmp_between, ARGS_REQ(2)); /* 15.3.3.2.6 */
mrb_define_module(mrb, "Comparable");
}

0 comments on commit 6a0c30d

Please sign in to comment.