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