Add .clang-format and apply it

This commit is contained in:
2021-12-02 07:18:16 +00:00
parent e58dede1b6
commit cd5e2538df
103 changed files with 2714 additions and 2132 deletions

View File

@@ -9,36 +9,43 @@
enum class Opcode { Acc, Jmp, Nop };
using Value = int;
struct Instruction {
Instruction(std::string const &s)
: op_(to_opcode(s.substr(0, 3))), v_(to_value(s.substr(4))) {}
struct Instruction
{
Instruction(std::string const& s) : op_(to_opcode(s.substr(0, 3))), v_(to_value(s.substr(4))) {}
Opcode opcode() const noexcept { return op_; }
Value value() const noexcept { return v_; }
private:
Opcode to_opcode(std::string const &s) {
Opcode to_opcode(std::string const& s)
{
if (s == "acc") {
return Opcode::Acc;
} else if (s == "jmp") {
}
else if (s == "jmp") {
return Opcode::Jmp;
} else if (s == "nop") {
}
else if (s == "nop") {
return Opcode::Nop;
} else {
}
else {
assert(false);
return Opcode::Nop;
}
}
Value to_value(std::string const &s) {
Value to_value(std::string const& s)
{
int sign = 0;
int v = 0;
std::size_t pos = 0;
if (s[0] == '+') {
sign = 1;
} else if (s[0] == '-') {
}
else if (s[0] == '-') {
sign = -1;
} else {
}
else {
assert(false);
}
v = std::stoi(s.substr(1), &pos);
@@ -50,7 +57,8 @@ private:
Value v_;
};
std::ostream &operator<<(std::ostream &os, Instruction const &i) {
std::ostream& operator<<(std::ostream& os, Instruction const& i)
{
switch (i.opcode()) {
case Opcode::Acc:
os << "acc";
@@ -74,15 +82,18 @@ std::ostream &operator<<(std::ostream &os, Instruction const &i) {
}
using Instructions = std::vector<Instruction>;
struct VM {
struct VM
{
VM() : pc_(0), acc_(0) {}
void add_instruction(Instruction const &i) {
void add_instruction(Instruction const& i)
{
std::cout << i << "\n";
instrs_.push_back(i);
}
void execute() {
void execute()
{
std::vector<bool> seen(instrs_.size(), false);
while (!seen[pc_]) {
assert(pc_ < instrs_.size());
@@ -96,7 +107,8 @@ struct VM {
Value acc() const noexcept { return acc_; }
private:
void execute(Instruction const &i) {
void execute(Instruction const& i)
{
std::cout << pc_ << ": " << i;
switch (i.opcode()) {
case Opcode::Acc:
@@ -120,7 +132,8 @@ private:
Value acc_;
};
int main(int argc, char **argv) {
int main(int argc, char** argv)
{
VM vm;
for (std::string line; std::getline(std::cin, line);) {
vm.add_instruction(Instruction(line));