diff --git a/include/ground.h b/include/ground.h index 71a36ed..7f7b932 100644 --- a/include/ground.h +++ b/include/ground.h @@ -305,8 +305,17 @@ struct GroundBytecodeFunction { // Only for native function GroundType* returnType; }; -struct GroundBytecodeStruct {}; -struct GroundBytecodeObject {}; + +struct GroundBytecodeStruct { + GroundBytecodeValue* values; + size_t size; + size_t capacity; +}; +struct GroundBytecodeObject { + GroundBytecodeValue* values; + size_t size; + size_t capacity; +}; struct GroundBytecodeValue { union { diff --git a/src/New/BytecodeValue.c b/src/New/BytecodeValue.c index 1bf4a31..cd70002 100644 --- a/src/New/BytecodeValue.c +++ b/src/New/BytecodeValue.c @@ -52,6 +52,60 @@ static inline GroundBytecodeFunction doFunction(GroundFunction* function) { return bf; } +static inline GroundBytecodeStruct doStruct(GroundStruct* gs) { + GroundBytecodeStruct gbs = { + .values = malloc(sizeof(GroundBytecodeValue) * 16), + .capacity = 16, + .size = 0 + }; + + GroundObjectField *s, *tmp; + + HASH_ITER(hh, gs->fields, s, tmp) { + if (gbs.size >= gbs.capacity) { + GroundBytecodeValue* tmp = malloc(sizeof(GroundBytecodeValue) * gbs.capacity * 2); + if (tmp == NULL) { + Ground.Log.Error("malloc failedd in Ground.New.BytecodeValue -> doStruct"); + Ground.Flags.error = true; + return gbs; + } + gbs.values = tmp; + gbs.capacity *= 2; + } + gbs.values[gbs.size] = Ground.New.BytecodeValue(s->value); + gbs.size++; + } + + return gbs; +} + +static inline GroundBytecodeObject doObject(GroundObject* go) { + GroundBytecodeObject gbo = { + .values = malloc(sizeof(GroundBytecodeValue) * 16), + .capacity = 16, + .size = 0 + }; + + GroundObjectField *s, *tmp; + + HASH_ITER(hh, go->fields, s, tmp) { + if (gbo.size >= gbo.capacity) { + GroundBytecodeValue* tmp = malloc(sizeof(GroundBytecodeValue) * gbo.capacity * 2); + if (tmp == NULL) { + Ground.Log.Error("malloc failedd in Ground.New.BytecodeValue -> doStruct"); + Ground.Flags.error = true; + return gbo; + } + gbo.values = tmp; + gbo.capacity *= 2; + } + gbo.values[gbo.size] = Ground.New.BytecodeValue(s->value); + gbo.size++; + } + + return gbo; +} + GroundBytecodeValue _GroundNewBytecodeValue(GroundValue value) { GroundBytecodeValue bv = { @@ -89,11 +143,11 @@ GroundBytecodeValue _GroundNewBytecodeValue(GroundValue value) { break; } case GroundType_Struct: { - // TODO - convert to bytecode struct + bv.as.Struct = doStruct(&value.as.Struct); break; } case GroundType_Object: { - // TODO - convert to bytecode object + bv.as.Object = doObject(&value.as.Object); break; } case GroundType_CoreType: {