aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2025-05-08 14:56:58 +0200
committerHimbeer <himbeer@disroot.org>2025-05-08 14:56:58 +0200
commit26f15aea436f136575a96af2c57051142f87ba13 (patch)
treebc0240cfcd83b1161b12ab8d49f69a551d47b579
parent999491d046547dafa7238163d6231a5b3257c250 (diff)
Add lexical token for character literals
-rw-r--r--include/lex.h2
-rw-r--r--src/lex.c15
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;
};
diff --git a/src/lex.c b/src/lex.c
index 97746e2..ca21ba9 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -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: