lang: lex: convert lexer token queue to b_queue
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
#include <ivy/line-source.h>
|
#include <ivy/line-source.h>
|
||||||
#include <ivy/misc.h>
|
#include <ivy/misc.h>
|
||||||
#include <ivy/status.h>
|
#include <ivy/status.h>
|
||||||
|
#include <blue/core/queue.h>
|
||||||
|
|
||||||
enum ivy_token_type {
|
enum ivy_token_type {
|
||||||
IVY_TOK_NONE = 0,
|
IVY_TOK_NONE = 0,
|
||||||
@@ -103,7 +104,7 @@ enum ivy_symbol {
|
|||||||
|
|
||||||
struct ivy_token {
|
struct ivy_token {
|
||||||
enum ivy_token_type t_type;
|
enum ivy_token_type t_type;
|
||||||
struct ivy_token *t_next;
|
b_queue_entry t_entry;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
enum ivy_keyword t_keyword;
|
enum ivy_keyword t_keyword;
|
||||||
|
|||||||
36
lang/lex.c
36
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)
|
void ivy_lexer_destroy(struct ivy_lexer *lex)
|
||||||
{
|
{
|
||||||
while (lex->lex_queue) {
|
b_queue_iterator it = {0};
|
||||||
struct ivy_token *next = lex->lex_queue->t_next;
|
b_queue_iterator_begin(&lex->lex_queue, &it);
|
||||||
ivy_token_destroy(lex->lex_queue);
|
|
||||||
lex->lex_queue = next;
|
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) {
|
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)
|
static enum ivy_status push_token(struct ivy_lexer *lex, struct ivy_token *tok)
|
||||||
{
|
{
|
||||||
struct ivy_token **slot = &lex->lex_queue;
|
b_queue_push_back(&lex->lex_queue, &tok->t_entry);
|
||||||
|
|
||||||
while (*slot) {
|
|
||||||
slot = &(*slot)->t_next;
|
|
||||||
}
|
|
||||||
|
|
||||||
*slot = tok;
|
|
||||||
lex->lex_prev_token = tok->t_type;
|
|
||||||
return IVY_OK;
|
return IVY_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1017,7 +1013,7 @@ struct ivy_token *ivy_lexer_peek(struct ivy_lexer *lex)
|
|||||||
{
|
{
|
||||||
enum ivy_status status = IVY_OK;
|
enum ivy_status status = IVY_OK;
|
||||||
|
|
||||||
while (!lex->lex_queue) {
|
while (b_queue_empty(&lex->lex_queue)) {
|
||||||
status = pump_tokens(lex);
|
status = pump_tokens(lex);
|
||||||
|
|
||||||
if (status != IVY_OK) {
|
if (status != IVY_OK) {
|
||||||
@@ -1027,7 +1023,9 @@ struct ivy_token *ivy_lexer_peek(struct ivy_lexer *lex)
|
|||||||
}
|
}
|
||||||
|
|
||||||
lex->lex_status = status;
|
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;
|
return tok;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1035,7 +1033,7 @@ struct ivy_token *ivy_lexer_read(struct ivy_lexer *lex)
|
|||||||
{
|
{
|
||||||
enum ivy_status status = IVY_OK;
|
enum ivy_status status = IVY_OK;
|
||||||
|
|
||||||
while (!lex->lex_queue) {
|
while (b_queue_empty(&lex->lex_queue)) {
|
||||||
status = pump_tokens(lex);
|
status = pump_tokens(lex);
|
||||||
|
|
||||||
if (status != IVY_OK) {
|
if (status != IVY_OK) {
|
||||||
@@ -1043,15 +1041,15 @@ struct ivy_token *ivy_lexer_read(struct ivy_lexer *lex)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ivy_token *tok = lex->lex_queue;
|
b_queue_entry *entry = b_queue_pop_front(&lex->lex_queue);
|
||||||
lex->lex_queue = lex->lex_queue->t_next;
|
struct ivy_token *tok = b_unbox(struct ivy_token, entry, t_entry);
|
||||||
return tok;
|
return tok;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ivy_lexer_tokens_available(struct ivy_lexer *lex)
|
bool ivy_lexer_tokens_available(struct ivy_lexer *lex)
|
||||||
{
|
{
|
||||||
if (lex->lex_queue) {
|
if (!b_queue_empty(&lex->lex_queue)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ struct ivy_lexer {
|
|||||||
b_dict *lex_keywords;
|
b_dict *lex_keywords;
|
||||||
enum ivy_status lex_status;
|
enum ivy_status lex_status;
|
||||||
|
|
||||||
struct ivy_token *lex_queue;
|
b_queue lex_queue;
|
||||||
enum ivy_token_type lex_prev_token;
|
enum ivy_token_type lex_prev_token;
|
||||||
|
|
||||||
b_string *lex_temp;
|
b_string *lex_temp;
|
||||||
|
|||||||
Reference in New Issue
Block a user