-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
131 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#pragma once | ||
#include <functional> | ||
#include <optional> | ||
#include <unordered_map> | ||
#include <span> | ||
|
||
#include "value.hpp" | ||
|
||
/// @brief The namespace for configuring the Rift interpreter | ||
namespace rift::config { | ||
|
||
using RuntimeFunc = std::function<Value(std::span<Value>)>; // Function signature for runtime functions | ||
using RuntimeFuncMap = std::unordered_map<std::string, RuntimeFunc>; // Map of runtime functions | ||
|
||
/// @brief Get the map of runtime functions | ||
const RuntimeFuncMap& getRuntimeFunctions(); | ||
|
||
/// @brief Add a runtime function to the map | ||
void addRuntimeFunction(const std::string& name, const RuntimeFunc& func); | ||
|
||
} | ||
|
||
namespace rift { | ||
/// @brief Get an argument from a span of values | ||
template <typename T> | ||
std::optional<T> getArgument(std::span<Value> args, size_t index) { | ||
if (args.size() <= index) { | ||
return std::nullopt; | ||
} | ||
|
||
if constexpr (std::is_same_v<T, std::string>) { | ||
if (!args[index].isString()) { | ||
return std::nullopt; | ||
} | ||
return args[index].toString(); | ||
} else if constexpr (std::is_same_v<T, int>) { | ||
if (args[index].isInteger()) { | ||
return args[index].getInteger(); | ||
} | ||
if (args[index].isFloat()) { | ||
return static_cast<int>(args[index].getFloat()); | ||
} | ||
return std::nullopt; | ||
} else if constexpr (std::is_same_v<T, float>) { | ||
if (args[index].isFloat()) { | ||
return args[index].getFloat(); | ||
} | ||
if (args[index].isInteger()) { | ||
return static_cast<float>(args[index].getInteger()); | ||
} | ||
return std::nullopt; | ||
} else if constexpr (std::is_same_v<T, bool>) { | ||
if (!args[index].isBoolean()) { | ||
return std::nullopt; | ||
} | ||
return args[index].getBoolean(); | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <rift/config.hpp> | ||
|
||
namespace rift::config { | ||
|
||
static RuntimeFuncMap runtimeFunctions; | ||
|
||
const RuntimeFuncMap& getRuntimeFunctions() { | ||
return runtimeFunctions; | ||
} | ||
|
||
void addRuntimeFunction(const std::string& name, const RuntimeFunc& func) { | ||
runtimeFunctions[name] = func; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters