source: git/Singular/bigintm.cc @ 60dcbbc

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