From 5bd85195179972a40b2264c62fa43e0d71e79e53 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Mon, 25 Aug 2025 13:22:15 +1000 Subject: [PATCH] External library support --- src/main.cpp | 44 ++++++++++++++++++++++++++++++++++++++++-- tests/use/library.grnd | 3 +++ tests/use/use.grnd | 5 +++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/use/library.grnd create mode 100644 tests/use/use.grnd diff --git a/src/main.cpp b/src/main.cpp index 3ab90f2..0c970ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,6 +41,8 @@ #include #include #include +#include +#include using namespace std; @@ -402,9 +404,15 @@ string procFnName = ""; bool inFunction = false; -// Forward declaration for the call instruction +// Forward declaration for the call instruction and use instruction Literal exec(vector in); +// Forward declaration for the use instruction +vector parser(vector> in); + +// Forward declaration for the use instruction +vector> lexer(string in); + /* exec function This function takes a list of instructions (see Instruction struct above and parser @@ -1734,7 +1742,39 @@ Literal exec(vector in, bool executingFunction) { } break; case Instructions::Use: - cout << "Still to be implemented" << endl; + if (l.args.size() < 1) { + error("Could not find all arguments for Use inbuilt"); + } + { + string useName; + + if (holds_alternative(l.args[0])) { + if (holds_alternative(get(l.args[0]).val)) { + useName = get(get(l.args[0]).val) + ".grnd"; + } else { + error("First argument for use requires a string literal"); + } + } else { + error("First argument for use requires a string literal"); + } + + string groundLibsDir = getenv("GROUND_LIBS"); + + if (filesystem::exists(useName)) { + + } else if (groundLibsDir != "" && filesystem::exists(groundLibsDir + useName)) { + useName = groundLibsDir + useName; + } else { + error("Could not find external Ground library in $GROUND_LIBS or current directory."); + } + + ifstream file(useName); + string lns; + string in; + while (getline(file, lns)) { + in += lns += "\n"; + } + Literal ret = exec(parser(lexer(in)), false); } break; case Instructions::Extern: cout << "Still to be implemented" << endl; diff --git a/tests/use/library.grnd b/tests/use/library.grnd new file mode 100644 index 0000000..de99953 --- /dev/null +++ b/tests/use/library.grnd @@ -0,0 +1,3 @@ +fun -string !dingus +return "Hello from the library" +endfun diff --git a/tests/use/use.grnd b/tests/use/use.grnd new file mode 100644 index 0000000..1b5887a --- /dev/null +++ b/tests/use/use.grnd @@ -0,0 +1,5 @@ +use "library" + +call !dingus &var + +stdlnout $var