diff --git a/lang/CMakeLists.txt b/lang/CMakeLists.txt index a77fc60..eba1caf 100644 --- a/lang/CMakeLists.txt +++ b/lang/CMakeLists.txt @@ -11,5 +11,5 @@ else () endif () target_include_directories(ivy-lang PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/) -target_link_libraries(ivy-lang mie ivy-diag ivy-common Bluelib::Core Bluelib::Object Bluelib::Term) +target_link_libraries(ivy-lang mie ivy-diag ivy-common Bluelib::Core Bluelib::Ds Bluelib::Term) target_compile_definitions(ivy-lang PRIVATE IVY_EXPORT=1 IVY_STATIC=${IVY_STATIC}) diff --git a/lang/ast/atom.c b/lang/ast/atom.c index eb0e401..6ccf9e6 100644 --- a/lang/ast/atom.c +++ b/lang/ast/atom.c @@ -1,14 +1,15 @@ #include "ctx.h" #include "node.h" -#include +#include static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_atom_node *v = (struct ivy_ast_atom_node *)node; - b_string_append_cstrf(str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), - v->n_content->t_str); + b_string_append_cstrf( + str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), + v->n_content->t_str); } struct ast_node_type atom_node_ops = { diff --git a/lang/ast/block.c b/lang/ast/block.c index b85eda8..1ac741e 100644 --- a/lang/ast/block.c +++ b/lang/ast/block.c @@ -5,7 +5,7 @@ #include "iterate.h" #include "node.h" -#include +#include #include void block_add_terminator(struct block_parser_state *state, unsigned short tok) @@ -143,11 +143,12 @@ static void collect_children( struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) { struct ivy_ast_block_node *block = (struct ivy_ast_block_node *)node; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &block->n_expr) { + b_queue_entry *entry = b_queue_first(&block->n_expr); + while (entry) { struct ivy_ast_node *expr - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, expr); + entry = b_queue_next(entry); } } diff --git a/lang/ast/cascade.c b/lang/ast/cascade.c index 99dbdab..323f746 100644 --- a/lang/ast/cascade.c +++ b/lang/ast/cascade.c @@ -11,11 +11,12 @@ static void collect_children( ast_node_iterator_enqueue_node(iterator, node, cascade->n_recipient); } - b_queue_iterator it = {0}; - b_queue_foreach (&it, &cascade->n_msg) { + b_queue_entry *entry = b_queue_first(&cascade->n_msg); + while (entry) { struct ivy_ast_node *arg - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, arg); + entry = b_queue_next(entry); } } diff --git a/lang/ast/class.c b/lang/ast/class.c index 6d72d4f..457142a 100644 --- a/lang/ast/class.c +++ b/lang/ast/class.c @@ -2,7 +2,7 @@ #include "iterate.h" #include "node.h" -#include +#include #include #include @@ -162,17 +162,20 @@ static void collect_children( struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) { struct ivy_ast_class_node *c = (struct ivy_ast_class_node *)node; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &c->n_properties) { + b_queue_entry *entry = b_queue_first(&c->n_properties); + while (entry) { struct ivy_ast_node *child - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, child); + entry = b_queue_next(entry); } - b_queue_foreach (&it, &c->n_msg_handlers) { + entry = b_queue_first(&c->n_msg_handlers); + while (entry) { struct ivy_ast_node *child - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, child); + entry = b_queue_next(entry); } } diff --git a/lang/ast/cond.c b/lang/ast/cond.c index 72628b6..58e303d 100644 --- a/lang/ast/cond.c +++ b/lang/ast/cond.c @@ -339,11 +339,12 @@ static void cond_group_collect_children( struct ivy_ast_cond_group_node *group = (struct ivy_ast_cond_group_node *)node; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &group->n_branches) { + b_queue_entry *entry = b_queue_first(&group->n_branches); + while (entry) { struct ivy_ast_node *branch - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, branch); + entry = b_queue_next(entry); } } diff --git a/lang/ast/const.c b/lang/ast/const.c index 2215e91..500c8e0 100644 --- a/lang/ast/const.c +++ b/lang/ast/const.c @@ -1,8 +1,6 @@ #include "ctx.h" #include "node.h" -#include - struct ast_node_type c_true_node_ops = { .n_state_size = sizeof(struct parser_state), .n_node_size = sizeof(struct ivy_ast_node), diff --git a/lang/ast/ctx.c b/lang/ast/ctx.c index 32b2b0f..68beade 100644 --- a/lang/ast/ctx.c +++ b/lang/ast/ctx.c @@ -44,12 +44,13 @@ void ivy_parser_destroy(struct ivy_parser *parser, struct ivy_ast_node **result) { struct ivy_ast_node *root = NULL; - b_queue_iterator it; - b_queue_iterator_begin(&parser->p_state, &it); - while (b_queue_iterator_is_valid(&it)) { + b_queue_entry *entry = b_queue_first(&parser->p_state); + ; + while (entry) { struct parser_state *state - = b_unbox(struct parser_state, it.entry, s_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct parser_state, entry, s_entry); + b_queue_entry *next = b_queue_next(entry); + b_queue_delete(&parser->p_state, entry); if (root) { ivy_ast_node_destroy(root); @@ -57,6 +58,7 @@ void ivy_parser_destroy(struct ivy_parser *parser, struct ivy_ast_node **result) root = state->s_node; free(state); + entry = next; } if (result) { diff --git a/lang/ast/double.c b/lang/ast/double.c index 80a7244..c9664d5 100644 --- a/lang/ast/double.c +++ b/lang/ast/double.c @@ -1,7 +1,7 @@ #include "ctx.h" #include "node.h" -#include +#include static void to_string(struct ivy_ast_node *node, b_string *str) { diff --git a/lang/ast/expr/arith.c b/lang/ast/expr/arith.c index 7368044..0e86906 100644 --- a/lang/ast/expr/arith.c +++ b/lang/ast/expr/arith.c @@ -3,7 +3,7 @@ #include "../node.h" #include "expr.h" -#include +#include #include #include #include @@ -296,7 +296,7 @@ enum ivy_status expr_finalise_arith( struct expr_parser_state *state, struct ivy_ast_node **expr_tree, enum ivy_operator_precedence minimum_precedence) { - b_queue_iterator it = {0}; + b_queue_entry *entry = NULL; /* first, take all the operators still left on the operator stack and * add them to the output queue. @@ -304,7 +304,7 @@ enum ivy_status expr_finalise_arith( * since each set of parentheses has its own expression context, * we don't have to handle parentheses here */ while (true) { - b_queue_entry *entry = b_queue_pop_back(&state->s_operator_stack); + entry = b_queue_pop_back(&state->s_operator_stack); if (!entry) { break; } @@ -355,20 +355,21 @@ enum ivy_status expr_finalise_arith( b_queue q = B_QUEUE_INIT; b_queue_entry *tmp = NULL; - b_queue_iterator_begin(&state->s_output_queue, &it); + entry = b_queue_first(&state->s_output_queue); int i = 0; - while (b_queue_iterator_is_valid(&it)) { + while (entry) { struct ivy_ast_node *item - = b_unbox(struct ivy_ast_node, it.entry, n_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct ivy_ast_node, entry, n_entry); + b_queue_entry *next = b_queue_next(entry); + b_queue_delete(&state->s_output_queue, entry); /* if the node is an operand, just push it to a temporary queue * and come back to it later. */ if (item->n_type != IVY_AST_OP && item->n_type != IVY_AST_MSG) { /* operand */ b_queue_push_back(&q, &item->n_entry); - continue; + goto next; } const struct ivy_operator *op = NULL; @@ -388,7 +389,7 @@ enum ivy_status expr_finalise_arith( struct ivy_ast_node, tmp, n_entry); } b_queue_push_back(&q, &msg->n_base.n_entry); - continue; + goto next; } struct ivy_ast_op_node *op_node = (struct ivy_ast_op_node *)item; @@ -397,7 +398,7 @@ enum ivy_status expr_finalise_arith( * queue as-is */ if (op_node_is_complete(op_node)) { b_queue_push_back(&q, &item->n_entry); - continue; + goto next; } /* otherwise, pop the relevant operands from the operand @@ -432,6 +433,8 @@ enum ivy_status expr_finalise_arith( /* ...and push the newly-completed operator node to the operand * queue */ b_queue_push_back(&q, &op_node->n_base.n_entry); + next: + entry = next; } #if 0 @@ -445,13 +448,15 @@ enum ivy_status expr_finalise_arith( * their operands have just been moved to the temporary operand stack * used above. move them back to the parser state's output queue here * so they can be used later. */ - b_queue_foreach (&it, &state->s_operator_stack) { - b_queue_entry *entry = b_queue_pop_front(&q); - if (!entry) { + entry = b_queue_first(&state->s_operator_stack); + while (entry) { + b_queue_entry *entry2 = b_queue_pop_front(&q); + if (!entry2) { return IVY_ERR_INTERNAL_FAILURE; } - b_queue_push_back(&state->s_output_queue, entry); + b_queue_push_back(&state->s_output_queue, entry2); + entry = b_queue_next(entry); } #if 0 @@ -908,14 +913,17 @@ static struct ivy_ast_selector_node *keyword_selector_from_label_list(b_queue *l struct ivy_ast_selector_node *sel = (struct ivy_ast_selector_node *)ast_node_create(IVY_AST_SELECTOR); - b_queue_iterator it = {0}; + b_queue_entry *entry = b_queue_first(labels); + b_queue_entry *next = NULL; - b_queue_iterator_begin(labels, &it); - while (b_queue_iterator_is_valid(&it)) { + while (entry) { struct ivy_token *label - = b_unbox(struct ivy_token, it.entry, t_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct ivy_token, entry, t_entry); + next = b_queue_next(entry); + b_queue_delete(labels, entry); + b_queue_push_back(&sel->n_arg_labels, &label->t_entry); + entry = next; } return sel; @@ -929,13 +937,17 @@ static struct ivy_ast_cascade_node *expr_finalise_cascade( cascade->n_recipient = state->s_recipient; - b_queue_iterator it = {0}; - b_queue_iterator_begin(&state->s_cascade_msg, &it); - while (b_queue_iterator_is_valid(&it)) { + b_queue_entry *entry = b_queue_first(&state->s_cascade_msg); + b_queue_entry *next = NULL; + + while (entry) { struct ivy_ast_node *msg - = b_unbox(struct ivy_ast_node, it.entry, n_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct ivy_ast_node, entry, n_entry); + next = b_queue_next(entry); + b_queue_delete(&state->s_cascade_msg, entry); + b_queue_push_back(&cascade->n_msg, &msg->n_entry); + entry = next; } return cascade; @@ -950,15 +962,17 @@ static struct ivy_ast_msg_node *expr_finalise_keyword_msg( msg->n_recipient = state->s_recipient; msg->n_sel = keyword_selector_from_label_list(&state->s_labels); - b_queue_iterator it = {0}; + b_queue_entry *entry = b_queue_first(&state->s_args); + b_queue_entry *next = NULL; - b_queue_iterator_begin(&state->s_args, &it); - while (b_queue_iterator_is_valid(&it)) { + while (entry) { struct ivy_ast_node *arg - = b_unbox(struct ivy_ast_node, it.entry, n_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct ivy_ast_node, entry, n_entry); + next = b_queue_next(entry); + b_queue_delete(&state->s_args, entry); b_queue_push_back(&msg->n_arg, &arg->n_entry); + entry = next; } return msg; @@ -974,22 +988,28 @@ static struct ivy_ast_msg_node *expr_finalise_complex_msg( state->s_msg = NULL; - b_queue_iterator it = {0}; - b_queue_iterator_begin(&state->s_labels, &it); - while (b_queue_iterator_is_valid(&it)) { + b_queue_entry *entry = b_queue_first(&state->s_labels); + b_queue_entry *next = NULL; + while (entry) { struct ivy_token *label - = b_unbox(struct ivy_token, it.entry, t_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct ivy_token, entry, t_entry); + next = b_queue_next(entry); + b_queue_delete(&state->s_labels, entry); b_queue_push_back(&msg->n_sel->n_arg_labels, &label->t_entry); + + entry = next; } - b_queue_iterator_begin(&state->s_args, &it); - while (b_queue_iterator_is_valid(&it)) { + entry = b_queue_first(&state->s_args); + while (entry) { struct ivy_ast_node *arg - = b_unbox(struct ivy_ast_node, it.entry, n_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct ivy_ast_node, entry, n_entry); + next = b_queue_next(entry); + b_queue_delete(&state->s_args, entry); b_queue_push_back(&msg->n_arg, &arg->n_entry); + + entry = next; } return msg; diff --git a/lang/ast/expr/stmt.c b/lang/ast/expr/stmt.c index c352341..290f463 100644 --- a/lang/ast/expr/stmt.c +++ b/lang/ast/expr/stmt.c @@ -1,7 +1,7 @@ #include "../node.h" #include "expr.h" -#include +#include #include #include #include diff --git a/lang/ast/ident.c b/lang/ast/ident.c index 2ed12c3..1922975 100644 --- a/lang/ast/ident.c +++ b/lang/ast/ident.c @@ -1,14 +1,15 @@ #include "ctx.h" #include "node.h" -#include +#include static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_ident_node *ident = (struct ivy_ast_ident_node *)node; - b_string_append_cstrf(str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), - ident->n_content->t_str); + b_string_append_cstrf( + str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), + ident->n_content->t_str); } struct ast_node_type ident_node_ops = { diff --git a/lang/ast/int.c b/lang/ast/int.c index 3849d35..1ac1626 100644 --- a/lang/ast/int.c +++ b/lang/ast/int.c @@ -1,14 +1,15 @@ #include "ctx.h" #include "node.h" -#include +#include static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_int_node *v = (struct ivy_ast_int_node *)node; - b_string_append_cstrf(str, "%s (%llu)", ivy_ast_node_type_to_string(node->n_type), - v->n_value->t_int); + b_string_append_cstrf( + str, "%s (%llu)", ivy_ast_node_type_to_string(node->n_type), + v->n_value->t_int); } struct ast_node_type int_node_ops = { diff --git a/lang/ast/iterate.c b/lang/ast/iterate.c index 6036a95..05a8d36 100644 --- a/lang/ast/iterate.c +++ b/lang/ast/iterate.c @@ -63,12 +63,11 @@ enum ivy_status iterate_postorder( b_queue_push_back(&it->it_queue, &node->n_it.it_entry); node->n_it.it_depth = 0; - b_queue_iterator q_it; - b_queue_iterator_begin(&it->it_queue, &q_it); + b_queue_entry *entry = b_queue_first(&it->it_queue); - while (b_queue_iterator_is_valid(&q_it)) { + while (entry) { struct ivy_ast_node_iterator_entry *it_entry = b_unbox( - struct ivy_ast_node_iterator_entry, q_it.entry, it_entry); + struct ivy_ast_node_iterator_entry, entry, it_entry); node = b_unbox(struct ivy_ast_node, it_entry, n_it); if (!node) { @@ -78,11 +77,11 @@ enum ivy_status iterate_postorder( const struct ast_node_type *type = get_ast_node_type(node->n_type); if (type->n_collect_children) { - it->it_insert_after = q_it.entry; + it->it_insert_after = entry; type->n_collect_children(node, it); } - b_queue_iterator_next(&q_it); + entry = b_queue_next(entry); } while (!b_queue_empty(&it->it_queue)) { @@ -111,12 +110,11 @@ enum ivy_status ivy_ast_node_iterate( b_queue_push_back(&it->it_queue, &node->n_it.it_entry); node->n_it.it_depth = 0; - b_queue_iterator q_it; - b_queue_iterator_begin(&it->it_queue, &q_it); + b_queue_entry *entry = b_queue_first(&it->it_queue); - while (b_queue_iterator_is_valid(&q_it)) { + while (entry) { struct ivy_ast_node_iterator_entry *it_entry = b_unbox( - struct ivy_ast_node_iterator_entry, q_it.entry, it_entry); + struct ivy_ast_node_iterator_entry, entry, it_entry); node = b_unbox(struct ivy_ast_node, it_entry, n_it); if (!node) { @@ -132,11 +130,11 @@ enum ivy_status ivy_ast_node_iterate( const struct ast_node_type *type = get_ast_node_type(node->n_type); if (type->n_collect_children) { - it->it_insert_after = q_it.entry; + it->it_insert_after = entry; type->n_collect_children(node, it); } - b_queue_iterator_next(&q_it); + entry = b_queue_next(entry); } while (!b_queue_empty(&it->it_queue)) { diff --git a/lang/ast/lambda.c b/lang/ast/lambda.c index 979974e..a509657 100644 --- a/lang/ast/lambda.c +++ b/lang/ast/lambda.c @@ -147,11 +147,12 @@ static void collect_children( struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) { struct ivy_ast_lambda_node *lambda = (struct ivy_ast_lambda_node *)node; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &lambda->n_arg) { + b_queue_entry *entry = b_queue_first(&lambda->n_arg); + while (entry) { struct ivy_ast_node *expr - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, expr); + entry = b_queue_next(entry); } if (lambda->n_body) { diff --git a/lang/ast/loop-control.c b/lang/ast/loop-control.c index 59e16e1..ea2f795 100644 --- a/lang/ast/loop-control.c +++ b/lang/ast/loop-control.c @@ -1,7 +1,7 @@ #include "ctx.h" #include "node.h" -#include +#include static void to_string(struct ivy_ast_node *node, b_string *str) { diff --git a/lang/ast/match.c b/lang/ast/match.c index d98de0a..0295e7e 100644 --- a/lang/ast/match.c +++ b/lang/ast/match.c @@ -262,10 +262,10 @@ static void match_collect_children( ast_node_iterator_enqueue_node(iterator, node, match->n_cond); - b_queue_iterator it = {0}; - b_queue_foreach (&it, &match->n_branches) { + b_queue_entry *entry = b_queue_first(&match->n_branches); + while (entry) { struct ivy_ast_node *branch - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, branch); } } diff --git a/lang/ast/msg.c b/lang/ast/msg.c index a8c215c..343d635 100644 --- a/lang/ast/msg.c +++ b/lang/ast/msg.c @@ -16,11 +16,12 @@ static void collect_children( iterator, node, (struct ivy_ast_node *)msg->n_sel); } - b_queue_iterator it = {0}; - b_queue_foreach (&it, &msg->n_arg) { + b_queue_entry *entry = b_queue_first(&msg->n_arg); + while (entry) { struct ivy_ast_node *arg - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, arg); + entry = b_queue_next(entry); } } diff --git a/lang/ast/msgh.c b/lang/ast/msgh.c index 380c5e2..5c1bc14 100644 --- a/lang/ast/msgh.c +++ b/lang/ast/msgh.c @@ -4,7 +4,7 @@ #include "ivy/status.h" #include "node.h" -#include +#include #include struct msgh_parser_state { diff --git a/lang/ast/node.c b/lang/ast/node.c index e451693..2cd7408 100644 --- a/lang/ast/node.c +++ b/lang/ast/node.c @@ -1,6 +1,6 @@ #include "node.h" -#include +#include #include #include #include @@ -250,7 +250,7 @@ struct ivy_ast_node *ast_node_create(enum ivy_ast_node_type type) return node; } -void ivy_ast_node_to_string(struct ivy_ast_node *node, struct b_string *out) +void ivy_ast_node_to_string(struct ivy_ast_node *node, b_string *out) { const struct ast_node_type *type_info = get_ast_node_type(node->n_type); if (!type_info) { @@ -304,12 +304,11 @@ void ivy_ast_node_destroy(struct ivy_ast_node *node) b_queue_push_back(&it.it_queue, &node->n_it.it_entry); node->n_it.it_depth = 0; - b_queue_iterator q_it; - b_queue_iterator_begin(&it.it_queue, &q_it); + b_queue_entry *entry = b_queue_first(&it.it_queue); - while (b_queue_iterator_is_valid(&q_it)) { + while (entry) { struct ivy_ast_node_iterator_entry *it_entry = b_unbox( - struct ivy_ast_node_iterator_entry, q_it.entry, it_entry); + struct ivy_ast_node_iterator_entry, entry, it_entry); node = b_unbox(struct ivy_ast_node, it_entry, n_it); if (!node) { @@ -319,11 +318,11 @@ void ivy_ast_node_destroy(struct ivy_ast_node *node) const struct ast_node_type *type = get_ast_node_type(node->n_type); if (type->n_collect_children) { - it.it_insert_after = q_it.entry; + it.it_insert_after = entry; type->n_collect_children(node, &it); } - b_queue_iterator_next(&q_it); + entry = b_queue_next(entry); } while (!b_queue_empty(&it.it_queue)) { diff --git a/lang/ast/node.h b/lang/ast/node.h index 7b1a5d2..2b4b1b3 100644 --- a/lang/ast/node.h +++ b/lang/ast/node.h @@ -1,6 +1,7 @@ #ifndef _AST_NODE_H_ #define _AST_NODE_H_ +#include #include #include @@ -46,7 +47,7 @@ typedef struct token_parse_result (*token_parse_function)( struct ast_node_type { enum ivy_status (*n_add_child)( struct parser_state *, struct ivy_ast_node *); - void (*n_to_string)(struct ivy_ast_node *, struct b_string *); + void (*n_to_string)(struct ivy_ast_node *, b_string *); void (*n_init_state)(struct ivy_parser *, struct parser_state *, uintptr_t); void (*n_collect_children)( struct ivy_ast_node *, struct ivy_ast_node_iterator *); diff --git a/lang/ast/op.c b/lang/ast/op.c index f1b487c..96bfb9f 100644 --- a/lang/ast/op.c +++ b/lang/ast/op.c @@ -1,13 +1,16 @@ #include "ctx.h" -#include "node.h" #include "iterate.h" -#include +#include "node.h" + +#include static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_op_node *op = (struct ivy_ast_op_node *)node; - b_string_append_cstrf(str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), ivy_operator_id_to_string(op->n_op->op_id)); + b_string_append_cstrf( + str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), + ivy_operator_id_to_string(op->n_op->op_id)); } static void collect_children( diff --git a/lang/ast/package.c b/lang/ast/package.c index 52be940..b680ca1 100644 --- a/lang/ast/package.c +++ b/lang/ast/package.c @@ -4,8 +4,8 @@ #include "iterate.h" #include "node.h" +#include #include -#include enum package_type { PACKAGE_NONE = 0, @@ -34,9 +34,13 @@ struct package_parser_state { struct ivy_ast_node *s_cond; }; -static enum ivy_status add_package_item(struct package_parser_state *state, struct ivy_ast_node *index, struct ivy_ast_node *value) +static enum ivy_status add_package_item( + struct package_parser_state *state, struct ivy_ast_node *index, + struct ivy_ast_node *value) { - struct ivy_ast_pkg_static_item_node *item = (struct ivy_ast_pkg_static_item_node *)ast_node_create(IVY_AST_PKG_ITEM); + struct ivy_ast_pkg_static_item_node *item + = (struct ivy_ast_pkg_static_item_node *)ast_node_create( + IVY_AST_PKG_ITEM); if (!item) { return IVY_ERR_NO_MEMORY; } @@ -64,7 +68,8 @@ static struct ivy_ast_node *finalise_package(struct package_parser_state *state) switch (state->s_type) { case PACKAGE_STATIC: - s = (struct ivy_ast_pkg_static_node *)ast_node_create(IVY_AST_PKG_STATIC); + s = (struct ivy_ast_pkg_static_node *)ast_node_create( + IVY_AST_PKG_STATIC); if (!s) { return NULL; @@ -74,7 +79,8 @@ static struct ivy_ast_node *finalise_package(struct package_parser_state *state) state->s_items = B_QUEUE_INIT; return (struct ivy_ast_node *)s; case PACKAGE_DYNAMIC: - d = (struct ivy_ast_pkg_dynamic_node *)ast_node_create(IVY_AST_PKG_DYNAMIC); + d = (struct ivy_ast_pkg_dynamic_node *)ast_node_create( + IVY_AST_PKG_DYNAMIC); if (!d) { return NULL; } @@ -98,7 +104,8 @@ static struct ivy_ast_node *finalise_package(struct package_parser_state *state) static struct token_parse_result parse_equal_right_angle( struct ivy_parser *ctx, struct ivy_token *tok) { - struct package_parser_state *state = parser_get_state(ctx, struct package_parser_state); + struct package_parser_state *state + = parser_get_state(ctx, struct package_parser_state); if (state->s_type == PACKAGE_NONE) { state->s_type = PACKAGE_STATIC; @@ -123,7 +130,9 @@ static struct token_parse_result parse_equal_right_angle( state->s_next_index = state->s_prev_node; state->s_prev_node = NULL; - struct expr_parser_state *expr = (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR, 0); + struct expr_parser_state *expr + = (struct expr_parser_state *)parser_push_state( + ctx, IVY_AST_EXPR, 0); expr_add_terminator(expr, IVY_SYM_COMMA); expr->s_subexpr_depth = 1; @@ -133,7 +142,8 @@ static struct token_parse_result parse_equal_right_angle( static struct token_parse_result parse_comma( struct ivy_parser *ctx, struct ivy_token *tok) { - struct package_parser_state *state = parser_get_state(ctx, struct package_parser_state); + struct package_parser_state *state + = parser_get_state(ctx, struct package_parser_state); if (state->s_type == PACKAGE_NONE) { state->s_type = PACKAGE_STATIC; @@ -144,7 +154,8 @@ static struct token_parse_result parse_comma( return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } - if (state->s_prev != IVY_SYM_LEFT_BRACE && state->s_prev != IVY_SYM_COMMA && state->s_prev != IVY_SYM_EQUAL_RIGHT_ANGLE) { + if (state->s_prev != IVY_SYM_LEFT_BRACE && state->s_prev != IVY_SYM_COMMA + && state->s_prev != IVY_SYM_EQUAL_RIGHT_ANGLE) { /* token unexpected at this time. */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } @@ -154,14 +165,17 @@ static struct token_parse_result parse_comma( return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } - enum ivy_status status = add_package_item(state, state->s_next_index, state->s_prev_node); + enum ivy_status status = add_package_item( + state, state->s_next_index, state->s_prev_node); if (status != IVY_OK) { return PARSE_RESULT(status, 0); } state->s_prev = IVY_SYM_COMMA; - struct expr_parser_state *expr = (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR, 0); + struct expr_parser_state *expr + = (struct expr_parser_state *)parser_push_state( + ctx, IVY_AST_EXPR, 0); expr_add_terminator(expr, IVY_SYM_EQUAL_RIGHT_ANGLE); expr->s_subexpr_depth = 1; @@ -171,7 +185,8 @@ static struct token_parse_result parse_comma( static struct token_parse_result parse_right_brace( struct ivy_parser *ctx, struct ivy_token *tok) { - struct package_parser_state *state = parser_get_state(ctx, struct package_parser_state); + struct package_parser_state *state + = parser_get_state(ctx, struct package_parser_state); if (state->s_type == PACKAGE_NONE) { state->s_type = PACKAGE_STATIC; @@ -181,13 +196,15 @@ static struct token_parse_result parse_right_brace( switch (state->s_type) { case PACKAGE_STATIC: - if (state->s_prev != IVY_SYM_LEFT_BRACE && state->s_prev != IVY_SYM_COMMA && state->s_prev != IVY_SYM_EQUAL_RIGHT_ANGLE) { + if (state->s_prev != IVY_SYM_LEFT_BRACE && state->s_prev != IVY_SYM_COMMA + && state->s_prev != IVY_SYM_EQUAL_RIGHT_ANGLE) { /* token unexpected at this time. */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } if (state->s_prev_node) { - status = add_package_item(state, state->s_next_index, state->s_prev_node); + status = add_package_item( + state, state->s_next_index, state->s_prev_node); } break; @@ -235,7 +252,8 @@ static struct token_parse_result parse_right_brace( static struct token_parse_result parse_for( struct ivy_parser *ctx, struct ivy_token *tok) { - struct package_parser_state *state = parser_get_state(ctx, struct package_parser_state); + struct package_parser_state *state + = parser_get_state(ctx, struct package_parser_state); if (state->s_type == PACKAGE_NONE) { state->s_type = PACKAGE_DYNAMIC; @@ -260,7 +278,9 @@ static struct token_parse_result parse_for( state->s_transform = state->s_prev_node; state->s_prev_node = NULL; - struct expr_parser_state *expr = (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR, 0); + struct expr_parser_state *expr + = (struct expr_parser_state *)parser_push_state( + ctx, IVY_AST_EXPR, 0); expr_add_terminator(expr, IVY_KW_IN); expr->s_subexpr_depth = 1; @@ -270,7 +290,8 @@ static struct token_parse_result parse_for( static struct token_parse_result parse_in( struct ivy_parser *ctx, struct ivy_token *tok) { - struct package_parser_state *state = parser_get_state(ctx, struct package_parser_state); + struct package_parser_state *state + = parser_get_state(ctx, struct package_parser_state); if (state->s_type != PACKAGE_DYNAMIC) { /* this token cannot be used in this context. */ @@ -291,7 +312,9 @@ static struct token_parse_result parse_in( state->s_item = state->s_prev_node; state->s_prev_node = NULL; - struct expr_parser_state *expr = (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR, 0); + struct expr_parser_state *expr + = (struct expr_parser_state *)parser_push_state( + ctx, IVY_AST_EXPR, 0); expr_add_terminator(expr, IVY_KW_IF); expr->s_subexpr_depth = 1; @@ -301,7 +324,8 @@ static struct token_parse_result parse_in( static struct token_parse_result parse_if( struct ivy_parser *ctx, struct ivy_token *tok) { - struct package_parser_state *state = parser_get_state(ctx, struct package_parser_state); + struct package_parser_state *state + = parser_get_state(ctx, struct package_parser_state); if (state->s_type != PACKAGE_DYNAMIC) { /* this token cannot be used in this context. */ @@ -322,7 +346,9 @@ static struct token_parse_result parse_if( state->s_source = state->s_prev_node; state->s_prev_node = NULL; - struct expr_parser_state *expr = (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR, 0); + struct expr_parser_state *expr + = (struct expr_parser_state *)parser_push_state( + ctx, IVY_AST_EXPR, 0); expr_add_terminator(expr, IVY_SYM_RIGHT_BRACE); expr->s_subexpr_depth = 1; @@ -332,8 +358,7 @@ static struct token_parse_result parse_if( static enum ivy_status add_child( struct parser_state *parent, struct ivy_ast_node *child) { - struct package_parser_state *state - = (struct package_parser_state *)parent; + struct package_parser_state *state = (struct package_parser_state *)parent; if (state->s_prev_node) { return IVY_ERR_BAD_SYNTAX; @@ -346,19 +371,22 @@ static enum ivy_status add_child( static void pkg_static_collect_children( struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) { - struct ivy_ast_pkg_static_node *pkg = (struct ivy_ast_pkg_static_node *)node; - - b_queue_iterator it = {0}; - b_queue_foreach (&it, &pkg->n_items) { - struct ivy_ast_node *item = b_unbox(struct ivy_ast_node, it.entry, n_entry); + struct ivy_ast_pkg_static_node *pkg + = (struct ivy_ast_pkg_static_node *)node; + + b_queue_entry *entry = b_queue_first(&pkg->n_items); + while (entry) { + struct ivy_ast_node *item + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, item); + entry = b_queue_next(entry); } } - static void pkg_static_item_to_string(struct ivy_ast_node *node, b_string *str) { - struct ivy_ast_pkg_static_item_node *item = (struct ivy_ast_pkg_static_item_node *)node; + struct ivy_ast_pkg_static_item_node *item + = (struct ivy_ast_pkg_static_item_node *)node; b_string_append_cstr(str, ivy_ast_node_type_to_string(node->n_type)); @@ -370,7 +398,8 @@ static void pkg_static_item_to_string(struct ivy_ast_node *node, b_string *str) static void pkg_static_item_collect_children( struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) { - struct ivy_ast_pkg_static_item_node *item = (struct ivy_ast_pkg_static_item_node *)node; + struct ivy_ast_pkg_static_item_node *item + = (struct ivy_ast_pkg_static_item_node *)node; if (item->n_index) { ast_node_iterator_enqueue_node(iterator, node, item->n_index); @@ -384,7 +413,8 @@ static void pkg_static_item_collect_children( static void pkg_dynamic_collect_children( struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) { - struct ivy_ast_pkg_dynamic_node *pkg = (struct ivy_ast_pkg_dynamic_node *)node; + struct ivy_ast_pkg_dynamic_node *pkg + = (struct ivy_ast_pkg_dynamic_node *)node; if (pkg->n_transform) { ast_node_iterator_enqueue_node(iterator, node, pkg->n_transform); @@ -410,7 +440,9 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_ state->s_prev = IVY_SYM_LEFT_BRACE; state->s_next_implicit_index = 0; - struct expr_parser_state *expr = (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR, 0); + struct expr_parser_state *expr + = (struct expr_parser_state *)parser_push_state( + ctx, IVY_AST_EXPR, 0); expr_add_terminator(expr, IVY_KW_FOR); expr->s_subexpr_depth = 1; } diff --git a/lang/ast/property.c b/lang/ast/property.c index f75adcc..3762d51 100644 --- a/lang/ast/property.c +++ b/lang/ast/property.c @@ -5,7 +5,7 @@ #include "ivy/status.h" #include "node.h" -#include +#include #include #include diff --git a/lang/ast/selector.c b/lang/ast/selector.c index 339fd59..77cd70b 100644 --- a/lang/ast/selector.c +++ b/lang/ast/selector.c @@ -2,7 +2,7 @@ #include "ivy/status.h" #include "node.h" -#include +#include #include #include @@ -252,22 +252,19 @@ static void to_string(struct ivy_ast_node *node, b_string *str) b_string_append_cstr(str, "("); } - b_queue_iterator label_it = {0}; - b_queue_iterator name_it = {0}; - - b_queue_iterator_begin(&sel->n_arg_labels, &label_it); - b_queue_iterator_begin(&sel->n_arg_names, &name_it); + b_queue_entry *label_entry = b_queue_first(&sel->n_arg_labels); + b_queue_entry *name_entry = b_queue_first(&sel->n_arg_names); int i = 0; - while (b_queue_iterator_is_valid(&label_it)) { + while (label_entry) { if (i > 0) { b_string_append_cstr(str, " "); } struct ivy_token *label, *name; - label = b_unbox(struct ivy_token, label_it.entry, t_entry); - name = b_unbox(struct ivy_token, name_it.entry, t_entry); + label = b_unbox(struct ivy_token, label_entry, t_entry); + name = b_unbox(struct ivy_token, name_entry, t_entry); if (label && label->t_type == IVY_TOK_LABEL && label->t_str) { b_string_append_cstrf(str, "%s:", label->t_str); @@ -283,8 +280,8 @@ static void to_string(struct ivy_ast_node *node, b_string *str) i++; } - b_queue_iterator_next(&label_it); - b_queue_iterator_next(&name_it); + label_entry = b_queue_next(label_entry); + name_entry = b_queue_next(name_entry); } if (sel->n_msg_name && !b_queue_empty(&sel->n_arg_labels)) { diff --git a/lang/ast/string.c b/lang/ast/string.c index d7e9e82..f5342fa 100644 --- a/lang/ast/string.c +++ b/lang/ast/string.c @@ -3,7 +3,7 @@ #include "iterate.h" #include "node.h" -#include +#include static void to_string(struct ivy_ast_node *node, b_string *str) { @@ -68,11 +68,12 @@ static void collect_children( struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) { struct ivy_ast_fstring_node *str = (struct ivy_ast_fstring_node *)node; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &str->n_value) { + b_queue_entry *entry = b_queue_first(&str->n_value); + while (entry) { struct ivy_ast_node *child - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, child); + entry = b_queue_next(entry); } } diff --git a/lang/ast/try.c b/lang/ast/try.c index 45f6ea7..28491fb 100644 --- a/lang/ast/try.c +++ b/lang/ast/try.c @@ -236,11 +236,12 @@ static void try_collect_children( ast_node_iterator_enqueue_node(iterator, node, try->n_try); - b_queue_iterator it = {0}; - b_queue_foreach (&it, &try->n_catch) { + b_queue_entry *entry = b_queue_first(&try->n_catch); + while (entry) { struct ivy_ast_node *catch - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, catch); + entry = b_queue_next(entry); } if (try->n_finally) { diff --git a/lang/ast/tuple.c b/lang/ast/tuple.c index 2e01cbd..0166f11 100644 --- a/lang/ast/tuple.c +++ b/lang/ast/tuple.c @@ -4,7 +4,7 @@ #include "ivy/status.h" #include "node.h" -#include +#include #include #include @@ -117,11 +117,12 @@ static void collect_children( { struct ivy_ast_tuple_node *tuple = (struct ivy_ast_tuple_node *)node; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &tuple->n_members) { + b_queue_entry *entry = b_queue_first(&tuple->n_members); + while (entry) { struct ivy_ast_node *item - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, item); + entry = b_queue_next(entry); } } diff --git a/lang/ast/unit-import.c b/lang/ast/unit-import.c index c2f8071..5a6f2b0 100644 --- a/lang/ast/unit-import.c +++ b/lang/ast/unit-import.c @@ -1,7 +1,7 @@ #include "ctx.h" #include "node.h" -#include +#include #include #include @@ -68,10 +68,9 @@ static void to_string(struct ivy_ast_node *node, b_string *str) int i = 0; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &unit_import->n_ident) { - struct ivy_token *tok - = b_unbox(struct ivy_token, it.entry, t_entry); + b_queue_entry *entry = b_queue_first(&unit_import->n_ident); + while (entry) { + struct ivy_token *tok = b_unbox(struct ivy_token, entry, t_entry); if (i > 0) { b_string_append_cstr(str, "."); @@ -79,6 +78,7 @@ static void to_string(struct ivy_ast_node *node, b_string *str) b_string_append_cstr(str, tok->t_str); i++; + entry = b_queue_next(entry); } b_string_append_cstr(str, ")"); diff --git a/lang/ast/unit-package.c b/lang/ast/unit-package.c index 3e321fa..92e2a6a 100644 --- a/lang/ast/unit-package.c +++ b/lang/ast/unit-package.c @@ -1,7 +1,7 @@ #include "ctx.h" #include "node.h" -#include +#include #include #include @@ -66,10 +66,9 @@ static void to_string(struct ivy_ast_node *node, b_string *str) b_string_append_cstr(str, " ("); int i = 0; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &unit_package->n_ident) { - struct ivy_token *tok - = b_unbox(struct ivy_token, it.entry, t_entry); + b_queue_entry *entry = b_queue_first(&unit_package->n_ident); + while (entry) { + struct ivy_token *tok = b_unbox(struct ivy_token, entry, t_entry); if (i > 0) { b_string_append_cstr(str, "."); diff --git a/lang/ast/unit.c b/lang/ast/unit.c index b7bf170..9f54838 100644 --- a/lang/ast/unit.c +++ b/lang/ast/unit.c @@ -58,11 +58,12 @@ static void collect_children( struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) { struct ivy_ast_unit_node *unit = (struct ivy_ast_unit_node *)node; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &unit->n_children) { + b_queue_entry *entry = b_queue_first(&unit->n_children); + while (entry) { struct ivy_ast_node *child - = b_unbox(struct ivy_ast_node, it.entry, n_entry); + = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, child); + entry = b_queue_next(entry); } } diff --git a/lang/codegen/cond-group.c b/lang/codegen/cond-group.c index c18446c..c5435a3 100644 --- a/lang/codegen/cond-group.c +++ b/lang/codegen/cond-group.c @@ -55,12 +55,12 @@ static enum ivy_status get_eval_type( { struct mie_type *type = NULL; - b_queue_iterator it; - b_queue_foreach (&it, &state->s_edges) { + b_queue_entry *entry = b_queue_first(&state->s_edges); + while (entry) { struct mie_phi_edge *edge - = b_unbox(struct mie_phi_edge, it.entry, e_entry); + = b_unbox(struct mie_phi_edge, entry, e_entry); if (!edge->e_value) { - continue; + goto next; } struct mie_type *edge_type @@ -68,8 +68,10 @@ static enum ivy_status get_eval_type( if (!type) { type = edge_type; - continue; } + + next: + entry = b_queue_next(entry); } *out = type; @@ -92,13 +94,14 @@ static enum ivy_status emit_phi_instr( size_t i = 0; - b_queue_iterator it; - b_queue_foreach (&it, &state->s_edges) { + b_queue_entry *entry = b_queue_first(&state->s_edges); + while (entry) { struct mie_phi_edge *edge - = b_unbox(struct mie_phi_edge, it.entry, e_entry); + = b_unbox(struct mie_phi_edge, entry, e_entry); memcpy(&edges[i], edge, sizeof *edge); i++; + entry = b_queue_next(entry); } struct mie_value *phi = mie_builder_phi( diff --git a/lang/codegen/msg.c b/lang/codegen/msg.c index dfcb2b4..051bc41 100644 --- a/lang/codegen/msg.c +++ b/lang/codegen/msg.c @@ -128,35 +128,36 @@ static enum ivy_status state_init( static void serialise_selector( struct ivy_ast_selector_node *sel, b_stringstream *out) { - b_stringstream_add(out, "_M"); + b_stream_write_string(out, "_M", NULL); if (sel->n_msg_name) { const char *msg_name = sel->n_msg_name->t_str; - b_stringstream_addf(out, "%zu%s", strlen(msg_name), msg_name); + b_stream_write_fmt(out, NULL, "%zu%s", strlen(msg_name), msg_name); } else { - b_stringstream_add(out, "0"); + b_stream_write_char(out, '0'); } - b_queue_iterator it; - b_queue_foreach (&it, &sel->n_arg_labels) { - struct ivy_token *arg - = b_unbox(struct ivy_token, it.entry, t_entry); + b_queue_entry *entry = b_queue_first(&sel->n_arg_labels); + while (entry) { + struct ivy_token *arg = b_unbox(struct ivy_token, entry, t_entry); if (arg->t_type != IVY_TOK_IDENT && arg->t_type != IVY_TOK_LABEL) { - b_stringstream_addf(out, "0"); - continue; + b_stream_write_char(out, '0'); + goto next; } const char *label = arg->t_str; if (label) { - b_stringstream_addf(out, "%zu%s", strlen(label), label); + b_stream_write_fmt(out, NULL, "%zu%s", strlen(label), label); } else { - b_stringstream_addf(out, "0"); + b_stream_write_char(out, '0'); } + next: + entry = b_queue_next(entry); } - b_stringstream_add(out, "E"); + b_stream_write_char(out, 'E'); } static enum ivy_status state_fini( @@ -167,10 +168,11 @@ static enum ivy_status state_fini( struct msg_codegen_state *msg = (struct msg_codegen_state *)state; - b_stringstream str; - b_stringstream_begin_dynamic(&str); - serialise_selector(msg->s_selector, &str); - char *sel = b_stringstream_end(&str); + b_stringstream *str = b_stringstream_create(); + serialise_selector(msg->s_selector, str); + char *sel = b_stringstream_steal(str); + b_stringstream_unref(str); + struct mie_value *sel_value = mie_ctx_get_selector(gen->c_ctx, sel); free(sel); diff --git a/lang/codegen/unit.c b/lang/codegen/unit.c index 09860a8..c18b64a 100644 --- a/lang/codegen/unit.c +++ b/lang/codegen/unit.c @@ -2,7 +2,7 @@ #include "var-map.h" #include -#include +#include #include #include #include @@ -144,17 +144,17 @@ static struct code_generator_result gen_while_loop( static void serialise_package_name(b_queue *parts, b_stringstream *out) { - b_stringstream_add(out, "_ZN"); + b_stream_write_string(out, "_ZN", NULL); - b_queue_iterator it; - b_queue_foreach (&it, parts) { - struct ivy_token *tok - = b_unbox(struct ivy_token, it.entry, t_entry); + b_queue_entry *entry = b_queue_first(parts); + while (entry) { + struct ivy_token *tok = b_unbox(struct ivy_token, entry, t_entry); size_t len = strlen(tok->t_str); - b_stringstream_addf(out, "%zu%s", len, tok->t_str); + b_stream_write_fmt(out, NULL, "%zu%s", len, tok->t_str); + entry = b_queue_next(entry); } - b_stringstream_add(out, "E"); + b_stream_write_char(out, 'E'); } static struct code_generator_result gen_unit_package( @@ -165,10 +165,10 @@ static struct code_generator_result gen_unit_package( struct ivy_ast_unit_package_node *pkg = (struct ivy_ast_unit_package_node *)node; - b_stringstream str; - b_stringstream_begin_dynamic(&str); - serialise_package_name(&pkg->n_ident, &str); - char *ident = b_stringstream_end(&str); + b_stringstream *str = b_stringstream_create(); + serialise_package_name(&pkg->n_ident, str); + char *ident = b_stringstream_steal(str); + b_stringstream_unref(str); struct mie_value *ident_val = mie_ctx_get_string(gen->c_ctx, ident); free(ident); @@ -198,10 +198,10 @@ static struct code_generator_result gen_unit_import( import_list = MIE_ARRAY(rec->r_value); } - b_stringstream str; - b_stringstream_begin_dynamic(&str); - serialise_package_name(&pkg->n_ident, &str); - char *ident = b_stringstream_end(&str); + b_stringstream *str = b_stringstream_create(); + serialise_package_name(&pkg->n_ident, str); + char *ident = b_stringstream_steal(str); + b_stringstream_unref(str); struct mie_value *ident_val = mie_ctx_get_string(gen->c_ctx, ident); free(ident); diff --git a/lang/codegen/var-map.c b/lang/codegen/var-map.c index 0ad0bd0..d9075a7 100644 --- a/lang/codegen/var-map.c +++ b/lang/codegen/var-map.c @@ -1,7 +1,7 @@ #include "var-map.h" -#include -#include +#include +#include #include #include @@ -15,7 +15,7 @@ enum ivy_status codegen_var_map_init(struct codegen_var_map *map) enum ivy_status codegen_var_map_fini(struct codegen_var_map *map) { - b_hashmap_release(map->m_map); + b_hashmap_unref(map->m_map); return IVY_OK; } diff --git a/lang/codegen/var-map.h b/lang/codegen/var-map.h index 1fdfd62..ce8cd74 100644 --- a/lang/codegen/var-map.h +++ b/lang/codegen/var-map.h @@ -1,9 +1,9 @@ #ifndef CODEGEN_VAR_MAP_H_ #define CODEGEN_VAR_MAP_H_ +#include #include -struct b_hashmap; struct mie_value; struct ivy_ast_node; @@ -20,7 +20,7 @@ struct codegen_var { }; struct codegen_var_map { - struct b_hashmap *m_map; + b_hashmap *m_map; }; extern enum ivy_status codegen_var_map_init(struct codegen_var_map *map); diff --git a/lang/include/ivy/lang/ast.h b/lang/include/ivy/lang/ast.h index c521b43..76015da 100644 --- a/lang/include/ivy/lang/ast.h +++ b/lang/include/ivy/lang/ast.h @@ -2,6 +2,7 @@ #define IVY_LANG_AST_H_ #include +#include #include #include #include @@ -364,7 +365,7 @@ IVY_API struct ivy_ast_node *ivy_ast_node_create(enum ivy_ast_node_type type); IVY_API enum ivy_status ivy_ast_node_iterate( struct ivy_ast_node *node, struct ivy_ast_node_iterator *it, ivy_ast_node_iteration_callback callback, void *arg); -IVY_API void ivy_ast_node_to_string(struct ivy_ast_node *node, struct b_string *out); +IVY_API void ivy_ast_node_to_string(struct ivy_ast_node *node, b_string *out); IVY_API void ivy_ast_node_set_bounds_from_token( struct ivy_ast_node *parent, const struct ivy_token *tok); IVY_API void ivy_ast_node_extend_bounds( diff --git a/lang/internal.c b/lang/internal.c index 05c2d4b..c78cf78 100644 --- a/lang/internal.c +++ b/lang/internal.c @@ -1,9 +1,10 @@ -#include #include "lex.h" -#include -#include -#include + +#include #include +#include +#include +#include static void print_symbol_node(struct ivy_lexer_symbol_node *node, int depth) { @@ -19,27 +20,31 @@ static void print_symbol_node(struct ivy_lexer_symbol_node *node, int depth) b_printf("\n"); - 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); print_symbol_node(child, depth + 1); + entry = b_queue_next(entry); } } -void internal_lexer_print_symbol_tree(struct ivy_lexer* lex) +void internal_lexer_print_symbol_tree(struct ivy_lexer *lex) { print_symbol_node(lex->lex_sym_tree, -1); } -void internal_lexer_print_keyword_dict(struct ivy_lexer* lex) +void internal_lexer_print_keyword_dict(struct ivy_lexer *lex) { - b_dict_iterator it = {0}; - b_dict_foreach(&it, lex->lex_keywords) { - const char *kw_name = it.key; - enum ivy_keyword kw_id = b_number_get_int(B_NUMBER(it.value)); + b_iterator *it = b_iterator_begin(lex->lex_keywords); + b_foreach_ptr(b_dict_item, item, it) + { + const b_string *kw_name = item->key; + enum ivy_keyword kw_id = b_number_get_int(item->value); - b_printf("([magenta]%s[reset]) = [green]%s[reset]\n", ivy_keyword_to_string(kw_id), kw_name); + b_printf( + "([magenta]%s[reset]) = [green]%s[reset]\n", + ivy_keyword_to_string(kw_id), b_string_ptr(kw_name)); } -} \ No newline at end of file +} diff --git a/lang/lex.c b/lang/lex.c index 0ec3eaf..01cee07 100644 --- a/lang/lex.c +++ b/lang/lex.c @@ -2,9 +2,9 @@ #include #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -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); diff --git a/lang/lex.h b/lang/lex.h index 3aa951a..6a841e1 100644 --- a/lang/lex.h +++ b/lang/lex.h @@ -2,8 +2,8 @@ #define _LEX_H_ #include -#include -#include +#include +#include #include #include #include