ncc.h: move array limits to ncc.h
[neatcc/cc.git] / tok.c
blobd9550c8ee5a0a138f7c67df8e30ca8e53c8b7381
1 #include <ctype.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "gen.h"
6 #include "ncc.h"
7 #include "tok.h"
9 static char buf[BUFLEN];
10 static int len;
11 static int cur;
12 static char name[NAMELEN];
13 static int next = -1;
14 static int pre;
16 static struct {
17 char *name;
18 unsigned id;
19 } kwds[] = {
20 {"void", TOK_VOID},
21 {"static", TOK_STATIC},
22 {"extern", TOK_EXTERN},
23 {"return", TOK_RETURN},
24 {"unsigned", TOK_UNSIGNED},
25 {"signed", TOK_SIGNED},
26 {"short", TOK_SHORT},
27 {"long", TOK_LONG},
28 {"int", TOK_INT},
29 {"char", TOK_CHAR},
30 {"struct", TOK_STRUCT},
31 {"union", TOK_UNION},
32 {"enum", TOK_ENUM},
33 {"typedef", TOK_TYPEDEF},
34 {"if", TOK_IF},
35 {"else", TOK_ELSE},
36 {"for", TOK_FOR},
37 {"while", TOK_WHILE},
38 {"do", TOK_DO},
39 {"switch", TOK_SWITCH},
40 {"case", TOK_CASE},
41 {"sizeof", TOK_SIZEOF},
42 {"break", TOK_BREAK},
43 {"continue", TOK_CONTINUE},
44 {"default", TOK_DEFAULT},
45 {"goto", TOK_GOTO},
48 static char *tok3[] = {
49 "<<=", ">>=", "...", "<<", ">>", "++", "--", "+=", "-=", "*=", "/=",
50 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->"
53 static int get_tok3(int num)
55 int i;
56 for (i = 0; i < LEN(tok3); i++)
57 if (num == TOK3(tok3[i]))
58 return num;
59 return 0;
62 static char *esc_code = "abefnrtv";
63 static char *esc = "\a\b\e\f\n\r\t\v";
64 static char *digs = "0123456789abcdef";
66 static int esc_char(int *c, char *s)
68 if (*s != '\\') {
69 *c = *s;
70 return 1;
72 if (strchr(esc_code, s[1])) {
73 *c = esc[strchr(esc_code, s[1]) - esc_code];
74 return 2;
76 if (isdigit(s[1]) || s[1] == 'x') {
77 int ret = 0;
78 int base = 8;
79 int i = 1;
80 char *d;
81 if (s[1] == 'x') {
82 base = 16;
83 i++;
85 while ((d = memchr(digs, s[i], base))) {
86 ret *= base;
87 ret += d - digs;
88 i++;
90 *c = ret;
91 return i;
93 *c = s[1];
94 return 2;
97 static long num;
98 static int num_bt;
100 int tok_num(long *n)
102 *n = num;
103 return num_bt;
106 static void readnum(void)
108 int base = 10;
109 num_bt = 4 | BT_SIGNED;
110 if (buf[cur] == '0' && buf[cur + 1] == 'x') {
111 num_bt &= ~BT_SIGNED;
112 base = 16;
113 cur += 2;
115 if (strchr(digs, tolower(buf[cur]))) {
116 long result = 0;
117 char *c;
118 if (base == 10 && buf[cur] == '0')
119 base = 8;
120 while (cur < len && (c = strchr(digs, tolower(buf[cur])))) {
121 result *= base;
122 result += c - digs;
123 cur++;
125 num = result;
126 while (cur < len) {
127 int c = tolower(buf[cur]);
128 if (c != 'u' && c != 'l')
129 break;
130 if (c == 'u')
131 num_bt &= ~BT_SIGNED;
132 if (c == 'l')
133 num_bt = (num_bt & BT_SIGNED) | LONGSZ;
134 cur++;
136 return;
138 if (buf[cur] == '\'') {
139 int ret;
140 cur += 2 + esc_char(&ret, buf + cur + 1);
141 num = ret;
142 return;
144 num = -1;
147 static char str[STRLEN];
148 static int str_len;
150 int tok_str(char *buf)
152 if (buf)
153 memcpy(buf, str, str_len);
154 return str_len;
157 static int readstr(char *out)
159 char *s = out;
160 char *r = buf + cur;
161 char *e = buf + len;
162 r++;
163 while (r < e && *r != '"') {
164 if (*r == '\\') {
165 int c;
166 r += esc_char(&c, r);
167 *s++ = c;
168 } else {
169 *s++ = *r++;
172 *s++ = '\0';
173 cur = r - buf + 1;
174 return s - out - 1;
177 static int id_char(int c)
179 return isalnum(c) || c == '_';
182 static int skipws(void)
184 while (1) {
185 if (cur == len) {
186 int r;
187 while (!(r = cpp_read(buf + cur)))
189 if (r == -1)
190 return 1;
191 len += r;
193 while (cur < len && isspace(buf[cur]))
194 cur++;
195 if (cur == len)
196 continue;
197 if (buf[cur] == '\\' && buf[cur + 1] == '\n') {
198 cur += 2;
199 continue;
201 if (buf[cur] == '/' && buf[cur + 1] == '/') {
202 while (cur < len && buf[cur] != '\n')
203 cur++;
204 continue;
206 if (buf[cur] == '/' && buf[cur + 1] == '*') {
207 while (++cur < len) {
208 if (buf[cur] == '*' && buf[cur + 1] == '/') {
209 cur += 2;
210 break;
213 continue;
215 break;
217 return 0;
220 int tok_get(void)
222 int num;
223 if (next != -1) {
224 int tok = next;
225 next = -1;
226 return tok;
228 pre = cur;
229 if (skipws())
230 return TOK_EOF;
231 if (buf[cur] == '"') {
232 str_len = 0;
233 while (buf[cur] == '"') {
234 str_len += readstr(str + str_len);
235 if (skipws())
236 return TOK_EOF;
238 str_len++;
239 return TOK_STR;
241 if (isdigit(buf[cur]) || buf[cur] == '\'') {
242 readnum();
243 return TOK_NUM;
245 if (id_char(buf[cur])) {
246 char *s = name;
247 int i;
248 while (cur < len && id_char(buf[cur]))
249 *s++ = buf[cur++];
250 *s = '\0';
251 for (i = 0; i < LEN(kwds); i++)
252 if (!strcmp(kwds[i].name, name))
253 return kwds[i].id;
254 return TOK_NAME;
256 if (cur + 3 <= len && (num = get_tok3(TOK3(buf + cur)))) {
257 cur += 3;
258 return num;
260 if ((num = get_tok3(TOK2(buf + cur)))) {
261 cur += 2;
262 return num;
264 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf[cur]))
265 return buf[cur++];
266 return -1;
269 int tok_see(void)
271 if (next == -1)
272 next = tok_get();
273 return next;
276 char *tok_id(void)
278 return name;
281 long tok_addr(void)
283 return next == -1 ? cur : pre;
286 void tok_jump(long addr)
288 cur = addr;
289 pre = cur - 1;
290 next = -1;