From a3c602e2753760f6f66b9f9b9863215294c1d138 Mon Sep 17 00:00:00 2001 From: omdxp Date: Sat, 10 Feb 2024 10:35:30 +0100 Subject: [PATCH] creating keyword token --- lexer.c | 42 +++++++++++++++++++++++++++++++++++++++++- test.c | 2 +- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lexer.c b/lexer.c index 343d4bc..ae4b04d 100644 --- a/lexer.c +++ b/lexer.c @@ -254,6 +254,42 @@ bool lex_is_in_expression() return lex_process->current_expression_count >= 0; } +bool is_keyword(const char *str) +{ + + return S_EQ(str, "unsigned") || + S_EQ(str, "signed") || + S_EQ(str, "char") || + S_EQ(str, "short") || + S_EQ(str, "int") || + S_EQ(str, "long") || + S_EQ(str, "float") || + S_EQ(str, "double") || + S_EQ(str, "void") || + S_EQ(str, "struct") || + S_EQ(str, "union") || + S_EQ(str, "static") || + S_EQ(str, "__ingore_typecheck") || + S_EQ(str, "return") || + S_EQ(str, "include") || + S_EQ(str, "sizeof") || + S_EQ(str, "if") || + S_EQ(str, "else") || + S_EQ(str, "while") || + S_EQ(str, "for") || + S_EQ(str, "do") || + S_EQ(str, "break") || + S_EQ(str, "continue") || + S_EQ(str, "switch") || + S_EQ(str, "case") || + S_EQ(str, "default") || + S_EQ(str, "goto") || + S_EQ(str, "typedef") || + S_EQ(str, "const") || + S_EQ(str, "extern") || + S_EQ(str, "restrict"); +} + static struct token *token_make_operator_or_string() { char op = peekc(); @@ -295,7 +331,11 @@ static struct token *token_make_identifier_or_keyword() // null terminator buffer_write(buffer, 0x00); - // TODO-OB: check if it is a keyword + // check if it is a keyword + if (is_keyword(buffer_ptr(buffer))) + { + return token_create(&(struct token){.type = TOKEN_TYPE_KEYWORD, .sval = buffer_ptr(buffer)}); + } return token_create(&(struct token){.type = TOKEN_TYPE_IDENTIFIER, .sval = buffer_ptr(buffer)}); } diff --git a/test.c b/test.c index f5960a5..1327387 100644 --- a/test.c +++ b/test.c @@ -1 +1 @@ -50 + 20 - 10 ++(50 + 20)[#] dksdskdk6sd rele_frle pksdk +50 + 20 - 10 ++(50 + 20)[#] dksdskdk6sd rele_frle pksdk int restrict float continue