Skip to content

Commit

Permalink
creating keyword token
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Feb 10, 2024
1 parent c414d28 commit a3c602e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
42 changes: 41 additions & 1 deletion lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)});
}
Expand Down
2 changes: 1 addition & 1 deletion test.c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50 + 20 - 10 ++(50 + 20)[#] dksdskdk6sd rele_frle pksdk
50 + 20 - 10 ++(50 + 20)[#] dksdskdk6sd rele_frle pksdk int restrict float continue

0 comments on commit a3c602e

Please sign in to comment.