2010-06-28 17:20:45 +00:00
|
|
|
#ifndef INTERPRETER_INTERPRETER_H_INCLUDED
|
|
|
|
#define INTERPRETER_INTERPRETER_H_INCLUDED
|
|
|
|
|
2022-01-27 19:18:57 +00:00
|
|
|
#include <cassert>
|
2010-06-28 18:46:15 +00:00
|
|
|
#include <map>
|
2022-01-27 19:18:57 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <stack>
|
|
|
|
#include <utility>
|
2010-06-28 18:46:15 +00:00
|
|
|
|
2023-01-10 03:10:18 +00:00
|
|
|
#include "components/interpreter/program.hpp"
|
2022-01-27 19:18:57 +00:00
|
|
|
#include "opcodes.hpp"
|
2010-06-28 17:20:45 +00:00
|
|
|
#include "runtime.hpp"
|
|
|
|
#include "types.hpp"
|
|
|
|
|
|
|
|
namespace Interpreter
|
|
|
|
{
|
2023-01-10 03:10:18 +00:00
|
|
|
struct Program;
|
|
|
|
|
2010-06-28 17:20:45 +00:00
|
|
|
class Interpreter
|
|
|
|
{
|
2015-10-09 10:14:22 +00:00
|
|
|
std::stack<Runtime> mCallstack;
|
2023-01-10 03:05:53 +00:00
|
|
|
bool mRunning = false;
|
2010-06-28 17:20:45 +00:00
|
|
|
Runtime mRuntime;
|
2022-01-27 19:18:57 +00:00
|
|
|
std::map<int, std::unique_ptr<Opcode1>> mSegment0;
|
|
|
|
std::map<int, std::unique_ptr<Opcode1>> mSegment2;
|
|
|
|
std::map<int, std::unique_ptr<Opcode1>> mSegment3;
|
|
|
|
std::map<int, std::unique_ptr<Opcode0>> mSegment5;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2010-06-28 18:46:15 +00:00
|
|
|
void execute(Type_Code code);
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2015-10-09 10:14:22 +00:00
|
|
|
void begin();
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2015-10-09 10:14:22 +00:00
|
|
|
void end();
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2022-01-27 19:18:57 +00:00
|
|
|
template <typename TSeg, typename TOp>
|
|
|
|
void installSegment(TSeg& seg, int code, TOp&& op)
|
|
|
|
{
|
|
|
|
assert(seg.find(code) == seg.end());
|
|
|
|
seg.emplace(code, std::move(op));
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2010-06-28 17:20:45 +00:00
|
|
|
public:
|
2023-01-10 03:05:53 +00:00
|
|
|
Interpreter() = default;
|
|
|
|
|
|
|
|
Interpreter(const Interpreter&) = delete;
|
|
|
|
Interpreter& operator=(const Interpreter&) = delete;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2022-01-27 19:18:57 +00:00
|
|
|
template <typename T, typename... TArgs>
|
|
|
|
void installSegment0(int code, TArgs&&... args)
|
|
|
|
{
|
|
|
|
installSegment(mSegment0, code, std::make_unique<T>(std::forward<TArgs>(args)...));
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2022-01-27 19:18:57 +00:00
|
|
|
template <typename T, typename... TArgs>
|
|
|
|
void installSegment2(int code, TArgs&&... args)
|
|
|
|
{
|
|
|
|
installSegment(mSegment2, code, std::make_unique<T>(std::forward<TArgs>(args)...));
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2022-01-27 19:18:57 +00:00
|
|
|
template <typename T, typename... TArgs>
|
|
|
|
void installSegment3(int code, TArgs&&... args)
|
|
|
|
{
|
|
|
|
installSegment(mSegment3, code, std::make_unique<T>(std::forward<TArgs>(args)...));
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2022-01-27 19:18:57 +00:00
|
|
|
template <typename T, typename... TArgs>
|
|
|
|
void installSegment5(int code, TArgs&&... args)
|
|
|
|
{
|
|
|
|
installSegment(mSegment5, code, std::make_unique<T>(std::forward<TArgs>(args)...));
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2023-01-10 03:10:18 +00:00
|
|
|
void run(const Program& program, Context& context);
|
2010-06-28 17:20:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|