chibicc/tokenize.c

156 lines
3.3 KiB
C
Raw Normal View History

2019-08-03 11:17:13 +03:00
#include "chibicc.h"
char *user_input;
Token *token;
// Reports an error and exit.
void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
// Reports an error location and exit.
void error_at(char *loc, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int pos = loc - user_input;
fprintf(stderr, "%s\n", user_input);
fprintf(stderr, "%*s", pos, ""); // print pos spaces.
fprintf(stderr, "^ ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
2019-08-04 09:21:44 +03:00
char *strndup(char *p, int len) {
char *buf = malloc(len + 1);
strncpy(buf, p, len);
buf[len] = '\0';
return buf;
}
2019-08-03 11:17:13 +03:00
// Consumes the current token if it matches `op`.
bool consume(char *op) {
if (token->kind != TK_RESERVED || strlen(op) != token->len ||
memcmp(token->str, op, token->len))
return false;
token = token->next;
return true;
}
2019-08-03 13:05:07 +03:00
// Consumes the current token if it is an identifier.
Token *consume_ident() {
if (token->kind != TK_IDENT)
return NULL;
Token *t = token;
token = token->next;
return t;
}
2019-08-03 11:17:13 +03:00
// Ensure that the current token is `op`.
void expect(char *op) {
if (token->kind != TK_RESERVED || strlen(op) != token->len ||
memcmp(token->str, op, token->len))
error_at(token->str, "expected \"%s\"", op);
token = token->next;
}
// Ensure that the current token is TK_NUM.
int expect_number() {
if (token->kind != TK_NUM)
error_at(token->str, "expected a number");
int val = token->val;
token = token->next;
return val;
}
bool at_eof() {
return token->kind == TK_EOF;
}
// Create a new token and add it as the next token of `cur`.
Token *new_token(TokenKind kind, Token *cur, char *str, int len) {
Token *tok = calloc(1, sizeof(Token));
tok->kind = kind;
tok->str = str;
tok->len = len;
cur->next = tok;
return tok;
}
bool startswith(char *p, char *q) {
return memcmp(p, q, strlen(q)) == 0;
}
2019-08-03 12:30:00 +03:00
bool is_alpha(char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_';
}
bool is_alnum(char c) {
return is_alpha(c) || ('0' <= c && c <= '9');
}
2019-08-03 11:17:13 +03:00
// Tokenize `user_input` and returns new tokens.
Token *tokenize() {
char *p = user_input;
Token head;
head.next = NULL;
Token *cur = &head;
while (*p) {
// Skip whitespace characters.
if (isspace(*p)) {
p++;
continue;
}
2019-08-03 12:30:00 +03:00
// Keyword
if (startswith(p, "return") && !is_alnum(p[6])) {
cur = new_token(TK_RESERVED, cur, p, 6);
p += 6;
continue;
}
2019-08-03 11:17:13 +03:00
// Multi-letter punctuator
if (startswith(p, "==") || startswith(p, "!=") ||
startswith(p, "<=") || startswith(p, ">=")) {
cur = new_token(TK_RESERVED, cur, p, 2);
p += 2;
continue;
}
// Single-letter punctuator
2019-08-03 13:05:07 +03:00
if (strchr("+-*/()<>;=", *p)) {
2019-08-03 11:17:13 +03:00
cur = new_token(TK_RESERVED, cur, p++, 1);
continue;
}
2019-08-03 13:05:07 +03:00
// Identifier
2019-08-04 09:21:44 +03:00
if (is_alpha(*p)) {
char *q = p++;
while (is_alnum(*p))
p++;
cur = new_token(TK_IDENT, cur, q, p - q);
2019-08-03 13:05:07 +03:00
continue;
}
2019-08-03 11:17:13 +03:00
// Integer literal
if (isdigit(*p)) {
cur = new_token(TK_NUM, cur, p, 0);
char *q = p;
cur->val = strtol(p, &p, 10);
cur->len = p - q;
continue;
}
error_at(p, "invalid token");
}
new_token(TK_EOF, cur, p, 0);
return head.next;
}