Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions Parser/include/RigCParser/Grammar/Tokens.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,8 @@ namespace rigc

struct Name;

struct PackageImportNames
: p::seq<
Name,
p::opt<
p::seq< p::one<'.'>, PackageImportNames>
>
>
{
};

struct PackageImportFullName
:
p::sor<
StringLiteral,
PackageImportNames
>
: StringLiteral
{
};

Expand Down
6 changes: 6 additions & 0 deletions VM/include/RigCVM/Environment.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include RIGCVM_PCH

namespace rigc::vm
{
fs::path getVmAppPath();
}
1 change: 1 addition & 0 deletions VM/include/RigCVM/ErrorHandling/Exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

struct RigCError : std::exception
{
private:
std::string basicMessage;

std::string helpMessage;
Expand Down
2 changes: 1 addition & 1 deletion VM/include/RigCVM/ErrorHandling/Formatting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ inline auto errorWithLineArgPair(std::size_t line) -> ArgPair

auto const value = fmt::format(
"{}{}{}{}{}",
format(boldRedFmt, "[Error"),
fmt::format(boldRedFmt, "[Error"),
reset,
" on line ",
format(s().LightBlue, "{}", line),
Expand Down
6 changes: 4 additions & 2 deletions VM/include/RigCVM/TypeSystem/FuncType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

namespace rigc::vm
{
//todo: refactor maybe use template?
struct FuncType : IType
{
private:
InnerType result;
std::vector<InnerType> parameters;

public:
bool isVariadic = false;

auto name() const -> std::string override;
Expand All @@ -35,11 +36,12 @@ struct FuncType : IType

struct MethodType : IType
{
private:
InnerType result;
InnerType classType;
std::vector<InnerType> parameters;


public:
auto name() const -> std::string override;
auto symbolName() const -> std::string override {
return "Method";
Expand Down
1 change: 1 addition & 0 deletions VM/include/RigCVM/VM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct Instance

auto parseModule(std::string_view name_) -> Module*;

auto getPathFromAlias(std::string_view alias_) const;
auto evaluateModule(Module& module_) -> void;
auto findModulePath(std::string_view name_) const -> fs::path;

Expand Down
24 changes: 24 additions & 0 deletions VM/src/Environment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "RigCVM/Environment.hpp"

namespace rigc::vm
{
fs::path getVmAppPath()
{
std::array<char, 4 * 1024> buf;

#ifdef PACC_SYSTEM_WINDOWS
size_t bytes;
#elif defined(PACC_SYSTEM_LINUX)
ssize_t bytes;
#endif

// Obtain the path in `buf` and length in `bytes`
#ifdef PACC_SYSTEM_WINDOWS
bytes = static_cast<std::size_t>( GetModuleFileNameA(nullptr, buf.data(), static_cast<DWORD>(buf.size()) ) );
#elif defined(PACC_SYSTEM_LINUX)
bytes = std::min(readlink("/proc/self/exe", buf.data(), buf.size()), ssize_t(buf.size() - 1));
#endif

return fs::path(std::string(buf.data(), bytes));
}
}
1 change: 0 additions & 1 deletion VM/src/Executors/All.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ auto evaluateExpression(Instance &vm_, rigc::ParserNode const& expr_) -> OptValu
return ExpressionExecutor{vm_, expr_}.evaluate();
}

//todo refactor evaluateSymbol and evaluateName
////////////////////////////////////////
auto evaluateSymbol(Instance &vm_, rigc::ParserNode const& expr_) -> OptValue
{
Expand Down
88 changes: 72 additions & 16 deletions VM/src/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

#include <RigCVM/VM.hpp>

#include "RigCVM/Environment.hpp"
#include <RigCVM/Executors/All.hpp>
#include <RigCVM/Value.hpp>
#include <RigCVM/TypeSystem/ClassTemplate.hpp>
#include <RigCVM/TypeSystem/ClassType.hpp>
#include <RigCVM/TypeSystem/RefType.hpp>
#include <RigCVM/TypeSystem/ArrayType.hpp>
#include <RigCVM/TypeSystem/FuncType.hpp>

#include <RigCVM/ErrorHandling/Exceptions.hpp>

namespace rigc::vm
Expand All @@ -32,31 +32,87 @@ DEFINE_BUILTIN_CONVERT_OP (double, Float64);

#undef DEFINE_BUILTIN_CONVERT_OP

//////////////////////////////////////////
auto Instance::findModulePath(std::string_view name_) const -> fs::path
auto getSeparatorPos(std::string_view name_)
{
auto relativeTo = fs::current_path();
auto path = fs::path(std::string(name_));
return name_.find(fs::path::preferred_separator);
}

if (currentModule && name_.starts_with("./") || name_.starts_with(".\\"))
auto getAlias(std::string_view name_)
{
auto separatorPos = getSeparatorPos(name_);
return std::string(name_.substr(0,separatorPos));
}

auto Instance::getPathFromAlias(std::string_view alias_) const
{
auto const relativeStdPath = fs::path("lib/std").make_preferred();
auto const stdPath = getVmAppPath().parent_path().parent_path() / relativeStdPath;

auto aliasesPaths = std::unordered_map<std::string,fs::path>{
{"root",fs::current_path()},
{"std",stdPath}
};
auto const separatorPos = getSeparatorPos(alias_);

if(separatorPos == std::string_view::npos)
{
relativeTo = currentModule->absolutePath.parent_path();
throw RigCError("Could not find module {} - only provided a directory alias", alias_)
.withHelp("Provide a module name, example: {}/Module",alias_)
.withLine(lastEvaluatedLine);
}
else
{
auto const alias = std::string(alias_.substr(0,separatorPos));
auto const mappedPath = aliasesPaths.find(alias);
if(mappedPath == aliasesPaths.end())
{
throw RigCError("Could not find alias {}",alias_)
.withHelp("Ensure the {} alias is defined within the configuration file", alias_)
.withLine(lastEvaluatedLine);
}
else
{
alias_.remove_prefix(alias.size() + 1);
return mappedPath->second;
}
}
}

path = relativeTo / path;

if (!path.has_extension())
auto modulePathExtenstionExists(fs::path modulePath)
{
if (!modulePath.has_extension())
{
path.replace_extension(".rigc");
if (!fs::exists(path))
modulePath.replace_extension(".rigc");
if (!fs::exists(modulePath))
{
path.replace_extension(".rigcz");
if (!fs::exists(path))
modulePath.replace_extension(".rigcz");
if (!fs::exists(modulePath))
return fs::path{};
}
}
return modulePath;
}

auto Instance::findModulePath(std::string_view name_) const -> fs::path
{
auto relativeTo = fs::current_path();
auto modulePath = fs::path(std::string(name_));
auto alias = getAlias(name_);

if (currentModule && name_.starts_with("./") || name_.starts_with(".\\"))
{
relativeTo = currentModule->absolutePath.parent_path();
}
if(name_.starts_with('@'))
{
relativeTo = getPathFromAlias(alias);
}

modulePath = relativeTo / modulePath;

return path;
modulePathExtenstionExists(modulePath);

return modulePath;
}

//////////////////////////////////////////
Expand Down Expand Up @@ -560,4 +616,4 @@ auto Instance::popStackFrame() -> void
stack.popFrame();
currentScope = stack.frames.back().scope;
}
}
}