Files
bshell/bshell.runtime/runtime/runtime.c
T
2026-05-30 19:52:36 +01:00

146 lines
3.0 KiB
C

#include "scope.h"
#include <bshell/runtime/runtime.h>
#include <stdlib.h>
#include <string.h>
struct bshell_runtime *bshell_runtime_create(void)
{
struct bshell_runtime *out = malloc(sizeof *out);
if (!out) {
return NULL;
}
memset(out, 0x0, sizeof *out);
out->rt_global = runtime_push_scope(out, RUNTIME_SCOPE_GLOBAL, NULL);
out->rt_aliases = fx_hashtable_create();
return out;
}
void bshell_runtime_destroy(struct bshell_runtime *rt)
{
while (!fx_queue_empty(&rt->rt_scope)) {
runtime_pop_scope(rt);
}
fx_hashtable_unref(rt->rt_aliases);
free(rt);
}
bshell_variable *bshell_runtime_find_var(
struct bshell_runtime *rt,
const char *name)
{
bshell_variable *var = NULL;
fx_queue_entry *cur = fx_queue_last(&rt->rt_scope);
while (cur) {
struct bshell_runtime_scope *scope
= fx_unbox(struct bshell_runtime_scope, cur, s_entry);
var = var_map_get(&scope->s_vars, name);
if (var) {
break;
}
cur = fx_queue_prev(cur);
}
return var;
}
bshell_variable *bshell_runtime_define_var(
struct bshell_runtime *rt,
const char *name)
{
struct bshell_runtime_scope *scope = runtime_get_scope(rt);
if (!scope) {
return NULL;
}
bshell_variable *var = bshell_variable_create(name);
if (!var) {
return NULL;
}
var_map_put(&scope->s_vars, var);
return var;
}
enum bshell_status bshell_runtime_define_function(
struct bshell_runtime *rt,
bshell_function *func)
{
struct bshell_runtime_scope *scope = runtime_get_scope(rt);
if (!scope) {
return BSHELL_ERR_BAD_STATE;
}
fx_string *name
= fx_string_create_from_cstr(bshell_function_get_name(func));
if (!name) {
return BSHELL_ERR_NO_MEMORY;
}
fx_string_transform_lowercase(name);
fx_hashtable_put(
scope->s_functions,
&FX_VALUE_OBJECT(name),
&FX_VALUE_OBJECT(func));
fx_string_unref(name);
return BSHELL_SUCCESS;
}
bshell_command *bshell_runtime_find_command(
struct bshell_runtime *rt,
const char *callable_name)
{
bshell_command *alias = NULL, *func = NULL;
const fx_value *alias_v
= fx_hashtable_get(rt->rt_aliases, &FX_CSTR(callable_name));
if (alias_v) {
fx_value_get_object(alias_v, &alias);
bshell_command_ref(alias);
return alias;
}
fx_queue_entry *cur = fx_queue_last(&rt->rt_scope);
while (cur) {
struct bshell_runtime_scope *scope
= fx_unbox(struct bshell_runtime_scope, cur, s_entry);
const fx_value *func_v = fx_hashtable_get(
scope->s_functions,
&FX_CSTR(callable_name));
if (func_v) {
fx_value_get_object(func_v, &func);
bshell_command_ref(func);
return func;
}
cur = fx_queue_prev(cur);
}
bshell_command *cmd = bshell_command_find_static(callable_name);
if (cmd) {
return cmd;
}
/* TODO find native executables */
return NULL;
}
struct bshell_runtime_scope *bshell_runtime_get_current_scope(
struct bshell_runtime *rt)
{
return runtime_get_scope(rt);
}
struct bshell_runtime_scope *bshell_runtime_get_parent_scope(
struct bshell_runtime *rt,
struct bshell_runtime_scope *scope)
{
fx_queue_entry *parent = fx_queue_prev(&scope->s_entry);
return fx_unbox(struct bshell_runtime_scope, parent, s_entry);
}