lang: update bluelib api usage

This commit is contained in:
2025-11-06 10:38:32 +00:00
parent b26c37c349
commit 4386965cd9
40 changed files with 351 additions and 271 deletions

View File

@@ -2,9 +2,9 @@
#include <blue/core/hash.h>
#include <blue/core/queue.h>
#include <blue/object/dict.h>
#include <blue/object/number.h>
#include <blue/object/string.h>
#include <blue/ds/dict.h>
#include <blue/ds/number.h>
#include <blue/ds/string.h>
#include <ctype.h>
#include <ivy/diag.h>
#include <ivy/lang/diag.h>
@@ -17,10 +17,7 @@
#define LINEBUF_DEFAULT_CAPACITY 1024
#define LEX_TOKEN_DEF(i, n) \
{ \
.id = (i), .name = (n) \
}
#define LEX_TOKEN_DEF(i, n) {.id = (i), .name = (n)}
static struct lex_token_def keywords[] = {
LEX_TOKEN_DEF(IVY_KW_PACKAGE, "package"),
@@ -154,27 +151,31 @@ static struct lexer_state *get_lexer_state(struct ivy_lexer *lex)
static void destroy_state_stack(b_queue *state)
{
b_queue_iterator it;
b_queue_iterator_begin(state, &it);
while (b_queue_iterator_is_valid(&it)) {
b_queue_entry *entry = b_queue_first(state);
while (entry) {
struct lexer_state *node
= b_unbox(struct lexer_state, it.entry, s_entry);
b_queue_iterator_erase(&it);
= b_unbox(struct lexer_state, entry, s_entry);
b_queue_entry *next = b_queue_next(entry);
b_queue_delete(state, entry);
free(node);
entry = next;
}
}
static struct ivy_lexer_symbol_node *get_symbol_node(
struct ivy_lexer_symbol_node *node, char c)
{
b_queue_iterator it;
b_queue_foreach (&it, &node->s_children) {
struct ivy_lexer_symbol_node *child = b_unbox(
struct ivy_lexer_symbol_node, it.entry, s_entry);
b_queue_entry *entry = b_queue_first(&node->s_children);
while (entry) {
struct ivy_lexer_symbol_node *child
= b_unbox(struct ivy_lexer_symbol_node, entry, s_entry);
if (child->s_char == c) {
return child;
}
entry = b_queue_next(entry);
}
return NULL;
@@ -221,14 +222,16 @@ static enum ivy_status put_symbol(
static void destroy_symbol_tree(struct ivy_lexer_symbol_node *tree)
{
b_queue_iterator it;
b_queue_iterator_begin(&tree->s_children, &it);
while (b_queue_iterator_is_valid(&it)) {
struct ivy_lexer_symbol_node *node = b_unbox(
struct ivy_lexer_symbol_node, it.entry, s_entry);
b_queue_iterator_erase(&it);
b_queue_entry *entry = b_queue_first(&tree->s_children);
while (entry) {
struct ivy_lexer_symbol_node *node
= b_unbox(struct ivy_lexer_symbol_node, entry, s_entry);
b_queue_entry *next = b_queue_next(entry);
b_queue_delete(&tree->s_children, entry);
destroy_symbol_tree(node);
entry = next;
}
free(tree);
@@ -267,7 +270,7 @@ static void init_keywords(b_dict *keyword_dict)
static enum ivy_keyword find_keyword_by_name(struct ivy_lexer *lex, const char *s)
{
b_number *id = B_NUMBER(b_dict_at(lex->lex_keywords, s));
b_number *id = b_dict_at(lex->lex_keywords, s);
if (!id) {
return IVY_KW_NONE;
}
@@ -312,14 +315,16 @@ enum ivy_status ivy_lexer_create(struct ivy_lexer **lexp)
void ivy_lexer_destroy(struct ivy_lexer *lex)
{
b_queue_iterator it = {0};
b_queue_iterator_begin(&lex->lex_queue, &it);
b_queue_entry *entry = b_queue_first(&lex->lex_queue);
while (entry) {
struct ivy_token *tok = b_unbox(struct ivy_token, entry, t_entry);
b_queue_entry *next = b_queue_next(entry);
b_queue_delete(&lex->lex_queue, entry);
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);
entry = next;
}
if (lex->lex_linebuf) {
@@ -331,11 +336,11 @@ void ivy_lexer_destroy(struct ivy_lexer *lex)
}
if (lex->lex_temp) {
b_string_release(lex->lex_temp);
b_string_unref(lex->lex_temp);
}
if (lex->lex_keywords) {
b_dict_release(lex->lex_keywords);
b_dict_unref(lex->lex_keywords);
}
destroy_state_stack(&lex->lex_state);