GCCPY:
[official-gcc.git] / libgpython / runtime / gpy-object-integer.c
blob5231b2694d50e08a8b7456a23da0ffef898db6d7
1 /* This file is part of GCC.
3 GCC is free software; you can redistribute it and/or modify it under
4 the terms of the GNU General Public License as published by the Free
5 Software Foundation; either version 3, or (at your option) any later
6 version.
8 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 for more details.
13 You should have received a copy of the GNU General Public License
14 along with GCC; see the file COPYING3. If not see
15 <http://www.gnu.org/licenses/>. */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
26 #include <gpython/gpython.h>
27 #include <gpython/vectors.h>
28 #include <gpython/objects.h>
30 struct gpy_obj_integer_t {
31 int Int;
32 /* other possible members */
35 gpy_object_t * gpy_obj_integer_new (gpy_typedef_t * type,
36 gpy_object_t ** args)
38 gpy_object_t * retval = NULL_OBJECT;
40 bool check = gpy_args_check_fmt (args, "i.");
41 gpy_assert (check);
43 int val = gpy_args_lit_parse_int (args[0]);
44 struct gpy_obj_integer_t * self = (struct gpy_obj_integer_t *)
45 gpy_malloc (sizeof (struct gpy_obj_integer_t));
46 self->Int = val;
48 retval = gpy_create_object_state (type, self);
50 return retval;
53 /* Destroys self (type) not the object state */
54 void gpy_obj_integer_destroy (gpy_object_t * self)
56 gpy_assert (self->T == TYPE_OBJECT_STATE);
57 gpy_object_state_t * x = self->o.object_state;
58 struct gpy_obj_integer_t *x1 = (struct gpy_obj_integer_t *)
59 x->state;
61 gpy_free (x1);
62 x->state = NULL;
65 void gpy_obj_integer_print (gpy_object_t * self, FILE * fd, bool newline)
67 gpy_assert (self->T == TYPE_OBJECT_STATE);
68 gpy_object_state_t * x = self->o.object_state;
69 struct gpy_obj_integer_t *x1 = (struct gpy_obj_integer_t *)
70 x->state;
72 fprintf (fd, "%i ", x1->Int);
74 if (newline)
75 fprintf (fd, "\n");
78 int gpy_obj_integer_getInt (gpy_object_t * self)
80 gpy_assert (self->T == TYPE_OBJECT_STATE);
81 gpy_object_state_t * x = self->o.object_state;
82 gpy_assert (!strcmp (x->definition->identifier, "Int"));
84 struct gpy_obj_integer_t *x1 = (struct gpy_obj_integer_t *)
85 x->state;
86 return (x1->Int);
89 gpy_object_t *
90 gpy_obj_integer_add (gpy_object_t * o1, gpy_object_t * o2)
92 gpy_object_t * retval = NULL_OBJECT;
94 gpy_object_state_t * x = o1->o.object_state;
95 gpy_object_state_t * y = o2->o.object_state;
97 if (!strcmp (x->identifier, "Int"))
99 if (!strcmp (y->identifier, "Int"))
101 struct gpy_obj_integer_t *t1 = (struct gpy_obj_integer_t*) x->state;
102 struct gpy_obj_integer_t *t2 = (struct gpy_obj_integer_t*) y->state;
104 int z = t1->Int + t2->Int;
105 retval = gpy_rr_fold_integer (z);
107 else
109 fatal ("invalid object type <%s>!\n", y->identifier);
112 else
114 fatal ("invalid object type <%s>!\n", x->identifier);
116 return retval;
119 gpy_object_t *
120 gpy_obj_integer_minus (gpy_object_t * o1, gpy_object_t * o2)
122 gpy_object_t * retval = NULL_OBJECT;
124 gpy_object_state_t * x = o1->o.object_state;
125 gpy_object_state_t * y = o2->o.object_state;
127 if (!strcmp (x->identifier, "Int"))
129 if (!strcmp (y->identifier, "Int"))
131 struct gpy_obj_integer_t *t1 = (struct gpy_obj_integer_t*) x->state;
132 struct gpy_obj_integer_t *t2 = (struct gpy_obj_integer_t*) y->state;
134 int z = t1->Int - t2->Int;
135 retval = gpy_rr_fold_integer (z);
137 else
139 fatal ("invalid object type <%s>!\n", y->identifier);
142 else
144 fatal ("invalid object type <%s>!\n", x->identifier);
146 return retval;
149 gpy_object_t *
150 gpy_obj_integer_mult (gpy_object_t * o1, gpy_object_t * o2)
152 gpy_object_t * retval = NULL_OBJECT;
154 gpy_object_state_t * x = o1->o.object_state;
155 gpy_object_state_t * y = o2->o.object_state;
157 if (!strcmp (x->identifier, "Int"))
159 if (!strcmp (y->identifier, "Int"))
161 struct gpy_obj_integer_t *t1 = (struct gpy_obj_integer_t*) x->state;
162 struct gpy_obj_integer_t *t2 = (struct gpy_obj_integer_t*) y->state;
164 int z = t1->Int * t2->Int;
165 retval = gpy_rr_fold_integer (z);
167 else
169 fatal ("invalid object type <%s>!\n", y->identifier);
172 else
174 fatal ("invalid object type <%s>!\n", x->identifier);
176 return retval;
180 gpy_object_t *
181 gpy_obj_integer_less_than (gpy_object_t * o1, gpy_object_t * o2)
183 gpy_object_t * retval = NULL_OBJECT;
185 gpy_object_state_t * x = o1->o.object_state;
186 gpy_object_state_t * y = o2->o.object_state;
188 if (!strcmp (x->identifier, "Int"))
190 if (!strcmp (y->identifier, "Int"))
192 struct gpy_obj_integer_t *t1 = (struct gpy_obj_integer_t*) x->state;
193 struct gpy_obj_integer_t *t2 = (struct gpy_obj_integer_t*) y->state;
195 int x = t1->Int;
196 int y = t2->Int;
197 int z = 0;
199 if (x < y)
200 z = 1;
201 retval = gpy_rr_fold_integer (z);
203 else
205 fatal ("invalid object type <%s>!\n", y->identifier);
208 else
210 fatal ("invalid object type <%s>!\n", x->identifier);
212 return retval;
215 gpy_object_t *
216 gpy_obj_integer_greater_than (gpy_object_t * o1, gpy_object_t * o2)
218 gpy_object_t * retval = NULL_OBJECT;
220 gpy_object_state_t * x = o1->o.object_state;
221 gpy_object_state_t * y = o2->o.object_state;
223 if (!strcmp (x->identifier, "Int"))
225 if (!strcmp (y->identifier, "Int"))
227 struct gpy_obj_integer_t *t1 = (struct gpy_obj_integer_t*) x->state;
228 struct gpy_obj_integer_t *t2 = (struct gpy_obj_integer_t*) y->state;
230 int x = t1->Int;
231 int y = t2->Int;
232 int z = 0;
234 if (x > y)
235 z = 1;
236 retval = gpy_rr_fold_integer (z);
238 else
240 fatal ("invalid object type <%s>!\n", y->identifier);
243 else
245 fatal ("invalid object type <%s>!\n", x->identifier);
247 return retval;
250 gpy_object_t *
251 gpy_obj_integer_equal_to (gpy_object_t * o1, gpy_object_t * o2)
253 gpy_object_t * retval = NULL_OBJECT;
255 gpy_object_state_t * x = o1->o.object_state;
256 gpy_object_state_t * y = o2->o.object_state;
258 if (!strcmp (x->identifier, "Int"))
260 if (!strcmp (y->identifier, "Int"))
262 struct gpy_obj_integer_t *t1 = (struct gpy_obj_integer_t*) x->state;
263 struct gpy_obj_integer_t *t2 = (struct gpy_obj_integer_t*) y->state;
265 int x = t1->Int;
266 int y = t2->Int;
267 int z = 0;
269 if (x == y)
270 z = 1;
271 retval = gpy_rr_fold_integer (z);
273 else
275 fatal ("invalid object type <%s>!\n", y->identifier);
278 else
280 fatal ("invalid object type <%s>!\n", x->identifier);
282 return retval;
285 gpy_object_t *
286 gpy_obj_integer_not_eq_to (gpy_object_t * o1, gpy_object_t * o2)
288 gpy_object_t * retval = NULL_OBJECT;
290 gpy_object_state_t * x = o1->o.object_state;
291 gpy_object_state_t * y = o2->o.object_state;
293 if (!strcmp (x->identifier, "Int"))
295 if (!strcmp (y->identifier, "Int"))
297 struct gpy_obj_integer_t * t1 = (struct gpy_obj_integer_t *)
298 x->state;
299 struct gpy_obj_integer_t * t2 = (struct gpy_obj_integer_t *)
300 y->state;
302 int x = t1->Int;
303 int y = t2->Int;
304 int z = 0;
306 if (x != y)
307 z = 1;
308 retval = gpy_rr_fold_integer (z);
310 else
312 fatal ("invalid object type <%s>!\n", y->identifier);
315 else
317 fatal ("invalid object type <%s>!\n", x->identifier);
319 return retval;
322 bool gpy_obj_integer_eval_bool (gpy_object_t * x)
324 bool retval = false;
325 gpy_object_state_t * t = x->o.object_state;
326 struct gpy_obj_integer_t *state = (struct gpy_obj_integer_t*) t->state;
328 if (state->Int)
329 retval = true;
331 return retval;
334 static struct gpy_number_prot_t integer_binary_ops = {
335 &gpy_obj_integer_add,
336 &gpy_obj_integer_minus,
337 NULL,
338 &gpy_obj_integer_mult,
339 NULL,
340 &gpy_obj_integer_less_than,
341 NULL,
342 &gpy_obj_integer_greater_than,
343 NULL,
344 &gpy_obj_integer_equal_to,
345 &gpy_obj_integer_not_eq_to,
346 NULL,
347 NULL,
350 static struct gpy_typedef_t integer_obj = {
351 "Int",
352 sizeof (struct gpy_obj_integer_t),
353 &gpy_obj_integer_new,
354 &gpy_obj_integer_destroy,
355 &gpy_obj_integer_print,
356 NULL,
357 NULL,
358 &gpy_obj_integer_eval_bool,
359 &integer_binary_ops,
360 NULL
364 Should be used for handling any Field initilizers!
366 void gpy_obj_integer_mod_init (gpy_vector_t * const vec)
368 gpy_vec_push (vec, &integer_obj);