source: git/Singular/bigintm.cc @ a04a05

spielwiese
Last change on this file since a04a05 was 762407, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
config.h is for sources files only FIX: config.h should only be used by source (not from inside kernel/mod2.h!) NOTE: each source file should better include mod2.h right after config.h, while headers should better not include mod2.h.
  • Property mode set to 100644
File size: 4.2 KB
Line 
1// as this file provides only example code,
2// no trace of it must appear in Singular and Singularg
3//
4#if 0
5//
6#include "config.h"
7#include <kernel/mod2.h>
8#include <Singular/ipid.h>
9#include <Singular/blackbox.h>
10#include <omalloc/omalloc.h>
11#include <kernel/febase.h>
12#include <kernel/longrat.h>
13#include <Singular/subexpr.h>
14#include <Singular/bigintm.h>
15
16
17static char * bigintm_String(blackbox *b, void *d)
18{ if (d==NULL) return omStrDup("oo");
19   else
20   {
21     StringSetS("");
22     number n=(number)d; nlWrite(n,NULL); d=(void*)n;
23     return omStrDup(StringAppendS(""));
24    }
25}
26static void * bigintm_Copy(blackbox*b, void *d)
27{  number n=(number)d; return nlCopy(n); }
28
29static BOOLEAN bigintm_Assign(leftv l, leftv r)
30{
31  blackbox *ll=getBlackboxStuff(l->Typ());
32 
33  if (r->Typ()>MAX_TOK)
34  {
35    if (l->Typ() == r->Typ())
36    {
37      blackbox *rr=getBlackboxStuff(r->Typ());
38     
39      if (l->Data()!=NULL) { number n1=(number)l->Data(); nlDelete(&n1,NULL); }
40      number n2=(number)r->CopyD();
41      if (l->rtyp==IDHDL)
42      {
43        IDDATA((idhdl)l->data)=(char *)n2;
44      }
45      else
46      {
47        l->data=(void *)n2;
48      }
49      return FALSE;
50    }
51    else
52    {
53      Werror("bigintm_Assign: assign %s (%d) = %s (%d)",
54             getBlackboxName(l->Typ()), l->Typ(),
55             getBlackboxName(r->Typ()), r->Typ());
56      return TRUE;
57    }
58  }
59  else if (r->Typ()==INT_CMD)
60  {
61    if (l->Data()!=NULL) { number n1=(number)l->Data(); nlDelete(&n1,NULL); }
62    number n2=nlInit((int)(long)r->Data(),NULL);
63    if (l->rtyp==IDHDL)
64    {
65      IDDATA((idhdl)l->data)=(char *)n2;
66    }
67    else
68    {
69      l->data=(void *)n2;
70    }
71    return FALSE;
72  }
73  else
74    Werror("assign %d = %d",l->Typ(),r->Typ());
75  return TRUE;
76}
77static BOOLEAN bigintm_OpM(int op, leftv res, leftv args);
78
79
80static BOOLEAN bigintm_Op2(int op, leftv res, leftv a1, leftv a2)
81{
82  // interpreter: a1 is ist bigintm
83 
84  blackbox *a=getBlackboxStuff(a1->Typ());
85  number n1=(number)a1->Data();
86  switch(op)
87  {
88    case '+':
89    {
90      if (a2->Typ()==INT_CMD)
91      {
92        number n2=nlInit((int)(long)a2->Data(),NULL);
93        number n=nlAdd(n1,n2);
94        res->data=(void *)n;
95        res->rtyp=a1->Typ();
96        return FALSE;
97      }
98      else if (a2->Typ()==a1->Typ())
99      {
100        number n2=(number)a2->Data();
101        number n=nlAdd(n1,n2);
102        res->data=(void *)n;
103        res->rtyp=a1->Typ();
104        return FALSE;
105      }
106      return TRUE;
107    }
108    default:
109      return blackboxDefaultOp2(op,res,a1,a2);
110  }
111  return blackboxDefaultOp2(op,res,a1,a2);
112}
113// BOOLEAN opM(int op, leftv res, leftv args)
114static BOOLEAN bigintm_OpM(int op, leftv res, leftv args)
115{
116  // interpreter: args->1. arg is ist bigintm
117  blackbox *a=getBlackboxStuff(args->Typ());
118  switch(op)
119  {
120    case STRING_CMD:
121    {
122      res->data=(void *)a->blackbox_String(a,args->Data());
123      res->rtyp=STRING_CMD;
124      return FALSE;
125    }
126    default:
127      return blackboxDefaultOpM(op,res,args);
128      break;
129  }
130  return blackboxDefaultOpM(op,res,args);
131}
132static void bigintm_destroy(blackbox *b, void *d)
133{
134  if (d!=NULL)
135  {
136    number n=(number)d;
137    nlDelete(&n,NULL);
138  }
139}
140
141
142BOOLEAN bigintm_setup()
143{
144  // it is not very usefull to call this setup routine twice,
145  // but (as this is an example) it is possible.
146  // Other blackbox type really require multiple setup calls
147  // (with different names).
148  // Also the format of this setup routine is not fixed,
149  // it may take additional parameters, it may return a value or not
150  // - it really depends.
151
152  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
153  // all undefined entries will be set to default in setBlackboxStuff
154  // the default Print is quite usefule,
155  // all other are simply error messages
156  b->blackbox_destroy=bigintm_destroy;
157  b->blackbox_String=bigintm_String;
158  //b->blackbox_Print=blackbox_default_Print;
159  //b->blackbox_Init=blackbox_default_Init;
160  b->blackbox_Copy=bigintm_Copy;
161  b->blackbox_Assign=bigintm_Assign;
162  //b->blackbox_Op1=blackboxDefaultOp1;
163  b->blackbox_Op2=bigintm_Op2;
164  //b->blackbox_Op3=blackbox_default_Op3;
165  b->blackbox_OpM=bigintm_OpM;
166  int rt=setBlackboxStuff(b,"bigintm");
167
168  Print("bigintm_setup: create type %d (%s)\n",rt, getBlackboxName(rt));
169
170  return FALSE; // ok, TRUE = error!: this example never fails
171}
172
173#endif
Note: See TracBrowser for help on using the repository browser.