Keep working on structs
This commit is contained in:
@@ -13,7 +13,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
||||
if (label == NULL) {
|
||||
label = malloc(sizeof(GroundLabel));
|
||||
if (label == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.Internal.run()");
|
||||
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doLabels");
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
}
|
||||
@@ -33,7 +33,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
||||
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
||||
if (line == NULL) {
|
||||
char buf[2048];
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction JUMP at %zu) in Ground.Internal.Run()", arg->as.ref->string, i);
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction JUMP at %zu) in Ground.New.Bytecode -> doLabels", arg->as.ref->string, i);
|
||||
Ground.Log.Error(buf);
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
@@ -46,7 +46,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
||||
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
||||
if (line == NULL) {
|
||||
char buf[2048];
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction IF at %zu) in Ground.Internal.Run()", arg->as.ref->string, i);
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction IF at %zu) in Ground.New.Bytecode -> doLabels", arg->as.ref->string, i);
|
||||
Ground.Log.Error(buf);
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
@@ -59,7 +59,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
||||
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
||||
if (line == NULL) {
|
||||
char buf[2048];
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction CATCH at %zu) in Ground.Internal.Run()", arg->as.ref->string, i);
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction CATCH at %zu) in Ground.New.Bytecode -> doLabels", arg->as.ref->string, i);
|
||||
Ground.Log.Error(buf);
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
@@ -85,6 +85,92 @@ static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, Ground
|
||||
}
|
||||
|
||||
for (GroundSize i = 0; i < gp->len; i++) {
|
||||
// Handle instructions which deal with struct fields
|
||||
switch (gp->at[i].type) {
|
||||
case GroundInstruction_STRUCT: {
|
||||
GroundInstruction* inst = &gp->at[i];
|
||||
GroundVariable* gsv = NULL;
|
||||
HASH_FIND_STR(state->variables, inst->args.at[0].as.ref->string, gsv);
|
||||
if (gsv == NULL) {
|
||||
Ground.Log.Error("unexpected null variable in Ground.New.Bytecode -> doOffsets");
|
||||
Ground.Flags.error = true;
|
||||
return 0;
|
||||
}
|
||||
if (gsv->value.type.type != GroundType_Struct) {
|
||||
Ground.Log.Error("unexpected null variable in Ground.New.Bytecode -> doOffsets");
|
||||
Ground.Flags.error = true;
|
||||
return 0;
|
||||
}
|
||||
GroundStruct* gs = &gsv->value.as.Struct;
|
||||
|
||||
// Set offset for each struct field
|
||||
GroundObjectField *s, *tmp;
|
||||
GroundSize i = 0;
|
||||
HASH_ITER(hh, gs->fields, s, tmp) {
|
||||
s->offset = i++;
|
||||
}
|
||||
|
||||
// Now go through and add offsets to everything
|
||||
for (GroundSize j = 0; j < inst->args.len; j++) {
|
||||
// struct field name
|
||||
GroundArg* arg = &inst->args.at[j];
|
||||
GroundObjectField* field = NULL;
|
||||
HASH_FIND_STR(state->variables, arg->as.ref->string, field);
|
||||
if (field == NULL) {
|
||||
Ground.Log.Error("unexpected null struct field in Ground.New.Bytecode -> doOffsets");
|
||||
Ground.Flags.error = true;
|
||||
return 0;
|
||||
}
|
||||
arg->_offset = field->offset;
|
||||
// 2nd arg offset is already set, but what we do next depends on it
|
||||
j++;
|
||||
// 0 -> none
|
||||
// 1 -> init
|
||||
// 2 -> set
|
||||
// 3 -> attach closure to function
|
||||
switch (inst->args.at[j]._offset) {
|
||||
case 0: case 3: break; // these do not require extra values
|
||||
case 1: // but these do, so we process them
|
||||
case 2: { // it's safe to assume that literals will have
|
||||
j++; // been dealt with accordingly
|
||||
arg = &inst->args.at[j];
|
||||
GroundVariable* item = NULL;
|
||||
HASH_FIND_STR(state->variables, arg->as.ref->string, item);
|
||||
|
||||
if (item == NULL) {
|
||||
item = malloc(sizeof(GroundVariable));
|
||||
if (item == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doOffsets");
|
||||
Ground.Flags.error = true;
|
||||
return 0;
|
||||
}
|
||||
item->value = Ground.New.Value.Int(0);
|
||||
strncpy(item->name, arg->as.ref->string, 2047);
|
||||
item->_offset = size++;
|
||||
HASH_ADD_STR(state->variables, name, item);
|
||||
}
|
||||
|
||||
arg->_offset = item->_offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
case GroundInstruction_GETFIELD: {
|
||||
|
||||
continue;
|
||||
}
|
||||
case GroundInstruction_SETFIELD: {
|
||||
|
||||
continue;
|
||||
}
|
||||
case GroundInstruction_CALLMETHOD: {
|
||||
|
||||
continue;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
for (GroundSize j = 0; j < gp->at[i].args.len; j++) {
|
||||
GroundArg* arg = &gp->at[i].args.at[j];
|
||||
|
||||
@@ -96,7 +182,7 @@ static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, Ground
|
||||
if (arg->type == GroundArg_Value) {
|
||||
GroundVariable* item = malloc(sizeof(GroundVariable));
|
||||
if (item == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.Internal.run()");
|
||||
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doOffsets");
|
||||
Ground.Flags.error = true;
|
||||
return 0;
|
||||
}
|
||||
@@ -115,7 +201,7 @@ static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, Ground
|
||||
if (item == NULL) {
|
||||
item = malloc(sizeof(GroundVariable));
|
||||
if (item == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.Internal.run()");
|
||||
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doOffsets");
|
||||
Ground.Flags.error = true;
|
||||
return 0;
|
||||
}
|
||||
@@ -137,7 +223,7 @@ static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, Ground
|
||||
};
|
||||
|
||||
if (inst.args.at == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.Internal.run()");
|
||||
Ground.Log.Error("malloc failed in Ground.New.Bytecode -> doOffsets");
|
||||
Ground.Flags.error = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user