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
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
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/>. */
26 #include <gpython/gpython.h>
27 #include <gpython/vectors.h>
28 #include <gpython/objects.h>
30 struct gpy_obj_integer_t
{
32 /* other possible members */
35 gpy_object_t
* gpy_obj_integer_new (gpy_typedef_t
* type
,
38 gpy_object_t
* retval
= NULL_OBJECT
;
40 bool check
= gpy_args_check_fmt (args
, "i.");
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
));
48 retval
= gpy_create_object_state (type
, self
);
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
*)
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
*)
72 fprintf (fd
, "%i ", x1
->Int
);
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
*)
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
);
109 fatal ("invalid object type <%s>!\n", y
->identifier
);
114 fatal ("invalid object type <%s>!\n", x
->identifier
);
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
);
139 fatal ("invalid object type <%s>!\n", y
->identifier
);
144 fatal ("invalid object type <%s>!\n", x
->identifier
);
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
);
169 fatal ("invalid object type <%s>!\n", y
->identifier
);
174 fatal ("invalid object type <%s>!\n", x
->identifier
);
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
;
201 retval
= gpy_rr_fold_integer (z
);
205 fatal ("invalid object type <%s>!\n", y
->identifier
);
210 fatal ("invalid object type <%s>!\n", x
->identifier
);
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
;
236 retval
= gpy_rr_fold_integer (z
);
240 fatal ("invalid object type <%s>!\n", y
->identifier
);
245 fatal ("invalid object type <%s>!\n", x
->identifier
);
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
;
271 retval
= gpy_rr_fold_integer (z
);
275 fatal ("invalid object type <%s>!\n", y
->identifier
);
280 fatal ("invalid object type <%s>!\n", x
->identifier
);
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
*)
299 struct gpy_obj_integer_t
* t2
= (struct gpy_obj_integer_t
*)
308 retval
= gpy_rr_fold_integer (z
);
312 fatal ("invalid object type <%s>!\n", y
->identifier
);
317 fatal ("invalid object type <%s>!\n", x
->identifier
);
322 bool gpy_obj_integer_eval_bool (gpy_object_t
* x
)
325 gpy_object_state_t
* t
= x
->o
.object_state
;
326 struct gpy_obj_integer_t
*state
= (struct gpy_obj_integer_t
*) t
->state
;
334 static struct gpy_number_prot_t integer_binary_ops
= {
335 &gpy_obj_integer_add
,
336 &gpy_obj_integer_minus
,
338 &gpy_obj_integer_mult
,
340 &gpy_obj_integer_less_than
,
342 &gpy_obj_integer_greater_than
,
344 &gpy_obj_integer_equal_to
,
345 &gpy_obj_integer_not_eq_to
,
350 static struct gpy_typedef_t integer_obj
= {
352 sizeof (struct gpy_obj_integer_t
),
353 &gpy_obj_integer_new
,
354 &gpy_obj_integer_destroy
,
355 &gpy_obj_integer_print
,
358 &gpy_obj_integer_eval_bool
,
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
);