Skip to content

Commit

Permalink
creating identifier token
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Feb 10, 2024
1 parent 841445e commit c414d28
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 31 additions & 1 deletion lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "helpers/buffer.h"
#include <string.h>
#include <assert.h>
#include <ctype.h>

#define LEX_GETC_IF(buffer, c, exp) \
for (c = peekc(); exp; c = peekc()) \
Expand Down Expand Up @@ -285,6 +286,31 @@ static struct token *token_make_symbol()
return token;
}

static struct token *token_make_identifier_or_keyword()
{
struct buffer *buffer = buffer_create();
char c = 0;
LEX_GETC_IF(buffer, c, (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_');

// null terminator
buffer_write(buffer, 0x00);

// TODO-OB: check if it is a keyword

return token_create(&(struct token){.type = TOKEN_TYPE_IDENTIFIER, .sval = buffer_ptr(buffer)});
}

struct token *read_special_token()
{
char c = peekc();
if (isalpha(c) || c == '_')
{
return token_make_identifier_or_keyword();
}

return NULL;
}

struct token *
read_next_token()
{
Expand Down Expand Up @@ -319,7 +345,11 @@ read_next_token()
// Finished the lexical analysis on the file
break;
default:
compiler_error(lex_process->compiler, "Unexpected token\n");
token = read_special_token();
if (!token)
{
compiler_error(lex_process->compiler, "Unexpected token\n");
}
}
return token;
}
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)[#]
50 + 20 - 10 ++(50 + 20)[#] dksdskdk6sd rele_frle pksdk

0 comments on commit c414d28

Please sign in to comment.