blackbox.cc
Go to the documentation of this file.
1 
2 
3 
4 #include <kernel/mod2.h>
5 #include <misc/auxiliary.h>
6 
7 #include "tok.h"
8 #include "subexpr.h"
9 #include "ipshell.h"
10 
11 #include "blackbox.h"
12 
13 #define MAX_BB_TYPES 256
14 // #define BLACKBOX_DEVEL 1
15 
16 static blackbox* blackboxTable[MAX_BB_TYPES];
17 static char * blackboxName[MAX_BB_TYPES];
18 static int blackboxTableCnt=0;
19 #define BLACKBOX_OFFSET (MAX_TOK+1)
20 blackbox* getBlackboxStuff(const int t)
21 {
22  if (t>MAX_TOK) /*MAX_TOK+1 is BLACKBOX_OFFSET*/
23  return (blackboxTable[t-BLACKBOX_OFFSET]);
24  else
25  return NULL;
26 }
27 
28 
29 void blackbox_default_destroy(blackbox */*b*/, void */*d*/)
30 {
31  WerrorS("missing blackbox_destroy");
32 }
33 char *blackbox_default_String(blackbox */*b*/,void */*d*/)
34 {
35  WerrorS("missing blackbox_String");
36  return omStrDup("");
37 }
38 void *blackbox_default_Copy(blackbox */*b*/,void */*d*/)
39 {
40  WerrorS("missing blackbox_Copy");
41  return NULL;
42 }
43 void blackbox_default_Print(blackbox *b,void *d)
44 {
45  char *s=b->blackbox_String(b,d);
46  PrintS(s);
47  omFree(s);
48 }
49 void *blackbox_default_Init(blackbox */*b*/)
50 {
51  return NULL;
52 }
53 
54 BOOLEAN blackbox_default_serialize(blackbox */*b*/, void */*d*/, si_link /*f*/)
55 {
56  WerrorS("blackbox_serialize is not implemented");
57  return TRUE;
58 }
59 
60 BOOLEAN blackbox_default_deserialize(blackbox **/*b*/, void **/*d*/, si_link /*f*/)
61 {
62  WerrorS("blackbox_deserialize is not implemented");
63  return TRUE;
64 }
65 
67 {
68  if (op==TYPEOF_CMD)
69  {
70  l->data=omStrDup(getBlackboxName(r->Typ()));
71  l->rtyp=STRING_CMD;
72  return FALSE;
73  }
74  else if (op==NAMEOF_CMD)
75  {
76  if (r->name==NULL) l->data=omStrDup("");
77  else l->data=omStrDup(r->name);
78  l->rtyp=STRING_CMD;
79  return FALSE;
80  }
81 
82  return TRUE;
83 }
84 
85 BOOLEAN blackboxDefaultOp2(int op,leftv /*l*/, leftv r1, leftv /*r2*/)
86 {
87  return TRUE;
88 }
89 
90 BOOLEAN blackboxDefaultOp3(int op,leftv /*l*/, leftv r1,leftv /*r2*/, leftv /*r3*/)
91 {
92  return TRUE;
93 }
94 
96 {
97  if (op==LIST_CMD)
98  {
99  res->rtyp=LIST_CMD;
100  return jjLIST_PL(res,args);
101  }
102  else if(op==STRING_CMD)
103  {
104  blackbox *b=getBlackboxStuff(args->Typ());
105  res->data=b->blackbox_String(b,args->Data());
106  res->rtyp=STRING_CMD;
107  args=args->next;
108  if(args!=NULL)
109  {
110  sleftv res2;
111  int ret=iiExprArithM(&res2,args,op);
112  if (ret) return TRUE;
113  char *s2=(char*)omAlloc(strlen((char*)res->data)+strlen((char*)res2.data)+1);
114  sprintf(s2,"%s%s",(char*)res->data,(char*)res2.data);
115  omFree(res2.data);
116  omFree(res->data);
117  res->data=s2;
118  }
119  return FALSE;
120  }
121  return TRUE;
122 }
123 
125 {
126  return FALSE;
127 }
128 int setBlackboxStuff(blackbox *bb, const char *n)
129 {
130  int where=-1;
132  {
133  // second try, find empty slot from removed bb:
134  for (int i=0;i<MAX_BB_TYPES;i++)
135  {
136  if (blackboxTable[i]==NULL) { where=i; break; }
137  }
138  }
139  else
140  {
141  where=blackboxTableCnt;
143  }
144  if (where==-1)
145  {
146  WerrorS("too many bb types defined");
147  return 0;
148  }
149  else
150  {
151  // check for alreday defined bb:
152  for (int i=0;i<MAX_BB_TYPES;i++)
153  {
154  if ((blackboxName[i]!=NULL) && (strcmp(blackboxName[i],n)==0))
155  {
156  Warn("redefining blackbox type %s (%d -> %d)",n,i+BLACKBOX_OFFSET,where+BLACKBOX_OFFSET);
157  }
158  }
159  blackboxTable[where]=bb;
160  blackboxName[where]=omStrDup(n);
161 #ifdef BLACKBOX_DEVEL
162  Print("setBlackboxStuff: define bb:name=%s:rt=%d (table:cnt=%d)\n",blackboxName[where],where+BLACKBOX_OFFSET,where);
163 #endif
164  if (bb->blackbox_destroy==NULL) bb->blackbox_destroy=blackbox_default_destroy;
165  if (bb->blackbox_String==NULL) bb->blackbox_String=blackbox_default_String;
166  if (bb->blackbox_Print==NULL) bb->blackbox_Print=blackbox_default_Print;
167  if (bb->blackbox_Init==NULL) bb->blackbox_Init=blackbox_default_Init;
168  if (bb->blackbox_Copy==NULL) bb->blackbox_Copy=blackbox_default_Copy;
169  if (bb->blackbox_Op1==NULL) bb->blackbox_Op1=blackboxDefaultOp1;
170  if (bb->blackbox_Op2==NULL) bb->blackbox_Op2=blackboxDefaultOp2;
171  if (bb->blackbox_Op3==NULL) bb->blackbox_Op3=blackboxDefaultOp3;
172  if (bb->blackbox_OpM==NULL) bb->blackbox_OpM=blackboxDefaultOpM;
173  if (bb->blackbox_CheckAssign==NULL) bb->blackbox_CheckAssign=blackbox_default_Check;
174  if (bb->blackbox_serialize==NULL) bb->blackbox_serialize=blackbox_default_serialize;
175  if (bb->blackbox_deserialize==NULL) bb->blackbox_deserialize=blackbox_default_deserialize;
176  return where+BLACKBOX_OFFSET;
177  }
178 }
179 
180 void removeBlackboxStuff(const int rt)
181 {
186 }
187 const char *getBlackboxName(const int t)
188 {
189  char *b=blackboxName[t-BLACKBOX_OFFSET];
190  if (b!=NULL) return b;
191  else return "";
192 }
193 int blackboxIsCmd(const char *n, int & tok)
194 {
195  for(int i=blackboxTableCnt-1;i>=0;i--)
196  {
197  if(strcmp(n,blackboxName[i])==0)
198  {
199 #ifdef BLACKBOX_DEVEL
200  Print("blackboxIsCmd: found bb:%s:%d (table:%d)\n",n,i+BLACKBOX_OFFSET,i);
201 #endif
202  tok=i+BLACKBOX_OFFSET;
203  return ROOT_DECL;
204  }
205  }
206  tok=0;
207  return 0;
208 }
209 
211 {
212  for(int i=blackboxTableCnt-1;i>=0;i--)
213  {
214  if (blackboxName[i]!=NULL)
215  Print("type %d: %s\n",i,blackboxName[i]);
216  }
217 }
void blackbox_default_Print(blackbox *b, void *d)
default procedure blackbox_default_Print: print the string
Definition: blackbox.cc:43
char * blackbox_default_String(blackbox *, void *)
Definition: blackbox.cc:33
const CanonicalForm int s
Definition: facAbsFact.cc:55
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
BOOLEAN blackbox_default_Check(blackbox *, leftv, leftv)
Definition: blackbox.cc:124
#define Print
Definition: emacs.cc:83
BOOLEAN blackbox_default_serialize(blackbox *, void *, si_link)
Definition: blackbox.cc:54
static int blackboxTableCnt
Definition: blackbox.cc:18
#define FALSE
Definition: auxiliary.h:140
BOOLEAN blackboxDefaultOp3(int op, leftv, leftv r1, leftv, leftv)
default procedure blackboxDefaultOp3, to be called as "default:" branch
Definition: blackbox.cc:90
static blackbox * blackboxTable[MAX_BB_TYPES]
Definition: blackbox.cc:16
Definition: tok.h:170
#define TRUE
Definition: auxiliary.h:144
BOOLEAN iiExprArithM(leftv res, leftv a, int op)
Definition: iparith.cc:8630
void WerrorS(const char *s)
Definition: feFopen.cc:24
#define BLACKBOX_OFFSET
Definition: blackbox.cc:19
int Typ()
Definition: subexpr.cc:969
#define omAlloc(size)
Definition: omAllocDecl.h:210
void * data
Definition: subexpr.h:89
void printBlackboxTypes()
list all defined type (for debugging)
Definition: blackbox.cc:210
poly res
Definition: myNF.cc:322
const ring r
Definition: syzextra.cc:208
const char * name
Definition: subexpr.h:88
#define omFree(addr)
Definition: omAllocDecl.h:261
#define omfree(addr)
Definition: omAllocDecl.h:237
BOOLEAN blackbox_default_deserialize(blackbox **, void **, si_link)
Definition: blackbox.cc:60
BOOLEAN blackboxDefaultOp2(int op, leftv, leftv r1, leftv)
default procedure blackboxDefaultOp2, to be called as "default:" branch
Definition: blackbox.cc:85
BOOLEAN blackboxDefaultOp1(int op, leftv l, leftv r)
default procedure blackboxDefaultOp1, to be called as "default:" branch
Definition: blackbox.cc:66
All the auxiliary stuff.
int i
Definition: cfEzgcd.cc:123
void PrintS(const char *s)
Definition: reporter.cc:294
leftv next
Definition: subexpr.h:87
static char * blackboxName[MAX_BB_TYPES]
Definition: blackbox.cc:17
void removeBlackboxStuff(const int rt)
Definition: blackbox.cc:180
BOOLEAN jjLIST_PL(leftv res, leftv v)
Definition: iparith.cc:7411
#define NULL
Definition: omList.c:10
void * blackbox_default_Init(blackbox *)
Definition: blackbox.cc:49
int blackboxIsCmd(const char *n, int &tok)
used by scanner: returns ROOT_DECL for known types (and the type number in tok)
Definition: blackbox.cc:193
void * blackbox_default_Copy(blackbox *, void *)
Definition: blackbox.cc:38
BOOLEAN blackboxDefaultOpM(int op, leftv res, leftv args)
default procedure blackboxDefaultOpM, to be called as "default:" branch
Definition: blackbox.cc:95
int rtyp
Definition: subexpr.h:92
void * Data()
Definition: subexpr.cc:1111
Definition: tok.h:96
#define MAX_BB_TYPES
Definition: blackbox.cc:13
void blackbox_default_destroy(blackbox *, void *)
Definition: blackbox.cc:29
const char * getBlackboxName(const int t)
return the name to the type given by t (r/o)
Definition: blackbox.cc:187
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition: blackbox.cc:128
int BOOLEAN
Definition: auxiliary.h:131
const poly b
Definition: syzextra.cc:213
int l
Definition: cfEzgcd.cc:94
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition: blackbox.cc:20
#define Warn
Definition: emacs.cc:80
#define omStrDup(s)
Definition: omAllocDecl.h:263