source: git/Singular/ipid.cc @ 2ba9a6

spielwiese
Last change on this file since 2ba9a6 was 2ba9a6, checked in by Kai Krüger <krueger@…>, 26 years ago
Implementation of new proc-scheme done. git-svn-id: file:///usr/local/Singular/svn/trunk@1033 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ipid.cc,v 1.9 1998-01-16 14:29:52 krueger Exp $ */
5
6/*
7* ABSTRACT: identfier handling
8*/
9
10#include <string.h>
11
12#include "mod2.h"
13#include "tok.h"
14#include "ipshell.h"
15#include "intvec.h"
16#include "febase.h"
17#include "mmemory.h"
18#include "numbers.h"
19#include "polys.h"
20#include "ring.h"
21#include "ideals.h"
22#include "matpol.h"
23#include "lists.h"
24#include "attrib.h"
25#include "silink.h"
26#include "syz.h"
27#include "ipid.h"
28
29idhdl idroot = NULL;
30idhdl currRingHdl = NULL;
31ring  currRing = NULL;
32ideal currQuotient = NULL;
33char* iiNoName="_";
34
35/*0 implementation*/
36
37idhdl idrec::get(const char * s, int lev)
38{
39  idhdl h = this;
40  idhdl found=NULL;
41  int l;
42  while (h!=NULL)
43  {
44    l=IDLEV(h);
45    mmTestLP(IDID(h));
46    if ((l==lev) && (0 == strcmp(s,IDID(h)))) return h;
47    if ((l==0) && (found==NULL) && (0 == strcmp(s,IDID(h))))
48    {
49      found=h;
50    }
51    h = IDNEXT(h);
52  }
53  return found;
54}
55
56//idrec::~idrec()
57//{
58//  if (id!=NULL)
59//  {
60//    FreeL((ADDRESS)id);
61//    id=NULL;
62//  }
63//  /* much more !! */
64//}
65
66idhdl idrec::set(char * s, int lev, idtyp t, BOOLEAN init)
67{
68  //printf("define %s, %x, lev: %d, typ: %d\n", s,s,lev,t);
69  idhdl h = (idrec *)Alloc0(sizeof(idrec));
70  int   len = 0;
71  IDID(h)   = s;
72  IDTYP(h)  = t;
73  IDLEV(h)  = lev;
74  IDNEXT(h) = this;
75  if (init)
76  {
77    switch (t)
78    {
79    //the type with init routines:
80      case INTVEC_CMD:
81      case INTMAT_CMD:
82        IDINTVEC(h) = new intvec();
83        break;
84      case NUMBER_CMD:
85        IDNUMBER(h) = nInit(0);
86        break;
87      case IDEAL_CMD:
88      case MODUL_CMD:
89        IDFLAG(h) = Sy_bit(FLAG_STD);
90      case MATRIX_CMD:
91        IDIDEAL(h) = idInit(1,1);
92        break;
93      case MAP_CMD:
94        IDIDEAL(h) = idInit(1,1);
95        IDMAP(h)->preimage = mstrdup(IDID(currRingHdl));
96        break;
97      case STRING_CMD:
98        IDSTRING(h) = mstrdup("");
99        break;
100      case LIST_CMD:
101        IDLIST(h)=(lists)Alloc(sizeof(slists));
102        IDLIST(h)->Init();
103        break;
104    //the types with the standard init: set the struct to zero
105      case LINK_CMD:
106        len=sizeof(ip_link);
107        break;
108      case RING_CMD:
109      case QRING_CMD:
110        len = sizeof(ip_sring);
111        break;
112      case PACKAGE_CMD:
113        len = sizeof(ip_package);
114        break;
115      case RESOLUTION_CMD:
116        len=sizeof(ssyStrategy);
117        break;   
118    //other types: without init (int,script,poly,def,package)
119    }
120    if (len!=0)
121    {
122      IDSTRING(h) = (char *)Alloc0(len);
123    }
124  }
125  if(t == PROC_CMD) {
126    IDPROC(h) = (procinfo *)Alloc(sizeof(procinfo));
127    memset(IDPROC(h),0,sizeof(*IDPROC(h)));
128    IDPROC(h)->language=LANG_NONE;
129  }
130  return  h;
131}
132
133idhdl enterid(char * s, int lev, idtyp t, idhdl* root, BOOLEAN init)
134{
135  idhdl h;
136  // is it the currRing - id ?
137  if ((currRingHdl!=NULL)
138  &&(IDLEV(currRingHdl)!=lev)
139  &&(s==IDID(currRingHdl)))
140  {
141    s=mstrdup(s);
142  }
143  // is it already defined in root ?
144  else if ((h=(*root)->get(s,lev))!=NULL)
145  {
146    if (IDLEV(h)!=lev)
147    {
148      s=mstrdup(s);
149    }
150    else if ((IDTYP(h) == t)||(t==DEF_CMD))
151    {
152      if (BVERBOSE(V_REDEFINE))
153        Warn("redefining %s **",s);
154      if (s==IDID(h))
155        IDID(h)=NULL;
156      killhdl(h,root);
157    }
158    else
159      goto errlabel;
160  }
161  // is it already defined in idroot ?
162  else if (*root != idroot)
163  {
164    if ((h=idroot->get(s,lev))!=NULL)
165    {
166      if (IDLEV(h)!=lev)
167      {
168        s=mstrdup(s);
169      }
170      else if ((IDTYP(h) == t)||(t==DEF_CMD))
171      {
172        if (BVERBOSE(V_REDEFINE))
173          Warn("redefining %s **",s);
174        IDID(h)=NULL;
175        killhdl(h,&idroot);
176      }
177      else
178      {
179        goto errlabel;
180      }
181    }
182  }
183  // is it already defined in currRing->idroot ?
184  else if ((currRing!=NULL)&&((*root) != currRing->idroot))
185  {
186    if ((h=currRing->idroot->get(s,lev))!=NULL)
187    {
188      if (IDLEV(h)!=lev)
189      {
190        s=mstrdup(s);
191      }
192      else if ((IDTYP(h) == t)||(t==DEF_CMD))
193      {
194        if (BVERBOSE(V_REDEFINE))
195          Warn("redefining %s **",s);
196        IDID(h)=NULL;
197        killhdl(h,&currRing->idroot);
198      }
199      else
200      {
201        goto errlabel;
202      }
203    }
204  }
205  return *root = (*root)->set(s, lev, t, init);
206
207  errlabel:
208    Werror("identifier `%s` in use",s);
209    return NULL;
210}
211
212void killid(char * id, idhdl * ih)
213{
214  if (id!=NULL)
215  {
216    idhdl h = (*ih)->get(id,myynest);
217
218    // id not found in global list, is it defined in current ring ?
219    if (h==NULL)
220    {
221      if ((currRing!=NULL) && (*ih != (currRing->idroot)))
222      {
223        h = currRing->idroot->get(id,myynest);
224        if (h!=NULL)
225        {
226          killhdl(h,&(currRing->idroot));
227          return;
228        }
229      }
230      Werror("`%s` is not defined",id);
231      return;
232    }
233    killhdl(h,ih);
234  }
235  else
236    Werror("kill what ?");
237}
238
239void killhdl(idhdl h)
240{
241  int t=IDTYP(h);
242  if ((BEGIN_RING<t) && (t<END_RING) && (t!=QRING_CMD))
243    killhdl(h,&currRing->idroot);
244  else
245  {
246    idhdl s=idroot;
247    while ((s!=h) && (s!=NULL)) s=s->next;
248    if (s==NULL) killhdl(h,&currRing->idroot);
249    else killhdl(h,&idroot);
250  }
251}
252
253void killhdl(idhdl h, idhdl * ih)
254{
255  //printf("kill %s, id %x, typ %d lev: %d\n",IDID(h),(int)IDID(h),IDTYP(h),IDLEV(h));
256  idhdl hh;
257  BOOLEAN killOtherRing = TRUE;
258  BOOLEAN needResetRing = FALSE;
259
260  if (h->attribute!=NULL)
261  {
262    atKillAll(h);
263    //h->attribute=NULL;
264  }
265  // ring / qring  --------------------------------------------------------
266  // package  -------------------------------------------------------------
267  if ((IDTYP(h) == RING_CMD) || (IDTYP(h) == QRING_CMD)
268  || (IDTYP(h) == PACKAGE_CMD) )
269  {
270    idhdl savecurrRingHdl = currRingHdl;
271    ring  savecurrRing = currRing;
272
273    // any objects defined for this ring ?
274    if (((IDTYP(h)==PACKAGE_CMD) || (IDRING(h)->ref<=0))
275    &&  (IDRING(h)->idroot!=NULL))
276    {
277      idhdl * hd = &IDRING(h)->idroot;
278      idhdl  hdh = IDNEXT(*hd);
279      idhdl  temp;
280      killOtherRing=(IDTYP(h)!=PACKAGE_CMD) && (IDRING(h)!=currRing);
281      if (killOtherRing) //we are not killing the base ring, so switch
282      {
283        needResetRing=TRUE;
284        rSetHdl(h,FALSE);
285        /* no complete init*/
286      }
287      while (hdh!=NULL)
288      {
289        temp = IDNEXT(hdh);
290        killhdl(hdh,&(IDRING(h)->idroot));
291        hdh = temp;
292      }
293      killhdl(*hd,hd);
294    }
295    // reset currRing ?
296    if (needResetRing) // && (killOtherRing)
297    {
298      //we have to switch back to the base ring
299      //currRing = savecurrRing;
300      //currRingHdl = savecurrRingHdl;
301      if (savecurrRing!=NULL)
302      {
303        rSetHdl(savecurrRingHdl,TRUE);
304      }
305    }
306    rKill(h);
307  }
308  // poly / vector -------------------------------------------------------
309  else if ((IDTYP(h) == POLY_CMD) || (IDTYP(h) == VECTOR_CMD))
310  {
311    pDelete(&IDPOLY(h));
312  }
313  // ideal / module/ matrix / map ----------------------------------------
314  else if ((IDTYP(h) == IDEAL_CMD)
315           || (IDTYP(h) == MODUL_CMD)
316           || (IDTYP(h) == MATRIX_CMD)
317           || (IDTYP(h) == MAP_CMD))
318  {
319    ideal iid = IDIDEAL(h);
320    if (IDTYP(h) == MAP_CMD)
321    {
322      map im = IDMAP(h);
323      FreeL((ADDRESS)im->preimage);
324    }
325    idDelete(&iid);
326  }
327  // string -------------------------------------------------------------
328  else if (IDTYP(h) == STRING_CMD)
329  {
330    FreeL((ADDRESS)IDSTRING(h));
331    //IDSTRING(h)=NULL;
332  }
333  // proc ---------------------------------------------------------------
334  else if (IDTYP(h) == PROC_CMD)
335  {
336    piKill(IDPROC(h));
337  }
338  // number -------------------------------------------------------------
339  else if (IDTYP(h) == NUMBER_CMD)
340  {
341    nDelete(&IDNUMBER(h));
342  }
343  // intvec / intmat  ---------------------------------------------------
344  else if ((IDTYP(h) == INTVEC_CMD)||(IDTYP(h) == INTMAT_CMD))
345  {
346    delete IDINTVEC(h);
347  }
348  // list  -------------------------------------------------------------
349  else if (IDTYP(h)==LIST_CMD)
350  {
351    IDLIST(h)->Clean();
352    //Free((ADDRESS)IDLIST(h)->m, (IDLIST(h)->nr+1)*sizeof(sleftv));
353    //Free((ADDRESS)IDLIST(h), sizeof(slists));
354  }
355  // link  -------------------------------------------------------------
356  else if (IDTYP(h)==LINK_CMD)
357  {
358    slKill(IDLINK(h));
359  }
360  else if((IDTYP(h)==RESOLUTION_CMD)&&(IDDATA(h)!=NULL))
361  {
362    syKillComputation((syStrategy)IDDATA(h));
363  }
364#ifdef TEST
365  else if ((IDTYP(h)!= INT_CMD)&&(IDTYP(h)!=DEF_CMD))
366    Warn("unknown type to kill: %s(%d)",Tok2Cmdname(IDTYP(h)),IDTYP(h));
367#endif
368
369  //  general  -------------------------------------------------------------
370  // now dechain it and delete idrec
371  FreeL((ADDRESS)IDID(h));
372  //IDID(h)=NULL;
373  if (h == (*ih))
374  {
375    // h is at the beginning of the list
376    *ih = IDNEXT(*ih);
377  }
378  else
379  {
380    // h is somethere in the list:
381    hh = *ih;
382    loop
383    {
384      idhdl hhh = IDNEXT(hh);
385      if (hhh == h)
386      {
387        IDNEXT(hh) = IDNEXT(hhh);
388        break;
389      }
390      hh = hhh;
391    }
392  }
393  Free((ADDRESS)h,sizeof(idrec));
394}
395
396idhdl ggetid(const char *n)
397{
398  idhdl h = idroot->get(n,myynest);
399  idhdl h2=NULL;
400  if ((currRing!=NULL) && ((h==NULL)||(IDLEV(h)!=myynest)))
401  {
402    h2 = currRing->idroot->get(n,myynest);
403  }
404  if (h2==NULL) return h;
405  return h2;
406}
407
408void ipListFlag(idhdl h)
409{
410  if (hasFlag(h,FLAG_STD)) PrintS(" (SB)");
411  if (hasFlag(h,FLAG_DRING)) PrintS(" (D)");
412}
413
414lists ipNameList(idhdl root)
415{
416  idhdl h=root;
417  /* compute the length */
418  int l=0;
419  while (h!=NULL) { l++; h=IDNEXT(h); }
420  /* allocate list */
421  lists L=(lists)Alloc(sizeof(slists));
422  L->Init(l);
423  /* copy names */
424  h=root;
425  l=0;
426  while (h!=NULL)
427  {
428    /* list is initialized with 0 => no need to clear anything */
429    L->m[l].rtyp=STRING_CMD;
430    L->m[l].data=mstrdup(IDID(h));
431    l++;
432    h=IDNEXT(h);
433  }
434  return L;
435}
436
437/*
438* move 'tomove' from root1 list to root2 list
439*/
440static void ipSwapId(idhdl tomove, idhdl &root1, idhdl &root2)
441{
442  idhdl h;
443  /* search 'tomove' in root2 : if found -> do nothing */
444  h=root2;
445  while ((h!=NULL) && (h!=tomove)) h=IDNEXT(h);
446  if (h!=NULL) return;
447  /* search predecessor of h in root1, remove 'tomove' */
448  h=root1;
449  if (tomove==h)
450  {
451    root1=IDNEXT(h);
452  }
453  else
454  {
455    while ((h!=NULL) && (IDNEXT(h)!=tomove)) h=IDNEXT(h);
456    if (h==NULL) return; /* not in the list root1 -> do nothing */
457    IDNEXT(h)=IDNEXT(tomove);
458  }
459  /* add to root2 list */
460  IDNEXT(tomove)=root2;
461  root2=tomove;
462}
463
464void  ipMoveId(idhdl tomove)
465{
466  if ((currRing!=NULL)&&(tomove!=NULL))
467  {
468    if (((QRING_CMD!=IDTYP(tomove)) && (BEGIN_RING<IDTYP(tomove)) && (IDTYP(tomove)<END_RING))
469    || ((IDTYP(tomove)==LIST_CMD) && (lRingDependend(IDLIST(tomove)))))
470    {
471      /*move 'tomove' to ring id's*/
472      ipSwapId(tomove,idroot,currRing->idroot);
473    }
474    else
475    {
476      /*move 'tomove' to global id's*/
477      ipSwapId(tomove,currRing->idroot,idroot);
478    }
479  }
480}
481
482char * piProcinfo(procinfov pi, char *request)
483{
484  if(pi == NULL) return "empty proc";
485  else if (strcmp(request, "libname")  == 0) return pi->libname;
486  else if (strcmp(request, "procname") == 0) return pi->procname;
487  else if (strcmp(request, "type")     == 0) {
488    switch (pi->language) {
489      case LANG_SINGULAR: return "singular"; break;
490      case LANG_C:        return "object";   break;
491      case LANG_NONE:     return "none";     break;
492      default:            return "unknow language";
493    }
494  } else if (strcmp(request, "ref")      == 0) {
495    char p[8];
496    sprintf(p, "%d\0", pi->ref);
497    return mstrdup(p);
498  }
499
500}
501
502void piCleanUp(procinfov pi)
503{
504  (pi->ref)--;
505  if (pi->ref <= 0)
506  {
507    FreeL((ADDRESS)pi->libname);
508    FreeL((ADDRESS)pi->procname);
509    if( pi->language == LANG_SINGULAR) { 
510      FreeL((ADDRESS)pi->data.s.body);
511    }
512    if( pi->language == LANG_C) {
513    }
514    memset((void *) pi, 0, sizeof(procinfo));
515    pi->language=LANG_NONE;
516  }
517}
518
519void piKill(procinfov pi)
520{
521  piCleanUp(pi);
522  if (pi->ref <= 0)
523    Free((ADDRESS)pi, sizeof(procinfo));
524}
525
Note: See TracBrowser for help on using the repository browser.