From c262b53eab7ba592c172f241feb24be0ba6d2b9f Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 20 Jun 2026 15:09:10 +0100 Subject: [PATCH] runtime: move variable definition from runtime to individual scopes --- bshell.runtime/command/function.c | 5 ++-- .../include/bshell/runtime/runtime.h | 5 ++-- bshell.runtime/include/bshell/runtime/scope.h | 4 ++++ bshell.runtime/runtime/eval.c | 5 +++- bshell.runtime/runtime/runtime.c | 24 +++++-------------- bshell.runtime/runtime/scope.c | 17 +++++++++++++ bshell.runtime/runtime/scope.h | 10 ++++---- 7 files changed, 42 insertions(+), 28 deletions(-) diff --git a/bshell.runtime/command/function.c b/bshell.runtime/command/function.c index 9fd40be..4b85885 100644 --- a/bshell.runtime/command/function.c +++ b/bshell.runtime/command/function.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -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); } diff --git a/bshell.runtime/include/bshell/runtime/runtime.h b/bshell.runtime/include/bshell/runtime/runtime.h index 1bb6aee..f216ab3 100644 --- a/bshell.runtime/include/bshell/runtime/runtime.h +++ b/bshell.runtime/include/bshell/runtime/runtime.h @@ -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( diff --git a/bshell.runtime/include/bshell/runtime/scope.h b/bshell.runtime/include/bshell/runtime/scope.h index f928998..ddde8df 100644 --- a/bshell.runtime/include/bshell/runtime/scope.h +++ b/bshell.runtime/include/bshell/runtime/scope.h @@ -1,11 +1,15 @@ #ifndef BSHELL_RUNTIME_SCOPE_H_ #define BSHELL_RUNTIME_SCOPE_H_ +#include #include 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 diff --git a/bshell.runtime/runtime/eval.c b/bshell.runtime/runtime/eval.c index 75f271c..ba9a3d0 100644 --- a/bshell.runtime/runtime/eval.c +++ b/bshell.runtime/runtime/eval.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -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); diff --git a/bshell.runtime/runtime/runtime.c b/bshell.runtime/runtime/runtime.c index a716ef5..6510d32 100644 --- a/bshell.runtime/runtime/runtime.c +++ b/bshell.runtime/runtime/runtime.c @@ -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) { diff --git a/bshell.runtime/runtime/scope.c b/bshell.runtime/runtime/scope.c index 7617ca1..e4d9711 100644 --- a/bshell.runtime/runtime/scope.c +++ b/bshell.runtime/runtime/scope.c @@ -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; +} diff --git a/bshell.runtime/runtime/scope.h b/bshell.runtime/runtime/scope.h index 5fe3d5f..593411f 100644 --- a/bshell.runtime/runtime/scope.h +++ b/bshell.runtime/runtime/scope.h @@ -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,