support |, & and ^ operators
[neatcc.git] / tok.c
blobd562cc92a53c0cda4ef114fb7f6a7a9772134b87
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 char *tok3[] = {
38 "<<", ">>", "++", "--", "<<=", ">>=", "...", "+=", "-=", "*=", "/=",
39 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">="
42 static int get_tok3(int num)
44 int i;
45 for (i = 0; i < ARRAY_SIZE(tok3); i++)
46 if (num == TOK3(tok3[i]))
47 return num;
48 return 0;
51 static int id_char(int c)
53 return isalnum(c) || c == '_';
56 int tok_get(void)
58 int num;
59 if (next != -1) {
60 int tok = next;
61 next = -1;
62 return tok;
64 while (cur < len && isspace(buf[cur]))
65 cur++;
66 if (cur == len)
67 return TOK_EOF;
68 if (isdigit(buf[cur])) {
69 char *s = name;
70 while (cur < len && isdigit(buf[cur]))
71 *s++ = buf[cur++];
72 *s = '\0';
73 return TOK_NUM;
75 if (id_char(buf[cur])) {
76 char *s = name;
77 int i;
78 while (cur < len && id_char(buf[cur]))
79 *s++ = buf[cur++];
80 *s = '\0';
81 for (i = 0; i < ARRAY_SIZE(kwds); i++)
82 if (!strcmp(kwds[i].name, name))
83 return kwds[i].id;
84 return TOK_NAME;
86 if ((num = get_tok3(TOK3(buf + cur)))) {
87 cur += 3;
88 return num;
90 if ((num = get_tok3(TOK2(buf + cur)))) {
91 cur += 2;
92 return num;
94 if (strchr(";,{}()[]<>*&!=+-/%?:|^", buf[cur]))
95 return buf[cur++];
96 return -1;
99 int tok_see(void)
101 if (next == -1)
102 next = tok_get();
103 return next;
106 void tok_init(int fd)
108 int n = 0;
109 while ((n = read(fd, buf + len, sizeof(buf) - len)) > 0)
110 len += n;
111 next = -1;
114 char *tok_id(void)
116 return name;