Skip to content

Commit

Permalink
Importer Checks exe paths as well
Browse files Browse the repository at this point in the history
  • Loading branch information
SujalChoudhari committed Sep 16, 2023
1 parent 11081aa commit 2f01062
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Coda.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
<ItemGroup>
<None Include=".gitattributes" />
<None Include=".gitignore" />
<None Include="bin\Debug\x64\Maths.coda" />
<None Include="CHANGELOG.md" />
<None Include="CODE_OF_CONDUCT.md" />
<None Include="CppProperties.json" />
Expand Down
1 change: 1 addition & 0 deletions Coda.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,6 @@
<None Include="Test\debug.coda" />
<None Include="LICENSE" />
<None Include="CODE_OF_CONDUCT.md" />
<None Include="bin\Debug\x64\Maths.coda" />
</ItemGroup>
</Project>
81 changes: 78 additions & 3 deletions Frontend/Importer/Importer.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
#include "Importer.h"
#include <algorithm>

#if _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#include <linux/limits.h>
#endif //


#include <filesystem>
#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()) {
Expand Down Expand Up @@ -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
}
}
9 changes: 9 additions & 0 deletions Frontend/Importer/Importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions Test/debug.coda
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import dir.Maths
import Maths

println(subtract(25,7));
println(sin(3.142));
30 changes: 15 additions & 15 deletions Test/dir/Maths.coda
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
30 changes: 15 additions & 15 deletions dll/Maths/Maths.coda
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit 2f01062

Please sign in to comment.