home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / misc / smc203.ark / CC12.C < prev    next >
Encoding:
Text File  |  1983-07-28  |  8.0 KB  |  317 lines

  1.  
  2. /*
  3. ** open an include file
  4. */
  5. doinclude()  {
  6.   char c, *fname, buff[15];
  7.   int i;
  8.   /*
  9.    * test for nested includes added 4/7/83 br.
  10.    * (this may be buried somewhere else, but I don't see it)
  11.    */
  12.   if(input2 != EOF) {
  13.     error("nested include files not allowed");
  14.     kill();     /* ignore rest of line */
  15.     return;
  16.     }
  17.   blanks();       /* skip over to name */
  18.   /*
  19.    * added code to handle include filename in quotes or brackets
  20.    * 4/5/83 br
  21.    */
  22.   if ((*lptr == '"') | (*lptr == '<')) {
  23.     i = 0;
  24.     fname = buff;
  25.     while (i<14) {
  26.       c = *++lptr;
  27.       if ((c == '"') | (c == '>'))
  28.         i = 14; /* force exit from loop */
  29.       else
  30.         *fname++ = c;
  31.       }
  32.     *fname = '\0';
  33.     fname = buff;
  34.     }
  35.   else
  36.     fname = lptr;       /* no '"' or '<' (original convention) */
  37.   if((input2=fopen(fname,"r"))==NULL) {
  38.     input2= EOF;
  39.     error("open failure on include file");
  40.     }
  41.   kill();         /* clear rest of line */
  42.       /* so next read will come from */
  43.       /* new file (if open) */
  44.   }
  45.  
  46. /*
  47. ** test for global declarations
  48. */
  49. dodeclare(class) int class; {
  50.   if(amatch("char",4)) {
  51.     declglb(CCHAR, class);
  52.     ns();
  53.     return 1;
  54.     }
  55.   else if((amatch("int",3))|(class==EXTERNAL)) {
  56.     declglb(CINT, class);
  57.     ns();
  58.     return 1;
  59.     }
  60.   return 0;
  61.   }
  62.  
  63. /*
  64. ** declare a static variable
  65. */
  66. declglb(type, class)  int type, class; {
  67.   int k, j;
  68.   while(1) {
  69.     if(endst()) return;     /* do line */
  70.     if(match("*")) {
  71.       j=POINTER;
  72.       k=0;
  73.       }
  74.     else {
  75.       j=VARIABLE;
  76.       k=1;
  77.       }
  78.     if (symname(ssname, YES)==0) illname();
  79.     if(findglb(ssname)) multidef(ssname);
  80.     if(match("()")) j=FUNCTION;
  81.     else if (match("[")) {
  82.       k=needsub();    /* get size */
  83.       /*
  84.        * check for `*var[nn]'  4/7/83 br
  85.        */
  86.       if (j==POINTER)
  87.         error("declaration type not allowed");
  88.       j=ARRAY;   /* !0=array */ /* I don't understand this comment. br */
  89.       }
  90.     if(class==EXTERNAL) external(ssname);
  91.     else j=initials(type>>2, j, k);
  92.     addsym(ssname, j, type, k, &glbptr, class);
  93.     if (match(",")==0) return; /* more? */
  94.     }
  95.   }
  96.  
  97. /*
  98. ** declare local variables
  99. */
  100. declloc(typ)  int typ;  {
  101.   int k,j;
  102.  
  103. #ifdef STGOTO
  104.   if(noloc) error("not allowed with goto");
  105. #endif /* STGOTO */
  106.  
  107.   if(declared < 0) error("must declare first in block");
  108.   while(1) {
  109.     while(1) {
  110.       if(endst()) return;
  111.       if(match("*")) j=POINTER;
  112.       else j=VARIABLE;
  113.       if (symname(ssname, YES)==0) illname();
  114.       /* no multidef check, block-locals are together */
  115.       k=BPW;
  116.       if (match("[")) {
  117.         k=needsub();
  118.         if(k) {
  119.           /*
  120.            * check for `*var[nn]'  4/7/83 br
  121.            */
  122.           if (j==POINTER)
  123.             error("declaration type not allowed");
  124.           j=ARRAY;
  125.           if(typ==CINT)k=k<<LBPW;
  126.           }
  127.         else j=POINTER;
  128.         }
  129.       else if(match("()")) j=FUNCTION;
  130.       else if((typ==CCHAR)&(j==VARIABLE)) k=SBPC;
  131.       declared = declared + k;
  132.       addsym(ssname, j, typ, csp - declared, &locptr, AUTOMATIC);
  133.       break;
  134.       }
  135.     if (match(",")==0) return;
  136.     }
  137.   }
  138.  
  139. /*
  140. ** initialize global objects
  141. */
  142. initials(size, ident, dim) int size, ident, dim; {
  143.   int savedim;
  144.   litptr=0;
  145.   if(dim==0) dim = -1;
  146.   savedim=dim;
  147.   entry();
  148.   if(match("=")) {
  149.     if(match("{")) {
  150.       while(dim) {
  151.         init(size, ident, &dim);
  152.         if(match(",")==0) break;
  153.         }
  154.       needtoken("}");
  155.       }
  156.     else init(size, ident, &dim);
  157.     }
  158.   if((dim == -1)&(dim==savedim)) {
  159.      stowlit(0, size=BPW);
  160.     ident=POINTER;
  161.     }
  162.   dumplits(size);
  163.   dumpzero(size, dim);
  164.   return ident;
  165.   }
  166.  
  167. /*
  168. ** evaluate one initializer
  169. */
  170. init(size, ident, dim) int size, ident, *dim; {
  171.   int value;
  172.   if(qstr(&value)) {
  173.     if((ident==VARIABLE)|(size!=1))
  174.       error("must assign to char pointer or array");
  175.     *dim = *dim - (litptr - value);
  176.     if(ident==POINTER) point();
  177.     }
  178.   else if(constexpr(&value)) {
  179.     if(ident==POINTER) error("cannot assign to pointer");
  180.     stowlit(value, size);
  181.     *dim = *dim - 1;
  182.     }
  183.   }
  184.  
  185. /*
  186. ** get required array size
  187. */
  188. needsub()  {
  189.   int val;
  190.   if(match("]")) return 0; /* null size */
  191.   if (constexpr(&val)==0) val=1;
  192.   if (val<0) {
  193.     error("negative size illegal");
  194.     val = -val;
  195.     }
  196.   needtoken("]");      /* force single dimension */
  197.   return val;          /* and return size */
  198.   }
  199.  
  200. /*
  201. ** begin a function
  202. **
  203. ** called from "parse" and tries to make a function
  204. ** out of the following text
  205. **
  206. ** Patched per P.L. Woods (DDJ #52)
  207. */
  208. newfunc()  {
  209.   char *ptr;
  210.  
  211. #ifdef STGOTO
  212.   nogo  =             /* enable goto statements */
  213.   noloc = 0;          /* enable block-local declarations */
  214. #endif /* STGOTO */
  215.  
  216.   lastst=             /* no statement yet */
  217.   litptr=0;           /* clear lit pool */
  218.   litlab=getlabel();  /* label next lit pool */
  219.   locptr=STARTLOC;    /* clear local variables */
  220.   if(monitor) lout(line, stderr);
  221.   if (symname(ssname, YES)==0) {
  222.     error("illegal function or declaration");
  223.     kill(); /* invalidate line */
  224.     return;
  225.     }
  226.   if(func1) {
  227.     postlabel(beglab);
  228.     func1=0;
  229.     }
  230.   if(ptr=findglb(ssname)) {      /* already in symbol table ? */
  231.     if(ptr[IDENT]!=FUNCTION)       multidef(ssname);
  232.     else if(ptr[OFFSET]==FUNCTION) multidef(ssname);
  233.     else ptr[OFFSET]=FUNCTION;
  234.       /*  earlier assumed to be a function */
  235.     }
  236.   else
  237.     addsym(ssname, FUNCTION, CINT, FUNCTION, &glbptr, STATIC);
  238.   if(match("(")==0) error("no open paren");
  239.   entry();
  240.   locptr=STARTLOC;
  241.   argstk=0;               /* init arg count */
  242.   while(match(")")==0) {  /* then count args */
  243.     /* any legal name bumps arg count */
  244.     if(symname(ssname, YES)) {
  245.       if(findloc(ssname)) multidef(ssname);
  246.       else {
  247.         addsym(ssname, 0, 0, argstk, &locptr, AUTOMATIC);
  248.         argstk=argstk+BPW;
  249.         }
  250.       }
  251.     else {error("illegal argument name");junk();}
  252.     blanks();
  253.     /* if not closing paren, should be comma */
  254.     if(streq(lptr,")")==0) {
  255.       if(match(",")==0) error("no comma");
  256.       }
  257.     if(endst()) break;
  258.     }
  259.   csp=0;        /* preset stack ptr */
  260.   argtop=argstk;
  261.   while(argstk) {
  262.     /* now let user declare what types of things */
  263.     /*      those arguments were */
  264.     if(amatch("char",4))     {doargs(CCHAR);ns();}
  265.     else if(amatch("int",3)) {doargs(CINT);ns();}
  266.     else {error("wrong number of arguments");break;}
  267.     }
  268.   if(statement()!=STRETURN) zzret();
  269.   if(litptr) {
  270.     printlabel(litlab);
  271.     col();
  272.     dumplits(1); /* dump literals */
  273.     }
  274.   }
  275.  
  276. /*
  277. ** declare argument types
  278. **
  279. ** called from "newfunc" this routine adds an entry in the
  280. ** local symbol table for each named argument
  281. **
  282. ** rewritten per P.L. Woods (DDJ #52)
  283. */
  284. doargs(t) int t; {
  285.   int j, legalname;
  286.   char c, *argptr;
  287.   while(1) {
  288.     if(argstk==0) return; /* no arguments */
  289.     if(match("*")) j=POINTER;
  290.     else j=VARIABLE;
  291.     if((legalname=symname(ssname, YES))==0) illname();
  292.     if(match("[")) {   /* is it a pointer? */
  293.       /* yes, so skip stuff between "[...]" */
  294.       while(inbyte()!=']') if(endst()) break;
  295.       /*
  296.        * check for `*var[nn]'  4/7/83 br
  297.        */
  298.       if (j==POINTER)
  299.         error("declaration type not allowed");
  300.       j=POINTER; /* add entry as pointer */
  301.       }
  302.     if(legalname) {
  303.       if(argptr=findloc(ssname)) {
  304.         /* add details of type and address */
  305.         argptr[IDENT]=j;
  306.         argptr[TYPE]=t;
  307.         putint(argtop-getint(argptr+OFFSET, OFFSIZE), argptr+OFFSET, OFFSIZE);
  308.         }
  309.       else error("not an argument");
  310.       }
  311.     argstk=argstk-BPW;        /* cnt down */
  312.     if(endst())return;
  313.     if(match(",")==0) error("no comma");
  314.     }
  315.   }
  316. 
  317. σσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσσ