source: git/dyn_modules/bigintm/bigintm.cc @ 92e0eb

spielwiese
Last change on this file since 92e0eb was e6d9a32, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
ADD: bigintm as a module sample ADD: public blackbox_default_OpM ADD: more commands/ops From: Oleksandr Motsak <motsak@mathematik.uni-kl.de> git-svn-id: file:///usr/local/Singular/svn/trunk@13940 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.0 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  if (op==TYPEOF_CMD)
110  {
111    l->data=omStrDup(getBlackboxName(r->Typ()));
112    l->rtyp=STRING_CMD;
113    return FALSE;
114  }
115 
116  if( op=='(' ) // <bigintm>  VAR();
117  {
118    Werror("bigintm_Op1: What du you mean by '<bigintm>()'?!");
119    return TRUE; 
120  }
121
122  WrongOp("bigintm_Op1", op, r);
123  return blackboxDefaultOp1(op, l, r);
124}
125
126
127static BOOLEAN bigintm_OpM(int op, leftv res, leftv args);
128
129
130static BOOLEAN bigintm_Op2(int op, leftv res, leftv a1, leftv a2)
131{
132  // interpreter: a1 is ist bigintm
133  assume( a1->Typ() == bigintm_type_id );
134 
135  blackbox *a=getBlackboxStuff(a1->Typ());
136  number n1=(number)a1->Data(); 
137  switch(op)
138  {
139    case '+':
140    {
141      if (a2->Typ()==INT_CMD)
142      {
143        number n2=nlInit((int)(long)a2->Data(),NULL);
144        number n=nlAdd(n1,n2);
145        res->data=(void *)n;
146        res->rtyp=a1->Typ();
147        return FALSE;
148      }
149      else if (a2->Typ()==a1->Typ())
150      {
151        number n2=(number)a2->Data(); 
152        number n=nlAdd(n1,n2);
153        res->data=(void *)n;
154        res->rtyp=a1->Typ();
155        return FALSE;
156      }
157
158      Werror("bigintm_Op2: Op: '+': Sorry unsupported 2nd argument-type: %s in", Tok2Cmdname(a2->Typ()));
159      WrongOp("bigintm_Op2", op, a1);
160      return TRUE;
161    }
162
163    case '-':
164    {
165      if (a2->Typ()==INT_CMD)
166      {
167        number n2=nlInit((int)(long)a2->Data(),NULL);
168        number n=nlSub(n1,n2);
169        res->data=(void *)n;
170        res->rtyp=a1->Typ();
171        return FALSE;
172      }
173      else if (a2->Typ()==a1->Typ())
174      {
175        number n2=(number)a2->Data(); 
176        number n=nlSub(n1,n2);
177        res->data=(void *)n;
178        res->rtyp=a1->Typ();
179        return FALSE;
180      }
181     
182      Werror("bigintm_Op2: Op: '-': Sorry unsupported 2nd argument-type: %s in", Tok2Cmdname(a2->Typ()));
183      WrongOp("bigintm_Op2", op, a1);
184      return TRUE;
185    }
186
187
188    case '*':
189    {
190      if (a2->Typ()==INT_CMD)
191      {
192        number n2=nlInit((int)(long)a2->Data(),NULL);
193        number n=nlMult(n1,n2);
194        res->data=(void *)n;
195        res->rtyp=a1->Typ();
196        return FALSE;
197      }
198      else if (a2->Typ()==a1->Typ())
199      {
200        number n2=(number)a2->Data(); 
201        number n=nlMult(n1,n2);
202        res->data=(void *)n;
203        res->rtyp=a1->Typ();
204        return FALSE;
205      }
206      Werror("bigintm_Op2: Op: '*': Sorry unsupported 2nd argument-type: '%s' in", Tok2Cmdname(a2->Typ()));
207      WrongOp("bigintm_Op2", op, a1);
208      return TRUE;
209    }
210/*
211    /// TODO: Why is this ignored???!
212    case '(' : // <bigintm>  VAR(b);
213    {
214      Werror("bigintm_Op2: What du you mean by '<bigintm>(...%s...)'?!", Tok2Cmdname(a2->Typ()));
215      return TRUE; 
216    }
217*/
218    case EQUAL_EQUAL:
219    {
220      if( a1 == a2)
221      {
222        res->data= (void *) (TRUE);
223        res->rtyp= INT_CMD;
224        return FALSE;
225      } else
226      if (a2->Typ()==INT_CMD)
227      {
228        number n2=nlInit((int)(long)a2->Data(),NULL);
229        res->data=(void *) nlEqual(n1,n2);
230        res->rtyp= INT_CMD;
231        return FALSE;
232      }
233      else if (a2->Typ()==a1->Typ())
234      {
235        number n2=(number)a2->Data(); 
236        res->data=(void *) nlEqual(n1,n2);
237        res->rtyp= INT_CMD;
238        return FALSE;
239      }
240
241      Werror("bigintm_Op2: Op: '==': Sorry unsupported 2nd argument-type: '%d' in", Tok2Cmdname(a2->Typ()));
242      WrongOp("bigintm_Op2", op, a1);
243      return TRUE;
244    }
245
246    case '.':
247    {
248      Werror("bigintm_Op2: Op: '.': 2nd argument-type: '%s'(%d) in", Tok2Cmdname(a2->Typ()), a2->Typ());     
249      return blackboxDefaultOp2(op,res,a1,a2);
250      return TRUE;
251    }
252
253    default:
254    {
255      WrongOp("bigintm_Op2", op, a1);
256      return blackboxDefaultOp2(op,res,a1,a2);
257      break;
258    }
259  }
260}
261// BOOLEAN opM(int op, leftv res, leftv args)
262static BOOLEAN bigintm_OpM(int op, leftv res, leftv args)
263{
264  // interpreter: args->1. arg is ist bigintm
265  assume( args->Typ() == bigintm_type_id );
266  blackbox *a=getBlackboxStuff(args->Typ());
267  switch(op)
268  {
269    case STRING_CMD:
270    {
271      res->data=(void *)a->blackbox_String(a,args->Data());
272      res->rtyp=STRING_CMD;
273      return FALSE;
274    }
275
276    /// TODO: Why is this used for ALL the cases: even for "a(1)"  ???!
277    case '(' : // <bigintm>  VAR(b,...);
278    {
279      Werror("bigintm_OpM: What du you mean by '<bigintm>(...)'?!");
280      return TRUE; 
281    }
282   
283    default:
284      WrongOp("bigintm_OpM", op, args);
285      break;
286  }
287  return blackbox_default_OpM(op, res, args);
288}
289
290static void bigintm_destroy(blackbox *b, void *d)
291{
292  if (d!=NULL)
293  {
294    number n=(number)d;
295    nlDelete(&n,NULL);
296  }
297}
298
299#endif
300
301
302
303
304// this is only a demo
305BOOLEAN bigintm_setup()
306{
307#ifndef HAVE_BIGINTM
308  Werror("bigintm_setup: Sorry BIGINTM was not compiled in!");
309  return TRUE; // ok, TRUE = error!
310#else
311
312  if( bigintm_type_id == -1 )
313  {
314    blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
315    // all undefined entries will be set to default in setBlackboxStuff
316    // the default Print is quite usefule,
317    // all other are simply error messages
318    b->blackbox_destroy=bigintm_destroy;
319    b->blackbox_String=bigintm_String;
320    //b->blackbox_Print=blackbox_default_Print;
321    //b->blackbox_Init=blackbox_default_Init;
322    b->blackbox_Copy=bigintm_Copy;
323    b->blackbox_Assign=bigintm_Assign; // TO ASK: no default?!
324    b->blackbox_Op1=bigintm_Op1;
325    b->blackbox_Op2=bigintm_Op2;
326    //b->blackbox_Op3=blackbox_default_Op3;
327    b->blackbox_OpM=bigintm_OpM;
328
329    bigintm_type_id = setBlackboxStuff(b,"bigintm");
330
331    Print("bigintm_setup: created a blackbox type [%d] '%s'",bigintm_type_id, getBlackboxName(bigintm_type_id));
332    PrintLn();
333
334    return FALSE; // ok, TRUE = error!
335  } else
336  {
337    Werror("bigintm_setup: Sorry should NOT be run twice!");
338    return TRUE; // ok, TRUE = error!
339  }
340
341#endif
342}
343
344
345
Note: See TracBrowser for help on using the repository browser.