added addition bin op and print hook again
[official-gcc.git] / libgpython / runtime / obj-integer.c
blobe27201afafd81868f538ca317f03b1909baf30bc
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 <gmp.h>
27 #include <mpfr.h>
29 #include <gpython/gpython.h>
30 #include <gpython/objects.h>
31 #include <gpython/vectors.h>
32 #include <gpython/garbage.h>
34 struct gpy_obj_integer_t {
35 int Int;
39 This Represents:
41 class foo:
42 def __init__ (self, ... )
45 Where program wise __init__ is called and the instance
46 of the object is created as gpy_object_state_t *
48 gpy_object_t * gpy_obj_integer_init (gpy_typedef_t * type,
49 gpy_object_t ** args)
51 gpy_object_t * retval = NULL_OBJECT;
53 bool check = gpy_args_check_fmt (args, "i");
54 gpy_assert(check);
56 int val = gpy_args_lit_parse_int (args[0]);
57 struct gpy_obj_integer_t * self = (struct gpy_obj_integer_t *)
58 gpy_malloc (sizeof(struct gpy_obj_integer_t));
59 self->Int = val;
61 retval = gpy_create_object_state (type,self);
63 return retval;
66 /* Destroys self (type) not the object state */
67 void gpy_obj_integer_destroy (gpy_object_t * self)
69 return;
72 void gpy_obj_integer_print (gpy_object_t * self, FILE * fd, bool newline)
74 gpy_assert (self->T == TYPE_OBJECT_STATE);
75 gpy_object_state_t * x = self->o.object_state;
76 struct gpy_obj_integer_t *x1 = (struct gpy_obj_integer_t *)
77 x->self;
79 fprintf (fd, "%i ", x1->Int);
81 if (newline)
82 fprintf (fd, "\n");
85 gpy_object_t *
86 gpy_obj_integer_whoop_noargs (gpy_object_t * self, gpy_object_t ** args )
88 printf("inside whoop function!\n\n");
89 return NULL_OBJECT;
92 gpy_object_t *
93 gpy_obj_integer_add (gpy_object_t * o1, gpy_object_t * o2)
95 gpy_object_t * retval = NULL_OBJECT;
97 debug ("Integer Addition!\n");
99 gpy_object_state_t * x = o1->o.object_state;
100 gpy_object_state_t * y = o2->o.object_state;
102 if( !strcmp( x->obj_t_ident, "Int" ) )
104 if( !strcmp( y->obj_t_ident, "Int") )
106 struct gpy_obj_integer_t *t1 = (struct gpy_obj_integer_t*) x->self;
107 struct gpy_obj_integer_t *t2 = (struct gpy_obj_integer_t*) y->self;
109 mpfr_t x,y,z;
110 mpfr_init( z );
111 mpfr_init_set_si( x, t1->Int, GMP_RNDU );
112 mpfr_init_set_si( y, t2->Int, GMP_RNDU );
114 if( mpfr_add( z, x, y, GMP_RNDU ) )
116 fatal("overflow in integer addition!\n");
119 retval = gpy_rr_fold_integer( mpfr_get_si( z, GMP_RNDU ) );
120 mpfr_clears( x, y, z, (mpfr_ptr)0 );
122 else
124 fatal("invalid object type <%s>!\n", y->obj_t_ident );
127 else
129 fatal("invalid object type <%s>!\n", x->obj_t_ident );
132 return retval;
136 The member methods table
137 - member fields could be handle'd in a similar fashion
139 static gpy_method_def_t gpy_obj_integer_methods[] = {
140 { "whoop_noargs", (gpy_builtin_callback__)
141 &gpy_obj_integer_whoop_noargs, METH_NOARGS },
142 { NULL, NULL, 0 }
145 /* The binary protocol handles */
146 static struct gpy_number_prot_t integer_binary_ops = {
147 true,
148 &gpy_obj_integer_add,
149 NULL,
150 NULL,
151 NULL,
152 NULL,
153 NULL,
154 NULL,
155 NULL,
156 NULL,
157 NULL,
158 NULL,
159 NULL,
160 NULL,
163 static struct gpy_typedef_t integer_obj = {
164 "Int",
165 sizeof(struct gpy_obj_integer_t),
166 gpy_obj_integer_init,
167 gpy_obj_integer_destroy,
168 gpy_obj_integer_print,
169 &integer_binary_ops,
170 gpy_obj_integer_methods
174 Should be used for handling any Field initilizers!
176 void gpy_obj_integer_mod_init( gpy_vector_t * const vec )
178 gpy_vec_push( vec, &integer_obj );