feat(ast): data as void* and basic helpers

This commit is contained in:
william.valenduc 2026-01-09 22:36:48 +00:00
parent 62e44b8101
commit 807416940c
2 changed files with 34 additions and 12 deletions

View file

@ -0,0 +1,23 @@
#include "ast.h"
#include <stdbool.h>
bool ast_is_if(struct ast *node)
{
return node->type == AST_IF;
}
bool ast_is_cmd(struct ast *node)
{
return node->type == AST_CMD;
}
struct ast_if *ast_get_if(struct ast *node)
{
return (struct ast_if *)node->data;
}
struct ast_cmd *ast_get_cmd(struct ast *node)
{
return (struct ast_cmd *)node->data;
}

View file

@ -5,7 +5,7 @@
enum ast_type
{
AST_END = 0,
AST_END,
AST_IF,
AST_CMD
};
@ -15,14 +15,6 @@ struct ast_cmd
struct list *cmd;
};
union ast_node;
struct ast
{
enum ast_type type;
union ast_node *data;
};
struct ast_if
{
struct ast *condition;
@ -30,10 +22,17 @@ struct ast_if
struct ast *else_clause;
};
union ast_node
struct ast
{
struct ast_if *ast_if;
struct ast_cmd *ast_cmd;
enum ast_type type;
/**
* Data associated with this AST node. It can be one of the following:
* - NULL (AST_END)
* - struct ast_if* (AST_IF)
* - struct ast_cmd* (AST_CMD)
*/
void *data;
};
#endif /* ! AST_H */