home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / ada / adaed-1.11 / adaed-1 / Adaed-1.11.0a / prsutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-07  |  9.5 KB  |  397 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. #include "ada.h"
  10. #include "stdlib.h"
  11. #include "adalexprots.h"
  12. #include "miscprots.h"
  13. #include "prsutilprots.h"
  14.  
  15. int    stack_count(struct prsstack *ps)                        /*;stack_count*/
  16. {
  17.     int size = 0;
  18.  
  19.     while( ps != (struct prsstack *)0) {
  20.         ps = ps->prev;
  21.         size++;
  22.     }
  23.  
  24.     return (size);
  25. }
  26.  
  27. void copystack(struct two_pool *s, struct two_pool **head, int *size)
  28.                                                             /*;copystack*/
  29. {
  30.     /*    This procedure copies the stack s represented by a singly linked list
  31.      *  It returns a pointer to the head (top) of the new stack, as well as a
  32.      *  count of the size of the stack, returned in size
  33.      *  Note : lists in the front-end are circular, so the link of the last node
  34.      *       points to the head of the list.
  35.      */
  36.  
  37.     struct two_pool *next,
  38.     *tmp_tp;
  39.  
  40.     *size = 1;
  41.     /*    Copy head node     */
  42.     tmp_tp = *head = TALLOC ();
  43.     tmp_tp -> link = NULL;
  44.     tmp_tp -> val.state = s -> val.state;
  45.     /*  Copy rest of list     */
  46.     next = s -> link;
  47.     while (next != NULL) {
  48.         tmp_tp -> link = TALLOC ();
  49.         tmp_tp = tmp_tp -> link;
  50.         tmp_tp -> link = NULL;
  51.         tmp_tp -> val.state = next -> val.state;
  52.         next = next -> link;
  53.         (*size)++;
  54.     }
  55. }
  56.  
  57. void dump_stack(struct two_pool *s)                        /*;dump_stack*/
  58. {
  59.     int        count = 0;
  60. #ifdef DEBUG
  61.     if (trcopt) {
  62.         while (s != (struct two_pool *)0) {
  63.             count++;
  64.             fprintf (errfile, "%d %s", s -> val.state,
  65.               ( ((count%10) == 0) ? "\n":",") );
  66.             s = s -> link;
  67.         }
  68.         fprintf (errfile, " ======>size = %d<====== \n",count);
  69.     }
  70. #endif
  71. }
  72.  
  73. void dump_prssyms(struct two_pool *s)                    /*;dump_prssyms*/
  74. {
  75.     int        count = 0;
  76. #ifdef DEBUG
  77.     if (trcopt) {
  78.         while (s != (struct two_pool *)0) {
  79.             count++;
  80.             fprintf (errfile, "%s %s", TOKSTR(s -> val.state),
  81.               ( ((count%5) == 0) ? "\n":",") );
  82.             s = s -> link;
  83.         }
  84.         fprintf (errfile, " ======>size = %d<====== \n",count);
  85.     }
  86. #endif
  87. }
  88.  
  89. void dump_prsstack(struct prsstack *p)                    /*;dump_prsstack*/ 
  90. {
  91.     /* dump symbols of parse stack */
  92.     int count = 0;
  93. #ifdef DEBUG
  94.     if (trcopt) {
  95.         while (p!=(struct prsstack *)0) {
  96.             count++;
  97.             fprintf(errfile,"%s %c",TOKSTR(p->symbol),
  98.               ( (count%5 == 0) ? '\n' : ' ') );
  99.             p = p->prev;
  100.         }
  101.         fprintf(errfile,"     size = %d\n",count);
  102.     }
  103. #endif
  104. }
  105.  
  106. void dump_toksyms(char *s)                            /*;dump_toksyms*/
  107. {
  108.     int        t;
  109.  
  110. #ifdef DEBUG
  111.     if (trcopt) {
  112.         fprintf (errfile, "Dump of TOKSYMS [%s]\n", s);
  113.         for (t = n_TOKSYMS; t >= 0; t--) {
  114.             fprintf (errfile, "%s%s", TOKSTR(TOKSYMS[t]),
  115.               ((t % 15) == 14 ? "\n" : " "));
  116.         }
  117.         fprintf (errfile, "\n    < ===== >\n");
  118.     }
  119. #endif
  120. }
  121.  
  122.  
  123. void convtolower(char *s)                                /*;convtolower*/
  124. {
  125.     /* Convtolower: convert an entire string to lowercase. */
  126.     while (*s) {
  127.         if (isupper(*s))
  128.             *s = tolower(*s);
  129.         s++;
  130.     }
  131. }
  132.  
  133. void cand_clear()                                        /*;cand_clear*/
  134. {
  135.     /* cand_clear :     clear (reinitialize) all candidate sets to empty */
  136.     short x;
  137.  
  138.     for (x = DELETE; x <= SPELL_SUBST; x++) {
  139.         /* TBSL: free current candidates */
  140.         CANDIDATES[x] = NULL;
  141.         n_CANDIDATES[x] = 0;
  142.     }
  143. }
  144.  
  145. void dump_cand()                                        /*;dump_cand*/
  146. {
  147.     /* dump_cand :    dump contents of the candidates map to errfile */
  148.     short x,i;
  149.     struct cand *c;
  150.     char *sub;
  151.  
  152. #ifdef DEBUG
  153.     if (trcopt) {
  154.         for (x = DELETE; x <= SPELL_SUBST; x++) {
  155.             switch (x) {
  156.             case DELETE:
  157.             case MERGE:
  158.                 if (n_CANDIDATES[x] == 0)
  159.                     break;
  160.                 fprintf(errfile,"%s :\n",(x == DELETE ? "delete" : "merge"));
  161.                 c = CANDIDATES[x];
  162.                 i=0;
  163.                 while (c != (struct cand *)0) {
  164.                     fprintf(errfile,"[ %s %d ] %c",TOKSTR(c->token_sym),
  165.                       c->backup_toks+1,((++i % 5) == 0 ? '\n' : ' ') );
  166.                     c = c->next;
  167.                 }  /* end while */
  168.                 if (i%5 != 0) fprintf(errfile,"\n");
  169.                 break;
  170.             case INSERT:
  171.             case SUBST:
  172.             case SPELL_SUBST:
  173.                 if (n_CANDIDATES[x] == 0)
  174.                     break;
  175.                 if (x == INSERT) sub = "insert";
  176.                 else if (x == SUBST) sub = "subst";
  177.                 else sub = "spell_subst";
  178.  
  179.                 fprintf(errfile,"%s :\n",sub);
  180.                 c = CANDIDATES[x];
  181.                 i=0;
  182.                 while (c != (struct cand *)0) {
  183.                     fprintf(errfile,"[ %s %s %d ] %c",TOKSTR(c->token_sym),
  184.                       TOKSTR(c->t3),
  185.                       c->backup_toks+1,((++i % 4) == 0 ? '\n' : ' ') );
  186.                     c = c->next;
  187.                 }
  188.                 if (i%4 != 0) fprintf(errfile,"\n");
  189.                 break;
  190.             } /* end switch */
  191.         } /* end for */
  192.     }
  193. #endif
  194. }
  195.  
  196. struct prsstack *copytoken(struct prsstack *b)                /*;copytoken*/
  197. {
  198.     struct prsstack *a;
  199.     char *oldp, *newp;
  200.     int i;
  201.  
  202.     /* First get space for a */
  203.     a = PRSALLOC();
  204.     a->symbol = b->symbol;
  205.     a->prev = NULL;
  206.     if ( ISTOKEN(b) ) { /* copy token structure */
  207.  
  208.         a->ptr.token = TOKALLOC();
  209.         a->ptr.token->index = b->ptr.token->index;
  210.         a->ptr.token->loc = b->ptr.token->loc;
  211.     }
  212.     else {  /* copy ast structure */
  213.  
  214.         a->ptr.ast = new_node(b->ptr.ast->kind);
  215.         oldp = (char *)(b->ptr.ast); 
  216.         newp = (char *)(a->ptr.ast);
  217.         for (i = 0; i < sizeof(struct ast); i++)
  218.             *newp++ = *oldp++;
  219.     }
  220.     /* end of macro definition */
  221.     return a;
  222. }
  223.  
  224. /* start allocation routines */
  225.  
  226. /* This file contains the allocation and freeing routines for various
  227.    structures */
  228. /* replace EMalloc by emalloc (misc.c)
  229.  * char *EMalloc(n)
  230.  */
  231.  
  232.  
  233. static struct prsstack *deadprsstack = (struct prsstack *)0;
  234. static void prsfree1(struct prsstack *, struct prsstack *);
  235. static void prsfree2(struct prsstack *, struct prsstack *);
  236. static struct prsstack * prsfakealloc();
  237. struct prsstack *(*prsalloc)(unsigned) =(struct prsstack *(*)(unsigned))emalloc;
  238. void (*prsfree)(struct prsstack *, struct prsstack *) = prsfree1;
  239.  
  240. static struct prsstack *prsfakealloc()                    /*;prsfakealloc*/
  241. {
  242.     struct prsstack *tmp;
  243.  
  244.     if (deadprsstack != (struct prsstack *)0) {
  245.         tmp = deadprsstack;
  246.         deadprsstack = deadprsstack->prev;
  247.         return(tmp);
  248.     }
  249.     prsalloc = (struct prsstack *(*)(unsigned)) emalloc;
  250.     prsfree = prsfree1;
  251.     return((struct prsstack *)emalloc(sizeof(struct prsstack)));
  252. }
  253.  
  254. static void prsfree1(struct prsstack *p, struct prsstack *q)    /*;prsfree1*/
  255. {
  256.     prsfree = prsfree2;
  257.     prsalloc = (struct prsstack *(*)(unsigned)) prsfakealloc;
  258.     q->prev = deadprsstack;
  259.     deadprsstack = p;
  260. }
  261.  
  262. static void prsfree2(struct prsstack *p, struct prsstack *q)    /*;prsfree2*/
  263. {
  264.     q->prev = deadprsstack;
  265.     deadprsstack = p;
  266. }
  267.  
  268. static struct two_pool *deadtwostack = (struct two_pool *)0;
  269. static void tfree1(struct two_pool *, struct two_pool *);
  270. static void tfree2(struct two_pool *, struct two_pool *);
  271. static struct two_pool *tfakealloc();
  272. struct two_pool *(*talloc)(unsigned) = (struct two_pool *(*)(unsigned)) emalloc;
  273. void (*tfree)(struct two_pool *, struct two_pool *) = tfree1;
  274.  
  275. static struct two_pool *tfakealloc()
  276. {
  277.     struct two_pool *tmp;
  278.  
  279.     if (deadtwostack != (struct two_pool *)0) {
  280.         tmp = deadtwostack;
  281.         deadtwostack = deadtwostack->link;
  282.         return(tmp);
  283.     }
  284.     talloc = (struct two_pool *(*)(unsigned)) emalloc;
  285.     tfree = tfree1;
  286.     return((struct two_pool *)emalloc(sizeof(struct two_pool)));
  287. }
  288.  
  289. static void tfree1(struct two_pool *p, struct two_pool *q)        /*;tfree1*/
  290. {
  291.     tfree = tfree2;
  292.     talloc = (struct two_pool *(*)(unsigned)) tfakealloc;
  293.     q->link = deadtwostack;
  294.     deadtwostack = p;
  295. }
  296.  
  297. static void tfree2(struct two_pool *p, struct two_pool *q)        /*;tfree2*/
  298. {
  299.     q->link = deadtwostack;
  300.     deadtwostack = p;
  301. }
  302.  
  303. static struct token *deadtokstack = (struct token *)0;
  304. static void tokfree1(struct token *);                        /*;tokfree1*/
  305. static void tokfree2(struct token *);                        /*;tokfree1*/
  306. static struct token *tokfakealloc();
  307. struct token *(*tokalloc)(unsigned) = (struct token *(*)(unsigned)) emalloc;
  308. void (*tokfree)(struct token *) = tokfree1;
  309.  
  310. static struct token *tokfakealloc()                        /*;tokfakealloc*/
  311. {
  312.     struct token *tmp;
  313.  
  314.     if (deadtokstack == (struct token *)0) {
  315.         tokalloc = (struct token *(*)(unsigned)) emalloc;
  316.         tokfree = tokfree1;
  317.         return((struct token *)emalloc(sizeof(struct token)));
  318.     }
  319.     tmp = deadtokstack;
  320.     deadtokstack = ((struct tmptok *)deadtokstack)->link;
  321.     return(tmp);
  322. }
  323.  
  324. static void tokfree1(struct token *p)                        /*;tokfree1*/
  325. {
  326.     ((struct tmptok *)p)->link = deadtokstack;
  327.     deadtokstack = p;
  328.     tokalloc = (struct token *(*)(unsigned)) tokfakealloc;
  329.     tokfree = tokfree2;
  330. }
  331.  
  332. static void tokfree2(struct token *p)                        /*;tokfree2*/
  333. {
  334.     ((struct tmptok *)p)->link = deadtokstack;
  335.     deadtokstack = p;
  336. }
  337.  
  338. static struct ast *deadnodestack = (struct ast *)0;
  339.  
  340. struct ast *new_node(int kind)                                /*;new_node*/
  341. {
  342.     struct ast *node;
  343.  
  344.     if (deadnodestack == (struct ast *) 0)
  345.         node = (struct ast *)emalloc(sizeof(struct ast));
  346.     else {
  347.         node = deadnodestack;
  348.         deadnodestack = ((struct tmpnode *)deadnodestack)->link;
  349.     }
  350.     node->kind = kind;
  351.     node->count = 0;  /* This is used to check if a count has been set */
  352. #ifdef DEBUG
  353.     /* initialize spans information to 0 (may not be necessary!!) */
  354.     node->span.line = node->span.col = 0;
  355. #endif
  356.     return(node);
  357. }
  358.  
  359. void nodefree(struct ast *p)                                /*;nodefree*/
  360. {
  361.     if (p != opt_node && p != any_node && p->kind != AS_FREE) {
  362.         ((struct tmpnode *)p)->link = deadnodestack;
  363.         ((struct tmpnode *)p)->kind = AS_FREE;
  364.         deadnodestack = p;
  365.     }
  366. }
  367.  
  368. static struct ast **deadaststack = (struct ast **)0;
  369.  
  370. struct ast **astalloc()                                            /*;astalloc*/
  371. {
  372.     struct ast **tmp;
  373.  
  374.     if (deadaststack == (struct ast **)0)
  375.         return((struct ast **)emalloc(MAX_AST * sizeof(struct ast *)));
  376.     tmp = deadaststack;
  377.     deadaststack = ((struct tmpast *)deadaststack)->link;
  378.     return(tmp);
  379. }
  380.  
  381. void astfree(struct ast **p)                                    /*;astfree*/
  382. {
  383.     ((struct tmpast *)p)->link = deadaststack;
  384.     deadaststack = p;
  385. }
  386.  
  387. /* Cand stuff is for error recovery */
  388.  
  389. struct cand *(*candalloc)(unsigned) = (struct cand *(*)(unsigned)) emalloc;
  390.  
  391. char *find_name(struct prsstack *x)                                /*;find_name*/
  392. {
  393.     if  (ISTOKEN(x)) return TOKSTR(x->ptr.token->index);
  394.     return namelist(x->symbol);
  395. }
  396.  
  397.