source: git/dyn_modules/bigintm/bigintm.cc @ 5cdbfe

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