Files
bshell/bshell/ast/ast.h
T

228 lines
4.3 KiB
C

#ifndef AST_H_
#define AST_H_
#include "../status.h"
#include <fx/bstr.h>
#include <fx/queue.h>
struct lex_token;
#define AST_ITERATE_OK(flags) \
((struct ast_iterate_result) {.r_status = BSHELL_SUCCESS, \
.r_flags = (flags)})
#define AST_ITERATE_ERR(status) \
((struct ast_iterate_result) {.r_status = (status)})
enum ast_node_type {
AST_NONE = 0x00u,
AST_NULL,
AST_STMT_LIST,
AST_INT,
AST_DOUBLE,
AST_WORD,
AST_STRING,
AST_FSTRING,
AST_VAR,
AST_VAR_SPLAT,
AST_FLAG,
AST_CMDCALL,
AST_FUNCALL,
AST_PIPELINE,
AST_REDIRECTION,
AST_BLOCK,
AST_FUNC,
AST_ARRAY,
AST_HASHTABLE,
AST_HASHTABLE_ITEM,
AST_OP,
AST_IF,
AST_IF_BRANCH,
};
struct ast_iterator_entry {
fx_queue_entry e_entry;
unsigned long e_depth;
};
struct ast_node {
enum ast_node_type n_type;
struct ast_node *n_parent;
fx_queue_entry n_entry;
struct ast_iterator_entry n_it;
};
struct null_ast_node {
struct ast_node n_base;
};
struct int_ast_node {
struct ast_node n_base;
struct lex_token *n_value;
};
struct double_ast_node {
struct ast_node n_base;
struct lex_token *n_value;
};
struct word_ast_node {
struct ast_node n_base;
struct lex_token *n_value;
};
struct string_ast_node {
struct ast_node n_base;
struct lex_token *n_value;
};
struct fstring_ast_node {
struct ast_node n_base;
fx_queue n_elements;
};
struct var_ast_node {
struct ast_node n_base;
struct lex_token *n_ident;
};
struct var_splat_ast_node {
struct ast_node n_base;
struct lex_token *n_ident;
};
struct cmdcall_ast_node {
struct ast_node n_base;
fx_queue n_args;
fx_queue n_redirect;
};
struct funcall_ast_node {
struct ast_node n_base;
struct ast_node *n_func;
fx_queue n_args;
};
struct pipeline_ast_node {
struct ast_node n_base;
fx_queue n_stages;
};
struct redirection_ast_node {
struct ast_node n_base;
bool n_append : 1;
bool n_out_is_fd : 1;
bool n_out_is_expr : 1;
unsigned int n_in, n_out;
struct ast_node *n_out_path_expr;
const char *n_out_path;
struct lex_token *n_out_tok;
};
struct stmt_list_ast_node {
struct ast_node n_base;
fx_queue n_statements;
};
struct block_ast_node {
struct ast_node n_base;
fx_queue n_statements;
};
struct func_ast_node {
struct ast_node n_base;
struct lex_token *n_name;
fx_queue n_params;
struct ast_node *n_body;
};
struct array_ast_node {
struct ast_node n_base;
fx_queue n_items;
};
struct hashtable_ast_node {
struct ast_node n_base;
fx_queue n_items;
};
struct hashtable_item_ast_node {
struct ast_node n_base;
struct ast_node *n_key, *n_value;
};
struct op_ast_node {
struct ast_node n_base;
const struct operator_info *n_op;
struct ast_node *n_left, *n_right;
};
struct if_branch_ast_node {
struct ast_node n_base;
struct ast_node *n_cond;
struct ast_node *n_body;
};
struct if_ast_node {
struct ast_node n_base;
fx_queue n_branches;
};
struct ast_iterator {
struct ast_node *it_cur;
fx_queue it_queue;
unsigned int it_depth;
fx_queue_entry *it_insert_after;
};
struct ast_iterate_result {
enum bshell_status r_status;
enum {
AST_ITERATE_CONTINUE = 0x00u,
AST_ITERATE_STOP = 0x01u,
AST_ITERATE_ADD_CHILDREN = 0x02u,
AST_ITERATE_REPEAT = 0x04u,
} r_flags;
};
enum ast_iteration_type {
AST_ITERATION_PRE,
AST_ITERATION_POST,
};
typedef struct ast_iterate_result (*ast_iterator_callback)(
struct ast_node *,
enum ast_iteration_type,
struct ast_iterator *,
void *);
struct ast_node_definition {
enum ast_node_type def_id;
size_t def_node_size;
enum bshell_status (*def_collect_children)(
struct ast_node *,
struct ast_iterator *);
enum bshell_status (*def_cleanup)(struct ast_node *);
void (*def_to_string)(const struct ast_node *, fx_bstr *);
};
extern struct ast_node *ast_node_create(enum ast_node_type type);
extern void ast_node_destroy(struct ast_node *node);
extern void ast_node_to_string(const struct ast_node *node, fx_bstr *out);
extern enum bshell_status ast_node_iterate(
struct ast_node *node,
struct ast_iterator *it,
ast_iterator_callback callback,
void *arg);
extern const char *ast_node_type_to_string(enum ast_node_type type);
extern struct ast_node *ast_iterator_peek(struct ast_iterator *it);
extern struct ast_node *ast_iterator_dequeue(struct ast_iterator *it);
extern void ast_iterator_enqueue(
struct ast_iterator *it,
struct ast_node *node);
#endif