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

@ -1,7 +1,7 @@
#include <iostream>
#include <utility>
#include <intcode.hpp>
#include <intcode/intcode.hpp>
using namespace intcode;

View File

@ -1,7 +1,7 @@
#include <iostream>
#include <utility>
#include <intcode.hpp>
#include <intcode/intcode.hpp>
using namespace intcode;
namespace {

View File

@ -5,7 +5,7 @@
#include <utility>
#include <vector>
#include <intcode.hpp>
#include <intcode/intcode.hpp>
using namespace intcode;
namespace {

View File

@ -1,7 +1,7 @@
#include <iostream>
#include <utility>
#include <intcode.hpp>
#include <intcode/intcode.hpp>
using namespace intcode;
namespace {

View File

@ -3,7 +3,7 @@
#include <tuple>
#include <map>
#include <intcode.hpp>
#include <intcode/intcode.hpp>
using namespace intcode;
namespace {

View File

@ -3,7 +3,7 @@
#include <tuple>
#include <set>
#include <intcode.hpp>
#include <intcode/intcode.hpp>
using namespace intcode;
namespace {

View File

@ -6,7 +6,7 @@
#include <deque>
#include <vector>
#include <intcode.hpp>
#include <intcode/intcode.hpp>
using namespace intcode;
namespace {

View File

@ -1,2 +1,2 @@
add_library(intcode intcode.cpp)
add_library(intcode src/Parser.cpp src/Interpreter.cpp src/Machine.cpp)
target_include_directories(intcode PUBLIC include)

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

@ -1,31 +1,14 @@
#ifndef INTCODE_HPP_
#define INTCODE_HPP_
#ifndef INTCODE_MACHINE_HPP_
#define INTCODE_MACHINE_HPP_
#include <cstddef>
#include <cstdint>
#include <istream>
#include <unordered_map>
#include <stdexcept>
#include <variant>
#include <vector>
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
namespace intcode {
using ValueType = std::int64_t;
struct Input {
ValueType &pos;
};
struct Output {
ValueType val;
};
struct Halt {};
class Machine {
std::vector<ValueType> rom_;
std::unordered_map<std::size_t, ValueType> ram_;
@ -51,14 +34,6 @@ public:
auto Rebase(std::size_t offset) -> void;
};
auto Step(Machine & m) -> std::variant<Input, Output, Halt>;
} // namespace
struct BadInstruction : public std::runtime_error {
explicit BadInstruction(char const* what);
};
auto ParseStream(std::istream &in) -> std::vector<ValueType>;
}
#endif // INTCODE_HPP_
#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

View File

@ -1,37 +1,10 @@
#include "intcode.hpp"
#include <iostream>
#include <intcode/Interpreter.hpp>
namespace intcode {
BadInstruction::BadInstruction(char const* what)
: std::runtime_error{what} {}
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
Machine::Machine(std::vector<ValueType> ram)
: rom_{ram}, ram_{}, pc_{0}, base_{0} {}
auto Machine::At(std::size_t i) -> ValueType & {
return i < rom_.size() ? rom_[i] : ram_[i];
}
auto Machine::Rel(std::size_t i) -> ValueType & {
return At(base_ + i);
}
auto Machine::Rebase(std::size_t offset) -> void {
base_ += offset;
}
auto Machine::Next() -> ValueType & {
return At(pc_++);
}
auto Machine::Goto(std::size_t address) -> void {
pc_ = address;
}
auto Step(Machine & m) -> std::variant<Input, Output, Halt> {
for (;;) {
auto instruction = m.Next();
@ -119,14 +92,4 @@ auto Step(Machine & m) -> std::variant<Input, Output, Halt> {
}
}
auto ParseStream(std::istream &in) -> std::vector<ValueType> {
ValueType x;
std::string str;
std::vector<ValueType> result;
while (std::getline(in, str, ',')) {
result.push_back(std::stol(str));
}
return result;
}
}
} // namespace

30
lib/src/Machine.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <intcode/Machine.hpp>
namespace intcode {
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
Machine::Machine(std::vector<ValueType> ram)
: rom_{ram}, ram_{}, pc_{0}, base_{0} {}
auto Machine::At(std::size_t i) -> ValueType & {
return i < rom_.size() ? rom_[i] : ram_[i];
}
auto Machine::Rel(std::size_t i) -> ValueType & {
return At(base_ + i);
}
auto Machine::Rebase(std::size_t offset) -> void {
base_ += offset;
}
auto Machine::Next() -> ValueType & {
return At(pc_++);
}
auto Machine::Goto(std::size_t address) -> void {
pc_ = address;
}
} // namespace

17
lib/src/Parser.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <intcode/Parser.hpp>
#include <iostream>
namespace intcode {
auto ParseStream(std::istream &in) -> std::vector<ValueType> {
ValueType x;
std::string str;
std::vector<ValueType> result;
while (std::getline(in, str, ',')) {
result.push_back(std::stol(str));
}
return result;
}
} // namespace