Compare commits
2 Commits
3b8bcdf020
...
05733c693c
| Author | SHA1 | Date | |
|---|---|---|---|
| 05733c693c | |||
| 338241519e |
@@ -105,12 +105,6 @@ struct GroundStruct {
|
|||||||
GroundObjectField* fields;
|
GroundObjectField* fields;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GroundObjectField {
|
|
||||||
char name[2048];
|
|
||||||
GroundValue* value;
|
|
||||||
UT_hash_handle hh;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct GroundObject {
|
struct GroundObject {
|
||||||
GroundObjectField* fields;
|
GroundObjectField* fields;
|
||||||
GroundStruct* type;
|
GroundStruct* type;
|
||||||
@@ -160,6 +154,13 @@ struct GroundValue {
|
|||||||
GroundType type;
|
GroundType type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct GroundObjectField {
|
||||||
|
char name[2048];
|
||||||
|
GroundValue value;
|
||||||
|
GroundSize offset;
|
||||||
|
UT_hash_handle hh;
|
||||||
|
};
|
||||||
|
|
||||||
enum GroundArgType {
|
enum GroundArgType {
|
||||||
GroundArg_Value, GroundArg_ValueRef, GroundArg_DirectRef,
|
GroundArg_Value, GroundArg_ValueRef, GroundArg_DirectRef,
|
||||||
GroundArg_LineRef, GroundArg_Label,
|
GroundArg_LineRef, GroundArg_Label,
|
||||||
|
|||||||
@@ -22,14 +22,7 @@ GroundObject _GroundCopyObject(GroundObject* in) {
|
|||||||
|
|
||||||
strncpy(item->name, s->name, 2047);
|
strncpy(item->name, s->name, 2047);
|
||||||
|
|
||||||
item->value = malloc(sizeof(GroundValue));
|
item->value = Ground.Copy.Value(&s->value);
|
||||||
if (item->value == NULL) {
|
|
||||||
Ground.Log.Error("malloc failed in Ground.Copy.Object()");
|
|
||||||
Ground.Flags.error = true;
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
*item->value = Ground.Copy.Value(s->value);
|
|
||||||
|
|
||||||
if (Ground.Flags.error) {
|
if (Ground.Flags.error) {
|
||||||
return object;
|
return object;
|
||||||
|
|||||||
@@ -21,14 +21,7 @@ GroundStruct _GroundCopyStruct(GroundStruct* in) {
|
|||||||
|
|
||||||
strncpy(item->name, s->name, 2047);
|
strncpy(item->name, s->name, 2047);
|
||||||
|
|
||||||
item->value = malloc(sizeof(GroundValue));
|
item->value = Ground.Copy.Value(&s->value);
|
||||||
if (item->value == NULL) {
|
|
||||||
Ground.Log.Error("malloc failed in Ground.Copy.Struct()");
|
|
||||||
Ground.Flags.error = true;
|
|
||||||
return gs;
|
|
||||||
}
|
|
||||||
|
|
||||||
*item->value = Ground.Copy.Value(s->value);
|
|
||||||
|
|
||||||
if (Ground.Flags.error) {
|
if (Ground.Flags.error) {
|
||||||
return gs;
|
return gs;
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
#include "../../include/ground.h"
|
#include "../../include/ground.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void _GroundFreeObject(GroundObject* in) {
|
void _GroundFreeObject(GroundObject* in) {
|
||||||
GroundObjectField *s, *tmp;
|
GroundObjectField *s, *tmp;
|
||||||
|
|
||||||
@@ -9,7 +7,6 @@ void _GroundFreeObject(GroundObject* in) {
|
|||||||
|
|
||||||
HASH_DEL(in->fields, s);
|
HASH_DEL(in->fields, s);
|
||||||
|
|
||||||
Ground.Free.Value(s->value);
|
Ground.Free.Value(&s->value);
|
||||||
free(s->value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
#include "../../include/ground.h"
|
#include "../../include/ground.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void _GroundFreeStruct(GroundStruct* in) {
|
void _GroundFreeStruct(GroundStruct* in) {
|
||||||
GroundObjectField *s, *tmp;
|
GroundObjectField *s, *tmp;
|
||||||
|
|
||||||
@@ -9,7 +7,6 @@ void _GroundFreeStruct(GroundStruct* in) {
|
|||||||
|
|
||||||
HASH_DEL(in->fields, s);
|
HASH_DEL(in->fields, s);
|
||||||
|
|
||||||
Ground.Free.Value(s->value);
|
Ground.Free.Value(&s->value);
|
||||||
free(s->value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
|||||||
if (label == NULL) {
|
if (label == NULL) {
|
||||||
label = malloc(sizeof(GroundLabel));
|
label = malloc(sizeof(GroundLabel));
|
||||||
if (label == NULL) {
|
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;
|
Ground.Flags.error = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
|||||||
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
||||||
if (line == NULL) {
|
if (line == NULL) {
|
||||||
char buf[2048];
|
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.Log.Error(buf);
|
||||||
Ground.Flags.error = true;
|
Ground.Flags.error = true;
|
||||||
return;
|
return;
|
||||||
@@ -46,7 +46,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
|||||||
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
||||||
if (line == NULL) {
|
if (line == NULL) {
|
||||||
char buf[2048];
|
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.Log.Error(buf);
|
||||||
Ground.Flags.error = true;
|
Ground.Flags.error = true;
|
||||||
return;
|
return;
|
||||||
@@ -59,7 +59,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
|||||||
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
GroundSize* line = Ground.State.findLabel(state, arg->as.ref->string);
|
||||||
if (line == NULL) {
|
if (line == NULL) {
|
||||||
char buf[2048];
|
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.Log.Error(buf);
|
||||||
Ground.Flags.error = true;
|
Ground.Flags.error = true;
|
||||||
return;
|
return;
|
||||||
@@ -85,6 +85,92 @@ static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, Ground
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (GroundSize i = 0; i < gp->len; i++) {
|
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++) {
|
for (GroundSize j = 0; j < gp->at[i].args.len; j++) {
|
||||||
GroundArg* arg = &gp->at[i].args.at[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) {
|
if (arg->type == GroundArg_Value) {
|
||||||
GroundVariable* item = malloc(sizeof(GroundVariable));
|
GroundVariable* item = malloc(sizeof(GroundVariable));
|
||||||
if (item == NULL) {
|
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;
|
Ground.Flags.error = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -115,7 +201,7 @@ static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, Ground
|
|||||||
if (item == NULL) {
|
if (item == NULL) {
|
||||||
item = malloc(sizeof(GroundVariable));
|
item = malloc(sizeof(GroundVariable));
|
||||||
if (item == NULL) {
|
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;
|
Ground.Flags.error = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -137,7 +223,7 @@ static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, Ground
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (inst.args.at == NULL) {
|
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;
|
Ground.Flags.error = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,14 +22,7 @@ GroundObject _GroundNewObject(GroundStruct* in) {
|
|||||||
|
|
||||||
strncpy(item->name, s->name, 2047);
|
strncpy(item->name, s->name, 2047);
|
||||||
|
|
||||||
item->value = malloc(sizeof(GroundValue));
|
item->value = Ground.Copy.Value(&s->value);
|
||||||
if (item->value == NULL) {
|
|
||||||
Ground.Log.Error("malloc failed in Ground.New.Object()");
|
|
||||||
Ground.Flags.error = true;
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
*item->value = Ground.Copy.Value(s->value);
|
|
||||||
|
|
||||||
if (Ground.Flags.error) {
|
if (Ground.Flags.error) {
|
||||||
return object;
|
return object;
|
||||||
|
|||||||
@@ -1,17 +1,11 @@
|
|||||||
#include "../../include/ground.h"
|
#include "../../include/ground.h"
|
||||||
|
|
||||||
GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* state) {
|
static void doFunction(GroundProgram* program, GroundProgram* newProgram, GroundState* state, GroundSize* i) {
|
||||||
|
|
||||||
GroundProgram newProgram = Ground.New.Program();
|
|
||||||
|
|
||||||
for (GroundSize i = 0; i < program->len; i++) {
|
|
||||||
switch (program->at[i].type) {
|
|
||||||
case GroundInstruction_FUN: {
|
|
||||||
|
|
||||||
GroundFunction function = Ground.New.Function(state);
|
GroundFunction function = Ground.New.Function(state);
|
||||||
|
|
||||||
// Parse signature
|
// Parse signature
|
||||||
GroundInstruction* sig = &program->at[i];
|
GroundInstruction* sig = &program->at[*i];
|
||||||
char* id = sig->args.at[0].as.ref->string;
|
char* id = sig->args.at[0].as.ref->string;
|
||||||
|
|
||||||
for (GroundSize j = 1; j < sig->args.len; j++) {
|
for (GroundSize j = 1; j < sig->args.len; j++) {
|
||||||
@@ -28,7 +22,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
|
|||||||
if (argVar == NULL) {
|
if (argVar == NULL) {
|
||||||
Ground.Flags.error = true;
|
Ground.Flags.error = true;
|
||||||
Ground.Log.Error("malloc failed in Ground.Program.Preprocess");
|
Ground.Log.Error("malloc failed in Ground.Program.Preprocess");
|
||||||
return newProgram;
|
return;
|
||||||
}
|
}
|
||||||
snprintf(argVar->name, 2047, "%s", argId);
|
snprintf(argVar->name, 2047, "%s", argId);
|
||||||
argVar->value = Ground.New.Value.Int(0);
|
argVar->value = Ground.New.Value.Int(0);
|
||||||
@@ -40,13 +34,13 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
|
|||||||
GroundSize structCount = 0;
|
GroundSize structCount = 0;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
i++;
|
(*i)++;
|
||||||
if (i >= program->len) {
|
if (*i >= program->len) {
|
||||||
Ground.Flags.error = true;
|
Ground.Flags.error = true;
|
||||||
Ground.Log.Error("expecting ENDFUN instruction, reached end of program in Ground.Program.Preprocess");
|
Ground.Log.Error("expecting ENDFUN instruction, reached end of program in Ground.Program.Preprocess");
|
||||||
return newProgram;
|
return;
|
||||||
}
|
}
|
||||||
GroundInstruction* inst = &program->at[i];
|
GroundInstruction* inst = &program->at[*i];
|
||||||
switch (inst->type) {
|
switch (inst->type) {
|
||||||
case GroundInstruction_FUN: {
|
case GroundInstruction_FUN: {
|
||||||
funCount++;
|
funCount++;
|
||||||
@@ -56,7 +50,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
|
|||||||
if (funCount < 1) {
|
if (funCount < 1) {
|
||||||
Ground.Flags.error = true;
|
Ground.Flags.error = true;
|
||||||
Ground.Log.Error("extra ENDFUN instruction (expecting ENDSTRUCT before) in Ground.Program.Preprocess");
|
Ground.Log.Error("extra ENDFUN instruction (expecting ENDSTRUCT before) in Ground.Program.Preprocess");
|
||||||
return newProgram;
|
return;
|
||||||
}
|
}
|
||||||
funCount--;
|
funCount--;
|
||||||
break;
|
break;
|
||||||
@@ -69,7 +63,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
|
|||||||
if (structCount < 1) {
|
if (structCount < 1) {
|
||||||
Ground.Flags.error = true;
|
Ground.Flags.error = true;
|
||||||
Ground.Log.Error("extra ENDSTRUCT instruction (expecting ENDFUN before) in Ground.Program.Preprocess");
|
Ground.Log.Error("extra ENDSTRUCT instruction (expecting ENDFUN before) in Ground.Program.Preprocess");
|
||||||
return newProgram;
|
return;
|
||||||
}
|
}
|
||||||
structCount--;
|
structCount--;
|
||||||
break;
|
break;
|
||||||
@@ -88,7 +82,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
|
|||||||
// Preprocess function's body
|
// Preprocess function's body
|
||||||
GroundProgram functionBody = Ground.Program.preprocess(function.program.ground, function.closure);
|
GroundProgram functionBody = Ground.Program.preprocess(function.program.ground, function.closure);
|
||||||
if (Ground.Flags.error) {
|
if (Ground.Flags.error) {
|
||||||
return newProgram;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ground.Free.Program(function.program.ground);
|
Ground.Free.Program(function.program.ground);
|
||||||
@@ -98,7 +92,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
|
|||||||
if (var == NULL) {
|
if (var == NULL) {
|
||||||
Ground.Flags.error = true;
|
Ground.Flags.error = true;
|
||||||
Ground.Log.Error("malloc failed in Ground.Program.Preprocess");
|
Ground.Log.Error("malloc failed in Ground.Program.Preprocess");
|
||||||
return newProgram;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(var->name, 2047, "%s", id);
|
snprintf(var->name, 2047, "%s", id);
|
||||||
@@ -109,12 +103,173 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
|
|||||||
// Add instruction to attach closure to function
|
// Add instruction to attach closure to function
|
||||||
GroundInstruction inst = Ground.New.Instruction(GroundInstruction_FUN);
|
GroundInstruction inst = Ground.New.Instruction(GroundInstruction_FUN);
|
||||||
Ground.Instruction.append(&inst, Ground.New.Arg.FunctionRef(sig->args.at[0].as.ref));
|
Ground.Instruction.append(&inst, Ground.New.Arg.FunctionRef(sig->args.at[0].as.ref));
|
||||||
Ground.Program.append(&newProgram, inst);
|
Ground.Program.append(newProgram, inst);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundState* state, GroundSize* i);
|
||||||
|
static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundState* state, GroundSize* i) {
|
||||||
|
GroundIdentifier* structName = Ground.Copy.Identifier(program->at[*i].args.at[0].as.ref);
|
||||||
|
GroundStruct gs = Ground.New.Struct();
|
||||||
|
|
||||||
|
// The aim is to compile an instruction which looks like this:
|
||||||
|
// struct &field OP $value &field OP $value &field OP $value...
|
||||||
|
// where:
|
||||||
|
// &field is the field to initialise
|
||||||
|
// OP is a numeric identifier:
|
||||||
|
// 0 -> none
|
||||||
|
// 1 -> init
|
||||||
|
// 2 -> set
|
||||||
|
// 3 -> attach closure to function
|
||||||
|
// $value is either a type reference (OP=0), a value reference (OP=1), or none (OP=2)
|
||||||
|
// Functions will be moved to the end of the struct, and a closure will be attached to all of them
|
||||||
|
//
|
||||||
|
// Oh yeah, we also need to make a GroundStruct value
|
||||||
|
|
||||||
|
GroundSize structCount = 0;
|
||||||
|
|
||||||
|
GroundInstruction output = Ground.New.Instruction(GroundInstruction_STRUCT);
|
||||||
|
Ground.Instruction.append(&output, Ground.New.Arg.TypeRef(structName));
|
||||||
|
|
||||||
|
// Parse struct body
|
||||||
|
bool parsing = true;
|
||||||
|
while (parsing) {
|
||||||
|
(*i)++;
|
||||||
|
if (*i >= program->len) {
|
||||||
|
Ground.Flags.error = true;
|
||||||
|
Ground.Log.Error("expecting ENDFUN instruction, reached end of program in Ground.Program.Preprocess");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundInstruction* instruction = &program->at[*i];
|
||||||
|
switch (instruction->type) {
|
||||||
|
case GroundInstruction_SET: {
|
||||||
|
|
||||||
|
GroundObjectField* field = malloc(sizeof(GroundObjectField));
|
||||||
|
if (field == NULL) {
|
||||||
|
Ground.Log.Error("malloc failed in Ground.Program.preprocess -> doStruct");
|
||||||
|
Ground.Flags.error = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(field->name, 2047, "%s", instruction->args.at[0].as.ref->string);
|
||||||
|
|
||||||
|
// If we know the value type now, add it to the instruction
|
||||||
|
if (instruction->args.at[1].type == GroundArg_Value) {
|
||||||
|
field->value = Ground.Copy.Value(&instruction->args.at[1].as.value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
case GroundInstruction_INIT: {
|
||||||
|
GroundObjectField* field = malloc(sizeof(GroundObjectField));
|
||||||
|
if (field == NULL) {
|
||||||
|
Ground.Log.Error("malloc failed in Ground.Program.preprocess -> doStruct");
|
||||||
|
Ground.Flags.error = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(field->name, 2047, "%s", instruction->args.at[0].as.ref->string);
|
||||||
|
|
||||||
|
// If we know the type now, add it to the instruction
|
||||||
|
if (strcmp(field->name, "int") == 0) {
|
||||||
|
field->value = Ground.New.Value.Int(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (strcmp(field->name, "double") == 0) {
|
||||||
|
field->value = Ground.New.Value.Double(0.0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (strcmp(field->name, "string") == 0) {
|
||||||
|
field->value = Ground.New.Value.String(Ground.New.String(""));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (strcmp(field->name, "char") == 0) {
|
||||||
|
field->value = Ground.New.Value.Char(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (strcmp(field->name, "bool") == 0) {
|
||||||
|
field->value = Ground.New.Value.Bool(false);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (strcmp(field->name, "function") == 0) {
|
||||||
|
field->value = Ground.New.Value.Function(Ground.New.Function(state));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
HASH_ADD_STR(gs.fields, name, field);
|
||||||
|
|
||||||
|
// init &field -type -> &field 1 -type
|
||||||
|
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[0]));
|
||||||
|
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 1});
|
||||||
|
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[1]));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case GroundInstruction_STRUCT: {
|
case GroundInstruction_STRUCT: {
|
||||||
// TODO: Preprocess structs
|
// TODO structs in structs
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GroundInstruction_EXTERN: {
|
||||||
|
// TODO external functions as struct members
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GroundInstruction_FUN: {
|
||||||
|
// Parse the function itself
|
||||||
|
doFunction(program, newProgram, state, i);
|
||||||
|
// Read and remove the emitted instruction
|
||||||
|
GroundInstruction* inst = &newProgram->at[newProgram->len - 1];
|
||||||
|
Ground.Instruction.append(&output, Ground.New.Arg.FunctionRef(Ground.Copy.Identifier(inst->args.at[0].as.ref)));
|
||||||
|
Ground.Free.Instruction(inst);
|
||||||
|
newProgram->len--;
|
||||||
|
|
||||||
|
// Add 3 to signal addition of closure
|
||||||
|
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 1});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GroundInstruction_ENDSTRUCT: {
|
||||||
|
parsing = false;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
Ground.Log.Error("invalid instruction inside struct in ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundVariable* variable = malloc(sizeof(GroundVariable));
|
||||||
|
if (variable == NULL) {
|
||||||
|
Ground.Log.Error("malloc failed in Ground.Program.Preprocess -> doStruct");
|
||||||
|
Ground.Flags.error = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(variable->name, 2047, "%s", structName->string);
|
||||||
|
variable->value = Ground.New.Value.Struct(gs);
|
||||||
|
|
||||||
|
HASH_ADD_STR(state->variables, name, variable);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* state) {
|
||||||
|
|
||||||
|
GroundProgram newProgram = Ground.New.Program();
|
||||||
|
|
||||||
|
for (GroundSize i = 0; i < program->len; i++) {
|
||||||
|
switch (program->at[i].type) {
|
||||||
|
case GroundInstruction_FUN: {
|
||||||
|
doFunction(program, &newProgram, state, &i);
|
||||||
|
if (Ground.Flags.error) return newProgram;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GroundInstruction_STRUCT: {
|
||||||
|
doStruct(program, &newProgram, state, &i);
|
||||||
|
if (Ground.Flags.error) return newProgram;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ void _GroundStructAddField(GroundStruct* gs, const char* id, GroundValue value)
|
|||||||
HASH_FIND_STR(gs->fields, id, field);
|
HASH_FIND_STR(gs->fields, id, field);
|
||||||
|
|
||||||
if (field != NULL) {
|
if (field != NULL) {
|
||||||
Ground.Free.Value(field->value);
|
Ground.Free.Value(&field->value);
|
||||||
*field->value = Ground.Copy.Value(&value);
|
field->value = Ground.Copy.Value(&value);
|
||||||
|
|
||||||
strncpy(field->name, id, 2047);
|
strncpy(field->name, id, 2047);
|
||||||
} else {
|
} else {
|
||||||
@@ -18,16 +18,11 @@ void _GroundStructAddField(GroundStruct* gs, const char* id, GroundValue value)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
field->value = malloc(sizeof(GroundValue));
|
field->value = Ground.Copy.Value(&value);
|
||||||
if (field->value == NULL) {
|
|
||||||
Ground.Log.Error("malloc failed in Ground.Struct.addField()");
|
|
||||||
Ground.Flags.error = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
*field->value = Ground.Copy.Value(&value);
|
|
||||||
strncpy(field->name, id, 2047);
|
strncpy(field->name, id, 2047);
|
||||||
|
|
||||||
|
HASH_ADD_STR(gs->fields, name, field);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user