source: git/Singular/gentable2.cc @ 018dec

fieker-DuValspielwiese
Last change on this file since 018dec was 8a224e, checked in by Hans Schoenemann <hannes@…>, 14 years ago
fix cygwin build of gentable2 git-svn-id: file:///usr/local/Singular/svn/trunk@13509 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 14.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5
6/*
7* ABSTRACT: generate mpsr_Tok.inc
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};
76
77
78#define jjWRONG  1
79#define jjWRONG2  1
80#define jjWRONG3  1
81#define XS(A) A
82
83
84#define D(A)    2
85#define IPARITH
86#define GENTABLE
87
88#include "table.h"
89
90const char * Tok2Cmdname(int tok)
91{
92  int i = 0;
93  if (tok < 0)
94  {
95    return cmds[0].name;
96  }
97  if (tok==COMMAND) return "command";
98  if (tok==ANY_TYPE) return "any_type";
99  if (tok==NONE) return "nothing";
100  //if (tok==IFBREAK) return "if_break";
101  //if (tok==VECTOR_FROM_POLYS) return "vector_from_polys";
102  //if (tok==ORDER_VECTOR) return "ordering";
103  //if (tok==REF_VAR) return "ref";
104  //if (tok==OBJECT) return "object";
105  //if (tok==PRINT_EXPR) return "print_expr";
106  if (tok==IDHDL) return "identifier";
107  while (cmds[i].tokval!=0)
108  {
109    if ((cmds[i].tokval == tok)&&(cmds[i].alias==0))
110    {
111      return cmds[i].name;
112    }
113    i++;
114  }
115  return cmds[0].name;
116}
117
118const char * iiTwoOps(int t)
119{
120  if (t<127)
121  {
122    static char ch[2];
123    switch (t)
124    {
125      case '&':
126        return "and";
127      case '|':
128        return "or";
129      default:
130        ch[0]=t;
131        ch[1]='\0';
132        return ch;
133    }
134  }
135  switch (t)
136  {
137    case COLONCOLON:  return "::";
138    case DOTDOT:      return "..";
139    //case PLUSEQUAL:   return "+=";
140    //case MINUSEQUAL:  return "-=";
141    case MINUSMINUS:  return "--";
142    case PLUSPLUS:    return "++";
143    case EQUAL_EQUAL: return "==";
144    case LE:          return "<=";
145    case GE:          return ">=";
146    case NOTEQUAL:    return "<>";
147    default:          return Tok2Cmdname(t);
148  }
149}
150/*-------------------------------------------------------------------*/
151
152  // some special cmds which do not fit in with the others, and
153  // nevertheless need to be transmitted
154short ExtraCmds[] =
155{
156  OPTION_CMD,
157  NAMES_CMD,
158//  RESERVEDNAME_CMD,
159  PROC_CMD,
160  MAP_CMD,
161  PACKAGE_CMD,
162  '=',
163  0
164};
165
166// This returns 1 if tok is a token which can appear in a Singular
167// (quoted) command, and 0 otherwise
168short IsCmdToken(short tok)
169{
170  int i = 0;
171  // cmds with one arg
172  while (dArith1[i].cmd != 0)
173    if (dArith1[i].cmd == tok) return 1;
174    else i++;
175
176  // cmds with two args
177  i=0;
178  while (dArith2[i].cmd != 0)
179    if (dArith2[i].cmd == tok) return 1;
180    else i++;
181
182  // cmds with three args
183  i=0;
184  while (dArith3[i].cmd != 0)
185    if (dArith3[i].cmd == tok) return 1;
186    else i++;
187
188  // cmds with many args
189  i=0;
190  while (dArithM[i].cmd != 0)
191    if (dArithM[i].cmd == tok) return 1;
192    else i++;
193
194  // cmds which are somewhat special (like those having 0 args)
195  i=0;
196  while (ExtraCmds[i] != 0)
197    if (ExtraCmds[i] == tok) return 1;
198    else i++;
199
200  return 0;
201}
202
203#ifdef HAVE_MPSR
204#include <Singular/mpsr.h>
205#include <Singular/mpsr_Tok.h>
206#define MAX_COP 256 // there may be at most 256 cops
207
208// this is the main data struct for storing the relation
209// Singular token <-> (Dict, OP)
210typedef struct mpsr_cmd
211{
212
213  short         tok;    // The Singular token encoding
214
215  // The MP Dict tag in which this token is defined,
216  MP_DictTag_t  dict;
217
218  // The MP operator corresponding to this token
219  MP_Common_t   cop; // operator
220} mpsr_cmd;
221
222#define MAX_SR_DICT     5
223
224// this provides a mapping from MP dict tags to more useful (small)
225// array indicies
226inline short mpdict2srdict(MP_DictTag_t dict)
227{
228  if (dict == MP_SingularDict) return 0;
229  else if (dict == MP_BasicDict) return 1;
230  else if (dict == MP_PolyDict) return 2;
231  else if (dict == MP_MatrixDict) return 3;
232  else if (dict == MP_NumberDict) return 4;
233  else return MAX_SR_DICT;
234}
235
236// This is the array which holds all mpsr_cmds
237// It is initialized in mpsr_tok.inc
238static mpsr_cmd mpsr_cmds[MAX_TOK];
239
240// This is the array which stores the mapping from token to an mpsr_cmd
241// A value is either an index into mpsr_cmds, or MAX_TOK
242static short tok2mp[MAX_TOK];
243
244// This is the array which stores the mapping from (dict, cop) to a
245// mpsr_cmd. First index mpdict2srdict(dict), second is cop
246static short mp2tok[MAX_SR_DICT][MAX_COP];
247
248// This the list of all tokens which have an MP representation as a
249// cop in the Singular dictionary
250short sr_cmds[] =
251{
252  OPTION_CMD,
253  NAMES_CMD,
254  ATTRIB_CMD,
255  CHARSTR_CMD,
256  CLOSE_CMD,
257  DEF_CMD,
258  DEGREE_CMD,
259  DEFINED_CMD,
260  E_CMD,
261  EXECUTE_CMD,
262  FREEMODULE_CMD,
263  INT_CMD,
264  INTERRED_CMD,
265  INTMAT_CMD,
266  INTVEC_CMD,
267  IS_RINGVAR,
268  KILLATTR_CMD,
269  MAP_CMD,
270  MEMORY_CMD,
271  MONITOR_CMD,
272  NAMEOF_CMD,
273  NUMBER_CMD,
274  NPARS_CMD,
275  NVARS_CMD,
276  OPEN_CMD,
277  ORDSTR_CMD,
278  PAR_CMD,
279  PARSTR_CMD,
280  PARDEG_CMD,
281  POLY_CMD,
282  PRINT_CMD,
283  READ_CMD,
284  SORTVEC_CMD,
285  STRING_CMD,
286  SYSTEM_CMD,
287  TYPEOF_CMD,
288  VECTOR_CMD,
289  VAR_CMD,
290  VARSTR_CMD,
291  WEIGHT_CMD,
292  '(',
293  COEF_CMD,
294  DELETE_CMD,
295  FETCH_CMD,
296  FIND_CMD,
297  IMAP_CMD,
298  INSERT_CMD,
299  SIMPLIFY_CMD,
300  SRES_CMD,
301  DBPRINT_CMD,
302  TEST_CMD,
303  PROC_CMD,
304  MSTD_CMD,
305  RESERVEDNAME_CMD,
306  WRITE_CMD,
307  QRING_CMD,
308  FGLM_CMD,
309  FGLMQUOT_CMD,
310  DUMP_CMD,
311  GETDUMP_CMD,
312  STATUS_CMD,
313  LIB_CMD,
314  PACKAGE_CMD
315};
316
317// struct used for specifying the cmd <-> cop relations
318typedef struct cmd_cop
319{
320  short cmd;
321  MP_Common_t cop;
322} cmd_op;
323
324typedef struct cmd_dictcop
325{
326  MP_DictTag_t  dict;
327  cmd_op        cmd_ops[255];
328} cmd_dictcop;
329
330cmd_dictcop cmd_dictcops[] =
331{
332  {
333    MP_PolyDict,
334    // This is the list of all tokens which have an MP representation as a
335    // cop in the Poly dictionary
336    {
337      {BETTI_CMD, MP_CopPolyBetti},
338      {CHARACTERISTIC_CMD, MP_CopPolyChar},
339      {CHAR_SERIES_CMD, MP_CopPolyCharSeries},
340      {CONTENT_CMD, MP_CopPolyClearDenom },
341      {DEG_CMD, MP_CopPolyDeg},
342      {DIM_CMD, MP_CopPolyDim},
343      {FAC_CMD, MP_CopPolyFactorize},
344      {FACSTD_CMD, MP_CopPolyFacStd},
345      {HILBERT_CMD, MP_CopPolyHilb},
346      {HOMOG_CMD, MP_CopPolyHomog},
347      {INDEPSET_CMD, MP_CopPolyInDepSet},
348      {IDEAL_CMD, MP_CopPolyIdeal},
349      {KBASE_CMD, MP_CopPolyKbase},
350      {LEAD_CMD, MP_CopPolyLead},
351      {LEADCOEF_CMD, MP_CopPolyLeadCoef},
352      {LEADEXP_CMD, MP_CopPolyLeadExp},
353      {MAXID_CMD, MP_CopPolyMaxIdeal},
354      {MINBASE_CMD, MP_CopPolyMinBase},
355      {MINRES_CMD, MP_CopPolyMinRes},
356      {MODUL_CMD, MP_CopPolyModule},
357      {MULTIPLICITY_CMD, MP_CopPolyMultiplicity},
358      {ORD_CMD, MP_CopPolyOrder},
359      {PRUNE_CMD, MP_CopPolyPrune},
360      {QHWEIGHT_CMD, MP_CopPolyQHWeight},
361      {REGULARITY_CMD, MP_CopPolyRegularity},
362      {RESULTANT_CMD, MP_CopPolyResultant},
363      {STD_CMD, MP_CopPolyStd},
364      {SYZYGY_CMD, MP_CopPolySyz},
365      {VDIM_CMD, MP_CopPolyVdim},
366      {COEFFS_CMD,  MP_CopPolyCoeffs},
367      {CONTRACT_CMD, MP_CopPolyContract},
368      {ELIMINATION_CMD, MP_CopPolyEliminate},
369      {JET_CMD, MP_CopPolyJet},
370      {LIFT_CMD, MP_CopPolyLift},
371      {LIFTSTD_CMD, MP_CopPolyLiftstd},
372      {MODULO_CMD, MP_CopPolyModulo},
373      {MRES_CMD, MP_CopPolyMres},
374      {QUOTIENT_CMD, MP_CopPolyQuotient},
375      {REDUCE_CMD, MP_CopPolyReduce},
376      {PREIMAGE_CMD, MP_CopPolyPreimage},
377      {RES_CMD, MP_CopPolyRes},
378      {RING_CMD, MP_CopPolyRing},
379      {MAX_TOK, 0}
380    }
381  },
382  {
383    MP_NumberDict,
384    // This is the list of all tokens which have an MP representation as a
385    // cop in the Number dictionary
386    {
387      {PRIME_CMD, MP_CopNumberPrime},
388      {EXTGCD_CMD, MP_CopNumberExtGcd},
389      {GCD_CMD, MP_CopNumberGcd},
390      {RANDOM_CMD, MP_CopNumberRandom},
391      {MAX_TOK, 0}
392    }
393  },
394  {
395    MP_MatrixDict,
396    // This is the list of all tokens which have an MP representation as a
397    // cop in the Matrix dictionary
398    {
399      {BAREISS_CMD, MP_CopMatrixBareiss},
400      {COLS_CMD, MP_CopMatrixCols},
401      {DET_CMD, MP_CopMatrixDet},
402      {JACOB_CMD, MP_CopMatrixJacobi},
403      {MATRIX_CMD, MP_CopMatrixDenseMatrix},
404      {ROWS_CMD, MP_CopMatrixRows},
405      {TRACE_CMD, MP_CopMatrixTrace},
406      {TRANSPOSE_CMD, MP_CopMatrixTranspose},
407      {KOSZUL_CMD, MP_CopMatrixKoszul},
408      {MINOR_CMD, MP_CopMatrixMinor},
409      {WEDGE_CMD, MP_CopMatrixWedge},
410      {MAX_TOK, 0}
411    }
412  },
413  {
414    MP_BasicDict,
415    // This is the list of all tokens which have an MP representation as a
416    // cop in the MP Basic dictionary
417    {
418      {PLUSPLUS, MP_CopBasicInc},
419      {MINUSMINUS,  MP_CopBasicDec},
420      {COUNT_CMD, MP_CopBasicSize},
421      {LIST_CMD, MP_CopBasicList},
422      {'+', MP_CopBasicAdd},
423      {'-', MP_CopBasicMinus},
424      {'*', MP_CopBasicMult},
425      {'/', MP_CopBasicDiv},
426      {'%', MP_CopBasicMod},
427      {'^', MP_CopBasicPow},
428      {GE, MP_CopBasicGreaterEqual},
429      {'<', MP_CopBasicGreater},
430      {LE, MP_CopBasicLessEqual},
431      {'>', MP_CopBasicLess},
432      {'&', MP_CopBasicAnd},
433      {'|', MP_CopBasicOr},
434      {'=', MP_CopBasicAssign},
435      {EQUAL_EQUAL, MP_CopBasicEqual},
436      {NOTEQUAL, MP_CopBasicNotEqual},
437      {DOTDOT, MP_CopBasicRange},
438      {'[', MP_CopBasicIndex},
439      {DIFF_CMD, MP_CopBasicDiff},
440      {INTERSECT_CMD, MP_CopBasicInterSect},
441      {SUBST_CMD, MP_CopBasicSubst},
442      {NOT, MP_CopBasicNot},
443      {COLONCOLON, MP_CopBasicPackage},
444      {MAX_TOK, 0}
445    }
446  }
447};
448
449
450// Given a Singular token, find matching (dict,op): Return 1 if one is
451// found, 0, otherwise
452static short GetMPDictTok(short tok, MP_DictTag_t *dict, MP_Common_t *cop)
453{
454  short i, l, j;
455  cmd_op *cmd_ops;
456
457  // first, look through Singular specific commands
458  l = sizeof(sr_cmds)/sizeof(short);
459  if (l > MAX_COP)
460  {
461    fprintf(stderr,
462            "Error: There are more than 256 entries in MP_SingularDict\n");
463    exit(1);
464  }
465  for (i=0; i<l; i++)
466    if (sr_cmds[i] == tok)
467    {
468      *dict = MP_SingularDict;
469      *cop = i;
470      return 1;
471    }
472
473  // look through all the other dicts
474  for (j=0; j<MAX_SR_DICT-1; j++)
475  {
476    cmd_ops = cmd_dictcops[j].cmd_ops;
477    for (i=0; (cmd_ops[i]).cmd != MAX_TOK; i++)
478    {
479      if (i > MAX_COP)
480      {
481        fprintf(stderr,
482                "Error: There are more than 256 entries in dict %d's\n",j);
483        exit(1);
484      }
485      if (cmd_ops[i].cmd == tok)
486      {
487        *dict = cmd_dictcops[j].dict;
488        *cop = cmd_ops[i].cop;
489        return 1;
490      }
491    }
492  }
493  return 0;
494}
495
496
497// This actually generates the tables of mpsr_tok.inc
498char *mpsr_Tok_inc;
499void mpsr_ttGen()
500{
501  mpsr_cmd mpsrcmds[MAX_TOK];
502  short tok2mp[MAX_TOK];
503  short mp2tok[MAX_SR_DICT][MAX_COP];
504  short max_cmd = 0, i, j;
505  MP_Common_t cop;
506  FILE *outfile;
507  MP_DictTag_t dict;
508
509
510  // init all arrays
511  for (i=0; i<MAX_TOK; i++)
512  {
513    mpsrcmds[i].tok = MAX_TOK;
514    tok2mp[i] = MAX_TOK;
515  }
516  for (i=0; i<MAX_SR_DICT; i++)
517    for (j=0; j<MAX_COP; j++)
518      mp2tok[i][j] = MAX_TOK;
519
520  // Now go through all the token and test them
521  for (i=0; i<MAX_TOK; i++)
522  {
523    if (IsCmdToken(i))
524    {
525      if (GetMPDictTok(i, &dict, &cop))
526      {
527        mpsrcmds[max_cmd].tok = i;
528        mpsrcmds[max_cmd].dict = dict;
529        mpsrcmds[max_cmd].cop = cop;
530        tok2mp[i] = max_cmd;
531        mp2tok[mpdict2srdict(dict)][cop] = i;
532        max_cmd++;
533      }
534     //else
535     //{
536     //  fprintf(stderr, "Warning: mpsr_ttGen: Unknown Cmd Token: %d(%s)\n",
537     //                  i, iiTwoOps(i));
538     // }
539    }
540  }
541
542  // Generate the template file
543  mpsr_Tok_inc=strdup("mpsr_Tok.xxxxxxxx");
544  int pid=getpid();
545  mpsr_Tok_inc[8]=(pid %10)+'0'; pid/=10;
546  mpsr_Tok_inc[9]=(pid %10)+'0'; pid/=10;
547  mpsr_Tok_inc[10]=(pid %10)+'0'; pid/=10;
548  mpsr_Tok_inc[11]=(pid %10)+'0'; pid/=10;
549  mpsr_Tok_inc[12]=(pid %10)+'0'; pid/=10;
550  mpsr_Tok_inc[13]=(pid %10)+'0';
551
552  outfile = fopen(mpsr_Tok_inc, "w");
553  if (outfile == NULL)
554  {
555    fprintf(stderr, "Error: mpsr_ttGen: Cannot open file mpsr_Tok.inc\n");
556    exit(1);
557  }
558
559  // header
560  fprintf(outfile,
561   "/***************************************************************\n"
562   "*\n"
563   "* File:       mpsr_tok.inc\n"
564   "* Purpose:    tables for mapping Singular cmds to/from MP (dict, op)\n"
565   "*\n"
566   "* THIS FILE WAS AUTOMATICALLY GENERATED BY mpsr_ttGen(). DO NOT EDIT!\n"
567   "*\n"
568   "***************************************************************/\n"
569   "#ifndef MPSR_STRING_TABLES\n"
570   "mpsr_cmd mpsr_cmds[] =\n"
571   "{\n"
572   "  { %d,\t %d,\t %d }", mpsrcmds[0].tok, mpsrcmds[0].dict, mpsrcmds[0].cop);
573
574  // mpsrcmds
575  for (i=1; i<max_cmd; i++)
576  {
577    fprintf(outfile, ",\n  { %d,\t %d,\t %d }",
578            mpsrcmds[i].tok, mpsrcmds[i].dict, mpsrcmds[i].cop);
579  }
580  fprintf(outfile,"\n};\n\n");
581
582  // tok2mp
583  fprintf(outfile, "short tok2mp[] = { %d", tok2mp[0]);
584  for (i=1; i<MAX_TOK; i++)
585    fprintf(outfile, ", %d", tok2mp[i]);
586  fprintf(outfile, "};\n\n");
587
588  // mp2tok
589  fprintf(outfile, "short mp2tok[MAX_SR_DICT][MAX_COP] = \n{");
590  for (i=0; i<MAX_SR_DICT; i++)
591  {
592    fprintf(outfile, "\n{\n");
593    for (j=0; j<MAX_COP; j++)
594    {
595      fprintf(outfile, " %d",mp2tok[i][j]);
596      if  (j!=MAX_COP-1) fprintf(outfile, ",");
597    }
598    if (i!=MAX_SR_DICT-1) fprintf(outfile, "},");
599    else                  fprintf(outfile, "}");
600  }
601  fprintf(outfile,"\n};\n\n");
602
603  fprintf(outfile, "#else /* MPSR_STRING_TABLE */\n"
604    "mpsr_cmd mpsr_cmds[] =\n"
605    "{\n"
606    "{ \"%s\",\t %d,\t %d }",
607    iiTwoOps(mpsrcmds[0].tok), mpsrcmds[0].dict, mpsrcmds[0].cop);
608
609  for (i=1; i<max_cmd; i++)
610  {
611    fprintf(outfile, ",\n  { \"%s\",\t %d,\t %d }",
612            iiTwoOps(mpsrcmds[i].tok), mpsrcmds[i].dict, mpsrcmds[i].cop);
613  }
614  fprintf(outfile, ",\n { NULL, \t 0, \t 0}");
615  fprintf(outfile,"\n};\n\n#endif /* ! MPSR_STRING_TABLE */");
616
617  fclose(outfile);
618} // That's all
619
620#else
621void mpsr_ttGen()
622{
623  system("touch mpsr_Tok.xx");
624}
625#endif
626
627int main()
628{
629  mpsr_ttGen();
630  #ifdef HAVE_MPSR
631  rename(mpsr_Tok_inc,"mpsr_Tok.inc");
632  #endif
633  return 0;
634}
Note: See TracBrowser for help on using the repository browser.