Run
This commit is contained in:
7
lib/include/Overload.hpp
Normal file
7
lib/include/Overload.hpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef OVERLOAD_HPP_
|
||||
#define OVERLOAD_HPP_
|
||||
|
||||
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
||||
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
||||
|
||||
#endif
|
@@ -1,8 +1,10 @@
|
||||
#ifndef INTCODE_INTERPRETER_HPP_
|
||||
#define INTCODE_INTERPRETER_HPP_
|
||||
|
||||
#include "Machine.hpp"
|
||||
#include <variant>
|
||||
|
||||
#include <intcode/Machine.hpp>
|
||||
#include <Overload.hpp>
|
||||
namespace intcode {
|
||||
|
||||
struct Input {
|
||||
@@ -21,6 +23,21 @@ struct BadInstruction : public std::runtime_error {
|
||||
explicit BadInstruction(char const* what);
|
||||
};
|
||||
|
||||
template <class Fin, class Fout>
|
||||
auto Run(Machine & machine, Fin input, Fout output) -> void {
|
||||
while (std::visit(overloaded{
|
||||
[](Halt) { return false; },
|
||||
[&](Input arg) {
|
||||
arg.pos = input();
|
||||
return true;
|
||||
},
|
||||
[&](Output arg) {
|
||||
output(arg.val);
|
||||
return true;
|
||||
}},
|
||||
Step(machine)));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
|
@@ -1,11 +1,9 @@
|
||||
#ifndef INTCODE_HPP_
|
||||
#ifndef INTCODE_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...>;
|
||||
#include <Overload.hpp>
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user