diff options
author | Himbeer <himbeer@disroot.org> | 2025-05-08 14:56:58 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2025-05-08 14:56:58 +0200 |
commit | 26f15aea436f136575a96af2c57051142f87ba13 (patch) | |
tree | bc0240cfcd83b1161b12ab8d49f69a551d47b579 | |
parent | 999491d046547dafa7238163d6231a5b3257c250 (diff) |
Add lexical token for character literals
-rw-r--r-- | include/lex.h | 2 | ||||
-rw-r--r-- | src/lex.c | 15 |
2 files changed, 17 insertions, 0 deletions
diff --git a/include/lex.h b/include/lex.h index 199ad74..c3d7115 100644 --- a/include/lex.h +++ b/include/lex.h @@ -99,6 +99,7 @@ enum lexical_token { T_COMMENT, T_NAME, T_NUMBER, + T_CHAR, T_STRING, // Magic values @@ -126,6 +127,7 @@ struct token { struct location loc; union { struct number num; + unsigned int chr; const char *str; } info; }; @@ -218,6 +218,18 @@ lex_number(struct lexer *lexer, struct token *out) return out->token; } +static void +lex_char(struct lexer *lexer, struct token *out) +{ + uint32_t c = next(lexer); + if (next(lexer) != '\'') { + error(lex_loc(lexer), "unclosed or multi-character char literal"); + } + + out->info.chr = c; + out->token = T_CHAR; +} + static enum lexical_token lex_string(struct lexer *lexer, struct token *out, uint32_t delim) { @@ -441,6 +453,9 @@ lex_any(struct lexer *lexer, struct token *out) case '_': out->token = T_UNDERSCORE; break; + case '\'': + lex_char(lexer, out); + break; case '"': return lex_string(lexer, out, c); default: |