rename vs to bt for basic type
[neatcc/cc.git] / tok.c
blobebd959f6524ab2c18c2dc792bfeda8583bf11360
1 #include <ctype.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include "tok.h"
6 static char buf[BUFSIZE];
7 static int len;
8 static int cur;
9 static char name[NAMELEN];
10 static int next;
12 static struct {
13 char *name;
14 unsigned id;
15 } kwds[] = {
16 {"void", TOK_VOID},
17 {"static", TOK_STATIC},
18 {"return", TOK_RETURN},
19 {"unsigned", TOK_UNSIGNED},
20 {"signed", TOK_SIGNED},
21 {"short", TOK_SHORT},
22 {"long", TOK_LONG},
23 {"int", TOK_INT},
24 {"char", TOK_CHAR},
25 {"struct", TOK_STRUCT},
26 {"enum", TOK_ENUM},
27 {"if", TOK_IF},
28 {"else", TOK_ELSE},
29 {"for", TOK_FOR},
30 {"while", TOK_WHILE},
31 {"do", TOK_DO},
32 {"switch", TOK_SWITCH},
33 {"case", TOK_CASE},
34 {"sizeof", TOK_SIZEOF},
37 static int id_char(int c)
39 return isalnum(c) || c == '_';
42 int tok_get(void)
44 if (next != -1) {
45 int tok = next;
46 next = -1;
47 return tok;
49 while (cur < len && isspace(buf[cur]))
50 cur++;
51 if (cur == len)
52 return TOK_EOF;
53 if (isdigit(buf[cur])) {
54 char *s = name;
55 while (cur < len && isdigit(buf[cur]))
56 *s++ = buf[cur++];
57 *s = '\0';
58 return TOK_NUM;
60 if (id_char(buf[cur])) {
61 char *s = name;
62 int i;
63 while (cur < len && id_char(buf[cur]))
64 *s++ = buf[cur++];
65 *s = '\0';
66 for (i = 0; i < ARRAY_SIZE(kwds); i++)
67 if (!strcmp(kwds[i].name, name))
68 return kwds[i].id;
69 return TOK_NAME;
71 if (strchr(";,{}()[]*=+-/", buf[cur]))
72 return buf[cur++];
73 return -1;
76 int tok_see(void)
78 if (next == -1)
79 next = tok_get();
80 return next;
83 void tok_init(int fd)
85 int n = 0;
86 while ((n = read(fd, buf + len, sizeof(buf) - len)) > 0)
87 len += n;
88 next = -1;
91 char *tok_id(void)
93 return name;