source: git/Singular/bigintm.cc @ 317d77f

spielwiese
Last change on this file since 317d77f was d315d69, checked in by Hans Schoenemann <hannes@…>, 13 years ago
re-introduce the bigintm example git-svn-id: file:///usr/local/Singular/svn/trunk@13874 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 3.8 KB
Line 
1#include <Singular/mod2.h>
2#include <Singular/ipid.h>
3#include <Singular/blackbox.h>
4#include <omalloc/omalloc.h>
5#include <kernel/febase.h>
6#include <kernel/longrat.h>
7#include <Singular/subexpr.h>
8#include <Singular/bigintm.h>
9
10// as this is only a demo,
11// we do not included compiled code:
12#if 0
13char * bigintm_String(blackbox *b, void *d);
14void * bigintm_Copy(blackbox*b, void *d);
15BOOLEAN bigintm_Assign(leftv l, leftv r);
16BOOLEAN bigintm_Op2(int op, leftv res, leftv a1, leftv a2);
17BOOLEAN bigintm_OpM(int op, leftv res, leftv args);
18void bigintm_destroy(blackbox *b, void *d);
19
20void bigintm_setup()
21{
22  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
23  // all undefined entries will be set to default in setBlackboxStuff
24  // the default Print is quite usefule,
25  // all other are simply error messages
26  b->blackbox_destroy=bigintm_destroy;
27  b->blackbox_String=bigintm_String;
28  //b->blackbox_Print=blackbox_default_Print;
29  //b->blackbox_Init=blackbox_default_Init;
30  b->blackbox_Copy=bigintm_Copy;
31  b->blackbox_Assign=bigintm_Assign;
32  //b->blackbox_Op1=blackboxDefaultOp1;
33  b->blackbox_Op2=bigintm_Op2;
34  //b->blackbox_Op3=blackbox_default_Op3;
35  b->blackbox_OpM=bigintm_OpM;
36  int rt=setBlackboxStuff(b,"bigintm");
37  Print("create type %d (bigintm)\n",rt);
38}
39
40char * bigintm_String(blackbox *b, void *d)
41{ if (d==NULL) return omStrDup("oo");
42   else
43   {
44     StringSetS("");
45     number n=(number)d; nlWrite(n,NULL); d=(void*)n;
46     return omStrDup(StringAppendS(""));
47    }
48}
49void * bigintm_Copy(blackbox*b, void *d)
50{  number n=(number)d; return nlCopy(n); }
51
52BOOLEAN bigintm_Assign(leftv l, leftv r)
53{
54  blackbox *ll=getBlackboxStuff(l->Typ());
55  if (r->Typ()>MAX_TOK)
56  {
57    blackbox *rr=getBlackboxStuff(r->Typ());
58    if (l->Typ()==r->Typ())
59    {
60      if (l->Data()!=NULL) { number n1=(number)l->Data(); nlDelete(&n1,NULL); }
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("assign %d = %d",l->Typ(),r->Typ());
75      return TRUE;
76    }
77  }
78  else if (r->Typ()==INT_CMD)
79  {
80    if (l->Data()!=NULL) { number n1=(number)l->Data(); nlDelete(&n1,NULL); }
81    number n2=nlInit((int)(long)r->Data(),NULL);
82    if (l->rtyp==IDHDL)
83    {
84      IDDATA((idhdl)l->data)=(char *)n2;
85    }
86    else
87    {
88      l->data=(void *)n2;
89    }
90    return FALSE;
91  }
92  else
93    Werror("assign %d = %d",l->Typ(),r->Typ());
94  return TRUE;
95}
96BOOLEAN bigintm_OpM(int op, leftv res, leftv args);
97
98
99BOOLEAN bigintm_Op2(int op, leftv res, leftv a1, leftv a2)
100{
101  // interpreter: a1 is ist bigintm
102  blackbox *a=getBlackboxStuff(a1->Typ());
103  number n1=(number)a1->Data();
104  switch(op)
105  {
106    case '+':
107    {
108      if (a2->Typ()==INT_CMD)
109      {
110        number n2=nlInit((int)(long)a2->Data(),NULL);
111        number n=nlAdd(n1,n2);
112        res->data=(void *)n;
113        res->rtyp=a1->Typ();
114        return FALSE;
115      }
116      else if (a2->Typ()==a1->Typ())
117      {
118        number n2=(number)a2->Data();
119        number n=nlAdd(n1,n2);
120        res->data=(void *)n;
121        res->rtyp=a1->Typ();
122        return FALSE;
123      }
124      return TRUE;
125    }
126  }
127  return blackboxDefaultOp2(op,res,a1,a2);
128}
129// BOOLEAN opM(int op, leftv res, leftv args)
130BOOLEAN bigintm_OpM(int op, leftv res, leftv args)
131{
132  // interpreter: args->1. arg is ist bigintm
133  blackbox *a=getBlackboxStuff(args->Typ());
134  switch(op)
135  {
136    case STRING_CMD:
137    {
138      res->data=(void *)a->blackbox_String(a,args->Data());
139      res->rtyp=STRING_CMD;
140      return FALSE;
141    }
142    default:
143      Werror("op %d not implemented for type %d",op,args->Typ());
144      break;
145  }
146  return TRUE;
147}
148void bigintm_destroy(blackbox *b, void *d)
149{
150  if (d!=NULL)
151  {
152    number n=(number)d;
153    nlDelete(&n,NULL);
154  }
155}
156#endif
Note: See TracBrowser for help on using the repository browser.