From 8f7a0b9fb82c1d4229d67e39c9146ae75ef4afb9 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 24 May 2026 20:21:39 +0100 Subject: [PATCH] runtime: add a class to represent variables and a data structure to store them --- bshell/runtime/var-map.c | 35 ++++++++++++++ bshell/runtime/var-map.h | 23 ++++++++++ bshell/runtime/var.c | 98 ++++++++++++++++++++++++++++++++++++++++ bshell/runtime/var.h | 26 +++++++++++ 4 files changed, 182 insertions(+) create mode 100644 bshell/runtime/var-map.c create mode 100644 bshell/runtime/var-map.h create mode 100644 bshell/runtime/var.c create mode 100644 bshell/runtime/var.h diff --git a/bshell/runtime/var-map.c b/bshell/runtime/var-map.c new file mode 100644 index 0000000..55a3cde --- /dev/null +++ b/bshell/runtime/var-map.c @@ -0,0 +1,35 @@ +#include "var-map.h" + +void var_map_cleanup(struct var_map *map) +{ +} + +enum bshell_status var_map_put(struct var_map *map, bshell_variable *var) +{ + const char *name = bshell_variable_get_name(var); + struct var_map_entry *entry = malloc(sizeof *entry); + if (!entry) { + return BSHELL_ERR_NO_MEMORY; + } + + memset(entry, 0x0, sizeof *entry); + + entry->e_var = var; + + fx_namemap_put(&map->m_vars, name, &entry->e_entry); + return BSHELL_SUCCESS; +} + +bshell_variable *var_map_get(struct var_map *map, const char *name) +{ + fx_namemap_entry *e = fx_namemap_get(&map->m_vars, name); + if (!e) { + return NULL; + } + + struct var_map_entry *entry = fx_unbox( + struct var_map_entry, + e, + e_entry); + return entry->e_var; +} diff --git a/bshell/runtime/var-map.h b/bshell/runtime/var-map.h new file mode 100644 index 0000000..d24fff8 --- /dev/null +++ b/bshell/runtime/var-map.h @@ -0,0 +1,23 @@ +#ifndef RUNTIME_VAR_MAP_H_ +#define RUNTIME_VAR_MAP_H_ + +#include "../status.h" +#include "var.h" + +struct var_map_entry { + bshell_variable *e_var; + fx_namemap_entry e_entry; +}; + +struct var_map { + fx_namemap m_vars; +}; + +extern void var_map_cleanup(struct var_map *map); + +extern enum bshell_status var_map_put( + struct var_map *map, + bshell_variable *var); +extern bshell_variable *var_map_get(struct var_map *map, const char *name); + +#endif diff --git a/bshell/runtime/var.c b/bshell/runtime/var.c new file mode 100644 index 0000000..cc6ccd0 --- /dev/null +++ b/bshell/runtime/var.c @@ -0,0 +1,98 @@ +#include "var.h" + +#include +#include +#include + +struct bshell_variable_p { + char *var_name; + fx_value var_value; +}; + +static void variable_fini(fx_object *obj, void *priv) +{ + struct bshell_variable_p *block = priv; + + if (block->var_name) { + free(block->var_name); + } + + fx_value_unset(&block->var_value); +} + +static const char *variable_get_name(struct bshell_variable_p *var) +{ + return var->var_name; +} + +static const fx_value *variable_get_value(const struct bshell_variable_p *var) +{ + return &var->var_value; +} + +static void variable_set_value(struct bshell_variable_p *var, fx_value *value) +{ + fx_value_unset(&var->var_value); + fx_value_copy(&var->var_value, value); +} + +bshell_variable *bshell_variable_create(const char *name) +{ + bshell_variable *var = fx_object_create(BSHELL_TYPE_VARIABLE); + if (!var) { + return NULL; + } + + struct bshell_variable_p *p = fx_object_get_private( + var, + BSHELL_TYPE_VARIABLE); + + p->var_name = fx_strdup(name); + if (!p->var_name) { + bshell_variable_unref(var); + return NULL; + } + + return var; +} + +const char *bshell_variable_get_name(const bshell_variable *var) +{ + FX_CLASS_DISPATCH_STATIC_0( + BSHELL_TYPE_VARIABLE, + variable_get_name, + var); +} + +const fx_value *bshell_variable_get_value(const bshell_variable *var) +{ + FX_CLASS_DISPATCH_STATIC_0( + BSHELL_TYPE_VARIABLE, + variable_get_value, + var); +} + +void bshell_variable_set_value(bshell_variable *var, fx_value *value) +{ + FX_CLASS_DISPATCH_STATIC_V( + BSHELL_TYPE_VARIABLE, + variable_set_value, + var, + value); +} + +FX_TYPE_CLASS_BEGIN(bshell_variable) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) + + FX_TYPE_CONSTRUCTOR("create", bshell_variable_create, 1, FX_TYPE_CSTR); +FX_TYPE_CLASS_END(bshell_variable) + +FX_TYPE_DEFINITION_BEGIN(bshell_variable) + FX_TYPE_ID(0x4e93b9d9, 0x2c11, 0x4fd7, 0xa9ce, 0x41b7cd20f0d4); + FX_TYPE_NAME("bshell.variable"); + FX_TYPE_CLASS(bshell_variable_class); + FX_TYPE_INSTANCE_PRIVATE(struct bshell_variable_p); + FX_TYPE_INSTANCE_FINI(variable_fini); +FX_TYPE_DEFINITION_END(bshell_variable) diff --git a/bshell/runtime/var.h b/bshell/runtime/var.h new file mode 100644 index 0000000..7ef2a7d --- /dev/null +++ b/bshell/runtime/var.h @@ -0,0 +1,26 @@ +#ifndef RUNTIME_VAR_H_ +#define RUNTIME_VAR_H_ + +#include +#include + +FX_DECLS_BEGIN; + +#define BSHELL_TYPE_VARIABLE (bshell_variable_get_type()) + +FX_DECLARE_TYPE(bshell_variable); + +FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_variable) +FX_TYPE_CLASS_DECLARATION_END(bshell_variable) + +extern fx_type_id bshell_variable_get_type(void); + +extern bshell_variable *bshell_variable_create(const char *name); + +extern const char *bshell_variable_get_name(const bshell_variable *var); +extern const fx_value *bshell_variable_get_value(const bshell_variable *var); +extern void bshell_variable_set_value(bshell_variable *var, fx_value *value); + +FX_DECLS_END; + +#endif