2026-05-17 15:33:44 +01:00
|
|
|
#include "compile-node.h"
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static struct bshell_ast_iterate_result do_compile_node(
|
|
|
|
|
struct bshell_ast_node *node,
|
|
|
|
|
enum bshell_ast_iteration_type it_type,
|
|
|
|
|
struct bshell_ast_iterator *it,
|
2026-05-17 15:33:44 +01:00
|
|
|
struct compile_ctx *ctx)
|
|
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
if (it_type != BSHELL_AST_ITERATION_POST) {
|
2026-05-17 15:33:44 +01:00
|
|
|
int flags = 0;
|
|
|
|
|
switch (node->n_type) {
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_AST_BLOCK:
|
2026-05-17 15:33:44 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
2026-05-25 10:33:29 +01:00
|
|
|
flags |= BSHELL_AST_ITERATE_ADD_CHILDREN;
|
2026-05-17 15:33:44 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
return BSHELL_AST_ITERATE_OK(flags);
|
2026-05-17 15:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx->c_depth = node->n_it.e_depth;
|
|
|
|
|
struct compile_state *state = compile_get_state(ctx);
|
|
|
|
|
while (state->s_depth >= ctx->c_depth && state->s_depth > 0) {
|
|
|
|
|
compile_pop_state(ctx);
|
|
|
|
|
state = compile_get_state(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
compile_node_impl impl = NULL;
|
|
|
|
|
|
|
|
|
|
if (node->n_type < state->s_type->s_nr_compile_node) {
|
|
|
|
|
impl = state->s_type->s_compile_node[node->n_type];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!impl) {
|
|
|
|
|
impl = state->s_type->s_compile_node_generic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!impl) {
|
2026-05-25 10:33:29 +01:00
|
|
|
return BSHELL_AST_ITERATE_OK(0);
|
2026-05-17 15:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct compile_result result = impl(ctx, node);
|
|
|
|
|
unsigned int flags = 0;
|
|
|
|
|
if (result.r_flags & COMPILE_REPEAT_NODE) {
|
2026-05-25 10:33:29 +01:00
|
|
|
flags |= BSHELL_AST_ITERATE_REPEAT;
|
2026-05-17 15:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.r_status == BSHELL_SUCCESS) {
|
2026-05-25 10:33:29 +01:00
|
|
|
return BSHELL_AST_ITERATE_OK(flags);
|
2026-05-17 15:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
return BSHELL_AST_ITERATE_ERR(result.r_status);
|
2026-05-17 15:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum bshell_status compile_expression(
|
|
|
|
|
struct compile_ctx *ctx,
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_node *src)
|
2026-05-17 15:33:44 +01:00
|
|
|
{
|
|
|
|
|
compile_push_state(ctx, COMPILE_NORMAL);
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_ast_iterator it = {0};
|
|
|
|
|
bshell_ast_node_iterate(
|
|
|
|
|
src,
|
|
|
|
|
&it,
|
|
|
|
|
(bshell_ast_iterator_callback)do_compile_node,
|
|
|
|
|
ctx);
|
2026-05-17 15:33:44 +01:00
|
|
|
|
|
|
|
|
struct compile_value *result = compile_pop_value(ctx);
|
|
|
|
|
if (result) {
|
|
|
|
|
compile_value_load(ctx, result);
|
|
|
|
|
compile_value_destroy(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|