source: git/Singular/subexpr.cc @ 254e61

spielwiese
Last change on this file since 254e61 was 254e61, checked in by Hans Schoenemann <hannes@…>, 10 years ago
fix: tr.#644 (string in newstruct)
  • Property mode set to 100644
File size: 44.7 KB
RevLine 
[0e1846]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
[6ae4f5]5* ABSTRACT: handling of leftv
[0e1846]6*/
7
[b1dfaf]8#include <kernel/mod2.h>
[5ff1d3]9
10#include <omalloc/omalloc.h>
11
[0fb34ba]12#include <misc/intvec.h>
13#include <misc/options.h>
[5ff1d3]14
15
16#include <coeffs/ffields.h>
17#include <coeffs/numbers.h>
[75f10d]18#include <coeffs/bigintmat.h>
[5ff1d3]19
[0fb34ba]20#include <polys/monomials/maps.h>
21#include <polys/matpol.h>
[5ff1d3]22#include <polys/monomials/ring.h>
[737a68]23#include <kernel/polys.h>
[5ff1d3]24
[14814e3]25#include <coeffs/longrat.h>
[5ff1d3]26// #include <coeffs/longrat.h>
27
28#include <kernel/ideals.h>
[57fa2c4]29#include <kernel/GBEngine/kstd1.h>
[c5e82e]30#include <kernel/oswrapper/timer.h>
[57fa2c4]31#include <kernel/GBEngine/syz.h>
[5ff1d3]32
33#include <Singular/tok.h>
34#include <Singular/ipid.h>
[599326]35#include <Singular/ipshell.h>
36#include <Singular/lists.h>
37#include <Singular/attrib.h>
[44ca2f]38#include <Singular/links/silink.h>
[599326]39#include <Singular/attrib.h>
40#include <Singular/subexpr.h>
[b117e8]41#include <Singular/blackbox.h>
[9b12fce]42#include <Singular/number2.h>
[855a8f]43
[fd1b1be]44#include <stdlib.h>
45#include <stdio.h>
46#include <string.h>
47#include <ctype.h>
48#include <unistd.h>
[5ff1d3]49
[0e051f]50omBin sSubexpr_bin = omGetSpecBin(sizeof(_ssubexpr));
[c232af]51omBin sleftv_bin = omGetSpecBin(sizeof(sleftv));
52omBin procinfo_bin = omGetSpecBin(sizeof(procinfo));
53omBin libstack_bin = omGetSpecBin(sizeof(libstack));
[ec7aac]54static omBin size_two_bin = omGetSpecBin(2);
[c232af]55
[0e1846]56sleftv     sLastPrinted;
57const char sNoName[]="_";
58#ifdef SIQ
59BOOLEAN siq=FALSE;
60#endif
61
62int sleftv::listLength()
63{
64  int n = 1;
65  leftv sl = next;
66  while (sl!=NULL)
67  {
68    n++;
69    sl=sl->next;
70  }
71  return n;
72}
73
74void sleftv::Print(leftv store, int spaces)
75{
76  int  t=Typ();
77  if (errorreported) return;
78#ifdef SIQ
79  if (rtyp==COMMAND)
80  {
81    command c=(command)data;
82    char ch[2];
83    ch[0]=c->op;ch[1]='\0';
[85e68dd]84    const char *s=ch;
[2c89d2]85    if (c->op>127) s=iiTwoOps(c->op);
[0e1846]86    ::Print("##command %d(%s), %d args\n",
87      c->op, s, c->argc);
88    if (c->argc>0)
89      c->arg1.Print(NULL,spaces+2);
90    if(c->argc<4)
91    {
92      if (c->argc>1)
93        c->arg2.Print(NULL,spaces+2);
94      if (c->argc>2)
95        c->arg3.Print(NULL,spaces+2);
96    }
97    PrintS("##end");
98  }
99  else
100#endif
101  {
[2ba9a6]102    const char *n=Name();
103    char *s;
104    void *d=Data();
[49fbc3f]105    if (errorreported) return;
[2ba9a6]106    if ((store!=NULL)&&(store!=this))
107      store->CleanUp();
[a3432c]108
[2ba9a6]109    switch (t /*=Typ()*/)
[0e1846]110      {
[9b12fce]111#ifdef SINGULAR_4_1
112        case CRING_CMD:
113          crPrint((coeffs)d);
114          break;
[4a51c1]115        case CNUMBER_CMD:
116          n2Print((number2)d);
117          break;
[2f1afc]118        case CMATRIX_CMD: // like BIGINTMAT
[9b12fce]119#endif
[2f1afc]120        case BIGINTMAT_CMD:
121          ((bigintmat *)d)->pprint(80);
122          break;
[0e1846]123        case UNKNOWN:
124        case DEF_CMD:
[490a23b]125          PrintNSpaces(spaces);
126          PrintS("`");PrintS(n);PrintS("`");
[0e1846]127          break;
[95197e]128        case PACKAGE_CMD:
[4c9cf1]129          PrintNSpaces(spaces);
130          paPrint(n,(package)d);
131          break;
[0e1846]132        case NONE:
133          return;
134        case INTVEC_CMD:
135        case INTMAT_CMD:
[6d3ffec]136          ((intvec *)d)->show(t,spaces);
[0e1846]137          break;
138        case RING_CMD:
139        case QRING_CMD:
[db34980]140        {
[490a23b]141          PrintNSpaces(spaces);
[db34980]142          const ring r = (const ring)d;
[fb85f97]143          rWrite(r, currRing == r);
[0e1846]144          break;
[db34980]145        }
[0e1846]146        case MATRIX_CMD:
[8fa9ea]147          iiWriteMatrix((matrix)d,n,2, currRing, spaces);
[0e1846]148          break;
149        case MODUL_CMD:
150        case IDEAL_CMD:
[ac00e2f]151          if ((TEST_V_QRING)  &&(currRing->qideal!=NULL)
[3e7095]152          &&(!hasFlag(this,FLAG_QRING)))
[6c4db17]153          {
154            jjNormalizeQRingId(this);
155            d=Data();
156          }
157          // no break:
158        case MAP_CMD:
[8fa9ea]159          iiWriteMatrix((matrix)d,n,1, currRing, spaces);
[0e1846]160          break;
161        case POLY_CMD:
162        case VECTOR_CMD:
[ac00e2f]163          if ((TEST_V_QRING)  &&(currRing->qideal!=NULL)
[3e7095]164          &&(!hasFlag(this,FLAG_QRING)))
[6c4db17]165          {
166            jjNormalizeQRingP(this);
167            d=Data();
168          }
[490a23b]169          PrintNSpaces(spaces);
[0e1846]170          pWrite0((poly)d);
171          break;
[dfc6b54]172        case RESOLUTION_CMD:
[22579cf]173        {
[e7463f]174          syStrategy tmp=(syStrategy)d;
[9b01b1]175          syPrint(tmp,IDID(currRingHdl));
[a3432c]176          break;
[22579cf]177        }
[0e1846]178        case STRING_CMD:
[490a23b]179          PrintNSpaces(spaces);
180          PrintS((char *)d);
[0e1846]181          break;
182       case INT_CMD:
[490a23b]183          PrintNSpaces(spaces);
184          ::Print("%d",(int)(long)d);
[0e1846]185          break;
[2ba9a6]186       case PROC_CMD:
187         {
188           procinfov pi=(procinfov)d;
[490a23b]189
190           PrintNSpaces(spaces);
191           PrintS("// libname  : ");
192           PrintS(piProcinfo(pi, "libname"));
[17681b]193           PrintLn();
[490a23b]194
195           PrintNSpaces(spaces);
196           PrintS("// procname : ");
197           PrintS(piProcinfo(pi, "procname"));
[17681b]198           PrintLn();
[490a23b]199
200           PrintNSpaces(spaces);
201           PrintS("// type     : ");
202           PrintS(piProcinfo(pi, "type"));
[a3432c]203           //           ::Print("%-*.*s// ref      : %s",spaces,spaces," ",
204           //   piProcinfo(pi, "ref"));
205           break;
[2ba9a6]206         }
[0e1846]207       case LINK_CMD:
208          {
209            si_link l=(si_link)d;
[17681b]210            PrintNSpaces(spaces);
[490a23b]211            ::Print("// type : %s\n", slStatus(l, "type"));
[17681b]212            PrintNSpaces(spaces);
[490a23b]213            ::Print("// mode : %s\n", slStatus(l, "mode"));
[17681b]214            PrintNSpaces(spaces);
[490a23b]215            ::Print("// name : %s\n", slStatus(l, "name"));
[17681b]216            PrintNSpaces(spaces);
[490a23b]217            ::Print("// open : %s\n", slStatus(l, "open"));
[17681b]218            PrintNSpaces(spaces);
[490a23b]219            ::Print("// read : %s\n", slStatus(l, "read"));
[17681b]220            PrintNSpaces(spaces);
[490a23b]221            ::Print("// write: %s", slStatus(l, "write"));
[d754b7]222          break;
[0e1846]223          }
224        case NUMBER_CMD:
[84a6a7]225        case BIGINT_CMD:
[0e1846]226          s=String(d);
227          if (s==NULL) return;
[17681b]228          PrintNSpaces(spaces);
[0e1846]229          PrintS(s);
[c232af]230          omFree((ADDRESS)s);
[0e1846]231          break;
232        case LIST_CMD:
233        {
234          lists l=(lists)d;
[ecf019]235          if (lSize(l)<0)
[17681b]236          {
237             PrintNSpaces(spaces);
[490a23b]238             PrintS("empty list\n");
[17681b]239          }
[0e1846]240          else
241          {
242            int i=0;
243            for (;i<=l->nr;i++)
244            {
245              if (l->m[i].rtyp!=DEF_CMD)
246              {
[17681b]247                PrintNSpaces(spaces);
[490a23b]248                ::Print("[%d]:\n",i+1);
[0e1846]249                l->m[i].Print(NULL,spaces+3);
250              }
251            }
252          }
253          break;
254        }
[b117e8]255
[0e1846]256        default:
[c4dab4]257          if (t>MAX_TOK)
258          {
259            blackbox * bb=getBlackboxStuff(t);
[b117e8]260            PrintNSpaces(spaces);
[c4dab4]261            if (bb!=NULL) { bb->blackbox_Print(bb,d); }
262            else          { ::Print("Print: blackbox %d(bb=NULL)",t); }
263          }
264          else
[0e1846]265          ::Print("Print:unknown type %s(%d)", Tok2Cmdname(t),t);
266      } /* end switch: (Typ()) */
267  }
268  if (next!=NULL)
269  {
270    if (t==COMMAND) PrintLn();
271    else if (t!=LIST_CMD) PrintS(" ");
272    next->Print(NULL,spaces);
273  }
274  else if (t!=LIST_CMD)
275  {
276    PrintLn();
[a3432c]277  }
[0e1846]278#ifdef SIQ
279  if (rtyp!=COMMAND)
280#endif
281  {
282    if ((store!=NULL)
[e7463f]283    && (store!=this))
[0e1846]284    {
[e7463f]285      if((t/*Typ()*/!=LINK_CMD)
286      && (t/*Typ()*/!=PACKAGE_CMD)
287      && (t/*Typ()*/!=DEF_CMD)
288      )
[3074334]289      {
[e7463f]290        store->rtyp=t/*Typ()*/;
291        store->data=CopyD();
292        if(attribute!=NULL)
293        {
294          store->attribute=CopyA();
295        }
296        store->flag=flag;
[3074334]297      }
[0e1846]298    }
299  }
300}
301
[16acb0]302void sleftv::CleanUp(ring r)
[0e1846]303{
[ea947e]304  if ((name!=NULL) && (name!=sNoName) && (rtyp!=IDHDL) && (rtyp!=ALIAS_CMD))
[0e1846]305  {
306    //::Print("free %x (%s)\n",name,name);
[c232af]307    omFree((ADDRESS)name);
[0e1846]308  }
[f2dff02]309  //name=NULL;
310  //flag=0;
[0e1846]311  if (data!=NULL)
312  {
[c4dab4]313     if (rtyp==IDHDL) attribute=NULL; // is only a pointer to attribute of id
314     else s_internalDelete(rtyp,data,r);
[ff7e529]315    //data=NULL; // will be done by Init() at the end
[0e1846]316  }
317  if (attribute!=NULL)
318  {
319    switch (rtyp)
320    {
321      case PACKAGE_CMD:
322      case IDHDL:
323      case ANY_TYPE:
324      case VECHO:
325      case VPRINTLEVEL:
326      case VCOLMAX:
327      case VTIMER:
[7a7652]328      case VRTIMER:
[0e1846]329      case VOICE:
330      case VMAXDEG:
331      case VMAXMULT:
332      case TRACE:
333      case VSHORTOUT:
334      case VNOETHER:
335      case VMINPOLY:
[7a7652]336      case LIB_CMD:
[0e1846]337      case 0:
[ff7e529]338        //attribute=NULL; // will be done by Init() at the end
[0e1846]339        break;
340      default:
341      {
[c227a2f]342        attribute->killAll(r);
[0e1846]343      }
344    }
345  }
346  Subexpr h;
347  while (e!=NULL)
348  {
349    h=e->next;
[c232af]350    omFreeBin((ADDRESS)e, sSubexpr_bin);
[0e1846]351    e=h;
352  }
[ff7e529]353  //rtyp=NONE; // will be done by Init() at the end
[0e1846]354  if (next!=NULL)
355  {
[df4422]356    leftv tmp_n;
357    do
358    {
359      tmp_n=next->next;
360      //next->name=NULL;
361      next->next=NULL;
[16acb0]362      next->CleanUp(r);
[c232af]363      omFreeBin((ADDRESS)next, sleftv_bin);
[df4422]364      next=tmp_n;
365    } while (next!=NULL);
[0e1846]366  }
[f2dff02]367  Init();
[0e1846]368}
369
[4cbe5d]370BOOLEAN sleftv::RingDependend()
371{
372  int rt=Typ();
[df5fc1]373  if(::RingDependend(rt) && (rt!=QRING_CMD))
[4cbe5d]374    return TRUE;
375  if (rt==LIST_CMD)
376    return lRingDependend((lists)Data());
[3907ab]377  if (this->next!=NULL)
378    return this->next->RingDependend();
[a3bc95e]379  return FALSE;
[4cbe5d]380}
381
[a1a595]382static inline void * s_internalCopy(const int t,  void *d)
[0e1846]383{
[dfc6b54]384  switch (t)
[0e1846]385  {
[9b12fce]386#ifdef SINGULAR_4_1
387    case CRING_CMD:
388      {
389        coeffs cf=(coeffs)d;
390        cf->ref++;
391        return (void*)d;
392      }
[4a51c1]393    case CNUMBER_CMD:
394      return (void*)n2Copy((number2)d);
[2f1afc]395    case CMATRIX_CMD: // like BIGINTMAT
[9b12fce]396#endif
[2f1afc]397    case BIGINTMAT_CMD:
398      return (void*)bimCopy((bigintmat *)d);
[0e1846]399    case INTVEC_CMD:
400    case INTMAT_CMD:
401      return (void *)ivCopy((intvec *)d);
402    case MATRIX_CMD:
[8fa9ea]403      return (void *)mp_Copy((matrix)d, currRing);
[0e1846]404    case IDEAL_CMD:
405    case MODUL_CMD:
406      return  (void *)idCopy((ideal)d);
407    case STRING_CMD:
[c232af]408        return (void *)omStrDup((char *)d);
[daeb6d]409    case PACKAGE_CMD:
410      return  (void *)paCopy((package) d);
[0e1846]411    case PROC_CMD:
[2ba9a6]412      return  (void *)piCopy((procinfov) d);
[0e1846]413    case POLY_CMD:
414    case VECTOR_CMD:
415      return  (void *)pCopy((poly)d);
416    case INT_CMD:
417      return  d;
418    case NUMBER_CMD:
419      return  (void *)nCopy((number)d);
[84a6a7]420    case BIGINT_CMD:
[4ff07f]421      return  (void *)n_Copy((number)d, coeffs_BIGINT);
[0e1846]422    case MAP_CMD:
[8fa9ea]423      return  (void *)maCopy((map)d, currRing);
[0e1846]424    case LIST_CMD:
425      return  (void *)lCopy((lists)d);
[e6969d]426    case LINK_CMD:
427      return (void *)slCopy((si_link) d);
[2f12a6f]428    case RING_CMD:
429    case QRING_CMD:
430      {
431        ring r=(ring)d;
[56aaae]432        if (r!=NULL) r->ref++;
[e7463f]433        //Print("+  ring %d, ref %d\n",r,r->ref);
[2f12a6f]434        return d;
435      }
[dfc6b54]436    case RESOLUTION_CMD:
437      return (void*)syCopy((syStrategy)d);
[0e1846]438    case DEF_CMD:
439    case NONE:
[8739c9]440    case 0: /* type in error case */
[0e1846]441      break; /* error recovery: do nothing */
442    //case COMMAND:
443    default:
[b117e8]444    {
445      if (t>MAX_TOK)
446      {
447        blackbox *b=getBlackboxStuff(t);
[c4dab4]448        if (b!=NULL) return b->blackbox_Copy(b,d);
[b117e8]449        return NULL;
450      }
451      else
[b70e54]452      Warn("s_internalCopy: cannot copy type %s(%d)",
[a1a595]453            Tok2Cmdname(t),t);
[b117e8]454    }
[0e1846]455  }
456  return NULL;
457}
458
[c4dab4]459void s_internalDelete(const int t,  void *d, const ring r)
460{
[6559a1]461  assume(d!=NULL);
[c4dab4]462  switch (t)
463  {
[9b12fce]464#ifdef SINGULAR_4_1
465    case CRING_CMD:
466      nKillChar((coeffs)d);
467      break;
[4a51c1]468    case CNUMBER_CMD:
469      {
470        number2 n=(number2)d;
471        n2Delete(n);
472        break;
473      }
[2f1afc]474    case CMATRIX_CMD: //like BIGINTMAT
[9b12fce]475#endif
[2f1afc]476    case BIGINTMAT_CMD:
[c4dab4]477    {
[2f1afc]478      bigintmat *v=(bigintmat*)d;
[c4dab4]479      delete v;
480      break;
481    }
[2f1afc]482    case INTVEC_CMD:
483    case INTMAT_CMD:
[75f10d]484    {
[2f1afc]485      intvec *v=(intvec*)d;
[75f10d]486      delete v;
487      break;
488    }
[c4dab4]489    case MAP_CMD:
490    {
491      map m=(map)d;
[95197e]492      omFreeBinAddr((ADDRESS)m->preimage);
[c4dab4]493      m->preimage=NULL;
494      /* no break: continue as IDEAL*/
495    }
496    case MATRIX_CMD:
497    case IDEAL_CMD:
498    case MODUL_CMD:
499    {
500      ideal i=(ideal)d;
501      id_Delete(&i,r);
502      break;
503    }
504    case STRING_CMD:
505      omFree(d);
506      break;
507    //case PACKAGE_CMD:
508    //  return  (void *)paCopy((package) d);
509    case PROC_CMD:
510      piKill((procinfo*)d);
511      break;
512    case POLY_CMD:
513    case VECTOR_CMD:
514    {
515      poly p=(poly)d;
516      p_Delete(&p,r);
517      break;
518    }
519    case NUMBER_CMD:
520    {
521      number n=(number)d;
522      n_Delete(&n,r);
523      break;
524    }
525    case BIGINT_CMD:
526    {
527      number n=(number)d;
[4ff07f]528      n_Delete(&n,coeffs_BIGINT);
[c4dab4]529      break;
530    }
531    case LIST_CMD:
532    {
533      lists l=(lists)d;
534      l->Clean(r);
535      break;
536    }
537    case LINK_CMD:
538    {
539      si_link l=(si_link)d;
540      slKill(l);
541      break;
542    }
543    case RING_CMD:
544    case QRING_CMD:
545    {
546      ring R=(ring)d;
[7ddcb2]547      if ((R!=currRing)||(R->ref>=0))
548        rKill(R);
[c4dab4]549      #ifdef TEST
[172d18c]550      else
[e7463f]551        Print("currRing? ref=%d\n",R->ref);
[c4dab4]552      #endif
553      break;
554    }
555    case RESOLUTION_CMD:
556    {
557      syStrategy s=(syStrategy)d;
558      if (s!=NULL) syKillComputation(s,r);
559      break;
560    }
561    case COMMAND:
562    {
563     command cmd=(command)d;
564     if (cmd->arg1.rtyp!=0) cmd->arg1.CleanUp(r);
565     if (cmd->arg2.rtyp!=0) cmd->arg2.CleanUp(r);
566     if (cmd->arg3.rtyp!=0) cmd->arg3.CleanUp(r);
567     omFreeBin((ADDRESS)d, sip_command_bin);
568     break;
569    }
570    case INT_CMD:
571    case DEF_CMD:
572    case ALIAS_CMD:
573    case PACKAGE_CMD:
574    case IDHDL:
575    case NONE:
576    case ANY_TYPE:
577    case VECHO:
578    case VPRINTLEVEL:
579    case VCOLMAX:
580    case VTIMER:
581    case VRTIMER:
582    case VOICE:
583    case VMAXDEG:
584    case VMAXMULT:
585    case TRACE:
586    case VSHORTOUT:
587    case VNOETHER:
588    case VMINPOLY:
589    case LIB_CMD:
590    case 0: /* type in error case */
591      break; /* error recovery: do nothing */
592    //case COMMAND:
593    //case COMMAND:
594    default:
595    {
596      if (t>MAX_TOK)
597      {
598        blackbox *b=getBlackboxStuff(t);
599        if (b!=NULL) b->blackbox_destroy(b,d);
600        break;
601      }
602      else
603      Warn("s_internalDelete: cannot delete type %s(%d)",
604            Tok2Cmdname(t),t);
605    }
606  }
607}
[f71681]608
[a1a595]609void * slInternalCopy(leftv source, const int t, void *d, Subexpr e)
610{
611  if (t==STRING_CMD)
612  {
613      if ((e==NULL)
614      || (source->rtyp==LIST_CMD)
[254e61]615      || ((source->rtyp==IDHDL)
616          &&((IDTYP((idhdl)source->data)==LIST_CMD)
617            || (IDTYP((idhdl)source->data)>MAX_TOK)))
618      || (source->rtyp>MAX_TOK))
[a1a595]619        return (void *)omStrDup((char *)d);
620      else if (e->next==NULL)
621      {
622        char *s=(char*)omAllocBin(size_two_bin);
623        s[0]=*(char *)d;
624        s[1]='\0';
625        return s;
626      }
627      #ifdef TEST
628      else
629      {
630        Werror("not impl. string-op in `%s`",my_yylinebuf);
631        return NULL;
632      }
633      #endif
634  }
635  return s_internalCopy(t,d);
636}
637
[0e1846]638void sleftv::Copy(leftv source)
639{
[f2dff02]640  Init();
[0e1846]641  rtyp=source->Typ();
642  void *d=source->Data();
643  if(!errorreported)
644  {
[a1a595]645    data=s_internalCopy(rtyp,d);
[8c94ba]646    if ((source->attribute!=NULL)||(source->e!=NULL))
647      attribute=source->CopyA();
648    flag=source->flag;
[24c186a]649    if (source->next!=NULL)
650    {
[c232af]651      next=(leftv)omAllocBin(sleftv_bin);
[24c186a]652      next->Copy(source->next);
653    }
[0e1846]654  }
655}
656
657void * sleftv::CopyD(int t)
658{
[98adcd]659  if ((rtyp!=IDHDL)&&(rtyp!=ALIAS_CMD)&&(e==NULL))
[0e1846]660  {
[a3432c]661    if (iiCheckRing(t)) return NULL;
[8b87135]662    void *x = data;
663    if (rtyp==VNOETHER) x = (void *)pCopy((currRing->ppNoether));
664    else if ((rtyp==VMINPOLY) && nCoeff_is_algExt(currRing->cf) && (!nCoeff_is_GF(currRing->cf)))
665    {
666      const ring A = currRing->cf->extRing;
[db34980]667
[8b87135]668      assume( A != NULL );
[dd668f]669      assume( A->qideal != NULL );
[db34980]670
[dd668f]671      x=(void *)p_Copy(A->qideal->m[0], A);
[8b87135]672    }
[0e1846]673    data=NULL;
674    return x;
675  }
[a3432c]676  void *d=Data(); // will also do a iiCheckRing
677  if ((!errorreported) && (d!=NULL)) return slInternalCopy(this,t,d,e);
[0e1846]678  return NULL;
679}
680
[2166ad3]681//void * sleftv::CopyD()
682//{
[24c186a]683  //if ((rtyp!=IDHDL)&&(e==NULL)
684  //&&(rtyp!=VNOETHER)&&(rtyp!=LIB_CMD)&&(rtyp!=VMINPOLY))
685  //{
686  //  void *x=data;
687  //  data=NULL;
688  //  return x;
689  //}
[2166ad3]690//  return CopyD(Typ());
691//}
[0e1846]692
[8c94ba]693attr sleftv::CopyA()
694{
695  attr *a=Attribute();
696  if ((a!=NULL) && (*a!=NULL))
697    return (*a)->Copy();
698  return NULL;
699}
[0e1846]700
[a79a128]701char *  sleftv::String(void *d, BOOLEAN typed, int dim)
[0e1846]702{
703#ifdef SIQ
704  if (rtyp==COMMAND)
705  {
706    ::Print("##command %d\n",((command)data)->op);
707    if (((command)data)->arg1.rtyp!=0)
708      ((command)data)->arg1.Print(NULL,2);
709    if (((command)data)->arg2.rtyp!=0)
710      ((command)data)->arg2.Print(NULL,2);
711    if (((command)data)->arg3.rtyp==0)
712      ((command)data)->arg3.Print(NULL,2);
713    PrintS("##end\n");
[c232af]714    return omStrDup("");
[0e1846]715  }
716#endif
717  if (d==NULL) d=Data();
718  if (!errorreported)
719  {
720    char *s;
[84a6a7]721    int t=Typ();
722    switch (t /*Typ()*/)
[0e1846]723    {
[4b2155]724        case INT_CMD:
[7604db]725          if (typed)
726          {
[c232af]727            s=(char *)omAlloc(MAX_INT_LEN+7);
[7447d8]728            sprintf(s,"int(%d)",(int)(long)d);
[7604db]729          }
730          else
731          {
[c232af]732            s=(char *)omAlloc(MAX_INT_LEN+2);
[7447d8]733            sprintf(s,"%d",(int)(long)d);
[7604db]734          }
735          return s;
736
[4b2155]737        case STRING_CMD:
[7604db]738          if (d == NULL)
739          {
[c232af]740            if (typed) return omStrDup("\"\"");
741            return omStrDup("");
[7604db]742          }
743          if (typed)
744          {
[c232af]745            s = (char*) omAlloc(strlen((char*) d) + 3);
[7604db]746            sprintf(s,"\"%s\"", (char*) d);
747            return s;
748          }
749          else
750          {
[c232af]751            return omStrDup((char*)d);
[7604db]752          }
[c931a2]753
[4b2155]754        case POLY_CMD:
755        case VECTOR_CMD:
[7604db]756          if (typed)
757          {
758            char* ps = pString((poly) d);
[c232af]759            s = (char*) omAlloc(strlen(ps) + 10);
[84a6a7]760            sprintf(s,"%s(%s)", (t /*Typ()*/ == POLY_CMD ? "poly" : "vector"), ps);
[4c9cf1]761            omFree(ps);
[7604db]762            return s;
763          }
764          else
[ea9fd60]765            return pString((poly)d);
[7604db]766
[4b2155]767        case NUMBER_CMD:
[a70441f]768          StringSetS((char*) (typed ? "number(" : ""));
[4b2155]769          if ((rtyp==IDHDL)&&(IDTYP((idhdl)data)==NUMBER_CMD))
770          {
771            nWrite(IDNUMBER((idhdl)data));
772          }
773          else if (rtyp==NUMBER_CMD)
774          {
775            number n=(number)data;
776            nWrite(n);
777            data=(char *)n;
778          }
[8fa9ea]779          else if((rtyp==VMINPOLY)&&(rField_is_GF(currRing)))
[4b2155]780          {
[8fa9ea]781            nfShowMipo(currRing->cf);
[4b2155]782          }
783          else
784          {
785            number n=nCopy((number)d);
786            nWrite(n);
787            nDelete(&n);
788          }
[538512]789          StringAppendS((char*) (typed ? ")" : ""));
790          return StringEndS();
[c931a2]791
[84a6a7]792        case BIGINT_CMD:
793          {
794          StringSetS((char*) (typed ? "bigint(" : ""));
795          number nl=(number)d;
[4ff07f]796          n_Write(nl,coeffs_BIGINT);
[538512]797          StringAppendS((char*) (typed ? ")" : ""));
798          return StringEndS();
[84a6a7]799          }
800
[4b2155]801        case MATRIX_CMD:
[8fa9ea]802          s= iiStringMatrix((matrix)d,dim, currRing);
[7604db]803          if (typed)
804          {
[c232af]805            char* ns = (char*) omAlloc(strlen(s) + 40);
[c931a2]806            sprintf(ns, "matrix(ideal(%s),%d,%d)", s,
[7604db]807                    ((ideal) d)->nrows, ((ideal) d)->ncols);
[c232af]808            omCheckAddr(ns);
[7604db]809            return ns;
810          }
811          else
812          {
[c232af]813            return omStrDup(s);
[7604db]814          }
815
[4b2155]816        case MODUL_CMD:
817        case IDEAL_CMD:
818        case MAP_CMD:
[8fa9ea]819          s= iiStringMatrix((matrix)d,dim, currRing);
[7604db]820          if (typed)
821          {
[c232af]822            char* ns = (char*) omAlloc(strlen(s) + 10);
[84a6a7]823            sprintf(ns, "%s(%s)", (t/*Typ()*/==MODUL_CMD ? "module" : "ideal"), s);
[c232af]824            omCheckAddr(ns);
[7604db]825            return ns;
826          }
[c232af]827          return omStrDup(s);
[7604db]828
[4b2155]829        case INTVEC_CMD:
830        case INTMAT_CMD:
831        {
832          intvec *v=(intvec *)d;
[a79a128]833          s = v->String(dim);
[7604db]834          if (typed)
835          {
836            char* ns;
[84a6a7]837            if (t/*Typ()*/ == INTMAT_CMD)
[7604db]838            {
[c232af]839              ns = (char*) omAlloc(strlen(s) + 40);
[7604db]840              sprintf(ns, "intmat(intvec(%s),%d,%d)", s, v->rows(), v->cols());
841            }
842            else
843            {
[c232af]844              ns = (char*) omAlloc(strlen(s) + 10);
[7604db]845              sprintf(ns, "intvec(%s)", s);
846            }
[c232af]847            omCheckAddr(ns);
848            omFree(s);
[7604db]849            return ns;
850          }
851          else
852            return s;
[4b2155]853        }
[75f10d]854        case BIGINTMAT_CMD:
855        {
856          bigintmat *bim=(bigintmat*)d;
857          s = bim->String();
858          if (typed)
859          {
860            char* ns = (char*) omAlloc0(strlen(s) + 40);
861            sprintf(ns, "bigintmat(bigintvec(%s),%d,%d)", s, bim->rows(), bim->cols());
862            omCheckAddr(ns);
863            return ns;
864          }
865          else
866            return omStrDup(s);
[4c9cf1]867        }
[7604db]868
[4b2155]869        case RING_CMD:
870        case QRING_CMD:
[7604db]871          s  = rString((ring)d);
[c931a2]872
[7604db]873          if (typed)
874          {
875            char* ns;
[84a6a7]876            if (t/*Typ()*/ == QRING_CMD)
[7604db]877            {
[8fa9ea]878              char* id = iiStringMatrix((matrix) ((ring) d)->qideal, dim,
[db34980]879                              currRing);
[c232af]880              ns = (char*) omAlloc(strlen(s) + strlen(id) + 20);
[7604db]881              sprintf(ns, "\"%s\";%sideal(%s)", s,(dim == 2 ? "\n" : " "), id);
882            }
883            else
884            {
[c232af]885              ns = (char*) omAlloc(strlen(s) + 4);
[7604db]886              sprintf(ns, "\"%s\"", s);
887            }
[c232af]888            omFree(s);
889            omCheckAddr(ns);
[7604db]890            return ns;
891          }
892          return s;
[4b2155]893        case RESOLUTION_CMD:
[077e9c]894        {
[4b2155]895          lists l = syConvRes((syStrategy)d);
[a79a128]896          s = lString(l, typed, dim);
[4b2155]897          l->Clean();
[7604db]898          return s;
[077e9c]899        }
[7604db]900
[4b2155]901        case PROC_CMD:
[0e1846]902        {
[4b2155]903          procinfo* pi = (procinfo*) d;
904          if((pi->language == LANG_SINGULAR) && (pi->data.s.body!=NULL))
[7604db]905            s = (pi->data.s.body);
[4b2155]906          else
[85e68dd]907            s = (char *)"";
[7604db]908          if (typed)
909          {
[c232af]910            char* ns = (char*) omAlloc(strlen(s) + 4);
[7604db]911            sprintf(ns, "\"%s\"", s);
[c232af]912            omCheckAddr(ns);
[7604db]913            return ns;
914          }
[c232af]915          return omStrDup(s);
[0e1846]916        }
[c931a2]917
[f82470]918        case LINK_CMD:
[a79a128]919          s = slString((si_link) d);
[7604db]920          if (typed)
921          {
[c232af]922            char* ns = (char*) omAlloc(strlen(s) + 10);
[7604db]923            sprintf(ns, "link(\"%s\")", s);
[95197e]924            omFreeBinAddr(s);
[c232af]925            omCheckAddr(ns);
[7604db]926            return ns;
927          }
928          return s;
[c931a2]929
[4b2155]930        case LIST_CMD:
[7604db]931          return lString((lists) d, typed, dim);
[b117e8]932
933        default:
[c4dab4]934          if(t> MAX_TOK)
935          {
936            blackbox *bb=getBlackboxStuff(t);
937            if (bb!=NULL) return bb->blackbox_String(bb,d);
938          }
[0e1846]939    } /* end switch: (Typ()) */
940  }
[c232af]941  return omStrDup("");
[0e1846]942}
943
[c931a2]944
[0e1846]945int  sleftv::Typ()
946{
947  if (e==NULL)
948  {
[38cfbb]949    switch (rtyp)
[0e1846]950    {
[11ca48]951      case IDHDL:
952        return IDTYP((idhdl)data);
[ea947e]953      case ALIAS_CMD:
954         {
955           idhdl h=(idhdl)data;
956           return  ((idhdl)h->data.ustring)->typ;
957         }
[0e1846]958      case VECHO:
959      case VPRINTLEVEL:
960      case VCOLMAX:
961      case VTIMER:
[7a7652]962      case VRTIMER:
[0e1846]963      case VOICE:
964      case VMAXDEG:
965      case VMAXMULT:
966      case TRACE:
967      case VSHORTOUT:
968        return INT_CMD;
969      case VMINPOLY:
[4dc1d7]970        data=NULL;
[0e1846]971        return NUMBER_CMD;
972      case VNOETHER:
[4dc1d7]973        data=NULL;
[0e1846]974        return POLY_CMD;
975      //case COMMAND:
976      //  return COMMAND;
977      default:
978        return rtyp;
979    }
980  }
981  int r=0;
982  int t=rtyp;
[2f1afc]983  void *d=data;
[11ca48]984  if (t==IDHDL) t=IDTYP((idhdl)data);
[2f1afc]985  else if (t==ALIAS_CMD)
[92740ae]986  { idhdl h=(idhdl)IDDATA((idhdl)data); t=IDTYP(h);d=IDDATA(h); }
[0e1846]987  switch (t)
988  {
[2f1afc]989#ifdef SINGULAR_4_1
990    case CMATRIX_CMD:
991    {
992      bigintmat *b=(bigintmat*)d;
993      if ((currRing!=NULL)&&(currRing->cf==b->basecoeffs()))
994        return NUMBER_CMD;
995      else
996        return CNUMBER_CMD;
997    }
998#endif
[0e1846]999    case INTVEC_CMD:
1000    case INTMAT_CMD:
1001      r=INT_CMD;
1002      break;
[75f10d]1003    case BIGINTMAT_CMD:
1004      r=BIGINT_CMD;
1005      break;
[0e1846]1006    case IDEAL_CMD:
1007    case MATRIX_CMD:
1008    case MAP_CMD:
1009      r=POLY_CMD;
1010      break;
1011    case MODUL_CMD:
1012      r=VECTOR_CMD;
1013      break;
1014    case STRING_CMD:
1015      r=STRING_CMD;
1016      break;
[8db6c3]1017    default:
[0e1846]1018    {
[8db6c3]1019      blackbox *b=NULL;
1020      if (t>MAX_TOK)
[0e1846]1021      {
[8db6c3]1022        b=getBlackboxStuff(t);
[0e1846]1023      }
[8db6c3]1024      if ((t==LIST_CMD)||((b!=NULL)&&BB_LIKE_LIST(b)))
[0e1846]1025      {
[8db6c3]1026        lists l;
1027        if (rtyp==IDHDL) l=IDLIST((idhdl)data);
[db34980]1028        else if (rtyp==ALIAS_CMD)
1029        {
1030          idhdl h=(idhdl)data;
1031          l=(lists)(((idhdl)h->data.ustring)->data.ustring);
1032        }
[8db6c3]1033        else             l=(lists)data;
1034        if ((0<e->start)&&(e->start<=l->nr+1))
1035        {
1036          Subexpr tmp=l->m[e->start-1].e;
1037          l->m[e->start-1].e=e->next;
1038          r=l->m[e->start-1].Typ();
1039          e->next=l->m[e->start-1].e;
1040          l->m[e->start-1].e=tmp;
1041        }
1042        else
1043        {
1044          //Warn("out of range: %d not in 1..%d",e->start,l->nr+1);
[3747fa8]1045          r=DEF_CMD;
[8db6c3]1046        }
[0e1846]1047      }
[8db6c3]1048      else
1049        Werror("cannot index type %s(%d)",Tok2Cmdname(t),t);
[0e1846]1050      break;
1051    }
1052  }
1053  return r;
1054}
1055
1056int  sleftv::LTyp()
1057{
1058  lists l=NULL;
1059  int r;
1060  if (rtyp==LIST_CMD)
1061    l=(lists)data;
1062  else if ((rtyp==IDHDL)&& (IDTYP((idhdl)data)==LIST_CMD))
1063    l=IDLIST((idhdl)data);
1064  else
1065    return Typ();
1066  //if (l!=NULL)
1067  {
1068    if ((e!=NULL) && (e->next!=NULL))
1069    {
1070      if ((0<e->start)&&(e->start<=l->nr+1))
1071      {
1072        l->m[e->start-1].e=e->next;
1073        r=l->m[e->start-1].LTyp();
1074        l->m[e->start-1].e=NULL;
1075      }
1076      else
1077      {
1078        //Warn("out of range: %d not in 1..%d",e->start,l->nr+1);
1079        r=NONE;
1080      }
1081      return r;
1082    }
1083    return LIST_CMD;
1084  }
1085  return Typ();
1086}
1087
[2f1afc]1088#ifdef SINGULAR_4_1
1089static snumber2 iiNumber2Data[4];
1090static int iiCmatrix_index=0;
1091#endif
[0e1846]1092void * sleftv::Data()
1093{
[17681b]1094  if ((rtyp!=IDHDL) && iiCheckRing(rtyp))
[0e1846]1095     return NULL;
1096  if (e==NULL)
1097  {
1098    switch (rtyp)
1099    {
[11ca48]1100      case ALIAS_CMD:
1101      {
1102        idhdl h=(idhdl)data;
1103        return  ((idhdl)h->data.ustring)->data.ustring;
1104      }
[979b4e6]1105      case VECHO:      return (void *)(long)si_echo;
1106      case VPRINTLEVEL:return (void *)(long)printlevel;
1107      case VCOLMAX:    return (void *)(long)colmax;
1108      case VTIMER:     return (void *)(long)getTimer();
1109      case VRTIMER:    return (void *)(long)getRTimer();
1110      case VOICE:      return (void *)(long)(myynest+1);
1111      case VMAXDEG:    return (void *)(long)Kstd1_deg;
1112      case VMAXMULT:   return (void *)(long)Kstd1_mu;
1113      case TRACE:      return (void *)(long)traceit;
1114      case VSHORTOUT:  return (void *)(long)(currRing != NULL ? currRing->ShortOut : 0);
[8b87135]1115      case VMINPOLY:
1116        if ( (currRing != NULL)  && nCoeff_is_algExt(currRing->cf) && !nCoeff_is_GF(currRing->cf))
1117        {
1118          /* Q(a), Fp(a), but not GF(q) */
1119          const ring A = currRing->cf->extRing;
[db34980]1120
[8b87135]1121          assume( A != NULL );
[dd668f]1122          assume( A->qideal != NULL );
[db34980]1123
[dd668f]1124          return (void *)A->qideal->m[0];
[8b87135]1125        }
1126        else
1127          return (void *)currRing->cf->nNULL;
[db34980]1128
[993ae2]1129      case VNOETHER:   return (void *) (currRing->ppNoether);
[0e1846]1130      case IDHDL:
1131        return IDDATA((idhdl)data);
1132      case COMMAND:
1133        //return NULL;
1134      default:
1135        return data;
1136    }
1137  }
1138  /* e != NULL : */
1139  int t=rtyp;
1140  void *d=data;
1141  if (t==IDHDL)
1142  {
1143    t=((idhdl)data)->typ;
1144    d=IDDATA((idhdl)data);
1145  }
[11ca48]1146  else if (t==ALIAS_CMD)
1147  {
1148    idhdl h=(idhdl)IDDATA((idhdl)data);
1149    t=IDTYP(h);
1150    d=IDDATA(h);
1151  }
[0e1846]1152  if (iiCheckRing(t))
1153    return NULL;
1154  char *r=NULL;
[c931a2]1155  int index=e->start;
[0e1846]1156  switch (t)
1157  {
1158    case INTVEC_CMD:
1159    {
1160      intvec *iv=(intvec *)d;
[c931a2]1161      if ((index<1)||(index>iv->length()))
[0e1846]1162      {
1163        if (!errorreported)
[cdb69c]1164          Werror("wrong range[%d] in intvec %s(%d)",index,this->Name(),iv->length());
[0e1846]1165      }
1166      else
[a867b2]1167        r=(char *)(long)((*iv)[index-1]);
[0e1846]1168      break;
1169    }
1170    case INTMAT_CMD:
1171    {
1172      intvec *iv=(intvec *)d;
[c931a2]1173      if ((index<1)
1174         ||(index>iv->rows())
[0e1846]1175         ||(e->next->start<1)
1176         ||(e->next->start>iv->cols()))
1177      {
1178        if (!errorreported)
[cdb69c]1179        Werror("wrong range[%d,%d] in intmat %s(%dx%d)",index,e->next->start,
1180                                           this->Name(),iv->rows(),iv->cols());
[0e1846]1181      }
1182      else
[a867b2]1183        r=(char *)(long)(IMATELEM((*iv),index,e->next->start));
[0e1846]1184      break;
1185    }
[75f10d]1186    case BIGINTMAT_CMD:
1187    {
1188      bigintmat *m=(bigintmat *)d;
1189      if ((index<1)
1190         ||(index>m->rows())
1191         ||(e->next->start<1)
1192         ||(e->next->start>m->cols()))
1193      {
1194        if (!errorreported)
[cdb69c]1195        Werror("wrong range[%d,%d] in bigintmat %s(%dx%d)",index,e->next->start,
1196                                                     this->Name(),m->rows(),m->cols());
[75f10d]1197      }
1198      else
1199        r=(char *)(BIMATELEM((*m),index,e->next->start));
1200      break;
1201    }
[2f1afc]1202#ifdef SINGULAR_4_1
1203    case CMATRIX_CMD:
1204    {
1205      bigintmat *m=(bigintmat *)d;
1206      if ((index<1)
1207         ||(index>m->rows())
1208         ||(e->next->start<1)
1209         ||(e->next->start>m->cols()))
1210      {
1211        if (!errorreported)
1212        Werror("wrong range[%d,%d] in matrix %s(%dx%d)",index,e->next->start,
1213                                                     this->Name(),m->rows(),m->cols());
1214      }
1215      else
1216      {
1217        iiNumber2Data[iiCmatrix_index].cf=m->basecoeffs();
1218        iiNumber2Data[iiCmatrix_index].n=BIMATELEM((*m),index,e->next->start);
1219        r=(char*)&iiNumber2Data[iiCmatrix_index];
1220        iiCmatrix_index=(iiCmatrix_index+1) % 4;
1221      }
1222      break;
1223    }
1224#endif
[0e1846]1225    case IDEAL_CMD:
1226    case MODUL_CMD:
1227    case MAP_CMD:
1228    {
1229      ideal I=(ideal)d;
[c931a2]1230      if ((index<1)||(index>IDELEMS(I)))
[0e1846]1231      {
1232        if (!errorreported)
[cdb69c]1233          Werror("wrong range[%d] in ideal/module %s(%d)",index,this->Name(),IDELEMS(I));
[0e1846]1234      }
1235      else
[c931a2]1236        r=(char *)I->m[index-1];
[0e1846]1237      break;
1238    }
1239    case STRING_CMD:
1240    {
[c931a2]1241      // this was a memory leak
1242      // we evalute it, cleanup and replace this leftv by it's evalutated form
1243      // the evalutated form will be build in tmp
1244      sleftv tmp;
[f2dff02]1245      tmp.Init();
[c931a2]1246      tmp.rtyp=STRING_CMD;
[ec7aac]1247      r=(char *)omAllocBin(size_two_bin);
[c931a2]1248      if ((index>0)&& (index<=(int)strlen((char *)d)))
[0e1846]1249      {
[c931a2]1250        r[0]=*(((char *)d)+index-1);
[0e1846]1251        r[1]='\0';
1252      }
1253      else
1254      {
1255        r[0]='\0';
1256      }
[c931a2]1257      tmp.data=r;
[0bf7dff]1258      if ((rtyp==IDHDL)||(rtyp==STRING_CMD))
[c931a2]1259      {
1260        tmp.next=next; next=NULL;
[17681b]1261        //if (rtyp==STRING_CMD) { omFree((ADDRESS)data); }
1262        //data=NULL;
1263        d=NULL;
[c931a2]1264        CleanUp();
1265        memcpy(this,&tmp,sizeof(tmp));
1266      }
1267      // and, remember, r is also the result...
1268      else
1269      {
1270        // ???
1271        // here we still have a memory leak...
[82063a]1272        // example: list L="123","456";
1273        // L[1][2];
1274        // therefore, it should never happen:
1275        assume(0);
1276        // but if it happens: here is the temporary fix:
1277        // omMarkAsStaticAddr(r);
[c931a2]1278      }
[0e1846]1279      break;
1280    }
1281    case MATRIX_CMD:
1282    {
[c931a2]1283      if ((index<1)
1284         ||(index>MATROWS((matrix)d))
[0e1846]1285         ||(e->next->start<1)
1286         ||(e->next->start>MATCOLS((matrix)d)))
1287      {
1288        if (!errorreported)
[cdb69c]1289          Werror("wrong range[%d,%d] in matrix %s(%dx%d)",
[c931a2]1290                  index,e->next->start,
[9b12fce]1291                  this->Name(),
[c931a2]1292                  MATROWS((matrix)d),MATCOLS((matrix)d));
[0e1846]1293      }
1294      else
[c931a2]1295        r=(char *)MATELEM((matrix)d,index,e->next->start);
[0e1846]1296      break;
1297    }
[8db6c3]1298    default:
[0e1846]1299    {
[8db6c3]1300      blackbox *b=NULL;
1301      if (t>MAX_TOK)
1302      {
1303        b=getBlackboxStuff(t);
1304      }
1305      if ((t==LIST_CMD)||((b!=NULL)&&(BB_LIKE_LIST(b))))
[0e1846]1306      {
[8db6c3]1307        lists l=(lists)d;
1308        if ((0<index)&&(index<=l->nr+1))
[82063a]1309        {
[8db6c3]1310          if ((e->next!=NULL)
1311          && (l->m[index-1].rtyp==STRING_CMD))
1312          // string[..].Data() modifies sleftv, so let's do it ourself
[82063a]1313          {
[8db6c3]1314            char *dd=(char *)l->m[index-1].data;
1315            int j=e->next->start-1;
1316            r=(char *)omAllocBin(size_two_bin);
1317            if ((j>=0) && (j<(int)strlen(dd)))
1318            {
1319              r[0]=*(dd+j);
1320              r[1]='\0';
1321            }
1322            else
1323            {
1324              r[0]='\0';
1325            }
[82063a]1326          }
1327          else
1328          {
[8db6c3]1329            Subexpr tmp=l->m[index-1].e;
1330            l->m[index-1].e=e->next;
1331            r=(char *)l->m[index-1].Data();
1332            e->next=l->m[index-1].e;
1333            l->m[index-1].e=tmp;
[82063a]1334          }
1335        }
[8db6c3]1336        else //if (!errorreported)
[cdb69c]1337          Werror("wrong range[%d] in list %s(%d)",index,this->Name(),l->nr+1);
[0e1846]1338      }
[8db6c3]1339      else
[cdb69c]1340        Werror("cannot index %s of type %s(%d)",this->Name(),Tok2Cmdname(t),t);
[0e1846]1341      break;
1342    }
1343  }
1344  return r;
1345}
1346
1347attr * sleftv::Attribute()
1348{
[8c94ba]1349  if (e==NULL) return &attribute;
[13e2da]1350  if ((rtyp==LIST_CMD)
[fa50209]1351  ||((rtyp==IDHDL)&&(IDTYP((idhdl)data)==LIST_CMD))
1352  || (rtyp>MAX_TOK)
1353  || ((rtyp==IDHDL)&&(IDTYP((idhdl)data)>MAX_TOK)))
[0e1846]1354  {
1355    leftv v=LData();
1356    return &(v->attribute);
1357  }
1358  return NULL;
1359}
1360
1361leftv sleftv::LData()
1362{
1363  if (e!=NULL)
1364  {
1365    lists l=NULL;
[fa50209]1366    blackbox *b=getBlackboxStuff(rtyp);
[0e1846]1367
[fa50209]1368    if ((rtyp==LIST_CMD)
1369    || ((b!=NULL)&&(BB_LIKE_LIST(b))))
[0e1846]1370      l=(lists)data;
[db34980]1371    else if ((rtyp==IDHDL)&& (IDTYP((idhdl)data)==LIST_CMD))
[0e1846]1372      l=IDLIST((idhdl)data);
[fa50209]1373    else if ((rtyp==IDHDL)&& (IDTYP((idhdl)data)>MAX_TOK))
1374    {
1375      b=getBlackboxStuff(IDTYP((idhdl)data));
1376      if (BB_LIKE_LIST(b)) l=IDLIST((idhdl)data);
1377    }
[db34980]1378    else if (rtyp==ALIAS_CMD)
1379    {
1380      idhdl h=(idhdl)data;
1381      l= (lists)(((idhdl)h->data.ustring)->data.ustring);
1382    }
[0e1846]1383    if (l!=NULL)
1384    {
1385      if ((0>=e->start)||(e->start>l->nr+1))
1386        return NULL;
1387      if (e->next!=NULL)
1388      {
1389        l->m[e->start-1].e=e->next;
1390        leftv r=l->m[e->start-1].LData();
1391        l->m[e->start-1].e=NULL;
1392        return r;
1393      }
1394      return &(l->m[e->start-1]);
1395    }
1396  }
1397  return this;
1398}
1399
[7ddcb2]1400#if 0
[0e1846]1401leftv sleftv::LHdl()
1402{
1403  if (e!=NULL)
1404  {
[577d19f]1405    lists l=NULL;
1406
1407    if (rtyp==LIST_CMD)
1408      l=(lists)data;
1409    if ((rtyp==IDHDL)&& (IDTYP((idhdl)data)==LIST_CMD))
1410      l=IDLIST((idhdl)data);
1411    if (l!=NULL)
1412    {
1413      if ((0>=e->start)||(e->start>l->nr+1))
1414        return NULL;
1415      if (e->next!=NULL)
1416      {
1417        l->m[e->start-1].e=e->next;
1418        leftv r=l->m[e->start-1].LHdl();
1419        l->m[e->start-1].e=NULL;
1420        return r;
1421      }
1422      return &(l->m[e->start-1]);
1423    }
[0e1846]1424  }
1425  return this;
1426}
[7ddcb2]1427#endif
[0e1846]1428
1429BOOLEAN assumeStdFlag(leftv h)
1430{
1431  if ((h->e!=NULL)&&(h->LTyp()==LIST_CMD))
1432  {
1433    return assumeStdFlag(h->LData());
1434  }
1435  if (!hasFlag(h,FLAG_STD))
1436  {
1437    if (!TEST_VERB_NSB)
[95197e]1438    {
1439      if (TEST_V_ALLWARN)
1440        Warn("%s is no standard basis in >>%s<<",h->Name(),my_yylinebuf);
1441      else
1442        Warn("%s is no standard basis",h->Name());
1443    }
[0e1846]1444    return FALSE;
1445  }
1446  return TRUE;
1447}
1448
1449/*2
[c232af]1450* transforms a name (as an string created by omAlloc or omStrDup)
[0e1846]1451* into an expression (sleftv), deletes the string
1452* utility for grammar and iparith
1453*/
[85e68dd]1454void syMake(leftv v,const char * id, idhdl packhdl)
[0e1846]1455{
1456  /* resolv an identifier: (to DEF_CMD, if siq>0)
1457  * 1) reserved id: done by scanner
[8f1a54]1458  * 2) `basering` / 'Current`
[0e1846]1459  * 3) existing identifier, local
[4c9cf1]1460  * 4) ringvar, ringpar, local ring
[0e1846]1461  * 5) existing identifier, global
1462  * 6) monom (resp. number), local ring: consisting of:
[4c9cf1]1463  * 6') ringvar,  ringpar,global ring
[0e1846]1464  * 6'') monom (resp. number), local ring
1465  * 7) monom (resp. number), non-local ring
1466  * 8) basering
1467  * 9) `_`
1468  * 10) everything else is of type 0
1469  */
1470#ifdef TEST
1471  if ((*id<' ')||(*id>(char)126))
1472  {
1473    Print("wrong id :%s:\n",id);
1474  }
1475#endif
[0e051f]1476  idhdl save_ring=currRingHdl;
[f2dff02]1477  v->Init();
[bd4cb92]1478  if(packhdl != NULL)
[17ccde]1479  {
1480  //  Print("setting req_packhdl to %s\n",IDID(packhdl));
[bd4cb92]1481    v->req_packhdl = IDPACKAGE(packhdl);
[ff7e529]1482  }
[a3bc95e]1483  else v->req_packhdl = currPack;
1484//  if (v->req_packhdl!=basePack)
1485//    Print("search %s in %s\n",id,v->req_packhdl->libname);
[610881]1486  idhdl h=NULL;
[0e1846]1487#ifdef SIQ
1488  if (siq<=0)
1489#endif
1490  {
1491    if (!isdigit(id[0]))
1492    {
1493      if (strcmp(id,"basering")==0)
1494      {
1495        if (currRingHdl!=NULL)
1496        {
[95197e]1497          if (id!=IDID(currRingHdl)) omFreeBinAddr((ADDRESS)id);
[610881]1498          h=currRingHdl;
1499          goto id_found;
[0e1846]1500        }
1501        else
1502        {
1503          v->name = id;
1504          return; /* undefined */
1505        }
1506      }
[610881]1507      else if (strcmp(id,"Current")==0)
[8f1a54]1508      {
1509        if (currPackHdl!=NULL)
1510        {
[95197e]1511          omFreeBinAddr((ADDRESS)id);
[610881]1512          h=currPackHdl;
1513          goto id_found;
[8f1a54]1514        }
1515        else
1516        {
1517          v->name = id;
1518          return; /* undefined */
1519        }
1520      }
[bd4cb92]1521      if(v->req_packhdl!=currPack)
1522      {
1523        h=v->req_packhdl->idroot->get(id,myynest);
1524      }
1525      else
[9c9fb9]1526      h=ggetid(id);
[0e1846]1527      /* 3) existing identifier, local */
1528      if ((h!=NULL) && (IDLEV(h)==myynest))
1529      {
[95197e]1530        if (id!=IDID(h)) omFreeBinAddr((ADDRESS)id); /*assume strlen(id) <1000 */
[610881]1531        goto id_found;
[0e1846]1532      }
1533    }
[0e051f]1534    if (yyInRingConstruction)
1535    {
1536      currRingHdl=NULL;
1537    }
[0e1846]1538    /* 4. local ring: ringvar */
[b9e18a3]1539    if ((currRingHdl!=NULL) && (IDLEV(currRingHdl)==myynest)
[0e051f]1540    /*&& (!yyInRingConstruction)*/)
[0e1846]1541    {
1542      int vnr;
[7bb7da5]1543      if ((vnr=r_IsRingVar(id, currRing->names,currRing->N))>=0)
[0e1846]1544      {
1545        poly p=pOne();
1546        pSetExp(p,vnr+1,1);
1547        pSetm(p);
1548        v->data = (void *)p;
1549        v->name = id;
1550        v->rtyp = POLY_CMD;
1551        return;
1552      }
[7bb7da5]1553      if((n_NumberOfParameters(currRing->cf)>0)
1554      &&((vnr=r_IsRingVar(id, (char**)n_ParameterNames(currRing->cf),
1555                              n_NumberOfParameters(currRing->cf))>=0)))
[4c9cf1]1556      {
1557        BOOLEAN ok=FALSE;
1558        poly p = pmInit(id,ok);
[4740e8]1559        if (ok && (p!=NULL))
[4c9cf1]1560        {
1561          v->data = pGetCoeff(p);
1562          pGetCoeff(p)=NULL;
1563          pLmFree(p);
1564          v->rtyp = NUMBER_CMD;
1565          v->name = id;
1566          return;
1567        }
1568      }
[0e1846]1569    }
1570    /* 5. existing identifier, global */
1571    if (h!=NULL)
1572    {
[95197e]1573      if (id!=IDID(h)) omFreeBinAddr((ADDRESS)id);  /*assume strlen(id) <1000 */
[610881]1574      goto id_found;
[0e1846]1575    }
1576    /* 6. local ring: number/poly */
1577    if ((currRingHdl!=NULL) && (IDLEV(currRingHdl)==myynest))
1578    {
1579      BOOLEAN ok=FALSE;
[0e051f]1580      /*poly p = (!yyInRingConstruction) ? pmInit(id,ok) : (poly)NULL;*/
1581      poly p = pmInit(id,ok);
[0e1846]1582      if (ok)
1583      {
1584        if (p==NULL)
1585        {
1586          v->data = (void *)nInit(0);
1587          v->rtyp = NUMBER_CMD;
[875ec3]1588          #ifdef HAVE_PLURAL
1589          // in this case we may have monomials equal to 0 in p_Read
1590          v->name = id;
1591          #else
[95197e]1592          omFreeBinAddr((ADDRESS)id);
[875ec3]1593          #endif
[0e1846]1594        }
[0e051f]1595        else if (pIsConstant(p))
[0e1846]1596        {
1597          v->data = pGetCoeff(p);
1598          pGetCoeff(p)=NULL;
[512a2b]1599          pLmFree(p);
[0e1846]1600          v->rtyp = NUMBER_CMD;
1601          v->name = id;
1602        }
1603        else
1604        {
1605          v->data = p;
1606          v->rtyp = POLY_CMD;
1607          v->name = id;
1608        }
1609        return;
1610      }
1611    }
1612    /* 7. non-local ring: number/poly */
1613    {
1614      BOOLEAN ok=FALSE;
[1aa559b]1615      poly p = ((currRing!=NULL)     /* ring required */
1616               && (currRingHdl!=NULL)
[0e051f]1617               /*&& (!yyInRingConstruction) - not in decl */
[1aa559b]1618               && (IDLEV(currRingHdl)!=myynest)) /* already in case 4/6 */
[0e1846]1619                     ? pmInit(id,ok) : (poly)NULL;
1620      if (ok)
1621      {
1622        if (p==NULL)
1623        {
1624          v->data = (void *)nInit(0);
1625          v->rtyp = NUMBER_CMD;
[875ec3]1626          #ifdef HAVE_PLURAL
1627          // in this case we may have monomials equal to 0 in p_Read
1628          v->name = id;
1629          #else
[95197e]1630          omFreeBinAddr((ADDRESS)id);
[875ec3]1631          #endif
[0e1846]1632        }
1633        else
1634        if (pIsConstant(p))
1635        {
1636          v->data = pGetCoeff(p);
1637          pGetCoeff(p)=NULL;
[512a2b]1638          pLmFree(p);
[0e1846]1639          v->rtyp = NUMBER_CMD;
1640          v->name = id;
1641        }
1642        else
1643        {
1644          v->data = p;
1645          v->rtyp = POLY_CMD;
1646          v->name = id;
1647        }
[f3578ca]1648        if (TEST_V_ALLWARN /*&& (myynest>0)*/
[7bb7da5]1649        && ((r_IsRingVar(id, currRing->names,currRing->N)>=0)
1650          || ((n_NumberOfParameters(currRing->cf)>0)
1651             &&(r_IsRingVar(id, (char**)n_ParameterNames(currRing->cf),
[172d18c]1652                                n_NumberOfParameters(currRing->cf))>=0))))
[f3578ca]1653        {
1654        // WARNING: do not use ring variable names in procedures
[bdda288]1655          Warn("use of variable >>%s<< in a procedure in line %s",id,my_yylinebuf);
[f3578ca]1656        }
[0e1846]1657        return;
1658      }
1659    }
1660    /* 8. basering ? */
1661    if ((myynest>1)&&(currRingHdl!=NULL))
1662    {
1663      if (strcmp(id,IDID(currRingHdl))==0)
1664      {
[bceefc]1665        if (IDID(currRingHdl)!=id) omFreeBinAddr((ADDRESS)id); /*assume strlen (id) <1000 */
[610881]1666        h=currRingHdl;
1667        goto id_found;
[0e1846]1668      }
1669    }
[8f1a54]1670    if((v->req_packhdl!=basePack) && (v->req_packhdl==currPack))
[a3bc95e]1671    {
[5bce7c]1672      h=basePack->idroot->get(id,myynest);
1673      if (h!=NULL)
[ff7e529]1674      {
[95197e]1675        if (id!=IDID(h)) omFreeBinAddr((ADDRESS)id); /*assume strlen(id) <1000 */
[5bce7c]1676        v->req_packhdl=basePack;
[610881]1677        goto id_found;
[5bce7c]1678      }
[a3bc95e]1679    }
[0e1846]1680  }
1681#ifdef SIQ
1682  else
1683    v->rtyp=DEF_CMD;
1684#endif
1685  /* 9: _ */
1686  if (strcmp(id,"_")==0)
1687  {
[95197e]1688    omFreeBinAddr((ADDRESS)id);
[0e1846]1689    v->Copy(&sLastPrinted);
1690  }
1691  else
1692  {
1693    /* 10: everything else */
1694    /* v->rtyp = UNKNOWN;*/
1695    v->name = id;
1696  }
[0e051f]1697  currRingHdl=save_ring;
[610881]1698  return;
1699id_found: // we have an id (in h) found, to set the data in from h
[ea947e]1700  if (IDTYP(h)!=ALIAS_CMD)
1701  {
1702    v->rtyp = IDHDL;
1703    v->flag = IDFLAG(h);
1704    v->attribute=IDATTR(h);
1705  }
1706  else
1707  {
1708    v->rtyp = ALIAS_CMD;
1709  }
[610881]1710  v->name = IDID(h);
[ea947e]1711  v->data = (char *)h;
[0e051f]1712  currRingHdl=save_ring;
[0e1846]1713}
1714
1715int sleftv::Eval()
1716{
1717  BOOLEAN nok=FALSE;
1718  leftv nn=next;
1719  next=NULL;
1720  if(rtyp==IDHDL)
1721  {
1722    int t=Typ();
[2a2c07]1723    if (t!=PROC_CMD)
1724    {
1725      void *d=CopyD(t);
1726      data=d;
1727      rtyp=t;
1728      name=NULL;
1729      e=NULL;
1730    }
[0e1846]1731  }
1732  else if (rtyp==COMMAND)
1733  {
1734    command d=(command)data;
1735    if(d->op==PROC_CMD) //assume d->argc==2
1736    {
1737      char *what=(char *)(d->arg1.Data());
1738      idhdl h=ggetid(what);
1739      if((h!=NULL)&&(IDTYP(h)==PROC_CMD))
1740      {
1741        nok=d->arg2.Eval();
1742        if(!nok)
1743        {
[5edb77]1744          nok=iiMake_proc(h,req_packhdl,&d->arg2);
[84d88f7]1745          this->CleanUp(currRing);
[5edb77]1746          if (!nok)
[84d88f7]1747          {
[5edb77]1748            memcpy(this,&iiRETURNEXPR,sizeof(sleftv));
[84d88f7]1749            memset(&iiRETURNEXPR,0,sizeof(sleftv));
1750          }
[0e1846]1751        }
1752      }
1753      else nok=TRUE;
1754    }
1755    else if (d->op=='=') //assume d->argc==2
1756    {
[faeb82]1757      if ((d->arg1.rtyp!=IDHDL)&&(d->arg1.rtyp!=DEF_CMD))
[0e1846]1758      {
[faeb82]1759        nok=d->arg1.Eval();
1760      }
1761      if (!nok)
1762      {
[85e68dd]1763        const char *n=d->arg1.name;
[fff984]1764        nok=(n == NULL) || d->arg2.Eval();
[0e1846]1765        if (!nok)
1766        {
[d754b7]1767          int save_typ=d->arg1.rtyp;
[85e68dd]1768          omCheckAddr((ADDRESS)n);
[faeb82]1769          if (d->arg1.rtyp!=IDHDL)
[fff984]1770          syMake(&d->arg1,n);
[85e68dd]1771          omCheckAddr((ADDRESS)d->arg1.name);
[0e1846]1772          if (d->arg1.rtyp==IDHDL)
1773          {
[c232af]1774            n=omStrDup(IDID((idhdl)d->arg1.data));
[0e1846]1775            killhdl((idhdl)d->arg1.data);
[a3bc95e]1776            d->arg1.Init();
[f077d2]1777            //d->arg1.data=NULL;
[0e1846]1778            d->arg1.name=n;
1779          }
[f077d2]1780          d->arg1.rtyp=DEF_CMD;
[0e1846]1781          sleftv t;
[d754b7]1782          if(save_typ!=PROC_CMD) save_typ=d->arg2.rtyp;
[df5fc1]1783          if (::RingDependend(d->arg2.rtyp))
[d754b7]1784            nok=iiDeclCommand(&t,&d->arg1,0,save_typ,&currRing->idroot);
[0e1846]1785          else
[46d09b]1786            nok=iiDeclCommand(&t,&d->arg1,0,save_typ,&IDROOT);
[0e1846]1787          memcpy(&d->arg1,&t,sizeof(sleftv));
[85e68dd]1788          omCheckAddr((ADDRESS)d->arg1.name);
[0e1846]1789          nok=nok||iiAssign(&d->arg1,&d->arg2);
[9d72fe]1790          omCheckIf(d->arg1.name != NULL,  // OB: ????
[85e68dd]1791                    omCheckAddr((ADDRESS)d->arg1.name));
[0e1846]1792          if (!nok)
1793          {
1794            memset(&d->arg1,0,sizeof(sleftv));
[4f568c]1795            this->CleanUp();
1796            rtyp=NONE;
[0e1846]1797          }
1798        }
1799      }
1800      else nok=TRUE;
1801    }
[91ef34]1802    else
[0e1846]1803    {
[84d88f7]1804      sleftv tmp;
[91ef34]1805      int toktype=iiTokType(d->op);
1806      if ((toktype==CMD_M)
1807      ||( toktype==ROOT_DECL_LIST)
[1f03aba]1808      ||( toktype==RING_DECL_LIST))
[91ef34]1809      {
1810        if (d->argc <=3)
1811        {
1812          if (d->argc>=1) nok=d->arg1.Eval();
1813          if ((!nok) && (d->argc>=2))
[172d18c]1814          {
1815            nok=d->arg2.Eval();
1816            d->arg1.next=(leftv)omAllocBin(sleftv_bin);
1817            memcpy(d->arg1.next,&d->arg2,sizeof(sleftv));
1818            d->arg2.Init();
1819          }
[91ef34]1820          if ((!nok) && (d->argc==3))
[172d18c]1821          {
1822            nok=d->arg3.Eval();
1823            d->arg1.next->next=(leftv)omAllocBin(sleftv_bin);
1824            memcpy(d->arg1.next->next,&d->arg3,sizeof(sleftv));
1825            d->arg3.Init();
1826          }
[91ef34]1827          if (d->argc==0)
[84d88f7]1828            nok=nok||iiExprArithM(&tmp,NULL,d->op);
[91ef34]1829          else
[84d88f7]1830            nok=nok||iiExprArithM(&tmp,&d->arg1,d->op);
[91ef34]1831        }
1832        else
1833        {
1834          nok=d->arg1.Eval();
[84d88f7]1835          nok=nok||iiExprArithM(&tmp,&d->arg1,d->op);
[91ef34]1836        }
1837      }
1838      else if (d->argc==1)
1839      {
1840        nok=d->arg1.Eval();
[84d88f7]1841        nok=nok||iiExprArith1(&tmp,&d->arg1,d->op);
[91ef34]1842      }
1843      else if(d->argc==2)
1844      {
1845        nok=d->arg1.Eval();
1846        nok=nok||d->arg2.Eval();
[84d88f7]1847        nok=nok||iiExprArith2(&tmp,&d->arg1,d->op,&d->arg2);
[91ef34]1848      }
1849      else if(d->argc==3)
1850      {
1851        nok=d->arg1.Eval();
1852        nok=nok||d->arg2.Eval();
1853        nok=nok||d->arg3.Eval();
[84d88f7]1854        nok=nok||iiExprArith3(&tmp,d->op,&d->arg1,&d->arg2,&d->arg3);
[91ef34]1855      }
1856      else if(d->argc!=0)
1857      {
1858        nok=d->arg1.Eval();
[84d88f7]1859        nok=nok||iiExprArithM(&tmp,&d->arg1,d->op);
[91ef34]1860      }
1861      else // d->argc == 0
1862      {
[84d88f7]1863        nok = iiExprArithM(&tmp, NULL, d->op);
[91ef34]1864      }
[84d88f7]1865      this->CleanUp();
1866      memcpy(this,&tmp,sizeof(tmp));
[0e1846]1867    }
1868  }
1869  else if (((rtyp==0)||(rtyp==DEF_CMD))
1870    &&(name!=NULL))
1871  {
1872     syMake(this,name);
1873  }
1874#ifdef MDEBUG
1875  switch(Typ())
1876  {
1877    case NUMBER_CMD:
1878#ifdef LDEBUG
1879      nTest((number)Data());
[84a6a7]1880#endif
1881      break;
1882    case BIGINT_CMD:
1883#ifdef LDEBUG
[4ff07f]1884      n_Test((number)Data(),coeffs_BIGINT);
[0e1846]1885#endif
1886      break;
1887    case POLY_CMD:
1888      pTest((poly)Data());
1889      break;
1890    case IDEAL_CMD:
1891    case MODUL_CMD:
1892    case MATRIX_CMD:
1893      {
1894        ideal id=(ideal)Data();
[c232af]1895        omCheckAddrSize(id,sizeof(*id));
[0e1846]1896        int i=id->ncols*id->nrows-1;
1897        for(;i>=0;i--) pTest(id->m[i]);
1898      }
1899      break;
1900  }
1901#endif
1902  if (nn!=NULL) nok=nok||nn->Eval();
1903  next=nn;
1904  return nok;
1905}
1906
[85e68dd]1907const char *iiSleftv2name(leftv v)
[c12cf96]1908{
1909  return(v->name);
1910}
[f71681]1911
1912void * sattr::CopyA()
1913{
1914  omCheckAddrSize(this,sizeof(sattr));
1915  return s_internalCopy(atyp,data);
1916}
1917
Note: See TracBrowser for help on using the repository browser.