My Project
Loading...
Searching...
No Matches
blackbox.cc
Go to the documentation of this file.
1#include "kernel/mod2.h"
2
3#include "Singular/tok.h"
4#include "Singular/subexpr.h"
5#include "Singular/ipshell.h"
6#include "Singular/ipid.h"
7
8#include "Singular/blackbox.h"
9
10#define MAX_BB_TYPES 256
11// #define BLACKBOX_DEVEL 1
12
16#define BLACKBOX_OFFSET (MAX_TOK+1)
17blackbox* getBlackboxStuff(const int t)
18{
19 if (t>MAX_TOK) /*MAX_TOK+1 is BLACKBOX_OFFSET*/
21 else
22 return NULL;
23}
24
25
26void blackbox_default_destroy(blackbox */*b*/, void */*d*/)
27{
28 WerrorS("missing blackbox_destroy");
29}
30char *blackbox_default_String(blackbox */*b*/,void */*d*/)
31{
32 WerrorS("missing blackbox_String");
33 return omStrDup("");
34}
35void *blackbox_default_Copy(blackbox */*b*/,void */*d*/)
36{
37 WerrorS("missing blackbox_Copy");
38 return NULL;
39}
41{
42 int lt=l->Typ();
43 blackbox* b=getBlackboxStuff(lt);
44 if ((lt==r->Typ())
45 && (l->Data()!=r->Data()))
46 {
47 b->blackbox_destroy(b,(void*)l->Data());
48 if (l->rtyp==IDHDL)
49 IDDATA((idhdl)l->data)=(char*)b->blackbox_Copy(b,r->Data());
50 else
51 l->data=b->blackbox_Copy(b,r->Data());
52 }
53 return FALSE;
54}
55void blackbox_default_Print(blackbox *b,void *d)
56{
57 char *s=b->blackbox_String(b,d);
58 PrintS(s);
59 omFree(s);
60}
61void *blackbox_default_Init(blackbox */*b*/)
62{
63 return NULL;
64}
65
66BOOLEAN blackbox_default_serialize(blackbox */*b*/, void */*d*/, si_link /*f*/)
67{
68 WerrorS("blackbox_serialize is not implemented");
69 return TRUE;
70}
71
72BOOLEAN blackbox_default_deserialize(blackbox **/*b*/, void **/*d*/, si_link /*f*/)
73{
74 WerrorS("blackbox_deserialize is not implemented");
75 return TRUE;
76}
77
79{
80 if (op==TYPEOF_CMD)
81 {
82 l->data=omStrDup(getBlackboxName(r->Typ()));
83 l->rtyp=STRING_CMD;
84 return FALSE;
85 }
86 else if (op==NAMEOF_CMD)
87 {
88 if (r->name==NULL) l->data=omStrDup("");
89 else l->data=omStrDup(r->name);
90 l->rtyp=STRING_CMD;
91 return FALSE;
92 }
93
94 return TRUE;
95}
96
97BOOLEAN blackboxDefaultOp2(int /*op*/,leftv /*l*/, leftv /*r1*/, leftv /*r2*/)
98{
99 return TRUE;
100}
101
102BOOLEAN blackboxDefaultOp3(int /*op*/,leftv /*l*/, leftv /*r1*/,leftv /*r2*/, leftv /*r3*/)
103{
104 return TRUE;
105}
106
108{
109 if (op==LIST_CMD)
110 {
111 res->rtyp=LIST_CMD;
112 BOOLEAN bo=jjLIST_PL(res,args);
113 args->CleanUp();
114 return bo;
115 }
116 else if(op==STRING_CMD)
117 {
118 blackbox *b=getBlackboxStuff(args->Typ());
119 res->data=b->blackbox_String(b,args->Data());
120 res->rtyp=STRING_CMD;
121 args=args->next;
122 if(args!=NULL)
123 {
124 sleftv res2;
125 int ret=iiExprArithM(&res2,args,op);
126 if (ret) return TRUE;
127 char *s2=(char*)omAlloc(strlen((char*)res->data)+strlen((char*)res2.data)+1);
128 sprintf(s2,"%s%s",(char*)res->data,(char*)res2.data);
129 omFree(res2.data);
130 omFree(res->data);
131 res->data=s2;
132 }
133 return FALSE;
134 }
135 return TRUE;
136}
137
139{
140 return FALSE;
141}
142int setBlackboxStuff(blackbox *bb, const char *n)
143{
144 int where = -1;
145 for (int i=0;i<MAX_BB_TYPES;i++)
146 {
147 if (blackboxTable[i]!=NULL && strcmp(blackboxName[i],n)==0) {
148 where = i;
149 break;
150 }
151 }
152 if (where < 0) {
154 {
155 // second try, find empty slot from removed bb:
156 for (int i=0;i<MAX_BB_TYPES;i++)
157 {
158 if (blackboxTable[i]==NULL) { where=i; break; }
159 }
160 }
161 else
162 {
163 where=blackboxTableCnt;
165 }
166 }
167 if (where==-1)
168 {
169 WerrorS("too many bb types defined");
170 return 0;
171 }
172 else
173 {
174 // check for alreday defined bb:
175 for (int i=0;i<MAX_BB_TYPES;i++)
176 {
177 if ((blackboxName[i]!=NULL) && (strcmp(blackboxName[i],n)==0))
178 {
179 Warn("not redefining blackbox type %s (%d)",n,i+BLACKBOX_OFFSET);
180 return 0;
181 }
182 }
183 blackboxTable[where]=bb;
184 blackboxName[where]=omStrDup(n);
185#ifdef BLACKBOX_DEVEL
186 Print("setBlackboxStuff: define bb:name=%s:rt=%d (table:cnt=%d)\n",blackboxName[where],where+BLACKBOX_OFFSET,where);
187#endif
188 if (bb->blackbox_destroy==NULL) bb->blackbox_destroy=blackbox_default_destroy;
189 if (bb->blackbox_String==NULL) bb->blackbox_String=blackbox_default_String;
190 if (bb->blackbox_Print==NULL) bb->blackbox_Print=blackbox_default_Print;
191 if (bb->blackbox_Init==NULL) bb->blackbox_Init=blackbox_default_Init;
192 if (bb->blackbox_Copy==NULL) bb->blackbox_Copy=blackbox_default_Copy;
193 if (bb->blackbox_Assign==NULL) bb->blackbox_Assign=blackbox_default_Assign;
194 if (bb->blackbox_Op1==NULL) bb->blackbox_Op1=blackboxDefaultOp1;
195 if (bb->blackbox_Op2==NULL) bb->blackbox_Op2=blackboxDefaultOp2;
196 if (bb->blackbox_Op3==NULL) bb->blackbox_Op3=blackboxDefaultOp3;
197 if (bb->blackbox_OpM==NULL) bb->blackbox_OpM=blackboxDefaultOpM;
198 if (bb->blackbox_CheckAssign==NULL) bb->blackbox_CheckAssign=blackbox_default_Check;
199 if (bb->blackbox_serialize==NULL) bb->blackbox_serialize=blackbox_default_serialize;
200 if (bb->blackbox_deserialize==NULL) bb->blackbox_deserialize=blackbox_default_deserialize;
201 return where+BLACKBOX_OFFSET;
202 }
203}
204
205void removeBlackboxStuff(const int rt)
206{
211}
212const char *getBlackboxName(const int t)
213{
215 if (b!=NULL) return b;
216 else return "";
217}
218int blackboxIsCmd(const char *n, int & tok)
219{
220 for(int i=blackboxTableCnt-1;i>=0;i--)
221 {
222 if(strcmp(n,blackboxName[i])==0)
223 {
224#ifdef BLACKBOX_DEVEL
225 Print("blackboxIsCmd: found bb:%s:%d (table:%d)\n",n,i+BLACKBOX_OFFSET,i);
226#endif
227 tok=i+BLACKBOX_OFFSET;
228 return ROOT_DECL;
229 }
230 }
231 tok=0;
232 return 0;
233}
234
236{
237 for(int i=blackboxTableCnt-1;i>=0;i--)
238 {
239 if (blackboxName[i]!=NULL)
240 Print("type %d: %s\n",i,blackboxName[i]);
241 }
242}
243
245{
246 int i = 0;
247 void **l = (void **)omalloc0(blackboxTableCnt * sizeof(void *));
248 struct blackbox_list *list_struct = (struct blackbox_list *) omAlloc0(sizeof(struct blackbox_list));
249 list_struct->count = blackboxTableCnt;
250 list_struct->list = l;
251
252 for (i = blackboxTableCnt-1; i >= 0 ;i--)
253 {
254 if (blackboxName[i]!=NULL) {
255 l[i] = (void *)omStrDup(blackboxName[i]);
256 //Print("type %d: %s\n",i,blackboxName[i]);
257 } else {
258 l[i] = NULL;
259 }
260 }
261 return list_struct;
262}
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
struct blackbox_list * getBlackboxTypes()
return array of all define types.
Definition: blackbox.cc:244
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition: blackbox.cc:142
BOOLEAN blackboxDefaultOp2(int, leftv, leftv, leftv)
default procedure blackboxDefaultOp2, to be called as "default:" branch
Definition: blackbox.cc:97
const char * getBlackboxName(const int t)
return the name to the type given by t (r/o)
Definition: blackbox.cc:212
void removeBlackboxStuff(const int rt)
Definition: blackbox.cc:205
void * blackbox_default_Copy(blackbox *, void *)
Definition: blackbox.cc:35
char * blackbox_default_String(blackbox *, void *)
Definition: blackbox.cc:30
BOOLEAN blackbox_default_serialize(blackbox *, void *, si_link)
Definition: blackbox.cc:66
BOOLEAN blackboxDefaultOp3(int, leftv, leftv, leftv, leftv)
default procedure blackboxDefaultOp3, to be called as "default:" branch
Definition: blackbox.cc:102
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition: blackbox.cc:17
BOOLEAN blackbox_default_Check(blackbox *, leftv, leftv)
Definition: blackbox.cc:138
BOOLEAN blackbox_default_Assign(leftv l, leftv r)
Definition: blackbox.cc:40
void blackbox_default_Print(blackbox *b, void *d)
default procedure blackbox_default_Print: print the string
Definition: blackbox.cc:55
void * blackbox_default_Init(blackbox *)
Definition: blackbox.cc:61
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:218
static GLOBAL_VAR char * blackboxName[MAX_BB_TYPES]
Definition: blackbox.cc:14
BOOLEAN blackbox_default_deserialize(blackbox **, void **, si_link)
Definition: blackbox.cc:72
void blackbox_default_destroy(blackbox *, void *)
Definition: blackbox.cc:26
static GLOBAL_VAR int blackboxTableCnt
Definition: blackbox.cc:15
void printBlackboxTypes()
list all defined type (for debugging)
Definition: blackbox.cc:235
BOOLEAN blackboxDefaultOpM(int op, leftv res, leftv args)
default procedure blackboxDefaultOpM, to be called as "default:" branch
Definition: blackbox.cc:107
BOOLEAN blackboxDefaultOp1(int op, leftv l, leftv r)
default procedure blackboxDefaultOp1, to be called as "default:" branch
Definition: blackbox.cc:78
#define MAX_BB_TYPES
Definition: blackbox.cc:10
static GLOBAL_VAR blackbox * blackboxTable[MAX_BB_TYPES]
Definition: blackbox.cc:13
#define BLACKBOX_OFFSET
Definition: blackbox.cc:16
void ** list
Definition: blackbox.h:86
struct for containing list of blackbox names and the number of them.
Definition: blackbox.h:84
int l
Definition: cfEzgcd.cc:100
int i
Definition: cfEzgcd.cc:132
CanonicalForm b
Definition: cfModGcd.cc:4103
Definition: idrec.h:35
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
int Typ()
Definition: subexpr.cc:1019
const char * name
Definition: subexpr.h:87
void * Data()
Definition: subexpr.cc:1162
leftv next
Definition: subexpr.h:86
void * data
Definition: subexpr.h:88
void CleanUp(ring r=currRing)
Definition: subexpr.cc:348
#define Print
Definition: emacs.cc:80
#define Warn
Definition: emacs.cc:77
const CanonicalForm int s
Definition: facAbsFact.cc:51
CanonicalForm res
Definition: facAbsFact.cc:60
void WerrorS(const char *s)
Definition: feFopen.cc:24
#define GLOBAL_VAR
Definition: globaldefs.h:11
@ ROOT_DECL
Definition: grammar.cc:319
BOOLEAN iiExprArithM(leftv res, leftv a, int op)
Definition: iparith.cc:9414
BOOLEAN jjLIST_PL(leftv res, leftv v)
Definition: iparith.cc:7998
#define IDDATA(a)
Definition: ipid.h:126
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omfree(addr)
Definition: omAllocDecl.h:237
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omalloc0(size)
Definition: omAllocDecl.h:229
#define omFree(addr)
Definition: omAllocDecl.h:261
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define NULL
Definition: omList.c:12
void PrintS(const char *s)
Definition: reporter.cc:284
#define IDHDL
Definition: tok.h:31
@ LIST_CMD
Definition: tok.h:118
@ NAMEOF_CMD
Definition: tok.h:134
@ TYPEOF_CMD
Definition: tok.h:194
@ STRING_CMD
Definition: tok.h:185
@ MAX_TOK
Definition: tok.h:218