TADや、その他BTRON基本データ型を扱うmrbgemの開発用
Révision | 27b6a2155f0c87345d880ea239c3c0f709445ed4 (tree) |
---|---|
l'heure | 2017-06-23 17:46:36 |
Auteur | ornse01 <ornse01@user...> |
Commiter | ornse01 |
rouphly coding.
@@ -1,8 +1,192 @@ | ||
1 | 1 | #include "mruby.h" |
2 | 2 | |
3 | +const static struct mrb_data_type mrb_PNT_type = { "PNT", mrb_free }; | |
4 | + | |
5 | +LOCAL mrb_value | |
6 | +mrb_PNT_init(mrb_state *mrb, mrb_value self) | |
7 | +{ | |
8 | + PNT *p, arg = {0, 0}; | |
9 | + mrb_int x = 0, y = 0, argc; | |
10 | + | |
11 | + argc = mrb_get_args(mrb, "|ii", &x, &y); | |
12 | + if (argc >= 1) { | |
13 | + if (x < -32768 || 32767 < x) { | |
14 | + mrb_raise(mrb, E_ARGUMENT_ERROR, "Too large value."); | |
15 | + } | |
16 | + arg.x = x; | |
17 | + } | |
18 | + if (argc >= 2) { | |
19 | + if (y < -32768 || 32767 < y) { | |
20 | + mrb_raise(mrb, E_ARGUMENT_ERROR, "Too large value."); | |
21 | + } | |
22 | + arg.y = y; | |
23 | + } | |
24 | + | |
25 | + p = (PNT *)mrb_malloc(mrb, sizeof(PNT)); | |
26 | + p->x = arg.x; | |
27 | + p->y = arg.y; | |
28 | + | |
29 | + DATA_TYPE(self) = &mrb_PNT_type; | |
30 | + DATA_PTR(self) = h; | |
31 | + | |
32 | + return self; | |
33 | +} | |
34 | + | |
35 | +LOCAL mrb_value | |
36 | +mrb_PNT_get_x(mrb_state *mrb, mrb_value self) | |
37 | +{ | |
38 | + PNT *p; | |
39 | + | |
40 | + p = DATA_PTR(self); | |
41 | + return mrb_fixnum_value(p->x); | |
42 | +} | |
43 | + | |
44 | +LOCAL mrb_value | |
45 | +mrb_PNT_get_y(mrb_state *mrb, mrb_value self) | |
46 | +{ | |
47 | + PNT *p; | |
48 | + | |
49 | + p = DATA_PTR(self); | |
50 | + return mrb_fixnum_value(p->y); | |
51 | +} | |
52 | + | |
53 | +LOCAL mrb_value | |
54 | +mrb_PNT_set_x(mrb_state *mrb, mrb_value self) | |
55 | +{ | |
56 | + PNT *p; | |
57 | + mrb_int num; | |
58 | + | |
59 | + mrb_get_args(mrb, "i", &num); | |
60 | + p = DATA_PTR(self); | |
61 | + p->x = num; | |
62 | + return mrb_fixnum_value(num); | |
63 | +} | |
64 | + | |
65 | +LOCAL mrb_value | |
66 | +mrb_PNT_set_y(mrb_state *mrb, mrb_value self) | |
67 | +{ | |
68 | + PNT *p; | |
69 | + mrb_int num; | |
70 | + | |
71 | + mrb_get_args(mrb, "i", &num); | |
72 | + p = DATA_PTR(self); | |
73 | + p->y = num; | |
74 | + return mrb_fixnum_value(num); | |
75 | +} | |
76 | + | |
77 | +LOCAL mrb_value | |
78 | +mrb_PNT_init_copy(mrb_state *mrb, mrb_value copy) | |
79 | +{ | |
80 | + mrb_value src; | |
81 | + | |
82 | + mrb_get_args(mrb, "o", &src); | |
83 | + if (mrb_obj_equal(mrb, copy, src)) { | |
84 | + return copy; | |
85 | + } | |
86 | + if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) { | |
87 | + mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); | |
88 | + } | |
89 | + if (!DATA_PTR(copy)) { | |
90 | + DATA_PTR(copy) = (struct hoge *)mrb_malloc(mrb, sizeof(struct hoge)); | |
91 | + DATA_TYPE(copy) = &mrb_PNT_type; | |
92 | + } | |
93 | + *(PNT*)DATA_PTR(copy) = *(PNT*)DATA_PTR(src); | |
94 | + | |
95 | + return copy; | |
96 | +} | |
97 | + | |
98 | +LOCAL mrb_value | |
99 | +mrb_PNT_equal(mrb_state *mrb, mrb_value self) | |
100 | +{ | |
101 | + mrb_value src; | |
102 | + PNT *self_real, *other_real; | |
103 | + | |
104 | + mrb_get_args(mrb, "o", &other); | |
105 | + if (mrb_obj_equal(mrb, self, other)) { | |
106 | + return mrb_bool_value(FALSE); | |
107 | + } | |
108 | + if (!mrb_obj_is_instance_of(mrb, other, mrb_obj_class(mrb, copy))) { | |
109 | + return mrb_bool_value(FALSE); | |
110 | + } | |
111 | + if (!DATA_PTR(other)) { | |
112 | + return mrb_bool_value(FALSE); | |
113 | + } | |
114 | + | |
115 | + self_real = DATA_PTR(self); | |
116 | + other_real = DATA_PTR(other); | |
117 | + if ((self_real->x == other_real->x) && (self_real->y == other_real.y)) { | |
118 | + return mrb_bool_value(TRUE); | |
119 | + } | |
120 | + return mrb_bool_value(FALSE); | |
121 | +} | |
122 | + | |
123 | +LOCAL mrb_value | |
124 | +mrb_PNT_plus(mrb_state *mrb, mrb_value self) | |
125 | +{ | |
126 | + struct RClass *cls; | |
127 | + mrb_value src, result; | |
128 | + PNT *self_real, *other_real; | |
129 | + | |
130 | + cls = mrb_obj_class(mrb, self); | |
131 | + result = mrb_obj_value(cls); | |
132 | + | |
133 | + mrb_get_args(mrb, "o", &other); | |
134 | + if (!mrb_obj_is_instance_of(mrb, other, cls)) { | |
135 | + mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); | |
136 | + } | |
137 | + if (!DATA_PTR(other)) { | |
138 | + mrb_raise(mrb, E_TYPE_ERROR, "wrong argument value"); | |
139 | + } | |
140 | + | |
141 | + self_real = DATA_PTR(self); | |
142 | + other_real = DATA_PTR(other); | |
143 | + | |
144 | + return mrb_funcall(mrb, result, "new", 2, | |
145 | + mrb_fixnum_value(self_real->x + other_real->x), | |
146 | + mrb_fixnum_value(self_real->y + other_real->y)); | |
147 | +} | |
148 | + | |
149 | +LOCAL mrb_value | |
150 | +mrb_PNT_minus(mrb_state *mrb, mrb_value self) | |
151 | +{ | |
152 | + struct RClass *cls; | |
153 | + mrb_value src, result; | |
154 | + PNT *self_real, *other_real; | |
155 | + | |
156 | + cls = mrb_obj_class(mrb, self); | |
157 | + result = mrb_obj_value(cls); | |
158 | + | |
159 | + mrb_get_args(mrb, "o", &other); | |
160 | + if (!mrb_obj_is_instance_of(mrb, other, cls)) { | |
161 | + mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); | |
162 | + } | |
163 | + if (!DATA_PTR(other)) { | |
164 | + mrb_raise(mrb, E_TYPE_ERROR, "wrong argument value"); | |
165 | + } | |
166 | + | |
167 | + self_real = DATA_PTR(self); | |
168 | + other_real = DATA_PTR(other); | |
169 | + | |
170 | + return mrb_funcall(mrb, result, "new", 2, | |
171 | + mrb_fixnum_value(self_real->x - other_real->x), | |
172 | + mrb_fixnum_value(self_real->y - other_real->y)); | |
173 | +} | |
174 | + | |
3 | 175 | void |
4 | 176 | mrb_mruby_tad_gem_init(mrb_state* mrb) |
5 | 177 | { |
178 | + struct RClass *cls; | |
179 | + | |
180 | + cls = mrb_define_class(mrb, "PNT", mrb->object_class); | |
181 | + MRB_SET_INSTANCE_TT(cls, MRB_TT_DATA); | |
182 | + mrb_define_method(mrb, cls, "initialize", mrb_PNT_init, MRB_ARGS_OPT(2)); | |
183 | + mrb_define_method(mrb, cls, "x", mrb_PNT_get_x, MRB_ARGS_NONE()); | |
184 | + mrb_define_method(mrb, cls, "y", mrb_PNT_get_y, MRB_ARGS_NONE()); | |
185 | + mrb_define_method(mrb, cls, "put", mrb_PNT_set_x, MRB_ARGS_REQ(1)); | |
186 | + mrb_define_method(mrb, cls, "initialize_copy", mrb_PNT_init_copy, MRB_ARGS_REQ(1)); | |
187 | + mrb_define_method(mrb, cls, "==", mrb_PNT_equal, MRB_ARGS_REQ(1)); | |
188 | + mrb_define_method(mrb, cls, "+", mrb_PNT_plus, MRB_ARGS_REQ(1)); | |
189 | + mrb_define_method(mrb, cls, "-", mrb_PNT_minus, MRB_ARGS_REQ(1)); | |
6 | 190 | } |
7 | 191 | |
8 | 192 | void |