rearrange

This commit is contained in:
Eric Mertens
2022-11-04 09:38:01 -07:00
parent fd39209ff0
commit e98a71dad6
15 changed files with 115 additions and 78 deletions

View File

@@ -0,0 +1,26 @@
#ifndef INTCODE_INTERPRETER_HPP_
#define INTCODE_INTERPRETER_HPP_
#include "Machine.hpp"
namespace intcode {
struct Input {
ValueType &pos;
};
struct Output {
ValueType val;
};
struct Halt {};
auto Step(Machine & m) -> std::variant<Input, Output, Halt>;
struct BadInstruction : public std::runtime_error {
explicit BadInstruction(char const* what);
};
} // namespace
#endif

View File

@@ -0,0 +1,39 @@
#ifndef INTCODE_MACHINE_HPP_
#define INTCODE_MACHINE_HPP_
#include <cstddef>
#include <unordered_map>
#include <vector>
namespace intcode {
using ValueType = std::int64_t;
class Machine {
std::vector<ValueType> rom_;
std::unordered_map<std::size_t, ValueType> ram_;
std::size_t pc_;
std::size_t base_;
public:
Machine();
explicit Machine(std::vector<ValueType> ram);
/// Access memory at absolute address
/// @param address
/// @return reference to memory at given address
auto At(std::size_t address) -> ValueType &;
/// Access memory at address relative to base pointer
/// @param offset from base pointer
/// @return reference to memory at given offset
auto Rel(std::size_t offset) -> ValueType &;
auto Next() -> ValueType &;
auto Goto(std::size_t address) -> void;
auto Rebase(std::size_t offset) -> void;
};
} // namespace
#endif

View File

@@ -0,0 +1,15 @@
#ifndef INTCODE_PARSER_HPP_
#define INTCODE_PARSER_HPP_
#include <istream>
#include <vector>
#include "Machine.hpp"
namespace intcode {
auto ParseStream(std::istream &in) -> std::vector<ValueType>;
} // namespace
#endif

View File

@@ -0,0 +1,11 @@
#ifndef INTCODE_HPP_
#define INTCODE_INTCODE_HPP_
#include <intcode/Machine.hpp>
#include <intcode/Parser.hpp>
#include <intcode/Interpreter.hpp>
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
#endif