let tok_num() return the bt
[neatcc.git] / tok.c
blob76acb046beeb8c2c725c88ae731f50d940fcdcb3
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 extern int cpp_read(char *s);
10 static char buf[BUFSIZE];
11 static int len;
12 static int cur;
13 static char name[NAMELEN];
14 static int next = -1;
15 static int pre;
17 static struct {
18 char *name;
19 unsigned id;
20 } kwds[] = {
21 {"void", TOK_VOID},
22 {"static", TOK_STATIC},
23 {"extern", TOK_EXTERN},
24 {"return", TOK_RETURN},
25 {"unsigned", TOK_UNSIGNED},
26 {"signed", TOK_SIGNED},
27 {"short", TOK_SHORT},
28 {"long", TOK_LONG},
29 {"int", TOK_INT},
30 {"char", TOK_CHAR},
31 {"struct", TOK_STRUCT},
32 {"union", TOK_UNION},
33 {"enum", TOK_ENUM},
34 {"typedef", TOK_TYPEDEF},
35 {"if", TOK_IF},
36 {"else", TOK_ELSE},
37 {"for", TOK_FOR},
38 {"while", TOK_WHILE},
39 {"do", TOK_DO},
40 {"switch", TOK_SWITCH},
41 {"case", TOK_CASE},
42 {"sizeof", TOK_SIZEOF},
43 {"break", TOK_BREAK},
44 {"continue", TOK_CONTINUE},
45 {"default", TOK_DEFAULT},
46 {"goto", TOK_GOTO},
49 static char *tok3[] = {
50 "<<", ">>", "++", "--", "<<=", ">>=", "...", "+=", "-=", "*=", "/=",
51 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->", "/*"
54 static int get_tok3(int num)
56 int i;
57 for (i = 0; i < ARRAY_SIZE(tok3); i++)
58 if (num == TOK3(tok3[i]))
59 return num;
60 return 0;
63 static char *esc_code = "abefnrtv";
64 static char *esc = "\a\b\e\f\n\r\t\v";
65 static char *digs = "0123456789abcdef";
67 static int esc_char(int *c, char *s)
69 if (*s != '\\') {
70 *c = *s;
71 return 1;
73 if (strchr(esc_code, s[1])) {
74 *c = esc[strchr(esc_code, s[1]) - esc_code];
75 return 2;
77 if (isdigit(s[1]) || s[1] == 'x') {
78 int ret = 0;
79 int base = 8;
80 int i = 1;
81 char *d;
82 if (s[1] == 'x') {
83 base = 16;
84 i++;
86 while ((d = strchr(digs, s[i]))) {
87 ret *= base;
88 ret += d - digs;
89 i++;
91 *c = ret;
92 return i;
94 *c = s[1];
95 return 2;
98 static long num;
99 static int num_bt;
101 int tok_num(long *n)
103 *n = num;
104 return num_bt;
107 static void readnum(void)
109 int base = 10;
110 num_bt = 4 | BT_SIGNED;
111 if (buf[cur] == '0' && buf[cur + 1] == 'x') {
112 base = 16;
113 cur += 2;
115 if (strchr(digs, 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) | 8;
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[BUFSIZE];
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 (TOK2(buf + cur) != TOK2("/*"))
198 return 0;
199 while (++cur < len) {
200 if (buf[cur] == '*' && buf[cur + 1] == '/') {
201 cur += 2;
202 break;
206 return 0;
209 int tok_get(void)
211 int num;
212 if (next != -1) {
213 int tok = next;
214 next = -1;
215 return tok;
217 pre = cur;
218 if (skipws())
219 return TOK_EOF;
220 if (buf[cur] == '"') {
221 str_len = 0;
222 while (buf[cur] == '"') {
223 str_len += readstr(str + str_len);
224 if (skipws())
225 return TOK_EOF;
227 str_len++;
228 return TOK_STR;
230 if (isdigit(buf[cur]) || buf[cur] == '\'') {
231 readnum();
232 return TOK_NUM;
234 if (id_char(buf[cur])) {
235 char *s = name;
236 int i;
237 while (cur < len && id_char(buf[cur]))
238 *s++ = buf[cur++];
239 *s = '\0';
240 for (i = 0; i < ARRAY_SIZE(kwds); i++)
241 if (!strcmp(kwds[i].name, name))
242 return kwds[i].id;
243 return TOK_NAME;
245 if ((num = get_tok3(TOK3(buf + cur)))) {
246 cur += 3;
247 return num;
249 if ((num = get_tok3(TOK2(buf + cur)))) {
250 cur += 2;
251 return num;
253 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf[cur]))
254 return buf[cur++];
255 return -1;
258 int tok_see(void)
260 if (next == -1)
261 next = tok_get();
262 return next;
265 char *tok_id(void)
267 return name;
270 long tok_addr(void)
272 return next == -1 ? cur : pre;
275 void tok_jump(long addr)
277 cur = addr;
278 pre = cur - 1;
279 next = -1;