From f524899da47eb5780371bde1bc46c019bb34411e Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 24 Nov 2024 16:12:06 +0000 Subject: [PATCH] lang: lex: convert lexer token queue to b_queue --- lang/include/ivy/lang/lex.h | 3 ++- lang/lex.c | 36 +++++++++++++++++------------------- lang/lex.h | 2 +- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lang/include/ivy/lang/lex.h b/lang/include/ivy/lang/lex.h index 107c271..40a0ca0 100644 --- a/lang/include/ivy/lang/lex.h +++ b/lang/include/ivy/lang/lex.h @@ -4,6 +4,7 @@ #include #include #include +#include enum ivy_token_type { IVY_TOK_NONE = 0, @@ -103,7 +104,7 @@ enum ivy_symbol { struct ivy_token { enum ivy_token_type t_type; - struct ivy_token *t_next; + b_queue_entry t_entry; union { enum ivy_keyword t_keyword; diff --git a/lang/lex.c b/lang/lex.c index fd9ddb3..0317e35 100644 --- a/lang/lex.c +++ b/lang/lex.c @@ -294,10 +294,13 @@ enum ivy_status ivy_lexer_create(struct ivy_lexer **lexp) void ivy_lexer_destroy(struct ivy_lexer *lex) { - while (lex->lex_queue) { - struct ivy_token *next = lex->lex_queue->t_next; - ivy_token_destroy(lex->lex_queue); - lex->lex_queue = next; + b_queue_iterator it = {0}; + b_queue_iterator_begin(&lex->lex_queue, &it); + + while (b_queue_iterator_is_valid(&it)) { + struct ivy_token *tok = b_unbox(struct ivy_token, it.entry, t_entry); + b_queue_iterator_erase(&it); + ivy_token_destroy(tok); } if (lex->lex_linebuf) { @@ -443,14 +446,7 @@ static struct ivy_token *create_token(enum ivy_token_type type) static enum ivy_status push_token(struct ivy_lexer *lex, struct ivy_token *tok) { - struct ivy_token **slot = &lex->lex_queue; - - while (*slot) { - slot = &(*slot)->t_next; - } - - *slot = tok; - lex->lex_prev_token = tok->t_type; + b_queue_push_back(&lex->lex_queue, &tok->t_entry); return IVY_OK; } @@ -1017,7 +1013,7 @@ struct ivy_token *ivy_lexer_peek(struct ivy_lexer *lex) { enum ivy_status status = IVY_OK; - while (!lex->lex_queue) { + while (b_queue_empty(&lex->lex_queue)) { status = pump_tokens(lex); if (status != IVY_OK) { @@ -1027,7 +1023,9 @@ struct ivy_token *ivy_lexer_peek(struct ivy_lexer *lex) } lex->lex_status = status; - struct ivy_token *tok = lex->lex_queue; + + b_queue_entry *entry = b_queue_first(&lex->lex_queue); + struct ivy_token *tok = b_unbox(struct ivy_token, entry, t_entry); return tok; } @@ -1035,7 +1033,7 @@ struct ivy_token *ivy_lexer_read(struct ivy_lexer *lex) { enum ivy_status status = IVY_OK; - while (!lex->lex_queue) { + while (b_queue_empty(&lex->lex_queue)) { status = pump_tokens(lex); if (status != IVY_OK) { @@ -1043,15 +1041,15 @@ struct ivy_token *ivy_lexer_read(struct ivy_lexer *lex) return NULL; } } - - struct ivy_token *tok = lex->lex_queue; - lex->lex_queue = lex->lex_queue->t_next; + + b_queue_entry *entry = b_queue_pop_front(&lex->lex_queue); + struct ivy_token *tok = b_unbox(struct ivy_token, entry, t_entry); return tok; } bool ivy_lexer_tokens_available(struct ivy_lexer *lex) { - if (lex->lex_queue) { + if (!b_queue_empty(&lex->lex_queue)) { return true; } diff --git a/lang/lex.h b/lang/lex.h index c95f88c..5a02db9 100644 --- a/lang/lex.h +++ b/lang/lex.h @@ -14,7 +14,7 @@ struct ivy_lexer { b_dict *lex_keywords; enum ivy_status lex_status; - struct ivy_token *lex_queue; + b_queue lex_queue; enum ivy_token_type lex_prev_token; b_string *lex_temp;