Further struct work, fix lots of warnings

This commit is contained in:
2026-01-17 12:21:43 +11:00
parent c51bb82f62
commit 96d7d9470a
4 changed files with 68 additions and 23 deletions

View File

@@ -11,7 +11,7 @@
int currentInstruction = 0;
void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine) {
[[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine) {
printf("Ground runtime error:\n ErrorType: ");
switch (error) {
case ARG_TYPE_MISMATCH: {
@@ -46,6 +46,18 @@ void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where
printf("MathError");
break;
}
case RETURN_TYPE_MISMATCH: {
printf("ReturnTypeMismatch");
break;
}
case PREMATURE_EOF: {
printf("PrematureEof");
break;
}
case INVALID_INSTRUCTION: {
printf("InvalidInstruction");
break;
}
default:
case FIXME: {
printf("FIXME (please report issue to https://chsp.au/ground/cground)");
@@ -173,7 +185,7 @@ GroundDebugInstruction parseDebugInstruction(char* in) {
}
}
if (spacepos == -1) {
if (spacepos == (size_t) -1) {
spacepos = insize;
}
@@ -234,8 +246,25 @@ void groundAddNativeFunction(GroundScope* scope, char* name, NativeGroundFunctio
addVariable(scope->variables, name, createFunctionGroundValue(gf));
}
GroundStruct parseStruct(GroundProgram* in) {
GroundStruct parseStruct(GroundProgram* in, size_t errorOffset) {
GroundStruct gstruct = createStruct();
for (size_t i = 0; i < in->size; i++) {
switch (in->instructions[i].type) {
case SET: {
break;
}
case INIT: {
break;
}
case FUN: {
break;
}
default: {
runtimeError(INVALID_INSTRUCTION, "Unsupported instruction while inside struct", &in->instructions[i], errorOffset + i);
}
}
}
return gstruct;
}
@@ -252,7 +281,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
scope.variables = &variables;
}
// Preprocess all labels, structs and functions
for (int i = 0; i < in->size; i++) {
for (size_t i = 0; i < in->size; i++) {
if (in->instructions[i].type == CREATELABEL) {
addLabel(scope.labels, in->instructions[i].args.args[0].value.refName, i);
}
@@ -309,7 +338,8 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
i++;
size_t counter = 1;
GroundProgram gp;
GroundProgram gp = createGroundProgram();
size_t errorOffset = i;
while (counter > 0) {
if (i >= in->size) {
runtimeError(PREMATURE_EOF, "Reached end of scope before struct definition ended", &in->instructions[i - 1], i - 1);
@@ -321,12 +351,13 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
counter--;
}
addInstructionToProgram(&gp, in->instructions[i]);
i++;
}
GroundStruct gs = parseStruct(&gp);
GroundStruct gs = parseStruct(&gp, errorOffset);
}
}
for (int i = 0; i < in->size; i++) {
for (size_t i = 0; i < in->size; i++) {
if (in->instructions[i].type == FUN) {
int count = 1;
while (count > 0) {
@@ -463,7 +494,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
GroundInstruction* in = &copied_inst;
// Insert variables prefixed with $
for (int i = 0; i < in->args.length; i++) {
for (size_t i = 0; i < in->args.length; i++) {
if (in->args.args[i].type == VALREF) {
GroundVariable* variable = findVariable(*scope->variables, in->args.args[i].value.refName);
if (variable) {
@@ -476,9 +507,11 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
}
}
switch (in->type) {
// We can safely ignore any CREATELABEL, FUN, and ENDFUN instructions, these have been preprocessed
// We can safely ignore these instructions, as they have been preprocessed
case FUN:
case ENDFUN:
case STRUCT:
case ENDSTRUCT:
case CREATELABEL: {
break;
}
@@ -551,7 +584,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
if (in->args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 or more args", in, currentInstruction);
}
for (int i = 0; i < in->args.length; i++) {
for (size_t i = 0; i < in->args.length; i++) {
if (i != 0) printf(" ");
printGroundArg(&in->args.args[i]);
}
@@ -561,7 +594,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
if (in->args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 or more args", in, currentInstruction);
}
for (int i = 0; i < in->args.length; i++) {
for (size_t i = 0; i < in->args.length; i++) {
if (i != 0) printf(" ");
printGroundArg(&in->args.args[i]);
}
@@ -653,9 +686,15 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
}
case FUNCTION: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("function"));
break;
}
case STRUCTVAL: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("struct"));
break;
}
case NONE: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("none"));
break;
}
}
@@ -690,7 +729,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 1", in, currentInstruction);
}
List newList = createList();
for (int i = 1; i < in->args.length; i++) {
for (size_t i = 1; i < in->args.length; i++) {
if (in->args.args[i].type != VALUE) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value for all args after arg 1", in, currentInstruction);
}
@@ -1167,7 +1206,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", in, currentInstruction);
}
char* str = in->args.args[0].value.value.data.stringVal;
int64_t idx = in->args.args[1].value.value.data.intVal;
size_t idx = in->args.args[1].value.value.data.intVal;
if (idx < strlen(str)) {
addVariable(scope->variables, in->args.args[2].value.refName, createCharGroundValue(str[idx]));
} else {