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