diff --git a/components/interpreter/interpreter.cpp b/components/interpreter/interpreter.cpp index 6a51a2cddf..be6d7d1adf 100644 --- a/components/interpreter/interpreter.cpp +++ b/components/interpreter/interpreter.cpp @@ -1,6 +1,7 @@ #include "interpreter.hpp" #include +#include #include #include @@ -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 - 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 + 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) diff --git a/components/interpreter/interpreter.hpp b/components/interpreter/interpreter.hpp index 381cb5dd6b..6ad7a9dc96 100644 --- a/components/interpreter/interpreter.hpp +++ b/components/interpreter/interpreter.hpp @@ -4,13 +4,9 @@ #include #include #include -#include #include -#include - #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 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(std::forward(args)...)); }