home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_07 / 9n07067a < prev    next >
Text File  |  1991-05-07  |  5KB  |  200 lines

  1.  
  2. /*
  3. ** declare a static variable
  4. ** (Modified for 2-D arrays) - D. Lang 1/91
  5. */
  6. declglb(type, class) int type, class; {
  7. int k, j, dim2, ndim;
  8. char *ste; /* symbol table entry */
  9.   dim2 = 1;
  10.   ndim = 0;
  11.   while(1) {
  12.     if(match(";")) return;
  13.     if(match("*")) {
  14.       j=POINTER;
  15.       k=0;
  16.       }
  17.     else {
  18.       j=VARIABLE;
  19.       k=1;
  20.       }
  21.     if(symname(ssname, YES)==0) illname();
  22.     blanks();
  23.     if(streq(lptr, "(")) {
  24.       if(j==POINTER) error("pointer functions not allowed");
  25.    if(type==CCHAR) error("character functions not allowed");
  26.       if((class==STATIC)|(class==PUBLIC)) {
  27.         newfunc(class);
  28.         return;
  29.         }
  30.       else {
  31.         j=FUNCTION;
  32.         if(match("()") == 0) error("missing closing paren");
  33.         }
  34.       }
  35.     if(findglb(ssname)) multidef(ssname);
  36.     if(j!=FUNCTION) {
  37.       if(match("[")) {
  38.     k=needsub();  /* get size                         */
  39.     j=ARRAY;      /* !0=array                         */
  40.     ndim = 1;
  41.     if(match("[")) {
  42.       dim2 = needsub();
  43.       ndim = 2;
  44.       if(dim2 == 0) {
  45.          error("need array size");
  46.          k = dim2 = 1;
  47.       } /* if dim2 */
  48.     } /* if match */
  49.       } /* if match */
  50.     } /* if j!= */
  51.  if(class!=EXTERNAL) j=initials(type>>2, j, k, class, dim2);
  52.  ste=addsym(ssname, j, type, k, &glbptr, class, dim2, ndim);
  53.     if(ste) ste[STATUS] |= DECLARED;
  54.     if(match(",")==0) {ns(); return;}  /* more? */
  55.     }
  56.   }
  57.  
  58. /*
  59. ** declare local variables
  60. ** (Modified for 2-D arrays) - D. Lang 1/91
  61. */
  62. declloc(typ) int typ; {
  63.   int k, j, dim2, ndim;
  64. #ifdef STGOTO
  65.   if(noloc) error("not allowed with goto");
  66. #endif
  67.   if(declared < 0)  error("must declare first in block");
  68.   while(1) {
  69.     while(1) {
  70.       if(endst()) return;
  71.       if(match("*")) j=POINTER;
  72.       else j=VARIABLE;
  73.       if(symname(ssname, YES)==0) illname();
  74.       /* no multidef check, block-locals are together     */
  75.       k=BPW;
  76.       if (match("[")) {
  77.     ndim = 1;
  78.         k=needsub();
  79.         if(k) {
  80.           j=ARRAY;
  81.       if(typ == CINT) k = k << LBPW;
  82.       if(match("[")) {
  83.           dim2 = needsub();
  84.           k = k * dim2;
  85.           ndim = 2;
  86.       }
  87.     }
  88.         else {
  89.           j = POINTER;
  90.           k = BPW;
  91.       if(match("[")) {
  92.           dim2 = needsub();
  93.           while(inbyte() != ']') if(endst()) break;
  94.           ndim = 2;
  95.        }
  96.       }
  97.         }
  98.       else if(match("()")) j=FUNCTION;
  99.       else if((typ==CCHAR)&(j==VARIABLE)) k=SBPC;
  100.       declared = declared + k;
  101.       addsym(ssname, j, typ, csp - declared, &locptr,
  102.                      AUTOMATIC, dim2, ndim);
  103.       break;
  104.       }
  105.     if (match(",")==0) return;
  106.     }
  107.   }
  108.  
  109. /*
  110. ** initalize global objects
  111. ** (Modified for 2-D arrays) - D. Lang 1/91
  112. */
  113. initials(size, ident, dim, class, dim2) int size, ident,
  114.                      dim, class, dim2; {
  115.   int savedim, savectxt, dimsz, sflag, otemp;
  116.   savectxt = ctext;
  117.   sflag = -1;
  118.   ctext = 0; /* turn off interleaved source */
  119.   litptr=0;
  120.   if(dim==0) dim = -1;
  121.   dimsz = dim * dim2;
  122.   savedim=dim;
  123.   if(class==PUBLIC) entry();
  124.   startglob();
  125.   if(match("=")) {
  126.     if(match("{")) {
  127.       while(dimsz) {
  128.     otemp = dimsz;
  129.     init(size, ident, &dimsz);
  130.     if(match(",")==0) break;
  131.     if(otemp != dimsz) sflag = 1;
  132.         }
  133.       needtoken("}");
  134.       }
  135.     else {
  136.       otemp = dimsz;
  137.       init(size, ident, &dimsz);
  138.       if (otemp != dimsz) sflag = 1;
  139.       }
  140.     }
  141.   if((savedim == -1) & (sflag == -1)) {
  142.     stowlit(0, size=BPW);
  143.     ident=POINTER;
  144.     }
  145.   dumplits(size);
  146.   dumpzero(size, dimsz);
  147.   ctext = savectxt; /* restore source code interleave mode*/
  148.   return ident;
  149.   }
  150.  
  151. /*
  152. ** declare argument types
  153. **
  154. ** called from "newfunc" this routine adds an entry in the
  155. ** local symbol table for each named argument
  156. **
  157. ** rewritten per P. L. Woods (DDJ #52)
  158. ** (Modified for 2-D arrays) - D. Lang 1/91
  159. */
  160. doargs(t) int t; {
  161.   int j, legalname, dim2, ndim;
  162.   char c, *argptr;
  163.   while(1) {
  164.     if(argstk==0) return;      /* no arguments            */
  165.     if(match("*")) j=POINTER; else j=VARIABLE;
  166.     if((legalname=symname(ssname, YES))==0) illname();
  167.     if(match("[")) {   /* is it a pointer ?               */
  168.       ndim = 1;
  169.       /* yes, so skip stuff between "[...]"               */
  170.       while(inbyte()!=']') if(endst()) break;
  171.                else if(streq(lptr,"[")) break;
  172.       j=POINTER;      /* add entry as pointer             */
  173.       if(match("[")) {
  174.     dim2 = needsub();
  175.     ndim = 2;
  176.       }
  177.     }
  178.     if(legalname) {
  179.       if(argptr=findloc(ssname)) {
  180.         if(argptr[TYPE]==NULL) {
  181.       /* add details of type and address              */
  182.           argptr[IDENT]=j;
  183.           argptr[TYPE]=t;
  184.       putint((((argtop-getint(argptr+OFFSET, OFFSIZE))
  185.             *BPW)+STKFRSZ), argptr+OFFSET, OFFSIZE);
  186.       argptr[NDIM] = ndim;
  187.       putint(dim2, argptr + CDIM, 2);
  188.           }
  189.         else error("duplicate argument declaration");
  190.         }
  191.       else error("not an argument");
  192.       }
  193.     --argstk; /* count down */
  194.     if(endst())return;
  195.     if(match(",")==0) error(" no comma");
  196.     }
  197.   }
  198.  
  199.  
  200.