From 76e36b7ca396a1f6646b200b45d03b15205a2089 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sat, 30 Aug 2025 10:06:25 +1000 Subject: [PATCH] Add a prefix for imported libraries --- docs/syntax.md | 4 ++-- src/main.cpp | 15 +++++++++++++-- tests/use/use.grnd | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/docs/syntax.md b/docs/syntax.md index f1e88ba..eff851e 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -276,7 +276,7 @@ Usage: `call !function &var #### use (Experimental, please report bugs!) -Attempts to import another Ground program. Gets inserted wherever the use statement is. Any code (including code outside function declarations) will be executed. +Attempts to import another Ground program. Gets inserted wherever the use statement is. Any code (including code outside function declarations) will be executed. All functions from the library will be given a prefix, meaning functions will be registered as `!libName:functionName`. Note: Ground will check the directory where the program is being run from when trying to find imported programs. If that fails, it will check the directory set in the $GROUND_LIBS environment variable set by your system. The '.grnd' extension is appended automatically. @@ -284,7 +284,7 @@ Usage: `use $stringvalue` #### extern (Experimental, please report bugs!) -Attempts to import a shared object library written for Ground. All functions in the external library will be usable with `call`. +Attempts to import a shared object library written for Ground. All functions in the external library will be usable with `call`. All functions from the library will be given a prefix, meaning functions will be registered as `!libName:functionName`. Note: Ground will check the directory set in the $GROUND_LIBS environment variable set by your system. The '.so' (Linux), '.dylib' (macOS), or '.dll' (Windows) extension is appended automatically. diff --git a/src/main.cpp b/src/main.cpp index bc3d51d..604bfe9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -486,6 +487,9 @@ string procFnName = ""; bool inFunction = false; +// Stack of strings for keeping track of which thing we're importing +stack importing; + // Forward declaration for the call instruction and use instruction Literal exec(vector in); @@ -1630,6 +1634,10 @@ Literal exec(vector in, bool executingFunction) { error("Second argument of function must be a function reference"); } + if (importing.size() > 0) { + fnName = importing.top() + ":" + fnName; + } + // Parse function arguments (type-direct pairs) if ((l.args.size() - 2) % 2 != 0) { error("Function arguments must be in type-direct pairs"); @@ -1821,7 +1829,10 @@ Literal exec(vector in, bool executingFunction) { while (getline(file, lns)) { in += lns += "\n"; } - Literal ret = exec(parser(lexer(in)), false); } + importing.push(get(get(l.args[0]).val)); + Literal ret = exec(parser(lexer(in)), false); + importing.pop(); + } break; case Instructions::Extern: if (l.args.size() < 1) { @@ -1911,7 +1922,7 @@ Literal exec(vector in, bool executingFunction) { for (int i = 0; functionNames[i] != nullptr; i++) { void* funcPtr = getFunction(functionNames[i]); if (funcPtr) { - externalFunctions[string(functionNames[i])] = funcPtr; + externalFunctions[libName + ":" + string(functionNames[i])] = funcPtr; functionCount++; } else { error("Failed to get function pointer for: " + string(functionNames[i])); diff --git a/tests/use/use.grnd b/tests/use/use.grnd index 1b5887a..44f04b2 100644 --- a/tests/use/use.grnd +++ b/tests/use/use.grnd @@ -1,5 +1,5 @@ use "library" -call !dingus &var +call !library:dingus &var stdlnout $var