Closures in Ground

This commit is contained in:
2026-03-02 10:09:10 +11:00
parent d011d2beb4
commit 5577679ded
7 changed files with 152 additions and 62 deletions

View File

@@ -83,9 +83,19 @@ GroundValue groundCreateValue(GroundValueType type, ...) {
return createNoneGroundValue();
break;
}
case ERROR:
case CUSTOM: {
// CUSTOM values are created from structs
GroundValue gv;
GroundStruct* gstruct = va_arg(args, GroundStruct*);
gv.type = CUSTOM;
gv.data.customVal = malloc(sizeof(GroundObject));
*gv.data.customVal = createObject(*gstruct);
gv.customType = gstruct;
break;
}
case ERROR: {
// FIXME
return createNoneGroundValue();
break;
}
}
@@ -128,3 +138,25 @@ void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field
char* groundCompileProgram(GroundProgram* program) {
return compileGroundProgram(program);
}
GroundValue groundRunFunction(GroundFunction* function, size_t argc, ...) {
va_list args;
va_start(args, argc);
GroundScope callScope = copyGroundScope(&function->closure);
if (argc != function->argSize) {
return createErrorGroundValue(createGroundError("Too few or too many arguments for function", "callError", NULL, NULL));
}
for (size_t i = 0; i < argc; i++) {
GroundValue gv = va_arg(args, GroundValue);
if (checkFnTypes(&gv, &function->args[i]) == false) {
return createErrorGroundValue(createGroundError("Mismatched argument type", "callError", NULL, NULL));
}
addVariable(callScope.variables, function->args[i].name, gv);
}
va_end(args);
return interpretGroundProgram(&function->program, &callScope);
}