forked from ground/ground
Add a prefix for imported libraries
This commit is contained in:
@@ -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.
|
||||
|
||||
|
15
src/main.cpp
15
src/main.cpp
@@ -39,6 +39,7 @@
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <stack>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
@@ -486,6 +487,9 @@ string procFnName = "";
|
||||
|
||||
bool inFunction = false;
|
||||
|
||||
// Stack of strings for keeping track of which thing we're importing
|
||||
stack<string> importing;
|
||||
|
||||
// Forward declaration for the call instruction and use instruction
|
||||
Literal exec(vector<Instruction> in);
|
||||
|
||||
@@ -1630,6 +1634,10 @@ Literal exec(vector<Instruction> 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<Instruction> in, bool executingFunction) {
|
||||
while (getline(file, lns)) {
|
||||
in += lns += "\n";
|
||||
}
|
||||
Literal ret = exec(parser(lexer(in)), false); }
|
||||
importing.push(get<string>(get<Literal>(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<Instruction> 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]));
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use "library"
|
||||
|
||||
call !dingus &var
|
||||
call !library:dingus &var
|
||||
|
||||
stdlnout $var
|
||||
|
Reference in New Issue
Block a user