1 | #include <kernel/mod2.h> |
---|
2 | |
---|
3 | #include <omalloc/omalloc.h> |
---|
4 | #include <coeffs/coeffs.h> |
---|
5 | #include <kernel/longrat.h> |
---|
6 | |
---|
7 | #include <kernel/febase.h> |
---|
8 | |
---|
9 | #include <Singular/ipid.h> |
---|
10 | #include <Singular/subexpr.h> |
---|
11 | #include <Singular/tok.h> |
---|
12 | #include <Singular/blackbox.h> |
---|
13 | #include <Singular/ipshell.h> |
---|
14 | |
---|
15 | #include <Singular/ipid.h> |
---|
16 | // extern coeffs coeffs_BIGINT |
---|
17 | |
---|
18 | |
---|
19 | #include "bigintm.h" |
---|
20 | |
---|
21 | |
---|
22 | #define HAVE_BIGINTM 1 |
---|
23 | |
---|
24 | namespace |
---|
25 | { |
---|
26 | |
---|
27 | #ifdef HAVE_BIGINTM |
---|
28 | static int bigintm_type_id = -1; |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifdef HAVE_BIGINTM |
---|
32 | static char * bigintm_String(blackbox *b, void *d) |
---|
33 | { if (d==NULL) return omStrDup("oo"); |
---|
34 | else |
---|
35 | { |
---|
36 | StringSetS(""); |
---|
37 | number n=(number)d; n_Write(n, coeffs_BIGINT); d=(void*)n; |
---|
38 | return omStrDup(StringAppendS("")); |
---|
39 | } |
---|
40 | } |
---|
41 | static void * bigintm_Copy(blackbox*b, void *d) |
---|
42 | { number n=(number)d; return n_Copy(n, coeffs_BIGINT); } |
---|
43 | |
---|
44 | static BOOLEAN bigintm_Assign(leftv l, leftv r) |
---|
45 | { |
---|
46 | assume( l->Typ() == bigintm_type_id ); |
---|
47 | |
---|
48 | blackbox *ll=getBlackboxStuff(l->Typ()); |
---|
49 | |
---|
50 | if (r->Typ()>MAX_TOK) |
---|
51 | { |
---|
52 | if (bigintm_type_id == r->Typ()) |
---|
53 | { |
---|
54 | blackbox *rr=getBlackboxStuff(r->Typ()); |
---|
55 | |
---|
56 | if (l->Data()!=NULL) { number n1=(number)l->Data(); n_Delete(&n1,coeffs_BIGINT); } |
---|
57 | number n2=(number)r->CopyD(); |
---|
58 | if (l->rtyp==IDHDL) |
---|
59 | { |
---|
60 | IDDATA((idhdl)l->data)=(char *)n2; |
---|
61 | } |
---|
62 | else |
---|
63 | { |
---|
64 | l->data=(void *)n2; |
---|
65 | } |
---|
66 | return FALSE; |
---|
67 | } |
---|
68 | else |
---|
69 | { |
---|
70 | Werror("bigintm_Assign: assign %s (%d) = %s (%d)", |
---|
71 | getBlackboxName(l->Typ()), l->Typ(), |
---|
72 | getBlackboxName(r->Typ()), r->Typ()); |
---|
73 | return TRUE; |
---|
74 | } |
---|
75 | } |
---|
76 | else if (r->Typ()==INT_CMD) |
---|
77 | { |
---|
78 | if (l->Data()!=NULL) { number n1=(number)l->Data(); n_Delete(&n1,coeffs_BIGINT); } |
---|
79 | number n2=n_Init((int)(long)r->Data(),coeffs_BIGINT); |
---|
80 | if (l->rtyp==IDHDL) |
---|
81 | { |
---|
82 | IDDATA((idhdl)l->data)=(char *)n2; |
---|
83 | } |
---|
84 | else |
---|
85 | { |
---|
86 | l->data=(void *)n2; |
---|
87 | } |
---|
88 | return FALSE; |
---|
89 | } |
---|
90 | else |
---|
91 | Werror("assign %d = %d",l->Typ(),r->Typ()); |
---|
92 | |
---|
93 | return TRUE; |
---|
94 | } |
---|
95 | |
---|
96 | BOOLEAN bigintm_Op1(int op,leftv l, leftv r) |
---|
97 | { |
---|
98 | // interpreter: a1 is ist bigintm |
---|
99 | assume( r->Typ() == bigintm_type_id ); |
---|
100 | /* |
---|
101 | // "typeof( <blackbox> )" is handled by 'blackboxDefaultOp1' |
---|
102 | if (op==TYPEOF_CMD) |
---|
103 | { |
---|
104 | l->data=omStrDup(getBlackboxName(r->Typ())); |
---|
105 | l->rtyp=STRING_CMD; |
---|
106 | return FALSE; |
---|
107 | } |
---|
108 | */ |
---|
109 | |
---|
110 | if( op=='(' ) // <bigintm> VAR(); |
---|
111 | { |
---|
112 | Werror("bigintm_Op1: What do you mean by '<bigintm>()'?!"); |
---|
113 | return TRUE; |
---|
114 | } |
---|
115 | |
---|
116 | return blackboxDefaultOp1(op, l, r); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | static BOOLEAN bigintm_OpM(int op, leftv res, leftv args); |
---|
121 | |
---|
122 | |
---|
123 | static BOOLEAN bigintm_Op2(int op, leftv res, leftv a1, leftv a2) |
---|
124 | { |
---|
125 | // interpreter: a1 is ist bigintm |
---|
126 | assume( a1->Typ() == bigintm_type_id ); |
---|
127 | |
---|
128 | blackbox *a=getBlackboxStuff(a1->Typ()); |
---|
129 | number n1=(number)a1->Data(); |
---|
130 | switch(op) |
---|
131 | { |
---|
132 | case '+': |
---|
133 | { |
---|
134 | if (a2->Typ()==INT_CMD) |
---|
135 | { |
---|
136 | number n2=n_Init((int)(long)a2->Data(), coeffs_BIGINT); |
---|
137 | number n=n_Add(n1,n2, coeffs_BIGINT); |
---|
138 | res->data=(void *)n; |
---|
139 | res->rtyp=a1->Typ(); |
---|
140 | return FALSE; |
---|
141 | } |
---|
142 | else if (a2->Typ()==a1->Typ()) |
---|
143 | { |
---|
144 | number n2=(number)a2->Data(); |
---|
145 | number n=n_Add(n1,n2, coeffs_BIGINT); |
---|
146 | res->data=(void *)n; |
---|
147 | res->rtyp=a1->Typ(); |
---|
148 | return FALSE; |
---|
149 | } |
---|
150 | |
---|
151 | Werror("bigintm_Op2: Op: '+': Sorry unsupported 2nd argument-type: %s in", Tok2Cmdname(a2->Typ())); |
---|
152 | return WrongOp("bigintm_Op2", op, a1); |
---|
153 | } |
---|
154 | |
---|
155 | case '-': |
---|
156 | { |
---|
157 | if (a2->Typ()==INT_CMD) |
---|
158 | { |
---|
159 | number n2=n_Init((int)(long)a2->Data(),coeffs_BIGINT); |
---|
160 | number n=n_Sub(n1,n2, coeffs_BIGINT); |
---|
161 | res->data=(void *)n; |
---|
162 | res->rtyp=a1->Typ(); |
---|
163 | return FALSE; |
---|
164 | } |
---|
165 | else if (a2->Typ()==a1->Typ()) |
---|
166 | { |
---|
167 | number n2=(number)a2->Data(); |
---|
168 | number n=n_Sub(n1,n2, coeffs_BIGINT); |
---|
169 | res->data=(void *)n; |
---|
170 | res->rtyp=a1->Typ(); |
---|
171 | return FALSE; |
---|
172 | } |
---|
173 | |
---|
174 | Werror("bigintm_Op2: Op: '-': Sorry unsupported 2nd argument-type: %s in", Tok2Cmdname(a2->Typ())); |
---|
175 | WrongOp("bigintm_Op2", op, a1); |
---|
176 | return TRUE; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | case '*': |
---|
181 | { |
---|
182 | if (a2->Typ()==INT_CMD) |
---|
183 | { |
---|
184 | number n2=n_Init((int)(long)a2->Data(), coeffs_BIGINT); |
---|
185 | number n=n_Mult(n1,n2, coeffs_BIGINT); |
---|
186 | res->data=(void *)n; |
---|
187 | res->rtyp=a1->Typ(); |
---|
188 | return FALSE; |
---|
189 | } |
---|
190 | else if (a2->Typ()==a1->Typ()) |
---|
191 | { |
---|
192 | number n2=(number)a2->Data(); |
---|
193 | number n=n_Mult(n1,n2, coeffs_BIGINT); |
---|
194 | res->data=(void *)n; |
---|
195 | res->rtyp=a1->Typ(); |
---|
196 | return FALSE; |
---|
197 | } |
---|
198 | Werror("bigintm_Op2: Op: '*': Sorry unsupported 2nd argument-type: '%s' in", Tok2Cmdname(a2->Typ())); |
---|
199 | WrongOp("bigintm_Op2", op, a1); |
---|
200 | return TRUE; |
---|
201 | } |
---|
202 | /* |
---|
203 | /// TODO: Why is this ignored???! |
---|
204 | case '(' : // <bigintm> VAR(b); |
---|
205 | { |
---|
206 | Werror("bigintm_Op2: What du you mean by '<bigintm>(...%s...)'?!", Tok2Cmdname(a2->Typ())); |
---|
207 | return TRUE; |
---|
208 | } |
---|
209 | */ |
---|
210 | case EQUAL_EQUAL: |
---|
211 | { |
---|
212 | if( a1 == a2) |
---|
213 | { |
---|
214 | res->data= (void *) (TRUE); |
---|
215 | res->rtyp= INT_CMD; |
---|
216 | return FALSE; |
---|
217 | } else |
---|
218 | if (a2->Typ()==INT_CMD) |
---|
219 | { |
---|
220 | number n2=n_Init((int)(long)a2->Data(), coeffs_BIGINT); |
---|
221 | res->data=(void *) n_Equal(n1,n2, coeffs_BIGINT); |
---|
222 | res->rtyp= INT_CMD; |
---|
223 | return FALSE; |
---|
224 | } |
---|
225 | else if (a2->Typ()==a1->Typ()) |
---|
226 | { |
---|
227 | number n2=(number)a2->Data(); |
---|
228 | res->data=(void *) n_Equal(n1,n2, coeffs_BIGINT); |
---|
229 | res->rtyp= INT_CMD; |
---|
230 | return FALSE; |
---|
231 | } |
---|
232 | |
---|
233 | Werror("bigintm_Op2: Op: '==': Sorry unsupported 2nd argument-type: '%d' in", Tok2Cmdname(a2->Typ())); |
---|
234 | WrongOp("bigintm_Op2", op, a1); |
---|
235 | return TRUE; |
---|
236 | } |
---|
237 | |
---|
238 | case '.': |
---|
239 | { |
---|
240 | |
---|
241 | if (a2->name==NULL) |
---|
242 | { |
---|
243 | Werror("bigintm_Op2: Op: '.': 2nd argument-type: '%s'(%d) should be a NAME", Tok2Cmdname(a2->Typ()), a2->Typ()); |
---|
244 | return TRUE; |
---|
245 | } |
---|
246 | |
---|
247 | Werror("bigintm_Op2: Op: '.': 2nd argument-type: '%s'(%d) is called '%s' in ", Tok2Cmdname(a2->Typ()), a2->Typ(), a2->name); |
---|
248 | return blackboxDefaultOp2(op,res,a1,a2); |
---|
249 | return TRUE; |
---|
250 | } |
---|
251 | |
---|
252 | default: |
---|
253 | { |
---|
254 | WrongOp("bigintm_Op2", op, a1); |
---|
255 | return blackboxDefaultOp2(op,res,a1,a2); |
---|
256 | break; |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | // BOOLEAN opM(int op, leftv res, leftv args) |
---|
261 | static BOOLEAN bigintm_OpM(int op, leftv res, leftv args) |
---|
262 | { |
---|
263 | // interpreter: args->1. arg is ist bigintm |
---|
264 | assume( args->Typ() == bigintm_type_id ); |
---|
265 | blackbox *a=getBlackboxStuff(args->Typ()); |
---|
266 | switch(op) |
---|
267 | { |
---|
268 | case STRING_CMD: |
---|
269 | { |
---|
270 | res->data=(void *)a->blackbox_String(a,args->Data()); |
---|
271 | res->rtyp=STRING_CMD; |
---|
272 | return FALSE; |
---|
273 | } |
---|
274 | |
---|
275 | /// TODO: Why is this used for ALL the cases: even for "a(1)" ???! |
---|
276 | case '(' : // <bigintm> VAR(b,...); |
---|
277 | { |
---|
278 | Werror("bigintm_OpM: What do you mean by '<bigintm>(...)'?!"); |
---|
279 | return TRUE; |
---|
280 | } |
---|
281 | |
---|
282 | default: |
---|
283 | WrongOp("bigintm_OpM", op, args); |
---|
284 | break; |
---|
285 | } |
---|
286 | return blackbox_default_OpM(op, res, args); |
---|
287 | } |
---|
288 | |
---|
289 | static void bigintm_destroy(blackbox *b, void *d) |
---|
290 | { |
---|
291 | if (d!=NULL) |
---|
292 | { |
---|
293 | number n=(number)d; |
---|
294 | n_Delete(&n, coeffs_BIGINT); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | #endif |
---|
299 | |
---|
300 | } |
---|
301 | |
---|
302 | // this is only a demo |
---|
303 | BOOLEAN bigintm_setup() |
---|
304 | { |
---|
305 | #ifndef HAVE_BIGINTM |
---|
306 | Werror("bigintm_setup: Sorry BIGINTM was not compiled in!"); |
---|
307 | return TRUE; // ok, TRUE = error! |
---|
308 | #else |
---|
309 | |
---|
310 | if( bigintm_type_id == -1 ) |
---|
311 | { |
---|
312 | blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox)); |
---|
313 | // all undefined entries will be set to default in setBlackboxStuff |
---|
314 | // the default Print is quite usefule, |
---|
315 | // all other are simply error messages |
---|
316 | b->blackbox_destroy=bigintm_destroy; |
---|
317 | b->blackbox_String=bigintm_String; |
---|
318 | //b->blackbox_Print=blackbox_default_Print; |
---|
319 | //b->blackbox_Init=blackbox_default_Init; |
---|
320 | b->blackbox_Copy=bigintm_Copy; |
---|
321 | b->blackbox_Assign=bigintm_Assign; // TO ASK: no default?! |
---|
322 | b->blackbox_Op1=bigintm_Op1; |
---|
323 | b->blackbox_Op2=bigintm_Op2; |
---|
324 | //b->blackbox_Op3=blackbox_default_Op3; |
---|
325 | b->blackbox_OpM=bigintm_OpM; |
---|
326 | |
---|
327 | bigintm_type_id = setBlackboxStuff(b,"bigintm"); |
---|
328 | |
---|
329 | Print("bigintm_setup: created a blackbox type [%d] '%s'",bigintm_type_id, getBlackboxName(bigintm_type_id)); |
---|
330 | PrintLn(); |
---|
331 | |
---|
332 | return FALSE; // ok, TRUE = error! |
---|
333 | } else |
---|
334 | { |
---|
335 | Werror("bigintm_setup: Sorry should NOT be run twice!"); |
---|
336 | return TRUE; // ok, TRUE = error! |
---|
337 | } |
---|
338 | |
---|
339 | #endif |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | |
---|