From fcf9a13fa13f417f574df28e55380d955e4b5639 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sat, 11 Apr 2026 12:18:57 +1000 Subject: [PATCH] groundAddFunctionToStruct interface --- include/groundext.h | 1 + src/interface.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/groundext.h b/include/groundext.h index d3c76b5..6684de5 100644 --- a/include/groundext.h +++ b/include/groundext.h @@ -38,6 +38,7 @@ typedef GroundValue (*NativeGroundFunction)(GroundScope* scope, List args); * ...: A sequence of (GroundValueType type, char* name) for each argument. */ void groundAddNativeFunction(GroundScope* scope, char* name, NativeGroundFunction fn, GroundValueType returnType, int argCount, ...); +void groundAddFunctionToStruct(GroundStruct* gstruct, char* name, NativeGroundFunction fn, GroundValueType returnType, int argCount, ...); #ifdef __cplusplus } diff --git a/src/interface.c b/src/interface.c index 26afaef..4cef8d8 100644 --- a/src/interface.c +++ b/src/interface.c @@ -151,6 +151,27 @@ void groundAddValueToScope(GroundScope* gs, const char* name, GroundValue value) void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field) { addFieldToStruct(gstruct, name, field); } +GroundFunction* createGroundFunction(); +void addArgsToGroundFunction(GroundFunction* function, GroundValueType type, char* name); + +void groundAddFunctionToStruct(GroundStruct* gstruct, char* name, NativeGroundFunction fn, GroundValueType returnType, int argCount, ...) { + GroundFunction* gf = createGroundFunction(); + gf->isNative = true; + gf->nativeFn = fn; + gf->returnType = returnType; + + va_list args; + va_start(args, argCount); + for(int i = 0; i < argCount; i++) { + GroundValueType type = va_arg(args, GroundValueType); + char* argName = va_arg(args, char*); + addArgsToGroundFunction(gf, type, argName); + } + va_end(args); + + GroundValue gv = createFunctionGroundValue(gf); + addFieldToStruct(gstruct, name, gv); +} void groundCompileProgram(GroundProgram* program) { compileGroundProgram(program, "program.gexe");