Replace StringUtils::format in components/interpreter

This commit is contained in:
Evil Eye 2025-08-24 14:07:41 +02:00
parent a0d081adb9
commit 58a232d6c7
2 changed files with 31 additions and 24 deletions

View File

@ -1,6 +1,7 @@
#include "interpreter.hpp"
#include <cassert>
#include <format>
#include <stdexcept>
#include <string>
@ -9,27 +10,36 @@
namespace Interpreter
{
[[noreturn]] static void abortUnknownCode(int segment, int opcode)
namespace
{
const std::string error = "unknown opcode " + std::to_string(opcode) + " in segment " + std::to_string(segment);
throw std::runtime_error(error);
}
[[noreturn]] static void abortUnknownSegment(Type_Code code)
{
const std::string error = "opcode outside of the allocated segment range: " + std::to_string(code);
throw std::runtime_error(error);
}
template <typename T>
auto& getDispatcher(const T& segment, unsigned int seg, int opcode)
{
auto it = segment.find(opcode);
if (it == segment.end())
[[noreturn]] void abortUnknownCode(int segment, int opcode)
{
abortUnknownCode(seg, opcode);
const std::string error = std::format("unknown opcode {} in segment {}", opcode, segment);
throw std::runtime_error(error);
}
return it->second;
[[noreturn]] void abortUnknownSegment(Type_Code code)
{
const std::string error = std::format("opcode outside of the allocated segment range: {}", code);
throw std::runtime_error(error);
}
template <typename T>
auto& getDispatcher(const T& segment, unsigned int seg, int opcode)
{
auto it = segment.find(opcode);
if (it == segment.end())
{
abortUnknownCode(seg, opcode);
}
return it->second;
}
}
[[noreturn]] void Interpreter::abortDuplicateInstruction(std::string_view name, int code)
{
throw std::invalid_argument(
std::format("Duplicated interpreter instruction code in segment {}: {:#x}", name, code));
}
void Interpreter::execute(Type_Code code)

View File

@ -4,13 +4,9 @@
#include <map>
#include <memory>
#include <stack>
#include <stdexcept>
#include <utility>
#include <components/misc/strings/format.hpp>
#include "opcodes.hpp"
#include "program.hpp"
#include "runtime.hpp"
#include "types.hpp"
@ -34,12 +30,13 @@ namespace Interpreter
void end();
[[noreturn]] void abortDuplicateInstruction(std::string_view name, int code);
template <typename T, typename... Args>
void installSegment(auto& segment, std::string_view name, int code, Args&&... args)
{
if (segment.find(code) != segment.end())
throw std::invalid_argument(Misc::StringUtils::format(
"Duplicated interpreter instruction code in segment %s: 0x%x", name, code));
abortDuplicateInstruction(name, code);
segment.emplace(code, std::make_unique<T>(std::forward<Args>(args)...));
}