Annotation of 43BSD/contrib/icon/link/llex.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Lexical analysis routines.
                      3:  */
                      4: 
                      5: #include "ilink.h"
                      6: #include "opcode.h"
                      7: 
                      8: static int nlflag = 0;         /* newline last seen */
                      9: 
                     10: /*
                     11:  * getop - get an opcode from infile, return the opcode number (via
                     12:  *  binary search of opcode table), and point id at the name of the opcode.
                     13:  */
                     14: getop(id)
                     15: char **id;
                     16:    {
                     17:    register char *s;
                     18:    register struct opentry *p;
                     19:    register int test;
                     20:    int low, high, cmp;
                     21:    extern char *getstr();
                     22: 
                     23:    s = getstr();
                     24:    if (s == NULL)
                     25:       return (EOF);
                     26:    low = 0;
                     27:    high = NOPCODES;
                     28:    do {
                     29:       test = (low + high) / 2;
                     30:       p = &optable[test];
                     31:       if ((cmp = strcmp(p->op_name, s)) < 0)
                     32:          low = test + 1;
                     33:       else if (cmp > 0)
                     34:          high = test;
                     35:       else {
                     36:          *id = p->op_name;
                     37:          return (p->op_code);
                     38:          }
                     39:       } while (low < high);
                     40:    *id = s;
                     41:    return (NULL);
                     42:    }
                     43: 
                     44: /*
                     45:  * getid - get an identifier from infile, put it in the identifier
                     46:  *  table, and return a pointer to it.
                     47:  */
                     48: char *getid()
                     49:    {
                     50:    register char *s;
                     51:    extern char *getstr();
                     52:    extern char *putident();
                     53: 
                     54:    s = getstr();
                     55:    if (s == NULL)
                     56:       return (NULL);
                     57:    return (putident(strlen(s)+1));
                     58:    }
                     59: 
                     60: /*
                     61:  * getstr - get an identifier from infile and return a pointer to it.
                     62:  */
                     63: char *getstr()
                     64:    {
                     65:    register int c;
                     66:    register char *p;
                     67: 
                     68:    p = sfree;
                     69:    while ((c = getc(infile)) == ' ' || c == '\t') ;
                     70:    if (c == EOF)
                     71:       return (NULL);
                     72:    while (c != ' ' && c != '\t' && c != '\n' && c != ',' && c != EOF) {
                     73:       *p++ = c;
                     74:       c = getc(infile);
                     75:       }
                     76:    *p = 0;
                     77:    nlflag = (c == '\n');
                     78:    return (sfree);
                     79:    }
                     80: 
                     81: /*
                     82:  * getdec - get a decimal integer from infile, and return it.
                     83:  */
                     84: getdec()
                     85:    {
                     86:    register int c, n;
                     87: 
                     88:    n = 0;
                     89:    while ((c = getc(infile)) == ' ' || c == '\t') ;
                     90:    if (c == EOF)
                     91:       return (0);
                     92:    while (c >= '0' && c <= '9') {
                     93:       n = n * 10 + (c - '0');
                     94:       c = getc(infile);
                     95:       }
                     96:    nlflag = (c == '\n');
                     97:    return (n);
                     98:    }
                     99: 
                    100: /*
                    101:  * getoct - get an octal number from infile, and return it.
                    102:  */
                    103: getoct()
                    104:    {
                    105:    register int c, n;
                    106: 
                    107:    n = 0;
                    108:    while ((c = getc(infile)) == ' ' || c == '\t') ;
                    109:    if (c == EOF)
                    110:       return (0);
                    111:    while (c >= '0' && c <= '7') {
                    112:       n = (n << 3) | (c - '0');
                    113:       c = getc(infile);
                    114:       }
                    115:    nlflag = (c == '\n');
                    116:    return (n);
                    117:    }
                    118: 
                    119: /*
                    120:  * getint - get an Icon integer from infile, and return it.
                    121:  */
                    122: long getint()
                    123:    {
                    124:    register c;
                    125:    register int r;
                    126:    long int n;
                    127: 
                    128:    n = 0L;
                    129:    while ((c = getc(infile)) >= '0' && c <= '9')
                    130:       n = n * 10 + (c - '0');
                    131:    if (c == 'r' || c == 'R') {
                    132:       r = n;
                    133:       n = 0L;
                    134:       while (c = getc(infile)) {
                    135:          if (c >= '0' && c <= '9')
                    136:             c -= '0';
                    137:          else if (c >= 'a' && c <= 'z')
                    138:             c -= 'a' - 10;
                    139:          else if (c >= 'A' && c <= 'Z')
                    140:             c -= 'A' - 10;
                    141:          else
                    142:             break;
                    143:          n = n * r + c;
                    144:          }
                    145:       }
                    146:    nlflag = (c == '\n');
                    147:    return (n);
                    148:    }
                    149: 
                    150: /*
                    151:  * getreal - get an Icon real number from infile, and return it.
                    152:  */
                    153: double getreal()
                    154:    {
                    155:    double n;
                    156:    register int c, d, e;
                    157:    int esign;
                    158:    static double tens[] = {
                    159:       1.0e0,  1.0e1,  1.0e2,  1.0e3,  1.0e4,  1.0e5,
                    160:       1.0e6,  1.0e7,  1.0e8,  1.0e9,  1.0e10, 1.0e11,
                    161:       1.0e12, 1.0e13, 1.0e14, 1.0e15, 1.0e16, 1.0e17,
                    162:       1.0e18, 1.0e19, 1.0e20, 1.0e21, 1.0e22, 1.0e23,
                    163:       1.0e24, 1.0e25, 1.0e26, 1.0e27, 1.0e28, 1.0e29,
                    164:       1.0e30, 1.0e31, 1.0e32, 1.0e33, 1.0e34, 1.0e35,
                    165:       1.0e36, 1.0e37, 1.0e38
                    166:       };
                    167:    static double ntens[] = {
                    168:       1.0e0,   1.0e-1,  1.0e-2,  1.0e-3,  1.0e-4,  1.0e-5,
                    169:       1.0e-6,  1.0e-7,  1.0e-8,  1.0e-9,  1.0e-10, 1.0e-11,
                    170:       1.0e-12, 1.0e-13, 1.0e-14, 1.0e-15, 1.0e-16, 1.0e-17,
                    171:       1.0e-18, 1.0e-19, 1.0e-20, 1.0e-21, 1.0e-22, 1.0e-23,
                    172:       1.0e-24, 1.0e-25, 1.0e-26, 1.0e-27, 1.0e-28, 1.0e-29,
                    173:       1.0e-30, 1.0e-31, 1.0e-32, 1.0e-33, 1.0e-34, 1.0e-35,
                    174:       1.0e-36, 1.0e-37, 1.0e-38
                    175:       };
                    176: 
                    177:    n = 0.0;
                    178:    d = e = 0;
                    179:    esign = 1;
                    180:    while ((c = getc(infile)) >= '0' && c <= '9')
                    181:       n = n * 10 + (c - '0');
                    182:    if (c == '.') {
                    183:       while ((c = getc(infile)) >= '0' && c <= '9') {
                    184:          n = n * 10 + (c - '0');
                    185:          d++;
                    186:          }
                    187:       }
                    188:    if (c == 'e' || c == 'E') {
                    189:       if ((c = getc(infile)) == '+' || c == '-') {
                    190:          if (c == '-')
                    191:             esign = -1;
                    192:          c = getc(infile);
                    193:          }
                    194:       while (c >= '0' && c <= '9') {
                    195:          e = e * 10 + (c - '0');
                    196:          c = getc(infile);
                    197:          }
                    198:       }
                    199:    if (esign < 0)
                    200:       e = -e;
                    201:    e = e - d;
                    202:    if (e >= -38 && e < 0)
                    203:       n = n * ntens[-e];
                    204:    else if (e > 0 && e <= 38)
                    205:       n = n * tens[e];
                    206:    nlflag = (c == '\n');
                    207:    return (n);
                    208:    }
                    209: 
                    210: /*
                    211:  * getlab - get a label ("L" followed by a number) from infile,
                    212:  *  and return the number.
                    213:  */
                    214: 
                    215: getlab()
                    216:    {
                    217:    register int c;
                    218: 
                    219:    while ((c = getc(infile)) != 'L' && c != EOF && c != '\n') ;
                    220:    if (c == 'L')
                    221:       return (getdec());
                    222:    nlflag = (c == '\n');
                    223:    return (0);
                    224:    }
                    225: 
                    226: /*
                    227:  * getstrlit - get a string literal from infile, as a string
                    228:  *  of octal bytes, and return it.
                    229:  */
                    230: char *getstrlit(l)
                    231: register int l;
                    232:    {
                    233:    register char *p;
                    234:    extern char *putident();
                    235: 
                    236:    p = sfree;
                    237:    while (!nlflag && l--)
                    238:       *p++ = getoct();
                    239:    *p++ = 0;
                    240:    return (putident(p-sfree));
                    241:    }
                    242: 
                    243: /*
                    244:  * newline - skip to next line.
                    245:  */
                    246: newline()
                    247:    {
                    248:    register int c;
                    249: 
                    250:    if (!nlflag) {
                    251:       while ((c = getc(infile)) != '\n' && c != EOF) ;
                    252:       }
                    253:    nlflag = 0;
                    254:    }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.