Add L and LL prefixes

This commit is contained in:
Rui Ueyama 2019-09-21 16:17:40 +09:00
parent 06d3190515
commit 11bdd08cff
5 changed files with 33 additions and 12 deletions

View File

@ -34,6 +34,7 @@ struct Token {
TokenKind kind; // Token kind
Token *next; // Next token
long val; // If kind is TK_NUM, its value
Type *ty; // Used if TK_NUM
char *str; // Token string
int len; // Token length
@ -49,7 +50,6 @@ Token *peek(char *s);
Token *consume(char *op);
Token *consume_ident(void);
void expect(char *op);
long expect_number(void);
char *expect_ident(void);
bool at_eof(void);
Token *tokenize(void);

View File

@ -99,6 +99,7 @@ static Node *new_unary(NodeKind kind, Node *expr, Token *tok) {
static Node *new_num(long val, Token *tok) {
Node *node = new_node(ND_NUM, tok);
node->val = val;
node->ty = int_type;
return node;
}
@ -1887,5 +1888,9 @@ static Node *primary(void) {
if (tok->kind != TK_NUM)
error_tok(tok, "expected expression");
return new_num(expect_number(), tok);
token = tok->next;
Node *node = new_num(tok->val, tok);
node->ty = tok->ty;
return node;
}

10
tests
View File

@ -732,6 +732,16 @@ int main() {
assert(8, sizeof(signed long long), "sizeof(signed long long)");
assert(8, sizeof(signed long long int), "sizeof(signed long long int)");
assert(4, sizeof(0), "sizeof(0)");
assert(8, sizeof(0L), "sizeof(0L)");
assert(8, sizeof(0LL), "sizeof(0LL)");
assert(8, sizeof(0l), "sizeof(0l)");
assert(8, sizeof(0ll), "sizeof(0ll)");
assert(8, sizeof(0x0L), "sizeof(0x0L)");
assert(8, sizeof(0b0L), "sizeof(0b0L)");
assert(4, sizeof(2147483647), "sizeof(2147483647)");
assert(8, sizeof(2147483648), "sizeof(2147483648)");
printf("OK\n");
return 0;
}

View File

@ -101,15 +101,6 @@ void expect(char *s) {
token = token->next;
}
// Ensure that the current token is TK_NUM.
long expect_number(void) {
if (token->kind != TK_NUM)
error_tok(token, "expected a number");
long val = token->val;
token = token->next;
return val;
}
// Ensure that the current token is TK_IDENT.
char *expect_ident(void) {
if (token->kind != TK_IDENT)
@ -234,12 +225,14 @@ static Token *read_char_literal(Token *cur, char *start) {
Token *tok = new_token(TK_NUM, cur, start, p - start);
tok->val = c;
tok->ty = int_type;
return tok;
}
static Token *read_int_literal(Token *cur, char *start) {
char *p = start;
// Read a binary, octal, decimal or hexadecimal number.
int base;
if (!strncasecmp(p, "0x", 2) && is_alnum(p[2])) {
p += 2;
@ -254,11 +247,25 @@ static Token *read_int_literal(Token *cur, char *start) {
}
long val = strtol(p, &p, base);
Type *ty = int_type;
// Read L or LL prefix or infer a type.
if (startswith(p, "LL") || startswith(p, "ll")) {
p += 2;
ty = long_type;
} else if (*p == 'L' || *p == 'l') {
p++;
ty = long_type;
} else if (val != (int)val) {
ty = long_type;
}
if (is_alnum(*p))
error_at(p, "invalid digit");
Token *tok = new_token(TK_NUM, cur, start, p - start);
tok->val = val;
tok->ty = ty;
return tok;
}

1
type.c
View File

@ -84,7 +84,6 @@ void add_type(Node *node) {
case ND_NE:
case ND_LT:
case ND_LE:
case ND_NUM:
case ND_NOT:
case ND_LOGOR:
case ND_LOGAND: