runtime: move variable definition from runtime to individual scopes

This commit is contained in:
2026-06-20 15:09:10 +01:00
parent c2d3e05430
commit c262b53eab
7 changed files with 42 additions and 28 deletions
+3 -2
View File
@@ -2,6 +2,7 @@
#include <bshell/command/function.h>
#include <bshell/runtime/pipeline.h>
#include <bshell/runtime/runtime.h>
#include <bshell/runtime/scope.h>
#include <bshell/runtime/script-block.h>
#include <bshell/status.h>
#include <fx/collections/array.h>
@@ -72,8 +73,8 @@ static enum bshell_status process_record(
break;
}
bshell_variable *var = bshell_runtime_define_var(
rt,
bshell_variable *var = bshell_runtime_scope_define_variable(
bshell_runtime_get_current_scope(rt),
fx_string_get_cstr(param));
bshell_variable_set_value(var, *arg);
}
@@ -32,9 +32,6 @@ extern fx_value bshell_runtime_eval_script(
extern bshell_variable *bshell_runtime_find_var(
struct bshell_runtime *rt,
const char *name);
extern bshell_variable *bshell_runtime_define_var(
struct bshell_runtime *rt,
const char *name);
extern enum bshell_status bshell_runtime_define_function(
struct bshell_runtime *rt,
bshell_function *func);
@@ -49,6 +46,8 @@ extern enum bshell_status bshell_runtime_push_scope(
extern enum bshell_status bshell_runtime_pop_scope(struct bshell_runtime *rt);
extern fx_value bshell_runtime_eval(struct bshell_runtime *rt);
extern struct bshell_runtime_scope *bshell_runtime_get_global_scope(
struct bshell_runtime *rt);
extern struct bshell_runtime_scope *bshell_runtime_get_current_scope(
struct bshell_runtime *rt);
extern struct bshell_runtime_scope *bshell_runtime_get_parent_scope(
@@ -1,11 +1,15 @@
#ifndef BSHELL_RUNTIME_SCOPE_H_
#define BSHELL_RUNTIME_SCOPE_H_
#include <bshell/runtime/var.h>
#include <fx/collections/hashtable.h>
struct bshell_runtime_scope;
extern fx_hashtable *bshell_runtime_scope_get_functions(
struct bshell_runtime_scope *scope);
extern bshell_variable *bshell_runtime_scope_define_variable(
struct bshell_runtime_scope *scope,
const char *name);
#endif
+4 -1
View File
@@ -4,6 +4,7 @@
#include <bshell/runtime/cmdcall.h>
#include <bshell/runtime/pipeline.h>
#include <bshell/runtime/runtime.h>
#include <bshell/runtime/scope.h>
#include <bshell/status.h>
#include <fx/collections/array.h>
#include <fx/collections/hashtable.h>
@@ -250,7 +251,9 @@ static enum bshell_status eval_instruction(
fx_value_get_cstr(pool_value, &s);
var = bshell_runtime_find_var(rt, s);
if (!var) {
var = bshell_runtime_define_var(rt, s);
var = bshell_runtime_scope_define_variable(
bshell_runtime_get_current_scope(rt),
s);
}
bshell_variable_set_value(var, x);
+6 -18
View File
@@ -68,24 +68,6 @@ bshell_variable *bshell_runtime_find_var(
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)
@@ -149,6 +131,12 @@ bshell_command *bshell_runtime_find_command(
return NULL;
}
struct bshell_runtime_scope *bshell_runtime_get_global_scope(
struct bshell_runtime *rt)
{
return rt->rt_global;
}
struct bshell_runtime_scope *bshell_runtime_get_current_scope(
struct bshell_runtime *rt)
{
+17
View File
@@ -116,3 +116,20 @@ fx_hashtable *bshell_runtime_scope_get_functions(
{
return scope->s_functions;
}
bshell_variable *bshell_runtime_scope_define_variable(
struct bshell_runtime_scope *scope,
const char *name)
{
if (!scope) {
return NULL;
}
bshell_variable *var = bshell_variable_create(name);
if (!var) {
return NULL;
}
var_map_put(&scope->s_vars, var);
return var;
}
+6 -4
View File
@@ -1,5 +1,5 @@
#ifndef BSHELL_RUNTIME_SCOPE_H_
#define BSHELL_RUNTIME_SCOPE_H_
#ifndef RUNTIME_SCOPE_H_
#define RUNTIME_SCOPE_H_
#include "var-map.h"
@@ -37,12 +37,14 @@ extern struct bshell_runtime_scope *runtime_push_scope(
enum bshell_runtime_scope_type type,
bshell_scriptblock *block);
extern void runtime_pop_scope(struct bshell_runtime *rt);
extern struct bshell_runtime_scope *runtime_get_scope(struct bshell_runtime *rt);
extern struct bshell_runtime_scope *runtime_get_scope(
struct bshell_runtime *rt);
extern void bshell_runtime_scope_push_value(
struct bshell_runtime_scope *scope,
const fx_value *value);
extern fx_value bshell_runtime_scope_pop_value(struct bshell_runtime_scope *scope);
extern fx_value bshell_runtime_scope_pop_value(
struct bshell_runtime_scope *scope);
extern void bshell_runtime_scope_set_block(
struct bshell_runtime_scope *scope,