7 static char buf
[BUFSIZE
];
10 static char name
[NAMELEN
];
19 {"static", TOK_STATIC
},
20 {"extern", TOK_EXTERN
},
21 {"return", TOK_RETURN
},
22 {"unsigned", TOK_UNSIGNED
},
23 {"signed", TOK_SIGNED
},
28 {"struct", TOK_STRUCT
},
31 {"typedef", TOK_TYPEDEF
},
37 {"switch", TOK_SWITCH
},
39 {"sizeof", TOK_SIZEOF
},
41 {"continue", TOK_CONTINUE
},
42 {"default", TOK_DEFAULT
},
46 static char *tok3
[] = {
47 "<<", ">>", "++", "--", "<<=", ">>=", "...", "+=", "-=", "*=", "/=",
48 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->", "/*"
51 static int get_tok3(int num
)
54 for (i
= 0; i
< ARRAY_SIZE(tok3
); i
++)
55 if (num
== TOK3(tok3
[i
]))
60 static char *esc_code
= "abefnrtv";
61 static char *esc
= "\a\b\e\f\n\r\t\v";
62 static char *digs
= "0123456789abcdef";
64 static int esc_char(int *c
, char *s
)
70 if (strchr(esc_code
, s
[1])) {
71 *c
= esc
[strchr(esc_code
, s
[1]) - esc_code
];
74 if (isdigit(s
[1]) || s
[1] == 'x') {
83 while ((d
= strchr(digs
, s
[i
]))) {
102 static void readnum(void)
105 if (buf
[cur
] == '0' && buf
[cur
+ 1] == 'x') {
109 if (strchr(digs
, buf
[cur
])) {
112 if (base
== 10 && buf
[cur
] == '0')
114 while ((c
= strchr(digs
, buf
[cur
]))) {
120 while (tolower(buf
[cur
]) == 'u' || tolower(buf
[cur
]) == 'l')
124 if (buf
[cur
] == '\'') {
126 cur
+= 2 + esc_char(&ret
, buf
+ cur
+ 1);
133 static char str
[BUFSIZE
];
136 int tok_str(char *buf
)
139 memcpy(buf
, str
, str_len
);
143 static int readstr(char *out
)
149 while (r
< e
&& *r
!= '"') {
152 r
+= esc_char(&c
, r
);
163 static int id_char(int c
)
165 return isalnum(c
) || c
== '_';
168 static int skipws(void)
171 while (cur
< len
&& isspace(buf
[cur
]))
175 if (TOK2(buf
+ cur
) != TOK2("/*"))
177 while (++cur
< len
) {
178 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
198 if (buf
[cur
] == '"') {
200 while (buf
[cur
] == '"') {
201 str_len
+= readstr(str
+ str_len
);
208 if (isdigit(buf
[cur
]) || buf
[cur
] == '\'') {
212 if (id_char(buf
[cur
])) {
215 while (cur
< len
&& id_char(buf
[cur
]))
218 for (i
= 0; i
< ARRAY_SIZE(kwds
); i
++)
219 if (!strcmp(kwds
[i
].name
, name
))
223 if ((num
= get_tok3(TOK3(buf
+ cur
)))) {
227 if ((num
= get_tok3(TOK2(buf
+ cur
)))) {
231 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf
[cur
]))
243 void tok_init(int fd
)
246 while ((n
= read(fd
, buf
+ len
, sizeof(buf
) - len
)) > 0)
258 return next
== -1 ? cur
: pre
;
261 void tok_jump(long addr
)