-
Notifications
You must be signed in to change notification settings - Fork 1
/
executable.cpp
207 lines (173 loc) · 5.74 KB
/
executable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "executable.hpp"
#include <unordered_map>
#include <functional>
#include "util.hpp"
#include <iostream>
Executable::Executable(const std::string &path) {
auto in = FileOpenIn(path);
std::vector<std::vector<std::string>> lines;
std::string line;
while (std::getline(in, line)) {
lines.resize(lines.size() + 1);
std::size_t i = 0;
std::size_t d;
do {
std::string s;
d = line.find(' ', i);
if (d == s.npos)
s = std::string(line, i);
else
s = std::string(line, i, d - i);
if (s.length())
lines.back().push_back(s);
i = d + 1;
} while (d != std::string::npos);
}
int n = 0;
std::vector<Word> jmp = {0};
for (auto &line : lines) {
std::unordered_map<std::string, Word> sizes = {
{"eat", 2},
{"go", 2},
{"clon", 1},
{"str", 2},
{"left", 1},
{"right", 1},
{"back", 1},
{"turn", 1},
{"jg", 3},
{"jl", 3},
{"j", 2},
{"je", 2}
};
try {
jmp.push_back(jmp.back() + sizes.at(line.front()));
} catch (const std::logic_error &) {
throw ExecutableError(path, n);
}
}
n = 0;
for (auto &insn : lines) {
auto parseOpN = [this, insn, path, n] {
switch (insn.size()) {
case 1:
bytecode.push_back(1);
break;
case 2:
try {
bytecode.push_back((Word)std::stoul(insn[1]));
break;
} catch (const std::invalid_argument &) {}
default:
throw ExecutableError(path, n);
}
};
auto parseOpR = [this, insn, path, n] {
if (insn.size() != 2 || insn[1] != "r")
throw ExecutableError(path, n);
};
auto parseOpNR = [this, insn, path, n] {
switch (insn.size()) {
case 1:
bytecode.push_back(1);
break;
case 2:
if (insn[1] == "r")
bytecode.push_back(0);
else {
try {
bytecode.push_back(static_cast<Word>(std::stoul(insn[1])));
} catch (const std::invalid_argument &) {
throw ExecutableError(path, n);
}
if (bytecode.back() < 2 || bytecode.back() > 99)
throw ExecutableError(path, n);
}
break;
default:
throw ExecutableError(path, n);
}
};
auto parseOpNN = [this, insn, path, n] {
try {
bytecode.push_back(static_cast<Word>(std::stoul(insn.at(1))));
bytecode.push_back(static_cast<Word>(std::stoul(insn.at(2))));
} catch (const std::logic_error &) {
throw ExecutableError(path, n);
}
};
auto parseOpNone = [this, insn, path, n] {
if (insn.size() != 1)
throw ExecutableError(path, n);
};
auto leaveJumpAddr = [this, jmp, path, n](Word pc) {
if (pc >= jmp.size())
throw ExecutableError(path, n);
bytecode.push_back(jmp[pc]);
};
auto parseOpNJ = [this, insn, path, n, leaveJumpAddr] {
try {
bytecode.push_back((Word)std::stoul(insn.at(1)));
leaveJumpAddr((Word)std::stoul(insn[2]));
} catch (const std::logic_error &) {
throw ExecutableError(path, n);
}
};
auto parseOpJ = [this, insn, path, n, leaveJumpAddr] {
try {
leaveJumpAddr((Word)std::stoul(insn.at(1)));
} catch (const std::logic_error &) {
throw ExecutableError(path, n);
}
};
std::unordered_map<std::string, std::function<void()>> handle = {
#define INSN(n, i, o) \
{#n, [this, parseOp ## o] {\
bytecode.push_back(Insn ## i);\
parseOp ## o();\
}}
INSN(eat, Eat, NR),
INSN(go, Go, NR),
INSN(clon, Clon, None),
INSN(str, Str, N),
INSN(left, Left, None),
INSN(right, Right, None),
INSN(back, Back, None),
INSN(turn, Turn, R),
INSN(jg, JG, NJ),
INSN(jl, JL, NJ),
INSN(j, J, J),
INSN(je, JE, J)
#undef INSN
};
try {
handle.at(insn[0])();
} catch (const std::out_of_range &) {
throw ExecutableError(path, n);
}
n++;
}
}
ExecutableError::ExecutableError(const std::string &path, int line) {
reason = path + ":" + std::to_string(line) + ": Syntax error.";
}
const std::string &DisasmOpcode(Executable::Word opcode) {
static std::string mnemonics[] = {
"eat",
"go",
"clon",
"str",
"left",
"right",
"back",
"turn",
"jg",
"jl",
"j",
"je"
};
static std::string invalid = "(invalid)";
if (opcode > Executable::InsnMax)
return invalid;
return mnemonics[opcode];
}