1 | #include <ctype.h> |
---|
2 | |
---|
3 | #include <Singular/mod2.h> |
---|
4 | #include <Singular/ipid.h> |
---|
5 | #include <Singular/blackbox.h> |
---|
6 | #include <Singular/lists.h> |
---|
7 | #include <Singular/ipid.h> |
---|
8 | #include <Singular/ipshell.h> |
---|
9 | #include <Singular/newstruct.h> |
---|
10 | |
---|
11 | struct newstruct_member_s; |
---|
12 | typedef struct newstruct_member_s *newstruct_member; |
---|
13 | struct newstruct_member_s |
---|
14 | { |
---|
15 | newstruct_member next; |
---|
16 | char * name; |
---|
17 | int typ; |
---|
18 | int pos; |
---|
19 | }; |
---|
20 | |
---|
21 | struct newstruct_desc_s |
---|
22 | { |
---|
23 | newstruct_member member; |
---|
24 | newstruct_desc parent; |
---|
25 | int size; // number of mebers +1 |
---|
26 | int id; // the type id assigned to this bb |
---|
27 | }; |
---|
28 | |
---|
29 | |
---|
30 | char * newstruct_String(blackbox *b, void *d) |
---|
31 | { |
---|
32 | if (d==NULL) return omStrDup("oo"); |
---|
33 | else |
---|
34 | { |
---|
35 | newstruct_desc ad=(newstruct_desc)b->data; |
---|
36 | lists l=(lists)d; |
---|
37 | newstruct_member a=ad->member; |
---|
38 | StringSetS(""); |
---|
39 | loop |
---|
40 | { |
---|
41 | StringAppendS(a->name); |
---|
42 | char *tmp=omStrDup(StringAppendS("=")); |
---|
43 | if ((!RingDependend(a->typ)) |
---|
44 | || ((l->m[a->pos-1].data==(void *)currRing) |
---|
45 | && (currRing!=NULL))) |
---|
46 | { |
---|
47 | if (l->m[a->pos].rtyp==LIST_CMD) |
---|
48 | { |
---|
49 | StringAppendS("<list>"); |
---|
50 | } |
---|
51 | else |
---|
52 | { |
---|
53 | StringSetS(""); |
---|
54 | char *tmp2=omStrDup(l->m[a->pos].String()); |
---|
55 | StringSetS(tmp); |
---|
56 | if ((strlen(tmp2)>80)||(strchr(tmp2,'\n')!=NULL)) |
---|
57 | { |
---|
58 | StringAppend("<%s>",Tok2Cmdname(l->m[a->pos].rtyp)); |
---|
59 | } |
---|
60 | else StringAppendS(tmp2); |
---|
61 | omFree(tmp2); |
---|
62 | } |
---|
63 | } |
---|
64 | else StringAppendS("??"); |
---|
65 | omFree(tmp); |
---|
66 | if (a->next==NULL) break; |
---|
67 | StringAppendS("\n"); |
---|
68 | if(errorreported) break; |
---|
69 | a=a->next; |
---|
70 | } |
---|
71 | return omStrDup(StringAppendS("")); |
---|
72 | } |
---|
73 | } |
---|
74 | lists lCopy_newstruct(lists L) |
---|
75 | { |
---|
76 | lists N=(lists)omAlloc0Bin(slists_bin); |
---|
77 | int n=L->nr; |
---|
78 | ring save_ring=currRing; |
---|
79 | N->Init(n+1); |
---|
80 | for(;n>=0;n--) |
---|
81 | { |
---|
82 | if (RingDependend(L->m[n].rtyp)) |
---|
83 | { |
---|
84 | assume((L->m[n-1].rtyp==RING_CMD) || (L->m[n-1].data==NULL)); |
---|
85 | if(L->m[n-1].data!=NULL) |
---|
86 | { |
---|
87 | if (L->m[n-1].data!=(void*)currRing) |
---|
88 | rChangeCurrRing((ring)(L->m[n-1].data)); |
---|
89 | N->m[n].Copy(&L->m[n]); |
---|
90 | } |
---|
91 | else |
---|
92 | { |
---|
93 | N->m[n].rtyp=L->m[n].rtyp; |
---|
94 | N->m[n].data=idrecDataInit(L->m[n].rtyp); |
---|
95 | } |
---|
96 | } |
---|
97 | else if((L->m[n].rtyp>MAX_TOK)||(L->m[n].rtyp==LIST_CMD)) |
---|
98 | { |
---|
99 | N->m[n].rtyp=L->m[n].rtyp; |
---|
100 | N->m[n].data=(void *)lCopy_newstruct((lists)(L->m[n].data)); |
---|
101 | } |
---|
102 | else |
---|
103 | N->m[n].Copy(&L->m[n]); |
---|
104 | } |
---|
105 | if (currRing!=save_ring) rChangeCurrRing(save_ring); |
---|
106 | return N; |
---|
107 | } |
---|
108 | void * newstruct_Copy(blackbox*b, void *d) |
---|
109 | { |
---|
110 | lists n1=(lists)d; |
---|
111 | return (void*)lCopy_newstruct(n1); |
---|
112 | } |
---|
113 | |
---|
114 | BOOLEAN newstruct_Assign(leftv l, leftv r) |
---|
115 | { |
---|
116 | blackbox *ll=getBlackboxStuff(l->Typ()); |
---|
117 | if (r->Typ()>MAX_TOK) |
---|
118 | { |
---|
119 | blackbox *rr=getBlackboxStuff(r->Typ()); |
---|
120 | if (l->Typ()!=r->Typ()) |
---|
121 | { |
---|
122 | newstruct_desc rrn=(newstruct_desc)rr->data; |
---|
123 | newstruct_desc rrp=rrn->parent; |
---|
124 | while ((rrp!=NULL)&&(rrp->id!=l->Typ())) rrp=rrp->parent; |
---|
125 | if (rrp!=NULL) |
---|
126 | { |
---|
127 | if (l->rtyp==IDHDL) |
---|
128 | { |
---|
129 | IDTYP((idhdl)l->data)=r->Typ(); |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | l->rtyp=r->Typ(); |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | if (l->Typ()==r->Typ()) |
---|
138 | { |
---|
139 | if (l->Data()!=NULL) |
---|
140 | { |
---|
141 | lists n1=(lists)l->Data(); |
---|
142 | n1->Clean(); n1=NULL; |
---|
143 | } |
---|
144 | lists n2=(lists)r->Data(); |
---|
145 | n2=lCopy_newstruct(n2); |
---|
146 | if (l->rtyp==IDHDL) |
---|
147 | { |
---|
148 | IDDATA((idhdl)l->data)=(char *)n2; |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | l->data=(void *)n2; |
---|
153 | } |
---|
154 | return FALSE; |
---|
155 | } |
---|
156 | } |
---|
157 | Werror("assign %s(%d) = %s(%d)", |
---|
158 | Tok2Cmdname(l->Typ()),l->Typ(),Tok2Cmdname(r->Typ()),r->Typ()); |
---|
159 | return TRUE; |
---|
160 | } |
---|
161 | |
---|
162 | BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2) |
---|
163 | { |
---|
164 | // interpreter: a1 is newstruct |
---|
165 | blackbox *a=getBlackboxStuff(a1->Typ()); |
---|
166 | newstruct_desc nt=(newstruct_desc)a->data; |
---|
167 | lists al=(lists)a1->Data(); |
---|
168 | switch(op) |
---|
169 | { |
---|
170 | case '.': |
---|
171 | { |
---|
172 | if (a2->name!=NULL) |
---|
173 | { |
---|
174 | newstruct_member nm=nt->member; |
---|
175 | while ((nm!=NULL)&&(strcmp(nm->name,a2->name)!=0)) nm=nm->next; |
---|
176 | if (nm==NULL) |
---|
177 | { |
---|
178 | Werror("member %s nor found", a2->name); |
---|
179 | return TRUE; |
---|
180 | } |
---|
181 | if (RingDependend(nm->typ)) |
---|
182 | { |
---|
183 | if (al->m[nm->pos].data==NULL) |
---|
184 | { |
---|
185 | // NULL belongs to any ring |
---|
186 | ring r=(ring)al->m[nm->pos-1].data; |
---|
187 | if (r!=NULL) |
---|
188 | { |
---|
189 | r->ref--; |
---|
190 | al->m[nm->pos-1].data=NULL; |
---|
191 | al->m[nm->pos-1].rtyp=DEF_CMD; |
---|
192 | } |
---|
193 | } |
---|
194 | else |
---|
195 | { |
---|
196 | //Print("checking ring at pos %d for dat at pos %d\n",nm->pos-1,nm->pos); |
---|
197 | if ((al->m[nm->pos-1].data!=(void *)currRing) |
---|
198 | &&(al->m[nm->pos-1].data!=(void*)0L)) |
---|
199 | { |
---|
200 | Werror("different ring %lx(data) - %lx(basering)", |
---|
201 | (long unsigned)(al->m[nm->pos-1].data),(long unsigned)currRing); |
---|
202 | return TRUE; |
---|
203 | } |
---|
204 | } |
---|
205 | if ((currRing!=NULL)&&(al->m[nm->pos-1].data==NULL)) |
---|
206 | { |
---|
207 | // remember the ring, if not already set |
---|
208 | al->m[nm->pos-1].data=(void *)currRing; |
---|
209 | al->m[nm->pos-1].rtyp=RING_CMD; |
---|
210 | currRing->ref++; |
---|
211 | } |
---|
212 | } |
---|
213 | Subexpr r=(Subexpr)omAlloc0Bin(sSubexpr_bin); |
---|
214 | r->start = nm->pos+1; |
---|
215 | memcpy(res,a1,sizeof(sleftv)); |
---|
216 | memset(a1,0,sizeof(sleftv)); |
---|
217 | if (res->e==NULL) res->e=r; |
---|
218 | else |
---|
219 | { |
---|
220 | Subexpr sh=res->e; |
---|
221 | while (sh->next != NULL) sh=sh->next; |
---|
222 | sh->next=r; |
---|
223 | } |
---|
224 | return FALSE; |
---|
225 | } |
---|
226 | else |
---|
227 | { |
---|
228 | WerrorS("name expected"); |
---|
229 | return TRUE; |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | return blackboxDefaultOp2(op,res,a1,a2); |
---|
234 | } |
---|
235 | |
---|
236 | // BOOLEAN opM(int op, leftv res, leftv args) |
---|
237 | BOOLEAN newstruct_OpM(int op, leftv res, leftv args) |
---|
238 | { |
---|
239 | // interpreter: args->1. arg is newstruct |
---|
240 | blackbox *a=getBlackboxStuff(args->Typ()); |
---|
241 | switch(op) |
---|
242 | { |
---|
243 | case STRING_CMD: |
---|
244 | { |
---|
245 | res->data=(void *)a->blackbox_String(a,args->Data()); |
---|
246 | res->rtyp=STRING_CMD; |
---|
247 | return FALSE; |
---|
248 | } |
---|
249 | default: |
---|
250 | return blackbox_default_OpM(op,res,args); |
---|
251 | break; |
---|
252 | } |
---|
253 | return TRUE; |
---|
254 | } |
---|
255 | |
---|
256 | void newstruct_destroy(blackbox *b, void *d) |
---|
257 | { |
---|
258 | if (d!=NULL) |
---|
259 | { |
---|
260 | lists n=(lists)d; |
---|
261 | n->Clean(); |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | void *newstruct_Init(blackbox *b) |
---|
266 | { |
---|
267 | newstruct_desc n=(newstruct_desc)b->data; |
---|
268 | lists l=(lists)omAlloc0Bin(slists_bin); |
---|
269 | l->Init(n->size); |
---|
270 | newstruct_member nm=n->member; |
---|
271 | while (nm!=NULL) |
---|
272 | { |
---|
273 | l->m[nm->pos].rtyp=nm->typ; |
---|
274 | l->m[nm->pos].data=idrecDataInit(nm->typ); |
---|
275 | nm=nm->next; |
---|
276 | } |
---|
277 | return l; |
---|
278 | } |
---|
279 | |
---|
280 | BOOLEAN newstruct_Check(blackbox *b, void *d) |
---|
281 | { |
---|
282 | newstruct_desc n=(newstruct_desc)b->data; |
---|
283 | lists l=(lists)d; |
---|
284 | newstruct_member nm=n->member; |
---|
285 | while (nm!=NULL) |
---|
286 | { |
---|
287 | if ((l->m[nm->pos].rtyp!=nm->typ) |
---|
288 | &&( nm->typ!=DEF_CMD)) |
---|
289 | { |
---|
290 | Werror("type change in member %s (%s(%d) -> %s(%d))",nm->name, |
---|
291 | Tok2Cmdname(nm->typ),nm->typ, |
---|
292 | Tok2Cmdname(l->m[nm->pos].rtyp),l->m[nm->pos].rtyp); |
---|
293 | return TRUE; |
---|
294 | } |
---|
295 | nm=nm->next; |
---|
296 | } |
---|
297 | return FALSE; |
---|
298 | } |
---|
299 | |
---|
300 | void newstruct_setup(const char *n, newstruct_desc d ) |
---|
301 | { |
---|
302 | blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox)); |
---|
303 | // all undefined entries will be set to default in setBlackboxStuff |
---|
304 | // the default Print is quite usefule, |
---|
305 | // all other are simply error messages |
---|
306 | b->blackbox_destroy=newstruct_destroy; |
---|
307 | b->blackbox_String=newstruct_String; |
---|
308 | //b->blackbox_Print=blackbox_default_Print; |
---|
309 | b->blackbox_Init=newstruct_Init; |
---|
310 | b->blackbox_Copy=newstruct_Copy; |
---|
311 | b->blackbox_Assign=newstruct_Assign; |
---|
312 | //b->blackbox_Op1=blackboxDefaultOp1; |
---|
313 | b->blackbox_Op2=newstruct_Op2; |
---|
314 | //b->blackbox_Op3=blackbox_default_Op3; |
---|
315 | b->blackbox_OpM=newstruct_OpM; |
---|
316 | b->blackbox_Check=newstruct_Check; |
---|
317 | b->data=d; |
---|
318 | b->properties=1; // list_like |
---|
319 | int rt=setBlackboxStuff(b,n); |
---|
320 | d->id=rt; |
---|
321 | //Print("create type %d (%s)\n",rt,n); |
---|
322 | } |
---|
323 | |
---|
324 | static newstruct_desc scanNewstructFromString(const char *s, newstruct_desc res) |
---|
325 | { |
---|
326 | char *ss=omStrDup(s); |
---|
327 | char *p=ss; |
---|
328 | char *start; |
---|
329 | int t; |
---|
330 | char c; |
---|
331 | newstruct_member elem; |
---|
332 | |
---|
333 | idhdl save_ring=currRingHdl; |
---|
334 | currRingHdl=(idhdl)1; // fake ring detection |
---|
335 | loop |
---|
336 | { |
---|
337 | // read type: |
---|
338 | while (*p==' ') p++; |
---|
339 | start=p; |
---|
340 | while (isalpha(*p)) p++; |
---|
341 | *p='\0'; |
---|
342 | IsCmd(start,t); |
---|
343 | if (t==0) |
---|
344 | { |
---|
345 | Werror("unknown type `%s`",start); |
---|
346 | omFree(ss); |
---|
347 | omFree(res); |
---|
348 | currRingHdl=save_ring; |
---|
349 | return NULL; |
---|
350 | } |
---|
351 | if (RingDependend(t)) |
---|
352 | res->size++; // one additional field for the ring (before the data) |
---|
353 | //Print("found type %s at real-pos %d",start,res->size); |
---|
354 | elem=(newstruct_member)omAlloc0(sizeof(*elem)); |
---|
355 | // read name: |
---|
356 | p++; |
---|
357 | while (*p==' ') p++; |
---|
358 | start=p; |
---|
359 | while (isalpha(*p)) p++; |
---|
360 | c=*p; |
---|
361 | *p='\0'; |
---|
362 | elem->typ=t; |
---|
363 | elem->pos=res->size; |
---|
364 | if (*start=='\0') /*empty name*/ |
---|
365 | { |
---|
366 | WerrorS("empty name for element"); |
---|
367 | goto error_in_newstruct_def; |
---|
368 | } |
---|
369 | elem->name=omStrDup(start); |
---|
370 | //Print(" name:%s\n",start); |
---|
371 | elem->next=res->member; |
---|
372 | res->member=elem; |
---|
373 | res->size++; |
---|
374 | |
---|
375 | // next ? |
---|
376 | *p=c; |
---|
377 | while (*p==' ') p++; |
---|
378 | if (*p!=',') |
---|
379 | { |
---|
380 | if (*p!='\0') |
---|
381 | { |
---|
382 | Werror("unknown character in newstruct:>>%s<<",p); |
---|
383 | goto error_in_newstruct_def; |
---|
384 | } |
---|
385 | break; // end-of-list |
---|
386 | } |
---|
387 | p++; |
---|
388 | } |
---|
389 | omFree(ss); |
---|
390 | currRingHdl=save_ring; |
---|
391 | //Print("new type with %d elements\n",res->size); |
---|
392 | return res; |
---|
393 | error_in_newstruct_def: |
---|
394 | omFree(elem); |
---|
395 | omFree(ss); |
---|
396 | omFree(res); |
---|
397 | currRingHdl=save_ring; |
---|
398 | return NULL; |
---|
399 | } |
---|
400 | newstruct_desc newstructFromString(const char *s) |
---|
401 | { |
---|
402 | newstruct_desc res=(newstruct_desc)omAlloc0(sizeof(*res)); |
---|
403 | res->size=0; |
---|
404 | |
---|
405 | return scanNewstructFromString(s,res); |
---|
406 | } |
---|
407 | newstruct_desc newstructChildFromString(const char *parent, const char *s) |
---|
408 | { |
---|
409 | // find parent: |
---|
410 | int parent_id=0; |
---|
411 | blackboxIsCmd(parent,parent_id); |
---|
412 | if (parent_id<MAX_TOK) |
---|
413 | { |
---|
414 | Werror(">>%s< not found",parent); |
---|
415 | return NULL; |
---|
416 | } |
---|
417 | blackbox *parent_bb=getBlackboxStuff(parent_id); |
---|
418 | // check for the correct type: |
---|
419 | if (parent_bb->blackbox_destroy!=newstruct_destroy) |
---|
420 | { |
---|
421 | Werror(">>%s< is not a user defined type",parent); |
---|
422 | return NULL; |
---|
423 | } |
---|
424 | // setup for scanNewstructFromString: |
---|
425 | newstruct_desc res=(newstruct_desc)omAlloc0(sizeof(*res)); |
---|
426 | newstruct_desc parent_desc=(newstruct_desc)parent_bb->data; |
---|
427 | res->size=parent_desc->size; |
---|
428 | res->member=parent_desc->member; |
---|
429 | res->parent=parent_desc; |
---|
430 | |
---|
431 | return scanNewstructFromString(s,res); |
---|
432 | } |
---|