2024-11-24 11:10:42 +00:00
|
|
|
#include "node.h"
|
|
|
|
|
|
2024-12-06 21:33:53 +00:00
|
|
|
#include <blue/object/string.h>
|
2024-11-24 11:10:42 +00:00
|
|
|
#include <ivy/lang/ast.h>
|
2024-11-24 21:31:49 +00:00
|
|
|
#include <stdio.h>
|
2024-11-24 11:10:42 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
extern struct ast_node_type unit_node_ops;
|
|
|
|
|
extern struct ast_node_type unit_package_node_ops;
|
2024-11-25 16:50:42 +00:00
|
|
|
extern struct ast_node_type unit_import_node_ops;
|
|
|
|
|
extern struct ast_node_type class_node_ops;
|
2024-11-26 21:30:40 +00:00
|
|
|
extern struct ast_node_type msgh_node_ops;
|
|
|
|
|
extern struct ast_node_type selector_node_ops;
|
2024-11-27 22:29:29 +00:00
|
|
|
extern struct ast_node_type expr_node_ops;
|
2024-11-28 16:59:04 +00:00
|
|
|
extern struct ast_node_type block_node_ops;
|
2024-11-28 22:05:37 +00:00
|
|
|
extern struct ast_node_type msg_node_ops;
|
|
|
|
|
extern struct ast_node_type op_node_ops;
|
2025-09-08 15:50:50 +01:00
|
|
|
extern struct ast_node_type global_node_ops;
|
2024-11-28 22:05:37 +00:00
|
|
|
extern struct ast_node_type ident_node_ops;
|
|
|
|
|
extern struct ast_node_type int_node_ops;
|
|
|
|
|
extern struct ast_node_type double_node_ops;
|
2024-12-06 13:46:41 +00:00
|
|
|
extern struct ast_node_type atom_node_ops;
|
2024-12-08 12:28:47 +00:00
|
|
|
extern struct ast_node_type tuple_node_ops;
|
2024-11-28 22:05:37 +00:00
|
|
|
extern struct ast_node_type string_node_ops;
|
2024-12-06 13:46:58 +00:00
|
|
|
extern struct ast_node_type fstring_node_ops;
|
2024-12-03 21:57:44 +00:00
|
|
|
extern struct ast_node_type cascade_node_ops;
|
2024-12-04 16:35:19 +00:00
|
|
|
extern struct ast_node_type cond_group_node_ops;
|
|
|
|
|
extern struct ast_node_type cond_node_ops;
|
2024-12-04 22:22:25 +00:00
|
|
|
extern struct ast_node_type match_node_ops;
|
2024-12-05 19:29:21 +00:00
|
|
|
extern struct ast_node_type while_loop_node_ops;
|
2024-12-06 10:02:31 +00:00
|
|
|
extern struct ast_node_type for_loop_node_ops;
|
2025-09-08 15:55:12 +01:00
|
|
|
extern struct ast_node_type loop_break_node_ops;
|
|
|
|
|
extern struct ast_node_type loop_repeat_node_ops;
|
2024-12-06 10:01:56 +00:00
|
|
|
extern struct ast_node_type return_node_ops;
|
2024-12-06 19:47:27 +00:00
|
|
|
extern struct ast_node_type property_node_ops;
|
2024-12-06 22:26:06 +00:00
|
|
|
extern struct ast_node_type lambda_node_ops;
|
2024-12-07 20:56:57 +00:00
|
|
|
extern struct ast_node_type pkg_node_ops;
|
|
|
|
|
extern struct ast_node_type pkg_static_node_ops;
|
|
|
|
|
extern struct ast_node_type pkg_static_item_node_ops;
|
|
|
|
|
extern struct ast_node_type pkg_dynamic_node_ops;
|
2024-12-04 22:22:25 +00:00
|
|
|
extern struct ast_node_type discard_node_ops;
|
2025-01-16 13:15:48 +00:00
|
|
|
extern struct ast_node_type try_node_ops;
|
|
|
|
|
extern struct ast_node_type try_catch_node_ops;
|
2025-09-08 15:56:03 +01:00
|
|
|
extern struct ast_node_type c_true_node_ops;
|
|
|
|
|
extern struct ast_node_type c_false_node_ops;
|
|
|
|
|
extern struct ast_node_type c_null_node_ops;
|
2024-11-24 11:10:42 +00:00
|
|
|
|
|
|
|
|
static const struct ast_node_type *node_ops[] = {
|
|
|
|
|
[IVY_AST_UNIT] = &unit_node_ops,
|
|
|
|
|
[IVY_AST_UNIT_PACKAGE] = &unit_package_node_ops,
|
2024-11-25 16:50:42 +00:00
|
|
|
[IVY_AST_UNIT_IMPORT] = &unit_import_node_ops,
|
|
|
|
|
[IVY_AST_CLASS] = &class_node_ops,
|
2024-11-27 12:56:10 +00:00
|
|
|
[IVY_AST_MSGH] = &msgh_node_ops,
|
|
|
|
|
[IVY_AST_SELECTOR] = &selector_node_ops,
|
2024-11-27 22:29:29 +00:00
|
|
|
[IVY_AST_EXPR] = &expr_node_ops,
|
2024-11-28 16:59:04 +00:00
|
|
|
[IVY_AST_BLOCK] = &block_node_ops,
|
2024-11-28 22:05:37 +00:00
|
|
|
[IVY_AST_MSG] = &msg_node_ops,
|
|
|
|
|
[IVY_AST_OP] = &op_node_ops,
|
2025-09-08 15:50:50 +01:00
|
|
|
[IVY_AST_GLOBAL] = &global_node_ops,
|
2024-11-28 22:05:37 +00:00
|
|
|
[IVY_AST_IDENT] = &ident_node_ops,
|
|
|
|
|
[IVY_AST_INT] = &int_node_ops,
|
|
|
|
|
[IVY_AST_DOUBLE] = &double_node_ops,
|
2024-12-06 13:46:41 +00:00
|
|
|
[IVY_AST_ATOM] = &atom_node_ops,
|
2024-12-08 12:28:47 +00:00
|
|
|
[IVY_AST_TUPLE] = &tuple_node_ops,
|
2024-11-28 22:05:37 +00:00
|
|
|
[IVY_AST_STRING] = &string_node_ops,
|
2024-12-06 13:46:58 +00:00
|
|
|
[IVY_AST_FSTRING] = &fstring_node_ops,
|
2024-12-03 21:57:44 +00:00
|
|
|
[IVY_AST_CASCADE] = &cascade_node_ops,
|
2024-12-04 16:35:19 +00:00
|
|
|
[IVY_AST_COND_GROUP] = &cond_group_node_ops,
|
|
|
|
|
[IVY_AST_COND] = &cond_node_ops,
|
2024-12-04 22:22:25 +00:00
|
|
|
[IVY_AST_MATCH] = &match_node_ops,
|
2024-12-05 19:29:21 +00:00
|
|
|
[IVY_AST_WHILE_LOOP] = &while_loop_node_ops,
|
2024-12-06 10:02:31 +00:00
|
|
|
[IVY_AST_FOR_LOOP] = &for_loop_node_ops,
|
2025-09-08 15:55:12 +01:00
|
|
|
[IVY_AST_LOOP_BREAK] = &loop_break_node_ops,
|
|
|
|
|
[IVY_AST_LOOP_REPEAT] = &loop_repeat_node_ops,
|
2024-12-06 10:01:56 +00:00
|
|
|
[IVY_AST_RETURN] = &return_node_ops,
|
2024-12-06 19:47:27 +00:00
|
|
|
[IVY_AST_PROPERTY] = &property_node_ops,
|
2024-12-06 22:26:06 +00:00
|
|
|
[IVY_AST_LAMBDA] = &lambda_node_ops,
|
2024-12-07 20:56:57 +00:00
|
|
|
[IVY_AST_PKG] = &pkg_node_ops,
|
|
|
|
|
[IVY_AST_PKG_STATIC] = &pkg_static_node_ops,
|
2024-12-07 21:46:24 +00:00
|
|
|
[IVY_AST_PKG_ITEM] = &pkg_static_item_node_ops,
|
2024-12-07 20:56:57 +00:00
|
|
|
[IVY_AST_PKG_DYNAMIC] = &pkg_dynamic_node_ops,
|
2024-12-04 22:22:25 +00:00
|
|
|
[IVY_AST_DISCARD] = &discard_node_ops,
|
2025-01-16 13:15:48 +00:00
|
|
|
[IVY_AST_TRY] = &try_node_ops,
|
|
|
|
|
[IVY_AST_TRY_CATCH] = &try_catch_node_ops,
|
2025-09-08 15:56:03 +01:00
|
|
|
[IVY_AST_C_TRUE] = &c_true_node_ops,
|
|
|
|
|
[IVY_AST_C_FALSE] = &c_false_node_ops,
|
|
|
|
|
[IVY_AST_C_NULL] = &c_null_node_ops,
|
2024-11-24 11:10:42 +00:00
|
|
|
};
|
|
|
|
|
static const size_t nr_node_ops = sizeof node_ops / sizeof node_ops[0];
|
|
|
|
|
|
|
|
|
|
const struct ast_node_type *get_ast_node_type(enum ivy_ast_node_type type)
|
|
|
|
|
{
|
|
|
|
|
if (type >= nr_node_ops) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return node_ops[type];
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 22:45:34 +00:00
|
|
|
enum token_expr_type get_token_expr_type(struct ivy_token *tok)
|
2024-11-27 12:56:10 +00:00
|
|
|
{
|
2024-11-28 16:59:04 +00:00
|
|
|
const struct ivy_operator *op = NULL;
|
|
|
|
|
|
2024-11-27 12:56:10 +00:00
|
|
|
switch (tok->t_type) {
|
|
|
|
|
case IVY_TOK_IDENT:
|
|
|
|
|
case IVY_TOK_INT:
|
|
|
|
|
case IVY_TOK_DOUBLE:
|
|
|
|
|
case IVY_TOK_STRING:
|
|
|
|
|
case IVY_TOK_STR_START:
|
|
|
|
|
case IVY_TOK_ATOM:
|
|
|
|
|
return TOK_EXPR_BEGIN;
|
|
|
|
|
case IVY_TOK_SYMBOL:
|
|
|
|
|
switch (tok->t_symbol) {
|
|
|
|
|
case IVY_SYM_LEFT_PAREN:
|
2024-12-06 22:26:06 +00:00
|
|
|
case IVY_SYM_LEFT_BRACKET:
|
|
|
|
|
case IVY_SYM_LEFT_BRACE:
|
2024-11-27 12:56:10 +00:00
|
|
|
case IVY_SYM_CARET:
|
2025-01-15 21:11:19 +00:00
|
|
|
case IVY_SYM_UNDERSCORE:
|
2024-11-27 12:56:10 +00:00
|
|
|
return TOK_EXPR_BEGIN;
|
|
|
|
|
case IVY_SYM_COMMA:
|
|
|
|
|
return TOK_EXPR_ANY;
|
|
|
|
|
default:
|
2024-12-01 13:25:36 +00:00
|
|
|
op = ivy_operator_get_by_token(tok->t_symbol);
|
2024-11-28 16:59:04 +00:00
|
|
|
return op ? TOK_EXPR_ANY : TOK_EXPR_NONE;
|
2024-11-27 12:56:10 +00:00
|
|
|
}
|
|
|
|
|
case IVY_TOK_KEYWORD:
|
|
|
|
|
switch (tok->t_keyword) {
|
|
|
|
|
case IVY_KW_IF:
|
|
|
|
|
case IVY_KW_ELSE:
|
|
|
|
|
case IVY_KW_MATCH:
|
|
|
|
|
case IVY_KW_FOR:
|
|
|
|
|
case IVY_KW_WHILE:
|
2025-09-08 15:55:12 +01:00
|
|
|
case IVY_KW_BREAK:
|
|
|
|
|
case IVY_KW_CONTINUE:
|
2024-11-27 12:56:10 +00:00
|
|
|
case IVY_KW_TRY:
|
|
|
|
|
case IVY_KW_THROW:
|
2025-09-08 15:56:03 +01:00
|
|
|
case IVY_KW_TRUE:
|
|
|
|
|
case IVY_KW_FALSE:
|
|
|
|
|
case IVY_KW_NULL:
|
2024-11-27 12:56:10 +00:00
|
|
|
return TOK_EXPR_BEGIN;
|
|
|
|
|
default:
|
2024-12-01 13:25:36 +00:00
|
|
|
op = ivy_operator_get_by_token(tok->t_keyword);
|
2024-11-28 16:59:04 +00:00
|
|
|
return op ? TOK_EXPR_ANY : TOK_EXPR_NONE;
|
2024-11-27 12:56:10 +00:00
|
|
|
return TOK_EXPR_NONE;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return TOK_EXPR_NONE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-24 11:10:42 +00:00
|
|
|
token_parse_function get_token_parser(
|
|
|
|
|
struct ivy_ast_node *context, struct ivy_token *tok)
|
|
|
|
|
{
|
2024-12-06 22:24:08 +00:00
|
|
|
token_parse_function type_parser = NULL;
|
|
|
|
|
token_parse_function type_fallback_parser = NULL;
|
|
|
|
|
token_parse_function token_parser = NULL;
|
|
|
|
|
token_parse_function expr_begin_parser = NULL;
|
|
|
|
|
token_parse_function expr_other_parser = NULL;
|
|
|
|
|
token_parse_function expr_parser = NULL;
|
|
|
|
|
token_parse_function token_fallback_parser = NULL;
|
|
|
|
|
|
2024-11-24 11:10:42 +00:00
|
|
|
const struct ast_node_type *type = get_ast_node_type(context->n_type);
|
|
|
|
|
if (!type) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2024-11-27 22:29:29 +00:00
|
|
|
|
2024-12-06 22:24:08 +00:00
|
|
|
token_parser = type->n_token_parsers[__TOK_PARSER_INDEX(tok->t_type)];
|
|
|
|
|
token_fallback_parser = type->n_token_parsers[__TOK_PARSER_FALLBACK_INDEX];
|
2024-11-27 22:29:29 +00:00
|
|
|
|
2024-11-24 11:10:42 +00:00
|
|
|
token_parse_function better_parser = NULL;
|
|
|
|
|
|
|
|
|
|
switch (tok->t_type) {
|
|
|
|
|
case IVY_TOK_KEYWORD:
|
2024-12-06 22:24:08 +00:00
|
|
|
type_parser
|
2024-12-01 13:25:36 +00:00
|
|
|
= type->n_keyword_parsers[__KW_PARSER_INDEX(tok->t_keyword)];
|
2024-11-28 10:56:43 +00:00
|
|
|
if (type->n_keyword_parsers[__KW_PARSER_FALLBACK_INDEX]) {
|
2024-12-06 22:24:08 +00:00
|
|
|
type_fallback_parser
|
2024-12-01 13:25:36 +00:00
|
|
|
= type->n_keyword_parsers[__KW_PARSER_FALLBACK_INDEX];
|
2024-11-27 22:29:29 +00:00
|
|
|
}
|
2024-11-24 11:10:42 +00:00
|
|
|
break;
|
|
|
|
|
case IVY_TOK_SYMBOL:
|
2024-12-06 22:24:08 +00:00
|
|
|
type_parser
|
2024-12-01 13:25:36 +00:00
|
|
|
= type->n_symbol_parsers[__SYM_PARSER_INDEX(tok->t_symbol)];
|
2024-11-28 10:56:43 +00:00
|
|
|
if (type->n_symbol_parsers[__SYM_PARSER_FALLBACK_INDEX]) {
|
2024-12-06 22:24:08 +00:00
|
|
|
type_fallback_parser
|
2024-12-01 13:25:36 +00:00
|
|
|
= type->n_symbol_parsers[__SYM_PARSER_FALLBACK_INDEX];
|
2024-11-27 22:29:29 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 22:45:34 +00:00
|
|
|
enum token_expr_type expr_type = get_token_expr_type(tok);
|
2024-11-27 22:29:29 +00:00
|
|
|
switch (expr_type) {
|
|
|
|
|
case TOK_EXPR_BEGIN:
|
2024-12-06 22:24:08 +00:00
|
|
|
expr_begin_parser = type->n_expr_parser.expr_begin;
|
|
|
|
|
expr_parser = type->n_expr_parser.expr_all;
|
2024-11-27 22:29:29 +00:00
|
|
|
break;
|
|
|
|
|
case TOK_EXPR_ANY:
|
2024-12-06 22:24:08 +00:00
|
|
|
expr_other_parser = type->n_expr_parser.expr_other;
|
|
|
|
|
expr_parser = type->n_expr_parser.expr_all;
|
2024-11-24 11:10:42 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-06 22:24:08 +00:00
|
|
|
bool token_has_sub_id
|
|
|
|
|
= (tok->t_type == IVY_TOK_KEYWORD || tok->t_type == IVY_TOK_SYMBOL);
|
|
|
|
|
|
|
|
|
|
if (type_parser)
|
|
|
|
|
return type_parser;
|
|
|
|
|
if (token_parser && !token_has_sub_id)
|
|
|
|
|
return token_parser;
|
|
|
|
|
if (expr_begin_parser)
|
|
|
|
|
return expr_begin_parser;
|
|
|
|
|
if (expr_other_parser)
|
|
|
|
|
return expr_other_parser;
|
|
|
|
|
if (expr_parser)
|
|
|
|
|
return expr_parser;
|
|
|
|
|
if (type_fallback_parser)
|
|
|
|
|
return type_fallback_parser;
|
|
|
|
|
if (token_parser && token_has_sub_id)
|
|
|
|
|
return token_parser;
|
|
|
|
|
if (token_fallback_parser)
|
|
|
|
|
return token_fallback_parser;
|
|
|
|
|
return NULL;
|
2024-11-24 11:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-16 21:58:52 +01:00
|
|
|
struct ivy_ast_node *ivy_ast_node_create(enum ivy_ast_node_type type)
|
|
|
|
|
{
|
|
|
|
|
return ast_node_create(type);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 22:06:25 +00:00
|
|
|
struct ivy_ast_node *ast_node_create(enum ivy_ast_node_type type)
|
2024-11-24 11:10:42 +00:00
|
|
|
{
|
2024-11-28 22:06:25 +00:00
|
|
|
const struct ast_node_type *type_info = get_ast_node_type(type);
|
|
|
|
|
if (!type_info) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ivy_ast_node *node = malloc(type_info->n_node_size);
|
2024-11-24 11:10:42 +00:00
|
|
|
if (!node) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 22:06:25 +00:00
|
|
|
memset(node, 0x0, type_info->n_node_size);
|
2024-11-24 11:10:42 +00:00
|
|
|
|
|
|
|
|
node->n_type = type;
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-06 20:24:08 +00:00
|
|
|
void ivy_ast_node_to_string(struct ivy_ast_node *node, struct b_string *out)
|
2024-11-24 20:50:12 +00:00
|
|
|
{
|
2024-12-06 20:24:08 +00:00
|
|
|
const struct ast_node_type *type_info = get_ast_node_type(node->n_type);
|
|
|
|
|
if (!type_info) {
|
|
|
|
|
return;
|
2024-11-24 20:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-06 20:24:08 +00:00
|
|
|
if (type_info->n_to_string) {
|
|
|
|
|
type_info->n_to_string(node, out);
|
2024-11-24 20:50:12 +00:00
|
|
|
} else {
|
2024-12-06 21:33:53 +00:00
|
|
|
b_string_append_cstr(
|
|
|
|
|
out, ivy_ast_node_type_to_string(node->n_type));
|
2024-11-24 20:50:12 +00:00
|
|
|
}
|
2024-11-24 16:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-08 22:28:01 +01:00
|
|
|
void ivy_ast_node_set_bounds_from_token(
|
|
|
|
|
struct ivy_ast_node *parent, const struct ivy_token *tok)
|
|
|
|
|
{
|
|
|
|
|
parent->n_start = tok->t_start;
|
|
|
|
|
parent->n_end = tok->t_end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ivy_ast_node_extend_bounds(
|
|
|
|
|
struct ivy_ast_node *parent, const struct ivy_ast_node *child)
|
|
|
|
|
{
|
|
|
|
|
if (child->n_start.c_row < parent->n_start.c_row) {
|
|
|
|
|
parent->n_start = child->n_start;
|
|
|
|
|
} else if (child->n_start.c_col < parent->n_start.c_col) {
|
|
|
|
|
parent->n_start.c_col = child->n_start.c_col;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (child->n_end.c_row > parent->n_end.c_row) {
|
|
|
|
|
parent->n_end = child->n_end;
|
|
|
|
|
} else if (child->n_end.c_col > parent->n_end.c_col) {
|
|
|
|
|
parent->n_end.c_col = child->n_end.c_col;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ivy_ast_node_extend_bounds_recursive(
|
|
|
|
|
struct ivy_ast_node *parent, const struct ivy_ast_node *child)
|
|
|
|
|
{
|
|
|
|
|
while (parent) {
|
|
|
|
|
ivy_ast_node_extend_bounds(parent, child);
|
|
|
|
|
child = parent;
|
|
|
|
|
parent = parent->n_parent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-24 16:12:31 +00:00
|
|
|
void ivy_ast_node_destroy(struct ivy_ast_node *node)
|
|
|
|
|
{
|
2025-04-17 23:15:48 +01:00
|
|
|
struct ivy_ast_node_iterator it = {};
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
while (b_queue_iterator_is_valid(&q_it)) {
|
|
|
|
|
struct ivy_ast_node_iterator_entry *it_entry = b_unbox(
|
|
|
|
|
struct ivy_ast_node_iterator_entry, q_it.entry, it_entry);
|
|
|
|
|
node = b_unbox(struct ivy_ast_node, it_entry, n_it);
|
|
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
|
/* this should never happen. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
type->n_collect_children(node, &it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_queue_iterator_next(&q_it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!b_queue_empty(&it.it_queue)) {
|
|
|
|
|
b_queue_entry *entry = b_queue_pop_back(&it.it_queue);
|
|
|
|
|
if (!entry) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node = b_unbox(struct ivy_ast_node, entry, n_it);
|
|
|
|
|
|
|
|
|
|
const struct ast_node_type *type = get_ast_node_type(node->n_type);
|
|
|
|
|
if (type && type->n_destroy) {
|
|
|
|
|
type->n_destroy(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(node);
|
|
|
|
|
}
|
2024-11-24 20:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define ENUM_STR(x) \
|
|
|
|
|
case x: \
|
|
|
|
|
return #x
|
|
|
|
|
|
|
|
|
|
const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v)
|
|
|
|
|
{
|
|
|
|
|
switch (v) {
|
|
|
|
|
ENUM_STR(IVY_AST_NONE);
|
|
|
|
|
ENUM_STR(IVY_AST_UNIT);
|
|
|
|
|
ENUM_STR(IVY_AST_OP);
|
|
|
|
|
ENUM_STR(IVY_AST_MSG);
|
|
|
|
|
ENUM_STR(IVY_AST_CLASS);
|
|
|
|
|
ENUM_STR(IVY_AST_MSGH);
|
|
|
|
|
ENUM_STR(IVY_AST_PROPERTY);
|
2024-11-28 16:59:04 +00:00
|
|
|
ENUM_STR(IVY_AST_SELECTOR);
|
|
|
|
|
ENUM_STR(IVY_AST_EXPR);
|
2024-11-24 20:50:12 +00:00
|
|
|
ENUM_STR(IVY_AST_LAMBDA);
|
|
|
|
|
ENUM_STR(IVY_AST_UNIT_PACKAGE);
|
|
|
|
|
ENUM_STR(IVY_AST_UNIT_IMPORT);
|
2024-12-04 22:22:25 +00:00
|
|
|
ENUM_STR(IVY_AST_DISCARD);
|
2024-11-24 20:50:12 +00:00
|
|
|
ENUM_STR(IVY_AST_INT);
|
2025-09-08 15:50:50 +01:00
|
|
|
ENUM_STR(IVY_AST_GLOBAL);
|
2024-11-24 20:50:12 +00:00
|
|
|
ENUM_STR(IVY_AST_DOUBLE);
|
|
|
|
|
ENUM_STR(IVY_AST_STRING);
|
|
|
|
|
ENUM_STR(IVY_AST_FSTRING);
|
|
|
|
|
ENUM_STR(IVY_AST_ATOM);
|
|
|
|
|
ENUM_STR(IVY_AST_IDENT);
|
|
|
|
|
ENUM_STR(IVY_AST_FOR_LOOP);
|
|
|
|
|
ENUM_STR(IVY_AST_WHILE_LOOP);
|
2025-09-08 15:55:12 +01:00
|
|
|
ENUM_STR(IVY_AST_LOOP_BREAK);
|
|
|
|
|
ENUM_STR(IVY_AST_LOOP_REPEAT);
|
2024-12-03 21:57:44 +00:00
|
|
|
ENUM_STR(IVY_AST_CASCADE);
|
2024-11-24 20:50:12 +00:00
|
|
|
ENUM_STR(IVY_AST_COND_GROUP);
|
2024-12-04 22:22:25 +00:00
|
|
|
ENUM_STR(IVY_AST_MATCH);
|
2024-11-24 20:50:12 +00:00
|
|
|
ENUM_STR(IVY_AST_COND);
|
|
|
|
|
ENUM_STR(IVY_AST_TUPLE);
|
2024-11-28 10:26:53 +00:00
|
|
|
ENUM_STR(IVY_AST_BLOCK);
|
2024-12-07 20:56:57 +00:00
|
|
|
ENUM_STR(IVY_AST_PKG);
|
|
|
|
|
ENUM_STR(IVY_AST_PKG_STATIC);
|
2024-12-07 21:46:24 +00:00
|
|
|
ENUM_STR(IVY_AST_PKG_ITEM);
|
2024-12-07 20:56:57 +00:00
|
|
|
ENUM_STR(IVY_AST_PKG_DYNAMIC);
|
2024-12-06 10:01:56 +00:00
|
|
|
ENUM_STR(IVY_AST_RETURN);
|
2025-01-16 13:15:48 +00:00
|
|
|
ENUM_STR(IVY_AST_TRY);
|
|
|
|
|
ENUM_STR(IVY_AST_TRY_CATCH);
|
2024-11-24 20:50:12 +00:00
|
|
|
ENUM_STR(IVY_AST_TYPE_COUNT);
|
2025-09-08 15:56:03 +01:00
|
|
|
ENUM_STR(IVY_AST_C_TRUE);
|
|
|
|
|
ENUM_STR(IVY_AST_C_FALSE);
|
|
|
|
|
ENUM_STR(IVY_AST_C_NULL);
|
2024-11-24 20:50:12 +00:00
|
|
|
default:
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-16 21:58:52 +01:00
|
|
|
|
|
|
|
|
void ivy_ast_unit_add_node(struct ivy_ast_unit_node *unit, struct ivy_ast_node *child)
|
|
|
|
|
{
|
|
|
|
|
b_queue_push_back(&unit->n_children, &child->n_entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ivy_ast_node *ivy_ast_unit_dequeue_node(struct ivy_ast_unit_node *unit)
|
|
|
|
|
{
|
|
|
|
|
b_queue_entry *entry = b_queue_pop_front(&unit->n_children);
|
|
|
|
|
if (!entry) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return b_unbox(struct ivy_ast_node, entry, n_entry);
|
|
|
|
|
}
|