From 2f01062341981baac9a3f420e37abe9e70e85c1e Mon Sep 17 00:00:00 2001 From: Sujal Choudhari Date: Sat, 16 Sep 2023 21:53:00 +0530 Subject: [PATCH] Importer Checks exe paths as well --- CHANGELOG.md | 2 + Coda.vcxproj | 1 + Coda.vcxproj.filters | 1 + Frontend/Importer/Importer.cpp | 81 ++++++++++++++++++++++++++++++++-- Frontend/Importer/Importer.h | 9 ++++ Test/debug.coda | 4 +- Test/dir/Maths.coda | 30 ++++++------- dll/Maths/Maths.coda | 30 ++++++------- 8 files changed, 123 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6b148a..7d70020 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `IValue`, `INode` and `IEnvironment` added as abstraction for `Value`, `Node` and `Environment` respectively. - `native` keyword added for calling functions from dll. +- Trigonometry, Basic Functions to `Maths.coda` package. ### Changed - `sizeof` returns length of `list` and `objects` instead of a unrelated number. +- Importer first looks in `cwd` if failed to find required file, checks the directory of the `executable` file. ## [0.1.1] - 29-8-2023 diff --git a/Coda.vcxproj b/Coda.vcxproj index 9cbb29c..73a10b8 100644 --- a/Coda.vcxproj +++ b/Coda.vcxproj @@ -209,6 +209,7 @@ + diff --git a/Coda.vcxproj.filters b/Coda.vcxproj.filters index b25b7d4..689fd3e 100644 --- a/Coda.vcxproj.filters +++ b/Coda.vcxproj.filters @@ -216,5 +216,6 @@ + \ No newline at end of file diff --git a/Frontend/Importer/Importer.cpp b/Frontend/Importer/Importer.cpp index b0dd682..e7b6852 100644 --- a/Frontend/Importer/Importer.cpp +++ b/Frontend/Importer/Importer.cpp @@ -1,10 +1,21 @@ #include "Importer.h" #include + +#if _WIN32 +#include +#else +#include +#include +#endif // + + +#include #include "../../Error/Error.h" namespace Coda { namespace Frontend { std::string Importer::import(const std::string& filepath) { + Coda::Utils::FileReader fileReader(filepath); std::string sourceCode = fileReader.readToString(); if (!Error::Manager::isSafe()) { @@ -53,15 +64,79 @@ namespace Coda { if (lastSlashIndex == std::string::npos) { Error::Importer::raise("Invalid file path: " + filename); } - for (int i = 0; i < importString.size(); i++) { if (importString[i] == '.') { importString[i] = '/'; } } - std::string modulePath = filename.substr(0, lastSlashIndex + 1) + importString + ".coda"; - return modulePath; + // Check if the file exists in the base directory + std::string baseModulePath = filename.substr(0, lastSlashIndex + 1) + importString + ".coda"; + if (fileExists(baseModulePath)) { + return baseModulePath; + } + + // If the file doesn't exist in the base directory, check the directory where the executable is located + std::string executablePath = getExecutablePath(); // Implement this function to get the executable's directory + std::string exeModulePath = executablePath + importString + ".coda"; + + if (fileExists(exeModulePath)) { + return exeModulePath; + } + + // If the file doesn't exist in either location, raise an error + Error::Importer::raise("Imported file not found: " + importString); + } + + +#if _WIN32 // Check if the platform is Windows + + std::string Importer::getExecutablePath() { + wchar_t buffer[MAX_PATH]; + GetModuleFileNameW(NULL, buffer, MAX_PATH); + std::wstring executablePath(buffer); + size_t lastSlashIndex = executablePath.rfind(L"\\"); + if (lastSlashIndex != std::wstring::npos) { + executablePath = executablePath.substr(0, lastSlashIndex + 1); + } + + // Convert the wide character string to a narrow character string (UTF-8) + std::string narrowExecutablePath(executablePath.begin(), executablePath.end()); + + return narrowExecutablePath; + } + +#else // Assuming Unix-like system (Linux, macOS, etc.) + std::string Importer::getExecutablePath() { + char buffer[PATH_MAX]; + ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1); + if (len != -1) { + buffer[len] = '\0'; + std::string executablePath(buffer); + size_t lastSlashIndex = executablePath.rfind("/"); + if (lastSlashIndex != std::string::npos) { + executablePath = executablePath.substr(0, lastSlashIndex + 1); + } + return executablePath; + } + else { + throw "failed to get path of executable file" + return ""; + } + } +#endif + +#if _WIN32 + bool Importer::fileExists(const std::string& filePath) { + std::wstring wideFilePath(filePath.begin(), filePath.end()); + DWORD fileAttributes = GetFileAttributes(wideFilePath.c_str()); + return (fileAttributes != INVALID_FILE_ATTRIBUTES && !(fileAttributes & FILE_ATTRIBUTE_DIRECTORY)); + } +#else + bool fileExists(const std::string& filePath) { + std::wstring wideFilePath(filePath.begin(), filePath.end()); + return access(wideFilePath, F_OK) == 0; } +#endif } } diff --git a/Frontend/Importer/Importer.h b/Frontend/Importer/Importer.h index 0724a8d..5debcc6 100644 --- a/Frontend/Importer/Importer.h +++ b/Frontend/Importer/Importer.h @@ -44,6 +44,15 @@ namespace Coda { convert an import statement into an actual, absolute path */ std::string getAbsImportPath(const std::string& filename, std::string& importString); + + /* + get the path to the executable + */ + std::string getExecutablePath(); + /* + check if file exists or nots + */ + bool fileExists(const std::string& filePath); }; } // namespace Frontend } // namespace Coda diff --git a/Test/debug.coda b/Test/debug.coda index 75bad36..3ba1676 100644 --- a/Test/debug.coda +++ b/Test/debug.coda @@ -1,3 +1,3 @@ -import dir.Maths +import Maths -println(subtract(25,7)); \ No newline at end of file +println(sin(3.142)); \ No newline at end of file diff --git a/Test/dir/Maths.coda b/Test/dir/Maths.coda index 5ba6dc0..e6b8625 100644 --- a/Test/dir/Maths.coda +++ b/Test/dir/Maths.coda @@ -2,45 +2,45 @@ def add(first, second) { return native Maths coda_add(first, second); } -def subtract(first, second) { +def sub(first, second) { return native Maths coda_sub(first, second); } -def multiply(first, second) { +def mul(first, second) { return native Maths coda_mul(first, second); } -def divide(first, second) { +def div(first, second) { return native Maths coda_div(first, second); } -def power(base, exponent) { +def pow(base, exponent) { first = base; second = exponent; return native Maths coda_pow(first, second); } -def squareRoot(number) { +def sqrt(number) { return native Maths squareRoot(number); } -def naturalLog(value) { +def ln(value) { return native Maths naturalLog(value); } -def base10Log(value) { +def log10(value) { return native Maths base10Log(value); } -def exponential(exponent) { +def exp(exponent) { return native Maths exponential(exponent); } -def absoluteValue(number) { +def abs(number) { return native Maths absoluteValue(number); } -def roundToNearest(number) { +def round(number) { return native Maths roundToNearest(number); } @@ -52,23 +52,23 @@ def cos(angle) { return native Maths coda_cos(angle); } -def coda_tan(angle) { +def tan(angle) { return native Maths coda_tan(angle); } -def coda_asin(value) { +def asin(value) { return native Maths coda_asin(value); } -def coda_acos(value) { +def acos(value) { return native Maths coda_acos(value); } -def coda_atan(value) { +def atan(value) { return native Maths coda_atan(value); } -def coda_sec(angle) { +def sec(angle) { return native Maths coda_sec(angle); } diff --git a/dll/Maths/Maths.coda b/dll/Maths/Maths.coda index 5ba6dc0..e6b8625 100644 --- a/dll/Maths/Maths.coda +++ b/dll/Maths/Maths.coda @@ -2,45 +2,45 @@ def add(first, second) { return native Maths coda_add(first, second); } -def subtract(first, second) { +def sub(first, second) { return native Maths coda_sub(first, second); } -def multiply(first, second) { +def mul(first, second) { return native Maths coda_mul(first, second); } -def divide(first, second) { +def div(first, second) { return native Maths coda_div(first, second); } -def power(base, exponent) { +def pow(base, exponent) { first = base; second = exponent; return native Maths coda_pow(first, second); } -def squareRoot(number) { +def sqrt(number) { return native Maths squareRoot(number); } -def naturalLog(value) { +def ln(value) { return native Maths naturalLog(value); } -def base10Log(value) { +def log10(value) { return native Maths base10Log(value); } -def exponential(exponent) { +def exp(exponent) { return native Maths exponential(exponent); } -def absoluteValue(number) { +def abs(number) { return native Maths absoluteValue(number); } -def roundToNearest(number) { +def round(number) { return native Maths roundToNearest(number); } @@ -52,23 +52,23 @@ def cos(angle) { return native Maths coda_cos(angle); } -def coda_tan(angle) { +def tan(angle) { return native Maths coda_tan(angle); } -def coda_asin(value) { +def asin(value) { return native Maths coda_asin(value); } -def coda_acos(value) { +def acos(value) { return native Maths coda_acos(value); } -def coda_atan(value) { +def atan(value) { return native Maths coda_atan(value); } -def coda_sec(angle) { +def sec(angle) { return native Maths coda_sec(angle); }