=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/Kan/Attic/yylex_polymake.c,v retrieving revision 1.1 retrieving revision 1.3 diff -u -p -r1.1 -r1.3 --- OpenXM/src/kan96xx/Kan/Attic/yylex_polymake.c 2003/11/20 00:06:07 1.1 +++ OpenXM/src/kan96xx/Kan/Attic/yylex_polymake.c 2003/11/20 06:04:04 1.3 @@ -1,4 +1,4 @@ -/* $OpenXM$ */ +/* $OpenXM: OpenXM/src/kan96xx/Kan/yylex_polymake.c,v 1.2 2003/11/20 03:25:08 takayama Exp $ */ /* parser for polymake output */ /* This program requires @@ -9,8 +9,10 @@ #include "yylex_polymake.h" #include "yy_polymake.tab.h" -/* #define mymalloc(n) sGC_malloc(n) */ +#define mymalloc(n) sGC_malloc(n) +/* #define mymalloc(n) malloc(n) +*/ /* pm = PolyMake */ #define PM_emptyLineCode 1 @@ -23,8 +25,23 @@ static int PMdebug = 0; /* char *PMlval; */ +/* The function putstr() uses static variables inside, + so if it is under use, it must not be initialized. + The function putstr2() is for the second use and is identical + function with putstr(). +*/ static char *putstr(int c); +static char *putstr2(int c); +char *pmPutstr(int c) { + return putstr(c); +} + +pmSetS(char *s) { + S = s; + return 0; +} + int PMlex() { int type; type = PMlex_aux(); @@ -97,6 +114,34 @@ static char *putstr(int c) { return putstr(c); } } +static char *putstr2(int c) { + static char *s=NULL; + static int pt=0; + static int limit=0; + int i; + char *old; + if (c < 0) { + s = (char *)mymalloc(PUTSTR_INIT); + if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(10);} + limit = PUTSTR_INIT; + pt = 0; s[pt] = 0; + return s; + } + if (s == NULL) putstr(-1); + if (pt < limit-1) { + s[pt++]=c; s[pt]=0; + return s; + }else{ + old = s; + limit = 2*limit; + s = (char *)mymalloc(limit); + if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(10);} + for (i=0; i<=pt; i++) { + s[i] = old[i]; + } + return putstr(c); + } +} pmPreprocess() { int newp,oldp; @@ -194,10 +239,95 @@ pmObjectp pmCons(pmObjectp a,struct pmList *b) { t->right = b; return ob; } + +int pmListLength(struct pmList *list) { + struct pmList *t; + int n; + if (list == NULL) return 0; + n = 0; + t = list; + while (t != NULL) { + n++; t = t->right; + } + return n; +} + +pmObjectp pmNewTreeObjecto(pmObjectp s) +{ + if ((s == NULL) || (s->tag != PMobject_str)) { + warning_yylex_polymake("Invalid argument for pmNewObjecto\n"); + return pmNewTreeObject("?"); + } + return pmNewTreeObject((char *)(s->body)); +} +pmObjectp pmNewTreeObject(char *s) { + pmObjectp ob; + struct pmTree *aa; + ob = (pmObjectp) mymalloc(sizeof(struct pmObject)); + if (ob == NULL) { + fprintf(stderr,"No more memory.\n"); + exit(10); + } + aa= (struct pmTree *)mymalloc(sizeof(struct pmTree)); + if (aa == NULL) { + fprintf(stderr,"No more memory.\n"); + exit(10); + } + aa->nodeName = s; + aa->attrList = NULL; + aa->childs = NULL; + ob->tag = PMobject_tree; + ob->body = aa; + return ob; +} + +pmObjectp pmAddAttr(pmObjectp c,pmObjectp a) { + struct pmTree *tree; + if (a->tag != PMobject_tree) { + warning_yylex_polymake("pmAddAttr: the argument is not tree object.\n"); + return a; + } + tree = a->body; + if (tree->attrList == NULL) { + tree->attrList = pmNewListObject(c); + }else{ + if (tree->attrList->tag == PMobject_list) { + tree->attrList = pmCons(c,(struct pmList *)(tree->attrList->body)); + }else{ + warning_yylex_polymake("pmAddAttr: the attrbute list is broken.\n"); + } + } + return a; +} + +/* Add c to the tree a */ +pmObjectp pmAddChild(pmObjectp c,pmObjectp a) { + struct pmTree *tree; + if (a->tag != PMobject_tree) { + warning_yylex_polymake("pmAddAttr: the argument is not tree object.\n"); + return a; + } + tree = a->body; + if (tree->childs == NULL) { + tree->childs = pmNewListObject(c); + }else{ + if (tree->childs->tag == PMobject_list) { + tree->childs = pmCons(c,(struct pmList *)(tree->childs->body)); + }else{ + warning_yylex_polymake("pmAddAttr: the child list is broken.\n"); + } + } + return a; +} + +warning_yylex_polymake(char *s) { + fprintf(stderr,"Warning: %s",s); +} void pmPrintObject(FILE *fp,pmObjectp p) { int n,i; struct pmList *list; struct pmList *t; + struct pmTree *tree; if (p == NULL) { /* fprintf(stderr,"NULL "); */ return; @@ -210,11 +340,7 @@ void pmPrintObject(FILE *fp,pmObjectp p) { case PMobject_list: list = (struct pmList *)(p->body); if (list == NULL) break; - t = list; n = 0; - while (t != NULL) { - n++; - t = t->right; - } + n = pmListLength(list); t = list; fprintf(fp,"["); for (i=0; ibody; + fprintf(fp,"polymake.%s(",tree->nodeName); + /* Ignore attribute list */ + if (tree->childs == NULL) {n = 0; t = NULL; } + else { + t = tree->childs->body; + n = pmListLength(t); + } + for (i=0; ileft); + t = t->right; + if (i != n-1) fprintf(fp,","); + } + fprintf(fp,")"); + break; default: fprintf(stderr,"Unknown object tag %d.\n",p->tag); /* sleep(100); to call debugger. */ break; } -} - -main_t() { - int c,type; - putstr(-1); - while ((c=getchar()) != EOF) { - putstr(c); - } - S = putstr(0); - printf("%s\n",S); - pmPreprocess(S); - printf("--------------------------\n"); - printf("%s\n",S); - printf("--------------------------\n"); - while ((type=PMlex()) != PM_noToken) { - printf("type=%d ",type); - if ((type == PM_number) || (type == PM_keyword)) { - printf("value="); pmPrintObject(stdout,PMlval); - } - printf("\n"); - } -} - -main() { - int c,type; - - - putstr(-1); - while ((c=getchar()) != EOF) { - putstr(c); - } - S = putstr(0); - printf("%s\n",S); - pmPreprocess(S); - printf("--------------------------\n"); - printf("%s\n",S); - printf("--------------------------\n"); - PMparse(); } PMerror() {