object initialisation

This commit is contained in:
2026-07-08 12:06:18 +10:00
parent b507d79804
commit 6929f055de
4 changed files with 50 additions and 55 deletions

View File

@@ -17,32 +17,6 @@
#define END(res) (struct GroundExecutionResult) { .type = _GER_END, .as.end = res } #define END(res) (struct GroundExecutionResult) { .type = _GER_END, .as.end = res }
#define RETURN(val) (struct GroundExecutionResult) { .type = _GER_RETURN, .as.value = val } #define RETURN(val) (struct GroundExecutionResult) { .type = _GER_RETURN, .as.value = val }
static void printValue(GroundBytecodeValue* val) {
switch (val->type.type) {
case GroundType_Int:
printf("%" PRId64, val->as.Int);
break;
case GroundType_Double:
printf("%f", val->as.Double);
break;
case GroundType_Bool:
printf(val->as.Bool ? "true" : "false");
break;
case GroundType_Char:
printf("%c", val->as.Char);
break;
case GroundType_String:
printf("%s", val->as.String.cstr);
break;
case GroundType_Function:
printf("<function>");
break;
default:
printf("<fixme opt=%d>", val->type.type);
break;
}
}
static ffi_type* ffiTypeFromGroundType(GroundType* type) { static ffi_type* ffiTypeFromGroundType(GroundType* type) {
switch (type->type) { switch (type->type) {
case GroundType_Int: return &ffi_type_sint64; case GroundType_Int: return &ffi_type_sint64;
@@ -101,15 +75,13 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
} }
PRINT: { PRINT: {
for (GroundSize i = 0; i < instruction->args.len; i++) { for (GroundSize i = 0; i < instruction->args.len; i++) {
printValue(HEAP_GET(heap, instruction->args.at[i])); printf("%s ", Ground.Stringify.BytecodeValue(HEAP_GET(heap, instruction->args.at[i])));
printf(" ");
} }
return CONTINUE; return CONTINUE;
} }
PRINTLN: { PRINTLN: {
for (GroundSize i = 0; i < instruction->args.len; i++) { for (GroundSize i = 0; i < instruction->args.len; i++) {
printValue(HEAP_GET(heap, instruction->args.at[i])); printf("%s ", Ground.Stringify.BytecodeValue(HEAP_GET(heap, instruction->args.at[i])));
printf(" ");
} }
printf("\n"); printf("\n");
return CONTINUE; return CONTINUE;
@@ -935,7 +907,8 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
for (GroundSize i = 1; i < instruction->args.len; i++) { for (GroundSize i = 1; i < instruction->args.len; i++) {
GroundSize offset = instruction->args.at[i]; GroundSize offset = instruction->args.at[i];
i++; i++;
switch (i) { GroundSize op = instruction->args.at[i];
switch (op) {
case 0: break; case 0: break;
case 1: { case 1: {
i++; i++;
@@ -1024,7 +997,16 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
GroundBytecodeValue* type = HEAP_GET(heap, instruction->args.at[1]); GroundBytecodeValue* type = HEAP_GET(heap, instruction->args.at[1]);
switch (type->type.type) { switch (type->type.type) {
case GroundType_Struct: { case GroundType_Struct: {
// TODO nested structs // copy the contents of the struct into the object
GroundBytecodeObject object = {
.size = type->as.Struct.size,
.capacity = type->as.Struct.size,
.values = malloc(sizeof(GroundBytecodeValue) * type->as.Struct.size)
};
for (GroundSize i = 0; i < type->as.Struct.size; i++) {
object.values[i] = Ground.Copy.BytecodeValue(&type->as.Struct.values[i]);
}
HEAP_SET(heap, instruction->args.at[0], ((GroundBytecodeValue) {.as.Object = object, .type = {GroundType_Object}}));
break; break;
} }
case GroundType_CoreType: { case GroundType_CoreType: {
@@ -1052,6 +1034,7 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
default: break; // TODO implement all the other stuff default: break; // TODO implement all the other stuff
} }
} }
default: break; // should not be reached
} }
return CONTINUE; return CONTINUE;
} }

View File

@@ -110,12 +110,12 @@ static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, Ground
s->offset = i++; s->offset = i++;
} }
// Now go through and add offsets to everything // Now go through and add offsets to everything (skip struct name at index 0)
for (GroundSize j = 0; j < inst->args.len; j++) { for (GroundSize j = 1; j < inst->args.len; j++) {
// struct field name // struct field name
GroundArg* arg = &inst->args.at[j]; GroundArg* arg = &inst->args.at[j];
GroundObjectField* field = NULL; GroundObjectField* field = NULL;
HASH_FIND_STR(state->variables, arg->as.ref->string, field); HASH_FIND_STR(gs->fields, arg->as.ref->string, field);
if (field == NULL) { if (field == NULL) {
Ground.Log.Error("unexpected null struct field in Ground.New.Bytecode -> doOffsets"); Ground.Log.Error("unexpected null struct field in Ground.New.Bytecode -> doOffsets");
Ground.Flags.error = true; Ground.Flags.error = true;

View File

@@ -156,6 +156,11 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt
// If we know the value type now, add it to the instruction // If we know the value type now, add it to the instruction
if (instruction->args.at[1].type == GroundArg_Value) { if (instruction->args.at[1].type == GroundArg_Value) {
field->value = Ground.Copy.Value(&instruction->args.at[1].as.value); field->value = Ground.Copy.Value(&instruction->args.at[1].as.value);
HASH_ADD_STR(gs.fields, name, field);
// set &field $value -> &field 2 $value
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[0]));
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 2});
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[1]));
break; break;
} }
@@ -178,29 +183,21 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt
snprintf(field->name, 2047, "%s", instruction->args.at[0].as.ref->string); snprintf(field->name, 2047, "%s", instruction->args.at[0].as.ref->string);
// If we know the type now, add it to the instruction // If we know the type now, add it to the instruction
if (strcmp(field->name, "int") == 0) { char* typeName = instruction->args.at[1].as.ref->string;
if (strcmp(typeName, "int") == 0) {
field->value = Ground.New.Value.Int(0); field->value = Ground.New.Value.Int(0);
break; } else if (strcmp(typeName, "double") == 0) {
}
if (strcmp(field->name, "double") == 0) {
field->value = Ground.New.Value.Double(0.0); field->value = Ground.New.Value.Double(0.0);
break; } else if (strcmp(typeName, "string") == 0) {
}
if (strcmp(field->name, "string") == 0) {
field->value = Ground.New.Value.String(Ground.New.String("")); field->value = Ground.New.Value.String(Ground.New.String(""));
break; } else if (strcmp(typeName, "char") == 0) {
}
if (strcmp(field->name, "char") == 0) {
field->value = Ground.New.Value.Char(0); field->value = Ground.New.Value.Char(0);
break; } else if (strcmp(typeName, "bool") == 0) {
}
if (strcmp(field->name, "bool") == 0) {
field->value = Ground.New.Value.Bool(false); field->value = Ground.New.Value.Bool(false);
break; } else if (strcmp(typeName, "function") == 0) {
}
if (strcmp(field->name, "function") == 0) {
field->value = Ground.New.Value.Function(Ground.New.Function(state)); field->value = Ground.New.Value.Function(Ground.New.Function(state));
break; } else {
field->value = Ground.New.Value.Int(0);
} }
HASH_ADD_STR(gs.fields, name, field); HASH_ADD_STR(gs.fields, name, field);
@@ -229,7 +226,7 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt
newProgram->len--; newProgram->len--;
// Add 3 to signal addition of closure // Add 3 to signal addition of closure
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 1}); Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 3});
break; break;
} }
case GroundInstruction_ENDSTRUCT: { case GroundInstruction_ENDSTRUCT: {

View File

@@ -1,4 +1,5 @@
#include "../../include/ground.h" #include "../../include/ground.h"
#include "../include/estr.h"
#include <inttypes.h> #include <inttypes.h>
char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value) { char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value) {
@@ -53,10 +54,24 @@ char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value) {
// TODO implement function stringification // TODO implement function stringification
} }
case GroundType_Struct: { case GroundType_Struct: {
// TODO implement struct stringification Estr str = CREATE_ESTR("<struct fields: { ");
for (GroundSize i = 0; i < value->as.Struct.size; i++) {
char* field = Ground.Stringify.BytecodeValue(&value->as.Struct.values[i]);
APPEND_ESTR(str, field);
free(field);
}
APPEND_ESTR(str, " }>");
return str.str;
} }
case GroundType_Object: { case GroundType_Object: {
// TODO implement object stringification Estr str = CREATE_ESTR("<object fields: { ");
for (GroundSize i = 0; i < value->as.Struct.size; i++) {
char* field = Ground.Stringify.BytecodeValue(&value->as.Struct.values[i]);
APPEND_ESTR(str, field);
free(field);
}
APPEND_ESTR(str, " }>");
return str.str;
} }
} }