source: git/Singular/blackbox.cc @ 7626c5f

spielwiese
Last change on this file since 7626c5f was 146d90, checked in by Hans Schoenemann <hannes@…>, 13 years ago
fix list(newstruct,...) git-svn-id: file:///usr/local/Singular/svn/trunk@14109 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.3 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
45// Tok2Cmdname -> iiTwoOps
46BOOLEAN WrongOp(const char* cmd, int op, leftv bb)
47{
48  assume( bb->Typ() > MAX_TOK ); // it IS a blackbox type, right?!
49 
50  if( op > 127 )
51    Werror("'%s' of type %s(%d) for op %s(%d) not implemented",
52           cmd,
53           getBlackboxName(bb->Typ()),bb->Typ(),
54           iiTwoOps(op), op);
55  else
56    Werror("'%s' of type %s(%d) for op '%c' not implemented",
57           cmd,
58           getBlackboxName(bb->Typ()), bb->Typ(),
59           op);
60
61  return TRUE;
62}
63
64BOOLEAN blackboxDefaultOp1(int op,leftv l, leftv r)
65{
66  if (op==TYPEOF_CMD)
67  {
68    l->data=omStrDup(getBlackboxName(r->Typ()));
69    l->rtyp=STRING_CMD;
70    return FALSE;
71  }
72
73  return WrongOp("blackbox_Op1", op, r);
74}
75
76BOOLEAN blackboxDefaultOp2(int op,leftv l, leftv r1, leftv r2)
77{
78  return WrongOp("blackbox_Op2", op, r1);
79}
80
81BOOLEAN blackbox_default_Op3(int op,leftv l, leftv r1,leftv r2, leftv r3)
82{
83  return WrongOp("blackbox_Op3", op, r1);
84}
85
86BOOLEAN blackbox_default_OpM(int op,leftv res, leftv args)
87{
88  if (op==LIST_CMD)
89  {
90    res->rtyp=LIST_CMD;
91    return jjLIST_PL(res,args);
92  }
93  return WrongOp("blackbox_OpM", op, args);
94}
95
96BOOLEAN blackbox_default_Check(blackbox *b, void *d)
97{
98  return FALSE;
99}
100int setBlackboxStuff(blackbox *bb, const char *n)
101{
102  int where=-1;
103  if (MAX_BB_TYPES<=blackboxTableCnt)
104  {
105    // second try, find empty slot from removed bb:
106    for (int i=0;i<MAX_BB_TYPES;i++)
107    {
108      if (blackboxTable[i]==NULL) { where=i; break; }
109    }
110  }
111  else
112  {
113    where=blackboxTableCnt;
114    blackboxTableCnt++;
115  }
116  if (where==-1)
117  {
118    WerrorS("too many bb types defined");
119    return 0;
120  }
121  else
122  {
123    blackboxTable[where]=bb;
124    blackboxName[where]=omStrDup(n);
125#ifdef BLACKBOX_DEVEL
126  Print("setBlackboxStuff: define bb:name=%s:rt=%d (table:cnt=%d)\n",blackboxName[where],where+BLACKBOX_OFFSET,where);
127#endif
128    if (bb->blackbox_destroy==NULL) bb->blackbox_destroy=blackbox_default_destroy;
129    if (bb->blackbox_String==NULL)  bb->blackbox_String=blackbox_default_String;
130    if (bb->blackbox_Print==NULL)   bb->blackbox_Print=blackbox_default_Print;
131    if (bb->blackbox_Init==NULL)    bb->blackbox_Init=blackbox_default_Init;
132    if (bb->blackbox_Copy==NULL)    bb->blackbox_Copy=blackbox_default_Copy;
133    if (bb->blackbox_Op1==NULL)     bb->blackbox_Op1=blackboxDefaultOp1;
134    if (bb->blackbox_Op2==NULL)     bb->blackbox_Op2=blackboxDefaultOp2;
135    if (bb->blackbox_Op3==NULL)     bb->blackbox_Op3=blackbox_default_Op3;
136    if (bb->blackbox_OpM==NULL)     bb->blackbox_OpM=blackbox_default_OpM;
137    if (bb->blackbox_Check==NULL)   bb->blackbox_Check=blackbox_default_Check;
138    return where+BLACKBOX_OFFSET;
139  }
140}
141void removeBlackboxStuff(const int rt)
142{
143  omfree(blackboxTable[rt-BLACKBOX_OFFSET]);
144  omfree(blackboxName[rt-BLACKBOX_OFFSET]);
145  blackboxTable[rt-BLACKBOX_OFFSET]=NULL;
146  blackboxName[rt-BLACKBOX_OFFSET]=NULL;
147}
148const char *getBlackboxName(const int t)
149{
150 char *b=blackboxName[t-BLACKBOX_OFFSET];
151  if (b!=NULL) return b;
152  else         return "";
153}
154int blackboxIsCmd(const char *n, int & tok)
155{
156  for(int i=blackboxTableCnt-1;i>=0;i--)
157  {
158    if(strcmp(n,blackboxName[i])==0)
159    {
160#ifdef BLACKBOX_DEVEL
161      Print("blackboxIsCmd: found bb:%s:%d (table:%d)\n",n,i+BLACKBOX_OFFSET,i);
162#endif
163      tok=i+BLACKBOX_OFFSET;
164      return ROOT_DECL;
165    }
166  }
167  return 0;
168}
169
170void printBlackboxTypes()
171{
172  for(int i=blackboxTableCnt-1;i>=0;i--)
173  {
174    if (blackboxName[i]!=NULL)
175       Print("type %d: %s\n",i,blackboxName[i]);
176  }
177}
Note: See TracBrowser for help on using the repository browser.