source: git/Singular/gentable2.cc @ 4e654a2

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