runtime: add a class to represent variables and a data structure to store them

This commit is contained in:
2026-05-24 20:21:39 +01:00
parent b6aeb8d4bf
commit 8f7a0b9fb8
4 changed files with 182 additions and 0 deletions
+35
View File
@@ -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;
}
+23
View File
@@ -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
+98
View File
@@ -0,0 +1,98 @@
#include "var.h"
#include <fx/reflection/function.h>
#include <fx/string.h>
#include <fx/vector.h>
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)
+26
View File
@@ -0,0 +1,26 @@
#ifndef RUNTIME_VAR_H_
#define RUNTIME_VAR_H_
#include <fx/macros.h>
#include <fx/value.h>
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