bshell: implement bytecode generation for arithmetic expressions and if-statements
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
static struct ast_iterate_result do_compile_node(
|
||||
struct ast_node *node,
|
||||
enum ast_iteration_type it_type,
|
||||
struct ast_iterator *it,
|
||||
struct compile_ctx *ctx)
|
||||
{
|
||||
if (it_type != AST_ITERATION_POST) {
|
||||
int flags = 0;
|
||||
switch (node->n_type) {
|
||||
case AST_BLOCK:
|
||||
break;
|
||||
default:
|
||||
flags |= AST_ITERATE_ADD_CHILDREN;
|
||||
break;
|
||||
}
|
||||
|
||||
return AST_ITERATE_OK(flags);
|
||||
}
|
||||
|
||||
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) {
|
||||
return AST_ITERATE_OK(0);
|
||||
}
|
||||
|
||||
struct compile_result result = impl(ctx, node);
|
||||
unsigned int flags = 0;
|
||||
if (result.r_flags & COMPILE_REPEAT_NODE) {
|
||||
flags |= AST_ITERATE_REPEAT;
|
||||
}
|
||||
|
||||
if (result.r_status == BSHELL_SUCCESS) {
|
||||
return AST_ITERATE_OK(flags);
|
||||
}
|
||||
|
||||
return AST_ITERATE_ERR(result.r_status);
|
||||
}
|
||||
|
||||
enum bshell_status compile_expression(
|
||||
struct compile_ctx *ctx,
|
||||
struct ast_node *src)
|
||||
{
|
||||
compile_push_state(ctx, COMPILE_NORMAL);
|
||||
|
||||
struct ast_iterator it = {0};
|
||||
ast_node_iterate(src, &it, (ast_iterator_callback)do_compile_node, ctx);
|
||||
|
||||
struct compile_value *result = compile_pop_value(ctx);
|
||||
if (result) {
|
||||
compile_value_load(ctx, result);
|
||||
compile_value_destroy(result);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user