source: git/modules/modgen/misc.cc @ 250362

spielwiese
Last change on this file since 250362 was 250362, checked in by Hans Schönemann <hannes@…>, 19 years ago
*anne/hannes: include dir .... git-svn-id: file:///usr/local/Singular/svn/trunk@7926 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: misc.cc,v 1.23 2005-04-28 13:47:18 Singular Exp $ */
5/*
6* ABSTRACT: lib parsing
7*/
8
9#include <stdlib.h>
10#include <mod2.h>
11#include <febase.h>
12#include <grammar.h>
13#include <structs.h>
14#include <tok.h>
15#include <ipid.h>
16#include <ipshell.h>
17#include <structs.h>
18#include <subexpr.h>
19#include <regex.h>
20
21#include "modgen.h"
22#include "typmap.h"
23
24#define NEW_PARAM 1
25
26#define SYSTYP_NONE   0
27#define SYSTYP_LINUX  1
28#define SYSTYP_HPUX9  2
29#define SYSTYP_HPUX10 3
30
31#if 1
32#  define logx printf
33#else
34#  define logx
35#endif
36
37char *DYNAinclude[] = {
38   "",
39   "#include <dlfcn.h>",
40   "#include <dl.h>",
41   "#include <dl.h>",
42   "#include <>",
43   "#include <>",
44   "#include <>",
45   NULL
46};
47
48int systyp = SYSTYP_NONE;
49
50BOOLEAN    expected_parms;
51int        cmdtok;
52BOOLEAN siq=FALSE;
53char *lastreserved=NULL;
54#define SELF_CMD MAX_TOK+1
55
56extern void enter_id(FILE *fp, idtyp t, char *name, char *value,
57                     int lineno, char *file);
58extern void write_enter_id(FILE *fp);
59extern void write_add_singular_proc(FILE *fp);
60
61static void  mod_write_ctext(FILE *fp, FILE *fp_in);
62char type_conv[MAX_TOK][32];
63
64extern void write_intro(moddefv module);
65
66/*=============== types =====================*/
67struct sValCmdTab
68{
69  short cmd;
70  short start;
71};
72
73#include "../../Singular/iparith.inc"
74
75/*=================== general utilities ============================*/
76int IsCmd(char *n, int & tok)
77{
78  int an=1;
79  int i,v;
80  int en=LAST_IDENTIFIER;
81 
82  if( strcmp(n, "SELF") == 0) {
83    tok = SELF_CMD;
84    logx("IsCmd: [%d] %s\n", tok, n);
85    return tok;
86  }
87 
88  if( strcmp(n, "none") == 0) {
89    tok = NONE;
90    logx("IsCmd: [%d] %s\n", tok, n);
91    return tok;
92  }
93 
94  loop
95  {
96    if(an>=en-1)
97    {
98      if (strcmp(n, cmds[an].name) == 0)
99      {
100        i=an;
101        break;
102      }
103      else if (strcmp(n, cmds[en].name) == 0)
104      {
105        i=en;
106        break;
107      }
108      else
109      {
110        return 0;
111      }
112    }
113    i=(an+en)/2;
114    v=strcmp(n,cmds[i].name);
115    if(v<0)
116    {
117      en=i;
118    }
119    else if(v>0)
120    {
121      an=i;
122    }
123    else /*v==0*/
124    {
125      break;
126    }
127  }
128  lastreserved=cmds[i].name;
129  tok=cmds[i].tokval;
130  if(cmds[i].alias==2)
131  {
132    if(trace)printf("outdated identifier `%s` used - please change your code",
133    cmds[i].name);
134    cmds[i].alias=1;
135  }
136#if 0
137  if (!expected_parms)
138  {
139    switch (tok)
140    {
141      case IDEAL_CMD:
142      case INT_CMD:
143      case INTVEC_CMD:
144      case MAP_CMD:
145      case MATRIX_CMD:
146      case MODUL_CMD:
147      case POLY_CMD:
148      case PROC_CMD:
149      case RING_CMD:
150      case STRING_CMD:
151        cmdtok = tok;
152        break;
153    }
154  }
155#endif
156  logx("IsCmd: [%d] %s\n", tok, n);
157 
158  if( (cmds[i].toktype==ROOT_DECL) ||
159      (cmds[i].toktype==ROOT_DECL_LIST) ||
160      (cmds[i].toktype==RING_DECL) ||
161      (cmds[i].toktype==IDEAL_CMD) ||
162      (cmds[i].toktype==INTMAT_CMD) ||
163      (cmds[i].toktype==MODUL_CMD) ||
164      (cmds[i].toktype==MATRIX_CMD))// ||
165//      ((cmds[i].toktype>=DRING_CMD) && (cmds[i].toktype<=VECTOR_CMD)))
166    return cmds[i].toktype;
167  return 0;
168}
169
170char * decl2str(int n, char *name)
171{
172  switch(n)
173    {
174#   include "decl.inc"
175
176    /* first and last entry of tok.h cannot be grepped */
177    case MAX_TOK: strcpy(name,"MAX_TOK");       break;
178    case ALIAS_CMD: strcpy(name,"ALIAS_CMD");       break;
179    default: strcpy(name,"(null)");
180  }
181  return(name);
182}
183
184/*========================================================================*/
185struct valid_cmds_def
186{
187  char *name;
188  void (*write_cmd)(moddefv module, procdefv pi, void *arg = NULL);
189  cmd_token id;
190  cmd_type  type;
191  int args;
192} valid_cmds[] = {
193  { "declaration",  write_function_declaration, CMD_DECL,   CMDT_SINGLE, 0 },
194  { "error",        write_function_error,       CMD_ERROR,  CMDT_ANY,    0 },
195  { "nodeclaration",write_function_nodecl,      CMD_NODECL, CMDT_SINGLE, 0 },
196  { "typecheck",    write_function_typecheck,   CMD_CHECK,  CMDT_SINGLE, 0 },
197  { "return",       write_function_result,      CMD_RETURN, CMDT_EQ,     0 },
198//{ "return",       write_function_return,      CMD_RETURN, CMDT_SINGLE, 1 },
199  { "return",       write_function_return,      CMD_RETURN, CMDT_0,     1 },
200  { "return",       write_function_return,      CMD_RETURN, CMDT_ANY,   1 },
201  { "singularcmd",  write_function_singularcmd, CMD_SINGULAR, CMDT_ANY, 1 },
202  { NULL,           0, CMD_NONE, CMDT_ANY, 0 }
203};
204
205cmd_token checkcmd(
206  char *cmdname,
207  void (**write_cmd)(moddefv module, procdefv pi, void *arg),
208  cmd_type  type,
209  int args
210  )
211{
212  int i;
213  cmd_token rc = CMD_NONE;
214 
215  for(i=0; valid_cmds[i].name!=NULL; i++)
216    if(strcmp(valid_cmds[i].name, cmdname)==0) {
217      rc = CMD_BADSYNTAX;
218      if(valid_cmds[i].type == type) {
219        *write_cmd = valid_cmds[i].write_cmd;
220        return valid_cmds[i].id;
221      }
222    }
223  return rc;
224}
225 
226/*========================================================================*/
227struct valid_vars_def {
228  char *name;
229  var_type type;
230  var_token id;
231  void (*write_cmd)(moddefv module, var_token type = VAR_NONE,
232                    idtyp t, void *arg1 = NULL, void *arg2 = NULL);
233} valid_vars[] = {
234  { "help",         VAR_STRING,  VAR_HELP,     write_main_variable },
235  { "info",         VAR_STRING,  VAR_INFO,     write_main_variable },
236  { "package",      VAR_STRING,  VAR_MODULE,   0 },
237  { "version",      VAR_STRING,  VAR_VERSION,  write_main_variable },
238  { "category",     VAR_STRING,  VAR_CATEGORY, write_main_variable },
239  { NULL,           VAR_UNKNOWN, VAR_NONE, 0 }
240};
241
242var_token checkvar(
243  char *varname,
244  var_type type,
245  void (**write_cmd)(moddefv module, var_token type,
246                    idtyp t, void *arg1, void *arg2)
247  )
248{
249  int i;
250  for(i=0; valid_vars[i].name!=NULL; i++)
251    if((strcmp(valid_vars[i].name, varname)==0) &&
252       (valid_vars[i].type == type) ) {
253      *write_cmd = valid_vars[i].write_cmd;
254      return valid_vars[i].id;
255    }
256  return VAR_NONE;
257}
258
259/*========================================================================*/
260void write_main_variable(
261  moddefv module,
262  var_token type,
263  idtyp t, 
264  void *arg1,
265  void *arg2
266  )
267{
268  enter_id(module->fmtfp, t, (char *)arg1, (char *)arg2, yylineno,
269           module->filename);
270  switch(type) {
271    case VAR_INFO:
272          module->info = (char *)malloc(strlen((char *)arg2)+1);
273          memset(module->info, '\0', strlen((char *)arg2)+1);
274          memcpy(module->info,(char *)arg2,strlen((char *)arg2));
275          break;
276    case VAR_VERSION:
277          module->version = (char *)malloc(strlen((char *)arg2)+1);
278          memset(module->version, '\0', strlen((char *)arg2)+1);
279          memcpy(module->version,(char *)arg2,strlen((char *)arg2));
280          break;
281    case VAR_CATEGORY:
282          module->category = (char *)malloc(strlen((char *)arg2)+1);
283          memset(module->category, '\0', strlen((char *)arg2)+1);
284          memcpy(module->category,(char *)arg2,strlen((char *)arg2));
285          break;
286    default: break;
287  }
288}
289 
290/*========================================================================*/
291void PrintProc(
292  procdefv pi
293  )
294{
295  int i;
296 
297  printf("%4d proc: %s(", pi->lineno, pi->procname);
298 
299  for(i=0; i<pi->paramcnt; i++) {
300    printf("%s (%s)", (pi->param[i]).name, (pi->param[i]).typname);
301    if(i < (pi->paramcnt-1)) printf(",");
302  }
303  printf(")\n");
304  if(pi->return_val.typ!=0)
305    printf("\treturn = %s (%s)\n", pi->return_val.name,
306           pi->return_val.typname);
307  printf("{%s}\n", pi->c_code);
308}
309
310/*========================================================================*/
311void make_version(char *p, moddefv module)
312{
313  char ver[10];
314  char date[16];
315  char libnamebuf[128];
316 
317  module->major = 0;
318  module->minor = 0;
319  module->level = 0;
320 
321  ver[0]='0'; ver[1]='.'; ver[2]='0'; ver[3]='.'; ver[4]='0'; ver[5]='\0';
322  date[0]='?'; date[1]='\0';
323  sscanf(p,"%*s %*s %10s %16s",ver,date);
324  sscanf(ver, "%d.%d.%d", &module->major, &module->minor, &module->level);
325 
326  sprintf(libnamebuf,"(%s,%s)", ver, date);
327  if(strcmp(libnamebuf, "(0.0.0,?)")==0)
328  {
329    sscanf(p,"%*[^\"]\"%[^\"]\"",libnamebuf);
330  }
331  module->revision = (char *)malloc(strlen(libnamebuf)+1);
332  memset(module->revision, '\0', strlen(libnamebuf)+1);
333  memcpy(module->revision, libnamebuf, strlen(libnamebuf));
334}
335
336/*========================================================================*/
337void make_module_name(char *p, moddefv module)
338{
339  if(strlen(p)>=1)
340  {
341    module->targetname = (char *)malloc(strlen(p)+1);
342    memset(module->targetname, '\0', strlen(p)+1);
343    memcpy(module->targetname,p,strlen(p));
344  } else {
345    module->targetname = (char *)malloc(strlen(module->name)+1);
346    memset(module->targetname, '\0', strlen(module->name)+1);
347    memcpy(module->targetname,module->name,strlen(module->name));
348  }
349
350}
351
352/*========================================================================*/
353void Add2files(
354  moddefv module,
355  char *name
356  )
357{
358  cfiles cfnew;
359  memset((void *)&cfnew, '\0', sizeof(cfiles));
360
361  cfnew.filename = (char *)malloc(strlen(name)+1);
362  memset(cfnew.filename, '\0', strlen(name)+1);
363  memcpy(cfnew.filename, name, strlen(name));
364
365  if(module->filecnt==0) {
366    module->files = (cfilesv)malloc(sizeof(cfiles)+1);
367  }
368  else {
369    module->files = (cfilesv)realloc(module->files,
370                                   (module->filecnt+1)*sizeof(cfiles));
371  }
372  if(module->files == NULL) { printf("ERROR\n"); return; }
373 
374  memset((void *) &module->files[module->filecnt], '\0', sizeof(cfiles));
375  memcpy((void *)(&(module->files[module->filecnt])),
376         (void *)&cfnew, sizeof(cfiles));
377  (module->filecnt)++;
378}
379
380/*========================================================================*/
381void init_system_type()
382{
383  if(strcmp(S_UNAME, "ix86-Linux") == 0) {
384    systyp = SYSTYP_LINUX;
385  } else if (strcmp(S_UNAME, "HPUX-9")==0) {
386    systyp = SYSTYP_HPUX9;
387  } else if (strcmp(S_UNAME, "HPUX-10")==0) {
388    systyp = SYSTYP_HPUX10;
389  }
390}
391
392/*========================================================================*/
393void PrintProclist(
394  moddefv module
395  )
396{
397  logx("PrintProclist()\n");
398  int j;
399  for(j=0; j<module->proccnt; j++) {
400    PrintProc(&(module->procs[j]));
401  }
402}
403
404/*========================================================================*/
405void generate_mod(
406  moddefv module,
407  int section
408  )
409{
410  procdefv v = NULL;
411  cfilesv c_filelist = NULL;
412  int proccnt;
413  FILE *fp_h;
414  char *filename;
415 
416  switch(section) {
417      case 1:
418        write_intro(module);
419        return;
420       
421      case 2:
422        printf("Writing %s, section %d", filename, section);fflush(stdout);
423        break;
424  }
425 
426  sprintf(filename, "%s.h", module->name);
427  fp_h = fopen(filename, "w");
428  write_header(fp_h, module->name);
429  printf("%s  ...", filename);fflush(stdout);
430 
431  /* building mod_init() */
432 
433  for(proccnt=0; proccnt<module->proccnt; proccnt++) {
434    printf("->%s, %s\n", module->procs[proccnt].procname,
435           module->procs[proccnt].funcname);
436    fprintf(module->modfp, "  iiAddCproc(\"%s\",\"%s\",%s, mod_%s);\n",
437            module->name, module->procs[proccnt].procname, 
438            module->procs[proccnt].is_static ? "TRUE" : "FALSE",
439            module->procs[proccnt].funcname);
440  }
441  fprintf(module->modfp, "  return 0;\n}\n\n");
442
443  /* building entry-functions */
444  for(proccnt=0; proccnt<module->proccnt; proccnt++) {
445    //generate_function(&module->procs[proccnt], module->modfp);
446    //generate_header(&module->procs[proccnt], fp_h);
447  }
448  printf("  done.\n");fflush(stdout);
449  fclose(module->modfp);
450  fclose(fp_h);
451}
452
453
454
455
456/*========================================================================*/
457void  write_procedure_text(
458  moddefv module,
459  int lineno
460)
461{
462  int i;
463  procdefv pi = &module->procs[module->proccnt-1];
464
465  fprintf(module->fmtfp, "#line %d \"%s\"\n", lineno, module->filename);
466  fprintf(module->fmtfp, "%s\n", module->procs[module->proccnt-1].c_code);
467  free(module->procs[module->proccnt-1].c_code);
468  module->procs[module->proccnt-1].c_code = NULL;
469
470#if 0
471  if(pi->paramcnt>0) {
472  }
473  else {
474    switch( pi->return_val.typ) {
475        case SELF_CMD:
476          fprintf(module->fmtfp, "  return(%s(res));", pi->funcname);
477          break;
478
479        case NONE:
480          fprintf(module->fmtfp, "  return FALSE;");
481          break;
482           
483        default:
484          fprintf(module->fmtfp, "  res->rtyp = %s;\n",
485                  pi->return_val.typname);
486          fprintf(module->fmtfp, "  res->data = (void *)%s();\n",
487                  pi->funcname);
488          fprintf(module->fmtfp, "  return FALSE;");
489    }
490  }
491#endif
492  fprintf(module->fmtfp, "/*\n}\n\n*/");
493 
494}
495
496
497/*========================================================================*/
498void  mod_write_header(FILE *fp, char *module, char what)
499{
500
501  write_header(fp, module);
502  fprintf(fp, "#line %d \"%s.cc\"\n", modlineno++, module);
503  if(what != 'h') {
504    fprintf(fp, "#include <stdlib.h>\n");
505    fprintf(fp, "#include <stdio.h>\n");
506    fprintf(fp, "#include <string.h>\n");
507    fprintf(fp, "#include <ctype.h>\n");
508    fprintf(fp, "%s\n", DYNAinclude[systyp]);
509    fprintf(fp, "#include <unistd.h>\n");
510    fprintf(fp, "#include <sys/stat.h>");
511    fprintf(fp, "\n");
512    fprintf(fp, "#include <mod2.h>\n");
513    fprintf(fp, "#include <tok.h>\n");
514    fprintf(fp, "#include <structs.h>\n");
515    fprintf(fp, "#include <ipid.h>\n\n");
516    fprintf(fp, "#include <locals.h>\n");
517    fprintf(fp, "#include <omalloc.h>\n");
518    fprintf(fp, "#include \"%s.h\"\n", module);
519    modlineno+=8;
520   
521    fprintf(fp, "#line %d \"%s.cc\"\n", modlineno++, module);
522    write_enter_id(fp);
523    fprintf(fp, "\n");    modlineno+=1;
524    fprintf(fp, "#line %d \"%s.cc\"\n", modlineno++, module);
525    write_add_singular_proc(fp);
526    write_crccheck(fp);
527    fprintf(fp, "\n");
528   
529    fprintf(fp, "void fill_help_package();\n");
530    fprintf(fp, "void fill_example_package();\n");
531    modlineno+=3;
532  }
533  fprintf(fp, "\n");
534 
535}
536
537/*========================================================================*/
538void write_header(FILE *fp, char *module, char *comment)
539{
540  fprintf(fp, "%s/*\n%s * This was automatically generated by modgen\n",
541          comment, comment);
542  fprintf(fp, "%s * version %s\n", comment, MOD_GEN_VERSION);
543  fprintf(fp, "%s * module %s\n", comment, module);
544  fprintf(fp, "%s * Don't edit this file\n%s */\n", comment, comment);
545  fprintf(fp, "%s\n", comment);
546  fprintf(fp, "%s\n", comment);
547  if(strlen(comment)==1)modlineno+=10;
548}
549
550/*========================================================================*/
551void enter_id(
552  FILE *fp, 
553  idtyp t,
554  char *name,
555  char *value,
556  int lineno,
557  char *file
558  )
559{
560  char tname[32];
561 
562  if(lineno)
563    fprintf(fp, "#line %d \"%s\"\n", lineno, file);
564  fprintf(fp, "  enter_id(\"%s\",\"%s\", %s);\n",name, value,
565          decl2str(t, tname));
566}
567
568/*========================================================================*/
569void init_type_conv() 
570{
571  strcpy(type_conv[NONE], "none");
572//  strcpy(type_conv[NONE], "void");
573  strcpy(type_conv[INT_CMD], "int");
574  strcpy(type_conv[RING_CMD], "ring");
575  strcpy(type_conv[QRING_CMD], "ring");
576  strcpy(type_conv[POLY_CMD], "poly");
577  strcpy(type_conv[NUMBER_CMD], "number");
578  strcpy(type_conv[MODUL_CMD], "ideal");
579  strcpy(type_conv[VECTOR_CMD], "ideal");
580  strcpy(type_conv[IDEAL_CMD], "ideal");
581  strcpy(type_conv[MAP_CMD], "map");
582  strcpy(type_conv[MATRIX_CMD], "matrix");
583  strcpy(type_conv[STRING_CMD], "char *");
584  strcpy(type_conv[INTMAT_CMD], "intvec *");
585  strcpy(type_conv[INTVEC_CMD], "intvec *");
586  strcpy(type_conv[LIST_CMD], "lists");
587  strcpy(type_conv[LINK_CMD], "si_link");
588  strcpy(type_conv[PACKAGE_CMD], "package");
589  strcpy(type_conv[PROC_CMD], "procinfo *");
590/*
591  strcpy(type_conv[], "");
592  strcpy(type_conv[], "");
593  strcpy(type_conv[], "");
594  strcpy(type_conv[], "");
595  strcpy(type_conv[], "");
596  strcpy(type_conv[], "");
597  strcpy(type_conv[], "");
598  printf("[%d] %s\n", INT_CMD, type_conv[INT_CMD]);
599  printf("[%d] %s\n", MODUL_CMD, type_conv[MODUL_CMD]);
600  printf("[%d] %s\n", STRING_CMD, type_conv[STRING_CMD]);
601  printf("[%d] %s\n", LINK_CMD, type_conv[LINK_CMD]);
602  printf("[%d] %s\n", PACKAGE_CMD, type_conv[PACKAGE_CMD]);
603  / **/
604}
605
606/*========================================================================*/
Note: See TracBrowser for help on using the repository browser.