diff options
author | Himbeer <himbeer@disroot.org> | 2024-09-12 11:15:31 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-09-12 12:34:26 +0200 |
commit | 8712349ebc414cc4312cf44553f9363cae7b06e0 (patch) | |
tree | 63df4a352c4dff09dbdb3ab02b49305d3ec1e780 | |
parent | d49e14428603a5561147db576ebdafe43b9a613f (diff) |
Treat builtin types as keywords during lexical analysis
-rw-r--r-- | doc/grammar.txt | 12 | ||||
-rw-r--r-- | include/lex.h | 24 | ||||
-rw-r--r-- | src/lex.c | 17 |
3 files changed, 33 insertions, 20 deletions
diff --git a/doc/grammar.txt b/doc/grammar.txt index 8616eca..acf3762 100644 --- a/doc/grammar.txt +++ b/doc/grammar.txt @@ -48,12 +48,12 @@ defer := "defer" call type := integer | float | array | slice | struct | enum | union | "*" type | "?" type | "!" type | IDENT -integer := "i8" | "u8" - | "i16" | "u16" - | "i32" | "u32" - | "i64" | "u64" - | "isize" | "usize" -float := "f32" | "f64" +integer := "int8" | "uint8" + | "int16" | "uint16" + | "int32" | "uint32" + | "int64" | "uint64" + | "int" | "uint" +float := "float32" | "float64" array := "[" INTEGER "]" type slice := "[]" type struct := "struct" "{" fields "}" diff --git a/include/lex.h b/include/lex.h index 1087317..cf580ec 100644 --- a/include/lex.h +++ b/include/lex.h @@ -28,18 +28,18 @@ enum lexical_token { T_LAST_KEYWORD, // Builtin types - T_F32, - T_F64, - T_I8, - T_I16, - T_I32, - T_I64, - T_ISIZE, - T_U8, - T_U16, - T_U32, - T_U64, - T_USIZE, + T_FLOAT32, + T_FLOAT64, + T_INT8, + T_INT16, + T_INT32, + T_INT64, + T_INT, + T_UINT8, + T_UINT16, + T_UINT32, + T_UINT64, + T_UINT, T_LAST_BUILTIN_TYPE, // Operators @@ -29,6 +29,19 @@ const char *tokens[] = { [T_RETURN] = "return", [T_STRUCT] = "struct", [T_UNION] = "union", + + [T_FLOAT32] = "float32", + [T_FLOAT64] = "float64", + [T_INT8] = "int8", + [T_INT16] = "int16", + [T_INT32] = "int32", + [T_INT64] = "int64", + [T_INT] = "int", + [T_UINT8] = "uint8", + [T_UINT16] = "uint16", + [T_UINT32] = "uint32", + [T_UINT64] = "uint64", + [T_UINT] = "uint", }; void @@ -123,7 +136,7 @@ static void * search(const char *buf, int bound) { for (int i = 0; i < bound; ++i) { - if (strcmp(buf, tokens[i]) == 0) { + if (tokens[i] && (strcmp(buf, tokens[i]) == 0)) { return &tokens[i]; } } @@ -148,7 +161,7 @@ lex_ident(struct lexer *lexer, struct token *out) push(lexer, c); } - void *token = search(lexer->buf, T_LAST_KEYWORD); + void *token = search(lexer->buf, T_LAST_BUILTIN_TYPE); if (!token && isident) { out->token = T_IDENT; out->info.str = strdup(lexer->buf); |