Annotation of researchv10dc/cmd/icon/src/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 = strfree;
                     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 strfree;
                     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: #ifndef LATTICE
                    159:    static double tens[] = {
                    160:       1.0e0,  1.0e1,  1.0e2,  1.0e3,  1.0e4,  1.0e5,
                    161:       1.0e6,  1.0e7,  1.0e8,  1.0e9,  1.0e10, 1.0e11,
                    162:       1.0e12, 1.0e13, 1.0e14, 1.0e15, 1.0e16, 1.0e17,
                    163:       1.0e18, 1.0e19, 1.0e20, 1.0e21, 1.0e22, 1.0e23,
                    164:       1.0e24, 1.0e25, 1.0e26, 1.0e27, 1.0e28, 1.0e29,
                    165:       1.0e30, 1.0e31, 1.0e32, 1.0e33, 1.0e34, 1.0e35,
                    166:       1.0e36, 1.0e37, 1.0e38
                    167:       };
                    168:    static double ntens[] = {
                    169:       1.0e0,   1.0e-1,  1.0e-2,  1.0e-3,  1.0e-4,  1.0e-5,
                    170:       1.0e-6,  1.0e-7,  1.0e-8,  1.0e-9,  1.0e-10, 1.0e-11,
                    171:       1.0e-12, 1.0e-13, 1.0e-14, 1.0e-15, 1.0e-16, 1.0e-17,
                    172:       1.0e-18, 1.0e-19, 1.0e-20, 1.0e-21, 1.0e-22, 1.0e-23,
                    173:       1.0e-24, 1.0e-25, 1.0e-26, 1.0e-27, 1.0e-28, 1.0e-29,
                    174:       1.0e-30, 1.0e-31, 1.0e-32, 1.0e-33, 1.0e-34, 1.0e-35,
                    175:       1.0e-36, 1.0e-37, 1.0e-38
                    176:       };
                    177: 
                    178:    n = 0.0;
                    179:    d = e = 0;
                    180:    esign = 1;
                    181:    while ((c = getc(infile)) >= '0' && c <= '9')
                    182:       n = n * 10 + (c - '0');
                    183:    if (c == '.') {
                    184:       while ((c = getc(infile)) >= '0' && c <= '9') {
                    185:          n = n * 10 + (c - '0');
                    186:          d++;
                    187:          }
                    188:       }
                    189:    if (c == 'e' || c == 'E') {
                    190:       if ((c = getc(infile)) == '+' || c == '-') {
                    191:          if (c == '-')
                    192:             esign = -1;
                    193:          c = getc(infile);
                    194:          }
                    195:       while (c >= '0' && c <= '9') {
                    196:          e = e * 10 + (c - '0');
                    197:          c = getc(infile);
                    198:          }
                    199:       }
                    200:    if (esign < 0)
                    201:       e = -e;
                    202:    e = e - d;
                    203:    if (e >= -38 && e < 0)
                    204:       n = n * ntens[-e];
                    205:    else if (e > 0 && e <= 38)
                    206:       n = n * tens[e];
                    207:    nlflag = (c == '\n');
                    208: #else LATTICE
                    209:    fscanf(infile, "%lf", &n);
                    210:    c = getc(infile);
                    211: #endif LATTICE
                    212:    return n;
                    213:    }
                    214: 
                    215: /*
                    216:  * getlab - get a label ("L" followed by a number) from infile,
                    217:  *  and return the number.
                    218:  */
                    219: 
                    220: getlab()
                    221:    {
                    222:    register int c;
                    223: 
                    224:    while ((c = getc(infile)) != 'L' && c != EOF && c != '\n') ;
                    225:    if (c == 'L')
                    226:       return getdec();
                    227:    nlflag = (c == '\n');
                    228:    return 0;
                    229:    }
                    230: 
                    231: /*
                    232:  * getstrlit - get a string literal from infile, as a string
                    233:  *  of octal bytes, and return it.
                    234:  */
                    235: char *getstrlit(l)
                    236: register int l;
                    237:    {
                    238:    register char *p;
                    239:    extern char *putident();
                    240: 
                    241:    p = strfree;
                    242:    while (!nlflag && l--)
                    243:       *p++ = getoct();
                    244:    *p++ = 0;
                    245:    return putident((int)(p-strfree));
                    246:    }
                    247: 
                    248: /*
                    249:  * newline - skip to next line.
                    250:  */
                    251: newline()
                    252:    {
                    253:    register int c;
                    254: 
                    255:    if (!nlflag) {
                    256:       while ((c = getc(infile)) != '\n' && c != EOF) ;
                    257:       }
                    258:    nlflag = 0;
                    259:    }

unix.superglobalmegacorp.com

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