#include "node.h" #include #include #include extern struct ast_node_type unit_node_ops; extern struct ast_node_type unit_package_node_ops; static const struct ast_node_type *node_ops[] = { [IVY_AST_UNIT] = &unit_node_ops, [IVY_AST_UNIT_PACKAGE] = &unit_package_node_ops, }; 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]; } token_parse_function get_token_parser( struct ivy_ast_node *context, struct ivy_token *tok) { const struct ast_node_type *type = get_ast_node_type(context->n_type); if (!type) { return NULL; } token_parse_function generic_parser = type->n_token_parsers[tok->t_type]; token_parse_function better_parser = NULL; switch (tok->t_type) { case IVY_TOK_KEYWORD: better_parser = type->n_keyword_parsers[tok->t_keyword]; break; case IVY_TOK_SYMBOL: better_parser = type->n_symbol_parsers[tok->t_symbol]; break; default: break; } return better_parser ? better_parser : generic_parser; } struct ivy_ast_node *ast_node_create_with_size( enum ivy_ast_node_type type, size_t size) { struct ivy_ast_node *node = malloc(size); if (!node) { return NULL; } memset(node, 0x0, size); node->n_type = type; return node; } enum ivy_status ast_node_add_child( struct ivy_ast_node *parent, struct ivy_ast_node *child) { const struct ast_node_type *ops = get_ast_node_type(parent->n_type); if (!ops) { return IVY_ERR_NOT_SUPPORTED; } enum ivy_status (*add_child)(struct ivy_ast_node *, struct ivy_ast_node *) = ops->n_add_child; if (!add_child) { return IVY_ERR_NOT_SUPPORTED; } return add_child(parent, child); }