summaryrefslogtreecommitdiff
path: root/src/lexer.c
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-02 23:45:50 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-05-03 22:39:26 +0200
commit990f4d3e4c662c401a08e3704a39878fd6c1c1b6 (patch)
treeeb8e7469afafc6babae582304b8695611e179ad3 /src/lexer.c
parent77dbf3a5011566b4a6274b3cdfa075dd723642d3 (diff)
parser: Refactor return statement to return an ast_node
During the refactoring process, I identified a memory leak where the return argument was allocated but not freed in case of an error. It also introduces the concept of keyword tokens. Where return is now a keyword simplifying the parser. Signed-off-by: Carlos Maniero <carlos@maniero.me>
Diffstat (limited to 'src/lexer.c')
-rw-r--r--src/lexer.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/lexer.c b/src/lexer.c
index b60fbb5..9f2a57b 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -85,6 +85,7 @@ lexer_tokenize_name(lexer_t *lexer, token_t *token)
while (lexer_is_not_eof(lexer) && isalnum(lexer_current_char(lexer))) {
lexer_drop_char(lexer);
}
+
token->kind = TOKEN_NAME;
token->value = string_view_new(lexer->src + begin, lexer->cur - begin);
token->filepath = lexer->filepath;
@@ -93,6 +94,15 @@ lexer_tokenize_name(lexer_t *lexer, token_t *token)
token->bol = lexer->bol;
}
+static void
+lexer_token_process_keyword(token_t *token)
+{
+ if (string_view_eq(string_view_from_str("return"), token->value)) {
+ token->kind = TOKEN_KEYWORD_RETURN;
+ return;
+ }
+}
+
void
lexer_next_token(lexer_t *lexer, token_t *token)
{
@@ -111,6 +121,7 @@ lexer_next_token(lexer_t *lexer, token_t *token)
if (isalpha(lexer_current_char(lexer))) {
lexer_tokenize_name(lexer, token);
+ lexer_token_process_keyword(token);
return;
}
@@ -292,11 +303,12 @@ token_kind_to_str(token_kind_t kind)
return "/";
case TOKEN_EQUAL:
return "=";
+ case TOKEN_KEYWORD_RETURN:
+ return "return";
case TOKEN_EOF:
return "TOKEN_EOF";
case TOKEN_UNKNOWN:
return "TOKEN_UNKNOWN";
- default:
- assert(false && "unreachable");
}
+ assert(false && "unreachable");
}