Add object support
All checks were successful
CMake / ubuntu-latest - shared=OFF, pthread=OFF, posix=OFF (push) Successful in 13s
CMake / ubuntu-latest - shared=ON, pthread=OFF, posix=OFF (push) Successful in 11s
CMake / ubuntu-latest - shared=OFF, pthread=OFF, posix=ON (push) Successful in 13s
CMake / ubuntu-latest - shared=ON, pthread=OFF, posix=ON (push) Successful in 11s
CMake / ubuntu-latest - shared=OFF, pthread=ON, posix=ON (push) Successful in 11s
CMake / ubuntu-latest - shared=ON, pthread=ON, posix=ON (push) Successful in 11s

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
2025-07-27 21:56:54 +03:00
parent 93ddf7738c
commit 5fcbb97f7a
3 changed files with 89 additions and 19 deletions

View File

@@ -1014,6 +1014,26 @@ AST *Parser_parse(Parser *parser) { return parser_parse_value(parser); }
#define ALLOC(sz) (instance->alloc((sz)))
#define FREE(ptr) (instance->free((ptr)))
static Value *ensure_child_obj(Instance *inst, Value *parent, StringView key)
{
ValueObject *obj = &parent->v.o;
for (size_t i = 0; i < vector_size(obj->entryv); ++i) {
if (sv_eq(obj->entryv[i].k, key)) {
return obj->entryv[i].v;
}
}
Value *child = inst->alloc(sizeof *child);
child->instance = inst;
child->type = dcfg_ValueType_Object;
child->v.o.entryv = vector_create();
ValueObjectEntry e = { .k = key, .v = child };
vector_add(&obj->entryv, e);
return child;
}
Value *ast_to_value(dcfg_Instance *instance, AST *root)
{
Value *value = ALLOC(sizeof(*value));
@@ -1040,9 +1060,45 @@ Value *ast_to_value(dcfg_Instance *instance, AST *root)
value->type = dcfg_ValueType_Real;
value->v.r = root->v.r;
} else if (root->kind == ASTKind_Block) {
// FIXME: Implement
FREE(value);
return NULL;
value->type = dcfg_ValueType_Object;
value->v.o.entryv = vector_create();
for (size_t i = 0; i < vector_size(root->v.bl.entryv); ++i) {
ASTBlock_Entry *e = &root->v.bl.entryv[i];
Value *rhs = ast_to_value(instance, e->v);
if (!rhs) {
// FIXME: Free
return NULL;
}
Value *target = value;
StringView field;
if (e->k->kind == ASTKind_Key) {
field = e->k->v.s.s;
} else {
ASTMemberAccess *ma = &e->k->v.m;
for (size_t j = 0; j + 1 < vector_size(ma->accessv); ++j)
target = ensure_child_obj(instance, target, ma->accessv[j]);
field = ma->accessv[vector_size(ma->accessv) - 1];
}
ValueObject *obj = &target->v.o;
bool replaced = false;
for (size_t j = 0; j < vector_size(obj->entryv); ++j) {
if (sv_eq(obj->entryv[j].k, field)) {
dcfg_destroy(obj->entryv[j].v);
obj->entryv[j].v = rhs;
replaced = true;
break;
}
}
if (!replaced) {
ValueObjectEntry new_e = { .k = field, .v = rhs };
vector_add(&obj->entryv, new_e);
}
}
} else if (root->kind == ASTKind_Array) {
value->type = dcfg_ValueType_Array;
value->v.a.valuev = vector_create();