mirror of https://github.com/rui314/chibicc
Add L and LL prefixes
This commit is contained in:
parent
06d3190515
commit
11bdd08cff
2
chibi.h
2
chibi.h
|
@ -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);
|
||||
|
|
7
parse.c
7
parse.c
|
@ -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
10
tests
|
@ -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;
|
||||
}
|
||||
|
|
25
tokenize.c
25
tokenize.c
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue