ncc: define __i386__ for x86 builds
[neatcc.git] / tok.c
blobb3c805c3d791823d9200d8efc29ede17592bbec2
1 #include <ctype.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "gen.h"
6 #include "tok.h"
8 static char buf[BUFSIZE];
9 static int len;
10 static int cur;
11 static char name[NAMELEN];
12 static int next = -1;
13 static int pre;
15 static struct {
16 char *name;
17 unsigned id;
18 } kwds[] = {
19 {"void", TOK_VOID},
20 {"static", TOK_STATIC},
21 {"extern", TOK_EXTERN},
22 {"return", TOK_RETURN},
23 {"unsigned", TOK_UNSIGNED},
24 {"signed", TOK_SIGNED},
25 {"short", TOK_SHORT},
26 {"long", TOK_LONG},
27 {"int", TOK_INT},
28 {"char", TOK_CHAR},
29 {"struct", TOK_STRUCT},
30 {"union", TOK_UNION},
31 {"enum", TOK_ENUM},
32 {"typedef", TOK_TYPEDEF},
33 {"if", TOK_IF},
34 {"else", TOK_ELSE},
35 {"for", TOK_FOR},
36 {"while", TOK_WHILE},
37 {"do", TOK_DO},
38 {"switch", TOK_SWITCH},
39 {"case", TOK_CASE},
40 {"sizeof", TOK_SIZEOF},
41 {"break", TOK_BREAK},
42 {"continue", TOK_CONTINUE},
43 {"default", TOK_DEFAULT},
44 {"goto", TOK_GOTO},
47 static char *tok3[] = {
48 "<<=", ">>=", "...", "<<", ">>", "++", "--", "+=", "-=", "*=", "/=",
49 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->"
52 static int get_tok3(int num)
54 int i;
55 for (i = 0; i < ARRAY_SIZE(tok3); i++)
56 if (num == TOK3(tok3[i]))
57 return num;
58 return 0;
61 static char *esc_code = "abefnrtv";
62 static char *esc = "\a\b\e\f\n\r\t\v";
63 static char *digs = "0123456789abcdef";
65 static int esc_char(int *c, char *s)
67 if (*s != '\\') {
68 *c = *s;
69 return 1;
71 if (strchr(esc_code, s[1])) {
72 *c = esc[strchr(esc_code, s[1]) - esc_code];
73 return 2;
75 if (isdigit(s[1]) || s[1] == 'x') {
76 int ret = 0;
77 int base = 8;
78 int i = 1;
79 char *d;
80 if (s[1] == 'x') {
81 base = 16;
82 i++;
84 while ((d = strchr(digs, s[i]))) {
85 ret *= base;
86 ret += d - digs;
87 i++;
89 *c = ret;
90 return i;
92 *c = s[1];
93 return 2;
96 static long num;
97 static int num_bt;
99 int tok_num(long *n)
101 *n = num;
102 return num_bt;
105 static void readnum(void)
107 int base = 10;
108 num_bt = 4 | BT_SIGNED;
109 if (buf[cur] == '0' && buf[cur + 1] == 'x') {
110 num_bt &= ~BT_SIGNED;
111 base = 16;
112 cur += 2;
114 if (strchr(digs, tolower(buf[cur]))) {
115 long result = 0;
116 char *c;
117 if (base == 10 && buf[cur] == '0')
118 base = 8;
119 while (cur < len && (c = strchr(digs, tolower(buf[cur])))) {
120 result *= base;
121 result += c - digs;
122 cur++;
124 num = result;
125 while (cur < len) {
126 int c = tolower(buf[cur]);
127 if (c != 'u' && c != 'l')
128 break;
129 if (c == 'u')
130 num_bt &= ~BT_SIGNED;
131 if (c == 'l')
132 num_bt = (num_bt & BT_SIGNED) | LONGSZ;
133 cur++;
135 return;
137 if (buf[cur] == '\'') {
138 int ret;
139 cur += 2 + esc_char(&ret, buf + cur + 1);
140 num = ret;
141 return;
143 num = -1;
146 static char str[BUFSIZE];
147 static int str_len;
149 int tok_str(char *buf)
151 if (buf)
152 memcpy(buf, str, str_len);
153 return str_len;
156 static int readstr(char *out)
158 char *s = out;
159 char *r = buf + cur;
160 char *e = buf + len;
161 r++;
162 while (r < e && *r != '"') {
163 if (*r == '\\') {
164 int c;
165 r += esc_char(&c, r);
166 *s++ = c;
167 } else {
168 *s++ = *r++;
171 *s++ = '\0';
172 cur = r - buf + 1;
173 return s - out - 1;
176 static int id_char(int c)
178 return isalnum(c) || c == '_';
181 static int skipws(void)
183 while (1) {
184 if (cur == len) {
185 int r;
186 while (!(r = cpp_read(buf + cur)))
188 if (r == -1)
189 return 1;
190 len += r;
192 while (cur < len && isspace(buf[cur]))
193 cur++;
194 if (cur == len)
195 continue;
196 if (buf[cur] == '/' && buf[cur + 1] == '/') {
197 while (cur < len && buf[cur] != '\n')
198 cur++;
199 continue;
201 if (buf[cur] == '/' && buf[cur + 1] == '*') {
202 while (++cur < len) {
203 if (buf[cur] == '*' && buf[cur + 1] == '/') {
204 cur += 2;
205 break;
208 continue;
210 break;
212 return 0;
215 int tok_get(void)
217 int num;
218 if (next != -1) {
219 int tok = next;
220 next = -1;
221 return tok;
223 pre = cur;
224 if (skipws())
225 return TOK_EOF;
226 if (buf[cur] == '"') {
227 str_len = 0;
228 while (buf[cur] == '"') {
229 str_len += readstr(str + str_len);
230 if (skipws())
231 return TOK_EOF;
233 str_len++;
234 return TOK_STR;
236 if (isdigit(buf[cur]) || buf[cur] == '\'') {
237 readnum();
238 return TOK_NUM;
240 if (id_char(buf[cur])) {
241 char *s = name;
242 int i;
243 while (cur < len && id_char(buf[cur]))
244 *s++ = buf[cur++];
245 *s = '\0';
246 for (i = 0; i < ARRAY_SIZE(kwds); i++)
247 if (!strcmp(kwds[i].name, name))
248 return kwds[i].id;
249 return TOK_NAME;
251 if (cur + 3 <= len && (num = get_tok3(TOK3(buf + cur)))) {
252 cur += 3;
253 return num;
255 if ((num = get_tok3(TOK2(buf + cur)))) {
256 cur += 2;
257 return num;
259 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf[cur]))
260 return buf[cur++];
261 return -1;
264 int tok_see(void)
266 if (next == -1)
267 next = tok_get();
268 return next;
271 char *tok_id(void)
273 return name;
276 long tok_addr(void)
278 return next == -1 ? cur : pre;
281 void tok_jump(long addr)
283 cur = addr;
284 pre = cur - 1;
285 next = -1;