#include "compile-node.h" enum bshell_status cmdcall_load( struct compile_ctx *ctx, struct compile_value *value) { struct bshell_cmdcall_ast_node *cmdcall; cmdcall = (struct bshell_cmdcall_ast_node *)value->v_node; bshell_scriptblock_push_instruction( ctx->c_block, BSHELL_OPCODE_PEXEC, 1); return BSHELL_SUCCESS; } enum bshell_status pipeline_load( struct compile_ctx *ctx, struct compile_value *value) { struct bshell_pipeline_ast_node *pipeline; pipeline = (struct bshell_pipeline_ast_node *)value->v_node; size_t nr_items = fx_queue_length(&pipeline->n_stages); bshell_scriptblock_push_instruction( ctx->c_block, BSHELL_OPCODE_PEXEC, nr_items); return BSHELL_SUCCESS; } static const struct compile_value_type cmdcall_type = { .v_load = cmdcall_load, }; static const struct compile_value_type pipeline_type = { .v_load = pipeline_load, }; struct compile_result compile_cmdcall( struct compile_ctx *ctx, struct bshell_ast_node *src) { struct bshell_cmdcall_ast_node *cmdcall; cmdcall = (struct bshell_cmdcall_ast_node *)src; size_t nr_args = fx_queue_length(&cmdcall->n_args); for (size_t i = 0; i < nr_args; i++) { struct compile_value *arg = compile_pop_value(ctx); compile_value_load(ctx, arg); compile_value_destroy(arg); } bshell_scriptblock_push_instruction( ctx->c_block, BSHELL_OPCODE_LDCMD, nr_args); compile_push_value(ctx, &cmdcall_type, src); return COMPILE_OK(0); } struct compile_result compile_pipeline( struct compile_ctx *ctx, struct bshell_ast_node *src) { struct bshell_pipeline_ast_node *pipeline; pipeline = (struct bshell_pipeline_ast_node *)src; size_t nr_items = fx_queue_length(&pipeline->n_stages); for (size_t i = 0; i < nr_items; i++) { struct compile_value *item = compile_pop_value(ctx); if (item->v_node->n_type != BSHELL_AST_CMDCALL) { compile_value_load(ctx, item); } compile_value_destroy(item); } compile_push_value(ctx, &pipeline_type, src); return COMPILE_OK(0); }