source: git/Singular/blackbox.cc @ a71a00

spielwiese
Last change on this file since a71a00 was a088a12, checked in by Andreas Steenpaß <steenpas@…>, 13 years ago
writing newstructs to links git-svn-id: file:///usr/local/Singular/svn/trunk@14175 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.7 KB
Line 
1#include <Singular/mod2.h>
2#include <Singular/tok.h>
3#include <Singular/subexpr.h>
4#include <Singular/ipshell.h>
5#include <Singular/blackbox.h>
6
7#define MAX_BB_TYPES 256
8// #define BLACKBOX_DEVEL 1
9
10static blackbox* blackboxTable[MAX_BB_TYPES];
11static char *    blackboxName[MAX_BB_TYPES];
12static int blackboxTableCnt=0;
13#define BLACKBOX_OFFSET (MAX_TOK+1)
14blackbox* getBlackboxStuff(const int t)
15{
16  return (blackboxTable[t-BLACKBOX_OFFSET]);
17}
18
19
20void blackbox_default_destroy(blackbox  *b, void *d)
21{
22  Werror("missing blackbox_destroy");
23}
24char *blackbox_default_String(blackbox *b,void *d)
25{
26  Werror("missing blackbox_String");
27  return omStrDup("");
28}
29void *blackbox_default_Copy(blackbox *b,void *d)
30{
31  Werror("missing blackbox_Copy");
32  return NULL;
33}
34void blackbox_default_Print(blackbox *b,void *d)
35{
36  char *s=b->blackbox_String(b,d);
37  PrintS(s);
38  omFree(s);
39}
40void *blackbox_default_Init(blackbox *b)
41{
42  return NULL;
43}
44
45BOOLEAN blackbox_default_serialize(blackbox *b, void *d, si_link f)
46{
47  return TRUE;
48}
49
50BOOLEAN blackbox_default_deserialize(blackbox **b, void **d, si_link f)
51{
52  return TRUE;
53}
54
55// Tok2Cmdname -> iiTwoOps
56BOOLEAN WrongOp(const char* cmd, int op, leftv bb)
57{
58  assume( bb->Typ() > MAX_TOK ); // it IS a blackbox type, right?!
59 
60  if( op > 127 )
61    Werror("'%s' of type %s(%d) for op %s(%d) not implemented",
62           cmd,
63           getBlackboxName(bb->Typ()),bb->Typ(),
64           iiTwoOps(op), op);
65  else
66    Werror("'%s' of type %s(%d) for op '%c' not implemented",
67           cmd,
68           getBlackboxName(bb->Typ()), bb->Typ(),
69           op);
70
71  return TRUE;
72}
73
74BOOLEAN blackboxDefaultOp1(int op,leftv l, leftv r)
75{
76  if (op==TYPEOF_CMD)
77  {
78    l->data=omStrDup(getBlackboxName(r->Typ()));
79    l->rtyp=STRING_CMD;
80    return FALSE;
81  }
82
83  return WrongOp("blackbox_Op1", op, r);
84}
85
86BOOLEAN blackboxDefaultOp2(int op,leftv l, leftv r1, leftv r2)
87{
88  return WrongOp("blackbox_Op2", op, r1);
89}
90
91BOOLEAN blackbox_default_Op3(int op,leftv l, leftv r1,leftv r2, leftv r3)
92{
93  return WrongOp("blackbox_Op3", op, r1);
94}
95
96BOOLEAN blackbox_default_OpM(int op,leftv res, leftv args)
97{
98  if (op==LIST_CMD)
99  {
100    res->rtyp=LIST_CMD;
101    return jjLIST_PL(res,args);
102  }
103  return WrongOp("blackbox_OpM", op, args);
104}
105
106BOOLEAN blackbox_default_Check(blackbox *b, void *d)
107{
108  return FALSE;
109}
110int setBlackboxStuff(blackbox *bb, const char *n)
111{
112  int where=-1;
113  if (MAX_BB_TYPES<=blackboxTableCnt)
114  {
115    // second try, find empty slot from removed bb:
116    for (int i=0;i<MAX_BB_TYPES;i++)
117    {
118      if (blackboxTable[i]==NULL) { where=i; break; }
119    }
120  }
121  else
122  {
123    where=blackboxTableCnt;
124    blackboxTableCnt++;
125  }
126  if (where==-1)
127  {
128    WerrorS("too many bb types defined");
129    return 0;
130  }
131  else
132  {
133    blackboxTable[where]=bb;
134    blackboxName[where]=omStrDup(n);
135#ifdef BLACKBOX_DEVEL
136  Print("setBlackboxStuff: define bb:name=%s:rt=%d (table:cnt=%d)\n",blackboxName[where],where+BLACKBOX_OFFSET,where);
137#endif
138    if (bb->blackbox_destroy==NULL) bb->blackbox_destroy=blackbox_default_destroy;
139    if (bb->blackbox_String==NULL)  bb->blackbox_String=blackbox_default_String;
140    if (bb->blackbox_Print==NULL)   bb->blackbox_Print=blackbox_default_Print;
141    if (bb->blackbox_Init==NULL)    bb->blackbox_Init=blackbox_default_Init;
142    if (bb->blackbox_Copy==NULL)    bb->blackbox_Copy=blackbox_default_Copy;
143    if (bb->blackbox_Op1==NULL)     bb->blackbox_Op1=blackboxDefaultOp1;
144    if (bb->blackbox_Op2==NULL)     bb->blackbox_Op2=blackboxDefaultOp2;
145    if (bb->blackbox_Op3==NULL)     bb->blackbox_Op3=blackbox_default_Op3;
146    if (bb->blackbox_OpM==NULL)     bb->blackbox_OpM=blackbox_default_OpM;
147    if (bb->blackbox_Check==NULL)   bb->blackbox_Check=blackbox_default_Check;
148    if (bb->blackbox_serialize==NULL) bb->blackbox_serialize=blackbox_default_serialize;
149    if (bb->blackbox_deserialize==NULL) bb->blackbox_deserialize=blackbox_default_deserialize;
150    return where+BLACKBOX_OFFSET;
151  }
152}
153
154void removeBlackboxStuff(const int rt)
155{
156  omfree(blackboxTable[rt-BLACKBOX_OFFSET]);
157  omfree(blackboxName[rt-BLACKBOX_OFFSET]);
158  blackboxTable[rt-BLACKBOX_OFFSET]=NULL;
159  blackboxName[rt-BLACKBOX_OFFSET]=NULL;
160}
161const char *getBlackboxName(const int t)
162{
163 char *b=blackboxName[t-BLACKBOX_OFFSET];
164  if (b!=NULL) return b;
165  else         return "";
166}
167int blackboxIsCmd(const char *n, int & tok)
168{
169  for(int i=blackboxTableCnt-1;i>=0;i--)
170  {
171    if(strcmp(n,blackboxName[i])==0)
172    {
173#ifdef BLACKBOX_DEVEL
174      Print("blackboxIsCmd: found bb:%s:%d (table:%d)\n",n,i+BLACKBOX_OFFSET,i);
175#endif
176      tok=i+BLACKBOX_OFFSET;
177      return ROOT_DECL;
178    }
179  }
180  return 0;
181}
182
183void printBlackboxTypes()
184{
185  for(int i=blackboxTableCnt-1;i>=0;i--)
186  {
187    if (blackboxName[i]!=NULL)
188       Print("type %d: %s\n",i,blackboxName[i]);
189  }
190}
Note: See TracBrowser for help on using the repository browser.