feat: 新增 i32 / u64 / char 类型 + 字符字面量 "'a'"

This commit is contained in:
2026-06-05 20:47:44 +08:00
parent 6fc599e6c2
commit 18172ca724
12 changed files with 146 additions and 16 deletions
+17 -3
View File
@@ -60,9 +60,10 @@ static TokenKind check_keyword(const Token* tok) {
KW("while", TOK_WHILE); KW("for", TOK_FOR); KW("in", TOK_IN);
KW("to", TOK_TO);
KW("return", TOK_RETURN);
KW("i64", TOK_I64); KW("f64", TOK_F64);
KW("bool", TOK_BOOL); KW("str", TOK_STR);
KW("void", TOK_VOID);
KW("i32", TOK_I32); KW("i64", TOK_I64);
KW("u64", TOK_U64); KW("f64", TOK_F64);
KW("bool", TOK_BOOL); KW("char", TOK_CHAR);
KW("str", TOK_STR); KW("void", TOK_VOID);
KW("struct", TOK_STRUCT); KW("type", TOK_TYPE);
KW("enum", TOK_ENUM); KW("extend", TOK_EXTEND); KW("match", TOK_MATCH);
KW("_", TOK_UNDERSCORE);
@@ -101,6 +102,19 @@ Token* lex(Arena* a, const char* source, const char* filename,
char c = peek(&l);
if (isdigit(c)) { tokens[idx++] = lex_number(&l); }
else if (c == '\'') {
advance(&l); // 跳过开头的 '
int char_start = l.pos;
if (peek(&l) == '\\') advance(&l); // 转义字符: \n \t \\ \'
advance(&l); // 跳过字符内容
if (peek(&l) != '\'') {
*error = (ErrorInfo){.message="未闭合的字符字面量", .filename=filename, .line=line, .col=col};
return NULL;
}
int char_len = l.pos - char_start;
advance(&l); // 跳过结尾的 '
tokens[idx++] = make_token(&l, TOK_CHAR_LIT, char_start, char_len);
}
else if (c == '"') {
advance(&l); // 跳过开头的 "
int start = l.pos;