Skip to content
Newer
Older
100644 49 lines (43 sloc) 1.01 KB
8354436 add file header
mimaki authored Apr 23, 2012
1 /*
2 ** compar.c - Comparable module
4ec6d41 rm whitespace
roco authored Apr 30, 2012
3 **
8354436 add file header
mimaki authored Apr 23, 2012
4 ** See Copyright Notice in mruby.h
5 */
6
e0d6430 add mruby sources
mimaki authored Apr 20, 2012
7 #include "mruby.h"
8 #include "mruby/string.h"
9 #include "mruby/numeric.h"
10
11 void
12 mrb_cmperr(mrb_state *mrb, mrb_value x, mrb_value y)
13 {
14 const char *classname;
15
16 if (SPECIAL_CONST_P(y)) {
17 y = mrb_inspect(mrb, y);
18 classname = mrb_string_value_ptr(mrb, y);
19 }
20 else {
21 classname = mrb_obj_classname(mrb, y);
22 }
23 mrb_raise(mrb, E_ARGUMENT_ERROR, "comparison of %s with %s failed",
4ec6d41 rm whitespace
roco authored Apr 30, 2012
24 mrb_obj_classname(mrb, x), classname);
e0d6430 add mruby sources
mimaki authored Apr 20, 2012
25 }
26
27 int
28 mrb_cmpint(mrb_state *mrb, mrb_value val, mrb_value a, mrb_value b)
29 {
30 if (mrb_nil_p(val)) {
31 mrb_cmperr(mrb, a, b);
32 }
33 if (FIXNUM_P(val)) {
34 long l = mrb_fixnum(val);
35 if (l > 0) return 1;
36 if (l < 0) return -1;
37 return 0;
38 }
39 if (mrb_test(mrb_funcall(mrb, val, ">", 1, mrb_fixnum_value(0)))) return 1;
40 if (mrb_test(mrb_funcall(mrb, val, "<", 1, mrb_fixnum_value(0)))) return -1;
41 return 0;
42 }
43
44 void
45 mrb_init_comparable(mrb_state *mrb)
46 {
6a0c30d @matz move body of Comparison from compar.c to compar.rb
matz authored May 9, 2012
47 mrb_define_module(mrb, "Comparable");
e0d6430 add mruby sources
mimaki authored Apr 20, 2012
48 }
Something went wrong with that request. Please try again.