source: git/Singular/gentable.cc @ 4b889d

spielwiese
Last change on this file since 4b889d was 4b889d, checked in by Hans Schoenemann <hannes@…>, 14 years ago
RTIMER simplification git-svn-id: file:///usr/local/Singular/svn/trunk@13480 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 33.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5
6/*
7* ABSTRACT: table driven kernel interface, used by interpreter
8*/
9
10#include <stdlib.h>
11#include <string.h>
12#include <ctype.h>
13#include <stdio.h>
14#include <time.h>
15#include <unistd.h>
16
17#include <Singular/mod2.h>
18#include <Singular/tok.h>
19#include <Singular/grammar.h>
20
21  #define ALLOW_PLURAL     1
22  #define NO_PLURAL        0
23  #define COMM_PLURAL      2
24  #define  PLURAL_MASK     3
25
26#define ALLOW_RING       4
27#define NO_RING          0
28
29
30/*=============== types =====================*/
31struct _scmdnames
32{
33  const char *name;
34  short alias;
35  short tokval;
36  short toktype;
37};
38typedef struct _scmdnames cmdnames;
39
40
41struct sValCmd2
42{
43  int p;
44  short cmd;
45  short res;
46  short arg1;
47  short arg2;
48  short valid_for;
49};
50struct sValCmd1
51{
52  int p;
53  short cmd;
54  short res;
55  short arg;
56  short valid_for;
57};
58struct sValCmd3
59{
60  int p;
61  short cmd;
62  short res;
63  short arg1;
64  short arg2;
65  short arg3;
66  short valid_for;
67};
68struct sValCmdM
69{
70  int p;
71  short cmd;
72  short res;
73  short number_of_args; /* -1: any, -2: any >0, .. */
74  short valid_for;
75};
76struct sValAssign_sys
77{
78  int p;
79  short res;
80  short arg;
81};
82
83struct sValAssign
84{
85  int p;
86  short res;
87  short arg;
88};
89
90struct sConvertTypes
91{
92  int i_typ;
93  int o_typ;
94  int p;
95  int pl;
96};
97
98
99#define jjWRONG  1
100#define jjWRONG2  1
101#define jjWRONG3  1
102#define XS(A) A
103
104
105#define D(A)    2
106#define IPARITH
107#define GENTABLE
108#define IPCONV
109#define IPASSIGN
110
111#include "table.h"
112
113const char * Tok2Cmdname(int tok)
114{
115  int i = 0;
116  if (tok < 0)
117  {
118    return cmds[0].name;
119  }
120  if (tok==COMMAND) return "command";
121  if (tok==ANY_TYPE) return "any_type";
122  if (tok==NONE) return "nothing";
123  //if (tok==IFBREAK) return "if_break";
124  //if (tok==VECTOR_FROM_POLYS) return "vector_from_polys";
125  //if (tok==ORDER_VECTOR) return "ordering";
126  //if (tok==REF_VAR) return "ref";
127  //if (tok==OBJECT) return "object";
128  //if (tok==PRINT_EXPR) return "print_expr";
129  if (tok==IDHDL) return "identifier";
130  while (cmds[i].tokval!=0)
131  {
132    if ((cmds[i].tokval == tok)&&(cmds[i].alias==0))
133    {
134      return cmds[i].name;
135    }
136    i++;
137  }
138  return cmds[0].name;
139}
140/*---------------------------------------------------------------------*/
141/**
142 * @brief compares to entry of cmdsname-list
143
144 @param[in] a
145 @param[in] b
146
147 @return <ReturnValue>
148**/
149/*---------------------------------------------------------------------*/
150static int _gentable_sort_cmds( const void *a, const void *b )
151{
152  cmdnames *pCmdL = (cmdnames*)a;
153  cmdnames *pCmdR = (cmdnames*)b;
154
155  if(a==NULL || b==NULL)             return 0;
156
157  /* empty entries goes to the end of the list for later reuse */
158  if(pCmdL->name==NULL) return 1;
159  if(pCmdR->name==NULL) return -1;
160
161  /* $INVALID$ must come first */
162  if(strcmp(pCmdL->name, "$INVALID$")==0) return -1;
163  if(strcmp(pCmdR->name, "$INVALID$")==0) return  1;
164
165  /* tokval=-1 are reserved names at the end */
166  if (pCmdL->tokval==-1)
167  {
168    if (pCmdR->tokval==-1)
169       return strcmp(pCmdL->name, pCmdR->name);
170    /* pCmdL->tokval==-1, pCmdL goes at the end */
171    return 1;
172  }
173  /* pCmdR->tokval==-1, pCmdR goes at the end */
174  if(pCmdR->tokval==-1) return -1;
175
176  return strcmp(pCmdL->name, pCmdR->name);
177}
178
179/*generic*/
180const char * iiTwoOps(int t)
181{
182  if (t<127)
183  {
184    static char ch[2];
185    switch (t)
186    {
187      case '&':
188        return "and";
189      case '|':
190        return "or";
191      default:
192        ch[0]=t;
193        ch[1]='\0';
194        return ch;
195    }
196  }
197  switch (t)
198  {
199    case COLONCOLON:  return "::";
200    case DOTDOT:      return "..";
201    //case PLUSEQUAL:   return "+=";
202    //case MINUSEQUAL:  return "-=";
203    case MINUSMINUS:  return "--";
204    case PLUSPLUS:    return "++";
205    case EQUAL_EQUAL: return "==";
206    case LE:          return "<=";
207    case GE:          return ">=";
208    case NOTEQUAL:    return "<>";
209    default:          return Tok2Cmdname(t);
210  }
211}
212//
213// automatic conversions:
214//
215/*2
216* try to convert 'inputType' in 'outputType'
217* return 0 on failure, an index (<>0) on success
218*/
219int iiTestConvert (int inputType, int outputType)
220{
221  if ((inputType==outputType)
222  || (outputType==DEF_CMD)
223  || (outputType==IDHDL)
224  || (outputType==ANY_TYPE))
225  {
226    return -1;
227  }
228
229  // search the list
230  int i=0;
231  while (dConvertTypes[i].i_typ!=0)
232  {
233    if((dConvertTypes[i].i_typ==inputType)
234    &&(dConvertTypes[i].o_typ==outputType))
235    {
236      //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType,
237      //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1);
238      return i+1;
239    }
240    i++;
241  }
242  //Print("test convert %d to %d (%s -> %s):0\n",inputType,outputType,
243  // Tok2Cmdname(inputType), Tok2Cmdname(outputType));
244  return 0;
245}
246char *iparith_inc;
247void ttGen1()
248{
249  iparith_inc=strdup("iparith.xxxxxx");
250  int pid=getpid();
251  iparith_inc[8]=(pid %10)+'0'; pid/=10;
252  iparith_inc[9]=(pid %10)+'0'; pid/=10;
253  iparith_inc[10]=(pid %10)+'0'; pid/=10;
254  iparith_inc[11]=(pid %10)+'0'; pid/=10;
255  iparith_inc[12]=(pid %10)+'0'; pid/=10;
256  iparith_inc[13]=(pid %10)+'0';
257  FILE *outfile = fopen(iparith_inc,"w");
258  int i,j,l1=0,l2=0;
259  fprintf(outfile,
260  "/****************************************\n"
261  "*  Computer Algebra System SINGULAR     *\n"
262  "****************************************/\n\n");
263/*-------------------------------------------------------------------*/
264  fprintf(outfile,"// syntax table for Singular\n//\n");
265  fprintf(outfile,"// - search for an exact match of the argument types\n");
266  fprintf(outfile,"// - otherwise search for the first possibility\n");
267  fprintf(outfile,"//   with converted types of the arguments\n");
268  fprintf(outfile,"// - otherwise report an error\n//\n");
269
270  int op;
271  i=0;
272  while ((op=dArith1[i].cmd)!=0)
273  {
274    if (dArith1[i].p==jjWRONG)
275      fprintf(outfile,"// DUMMY ");
276    const char *s = iiTwoOps(op);
277    fprintf(outfile,"// operation: %s (%s)  ->  %s\n",
278          s,
279          Tok2Cmdname(dArith1[i].arg),
280          Tok2Cmdname(ABS(dArith1[i].res)));
281    i++;
282  }
283  fprintf(outfile,"/*---------------------------------------------*/\n");
284  i=0;
285  while ((op=dArith2[i].cmd)!=0)
286  {
287    if (dArith2[i].p==jjWRONG2)
288      fprintf(outfile,"// DUMMY ");
289    const char *s = iiTwoOps(op);
290    fprintf(outfile,"// operation: %s (%s, %s)  ->  %s\n",
291          s,
292          Tok2Cmdname(dArith2[i].arg1),
293          Tok2Cmdname(dArith2[i].arg2),
294          Tok2Cmdname(dArith2[i].res));
295    i++;
296  }
297  fprintf(outfile,"/*---------------------------------------------*/\n");
298  i=0;
299  while ((op=dArith3[i].cmd)!=0)
300  {
301    const char *s = iiTwoOps(op);
302    if (dArith3[i].p==jjWRONG3)
303      fprintf(outfile,"// DUMMY ");
304    fprintf(outfile,"// operation: %s (%s, %s, %s)  ->  %s\n",
305          s,
306          Tok2Cmdname(dArith3[i].arg1),
307          Tok2Cmdname(dArith3[i].arg2),
308          Tok2Cmdname(dArith3[i].arg3),
309          Tok2Cmdname(dArith3[i].res));
310    i++;
311  }
312  fprintf(outfile,"/*---------------------------------------------*/\n");
313  i=0;
314  while ((op=dArithM[i].cmd)!=0)
315  {
316    const char *s = iiTwoOps(op);
317    fprintf(outfile,"// operation: %s (...)  ->  %s",
318          s,
319          Tok2Cmdname(dArithM[i].res));
320    switch(dArithM[i].number_of_args)
321    {
322      case -2:
323         fprintf(outfile," ( number of arguments >0 )\n");
324         break;
325      case -1:
326         fprintf(outfile," ( any number of arguments )\n");
327         break;
328      default:
329         fprintf(outfile," ( %d arguments )\n",dArithM[i].number_of_args);
330         break;
331    }
332    i++;
333  }
334  fprintf(outfile,"/*---------------------------------------------*/\n");
335  i=0;
336  while ((op=dAssign[i].res)!=0)
337  {
338    fprintf(outfile,"// assign: %s =  %s\n",
339          Tok2Cmdname(op/*dAssign[i].res*/),
340          Tok2Cmdname(dAssign[i].arg));
341    i++;
342  }
343/*-------------------------------------------------------------------*/
344  fprintf(outfile,"/*---------------------------------------------*/\n");
345  for (j=257;j<=MAX_TOK+1;j++)
346  {
347    for(i=257;i<=MAX_TOK+1;i++)
348    {
349      if ((i!=j) && (j!=IDHDL) && (j!=DEF_CMD) && (j!=ANY_TYPE)
350      && iiTestConvert(i,j))
351      {
352        fprintf(outfile,"// convert %s -> %s\n",
353          Tok2Cmdname(i), Tok2Cmdname(j));
354        if (j==ANY_TYPE) break;
355      }
356    }
357  }
358  fprintf(outfile,"/*---------------------------------------------*/\n");
359  char ops[]="=><+*/[.^,%(;";
360  for(i=0;ops[i]!='\0';i++)
361    fprintf(outfile,"// token %d : %c\n", (int)ops[i], ops[i]);
362  for (i=257;i<=MAX_TOK;i++)
363  {
364    const char *s=iiTwoOps(i);
365    if (s[0]!='$')
366    {
367      fprintf(outfile,"// token %d : %s\n", i, s);
368    }
369  }
370/*-------------------------------------------------------------------*/
371  fprintf(outfile,"/*--max. token: %d, gr: %d --*/\n",MAX_TOK,UMINUS);
372/*-------------------------------------------------------------------*/
373  fprintf(outfile,"/*---------------------------------------------*/\n");
374  fprintf(outfile,
375  "struct sValCmdTab dArithTab1[]=\n"
376  "{\n");
377  for (j=1;j<=MAX_TOK+1;j++)
378  {
379    for(i=0;dArith1[i].cmd!=0;i++)
380    {
381      if (dArith1[i].cmd==j)
382      {
383        fprintf(outfile," { %d,%d },\n",j,i);
384        l1++;
385        break;
386      }
387    }
388  }
389  fprintf(outfile," { 10000,0 }\n};\n");
390  fprintf(outfile,"#define JJTAB1LEN %d\n",l1);
391/*-------------------------------------------------------------------*/
392  fprintf(outfile,
393  "struct sValCmdTab dArithTab2[]=\n"
394  "{\n");
395  for (j=1;j<=MAX_TOK+1;j++)
396  {
397    for(i=0;dArith2[i].cmd!=0;i++)
398    {
399      if (dArith2[i].cmd==j)
400      {
401        fprintf(outfile," { %d,%d },\n",j,i);
402        l2++;
403        break;
404      }
405    }
406  }
407  fprintf(outfile," { 10000,0 }\n};\n");
408  fprintf(outfile,"#define JJTAB2LEN %d\n",l2);
409  fclose(outfile);
410}
411/*-------------------------------------------------------------------*/
412#if 0
413void ttGen2()
414{
415  FILE *outfile = fopen(iparith_inc,"a");
416  fprintf(outfile,
417  "/****************************************\n"
418  "*  Computer Algebra System SINGULAR     *\n"
419  "****************************************/\n\n");
420/*-------------------------------------------------------------------*/
421  fprintf(outfile,"// identifier table for Singular\n//\n");
422
423  fprintf(outfile,
424  "cmdnames OLDcmds[] =\n"
425  "{  // name-string     alias  tokval toktype\n"
426  "{ \"$INVALID$\",            0,  -1, 0},\n");
427  int i=1;
428  int m=-1;
429  int id_nr=0;
430  BOOLEAN f=FALSE;
431  loop
432  {
433    while (cmds[i].tokval!=0)
434    {
435      if ((cmds[i].tokval!=-1)&&(cmds[i].name!=NULL))
436      {
437        if(m==-1)
438        {
439          m=i;
440          f=TRUE;
441        }
442        else if(strcmp(cmds[m].name,cmds[i].name)>0)
443        {
444          m=i;
445          f=TRUE;
446        }
447      }
448      i++;
449    }
450    if(f)
451    {
452      id_nr++;
453      fprintf(outfile,"  {\"%s\", %*d, %3d, ",cmds[m].name,
454                                             20-strlen(cmds[m].name),
455                                             cmds[m].alias,
456                                             cmds[m].tokval);
457      switch(cmds[m].toktype)
458      {
459        case CMD_1:            fprintf(outfile,"CMD_1 },\n"); break;
460        case CMD_2:            fprintf(outfile,"CMD_2 },\n"); break;
461        case CMD_3:            fprintf(outfile,"CMD_3 },\n"); break;
462        case CMD_12:           fprintf(outfile,"CMD_12 },\n"); break;
463        case CMD_123 :         fprintf(outfile,"CMD_123 },\n"); break;
464        case CMD_23:           fprintf(outfile,"CMD_23 },\n"); break;
465        case CMD_M:            fprintf(outfile,"CMD_M },\n"); break;
466        case SYSVAR:           fprintf(outfile,"SYSVAR },\n"); break;
467        case ROOT_DECL:        fprintf(outfile,"ROOT_DECL },\n"); break;
468        case ROOT_DECL_LIST:   fprintf(outfile,"ROOT_DECL_LIST },\n"); break;
469        case RING_DECL:        fprintf(outfile,"RING_DECL },\n"); break;
470        case NONE:             fprintf(outfile,"NONE },\n"); break;
471        default:               if((cmds[m].toktype>' ')
472                               &&(cmds[m].toktype<127))
473                               {
474                                 fprintf(outfile,"'%c' },\n",cmds[m].toktype);
475                               }
476                               else
477                               {
478                                 fprintf(outfile,"%d },\n",cmds[m].toktype);
479                               }
480                               break;
481      }
482      cmds[m].name=NULL;
483      m=-1;
484      i=1;
485      f=FALSE;
486    }
487    else break;
488  }
489  fprintf(outfile,
490"/* list of scanner identifiers/only for feread/reservedName */\n");
491  f=FALSE;
492  i=1;m=-1;
493  loop
494  {
495    while (cmds[i].tokval!=0)
496    {
497      if (cmds[i].name!=NULL)
498      {
499        if(m==-1)
500        {
501          m=i;
502          f=TRUE;
503        }
504        else if(strcmp(cmds[m].name,cmds[i].name)>0)
505        {
506          m=i;
507          f=TRUE;
508        }
509      }
510      i++;
511    }
512    if(f)
513    {
514      fprintf(outfile,"  {\"%s\", %*d,  -1, 0 },\n",cmds[m].name,
515                                             20-strlen(cmds[m].name),
516                                             0/*cmds[m].alias*/
517                                             /*-1 cmds[m].tokval*/
518                                             /*0 cmds[m].toktype*/);
519      cmds[m].name=NULL;
520      m=-1;
521      i=1;
522      f=FALSE;
523    }
524    else break;
525  }
526  fprintf(outfile,
527"/* end of list marker */\n"
528"  { NULL, 0, 0, 0}\n"
529"};\n"
530"#define LAST_IDENTIFIER %d\n"
531  ,id_nr);
532  fclose(outfile);
533}
534#endif
535/*---------------------------------------------------------------------*/
536/**
537 * @brief generate cmds initialisation
538**/
539/*---------------------------------------------------------------------*/
540
541void ttGen2b()
542{
543  int cmd_size = (sizeof(cmds)/sizeof(cmdnames))-1;
544
545  FILE *outfile = fopen(iparith_inc,"a");
546  fprintf(outfile,
547  "/****************************************\n"
548  "*  Computer Algebra System SINGULAR     *\n"
549  "****************************************/\n\n");
550/*-------------------------------------------------------------------*/
551  fprintf(outfile,"// identifier table for Singular\n//\n");
552
553  fprintf(
554    outfile,
555    "#ifdef MODULE_GENERATOR\n"
556    "#define omAlloc0(A) malloc(A)\n"
557    "#endif\n"
558    "void iiInitCmdName()\n{\n"
559    "  sArithBase.nCmdUsed      = 0;\n"
560    "  sArithBase.nCmdAllocated = %d;\n"
561    "  sArithBase.sCmds = (cmdnames*)omAlloc0(sArithBase.nCmdAllocated*sizeof(cmdnames));\n"
562    "\n"
563    "  // name-string                   alias  tokval toktype index\n",
564    cmd_size);
565  int m=0;
566  int id_nr=0;
567
568  qsort(&cmds, cmd_size, sizeof(cmdnames), (&_gentable_sort_cmds));
569
570  for(m=0; m<cmd_size; m++)
571  {
572    if(cmds[m].tokval>0) id_nr++;
573    fprintf(outfile,"  iiArithAddCmd(\"%s\", %*d, %3d, ",cmds[m].name,
574            (int)(20-strlen(cmds[m].name)),
575            cmds[m].alias,
576            cmds[m].tokval);
577    switch(cmds[m].toktype)
578    {
579        case CMD_1:            fprintf(outfile,"CMD_1"); break;
580        case CMD_2:            fprintf(outfile,"CMD_2"); break;
581        case CMD_3:            fprintf(outfile,"CMD_3"); break;
582        case CMD_12:           fprintf(outfile,"CMD_12"); break;
583        case CMD_123 :         fprintf(outfile,"CMD_123"); break;
584        case CMD_23:           fprintf(outfile,"CMD_23"); break;
585        case CMD_M:            fprintf(outfile,"CMD_M"); break;
586        case SYSVAR:           fprintf(outfile,"SYSVAR"); break;
587        case ROOT_DECL:        fprintf(outfile,"ROOT_DECL"); break;
588        case ROOT_DECL_LIST:   fprintf(outfile,"ROOT_DECL_LIST"); break;
589        case RING_DECL:        fprintf(outfile,"RING_DECL"); break;
590        case NONE:             fprintf(outfile,"NONE"); break;
591        default:
592          if((cmds[m].toktype>' ') &&(cmds[m].toktype<127))
593          {
594            fprintf(outfile,"'%c'",cmds[m].toktype);
595          }
596          else
597          {
598            fprintf(outfile,"%d",cmds[m].toktype);
599          }
600          break;
601#if 0
602          fprintf(outfile,"  iiArithAddCmd(\"%s\", %*d,  -1, 0 );\n",
603              cmds[m].name, 20-strlen(cmds[m].name),
604              0/*cmds[m].alias*/
605              /*-1 cmds[m].tokval*/
606              /*0 cmds[m].toktype*/);
607#endif
608    }
609    fprintf(outfile,", %d);\n", m);
610  }
611  fprintf(outfile, "/* end of list marker */\n");
612  fprintf(outfile,
613          "  sArithBase.nLastIdentifier = %d;\n",
614          id_nr);
615
616
617  fprintf(outfile,
618"}\n"
619"#define LAST_IDENTIFIER %d\n"
620  ,id_nr);
621  fclose(outfile);
622}
623/*-------------------------------------------------------------------*/
624#if 0
625void ttGen3()
626{
627  FILE *outfile = fopen("mpsr_tok.inc","w");
628  fprintf(outfile,
629  "/****************************************\n"
630  "*  Computer Algebra System SINGULAR     *\n"
631  "****************************************/\n\n");
632/*-------------------------------------------------------------------*/
633  fprintf(outfile,"// token table for Singular\n//\n");
634
635  fprintf(outfile,
636  "short vtok[] =\n"
637  "{\n");
638  // operations with 1 arg: ===========================================
639  int i=0;
640  while (dArith1[i].cmd!=0)
641  {
642    if ((dArith1[i].p!=jjWRONG)
643    &&((i==0)||(dArith1[i].cmd!=dArith1[i-1].cmd)))
644    {
645      fprintf(outfile,"  %d,\n",dArith1[i].cmd);
646    }
647    i++;
648  }
649  // operations with 2 args: ===========================================
650  i=0;
651  while (dArith2[i].cmd!=0)
652  {
653    if ((dArith2[i].p!=jjWRONG2)
654    &&((i==0)||(dArith2[i].cmd!=dArith2[i-1].cmd)))
655    {
656      fprintf(outfile,"  %d,\n",dArith2[i].cmd);
657    }
658    i++;
659  }
660  // operations with 3 args: ===========================================
661  i=0;
662  while (dArith3[i].cmd!=0)
663  {
664    if (
665    ((i==0)||(dArith3[i].cmd!=dArith3[i-1].cmd)))
666    {
667      fprintf(outfile,"  %d,\n",dArith3[i].cmd);
668    }
669    i++;
670  }
671  // operations with many args: ===========================================
672  i=0;
673  while (dArithM[i].cmd!=0)
674  {
675    if (
676    ((i==0)||(dArithM[i].cmd!=dArithM[i-1].cmd)))
677    {
678      fprintf(outfile,"  %d,\n",dArithM[i].cmd);
679    }
680    i++;
681  }
682  // ====================================================================
683  fprintf(outfile,
684  "/* end of list marker */\n"
685  " %d };\n",MAX_TOK);
686  fclose(outfile);
687}
688#endif
689void ttGen4()
690{
691  FILE *outfile = fopen("plural_cmd.xx","w");
692  int i;
693  const char *old_s="";
694  fprintf(outfile,
695  "@c *****************************************\n"
696  "@c *  Computer Algebra System SINGULAR     *\n"
697  "@c *****************************************\n\n");
698/*-------------------------------------------------------------------*/
699  fprintf(outfile,"@multicolumn .45 .45\n");
700  int op;
701  i=0;
702  while ((op=dArith1[i].cmd)!=0)
703  {
704    if (dArith1[i].p!=jjWRONG)
705    {
706      const char *s = iiTwoOps(op);
707      if ((s!=NULL) && (isalpha(s[0])) && (strcmp(s,old_s)!=0))
708      {
709        old_s=s;
710        #ifdef HAVE_PLURAL
711        switch (dArith1[i].valid_for & PLURAL_MASK)
712        {
713          case NO_PLURAL:
714            fprintf(outfile,"@item @ref{%s} @tab @code{---}\n",s);
715            break;
716          case ALLOW_PLURAL:
717            fprintf(outfile,"@item @ref{%s} @tab @ref{%s (plural)}\n",s,s);
718            break;
719          case COMM_PLURAL:
720            fprintf(outfile,"@item @ref{%s} @tab %s\n",s,s);
721            break;
722        }
723        #endif
724        #ifdef HAVE_RINGS
725        #endif
726      }
727    }
728    i++;
729  }
730  fprintf(outfile,"@c ---------------------------------------------\n");
731  i=0;
732  while ((op=dArith2[i].cmd)!=0)
733  {
734    if (dArith2[i].p!=jjWRONG2)
735    {
736      const char *s = iiTwoOps(op);
737      if ((s!=NULL) && (isalpha(s[0])) && (strcmp(s,old_s)!=0))
738      {
739        old_s=s;
740        #ifdef HAVE_PLURAL
741        switch (dArith2[i].valid_for & PLURAL_MASK)
742        {
743          case NO_PLURAL:
744            fprintf(outfile,"@item @ref{%s} @tab @code{---}\n",s);
745            break;
746          case ALLOW_PLURAL:
747            fprintf(outfile,"@item @ref{%s} @tab @ref{%s (plural)}\n",s,s);
748            break;
749          case COMM_PLURAL:
750            fprintf(outfile,"@item @ref{%s} @tab %s\n",s,s);
751            break;
752        }
753        #endif
754        #ifdef HAVE_RINGS
755        #endif
756      }
757    }
758    i++;
759  }
760  fprintf(outfile,"@c ---------------------------------------------\n");
761  i=0;
762  while ((op=dArith3[i].cmd)!=0)
763  {
764    const char *s = iiTwoOps(op);
765    if (dArith3[i].p!=jjWRONG3)
766    {
767      if ((s!=NULL) && (isalpha(s[0])) && (strcmp(s,old_s)!=0))
768      {
769        old_s=s;
770        #ifdef HAVE_PLURAL
771        switch (dArith3[i].valid_for & PLURAL_MASK)
772        {
773          case NO_PLURAL:
774            fprintf(outfile,"@item @ref{%s} @tab @code{---}\n",s);
775            break;
776          case ALLOW_PLURAL:
777            fprintf(outfile,"@item @ref{%s} @tab @ref{%s (plural)}\n",s,s);
778            break;
779          case COMM_PLURAL:
780            fprintf(outfile,"@item @ref{%s} @tab %s\n",s,s);
781            break;
782        }
783        #endif
784        #ifdef HAVE_RINGS
785        #endif
786      }
787    }
788    i++;
789  }
790  fprintf(outfile,"@c ---------------------------------------------\n");
791  i=0;
792  while ((op=dArithM[i].cmd)!=0)
793  {
794    const char *s = iiTwoOps(op);
795    if ((s!=NULL) && (isalpha(s[0])) && (strcmp(s,old_s)!=0))
796    {
797        old_s=s;
798        #ifdef HAVE_PLURAL
799        switch (dArithM[i].valid_for & PLURAL_MASK)
800        {
801          case NO_PLURAL:
802            fprintf(outfile,"@item @ref{%s} @tab @code{---}\n",s);
803            break;
804          case ALLOW_PLURAL:
805            fprintf(outfile,"@item @ref{%s} @tab @ref{%s (plural)}\n",s,s);
806            break;
807          case COMM_PLURAL:
808            fprintf(outfile,"@item @ref{%s} @tab %s\n",s,s);
809            break;
810        }
811        #endif
812        #ifdef HAVE_RINGS
813        #endif
814    }
815    i++;
816  }
817  fprintf(outfile,"@c ---------------------------------------------\n");
818  fprintf(outfile,"@end table\n");
819  fclose(outfile);
820}
821/*-------------------------------------------------------------------*/
822
823  // some special cmds which do not fit in with the others, and
824  // nevertheless need to be transmitted
825short ExtraCmds[] =
826{
827  OPTION_CMD,
828  NAMES_CMD,
829//  RESERVEDNAME_CMD,
830  PROC_CMD,
831  MAP_CMD,
832  PACKAGE_CMD,
833  '=',
834  0
835};
836
837// This returns 1 if tok is a token which can appear in a Singular
838// (quoted) command, and 0 otherwise
839short IsCmdToken(short tok)
840{
841  int i = 0;
842  // cmds with one arg
843  while (dArith1[i].cmd != 0)
844    if (dArith1[i].cmd == tok) return 1;
845    else i++;
846
847  // cmds with two args
848  i=0;
849  while (dArith2[i].cmd != 0)
850    if (dArith2[i].cmd == tok) return 1;
851    else i++;
852
853  // cmds with three args
854  i=0;
855  while (dArith3[i].cmd != 0)
856    if (dArith3[i].cmd == tok) return 1;
857    else i++;
858
859  // cmds with many args
860  i=0;
861  while (dArithM[i].cmd != 0)
862    if (dArithM[i].cmd == tok) return 1;
863    else i++;
864
865  // cmds which are somewhat special (like those having 0 args)
866  i=0;
867  while (ExtraCmds[i] != 0)
868    if (ExtraCmds[i] == tok) return 1;
869    else i++;
870
871  return 0;
872}
873
874#ifdef HAVE_MPSR
875#include <Singular/mpsr.h>
876#include <Singular/mpsr_Tok.h>
877#define MAX_COP 256 // there may be at most 256 cops
878
879// this is the main data struct for storing the relation
880// Singular token <-> (Dict, OP)
881typedef struct mpsr_cmd
882{
883
884  short         tok;    // The Singular token encoding
885
886  // The MP Dict tag in which this token is defined,
887  MP_DictTag_t  dict;
888
889  // The MP operator corresponding to this token
890  MP_Common_t   cop; // operator
891} mpsr_cmd;
892
893#define MAX_SR_DICT     5
894
895// this provides a mapping from MP dict tags to more useful (small)
896// array indicies
897inline short mpdict2srdict(MP_DictTag_t dict)
898{
899  if (dict == MP_SingularDict) return 0;
900  else if (dict == MP_BasicDict) return 1;
901  else if (dict == MP_PolyDict) return 2;
902  else if (dict == MP_MatrixDict) return 3;
903  else if (dict == MP_NumberDict) return 4;
904  else return MAX_SR_DICT;
905}
906
907// This is the array which holds all mpsr_cmds
908// It is initialized in mpsr_tok.inc
909static mpsr_cmd mpsr_cmds[MAX_TOK];
910
911// This is the array which stores the mapping from token to an mpsr_cmd
912// A value is either an index into mpsr_cmds, or MAX_TOK
913static short tok2mp[MAX_TOK];
914
915// This is the array which stores the mapping from (dict, cop) to a
916// mpsr_cmd. First index mpdict2srdict(dict), second is cop
917static short mp2tok[MAX_SR_DICT][MAX_COP];
918
919// This the list of all tokens which have an MP representation as a
920// cop in the Singular dictionary
921short sr_cmds[] =
922{
923  OPTION_CMD,
924  NAMES_CMD,
925  ATTRIB_CMD,
926  CHARSTR_CMD,
927  CLOSE_CMD,
928  DEF_CMD,
929  DEGREE_CMD,
930  DEFINED_CMD,
931  E_CMD,
932  EXECUTE_CMD,
933  FREEMODULE_CMD,
934  INT_CMD,
935  INTERRED_CMD,
936  INTMAT_CMD,
937  INTVEC_CMD,
938  IS_RINGVAR,
939  KILLATTR_CMD,
940  MAP_CMD,
941  MEMORY_CMD,
942  MONITOR_CMD,
943  NAMEOF_CMD,
944  NUMBER_CMD,
945  NPARS_CMD,
946  NVARS_CMD,
947  OPEN_CMD,
948  ORDSTR_CMD,
949  PAR_CMD,
950  PARSTR_CMD,
951  PARDEG_CMD,
952  POLY_CMD,
953  PRINT_CMD,
954  READ_CMD,
955  SORTVEC_CMD,
956  STRING_CMD,
957  SYSTEM_CMD,
958  TYPEOF_CMD,
959  VECTOR_CMD,
960  VAR_CMD,
961  VARSTR_CMD,
962  WEIGHT_CMD,
963  '(',
964  COEF_CMD,
965  DELETE_CMD,
966  FETCH_CMD,
967  FIND_CMD,
968  IMAP_CMD,
969  INSERT_CMD,
970  SIMPLIFY_CMD,
971  SRES_CMD,
972  DBPRINT_CMD,
973  TEST_CMD,
974  PROC_CMD,
975  MSTD_CMD,
976  RESERVEDNAME_CMD,
977  WRITE_CMD,
978  QRING_CMD,
979  FGLM_CMD,
980  FGLMQUOT_CMD,
981  DUMP_CMD,
982  GETDUMP_CMD,
983  STATUS_CMD,
984  LIB_CMD,
985  PACKAGE_CMD
986};
987
988// struct used for specifying the cmd <-> cop relations
989typedef struct cmd_cop
990{
991  short cmd;
992  MP_Common_t cop;
993} cmd_op;
994
995typedef struct cmd_dictcop
996{
997  MP_DictTag_t  dict;
998  cmd_op        cmd_ops[255];
999} cmd_dictcop;
1000
1001cmd_dictcop cmd_dictcops[] =
1002{
1003  {
1004    MP_PolyDict,
1005    // This is the list of all tokens which have an MP representation as a
1006    // cop in the Poly dictionary
1007    {
1008      {BETTI_CMD, MP_CopPolyBetti},
1009      {CHARACTERISTIC_CMD, MP_CopPolyChar},
1010      {CHAR_SERIES_CMD, MP_CopPolyCharSeries},
1011      {CONTENT_CMD, MP_CopPolyClearDenom },
1012      {DEG_CMD, MP_CopPolyDeg},
1013      {DIM_CMD, MP_CopPolyDim},
1014      {FAC_CMD, MP_CopPolyFactorize},
1015      {FACSTD_CMD, MP_CopPolyFacStd},
1016      {HILBERT_CMD, MP_CopPolyHilb},
1017      {HOMOG_CMD, MP_CopPolyHomog},
1018      {INDEPSET_CMD, MP_CopPolyInDepSet},
1019      {IDEAL_CMD, MP_CopPolyIdeal},
1020      {KBASE_CMD, MP_CopPolyKbase},
1021      {LEAD_CMD, MP_CopPolyLead},
1022      {LEADCOEF_CMD, MP_CopPolyLeadCoef},
1023      {LEADEXP_CMD, MP_CopPolyLeadExp},
1024      {MAXID_CMD, MP_CopPolyMaxIdeal},
1025      {MINBASE_CMD, MP_CopPolyMinBase},
1026      {MINRES_CMD, MP_CopPolyMinRes},
1027      {MODUL_CMD, MP_CopPolyModule},
1028      {MULTIPLICITY_CMD, MP_CopPolyMultiplicity},
1029      {ORD_CMD, MP_CopPolyOrder},
1030      {PRUNE_CMD, MP_CopPolyPrune},
1031      {QHWEIGHT_CMD, MP_CopPolyQHWeight},
1032      {REGULARITY_CMD, MP_CopPolyRegularity},
1033      {RESULTANT_CMD, MP_CopPolyResultant},
1034      {STD_CMD, MP_CopPolyStd},
1035      {SYZYGY_CMD, MP_CopPolySyz},
1036      {VDIM_CMD, MP_CopPolyVdim},
1037      {COEFFS_CMD,  MP_CopPolyCoeffs},
1038      {CONTRACT_CMD, MP_CopPolyContract},
1039      {ELIMINATION_CMD, MP_CopPolyEliminate},
1040      {JET_CMD, MP_CopPolyJet},
1041      {LIFT_CMD, MP_CopPolyLift},
1042      {LIFTSTD_CMD, MP_CopPolyLiftstd},
1043      {MODULO_CMD, MP_CopPolyModulo},
1044      {MRES_CMD, MP_CopPolyMres},
1045      {QUOTIENT_CMD, MP_CopPolyQuotient},
1046      {REDUCE_CMD, MP_CopPolyReduce},
1047      {PREIMAGE_CMD, MP_CopPolyPreimage},
1048      {RES_CMD, MP_CopPolyRes},
1049      {RING_CMD, MP_CopPolyRing},
1050      {MAX_TOK, 0}
1051    }
1052  },
1053  {
1054    MP_NumberDict,
1055    // This is the list of all tokens which have an MP representation as a
1056    // cop in the Number dictionary
1057    {
1058      {PRIME_CMD, MP_CopNumberPrime},
1059      {EXTGCD_CMD, MP_CopNumberExtGcd},
1060      {GCD_CMD, MP_CopNumberGcd},
1061      {RANDOM_CMD, MP_CopNumberRandom},
1062      {MAX_TOK, 0}
1063    }
1064  },
1065  {
1066    MP_MatrixDict,
1067    // This is the list of all tokens which have an MP representation as a
1068    // cop in the Matrix dictionary
1069    {
1070      {BAREISS_CMD, MP_CopMatrixBareiss},
1071      {COLS_CMD, MP_CopMatrixCols},
1072      {DET_CMD, MP_CopMatrixDet},
1073      {JACOB_CMD, MP_CopMatrixJacobi},
1074      {MATRIX_CMD, MP_CopMatrixDenseMatrix},
1075      {ROWS_CMD, MP_CopMatrixRows},
1076      {TRACE_CMD, MP_CopMatrixTrace},
1077      {TRANSPOSE_CMD, MP_CopMatrixTranspose},
1078      {KOSZUL_CMD, MP_CopMatrixKoszul},
1079      {MINOR_CMD, MP_CopMatrixMinor},
1080      {WEDGE_CMD, MP_CopMatrixWedge},
1081      {MAX_TOK, 0}
1082    }
1083  },
1084  {
1085    MP_BasicDict,
1086    // This is the list of all tokens which have an MP representation as a
1087    // cop in the MP Basic dictionary
1088    {
1089      {PLUSPLUS, MP_CopBasicInc},
1090      {MINUSMINUS,  MP_CopBasicDec},
1091      {COUNT_CMD, MP_CopBasicSize},
1092      {LIST_CMD, MP_CopBasicList},
1093      {'+', MP_CopBasicAdd},
1094      {'-', MP_CopBasicMinus},
1095      {'*', MP_CopBasicMult},
1096      {'/', MP_CopBasicDiv},
1097      {'%', MP_CopBasicMod},
1098      {'^', MP_CopBasicPow},
1099      {GE, MP_CopBasicGreaterEqual},
1100      {'<', MP_CopBasicGreater},
1101      {LE, MP_CopBasicLessEqual},
1102      {'>', MP_CopBasicLess},
1103      {'&', MP_CopBasicAnd},
1104      {'|', MP_CopBasicOr},
1105      {'=', MP_CopBasicAssign},
1106      {EQUAL_EQUAL, MP_CopBasicEqual},
1107      {NOTEQUAL, MP_CopBasicNotEqual},
1108      {DOTDOT, MP_CopBasicRange},
1109      {'[', MP_CopBasicIndex},
1110      {DIFF_CMD, MP_CopBasicDiff},
1111      {INTERSECT_CMD, MP_CopBasicInterSect},
1112      {SUBST_CMD, MP_CopBasicSubst},
1113      {NOT, MP_CopBasicNot},
1114      {COLONCOLON, MP_CopBasicPackage},
1115      {MAX_TOK, 0}
1116    }
1117  }
1118};
1119
1120
1121// Given a Singular token, find matching (dict,op): Return 1 if one is
1122// found, 0, otherwise
1123static short GetMPDictTok(short tok, MP_DictTag_t *dict, MP_Common_t *cop)
1124{
1125  short i, l, j;
1126  cmd_op *cmd_ops;
1127
1128  // first, look through Singular specific commands
1129  l = sizeof(sr_cmds)/sizeof(short);
1130  if (l > MAX_COP)
1131  {
1132    fprintf(stderr,
1133            "Error: There are more than 256 entries in MP_SingularDict\n");
1134    exit(1);
1135  }
1136  for (i=0; i<l; i++)
1137    if (sr_cmds[i] == tok)
1138    {
1139      *dict = MP_SingularDict;
1140      *cop = i;
1141      return 1;
1142    }
1143
1144  // look through all the other dicts
1145  for (j=0; j<MAX_SR_DICT-1; j++)
1146  {
1147    cmd_ops = cmd_dictcops[j].cmd_ops;
1148    for (i=0; (cmd_ops[i]).cmd != MAX_TOK; i++)
1149    {
1150      if (i > MAX_COP)
1151      {
1152        fprintf(stderr,
1153                "Error: There are more than 256 entries in dict %d's\n",j);
1154        exit(1);
1155      }
1156      if (cmd_ops[i].cmd == tok)
1157      {
1158        *dict = cmd_dictcops[j].dict;
1159        *cop = cmd_ops[i].cop;
1160        return 1;
1161      }
1162    }
1163  }
1164  return 0;
1165}
1166
1167
1168// This actually generates the tables of mpsr_tok.inc
1169char *mpsr_Tok_inc;
1170void mpsr_ttGen()
1171{
1172  mpsr_cmd mpsrcmds[MAX_TOK];
1173  short tok2mp[MAX_TOK];
1174  short mp2tok[MAX_SR_DICT][MAX_COP];
1175  short max_cmd = 0, i, j;
1176  MP_Common_t cop;
1177  FILE *outfile;
1178  MP_DictTag_t dict;
1179
1180
1181  // init all arrays
1182  for (i=0; i<MAX_TOK; i++)
1183  {
1184    mpsrcmds[i].tok = MAX_TOK;
1185    tok2mp[i] = MAX_TOK;
1186  }
1187  for (i=0; i<MAX_SR_DICT; i++)
1188    for (j=0; j<MAX_COP; j++)
1189      mp2tok[i][j] = MAX_TOK;
1190
1191  // Now go through all the token and test them
1192  for (i=0; i<MAX_TOK; i++)
1193  {
1194    if (IsCmdToken(i))
1195    {
1196      if (GetMPDictTok(i, &dict, &cop))
1197      {
1198        mpsrcmds[max_cmd].tok = i;
1199        mpsrcmds[max_cmd].dict = dict;
1200        mpsrcmds[max_cmd].cop = cop;
1201        tok2mp[i] = max_cmd;
1202        mp2tok[mpdict2srdict(dict)][cop] = i;
1203        max_cmd++;
1204      }
1205     //else
1206     //{
1207     //  fprintf(stderr, "Warning: mpsr_ttGen: Unknown Cmd Token: %d(%s)\n",
1208     //                  i, iiTwoOps(i));
1209     // }
1210    }
1211  }
1212
1213  // Generate the template file
1214  mpsr_Tok_inc=strdup("mpsr_Tok.xxxxxxxx");
1215  int pid=getpid();
1216  mpsr_Tok_inc[8]=(pid %10)+'0'; pid/=10;
1217  mpsr_Tok_inc[9]=(pid %10)+'0'; pid/=10;
1218  mpsr_Tok_inc[10]=(pid %10)+'0'; pid/=10;
1219  mpsr_Tok_inc[11]=(pid %10)+'0'; pid/=10;
1220  mpsr_Tok_inc[12]=(pid %10)+'0'; pid/=10;
1221  mpsr_Tok_inc[13]=(pid %10)+'0';
1222
1223  outfile = myfopen(mpsr_Tok_inc, "w");
1224  if (outfile == NULL)
1225  {
1226    fprintf(stderr, "Error: mpsr_ttGen: Cannot open file mpsr_Tok.inc\n");
1227    exit(1);
1228  }
1229
1230  // header
1231  fprintf(outfile,
1232   "/***************************************************************\n"
1233   "*\n"
1234   "* File:       mpsr_tok.inc\n"
1235   "* Purpose:    tables for mapping Singular cmds to/from MP (dict, op)\n"
1236   "*\n"
1237   "* THIS FILE WAS AUTOMATICALLY GENERATED BY mpsr_ttGen(). DO NOT EDIT!\n"
1238   "*\n"
1239   "***************************************************************/\n"
1240   "#ifndef MPSR_STRING_TABLES\n"
1241   "mpsr_cmd mpsr_cmds[] =\n"
1242   "{\n"
1243   "  { %d,\t %d,\t %d }", mpsrcmds[0].tok, mpsrcmds[0].dict, mpsrcmds[0].cop);
1244
1245  // mpsrcmds
1246  for (i=1; i<max_cmd; i++)
1247  {
1248    fprintf(outfile, ",\n  { %d,\t %d,\t %d }",
1249            mpsrcmds[i].tok, mpsrcmds[i].dict, mpsrcmds[i].cop);
1250  }
1251  fprintf(outfile,"\n};\n\n");
1252
1253  // tok2mp
1254  fprintf(outfile, "short tok2mp[] = { %d", tok2mp[0]);
1255  for (i=1; i<MAX_TOK; i++)
1256    fprintf(outfile, ", %d", tok2mp[i]);
1257  fprintf(outfile, "};\n\n");
1258
1259  // mp2tok
1260  fprintf(outfile, "short mp2tok[MAX_SR_DICT][MAX_COP] = \n{");
1261  for (i=0; i<MAX_SR_DICT; i++)
1262  {
1263    fprintf(outfile, "\n{\n");
1264    for (j=0; j<MAX_COP; j++)
1265    {
1266      fprintf(outfile, " %d",mp2tok[i][j]);
1267      if  (j!=MAX_COP-1) fprintf(outfile, ",");
1268    }
1269    if (i!=MAX_SR_DICT-1) fprintf(outfile, "},");
1270    else                  fprintf(outfile, "}");
1271  }
1272  fprintf(outfile,"\n};\n\n");
1273
1274  fprintf(outfile, "#else /* MPSR_STRING_TABLE */\n"
1275    "mpsr_cmd mpsr_cmds[] =\n"
1276    "{\n"
1277    "{ \"%s\",\t %d,\t %d }",
1278    iiTwoOps(mpsrcmds[0].tok), mpsrcmds[0].dict, mpsrcmds[0].cop);
1279
1280  for (i=1; i<max_cmd; i++)
1281  {
1282    fprintf(outfile, ",\n  { \"%s\",\t %d,\t %d }",
1283            iiTwoOps(mpsrcmds[i].tok), mpsrcmds[i].dict, mpsrcmds[i].cop);
1284  }
1285  fprintf(outfile, ",\n { NULL, \t 0, \t 0}");
1286  fprintf(outfile,"\n};\n\n#endif /* ! MPSR_STRING_TABLE */");
1287
1288  fclose(outfile);
1289} // That's all
1290
1291#else
1292void mpsr_ttGen()
1293{
1294  system("touch mpsr_Tok.xx");
1295}
1296#endif
1297
1298int main()
1299{
1300  //mpsr_ttGen();
1301  ttGen4();
1302  ttGen1();
1303  ttGen2b();
1304  rename(iparith_inc,"iparith.inc");
1305  rename("plural_cmd.xx","plural_cmd.inc");
1306  #ifdef HAVE_MPSR
1307  //rename(mpsr_Tok_inc,"mpsr_Tok.inc");
1308  #endif
1309  return 0;
1310}
Note: See TracBrowser for help on using the repository browser.