source: git/Singular/blackbox.cc @ eff324

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